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 876c234b 2018-09-10 stsp const struct got_error *
110 ad242220 2018-09-08 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
111 e033d803 2018-04-23 stsp {
112 e033d803 2018-04-23 stsp const struct got_error *err;
113 e033d803 2018-04-23 stsp ssize_t n;
114 e033d803 2018-04-23 stsp
115 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
116 876c234b 2018-09-10 stsp if (n == -1)
117 876c234b 2018-09-10 stsp return got_error_from_errno();
118 876c234b 2018-09-10 stsp
119 876c234b 2018-09-10 stsp while (n == 0) {
120 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
121 876c234b 2018-09-10 stsp if (err)
122 876c234b 2018-09-10 stsp return err;
123 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
124 876c234b 2018-09-10 stsp }
125 fe36cf76 2018-04-23 stsp
126 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
127 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
128 fe36cf76 2018-04-23 stsp
129 fe36cf76 2018-04-23 stsp return NULL;
130 fe36cf76 2018-04-23 stsp }
131 fe36cf76 2018-04-23 stsp
132 fe36cf76 2018-04-23 stsp static const struct got_error *
133 c4eae628 2018-04-23 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
134 c4eae628 2018-04-23 stsp {
135 c4eae628 2018-04-23 stsp struct got_imsg_error ierr;
136 c4eae628 2018-04-23 stsp
137 c4eae628 2018-04-23 stsp if (datalen != sizeof(ierr))
138 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
139 c4eae628 2018-04-23 stsp
140 c4eae628 2018-04-23 stsp memcpy(&ierr, imsg->data, sizeof(ierr));
141 c4eae628 2018-04-23 stsp if (ierr.code == GOT_ERR_ERRNO) {
142 c4eae628 2018-04-23 stsp static struct got_error serr;
143 c4eae628 2018-04-23 stsp serr.code = GOT_ERR_ERRNO;
144 c4eae628 2018-04-23 stsp serr.msg = strerror(ierr.errno_code);
145 c4eae628 2018-04-23 stsp return &serr;
146 c4eae628 2018-04-23 stsp }
147 c4eae628 2018-04-23 stsp
148 c4eae628 2018-04-23 stsp return got_error(ierr.code);
149 c4eae628 2018-04-23 stsp }
150 c4eae628 2018-04-23 stsp
151 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
152 2178c42e 2018-04-22 stsp void
153 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
154 2178c42e 2018-04-22 stsp {
155 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
156 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
157 2178c42e 2018-04-22 stsp int ret;
158 2178c42e 2018-04-22 stsp
159 2178c42e 2018-04-22 stsp ierr.code = err->code;
160 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
161 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
162 2178c42e 2018-04-22 stsp else
163 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
164 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
165 2178c42e 2018-04-22 stsp if (ret != -1) {
166 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
167 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
168 5d43e84d 2018-04-23 stsp return;
169 2178c42e 2018-04-22 stsp }
170 2178c42e 2018-04-22 stsp
171 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
172 5d43e84d 2018-04-23 stsp if (poll_err) {
173 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
174 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
175 5d43e84d 2018-04-23 stsp return;
176 5d43e84d 2018-04-23 stsp }
177 2178c42e 2018-04-22 stsp
178 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
179 5d43e84d 2018-04-23 stsp if (ret == -1) {
180 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
181 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
182 5d43e84d 2018-04-23 stsp return;
183 5d43e84d 2018-04-23 stsp }
184 e033d803 2018-04-23 stsp }
185 e033d803 2018-04-23 stsp
186 e033d803 2018-04-23 stsp static const struct got_error *
187 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
188 e033d803 2018-04-23 stsp {
189 e033d803 2018-04-23 stsp const struct got_error *err;
190 e033d803 2018-04-23 stsp
191 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
192 e033d803 2018-04-23 stsp if (err)
193 e033d803 2018-04-23 stsp return err;
194 e033d803 2018-04-23 stsp
195 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
196 e033d803 2018-04-23 stsp return got_error_from_errno();
197 e033d803 2018-04-23 stsp
198 e033d803 2018-04-23 stsp return NULL;
199 ad242220 2018-09-08 stsp }
200 ad242220 2018-09-08 stsp
201 ad242220 2018-09-08 stsp const struct got_error *
202 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
203 ad242220 2018-09-08 stsp {
204 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
205 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
206 ad242220 2018-09-08 stsp
207 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
208 ad242220 2018-09-08 stsp
209 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
210 ad242220 2018-09-08 stsp return got_error_from_errno();
211 ad242220 2018-09-08 stsp
212 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
213 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
214 ad242220 2018-09-08 stsp return err;
215 d6bda086 2018-09-10 stsp }
216 d6bda086 2018-09-10 stsp
217 ad242220 2018-09-08 stsp const struct got_error *
218 ad242220 2018-09-08 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
219 ad242220 2018-09-08 stsp {
220 ad242220 2018-09-08 stsp struct got_imsg_object iobj, *iobjp = NULL;
221 ad242220 2018-09-08 stsp size_t iobj_size = 0;
222 ad242220 2018-09-08 stsp int imsg_code = GOT_IMSG_OBJECT_REQUEST;
223 ad242220 2018-09-08 stsp
224 ad242220 2018-09-08 stsp if (obj) {
225 ad242220 2018-09-08 stsp switch (obj->type) {
226 ad242220 2018-09-08 stsp case GOT_OBJ_TYPE_TREE:
227 ad242220 2018-09-08 stsp imsg_code = GOT_IMSG_TREE_REQUEST;
228 ad242220 2018-09-08 stsp break;
229 ad242220 2018-09-08 stsp case GOT_OBJ_TYPE_COMMIT:
230 ad242220 2018-09-08 stsp imsg_code = GOT_IMSG_COMMIT_REQUEST;
231 ad242220 2018-09-08 stsp break;
232 55da3778 2018-09-10 stsp case GOT_OBJ_TYPE_BLOB:
233 55da3778 2018-09-10 stsp imsg_code = GOT_IMSG_BLOB_REQUEST;
234 55da3778 2018-09-10 stsp break;
235 ad242220 2018-09-08 stsp default:
236 ad242220 2018-09-08 stsp return got_error(GOT_ERR_OBJ_TYPE);
237 ad242220 2018-09-08 stsp }
238 ad242220 2018-09-08 stsp
239 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
240 ad242220 2018-09-08 stsp iobj.type = obj->type;
241 ad242220 2018-09-08 stsp iobj.flags = obj->flags;
242 ad242220 2018-09-08 stsp iobj.hdrlen = obj->hdrlen;
243 ad242220 2018-09-08 stsp iobj.size = obj->size;
244 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
245 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
246 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
247 c59b3346 2018-09-11 stsp }
248 ad242220 2018-09-08 stsp
249 ad242220 2018-09-08 stsp iobjp = &iobj;
250 ad242220 2018-09-08 stsp iobj_size = sizeof(iobj);
251 ad242220 2018-09-08 stsp }
252 ad242220 2018-09-08 stsp
253 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
254 ad242220 2018-09-08 stsp return got_error_from_errno();
255 d6bda086 2018-09-10 stsp
256 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
257 2178c42e 2018-04-22 stsp }
258 2178c42e 2018-04-22 stsp
259 2178c42e 2018-04-22 stsp const struct got_error *
260 55da3778 2018-09-10 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
261 55da3778 2018-09-10 stsp {
262 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
263 55da3778 2018-09-10 stsp == -1)
264 ad242220 2018-09-08 stsp return got_error_from_errno();
265 ad242220 2018-09-08 stsp
266 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
267 55da3778 2018-09-10 stsp }
268 ad242220 2018-09-08 stsp
269 55da3778 2018-09-10 stsp const struct got_error *
270 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
271 55da3778 2018-09-10 stsp {
272 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
273 3840f4c9 2018-09-12 stsp == -1)
274 3840f4c9 2018-09-12 stsp return got_error_from_errno();
275 3840f4c9 2018-09-12 stsp
276 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
277 3840f4c9 2018-09-12 stsp }
278 3840f4c9 2018-09-12 stsp
279 3840f4c9 2018-09-12 stsp const struct got_error *
280 3840f4c9 2018-09-12 stsp got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
281 3840f4c9 2018-09-12 stsp {
282 3840f4c9 2018-09-12 stsp if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
283 55da3778 2018-09-10 stsp == -1)
284 ad242220 2018-09-08 stsp return got_error_from_errno();
285 ad242220 2018-09-08 stsp
286 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
287 ad242220 2018-09-08 stsp }
288 ad242220 2018-09-08 stsp
289 ad242220 2018-09-08 stsp const struct got_error *
290 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
291 2178c42e 2018-04-22 stsp {
292 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
293 2178c42e 2018-04-22 stsp
294 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
295 2178c42e 2018-04-22 stsp iobj.type = obj->type;
296 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
297 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
298 2178c42e 2018-04-22 stsp iobj.size = obj->size;
299 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
300 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
301 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
302 c59b3346 2018-09-11 stsp }
303 2178c42e 2018-04-22 stsp
304 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
305 2178c42e 2018-04-22 stsp == -1)
306 2178c42e 2018-04-22 stsp return got_error_from_errno();
307 2178c42e 2018-04-22 stsp
308 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
309 cfd633c2 2018-09-10 stsp }
310 cfd633c2 2018-09-10 stsp
311 cfd633c2 2018-09-10 stsp const struct got_error *
312 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
313 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
314 cfd633c2 2018-09-10 stsp {
315 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
316 cfd633c2 2018-09-10 stsp struct got_imsg_object iobj;
317 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
318 cfd633c2 2018-09-10 stsp
319 cfd633c2 2018-09-10 stsp if (datalen != sizeof(iobj))
320 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
321 cfd633c2 2018-09-10 stsp
322 cfd633c2 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
323 cfd633c2 2018-09-10 stsp
324 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
325 cfd633c2 2018-09-10 stsp if (*obj == NULL)
326 cfd633c2 2018-09-10 stsp return got_error_from_errno();
327 cfd633c2 2018-09-10 stsp
328 c59b3346 2018-09-11 stsp memcpy((*obj)->id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
329 cfd633c2 2018-09-10 stsp (*obj)->type = iobj.type;
330 cfd633c2 2018-09-10 stsp (*obj)->flags = iobj.flags;
331 cfd633c2 2018-09-10 stsp (*obj)->hdrlen = iobj.hdrlen;
332 cfd633c2 2018-09-10 stsp (*obj)->size = iobj.size;
333 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
334 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
335 c59b3346 2018-09-11 stsp (*obj)->pack_offset = iobj.pack_offset;
336 c59b3346 2018-09-11 stsp (*obj)->pack_idx = iobj.pack_idx;
337 876c234b 2018-09-10 stsp }
338 876c234b 2018-09-10 stsp
339 876c234b 2018-09-10 stsp return err;
340 876c234b 2018-09-10 stsp }
341 876c234b 2018-09-10 stsp
342 2178c42e 2018-04-22 stsp const struct got_error *
343 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
344 2178c42e 2018-04-22 stsp {
345 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
346 2178c42e 2018-04-22 stsp struct imsg imsg;
347 2178c42e 2018-04-22 stsp size_t datalen;
348 c4eae628 2018-04-23 stsp const size_t min_datalen =
349 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
350 2178c42e 2018-04-22 stsp
351 2178c42e 2018-04-22 stsp *obj = NULL;
352 2178c42e 2018-04-22 stsp
353 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
354 2178c42e 2018-04-22 stsp if (err)
355 2178c42e 2018-04-22 stsp return err;
356 2178c42e 2018-04-22 stsp
357 2178c42e 2018-04-22 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
358 2178c42e 2018-04-22 stsp
359 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
360 2178c42e 2018-04-22 stsp case GOT_IMSG_ERROR:
361 c4eae628 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
362 2178c42e 2018-04-22 stsp break;
363 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
364 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
365 bff6ca00 2018-04-23 stsp break;
366 bff6ca00 2018-04-23 stsp default:
367 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
368 bff6ca00 2018-04-23 stsp break;
369 bff6ca00 2018-04-23 stsp }
370 bff6ca00 2018-04-23 stsp
371 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
372 bff6ca00 2018-04-23 stsp
373 bff6ca00 2018-04-23 stsp return err;
374 c75f7264 2018-09-11 stsp }
375 c75f7264 2018-09-11 stsp
376 c75f7264 2018-09-11 stsp static const struct got_error *
377 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
378 c75f7264 2018-09-11 stsp size_t logmsg_len)
379 c75f7264 2018-09-11 stsp {
380 c75f7264 2018-09-11 stsp size_t offset, remain;
381 c75f7264 2018-09-11 stsp
382 c75f7264 2018-09-11 stsp offset = 0;
383 c75f7264 2018-09-11 stsp remain = logmsg_len;
384 c75f7264 2018-09-11 stsp while (remain > 0) {
385 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
386 c75f7264 2018-09-11 stsp
387 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
388 c75f7264 2018-09-11 stsp commit->logmsg + offset, n) == -1)
389 c75f7264 2018-09-11 stsp return got_error_from_errno();
390 c75f7264 2018-09-11 stsp
391 c75f7264 2018-09-11 stsp if (imsg_flush(ibuf) == -1)
392 c75f7264 2018-09-11 stsp return got_error_from_errno();
393 c75f7264 2018-09-11 stsp
394 c75f7264 2018-09-11 stsp offset += n;
395 c75f7264 2018-09-11 stsp remain -= n;
396 c75f7264 2018-09-11 stsp }
397 c75f7264 2018-09-11 stsp
398 c75f7264 2018-09-11 stsp return NULL;
399 bff6ca00 2018-04-23 stsp }
400 bff6ca00 2018-04-23 stsp
401 bff6ca00 2018-04-23 stsp const struct got_error *
402 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
403 bff6ca00 2018-04-23 stsp {
404 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
405 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
406 bff6ca00 2018-04-23 stsp uint8_t *buf;
407 bff6ca00 2018-04-23 stsp size_t len, total;
408 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
409 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
410 bff6ca00 2018-04-23 stsp
411 86acc566 2018-04-23 stsp memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
412 bff6ca00 2018-04-23 stsp icommit.author_len = strlen(commit->author);
413 788c352e 2018-06-16 stsp memcpy(&icommit.tm_author, &commit->tm_author,
414 788c352e 2018-06-16 stsp sizeof(icommit.tm_author));
415 bff6ca00 2018-04-23 stsp icommit.committer_len = strlen(commit->committer);
416 788c352e 2018-06-16 stsp memcpy(&icommit.tm_committer, &commit->tm_committer,
417 788c352e 2018-06-16 stsp sizeof(icommit.tm_committer));
418 c75f7264 2018-09-11 stsp icommit.logmsg_len = logmsg_len;
419 bff6ca00 2018-04-23 stsp icommit.nparents = commit->nparents;
420 bff6ca00 2018-04-23 stsp
421 bff6ca00 2018-04-23 stsp total = sizeof(icommit) + icommit.author_len +
422 c75f7264 2018-09-11 stsp icommit.committer_len + icommit.nparents * SHA1_DIGEST_LENGTH;
423 bff6ca00 2018-04-23 stsp
424 bff6ca00 2018-04-23 stsp buf = malloc(total);
425 bff6ca00 2018-04-23 stsp if (buf == NULL)
426 bff6ca00 2018-04-23 stsp return got_error_from_errno();
427 bff6ca00 2018-04-23 stsp
428 bff6ca00 2018-04-23 stsp len = 0;
429 bff6ca00 2018-04-23 stsp memcpy(buf + len, &icommit, sizeof(icommit));
430 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
431 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->author, icommit.author_len);
432 bff6ca00 2018-04-23 stsp len += icommit.author_len;
433 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->committer, icommit.committer_len);
434 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
435 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
436 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
437 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
438 bff6ca00 2018-04-23 stsp }
439 bff6ca00 2018-04-23 stsp
440 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
441 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
442 bff6ca00 2018-04-23 stsp goto done;
443 bff6ca00 2018-04-23 stsp }
444 bff6ca00 2018-04-23 stsp
445 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
446 c75f7264 2018-09-11 stsp if (err)
447 c75f7264 2018-09-11 stsp goto done;
448 c75f7264 2018-09-11 stsp
449 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
450 bff6ca00 2018-04-23 stsp done:
451 bff6ca00 2018-04-23 stsp free(buf);
452 bff6ca00 2018-04-23 stsp return err;
453 bff6ca00 2018-04-23 stsp }
454 cfd633c2 2018-09-10 stsp
455 bff6ca00 2018-04-23 stsp const struct got_error *
456 068fd2bf 2018-04-24 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
457 bff6ca00 2018-04-23 stsp {
458 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
459 bff6ca00 2018-04-23 stsp struct imsg imsg;
460 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
461 bff6ca00 2018-04-23 stsp size_t len, datalen;
462 bff6ca00 2018-04-23 stsp int i;
463 bff6ca00 2018-04-23 stsp const size_t min_datalen =
464 bff6ca00 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
465 bff6ca00 2018-04-23 stsp sizeof(struct got_imsg_commit_object));
466 bff6ca00 2018-04-23 stsp uint8_t *data;
467 bff6ca00 2018-04-23 stsp
468 bff6ca00 2018-04-23 stsp *commit = NULL;
469 bff6ca00 2018-04-23 stsp
470 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
471 bff6ca00 2018-04-23 stsp if (err)
472 bff6ca00 2018-04-23 stsp return err;
473 bff6ca00 2018-04-23 stsp
474 bff6ca00 2018-04-23 stsp data = imsg.data;
475 bff6ca00 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
476 bff6ca00 2018-04-23 stsp len = 0;
477 bff6ca00 2018-04-23 stsp
478 bff6ca00 2018-04-23 stsp switch (imsg.hdr.type) {
479 bff6ca00 2018-04-23 stsp case GOT_IMSG_ERROR:
480 bff6ca00 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
481 bff6ca00 2018-04-23 stsp break;
482 bff6ca00 2018-04-23 stsp case GOT_IMSG_COMMIT:
483 bff6ca00 2018-04-23 stsp if (datalen < sizeof(icommit)) {
484 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
485 bff6ca00 2018-04-23 stsp break;
486 2178c42e 2018-04-22 stsp }
487 bff6ca00 2018-04-23 stsp
488 bff6ca00 2018-04-23 stsp memcpy(&icommit, data, sizeof(icommit));
489 bff6ca00 2018-04-23 stsp if (datalen != sizeof(icommit) + icommit.author_len +
490 c75f7264 2018-09-11 stsp icommit.committer_len +
491 86acc566 2018-04-23 stsp icommit.nparents * SHA1_DIGEST_LENGTH) {
492 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
493 bff6ca00 2018-04-23 stsp break;
494 bff6ca00 2018-04-23 stsp }
495 bff6ca00 2018-04-23 stsp if (icommit.nparents < 0) {
496 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
497 bff6ca00 2018-04-23 stsp break;
498 bff6ca00 2018-04-23 stsp }
499 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
500 bff6ca00 2018-04-23 stsp
501 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
502 bff6ca00 2018-04-23 stsp if (*commit == NULL) {
503 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
504 bff6ca00 2018-04-23 stsp break;
505 bff6ca00 2018-04-23 stsp }
506 bff6ca00 2018-04-23 stsp
507 86acc566 2018-04-23 stsp memcpy((*commit)->tree_id->sha1, icommit.tree_id,
508 86acc566 2018-04-23 stsp SHA1_DIGEST_LENGTH);
509 788c352e 2018-06-16 stsp memcpy(&(*commit)->tm_author, &icommit.tm_author,
510 788c352e 2018-06-16 stsp sizeof((*commit)->tm_author));
511 788c352e 2018-06-16 stsp memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
512 788c352e 2018-06-16 stsp sizeof((*commit)->tm_committer));
513 bff6ca00 2018-04-23 stsp
514 bff6ca00 2018-04-23 stsp if (icommit.author_len == 0) {
515 bff6ca00 2018-04-23 stsp (*commit)->author = strdup("");
516 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
517 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
518 bff6ca00 2018-04-23 stsp break;
519 bff6ca00 2018-04-23 stsp }
520 bff6ca00 2018-04-23 stsp } else {
521 bff6ca00 2018-04-23 stsp (*commit)->author = malloc(icommit.author_len + 1);
522 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
523 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
524 bff6ca00 2018-04-23 stsp break;
525 bff6ca00 2018-04-23 stsp }
526 bff6ca00 2018-04-23 stsp memcpy((*commit)->author, data + len,
527 bff6ca00 2018-04-23 stsp icommit.author_len);
528 bff6ca00 2018-04-23 stsp (*commit)->author[icommit.author_len] = '\0';
529 bff6ca00 2018-04-23 stsp }
530 bff6ca00 2018-04-23 stsp len += icommit.author_len;
531 6c281f94 2018-06-11 stsp
532 bff6ca00 2018-04-23 stsp if (icommit.committer_len == 0) {
533 bff6ca00 2018-04-23 stsp (*commit)->committer = strdup("");
534 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
535 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
536 bff6ca00 2018-04-23 stsp break;
537 bff6ca00 2018-04-23 stsp }
538 bff6ca00 2018-04-23 stsp } else {
539 bff6ca00 2018-04-23 stsp (*commit)->committer =
540 bff6ca00 2018-04-23 stsp malloc(icommit.committer_len + 1);
541 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
542 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
543 bff6ca00 2018-04-23 stsp break;
544 bff6ca00 2018-04-23 stsp }
545 bff6ca00 2018-04-23 stsp memcpy((*commit)->committer, data + len,
546 bff6ca00 2018-04-23 stsp icommit.committer_len);
547 bff6ca00 2018-04-23 stsp (*commit)->committer[icommit.committer_len] = '\0';
548 bff6ca00 2018-04-23 stsp }
549 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
550 bff6ca00 2018-04-23 stsp
551 bff6ca00 2018-04-23 stsp if (icommit.logmsg_len == 0) {
552 bff6ca00 2018-04-23 stsp (*commit)->logmsg = strdup("");
553 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
554 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
555 bff6ca00 2018-04-23 stsp break;
556 bff6ca00 2018-04-23 stsp }
557 bff6ca00 2018-04-23 stsp } else {
558 c75f7264 2018-09-11 stsp size_t offset = 0, remain = icommit.logmsg_len;
559 c75f7264 2018-09-11 stsp
560 bff6ca00 2018-04-23 stsp (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
561 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
562 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
563 bff6ca00 2018-04-23 stsp break;
564 bff6ca00 2018-04-23 stsp }
565 c75f7264 2018-09-11 stsp while (remain > 0) {
566 c75f7264 2018-09-11 stsp struct imsg imsg_log;
567 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
568 c75f7264 2018-09-11 stsp remain);
569 c75f7264 2018-09-11 stsp
570 c75f7264 2018-09-11 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
571 c75f7264 2018-09-11 stsp if (err)
572 c75f7264 2018-09-11 stsp return err;
573 c75f7264 2018-09-11 stsp
574 c75f7264 2018-09-11 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
575 c75f7264 2018-09-11 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
576 c75f7264 2018-09-11 stsp
577 c75f7264 2018-09-11 stsp memcpy((*commit)->logmsg + offset,
578 c75f7264 2018-09-11 stsp imsg_log.data, n);
579 c75f7264 2018-09-11 stsp imsg_free(&imsg_log);
580 c75f7264 2018-09-11 stsp offset += n;
581 c75f7264 2018-09-11 stsp remain -= n;
582 c75f7264 2018-09-11 stsp }
583 bff6ca00 2018-04-23 stsp (*commit)->logmsg[icommit.logmsg_len] = '\0';
584 bff6ca00 2018-04-23 stsp }
585 bff6ca00 2018-04-23 stsp
586 bff6ca00 2018-04-23 stsp for (i = 0; i < icommit.nparents; i++) {
587 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
588 86acc566 2018-04-23 stsp
589 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
590 79f35eb3 2018-06-11 stsp if (qid == NULL) {
591 86acc566 2018-04-23 stsp err = got_error_from_errno();
592 bff6ca00 2018-04-23 stsp break;
593 86acc566 2018-04-23 stsp }
594 79f35eb3 2018-06-11 stsp qid->id = calloc(1, sizeof(*qid->id));
595 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
596 86acc566 2018-04-23 stsp err = got_error_from_errno();
597 79f35eb3 2018-06-11 stsp free(qid);
598 86acc566 2018-04-23 stsp break;
599 86acc566 2018-04-23 stsp }
600 86acc566 2018-04-23 stsp
601 79f35eb3 2018-06-11 stsp memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
602 79f35eb3 2018-06-11 stsp sizeof(*qid->id));
603 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
604 86acc566 2018-04-23 stsp (*commit)->nparents++;
605 bff6ca00 2018-04-23 stsp }
606 2178c42e 2018-04-22 stsp break;
607 8c580685 2018-04-22 stsp default:
608 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
609 8c580685 2018-04-22 stsp break;
610 2178c42e 2018-04-22 stsp }
611 2178c42e 2018-04-22 stsp
612 2178c42e 2018-04-22 stsp imsg_free(&imsg);
613 e033d803 2018-04-23 stsp
614 e033d803 2018-04-23 stsp return err;
615 e033d803 2018-04-23 stsp }
616 e033d803 2018-04-23 stsp
617 e033d803 2018-04-23 stsp const struct got_error *
618 068fd2bf 2018-04-24 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
619 e033d803 2018-04-23 stsp {
620 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
621 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
622 e033d803 2018-04-23 stsp struct got_tree_entry *te;
623 e033d803 2018-04-23 stsp
624 883f0469 2018-06-23 stsp itree.nentries = tree->entries.nentries;
625 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
626 e033d803 2018-04-23 stsp == -1)
627 e033d803 2018-04-23 stsp return got_error_from_errno();
628 e033d803 2018-04-23 stsp
629 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
630 e033d803 2018-04-23 stsp if (err)
631 e033d803 2018-04-23 stsp return err;
632 e033d803 2018-04-23 stsp
633 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
634 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
635 e033d803 2018-04-23 stsp uint8_t *buf = NULL;
636 e033d803 2018-04-23 stsp size_t len = sizeof(ite) + strlen(te->name);
637 e033d803 2018-04-23 stsp
638 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
639 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
640 e033d803 2018-04-23 stsp
641 e033d803 2018-04-23 stsp buf = malloc(len);
642 e033d803 2018-04-23 stsp if (buf == NULL)
643 e033d803 2018-04-23 stsp return got_error_from_errno();
644 e033d803 2018-04-23 stsp
645 e033d803 2018-04-23 stsp memcpy(ite.id, te->id->sha1, sizeof(ite.id));
646 e033d803 2018-04-23 stsp ite.mode = te->mode;
647 e033d803 2018-04-23 stsp memcpy(buf, &ite, sizeof(ite));
648 e033d803 2018-04-23 stsp memcpy(buf + sizeof(ite), te->name, strlen(te->name));
649 e033d803 2018-04-23 stsp
650 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
651 e033d803 2018-04-23 stsp buf, len) == -1)
652 e033d803 2018-04-23 stsp err = got_error_from_errno();
653 e033d803 2018-04-23 stsp free(buf);
654 e033d803 2018-04-23 stsp if (err)
655 e033d803 2018-04-23 stsp return err;
656 e033d803 2018-04-23 stsp
657 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
658 e033d803 2018-04-23 stsp if (err)
659 e033d803 2018-04-23 stsp return err;
660 e033d803 2018-04-23 stsp }
661 e033d803 2018-04-23 stsp
662 e033d803 2018-04-23 stsp return NULL;
663 e033d803 2018-04-23 stsp }
664 e033d803 2018-04-23 stsp
665 e033d803 2018-04-23 stsp const struct got_error *
666 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
667 e033d803 2018-04-23 stsp {
668 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
669 e033d803 2018-04-23 stsp const size_t min_datalen =
670 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
671 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
672 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree = { 0 };
673 e033d803 2018-04-23 stsp int nentries = 0;
674 2178c42e 2018-04-22 stsp
675 e033d803 2018-04-23 stsp *tree = NULL;
676 e033d803 2018-04-23 stsp get_more:
677 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
678 e033d803 2018-04-23 stsp if (err)
679 1e51f5b9 2018-04-23 stsp goto done;
680 e033d803 2018-04-23 stsp
681 e033d803 2018-04-23 stsp while (1) {
682 e033d803 2018-04-23 stsp struct imsg imsg;
683 e033d803 2018-04-23 stsp size_t n;
684 e033d803 2018-04-23 stsp size_t datalen;
685 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
686 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
687 e033d803 2018-04-23 stsp
688 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
689 e033d803 2018-04-23 stsp if (n == 0) {
690 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries)
691 e033d803 2018-04-23 stsp goto get_more;
692 e033d803 2018-04-23 stsp break;
693 e033d803 2018-04-23 stsp }
694 e033d803 2018-04-23 stsp
695 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
696 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
697 e033d803 2018-04-23 stsp
698 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
699 e033d803 2018-04-23 stsp
700 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
701 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
702 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
703 e033d803 2018-04-23 stsp break;
704 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
705 e033d803 2018-04-23 stsp /* This message should only appear once. */
706 e033d803 2018-04-23 stsp if (*tree != NULL) {
707 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
708 e033d803 2018-04-23 stsp break;
709 e033d803 2018-04-23 stsp }
710 e033d803 2018-04-23 stsp if (datalen != sizeof(itree)) {
711 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
712 e033d803 2018-04-23 stsp break;
713 e033d803 2018-04-23 stsp }
714 e033d803 2018-04-23 stsp memcpy(&itree, imsg.data, sizeof(itree));
715 e033d803 2018-04-23 stsp *tree = calloc(1, sizeof(**tree));
716 e033d803 2018-04-23 stsp if (*tree == NULL) {
717 e033d803 2018-04-23 stsp err = got_error_from_errno();
718 e033d803 2018-04-23 stsp break;
719 e033d803 2018-04-23 stsp }
720 883f0469 2018-06-23 stsp (*tree)->entries.nentries = itree.nentries;
721 883f0469 2018-06-23 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
722 e033d803 2018-04-23 stsp break;
723 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
724 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
725 e033d803 2018-04-23 stsp if (*tree == NULL) {
726 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
727 e033d803 2018-04-23 stsp break;
728 e033d803 2018-04-23 stsp }
729 e033d803 2018-04-23 stsp if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
730 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
731 e033d803 2018-04-23 stsp break;
732 e033d803 2018-04-23 stsp }
733 e033d803 2018-04-23 stsp
734 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
735 e033d803 2018-04-23 stsp datalen -= sizeof(ite);
736 e033d803 2018-04-23 stsp memcpy(&ite, imsg.data, sizeof(ite));
737 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
738 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
739 e033d803 2018-04-23 stsp break;
740 e033d803 2018-04-23 stsp }
741 052d4dc3 2018-04-23 stsp
742 e033d803 2018-04-23 stsp te = got_alloc_tree_entry_partial();
743 e033d803 2018-04-23 stsp if (te == NULL) {
744 e033d803 2018-04-23 stsp err = got_error_from_errno();
745 e033d803 2018-04-23 stsp break;
746 e033d803 2018-04-23 stsp }
747 e033d803 2018-04-23 stsp te->name = malloc(datalen + 1);
748 e033d803 2018-04-23 stsp if (te->name == NULL) {
749 e033d803 2018-04-23 stsp free(te);
750 e033d803 2018-04-23 stsp err = got_error_from_errno();
751 e033d803 2018-04-23 stsp break;
752 e033d803 2018-04-23 stsp }
753 052d4dc3 2018-04-23 stsp memcpy(te->name, imsg.data + sizeof(ite), datalen);
754 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
755 e033d803 2018-04-23 stsp
756 e033d803 2018-04-23 stsp memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
757 e033d803 2018-04-23 stsp te->mode = ite.mode;
758 883f0469 2018-06-23 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
759 e033d803 2018-04-23 stsp nentries++;
760 e033d803 2018-04-23 stsp break;
761 e033d803 2018-04-23 stsp default:
762 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
763 e033d803 2018-04-23 stsp break;
764 e033d803 2018-04-23 stsp }
765 e033d803 2018-04-23 stsp
766 e033d803 2018-04-23 stsp imsg_free(&imsg);
767 e033d803 2018-04-23 stsp }
768 1e51f5b9 2018-04-23 stsp done:
769 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries) {
770 1e51f5b9 2018-04-23 stsp if (err == NULL)
771 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
772 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
773 e033d803 2018-04-23 stsp *tree = NULL;
774 ff6b18f8 2018-04-24 stsp }
775 ff6b18f8 2018-04-24 stsp
776 ff6b18f8 2018-04-24 stsp return err;
777 ff6b18f8 2018-04-24 stsp }
778 ff6b18f8 2018-04-24 stsp
779 ff6b18f8 2018-04-24 stsp const struct got_error *
780 2967a784 2018-04-24 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
781 ff6b18f8 2018-04-24 stsp {
782 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
783 2967a784 2018-04-24 stsp
784 2967a784 2018-04-24 stsp iblob.size = size;
785 ff6b18f8 2018-04-24 stsp /* Data has already been written to file descriptor. */
786 2967a784 2018-04-24 stsp
787 2967a784 2018-04-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
788 2967a784 2018-04-24 stsp == -1)
789 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
790 ff6b18f8 2018-04-24 stsp
791 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
792 ff6b18f8 2018-04-24 stsp }
793 ff6b18f8 2018-04-24 stsp
794 ff6b18f8 2018-04-24 stsp const struct got_error *
795 2967a784 2018-04-24 stsp got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
796 ff6b18f8 2018-04-24 stsp {
797 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
798 ff6b18f8 2018-04-24 stsp struct imsg imsg;
799 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
800 ff6b18f8 2018-04-24 stsp size_t datalen;
801 ff6b18f8 2018-04-24 stsp
802 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
803 ff6b18f8 2018-04-24 stsp if (err)
804 ff6b18f8 2018-04-24 stsp return err;
805 ff6b18f8 2018-04-24 stsp
806 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
807 ff6b18f8 2018-04-24 stsp
808 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
809 ff6b18f8 2018-04-24 stsp case GOT_IMSG_ERROR:
810 ff6b18f8 2018-04-24 stsp err = recv_imsg_error(&imsg, datalen);
811 ff6b18f8 2018-04-24 stsp break;
812 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
813 2967a784 2018-04-24 stsp if (datalen != sizeof(iblob))
814 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
815 2967a784 2018-04-24 stsp memcpy(&iblob, imsg.data, sizeof(iblob));
816 2967a784 2018-04-24 stsp *size = iblob.size;
817 ff6b18f8 2018-04-24 stsp /* Data has been written to file descriptor. */
818 ff6b18f8 2018-04-24 stsp break;
819 ff6b18f8 2018-04-24 stsp default:
820 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
821 ff6b18f8 2018-04-24 stsp break;
822 e033d803 2018-04-23 stsp }
823 e033d803 2018-04-23 stsp
824 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
825 ff6b18f8 2018-04-24 stsp
826 2178c42e 2018-04-22 stsp return err;
827 2178c42e 2018-04-22 stsp }
828 876c234b 2018-09-10 stsp
829 876c234b 2018-09-10 stsp const struct got_error *
830 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
831 876c234b 2018-09-10 stsp struct got_packidx *packidx)
832 876c234b 2018-09-10 stsp {
833 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
834 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
835 876c234b 2018-09-10 stsp int fd;
836 876c234b 2018-09-10 stsp
837 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
838 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
839 876c234b 2018-09-10 stsp if (fd == -1)
840 876c234b 2018-09-10 stsp return got_error_from_errno();
841 876c234b 2018-09-10 stsp
842 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
843 876c234b 2018-09-10 stsp sizeof(ipackidx)) == -1)
844 876c234b 2018-09-10 stsp return got_error_from_errno();
845 876c234b 2018-09-10 stsp
846 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
847 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
848 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
849 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
850 876c234b 2018-09-10 stsp
851 876c234b 2018-09-10 stsp fd = dup(pack->fd);
852 876c234b 2018-09-10 stsp if (fd == -1)
853 876c234b 2018-09-10 stsp return got_error_from_errno();
854 876c234b 2018-09-10 stsp
855 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
856 876c234b 2018-09-10 stsp == -1)
857 876c234b 2018-09-10 stsp return got_error_from_errno();
858 876c234b 2018-09-10 stsp
859 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
860 876c234b 2018-09-10 stsp }
861 876c234b 2018-09-10 stsp
862 876c234b 2018-09-10 stsp const struct got_error *
863 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
864 106807b4 2018-09-15 stsp struct got_object_id *id)
865 876c234b 2018-09-10 stsp {
866 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
867 876c234b 2018-09-10 stsp
868 876c234b 2018-09-10 stsp iobj.idx = idx;
869 106807b4 2018-09-15 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
870 876c234b 2018-09-10 stsp
871 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
872 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
873 876c234b 2018-09-10 stsp return got_error_from_errno();
874 876c234b 2018-09-10 stsp
875 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
876 876c234b 2018-09-10 stsp }