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 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
67 void
68 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
69 {
70 const struct got_error *poll_err;
71 struct got_imsg_error ierr;
72 int ret;
74 ierr.code = err->code;
75 if (err->code == GOT_ERR_ERRNO)
76 ierr.errno_code = errno;
77 else
78 ierr.errno_code = 0;
79 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
80 if (ret != -1) {
81 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
82 getprogname(), err->code, err->msg, strerror(errno));
83 }
85 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
86 if (poll_err)
87 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
88 getprogname(), err->code, err->msg, poll_err->msg);
90 ret = imsg_flush(ibuf);
91 if (ret == -1)
92 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
93 getprogname(), err->code, err->msg, strerror(errno));
94 }
96 const struct got_error *
97 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
98 {
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;
109 if (ndeltas > 0) {
110 /* TODO: Handle deltas */
113 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
114 == -1)
115 return got_error_from_errno();
117 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
118 if (err)
119 return err;
121 if (imsg_flush(ibuf) == -1)
122 return got_error_from_errno();
124 return NULL;
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;
132 struct imsg imsg;
133 struct got_imsg_object iobj;
134 ssize_t n, m;
135 size_t datalen;
136 int i;
138 *obj = NULL;
140 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
141 if (err)
142 return err;
144 n = imsg_read(ibuf);
145 if (n == -1) {
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);
150 if (n == 0)
151 return got_error(GOT_ERR_PRIVSEP_PIPE);
153 m = imsg_get(ibuf, &imsg);
154 if (m == 0)
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) {
163 case GOT_IMSG_ERROR:
164 if (datalen != sizeof(ierr)) {
165 err = got_error(GOT_ERR_PRIVSEP_LEN);
166 break;
168 memcpy(&ierr, imsg.data, sizeof(ierr));
169 if (ierr.code == GOT_ERR_ERRNO) {
170 static struct got_error serr;
171 serr.code = GOT_ERR_ERRNO;
172 serr.msg = strerror(ierr.errno_code);
173 err = &serr;
174 } else
175 err = got_error(ierr.code);
176 break;
177 case GOT_IMSG_OBJECT:
178 if (datalen != sizeof(iobj)) {
179 err = got_error(GOT_ERR_PRIVSEP_LEN);
180 break;
183 memcpy(&iobj, imsg.data, sizeof(iobj));
184 if (iobj.ndeltas < 0 ||
185 iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
186 err = got_error(GOT_ERR_PRIVSEP_LEN);
187 break;
190 *obj = calloc(1, sizeof(**obj));
191 if (*obj == NULL) {
192 err = got_error_from_errno();
193 break;
196 (*obj)->type = iobj.type;
197 (*obj)->hdrlen = iobj.hdrlen;
198 (*obj)->size = iobj.size;
199 memcpy((*obj)->id.sha1, iobj.id.sha1, SHA1_DIGEST_LENGTH);
200 for (i = 0; i < iobj.ndeltas; i++) {
201 /* TODO: Handle deltas */
203 break;
206 imsg_free(&imsg);
208 return err;