Blame


1 2178c42e 2018-04-22 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
4 2178c42e 2018-04-22 stsp *
5 2178c42e 2018-04-22 stsp * Permission to use, copy, modify, and distribute this software for any
6 2178c42e 2018-04-22 stsp * purpose with or without fee is hereby granted, provided that the above
7 2178c42e 2018-04-22 stsp * copyright notice and this permission notice appear in all copies.
8 2178c42e 2018-04-22 stsp *
9 2178c42e 2018-04-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 2178c42e 2018-04-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 2178c42e 2018-04-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 2178c42e 2018-04-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 2178c42e 2018-04-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 2178c42e 2018-04-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 2178c42e 2018-04-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 2178c42e 2018-04-22 stsp */
17 2178c42e 2018-04-22 stsp
18 2178c42e 2018-04-22 stsp #include <sys/types.h>
19 2178c42e 2018-04-22 stsp #include <sys/queue.h>
20 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
22 876c234b 2018-09-10 stsp #include <sys/wait.h>
23 2178c42e 2018-04-22 stsp
24 531c3985 2020-03-18 stsp #include <ctype.h>
25 2178c42e 2018-04-22 stsp #include <stdio.h>
26 2178c42e 2018-04-22 stsp #include <stdlib.h>
27 2178c42e 2018-04-22 stsp #include <string.h>
28 2178c42e 2018-04-22 stsp #include <errno.h>
29 2178c42e 2018-04-22 stsp #include <stdint.h>
30 2178c42e 2018-04-22 stsp #include <poll.h>
31 2178c42e 2018-04-22 stsp #include <imsg.h>
32 2178c42e 2018-04-22 stsp #include <sha1.h>
33 2178c42e 2018-04-22 stsp #include <zlib.h>
34 788c352e 2018-06-16 stsp #include <time.h>
35 2178c42e 2018-04-22 stsp
36 2178c42e 2018-04-22 stsp #include "got_object.h"
37 2178c42e 2018-04-22 stsp #include "got_error.h"
38 3022d272 2019-11-14 stsp #include "got_path.h"
39 cd95becd 2019-11-29 stsp #include "got_repository.h"
40 2178c42e 2018-04-22 stsp
41 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
42 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
43 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
44 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
45 a440fac0 2018-09-06 stsp #include "got_lib_object_parse.h"
46 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
47 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
48 2178c42e 2018-04-22 stsp
49 2178c42e 2018-04-22 stsp #ifndef MIN
50 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 c39c25dd 2019-08-09 stsp #endif
52 c39c25dd 2019-08-09 stsp
53 c39c25dd 2019-08-09 stsp #ifndef nitems
54 c39c25dd 2019-08-09 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 2178c42e 2018-04-22 stsp #endif
56 2178c42e 2018-04-22 stsp
57 2178c42e 2018-04-22 stsp static const struct got_error *
58 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
59 2178c42e 2018-04-22 stsp {
60 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
61 2178c42e 2018-04-22 stsp int n;
62 2178c42e 2018-04-22 stsp
63 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
64 2178c42e 2018-04-22 stsp pfd[0].events = events;
65 2178c42e 2018-04-22 stsp
66 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
67 2178c42e 2018-04-22 stsp if (n == -1)
68 638f9024 2019-05-13 stsp return got_error_from_errno("poll");
69 2178c42e 2018-04-22 stsp if (n == 0)
70 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
71 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
72 638f9024 2019-05-13 stsp return got_error_from_errno("poll error");
73 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
74 2178c42e 2018-04-22 stsp return NULL;
75 2178c42e 2018-04-22 stsp
76 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
77 2178c42e 2018-04-22 stsp }
78 2178c42e 2018-04-22 stsp
79 c4eae628 2018-04-23 stsp static const struct got_error *
80 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
81 fe36cf76 2018-04-23 stsp {
82 fe36cf76 2018-04-23 stsp const struct got_error *err;
83 e033d803 2018-04-23 stsp size_t n;
84 fe36cf76 2018-04-23 stsp
85 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
86 fe36cf76 2018-04-23 stsp if (err)
87 fe36cf76 2018-04-23 stsp return err;
88 fe36cf76 2018-04-23 stsp
89 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
90 fe36cf76 2018-04-23 stsp if (n == -1) {
91 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
92 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
93 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
94 fe36cf76 2018-04-23 stsp }
95 fe36cf76 2018-04-23 stsp if (n == 0)
96 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
97 fe36cf76 2018-04-23 stsp
98 e033d803 2018-04-23 stsp return NULL;
99 e033d803 2018-04-23 stsp }
100 e033d803 2018-04-23 stsp
101 ad242220 2018-09-08 stsp const struct got_error *
102 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
103 876c234b 2018-09-10 stsp {
104 876c234b 2018-09-10 stsp int child_status;
105 876c234b 2018-09-10 stsp
106 2cb49fa8 2019-05-10 stsp if (waitpid(pid, &child_status, 0) == -1)
107 638f9024 2019-05-13 stsp return got_error_from_errno("waitpid");
108 876c234b 2018-09-10 stsp
109 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
110 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
111 876c234b 2018-09-10 stsp
112 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
113 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
114 876c234b 2018-09-10 stsp
115 876c234b 2018-09-10 stsp return NULL;
116 876c234b 2018-09-10 stsp }
117 876c234b 2018-09-10 stsp
118 73b7854a 2018-11-11 stsp static const struct got_error *
119 73b7854a 2018-11-11 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
120 73b7854a 2018-11-11 stsp {
121 73b7854a 2018-11-11 stsp struct got_imsg_error *ierr;
122 73b7854a 2018-11-11 stsp
123 73b7854a 2018-11-11 stsp if (datalen != sizeof(*ierr))
124 73b7854a 2018-11-11 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
125 73b7854a 2018-11-11 stsp
126 73b7854a 2018-11-11 stsp ierr = imsg->data;
127 73b7854a 2018-11-11 stsp if (ierr->code == GOT_ERR_ERRNO) {
128 73b7854a 2018-11-11 stsp static struct got_error serr;
129 73b7854a 2018-11-11 stsp serr.code = GOT_ERR_ERRNO;
130 73b7854a 2018-11-11 stsp serr.msg = strerror(ierr->errno_code);
131 73b7854a 2018-11-11 stsp return &serr;
132 73b7854a 2018-11-11 stsp }
133 73b7854a 2018-11-11 stsp
134 73b7854a 2018-11-11 stsp return got_error(ierr->code);
135 73b7854a 2018-11-11 stsp }
136 73b7854a 2018-11-11 stsp
137 876c234b 2018-09-10 stsp const struct got_error *
138 46de5bfd 2018-11-11 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
139 46de5bfd 2018-11-11 stsp size_t min_datalen)
140 e033d803 2018-04-23 stsp {
141 e033d803 2018-04-23 stsp const struct got_error *err;
142 e033d803 2018-04-23 stsp ssize_t n;
143 e033d803 2018-04-23 stsp
144 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
145 876c234b 2018-09-10 stsp if (n == -1)
146 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_get");
147 876c234b 2018-09-10 stsp
148 876c234b 2018-09-10 stsp while (n == 0) {
149 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
150 876c234b 2018-09-10 stsp if (err)
151 876c234b 2018-09-10 stsp return err;
152 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
153 cbfaaf20 2020-01-06 stsp if (n == -1)
154 cbfaaf20 2020-01-06 stsp return got_error_from_errno("imsg_get");
155 876c234b 2018-09-10 stsp }
156 fe36cf76 2018-04-23 stsp
157 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
158 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
159 c4eae628 2018-04-23 stsp
160 73b7854a 2018-11-11 stsp if (imsg->hdr.type == GOT_IMSG_ERROR) {
161 73b7854a 2018-11-11 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
162 73b7854a 2018-11-11 stsp return recv_imsg_error(imsg, datalen);
163 c4eae628 2018-04-23 stsp }
164 c4eae628 2018-04-23 stsp
165 73b7854a 2018-11-11 stsp return NULL;
166 c4eae628 2018-04-23 stsp }
167 c4eae628 2018-04-23 stsp
168 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
169 2178c42e 2018-04-22 stsp void
170 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
171 2178c42e 2018-04-22 stsp {
172 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
173 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
174 2178c42e 2018-04-22 stsp int ret;
175 2178c42e 2018-04-22 stsp
176 2178c42e 2018-04-22 stsp ierr.code = err->code;
177 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
178 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
179 2178c42e 2018-04-22 stsp else
180 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
181 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
182 e93cd828 2018-11-11 stsp if (ret == -1) {
183 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
184 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
185 5d43e84d 2018-04-23 stsp return;
186 2178c42e 2018-04-22 stsp }
187 2178c42e 2018-04-22 stsp
188 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
189 5d43e84d 2018-04-23 stsp if (poll_err) {
190 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
191 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
192 5d43e84d 2018-04-23 stsp return;
193 5d43e84d 2018-04-23 stsp }
194 2178c42e 2018-04-22 stsp
195 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
196 5d43e84d 2018-04-23 stsp if (ret == -1) {
197 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
198 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
199 5d43e84d 2018-04-23 stsp return;
200 5d43e84d 2018-04-23 stsp }
201 e033d803 2018-04-23 stsp }
202 e033d803 2018-04-23 stsp
203 e033d803 2018-04-23 stsp static const struct got_error *
204 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
205 e033d803 2018-04-23 stsp {
206 e033d803 2018-04-23 stsp const struct got_error *err;
207 e033d803 2018-04-23 stsp
208 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
209 e033d803 2018-04-23 stsp if (err)
210 e033d803 2018-04-23 stsp return err;
211 e033d803 2018-04-23 stsp
212 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
213 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_flush");
214 e033d803 2018-04-23 stsp
215 e033d803 2018-04-23 stsp return NULL;
216 ad242220 2018-09-08 stsp }
217 ad242220 2018-09-08 stsp
218 ad242220 2018-09-08 stsp const struct got_error *
219 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
220 ad242220 2018-09-08 stsp {
221 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
222 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
223 ad242220 2018-09-08 stsp
224 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
225 ad242220 2018-09-08 stsp
226 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
227 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose STOP");
228 ad242220 2018-09-08 stsp
229 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
230 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
231 ad242220 2018-09-08 stsp return err;
232 7762fe12 2018-11-05 stsp }
233 93658fb9 2020-03-18 stsp
234 93658fb9 2020-03-18 stsp const struct got_error *
235 aea5f015 2018-12-24 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
236 ad242220 2018-09-08 stsp {
237 aea5f015 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
238 aea5f015 2018-12-24 stsp == -1)
239 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT_REQUEST");
240 1785f84a 2018-12-23 stsp
241 1785f84a 2018-12-23 stsp return flush_imsg(ibuf);
242 1785f84a 2018-12-23 stsp }
243 1785f84a 2018-12-23 stsp
244 1785f84a 2018-12-23 stsp const struct got_error *
245 1785f84a 2018-12-23 stsp got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
246 1785f84a 2018-12-23 stsp struct got_object_id *id, int pack_idx)
247 1785f84a 2018-12-23 stsp {
248 41496140 2019-02-21 stsp const struct got_error *err = NULL;
249 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj, *iobjp;
250 1785f84a 2018-12-23 stsp size_t len;
251 1785f84a 2018-12-23 stsp
252 1785f84a 2018-12-23 stsp if (id) { /* commit is packed */
253 1785f84a 2018-12-23 stsp iobj.idx = pack_idx;
254 1785f84a 2018-12-23 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
255 1785f84a 2018-12-23 stsp iobjp = &iobj;
256 1785f84a 2018-12-23 stsp len = sizeof(iobj);
257 1785f84a 2018-12-23 stsp } else {
258 1785f84a 2018-12-23 stsp iobjp = NULL;
259 1785f84a 2018-12-23 stsp len = 0;
260 1785f84a 2018-12-23 stsp }
261 1785f84a 2018-12-23 stsp
262 1785f84a 2018-12-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
263 41496140 2019-02-21 stsp == -1) {
264 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
265 41496140 2019-02-21 stsp close(fd);
266 41496140 2019-02-21 stsp return err;
267 41496140 2019-02-21 stsp }
268 13c729f7 2018-12-24 stsp
269 13c729f7 2018-12-24 stsp return flush_imsg(ibuf);
270 13c729f7 2018-12-24 stsp }
271 13c729f7 2018-12-24 stsp
272 13c729f7 2018-12-24 stsp const struct got_error *
273 13c729f7 2018-12-24 stsp got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
274 13c729f7 2018-12-24 stsp struct got_object_id *id, int pack_idx)
275 13c729f7 2018-12-24 stsp {
276 41496140 2019-02-21 stsp const struct got_error *err = NULL;
277 7f358e3b 2019-11-23 stsp struct ibuf *wbuf;
278 7f358e3b 2019-11-23 stsp size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
279 7f358e3b 2019-11-23 stsp
280 7f358e3b 2019-11-23 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
281 7f358e3b 2019-11-23 stsp if (wbuf == NULL)
282 7f358e3b 2019-11-23 stsp return got_error_from_errno("imsg_create TREE_REQUEST");
283 13c729f7 2018-12-24 stsp
284 13c729f7 2018-12-24 stsp if (id) { /* tree is packed */
285 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
286 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
287 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
288 7f358e3b 2019-11-23 stsp return err;
289 7f358e3b 2019-11-23 stsp }
290 13c729f7 2018-12-24 stsp
291 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
292 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
293 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
294 7f358e3b 2019-11-23 stsp return err;
295 7f358e3b 2019-11-23 stsp }
296 41496140 2019-02-21 stsp }
297 268f7291 2018-12-24 stsp
298 7f358e3b 2019-11-23 stsp wbuf->fd = fd;
299 7f358e3b 2019-11-23 stsp imsg_close(ibuf, wbuf);
300 7f358e3b 2019-11-23 stsp
301 268f7291 2018-12-24 stsp return flush_imsg(ibuf);
302 268f7291 2018-12-24 stsp }
303 268f7291 2018-12-24 stsp
304 268f7291 2018-12-24 stsp const struct got_error *
305 268f7291 2018-12-24 stsp got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
306 268f7291 2018-12-24 stsp struct got_object_id *id, int pack_idx)
307 268f7291 2018-12-24 stsp {
308 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
309 268f7291 2018-12-24 stsp size_t len;
310 268f7291 2018-12-24 stsp
311 268f7291 2018-12-24 stsp if (id) { /* tag is packed */
312 268f7291 2018-12-24 stsp iobj.idx = pack_idx;
313 268f7291 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
314 268f7291 2018-12-24 stsp iobjp = &iobj;
315 268f7291 2018-12-24 stsp len = sizeof(iobj);
316 268f7291 2018-12-24 stsp } else {
317 268f7291 2018-12-24 stsp iobjp = NULL;
318 268f7291 2018-12-24 stsp len = 0;
319 268f7291 2018-12-24 stsp }
320 268f7291 2018-12-24 stsp
321 268f7291 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
322 1785f84a 2018-12-23 stsp == -1)
323 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TAG_REQUEST");
324 7762fe12 2018-11-05 stsp
325 7762fe12 2018-11-05 stsp return flush_imsg(ibuf);
326 7762fe12 2018-11-05 stsp }
327 7762fe12 2018-11-05 stsp
328 7762fe12 2018-11-05 stsp const struct got_error *
329 ebc55e2d 2018-12-24 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
330 ebc55e2d 2018-12-24 stsp struct got_object_id *id, int pack_idx)
331 55da3778 2018-09-10 stsp {
332 41496140 2019-02-21 stsp const struct got_error *err = NULL;
333 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
334 ebc55e2d 2018-12-24 stsp size_t len;
335 ebc55e2d 2018-12-24 stsp
336 ebc55e2d 2018-12-24 stsp if (id) { /* blob is packed */
337 ebc55e2d 2018-12-24 stsp iobj.idx = pack_idx;
338 ebc55e2d 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
339 ebc55e2d 2018-12-24 stsp iobjp = &iobj;
340 ebc55e2d 2018-12-24 stsp len = sizeof(iobj);
341 ebc55e2d 2018-12-24 stsp } else {
342 ebc55e2d 2018-12-24 stsp iobjp = NULL;
343 ebc55e2d 2018-12-24 stsp len = 0;
344 ebc55e2d 2018-12-24 stsp }
345 ebc55e2d 2018-12-24 stsp
346 ebc55e2d 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
347 41496140 2019-02-21 stsp == -1) {
348 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_REQUEST");
349 41496140 2019-02-21 stsp close(infd);
350 41496140 2019-02-21 stsp return err;
351 41496140 2019-02-21 stsp }
352 ad242220 2018-09-08 stsp
353 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
354 55da3778 2018-09-10 stsp }
355 ad242220 2018-09-08 stsp
356 55da3778 2018-09-10 stsp const struct got_error *
357 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
358 55da3778 2018-09-10 stsp {
359 41496140 2019-02-21 stsp const struct got_error *err = NULL;
360 41496140 2019-02-21 stsp
361 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
362 41496140 2019-02-21 stsp == -1) {
363 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_OUTFD");
364 41496140 2019-02-21 stsp close(outfd);
365 41496140 2019-02-21 stsp return err;
366 41496140 2019-02-21 stsp }
367 3840f4c9 2018-09-12 stsp
368 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
369 3840f4c9 2018-09-12 stsp }
370 3840f4c9 2018-09-12 stsp
371 73ab1060 2020-03-18 stsp static const struct got_error *
372 73ab1060 2020-03-18 stsp send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
373 3840f4c9 2018-09-12 stsp {
374 41496140 2019-02-21 stsp const struct got_error *err = NULL;
375 41496140 2019-02-21 stsp
376 73ab1060 2020-03-18 stsp if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
377 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TMPFD");
378 41496140 2019-02-21 stsp close(fd);
379 41496140 2019-02-21 stsp return err;
380 41496140 2019-02-21 stsp }
381 ad242220 2018-09-08 stsp
382 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
383 ad242220 2018-09-08 stsp }
384 ad242220 2018-09-08 stsp
385 ad242220 2018-09-08 stsp const struct got_error *
386 73ab1060 2020-03-18 stsp got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
387 73ab1060 2020-03-18 stsp {
388 73ab1060 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
389 73ab1060 2020-03-18 stsp }
390 73ab1060 2020-03-18 stsp
391 73ab1060 2020-03-18 stsp const struct got_error *
392 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
393 2178c42e 2018-04-22 stsp {
394 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
395 2178c42e 2018-04-22 stsp
396 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
397 2178c42e 2018-04-22 stsp iobj.type = obj->type;
398 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
399 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
400 2178c42e 2018-04-22 stsp iobj.size = obj->size;
401 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
402 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
403 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
404 c59b3346 2018-09-11 stsp }
405 2178c42e 2018-04-22 stsp
406 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
407 2178c42e 2018-04-22 stsp == -1)
408 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT");
409 2178c42e 2018-04-22 stsp
410 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
411 cfd633c2 2018-09-10 stsp }
412 cfd633c2 2018-09-10 stsp
413 cfd633c2 2018-09-10 stsp const struct got_error *
414 33501562 2020-03-18 stsp got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
415 4ba14133 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
416 2690194b 2020-03-21 stsp struct got_pathlist_head *wanted_branches, int list_refs_only,
417 2690194b 2020-03-21 stsp int verbosity)
418 93658fb9 2020-03-18 stsp {
419 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
420 33501562 2020-03-18 stsp struct ibuf *wbuf;
421 4ba14133 2020-03-20 stsp size_t len;
422 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
423 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetchreq;
424 93658fb9 2020-03-18 stsp
425 4ba14133 2020-03-20 stsp memset(&fetchreq, 0, sizeof(fetchreq));
426 4ba14133 2020-03-20 stsp fetchreq.fetch_all_branches = fetch_all_branches;
427 41b0de12 2020-03-21 stsp fetchreq.list_refs_only = list_refs_only;
428 2690194b 2020-03-21 stsp fetchreq.verbosity = verbosity;
429 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, have_refs, entry)
430 4ba14133 2020-03-20 stsp fetchreq.n_have_refs++;
431 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry)
432 4ba14133 2020-03-20 stsp fetchreq.n_wanted_branches++;
433 659e7fbd 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_request);
434 33501562 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
435 33501562 2020-03-18 stsp close(fd);
436 33501562 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
437 33501562 2020-03-18 stsp }
438 33501562 2020-03-18 stsp
439 4ba14133 2020-03-20 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
440 4ba14133 2020-03-20 stsp &fetchreq, sizeof(fetchreq)) == -1)
441 4ba14133 2020-03-20 stsp return got_error_from_errno(
442 4ba14133 2020-03-20 stsp "imsg_compose FETCH_SERVER_PROGRESS");
443 33501562 2020-03-18 stsp
444 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
445 4ba14133 2020-03-20 stsp if (err) {
446 659e7fbd 2020-03-20 stsp close(fd);
447 659e7fbd 2020-03-20 stsp return err;
448 659e7fbd 2020-03-20 stsp }
449 4ba14133 2020-03-20 stsp fd = -1;
450 33501562 2020-03-18 stsp
451 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
452 33501562 2020-03-18 stsp const char *name = pe->path;
453 33501562 2020-03-18 stsp size_t name_len = pe->path_len;
454 33501562 2020-03-18 stsp struct got_object_id *id = pe->data;
455 33501562 2020-03-18 stsp
456 4ba14133 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
457 4ba14133 2020-03-20 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
458 4ba14133 2020-03-20 stsp if (wbuf == NULL)
459 4ba14133 2020-03-20 stsp return got_error_from_errno("imsg_create FETCH_HAVE_REF");
460 4ba14133 2020-03-20 stsp
461 33501562 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_have_ref! */
462 33501562 2020-03-18 stsp if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
463 4ba14133 2020-03-20 stsp err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
464 33501562 2020-03-18 stsp ibuf_free(wbuf);
465 33501562 2020-03-18 stsp return err;
466 33501562 2020-03-18 stsp }
467 33501562 2020-03-18 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
468 4ba14133 2020-03-20 stsp err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
469 33501562 2020-03-18 stsp ibuf_free(wbuf);
470 33501562 2020-03-18 stsp return err;
471 33501562 2020-03-18 stsp }
472 33501562 2020-03-18 stsp if (imsg_add(wbuf, name, name_len) == -1) {
473 4ba14133 2020-03-20 stsp err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
474 33501562 2020-03-18 stsp ibuf_free(wbuf);
475 33501562 2020-03-18 stsp return err;
476 33501562 2020-03-18 stsp }
477 4ba14133 2020-03-20 stsp
478 4ba14133 2020-03-20 stsp wbuf->fd = -1;
479 4ba14133 2020-03-20 stsp imsg_close(ibuf, wbuf);
480 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
481 4ba14133 2020-03-20 stsp if (err)
482 4ba14133 2020-03-20 stsp return err;
483 33501562 2020-03-18 stsp }
484 33501562 2020-03-18 stsp
485 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
486 4ba14133 2020-03-20 stsp const char *name = pe->path;
487 4ba14133 2020-03-20 stsp size_t name_len = pe->path_len;
488 4ba14133 2020-03-20 stsp
489 4ba14133 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
490 4ba14133 2020-03-20 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
491 4ba14133 2020-03-20 stsp len);
492 4ba14133 2020-03-20 stsp if (wbuf == NULL)
493 4ba14133 2020-03-20 stsp return got_error_from_errno(
494 4ba14133 2020-03-20 stsp "imsg_create FETCH_WANTED_BRANCH");
495 4ba14133 2020-03-20 stsp
496 4ba14133 2020-03-20 stsp /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
497 4ba14133 2020-03-20 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
498 4ba14133 2020-03-20 stsp err = got_error_from_errno(
499 4ba14133 2020-03-20 stsp "imsg_add FETCH_WANTED_BRANCH");
500 4ba14133 2020-03-20 stsp ibuf_free(wbuf);
501 4ba14133 2020-03-20 stsp return err;
502 4ba14133 2020-03-20 stsp }
503 4ba14133 2020-03-20 stsp if (imsg_add(wbuf, name, name_len) == -1) {
504 4ba14133 2020-03-20 stsp err = got_error_from_errno(
505 4ba14133 2020-03-20 stsp "imsg_add FETCH_WANTED_BRANCH");
506 4ba14133 2020-03-20 stsp ibuf_free(wbuf);
507 4ba14133 2020-03-20 stsp return err;
508 4ba14133 2020-03-20 stsp }
509 4ba14133 2020-03-20 stsp
510 4ba14133 2020-03-20 stsp wbuf->fd = -1;
511 4ba14133 2020-03-20 stsp imsg_close(ibuf, wbuf);
512 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
513 4ba14133 2020-03-20 stsp if (err)
514 4ba14133 2020-03-20 stsp return err;
515 4ba14133 2020-03-20 stsp }
516 4ba14133 2020-03-20 stsp
517 4ba14133 2020-03-20 stsp return NULL;
518 4ba14133 2020-03-20 stsp
519 93658fb9 2020-03-18 stsp }
520 93658fb9 2020-03-18 stsp
521 93658fb9 2020-03-18 stsp const struct got_error *
522 f826addf 2020-03-18 stsp got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
523 f826addf 2020-03-18 stsp {
524 f826addf 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
525 f826addf 2020-03-18 stsp }
526 f826addf 2020-03-18 stsp
527 f826addf 2020-03-18 stsp
528 f826addf 2020-03-18 stsp const struct got_error *
529 abe0f35f 2020-03-18 stsp got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
530 abe0f35f 2020-03-18 stsp struct got_pathlist_head *symrefs)
531 abe0f35f 2020-03-18 stsp {
532 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
533 abe0f35f 2020-03-18 stsp struct ibuf *wbuf;
534 abe0f35f 2020-03-18 stsp size_t len, nsymrefs = 0;
535 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
536 abe0f35f 2020-03-18 stsp
537 abe0f35f 2020-03-18 stsp len = sizeof(struct got_imsg_fetch_symrefs);
538 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
539 abe0f35f 2020-03-18 stsp const char *target = pe->data;
540 abe0f35f 2020-03-18 stsp len += sizeof(struct got_imsg_fetch_symref) +
541 abe0f35f 2020-03-18 stsp pe->path_len + strlen(target);
542 abe0f35f 2020-03-18 stsp nsymrefs++;
543 abe0f35f 2020-03-18 stsp }
544 abe0f35f 2020-03-18 stsp
545 abe0f35f 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
546 abe0f35f 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
547 abe0f35f 2020-03-18 stsp
548 abe0f35f 2020-03-18 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
549 abe0f35f 2020-03-18 stsp if (wbuf == NULL)
550 abe0f35f 2020-03-18 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
551 abe0f35f 2020-03-18 stsp
552 abe0f35f 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
553 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
554 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
555 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
556 abe0f35f 2020-03-18 stsp return err;
557 abe0f35f 2020-03-18 stsp }
558 abe0f35f 2020-03-18 stsp
559 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
560 abe0f35f 2020-03-18 stsp const char *name = pe->path;
561 abe0f35f 2020-03-18 stsp size_t name_len = pe->path_len;
562 abe0f35f 2020-03-18 stsp const char *target = pe->data;
563 abe0f35f 2020-03-18 stsp size_t target_len = strlen(target);
564 abe0f35f 2020-03-18 stsp
565 abe0f35f 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
566 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
567 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
568 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
569 abe0f35f 2020-03-18 stsp return err;
570 abe0f35f 2020-03-18 stsp }
571 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
572 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
573 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
574 abe0f35f 2020-03-18 stsp return err;
575 abe0f35f 2020-03-18 stsp }
576 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, name, name_len) == -1) {
577 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
578 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
579 abe0f35f 2020-03-18 stsp return err;
580 abe0f35f 2020-03-18 stsp }
581 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, target, target_len) == -1) {
582 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
583 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
584 abe0f35f 2020-03-18 stsp return err;
585 abe0f35f 2020-03-18 stsp }
586 abe0f35f 2020-03-18 stsp }
587 abe0f35f 2020-03-18 stsp
588 abe0f35f 2020-03-18 stsp wbuf->fd = -1;
589 abe0f35f 2020-03-18 stsp imsg_close(ibuf, wbuf);
590 abe0f35f 2020-03-18 stsp return flush_imsg(ibuf);
591 abe0f35f 2020-03-18 stsp }
592 abe0f35f 2020-03-18 stsp
593 abe0f35f 2020-03-18 stsp const struct got_error *
594 ea7396b9 2020-03-18 stsp got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
595 b9f99abf 2020-03-18 stsp struct got_object_id *refid, const char *refname)
596 b9f99abf 2020-03-18 stsp {
597 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
598 b9f99abf 2020-03-18 stsp struct ibuf *wbuf;
599 b9f99abf 2020-03-18 stsp size_t len, reflen = strlen(refname);
600 b9f99abf 2020-03-18 stsp
601 ea7396b9 2020-03-18 stsp len = sizeof(struct got_imsg_fetch_ref) + reflen;
602 b9f99abf 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
603 b9f99abf 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
604 b9f99abf 2020-03-18 stsp
605 ea7396b9 2020-03-18 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
606 b9f99abf 2020-03-18 stsp if (wbuf == NULL)
607 ea7396b9 2020-03-18 stsp return got_error_from_errno("imsg_create FETCH_REF");
608 ea7396b9 2020-03-18 stsp
609 ea7396b9 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_ref definition! */
610 b9f99abf 2020-03-18 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
611 ea7396b9 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REF");
612 b9f99abf 2020-03-18 stsp ibuf_free(wbuf);
613 b9f99abf 2020-03-18 stsp return err;
614 b9f99abf 2020-03-18 stsp }
615 b9f99abf 2020-03-18 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
616 ea7396b9 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REF");
617 b9f99abf 2020-03-18 stsp ibuf_free(wbuf);
618 b9f99abf 2020-03-18 stsp return err;
619 b9f99abf 2020-03-18 stsp }
620 b9f99abf 2020-03-18 stsp
621 b9f99abf 2020-03-18 stsp wbuf->fd = -1;
622 b9f99abf 2020-03-18 stsp imsg_close(ibuf, wbuf);
623 531c3985 2020-03-18 stsp return flush_imsg(ibuf);
624 531c3985 2020-03-18 stsp }
625 531c3985 2020-03-18 stsp
626 531c3985 2020-03-18 stsp const struct got_error *
627 531c3985 2020-03-18 stsp got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
628 531c3985 2020-03-18 stsp size_t msglen)
629 531c3985 2020-03-18 stsp {
630 531c3985 2020-03-18 stsp if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
631 531c3985 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
632 531c3985 2020-03-18 stsp
633 531c3985 2020-03-18 stsp if (msglen == 0)
634 531c3985 2020-03-18 stsp return NULL;
635 531c3985 2020-03-18 stsp
636 531c3985 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
637 531c3985 2020-03-18 stsp msg, msglen) == -1)
638 531c3985 2020-03-18 stsp return got_error_from_errno(
639 531c3985 2020-03-18 stsp "imsg_compose FETCH_SERVER_PROGRESS");
640 d2cdc636 2020-03-18 stsp
641 d2cdc636 2020-03-18 stsp return flush_imsg(ibuf);
642 d2cdc636 2020-03-18 stsp }
643 d2cdc636 2020-03-18 stsp
644 d2cdc636 2020-03-18 stsp const struct got_error *
645 d2cdc636 2020-03-18 stsp got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
646 d2cdc636 2020-03-18 stsp {
647 d2cdc636 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
648 d2cdc636 2020-03-18 stsp &bytes, sizeof(bytes)) == -1)
649 d2cdc636 2020-03-18 stsp return got_error_from_errno(
650 d2cdc636 2020-03-18 stsp "imsg_compose FETCH_DOWNLOAD_PROGRESS");
651 531c3985 2020-03-18 stsp
652 b9f99abf 2020-03-18 stsp return flush_imsg(ibuf);
653 b9f99abf 2020-03-18 stsp }
654 b9f99abf 2020-03-18 stsp
655 b9f99abf 2020-03-18 stsp const struct got_error *
656 93658fb9 2020-03-18 stsp got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
657 93658fb9 2020-03-18 stsp {
658 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
659 93658fb9 2020-03-18 stsp hash.sha1, SHA1_DIGEST_LENGTH) == -1)
660 93658fb9 2020-03-18 stsp return got_error_from_errno("imsg_compose FETCH");
661 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
662 93658fb9 2020-03-18 stsp }
663 93658fb9 2020-03-18 stsp
664 b9f99abf 2020-03-18 stsp
665 93658fb9 2020-03-18 stsp const struct got_error *
666 8f2d01a6 2020-03-18 stsp got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
667 531c3985 2020-03-18 stsp char **refname, struct got_pathlist_head *symrefs, char **server_progress,
668 d2cdc636 2020-03-18 stsp off_t *packfile_size, struct imsgbuf *ibuf)
669 b9f99abf 2020-03-18 stsp {
670 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
671 b9f99abf 2020-03-18 stsp struct imsg imsg;
672 b9f99abf 2020-03-18 stsp size_t datalen;
673 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symrefs *isymrefs = NULL;
674 abe0f35f 2020-03-18 stsp size_t n, remain;
675 abe0f35f 2020-03-18 stsp off_t off;
676 531c3985 2020-03-18 stsp int i;
677 b9f99abf 2020-03-18 stsp
678 8f2d01a6 2020-03-18 stsp *done = 0;
679 8f2d01a6 2020-03-18 stsp *id = NULL;
680 b9f99abf 2020-03-18 stsp *refname = NULL;
681 531c3985 2020-03-18 stsp *server_progress = NULL;
682 5a489642 2020-03-19 stsp *packfile_size = 0;
683 b9f99abf 2020-03-18 stsp
684 531c3985 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
685 b9f99abf 2020-03-18 stsp if (err)
686 b9f99abf 2020-03-18 stsp return err;
687 b9f99abf 2020-03-18 stsp
688 b9f99abf 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
689 b9f99abf 2020-03-18 stsp switch (imsg.hdr.type) {
690 b9f99abf 2020-03-18 stsp case GOT_IMSG_ERROR:
691 531c3985 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_error)) {
692 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
693 531c3985 2020-03-18 stsp break;
694 531c3985 2020-03-18 stsp }
695 b9f99abf 2020-03-18 stsp err = recv_imsg_error(&imsg, datalen);
696 b9f99abf 2020-03-18 stsp break;
697 abe0f35f 2020-03-18 stsp case GOT_IMSG_FETCH_SYMREFS:
698 abe0f35f 2020-03-18 stsp if (datalen < sizeof(*isymrefs)) {
699 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
700 abe0f35f 2020-03-18 stsp break;
701 abe0f35f 2020-03-18 stsp }
702 abe0f35f 2020-03-18 stsp if (isymrefs != NULL) {
703 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
704 abe0f35f 2020-03-18 stsp break;
705 abe0f35f 2020-03-18 stsp }
706 abe0f35f 2020-03-18 stsp isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
707 abe0f35f 2020-03-18 stsp off = sizeof(*isymrefs);
708 abe0f35f 2020-03-18 stsp remain = datalen - off;
709 abe0f35f 2020-03-18 stsp for (n = 0; n < isymrefs->nsymrefs; n++) {
710 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symref *s;
711 abe0f35f 2020-03-18 stsp char *name, *target;
712 abe0f35f 2020-03-18 stsp if (remain < sizeof(struct got_imsg_fetch_symref)) {
713 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
714 abe0f35f 2020-03-18 stsp goto done;
715 abe0f35f 2020-03-18 stsp }
716 abe0f35f 2020-03-18 stsp s = (struct got_imsg_fetch_symref *)(imsg.data + off);
717 abe0f35f 2020-03-18 stsp off += sizeof(*s);
718 abe0f35f 2020-03-18 stsp remain -= sizeof(*s);
719 abe0f35f 2020-03-18 stsp if (remain < s->name_len) {
720 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
721 abe0f35f 2020-03-18 stsp goto done;
722 abe0f35f 2020-03-18 stsp }
723 abe0f35f 2020-03-18 stsp name = strndup(imsg.data + off, s->name_len);
724 abe0f35f 2020-03-18 stsp if (name == NULL) {
725 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
726 abe0f35f 2020-03-18 stsp goto done;
727 abe0f35f 2020-03-18 stsp }
728 abe0f35f 2020-03-18 stsp off += s->name_len;
729 abe0f35f 2020-03-18 stsp remain -= s->name_len;
730 abe0f35f 2020-03-18 stsp if (remain < s->target_len) {
731 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
732 abe0f35f 2020-03-18 stsp free(name);
733 abe0f35f 2020-03-18 stsp goto done;
734 abe0f35f 2020-03-18 stsp }
735 abe0f35f 2020-03-18 stsp target = strndup(imsg.data + off, s->target_len);
736 abe0f35f 2020-03-18 stsp if (target == NULL) {
737 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
738 abe0f35f 2020-03-18 stsp free(name);
739 abe0f35f 2020-03-18 stsp goto done;
740 abe0f35f 2020-03-18 stsp }
741 abe0f35f 2020-03-18 stsp off += s->target_len;
742 abe0f35f 2020-03-18 stsp remain -= s->target_len;
743 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
744 abe0f35f 2020-03-18 stsp if (err) {
745 abe0f35f 2020-03-18 stsp free(name);
746 abe0f35f 2020-03-18 stsp free(target);
747 abe0f35f 2020-03-18 stsp goto done;
748 abe0f35f 2020-03-18 stsp }
749 abe0f35f 2020-03-18 stsp }
750 abe0f35f 2020-03-18 stsp break;
751 ea7396b9 2020-03-18 stsp case GOT_IMSG_FETCH_REF:
752 531c3985 2020-03-18 stsp if (datalen <= SHA1_DIGEST_LENGTH) {
753 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
754 531c3985 2020-03-18 stsp break;
755 531c3985 2020-03-18 stsp }
756 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
757 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
758 b9f99abf 2020-03-18 stsp err = got_error_from_errno("malloc");
759 b9f99abf 2020-03-18 stsp break;
760 b9f99abf 2020-03-18 stsp }
761 8f2d01a6 2020-03-18 stsp memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
762 b9f99abf 2020-03-18 stsp *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
763 b9f99abf 2020-03-18 stsp datalen - SHA1_DIGEST_LENGTH);
764 b9f99abf 2020-03-18 stsp if (*refname == NULL) {
765 b9f99abf 2020-03-18 stsp err = got_error_from_errno("strndup");
766 8f2d01a6 2020-03-18 stsp break;
767 8f2d01a6 2020-03-18 stsp }
768 8f2d01a6 2020-03-18 stsp break;
769 531c3985 2020-03-18 stsp case GOT_IMSG_FETCH_SERVER_PROGRESS:
770 531c3985 2020-03-18 stsp if (datalen == 0) {
771 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
772 531c3985 2020-03-18 stsp break;
773 531c3985 2020-03-18 stsp }
774 531c3985 2020-03-18 stsp *server_progress = strndup(imsg.data, datalen);
775 531c3985 2020-03-18 stsp if (*server_progress == NULL) {
776 531c3985 2020-03-18 stsp err = got_error_from_errno("strndup");
777 531c3985 2020-03-18 stsp break;
778 531c3985 2020-03-18 stsp }
779 531c3985 2020-03-18 stsp for (i = 0; i < datalen; i++) {
780 531c3985 2020-03-18 stsp if (!isprint((unsigned char)(*server_progress)[i]) &&
781 531c3985 2020-03-18 stsp !isspace((unsigned char)(*server_progress)[i])) {
782 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
783 531c3985 2020-03-18 stsp free(*server_progress);
784 531c3985 2020-03-18 stsp *server_progress = NULL;
785 531c3985 2020-03-18 stsp goto done;
786 531c3985 2020-03-18 stsp }
787 531c3985 2020-03-18 stsp }
788 531c3985 2020-03-18 stsp break;
789 d2cdc636 2020-03-18 stsp case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
790 d2cdc636 2020-03-18 stsp if (datalen < sizeof(*packfile_size)) {
791 d2cdc636 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
792 d2cdc636 2020-03-18 stsp break;
793 d2cdc636 2020-03-18 stsp }
794 d2cdc636 2020-03-18 stsp memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
795 d2cdc636 2020-03-18 stsp break;
796 8f2d01a6 2020-03-18 stsp case GOT_IMSG_FETCH_DONE:
797 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
798 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
799 8f2d01a6 2020-03-18 stsp err = got_error_from_errno("malloc");
800 b9f99abf 2020-03-18 stsp break;
801 b9f99abf 2020-03-18 stsp }
802 8f2d01a6 2020-03-18 stsp if (datalen != SHA1_DIGEST_LENGTH) {
803 8f2d01a6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
804 8f2d01a6 2020-03-18 stsp break;
805 8f2d01a6 2020-03-18 stsp }
806 8f2d01a6 2020-03-18 stsp memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
807 8f2d01a6 2020-03-18 stsp *done = 1;
808 b9f99abf 2020-03-18 stsp break;
809 b9f99abf 2020-03-18 stsp default:
810 b887aab6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
811 b887aab6 2020-03-18 stsp break;
812 b9f99abf 2020-03-18 stsp }
813 abe0f35f 2020-03-18 stsp done:
814 b887aab6 2020-03-18 stsp if (err) {
815 8f2d01a6 2020-03-18 stsp free(*id);
816 8f2d01a6 2020-03-18 stsp *id = NULL;
817 b887aab6 2020-03-18 stsp free(*refname);
818 b887aab6 2020-03-18 stsp *refname = NULL;
819 b887aab6 2020-03-18 stsp }
820 b9f99abf 2020-03-18 stsp imsg_free(&imsg);
821 b9f99abf 2020-03-18 stsp return err;
822 93658fb9 2020-03-18 stsp }
823 93658fb9 2020-03-18 stsp
824 93658fb9 2020-03-18 stsp const struct got_error *
825 668a20f6 2020-03-18 stsp got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
826 668a20f6 2020-03-18 stsp int fd)
827 93658fb9 2020-03-18 stsp {
828 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
829 93658fb9 2020-03-18 stsp
830 668a20f6 2020-03-18 stsp /* Keep in sync with struct got_imsg_index_pack_request */
831 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
832 668a20f6 2020-03-18 stsp pack_hash, SHA1_DIGEST_LENGTH) == -1) {
833 93658fb9 2020-03-18 stsp err = got_error_from_errno("imsg_compose INDEX_REQUEST");
834 93658fb9 2020-03-18 stsp close(fd);
835 93658fb9 2020-03-18 stsp return err;
836 93658fb9 2020-03-18 stsp }
837 baa9fea0 2020-03-18 stsp return flush_imsg(ibuf);
838 baa9fea0 2020-03-18 stsp }
839 baa9fea0 2020-03-18 stsp
840 baa9fea0 2020-03-18 stsp const struct got_error *
841 73ab1060 2020-03-18 stsp got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
842 73ab1060 2020-03-18 stsp {
843 73ab1060 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
844 73ab1060 2020-03-18 stsp }
845 73ab1060 2020-03-18 stsp
846 73ab1060 2020-03-18 stsp const struct got_error *
847 668a20f6 2020-03-18 stsp got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
848 668a20f6 2020-03-18 stsp int nobj_indexed, int nobj_loose, int nobj_resolved)
849 baa9fea0 2020-03-18 stsp {
850 baa9fea0 2020-03-18 stsp struct got_imsg_index_pack_progress iprogress;
851 baa9fea0 2020-03-18 stsp
852 668a20f6 2020-03-18 stsp iprogress.nobj_total = nobj_total;
853 668a20f6 2020-03-18 stsp iprogress.nobj_indexed = nobj_indexed;
854 668a20f6 2020-03-18 stsp iprogress.nobj_loose = nobj_loose;
855 668a20f6 2020-03-18 stsp iprogress.nobj_resolved = nobj_resolved;
856 baa9fea0 2020-03-18 stsp
857 baa9fea0 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
858 baa9fea0 2020-03-18 stsp &iprogress, sizeof(iprogress)) == -1)
859 baa9fea0 2020-03-18 stsp return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
860 baa9fea0 2020-03-18 stsp
861 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
862 93658fb9 2020-03-18 stsp }
863 93658fb9 2020-03-18 stsp
864 93658fb9 2020-03-18 stsp const struct got_error *
865 93658fb9 2020-03-18 stsp got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
866 93658fb9 2020-03-18 stsp {
867 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
868 93658fb9 2020-03-18 stsp return got_error_from_errno("imsg_compose FETCH");
869 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
870 93658fb9 2020-03-18 stsp }
871 93658fb9 2020-03-18 stsp
872 93658fb9 2020-03-18 stsp const struct got_error *
873 668a20f6 2020-03-18 stsp got_privsep_recv_index_progress(int *done, int *nobj_total,
874 668a20f6 2020-03-18 stsp int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
875 668a20f6 2020-03-18 stsp struct imsgbuf *ibuf)
876 93658fb9 2020-03-18 stsp {
877 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
878 93658fb9 2020-03-18 stsp struct imsg imsg;
879 baa9fea0 2020-03-18 stsp struct got_imsg_index_pack_progress *iprogress;
880 baa9fea0 2020-03-18 stsp size_t datalen;
881 93658fb9 2020-03-18 stsp
882 baa9fea0 2020-03-18 stsp *done = 0;
883 668a20f6 2020-03-18 stsp *nobj_total = 0;
884 668a20f6 2020-03-18 stsp *nobj_indexed = 0;
885 668a20f6 2020-03-18 stsp *nobj_resolved = 0;
886 baa9fea0 2020-03-18 stsp
887 93658fb9 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
888 93658fb9 2020-03-18 stsp if (err)
889 93658fb9 2020-03-18 stsp return err;
890 baa9fea0 2020-03-18 stsp
891 baa9fea0 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
892 baa9fea0 2020-03-18 stsp switch (imsg.hdr.type) {
893 baa9fea0 2020-03-18 stsp case GOT_IMSG_ERROR:
894 baa9fea0 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_error)) {
895 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
896 baa9fea0 2020-03-18 stsp break;
897 baa9fea0 2020-03-18 stsp }
898 baa9fea0 2020-03-18 stsp err = recv_imsg_error(&imsg, datalen);
899 baa9fea0 2020-03-18 stsp break;
900 baa9fea0 2020-03-18 stsp case GOT_IMSG_IDXPACK_PROGRESS:
901 baa9fea0 2020-03-18 stsp if (datalen < sizeof(*iprogress)) {
902 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
903 baa9fea0 2020-03-18 stsp break;
904 baa9fea0 2020-03-18 stsp }
905 baa9fea0 2020-03-18 stsp iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
906 668a20f6 2020-03-18 stsp *nobj_total = iprogress->nobj_total;
907 668a20f6 2020-03-18 stsp *nobj_indexed = iprogress->nobj_indexed;
908 668a20f6 2020-03-18 stsp *nobj_loose = iprogress->nobj_loose;
909 668a20f6 2020-03-18 stsp *nobj_resolved = iprogress->nobj_resolved;
910 baa9fea0 2020-03-18 stsp break;
911 baa9fea0 2020-03-18 stsp case GOT_IMSG_IDXPACK_DONE:
912 baa9fea0 2020-03-18 stsp if (datalen != 0) {
913 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
914 baa9fea0 2020-03-18 stsp break;
915 baa9fea0 2020-03-18 stsp }
916 baa9fea0 2020-03-18 stsp *done = 1;
917 baa9fea0 2020-03-18 stsp break;
918 baa9fea0 2020-03-18 stsp default:
919 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
920 baa9fea0 2020-03-18 stsp break;
921 baa9fea0 2020-03-18 stsp }
922 baa9fea0 2020-03-18 stsp
923 93658fb9 2020-03-18 stsp imsg_free(&imsg);
924 baa9fea0 2020-03-18 stsp return err;
925 93658fb9 2020-03-18 stsp }
926 93658fb9 2020-03-18 stsp
927 93658fb9 2020-03-18 stsp const struct got_error *
928 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
929 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
930 cfd633c2 2018-09-10 stsp {
931 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
932 291624d8 2018-11-07 stsp struct got_imsg_object *iobj;
933 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
934 cfd633c2 2018-09-10 stsp
935 291624d8 2018-11-07 stsp if (datalen != sizeof(*iobj))
936 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
937 291624d8 2018-11-07 stsp iobj = imsg->data;
938 cfd633c2 2018-09-10 stsp
939 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
940 cfd633c2 2018-09-10 stsp if (*obj == NULL)
941 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
942 cfd633c2 2018-09-10 stsp
943 291624d8 2018-11-07 stsp memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
944 291624d8 2018-11-07 stsp (*obj)->type = iobj->type;
945 291624d8 2018-11-07 stsp (*obj)->flags = iobj->flags;
946 291624d8 2018-11-07 stsp (*obj)->hdrlen = iobj->hdrlen;
947 291624d8 2018-11-07 stsp (*obj)->size = iobj->size;
948 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
949 291624d8 2018-11-07 stsp if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
950 291624d8 2018-11-07 stsp (*obj)->pack_offset = iobj->pack_offset;
951 291624d8 2018-11-07 stsp (*obj)->pack_idx = iobj->pack_idx;
952 876c234b 2018-09-10 stsp }
953 876c234b 2018-09-10 stsp
954 876c234b 2018-09-10 stsp return err;
955 876c234b 2018-09-10 stsp }
956 876c234b 2018-09-10 stsp
957 2178c42e 2018-04-22 stsp const struct got_error *
958 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
959 2178c42e 2018-04-22 stsp {
960 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
961 2178c42e 2018-04-22 stsp struct imsg imsg;
962 c4eae628 2018-04-23 stsp const size_t min_datalen =
963 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
964 2178c42e 2018-04-22 stsp
965 2178c42e 2018-04-22 stsp *obj = NULL;
966 2178c42e 2018-04-22 stsp
967 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
968 2178c42e 2018-04-22 stsp if (err)
969 2178c42e 2018-04-22 stsp return err;
970 2178c42e 2018-04-22 stsp
971 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
972 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
973 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
974 bff6ca00 2018-04-23 stsp break;
975 bff6ca00 2018-04-23 stsp default:
976 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
977 bff6ca00 2018-04-23 stsp break;
978 bff6ca00 2018-04-23 stsp }
979 bff6ca00 2018-04-23 stsp
980 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
981 bff6ca00 2018-04-23 stsp
982 bff6ca00 2018-04-23 stsp return err;
983 c75f7264 2018-09-11 stsp }
984 c75f7264 2018-09-11 stsp
985 c75f7264 2018-09-11 stsp static const struct got_error *
986 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
987 c75f7264 2018-09-11 stsp size_t logmsg_len)
988 c75f7264 2018-09-11 stsp {
989 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
990 c75f7264 2018-09-11 stsp size_t offset, remain;
991 c75f7264 2018-09-11 stsp
992 c75f7264 2018-09-11 stsp offset = 0;
993 c75f7264 2018-09-11 stsp remain = logmsg_len;
994 c75f7264 2018-09-11 stsp while (remain > 0) {
995 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
996 c75f7264 2018-09-11 stsp
997 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
998 fa4ffeb3 2018-11-04 stsp commit->logmsg + offset, n) == -1) {
999 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose "
1000 230a42bd 2019-05-11 jcs "COMMIT_LOGMSG");
1001 fa4ffeb3 2018-11-04 stsp break;
1002 fa4ffeb3 2018-11-04 stsp }
1003 c75f7264 2018-09-11 stsp
1004 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
1005 fa4ffeb3 2018-11-04 stsp if (err)
1006 fa4ffeb3 2018-11-04 stsp break;
1007 c75f7264 2018-09-11 stsp
1008 c75f7264 2018-09-11 stsp offset += n;
1009 c75f7264 2018-09-11 stsp remain -= n;
1010 c75f7264 2018-09-11 stsp }
1011 c75f7264 2018-09-11 stsp
1012 fa4ffeb3 2018-11-04 stsp return err;
1013 bff6ca00 2018-04-23 stsp }
1014 bff6ca00 2018-04-23 stsp
1015 bff6ca00 2018-04-23 stsp const struct got_error *
1016 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1017 bff6ca00 2018-04-23 stsp {
1018 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
1019 b9c33926 2018-11-07 stsp struct got_imsg_commit_object *icommit;
1020 bff6ca00 2018-04-23 stsp uint8_t *buf;
1021 bff6ca00 2018-04-23 stsp size_t len, total;
1022 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1023 b9c33926 2018-11-07 stsp size_t author_len = strlen(commit->author);
1024 b9c33926 2018-11-07 stsp size_t committer_len = strlen(commit->committer);
1025 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
1026 bff6ca00 2018-04-23 stsp
1027 b9c33926 2018-11-07 stsp total = sizeof(*icommit) + author_len + committer_len +
1028 b9c33926 2018-11-07 stsp commit->nparents * SHA1_DIGEST_LENGTH;
1029 bff6ca00 2018-04-23 stsp
1030 bff6ca00 2018-04-23 stsp buf = malloc(total);
1031 bff6ca00 2018-04-23 stsp if (buf == NULL)
1032 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1033 bff6ca00 2018-04-23 stsp
1034 b9c33926 2018-11-07 stsp icommit = (struct got_imsg_commit_object *)buf;
1035 a7403916 2018-12-24 stsp memcpy(icommit->tree_id, commit->tree_id->sha1,
1036 a7403916 2018-12-24 stsp sizeof(icommit->tree_id));
1037 b9c33926 2018-11-07 stsp icommit->author_len = author_len;
1038 b9c33926 2018-11-07 stsp icommit->author_time = commit->author_time;
1039 b9c33926 2018-11-07 stsp icommit->author_gmtoff = commit->author_gmtoff;
1040 b9c33926 2018-11-07 stsp icommit->committer_len = committer_len;
1041 b9c33926 2018-11-07 stsp icommit->committer_time = commit->committer_time;
1042 b9c33926 2018-11-07 stsp icommit->committer_gmtoff = commit->committer_gmtoff;
1043 b9c33926 2018-11-07 stsp icommit->logmsg_len = logmsg_len;
1044 b9c33926 2018-11-07 stsp icommit->nparents = commit->nparents;
1045 b9c33926 2018-11-07 stsp
1046 b9c33926 2018-11-07 stsp len = sizeof(*icommit);
1047 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->author, author_len);
1048 b9c33926 2018-11-07 stsp len += author_len;
1049 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->committer, committer_len);
1050 b9c33926 2018-11-07 stsp len += committer_len;
1051 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1052 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1053 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
1054 bff6ca00 2018-04-23 stsp }
1055 bff6ca00 2018-04-23 stsp
1056 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1057 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT");
1058 bff6ca00 2018-04-23 stsp goto done;
1059 bff6ca00 2018-04-23 stsp }
1060 bff6ca00 2018-04-23 stsp
1061 904df868 2018-11-04 stsp if (logmsg_len == 0 ||
1062 904df868 2018-11-04 stsp logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1063 904df868 2018-11-04 stsp err = flush_imsg(ibuf);
1064 904df868 2018-11-04 stsp if (err)
1065 904df868 2018-11-04 stsp goto done;
1066 904df868 2018-11-04 stsp }
1067 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
1068 bff6ca00 2018-04-23 stsp done:
1069 bff6ca00 2018-04-23 stsp free(buf);
1070 bff6ca00 2018-04-23 stsp return err;
1071 bff6ca00 2018-04-23 stsp }
1072 cfd633c2 2018-09-10 stsp
1073 ca6e02ac 2020-01-07 stsp static const struct got_error *
1074 ca6e02ac 2020-01-07 stsp get_commit_from_imsg(struct got_commit_object **commit,
1075 ca6e02ac 2020-01-07 stsp struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1076 bff6ca00 2018-04-23 stsp {
1077 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
1078 291624d8 2018-11-07 stsp struct got_imsg_commit_object *icommit;
1079 ca6e02ac 2020-01-07 stsp size_t len = 0;
1080 bff6ca00 2018-04-23 stsp int i;
1081 bff6ca00 2018-04-23 stsp
1082 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(*icommit))
1083 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1084 bff6ca00 2018-04-23 stsp
1085 ca6e02ac 2020-01-07 stsp icommit = imsg->data;
1086 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommit) + icommit->author_len +
1087 ca6e02ac 2020-01-07 stsp icommit->committer_len +
1088 ca6e02ac 2020-01-07 stsp icommit->nparents * SHA1_DIGEST_LENGTH)
1089 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1090 bff6ca00 2018-04-23 stsp
1091 ca6e02ac 2020-01-07 stsp if (icommit->nparents < 0)
1092 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1093 ca6e02ac 2020-01-07 stsp
1094 ca6e02ac 2020-01-07 stsp len += sizeof(*icommit);
1095 bff6ca00 2018-04-23 stsp
1096 ca6e02ac 2020-01-07 stsp *commit = got_object_commit_alloc_partial();
1097 ca6e02ac 2020-01-07 stsp if (*commit == NULL)
1098 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
1099 ca6e02ac 2020-01-07 stsp "got_object_commit_alloc_partial");
1100 ca6e02ac 2020-01-07 stsp
1101 ca6e02ac 2020-01-07 stsp memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1102 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
1103 ca6e02ac 2020-01-07 stsp (*commit)->author_time = icommit->author_time;
1104 ca6e02ac 2020-01-07 stsp (*commit)->author_gmtoff = icommit->author_gmtoff;
1105 ca6e02ac 2020-01-07 stsp (*commit)->committer_time = icommit->committer_time;
1106 ca6e02ac 2020-01-07 stsp (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1107 ca6e02ac 2020-01-07 stsp
1108 ca6e02ac 2020-01-07 stsp if (icommit->author_len == 0) {
1109 ca6e02ac 2020-01-07 stsp (*commit)->author = strdup("");
1110 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
1111 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1112 ca6e02ac 2020-01-07 stsp goto done;
1113 bff6ca00 2018-04-23 stsp }
1114 ca6e02ac 2020-01-07 stsp } else {
1115 ca6e02ac 2020-01-07 stsp (*commit)->author = malloc(icommit->author_len + 1);
1116 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
1117 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1118 ca6e02ac 2020-01-07 stsp goto done;
1119 ca6e02ac 2020-01-07 stsp }
1120 ca6e02ac 2020-01-07 stsp memcpy((*commit)->author, imsg->data + len,
1121 ca6e02ac 2020-01-07 stsp icommit->author_len);
1122 ca6e02ac 2020-01-07 stsp (*commit)->author[icommit->author_len] = '\0';
1123 ca6e02ac 2020-01-07 stsp }
1124 ca6e02ac 2020-01-07 stsp len += icommit->author_len;
1125 bff6ca00 2018-04-23 stsp
1126 ca6e02ac 2020-01-07 stsp if (icommit->committer_len == 0) {
1127 ca6e02ac 2020-01-07 stsp (*commit)->committer = strdup("");
1128 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
1129 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1130 ca6e02ac 2020-01-07 stsp goto done;
1131 ca6e02ac 2020-01-07 stsp }
1132 ca6e02ac 2020-01-07 stsp } else {
1133 ca6e02ac 2020-01-07 stsp (*commit)->committer =
1134 ca6e02ac 2020-01-07 stsp malloc(icommit->committer_len + 1);
1135 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
1136 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1137 ca6e02ac 2020-01-07 stsp goto done;
1138 ca6e02ac 2020-01-07 stsp }
1139 ca6e02ac 2020-01-07 stsp memcpy((*commit)->committer, imsg->data + len,
1140 ca6e02ac 2020-01-07 stsp icommit->committer_len);
1141 ca6e02ac 2020-01-07 stsp (*commit)->committer[icommit->committer_len] = '\0';
1142 ca6e02ac 2020-01-07 stsp }
1143 ca6e02ac 2020-01-07 stsp len += icommit->committer_len;
1144 ca6e02ac 2020-01-07 stsp
1145 ca6e02ac 2020-01-07 stsp if (icommit->logmsg_len == 0) {
1146 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = strdup("");
1147 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
1148 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1149 ca6e02ac 2020-01-07 stsp goto done;
1150 ca6e02ac 2020-01-07 stsp }
1151 ca6e02ac 2020-01-07 stsp } else {
1152 ca6e02ac 2020-01-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
1153 ca6e02ac 2020-01-07 stsp
1154 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1155 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
1156 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1157 ca6e02ac 2020-01-07 stsp goto done;
1158 bff6ca00 2018-04-23 stsp }
1159 ca6e02ac 2020-01-07 stsp while (remain > 0) {
1160 ca6e02ac 2020-01-07 stsp struct imsg imsg_log;
1161 ca6e02ac 2020-01-07 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1162 ca6e02ac 2020-01-07 stsp remain);
1163 6c281f94 2018-06-11 stsp
1164 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1165 ca6e02ac 2020-01-07 stsp if (err)
1166 ca6e02ac 2020-01-07 stsp goto done;
1167 bff6ca00 2018-04-23 stsp
1168 ca6e02ac 2020-01-07 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1169 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1170 ca6e02ac 2020-01-07 stsp goto done;
1171 bff6ca00 2018-04-23 stsp }
1172 c75f7264 2018-09-11 stsp
1173 ca6e02ac 2020-01-07 stsp memcpy((*commit)->logmsg + offset,
1174 ca6e02ac 2020-01-07 stsp imsg_log.data, n);
1175 ca6e02ac 2020-01-07 stsp imsg_free(&imsg_log);
1176 ca6e02ac 2020-01-07 stsp offset += n;
1177 ca6e02ac 2020-01-07 stsp remain -= n;
1178 bff6ca00 2018-04-23 stsp }
1179 ca6e02ac 2020-01-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
1180 ca6e02ac 2020-01-07 stsp }
1181 bff6ca00 2018-04-23 stsp
1182 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommit->nparents; i++) {
1183 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
1184 86acc566 2018-04-23 stsp
1185 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
1186 ca6e02ac 2020-01-07 stsp if (err)
1187 ca6e02ac 2020-01-07 stsp break;
1188 ca6e02ac 2020-01-07 stsp memcpy(qid->id, imsg->data + len +
1189 ca6e02ac 2020-01-07 stsp i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1190 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1191 ca6e02ac 2020-01-07 stsp (*commit)->nparents++;
1192 ca6e02ac 2020-01-07 stsp }
1193 ca6e02ac 2020-01-07 stsp done:
1194 ca6e02ac 2020-01-07 stsp if (err) {
1195 ca6e02ac 2020-01-07 stsp got_object_commit_close(*commit);
1196 ca6e02ac 2020-01-07 stsp *commit = NULL;
1197 ca6e02ac 2020-01-07 stsp }
1198 ca6e02ac 2020-01-07 stsp return err;
1199 ca6e02ac 2020-01-07 stsp }
1200 ca6e02ac 2020-01-07 stsp
1201 ca6e02ac 2020-01-07 stsp const struct got_error *
1202 ca6e02ac 2020-01-07 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1203 ca6e02ac 2020-01-07 stsp {
1204 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1205 ca6e02ac 2020-01-07 stsp struct imsg imsg;
1206 ca6e02ac 2020-01-07 stsp size_t datalen;
1207 ca6e02ac 2020-01-07 stsp const size_t min_datalen =
1208 ca6e02ac 2020-01-07 stsp MIN(sizeof(struct got_imsg_error),
1209 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_object));
1210 ca6e02ac 2020-01-07 stsp
1211 ca6e02ac 2020-01-07 stsp *commit = NULL;
1212 ca6e02ac 2020-01-07 stsp
1213 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1214 ca6e02ac 2020-01-07 stsp if (err)
1215 ca6e02ac 2020-01-07 stsp return err;
1216 ca6e02ac 2020-01-07 stsp
1217 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1218 ca6e02ac 2020-01-07 stsp
1219 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
1220 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
1221 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1222 2178c42e 2018-04-22 stsp break;
1223 8c580685 2018-04-22 stsp default:
1224 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1225 8c580685 2018-04-22 stsp break;
1226 2178c42e 2018-04-22 stsp }
1227 2178c42e 2018-04-22 stsp
1228 2178c42e 2018-04-22 stsp imsg_free(&imsg);
1229 e033d803 2018-04-23 stsp
1230 e033d803 2018-04-23 stsp return err;
1231 e033d803 2018-04-23 stsp }
1232 e033d803 2018-04-23 stsp
1233 e033d803 2018-04-23 stsp const struct got_error *
1234 3022d272 2019-11-14 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1235 3022d272 2019-11-14 stsp int nentries)
1236 e033d803 2018-04-23 stsp {
1237 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1238 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
1239 3022d272 2019-11-14 stsp struct got_pathlist_entry *pe;
1240 b00c9821 2018-11-04 stsp size_t totlen;
1241 6eb07a17 2018-11-04 stsp int nimsg; /* number of imsg queued in ibuf */
1242 e033d803 2018-04-23 stsp
1243 3022d272 2019-11-14 stsp itree.nentries = nentries;
1244 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1245 e033d803 2018-04-23 stsp == -1)
1246 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TREE");
1247 e033d803 2018-04-23 stsp
1248 b00c9821 2018-11-04 stsp totlen = sizeof(itree);
1249 6eb07a17 2018-11-04 stsp nimsg = 1;
1250 3022d272 2019-11-14 stsp TAILQ_FOREACH(pe, entries, entry) {
1251 3022d272 2019-11-14 stsp const char *name = pe->path;
1252 3022d272 2019-11-14 stsp struct got_parsed_tree_entry *pte = pe->data;
1253 3022d272 2019-11-14 stsp struct ibuf *wbuf;
1254 3022d272 2019-11-14 stsp size_t namelen = strlen(name);
1255 cd9e913a 2019-11-27 stsp size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1256 e033d803 2018-04-23 stsp
1257 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
1258 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
1259 e033d803 2018-04-23 stsp
1260 6eb07a17 2018-11-04 stsp nimsg++;
1261 6eb07a17 2018-11-04 stsp if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1262 b00c9821 2018-11-04 stsp err = flush_imsg(ibuf);
1263 b00c9821 2018-11-04 stsp if (err)
1264 b00c9821 2018-11-04 stsp return err;
1265 6eb07a17 2018-11-04 stsp nimsg = 0;
1266 b00c9821 2018-11-04 stsp }
1267 b00c9821 2018-11-04 stsp
1268 3022d272 2019-11-14 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1269 3022d272 2019-11-14 stsp if (wbuf == NULL)
1270 3022d272 2019-11-14 stsp return got_error_from_errno("imsg_create TREE_ENTRY");
1271 e033d803 2018-04-23 stsp
1272 3022d272 2019-11-14 stsp /* Keep in sync with struct got_imsg_tree_object definition! */
1273 3b647085 2019-11-23 stsp if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1274 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1275 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1276 e033d803 2018-04-23 stsp return err;
1277 3b647085 2019-11-23 stsp }
1278 3b647085 2019-11-23 stsp if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1279 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1280 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1281 3022d272 2019-11-14 stsp return err;
1282 3b647085 2019-11-23 stsp }
1283 3022d272 2019-11-14 stsp
1284 3b647085 2019-11-23 stsp if (imsg_add(wbuf, name, namelen) == -1) {
1285 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1286 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1287 3022d272 2019-11-14 stsp return err;
1288 3b647085 2019-11-23 stsp }
1289 3022d272 2019-11-14 stsp
1290 3022d272 2019-11-14 stsp wbuf->fd = -1;
1291 3022d272 2019-11-14 stsp imsg_close(ibuf, wbuf);
1292 3022d272 2019-11-14 stsp
1293 b00c9821 2018-11-04 stsp totlen += len;
1294 e033d803 2018-04-23 stsp }
1295 e033d803 2018-04-23 stsp
1296 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
1297 e033d803 2018-04-23 stsp }
1298 e033d803 2018-04-23 stsp
1299 e033d803 2018-04-23 stsp const struct got_error *
1300 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1301 e033d803 2018-04-23 stsp {
1302 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1303 e033d803 2018-04-23 stsp const size_t min_datalen =
1304 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
1305 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
1306 291624d8 2018-11-07 stsp struct got_imsg_tree_object *itree;
1307 e033d803 2018-04-23 stsp int nentries = 0;
1308 2178c42e 2018-04-22 stsp
1309 e033d803 2018-04-23 stsp *tree = NULL;
1310 e033d803 2018-04-23 stsp get_more:
1311 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
1312 e033d803 2018-04-23 stsp if (err)
1313 1e51f5b9 2018-04-23 stsp goto done;
1314 e033d803 2018-04-23 stsp
1315 656b1f76 2019-05-11 jcs for (;;) {
1316 e033d803 2018-04-23 stsp struct imsg imsg;
1317 e033d803 2018-04-23 stsp size_t n;
1318 e033d803 2018-04-23 stsp size_t datalen;
1319 c0588d8d 2018-11-07 stsp struct got_imsg_tree_entry *ite;
1320 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
1321 e033d803 2018-04-23 stsp
1322 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
1323 e033d803 2018-04-23 stsp if (n == 0) {
1324 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries)
1325 e033d803 2018-04-23 stsp goto get_more;
1326 e033d803 2018-04-23 stsp break;
1327 e033d803 2018-04-23 stsp }
1328 e033d803 2018-04-23 stsp
1329 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1330 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1331 e033d803 2018-04-23 stsp
1332 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1333 e033d803 2018-04-23 stsp
1334 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
1335 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
1336 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
1337 e033d803 2018-04-23 stsp break;
1338 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
1339 e033d803 2018-04-23 stsp /* This message should only appear once. */
1340 e033d803 2018-04-23 stsp if (*tree != NULL) {
1341 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1342 e033d803 2018-04-23 stsp break;
1343 e033d803 2018-04-23 stsp }
1344 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
1345 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1346 e033d803 2018-04-23 stsp break;
1347 e033d803 2018-04-23 stsp }
1348 291624d8 2018-11-07 stsp itree = imsg.data;
1349 c3b78ecc 2018-11-07 stsp *tree = malloc(sizeof(**tree));
1350 e033d803 2018-04-23 stsp if (*tree == NULL) {
1351 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1352 e033d803 2018-04-23 stsp break;
1353 e033d803 2018-04-23 stsp }
1354 56e0773d 2019-11-28 stsp (*tree)->entries = calloc(itree->nentries,
1355 56e0773d 2019-11-28 stsp sizeof(struct got_tree_entry));
1356 56e0773d 2019-11-28 stsp if ((*tree)->entries == NULL) {
1357 56e0773d 2019-11-28 stsp err = got_error_from_errno("malloc");
1358 56e0773d 2019-11-28 stsp break;
1359 56e0773d 2019-11-28 stsp }
1360 56e0773d 2019-11-28 stsp (*tree)->nentries = itree->nentries;
1361 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
1362 e033d803 2018-04-23 stsp break;
1363 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
1364 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
1365 e033d803 2018-04-23 stsp if (*tree == NULL) {
1366 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1367 e033d803 2018-04-23 stsp break;
1368 e033d803 2018-04-23 stsp }
1369 c0588d8d 2018-11-07 stsp if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1370 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1371 e033d803 2018-04-23 stsp break;
1372 e033d803 2018-04-23 stsp }
1373 e033d803 2018-04-23 stsp
1374 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
1375 c0588d8d 2018-11-07 stsp datalen -= sizeof(*ite);
1376 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1377 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1378 e033d803 2018-04-23 stsp break;
1379 e033d803 2018-04-23 stsp }
1380 c0588d8d 2018-11-07 stsp ite = imsg.data;
1381 052d4dc3 2018-04-23 stsp
1382 56e0773d 2019-11-28 stsp if (datalen + 1 > sizeof(te->name)) {
1383 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1384 e033d803 2018-04-23 stsp break;
1385 e033d803 2018-04-23 stsp }
1386 56e0773d 2019-11-28 stsp te = &(*tree)->entries[nentries];
1387 c0588d8d 2018-11-07 stsp memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1388 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
1389 e033d803 2018-04-23 stsp
1390 56e0773d 2019-11-28 stsp memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1391 c0588d8d 2018-11-07 stsp te->mode = ite->mode;
1392 56e0773d 2019-11-28 stsp te->idx = nentries;
1393 e033d803 2018-04-23 stsp nentries++;
1394 e033d803 2018-04-23 stsp break;
1395 e033d803 2018-04-23 stsp default:
1396 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1397 e033d803 2018-04-23 stsp break;
1398 e033d803 2018-04-23 stsp }
1399 e033d803 2018-04-23 stsp
1400 e033d803 2018-04-23 stsp imsg_free(&imsg);
1401 e033d803 2018-04-23 stsp }
1402 1e51f5b9 2018-04-23 stsp done:
1403 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries) {
1404 1e51f5b9 2018-04-23 stsp if (err == NULL)
1405 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1406 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
1407 e033d803 2018-04-23 stsp *tree = NULL;
1408 ff6b18f8 2018-04-24 stsp }
1409 ff6b18f8 2018-04-24 stsp
1410 ff6b18f8 2018-04-24 stsp return err;
1411 ff6b18f8 2018-04-24 stsp }
1412 ff6b18f8 2018-04-24 stsp
1413 ff6b18f8 2018-04-24 stsp const struct got_error *
1414 ac544f8c 2019-01-13 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1415 ac544f8c 2019-01-13 stsp const uint8_t *data)
1416 ff6b18f8 2018-04-24 stsp {
1417 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
1418 2967a784 2018-04-24 stsp
1419 2967a784 2018-04-24 stsp iblob.size = size;
1420 ebc55e2d 2018-12-24 stsp iblob.hdrlen = hdrlen;
1421 2967a784 2018-04-24 stsp
1422 ac544f8c 2019-01-13 stsp if (data) {
1423 ac544f8c 2019-01-13 stsp uint8_t *buf;
1424 ac544f8c 2019-01-13 stsp
1425 ac544f8c 2019-01-13 stsp if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1426 ac544f8c 2019-01-13 stsp return got_error(GOT_ERR_NO_SPACE);
1427 ac544f8c 2019-01-13 stsp
1428 ac544f8c 2019-01-13 stsp buf = malloc(sizeof(iblob) + size);
1429 ac544f8c 2019-01-13 stsp if (buf == NULL)
1430 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1431 ff6b18f8 2018-04-24 stsp
1432 ac544f8c 2019-01-13 stsp memcpy(buf, &iblob, sizeof(iblob));
1433 ac544f8c 2019-01-13 stsp memcpy(buf + sizeof(iblob), data, size);
1434 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1435 ac544f8c 2019-01-13 stsp sizeof(iblob) + size) == -1) {
1436 ac544f8c 2019-01-13 stsp free(buf);
1437 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1438 ac544f8c 2019-01-13 stsp }
1439 ac544f8c 2019-01-13 stsp free(buf);
1440 ac544f8c 2019-01-13 stsp } else {
1441 ac544f8c 2019-01-13 stsp /* Data has already been written to file descriptor. */
1442 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1443 ac544f8c 2019-01-13 stsp sizeof(iblob)) == -1)
1444 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1445 ac544f8c 2019-01-13 stsp }
1446 ac544f8c 2019-01-13 stsp
1447 ac544f8c 2019-01-13 stsp
1448 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
1449 ff6b18f8 2018-04-24 stsp }
1450 ff6b18f8 2018-04-24 stsp
1451 ff6b18f8 2018-04-24 stsp const struct got_error *
1452 ac544f8c 2019-01-13 stsp got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1453 ac544f8c 2019-01-13 stsp struct imsgbuf *ibuf)
1454 ff6b18f8 2018-04-24 stsp {
1455 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1456 ff6b18f8 2018-04-24 stsp struct imsg imsg;
1457 291624d8 2018-11-07 stsp struct got_imsg_blob *iblob;
1458 ff6b18f8 2018-04-24 stsp size_t datalen;
1459 ff6b18f8 2018-04-24 stsp
1460 ac544f8c 2019-01-13 stsp *outbuf = NULL;
1461 ac544f8c 2019-01-13 stsp
1462 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1463 ff6b18f8 2018-04-24 stsp if (err)
1464 ff6b18f8 2018-04-24 stsp return err;
1465 ff6b18f8 2018-04-24 stsp
1466 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1467 ff6b18f8 2018-04-24 stsp
1468 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
1469 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
1470 ac544f8c 2019-01-13 stsp if (datalen < sizeof(*iblob)) {
1471 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1472 18336eed 2018-11-04 stsp break;
1473 18336eed 2018-11-04 stsp }
1474 291624d8 2018-11-07 stsp iblob = imsg.data;
1475 291624d8 2018-11-07 stsp *size = iblob->size;
1476 ebc55e2d 2018-12-24 stsp *hdrlen = iblob->hdrlen;
1477 ac544f8c 2019-01-13 stsp
1478 ac544f8c 2019-01-13 stsp if (datalen == sizeof(*iblob)) {
1479 ac544f8c 2019-01-13 stsp /* Data has been written to file descriptor. */
1480 ac544f8c 2019-01-13 stsp break;
1481 ac544f8c 2019-01-13 stsp }
1482 ac544f8c 2019-01-13 stsp
1483 ac544f8c 2019-01-13 stsp if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1484 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1485 ac544f8c 2019-01-13 stsp break;
1486 ac544f8c 2019-01-13 stsp }
1487 ac544f8c 2019-01-13 stsp
1488 ac544f8c 2019-01-13 stsp *outbuf = malloc(*size);
1489 ac544f8c 2019-01-13 stsp if (*outbuf == NULL) {
1490 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1491 ac544f8c 2019-01-13 stsp break;
1492 ac544f8c 2019-01-13 stsp }
1493 ac544f8c 2019-01-13 stsp memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1494 f4a881ce 2018-11-17 stsp break;
1495 f4a881ce 2018-11-17 stsp default:
1496 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1497 f4a881ce 2018-11-17 stsp break;
1498 f4a881ce 2018-11-17 stsp }
1499 f4a881ce 2018-11-17 stsp
1500 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
1501 f4a881ce 2018-11-17 stsp
1502 f4a881ce 2018-11-17 stsp return err;
1503 f4a881ce 2018-11-17 stsp }
1504 f4a881ce 2018-11-17 stsp
1505 f4a881ce 2018-11-17 stsp static const struct got_error *
1506 f4a881ce 2018-11-17 stsp send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1507 f4a881ce 2018-11-17 stsp {
1508 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1509 f4a881ce 2018-11-17 stsp size_t offset, remain;
1510 f4a881ce 2018-11-17 stsp
1511 f4a881ce 2018-11-17 stsp offset = 0;
1512 f4a881ce 2018-11-17 stsp remain = tagmsg_len;
1513 f4a881ce 2018-11-17 stsp while (remain > 0) {
1514 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1515 f4a881ce 2018-11-17 stsp
1516 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1517 f4a881ce 2018-11-17 stsp tag->tagmsg + offset, n) == -1) {
1518 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1519 f4a881ce 2018-11-17 stsp break;
1520 f4a881ce 2018-11-17 stsp }
1521 f4a881ce 2018-11-17 stsp
1522 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1523 f4a881ce 2018-11-17 stsp if (err)
1524 f4a881ce 2018-11-17 stsp break;
1525 f4a881ce 2018-11-17 stsp
1526 f4a881ce 2018-11-17 stsp offset += n;
1527 f4a881ce 2018-11-17 stsp remain -= n;
1528 f4a881ce 2018-11-17 stsp }
1529 f4a881ce 2018-11-17 stsp
1530 f4a881ce 2018-11-17 stsp return err;
1531 f4a881ce 2018-11-17 stsp }
1532 f4a881ce 2018-11-17 stsp
1533 f4a881ce 2018-11-17 stsp const struct got_error *
1534 f4a881ce 2018-11-17 stsp got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1535 f4a881ce 2018-11-17 stsp {
1536 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1537 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1538 f4a881ce 2018-11-17 stsp uint8_t *buf;
1539 f4a881ce 2018-11-17 stsp size_t len, total;
1540 f4a881ce 2018-11-17 stsp size_t tag_len = strlen(tag->tag);
1541 f4a881ce 2018-11-17 stsp size_t tagger_len = strlen(tag->tagger);
1542 f4a881ce 2018-11-17 stsp size_t tagmsg_len = strlen(tag->tagmsg);
1543 f4a881ce 2018-11-17 stsp
1544 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1545 f4a881ce 2018-11-17 stsp
1546 f4a881ce 2018-11-17 stsp buf = malloc(total);
1547 f4a881ce 2018-11-17 stsp if (buf == NULL)
1548 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1549 f4a881ce 2018-11-17 stsp
1550 f4a881ce 2018-11-17 stsp itag = (struct got_imsg_tag_object *)buf;
1551 f4a881ce 2018-11-17 stsp memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1552 f4a881ce 2018-11-17 stsp itag->obj_type = tag->obj_type;
1553 f4a881ce 2018-11-17 stsp itag->tag_len = tag_len;
1554 f4a881ce 2018-11-17 stsp itag->tagger_len = tagger_len;
1555 f4a881ce 2018-11-17 stsp itag->tagger_time = tag->tagger_time;
1556 f4a881ce 2018-11-17 stsp itag->tagger_gmtoff = tag->tagger_gmtoff;
1557 f4a881ce 2018-11-17 stsp itag->tagmsg_len = tagmsg_len;
1558 f4a881ce 2018-11-17 stsp
1559 f4a881ce 2018-11-17 stsp len = sizeof(*itag);
1560 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tag, tag_len);
1561 f4a881ce 2018-11-17 stsp len += tag_len;
1562 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tagger, tagger_len);
1563 f4a881ce 2018-11-17 stsp len += tagger_len;
1564 f4a881ce 2018-11-17 stsp
1565 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1566 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG");
1567 f4a881ce 2018-11-17 stsp goto done;
1568 f4a881ce 2018-11-17 stsp }
1569 f4a881ce 2018-11-17 stsp
1570 f4a881ce 2018-11-17 stsp if (tagmsg_len == 0 ||
1571 f4a881ce 2018-11-17 stsp tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1572 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1573 f4a881ce 2018-11-17 stsp if (err)
1574 f4a881ce 2018-11-17 stsp goto done;
1575 f4a881ce 2018-11-17 stsp }
1576 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
1577 f4a881ce 2018-11-17 stsp done:
1578 f4a881ce 2018-11-17 stsp free(buf);
1579 f4a881ce 2018-11-17 stsp return err;
1580 f4a881ce 2018-11-17 stsp }
1581 f4a881ce 2018-11-17 stsp
1582 f4a881ce 2018-11-17 stsp const struct got_error *
1583 f4a881ce 2018-11-17 stsp got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1584 f4a881ce 2018-11-17 stsp {
1585 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1586 f4a881ce 2018-11-17 stsp struct imsg imsg;
1587 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1588 f4a881ce 2018-11-17 stsp size_t len, datalen;
1589 f4a881ce 2018-11-17 stsp const size_t min_datalen =
1590 f4a881ce 2018-11-17 stsp MIN(sizeof(struct got_imsg_error),
1591 f4a881ce 2018-11-17 stsp sizeof(struct got_imsg_tag_object));
1592 f4a881ce 2018-11-17 stsp
1593 f4a881ce 2018-11-17 stsp *tag = NULL;
1594 f4a881ce 2018-11-17 stsp
1595 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1596 f4a881ce 2018-11-17 stsp if (err)
1597 f4a881ce 2018-11-17 stsp return err;
1598 f4a881ce 2018-11-17 stsp
1599 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1600 f4a881ce 2018-11-17 stsp len = 0;
1601 f4a881ce 2018-11-17 stsp
1602 f4a881ce 2018-11-17 stsp switch (imsg.hdr.type) {
1603 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG:
1604 f4a881ce 2018-11-17 stsp if (datalen < sizeof(*itag)) {
1605 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1606 f4a881ce 2018-11-17 stsp break;
1607 f4a881ce 2018-11-17 stsp }
1608 f4a881ce 2018-11-17 stsp itag = imsg.data;
1609 f4a881ce 2018-11-17 stsp if (datalen != sizeof(*itag) + itag->tag_len +
1610 f4a881ce 2018-11-17 stsp itag->tagger_len) {
1611 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1612 f4a881ce 2018-11-17 stsp break;
1613 f4a881ce 2018-11-17 stsp }
1614 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
1615 f4a881ce 2018-11-17 stsp
1616 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
1617 f4a881ce 2018-11-17 stsp if (*tag == NULL) {
1618 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1619 f4a881ce 2018-11-17 stsp break;
1620 f4a881ce 2018-11-17 stsp }
1621 f4a881ce 2018-11-17 stsp
1622 f4a881ce 2018-11-17 stsp memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1623 f4a881ce 2018-11-17 stsp
1624 f4a881ce 2018-11-17 stsp if (itag->tag_len == 0) {
1625 f4a881ce 2018-11-17 stsp (*tag)->tag = strdup("");
1626 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1627 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1628 f4a881ce 2018-11-17 stsp break;
1629 f4a881ce 2018-11-17 stsp }
1630 f4a881ce 2018-11-17 stsp } else {
1631 f4a881ce 2018-11-17 stsp (*tag)->tag = malloc(itag->tag_len + 1);
1632 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1633 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1634 f4a881ce 2018-11-17 stsp break;
1635 f4a881ce 2018-11-17 stsp }
1636 f4a881ce 2018-11-17 stsp memcpy((*tag)->tag, imsg.data + len,
1637 f4a881ce 2018-11-17 stsp itag->tag_len);
1638 f4a881ce 2018-11-17 stsp (*tag)->tag[itag->tag_len] = '\0';
1639 f4a881ce 2018-11-17 stsp }
1640 f4a881ce 2018-11-17 stsp len += itag->tag_len;
1641 f4a881ce 2018-11-17 stsp
1642 f4a881ce 2018-11-17 stsp (*tag)->obj_type = itag->obj_type;
1643 f4a881ce 2018-11-17 stsp (*tag)->tagger_time = itag->tagger_time;
1644 f4a881ce 2018-11-17 stsp (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1645 f4a881ce 2018-11-17 stsp
1646 f4a881ce 2018-11-17 stsp if (itag->tagger_len == 0) {
1647 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup("");
1648 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1649 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1650 f4a881ce 2018-11-17 stsp break;
1651 f4a881ce 2018-11-17 stsp }
1652 f4a881ce 2018-11-17 stsp } else {
1653 f4a881ce 2018-11-17 stsp (*tag)->tagger = malloc(itag->tagger_len + 1);
1654 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1655 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1656 f4a881ce 2018-11-17 stsp break;
1657 f4a881ce 2018-11-17 stsp }
1658 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagger, imsg.data + len,
1659 f4a881ce 2018-11-17 stsp itag->tagger_len);
1660 f4a881ce 2018-11-17 stsp (*tag)->tagger[itag->tagger_len] = '\0';
1661 f4a881ce 2018-11-17 stsp }
1662 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1663 f4a881ce 2018-11-17 stsp
1664 f4a881ce 2018-11-17 stsp if (itag->tagmsg_len == 0) {
1665 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strdup("");
1666 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1667 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1668 f4a881ce 2018-11-17 stsp break;
1669 f4a881ce 2018-11-17 stsp }
1670 f4a881ce 2018-11-17 stsp } else {
1671 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1672 f4a881ce 2018-11-17 stsp
1673 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1674 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1675 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1676 f4a881ce 2018-11-17 stsp break;
1677 f4a881ce 2018-11-17 stsp }
1678 f4a881ce 2018-11-17 stsp while (remain > 0) {
1679 f4a881ce 2018-11-17 stsp struct imsg imsg_log;
1680 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1681 f4a881ce 2018-11-17 stsp remain);
1682 f4a881ce 2018-11-17 stsp
1683 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1684 f4a881ce 2018-11-17 stsp if (err)
1685 f4a881ce 2018-11-17 stsp return err;
1686 f4a881ce 2018-11-17 stsp
1687 f4a881ce 2018-11-17 stsp if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1688 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1689 f4a881ce 2018-11-17 stsp
1690 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1691 f4a881ce 2018-11-17 stsp n);
1692 f4a881ce 2018-11-17 stsp imsg_free(&imsg_log);
1693 f4a881ce 2018-11-17 stsp offset += n;
1694 f4a881ce 2018-11-17 stsp remain -= n;
1695 f4a881ce 2018-11-17 stsp }
1696 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1697 f4a881ce 2018-11-17 stsp }
1698 f4a881ce 2018-11-17 stsp
1699 ff6b18f8 2018-04-24 stsp break;
1700 ff6b18f8 2018-04-24 stsp default:
1701 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1702 ff6b18f8 2018-04-24 stsp break;
1703 e033d803 2018-04-23 stsp }
1704 e033d803 2018-04-23 stsp
1705 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1706 ff6b18f8 2018-04-24 stsp
1707 2178c42e 2018-04-22 stsp return err;
1708 2178c42e 2018-04-22 stsp }
1709 876c234b 2018-09-10 stsp
1710 876c234b 2018-09-10 stsp const struct got_error *
1711 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1712 876c234b 2018-09-10 stsp struct got_packidx *packidx)
1713 876c234b 2018-09-10 stsp {
1714 41496140 2019-02-21 stsp const struct got_error *err = NULL;
1715 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1716 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1717 876c234b 2018-09-10 stsp int fd;
1718 876c234b 2018-09-10 stsp
1719 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
1720 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
1721 876c234b 2018-09-10 stsp if (fd == -1)
1722 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1723 876c234b 2018-09-10 stsp
1724 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1725 41496140 2019-02-21 stsp sizeof(ipackidx)) == -1) {
1726 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACKIDX");
1727 41496140 2019-02-21 stsp close(fd);
1728 41496140 2019-02-21 stsp return err;
1729 41496140 2019-02-21 stsp }
1730 876c234b 2018-09-10 stsp
1731 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
1732 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1733 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
1734 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
1735 876c234b 2018-09-10 stsp
1736 876c234b 2018-09-10 stsp fd = dup(pack->fd);
1737 876c234b 2018-09-10 stsp if (fd == -1)
1738 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1739 876c234b 2018-09-10 stsp
1740 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1741 41496140 2019-02-21 stsp == -1) {
1742 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACK");
1743 41496140 2019-02-21 stsp close(fd);
1744 41496140 2019-02-21 stsp return err;
1745 41496140 2019-02-21 stsp }
1746 876c234b 2018-09-10 stsp
1747 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1748 876c234b 2018-09-10 stsp }
1749 876c234b 2018-09-10 stsp
1750 876c234b 2018-09-10 stsp const struct got_error *
1751 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1752 106807b4 2018-09-15 stsp struct got_object_id *id)
1753 876c234b 2018-09-10 stsp {
1754 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
1755 876c234b 2018-09-10 stsp
1756 876c234b 2018-09-10 stsp iobj.idx = idx;
1757 106807b4 2018-09-15 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1758 876c234b 2018-09-10 stsp
1759 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1760 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
1761 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose "
1762 230a42bd 2019-05-11 jcs "PACKED_OBJECT_REQUEST");
1763 aba9c984 2019-09-08 stsp
1764 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1765 aba9c984 2019-09-08 stsp }
1766 aba9c984 2019-09-08 stsp
1767 aba9c984 2019-09-08 stsp const struct got_error *
1768 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1769 aba9c984 2019-09-08 stsp {
1770 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1771 aba9c984 2019-09-08 stsp
1772 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1773 aba9c984 2019-09-08 stsp NULL, 0) == -1) {
1774 aba9c984 2019-09-08 stsp err = got_error_from_errno("imsg_compose "
1775 aba9c984 2019-09-08 stsp "GITCONFIG_PARSE_REQUEST");
1776 aba9c984 2019-09-08 stsp close(fd);
1777 aba9c984 2019-09-08 stsp return err;
1778 aba9c984 2019-09-08 stsp }
1779 aba9c984 2019-09-08 stsp
1780 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1781 aba9c984 2019-09-08 stsp }
1782 aba9c984 2019-09-08 stsp
1783 aba9c984 2019-09-08 stsp const struct got_error *
1784 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1785 aba9c984 2019-09-08 stsp {
1786 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1787 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1788 aba9c984 2019-09-08 stsp NULL, 0) == -1)
1789 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1790 aba9c984 2019-09-08 stsp "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1791 aba9c984 2019-09-08 stsp
1792 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1793 aba9c984 2019-09-08 stsp }
1794 aba9c984 2019-09-08 stsp
1795 aba9c984 2019-09-08 stsp const struct got_error *
1796 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1797 aba9c984 2019-09-08 stsp {
1798 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1799 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1800 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1801 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_NAME_REQUEST");
1802 aba9c984 2019-09-08 stsp
1803 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1804 aba9c984 2019-09-08 stsp }
1805 aba9c984 2019-09-08 stsp
1806 aba9c984 2019-09-08 stsp const struct got_error *
1807 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1808 aba9c984 2019-09-08 stsp {
1809 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1810 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1811 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1812 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1813 cd95becd 2019-11-29 stsp
1814 cd95becd 2019-11-29 stsp return flush_imsg(ibuf);
1815 cd95becd 2019-11-29 stsp }
1816 cd95becd 2019-11-29 stsp
1817 cd95becd 2019-11-29 stsp const struct got_error *
1818 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1819 cd95becd 2019-11-29 stsp {
1820 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf,
1821 cd95becd 2019-11-29 stsp GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1822 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose "
1823 cd95becd 2019-11-29 stsp "GITCONFIG_REMOTE_REQUEST");
1824 aba9c984 2019-09-08 stsp
1825 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1826 aba9c984 2019-09-08 stsp }
1827 aba9c984 2019-09-08 stsp
1828 aba9c984 2019-09-08 stsp const struct got_error *
1829 9a1cc63f 2020-02-03 stsp got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1830 9a1cc63f 2020-02-03 stsp {
1831 9a1cc63f 2020-02-03 stsp if (imsg_compose(ibuf,
1832 9a1cc63f 2020-02-03 stsp GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1833 9a1cc63f 2020-02-03 stsp return got_error_from_errno("imsg_compose "
1834 9a1cc63f 2020-02-03 stsp "GITCONFIG_OWNER_REQUEST");
1835 9a1cc63f 2020-02-03 stsp
1836 9a1cc63f 2020-02-03 stsp return flush_imsg(ibuf);
1837 9a1cc63f 2020-02-03 stsp }
1838 9a1cc63f 2020-02-03 stsp
1839 9a1cc63f 2020-02-03 stsp const struct got_error *
1840 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1841 aba9c984 2019-09-08 stsp {
1842 aba9c984 2019-09-08 stsp size_t len = value ? strlen(value) + 1 : 0;
1843 aba9c984 2019-09-08 stsp
1844 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1845 aba9c984 2019-09-08 stsp value, len) == -1)
1846 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1847 aba9c984 2019-09-08 stsp
1848 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1849 aba9c984 2019-09-08 stsp }
1850 aba9c984 2019-09-08 stsp
1851 aba9c984 2019-09-08 stsp const struct got_error *
1852 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1853 aba9c984 2019-09-08 stsp {
1854 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1855 aba9c984 2019-09-08 stsp struct imsg imsg;
1856 aba9c984 2019-09-08 stsp size_t datalen;
1857 aba9c984 2019-09-08 stsp const size_t min_datalen = 0;
1858 aba9c984 2019-09-08 stsp
1859 aba9c984 2019-09-08 stsp *str = NULL;
1860 aba9c984 2019-09-08 stsp
1861 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1862 aba9c984 2019-09-08 stsp if (err)
1863 aba9c984 2019-09-08 stsp return err;
1864 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1865 aba9c984 2019-09-08 stsp
1866 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1867 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_STR_VAL:
1868 aba9c984 2019-09-08 stsp if (datalen == 0)
1869 aba9c984 2019-09-08 stsp break;
1870 aba9c984 2019-09-08 stsp *str = malloc(datalen);
1871 aba9c984 2019-09-08 stsp if (*str == NULL) {
1872 aba9c984 2019-09-08 stsp err = got_error_from_errno("malloc");
1873 aba9c984 2019-09-08 stsp break;
1874 aba9c984 2019-09-08 stsp }
1875 aba9c984 2019-09-08 stsp if (strlcpy(*str, imsg.data, datalen) >= datalen)
1876 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_NO_SPACE);
1877 aba9c984 2019-09-08 stsp break;
1878 aba9c984 2019-09-08 stsp default:
1879 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1880 aba9c984 2019-09-08 stsp break;
1881 aba9c984 2019-09-08 stsp }
1882 876c234b 2018-09-10 stsp
1883 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1884 aba9c984 2019-09-08 stsp return err;
1885 aba9c984 2019-09-08 stsp }
1886 aba9c984 2019-09-08 stsp
1887 aba9c984 2019-09-08 stsp const struct got_error *
1888 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1889 aba9c984 2019-09-08 stsp {
1890 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1891 aba9c984 2019-09-08 stsp &value, sizeof(value)) == -1)
1892 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1893 aba9c984 2019-09-08 stsp
1894 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1895 63219cd2 2019-01-04 stsp }
1896 63219cd2 2019-01-04 stsp
1897 63219cd2 2019-01-04 stsp const struct got_error *
1898 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1899 aba9c984 2019-09-08 stsp {
1900 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1901 aba9c984 2019-09-08 stsp struct imsg imsg;
1902 aba9c984 2019-09-08 stsp size_t datalen;
1903 aba9c984 2019-09-08 stsp const size_t min_datalen =
1904 aba9c984 2019-09-08 stsp MIN(sizeof(struct got_imsg_error), sizeof(int));
1905 aba9c984 2019-09-08 stsp
1906 aba9c984 2019-09-08 stsp *val = 0;
1907 aba9c984 2019-09-08 stsp
1908 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1909 aba9c984 2019-09-08 stsp if (err)
1910 aba9c984 2019-09-08 stsp return err;
1911 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1912 aba9c984 2019-09-08 stsp
1913 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1914 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_INT_VAL:
1915 aba9c984 2019-09-08 stsp if (datalen != sizeof(*val)) {
1916 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1917 aba9c984 2019-09-08 stsp break;
1918 aba9c984 2019-09-08 stsp }
1919 aba9c984 2019-09-08 stsp memcpy(val, imsg.data, sizeof(*val));
1920 cd95becd 2019-11-29 stsp break;
1921 cd95becd 2019-11-29 stsp default:
1922 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1923 cd95becd 2019-11-29 stsp break;
1924 cd95becd 2019-11-29 stsp }
1925 cd95becd 2019-11-29 stsp
1926 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1927 cd95becd 2019-11-29 stsp return err;
1928 cd95becd 2019-11-29 stsp }
1929 cd95becd 2019-11-29 stsp
1930 cd95becd 2019-11-29 stsp const struct got_error *
1931 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1932 cd95becd 2019-11-29 stsp struct got_remote_repo *remotes, int nremotes)
1933 cd95becd 2019-11-29 stsp {
1934 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1935 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
1936 cd95becd 2019-11-29 stsp int i;
1937 cd95becd 2019-11-29 stsp
1938 cd95becd 2019-11-29 stsp iremotes.nremotes = nremotes;
1939 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1940 cd95becd 2019-11-29 stsp &iremotes, sizeof(iremotes)) == -1)
1941 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1942 cd95becd 2019-11-29 stsp
1943 cd95becd 2019-11-29 stsp err = flush_imsg(ibuf);
1944 cd95becd 2019-11-29 stsp imsg_clear(ibuf);
1945 cd95becd 2019-11-29 stsp if (err)
1946 cd95becd 2019-11-29 stsp return err;
1947 cd95becd 2019-11-29 stsp
1948 cd95becd 2019-11-29 stsp for (i = 0; i < nremotes; i++) {
1949 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
1950 cd95becd 2019-11-29 stsp size_t len = sizeof(iremote);
1951 cd95becd 2019-11-29 stsp struct ibuf *wbuf;
1952 cd95becd 2019-11-29 stsp
1953 fe6043b8 2020-03-20 stsp iremote.mirror_references = remotes[i].mirror_references;
1954 cd95becd 2019-11-29 stsp iremote.name_len = strlen(remotes[i].name);
1955 cd95becd 2019-11-29 stsp len += iremote.name_len;
1956 cd95becd 2019-11-29 stsp iremote.url_len = strlen(remotes[i].url);
1957 cd95becd 2019-11-29 stsp len += iremote.url_len;
1958 cd95becd 2019-11-29 stsp
1959 cd95becd 2019-11-29 stsp wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1960 cd95becd 2019-11-29 stsp if (wbuf == NULL)
1961 cd95becd 2019-11-29 stsp return got_error_from_errno(
1962 cd95becd 2019-11-29 stsp "imsg_create GITCONFIG_REMOTE");
1963 cd95becd 2019-11-29 stsp
1964 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1965 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1966 b364b1c2 2020-03-20 stsp "imsg_add GITCONFIG_REMOTE");
1967 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1968 cd95becd 2019-11-29 stsp return err;
1969 cd95becd 2019-11-29 stsp }
1970 cd95becd 2019-11-29 stsp
1971 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1972 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1973 b364b1c2 2020-03-20 stsp "imsg_add GITCONFIG_REMOTE");
1974 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1975 cd95becd 2019-11-29 stsp return err;
1976 cd95becd 2019-11-29 stsp }
1977 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1978 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1979 b364b1c2 2020-03-20 stsp "imsg_add GITCONFIG_REMOTE");
1980 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1981 cd95becd 2019-11-29 stsp return err;
1982 cd95becd 2019-11-29 stsp }
1983 cd95becd 2019-11-29 stsp
1984 cd95becd 2019-11-29 stsp wbuf->fd = -1;
1985 cd95becd 2019-11-29 stsp imsg_close(ibuf, wbuf);
1986 cd95becd 2019-11-29 stsp err = flush_imsg(ibuf);
1987 cd95becd 2019-11-29 stsp if (err)
1988 cd95becd 2019-11-29 stsp return err;
1989 cd95becd 2019-11-29 stsp }
1990 cd95becd 2019-11-29 stsp
1991 cd95becd 2019-11-29 stsp return NULL;
1992 cd95becd 2019-11-29 stsp }
1993 cd95becd 2019-11-29 stsp
1994 cd95becd 2019-11-29 stsp const struct got_error *
1995 cd95becd 2019-11-29 stsp got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1996 cd95becd 2019-11-29 stsp int *nremotes, struct imsgbuf *ibuf)
1997 cd95becd 2019-11-29 stsp {
1998 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1999 cd95becd 2019-11-29 stsp struct imsg imsg;
2000 cd95becd 2019-11-29 stsp size_t datalen;
2001 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
2002 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
2003 cd95becd 2019-11-29 stsp
2004 cd95becd 2019-11-29 stsp *remotes = NULL;
2005 cd95becd 2019-11-29 stsp *nremotes = 0;
2006 d669b9c9 2020-02-22 stsp iremotes.nremotes = 0;
2007 cd95becd 2019-11-29 stsp
2008 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2009 cd95becd 2019-11-29 stsp if (err)
2010 cd95becd 2019-11-29 stsp return err;
2011 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2012 cd95becd 2019-11-29 stsp
2013 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
2014 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES:
2015 cd95becd 2019-11-29 stsp if (datalen != sizeof(iremotes)) {
2016 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2017 cd95becd 2019-11-29 stsp break;
2018 cd95becd 2019-11-29 stsp }
2019 cd95becd 2019-11-29 stsp memcpy(&iremotes, imsg.data, sizeof(iremotes));
2020 cd95becd 2019-11-29 stsp if (iremotes.nremotes == 0) {
2021 cd95becd 2019-11-29 stsp imsg_free(&imsg);
2022 cd95becd 2019-11-29 stsp return NULL;
2023 cd95becd 2019-11-29 stsp }
2024 aba9c984 2019-09-08 stsp break;
2025 aba9c984 2019-09-08 stsp default:
2026 54b1c5b5 2020-02-22 stsp imsg_free(&imsg);
2027 54b1c5b5 2020-02-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
2028 aba9c984 2019-09-08 stsp }
2029 aba9c984 2019-09-08 stsp
2030 aba9c984 2019-09-08 stsp imsg_free(&imsg);
2031 cd95becd 2019-11-29 stsp
2032 5146eb39 2020-03-20 stsp *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2033 cd95becd 2019-11-29 stsp if (*remotes == NULL)
2034 cd95becd 2019-11-29 stsp return got_error_from_errno("recallocarray");
2035 cd95becd 2019-11-29 stsp
2036 cd95becd 2019-11-29 stsp while (*nremotes < iremotes.nremotes) {
2037 cd95becd 2019-11-29 stsp struct got_remote_repo *remote;
2038 cd95becd 2019-11-29 stsp
2039 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2040 cd95becd 2019-11-29 stsp if (err)
2041 cd95becd 2019-11-29 stsp break;
2042 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2043 cd95becd 2019-11-29 stsp
2044 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
2045 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTE:
2046 cd95becd 2019-11-29 stsp remote = &(*remotes)[*nremotes];
2047 cd95becd 2019-11-29 stsp if (datalen < sizeof(iremote)) {
2048 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2049 cd95becd 2019-11-29 stsp break;
2050 cd95becd 2019-11-29 stsp }
2051 cd95becd 2019-11-29 stsp memcpy(&iremote, imsg.data, sizeof(iremote));
2052 cd95becd 2019-11-29 stsp if (iremote.name_len == 0 || iremote.url_len == 0 ||
2053 cd95becd 2019-11-29 stsp (sizeof(iremote) + iremote.name_len +
2054 cd95becd 2019-11-29 stsp iremote.url_len) > datalen) {
2055 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2056 cd95becd 2019-11-29 stsp break;
2057 cd95becd 2019-11-29 stsp }
2058 cd95becd 2019-11-29 stsp remote->name = strndup(imsg.data + sizeof(iremote),
2059 cd95becd 2019-11-29 stsp iremote.name_len);
2060 cd95becd 2019-11-29 stsp if (remote->name == NULL) {
2061 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
2062 cd95becd 2019-11-29 stsp break;
2063 cd95becd 2019-11-29 stsp }
2064 cd95becd 2019-11-29 stsp remote->url = strndup(imsg.data + sizeof(iremote) +
2065 cd95becd 2019-11-29 stsp iremote.name_len, iremote.url_len);
2066 cd95becd 2019-11-29 stsp if (remote->url == NULL) {
2067 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
2068 cd95becd 2019-11-29 stsp free(remote->name);
2069 cd95becd 2019-11-29 stsp break;
2070 cd95becd 2019-11-29 stsp }
2071 469dd726 2020-03-20 stsp remote->mirror_references = iremote.mirror_references;
2072 cd95becd 2019-11-29 stsp (*nremotes)++;
2073 cd95becd 2019-11-29 stsp break;
2074 cd95becd 2019-11-29 stsp default:
2075 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2076 cd95becd 2019-11-29 stsp break;
2077 cd95becd 2019-11-29 stsp }
2078 cd95becd 2019-11-29 stsp
2079 cd95becd 2019-11-29 stsp imsg_free(&imsg);
2080 cd95becd 2019-11-29 stsp if (err)
2081 cd95becd 2019-11-29 stsp break;
2082 cd95becd 2019-11-29 stsp }
2083 cd95becd 2019-11-29 stsp
2084 cd95becd 2019-11-29 stsp if (err) {
2085 cd95becd 2019-11-29 stsp int i;
2086 cd95becd 2019-11-29 stsp for (i = 0; i < *nremotes; i++) {
2087 cd95becd 2019-11-29 stsp free((*remotes)[i].name);
2088 cd95becd 2019-11-29 stsp free((*remotes)[i].url);
2089 cd95becd 2019-11-29 stsp }
2090 cd95becd 2019-11-29 stsp free(*remotes);
2091 cd95becd 2019-11-29 stsp *remotes = NULL;
2092 cd95becd 2019-11-29 stsp *nremotes = 0;
2093 cd95becd 2019-11-29 stsp }
2094 aba9c984 2019-09-08 stsp return err;
2095 ca6e02ac 2020-01-07 stsp }
2096 ca6e02ac 2020-01-07 stsp
2097 ca6e02ac 2020-01-07 stsp const struct got_error *
2098 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2099 ca6e02ac 2020-01-07 stsp struct got_object_id *id, int idx, const char *path)
2100 ca6e02ac 2020-01-07 stsp {
2101 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2102 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
2103 ca6e02ac 2020-01-07 stsp size_t path_len = strlen(path) + 1;
2104 ca6e02ac 2020-01-07 stsp
2105 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2106 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_traversal_request) + path_len);
2107 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
2108 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
2109 ca6e02ac 2020-01-07 stsp "imsg_create COMMIT_TRAVERSAL_REQUEST");
2110 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2111 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2112 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2113 ca6e02ac 2020-01-07 stsp return err;
2114 ca6e02ac 2020-01-07 stsp }
2115 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2116 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2117 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2118 ca6e02ac 2020-01-07 stsp return err;
2119 ca6e02ac 2020-01-07 stsp }
2120 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, path, path_len) == -1) {
2121 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2122 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2123 ca6e02ac 2020-01-07 stsp return err;
2124 ca6e02ac 2020-01-07 stsp }
2125 ca6e02ac 2020-01-07 stsp
2126 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
2127 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
2128 ca6e02ac 2020-01-07 stsp
2129 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2130 aba9c984 2019-09-08 stsp }
2131 aba9c984 2019-09-08 stsp
2132 aba9c984 2019-09-08 stsp const struct got_error *
2133 ca6e02ac 2020-01-07 stsp got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2134 ca6e02ac 2020-01-07 stsp size_t ncommits, struct imsgbuf *ibuf)
2135 ca6e02ac 2020-01-07 stsp {
2136 ca6e02ac 2020-01-07 stsp const struct got_error *err;
2137 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
2138 ca6e02ac 2020-01-07 stsp int i;
2139 ca6e02ac 2020-01-07 stsp
2140 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2141 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_traversed_commits) +
2142 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH);
2143 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
2144 ca6e02ac 2020-01-07 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2145 ca6e02ac 2020-01-07 stsp
2146 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2147 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2148 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2149 ca6e02ac 2020-01-07 stsp return err;
2150 ca6e02ac 2020-01-07 stsp }
2151 ca6e02ac 2020-01-07 stsp for (i = 0; i < ncommits; i++) {
2152 ca6e02ac 2020-01-07 stsp struct got_object_id *id = &commit_ids[i];
2153 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2154 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
2155 ca6e02ac 2020-01-07 stsp "imsg_add TRAVERSED_COMMITS");
2156 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2157 ca6e02ac 2020-01-07 stsp return err;
2158 ca6e02ac 2020-01-07 stsp }
2159 ca6e02ac 2020-01-07 stsp }
2160 ca6e02ac 2020-01-07 stsp
2161 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
2162 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
2163 ca6e02ac 2020-01-07 stsp
2164 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2165 ca6e02ac 2020-01-07 stsp }
2166 ca6e02ac 2020-01-07 stsp
2167 ca6e02ac 2020-01-07 stsp const struct got_error *
2168 ca6e02ac 2020-01-07 stsp got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2169 ca6e02ac 2020-01-07 stsp struct got_object_id **changed_commit_id,
2170 ca6e02ac 2020-01-07 stsp struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2171 ca6e02ac 2020-01-07 stsp {
2172 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2173 ca6e02ac 2020-01-07 stsp struct imsg imsg;
2174 ca6e02ac 2020-01-07 stsp struct got_imsg_traversed_commits *icommits;
2175 ca6e02ac 2020-01-07 stsp size_t datalen;
2176 ca6e02ac 2020-01-07 stsp int i, done = 0;
2177 ca6e02ac 2020-01-07 stsp
2178 ca6e02ac 2020-01-07 stsp *changed_commit = NULL;
2179 ca6e02ac 2020-01-07 stsp *changed_commit_id = NULL;
2180 ca6e02ac 2020-01-07 stsp
2181 ca6e02ac 2020-01-07 stsp while (!done) {
2182 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2183 ca6e02ac 2020-01-07 stsp if (err)
2184 ca6e02ac 2020-01-07 stsp return err;
2185 ca6e02ac 2020-01-07 stsp
2186 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2187 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
2188 ca6e02ac 2020-01-07 stsp case GOT_IMSG_TRAVERSED_COMMITS:
2189 ca6e02ac 2020-01-07 stsp icommits = imsg.data;
2190 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommits) +
2191 ca6e02ac 2020-01-07 stsp icommits->ncommits * SHA1_DIGEST_LENGTH) {
2192 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2193 ca6e02ac 2020-01-07 stsp break;
2194 ca6e02ac 2020-01-07 stsp }
2195 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommits->ncommits; i++) {
2196 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
2197 ca6e02ac 2020-01-07 stsp uint8_t *sha1 = (uint8_t *)imsg.data +
2198 ca6e02ac 2020-01-07 stsp sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2199 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
2200 ca6e02ac 2020-01-07 stsp if (err)
2201 ca6e02ac 2020-01-07 stsp break;
2202 ca6e02ac 2020-01-07 stsp memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2203 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2204 ca6e02ac 2020-01-07 stsp
2205 ca6e02ac 2020-01-07 stsp /* The last commit may contain a change. */
2206 ca6e02ac 2020-01-07 stsp if (i == icommits->ncommits - 1) {
2207 ca6e02ac 2020-01-07 stsp *changed_commit_id =
2208 ca6e02ac 2020-01-07 stsp got_object_id_dup(qid->id);
2209 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2210 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
2211 ca6e02ac 2020-01-07 stsp "got_object_id_dup");
2212 ca6e02ac 2020-01-07 stsp break;
2213 ca6e02ac 2020-01-07 stsp }
2214 ca6e02ac 2020-01-07 stsp }
2215 ca6e02ac 2020-01-07 stsp }
2216 ca6e02ac 2020-01-07 stsp break;
2217 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
2218 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2219 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2220 ca6e02ac 2020-01-07 stsp break;
2221 ca6e02ac 2020-01-07 stsp }
2222 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(changed_commit, &imsg,
2223 ca6e02ac 2020-01-07 stsp datalen, ibuf);
2224 ca6e02ac 2020-01-07 stsp break;
2225 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2226 ca6e02ac 2020-01-07 stsp done = 1;
2227 ca6e02ac 2020-01-07 stsp break;
2228 ca6e02ac 2020-01-07 stsp default:
2229 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2230 ca6e02ac 2020-01-07 stsp break;
2231 ca6e02ac 2020-01-07 stsp }
2232 ca6e02ac 2020-01-07 stsp
2233 ca6e02ac 2020-01-07 stsp imsg_free(&imsg);
2234 ca6e02ac 2020-01-07 stsp if (err)
2235 ca6e02ac 2020-01-07 stsp break;
2236 ca6e02ac 2020-01-07 stsp }
2237 ca6e02ac 2020-01-07 stsp
2238 ca6e02ac 2020-01-07 stsp if (err)
2239 ca6e02ac 2020-01-07 stsp got_object_id_queue_free(commit_ids);
2240 ca6e02ac 2020-01-07 stsp return err;
2241 ca6e02ac 2020-01-07 stsp }
2242 ca6e02ac 2020-01-07 stsp
2243 ca6e02ac 2020-01-07 stsp const struct got_error *
2244 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2245 ca6e02ac 2020-01-07 stsp {
2246 ca6e02ac 2020-01-07 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2247 ca6e02ac 2020-01-07 stsp NULL, 0) == -1)
2248 ca6e02ac 2020-01-07 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2249 ca6e02ac 2020-01-07 stsp
2250 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2251 ca6e02ac 2020-01-07 stsp }
2252 ca6e02ac 2020-01-07 stsp
2253 ca6e02ac 2020-01-07 stsp const struct got_error *
2254 63219cd2 2019-01-04 stsp got_privsep_unveil_exec_helpers(void)
2255 63219cd2 2019-01-04 stsp {
2256 c39c25dd 2019-08-09 stsp const char *helpers[] = {
2257 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_PACK,
2258 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_OBJECT,
2259 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_COMMIT,
2260 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TREE,
2261 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_BLOB,
2262 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TAG,
2263 aba9c984 2019-09-08 stsp GOT_PATH_PROG_READ_GITCONFIG,
2264 ee448f5f 2020-03-18 stsp GOT_PATH_PROG_FETCH_PACK,
2265 ee448f5f 2020-03-18 stsp GOT_PATH_PROG_INDEX_PACK,
2266 c39c25dd 2019-08-09 stsp };
2267 c39c25dd 2019-08-09 stsp int i;
2268 63219cd2 2019-01-04 stsp
2269 c39c25dd 2019-08-09 stsp for (i = 0; i < nitems(helpers); i++) {
2270 c39c25dd 2019-08-09 stsp if (unveil(helpers[i], "x") == 0)
2271 c39c25dd 2019-08-09 stsp continue;
2272 c39c25dd 2019-08-09 stsp return got_error_from_errno2("unveil", helpers[i]);
2273 c39c25dd 2019-08-09 stsp }
2274 c39c25dd 2019-08-09 stsp
2275 63219cd2 2019-01-04 stsp return NULL;
2276 876c234b 2018-09-10 stsp }
2277 aba9c984 2019-09-08 stsp
2278 aba9c984 2019-09-08 stsp void
2279 aba9c984 2019-09-08 stsp got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2280 aba9c984 2019-09-08 stsp {
2281 aba9c984 2019-09-08 stsp if (close(imsg_fds[0]) != 0) {
2282 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2283 aba9c984 2019-09-08 stsp _exit(1);
2284 aba9c984 2019-09-08 stsp }
2285 aba9c984 2019-09-08 stsp
2286 aba9c984 2019-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2287 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2288 aba9c984 2019-09-08 stsp _exit(1);
2289 aba9c984 2019-09-08 stsp }
2290 aba9c984 2019-09-08 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2291 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2292 aba9c984 2019-09-08 stsp _exit(1);
2293 aba9c984 2019-09-08 stsp }
2294 aba9c984 2019-09-08 stsp
2295 aba9c984 2019-09-08 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
2296 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2297 aba9c984 2019-09-08 stsp strerror(errno));
2298 aba9c984 2019-09-08 stsp _exit(1);
2299 aba9c984 2019-09-08 stsp }
2300 aba9c984 2019-09-08 stsp }