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>
20 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
21 876c234b 2018-09-10 stsp #include <sys/wait.h>
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>
34 2178c42e 2018-04-22 stsp #include "got_object.h"
35 2178c42e 2018-04-22 stsp #include "got_error.h"
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"
46 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
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)
52 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
55 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
56 2178c42e 2018-04-22 stsp pfd[0].events = events;
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();
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;
68 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
71 c4eae628 2018-04-23 stsp static const struct got_error *
72 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
74 fe36cf76 2018-04-23 stsp const struct got_error *err;
77 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
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);
88 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
90 e033d803 2018-04-23 stsp return NULL;
93 ad242220 2018-09-08 stsp const struct got_error *
94 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
96 876c234b 2018-09-10 stsp int child_status;
98 876c234b 2018-09-10 stsp waitpid(pid, &child_status, 0);
100 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
101 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
103 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
104 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
106 876c234b 2018-09-10 stsp return NULL;
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)
112 e033d803 2018-04-23 stsp const struct got_error *err;
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();
119 876c234b 2018-09-10 stsp while (n == 0) {
120 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
122 876c234b 2018-09-10 stsp return err;
123 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
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);
129 fe36cf76 2018-04-23 stsp return NULL;
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)
135 c4eae628 2018-04-23 stsp struct got_imsg_error ierr;
137 c4eae628 2018-04-23 stsp if (datalen != sizeof(ierr))
138 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
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;
148 c4eae628 2018-04-23 stsp return got_error(ierr.code);
151 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
153 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
155 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
156 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
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;
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));
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);
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));
186 e033d803 2018-04-23 stsp static const struct got_error *
187 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
189 e033d803 2018-04-23 stsp const struct got_error *err;
191 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
193 e033d803 2018-04-23 stsp return err;
195 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
196 e033d803 2018-04-23 stsp return got_error_from_errno();
198 e033d803 2018-04-23 stsp return NULL;
201 ad242220 2018-09-08 stsp const struct got_error *
202 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
204 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
205 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
207 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
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();
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;
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)
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;
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;
229 ad242220 2018-09-08 stsp case GOT_OBJ_TYPE_COMMIT:
230 ad242220 2018-09-08 stsp imsg_code = GOT_IMSG_COMMIT_REQUEST;
232 55da3778 2018-09-10 stsp case GOT_OBJ_TYPE_BLOB:
233 55da3778 2018-09-10 stsp imsg_code = GOT_IMSG_BLOB_REQUEST;
236 ad242220 2018-09-08 stsp return got_error(GOT_ERR_OBJ_TYPE);
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;
249 ad242220 2018-09-08 stsp iobjp = &iobj;
250 ad242220 2018-09-08 stsp iobj_size = sizeof(iobj);
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();
256 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
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)
262 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
264 ad242220 2018-09-08 stsp return got_error_from_errno();
266 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
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)
272 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
274 ad242220 2018-09-08 stsp return got_error_from_errno();
276 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
279 ad242220 2018-09-08 stsp const struct got_error *
280 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
282 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
284 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
285 2178c42e 2018-04-22 stsp iobj.type = obj->type;
286 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
287 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
288 2178c42e 2018-04-22 stsp iobj.size = obj->size;
289 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
290 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
291 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
294 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
296 2178c42e 2018-04-22 stsp return got_error_from_errno();
298 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
301 cfd633c2 2018-09-10 stsp const struct got_error *
302 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
303 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
305 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
306 cfd633c2 2018-09-10 stsp struct got_imsg_object iobj;
307 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
309 cfd633c2 2018-09-10 stsp if (datalen != sizeof(iobj))
310 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
312 cfd633c2 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
314 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
315 cfd633c2 2018-09-10 stsp if (*obj == NULL)
316 cfd633c2 2018-09-10 stsp return got_error_from_errno();
318 c59b3346 2018-09-11 stsp memcpy((*obj)->id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
319 cfd633c2 2018-09-10 stsp (*obj)->type = iobj.type;
320 cfd633c2 2018-09-10 stsp (*obj)->flags = iobj.flags;
321 cfd633c2 2018-09-10 stsp (*obj)->hdrlen = iobj.hdrlen;
322 cfd633c2 2018-09-10 stsp (*obj)->size = iobj.size;
323 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
324 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
325 c59b3346 2018-09-11 stsp (*obj)->pack_offset = iobj.pack_offset;
326 c59b3346 2018-09-11 stsp (*obj)->pack_idx = iobj.pack_idx;
329 876c234b 2018-09-10 stsp return err;
332 2178c42e 2018-04-22 stsp const struct got_error *
333 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
335 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
336 2178c42e 2018-04-22 stsp struct imsg imsg;
337 2178c42e 2018-04-22 stsp size_t datalen;
338 c4eae628 2018-04-23 stsp const size_t min_datalen =
339 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
341 2178c42e 2018-04-22 stsp *obj = NULL;
343 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
345 2178c42e 2018-04-22 stsp return err;
347 2178c42e 2018-04-22 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
349 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
350 2178c42e 2018-04-22 stsp case GOT_IMSG_ERROR:
351 c4eae628 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
353 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
354 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
357 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
361 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
363 bff6ca00 2018-04-23 stsp return err;
366 c75f7264 2018-09-11 stsp static const struct got_error *
367 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
368 c75f7264 2018-09-11 stsp size_t logmsg_len)
370 c75f7264 2018-09-11 stsp size_t offset, remain;
372 c75f7264 2018-09-11 stsp offset = 0;
373 c75f7264 2018-09-11 stsp remain = logmsg_len;
374 c75f7264 2018-09-11 stsp while (remain > 0) {
375 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
377 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
378 c75f7264 2018-09-11 stsp commit->logmsg + offset, n) == -1)
379 c75f7264 2018-09-11 stsp return got_error_from_errno();
381 c75f7264 2018-09-11 stsp if (imsg_flush(ibuf) == -1)
382 c75f7264 2018-09-11 stsp return got_error_from_errno();
384 c75f7264 2018-09-11 stsp offset += n;
385 c75f7264 2018-09-11 stsp remain -= n;
388 c75f7264 2018-09-11 stsp return NULL;
391 bff6ca00 2018-04-23 stsp const struct got_error *
392 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
394 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
395 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
396 bff6ca00 2018-04-23 stsp uint8_t *buf;
397 bff6ca00 2018-04-23 stsp size_t len, total;
398 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
399 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
401 86acc566 2018-04-23 stsp memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
402 bff6ca00 2018-04-23 stsp icommit.author_len = strlen(commit->author);
403 788c352e 2018-06-16 stsp memcpy(&icommit.tm_author, &commit->tm_author,
404 788c352e 2018-06-16 stsp sizeof(icommit.tm_author));
405 bff6ca00 2018-04-23 stsp icommit.committer_len = strlen(commit->committer);
406 788c352e 2018-06-16 stsp memcpy(&icommit.tm_committer, &commit->tm_committer,
407 788c352e 2018-06-16 stsp sizeof(icommit.tm_committer));
408 c75f7264 2018-09-11 stsp icommit.logmsg_len = logmsg_len;
409 bff6ca00 2018-04-23 stsp icommit.nparents = commit->nparents;
411 bff6ca00 2018-04-23 stsp total = sizeof(icommit) + icommit.author_len +
412 c75f7264 2018-09-11 stsp icommit.committer_len + icommit.nparents * SHA1_DIGEST_LENGTH;
414 bff6ca00 2018-04-23 stsp buf = malloc(total);
415 bff6ca00 2018-04-23 stsp if (buf == NULL)
416 bff6ca00 2018-04-23 stsp return got_error_from_errno();
419 bff6ca00 2018-04-23 stsp memcpy(buf + len, &icommit, sizeof(icommit));
420 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
421 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->author, icommit.author_len);
422 bff6ca00 2018-04-23 stsp len += icommit.author_len;
423 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->committer, icommit.committer_len);
424 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
425 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
426 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
427 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
430 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
431 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
435 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
439 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
442 bff6ca00 2018-04-23 stsp return err;
445 bff6ca00 2018-04-23 stsp const struct got_error *
446 068fd2bf 2018-04-24 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
448 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
449 bff6ca00 2018-04-23 stsp struct imsg imsg;
450 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
451 bff6ca00 2018-04-23 stsp size_t len, datalen;
453 bff6ca00 2018-04-23 stsp const size_t min_datalen =
454 bff6ca00 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
455 bff6ca00 2018-04-23 stsp sizeof(struct got_imsg_commit_object));
456 bff6ca00 2018-04-23 stsp uint8_t *data;
458 bff6ca00 2018-04-23 stsp *commit = NULL;
460 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
462 bff6ca00 2018-04-23 stsp return err;
464 bff6ca00 2018-04-23 stsp data = imsg.data;
465 bff6ca00 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
468 bff6ca00 2018-04-23 stsp switch (imsg.hdr.type) {
469 bff6ca00 2018-04-23 stsp case GOT_IMSG_ERROR:
470 bff6ca00 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
472 bff6ca00 2018-04-23 stsp case GOT_IMSG_COMMIT:
473 bff6ca00 2018-04-23 stsp if (datalen < sizeof(icommit)) {
474 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
478 bff6ca00 2018-04-23 stsp memcpy(&icommit, data, sizeof(icommit));
479 bff6ca00 2018-04-23 stsp if (datalen != sizeof(icommit) + icommit.author_len +
480 c75f7264 2018-09-11 stsp icommit.committer_len +
481 86acc566 2018-04-23 stsp icommit.nparents * SHA1_DIGEST_LENGTH) {
482 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
485 bff6ca00 2018-04-23 stsp if (icommit.nparents < 0) {
486 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
489 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
491 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
492 bff6ca00 2018-04-23 stsp if (*commit == NULL) {
493 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
497 86acc566 2018-04-23 stsp memcpy((*commit)->tree_id->sha1, icommit.tree_id,
498 86acc566 2018-04-23 stsp SHA1_DIGEST_LENGTH);
499 788c352e 2018-06-16 stsp memcpy(&(*commit)->tm_author, &icommit.tm_author,
500 788c352e 2018-06-16 stsp sizeof((*commit)->tm_author));
501 788c352e 2018-06-16 stsp memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
502 788c352e 2018-06-16 stsp sizeof((*commit)->tm_committer));
504 bff6ca00 2018-04-23 stsp if (icommit.author_len == 0) {
505 bff6ca00 2018-04-23 stsp (*commit)->author = strdup("");
506 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
507 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
511 bff6ca00 2018-04-23 stsp (*commit)->author = malloc(icommit.author_len + 1);
512 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
513 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
516 bff6ca00 2018-04-23 stsp memcpy((*commit)->author, data + len,
517 bff6ca00 2018-04-23 stsp icommit.author_len);
518 bff6ca00 2018-04-23 stsp (*commit)->author[icommit.author_len] = '\0';
520 bff6ca00 2018-04-23 stsp len += icommit.author_len;
522 bff6ca00 2018-04-23 stsp if (icommit.committer_len == 0) {
523 bff6ca00 2018-04-23 stsp (*commit)->committer = strdup("");
524 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
525 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
529 bff6ca00 2018-04-23 stsp (*commit)->committer =
530 bff6ca00 2018-04-23 stsp malloc(icommit.committer_len + 1);
531 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
532 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
535 bff6ca00 2018-04-23 stsp memcpy((*commit)->committer, data + len,
536 bff6ca00 2018-04-23 stsp icommit.committer_len);
537 bff6ca00 2018-04-23 stsp (*commit)->committer[icommit.committer_len] = '\0';
539 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
541 bff6ca00 2018-04-23 stsp if (icommit.logmsg_len == 0) {
542 bff6ca00 2018-04-23 stsp (*commit)->logmsg = strdup("");
543 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
544 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
548 c75f7264 2018-09-11 stsp size_t offset = 0, remain = icommit.logmsg_len;
550 bff6ca00 2018-04-23 stsp (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
551 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
552 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
555 c75f7264 2018-09-11 stsp while (remain > 0) {
556 c75f7264 2018-09-11 stsp struct imsg imsg_log;
557 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
560 c75f7264 2018-09-11 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
562 c75f7264 2018-09-11 stsp return err;
564 c75f7264 2018-09-11 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
565 c75f7264 2018-09-11 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
567 c75f7264 2018-09-11 stsp memcpy((*commit)->logmsg + offset,
568 c75f7264 2018-09-11 stsp imsg_log.data, n);
569 c75f7264 2018-09-11 stsp imsg_free(&imsg_log);
570 c75f7264 2018-09-11 stsp offset += n;
571 c75f7264 2018-09-11 stsp remain -= n;
573 bff6ca00 2018-04-23 stsp (*commit)->logmsg[icommit.logmsg_len] = '\0';
576 bff6ca00 2018-04-23 stsp for (i = 0; i < icommit.nparents; i++) {
577 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
579 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
580 79f35eb3 2018-06-11 stsp if (qid == NULL) {
581 86acc566 2018-04-23 stsp err = got_error_from_errno();
584 79f35eb3 2018-06-11 stsp qid->id = calloc(1, sizeof(*qid->id));
585 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
586 86acc566 2018-04-23 stsp err = got_error_from_errno();
591 79f35eb3 2018-06-11 stsp memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
592 79f35eb3 2018-06-11 stsp sizeof(*qid->id));
593 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
594 86acc566 2018-04-23 stsp (*commit)->nparents++;
598 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
602 2178c42e 2018-04-22 stsp imsg_free(&imsg);
604 e033d803 2018-04-23 stsp return err;
607 e033d803 2018-04-23 stsp const struct got_error *
608 068fd2bf 2018-04-24 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
610 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
611 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
612 e033d803 2018-04-23 stsp struct got_tree_entry *te;
614 883f0469 2018-06-23 stsp itree.nentries = tree->entries.nentries;
615 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
617 e033d803 2018-04-23 stsp return got_error_from_errno();
619 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
621 e033d803 2018-04-23 stsp return err;
623 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
624 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
625 e033d803 2018-04-23 stsp uint8_t *buf = NULL;
626 e033d803 2018-04-23 stsp size_t len = sizeof(ite) + strlen(te->name);
628 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
629 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
631 e033d803 2018-04-23 stsp buf = malloc(len);
632 e033d803 2018-04-23 stsp if (buf == NULL)
633 e033d803 2018-04-23 stsp return got_error_from_errno();
635 e033d803 2018-04-23 stsp memcpy(ite.id, te->id->sha1, sizeof(ite.id));
636 e033d803 2018-04-23 stsp ite.mode = te->mode;
637 e033d803 2018-04-23 stsp memcpy(buf, &ite, sizeof(ite));
638 e033d803 2018-04-23 stsp memcpy(buf + sizeof(ite), te->name, strlen(te->name));
640 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
641 e033d803 2018-04-23 stsp buf, len) == -1)
642 e033d803 2018-04-23 stsp err = got_error_from_errno();
645 e033d803 2018-04-23 stsp return err;
647 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
649 e033d803 2018-04-23 stsp return err;
652 e033d803 2018-04-23 stsp return NULL;
655 e033d803 2018-04-23 stsp const struct got_error *
656 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
658 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
659 e033d803 2018-04-23 stsp const size_t min_datalen =
660 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
661 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
662 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree = { 0 };
663 e033d803 2018-04-23 stsp int nentries = 0;
665 e033d803 2018-04-23 stsp *tree = NULL;
667 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
671 e033d803 2018-04-23 stsp while (1) {
672 e033d803 2018-04-23 stsp struct imsg imsg;
674 e033d803 2018-04-23 stsp size_t datalen;
675 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
676 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
678 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
679 e033d803 2018-04-23 stsp if (n == 0) {
680 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries)
681 e033d803 2018-04-23 stsp goto get_more;
685 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
686 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
688 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
690 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
691 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
692 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
694 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
695 e033d803 2018-04-23 stsp /* This message should only appear once. */
696 e033d803 2018-04-23 stsp if (*tree != NULL) {
697 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
700 e033d803 2018-04-23 stsp if (datalen != sizeof(itree)) {
701 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
704 e033d803 2018-04-23 stsp memcpy(&itree, imsg.data, sizeof(itree));
705 e033d803 2018-04-23 stsp *tree = calloc(1, sizeof(**tree));
706 e033d803 2018-04-23 stsp if (*tree == NULL) {
707 e033d803 2018-04-23 stsp err = got_error_from_errno();
710 883f0469 2018-06-23 stsp (*tree)->entries.nentries = itree.nentries;
711 883f0469 2018-06-23 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
713 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
714 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
715 e033d803 2018-04-23 stsp if (*tree == NULL) {
716 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
719 e033d803 2018-04-23 stsp if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
720 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
724 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
725 e033d803 2018-04-23 stsp datalen -= sizeof(ite);
726 e033d803 2018-04-23 stsp memcpy(&ite, imsg.data, sizeof(ite));
727 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
728 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
732 e033d803 2018-04-23 stsp te = got_alloc_tree_entry_partial();
733 e033d803 2018-04-23 stsp if (te == NULL) {
734 e033d803 2018-04-23 stsp err = got_error_from_errno();
737 e033d803 2018-04-23 stsp te->name = malloc(datalen + 1);
738 e033d803 2018-04-23 stsp if (te->name == NULL) {
740 e033d803 2018-04-23 stsp err = got_error_from_errno();
743 052d4dc3 2018-04-23 stsp memcpy(te->name, imsg.data + sizeof(ite), datalen);
744 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
746 e033d803 2018-04-23 stsp memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
747 e033d803 2018-04-23 stsp te->mode = ite.mode;
748 883f0469 2018-06-23 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
749 e033d803 2018-04-23 stsp nentries++;
752 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
756 e033d803 2018-04-23 stsp imsg_free(&imsg);
759 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries) {
760 1e51f5b9 2018-04-23 stsp if (err == NULL)
761 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
762 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
763 e033d803 2018-04-23 stsp *tree = NULL;
766 ff6b18f8 2018-04-24 stsp return err;
769 ff6b18f8 2018-04-24 stsp const struct got_error *
770 2967a784 2018-04-24 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
772 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
774 2967a784 2018-04-24 stsp iblob.size = size;
775 ff6b18f8 2018-04-24 stsp /* Data has already been written to file descriptor. */
777 2967a784 2018-04-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
779 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
781 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
784 ff6b18f8 2018-04-24 stsp const struct got_error *
785 2967a784 2018-04-24 stsp got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
787 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
788 ff6b18f8 2018-04-24 stsp struct imsg imsg;
789 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
790 ff6b18f8 2018-04-24 stsp size_t datalen;
792 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
794 ff6b18f8 2018-04-24 stsp return err;
796 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
798 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
799 ff6b18f8 2018-04-24 stsp case GOT_IMSG_ERROR:
800 ff6b18f8 2018-04-24 stsp err = recv_imsg_error(&imsg, datalen);
802 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
803 2967a784 2018-04-24 stsp if (datalen != sizeof(iblob))
804 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
805 2967a784 2018-04-24 stsp memcpy(&iblob, imsg.data, sizeof(iblob));
806 2967a784 2018-04-24 stsp *size = iblob.size;
807 ff6b18f8 2018-04-24 stsp /* Data has been written to file descriptor. */
810 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
814 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
816 2178c42e 2018-04-22 stsp return err;
819 876c234b 2018-09-10 stsp const struct got_error *
820 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
821 876c234b 2018-09-10 stsp struct got_packidx *packidx)
823 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
824 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
827 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
828 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
829 876c234b 2018-09-10 stsp if (fd == -1)
830 876c234b 2018-09-10 stsp return got_error_from_errno();
832 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
833 876c234b 2018-09-10 stsp sizeof(ipackidx)) == -1)
834 876c234b 2018-09-10 stsp return got_error_from_errno();
836 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
837 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
838 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
839 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
841 876c234b 2018-09-10 stsp fd = dup(pack->fd);
842 876c234b 2018-09-10 stsp if (fd == -1)
843 876c234b 2018-09-10 stsp return got_error_from_errno();
845 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
847 876c234b 2018-09-10 stsp return got_error_from_errno();
849 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
852 876c234b 2018-09-10 stsp const struct got_error *
853 876c234b 2018-09-10 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx)
855 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
857 876c234b 2018-09-10 stsp iobj.idx = idx;
859 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
860 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
861 876c234b 2018-09-10 stsp return got_error_from_errno();
863 876c234b 2018-09-10 stsp return flush_imsg(ibuf);