Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <stdint.h>
26 #include <poll.h>
27 #include <imsg.h>
28 #include <sha1.h>
29 #include <zlib.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"
40 #ifndef MIN
41 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 #endif
44 static const struct got_error *
45 poll_fd(int fd, int events, int timeout)
46 {
47 struct pollfd pfd[1];
48 int n;
50 pfd[0].fd = fd;
51 pfd[0].events = events;
53 n = poll(pfd, 1, timeout);
54 if (n == -1)
55 return got_error_from_errno();
56 if (n == 0)
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))
61 return NULL;
63 return got_error(GOT_ERR_INTERRUPT);
64 }
66 static const struct got_error *
67 recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
68 {
69 const struct got_error *err;
70 ssize_t n, m;
72 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
73 if (err)
74 return err;
76 n = imsg_read(ibuf);
77 if (n == -1) {
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);
81 }
82 if (n == 0)
83 return got_error(GOT_ERR_PRIVSEP_PIPE);
85 m = imsg_get(ibuf, imsg);
86 if (m == 0)
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);
92 return NULL;
93 }
95 static const struct got_error *
96 recv_imsg_error(struct imsg *imsg, size_t datalen)
97 {
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);
108 return &serr;
111 return got_error(ierr.code);
114 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
115 void
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;
120 int ret;
122 ierr.code = err->code;
123 if (err->code == GOT_ERR_ERRNO)
124 ierr.errno_code = errno;
125 else
126 ierr.errno_code = 0;
127 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
128 if (ret != -1) {
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);
134 if (poll_err)
135 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
136 getprogname(), err->code, err->msg, poll_err->msg);
138 ret = imsg_flush(ibuf);
139 if (ret == -1)
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;
156 if (ndeltas > 0) {
157 /* TODO: Handle deltas */
160 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
161 == -1)
162 return got_error_from_errno();
164 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
165 if (err)
166 return err;
168 if (imsg_flush(ibuf) == -1)
169 return got_error_from_errno();
171 return NULL;
174 const struct got_error *
175 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
177 const struct got_error *err = NULL;
178 struct imsg imsg;
179 struct got_imsg_object iobj;
180 size_t datalen;
181 int i;
182 const size_t min_datalen =
183 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
185 *obj = NULL;
187 err = recv_one_imsg(&imsg, ibuf, min_datalen);
188 if (err)
189 return err;
191 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
193 switch (imsg.hdr.type) {
194 case GOT_IMSG_ERROR:
195 err = recv_imsg_error(&imsg, datalen);
196 break;
197 case GOT_IMSG_OBJECT:
198 if (datalen != sizeof(iobj)) {
199 err = got_error(GOT_ERR_PRIVSEP_LEN);
200 break;
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);
207 break;
210 *obj = calloc(1, sizeof(**obj));
211 if (*obj == NULL) {
212 err = got_error_from_errno();
213 break;
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 */
222 break;
223 default:
224 err = got_error(GOT_ERR_PRIVSEP_MSG);
225 break;
228 imsg_free(&imsg);
230 return err;