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 2178c42e 2018-04-22 stsp
21 2178c42e 2018-04-22 stsp #include <stdio.h>
22 2178c42e 2018-04-22 stsp #include <stdlib.h>
23 2178c42e 2018-04-22 stsp #include <string.h>
24 2178c42e 2018-04-22 stsp #include <errno.h>
25 2178c42e 2018-04-22 stsp #include <stdint.h>
26 2178c42e 2018-04-22 stsp #include <poll.h>
27 2178c42e 2018-04-22 stsp #include <imsg.h>
28 2178c42e 2018-04-22 stsp #include <sha1.h>
29 2178c42e 2018-04-22 stsp #include <zlib.h>
30 788c352e 2018-06-16 stsp #include <time.h>
31 2178c42e 2018-04-22 stsp
32 2178c42e 2018-04-22 stsp #include "got_object.h"
33 2178c42e 2018-04-22 stsp #include "got_error.h"
34 2178c42e 2018-04-22 stsp
35 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
36 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
37 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
38 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
39 a440fac0 2018-09-06 stsp #include "got_lib_object_parse.h"
40 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
41 2178c42e 2018-04-22 stsp
42 2178c42e 2018-04-22 stsp #ifndef MIN
43 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
44 2178c42e 2018-04-22 stsp #endif
45 2178c42e 2018-04-22 stsp
46 2178c42e 2018-04-22 stsp static const struct got_error *
47 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
48 2178c42e 2018-04-22 stsp {
49 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
50 2178c42e 2018-04-22 stsp int n;
51 2178c42e 2018-04-22 stsp
52 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
53 2178c42e 2018-04-22 stsp pfd[0].events = events;
54 2178c42e 2018-04-22 stsp
55 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
56 2178c42e 2018-04-22 stsp if (n == -1)
57 2178c42e 2018-04-22 stsp return got_error_from_errno();
58 2178c42e 2018-04-22 stsp if (n == 0)
59 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
60 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
61 2178c42e 2018-04-22 stsp return got_error_from_errno();
62 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
63 2178c42e 2018-04-22 stsp return NULL;
64 2178c42e 2018-04-22 stsp
65 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
66 2178c42e 2018-04-22 stsp }
67 2178c42e 2018-04-22 stsp
68 c4eae628 2018-04-23 stsp static const struct got_error *
69 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
70 fe36cf76 2018-04-23 stsp {
71 fe36cf76 2018-04-23 stsp const struct got_error *err;
72 e033d803 2018-04-23 stsp size_t n;
73 fe36cf76 2018-04-23 stsp
74 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
75 fe36cf76 2018-04-23 stsp if (err)
76 fe36cf76 2018-04-23 stsp return err;
77 fe36cf76 2018-04-23 stsp
78 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
79 fe36cf76 2018-04-23 stsp if (n == -1) {
80 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
81 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
82 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
83 fe36cf76 2018-04-23 stsp }
84 fe36cf76 2018-04-23 stsp if (n == 0)
85 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
86 fe36cf76 2018-04-23 stsp
87 e033d803 2018-04-23 stsp return NULL;
88 e033d803 2018-04-23 stsp }
89 e033d803 2018-04-23 stsp
90 ad242220 2018-09-08 stsp const struct got_error *
91 ad242220 2018-09-08 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
92 e033d803 2018-04-23 stsp {
93 e033d803 2018-04-23 stsp const struct got_error *err;
94 e033d803 2018-04-23 stsp ssize_t n;
95 e033d803 2018-04-23 stsp
96 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
97 e033d803 2018-04-23 stsp if (err)
98 e033d803 2018-04-23 stsp return err;
99 e033d803 2018-04-23 stsp
100 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
101 e033d803 2018-04-23 stsp if (n == 0)
102 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
103 fe36cf76 2018-04-23 stsp
104 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
105 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
106 fe36cf76 2018-04-23 stsp
107 fe36cf76 2018-04-23 stsp return NULL;
108 fe36cf76 2018-04-23 stsp }
109 fe36cf76 2018-04-23 stsp
110 fe36cf76 2018-04-23 stsp static const struct got_error *
111 c4eae628 2018-04-23 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
112 c4eae628 2018-04-23 stsp {
113 c4eae628 2018-04-23 stsp struct got_imsg_error ierr;
114 c4eae628 2018-04-23 stsp
115 c4eae628 2018-04-23 stsp if (datalen != sizeof(ierr))
116 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
117 c4eae628 2018-04-23 stsp
118 c4eae628 2018-04-23 stsp memcpy(&ierr, imsg->data, sizeof(ierr));
119 c4eae628 2018-04-23 stsp if (ierr.code == GOT_ERR_ERRNO) {
120 c4eae628 2018-04-23 stsp static struct got_error serr;
121 c4eae628 2018-04-23 stsp serr.code = GOT_ERR_ERRNO;
122 c4eae628 2018-04-23 stsp serr.msg = strerror(ierr.errno_code);
123 c4eae628 2018-04-23 stsp return &serr;
124 c4eae628 2018-04-23 stsp }
125 c4eae628 2018-04-23 stsp
126 c4eae628 2018-04-23 stsp return got_error(ierr.code);
127 c4eae628 2018-04-23 stsp }
128 c4eae628 2018-04-23 stsp
129 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
130 2178c42e 2018-04-22 stsp void
131 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
132 2178c42e 2018-04-22 stsp {
133 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
134 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
135 2178c42e 2018-04-22 stsp int ret;
136 2178c42e 2018-04-22 stsp
137 2178c42e 2018-04-22 stsp ierr.code = err->code;
138 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
139 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
140 2178c42e 2018-04-22 stsp else
141 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
142 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
143 2178c42e 2018-04-22 stsp if (ret != -1) {
144 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
145 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
146 5d43e84d 2018-04-23 stsp return;
147 2178c42e 2018-04-22 stsp }
148 2178c42e 2018-04-22 stsp
149 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
150 5d43e84d 2018-04-23 stsp if (poll_err) {
151 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
152 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
153 5d43e84d 2018-04-23 stsp return;
154 5d43e84d 2018-04-23 stsp }
155 2178c42e 2018-04-22 stsp
156 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
157 5d43e84d 2018-04-23 stsp if (ret == -1) {
158 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
159 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
160 5d43e84d 2018-04-23 stsp return;
161 5d43e84d 2018-04-23 stsp }
162 e033d803 2018-04-23 stsp }
163 e033d803 2018-04-23 stsp
164 e033d803 2018-04-23 stsp static const struct got_error *
165 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
166 e033d803 2018-04-23 stsp {
167 e033d803 2018-04-23 stsp const struct got_error *err;
168 e033d803 2018-04-23 stsp
169 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
170 e033d803 2018-04-23 stsp if (err)
171 e033d803 2018-04-23 stsp return err;
172 e033d803 2018-04-23 stsp
173 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
174 e033d803 2018-04-23 stsp return got_error_from_errno();
175 e033d803 2018-04-23 stsp
176 e033d803 2018-04-23 stsp return NULL;
177 ad242220 2018-09-08 stsp }
178 ad242220 2018-09-08 stsp
179 ad242220 2018-09-08 stsp const struct got_error *
180 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
181 ad242220 2018-09-08 stsp {
182 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
183 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
184 ad242220 2018-09-08 stsp
185 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
186 ad242220 2018-09-08 stsp
187 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
188 ad242220 2018-09-08 stsp return got_error_from_errno();
189 ad242220 2018-09-08 stsp
190 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
191 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
192 ad242220 2018-09-08 stsp return err;
193 ad242220 2018-09-08 stsp }
194 ad242220 2018-09-08 stsp
195 ad242220 2018-09-08 stsp const struct got_error *
196 ad242220 2018-09-08 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
197 ad242220 2018-09-08 stsp {
198 ad242220 2018-09-08 stsp struct got_imsg_object iobj, *iobjp = NULL;
199 ad242220 2018-09-08 stsp size_t iobj_size = 0;
200 ad242220 2018-09-08 stsp int imsg_code = GOT_IMSG_OBJECT_REQUEST;
201 ad242220 2018-09-08 stsp
202 ad242220 2018-09-08 stsp if (obj) {
203 ad242220 2018-09-08 stsp switch (obj->type) {
204 ad242220 2018-09-08 stsp case GOT_OBJ_TYPE_TREE:
205 ad242220 2018-09-08 stsp imsg_code = GOT_IMSG_TREE_REQUEST;
206 ad242220 2018-09-08 stsp break;
207 ad242220 2018-09-08 stsp case GOT_OBJ_TYPE_COMMIT:
208 ad242220 2018-09-08 stsp imsg_code = GOT_IMSG_COMMIT_REQUEST;
209 ad242220 2018-09-08 stsp break;
210 ad242220 2018-09-08 stsp default:
211 ad242220 2018-09-08 stsp return got_error(GOT_ERR_OBJ_TYPE);
212 ad242220 2018-09-08 stsp }
213 ad242220 2018-09-08 stsp
214 ad242220 2018-09-08 stsp iobj.type = obj->type;
215 ad242220 2018-09-08 stsp iobj.flags = obj->flags;
216 ad242220 2018-09-08 stsp iobj.hdrlen = obj->hdrlen;
217 ad242220 2018-09-08 stsp iobj.size = obj->size;
218 ad242220 2018-09-08 stsp iobj.ndeltas = 0;
219 ad242220 2018-09-08 stsp
220 ad242220 2018-09-08 stsp iobjp = &iobj;
221 ad242220 2018-09-08 stsp iobj_size = sizeof(iobj);
222 ad242220 2018-09-08 stsp }
223 ad242220 2018-09-08 stsp
224 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
225 ad242220 2018-09-08 stsp return got_error_from_errno();
226 ad242220 2018-09-08 stsp
227 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
228 2178c42e 2018-04-22 stsp }
229 2178c42e 2018-04-22 stsp
230 2178c42e 2018-04-22 stsp const struct got_error *
231 ad242220 2018-09-08 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int outfd, int infd)
232 ad242220 2018-09-08 stsp {
233 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
234 ad242220 2018-09-08 stsp
235 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
236 ad242220 2018-09-08 stsp == -1) {
237 ad242220 2018-09-08 stsp close(infd);
238 ad242220 2018-09-08 stsp close(outfd);
239 ad242220 2018-09-08 stsp return got_error_from_errno();
240 ad242220 2018-09-08 stsp }
241 ad242220 2018-09-08 stsp
242 ad242220 2018-09-08 stsp err = flush_imsg(ibuf);
243 ad242220 2018-09-08 stsp if (err) {
244 ad242220 2018-09-08 stsp close(outfd);
245 ad242220 2018-09-08 stsp return err;
246 ad242220 2018-09-08 stsp }
247 ad242220 2018-09-08 stsp
248 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
249 ad242220 2018-09-08 stsp == -1) {
250 ad242220 2018-09-08 stsp close(outfd);
251 ad242220 2018-09-08 stsp return got_error_from_errno();
252 ad242220 2018-09-08 stsp }
253 ad242220 2018-09-08 stsp
254 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
255 ad242220 2018-09-08 stsp }
256 ad242220 2018-09-08 stsp
257 ad242220 2018-09-08 stsp const struct got_error *
258 2178c42e 2018-04-22 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
259 2178c42e 2018-04-22 stsp {
260 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
261 2178c42e 2018-04-22 stsp
262 2178c42e 2018-04-22 stsp iobj.type = obj->type;
263 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
264 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
265 2178c42e 2018-04-22 stsp iobj.size = obj->size;
266 2178c42e 2018-04-22 stsp iobj.ndeltas = ndeltas;
267 2178c42e 2018-04-22 stsp
268 2178c42e 2018-04-22 stsp if (ndeltas > 0) {
269 2178c42e 2018-04-22 stsp /* TODO: Handle deltas */
270 2178c42e 2018-04-22 stsp }
271 2178c42e 2018-04-22 stsp
272 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
273 2178c42e 2018-04-22 stsp == -1)
274 2178c42e 2018-04-22 stsp return got_error_from_errno();
275 2178c42e 2018-04-22 stsp
276 e033d803 2018-04-23 stsp return flush_imsg(ibuf);
277 2178c42e 2018-04-22 stsp }
278 2178c42e 2018-04-22 stsp
279 2178c42e 2018-04-22 stsp const struct got_error *
280 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
281 2178c42e 2018-04-22 stsp {
282 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
283 2178c42e 2018-04-22 stsp struct imsg imsg;
284 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
285 2178c42e 2018-04-22 stsp size_t datalen;
286 2178c42e 2018-04-22 stsp int i;
287 c4eae628 2018-04-23 stsp const size_t min_datalen =
288 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
289 2178c42e 2018-04-22 stsp
290 2178c42e 2018-04-22 stsp *obj = NULL;
291 2178c42e 2018-04-22 stsp
292 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
293 2178c42e 2018-04-22 stsp if (err)
294 2178c42e 2018-04-22 stsp return err;
295 2178c42e 2018-04-22 stsp
296 2178c42e 2018-04-22 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
297 2178c42e 2018-04-22 stsp
298 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
299 2178c42e 2018-04-22 stsp case GOT_IMSG_ERROR:
300 c4eae628 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
301 2178c42e 2018-04-22 stsp break;
302 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
303 2178c42e 2018-04-22 stsp if (datalen != sizeof(iobj)) {
304 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
305 2178c42e 2018-04-22 stsp break;
306 2178c42e 2018-04-22 stsp }
307 2178c42e 2018-04-22 stsp
308 2178c42e 2018-04-22 stsp memcpy(&iobj, imsg.data, sizeof(iobj));
309 2178c42e 2018-04-22 stsp if (iobj.ndeltas < 0 ||
310 2178c42e 2018-04-22 stsp iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
311 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
312 2178c42e 2018-04-22 stsp break;
313 2178c42e 2018-04-22 stsp }
314 2178c42e 2018-04-22 stsp
315 2178c42e 2018-04-22 stsp *obj = calloc(1, sizeof(**obj));
316 2178c42e 2018-04-22 stsp if (*obj == NULL) {
317 2178c42e 2018-04-22 stsp err = got_error_from_errno();
318 2178c42e 2018-04-22 stsp break;
319 2178c42e 2018-04-22 stsp }
320 2178c42e 2018-04-22 stsp
321 2178c42e 2018-04-22 stsp (*obj)->type = iobj.type;
322 2178c42e 2018-04-22 stsp (*obj)->hdrlen = iobj.hdrlen;
323 2178c42e 2018-04-22 stsp (*obj)->size = iobj.size;
324 2178c42e 2018-04-22 stsp for (i = 0; i < iobj.ndeltas; i++) {
325 2178c42e 2018-04-22 stsp /* TODO: Handle deltas */
326 bff6ca00 2018-04-23 stsp }
327 bff6ca00 2018-04-23 stsp break;
328 bff6ca00 2018-04-23 stsp default:
329 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
330 bff6ca00 2018-04-23 stsp break;
331 bff6ca00 2018-04-23 stsp }
332 bff6ca00 2018-04-23 stsp
333 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
334 bff6ca00 2018-04-23 stsp
335 bff6ca00 2018-04-23 stsp return err;
336 bff6ca00 2018-04-23 stsp }
337 bff6ca00 2018-04-23 stsp
338 bff6ca00 2018-04-23 stsp const struct got_error *
339 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
340 bff6ca00 2018-04-23 stsp {
341 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
342 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
343 bff6ca00 2018-04-23 stsp uint8_t *buf;
344 bff6ca00 2018-04-23 stsp size_t len, total;
345 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
346 bff6ca00 2018-04-23 stsp
347 86acc566 2018-04-23 stsp memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
348 bff6ca00 2018-04-23 stsp icommit.author_len = strlen(commit->author);
349 788c352e 2018-06-16 stsp memcpy(&icommit.tm_author, &commit->tm_author,
350 788c352e 2018-06-16 stsp sizeof(icommit.tm_author));
351 bff6ca00 2018-04-23 stsp icommit.committer_len = strlen(commit->committer);
352 788c352e 2018-06-16 stsp memcpy(&icommit.tm_committer, &commit->tm_committer,
353 788c352e 2018-06-16 stsp sizeof(icommit.tm_committer));
354 bff6ca00 2018-04-23 stsp icommit.logmsg_len = strlen(commit->logmsg);
355 bff6ca00 2018-04-23 stsp icommit.nparents = commit->nparents;
356 bff6ca00 2018-04-23 stsp
357 bff6ca00 2018-04-23 stsp total = sizeof(icommit) + icommit.author_len +
358 788c352e 2018-06-16 stsp icommit.committer_len + icommit.logmsg_len +
359 86acc566 2018-04-23 stsp icommit.nparents * SHA1_DIGEST_LENGTH;
360 bff6ca00 2018-04-23 stsp /* XXX TODO support very large log messages properly */
361 bff6ca00 2018-04-23 stsp if (total > MAX_IMSGSIZE)
362 bff6ca00 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
363 bff6ca00 2018-04-23 stsp
364 bff6ca00 2018-04-23 stsp buf = malloc(total);
365 bff6ca00 2018-04-23 stsp if (buf == NULL)
366 bff6ca00 2018-04-23 stsp return got_error_from_errno();
367 bff6ca00 2018-04-23 stsp
368 bff6ca00 2018-04-23 stsp len = 0;
369 bff6ca00 2018-04-23 stsp memcpy(buf + len, &icommit, sizeof(icommit));
370 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
371 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->author, icommit.author_len);
372 bff6ca00 2018-04-23 stsp len += icommit.author_len;
373 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->committer, icommit.committer_len);
374 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
375 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
376 bff6ca00 2018-04-23 stsp len += icommit.logmsg_len;
377 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
378 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
379 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
380 bff6ca00 2018-04-23 stsp }
381 bff6ca00 2018-04-23 stsp
382 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
383 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
384 bff6ca00 2018-04-23 stsp goto done;
385 bff6ca00 2018-04-23 stsp }
386 bff6ca00 2018-04-23 stsp
387 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
388 bff6ca00 2018-04-23 stsp done:
389 bff6ca00 2018-04-23 stsp free(buf);
390 bff6ca00 2018-04-23 stsp return err;
391 bff6ca00 2018-04-23 stsp }
392 bff6ca00 2018-04-23 stsp const struct got_error *
393 068fd2bf 2018-04-24 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
394 bff6ca00 2018-04-23 stsp {
395 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
396 bff6ca00 2018-04-23 stsp struct imsg imsg;
397 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
398 bff6ca00 2018-04-23 stsp size_t len, datalen;
399 bff6ca00 2018-04-23 stsp int i;
400 bff6ca00 2018-04-23 stsp const size_t min_datalen =
401 bff6ca00 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
402 bff6ca00 2018-04-23 stsp sizeof(struct got_imsg_commit_object));
403 bff6ca00 2018-04-23 stsp uint8_t *data;
404 bff6ca00 2018-04-23 stsp
405 bff6ca00 2018-04-23 stsp *commit = NULL;
406 bff6ca00 2018-04-23 stsp
407 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
408 bff6ca00 2018-04-23 stsp if (err)
409 bff6ca00 2018-04-23 stsp return err;
410 bff6ca00 2018-04-23 stsp
411 bff6ca00 2018-04-23 stsp data = imsg.data;
412 bff6ca00 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
413 bff6ca00 2018-04-23 stsp len = 0;
414 bff6ca00 2018-04-23 stsp
415 bff6ca00 2018-04-23 stsp switch (imsg.hdr.type) {
416 bff6ca00 2018-04-23 stsp case GOT_IMSG_ERROR:
417 bff6ca00 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
418 bff6ca00 2018-04-23 stsp break;
419 bff6ca00 2018-04-23 stsp case GOT_IMSG_COMMIT:
420 bff6ca00 2018-04-23 stsp if (datalen < sizeof(icommit)) {
421 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
422 bff6ca00 2018-04-23 stsp break;
423 2178c42e 2018-04-22 stsp }
424 bff6ca00 2018-04-23 stsp
425 bff6ca00 2018-04-23 stsp memcpy(&icommit, data, sizeof(icommit));
426 bff6ca00 2018-04-23 stsp if (datalen != sizeof(icommit) + icommit.author_len +
427 788c352e 2018-06-16 stsp icommit.committer_len + icommit.logmsg_len +
428 86acc566 2018-04-23 stsp icommit.nparents * SHA1_DIGEST_LENGTH) {
429 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
430 bff6ca00 2018-04-23 stsp break;
431 bff6ca00 2018-04-23 stsp }
432 bff6ca00 2018-04-23 stsp if (icommit.nparents < 0) {
433 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
434 bff6ca00 2018-04-23 stsp break;
435 bff6ca00 2018-04-23 stsp }
436 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
437 bff6ca00 2018-04-23 stsp
438 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
439 bff6ca00 2018-04-23 stsp if (*commit == NULL) {
440 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
441 bff6ca00 2018-04-23 stsp break;
442 bff6ca00 2018-04-23 stsp }
443 bff6ca00 2018-04-23 stsp
444 86acc566 2018-04-23 stsp memcpy((*commit)->tree_id->sha1, icommit.tree_id,
445 86acc566 2018-04-23 stsp SHA1_DIGEST_LENGTH);
446 788c352e 2018-06-16 stsp memcpy(&(*commit)->tm_author, &icommit.tm_author,
447 788c352e 2018-06-16 stsp sizeof((*commit)->tm_author));
448 788c352e 2018-06-16 stsp memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
449 788c352e 2018-06-16 stsp sizeof((*commit)->tm_committer));
450 bff6ca00 2018-04-23 stsp
451 bff6ca00 2018-04-23 stsp if (icommit.author_len == 0) {
452 bff6ca00 2018-04-23 stsp (*commit)->author = strdup("");
453 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
454 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
455 bff6ca00 2018-04-23 stsp break;
456 bff6ca00 2018-04-23 stsp }
457 bff6ca00 2018-04-23 stsp } else {
458 bff6ca00 2018-04-23 stsp (*commit)->author = malloc(icommit.author_len + 1);
459 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
460 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
461 bff6ca00 2018-04-23 stsp break;
462 bff6ca00 2018-04-23 stsp }
463 bff6ca00 2018-04-23 stsp memcpy((*commit)->author, data + len,
464 bff6ca00 2018-04-23 stsp icommit.author_len);
465 bff6ca00 2018-04-23 stsp (*commit)->author[icommit.author_len] = '\0';
466 bff6ca00 2018-04-23 stsp }
467 bff6ca00 2018-04-23 stsp len += icommit.author_len;
468 6c281f94 2018-06-11 stsp
469 bff6ca00 2018-04-23 stsp if (icommit.committer_len == 0) {
470 bff6ca00 2018-04-23 stsp (*commit)->committer = strdup("");
471 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
472 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
473 bff6ca00 2018-04-23 stsp break;
474 bff6ca00 2018-04-23 stsp }
475 bff6ca00 2018-04-23 stsp } else {
476 bff6ca00 2018-04-23 stsp (*commit)->committer =
477 bff6ca00 2018-04-23 stsp malloc(icommit.committer_len + 1);
478 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
479 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
480 bff6ca00 2018-04-23 stsp break;
481 bff6ca00 2018-04-23 stsp }
482 bff6ca00 2018-04-23 stsp memcpy((*commit)->committer, data + len,
483 bff6ca00 2018-04-23 stsp icommit.committer_len);
484 bff6ca00 2018-04-23 stsp (*commit)->committer[icommit.committer_len] = '\0';
485 bff6ca00 2018-04-23 stsp }
486 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
487 bff6ca00 2018-04-23 stsp
488 bff6ca00 2018-04-23 stsp if (icommit.logmsg_len == 0) {
489 bff6ca00 2018-04-23 stsp (*commit)->logmsg = strdup("");
490 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
491 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
492 bff6ca00 2018-04-23 stsp break;
493 bff6ca00 2018-04-23 stsp }
494 bff6ca00 2018-04-23 stsp } else {
495 bff6ca00 2018-04-23 stsp (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
496 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
497 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
498 bff6ca00 2018-04-23 stsp break;
499 bff6ca00 2018-04-23 stsp }
500 bff6ca00 2018-04-23 stsp memcpy((*commit)->logmsg, data + len,
501 bff6ca00 2018-04-23 stsp icommit.logmsg_len);
502 bff6ca00 2018-04-23 stsp (*commit)->logmsg[icommit.logmsg_len] = '\0';
503 bff6ca00 2018-04-23 stsp }
504 bff6ca00 2018-04-23 stsp len += icommit.logmsg_len;
505 bff6ca00 2018-04-23 stsp
506 bff6ca00 2018-04-23 stsp for (i = 0; i < icommit.nparents; i++) {
507 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
508 86acc566 2018-04-23 stsp
509 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
510 79f35eb3 2018-06-11 stsp if (qid == NULL) {
511 86acc566 2018-04-23 stsp err = got_error_from_errno();
512 bff6ca00 2018-04-23 stsp break;
513 86acc566 2018-04-23 stsp }
514 79f35eb3 2018-06-11 stsp qid->id = calloc(1, sizeof(*qid->id));
515 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
516 86acc566 2018-04-23 stsp err = got_error_from_errno();
517 79f35eb3 2018-06-11 stsp free(qid);
518 86acc566 2018-04-23 stsp break;
519 86acc566 2018-04-23 stsp }
520 86acc566 2018-04-23 stsp
521 79f35eb3 2018-06-11 stsp memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
522 79f35eb3 2018-06-11 stsp sizeof(*qid->id));
523 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
524 86acc566 2018-04-23 stsp (*commit)->nparents++;
525 bff6ca00 2018-04-23 stsp }
526 2178c42e 2018-04-22 stsp break;
527 8c580685 2018-04-22 stsp default:
528 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
529 8c580685 2018-04-22 stsp break;
530 2178c42e 2018-04-22 stsp }
531 2178c42e 2018-04-22 stsp
532 2178c42e 2018-04-22 stsp imsg_free(&imsg);
533 e033d803 2018-04-23 stsp
534 e033d803 2018-04-23 stsp return err;
535 e033d803 2018-04-23 stsp }
536 e033d803 2018-04-23 stsp
537 e033d803 2018-04-23 stsp const struct got_error *
538 068fd2bf 2018-04-24 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
539 e033d803 2018-04-23 stsp {
540 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
541 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
542 e033d803 2018-04-23 stsp struct got_tree_entry *te;
543 e033d803 2018-04-23 stsp
544 883f0469 2018-06-23 stsp itree.nentries = tree->entries.nentries;
545 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
546 e033d803 2018-04-23 stsp == -1)
547 e033d803 2018-04-23 stsp return got_error_from_errno();
548 e033d803 2018-04-23 stsp
549 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
550 e033d803 2018-04-23 stsp if (err)
551 e033d803 2018-04-23 stsp return err;
552 e033d803 2018-04-23 stsp
553 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
554 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
555 e033d803 2018-04-23 stsp uint8_t *buf = NULL;
556 e033d803 2018-04-23 stsp size_t len = sizeof(ite) + strlen(te->name);
557 e033d803 2018-04-23 stsp
558 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
559 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
560 e033d803 2018-04-23 stsp
561 e033d803 2018-04-23 stsp buf = malloc(len);
562 e033d803 2018-04-23 stsp if (buf == NULL)
563 e033d803 2018-04-23 stsp return got_error_from_errno();
564 e033d803 2018-04-23 stsp
565 e033d803 2018-04-23 stsp memcpy(ite.id, te->id->sha1, sizeof(ite.id));
566 e033d803 2018-04-23 stsp ite.mode = te->mode;
567 e033d803 2018-04-23 stsp memcpy(buf, &ite, sizeof(ite));
568 e033d803 2018-04-23 stsp memcpy(buf + sizeof(ite), te->name, strlen(te->name));
569 e033d803 2018-04-23 stsp
570 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
571 e033d803 2018-04-23 stsp buf, len) == -1)
572 e033d803 2018-04-23 stsp err = got_error_from_errno();
573 e033d803 2018-04-23 stsp free(buf);
574 e033d803 2018-04-23 stsp if (err)
575 e033d803 2018-04-23 stsp return err;
576 e033d803 2018-04-23 stsp
577 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
578 e033d803 2018-04-23 stsp if (err)
579 e033d803 2018-04-23 stsp return err;
580 e033d803 2018-04-23 stsp }
581 e033d803 2018-04-23 stsp
582 e033d803 2018-04-23 stsp return NULL;
583 e033d803 2018-04-23 stsp }
584 e033d803 2018-04-23 stsp
585 e033d803 2018-04-23 stsp const struct got_error *
586 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
587 e033d803 2018-04-23 stsp {
588 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
589 e033d803 2018-04-23 stsp const size_t min_datalen =
590 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
591 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
592 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree = { 0 };
593 e033d803 2018-04-23 stsp int nentries = 0;
594 2178c42e 2018-04-22 stsp
595 e033d803 2018-04-23 stsp *tree = NULL;
596 e033d803 2018-04-23 stsp get_more:
597 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
598 e033d803 2018-04-23 stsp if (err)
599 1e51f5b9 2018-04-23 stsp goto done;
600 e033d803 2018-04-23 stsp
601 e033d803 2018-04-23 stsp while (1) {
602 e033d803 2018-04-23 stsp struct imsg imsg;
603 e033d803 2018-04-23 stsp size_t n;
604 e033d803 2018-04-23 stsp size_t datalen;
605 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
606 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
607 e033d803 2018-04-23 stsp
608 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
609 e033d803 2018-04-23 stsp if (n == 0) {
610 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries)
611 e033d803 2018-04-23 stsp goto get_more;
612 e033d803 2018-04-23 stsp break;
613 e033d803 2018-04-23 stsp }
614 e033d803 2018-04-23 stsp
615 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
616 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
617 e033d803 2018-04-23 stsp
618 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
619 e033d803 2018-04-23 stsp
620 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
621 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
622 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
623 e033d803 2018-04-23 stsp break;
624 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
625 e033d803 2018-04-23 stsp /* This message should only appear once. */
626 e033d803 2018-04-23 stsp if (*tree != NULL) {
627 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
628 e033d803 2018-04-23 stsp break;
629 e033d803 2018-04-23 stsp }
630 e033d803 2018-04-23 stsp if (datalen != sizeof(itree)) {
631 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
632 e033d803 2018-04-23 stsp break;
633 e033d803 2018-04-23 stsp }
634 e033d803 2018-04-23 stsp memcpy(&itree, imsg.data, sizeof(itree));
635 e033d803 2018-04-23 stsp *tree = calloc(1, sizeof(**tree));
636 e033d803 2018-04-23 stsp if (*tree == NULL) {
637 e033d803 2018-04-23 stsp err = got_error_from_errno();
638 e033d803 2018-04-23 stsp break;
639 e033d803 2018-04-23 stsp }
640 883f0469 2018-06-23 stsp (*tree)->entries.nentries = itree.nentries;
641 883f0469 2018-06-23 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
642 e033d803 2018-04-23 stsp break;
643 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
644 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
645 e033d803 2018-04-23 stsp if (*tree == NULL) {
646 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
647 e033d803 2018-04-23 stsp break;
648 e033d803 2018-04-23 stsp }
649 e033d803 2018-04-23 stsp if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
650 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
651 e033d803 2018-04-23 stsp break;
652 e033d803 2018-04-23 stsp }
653 e033d803 2018-04-23 stsp
654 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
655 e033d803 2018-04-23 stsp datalen -= sizeof(ite);
656 e033d803 2018-04-23 stsp memcpy(&ite, imsg.data, sizeof(ite));
657 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
658 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
659 e033d803 2018-04-23 stsp break;
660 e033d803 2018-04-23 stsp }
661 052d4dc3 2018-04-23 stsp
662 e033d803 2018-04-23 stsp te = got_alloc_tree_entry_partial();
663 e033d803 2018-04-23 stsp if (te == NULL) {
664 e033d803 2018-04-23 stsp err = got_error_from_errno();
665 e033d803 2018-04-23 stsp break;
666 e033d803 2018-04-23 stsp }
667 e033d803 2018-04-23 stsp te->name = malloc(datalen + 1);
668 e033d803 2018-04-23 stsp if (te->name == NULL) {
669 e033d803 2018-04-23 stsp free(te);
670 e033d803 2018-04-23 stsp err = got_error_from_errno();
671 e033d803 2018-04-23 stsp break;
672 e033d803 2018-04-23 stsp }
673 052d4dc3 2018-04-23 stsp memcpy(te->name, imsg.data + sizeof(ite), datalen);
674 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
675 e033d803 2018-04-23 stsp
676 e033d803 2018-04-23 stsp memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
677 e033d803 2018-04-23 stsp te->mode = ite.mode;
678 883f0469 2018-06-23 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
679 e033d803 2018-04-23 stsp nentries++;
680 e033d803 2018-04-23 stsp break;
681 e033d803 2018-04-23 stsp default:
682 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
683 e033d803 2018-04-23 stsp break;
684 e033d803 2018-04-23 stsp }
685 e033d803 2018-04-23 stsp
686 e033d803 2018-04-23 stsp imsg_free(&imsg);
687 e033d803 2018-04-23 stsp }
688 1e51f5b9 2018-04-23 stsp done:
689 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries) {
690 1e51f5b9 2018-04-23 stsp if (err == NULL)
691 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
692 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
693 e033d803 2018-04-23 stsp *tree = NULL;
694 ff6b18f8 2018-04-24 stsp }
695 ff6b18f8 2018-04-24 stsp
696 ff6b18f8 2018-04-24 stsp return err;
697 ff6b18f8 2018-04-24 stsp }
698 ff6b18f8 2018-04-24 stsp
699 ff6b18f8 2018-04-24 stsp const struct got_error *
700 2967a784 2018-04-24 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
701 ff6b18f8 2018-04-24 stsp {
702 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
703 2967a784 2018-04-24 stsp
704 2967a784 2018-04-24 stsp iblob.size = size;
705 ff6b18f8 2018-04-24 stsp /* Data has already been written to file descriptor. */
706 2967a784 2018-04-24 stsp
707 2967a784 2018-04-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
708 2967a784 2018-04-24 stsp == -1)
709 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
710 ff6b18f8 2018-04-24 stsp
711 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
712 ff6b18f8 2018-04-24 stsp }
713 ff6b18f8 2018-04-24 stsp
714 ff6b18f8 2018-04-24 stsp const struct got_error *
715 2967a784 2018-04-24 stsp got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
716 ff6b18f8 2018-04-24 stsp {
717 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
718 ff6b18f8 2018-04-24 stsp struct imsg imsg;
719 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
720 ff6b18f8 2018-04-24 stsp size_t datalen;
721 ff6b18f8 2018-04-24 stsp
722 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
723 ff6b18f8 2018-04-24 stsp if (err)
724 ff6b18f8 2018-04-24 stsp return err;
725 ff6b18f8 2018-04-24 stsp
726 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
727 ff6b18f8 2018-04-24 stsp
728 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
729 ff6b18f8 2018-04-24 stsp case GOT_IMSG_ERROR:
730 ff6b18f8 2018-04-24 stsp err = recv_imsg_error(&imsg, datalen);
731 ff6b18f8 2018-04-24 stsp break;
732 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
733 2967a784 2018-04-24 stsp if (datalen != sizeof(iblob))
734 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
735 2967a784 2018-04-24 stsp memcpy(&iblob, imsg.data, sizeof(iblob));
736 2967a784 2018-04-24 stsp *size = iblob.size;
737 ff6b18f8 2018-04-24 stsp /* Data has been written to file descriptor. */
738 ff6b18f8 2018-04-24 stsp break;
739 ff6b18f8 2018-04-24 stsp default:
740 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
741 ff6b18f8 2018-04-24 stsp break;
742 e033d803 2018-04-23 stsp }
743 e033d803 2018-04-23 stsp
744 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
745 ff6b18f8 2018-04-24 stsp
746 2178c42e 2018-04-22 stsp return err;
747 2178c42e 2018-04-22 stsp }