Blame


1 2178c42e 2018-04-22 stsp /*
2 2178c42e 2018-04-22 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 2178c42e 2018-04-22 stsp *
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.
7 2178c42e 2018-04-22 stsp *
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.
15 2178c42e 2018-04-22 stsp */
16 2178c42e 2018-04-22 stsp
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 2178c42e 2018-04-22 stsp
21 2178c42e 2018-04-22 stsp #include <stdio.h>
22 2178c42e 2018-04-22 stsp #include <stdlib.h>
23 2178c42e 2018-04-22 stsp #include <string.h>
24 2178c42e 2018-04-22 stsp #include <errno.h>
25 2178c42e 2018-04-22 stsp #include <stdint.h>
26 2178c42e 2018-04-22 stsp #include <poll.h>
27 2178c42e 2018-04-22 stsp #include <imsg.h>
28 2178c42e 2018-04-22 stsp #include <sha1.h>
29 2178c42e 2018-04-22 stsp #include <zlib.h>
30 2178c42e 2018-04-22 stsp
31 2178c42e 2018-04-22 stsp #include "got_object.h"
32 2178c42e 2018-04-22 stsp #include "got_error.h"
33 2178c42e 2018-04-22 stsp
34 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
35 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
36 2178c42e 2018-04-22 stsp #include "got_lib_zbuf.h"
37 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
38 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
39 2178c42e 2018-04-22 stsp
40 2178c42e 2018-04-22 stsp #ifndef MIN
41 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 2178c42e 2018-04-22 stsp #endif
43 2178c42e 2018-04-22 stsp
44 2178c42e 2018-04-22 stsp static const struct got_error *
45 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
46 2178c42e 2018-04-22 stsp {
47 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
48 2178c42e 2018-04-22 stsp int n;
49 2178c42e 2018-04-22 stsp
50 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
51 2178c42e 2018-04-22 stsp pfd[0].events = events;
52 2178c42e 2018-04-22 stsp
53 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
54 2178c42e 2018-04-22 stsp if (n == -1)
55 2178c42e 2018-04-22 stsp return got_error_from_errno();
56 2178c42e 2018-04-22 stsp if (n == 0)
57 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
58 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
59 2178c42e 2018-04-22 stsp return got_error_from_errno();
60 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
61 2178c42e 2018-04-22 stsp return NULL;
62 2178c42e 2018-04-22 stsp
63 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
64 2178c42e 2018-04-22 stsp }
65 2178c42e 2018-04-22 stsp
66 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
67 2178c42e 2018-04-22 stsp void
68 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
69 2178c42e 2018-04-22 stsp {
70 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
71 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
72 2178c42e 2018-04-22 stsp int ret;
73 2178c42e 2018-04-22 stsp
74 2178c42e 2018-04-22 stsp ierr.code = err->code;
75 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
76 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
77 2178c42e 2018-04-22 stsp else
78 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
79 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
80 2178c42e 2018-04-22 stsp if (ret != -1) {
81 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
82 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
83 2178c42e 2018-04-22 stsp }
84 2178c42e 2018-04-22 stsp
85 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
86 2178c42e 2018-04-22 stsp if (poll_err)
87 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
88 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
89 2178c42e 2018-04-22 stsp
90 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
91 2178c42e 2018-04-22 stsp if (ret == -1)
92 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
93 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
94 2178c42e 2018-04-22 stsp }
95 2178c42e 2018-04-22 stsp
96 2178c42e 2018-04-22 stsp const struct got_error *
97 2178c42e 2018-04-22 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
98 2178c42e 2018-04-22 stsp {
99 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
100 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
101 2178c42e 2018-04-22 stsp
102 2178c42e 2018-04-22 stsp iobj.type = obj->type;
103 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
104 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
105 2178c42e 2018-04-22 stsp iobj.size = obj->size;
106 2178c42e 2018-04-22 stsp memcpy(iobj.id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
107 2178c42e 2018-04-22 stsp iobj.ndeltas = ndeltas;
108 2178c42e 2018-04-22 stsp
109 2178c42e 2018-04-22 stsp if (ndeltas > 0) {
110 2178c42e 2018-04-22 stsp /* TODO: Handle deltas */
111 2178c42e 2018-04-22 stsp }
112 2178c42e 2018-04-22 stsp
113 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
114 2178c42e 2018-04-22 stsp == -1)
115 2178c42e 2018-04-22 stsp return got_error_from_errno();
116 2178c42e 2018-04-22 stsp
117 2178c42e 2018-04-22 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
118 2178c42e 2018-04-22 stsp if (err)
119 2178c42e 2018-04-22 stsp return err;
120 2178c42e 2018-04-22 stsp
121 2178c42e 2018-04-22 stsp if (imsg_flush(ibuf) == -1)
122 2178c42e 2018-04-22 stsp return got_error_from_errno();
123 2178c42e 2018-04-22 stsp
124 2178c42e 2018-04-22 stsp return NULL;
125 2178c42e 2018-04-22 stsp }
126 2178c42e 2018-04-22 stsp
127 2178c42e 2018-04-22 stsp const struct got_error *
128 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
129 2178c42e 2018-04-22 stsp {
130 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
131 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
132 2178c42e 2018-04-22 stsp struct imsg imsg;
133 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
134 2178c42e 2018-04-22 stsp ssize_t n, m;
135 2178c42e 2018-04-22 stsp size_t datalen;
136 2178c42e 2018-04-22 stsp int i;
137 2178c42e 2018-04-22 stsp
138 2178c42e 2018-04-22 stsp *obj = NULL;
139 2178c42e 2018-04-22 stsp
140 2178c42e 2018-04-22 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
141 2178c42e 2018-04-22 stsp if (err)
142 2178c42e 2018-04-22 stsp return err;
143 2178c42e 2018-04-22 stsp
144 2178c42e 2018-04-22 stsp n = imsg_read(ibuf);
145 2178c42e 2018-04-22 stsp if (n == -1) {
146 2178c42e 2018-04-22 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
147 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
148 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_PRIVSEP_READ);
149 2178c42e 2018-04-22 stsp }
150 2178c42e 2018-04-22 stsp if (n == 0)
151 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
152 2178c42e 2018-04-22 stsp
153 2178c42e 2018-04-22 stsp m = imsg_get(ibuf, &imsg);
154 2178c42e 2018-04-22 stsp if (m == 0)
155 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_PRIVSEP_READ);
156 2178c42e 2018-04-22 stsp
157 2178c42e 2018-04-22 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + MIN(sizeof(ierr), sizeof(obj)))
158 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
159 2178c42e 2018-04-22 stsp
160 2178c42e 2018-04-22 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
161 2178c42e 2018-04-22 stsp
162 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
163 2178c42e 2018-04-22 stsp case GOT_IMSG_ERROR:
164 2178c42e 2018-04-22 stsp if (datalen != sizeof(ierr)) {
165 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
166 2178c42e 2018-04-22 stsp break;
167 2178c42e 2018-04-22 stsp }
168 2178c42e 2018-04-22 stsp memcpy(&ierr, imsg.data, sizeof(ierr));
169 2178c42e 2018-04-22 stsp if (ierr.code == GOT_ERR_ERRNO) {
170 2178c42e 2018-04-22 stsp static struct got_error err;
171 2178c42e 2018-04-22 stsp err.code = GOT_ERR_ERRNO;
172 2178c42e 2018-04-22 stsp err.msg = strerror(ierr.errno_code);
173 2178c42e 2018-04-22 stsp } else
174 2178c42e 2018-04-22 stsp err = got_error(ierr.code);
175 2178c42e 2018-04-22 stsp break;
176 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
177 2178c42e 2018-04-22 stsp if (datalen != sizeof(iobj)) {
178 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
179 2178c42e 2018-04-22 stsp break;
180 2178c42e 2018-04-22 stsp }
181 2178c42e 2018-04-22 stsp
182 2178c42e 2018-04-22 stsp memcpy(&iobj, imsg.data, sizeof(iobj));
183 2178c42e 2018-04-22 stsp if (iobj.ndeltas < 0 ||
184 2178c42e 2018-04-22 stsp iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
185 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
186 2178c42e 2018-04-22 stsp break;
187 2178c42e 2018-04-22 stsp }
188 2178c42e 2018-04-22 stsp
189 2178c42e 2018-04-22 stsp *obj = calloc(1, sizeof(**obj));
190 2178c42e 2018-04-22 stsp if (*obj == NULL) {
191 2178c42e 2018-04-22 stsp err = got_error_from_errno();
192 2178c42e 2018-04-22 stsp break;
193 2178c42e 2018-04-22 stsp }
194 2178c42e 2018-04-22 stsp
195 2178c42e 2018-04-22 stsp (*obj)->type = iobj.type;
196 2178c42e 2018-04-22 stsp (*obj)->hdrlen = iobj.hdrlen;
197 2178c42e 2018-04-22 stsp (*obj)->size = iobj.size;
198 2178c42e 2018-04-22 stsp memcpy((*obj)->id.sha1, iobj.id.sha1, SHA1_DIGEST_LENGTH);
199 2178c42e 2018-04-22 stsp for (i = 0; i < iobj.ndeltas; i++) {
200 2178c42e 2018-04-22 stsp /* TODO: Handle deltas */
201 2178c42e 2018-04-22 stsp }
202 2178c42e 2018-04-22 stsp break;
203 2178c42e 2018-04-22 stsp }
204 2178c42e 2018-04-22 stsp
205 2178c42e 2018-04-22 stsp imsg_free(&imsg);
206 2178c42e 2018-04-22 stsp
207 2178c42e 2018-04-22 stsp return err;
208 2178c42e 2018-04-22 stsp }