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