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 33501562 2020-03-18 stsp got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
409 33501562 2020-03-18 stsp struct got_pathlist_head *have_refs)
410 93658fb9 2020-03-18 stsp {
411 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
412 33501562 2020-03-18 stsp struct ibuf *wbuf;
413 33501562 2020-03-18 stsp size_t len, n_have_refs = 0;
414 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
415 93658fb9 2020-03-18 stsp
416 33501562 2020-03-18 stsp len = sizeof(struct got_imsg_fetch_symrefs);
417 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
418 33501562 2020-03-18 stsp struct got_object_id *id = pe->data;
419 33501562 2020-03-18 stsp len += sizeof(struct got_imsg_fetch_have_ref) +
420 33501562 2020-03-18 stsp pe->path_len + sizeof(id->sha1);
421 33501562 2020-03-18 stsp n_have_refs++;
422 33501562 2020-03-18 stsp }
423 33501562 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
424 33501562 2020-03-18 stsp close(fd);
425 33501562 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
426 33501562 2020-03-18 stsp }
427 33501562 2020-03-18 stsp
428 33501562 2020-03-18 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
429 33501562 2020-03-18 stsp if (wbuf == NULL) {
430 33501562 2020-03-18 stsp close(fd);
431 33501562 2020-03-18 stsp return got_error_from_errno("imsg_create FETCH_REQUEST");
432 33501562 2020-03-18 stsp }
433 33501562 2020-03-18 stsp
434 33501562 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_have_refs definition! */
435 33501562 2020-03-18 stsp if (imsg_add(wbuf, &n_have_refs, sizeof(n_have_refs)) == -1) {
436 33501562 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REQUEST");
437 33501562 2020-03-18 stsp ibuf_free(wbuf);
438 93658fb9 2020-03-18 stsp close(fd);
439 93658fb9 2020-03-18 stsp return err;
440 93658fb9 2020-03-18 stsp }
441 33501562 2020-03-18 stsp
442 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
443 33501562 2020-03-18 stsp const char *name = pe->path;
444 33501562 2020-03-18 stsp size_t name_len = pe->path_len;
445 33501562 2020-03-18 stsp struct got_object_id *id = pe->data;
446 33501562 2020-03-18 stsp
447 33501562 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_have_ref! */
448 33501562 2020-03-18 stsp if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
449 33501562 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REQUEST");
450 33501562 2020-03-18 stsp ibuf_free(wbuf);
451 33501562 2020-03-18 stsp close(fd);
452 33501562 2020-03-18 stsp return err;
453 33501562 2020-03-18 stsp }
454 33501562 2020-03-18 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
455 33501562 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REQUEST");
456 33501562 2020-03-18 stsp ibuf_free(wbuf);
457 33501562 2020-03-18 stsp close(fd);
458 33501562 2020-03-18 stsp return err;
459 33501562 2020-03-18 stsp }
460 33501562 2020-03-18 stsp if (imsg_add(wbuf, name, name_len) == -1) {
461 33501562 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REQUEST");
462 33501562 2020-03-18 stsp ibuf_free(wbuf);
463 33501562 2020-03-18 stsp close(fd);
464 33501562 2020-03-18 stsp return err;
465 33501562 2020-03-18 stsp }
466 33501562 2020-03-18 stsp }
467 33501562 2020-03-18 stsp
468 33501562 2020-03-18 stsp wbuf->fd = fd;
469 33501562 2020-03-18 stsp imsg_close(ibuf, wbuf);
470 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
471 93658fb9 2020-03-18 stsp }
472 93658fb9 2020-03-18 stsp
473 93658fb9 2020-03-18 stsp const struct got_error *
474 abe0f35f 2020-03-18 stsp got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
475 abe0f35f 2020-03-18 stsp struct got_pathlist_head *symrefs)
476 abe0f35f 2020-03-18 stsp {
477 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
478 abe0f35f 2020-03-18 stsp struct ibuf *wbuf;
479 abe0f35f 2020-03-18 stsp size_t len, nsymrefs = 0;
480 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
481 abe0f35f 2020-03-18 stsp
482 abe0f35f 2020-03-18 stsp len = sizeof(struct got_imsg_fetch_symrefs);
483 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
484 abe0f35f 2020-03-18 stsp const char *target = pe->data;
485 abe0f35f 2020-03-18 stsp len += sizeof(struct got_imsg_fetch_symref) +
486 abe0f35f 2020-03-18 stsp pe->path_len + strlen(target);
487 abe0f35f 2020-03-18 stsp nsymrefs++;
488 abe0f35f 2020-03-18 stsp }
489 abe0f35f 2020-03-18 stsp
490 abe0f35f 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
491 abe0f35f 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
492 abe0f35f 2020-03-18 stsp
493 abe0f35f 2020-03-18 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
494 abe0f35f 2020-03-18 stsp if (wbuf == NULL)
495 abe0f35f 2020-03-18 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
496 abe0f35f 2020-03-18 stsp
497 abe0f35f 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
498 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
499 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
500 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
501 abe0f35f 2020-03-18 stsp return err;
502 abe0f35f 2020-03-18 stsp }
503 abe0f35f 2020-03-18 stsp
504 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
505 abe0f35f 2020-03-18 stsp const char *name = pe->path;
506 abe0f35f 2020-03-18 stsp size_t name_len = pe->path_len;
507 abe0f35f 2020-03-18 stsp const char *target = pe->data;
508 abe0f35f 2020-03-18 stsp size_t target_len = strlen(target);
509 abe0f35f 2020-03-18 stsp
510 abe0f35f 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
511 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
512 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
513 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
514 abe0f35f 2020-03-18 stsp return err;
515 abe0f35f 2020-03-18 stsp }
516 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
517 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
518 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
519 abe0f35f 2020-03-18 stsp return err;
520 abe0f35f 2020-03-18 stsp }
521 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, name, name_len) == -1) {
522 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
523 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
524 abe0f35f 2020-03-18 stsp return err;
525 abe0f35f 2020-03-18 stsp }
526 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, target, target_len) == -1) {
527 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
528 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
529 abe0f35f 2020-03-18 stsp return err;
530 abe0f35f 2020-03-18 stsp }
531 abe0f35f 2020-03-18 stsp }
532 abe0f35f 2020-03-18 stsp
533 abe0f35f 2020-03-18 stsp wbuf->fd = -1;
534 abe0f35f 2020-03-18 stsp imsg_close(ibuf, wbuf);
535 abe0f35f 2020-03-18 stsp return flush_imsg(ibuf);
536 abe0f35f 2020-03-18 stsp }
537 abe0f35f 2020-03-18 stsp
538 abe0f35f 2020-03-18 stsp const struct got_error *
539 b9f99abf 2020-03-18 stsp got_privsep_send_fetch_progress(struct imsgbuf *ibuf,
540 b9f99abf 2020-03-18 stsp struct got_object_id *refid, const char *refname)
541 b9f99abf 2020-03-18 stsp {
542 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
543 b9f99abf 2020-03-18 stsp struct ibuf *wbuf;
544 b9f99abf 2020-03-18 stsp size_t len, reflen = strlen(refname);
545 b9f99abf 2020-03-18 stsp
546 b9f99abf 2020-03-18 stsp len = sizeof(struct got_imsg_fetch_progress) + reflen;
547 b9f99abf 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
548 b9f99abf 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
549 b9f99abf 2020-03-18 stsp
550 b9f99abf 2020-03-18 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_PROGRESS, 0, 0, len);
551 b9f99abf 2020-03-18 stsp if (wbuf == NULL)
552 b9f99abf 2020-03-18 stsp return got_error_from_errno("imsg_create FETCH_PROGRESS");
553 b9f99abf 2020-03-18 stsp
554 b9f99abf 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_progress definition! */
555 b9f99abf 2020-03-18 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
556 b9f99abf 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_PROGRESS");
557 b9f99abf 2020-03-18 stsp ibuf_free(wbuf);
558 b9f99abf 2020-03-18 stsp return err;
559 b9f99abf 2020-03-18 stsp }
560 b9f99abf 2020-03-18 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
561 b9f99abf 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_PROGRESS");
562 b9f99abf 2020-03-18 stsp ibuf_free(wbuf);
563 b9f99abf 2020-03-18 stsp return err;
564 b9f99abf 2020-03-18 stsp }
565 b9f99abf 2020-03-18 stsp
566 b9f99abf 2020-03-18 stsp wbuf->fd = -1;
567 b9f99abf 2020-03-18 stsp imsg_close(ibuf, wbuf);
568 b9f99abf 2020-03-18 stsp return flush_imsg(ibuf);
569 b9f99abf 2020-03-18 stsp }
570 b9f99abf 2020-03-18 stsp
571 b9f99abf 2020-03-18 stsp const struct got_error *
572 93658fb9 2020-03-18 stsp got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
573 93658fb9 2020-03-18 stsp {
574 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
575 93658fb9 2020-03-18 stsp hash.sha1, SHA1_DIGEST_LENGTH) == -1)
576 93658fb9 2020-03-18 stsp return got_error_from_errno("imsg_compose FETCH");
577 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
578 93658fb9 2020-03-18 stsp }
579 93658fb9 2020-03-18 stsp
580 b9f99abf 2020-03-18 stsp
581 93658fb9 2020-03-18 stsp const struct got_error *
582 8f2d01a6 2020-03-18 stsp got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
583 abe0f35f 2020-03-18 stsp char **refname, struct got_pathlist_head *symrefs, struct imsgbuf *ibuf)
584 b9f99abf 2020-03-18 stsp {
585 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
586 b9f99abf 2020-03-18 stsp struct imsg imsg;
587 b9f99abf 2020-03-18 stsp size_t datalen;
588 b9f99abf 2020-03-18 stsp const size_t min_datalen =
589 abe0f35f 2020-03-18 stsp MIN(MIN(sizeof(struct got_imsg_error),
590 abe0f35f 2020-03-18 stsp sizeof(struct got_imsg_fetch_progress)),
591 abe0f35f 2020-03-18 stsp sizeof(struct got_imsg_fetch_symrefs));
592 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symrefs *isymrefs = NULL;
593 abe0f35f 2020-03-18 stsp size_t n, remain;
594 abe0f35f 2020-03-18 stsp off_t off;
595 b9f99abf 2020-03-18 stsp
596 8f2d01a6 2020-03-18 stsp *done = 0;
597 8f2d01a6 2020-03-18 stsp *id = NULL;
598 b9f99abf 2020-03-18 stsp *refname = NULL;
599 b9f99abf 2020-03-18 stsp
600 b9f99abf 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
601 b9f99abf 2020-03-18 stsp if (err)
602 b9f99abf 2020-03-18 stsp return err;
603 b9f99abf 2020-03-18 stsp
604 b9f99abf 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
605 b9f99abf 2020-03-18 stsp switch (imsg.hdr.type) {
606 b9f99abf 2020-03-18 stsp case GOT_IMSG_ERROR:
607 b9f99abf 2020-03-18 stsp err = recv_imsg_error(&imsg, datalen);
608 b9f99abf 2020-03-18 stsp break;
609 abe0f35f 2020-03-18 stsp case GOT_IMSG_FETCH_SYMREFS:
610 abe0f35f 2020-03-18 stsp if (datalen < sizeof(*isymrefs)) {
611 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
612 abe0f35f 2020-03-18 stsp break;
613 abe0f35f 2020-03-18 stsp }
614 abe0f35f 2020-03-18 stsp if (isymrefs != NULL) {
615 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
616 abe0f35f 2020-03-18 stsp break;
617 abe0f35f 2020-03-18 stsp }
618 abe0f35f 2020-03-18 stsp isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
619 abe0f35f 2020-03-18 stsp off = sizeof(*isymrefs);
620 abe0f35f 2020-03-18 stsp remain = datalen - off;
621 abe0f35f 2020-03-18 stsp for (n = 0; n < isymrefs->nsymrefs; n++) {
622 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symref *s;
623 abe0f35f 2020-03-18 stsp char *name, *target;
624 abe0f35f 2020-03-18 stsp if (remain < sizeof(struct got_imsg_fetch_symref)) {
625 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
626 abe0f35f 2020-03-18 stsp goto done;
627 abe0f35f 2020-03-18 stsp }
628 abe0f35f 2020-03-18 stsp s = (struct got_imsg_fetch_symref *)(imsg.data + off);
629 abe0f35f 2020-03-18 stsp off += sizeof(*s);
630 abe0f35f 2020-03-18 stsp remain -= sizeof(*s);
631 abe0f35f 2020-03-18 stsp if (remain < s->name_len) {
632 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
633 abe0f35f 2020-03-18 stsp goto done;
634 abe0f35f 2020-03-18 stsp }
635 abe0f35f 2020-03-18 stsp name = strndup(imsg.data + off, s->name_len);
636 abe0f35f 2020-03-18 stsp if (name == NULL) {
637 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
638 abe0f35f 2020-03-18 stsp goto done;
639 abe0f35f 2020-03-18 stsp }
640 abe0f35f 2020-03-18 stsp off += s->name_len;
641 abe0f35f 2020-03-18 stsp remain -= s->name_len;
642 abe0f35f 2020-03-18 stsp if (remain < s->target_len) {
643 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
644 abe0f35f 2020-03-18 stsp free(name);
645 abe0f35f 2020-03-18 stsp goto done;
646 abe0f35f 2020-03-18 stsp }
647 abe0f35f 2020-03-18 stsp target = strndup(imsg.data + off, s->target_len);
648 abe0f35f 2020-03-18 stsp if (target == NULL) {
649 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
650 abe0f35f 2020-03-18 stsp free(name);
651 abe0f35f 2020-03-18 stsp goto done;
652 abe0f35f 2020-03-18 stsp }
653 abe0f35f 2020-03-18 stsp off += s->target_len;
654 abe0f35f 2020-03-18 stsp remain -= s->target_len;
655 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
656 abe0f35f 2020-03-18 stsp if (err) {
657 abe0f35f 2020-03-18 stsp free(name);
658 abe0f35f 2020-03-18 stsp free(target);
659 abe0f35f 2020-03-18 stsp goto done;
660 abe0f35f 2020-03-18 stsp }
661 abe0f35f 2020-03-18 stsp }
662 abe0f35f 2020-03-18 stsp break;
663 b9f99abf 2020-03-18 stsp case GOT_IMSG_FETCH_PROGRESS:
664 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
665 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
666 b9f99abf 2020-03-18 stsp err = got_error_from_errno("malloc");
667 b9f99abf 2020-03-18 stsp break;
668 b9f99abf 2020-03-18 stsp }
669 8f2d01a6 2020-03-18 stsp memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
670 b9f99abf 2020-03-18 stsp if (datalen <= SHA1_DIGEST_LENGTH) {
671 b9f99abf 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
672 b9f99abf 2020-03-18 stsp break;
673 b9f99abf 2020-03-18 stsp }
674 b9f99abf 2020-03-18 stsp *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
675 b9f99abf 2020-03-18 stsp datalen - SHA1_DIGEST_LENGTH);
676 b9f99abf 2020-03-18 stsp if (*refname == NULL) {
677 b9f99abf 2020-03-18 stsp err = got_error_from_errno("strndup");
678 8f2d01a6 2020-03-18 stsp break;
679 8f2d01a6 2020-03-18 stsp }
680 8f2d01a6 2020-03-18 stsp break;
681 8f2d01a6 2020-03-18 stsp case GOT_IMSG_FETCH_DONE:
682 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
683 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
684 8f2d01a6 2020-03-18 stsp err = got_error_from_errno("malloc");
685 b9f99abf 2020-03-18 stsp break;
686 b9f99abf 2020-03-18 stsp }
687 8f2d01a6 2020-03-18 stsp if (datalen != SHA1_DIGEST_LENGTH) {
688 8f2d01a6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
689 8f2d01a6 2020-03-18 stsp break;
690 8f2d01a6 2020-03-18 stsp }
691 8f2d01a6 2020-03-18 stsp memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
692 8f2d01a6 2020-03-18 stsp *done = 1;
693 b9f99abf 2020-03-18 stsp break;
694 b9f99abf 2020-03-18 stsp default:
695 b887aab6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
696 b887aab6 2020-03-18 stsp break;
697 b9f99abf 2020-03-18 stsp }
698 abe0f35f 2020-03-18 stsp done:
699 b887aab6 2020-03-18 stsp if (err) {
700 8f2d01a6 2020-03-18 stsp free(*id);
701 8f2d01a6 2020-03-18 stsp *id = NULL;
702 b887aab6 2020-03-18 stsp free(*refname);
703 b887aab6 2020-03-18 stsp *refname = NULL;
704 b887aab6 2020-03-18 stsp }
705 b9f99abf 2020-03-18 stsp imsg_free(&imsg);
706 b9f99abf 2020-03-18 stsp return err;
707 93658fb9 2020-03-18 stsp }
708 93658fb9 2020-03-18 stsp
709 93658fb9 2020-03-18 stsp const struct got_error *
710 279090e1 2020-03-18 stsp got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id *hash)
711 93658fb9 2020-03-18 stsp {
712 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
713 93658fb9 2020-03-18 stsp
714 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
715 279090e1 2020-03-18 stsp hash->sha1, SHA1_DIGEST_LENGTH) == -1) {
716 93658fb9 2020-03-18 stsp err = got_error_from_errno("imsg_compose INDEX_REQUEST");
717 93658fb9 2020-03-18 stsp close(fd);
718 93658fb9 2020-03-18 stsp return err;
719 93658fb9 2020-03-18 stsp }
720 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
721 93658fb9 2020-03-18 stsp }
722 93658fb9 2020-03-18 stsp
723 93658fb9 2020-03-18 stsp const struct got_error *
724 93658fb9 2020-03-18 stsp got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
725 93658fb9 2020-03-18 stsp {
726 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
727 93658fb9 2020-03-18 stsp return got_error_from_errno("imsg_compose FETCH");
728 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
729 93658fb9 2020-03-18 stsp }
730 93658fb9 2020-03-18 stsp
731 93658fb9 2020-03-18 stsp const struct got_error *
732 93658fb9 2020-03-18 stsp got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
733 93658fb9 2020-03-18 stsp {
734 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
735 93658fb9 2020-03-18 stsp struct imsg imsg;
736 93658fb9 2020-03-18 stsp
737 93658fb9 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
738 93658fb9 2020-03-18 stsp if (err)
739 93658fb9 2020-03-18 stsp return err;
740 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
741 93658fb9 2020-03-18 stsp return NULL;
742 93658fb9 2020-03-18 stsp else
743 93658fb9 2020-03-18 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
744 93658fb9 2020-03-18 stsp imsg_free(&imsg);
745 93658fb9 2020-03-18 stsp }
746 93658fb9 2020-03-18 stsp
747 93658fb9 2020-03-18 stsp const struct got_error *
748 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
749 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
750 cfd633c2 2018-09-10 stsp {
751 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
752 291624d8 2018-11-07 stsp struct got_imsg_object *iobj;
753 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
754 cfd633c2 2018-09-10 stsp
755 291624d8 2018-11-07 stsp if (datalen != sizeof(*iobj))
756 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
757 291624d8 2018-11-07 stsp iobj = imsg->data;
758 cfd633c2 2018-09-10 stsp
759 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
760 cfd633c2 2018-09-10 stsp if (*obj == NULL)
761 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
762 cfd633c2 2018-09-10 stsp
763 291624d8 2018-11-07 stsp memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
764 291624d8 2018-11-07 stsp (*obj)->type = iobj->type;
765 291624d8 2018-11-07 stsp (*obj)->flags = iobj->flags;
766 291624d8 2018-11-07 stsp (*obj)->hdrlen = iobj->hdrlen;
767 291624d8 2018-11-07 stsp (*obj)->size = iobj->size;
768 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
769 291624d8 2018-11-07 stsp if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
770 291624d8 2018-11-07 stsp (*obj)->pack_offset = iobj->pack_offset;
771 291624d8 2018-11-07 stsp (*obj)->pack_idx = iobj->pack_idx;
772 876c234b 2018-09-10 stsp }
773 876c234b 2018-09-10 stsp
774 876c234b 2018-09-10 stsp return err;
775 876c234b 2018-09-10 stsp }
776 876c234b 2018-09-10 stsp
777 2178c42e 2018-04-22 stsp const struct got_error *
778 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
779 2178c42e 2018-04-22 stsp {
780 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
781 2178c42e 2018-04-22 stsp struct imsg imsg;
782 c4eae628 2018-04-23 stsp const size_t min_datalen =
783 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
784 2178c42e 2018-04-22 stsp
785 2178c42e 2018-04-22 stsp *obj = NULL;
786 2178c42e 2018-04-22 stsp
787 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
788 2178c42e 2018-04-22 stsp if (err)
789 2178c42e 2018-04-22 stsp return err;
790 2178c42e 2018-04-22 stsp
791 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
792 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
793 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
794 bff6ca00 2018-04-23 stsp break;
795 bff6ca00 2018-04-23 stsp default:
796 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
797 bff6ca00 2018-04-23 stsp break;
798 bff6ca00 2018-04-23 stsp }
799 bff6ca00 2018-04-23 stsp
800 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
801 bff6ca00 2018-04-23 stsp
802 bff6ca00 2018-04-23 stsp return err;
803 c75f7264 2018-09-11 stsp }
804 c75f7264 2018-09-11 stsp
805 c75f7264 2018-09-11 stsp static const struct got_error *
806 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
807 c75f7264 2018-09-11 stsp size_t logmsg_len)
808 c75f7264 2018-09-11 stsp {
809 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
810 c75f7264 2018-09-11 stsp size_t offset, remain;
811 c75f7264 2018-09-11 stsp
812 c75f7264 2018-09-11 stsp offset = 0;
813 c75f7264 2018-09-11 stsp remain = logmsg_len;
814 c75f7264 2018-09-11 stsp while (remain > 0) {
815 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
816 c75f7264 2018-09-11 stsp
817 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
818 fa4ffeb3 2018-11-04 stsp commit->logmsg + offset, n) == -1) {
819 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose "
820 230a42bd 2019-05-11 jcs "COMMIT_LOGMSG");
821 fa4ffeb3 2018-11-04 stsp break;
822 fa4ffeb3 2018-11-04 stsp }
823 c75f7264 2018-09-11 stsp
824 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
825 fa4ffeb3 2018-11-04 stsp if (err)
826 fa4ffeb3 2018-11-04 stsp break;
827 c75f7264 2018-09-11 stsp
828 c75f7264 2018-09-11 stsp offset += n;
829 c75f7264 2018-09-11 stsp remain -= n;
830 c75f7264 2018-09-11 stsp }
831 c75f7264 2018-09-11 stsp
832 fa4ffeb3 2018-11-04 stsp return err;
833 bff6ca00 2018-04-23 stsp }
834 bff6ca00 2018-04-23 stsp
835 bff6ca00 2018-04-23 stsp const struct got_error *
836 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
837 bff6ca00 2018-04-23 stsp {
838 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
839 b9c33926 2018-11-07 stsp struct got_imsg_commit_object *icommit;
840 bff6ca00 2018-04-23 stsp uint8_t *buf;
841 bff6ca00 2018-04-23 stsp size_t len, total;
842 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
843 b9c33926 2018-11-07 stsp size_t author_len = strlen(commit->author);
844 b9c33926 2018-11-07 stsp size_t committer_len = strlen(commit->committer);
845 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
846 bff6ca00 2018-04-23 stsp
847 b9c33926 2018-11-07 stsp total = sizeof(*icommit) + author_len + committer_len +
848 b9c33926 2018-11-07 stsp commit->nparents * SHA1_DIGEST_LENGTH;
849 bff6ca00 2018-04-23 stsp
850 bff6ca00 2018-04-23 stsp buf = malloc(total);
851 bff6ca00 2018-04-23 stsp if (buf == NULL)
852 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
853 bff6ca00 2018-04-23 stsp
854 b9c33926 2018-11-07 stsp icommit = (struct got_imsg_commit_object *)buf;
855 a7403916 2018-12-24 stsp memcpy(icommit->tree_id, commit->tree_id->sha1,
856 a7403916 2018-12-24 stsp sizeof(icommit->tree_id));
857 b9c33926 2018-11-07 stsp icommit->author_len = author_len;
858 b9c33926 2018-11-07 stsp icommit->author_time = commit->author_time;
859 b9c33926 2018-11-07 stsp icommit->author_gmtoff = commit->author_gmtoff;
860 b9c33926 2018-11-07 stsp icommit->committer_len = committer_len;
861 b9c33926 2018-11-07 stsp icommit->committer_time = commit->committer_time;
862 b9c33926 2018-11-07 stsp icommit->committer_gmtoff = commit->committer_gmtoff;
863 b9c33926 2018-11-07 stsp icommit->logmsg_len = logmsg_len;
864 b9c33926 2018-11-07 stsp icommit->nparents = commit->nparents;
865 b9c33926 2018-11-07 stsp
866 b9c33926 2018-11-07 stsp len = sizeof(*icommit);
867 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->author, author_len);
868 b9c33926 2018-11-07 stsp len += author_len;
869 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->committer, committer_len);
870 b9c33926 2018-11-07 stsp len += committer_len;
871 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
872 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
873 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
874 bff6ca00 2018-04-23 stsp }
875 bff6ca00 2018-04-23 stsp
876 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
877 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT");
878 bff6ca00 2018-04-23 stsp goto done;
879 bff6ca00 2018-04-23 stsp }
880 bff6ca00 2018-04-23 stsp
881 904df868 2018-11-04 stsp if (logmsg_len == 0 ||
882 904df868 2018-11-04 stsp logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
883 904df868 2018-11-04 stsp err = flush_imsg(ibuf);
884 904df868 2018-11-04 stsp if (err)
885 904df868 2018-11-04 stsp goto done;
886 904df868 2018-11-04 stsp }
887 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
888 bff6ca00 2018-04-23 stsp done:
889 bff6ca00 2018-04-23 stsp free(buf);
890 bff6ca00 2018-04-23 stsp return err;
891 bff6ca00 2018-04-23 stsp }
892 cfd633c2 2018-09-10 stsp
893 ca6e02ac 2020-01-07 stsp static const struct got_error *
894 ca6e02ac 2020-01-07 stsp get_commit_from_imsg(struct got_commit_object **commit,
895 ca6e02ac 2020-01-07 stsp struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
896 bff6ca00 2018-04-23 stsp {
897 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
898 291624d8 2018-11-07 stsp struct got_imsg_commit_object *icommit;
899 ca6e02ac 2020-01-07 stsp size_t len = 0;
900 bff6ca00 2018-04-23 stsp int i;
901 bff6ca00 2018-04-23 stsp
902 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(*icommit))
903 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
904 bff6ca00 2018-04-23 stsp
905 ca6e02ac 2020-01-07 stsp icommit = imsg->data;
906 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommit) + icommit->author_len +
907 ca6e02ac 2020-01-07 stsp icommit->committer_len +
908 ca6e02ac 2020-01-07 stsp icommit->nparents * SHA1_DIGEST_LENGTH)
909 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
910 bff6ca00 2018-04-23 stsp
911 ca6e02ac 2020-01-07 stsp if (icommit->nparents < 0)
912 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
913 ca6e02ac 2020-01-07 stsp
914 ca6e02ac 2020-01-07 stsp len += sizeof(*icommit);
915 bff6ca00 2018-04-23 stsp
916 ca6e02ac 2020-01-07 stsp *commit = got_object_commit_alloc_partial();
917 ca6e02ac 2020-01-07 stsp if (*commit == NULL)
918 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
919 ca6e02ac 2020-01-07 stsp "got_object_commit_alloc_partial");
920 ca6e02ac 2020-01-07 stsp
921 ca6e02ac 2020-01-07 stsp memcpy((*commit)->tree_id->sha1, icommit->tree_id,
922 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
923 ca6e02ac 2020-01-07 stsp (*commit)->author_time = icommit->author_time;
924 ca6e02ac 2020-01-07 stsp (*commit)->author_gmtoff = icommit->author_gmtoff;
925 ca6e02ac 2020-01-07 stsp (*commit)->committer_time = icommit->committer_time;
926 ca6e02ac 2020-01-07 stsp (*commit)->committer_gmtoff = icommit->committer_gmtoff;
927 ca6e02ac 2020-01-07 stsp
928 ca6e02ac 2020-01-07 stsp if (icommit->author_len == 0) {
929 ca6e02ac 2020-01-07 stsp (*commit)->author = strdup("");
930 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
931 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
932 ca6e02ac 2020-01-07 stsp goto done;
933 bff6ca00 2018-04-23 stsp }
934 ca6e02ac 2020-01-07 stsp } else {
935 ca6e02ac 2020-01-07 stsp (*commit)->author = malloc(icommit->author_len + 1);
936 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
937 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
938 ca6e02ac 2020-01-07 stsp goto done;
939 ca6e02ac 2020-01-07 stsp }
940 ca6e02ac 2020-01-07 stsp memcpy((*commit)->author, imsg->data + len,
941 ca6e02ac 2020-01-07 stsp icommit->author_len);
942 ca6e02ac 2020-01-07 stsp (*commit)->author[icommit->author_len] = '\0';
943 ca6e02ac 2020-01-07 stsp }
944 ca6e02ac 2020-01-07 stsp len += icommit->author_len;
945 bff6ca00 2018-04-23 stsp
946 ca6e02ac 2020-01-07 stsp if (icommit->committer_len == 0) {
947 ca6e02ac 2020-01-07 stsp (*commit)->committer = strdup("");
948 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
949 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
950 ca6e02ac 2020-01-07 stsp goto done;
951 ca6e02ac 2020-01-07 stsp }
952 ca6e02ac 2020-01-07 stsp } else {
953 ca6e02ac 2020-01-07 stsp (*commit)->committer =
954 ca6e02ac 2020-01-07 stsp malloc(icommit->committer_len + 1);
955 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
956 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
957 ca6e02ac 2020-01-07 stsp goto done;
958 ca6e02ac 2020-01-07 stsp }
959 ca6e02ac 2020-01-07 stsp memcpy((*commit)->committer, imsg->data + len,
960 ca6e02ac 2020-01-07 stsp icommit->committer_len);
961 ca6e02ac 2020-01-07 stsp (*commit)->committer[icommit->committer_len] = '\0';
962 ca6e02ac 2020-01-07 stsp }
963 ca6e02ac 2020-01-07 stsp len += icommit->committer_len;
964 ca6e02ac 2020-01-07 stsp
965 ca6e02ac 2020-01-07 stsp if (icommit->logmsg_len == 0) {
966 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = strdup("");
967 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
968 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
969 ca6e02ac 2020-01-07 stsp goto done;
970 ca6e02ac 2020-01-07 stsp }
971 ca6e02ac 2020-01-07 stsp } else {
972 ca6e02ac 2020-01-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
973 ca6e02ac 2020-01-07 stsp
974 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
975 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
976 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
977 ca6e02ac 2020-01-07 stsp goto done;
978 bff6ca00 2018-04-23 stsp }
979 ca6e02ac 2020-01-07 stsp while (remain > 0) {
980 ca6e02ac 2020-01-07 stsp struct imsg imsg_log;
981 ca6e02ac 2020-01-07 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
982 ca6e02ac 2020-01-07 stsp remain);
983 6c281f94 2018-06-11 stsp
984 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
985 ca6e02ac 2020-01-07 stsp if (err)
986 ca6e02ac 2020-01-07 stsp goto done;
987 bff6ca00 2018-04-23 stsp
988 ca6e02ac 2020-01-07 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
989 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
990 ca6e02ac 2020-01-07 stsp goto done;
991 bff6ca00 2018-04-23 stsp }
992 c75f7264 2018-09-11 stsp
993 ca6e02ac 2020-01-07 stsp memcpy((*commit)->logmsg + offset,
994 ca6e02ac 2020-01-07 stsp imsg_log.data, n);
995 ca6e02ac 2020-01-07 stsp imsg_free(&imsg_log);
996 ca6e02ac 2020-01-07 stsp offset += n;
997 ca6e02ac 2020-01-07 stsp remain -= n;
998 bff6ca00 2018-04-23 stsp }
999 ca6e02ac 2020-01-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
1000 ca6e02ac 2020-01-07 stsp }
1001 bff6ca00 2018-04-23 stsp
1002 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommit->nparents; i++) {
1003 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
1004 86acc566 2018-04-23 stsp
1005 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
1006 ca6e02ac 2020-01-07 stsp if (err)
1007 ca6e02ac 2020-01-07 stsp break;
1008 ca6e02ac 2020-01-07 stsp memcpy(qid->id, imsg->data + len +
1009 ca6e02ac 2020-01-07 stsp i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1010 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1011 ca6e02ac 2020-01-07 stsp (*commit)->nparents++;
1012 ca6e02ac 2020-01-07 stsp }
1013 ca6e02ac 2020-01-07 stsp done:
1014 ca6e02ac 2020-01-07 stsp if (err) {
1015 ca6e02ac 2020-01-07 stsp got_object_commit_close(*commit);
1016 ca6e02ac 2020-01-07 stsp *commit = NULL;
1017 ca6e02ac 2020-01-07 stsp }
1018 ca6e02ac 2020-01-07 stsp return err;
1019 ca6e02ac 2020-01-07 stsp }
1020 ca6e02ac 2020-01-07 stsp
1021 ca6e02ac 2020-01-07 stsp const struct got_error *
1022 ca6e02ac 2020-01-07 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1023 ca6e02ac 2020-01-07 stsp {
1024 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1025 ca6e02ac 2020-01-07 stsp struct imsg imsg;
1026 ca6e02ac 2020-01-07 stsp size_t datalen;
1027 ca6e02ac 2020-01-07 stsp const size_t min_datalen =
1028 ca6e02ac 2020-01-07 stsp MIN(sizeof(struct got_imsg_error),
1029 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_object));
1030 ca6e02ac 2020-01-07 stsp
1031 ca6e02ac 2020-01-07 stsp *commit = NULL;
1032 ca6e02ac 2020-01-07 stsp
1033 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1034 ca6e02ac 2020-01-07 stsp if (err)
1035 ca6e02ac 2020-01-07 stsp return err;
1036 ca6e02ac 2020-01-07 stsp
1037 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1038 ca6e02ac 2020-01-07 stsp
1039 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
1040 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
1041 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1042 2178c42e 2018-04-22 stsp break;
1043 8c580685 2018-04-22 stsp default:
1044 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1045 8c580685 2018-04-22 stsp break;
1046 2178c42e 2018-04-22 stsp }
1047 2178c42e 2018-04-22 stsp
1048 2178c42e 2018-04-22 stsp imsg_free(&imsg);
1049 e033d803 2018-04-23 stsp
1050 e033d803 2018-04-23 stsp return err;
1051 e033d803 2018-04-23 stsp }
1052 e033d803 2018-04-23 stsp
1053 e033d803 2018-04-23 stsp const struct got_error *
1054 3022d272 2019-11-14 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1055 3022d272 2019-11-14 stsp int nentries)
1056 e033d803 2018-04-23 stsp {
1057 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1058 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
1059 3022d272 2019-11-14 stsp struct got_pathlist_entry *pe;
1060 b00c9821 2018-11-04 stsp size_t totlen;
1061 6eb07a17 2018-11-04 stsp int nimsg; /* number of imsg queued in ibuf */
1062 e033d803 2018-04-23 stsp
1063 3022d272 2019-11-14 stsp itree.nentries = nentries;
1064 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1065 e033d803 2018-04-23 stsp == -1)
1066 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TREE");
1067 e033d803 2018-04-23 stsp
1068 b00c9821 2018-11-04 stsp totlen = sizeof(itree);
1069 6eb07a17 2018-11-04 stsp nimsg = 1;
1070 3022d272 2019-11-14 stsp TAILQ_FOREACH(pe, entries, entry) {
1071 3022d272 2019-11-14 stsp const char *name = pe->path;
1072 3022d272 2019-11-14 stsp struct got_parsed_tree_entry *pte = pe->data;
1073 3022d272 2019-11-14 stsp struct ibuf *wbuf;
1074 3022d272 2019-11-14 stsp size_t namelen = strlen(name);
1075 cd9e913a 2019-11-27 stsp size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1076 e033d803 2018-04-23 stsp
1077 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
1078 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
1079 e033d803 2018-04-23 stsp
1080 6eb07a17 2018-11-04 stsp nimsg++;
1081 6eb07a17 2018-11-04 stsp if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1082 b00c9821 2018-11-04 stsp err = flush_imsg(ibuf);
1083 b00c9821 2018-11-04 stsp if (err)
1084 b00c9821 2018-11-04 stsp return err;
1085 6eb07a17 2018-11-04 stsp nimsg = 0;
1086 b00c9821 2018-11-04 stsp }
1087 b00c9821 2018-11-04 stsp
1088 3022d272 2019-11-14 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1089 3022d272 2019-11-14 stsp if (wbuf == NULL)
1090 3022d272 2019-11-14 stsp return got_error_from_errno("imsg_create TREE_ENTRY");
1091 e033d803 2018-04-23 stsp
1092 3022d272 2019-11-14 stsp /* Keep in sync with struct got_imsg_tree_object definition! */
1093 3b647085 2019-11-23 stsp if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1094 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1095 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1096 e033d803 2018-04-23 stsp return err;
1097 3b647085 2019-11-23 stsp }
1098 3b647085 2019-11-23 stsp if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1099 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1100 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1101 3022d272 2019-11-14 stsp return err;
1102 3b647085 2019-11-23 stsp }
1103 3022d272 2019-11-14 stsp
1104 3b647085 2019-11-23 stsp if (imsg_add(wbuf, name, namelen) == -1) {
1105 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1106 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1107 3022d272 2019-11-14 stsp return err;
1108 3b647085 2019-11-23 stsp }
1109 3022d272 2019-11-14 stsp
1110 3022d272 2019-11-14 stsp wbuf->fd = -1;
1111 3022d272 2019-11-14 stsp imsg_close(ibuf, wbuf);
1112 3022d272 2019-11-14 stsp
1113 b00c9821 2018-11-04 stsp totlen += len;
1114 e033d803 2018-04-23 stsp }
1115 e033d803 2018-04-23 stsp
1116 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
1117 e033d803 2018-04-23 stsp }
1118 e033d803 2018-04-23 stsp
1119 e033d803 2018-04-23 stsp const struct got_error *
1120 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1121 e033d803 2018-04-23 stsp {
1122 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1123 e033d803 2018-04-23 stsp const size_t min_datalen =
1124 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
1125 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
1126 291624d8 2018-11-07 stsp struct got_imsg_tree_object *itree;
1127 e033d803 2018-04-23 stsp int nentries = 0;
1128 2178c42e 2018-04-22 stsp
1129 e033d803 2018-04-23 stsp *tree = NULL;
1130 e033d803 2018-04-23 stsp get_more:
1131 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
1132 e033d803 2018-04-23 stsp if (err)
1133 1e51f5b9 2018-04-23 stsp goto done;
1134 e033d803 2018-04-23 stsp
1135 656b1f76 2019-05-11 jcs for (;;) {
1136 e033d803 2018-04-23 stsp struct imsg imsg;
1137 e033d803 2018-04-23 stsp size_t n;
1138 e033d803 2018-04-23 stsp size_t datalen;
1139 c0588d8d 2018-11-07 stsp struct got_imsg_tree_entry *ite;
1140 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
1141 e033d803 2018-04-23 stsp
1142 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
1143 e033d803 2018-04-23 stsp if (n == 0) {
1144 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries)
1145 e033d803 2018-04-23 stsp goto get_more;
1146 e033d803 2018-04-23 stsp break;
1147 e033d803 2018-04-23 stsp }
1148 e033d803 2018-04-23 stsp
1149 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1150 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1151 e033d803 2018-04-23 stsp
1152 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1153 e033d803 2018-04-23 stsp
1154 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
1155 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
1156 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
1157 e033d803 2018-04-23 stsp break;
1158 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
1159 e033d803 2018-04-23 stsp /* This message should only appear once. */
1160 e033d803 2018-04-23 stsp if (*tree != NULL) {
1161 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1162 e033d803 2018-04-23 stsp break;
1163 e033d803 2018-04-23 stsp }
1164 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
1165 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1166 e033d803 2018-04-23 stsp break;
1167 e033d803 2018-04-23 stsp }
1168 291624d8 2018-11-07 stsp itree = imsg.data;
1169 c3b78ecc 2018-11-07 stsp *tree = malloc(sizeof(**tree));
1170 e033d803 2018-04-23 stsp if (*tree == NULL) {
1171 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1172 e033d803 2018-04-23 stsp break;
1173 e033d803 2018-04-23 stsp }
1174 56e0773d 2019-11-28 stsp (*tree)->entries = calloc(itree->nentries,
1175 56e0773d 2019-11-28 stsp sizeof(struct got_tree_entry));
1176 56e0773d 2019-11-28 stsp if ((*tree)->entries == NULL) {
1177 56e0773d 2019-11-28 stsp err = got_error_from_errno("malloc");
1178 56e0773d 2019-11-28 stsp break;
1179 56e0773d 2019-11-28 stsp }
1180 56e0773d 2019-11-28 stsp (*tree)->nentries = itree->nentries;
1181 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
1182 e033d803 2018-04-23 stsp break;
1183 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
1184 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
1185 e033d803 2018-04-23 stsp if (*tree == NULL) {
1186 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1187 e033d803 2018-04-23 stsp break;
1188 e033d803 2018-04-23 stsp }
1189 c0588d8d 2018-11-07 stsp if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1190 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1191 e033d803 2018-04-23 stsp break;
1192 e033d803 2018-04-23 stsp }
1193 e033d803 2018-04-23 stsp
1194 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
1195 c0588d8d 2018-11-07 stsp datalen -= sizeof(*ite);
1196 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1197 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1198 e033d803 2018-04-23 stsp break;
1199 e033d803 2018-04-23 stsp }
1200 c0588d8d 2018-11-07 stsp ite = imsg.data;
1201 052d4dc3 2018-04-23 stsp
1202 56e0773d 2019-11-28 stsp if (datalen + 1 > sizeof(te->name)) {
1203 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1204 e033d803 2018-04-23 stsp break;
1205 e033d803 2018-04-23 stsp }
1206 56e0773d 2019-11-28 stsp te = &(*tree)->entries[nentries];
1207 c0588d8d 2018-11-07 stsp memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1208 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
1209 e033d803 2018-04-23 stsp
1210 56e0773d 2019-11-28 stsp memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1211 c0588d8d 2018-11-07 stsp te->mode = ite->mode;
1212 56e0773d 2019-11-28 stsp te->idx = nentries;
1213 e033d803 2018-04-23 stsp nentries++;
1214 e033d803 2018-04-23 stsp break;
1215 e033d803 2018-04-23 stsp default:
1216 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1217 e033d803 2018-04-23 stsp break;
1218 e033d803 2018-04-23 stsp }
1219 e033d803 2018-04-23 stsp
1220 e033d803 2018-04-23 stsp imsg_free(&imsg);
1221 e033d803 2018-04-23 stsp }
1222 1e51f5b9 2018-04-23 stsp done:
1223 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries) {
1224 1e51f5b9 2018-04-23 stsp if (err == NULL)
1225 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1226 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
1227 e033d803 2018-04-23 stsp *tree = NULL;
1228 ff6b18f8 2018-04-24 stsp }
1229 ff6b18f8 2018-04-24 stsp
1230 ff6b18f8 2018-04-24 stsp return err;
1231 ff6b18f8 2018-04-24 stsp }
1232 ff6b18f8 2018-04-24 stsp
1233 ff6b18f8 2018-04-24 stsp const struct got_error *
1234 ac544f8c 2019-01-13 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1235 ac544f8c 2019-01-13 stsp const uint8_t *data)
1236 ff6b18f8 2018-04-24 stsp {
1237 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
1238 2967a784 2018-04-24 stsp
1239 2967a784 2018-04-24 stsp iblob.size = size;
1240 ebc55e2d 2018-12-24 stsp iblob.hdrlen = hdrlen;
1241 2967a784 2018-04-24 stsp
1242 ac544f8c 2019-01-13 stsp if (data) {
1243 ac544f8c 2019-01-13 stsp uint8_t *buf;
1244 ac544f8c 2019-01-13 stsp
1245 ac544f8c 2019-01-13 stsp if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1246 ac544f8c 2019-01-13 stsp return got_error(GOT_ERR_NO_SPACE);
1247 ac544f8c 2019-01-13 stsp
1248 ac544f8c 2019-01-13 stsp buf = malloc(sizeof(iblob) + size);
1249 ac544f8c 2019-01-13 stsp if (buf == NULL)
1250 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1251 ff6b18f8 2018-04-24 stsp
1252 ac544f8c 2019-01-13 stsp memcpy(buf, &iblob, sizeof(iblob));
1253 ac544f8c 2019-01-13 stsp memcpy(buf + sizeof(iblob), data, size);
1254 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1255 ac544f8c 2019-01-13 stsp sizeof(iblob) + size) == -1) {
1256 ac544f8c 2019-01-13 stsp free(buf);
1257 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1258 ac544f8c 2019-01-13 stsp }
1259 ac544f8c 2019-01-13 stsp free(buf);
1260 ac544f8c 2019-01-13 stsp } else {
1261 ac544f8c 2019-01-13 stsp /* Data has already been written to file descriptor. */
1262 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1263 ac544f8c 2019-01-13 stsp sizeof(iblob)) == -1)
1264 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1265 ac544f8c 2019-01-13 stsp }
1266 ac544f8c 2019-01-13 stsp
1267 ac544f8c 2019-01-13 stsp
1268 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
1269 ff6b18f8 2018-04-24 stsp }
1270 ff6b18f8 2018-04-24 stsp
1271 ff6b18f8 2018-04-24 stsp const struct got_error *
1272 ac544f8c 2019-01-13 stsp got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1273 ac544f8c 2019-01-13 stsp struct imsgbuf *ibuf)
1274 ff6b18f8 2018-04-24 stsp {
1275 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1276 ff6b18f8 2018-04-24 stsp struct imsg imsg;
1277 291624d8 2018-11-07 stsp struct got_imsg_blob *iblob;
1278 ff6b18f8 2018-04-24 stsp size_t datalen;
1279 ff6b18f8 2018-04-24 stsp
1280 ac544f8c 2019-01-13 stsp *outbuf = NULL;
1281 ac544f8c 2019-01-13 stsp
1282 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1283 ff6b18f8 2018-04-24 stsp if (err)
1284 ff6b18f8 2018-04-24 stsp return err;
1285 ff6b18f8 2018-04-24 stsp
1286 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1287 ff6b18f8 2018-04-24 stsp
1288 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
1289 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
1290 ac544f8c 2019-01-13 stsp if (datalen < sizeof(*iblob)) {
1291 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1292 18336eed 2018-11-04 stsp break;
1293 18336eed 2018-11-04 stsp }
1294 291624d8 2018-11-07 stsp iblob = imsg.data;
1295 291624d8 2018-11-07 stsp *size = iblob->size;
1296 ebc55e2d 2018-12-24 stsp *hdrlen = iblob->hdrlen;
1297 ac544f8c 2019-01-13 stsp
1298 ac544f8c 2019-01-13 stsp if (datalen == sizeof(*iblob)) {
1299 ac544f8c 2019-01-13 stsp /* Data has been written to file descriptor. */
1300 ac544f8c 2019-01-13 stsp break;
1301 ac544f8c 2019-01-13 stsp }
1302 ac544f8c 2019-01-13 stsp
1303 ac544f8c 2019-01-13 stsp if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1304 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1305 ac544f8c 2019-01-13 stsp break;
1306 ac544f8c 2019-01-13 stsp }
1307 ac544f8c 2019-01-13 stsp
1308 ac544f8c 2019-01-13 stsp *outbuf = malloc(*size);
1309 ac544f8c 2019-01-13 stsp if (*outbuf == NULL) {
1310 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1311 ac544f8c 2019-01-13 stsp break;
1312 ac544f8c 2019-01-13 stsp }
1313 ac544f8c 2019-01-13 stsp memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1314 f4a881ce 2018-11-17 stsp break;
1315 f4a881ce 2018-11-17 stsp default:
1316 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1317 f4a881ce 2018-11-17 stsp break;
1318 f4a881ce 2018-11-17 stsp }
1319 f4a881ce 2018-11-17 stsp
1320 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
1321 f4a881ce 2018-11-17 stsp
1322 f4a881ce 2018-11-17 stsp return err;
1323 f4a881ce 2018-11-17 stsp }
1324 f4a881ce 2018-11-17 stsp
1325 f4a881ce 2018-11-17 stsp static const struct got_error *
1326 f4a881ce 2018-11-17 stsp send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1327 f4a881ce 2018-11-17 stsp {
1328 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1329 f4a881ce 2018-11-17 stsp size_t offset, remain;
1330 f4a881ce 2018-11-17 stsp
1331 f4a881ce 2018-11-17 stsp offset = 0;
1332 f4a881ce 2018-11-17 stsp remain = tagmsg_len;
1333 f4a881ce 2018-11-17 stsp while (remain > 0) {
1334 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1335 f4a881ce 2018-11-17 stsp
1336 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1337 f4a881ce 2018-11-17 stsp tag->tagmsg + offset, n) == -1) {
1338 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1339 f4a881ce 2018-11-17 stsp break;
1340 f4a881ce 2018-11-17 stsp }
1341 f4a881ce 2018-11-17 stsp
1342 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1343 f4a881ce 2018-11-17 stsp if (err)
1344 f4a881ce 2018-11-17 stsp break;
1345 f4a881ce 2018-11-17 stsp
1346 f4a881ce 2018-11-17 stsp offset += n;
1347 f4a881ce 2018-11-17 stsp remain -= n;
1348 f4a881ce 2018-11-17 stsp }
1349 f4a881ce 2018-11-17 stsp
1350 f4a881ce 2018-11-17 stsp return err;
1351 f4a881ce 2018-11-17 stsp }
1352 f4a881ce 2018-11-17 stsp
1353 f4a881ce 2018-11-17 stsp const struct got_error *
1354 f4a881ce 2018-11-17 stsp got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1355 f4a881ce 2018-11-17 stsp {
1356 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1357 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1358 f4a881ce 2018-11-17 stsp uint8_t *buf;
1359 f4a881ce 2018-11-17 stsp size_t len, total;
1360 f4a881ce 2018-11-17 stsp size_t tag_len = strlen(tag->tag);
1361 f4a881ce 2018-11-17 stsp size_t tagger_len = strlen(tag->tagger);
1362 f4a881ce 2018-11-17 stsp size_t tagmsg_len = strlen(tag->tagmsg);
1363 f4a881ce 2018-11-17 stsp
1364 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1365 f4a881ce 2018-11-17 stsp
1366 f4a881ce 2018-11-17 stsp buf = malloc(total);
1367 f4a881ce 2018-11-17 stsp if (buf == NULL)
1368 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1369 f4a881ce 2018-11-17 stsp
1370 f4a881ce 2018-11-17 stsp itag = (struct got_imsg_tag_object *)buf;
1371 f4a881ce 2018-11-17 stsp memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1372 f4a881ce 2018-11-17 stsp itag->obj_type = tag->obj_type;
1373 f4a881ce 2018-11-17 stsp itag->tag_len = tag_len;
1374 f4a881ce 2018-11-17 stsp itag->tagger_len = tagger_len;
1375 f4a881ce 2018-11-17 stsp itag->tagger_time = tag->tagger_time;
1376 f4a881ce 2018-11-17 stsp itag->tagger_gmtoff = tag->tagger_gmtoff;
1377 f4a881ce 2018-11-17 stsp itag->tagmsg_len = tagmsg_len;
1378 f4a881ce 2018-11-17 stsp
1379 f4a881ce 2018-11-17 stsp len = sizeof(*itag);
1380 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tag, tag_len);
1381 f4a881ce 2018-11-17 stsp len += tag_len;
1382 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tagger, tagger_len);
1383 f4a881ce 2018-11-17 stsp len += tagger_len;
1384 f4a881ce 2018-11-17 stsp
1385 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1386 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG");
1387 f4a881ce 2018-11-17 stsp goto done;
1388 f4a881ce 2018-11-17 stsp }
1389 f4a881ce 2018-11-17 stsp
1390 f4a881ce 2018-11-17 stsp if (tagmsg_len == 0 ||
1391 f4a881ce 2018-11-17 stsp tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1392 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1393 f4a881ce 2018-11-17 stsp if (err)
1394 f4a881ce 2018-11-17 stsp goto done;
1395 f4a881ce 2018-11-17 stsp }
1396 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
1397 f4a881ce 2018-11-17 stsp done:
1398 f4a881ce 2018-11-17 stsp free(buf);
1399 f4a881ce 2018-11-17 stsp return err;
1400 f4a881ce 2018-11-17 stsp }
1401 f4a881ce 2018-11-17 stsp
1402 f4a881ce 2018-11-17 stsp const struct got_error *
1403 f4a881ce 2018-11-17 stsp got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1404 f4a881ce 2018-11-17 stsp {
1405 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1406 f4a881ce 2018-11-17 stsp struct imsg imsg;
1407 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1408 f4a881ce 2018-11-17 stsp size_t len, datalen;
1409 f4a881ce 2018-11-17 stsp const size_t min_datalen =
1410 f4a881ce 2018-11-17 stsp MIN(sizeof(struct got_imsg_error),
1411 f4a881ce 2018-11-17 stsp sizeof(struct got_imsg_tag_object));
1412 f4a881ce 2018-11-17 stsp
1413 f4a881ce 2018-11-17 stsp *tag = NULL;
1414 f4a881ce 2018-11-17 stsp
1415 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1416 f4a881ce 2018-11-17 stsp if (err)
1417 f4a881ce 2018-11-17 stsp return err;
1418 f4a881ce 2018-11-17 stsp
1419 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1420 f4a881ce 2018-11-17 stsp len = 0;
1421 f4a881ce 2018-11-17 stsp
1422 f4a881ce 2018-11-17 stsp switch (imsg.hdr.type) {
1423 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG:
1424 f4a881ce 2018-11-17 stsp if (datalen < sizeof(*itag)) {
1425 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1426 f4a881ce 2018-11-17 stsp break;
1427 f4a881ce 2018-11-17 stsp }
1428 f4a881ce 2018-11-17 stsp itag = imsg.data;
1429 f4a881ce 2018-11-17 stsp if (datalen != sizeof(*itag) + itag->tag_len +
1430 f4a881ce 2018-11-17 stsp itag->tagger_len) {
1431 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1432 f4a881ce 2018-11-17 stsp break;
1433 f4a881ce 2018-11-17 stsp }
1434 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
1435 f4a881ce 2018-11-17 stsp
1436 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
1437 f4a881ce 2018-11-17 stsp if (*tag == NULL) {
1438 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1439 f4a881ce 2018-11-17 stsp break;
1440 f4a881ce 2018-11-17 stsp }
1441 f4a881ce 2018-11-17 stsp
1442 f4a881ce 2018-11-17 stsp memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1443 f4a881ce 2018-11-17 stsp
1444 f4a881ce 2018-11-17 stsp if (itag->tag_len == 0) {
1445 f4a881ce 2018-11-17 stsp (*tag)->tag = strdup("");
1446 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1447 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1448 f4a881ce 2018-11-17 stsp break;
1449 f4a881ce 2018-11-17 stsp }
1450 f4a881ce 2018-11-17 stsp } else {
1451 f4a881ce 2018-11-17 stsp (*tag)->tag = malloc(itag->tag_len + 1);
1452 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1453 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1454 f4a881ce 2018-11-17 stsp break;
1455 f4a881ce 2018-11-17 stsp }
1456 f4a881ce 2018-11-17 stsp memcpy((*tag)->tag, imsg.data + len,
1457 f4a881ce 2018-11-17 stsp itag->tag_len);
1458 f4a881ce 2018-11-17 stsp (*tag)->tag[itag->tag_len] = '\0';
1459 f4a881ce 2018-11-17 stsp }
1460 f4a881ce 2018-11-17 stsp len += itag->tag_len;
1461 f4a881ce 2018-11-17 stsp
1462 f4a881ce 2018-11-17 stsp (*tag)->obj_type = itag->obj_type;
1463 f4a881ce 2018-11-17 stsp (*tag)->tagger_time = itag->tagger_time;
1464 f4a881ce 2018-11-17 stsp (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1465 f4a881ce 2018-11-17 stsp
1466 f4a881ce 2018-11-17 stsp if (itag->tagger_len == 0) {
1467 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup("");
1468 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1469 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1470 f4a881ce 2018-11-17 stsp break;
1471 f4a881ce 2018-11-17 stsp }
1472 f4a881ce 2018-11-17 stsp } else {
1473 f4a881ce 2018-11-17 stsp (*tag)->tagger = malloc(itag->tagger_len + 1);
1474 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1475 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1476 f4a881ce 2018-11-17 stsp break;
1477 f4a881ce 2018-11-17 stsp }
1478 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagger, imsg.data + len,
1479 f4a881ce 2018-11-17 stsp itag->tagger_len);
1480 f4a881ce 2018-11-17 stsp (*tag)->tagger[itag->tagger_len] = '\0';
1481 f4a881ce 2018-11-17 stsp }
1482 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1483 f4a881ce 2018-11-17 stsp
1484 f4a881ce 2018-11-17 stsp if (itag->tagmsg_len == 0) {
1485 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strdup("");
1486 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1488 f4a881ce 2018-11-17 stsp break;
1489 f4a881ce 2018-11-17 stsp }
1490 f4a881ce 2018-11-17 stsp } else {
1491 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1492 f4a881ce 2018-11-17 stsp
1493 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1494 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1495 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1496 f4a881ce 2018-11-17 stsp break;
1497 f4a881ce 2018-11-17 stsp }
1498 f4a881ce 2018-11-17 stsp while (remain > 0) {
1499 f4a881ce 2018-11-17 stsp struct imsg imsg_log;
1500 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1501 f4a881ce 2018-11-17 stsp remain);
1502 f4a881ce 2018-11-17 stsp
1503 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1504 f4a881ce 2018-11-17 stsp if (err)
1505 f4a881ce 2018-11-17 stsp return err;
1506 f4a881ce 2018-11-17 stsp
1507 f4a881ce 2018-11-17 stsp if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1508 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1509 f4a881ce 2018-11-17 stsp
1510 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1511 f4a881ce 2018-11-17 stsp n);
1512 f4a881ce 2018-11-17 stsp imsg_free(&imsg_log);
1513 f4a881ce 2018-11-17 stsp offset += n;
1514 f4a881ce 2018-11-17 stsp remain -= n;
1515 f4a881ce 2018-11-17 stsp }
1516 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1517 f4a881ce 2018-11-17 stsp }
1518 f4a881ce 2018-11-17 stsp
1519 ff6b18f8 2018-04-24 stsp break;
1520 ff6b18f8 2018-04-24 stsp default:
1521 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1522 ff6b18f8 2018-04-24 stsp break;
1523 e033d803 2018-04-23 stsp }
1524 e033d803 2018-04-23 stsp
1525 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1526 ff6b18f8 2018-04-24 stsp
1527 2178c42e 2018-04-22 stsp return err;
1528 2178c42e 2018-04-22 stsp }
1529 876c234b 2018-09-10 stsp
1530 876c234b 2018-09-10 stsp const struct got_error *
1531 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1532 876c234b 2018-09-10 stsp struct got_packidx *packidx)
1533 876c234b 2018-09-10 stsp {
1534 41496140 2019-02-21 stsp const struct got_error *err = NULL;
1535 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1536 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1537 876c234b 2018-09-10 stsp int fd;
1538 876c234b 2018-09-10 stsp
1539 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
1540 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
1541 876c234b 2018-09-10 stsp if (fd == -1)
1542 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1543 876c234b 2018-09-10 stsp
1544 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1545 41496140 2019-02-21 stsp sizeof(ipackidx)) == -1) {
1546 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACKIDX");
1547 41496140 2019-02-21 stsp close(fd);
1548 41496140 2019-02-21 stsp return err;
1549 41496140 2019-02-21 stsp }
1550 876c234b 2018-09-10 stsp
1551 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
1552 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1553 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
1554 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
1555 876c234b 2018-09-10 stsp
1556 876c234b 2018-09-10 stsp fd = dup(pack->fd);
1557 876c234b 2018-09-10 stsp if (fd == -1)
1558 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1559 876c234b 2018-09-10 stsp
1560 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1561 41496140 2019-02-21 stsp == -1) {
1562 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACK");
1563 41496140 2019-02-21 stsp close(fd);
1564 41496140 2019-02-21 stsp return err;
1565 41496140 2019-02-21 stsp }
1566 876c234b 2018-09-10 stsp
1567 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1568 876c234b 2018-09-10 stsp }
1569 876c234b 2018-09-10 stsp
1570 876c234b 2018-09-10 stsp const struct got_error *
1571 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1572 106807b4 2018-09-15 stsp struct got_object_id *id)
1573 876c234b 2018-09-10 stsp {
1574 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
1575 876c234b 2018-09-10 stsp
1576 876c234b 2018-09-10 stsp iobj.idx = idx;
1577 106807b4 2018-09-15 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1578 876c234b 2018-09-10 stsp
1579 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1580 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
1581 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose "
1582 230a42bd 2019-05-11 jcs "PACKED_OBJECT_REQUEST");
1583 aba9c984 2019-09-08 stsp
1584 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1585 aba9c984 2019-09-08 stsp }
1586 aba9c984 2019-09-08 stsp
1587 aba9c984 2019-09-08 stsp const struct got_error *
1588 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1589 aba9c984 2019-09-08 stsp {
1590 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1591 aba9c984 2019-09-08 stsp
1592 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1593 aba9c984 2019-09-08 stsp NULL, 0) == -1) {
1594 aba9c984 2019-09-08 stsp err = got_error_from_errno("imsg_compose "
1595 aba9c984 2019-09-08 stsp "GITCONFIG_PARSE_REQUEST");
1596 aba9c984 2019-09-08 stsp close(fd);
1597 aba9c984 2019-09-08 stsp return err;
1598 aba9c984 2019-09-08 stsp }
1599 aba9c984 2019-09-08 stsp
1600 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1601 aba9c984 2019-09-08 stsp }
1602 aba9c984 2019-09-08 stsp
1603 aba9c984 2019-09-08 stsp const struct got_error *
1604 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1605 aba9c984 2019-09-08 stsp {
1606 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1607 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1608 aba9c984 2019-09-08 stsp NULL, 0) == -1)
1609 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1610 aba9c984 2019-09-08 stsp "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1611 aba9c984 2019-09-08 stsp
1612 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1613 aba9c984 2019-09-08 stsp }
1614 aba9c984 2019-09-08 stsp
1615 aba9c984 2019-09-08 stsp const struct got_error *
1616 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1617 aba9c984 2019-09-08 stsp {
1618 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1619 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1620 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1621 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_NAME_REQUEST");
1622 aba9c984 2019-09-08 stsp
1623 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1624 aba9c984 2019-09-08 stsp }
1625 aba9c984 2019-09-08 stsp
1626 aba9c984 2019-09-08 stsp const struct got_error *
1627 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1628 aba9c984 2019-09-08 stsp {
1629 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1630 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1631 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1632 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1633 cd95becd 2019-11-29 stsp
1634 cd95becd 2019-11-29 stsp return flush_imsg(ibuf);
1635 cd95becd 2019-11-29 stsp }
1636 cd95becd 2019-11-29 stsp
1637 cd95becd 2019-11-29 stsp const struct got_error *
1638 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1639 cd95becd 2019-11-29 stsp {
1640 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf,
1641 cd95becd 2019-11-29 stsp GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1642 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose "
1643 cd95becd 2019-11-29 stsp "GITCONFIG_REMOTE_REQUEST");
1644 aba9c984 2019-09-08 stsp
1645 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1646 aba9c984 2019-09-08 stsp }
1647 aba9c984 2019-09-08 stsp
1648 aba9c984 2019-09-08 stsp const struct got_error *
1649 9a1cc63f 2020-02-03 stsp got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1650 9a1cc63f 2020-02-03 stsp {
1651 9a1cc63f 2020-02-03 stsp if (imsg_compose(ibuf,
1652 9a1cc63f 2020-02-03 stsp GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1653 9a1cc63f 2020-02-03 stsp return got_error_from_errno("imsg_compose "
1654 9a1cc63f 2020-02-03 stsp "GITCONFIG_OWNER_REQUEST");
1655 9a1cc63f 2020-02-03 stsp
1656 9a1cc63f 2020-02-03 stsp return flush_imsg(ibuf);
1657 9a1cc63f 2020-02-03 stsp }
1658 9a1cc63f 2020-02-03 stsp
1659 9a1cc63f 2020-02-03 stsp const struct got_error *
1660 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1661 aba9c984 2019-09-08 stsp {
1662 aba9c984 2019-09-08 stsp size_t len = value ? strlen(value) + 1 : 0;
1663 aba9c984 2019-09-08 stsp
1664 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1665 aba9c984 2019-09-08 stsp value, len) == -1)
1666 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1667 aba9c984 2019-09-08 stsp
1668 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1669 aba9c984 2019-09-08 stsp }
1670 aba9c984 2019-09-08 stsp
1671 aba9c984 2019-09-08 stsp const struct got_error *
1672 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1673 aba9c984 2019-09-08 stsp {
1674 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1675 aba9c984 2019-09-08 stsp struct imsg imsg;
1676 aba9c984 2019-09-08 stsp size_t datalen;
1677 aba9c984 2019-09-08 stsp const size_t min_datalen = 0;
1678 aba9c984 2019-09-08 stsp
1679 aba9c984 2019-09-08 stsp *str = NULL;
1680 aba9c984 2019-09-08 stsp
1681 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1682 aba9c984 2019-09-08 stsp if (err)
1683 aba9c984 2019-09-08 stsp return err;
1684 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1685 aba9c984 2019-09-08 stsp
1686 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1687 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_STR_VAL:
1688 aba9c984 2019-09-08 stsp if (datalen == 0)
1689 aba9c984 2019-09-08 stsp break;
1690 aba9c984 2019-09-08 stsp *str = malloc(datalen);
1691 aba9c984 2019-09-08 stsp if (*str == NULL) {
1692 aba9c984 2019-09-08 stsp err = got_error_from_errno("malloc");
1693 aba9c984 2019-09-08 stsp break;
1694 aba9c984 2019-09-08 stsp }
1695 aba9c984 2019-09-08 stsp if (strlcpy(*str, imsg.data, datalen) >= datalen)
1696 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_NO_SPACE);
1697 aba9c984 2019-09-08 stsp break;
1698 aba9c984 2019-09-08 stsp default:
1699 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1700 aba9c984 2019-09-08 stsp break;
1701 aba9c984 2019-09-08 stsp }
1702 876c234b 2018-09-10 stsp
1703 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1704 aba9c984 2019-09-08 stsp return err;
1705 aba9c984 2019-09-08 stsp }
1706 aba9c984 2019-09-08 stsp
1707 aba9c984 2019-09-08 stsp const struct got_error *
1708 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1709 aba9c984 2019-09-08 stsp {
1710 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1711 aba9c984 2019-09-08 stsp &value, sizeof(value)) == -1)
1712 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1713 aba9c984 2019-09-08 stsp
1714 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1715 63219cd2 2019-01-04 stsp }
1716 63219cd2 2019-01-04 stsp
1717 63219cd2 2019-01-04 stsp const struct got_error *
1718 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1719 aba9c984 2019-09-08 stsp {
1720 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1721 aba9c984 2019-09-08 stsp struct imsg imsg;
1722 aba9c984 2019-09-08 stsp size_t datalen;
1723 aba9c984 2019-09-08 stsp const size_t min_datalen =
1724 aba9c984 2019-09-08 stsp MIN(sizeof(struct got_imsg_error), sizeof(int));
1725 aba9c984 2019-09-08 stsp
1726 aba9c984 2019-09-08 stsp *val = 0;
1727 aba9c984 2019-09-08 stsp
1728 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1729 aba9c984 2019-09-08 stsp if (err)
1730 aba9c984 2019-09-08 stsp return err;
1731 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1732 aba9c984 2019-09-08 stsp
1733 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1734 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_INT_VAL:
1735 aba9c984 2019-09-08 stsp if (datalen != sizeof(*val)) {
1736 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1737 aba9c984 2019-09-08 stsp break;
1738 aba9c984 2019-09-08 stsp }
1739 aba9c984 2019-09-08 stsp memcpy(val, imsg.data, sizeof(*val));
1740 cd95becd 2019-11-29 stsp break;
1741 cd95becd 2019-11-29 stsp default:
1742 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1743 cd95becd 2019-11-29 stsp break;
1744 cd95becd 2019-11-29 stsp }
1745 cd95becd 2019-11-29 stsp
1746 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1747 cd95becd 2019-11-29 stsp return err;
1748 cd95becd 2019-11-29 stsp }
1749 cd95becd 2019-11-29 stsp
1750 cd95becd 2019-11-29 stsp const struct got_error *
1751 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1752 cd95becd 2019-11-29 stsp struct got_remote_repo *remotes, int nremotes)
1753 cd95becd 2019-11-29 stsp {
1754 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1755 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
1756 cd95becd 2019-11-29 stsp int i;
1757 cd95becd 2019-11-29 stsp
1758 cd95becd 2019-11-29 stsp iremotes.nremotes = nremotes;
1759 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1760 cd95becd 2019-11-29 stsp &iremotes, sizeof(iremotes)) == -1)
1761 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1762 cd95becd 2019-11-29 stsp
1763 cd95becd 2019-11-29 stsp err = flush_imsg(ibuf);
1764 cd95becd 2019-11-29 stsp imsg_clear(ibuf);
1765 cd95becd 2019-11-29 stsp if (err)
1766 cd95becd 2019-11-29 stsp return err;
1767 cd95becd 2019-11-29 stsp
1768 cd95becd 2019-11-29 stsp for (i = 0; i < nremotes; i++) {
1769 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
1770 cd95becd 2019-11-29 stsp size_t len = sizeof(iremote);
1771 cd95becd 2019-11-29 stsp struct ibuf *wbuf;
1772 cd95becd 2019-11-29 stsp
1773 cd95becd 2019-11-29 stsp iremote.name_len = strlen(remotes[i].name);
1774 cd95becd 2019-11-29 stsp len += iremote.name_len;
1775 cd95becd 2019-11-29 stsp iremote.url_len = strlen(remotes[i].url);
1776 cd95becd 2019-11-29 stsp len += iremote.url_len;
1777 cd95becd 2019-11-29 stsp
1778 cd95becd 2019-11-29 stsp wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1779 cd95becd 2019-11-29 stsp if (wbuf == NULL)
1780 cd95becd 2019-11-29 stsp return got_error_from_errno(
1781 cd95becd 2019-11-29 stsp "imsg_create GITCONFIG_REMOTE");
1782 cd95becd 2019-11-29 stsp
1783 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1784 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1785 cd95becd 2019-11-29 stsp "imsg_add GIITCONFIG_REMOTE");
1786 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1787 cd95becd 2019-11-29 stsp return err;
1788 cd95becd 2019-11-29 stsp }
1789 cd95becd 2019-11-29 stsp
1790 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1791 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1792 cd95becd 2019-11-29 stsp "imsg_add GIITCONFIG_REMOTE");
1793 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1794 cd95becd 2019-11-29 stsp return err;
1795 cd95becd 2019-11-29 stsp }
1796 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1797 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1798 cd95becd 2019-11-29 stsp "imsg_add GIITCONFIG_REMOTE");
1799 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1800 cd95becd 2019-11-29 stsp return err;
1801 cd95becd 2019-11-29 stsp }
1802 cd95becd 2019-11-29 stsp
1803 cd95becd 2019-11-29 stsp wbuf->fd = -1;
1804 cd95becd 2019-11-29 stsp imsg_close(ibuf, wbuf);
1805 cd95becd 2019-11-29 stsp err = flush_imsg(ibuf);
1806 cd95becd 2019-11-29 stsp if (err)
1807 cd95becd 2019-11-29 stsp return err;
1808 cd95becd 2019-11-29 stsp }
1809 cd95becd 2019-11-29 stsp
1810 cd95becd 2019-11-29 stsp return NULL;
1811 cd95becd 2019-11-29 stsp }
1812 cd95becd 2019-11-29 stsp
1813 cd95becd 2019-11-29 stsp const struct got_error *
1814 cd95becd 2019-11-29 stsp got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1815 cd95becd 2019-11-29 stsp int *nremotes, struct imsgbuf *ibuf)
1816 cd95becd 2019-11-29 stsp {
1817 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1818 cd95becd 2019-11-29 stsp struct imsg imsg;
1819 cd95becd 2019-11-29 stsp size_t datalen;
1820 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
1821 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
1822 cd95becd 2019-11-29 stsp
1823 cd95becd 2019-11-29 stsp *remotes = NULL;
1824 cd95becd 2019-11-29 stsp *nremotes = 0;
1825 d669b9c9 2020-02-22 stsp iremotes.nremotes = 0;
1826 cd95becd 2019-11-29 stsp
1827 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1828 cd95becd 2019-11-29 stsp if (err)
1829 cd95becd 2019-11-29 stsp return err;
1830 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1831 cd95becd 2019-11-29 stsp
1832 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
1833 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES:
1834 cd95becd 2019-11-29 stsp if (datalen != sizeof(iremotes)) {
1835 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1836 cd95becd 2019-11-29 stsp break;
1837 cd95becd 2019-11-29 stsp }
1838 cd95becd 2019-11-29 stsp memcpy(&iremotes, imsg.data, sizeof(iremotes));
1839 cd95becd 2019-11-29 stsp if (iremotes.nremotes == 0) {
1840 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1841 cd95becd 2019-11-29 stsp return NULL;
1842 cd95becd 2019-11-29 stsp }
1843 aba9c984 2019-09-08 stsp break;
1844 aba9c984 2019-09-08 stsp default:
1845 54b1c5b5 2020-02-22 stsp imsg_free(&imsg);
1846 54b1c5b5 2020-02-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1847 aba9c984 2019-09-08 stsp }
1848 aba9c984 2019-09-08 stsp
1849 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1850 cd95becd 2019-11-29 stsp
1851 cd95becd 2019-11-29 stsp *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1852 cd95becd 2019-11-29 stsp if (*remotes == NULL)
1853 cd95becd 2019-11-29 stsp return got_error_from_errno("recallocarray");
1854 cd95becd 2019-11-29 stsp
1855 cd95becd 2019-11-29 stsp while (*nremotes < iremotes.nremotes) {
1856 cd95becd 2019-11-29 stsp struct got_remote_repo *remote;
1857 cd95becd 2019-11-29 stsp
1858 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1859 cd95becd 2019-11-29 stsp if (err)
1860 cd95becd 2019-11-29 stsp break;
1861 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1862 cd95becd 2019-11-29 stsp
1863 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
1864 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTE:
1865 cd95becd 2019-11-29 stsp remote = &(*remotes)[*nremotes];
1866 cd95becd 2019-11-29 stsp if (datalen < sizeof(iremote)) {
1867 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1868 cd95becd 2019-11-29 stsp break;
1869 cd95becd 2019-11-29 stsp }
1870 cd95becd 2019-11-29 stsp memcpy(&iremote, imsg.data, sizeof(iremote));
1871 cd95becd 2019-11-29 stsp if (iremote.name_len == 0 || iremote.url_len == 0 ||
1872 cd95becd 2019-11-29 stsp (sizeof(iremote) + iremote.name_len +
1873 cd95becd 2019-11-29 stsp iremote.url_len) > datalen) {
1874 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1875 cd95becd 2019-11-29 stsp break;
1876 cd95becd 2019-11-29 stsp }
1877 cd95becd 2019-11-29 stsp remote->name = strndup(imsg.data + sizeof(iremote),
1878 cd95becd 2019-11-29 stsp iremote.name_len);
1879 cd95becd 2019-11-29 stsp if (remote->name == NULL) {
1880 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
1881 cd95becd 2019-11-29 stsp break;
1882 cd95becd 2019-11-29 stsp }
1883 cd95becd 2019-11-29 stsp remote->url = strndup(imsg.data + sizeof(iremote) +
1884 cd95becd 2019-11-29 stsp iremote.name_len, iremote.url_len);
1885 cd95becd 2019-11-29 stsp if (remote->url == NULL) {
1886 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
1887 cd95becd 2019-11-29 stsp free(remote->name);
1888 cd95becd 2019-11-29 stsp break;
1889 cd95becd 2019-11-29 stsp }
1890 cd95becd 2019-11-29 stsp (*nremotes)++;
1891 cd95becd 2019-11-29 stsp break;
1892 cd95becd 2019-11-29 stsp default:
1893 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1894 cd95becd 2019-11-29 stsp break;
1895 cd95becd 2019-11-29 stsp }
1896 cd95becd 2019-11-29 stsp
1897 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1898 cd95becd 2019-11-29 stsp if (err)
1899 cd95becd 2019-11-29 stsp break;
1900 cd95becd 2019-11-29 stsp }
1901 cd95becd 2019-11-29 stsp
1902 cd95becd 2019-11-29 stsp if (err) {
1903 cd95becd 2019-11-29 stsp int i;
1904 cd95becd 2019-11-29 stsp for (i = 0; i < *nremotes; i++) {
1905 cd95becd 2019-11-29 stsp free((*remotes)[i].name);
1906 cd95becd 2019-11-29 stsp free((*remotes)[i].url);
1907 cd95becd 2019-11-29 stsp }
1908 cd95becd 2019-11-29 stsp free(*remotes);
1909 cd95becd 2019-11-29 stsp *remotes = NULL;
1910 cd95becd 2019-11-29 stsp *nremotes = 0;
1911 cd95becd 2019-11-29 stsp }
1912 aba9c984 2019-09-08 stsp return err;
1913 ca6e02ac 2020-01-07 stsp }
1914 ca6e02ac 2020-01-07 stsp
1915 ca6e02ac 2020-01-07 stsp const struct got_error *
1916 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1917 ca6e02ac 2020-01-07 stsp struct got_object_id *id, int idx, const char *path)
1918 ca6e02ac 2020-01-07 stsp {
1919 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1920 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
1921 ca6e02ac 2020-01-07 stsp size_t path_len = strlen(path) + 1;
1922 ca6e02ac 2020-01-07 stsp
1923 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1924 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_traversal_request) + path_len);
1925 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
1926 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
1927 ca6e02ac 2020-01-07 stsp "imsg_create COMMIT_TRAVERSAL_REQUEST");
1928 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1929 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1930 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1931 ca6e02ac 2020-01-07 stsp return err;
1932 ca6e02ac 2020-01-07 stsp }
1933 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1934 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1935 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1936 ca6e02ac 2020-01-07 stsp return err;
1937 ca6e02ac 2020-01-07 stsp }
1938 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, path, path_len) == -1) {
1939 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1940 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1941 ca6e02ac 2020-01-07 stsp return err;
1942 ca6e02ac 2020-01-07 stsp }
1943 ca6e02ac 2020-01-07 stsp
1944 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
1945 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
1946 ca6e02ac 2020-01-07 stsp
1947 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
1948 aba9c984 2019-09-08 stsp }
1949 aba9c984 2019-09-08 stsp
1950 aba9c984 2019-09-08 stsp const struct got_error *
1951 ca6e02ac 2020-01-07 stsp got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1952 ca6e02ac 2020-01-07 stsp size_t ncommits, struct imsgbuf *ibuf)
1953 ca6e02ac 2020-01-07 stsp {
1954 ca6e02ac 2020-01-07 stsp const struct got_error *err;
1955 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
1956 ca6e02ac 2020-01-07 stsp int i;
1957 ca6e02ac 2020-01-07 stsp
1958 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
1959 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_traversed_commits) +
1960 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH);
1961 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
1962 ca6e02ac 2020-01-07 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
1963 ca6e02ac 2020-01-07 stsp
1964 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
1965 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
1966 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1967 ca6e02ac 2020-01-07 stsp return err;
1968 ca6e02ac 2020-01-07 stsp }
1969 ca6e02ac 2020-01-07 stsp for (i = 0; i < ncommits; i++) {
1970 ca6e02ac 2020-01-07 stsp struct got_object_id *id = &commit_ids[i];
1971 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1972 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
1973 ca6e02ac 2020-01-07 stsp "imsg_add TRAVERSED_COMMITS");
1974 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1975 ca6e02ac 2020-01-07 stsp return err;
1976 ca6e02ac 2020-01-07 stsp }
1977 ca6e02ac 2020-01-07 stsp }
1978 ca6e02ac 2020-01-07 stsp
1979 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
1980 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
1981 ca6e02ac 2020-01-07 stsp
1982 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
1983 ca6e02ac 2020-01-07 stsp }
1984 ca6e02ac 2020-01-07 stsp
1985 ca6e02ac 2020-01-07 stsp const struct got_error *
1986 ca6e02ac 2020-01-07 stsp got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1987 ca6e02ac 2020-01-07 stsp struct got_object_id **changed_commit_id,
1988 ca6e02ac 2020-01-07 stsp struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1989 ca6e02ac 2020-01-07 stsp {
1990 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1991 ca6e02ac 2020-01-07 stsp struct imsg imsg;
1992 ca6e02ac 2020-01-07 stsp struct got_imsg_traversed_commits *icommits;
1993 ca6e02ac 2020-01-07 stsp size_t datalen;
1994 ca6e02ac 2020-01-07 stsp int i, done = 0;
1995 ca6e02ac 2020-01-07 stsp
1996 ca6e02ac 2020-01-07 stsp *changed_commit = NULL;
1997 ca6e02ac 2020-01-07 stsp *changed_commit_id = NULL;
1998 ca6e02ac 2020-01-07 stsp
1999 ca6e02ac 2020-01-07 stsp while (!done) {
2000 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2001 ca6e02ac 2020-01-07 stsp if (err)
2002 ca6e02ac 2020-01-07 stsp return err;
2003 ca6e02ac 2020-01-07 stsp
2004 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2005 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
2006 ca6e02ac 2020-01-07 stsp case GOT_IMSG_TRAVERSED_COMMITS:
2007 ca6e02ac 2020-01-07 stsp icommits = imsg.data;
2008 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommits) +
2009 ca6e02ac 2020-01-07 stsp icommits->ncommits * SHA1_DIGEST_LENGTH) {
2010 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2011 ca6e02ac 2020-01-07 stsp break;
2012 ca6e02ac 2020-01-07 stsp }
2013 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommits->ncommits; i++) {
2014 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
2015 ca6e02ac 2020-01-07 stsp uint8_t *sha1 = (uint8_t *)imsg.data +
2016 ca6e02ac 2020-01-07 stsp sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2017 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
2018 ca6e02ac 2020-01-07 stsp if (err)
2019 ca6e02ac 2020-01-07 stsp break;
2020 ca6e02ac 2020-01-07 stsp memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2021 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2022 ca6e02ac 2020-01-07 stsp
2023 ca6e02ac 2020-01-07 stsp /* The last commit may contain a change. */
2024 ca6e02ac 2020-01-07 stsp if (i == icommits->ncommits - 1) {
2025 ca6e02ac 2020-01-07 stsp *changed_commit_id =
2026 ca6e02ac 2020-01-07 stsp got_object_id_dup(qid->id);
2027 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2028 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
2029 ca6e02ac 2020-01-07 stsp "got_object_id_dup");
2030 ca6e02ac 2020-01-07 stsp break;
2031 ca6e02ac 2020-01-07 stsp }
2032 ca6e02ac 2020-01-07 stsp }
2033 ca6e02ac 2020-01-07 stsp }
2034 ca6e02ac 2020-01-07 stsp break;
2035 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
2036 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2037 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2038 ca6e02ac 2020-01-07 stsp break;
2039 ca6e02ac 2020-01-07 stsp }
2040 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(changed_commit, &imsg,
2041 ca6e02ac 2020-01-07 stsp datalen, ibuf);
2042 ca6e02ac 2020-01-07 stsp break;
2043 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2044 ca6e02ac 2020-01-07 stsp done = 1;
2045 ca6e02ac 2020-01-07 stsp break;
2046 ca6e02ac 2020-01-07 stsp default:
2047 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2048 ca6e02ac 2020-01-07 stsp break;
2049 ca6e02ac 2020-01-07 stsp }
2050 ca6e02ac 2020-01-07 stsp
2051 ca6e02ac 2020-01-07 stsp imsg_free(&imsg);
2052 ca6e02ac 2020-01-07 stsp if (err)
2053 ca6e02ac 2020-01-07 stsp break;
2054 ca6e02ac 2020-01-07 stsp }
2055 ca6e02ac 2020-01-07 stsp
2056 ca6e02ac 2020-01-07 stsp if (err)
2057 ca6e02ac 2020-01-07 stsp got_object_id_queue_free(commit_ids);
2058 ca6e02ac 2020-01-07 stsp return err;
2059 ca6e02ac 2020-01-07 stsp }
2060 ca6e02ac 2020-01-07 stsp
2061 ca6e02ac 2020-01-07 stsp const struct got_error *
2062 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2063 ca6e02ac 2020-01-07 stsp {
2064 ca6e02ac 2020-01-07 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2065 ca6e02ac 2020-01-07 stsp NULL, 0) == -1)
2066 ca6e02ac 2020-01-07 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2067 ca6e02ac 2020-01-07 stsp
2068 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2069 ca6e02ac 2020-01-07 stsp }
2070 ca6e02ac 2020-01-07 stsp
2071 ca6e02ac 2020-01-07 stsp const struct got_error *
2072 63219cd2 2019-01-04 stsp got_privsep_unveil_exec_helpers(void)
2073 63219cd2 2019-01-04 stsp {
2074 c39c25dd 2019-08-09 stsp const char *helpers[] = {
2075 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_PACK,
2076 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_OBJECT,
2077 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_COMMIT,
2078 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TREE,
2079 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_BLOB,
2080 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TAG,
2081 aba9c984 2019-09-08 stsp GOT_PATH_PROG_READ_GITCONFIG,
2082 c39c25dd 2019-08-09 stsp };
2083 c39c25dd 2019-08-09 stsp int i;
2084 63219cd2 2019-01-04 stsp
2085 c39c25dd 2019-08-09 stsp for (i = 0; i < nitems(helpers); i++) {
2086 c39c25dd 2019-08-09 stsp if (unveil(helpers[i], "x") == 0)
2087 c39c25dd 2019-08-09 stsp continue;
2088 c39c25dd 2019-08-09 stsp return got_error_from_errno2("unveil", helpers[i]);
2089 c39c25dd 2019-08-09 stsp }
2090 c39c25dd 2019-08-09 stsp
2091 63219cd2 2019-01-04 stsp return NULL;
2092 876c234b 2018-09-10 stsp }
2093 aba9c984 2019-09-08 stsp
2094 aba9c984 2019-09-08 stsp void
2095 aba9c984 2019-09-08 stsp got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2096 aba9c984 2019-09-08 stsp {
2097 aba9c984 2019-09-08 stsp if (close(imsg_fds[0]) != 0) {
2098 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2099 aba9c984 2019-09-08 stsp _exit(1);
2100 aba9c984 2019-09-08 stsp }
2101 aba9c984 2019-09-08 stsp
2102 aba9c984 2019-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2103 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2104 aba9c984 2019-09-08 stsp _exit(1);
2105 aba9c984 2019-09-08 stsp }
2106 aba9c984 2019-09-08 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2107 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2108 aba9c984 2019-09-08 stsp _exit(1);
2109 aba9c984 2019-09-08 stsp }
2110 aba9c984 2019-09-08 stsp
2111 aba9c984 2019-09-08 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
2112 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2113 aba9c984 2019-09-08 stsp strerror(errno));
2114 aba9c984 2019-09-08 stsp _exit(1);
2115 aba9c984 2019-09-08 stsp }
2116 aba9c984 2019-09-08 stsp }