2 2178c42e 2018-04-22 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
31 2178c42e 2018-04-22 stsp #include "got_object.h"
32 2178c42e 2018-04-22 stsp #include "got_error.h"
34 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
35 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
36 2178c42e 2018-04-22 stsp #include "got_lib_zbuf.h"
37 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
38 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
41 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
44 2178c42e 2018-04-22 stsp static const struct got_error *
45 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
47 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
50 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
51 2178c42e 2018-04-22 stsp pfd[0].events = events;
53 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
54 2178c42e 2018-04-22 stsp if (n == -1)
55 2178c42e 2018-04-22 stsp return got_error_from_errno();
57 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
58 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
59 2178c42e 2018-04-22 stsp return got_error_from_errno();
60 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
61 2178c42e 2018-04-22 stsp return NULL;
63 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
66 c4eae628 2018-04-23 stsp static const struct got_error *
67 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
69 fe36cf76 2018-04-23 stsp const struct got_error *err;
72 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
76 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
77 fe36cf76 2018-04-23 stsp if (n == -1) {
78 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
79 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
80 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
83 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
85 e033d803 2018-04-23 stsp return NULL;
88 e033d803 2018-04-23 stsp static const struct got_error *
89 e033d803 2018-04-23 stsp recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
91 e033d803 2018-04-23 stsp const struct got_error *err;
94 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
98 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
100 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
102 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
103 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
105 fe36cf76 2018-04-23 stsp return NULL;
108 fe36cf76 2018-04-23 stsp static const struct got_error *
109 c4eae628 2018-04-23 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
111 c4eae628 2018-04-23 stsp struct got_imsg_error ierr;
113 c4eae628 2018-04-23 stsp if (datalen != sizeof(ierr))
114 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
116 c4eae628 2018-04-23 stsp memcpy(&ierr, imsg->data, sizeof(ierr));
117 c4eae628 2018-04-23 stsp if (ierr.code == GOT_ERR_ERRNO) {
118 c4eae628 2018-04-23 stsp static struct got_error serr;
119 c4eae628 2018-04-23 stsp serr.code = GOT_ERR_ERRNO;
120 c4eae628 2018-04-23 stsp serr.msg = strerror(ierr.errno_code);
121 c4eae628 2018-04-23 stsp return &serr;
124 c4eae628 2018-04-23 stsp return got_error(ierr.code);
127 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
129 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
131 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
132 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
135 2178c42e 2018-04-22 stsp ierr.code = err->code;
136 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
137 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
139 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
140 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
141 2178c42e 2018-04-22 stsp if (ret != -1) {
142 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
143 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
147 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
148 5d43e84d 2018-04-23 stsp if (poll_err) {
149 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
150 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
154 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
155 5d43e84d 2018-04-23 stsp if (ret == -1) {
156 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
157 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
162 e033d803 2018-04-23 stsp static const struct got_error *
163 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
165 e033d803 2018-04-23 stsp const struct got_error *err;
167 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
169 e033d803 2018-04-23 stsp return err;
171 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
172 e033d803 2018-04-23 stsp return got_error_from_errno();
174 e033d803 2018-04-23 stsp return NULL;
177 2178c42e 2018-04-22 stsp const struct got_error *
178 2178c42e 2018-04-22 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
180 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
182 2178c42e 2018-04-22 stsp iobj.type = obj->type;
183 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
184 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
185 2178c42e 2018-04-22 stsp iobj.size = obj->size;
186 2178c42e 2018-04-22 stsp iobj.ndeltas = ndeltas;
188 2178c42e 2018-04-22 stsp if (ndeltas > 0) {
189 2178c42e 2018-04-22 stsp /* TODO: Handle deltas */
192 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
194 2178c42e 2018-04-22 stsp return got_error_from_errno();
196 e033d803 2018-04-23 stsp return flush_imsg(ibuf);
199 2178c42e 2018-04-22 stsp const struct got_error *
200 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
202 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
203 2178c42e 2018-04-22 stsp struct imsg imsg;
204 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
205 2178c42e 2018-04-22 stsp size_t datalen;
207 c4eae628 2018-04-23 stsp const size_t min_datalen =
208 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
210 2178c42e 2018-04-22 stsp *obj = NULL;
212 fe36cf76 2018-04-23 stsp err = recv_one_imsg(&imsg, ibuf, min_datalen);
214 2178c42e 2018-04-22 stsp return err;
216 2178c42e 2018-04-22 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
218 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
219 2178c42e 2018-04-22 stsp case GOT_IMSG_ERROR:
220 c4eae628 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
222 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
223 2178c42e 2018-04-22 stsp if (datalen != sizeof(iobj)) {
224 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
228 2178c42e 2018-04-22 stsp memcpy(&iobj, imsg.data, sizeof(iobj));
229 2178c42e 2018-04-22 stsp if (iobj.ndeltas < 0 ||
230 2178c42e 2018-04-22 stsp iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
231 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
235 2178c42e 2018-04-22 stsp *obj = calloc(1, sizeof(**obj));
236 2178c42e 2018-04-22 stsp if (*obj == NULL) {
237 2178c42e 2018-04-22 stsp err = got_error_from_errno();
241 2178c42e 2018-04-22 stsp (*obj)->type = iobj.type;
242 2178c42e 2018-04-22 stsp (*obj)->hdrlen = iobj.hdrlen;
243 2178c42e 2018-04-22 stsp (*obj)->size = iobj.size;
244 2178c42e 2018-04-22 stsp for (i = 0; i < iobj.ndeltas; i++) {
245 2178c42e 2018-04-22 stsp /* TODO: Handle deltas */
249 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
253 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
255 bff6ca00 2018-04-23 stsp return err;
258 bff6ca00 2018-04-23 stsp const struct got_error *
259 bff6ca00 2018-04-23 stsp got_privsep_send_commit_obj(struct imsgbuf *ibuf, struct got_commit_object *commit)
261 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
262 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
263 bff6ca00 2018-04-23 stsp uint8_t *buf;
264 bff6ca00 2018-04-23 stsp size_t len, total;
265 bff6ca00 2018-04-23 stsp struct got_parent_id *pid;
267 86acc566 2018-04-23 stsp memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
268 bff6ca00 2018-04-23 stsp icommit.author_len = strlen(commit->author);
269 bff6ca00 2018-04-23 stsp icommit.committer_len = strlen(commit->committer);
270 bff6ca00 2018-04-23 stsp icommit.logmsg_len = strlen(commit->logmsg);
271 bff6ca00 2018-04-23 stsp icommit.nparents = commit->nparents;
273 bff6ca00 2018-04-23 stsp total = sizeof(icommit) + icommit.author_len +
274 bff6ca00 2018-04-23 stsp icommit.committer_len + icommit.logmsg_len +
275 86acc566 2018-04-23 stsp icommit.nparents * SHA1_DIGEST_LENGTH;
276 bff6ca00 2018-04-23 stsp /* XXX TODO support very large log messages properly */
277 bff6ca00 2018-04-23 stsp if (total > MAX_IMSGSIZE)
278 bff6ca00 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
280 bff6ca00 2018-04-23 stsp buf = malloc(total);
281 bff6ca00 2018-04-23 stsp if (buf == NULL)
282 bff6ca00 2018-04-23 stsp return got_error_from_errno();
285 bff6ca00 2018-04-23 stsp memcpy(buf + len, &icommit, sizeof(icommit));
286 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
287 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->author, icommit.author_len);
288 bff6ca00 2018-04-23 stsp len += icommit.author_len;
289 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->committer, icommit.committer_len);
290 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
291 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
292 bff6ca00 2018-04-23 stsp len += icommit.logmsg_len;
293 bff6ca00 2018-04-23 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
294 86acc566 2018-04-23 stsp memcpy(buf + len, pid->id, SHA1_DIGEST_LENGTH);
295 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
298 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
299 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
303 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
306 bff6ca00 2018-04-23 stsp return err;
308 bff6ca00 2018-04-23 stsp const struct got_error *
309 bff6ca00 2018-04-23 stsp got_privsep_recv_commit_obj(struct got_commit_object **commit,
310 bff6ca00 2018-04-23 stsp struct imsgbuf *ibuf)
312 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
313 bff6ca00 2018-04-23 stsp struct imsg imsg;
314 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
315 bff6ca00 2018-04-23 stsp size_t len, datalen;
317 bff6ca00 2018-04-23 stsp const size_t min_datalen =
318 bff6ca00 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
319 bff6ca00 2018-04-23 stsp sizeof(struct got_imsg_commit_object));
320 bff6ca00 2018-04-23 stsp uint8_t *data;
322 bff6ca00 2018-04-23 stsp *commit = NULL;
324 bff6ca00 2018-04-23 stsp err = recv_one_imsg(&imsg, ibuf, min_datalen);
326 bff6ca00 2018-04-23 stsp return err;
328 bff6ca00 2018-04-23 stsp data = imsg.data;
329 bff6ca00 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
332 bff6ca00 2018-04-23 stsp switch (imsg.hdr.type) {
333 bff6ca00 2018-04-23 stsp case GOT_IMSG_ERROR:
334 bff6ca00 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
336 bff6ca00 2018-04-23 stsp case GOT_IMSG_COMMIT:
337 bff6ca00 2018-04-23 stsp if (datalen < sizeof(icommit)) {
338 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
342 bff6ca00 2018-04-23 stsp memcpy(&icommit, data, sizeof(icommit));
343 bff6ca00 2018-04-23 stsp if (datalen != sizeof(icommit) + icommit.author_len +
344 bff6ca00 2018-04-23 stsp icommit.committer_len + icommit.logmsg_len +
345 86acc566 2018-04-23 stsp icommit.nparents * SHA1_DIGEST_LENGTH) {
346 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
349 bff6ca00 2018-04-23 stsp if (icommit.nparents < 0) {
350 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
353 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
355 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
356 bff6ca00 2018-04-23 stsp if (*commit == NULL) {
357 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
361 86acc566 2018-04-23 stsp memcpy((*commit)->tree_id->sha1, icommit.tree_id,
362 86acc566 2018-04-23 stsp SHA1_DIGEST_LENGTH);
364 bff6ca00 2018-04-23 stsp if (icommit.author_len == 0) {
365 bff6ca00 2018-04-23 stsp (*commit)->author = strdup("");
366 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
367 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
371 bff6ca00 2018-04-23 stsp (*commit)->author = malloc(icommit.author_len + 1);
372 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
373 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
376 bff6ca00 2018-04-23 stsp memcpy((*commit)->author, data + len,
377 bff6ca00 2018-04-23 stsp icommit.author_len);
378 bff6ca00 2018-04-23 stsp (*commit)->author[icommit.author_len] = '\0';
380 bff6ca00 2018-04-23 stsp len += icommit.author_len;
382 bff6ca00 2018-04-23 stsp if (icommit.committer_len == 0) {
383 bff6ca00 2018-04-23 stsp (*commit)->committer = strdup("");
384 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
385 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
389 bff6ca00 2018-04-23 stsp (*commit)->committer =
390 bff6ca00 2018-04-23 stsp malloc(icommit.committer_len + 1);
391 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
392 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
395 bff6ca00 2018-04-23 stsp memcpy((*commit)->committer, data + len,
396 bff6ca00 2018-04-23 stsp icommit.committer_len);
397 bff6ca00 2018-04-23 stsp (*commit)->committer[icommit.committer_len] = '\0';
399 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
401 bff6ca00 2018-04-23 stsp if (icommit.logmsg_len == 0) {
402 bff6ca00 2018-04-23 stsp (*commit)->logmsg = strdup("");
403 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
404 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
408 bff6ca00 2018-04-23 stsp (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
409 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
410 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
413 bff6ca00 2018-04-23 stsp memcpy((*commit)->logmsg, data + len,
414 bff6ca00 2018-04-23 stsp icommit.logmsg_len);
415 bff6ca00 2018-04-23 stsp (*commit)->logmsg[icommit.logmsg_len] = '\0';
417 bff6ca00 2018-04-23 stsp len += icommit.logmsg_len;
419 bff6ca00 2018-04-23 stsp for (i = 0; i < icommit.nparents; i++) {
420 86acc566 2018-04-23 stsp struct got_parent_id *pid;
422 86acc566 2018-04-23 stsp pid = calloc(1, sizeof(*pid));
423 86acc566 2018-04-23 stsp if (pid == NULL) {
424 86acc566 2018-04-23 stsp err = got_error_from_errno();
427 86acc566 2018-04-23 stsp pid->id = calloc(1, sizeof(*pid->id));
428 86acc566 2018-04-23 stsp if (pid->id == NULL) {
429 86acc566 2018-04-23 stsp err = got_error_from_errno();
434 86acc566 2018-04-23 stsp memcpy(pid->id, data + len + i * SHA1_DIGEST_LENGTH,
435 86acc566 2018-04-23 stsp sizeof(*pid->id));
436 86acc566 2018-04-23 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
437 86acc566 2018-04-23 stsp (*commit)->nparents++;
441 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
445 2178c42e 2018-04-22 stsp imsg_free(&imsg);
447 e033d803 2018-04-23 stsp return err;
450 e033d803 2018-04-23 stsp const struct got_error *
451 e033d803 2018-04-23 stsp got_privsep_send_tree_obj(struct imsgbuf *ibuf, struct got_tree_object *tree)
453 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
454 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
455 e033d803 2018-04-23 stsp struct got_tree_entry *te;
457 e033d803 2018-04-23 stsp itree.nentries = tree->nentries;
458 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
460 e033d803 2018-04-23 stsp return got_error_from_errno();
462 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
464 e033d803 2018-04-23 stsp return err;
466 e033d803 2018-04-23 stsp SIMPLEQ_FOREACH(te, &tree->entries, entry) {
467 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
468 e033d803 2018-04-23 stsp uint8_t *buf = NULL;
469 e033d803 2018-04-23 stsp size_t len = sizeof(ite) + strlen(te->name);
471 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
472 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
474 e033d803 2018-04-23 stsp buf = malloc(len);
475 e033d803 2018-04-23 stsp if (buf == NULL)
476 e033d803 2018-04-23 stsp return got_error_from_errno();
478 e033d803 2018-04-23 stsp memcpy(ite.id, te->id->sha1, sizeof(ite.id));
479 e033d803 2018-04-23 stsp ite.mode = te->mode;
480 e033d803 2018-04-23 stsp memcpy(buf, &ite, sizeof(ite));
481 e033d803 2018-04-23 stsp memcpy(buf + sizeof(ite), te->name, strlen(te->name));
483 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
484 e033d803 2018-04-23 stsp buf, len) == -1)
485 e033d803 2018-04-23 stsp err = got_error_from_errno();
488 e033d803 2018-04-23 stsp return err;
490 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
492 e033d803 2018-04-23 stsp return err;
495 e033d803 2018-04-23 stsp return NULL;
498 e033d803 2018-04-23 stsp const struct got_error *
499 e033d803 2018-04-23 stsp got_privsep_recv_tree_obj(struct got_tree_object **tree, struct imsgbuf *ibuf)
501 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
502 e033d803 2018-04-23 stsp const size_t min_datalen =
503 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
504 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
505 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree = { 0 };
506 e033d803 2018-04-23 stsp int nentries = 0;
508 e033d803 2018-04-23 stsp *tree = NULL;
510 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
512 e033d803 2018-04-23 stsp return err;
514 e033d803 2018-04-23 stsp while (1) {
515 e033d803 2018-04-23 stsp struct imsg imsg;
517 e033d803 2018-04-23 stsp size_t datalen;
518 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
519 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
521 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
522 e033d803 2018-04-23 stsp if (n == 0) {
523 e033d803 2018-04-23 stsp if (*tree && (*tree)->nentries != nentries)
524 e033d803 2018-04-23 stsp goto get_more;
528 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
529 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
531 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
533 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
534 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
535 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
537 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
538 e033d803 2018-04-23 stsp /* This message should only appear once. */
539 e033d803 2018-04-23 stsp if (*tree != NULL) {
540 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
543 e033d803 2018-04-23 stsp if (datalen != sizeof(itree)) {
544 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
547 e033d803 2018-04-23 stsp memcpy(&itree, imsg.data, sizeof(itree));
548 e033d803 2018-04-23 stsp *tree = calloc(1, sizeof(**tree));
549 e033d803 2018-04-23 stsp if (*tree == NULL) {
550 e033d803 2018-04-23 stsp err = got_error_from_errno();
553 e033d803 2018-04-23 stsp (*tree)->nentries = itree.nentries;
554 e033d803 2018-04-23 stsp SIMPLEQ_INIT(&(*tree)->entries);
556 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
557 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
558 e033d803 2018-04-23 stsp if (*tree == NULL) {
559 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
562 e033d803 2018-04-23 stsp if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
563 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
567 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
568 e033d803 2018-04-23 stsp datalen -= sizeof(ite);
569 e033d803 2018-04-23 stsp memcpy(&ite, imsg.data, sizeof(ite));
570 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
571 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
575 e033d803 2018-04-23 stsp te = got_alloc_tree_entry_partial();
576 e033d803 2018-04-23 stsp if (te == NULL) {
577 e033d803 2018-04-23 stsp err = got_error_from_errno();
580 e033d803 2018-04-23 stsp te->name = malloc(datalen + 1);
581 e033d803 2018-04-23 stsp if (te->name == NULL) {
583 e033d803 2018-04-23 stsp err = got_error_from_errno();
586 052d4dc3 2018-04-23 stsp memcpy(te->name, imsg.data + sizeof(ite), datalen);
587 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
589 e033d803 2018-04-23 stsp memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
590 e033d803 2018-04-23 stsp te->mode = ite.mode;
591 e033d803 2018-04-23 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
592 e033d803 2018-04-23 stsp nentries++;
595 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
599 e033d803 2018-04-23 stsp imsg_free(&imsg);
602 e033d803 2018-04-23 stsp if (*tree && (*tree)->nentries != nentries) {
603 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
604 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
605 e033d803 2018-04-23 stsp *tree = NULL;
608 2178c42e 2018-04-22 stsp return err;