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