Blame


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