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 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
21 876c234b 2018-09-10 stsp #include <sys/wait.h>
22 2178c42e 2018-04-22 stsp
23 2178c42e 2018-04-22 stsp #include <stdio.h>
24 2178c42e 2018-04-22 stsp #include <stdlib.h>
25 2178c42e 2018-04-22 stsp #include <string.h>
26 2178c42e 2018-04-22 stsp #include <errno.h>
27 2178c42e 2018-04-22 stsp #include <stdint.h>
28 2178c42e 2018-04-22 stsp #include <poll.h>
29 2178c42e 2018-04-22 stsp #include <imsg.h>
30 2178c42e 2018-04-22 stsp #include <sha1.h>
31 2178c42e 2018-04-22 stsp #include <zlib.h>
32 788c352e 2018-06-16 stsp #include <time.h>
33 2178c42e 2018-04-22 stsp
34 2178c42e 2018-04-22 stsp #include "got_object.h"
35 2178c42e 2018-04-22 stsp #include "got_error.h"
36 2178c42e 2018-04-22 stsp
37 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
38 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
39 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
40 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
41 a440fac0 2018-09-06 stsp #include "got_lib_object_parse.h"
42 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
43 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
44 2178c42e 2018-04-22 stsp
45 2178c42e 2018-04-22 stsp #ifndef MIN
46 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
47 2178c42e 2018-04-22 stsp #endif
48 2178c42e 2018-04-22 stsp
49 2178c42e 2018-04-22 stsp static const struct got_error *
50 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
51 2178c42e 2018-04-22 stsp {
52 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
53 2178c42e 2018-04-22 stsp int n;
54 2178c42e 2018-04-22 stsp
55 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
56 2178c42e 2018-04-22 stsp pfd[0].events = events;
57 2178c42e 2018-04-22 stsp
58 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
59 2178c42e 2018-04-22 stsp if (n == -1)
60 2178c42e 2018-04-22 stsp return got_error_from_errno();
61 2178c42e 2018-04-22 stsp if (n == 0)
62 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
63 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
64 2178c42e 2018-04-22 stsp return got_error_from_errno();
65 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
66 2178c42e 2018-04-22 stsp return NULL;
67 2178c42e 2018-04-22 stsp
68 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
69 2178c42e 2018-04-22 stsp }
70 2178c42e 2018-04-22 stsp
71 c4eae628 2018-04-23 stsp static const struct got_error *
72 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
73 fe36cf76 2018-04-23 stsp {
74 fe36cf76 2018-04-23 stsp const struct got_error *err;
75 e033d803 2018-04-23 stsp size_t n;
76 fe36cf76 2018-04-23 stsp
77 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
78 fe36cf76 2018-04-23 stsp if (err)
79 fe36cf76 2018-04-23 stsp return err;
80 fe36cf76 2018-04-23 stsp
81 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
82 fe36cf76 2018-04-23 stsp if (n == -1) {
83 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
84 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
85 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
86 fe36cf76 2018-04-23 stsp }
87 fe36cf76 2018-04-23 stsp if (n == 0)
88 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
89 fe36cf76 2018-04-23 stsp
90 e033d803 2018-04-23 stsp return NULL;
91 e033d803 2018-04-23 stsp }
92 e033d803 2018-04-23 stsp
93 ad242220 2018-09-08 stsp const struct got_error *
94 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
95 876c234b 2018-09-10 stsp {
96 876c234b 2018-09-10 stsp int child_status;
97 876c234b 2018-09-10 stsp
98 876c234b 2018-09-10 stsp waitpid(pid, &child_status, 0);
99 876c234b 2018-09-10 stsp
100 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
101 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
102 876c234b 2018-09-10 stsp
103 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
104 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
105 876c234b 2018-09-10 stsp
106 876c234b 2018-09-10 stsp return NULL;
107 876c234b 2018-09-10 stsp }
108 876c234b 2018-09-10 stsp
109 876c234b 2018-09-10 stsp const struct got_error *
110 ad242220 2018-09-08 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
111 e033d803 2018-04-23 stsp {
112 e033d803 2018-04-23 stsp const struct got_error *err;
113 e033d803 2018-04-23 stsp ssize_t n;
114 e033d803 2018-04-23 stsp
115 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
116 876c234b 2018-09-10 stsp if (n == -1)
117 876c234b 2018-09-10 stsp return got_error_from_errno();
118 876c234b 2018-09-10 stsp
119 876c234b 2018-09-10 stsp while (n == 0) {
120 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
121 876c234b 2018-09-10 stsp if (err)
122 876c234b 2018-09-10 stsp return err;
123 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
124 876c234b 2018-09-10 stsp }
125 fe36cf76 2018-04-23 stsp
126 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
127 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
128 fe36cf76 2018-04-23 stsp
129 fe36cf76 2018-04-23 stsp return NULL;
130 fe36cf76 2018-04-23 stsp }
131 fe36cf76 2018-04-23 stsp
132 fe36cf76 2018-04-23 stsp static const struct got_error *
133 c4eae628 2018-04-23 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
134 c4eae628 2018-04-23 stsp {
135 c4eae628 2018-04-23 stsp struct got_imsg_error ierr;
136 c4eae628 2018-04-23 stsp
137 c4eae628 2018-04-23 stsp if (datalen != sizeof(ierr))
138 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
139 c4eae628 2018-04-23 stsp
140 c4eae628 2018-04-23 stsp memcpy(&ierr, imsg->data, sizeof(ierr));
141 c4eae628 2018-04-23 stsp if (ierr.code == GOT_ERR_ERRNO) {
142 c4eae628 2018-04-23 stsp static struct got_error serr;
143 c4eae628 2018-04-23 stsp serr.code = GOT_ERR_ERRNO;
144 c4eae628 2018-04-23 stsp serr.msg = strerror(ierr.errno_code);
145 c4eae628 2018-04-23 stsp return &serr;
146 c4eae628 2018-04-23 stsp }
147 c4eae628 2018-04-23 stsp
148 c4eae628 2018-04-23 stsp return got_error(ierr.code);
149 c4eae628 2018-04-23 stsp }
150 c4eae628 2018-04-23 stsp
151 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
152 2178c42e 2018-04-22 stsp void
153 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
154 2178c42e 2018-04-22 stsp {
155 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
156 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
157 2178c42e 2018-04-22 stsp int ret;
158 2178c42e 2018-04-22 stsp
159 2178c42e 2018-04-22 stsp ierr.code = err->code;
160 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
161 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
162 2178c42e 2018-04-22 stsp else
163 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
164 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
165 2178c42e 2018-04-22 stsp if (ret != -1) {
166 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
167 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
168 5d43e84d 2018-04-23 stsp return;
169 2178c42e 2018-04-22 stsp }
170 2178c42e 2018-04-22 stsp
171 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
172 5d43e84d 2018-04-23 stsp if (poll_err) {
173 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
174 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
175 5d43e84d 2018-04-23 stsp return;
176 5d43e84d 2018-04-23 stsp }
177 2178c42e 2018-04-22 stsp
178 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
179 5d43e84d 2018-04-23 stsp if (ret == -1) {
180 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
181 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
182 5d43e84d 2018-04-23 stsp return;
183 5d43e84d 2018-04-23 stsp }
184 e033d803 2018-04-23 stsp }
185 e033d803 2018-04-23 stsp
186 e033d803 2018-04-23 stsp static const struct got_error *
187 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
188 e033d803 2018-04-23 stsp {
189 e033d803 2018-04-23 stsp const struct got_error *err;
190 e033d803 2018-04-23 stsp
191 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
192 e033d803 2018-04-23 stsp if (err)
193 e033d803 2018-04-23 stsp return err;
194 e033d803 2018-04-23 stsp
195 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
196 e033d803 2018-04-23 stsp return got_error_from_errno();
197 e033d803 2018-04-23 stsp
198 e033d803 2018-04-23 stsp return NULL;
199 ad242220 2018-09-08 stsp }
200 ad242220 2018-09-08 stsp
201 ad242220 2018-09-08 stsp const struct got_error *
202 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
203 ad242220 2018-09-08 stsp {
204 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
205 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
206 ad242220 2018-09-08 stsp
207 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
208 ad242220 2018-09-08 stsp
209 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
210 ad242220 2018-09-08 stsp return got_error_from_errno();
211 ad242220 2018-09-08 stsp
212 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
213 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
214 ad242220 2018-09-08 stsp return err;
215 ad242220 2018-09-08 stsp }
216 ad242220 2018-09-08 stsp
217 ad242220 2018-09-08 stsp const struct got_error *
218 ad242220 2018-09-08 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
219 ad242220 2018-09-08 stsp {
220 ad242220 2018-09-08 stsp struct got_imsg_object iobj, *iobjp = NULL;
221 ad242220 2018-09-08 stsp size_t iobj_size = 0;
222 ad242220 2018-09-08 stsp int imsg_code = GOT_IMSG_OBJECT_REQUEST;
223 ad242220 2018-09-08 stsp
224 ad242220 2018-09-08 stsp if (obj) {
225 ad242220 2018-09-08 stsp switch (obj->type) {
226 ad242220 2018-09-08 stsp case GOT_OBJ_TYPE_TREE:
227 ad242220 2018-09-08 stsp imsg_code = GOT_IMSG_TREE_REQUEST;
228 ad242220 2018-09-08 stsp break;
229 ad242220 2018-09-08 stsp case GOT_OBJ_TYPE_COMMIT:
230 ad242220 2018-09-08 stsp imsg_code = GOT_IMSG_COMMIT_REQUEST;
231 ad242220 2018-09-08 stsp break;
232 ad242220 2018-09-08 stsp default:
233 ad242220 2018-09-08 stsp return got_error(GOT_ERR_OBJ_TYPE);
234 ad242220 2018-09-08 stsp }
235 ad242220 2018-09-08 stsp
236 ad242220 2018-09-08 stsp iobj.type = obj->type;
237 ad242220 2018-09-08 stsp iobj.flags = obj->flags;
238 ad242220 2018-09-08 stsp iobj.hdrlen = obj->hdrlen;
239 ad242220 2018-09-08 stsp iobj.size = obj->size;
240 ad242220 2018-09-08 stsp iobj.ndeltas = 0;
241 876c234b 2018-09-10 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED)
242 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
243 ad242220 2018-09-08 stsp
244 ad242220 2018-09-08 stsp iobjp = &iobj;
245 ad242220 2018-09-08 stsp iobj_size = sizeof(iobj);
246 ad242220 2018-09-08 stsp }
247 ad242220 2018-09-08 stsp
248 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
249 ad242220 2018-09-08 stsp return got_error_from_errno();
250 ad242220 2018-09-08 stsp
251 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
252 2178c42e 2018-04-22 stsp }
253 2178c42e 2018-04-22 stsp
254 2178c42e 2018-04-22 stsp const struct got_error *
255 ad242220 2018-09-08 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int outfd, int infd)
256 ad242220 2018-09-08 stsp {
257 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
258 ad242220 2018-09-08 stsp
259 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
260 ad242220 2018-09-08 stsp == -1) {
261 ad242220 2018-09-08 stsp close(infd);
262 ad242220 2018-09-08 stsp close(outfd);
263 ad242220 2018-09-08 stsp return got_error_from_errno();
264 ad242220 2018-09-08 stsp }
265 ad242220 2018-09-08 stsp
266 ad242220 2018-09-08 stsp err = flush_imsg(ibuf);
267 ad242220 2018-09-08 stsp if (err) {
268 ad242220 2018-09-08 stsp close(outfd);
269 ad242220 2018-09-08 stsp return err;
270 ad242220 2018-09-08 stsp }
271 ad242220 2018-09-08 stsp
272 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
273 ad242220 2018-09-08 stsp == -1) {
274 ad242220 2018-09-08 stsp close(outfd);
275 ad242220 2018-09-08 stsp return got_error_from_errno();
276 ad242220 2018-09-08 stsp }
277 ad242220 2018-09-08 stsp
278 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
279 ad242220 2018-09-08 stsp }
280 ad242220 2018-09-08 stsp
281 876c234b 2018-09-10 stsp static const struct got_error *
282 876c234b 2018-09-10 stsp send_delta(struct got_delta *delta, struct imsgbuf *ibuf)
283 876c234b 2018-09-10 stsp {
284 876c234b 2018-09-10 stsp struct got_imsg_delta idelta;
285 876c234b 2018-09-10 stsp size_t offset, remain;
286 876c234b 2018-09-10 stsp
287 876c234b 2018-09-10 stsp idelta.offset = delta->offset;
288 876c234b 2018-09-10 stsp idelta.tslen = delta->tslen;
289 876c234b 2018-09-10 stsp idelta.type = delta->type;
290 876c234b 2018-09-10 stsp idelta.size = delta->size;
291 876c234b 2018-09-10 stsp idelta.data_offset = delta->data_offset;
292 876c234b 2018-09-10 stsp idelta.delta_len = delta->delta_len;
293 876c234b 2018-09-10 stsp
294 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_DELTA, 0, 0, -1,
295 876c234b 2018-09-10 stsp &idelta, sizeof(idelta)) == -1)
296 876c234b 2018-09-10 stsp return got_error_from_errno();
297 876c234b 2018-09-10 stsp
298 876c234b 2018-09-10 stsp if (imsg_flush(ibuf) == -1)
299 876c234b 2018-09-10 stsp return got_error_from_errno();
300 876c234b 2018-09-10 stsp
301 876c234b 2018-09-10 stsp offset = 0;
302 876c234b 2018-09-10 stsp remain = delta->delta_len;
303 876c234b 2018-09-10 stsp while (remain > 0) {
304 876c234b 2018-09-10 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
305 876c234b 2018-09-10 stsp
306 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_DELTA_STREAM, 0, 0, -1,
307 876c234b 2018-09-10 stsp delta->delta_buf + offset, n) == -1)
308 876c234b 2018-09-10 stsp return got_error_from_errno();
309 876c234b 2018-09-10 stsp
310 876c234b 2018-09-10 stsp if (imsg_flush(ibuf) == -1)
311 876c234b 2018-09-10 stsp return got_error_from_errno();
312 876c234b 2018-09-10 stsp
313 876c234b 2018-09-10 stsp offset += n;
314 876c234b 2018-09-10 stsp remain -= n;
315 876c234b 2018-09-10 stsp }
316 876c234b 2018-09-10 stsp
317 876c234b 2018-09-10 stsp return NULL;
318 876c234b 2018-09-10 stsp }
319 876c234b 2018-09-10 stsp
320 ad242220 2018-09-08 stsp const struct got_error *
321 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
322 2178c42e 2018-04-22 stsp {
323 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
324 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
325 876c234b 2018-09-10 stsp struct got_delta *delta;
326 2178c42e 2018-04-22 stsp
327 2178c42e 2018-04-22 stsp iobj.type = obj->type;
328 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
329 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
330 2178c42e 2018-04-22 stsp iobj.size = obj->size;
331 876c234b 2018-09-10 stsp iobj.ndeltas = obj->deltas.nentries;
332 876c234b 2018-09-10 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED)
333 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
334 2178c42e 2018-04-22 stsp
335 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
336 2178c42e 2018-04-22 stsp == -1)
337 2178c42e 2018-04-22 stsp return got_error_from_errno();
338 2178c42e 2018-04-22 stsp
339 876c234b 2018-09-10 stsp err = flush_imsg(ibuf);
340 876c234b 2018-09-10 stsp if (err)
341 876c234b 2018-09-10 stsp return err;
342 876c234b 2018-09-10 stsp
343 876c234b 2018-09-10 stsp SIMPLEQ_FOREACH(delta, &obj->deltas.entries, entry) {
344 876c234b 2018-09-10 stsp err = send_delta(delta, ibuf);
345 876c234b 2018-09-10 stsp if (err)
346 876c234b 2018-09-10 stsp break;
347 876c234b 2018-09-10 stsp }
348 876c234b 2018-09-10 stsp
349 876c234b 2018-09-10 stsp return err;
350 2178c42e 2018-04-22 stsp }
351 2178c42e 2018-04-22 stsp
352 876c234b 2018-09-10 stsp static const struct got_error *
353 876c234b 2018-09-10 stsp receive_delta(struct got_delta **delta, struct imsgbuf *ibuf)
354 876c234b 2018-09-10 stsp {
355 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
356 876c234b 2018-09-10 stsp struct imsg imsg;
357 876c234b 2018-09-10 stsp struct got_imsg_delta idelta;
358 876c234b 2018-09-10 stsp uint8_t *delta_buf = NULL;
359 876c234b 2018-09-10 stsp const size_t min_datalen =
360 876c234b 2018-09-10 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_delta));
361 876c234b 2018-09-10 stsp size_t datalen, offset, remain;
362 876c234b 2018-09-10 stsp
363 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
364 876c234b 2018-09-10 stsp if (err)
365 876c234b 2018-09-10 stsp return err;
366 876c234b 2018-09-10 stsp
367 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
368 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_ERROR)
369 876c234b 2018-09-10 stsp return recv_imsg_error(&imsg, datalen);
370 876c234b 2018-09-10 stsp
371 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_DELTA)
372 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
373 876c234b 2018-09-10 stsp if (datalen != sizeof(idelta))
374 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
375 876c234b 2018-09-10 stsp
376 876c234b 2018-09-10 stsp memcpy(&idelta, imsg.data, sizeof(idelta));
377 876c234b 2018-09-10 stsp imsg_free(&imsg);
378 876c234b 2018-09-10 stsp
379 876c234b 2018-09-10 stsp switch (idelta.type) {
380 876c234b 2018-09-10 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
381 876c234b 2018-09-10 stsp case GOT_OBJ_TYPE_REF_DELTA:
382 876c234b 2018-09-10 stsp if (idelta.delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
383 876c234b 2018-09-10 stsp return got_error(GOT_ERR_BAD_DELTA);
384 876c234b 2018-09-10 stsp break;
385 876c234b 2018-09-10 stsp default:
386 876c234b 2018-09-10 stsp if (idelta.delta_len != 0)
387 876c234b 2018-09-10 stsp return got_error(GOT_ERR_BAD_DELTA);
388 876c234b 2018-09-10 stsp break;
389 876c234b 2018-09-10 stsp }
390 876c234b 2018-09-10 stsp
391 876c234b 2018-09-10 stsp if (idelta.delta_len > 0) {
392 876c234b 2018-09-10 stsp delta_buf = calloc(1, idelta.delta_len);
393 876c234b 2018-09-10 stsp if (delta_buf == NULL)
394 876c234b 2018-09-10 stsp return got_error_from_errno();
395 876c234b 2018-09-10 stsp
396 876c234b 2018-09-10 stsp offset = 0;
397 876c234b 2018-09-10 stsp remain = idelta.delta_len;
398 876c234b 2018-09-10 stsp while (remain > 0) {
399 876c234b 2018-09-10 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
400 876c234b 2018-09-10 stsp
401 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, n);
402 876c234b 2018-09-10 stsp if (err)
403 876c234b 2018-09-10 stsp return err;
404 876c234b 2018-09-10 stsp
405 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_ERROR)
406 876c234b 2018-09-10 stsp return recv_imsg_error(&imsg, datalen);
407 876c234b 2018-09-10 stsp
408 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
409 876c234b 2018-09-10 stsp break;
410 876c234b 2018-09-10 stsp
411 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_DELTA_STREAM)
412 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
413 876c234b 2018-09-10 stsp
414 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
415 876c234b 2018-09-10 stsp if (datalen != n)
416 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
417 876c234b 2018-09-10 stsp
418 876c234b 2018-09-10 stsp memcpy(delta_buf + offset, imsg.data, n);
419 876c234b 2018-09-10 stsp imsg_free(&imsg);
420 876c234b 2018-09-10 stsp
421 876c234b 2018-09-10 stsp offset += n;
422 876c234b 2018-09-10 stsp remain -= n;
423 876c234b 2018-09-10 stsp }
424 876c234b 2018-09-10 stsp }
425 876c234b 2018-09-10 stsp
426 876c234b 2018-09-10 stsp *delta = got_delta_open(idelta.offset, idelta.tslen, idelta.type,
427 876c234b 2018-09-10 stsp idelta.size, idelta.data_offset, delta_buf, idelta.delta_len);
428 876c234b 2018-09-10 stsp if (*delta == NULL) {
429 876c234b 2018-09-10 stsp err = got_error_from_errno();
430 876c234b 2018-09-10 stsp free(delta_buf);
431 876c234b 2018-09-10 stsp }
432 876c234b 2018-09-10 stsp
433 876c234b 2018-09-10 stsp return err;
434 876c234b 2018-09-10 stsp }
435 876c234b 2018-09-10 stsp
436 2178c42e 2018-04-22 stsp const struct got_error *
437 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
438 2178c42e 2018-04-22 stsp {
439 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
440 2178c42e 2018-04-22 stsp struct imsg imsg;
441 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
442 2178c42e 2018-04-22 stsp size_t datalen;
443 2178c42e 2018-04-22 stsp int i;
444 c4eae628 2018-04-23 stsp const size_t min_datalen =
445 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
446 2178c42e 2018-04-22 stsp
447 2178c42e 2018-04-22 stsp *obj = NULL;
448 2178c42e 2018-04-22 stsp
449 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
450 2178c42e 2018-04-22 stsp if (err)
451 2178c42e 2018-04-22 stsp return err;
452 2178c42e 2018-04-22 stsp
453 2178c42e 2018-04-22 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
454 2178c42e 2018-04-22 stsp
455 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
456 2178c42e 2018-04-22 stsp case GOT_IMSG_ERROR:
457 c4eae628 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
458 2178c42e 2018-04-22 stsp break;
459 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
460 2178c42e 2018-04-22 stsp if (datalen != sizeof(iobj)) {
461 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
462 2178c42e 2018-04-22 stsp break;
463 2178c42e 2018-04-22 stsp }
464 2178c42e 2018-04-22 stsp
465 2178c42e 2018-04-22 stsp memcpy(&iobj, imsg.data, sizeof(iobj));
466 2178c42e 2018-04-22 stsp if (iobj.ndeltas < 0 ||
467 2178c42e 2018-04-22 stsp iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
468 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
469 2178c42e 2018-04-22 stsp break;
470 2178c42e 2018-04-22 stsp }
471 876c234b 2018-09-10 stsp if (iobj.ndeltas > 0 &&
472 876c234b 2018-09-10 stsp (iobj.flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
473 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
474 876c234b 2018-09-10 stsp break;
475 876c234b 2018-09-10 stsp }
476 2178c42e 2018-04-22 stsp
477 2178c42e 2018-04-22 stsp *obj = calloc(1, sizeof(**obj));
478 2178c42e 2018-04-22 stsp if (*obj == NULL) {
479 2178c42e 2018-04-22 stsp err = got_error_from_errno();
480 2178c42e 2018-04-22 stsp break;
481 2178c42e 2018-04-22 stsp }
482 2178c42e 2018-04-22 stsp
483 2178c42e 2018-04-22 stsp (*obj)->type = iobj.type;
484 876c234b 2018-09-10 stsp (*obj)->flags = iobj.flags;
485 2178c42e 2018-04-22 stsp (*obj)->hdrlen = iobj.hdrlen;
486 2178c42e 2018-04-22 stsp (*obj)->size = iobj.size;
487 876c234b 2018-09-10 stsp /* id and path_packfile might be copied in by caller */
488 876c234b 2018-09-10 stsp (*obj)->pack_offset = iobj.pack_offset;
489 876c234b 2018-09-10 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
490 2178c42e 2018-04-22 stsp for (i = 0; i < iobj.ndeltas; i++) {
491 876c234b 2018-09-10 stsp struct got_delta *delta;
492 876c234b 2018-09-10 stsp err = receive_delta(&delta, ibuf);
493 876c234b 2018-09-10 stsp if (err)
494 876c234b 2018-09-10 stsp break;
495 876c234b 2018-09-10 stsp (*obj)->deltas.nentries++;
496 876c234b 2018-09-10 stsp SIMPLEQ_INSERT_TAIL(&(*obj)->deltas.entries, delta,
497 876c234b 2018-09-10 stsp entry);
498 bff6ca00 2018-04-23 stsp }
499 bff6ca00 2018-04-23 stsp break;
500 bff6ca00 2018-04-23 stsp default:
501 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
502 bff6ca00 2018-04-23 stsp break;
503 bff6ca00 2018-04-23 stsp }
504 bff6ca00 2018-04-23 stsp
505 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
506 bff6ca00 2018-04-23 stsp
507 bff6ca00 2018-04-23 stsp return err;
508 bff6ca00 2018-04-23 stsp }
509 bff6ca00 2018-04-23 stsp
510 bff6ca00 2018-04-23 stsp const struct got_error *
511 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
512 bff6ca00 2018-04-23 stsp {
513 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
514 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
515 bff6ca00 2018-04-23 stsp uint8_t *buf;
516 bff6ca00 2018-04-23 stsp size_t len, total;
517 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
518 bff6ca00 2018-04-23 stsp
519 86acc566 2018-04-23 stsp memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
520 bff6ca00 2018-04-23 stsp icommit.author_len = strlen(commit->author);
521 788c352e 2018-06-16 stsp memcpy(&icommit.tm_author, &commit->tm_author,
522 788c352e 2018-06-16 stsp sizeof(icommit.tm_author));
523 bff6ca00 2018-04-23 stsp icommit.committer_len = strlen(commit->committer);
524 788c352e 2018-06-16 stsp memcpy(&icommit.tm_committer, &commit->tm_committer,
525 788c352e 2018-06-16 stsp sizeof(icommit.tm_committer));
526 bff6ca00 2018-04-23 stsp icommit.logmsg_len = strlen(commit->logmsg);
527 bff6ca00 2018-04-23 stsp icommit.nparents = commit->nparents;
528 bff6ca00 2018-04-23 stsp
529 bff6ca00 2018-04-23 stsp total = sizeof(icommit) + icommit.author_len +
530 788c352e 2018-06-16 stsp icommit.committer_len + icommit.logmsg_len +
531 86acc566 2018-04-23 stsp icommit.nparents * SHA1_DIGEST_LENGTH;
532 bff6ca00 2018-04-23 stsp /* XXX TODO support very large log messages properly */
533 bff6ca00 2018-04-23 stsp if (total > MAX_IMSGSIZE)
534 bff6ca00 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
535 bff6ca00 2018-04-23 stsp
536 bff6ca00 2018-04-23 stsp buf = malloc(total);
537 bff6ca00 2018-04-23 stsp if (buf == NULL)
538 bff6ca00 2018-04-23 stsp return got_error_from_errno();
539 bff6ca00 2018-04-23 stsp
540 bff6ca00 2018-04-23 stsp len = 0;
541 bff6ca00 2018-04-23 stsp memcpy(buf + len, &icommit, sizeof(icommit));
542 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
543 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->author, icommit.author_len);
544 bff6ca00 2018-04-23 stsp len += icommit.author_len;
545 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->committer, icommit.committer_len);
546 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
547 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
548 bff6ca00 2018-04-23 stsp len += icommit.logmsg_len;
549 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
550 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
551 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
552 bff6ca00 2018-04-23 stsp }
553 bff6ca00 2018-04-23 stsp
554 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
555 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
556 bff6ca00 2018-04-23 stsp goto done;
557 bff6ca00 2018-04-23 stsp }
558 bff6ca00 2018-04-23 stsp
559 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
560 bff6ca00 2018-04-23 stsp done:
561 bff6ca00 2018-04-23 stsp free(buf);
562 bff6ca00 2018-04-23 stsp return err;
563 bff6ca00 2018-04-23 stsp }
564 bff6ca00 2018-04-23 stsp const struct got_error *
565 068fd2bf 2018-04-24 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
566 bff6ca00 2018-04-23 stsp {
567 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
568 bff6ca00 2018-04-23 stsp struct imsg imsg;
569 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
570 bff6ca00 2018-04-23 stsp size_t len, datalen;
571 bff6ca00 2018-04-23 stsp int i;
572 bff6ca00 2018-04-23 stsp const size_t min_datalen =
573 bff6ca00 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
574 bff6ca00 2018-04-23 stsp sizeof(struct got_imsg_commit_object));
575 bff6ca00 2018-04-23 stsp uint8_t *data;
576 bff6ca00 2018-04-23 stsp
577 bff6ca00 2018-04-23 stsp *commit = NULL;
578 bff6ca00 2018-04-23 stsp
579 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
580 bff6ca00 2018-04-23 stsp if (err)
581 bff6ca00 2018-04-23 stsp return err;
582 bff6ca00 2018-04-23 stsp
583 bff6ca00 2018-04-23 stsp data = imsg.data;
584 bff6ca00 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
585 bff6ca00 2018-04-23 stsp len = 0;
586 bff6ca00 2018-04-23 stsp
587 bff6ca00 2018-04-23 stsp switch (imsg.hdr.type) {
588 bff6ca00 2018-04-23 stsp case GOT_IMSG_ERROR:
589 bff6ca00 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
590 bff6ca00 2018-04-23 stsp break;
591 bff6ca00 2018-04-23 stsp case GOT_IMSG_COMMIT:
592 bff6ca00 2018-04-23 stsp if (datalen < sizeof(icommit)) {
593 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
594 bff6ca00 2018-04-23 stsp break;
595 2178c42e 2018-04-22 stsp }
596 bff6ca00 2018-04-23 stsp
597 bff6ca00 2018-04-23 stsp memcpy(&icommit, data, sizeof(icommit));
598 bff6ca00 2018-04-23 stsp if (datalen != sizeof(icommit) + icommit.author_len +
599 788c352e 2018-06-16 stsp icommit.committer_len + icommit.logmsg_len +
600 86acc566 2018-04-23 stsp icommit.nparents * SHA1_DIGEST_LENGTH) {
601 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
602 bff6ca00 2018-04-23 stsp break;
603 bff6ca00 2018-04-23 stsp }
604 bff6ca00 2018-04-23 stsp if (icommit.nparents < 0) {
605 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
606 bff6ca00 2018-04-23 stsp break;
607 bff6ca00 2018-04-23 stsp }
608 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
609 bff6ca00 2018-04-23 stsp
610 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
611 bff6ca00 2018-04-23 stsp if (*commit == NULL) {
612 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
613 bff6ca00 2018-04-23 stsp break;
614 bff6ca00 2018-04-23 stsp }
615 bff6ca00 2018-04-23 stsp
616 86acc566 2018-04-23 stsp memcpy((*commit)->tree_id->sha1, icommit.tree_id,
617 86acc566 2018-04-23 stsp SHA1_DIGEST_LENGTH);
618 788c352e 2018-06-16 stsp memcpy(&(*commit)->tm_author, &icommit.tm_author,
619 788c352e 2018-06-16 stsp sizeof((*commit)->tm_author));
620 788c352e 2018-06-16 stsp memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
621 788c352e 2018-06-16 stsp sizeof((*commit)->tm_committer));
622 bff6ca00 2018-04-23 stsp
623 bff6ca00 2018-04-23 stsp if (icommit.author_len == 0) {
624 bff6ca00 2018-04-23 stsp (*commit)->author = strdup("");
625 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
626 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
627 bff6ca00 2018-04-23 stsp break;
628 bff6ca00 2018-04-23 stsp }
629 bff6ca00 2018-04-23 stsp } else {
630 bff6ca00 2018-04-23 stsp (*commit)->author = malloc(icommit.author_len + 1);
631 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
632 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
633 bff6ca00 2018-04-23 stsp break;
634 bff6ca00 2018-04-23 stsp }
635 bff6ca00 2018-04-23 stsp memcpy((*commit)->author, data + len,
636 bff6ca00 2018-04-23 stsp icommit.author_len);
637 bff6ca00 2018-04-23 stsp (*commit)->author[icommit.author_len] = '\0';
638 bff6ca00 2018-04-23 stsp }
639 bff6ca00 2018-04-23 stsp len += icommit.author_len;
640 6c281f94 2018-06-11 stsp
641 bff6ca00 2018-04-23 stsp if (icommit.committer_len == 0) {
642 bff6ca00 2018-04-23 stsp (*commit)->committer = strdup("");
643 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
644 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
645 bff6ca00 2018-04-23 stsp break;
646 bff6ca00 2018-04-23 stsp }
647 bff6ca00 2018-04-23 stsp } else {
648 bff6ca00 2018-04-23 stsp (*commit)->committer =
649 bff6ca00 2018-04-23 stsp malloc(icommit.committer_len + 1);
650 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
651 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
652 bff6ca00 2018-04-23 stsp break;
653 bff6ca00 2018-04-23 stsp }
654 bff6ca00 2018-04-23 stsp memcpy((*commit)->committer, data + len,
655 bff6ca00 2018-04-23 stsp icommit.committer_len);
656 bff6ca00 2018-04-23 stsp (*commit)->committer[icommit.committer_len] = '\0';
657 bff6ca00 2018-04-23 stsp }
658 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
659 bff6ca00 2018-04-23 stsp
660 bff6ca00 2018-04-23 stsp if (icommit.logmsg_len == 0) {
661 bff6ca00 2018-04-23 stsp (*commit)->logmsg = strdup("");
662 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
663 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
664 bff6ca00 2018-04-23 stsp break;
665 bff6ca00 2018-04-23 stsp }
666 bff6ca00 2018-04-23 stsp } else {
667 bff6ca00 2018-04-23 stsp (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
668 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
669 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
670 bff6ca00 2018-04-23 stsp break;
671 bff6ca00 2018-04-23 stsp }
672 bff6ca00 2018-04-23 stsp memcpy((*commit)->logmsg, data + len,
673 bff6ca00 2018-04-23 stsp icommit.logmsg_len);
674 bff6ca00 2018-04-23 stsp (*commit)->logmsg[icommit.logmsg_len] = '\0';
675 bff6ca00 2018-04-23 stsp }
676 bff6ca00 2018-04-23 stsp len += icommit.logmsg_len;
677 bff6ca00 2018-04-23 stsp
678 bff6ca00 2018-04-23 stsp for (i = 0; i < icommit.nparents; i++) {
679 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
680 86acc566 2018-04-23 stsp
681 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
682 79f35eb3 2018-06-11 stsp if (qid == NULL) {
683 86acc566 2018-04-23 stsp err = got_error_from_errno();
684 bff6ca00 2018-04-23 stsp break;
685 86acc566 2018-04-23 stsp }
686 79f35eb3 2018-06-11 stsp qid->id = calloc(1, sizeof(*qid->id));
687 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
688 86acc566 2018-04-23 stsp err = got_error_from_errno();
689 79f35eb3 2018-06-11 stsp free(qid);
690 86acc566 2018-04-23 stsp break;
691 86acc566 2018-04-23 stsp }
692 86acc566 2018-04-23 stsp
693 79f35eb3 2018-06-11 stsp memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
694 79f35eb3 2018-06-11 stsp sizeof(*qid->id));
695 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
696 86acc566 2018-04-23 stsp (*commit)->nparents++;
697 bff6ca00 2018-04-23 stsp }
698 2178c42e 2018-04-22 stsp break;
699 8c580685 2018-04-22 stsp default:
700 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
701 8c580685 2018-04-22 stsp break;
702 2178c42e 2018-04-22 stsp }
703 2178c42e 2018-04-22 stsp
704 2178c42e 2018-04-22 stsp imsg_free(&imsg);
705 e033d803 2018-04-23 stsp
706 e033d803 2018-04-23 stsp return err;
707 e033d803 2018-04-23 stsp }
708 e033d803 2018-04-23 stsp
709 e033d803 2018-04-23 stsp const struct got_error *
710 068fd2bf 2018-04-24 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
711 e033d803 2018-04-23 stsp {
712 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
713 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
714 e033d803 2018-04-23 stsp struct got_tree_entry *te;
715 e033d803 2018-04-23 stsp
716 883f0469 2018-06-23 stsp itree.nentries = tree->entries.nentries;
717 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
718 e033d803 2018-04-23 stsp == -1)
719 e033d803 2018-04-23 stsp return got_error_from_errno();
720 e033d803 2018-04-23 stsp
721 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
722 e033d803 2018-04-23 stsp if (err)
723 e033d803 2018-04-23 stsp return err;
724 e033d803 2018-04-23 stsp
725 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
726 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
727 e033d803 2018-04-23 stsp uint8_t *buf = NULL;
728 e033d803 2018-04-23 stsp size_t len = sizeof(ite) + strlen(te->name);
729 e033d803 2018-04-23 stsp
730 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
731 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
732 e033d803 2018-04-23 stsp
733 e033d803 2018-04-23 stsp buf = malloc(len);
734 e033d803 2018-04-23 stsp if (buf == NULL)
735 e033d803 2018-04-23 stsp return got_error_from_errno();
736 e033d803 2018-04-23 stsp
737 e033d803 2018-04-23 stsp memcpy(ite.id, te->id->sha1, sizeof(ite.id));
738 e033d803 2018-04-23 stsp ite.mode = te->mode;
739 e033d803 2018-04-23 stsp memcpy(buf, &ite, sizeof(ite));
740 e033d803 2018-04-23 stsp memcpy(buf + sizeof(ite), te->name, strlen(te->name));
741 e033d803 2018-04-23 stsp
742 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
743 e033d803 2018-04-23 stsp buf, len) == -1)
744 e033d803 2018-04-23 stsp err = got_error_from_errno();
745 e033d803 2018-04-23 stsp free(buf);
746 e033d803 2018-04-23 stsp if (err)
747 e033d803 2018-04-23 stsp return err;
748 e033d803 2018-04-23 stsp
749 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
750 e033d803 2018-04-23 stsp if (err)
751 e033d803 2018-04-23 stsp return err;
752 e033d803 2018-04-23 stsp }
753 e033d803 2018-04-23 stsp
754 e033d803 2018-04-23 stsp return NULL;
755 e033d803 2018-04-23 stsp }
756 e033d803 2018-04-23 stsp
757 e033d803 2018-04-23 stsp const struct got_error *
758 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
759 e033d803 2018-04-23 stsp {
760 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
761 e033d803 2018-04-23 stsp const size_t min_datalen =
762 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
763 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
764 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree = { 0 };
765 e033d803 2018-04-23 stsp int nentries = 0;
766 2178c42e 2018-04-22 stsp
767 e033d803 2018-04-23 stsp *tree = NULL;
768 e033d803 2018-04-23 stsp get_more:
769 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
770 e033d803 2018-04-23 stsp if (err)
771 1e51f5b9 2018-04-23 stsp goto done;
772 e033d803 2018-04-23 stsp
773 e033d803 2018-04-23 stsp while (1) {
774 e033d803 2018-04-23 stsp struct imsg imsg;
775 e033d803 2018-04-23 stsp size_t n;
776 e033d803 2018-04-23 stsp size_t datalen;
777 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
778 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
779 e033d803 2018-04-23 stsp
780 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
781 e033d803 2018-04-23 stsp if (n == 0) {
782 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries)
783 e033d803 2018-04-23 stsp goto get_more;
784 e033d803 2018-04-23 stsp break;
785 e033d803 2018-04-23 stsp }
786 e033d803 2018-04-23 stsp
787 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
788 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
789 e033d803 2018-04-23 stsp
790 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
791 e033d803 2018-04-23 stsp
792 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
793 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
794 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
795 e033d803 2018-04-23 stsp break;
796 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
797 e033d803 2018-04-23 stsp /* This message should only appear once. */
798 e033d803 2018-04-23 stsp if (*tree != NULL) {
799 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
800 e033d803 2018-04-23 stsp break;
801 e033d803 2018-04-23 stsp }
802 e033d803 2018-04-23 stsp if (datalen != sizeof(itree)) {
803 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
804 e033d803 2018-04-23 stsp break;
805 e033d803 2018-04-23 stsp }
806 e033d803 2018-04-23 stsp memcpy(&itree, imsg.data, sizeof(itree));
807 e033d803 2018-04-23 stsp *tree = calloc(1, sizeof(**tree));
808 e033d803 2018-04-23 stsp if (*tree == NULL) {
809 e033d803 2018-04-23 stsp err = got_error_from_errno();
810 e033d803 2018-04-23 stsp break;
811 e033d803 2018-04-23 stsp }
812 883f0469 2018-06-23 stsp (*tree)->entries.nentries = itree.nentries;
813 883f0469 2018-06-23 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
814 e033d803 2018-04-23 stsp break;
815 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
816 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
817 e033d803 2018-04-23 stsp if (*tree == NULL) {
818 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
819 e033d803 2018-04-23 stsp break;
820 e033d803 2018-04-23 stsp }
821 e033d803 2018-04-23 stsp if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
822 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
823 e033d803 2018-04-23 stsp break;
824 e033d803 2018-04-23 stsp }
825 e033d803 2018-04-23 stsp
826 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
827 e033d803 2018-04-23 stsp datalen -= sizeof(ite);
828 e033d803 2018-04-23 stsp memcpy(&ite, imsg.data, sizeof(ite));
829 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
830 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
831 e033d803 2018-04-23 stsp break;
832 e033d803 2018-04-23 stsp }
833 052d4dc3 2018-04-23 stsp
834 e033d803 2018-04-23 stsp te = got_alloc_tree_entry_partial();
835 e033d803 2018-04-23 stsp if (te == NULL) {
836 e033d803 2018-04-23 stsp err = got_error_from_errno();
837 e033d803 2018-04-23 stsp break;
838 e033d803 2018-04-23 stsp }
839 e033d803 2018-04-23 stsp te->name = malloc(datalen + 1);
840 e033d803 2018-04-23 stsp if (te->name == NULL) {
841 e033d803 2018-04-23 stsp free(te);
842 e033d803 2018-04-23 stsp err = got_error_from_errno();
843 e033d803 2018-04-23 stsp break;
844 e033d803 2018-04-23 stsp }
845 052d4dc3 2018-04-23 stsp memcpy(te->name, imsg.data + sizeof(ite), datalen);
846 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
847 e033d803 2018-04-23 stsp
848 e033d803 2018-04-23 stsp memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
849 e033d803 2018-04-23 stsp te->mode = ite.mode;
850 883f0469 2018-06-23 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
851 e033d803 2018-04-23 stsp nentries++;
852 e033d803 2018-04-23 stsp break;
853 e033d803 2018-04-23 stsp default:
854 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
855 e033d803 2018-04-23 stsp break;
856 e033d803 2018-04-23 stsp }
857 e033d803 2018-04-23 stsp
858 e033d803 2018-04-23 stsp imsg_free(&imsg);
859 e033d803 2018-04-23 stsp }
860 1e51f5b9 2018-04-23 stsp done:
861 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries) {
862 1e51f5b9 2018-04-23 stsp if (err == NULL)
863 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
864 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
865 e033d803 2018-04-23 stsp *tree = NULL;
866 ff6b18f8 2018-04-24 stsp }
867 ff6b18f8 2018-04-24 stsp
868 ff6b18f8 2018-04-24 stsp return err;
869 ff6b18f8 2018-04-24 stsp }
870 ff6b18f8 2018-04-24 stsp
871 ff6b18f8 2018-04-24 stsp const struct got_error *
872 2967a784 2018-04-24 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
873 ff6b18f8 2018-04-24 stsp {
874 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
875 2967a784 2018-04-24 stsp
876 2967a784 2018-04-24 stsp iblob.size = size;
877 ff6b18f8 2018-04-24 stsp /* Data has already been written to file descriptor. */
878 2967a784 2018-04-24 stsp
879 2967a784 2018-04-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
880 2967a784 2018-04-24 stsp == -1)
881 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
882 ff6b18f8 2018-04-24 stsp
883 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
884 ff6b18f8 2018-04-24 stsp }
885 ff6b18f8 2018-04-24 stsp
886 ff6b18f8 2018-04-24 stsp const struct got_error *
887 2967a784 2018-04-24 stsp got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
888 ff6b18f8 2018-04-24 stsp {
889 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
890 ff6b18f8 2018-04-24 stsp struct imsg imsg;
891 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
892 ff6b18f8 2018-04-24 stsp size_t datalen;
893 ff6b18f8 2018-04-24 stsp
894 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
895 ff6b18f8 2018-04-24 stsp if (err)
896 ff6b18f8 2018-04-24 stsp return err;
897 ff6b18f8 2018-04-24 stsp
898 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
899 ff6b18f8 2018-04-24 stsp
900 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
901 ff6b18f8 2018-04-24 stsp case GOT_IMSG_ERROR:
902 ff6b18f8 2018-04-24 stsp err = recv_imsg_error(&imsg, datalen);
903 ff6b18f8 2018-04-24 stsp break;
904 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
905 2967a784 2018-04-24 stsp if (datalen != sizeof(iblob))
906 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
907 2967a784 2018-04-24 stsp memcpy(&iblob, imsg.data, sizeof(iblob));
908 2967a784 2018-04-24 stsp *size = iblob.size;
909 ff6b18f8 2018-04-24 stsp /* Data has been written to file descriptor. */
910 ff6b18f8 2018-04-24 stsp break;
911 ff6b18f8 2018-04-24 stsp default:
912 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
913 ff6b18f8 2018-04-24 stsp break;
914 e033d803 2018-04-23 stsp }
915 e033d803 2018-04-23 stsp
916 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
917 ff6b18f8 2018-04-24 stsp
918 2178c42e 2018-04-22 stsp return err;
919 2178c42e 2018-04-22 stsp }
920 876c234b 2018-09-10 stsp
921 876c234b 2018-09-10 stsp const struct got_error *
922 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
923 876c234b 2018-09-10 stsp struct got_packidx *packidx)
924 876c234b 2018-09-10 stsp {
925 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
926 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
927 876c234b 2018-09-10 stsp int fd;
928 876c234b 2018-09-10 stsp
929 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
930 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
931 876c234b 2018-09-10 stsp if (fd == -1)
932 876c234b 2018-09-10 stsp return got_error_from_errno();
933 876c234b 2018-09-10 stsp
934 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
935 876c234b 2018-09-10 stsp sizeof(ipackidx)) == -1)
936 876c234b 2018-09-10 stsp return got_error_from_errno();
937 876c234b 2018-09-10 stsp
938 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
939 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
940 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
941 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
942 876c234b 2018-09-10 stsp
943 876c234b 2018-09-10 stsp fd = dup(pack->fd);
944 876c234b 2018-09-10 stsp if (fd == -1)
945 876c234b 2018-09-10 stsp return got_error_from_errno();
946 876c234b 2018-09-10 stsp
947 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
948 876c234b 2018-09-10 stsp == -1)
949 876c234b 2018-09-10 stsp return got_error_from_errno();
950 876c234b 2018-09-10 stsp
951 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
952 876c234b 2018-09-10 stsp }
953 876c234b 2018-09-10 stsp
954 876c234b 2018-09-10 stsp const struct got_error *
955 876c234b 2018-09-10 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx)
956 876c234b 2018-09-10 stsp {
957 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
958 876c234b 2018-09-10 stsp
959 876c234b 2018-09-10 stsp iobj.idx = idx;
960 876c234b 2018-09-10 stsp
961 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
962 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
963 876c234b 2018-09-10 stsp return got_error_from_errno();
964 876c234b 2018-09-10 stsp
965 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
966 876c234b 2018-09-10 stsp }