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 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
68 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
70 const struct got_error *poll_err;
71 struct got_imsg_error ierr;
74 ierr.code = err->code;
75 if (err->code == GOT_ERR_ERRNO)
76 ierr.errno_code = errno;
79 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
81 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
82 getprogname(), err->code, err->msg, strerror(errno));
85 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
87 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
88 getprogname(), err->code, err->msg, poll_err->msg);
90 ret = imsg_flush(ibuf);
92 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
93 getprogname(), err->code, err->msg, strerror(errno));
96 const struct got_error *
97 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
99 const struct got_error *err = NULL;
100 struct got_imsg_object iobj;
102 iobj.type = obj->type;
103 iobj.flags = obj->flags;
104 iobj.hdrlen = obj->hdrlen;
105 iobj.size = obj->size;
106 memcpy(iobj.id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
107 iobj.ndeltas = ndeltas;
110 /* TODO: Handle deltas */
113 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
115 return got_error_from_errno();
117 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
121 if (imsg_flush(ibuf) == -1)
122 return got_error_from_errno();
127 const struct got_error *
128 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
130 const struct got_error *err = NULL;
131 struct got_imsg_error ierr;
133 struct got_imsg_object iobj;
140 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
146 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
147 return got_error(GOT_ERR_PRIVSEP_NO_FD);
148 return got_error(GOT_ERR_PRIVSEP_READ);
151 return got_error(GOT_ERR_PRIVSEP_PIPE);
153 m = imsg_get(ibuf, &imsg);
155 return got_error(GOT_ERR_PRIVSEP_READ);
157 if (imsg.hdr.len < IMSG_HEADER_SIZE + MIN(sizeof(ierr), sizeof(obj)))
158 return got_error(GOT_ERR_PRIVSEP_LEN);
160 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
162 switch (imsg.hdr.type) {
164 if (datalen != sizeof(ierr)) {
165 err = got_error(GOT_ERR_PRIVSEP_LEN);
168 memcpy(&ierr, imsg.data, sizeof(ierr));
169 if (ierr.code == GOT_ERR_ERRNO) {
170 static struct got_error err;
171 err.code = GOT_ERR_ERRNO;
172 err.msg = strerror(ierr.errno_code);
174 err = got_error(ierr.code);
176 case GOT_IMSG_OBJECT:
177 if (datalen != sizeof(iobj)) {
178 err = got_error(GOT_ERR_PRIVSEP_LEN);
182 memcpy(&iobj, imsg.data, sizeof(iobj));
183 if (iobj.ndeltas < 0 ||
184 iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
185 err = got_error(GOT_ERR_PRIVSEP_LEN);
189 *obj = calloc(1, sizeof(**obj));
191 err = got_error_from_errno();
195 (*obj)->type = iobj.type;
196 (*obj)->hdrlen = iobj.hdrlen;
197 (*obj)->size = iobj.size;
198 memcpy((*obj)->id.sha1, iobj.id.sha1, SHA1_DIGEST_LENGTH);
199 for (i = 0; i < iobj.ndeltas; i++) {
200 /* TODO: Handle deltas */