Blame


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