2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
31 #include "got_object.h"
32 #include "got_error.h"
34 #include "got_lib_sha1.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_zbuf.h"
37 #include "got_lib_object.h"
38 #include "got_lib_privsep.h"
41 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
44 static const struct got_error *
45 poll_fd(int fd, int events, int timeout)
51 pfd[0].events = events;
53 n = poll(pfd, 1, timeout);
55 return got_error_from_errno();
57 return got_error(GOT_ERR_TIMEOUT);
58 if (pfd[0].revents & (POLLERR | POLLNVAL))
59 return got_error_from_errno();
60 if (pfd[0].revents & (events | POLLHUP))
63 return got_error(GOT_ERR_INTERRUPT);
66 static const struct got_error *
67 recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
69 const struct got_error *err;
72 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
78 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
79 return got_error(GOT_ERR_PRIVSEP_NO_FD);
80 return got_error(GOT_ERR_PRIVSEP_READ);
83 return got_error(GOT_ERR_PRIVSEP_PIPE);
85 m = imsg_get(ibuf, imsg);
87 return got_error(GOT_ERR_PRIVSEP_READ);
89 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
90 return got_error(GOT_ERR_PRIVSEP_LEN);
95 static const struct got_error *
96 recv_imsg_error(struct imsg *imsg, size_t datalen)
98 struct got_imsg_error ierr;
100 if (datalen != sizeof(ierr))
101 return got_error(GOT_ERR_PRIVSEP_LEN);
103 memcpy(&ierr, imsg->data, sizeof(ierr));
104 if (ierr.code == GOT_ERR_ERRNO) {
105 static struct got_error serr;
106 serr.code = GOT_ERR_ERRNO;
107 serr.msg = strerror(ierr.errno_code);
111 return got_error(ierr.code);
114 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
116 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
118 const struct got_error *poll_err;
119 struct got_imsg_error ierr;
122 ierr.code = err->code;
123 if (err->code == GOT_ERR_ERRNO)
124 ierr.errno_code = errno;
127 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
129 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
130 getprogname(), err->code, err->msg, strerror(errno));
133 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
135 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
136 getprogname(), err->code, err->msg, poll_err->msg);
138 ret = imsg_flush(ibuf);
140 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
141 getprogname(), err->code, err->msg, strerror(errno));
144 const struct got_error *
145 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
147 const struct got_error *err = NULL;
148 struct got_imsg_object iobj;
150 iobj.type = obj->type;
151 iobj.flags = obj->flags;
152 iobj.hdrlen = obj->hdrlen;
153 iobj.size = obj->size;
154 iobj.ndeltas = ndeltas;
157 /* TODO: Handle deltas */
160 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
162 return got_error_from_errno();
164 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
168 if (imsg_flush(ibuf) == -1)
169 return got_error_from_errno();
174 const struct got_error *
175 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
177 const struct got_error *err = NULL;
179 struct got_imsg_object iobj;
182 const size_t min_datalen =
183 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
187 err = recv_one_imsg(&imsg, ibuf, min_datalen);
191 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
193 switch (imsg.hdr.type) {
195 err = recv_imsg_error(&imsg, datalen);
197 case GOT_IMSG_OBJECT:
198 if (datalen != sizeof(iobj)) {
199 err = got_error(GOT_ERR_PRIVSEP_LEN);
203 memcpy(&iobj, imsg.data, sizeof(iobj));
204 if (iobj.ndeltas < 0 ||
205 iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
206 err = got_error(GOT_ERR_PRIVSEP_LEN);
210 *obj = calloc(1, sizeof(**obj));
212 err = got_error_from_errno();
216 (*obj)->type = iobj.type;
217 (*obj)->hdrlen = iobj.hdrlen;
218 (*obj)->size = iobj.size;
219 for (i = 0; i < iobj.ndeltas; i++) {
220 /* TODO: Handle deltas */
224 err = got_error(GOT_ERR_PRIVSEP_MSG);