Blame


1 2178c42e 2018-04-22 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 2178c42e 2018-04-22 stsp *
4 2178c42e 2018-04-22 stsp * Permission to use, copy, modify, and distribute this software for any
5 2178c42e 2018-04-22 stsp * purpose with or without fee is hereby granted, provided that the above
6 2178c42e 2018-04-22 stsp * copyright notice and this permission notice appear in all copies.
7 2178c42e 2018-04-22 stsp *
8 2178c42e 2018-04-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2178c42e 2018-04-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2178c42e 2018-04-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2178c42e 2018-04-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2178c42e 2018-04-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2178c42e 2018-04-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2178c42e 2018-04-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 2178c42e 2018-04-22 stsp */
16 2178c42e 2018-04-22 stsp
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 2178c42e 2018-04-22 stsp #include <sys/queue.h>
19 2178c42e 2018-04-22 stsp #include <sys/uio.h>
20 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
21 876c234b 2018-09-10 stsp #include <sys/wait.h>
22 2178c42e 2018-04-22 stsp
23 2178c42e 2018-04-22 stsp #include <stdio.h>
24 2178c42e 2018-04-22 stsp #include <stdlib.h>
25 2178c42e 2018-04-22 stsp #include <string.h>
26 2178c42e 2018-04-22 stsp #include <errno.h>
27 2178c42e 2018-04-22 stsp #include <stdint.h>
28 2178c42e 2018-04-22 stsp #include <poll.h>
29 2178c42e 2018-04-22 stsp #include <imsg.h>
30 2178c42e 2018-04-22 stsp #include <sha1.h>
31 2178c42e 2018-04-22 stsp #include <zlib.h>
32 788c352e 2018-06-16 stsp #include <time.h>
33 2178c42e 2018-04-22 stsp
34 2178c42e 2018-04-22 stsp #include "got_object.h"
35 2178c42e 2018-04-22 stsp #include "got_error.h"
36 2178c42e 2018-04-22 stsp
37 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
38 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
39 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
40 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
41 a440fac0 2018-09-06 stsp #include "got_lib_object_parse.h"
42 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
43 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
44 2178c42e 2018-04-22 stsp
45 2178c42e 2018-04-22 stsp #ifndef MIN
46 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
47 2178c42e 2018-04-22 stsp #endif
48 2178c42e 2018-04-22 stsp
49 2178c42e 2018-04-22 stsp static const struct got_error *
50 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
51 2178c42e 2018-04-22 stsp {
52 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
53 2178c42e 2018-04-22 stsp int n;
54 2178c42e 2018-04-22 stsp
55 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
56 2178c42e 2018-04-22 stsp pfd[0].events = events;
57 2178c42e 2018-04-22 stsp
58 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
59 2178c42e 2018-04-22 stsp if (n == -1)
60 638f9024 2019-05-13 stsp return got_error_from_errno("poll");
61 2178c42e 2018-04-22 stsp if (n == 0)
62 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
63 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
64 638f9024 2019-05-13 stsp return got_error_from_errno("poll error");
65 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
66 2178c42e 2018-04-22 stsp return NULL;
67 2178c42e 2018-04-22 stsp
68 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
69 2178c42e 2018-04-22 stsp }
70 2178c42e 2018-04-22 stsp
71 c4eae628 2018-04-23 stsp static const struct got_error *
72 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
73 fe36cf76 2018-04-23 stsp {
74 fe36cf76 2018-04-23 stsp const struct got_error *err;
75 e033d803 2018-04-23 stsp size_t n;
76 fe36cf76 2018-04-23 stsp
77 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
78 fe36cf76 2018-04-23 stsp if (err)
79 fe36cf76 2018-04-23 stsp return err;
80 fe36cf76 2018-04-23 stsp
81 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
82 fe36cf76 2018-04-23 stsp if (n == -1) {
83 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
84 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
85 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
86 fe36cf76 2018-04-23 stsp }
87 fe36cf76 2018-04-23 stsp if (n == 0)
88 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
89 fe36cf76 2018-04-23 stsp
90 e033d803 2018-04-23 stsp return NULL;
91 e033d803 2018-04-23 stsp }
92 e033d803 2018-04-23 stsp
93 ad242220 2018-09-08 stsp const struct got_error *
94 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
95 876c234b 2018-09-10 stsp {
96 876c234b 2018-09-10 stsp int child_status;
97 876c234b 2018-09-10 stsp
98 2cb49fa8 2019-05-10 stsp if (waitpid(pid, &child_status, 0) == -1)
99 638f9024 2019-05-13 stsp return got_error_from_errno("waitpid");
100 876c234b 2018-09-10 stsp
101 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
102 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
103 876c234b 2018-09-10 stsp
104 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
105 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
106 876c234b 2018-09-10 stsp
107 876c234b 2018-09-10 stsp return NULL;
108 876c234b 2018-09-10 stsp }
109 876c234b 2018-09-10 stsp
110 73b7854a 2018-11-11 stsp static const struct got_error *
111 73b7854a 2018-11-11 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
112 73b7854a 2018-11-11 stsp {
113 73b7854a 2018-11-11 stsp struct got_imsg_error *ierr;
114 73b7854a 2018-11-11 stsp
115 73b7854a 2018-11-11 stsp if (datalen != sizeof(*ierr))
116 73b7854a 2018-11-11 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
117 73b7854a 2018-11-11 stsp
118 73b7854a 2018-11-11 stsp ierr = imsg->data;
119 73b7854a 2018-11-11 stsp if (ierr->code == GOT_ERR_ERRNO) {
120 73b7854a 2018-11-11 stsp static struct got_error serr;
121 73b7854a 2018-11-11 stsp serr.code = GOT_ERR_ERRNO;
122 73b7854a 2018-11-11 stsp serr.msg = strerror(ierr->errno_code);
123 73b7854a 2018-11-11 stsp return &serr;
124 73b7854a 2018-11-11 stsp }
125 73b7854a 2018-11-11 stsp
126 73b7854a 2018-11-11 stsp return got_error(ierr->code);
127 73b7854a 2018-11-11 stsp }
128 73b7854a 2018-11-11 stsp
129 876c234b 2018-09-10 stsp const struct got_error *
130 46de5bfd 2018-11-11 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
131 46de5bfd 2018-11-11 stsp size_t min_datalen)
132 e033d803 2018-04-23 stsp {
133 e033d803 2018-04-23 stsp const struct got_error *err;
134 e033d803 2018-04-23 stsp ssize_t n;
135 e033d803 2018-04-23 stsp
136 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
137 876c234b 2018-09-10 stsp if (n == -1)
138 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_get");
139 876c234b 2018-09-10 stsp
140 876c234b 2018-09-10 stsp while (n == 0) {
141 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
142 876c234b 2018-09-10 stsp if (err)
143 876c234b 2018-09-10 stsp return err;
144 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
145 876c234b 2018-09-10 stsp }
146 fe36cf76 2018-04-23 stsp
147 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
148 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
149 c4eae628 2018-04-23 stsp
150 73b7854a 2018-11-11 stsp if (imsg->hdr.type == GOT_IMSG_ERROR) {
151 73b7854a 2018-11-11 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
152 73b7854a 2018-11-11 stsp return recv_imsg_error(imsg, datalen);
153 c4eae628 2018-04-23 stsp }
154 c4eae628 2018-04-23 stsp
155 73b7854a 2018-11-11 stsp return NULL;
156 c4eae628 2018-04-23 stsp }
157 c4eae628 2018-04-23 stsp
158 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
159 2178c42e 2018-04-22 stsp void
160 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
161 2178c42e 2018-04-22 stsp {
162 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
163 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
164 2178c42e 2018-04-22 stsp int ret;
165 2178c42e 2018-04-22 stsp
166 2178c42e 2018-04-22 stsp ierr.code = err->code;
167 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
168 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
169 2178c42e 2018-04-22 stsp else
170 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
171 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
172 e93cd828 2018-11-11 stsp if (ret == -1) {
173 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
174 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
175 5d43e84d 2018-04-23 stsp return;
176 2178c42e 2018-04-22 stsp }
177 2178c42e 2018-04-22 stsp
178 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
179 5d43e84d 2018-04-23 stsp if (poll_err) {
180 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
181 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
182 5d43e84d 2018-04-23 stsp return;
183 5d43e84d 2018-04-23 stsp }
184 2178c42e 2018-04-22 stsp
185 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
186 5d43e84d 2018-04-23 stsp if (ret == -1) {
187 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
188 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
189 5d43e84d 2018-04-23 stsp return;
190 5d43e84d 2018-04-23 stsp }
191 e033d803 2018-04-23 stsp }
192 e033d803 2018-04-23 stsp
193 e033d803 2018-04-23 stsp static const struct got_error *
194 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
195 e033d803 2018-04-23 stsp {
196 e033d803 2018-04-23 stsp const struct got_error *err;
197 e033d803 2018-04-23 stsp
198 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
199 e033d803 2018-04-23 stsp if (err)
200 e033d803 2018-04-23 stsp return err;
201 e033d803 2018-04-23 stsp
202 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
203 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_flush");
204 e033d803 2018-04-23 stsp
205 e033d803 2018-04-23 stsp return NULL;
206 ad242220 2018-09-08 stsp }
207 ad242220 2018-09-08 stsp
208 ad242220 2018-09-08 stsp const struct got_error *
209 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
210 ad242220 2018-09-08 stsp {
211 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
212 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
213 ad242220 2018-09-08 stsp
214 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
215 ad242220 2018-09-08 stsp
216 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
217 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose STOP");
218 ad242220 2018-09-08 stsp
219 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
220 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
221 ad242220 2018-09-08 stsp return err;
222 7762fe12 2018-11-05 stsp }
223 7762fe12 2018-11-05 stsp
224 ad242220 2018-09-08 stsp const struct got_error *
225 aea5f015 2018-12-24 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
226 ad242220 2018-09-08 stsp {
227 aea5f015 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
228 aea5f015 2018-12-24 stsp == -1)
229 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT_REQUEST");
230 1785f84a 2018-12-23 stsp
231 1785f84a 2018-12-23 stsp return flush_imsg(ibuf);
232 1785f84a 2018-12-23 stsp }
233 1785f84a 2018-12-23 stsp
234 1785f84a 2018-12-23 stsp const struct got_error *
235 1785f84a 2018-12-23 stsp got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
236 1785f84a 2018-12-23 stsp struct got_object_id *id, int pack_idx)
237 1785f84a 2018-12-23 stsp {
238 41496140 2019-02-21 stsp const struct got_error *err = NULL;
239 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj, *iobjp;
240 1785f84a 2018-12-23 stsp size_t len;
241 1785f84a 2018-12-23 stsp
242 1785f84a 2018-12-23 stsp if (id) { /* commit is packed */
243 1785f84a 2018-12-23 stsp iobj.idx = pack_idx;
244 1785f84a 2018-12-23 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
245 1785f84a 2018-12-23 stsp iobjp = &iobj;
246 1785f84a 2018-12-23 stsp len = sizeof(iobj);
247 1785f84a 2018-12-23 stsp } else {
248 1785f84a 2018-12-23 stsp iobjp = NULL;
249 1785f84a 2018-12-23 stsp len = 0;
250 1785f84a 2018-12-23 stsp }
251 1785f84a 2018-12-23 stsp
252 1785f84a 2018-12-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
253 41496140 2019-02-21 stsp == -1) {
254 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
255 41496140 2019-02-21 stsp close(fd);
256 41496140 2019-02-21 stsp return err;
257 41496140 2019-02-21 stsp }
258 13c729f7 2018-12-24 stsp
259 13c729f7 2018-12-24 stsp return flush_imsg(ibuf);
260 13c729f7 2018-12-24 stsp }
261 13c729f7 2018-12-24 stsp
262 13c729f7 2018-12-24 stsp const struct got_error *
263 13c729f7 2018-12-24 stsp got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
264 13c729f7 2018-12-24 stsp struct got_object_id *id, int pack_idx)
265 13c729f7 2018-12-24 stsp {
266 41496140 2019-02-21 stsp const struct got_error *err = NULL;
267 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
268 13c729f7 2018-12-24 stsp size_t len;
269 13c729f7 2018-12-24 stsp
270 13c729f7 2018-12-24 stsp if (id) { /* tree is packed */
271 13c729f7 2018-12-24 stsp iobj.idx = pack_idx;
272 13c729f7 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
273 13c729f7 2018-12-24 stsp iobjp = &iobj;
274 13c729f7 2018-12-24 stsp len = sizeof(iobj);
275 13c729f7 2018-12-24 stsp } else {
276 13c729f7 2018-12-24 stsp iobjp = NULL;
277 13c729f7 2018-12-24 stsp len = 0;
278 13c729f7 2018-12-24 stsp }
279 13c729f7 2018-12-24 stsp
280 13c729f7 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
281 41496140 2019-02-21 stsp == -1) {
282 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TREE_REQUEST");
283 41496140 2019-02-21 stsp close(fd);
284 41496140 2019-02-21 stsp return err;
285 41496140 2019-02-21 stsp }
286 268f7291 2018-12-24 stsp
287 268f7291 2018-12-24 stsp return flush_imsg(ibuf);
288 268f7291 2018-12-24 stsp }
289 268f7291 2018-12-24 stsp
290 268f7291 2018-12-24 stsp const struct got_error *
291 268f7291 2018-12-24 stsp got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
292 268f7291 2018-12-24 stsp struct got_object_id *id, int pack_idx)
293 268f7291 2018-12-24 stsp {
294 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
295 268f7291 2018-12-24 stsp size_t len;
296 268f7291 2018-12-24 stsp
297 268f7291 2018-12-24 stsp if (id) { /* tag is packed */
298 268f7291 2018-12-24 stsp iobj.idx = pack_idx;
299 268f7291 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
300 268f7291 2018-12-24 stsp iobjp = &iobj;
301 268f7291 2018-12-24 stsp len = sizeof(iobj);
302 268f7291 2018-12-24 stsp } else {
303 268f7291 2018-12-24 stsp iobjp = NULL;
304 268f7291 2018-12-24 stsp len = 0;
305 268f7291 2018-12-24 stsp }
306 268f7291 2018-12-24 stsp
307 268f7291 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
308 1785f84a 2018-12-23 stsp == -1)
309 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TAG_REQUEST");
310 7762fe12 2018-11-05 stsp
311 7762fe12 2018-11-05 stsp return flush_imsg(ibuf);
312 7762fe12 2018-11-05 stsp }
313 7762fe12 2018-11-05 stsp
314 7762fe12 2018-11-05 stsp const struct got_error *
315 ebc55e2d 2018-12-24 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
316 ebc55e2d 2018-12-24 stsp struct got_object_id *id, int pack_idx)
317 55da3778 2018-09-10 stsp {
318 41496140 2019-02-21 stsp const struct got_error *err = NULL;
319 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
320 ebc55e2d 2018-12-24 stsp size_t len;
321 ebc55e2d 2018-12-24 stsp
322 ebc55e2d 2018-12-24 stsp if (id) { /* blob is packed */
323 ebc55e2d 2018-12-24 stsp iobj.idx = pack_idx;
324 ebc55e2d 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
325 ebc55e2d 2018-12-24 stsp iobjp = &iobj;
326 ebc55e2d 2018-12-24 stsp len = sizeof(iobj);
327 ebc55e2d 2018-12-24 stsp } else {
328 ebc55e2d 2018-12-24 stsp iobjp = NULL;
329 ebc55e2d 2018-12-24 stsp len = 0;
330 ebc55e2d 2018-12-24 stsp }
331 ebc55e2d 2018-12-24 stsp
332 ebc55e2d 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
333 41496140 2019-02-21 stsp == -1) {
334 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_REQUEST");
335 41496140 2019-02-21 stsp close(infd);
336 41496140 2019-02-21 stsp return err;
337 41496140 2019-02-21 stsp }
338 ad242220 2018-09-08 stsp
339 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
340 55da3778 2018-09-10 stsp }
341 ad242220 2018-09-08 stsp
342 55da3778 2018-09-10 stsp const struct got_error *
343 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
344 55da3778 2018-09-10 stsp {
345 41496140 2019-02-21 stsp const struct got_error *err = NULL;
346 41496140 2019-02-21 stsp
347 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
348 41496140 2019-02-21 stsp == -1) {
349 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_OUTFD");
350 41496140 2019-02-21 stsp close(outfd);
351 41496140 2019-02-21 stsp return err;
352 41496140 2019-02-21 stsp }
353 3840f4c9 2018-09-12 stsp
354 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
355 3840f4c9 2018-09-12 stsp }
356 3840f4c9 2018-09-12 stsp
357 3840f4c9 2018-09-12 stsp const struct got_error *
358 3840f4c9 2018-09-12 stsp got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
359 3840f4c9 2018-09-12 stsp {
360 41496140 2019-02-21 stsp const struct got_error *err = NULL;
361 41496140 2019-02-21 stsp
362 3840f4c9 2018-09-12 stsp if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
363 41496140 2019-02-21 stsp == -1) {
364 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TMPFD");
365 41496140 2019-02-21 stsp close(fd);
366 41496140 2019-02-21 stsp return err;
367 41496140 2019-02-21 stsp }
368 ad242220 2018-09-08 stsp
369 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
370 ad242220 2018-09-08 stsp }
371 ad242220 2018-09-08 stsp
372 ad242220 2018-09-08 stsp const struct got_error *
373 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
374 2178c42e 2018-04-22 stsp {
375 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
376 2178c42e 2018-04-22 stsp
377 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
378 2178c42e 2018-04-22 stsp iobj.type = obj->type;
379 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
380 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
381 2178c42e 2018-04-22 stsp iobj.size = obj->size;
382 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
383 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
384 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
385 c59b3346 2018-09-11 stsp }
386 2178c42e 2018-04-22 stsp
387 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
388 2178c42e 2018-04-22 stsp == -1)
389 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT");
390 2178c42e 2018-04-22 stsp
391 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
392 cfd633c2 2018-09-10 stsp }
393 cfd633c2 2018-09-10 stsp
394 cfd633c2 2018-09-10 stsp const struct got_error *
395 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
396 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
397 cfd633c2 2018-09-10 stsp {
398 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
399 291624d8 2018-11-07 stsp struct got_imsg_object *iobj;
400 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
401 cfd633c2 2018-09-10 stsp
402 291624d8 2018-11-07 stsp if (datalen != sizeof(*iobj))
403 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
404 291624d8 2018-11-07 stsp iobj = imsg->data;
405 cfd633c2 2018-09-10 stsp
406 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
407 cfd633c2 2018-09-10 stsp if (*obj == NULL)
408 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
409 cfd633c2 2018-09-10 stsp
410 291624d8 2018-11-07 stsp memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
411 291624d8 2018-11-07 stsp (*obj)->type = iobj->type;
412 291624d8 2018-11-07 stsp (*obj)->flags = iobj->flags;
413 291624d8 2018-11-07 stsp (*obj)->hdrlen = iobj->hdrlen;
414 291624d8 2018-11-07 stsp (*obj)->size = iobj->size;
415 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
416 291624d8 2018-11-07 stsp if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
417 291624d8 2018-11-07 stsp (*obj)->pack_offset = iobj->pack_offset;
418 291624d8 2018-11-07 stsp (*obj)->pack_idx = iobj->pack_idx;
419 876c234b 2018-09-10 stsp }
420 876c234b 2018-09-10 stsp
421 876c234b 2018-09-10 stsp return err;
422 876c234b 2018-09-10 stsp }
423 876c234b 2018-09-10 stsp
424 2178c42e 2018-04-22 stsp const struct got_error *
425 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
426 2178c42e 2018-04-22 stsp {
427 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
428 2178c42e 2018-04-22 stsp struct imsg imsg;
429 2178c42e 2018-04-22 stsp size_t datalen;
430 c4eae628 2018-04-23 stsp const size_t min_datalen =
431 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
432 2178c42e 2018-04-22 stsp
433 2178c42e 2018-04-22 stsp *obj = NULL;
434 2178c42e 2018-04-22 stsp
435 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
436 2178c42e 2018-04-22 stsp if (err)
437 2178c42e 2018-04-22 stsp return err;
438 2178c42e 2018-04-22 stsp
439 2178c42e 2018-04-22 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
440 2178c42e 2018-04-22 stsp
441 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
442 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
443 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
444 bff6ca00 2018-04-23 stsp break;
445 bff6ca00 2018-04-23 stsp default:
446 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
447 bff6ca00 2018-04-23 stsp break;
448 bff6ca00 2018-04-23 stsp }
449 bff6ca00 2018-04-23 stsp
450 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
451 bff6ca00 2018-04-23 stsp
452 bff6ca00 2018-04-23 stsp return err;
453 c75f7264 2018-09-11 stsp }
454 c75f7264 2018-09-11 stsp
455 c75f7264 2018-09-11 stsp static const struct got_error *
456 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
457 c75f7264 2018-09-11 stsp size_t logmsg_len)
458 c75f7264 2018-09-11 stsp {
459 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
460 c75f7264 2018-09-11 stsp size_t offset, remain;
461 c75f7264 2018-09-11 stsp
462 c75f7264 2018-09-11 stsp offset = 0;
463 c75f7264 2018-09-11 stsp remain = logmsg_len;
464 c75f7264 2018-09-11 stsp while (remain > 0) {
465 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
466 c75f7264 2018-09-11 stsp
467 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
468 fa4ffeb3 2018-11-04 stsp commit->logmsg + offset, n) == -1) {
469 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose "
470 230a42bd 2019-05-11 jcs "COMMIT_LOGMSG");
471 fa4ffeb3 2018-11-04 stsp break;
472 fa4ffeb3 2018-11-04 stsp }
473 c75f7264 2018-09-11 stsp
474 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
475 fa4ffeb3 2018-11-04 stsp if (err)
476 fa4ffeb3 2018-11-04 stsp break;
477 c75f7264 2018-09-11 stsp
478 c75f7264 2018-09-11 stsp offset += n;
479 c75f7264 2018-09-11 stsp remain -= n;
480 c75f7264 2018-09-11 stsp }
481 c75f7264 2018-09-11 stsp
482 fa4ffeb3 2018-11-04 stsp return err;
483 bff6ca00 2018-04-23 stsp }
484 bff6ca00 2018-04-23 stsp
485 bff6ca00 2018-04-23 stsp const struct got_error *
486 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
487 bff6ca00 2018-04-23 stsp {
488 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
489 b9c33926 2018-11-07 stsp struct got_imsg_commit_object *icommit;
490 bff6ca00 2018-04-23 stsp uint8_t *buf;
491 bff6ca00 2018-04-23 stsp size_t len, total;
492 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
493 b9c33926 2018-11-07 stsp size_t author_len = strlen(commit->author);
494 b9c33926 2018-11-07 stsp size_t committer_len = strlen(commit->committer);
495 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
496 bff6ca00 2018-04-23 stsp
497 b9c33926 2018-11-07 stsp total = sizeof(*icommit) + author_len + committer_len +
498 b9c33926 2018-11-07 stsp commit->nparents * SHA1_DIGEST_LENGTH;
499 bff6ca00 2018-04-23 stsp
500 bff6ca00 2018-04-23 stsp buf = malloc(total);
501 bff6ca00 2018-04-23 stsp if (buf == NULL)
502 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
503 bff6ca00 2018-04-23 stsp
504 b9c33926 2018-11-07 stsp icommit = (struct got_imsg_commit_object *)buf;
505 a7403916 2018-12-24 stsp memcpy(icommit->tree_id, commit->tree_id->sha1,
506 a7403916 2018-12-24 stsp sizeof(icommit->tree_id));
507 b9c33926 2018-11-07 stsp icommit->author_len = author_len;
508 b9c33926 2018-11-07 stsp icommit->author_time = commit->author_time;
509 b9c33926 2018-11-07 stsp icommit->author_gmtoff = commit->author_gmtoff;
510 b9c33926 2018-11-07 stsp icommit->committer_len = committer_len;
511 b9c33926 2018-11-07 stsp icommit->committer_time = commit->committer_time;
512 b9c33926 2018-11-07 stsp icommit->committer_gmtoff = commit->committer_gmtoff;
513 b9c33926 2018-11-07 stsp icommit->logmsg_len = logmsg_len;
514 b9c33926 2018-11-07 stsp icommit->nparents = commit->nparents;
515 b9c33926 2018-11-07 stsp
516 b9c33926 2018-11-07 stsp len = sizeof(*icommit);
517 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->author, author_len);
518 b9c33926 2018-11-07 stsp len += author_len;
519 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->committer, committer_len);
520 b9c33926 2018-11-07 stsp len += committer_len;
521 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
522 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
523 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
524 bff6ca00 2018-04-23 stsp }
525 bff6ca00 2018-04-23 stsp
526 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
527 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT");
528 bff6ca00 2018-04-23 stsp goto done;
529 bff6ca00 2018-04-23 stsp }
530 bff6ca00 2018-04-23 stsp
531 904df868 2018-11-04 stsp if (logmsg_len == 0 ||
532 904df868 2018-11-04 stsp logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
533 904df868 2018-11-04 stsp err = flush_imsg(ibuf);
534 904df868 2018-11-04 stsp if (err)
535 904df868 2018-11-04 stsp goto done;
536 904df868 2018-11-04 stsp }
537 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
538 bff6ca00 2018-04-23 stsp done:
539 bff6ca00 2018-04-23 stsp free(buf);
540 bff6ca00 2018-04-23 stsp return err;
541 bff6ca00 2018-04-23 stsp }
542 cfd633c2 2018-09-10 stsp
543 bff6ca00 2018-04-23 stsp const struct got_error *
544 068fd2bf 2018-04-24 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
545 bff6ca00 2018-04-23 stsp {
546 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
547 bff6ca00 2018-04-23 stsp struct imsg imsg;
548 291624d8 2018-11-07 stsp struct got_imsg_commit_object *icommit;
549 bff6ca00 2018-04-23 stsp size_t len, datalen;
550 bff6ca00 2018-04-23 stsp int i;
551 bff6ca00 2018-04-23 stsp const size_t min_datalen =
552 bff6ca00 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
553 bff6ca00 2018-04-23 stsp sizeof(struct got_imsg_commit_object));
554 bff6ca00 2018-04-23 stsp
555 bff6ca00 2018-04-23 stsp *commit = NULL;
556 bff6ca00 2018-04-23 stsp
557 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
558 bff6ca00 2018-04-23 stsp if (err)
559 bff6ca00 2018-04-23 stsp return err;
560 bff6ca00 2018-04-23 stsp
561 bff6ca00 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
562 bff6ca00 2018-04-23 stsp len = 0;
563 bff6ca00 2018-04-23 stsp
564 bff6ca00 2018-04-23 stsp switch (imsg.hdr.type) {
565 bff6ca00 2018-04-23 stsp case GOT_IMSG_COMMIT:
566 291624d8 2018-11-07 stsp if (datalen < sizeof(*icommit)) {
567 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
568 bff6ca00 2018-04-23 stsp break;
569 2178c42e 2018-04-22 stsp }
570 291624d8 2018-11-07 stsp icommit = imsg.data;
571 291624d8 2018-11-07 stsp if (datalen != sizeof(*icommit) + icommit->author_len +
572 291624d8 2018-11-07 stsp icommit->committer_len +
573 291624d8 2018-11-07 stsp icommit->nparents * SHA1_DIGEST_LENGTH) {
574 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
575 bff6ca00 2018-04-23 stsp break;
576 bff6ca00 2018-04-23 stsp }
577 291624d8 2018-11-07 stsp if (icommit->nparents < 0) {
578 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
579 bff6ca00 2018-04-23 stsp break;
580 bff6ca00 2018-04-23 stsp }
581 291624d8 2018-11-07 stsp len += sizeof(*icommit);
582 bff6ca00 2018-04-23 stsp
583 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
584 bff6ca00 2018-04-23 stsp if (*commit == NULL) {
585 638f9024 2019-05-13 stsp err = got_error_from_errno(
586 230a42bd 2019-05-11 jcs "got_object_commit_alloc_partial");
587 bff6ca00 2018-04-23 stsp break;
588 bff6ca00 2018-04-23 stsp }
589 bff6ca00 2018-04-23 stsp
590 291624d8 2018-11-07 stsp memcpy((*commit)->tree_id->sha1, icommit->tree_id,
591 86acc566 2018-04-23 stsp SHA1_DIGEST_LENGTH);
592 291624d8 2018-11-07 stsp (*commit)->author_time = icommit->author_time;
593 291624d8 2018-11-07 stsp (*commit)->author_gmtoff = icommit->author_gmtoff;
594 291624d8 2018-11-07 stsp (*commit)->committer_time = icommit->committer_time;
595 291624d8 2018-11-07 stsp (*commit)->committer_gmtoff = icommit->committer_gmtoff;
596 bff6ca00 2018-04-23 stsp
597 291624d8 2018-11-07 stsp if (icommit->author_len == 0) {
598 bff6ca00 2018-04-23 stsp (*commit)->author = strdup("");
599 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
600 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
601 bff6ca00 2018-04-23 stsp break;
602 bff6ca00 2018-04-23 stsp }
603 bff6ca00 2018-04-23 stsp } else {
604 291624d8 2018-11-07 stsp (*commit)->author = malloc(icommit->author_len + 1);
605 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
606 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
607 bff6ca00 2018-04-23 stsp break;
608 bff6ca00 2018-04-23 stsp }
609 291624d8 2018-11-07 stsp memcpy((*commit)->author, imsg.data + len,
610 291624d8 2018-11-07 stsp icommit->author_len);
611 291624d8 2018-11-07 stsp (*commit)->author[icommit->author_len] = '\0';
612 bff6ca00 2018-04-23 stsp }
613 291624d8 2018-11-07 stsp len += icommit->author_len;
614 6c281f94 2018-06-11 stsp
615 291624d8 2018-11-07 stsp if (icommit->committer_len == 0) {
616 bff6ca00 2018-04-23 stsp (*commit)->committer = strdup("");
617 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
618 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
619 bff6ca00 2018-04-23 stsp break;
620 bff6ca00 2018-04-23 stsp }
621 bff6ca00 2018-04-23 stsp } else {
622 bff6ca00 2018-04-23 stsp (*commit)->committer =
623 291624d8 2018-11-07 stsp malloc(icommit->committer_len + 1);
624 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
625 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
626 bff6ca00 2018-04-23 stsp break;
627 bff6ca00 2018-04-23 stsp }
628 291624d8 2018-11-07 stsp memcpy((*commit)->committer, imsg.data + len,
629 291624d8 2018-11-07 stsp icommit->committer_len);
630 291624d8 2018-11-07 stsp (*commit)->committer[icommit->committer_len] = '\0';
631 bff6ca00 2018-04-23 stsp }
632 291624d8 2018-11-07 stsp len += icommit->committer_len;
633 bff6ca00 2018-04-23 stsp
634 291624d8 2018-11-07 stsp if (icommit->logmsg_len == 0) {
635 bff6ca00 2018-04-23 stsp (*commit)->logmsg = strdup("");
636 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
637 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
638 bff6ca00 2018-04-23 stsp break;
639 bff6ca00 2018-04-23 stsp }
640 bff6ca00 2018-04-23 stsp } else {
641 291624d8 2018-11-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
642 c75f7264 2018-09-11 stsp
643 291624d8 2018-11-07 stsp (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
644 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
645 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
646 bff6ca00 2018-04-23 stsp break;
647 bff6ca00 2018-04-23 stsp }
648 c75f7264 2018-09-11 stsp while (remain > 0) {
649 c75f7264 2018-09-11 stsp struct imsg imsg_log;
650 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
651 c75f7264 2018-09-11 stsp remain);
652 c75f7264 2018-09-11 stsp
653 c75f7264 2018-09-11 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
654 c75f7264 2018-09-11 stsp if (err)
655 c75f7264 2018-09-11 stsp return err;
656 c75f7264 2018-09-11 stsp
657 c75f7264 2018-09-11 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
658 c75f7264 2018-09-11 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
659 c75f7264 2018-09-11 stsp
660 c75f7264 2018-09-11 stsp memcpy((*commit)->logmsg + offset,
661 c75f7264 2018-09-11 stsp imsg_log.data, n);
662 c75f7264 2018-09-11 stsp imsg_free(&imsg_log);
663 c75f7264 2018-09-11 stsp offset += n;
664 c75f7264 2018-09-11 stsp remain -= n;
665 c75f7264 2018-09-11 stsp }
666 291624d8 2018-11-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
667 bff6ca00 2018-04-23 stsp }
668 bff6ca00 2018-04-23 stsp
669 291624d8 2018-11-07 stsp for (i = 0; i < icommit->nparents; i++) {
670 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
671 86acc566 2018-04-23 stsp
672 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
673 5df4932d 2018-11-05 stsp if (err)
674 41fa1437 2018-11-05 stsp break;
675 291624d8 2018-11-07 stsp memcpy(qid->id, imsg.data + len +
676 291624d8 2018-11-07 stsp i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
677 7762fe12 2018-11-05 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
678 7762fe12 2018-11-05 stsp (*commit)->nparents++;
679 7762fe12 2018-11-05 stsp }
680 2178c42e 2018-04-22 stsp break;
681 8c580685 2018-04-22 stsp default:
682 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
683 8c580685 2018-04-22 stsp break;
684 2178c42e 2018-04-22 stsp }
685 2178c42e 2018-04-22 stsp
686 2178c42e 2018-04-22 stsp imsg_free(&imsg);
687 e033d803 2018-04-23 stsp
688 e033d803 2018-04-23 stsp return err;
689 e033d803 2018-04-23 stsp }
690 e033d803 2018-04-23 stsp
691 e033d803 2018-04-23 stsp const struct got_error *
692 068fd2bf 2018-04-24 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
693 e033d803 2018-04-23 stsp {
694 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
695 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
696 e033d803 2018-04-23 stsp struct got_tree_entry *te;
697 b00c9821 2018-11-04 stsp size_t totlen;
698 6eb07a17 2018-11-04 stsp int nimsg; /* number of imsg queued in ibuf */
699 e033d803 2018-04-23 stsp
700 883f0469 2018-06-23 stsp itree.nentries = tree->entries.nentries;
701 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
702 e033d803 2018-04-23 stsp == -1)
703 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TREE");
704 e033d803 2018-04-23 stsp
705 b00c9821 2018-11-04 stsp totlen = sizeof(itree);
706 6eb07a17 2018-11-04 stsp nimsg = 1;
707 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
708 a58a49db 2018-11-07 stsp struct got_imsg_tree_entry *ite;
709 e033d803 2018-04-23 stsp uint8_t *buf = NULL;
710 a58a49db 2018-11-07 stsp size_t len = sizeof(*ite) + strlen(te->name);
711 e033d803 2018-04-23 stsp
712 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
713 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
714 e033d803 2018-04-23 stsp
715 6eb07a17 2018-11-04 stsp nimsg++;
716 6eb07a17 2018-11-04 stsp if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
717 b00c9821 2018-11-04 stsp err = flush_imsg(ibuf);
718 b00c9821 2018-11-04 stsp if (err)
719 b00c9821 2018-11-04 stsp return err;
720 6eb07a17 2018-11-04 stsp nimsg = 0;
721 b00c9821 2018-11-04 stsp }
722 b00c9821 2018-11-04 stsp
723 e033d803 2018-04-23 stsp buf = malloc(len);
724 e033d803 2018-04-23 stsp if (buf == NULL)
725 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
726 e033d803 2018-04-23 stsp
727 a58a49db 2018-11-07 stsp ite = (struct got_imsg_tree_entry *)buf;
728 a58a49db 2018-11-07 stsp memcpy(ite->id, te->id->sha1, sizeof(ite->id));
729 a58a49db 2018-11-07 stsp ite->mode = te->mode;
730 a58a49db 2018-11-07 stsp memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
731 e033d803 2018-04-23 stsp
732 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
733 e033d803 2018-04-23 stsp buf, len) == -1)
734 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TREE_ENTRY");
735 e033d803 2018-04-23 stsp free(buf);
736 e033d803 2018-04-23 stsp if (err)
737 e033d803 2018-04-23 stsp return err;
738 b00c9821 2018-11-04 stsp totlen += len;
739 e033d803 2018-04-23 stsp }
740 e033d803 2018-04-23 stsp
741 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
742 e033d803 2018-04-23 stsp }
743 e033d803 2018-04-23 stsp
744 e033d803 2018-04-23 stsp const struct got_error *
745 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
746 e033d803 2018-04-23 stsp {
747 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
748 e033d803 2018-04-23 stsp const size_t min_datalen =
749 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
750 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
751 291624d8 2018-11-07 stsp struct got_imsg_tree_object *itree;
752 e033d803 2018-04-23 stsp int nentries = 0;
753 2178c42e 2018-04-22 stsp
754 e033d803 2018-04-23 stsp *tree = NULL;
755 e033d803 2018-04-23 stsp get_more:
756 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
757 e033d803 2018-04-23 stsp if (err)
758 1e51f5b9 2018-04-23 stsp goto done;
759 e033d803 2018-04-23 stsp
760 656b1f76 2019-05-11 jcs for (;;) {
761 e033d803 2018-04-23 stsp struct imsg imsg;
762 e033d803 2018-04-23 stsp size_t n;
763 e033d803 2018-04-23 stsp size_t datalen;
764 c0588d8d 2018-11-07 stsp struct got_imsg_tree_entry *ite;
765 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
766 e033d803 2018-04-23 stsp
767 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
768 e033d803 2018-04-23 stsp if (n == 0) {
769 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries)
770 e033d803 2018-04-23 stsp goto get_more;
771 e033d803 2018-04-23 stsp break;
772 e033d803 2018-04-23 stsp }
773 e033d803 2018-04-23 stsp
774 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
775 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
776 e033d803 2018-04-23 stsp
777 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
778 e033d803 2018-04-23 stsp
779 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
780 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
781 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
782 e033d803 2018-04-23 stsp break;
783 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
784 e033d803 2018-04-23 stsp /* This message should only appear once. */
785 e033d803 2018-04-23 stsp if (*tree != NULL) {
786 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
787 e033d803 2018-04-23 stsp break;
788 e033d803 2018-04-23 stsp }
789 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
790 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
791 e033d803 2018-04-23 stsp break;
792 e033d803 2018-04-23 stsp }
793 291624d8 2018-11-07 stsp itree = imsg.data;
794 c3b78ecc 2018-11-07 stsp *tree = malloc(sizeof(**tree));
795 e033d803 2018-04-23 stsp if (*tree == NULL) {
796 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
797 e033d803 2018-04-23 stsp break;
798 e033d803 2018-04-23 stsp }
799 291624d8 2018-11-07 stsp (*tree)->entries.nentries = itree->nentries;
800 883f0469 2018-06-23 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
801 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
802 e033d803 2018-04-23 stsp break;
803 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
804 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
805 e033d803 2018-04-23 stsp if (*tree == NULL) {
806 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
807 e033d803 2018-04-23 stsp break;
808 e033d803 2018-04-23 stsp }
809 c0588d8d 2018-11-07 stsp if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
810 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
811 e033d803 2018-04-23 stsp break;
812 e033d803 2018-04-23 stsp }
813 e033d803 2018-04-23 stsp
814 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
815 c0588d8d 2018-11-07 stsp datalen -= sizeof(*ite);
816 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
817 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
818 e033d803 2018-04-23 stsp break;
819 e033d803 2018-04-23 stsp }
820 c0588d8d 2018-11-07 stsp ite = imsg.data;
821 052d4dc3 2018-04-23 stsp
822 e033d803 2018-04-23 stsp te = got_alloc_tree_entry_partial();
823 e033d803 2018-04-23 stsp if (te == NULL) {
824 638f9024 2019-05-13 stsp err = got_error_from_errno(
825 230a42bd 2019-05-11 jcs "got_alloc_tree_entry_partial");
826 e033d803 2018-04-23 stsp break;
827 e033d803 2018-04-23 stsp }
828 e033d803 2018-04-23 stsp te->name = malloc(datalen + 1);
829 e033d803 2018-04-23 stsp if (te->name == NULL) {
830 e033d803 2018-04-23 stsp free(te);
831 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
832 e033d803 2018-04-23 stsp break;
833 e033d803 2018-04-23 stsp }
834 c0588d8d 2018-11-07 stsp memcpy(te->name, imsg.data + sizeof(*ite), datalen);
835 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
836 e033d803 2018-04-23 stsp
837 c0588d8d 2018-11-07 stsp memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
838 c0588d8d 2018-11-07 stsp te->mode = ite->mode;
839 883f0469 2018-06-23 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
840 e033d803 2018-04-23 stsp nentries++;
841 e033d803 2018-04-23 stsp break;
842 e033d803 2018-04-23 stsp default:
843 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
844 e033d803 2018-04-23 stsp break;
845 e033d803 2018-04-23 stsp }
846 e033d803 2018-04-23 stsp
847 e033d803 2018-04-23 stsp imsg_free(&imsg);
848 e033d803 2018-04-23 stsp }
849 1e51f5b9 2018-04-23 stsp done:
850 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries) {
851 1e51f5b9 2018-04-23 stsp if (err == NULL)
852 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
853 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
854 e033d803 2018-04-23 stsp *tree = NULL;
855 ff6b18f8 2018-04-24 stsp }
856 ff6b18f8 2018-04-24 stsp
857 ff6b18f8 2018-04-24 stsp return err;
858 ff6b18f8 2018-04-24 stsp }
859 ff6b18f8 2018-04-24 stsp
860 ff6b18f8 2018-04-24 stsp const struct got_error *
861 ac544f8c 2019-01-13 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
862 ac544f8c 2019-01-13 stsp const uint8_t *data)
863 ff6b18f8 2018-04-24 stsp {
864 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
865 2967a784 2018-04-24 stsp
866 2967a784 2018-04-24 stsp iblob.size = size;
867 ebc55e2d 2018-12-24 stsp iblob.hdrlen = hdrlen;
868 2967a784 2018-04-24 stsp
869 ac544f8c 2019-01-13 stsp if (data) {
870 ac544f8c 2019-01-13 stsp uint8_t *buf;
871 ac544f8c 2019-01-13 stsp
872 ac544f8c 2019-01-13 stsp if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
873 ac544f8c 2019-01-13 stsp return got_error(GOT_ERR_NO_SPACE);
874 ac544f8c 2019-01-13 stsp
875 ac544f8c 2019-01-13 stsp buf = malloc(sizeof(iblob) + size);
876 ac544f8c 2019-01-13 stsp if (buf == NULL)
877 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
878 ff6b18f8 2018-04-24 stsp
879 ac544f8c 2019-01-13 stsp memcpy(buf, &iblob, sizeof(iblob));
880 ac544f8c 2019-01-13 stsp memcpy(buf + sizeof(iblob), data, size);
881 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
882 ac544f8c 2019-01-13 stsp sizeof(iblob) + size) == -1) {
883 ac544f8c 2019-01-13 stsp free(buf);
884 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
885 ac544f8c 2019-01-13 stsp }
886 ac544f8c 2019-01-13 stsp free(buf);
887 ac544f8c 2019-01-13 stsp } else {
888 ac544f8c 2019-01-13 stsp /* Data has already been written to file descriptor. */
889 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
890 ac544f8c 2019-01-13 stsp sizeof(iblob)) == -1)
891 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
892 ac544f8c 2019-01-13 stsp }
893 ac544f8c 2019-01-13 stsp
894 ac544f8c 2019-01-13 stsp
895 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
896 ff6b18f8 2018-04-24 stsp }
897 ff6b18f8 2018-04-24 stsp
898 ff6b18f8 2018-04-24 stsp const struct got_error *
899 ac544f8c 2019-01-13 stsp got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
900 ac544f8c 2019-01-13 stsp struct imsgbuf *ibuf)
901 ff6b18f8 2018-04-24 stsp {
902 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
903 ff6b18f8 2018-04-24 stsp struct imsg imsg;
904 291624d8 2018-11-07 stsp struct got_imsg_blob *iblob;
905 ff6b18f8 2018-04-24 stsp size_t datalen;
906 ff6b18f8 2018-04-24 stsp
907 ac544f8c 2019-01-13 stsp *outbuf = NULL;
908 ac544f8c 2019-01-13 stsp
909 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
910 ff6b18f8 2018-04-24 stsp if (err)
911 ff6b18f8 2018-04-24 stsp return err;
912 ff6b18f8 2018-04-24 stsp
913 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
914 ff6b18f8 2018-04-24 stsp
915 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
916 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
917 ac544f8c 2019-01-13 stsp if (datalen < sizeof(*iblob)) {
918 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
919 18336eed 2018-11-04 stsp break;
920 18336eed 2018-11-04 stsp }
921 291624d8 2018-11-07 stsp iblob = imsg.data;
922 291624d8 2018-11-07 stsp *size = iblob->size;
923 ebc55e2d 2018-12-24 stsp *hdrlen = iblob->hdrlen;
924 ac544f8c 2019-01-13 stsp
925 ac544f8c 2019-01-13 stsp if (datalen == sizeof(*iblob)) {
926 ac544f8c 2019-01-13 stsp /* Data has been written to file descriptor. */
927 ac544f8c 2019-01-13 stsp break;
928 ac544f8c 2019-01-13 stsp }
929 ac544f8c 2019-01-13 stsp
930 ac544f8c 2019-01-13 stsp if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
931 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
932 ac544f8c 2019-01-13 stsp break;
933 ac544f8c 2019-01-13 stsp }
934 ac544f8c 2019-01-13 stsp
935 ac544f8c 2019-01-13 stsp *outbuf = malloc(*size);
936 ac544f8c 2019-01-13 stsp if (*outbuf == NULL) {
937 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
938 ac544f8c 2019-01-13 stsp break;
939 ac544f8c 2019-01-13 stsp }
940 ac544f8c 2019-01-13 stsp memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
941 f4a881ce 2018-11-17 stsp break;
942 f4a881ce 2018-11-17 stsp default:
943 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
944 f4a881ce 2018-11-17 stsp break;
945 f4a881ce 2018-11-17 stsp }
946 f4a881ce 2018-11-17 stsp
947 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
948 f4a881ce 2018-11-17 stsp
949 f4a881ce 2018-11-17 stsp return err;
950 f4a881ce 2018-11-17 stsp }
951 f4a881ce 2018-11-17 stsp
952 f4a881ce 2018-11-17 stsp static const struct got_error *
953 f4a881ce 2018-11-17 stsp send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
954 f4a881ce 2018-11-17 stsp {
955 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
956 f4a881ce 2018-11-17 stsp size_t offset, remain;
957 f4a881ce 2018-11-17 stsp
958 f4a881ce 2018-11-17 stsp offset = 0;
959 f4a881ce 2018-11-17 stsp remain = tagmsg_len;
960 f4a881ce 2018-11-17 stsp while (remain > 0) {
961 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
962 f4a881ce 2018-11-17 stsp
963 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
964 f4a881ce 2018-11-17 stsp tag->tagmsg + offset, n) == -1) {
965 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG_TAGMSG");
966 f4a881ce 2018-11-17 stsp break;
967 f4a881ce 2018-11-17 stsp }
968 f4a881ce 2018-11-17 stsp
969 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
970 f4a881ce 2018-11-17 stsp if (err)
971 f4a881ce 2018-11-17 stsp break;
972 f4a881ce 2018-11-17 stsp
973 f4a881ce 2018-11-17 stsp offset += n;
974 f4a881ce 2018-11-17 stsp remain -= n;
975 f4a881ce 2018-11-17 stsp }
976 f4a881ce 2018-11-17 stsp
977 f4a881ce 2018-11-17 stsp return err;
978 f4a881ce 2018-11-17 stsp }
979 f4a881ce 2018-11-17 stsp
980 f4a881ce 2018-11-17 stsp const struct got_error *
981 f4a881ce 2018-11-17 stsp got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
982 f4a881ce 2018-11-17 stsp {
983 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
984 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
985 f4a881ce 2018-11-17 stsp uint8_t *buf;
986 f4a881ce 2018-11-17 stsp size_t len, total;
987 f4a881ce 2018-11-17 stsp size_t tag_len = strlen(tag->tag);
988 f4a881ce 2018-11-17 stsp size_t tagger_len = strlen(tag->tagger);
989 f4a881ce 2018-11-17 stsp size_t tagmsg_len = strlen(tag->tagmsg);
990 f4a881ce 2018-11-17 stsp
991 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
992 f4a881ce 2018-11-17 stsp
993 f4a881ce 2018-11-17 stsp buf = malloc(total);
994 f4a881ce 2018-11-17 stsp if (buf == NULL)
995 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
996 f4a881ce 2018-11-17 stsp
997 f4a881ce 2018-11-17 stsp itag = (struct got_imsg_tag_object *)buf;
998 f4a881ce 2018-11-17 stsp memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
999 f4a881ce 2018-11-17 stsp itag->obj_type = tag->obj_type;
1000 f4a881ce 2018-11-17 stsp itag->tag_len = tag_len;
1001 f4a881ce 2018-11-17 stsp itag->tagger_len = tagger_len;
1002 f4a881ce 2018-11-17 stsp itag->tagger_time = tag->tagger_time;
1003 f4a881ce 2018-11-17 stsp itag->tagger_gmtoff = tag->tagger_gmtoff;
1004 f4a881ce 2018-11-17 stsp itag->tagmsg_len = tagmsg_len;
1005 f4a881ce 2018-11-17 stsp
1006 f4a881ce 2018-11-17 stsp len = sizeof(*itag);
1007 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tag, tag_len);
1008 f4a881ce 2018-11-17 stsp len += tag_len;
1009 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tagger, tagger_len);
1010 f4a881ce 2018-11-17 stsp len += tagger_len;
1011 f4a881ce 2018-11-17 stsp
1012 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1013 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG");
1014 f4a881ce 2018-11-17 stsp goto done;
1015 f4a881ce 2018-11-17 stsp }
1016 f4a881ce 2018-11-17 stsp
1017 f4a881ce 2018-11-17 stsp if (tagmsg_len == 0 ||
1018 f4a881ce 2018-11-17 stsp tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1019 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1020 f4a881ce 2018-11-17 stsp if (err)
1021 f4a881ce 2018-11-17 stsp goto done;
1022 f4a881ce 2018-11-17 stsp }
1023 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
1024 f4a881ce 2018-11-17 stsp done:
1025 f4a881ce 2018-11-17 stsp free(buf);
1026 f4a881ce 2018-11-17 stsp return err;
1027 f4a881ce 2018-11-17 stsp }
1028 f4a881ce 2018-11-17 stsp
1029 f4a881ce 2018-11-17 stsp const struct got_error *
1030 f4a881ce 2018-11-17 stsp got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1031 f4a881ce 2018-11-17 stsp {
1032 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1033 f4a881ce 2018-11-17 stsp struct imsg imsg;
1034 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1035 f4a881ce 2018-11-17 stsp size_t len, datalen;
1036 f4a881ce 2018-11-17 stsp const size_t min_datalen =
1037 f4a881ce 2018-11-17 stsp MIN(sizeof(struct got_imsg_error),
1038 f4a881ce 2018-11-17 stsp sizeof(struct got_imsg_tag_object));
1039 f4a881ce 2018-11-17 stsp
1040 f4a881ce 2018-11-17 stsp *tag = NULL;
1041 f4a881ce 2018-11-17 stsp
1042 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1043 f4a881ce 2018-11-17 stsp if (err)
1044 f4a881ce 2018-11-17 stsp return err;
1045 f4a881ce 2018-11-17 stsp
1046 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1047 f4a881ce 2018-11-17 stsp len = 0;
1048 f4a881ce 2018-11-17 stsp
1049 f4a881ce 2018-11-17 stsp switch (imsg.hdr.type) {
1050 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG:
1051 f4a881ce 2018-11-17 stsp if (datalen < sizeof(*itag)) {
1052 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1053 f4a881ce 2018-11-17 stsp break;
1054 f4a881ce 2018-11-17 stsp }
1055 f4a881ce 2018-11-17 stsp itag = imsg.data;
1056 f4a881ce 2018-11-17 stsp if (datalen != sizeof(*itag) + itag->tag_len +
1057 f4a881ce 2018-11-17 stsp itag->tagger_len) {
1058 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1059 f4a881ce 2018-11-17 stsp break;
1060 f4a881ce 2018-11-17 stsp }
1061 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
1062 f4a881ce 2018-11-17 stsp
1063 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
1064 f4a881ce 2018-11-17 stsp if (*tag == NULL) {
1065 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1066 f4a881ce 2018-11-17 stsp break;
1067 f4a881ce 2018-11-17 stsp }
1068 f4a881ce 2018-11-17 stsp
1069 f4a881ce 2018-11-17 stsp memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1070 f4a881ce 2018-11-17 stsp
1071 f4a881ce 2018-11-17 stsp if (itag->tag_len == 0) {
1072 f4a881ce 2018-11-17 stsp (*tag)->tag = strdup("");
1073 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1074 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1075 f4a881ce 2018-11-17 stsp break;
1076 f4a881ce 2018-11-17 stsp }
1077 f4a881ce 2018-11-17 stsp } else {
1078 f4a881ce 2018-11-17 stsp (*tag)->tag = malloc(itag->tag_len + 1);
1079 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1080 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1081 f4a881ce 2018-11-17 stsp break;
1082 f4a881ce 2018-11-17 stsp }
1083 f4a881ce 2018-11-17 stsp memcpy((*tag)->tag, imsg.data + len,
1084 f4a881ce 2018-11-17 stsp itag->tag_len);
1085 f4a881ce 2018-11-17 stsp (*tag)->tag[itag->tag_len] = '\0';
1086 f4a881ce 2018-11-17 stsp }
1087 f4a881ce 2018-11-17 stsp len += itag->tag_len;
1088 f4a881ce 2018-11-17 stsp
1089 f4a881ce 2018-11-17 stsp (*tag)->obj_type = itag->obj_type;
1090 f4a881ce 2018-11-17 stsp (*tag)->tagger_time = itag->tagger_time;
1091 f4a881ce 2018-11-17 stsp (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1092 f4a881ce 2018-11-17 stsp
1093 f4a881ce 2018-11-17 stsp if (itag->tagger_len == 0) {
1094 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup("");
1095 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1096 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1097 f4a881ce 2018-11-17 stsp break;
1098 f4a881ce 2018-11-17 stsp }
1099 f4a881ce 2018-11-17 stsp } else {
1100 f4a881ce 2018-11-17 stsp (*tag)->tagger = malloc(itag->tagger_len + 1);
1101 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1102 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1103 f4a881ce 2018-11-17 stsp break;
1104 f4a881ce 2018-11-17 stsp }
1105 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagger, imsg.data + len,
1106 f4a881ce 2018-11-17 stsp itag->tagger_len);
1107 f4a881ce 2018-11-17 stsp (*tag)->tagger[itag->tagger_len] = '\0';
1108 f4a881ce 2018-11-17 stsp }
1109 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1110 f4a881ce 2018-11-17 stsp
1111 f4a881ce 2018-11-17 stsp if (itag->tagmsg_len == 0) {
1112 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strdup("");
1113 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1114 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1115 f4a881ce 2018-11-17 stsp break;
1116 f4a881ce 2018-11-17 stsp }
1117 f4a881ce 2018-11-17 stsp } else {
1118 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1119 f4a881ce 2018-11-17 stsp
1120 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1121 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1122 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1123 f4a881ce 2018-11-17 stsp break;
1124 f4a881ce 2018-11-17 stsp }
1125 f4a881ce 2018-11-17 stsp while (remain > 0) {
1126 f4a881ce 2018-11-17 stsp struct imsg imsg_log;
1127 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1128 f4a881ce 2018-11-17 stsp remain);
1129 f4a881ce 2018-11-17 stsp
1130 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1131 f4a881ce 2018-11-17 stsp if (err)
1132 f4a881ce 2018-11-17 stsp return err;
1133 f4a881ce 2018-11-17 stsp
1134 f4a881ce 2018-11-17 stsp if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1135 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1136 f4a881ce 2018-11-17 stsp
1137 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1138 f4a881ce 2018-11-17 stsp n);
1139 f4a881ce 2018-11-17 stsp imsg_free(&imsg_log);
1140 f4a881ce 2018-11-17 stsp offset += n;
1141 f4a881ce 2018-11-17 stsp remain -= n;
1142 f4a881ce 2018-11-17 stsp }
1143 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1144 f4a881ce 2018-11-17 stsp }
1145 f4a881ce 2018-11-17 stsp
1146 ff6b18f8 2018-04-24 stsp break;
1147 ff6b18f8 2018-04-24 stsp default:
1148 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1149 ff6b18f8 2018-04-24 stsp break;
1150 e033d803 2018-04-23 stsp }
1151 e033d803 2018-04-23 stsp
1152 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1153 ff6b18f8 2018-04-24 stsp
1154 2178c42e 2018-04-22 stsp return err;
1155 2178c42e 2018-04-22 stsp }
1156 876c234b 2018-09-10 stsp
1157 876c234b 2018-09-10 stsp const struct got_error *
1158 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1159 876c234b 2018-09-10 stsp struct got_packidx *packidx)
1160 876c234b 2018-09-10 stsp {
1161 41496140 2019-02-21 stsp const struct got_error *err = NULL;
1162 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1163 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1164 876c234b 2018-09-10 stsp int fd;
1165 876c234b 2018-09-10 stsp
1166 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
1167 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
1168 876c234b 2018-09-10 stsp if (fd == -1)
1169 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1170 876c234b 2018-09-10 stsp
1171 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1172 41496140 2019-02-21 stsp sizeof(ipackidx)) == -1) {
1173 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACKIDX");
1174 41496140 2019-02-21 stsp close(fd);
1175 41496140 2019-02-21 stsp return err;
1176 41496140 2019-02-21 stsp }
1177 876c234b 2018-09-10 stsp
1178 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
1179 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1180 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
1181 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
1182 876c234b 2018-09-10 stsp
1183 876c234b 2018-09-10 stsp fd = dup(pack->fd);
1184 876c234b 2018-09-10 stsp if (fd == -1)
1185 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1186 876c234b 2018-09-10 stsp
1187 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1188 41496140 2019-02-21 stsp == -1) {
1189 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACK");
1190 41496140 2019-02-21 stsp close(fd);
1191 41496140 2019-02-21 stsp return err;
1192 41496140 2019-02-21 stsp }
1193 876c234b 2018-09-10 stsp
1194 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1195 876c234b 2018-09-10 stsp }
1196 876c234b 2018-09-10 stsp
1197 876c234b 2018-09-10 stsp const struct got_error *
1198 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1199 106807b4 2018-09-15 stsp struct got_object_id *id)
1200 876c234b 2018-09-10 stsp {
1201 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
1202 876c234b 2018-09-10 stsp
1203 876c234b 2018-09-10 stsp iobj.idx = idx;
1204 106807b4 2018-09-15 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1205 876c234b 2018-09-10 stsp
1206 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1207 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
1208 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose "
1209 230a42bd 2019-05-11 jcs "PACKED_OBJECT_REQUEST");
1210 876c234b 2018-09-10 stsp
1211 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1212 63219cd2 2019-01-04 stsp }
1213 63219cd2 2019-01-04 stsp
1214 63219cd2 2019-01-04 stsp const struct got_error *
1215 63219cd2 2019-01-04 stsp got_privsep_unveil_exec_helpers(void)
1216 63219cd2 2019-01-04 stsp {
1217 63219cd2 2019-01-04 stsp if (unveil(GOT_PATH_PROG_READ_PACK, "x") != 0 ||
1218 63219cd2 2019-01-04 stsp unveil(GOT_PATH_PROG_READ_OBJECT, "x") != 0 ||
1219 63219cd2 2019-01-04 stsp unveil(GOT_PATH_PROG_READ_COMMIT, "x") != 0 ||
1220 63219cd2 2019-01-04 stsp unveil(GOT_PATH_PROG_READ_TREE, "x") != 0 ||
1221 63219cd2 2019-01-04 stsp unveil(GOT_PATH_PROG_READ_BLOB, "x") != 0 ||
1222 63219cd2 2019-01-04 stsp unveil(GOT_PATH_PROG_READ_TAG, "x") != 0)
1223 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
1224 63219cd2 2019-01-04 stsp
1225 63219cd2 2019-01-04 stsp return NULL;
1226 876c234b 2018-09-10 stsp }