Blame


1 2178c42e 2018-04-22 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 c39c25dd 2019-08-09 stsp #endif
48 c39c25dd 2019-08-09 stsp
49 c39c25dd 2019-08-09 stsp #ifndef nitems
50 c39c25dd 2019-08-09 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 2178c42e 2018-04-22 stsp #endif
52 2178c42e 2018-04-22 stsp
53 2178c42e 2018-04-22 stsp static const struct got_error *
54 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
55 2178c42e 2018-04-22 stsp {
56 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
57 2178c42e 2018-04-22 stsp int n;
58 2178c42e 2018-04-22 stsp
59 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
60 2178c42e 2018-04-22 stsp pfd[0].events = events;
61 2178c42e 2018-04-22 stsp
62 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
63 2178c42e 2018-04-22 stsp if (n == -1)
64 638f9024 2019-05-13 stsp return got_error_from_errno("poll");
65 2178c42e 2018-04-22 stsp if (n == 0)
66 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
67 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
68 638f9024 2019-05-13 stsp return got_error_from_errno("poll error");
69 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
70 2178c42e 2018-04-22 stsp return NULL;
71 2178c42e 2018-04-22 stsp
72 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
73 2178c42e 2018-04-22 stsp }
74 2178c42e 2018-04-22 stsp
75 c4eae628 2018-04-23 stsp static const struct got_error *
76 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
77 fe36cf76 2018-04-23 stsp {
78 fe36cf76 2018-04-23 stsp const struct got_error *err;
79 e033d803 2018-04-23 stsp size_t n;
80 fe36cf76 2018-04-23 stsp
81 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
82 fe36cf76 2018-04-23 stsp if (err)
83 fe36cf76 2018-04-23 stsp return err;
84 fe36cf76 2018-04-23 stsp
85 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
86 fe36cf76 2018-04-23 stsp if (n == -1) {
87 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
88 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
89 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
90 fe36cf76 2018-04-23 stsp }
91 fe36cf76 2018-04-23 stsp if (n == 0)
92 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
93 fe36cf76 2018-04-23 stsp
94 e033d803 2018-04-23 stsp return NULL;
95 e033d803 2018-04-23 stsp }
96 e033d803 2018-04-23 stsp
97 ad242220 2018-09-08 stsp const struct got_error *
98 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
99 876c234b 2018-09-10 stsp {
100 876c234b 2018-09-10 stsp int child_status;
101 876c234b 2018-09-10 stsp
102 2cb49fa8 2019-05-10 stsp if (waitpid(pid, &child_status, 0) == -1)
103 638f9024 2019-05-13 stsp return got_error_from_errno("waitpid");
104 876c234b 2018-09-10 stsp
105 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
106 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
107 876c234b 2018-09-10 stsp
108 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
109 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
110 876c234b 2018-09-10 stsp
111 876c234b 2018-09-10 stsp return NULL;
112 876c234b 2018-09-10 stsp }
113 876c234b 2018-09-10 stsp
114 73b7854a 2018-11-11 stsp static const struct got_error *
115 73b7854a 2018-11-11 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
116 73b7854a 2018-11-11 stsp {
117 73b7854a 2018-11-11 stsp struct got_imsg_error *ierr;
118 73b7854a 2018-11-11 stsp
119 73b7854a 2018-11-11 stsp if (datalen != sizeof(*ierr))
120 73b7854a 2018-11-11 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
121 73b7854a 2018-11-11 stsp
122 73b7854a 2018-11-11 stsp ierr = imsg->data;
123 73b7854a 2018-11-11 stsp if (ierr->code == GOT_ERR_ERRNO) {
124 73b7854a 2018-11-11 stsp static struct got_error serr;
125 73b7854a 2018-11-11 stsp serr.code = GOT_ERR_ERRNO;
126 73b7854a 2018-11-11 stsp serr.msg = strerror(ierr->errno_code);
127 73b7854a 2018-11-11 stsp return &serr;
128 73b7854a 2018-11-11 stsp }
129 73b7854a 2018-11-11 stsp
130 73b7854a 2018-11-11 stsp return got_error(ierr->code);
131 73b7854a 2018-11-11 stsp }
132 73b7854a 2018-11-11 stsp
133 876c234b 2018-09-10 stsp const struct got_error *
134 46de5bfd 2018-11-11 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
135 46de5bfd 2018-11-11 stsp size_t min_datalen)
136 e033d803 2018-04-23 stsp {
137 e033d803 2018-04-23 stsp const struct got_error *err;
138 e033d803 2018-04-23 stsp ssize_t n;
139 e033d803 2018-04-23 stsp
140 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
141 876c234b 2018-09-10 stsp if (n == -1)
142 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_get");
143 876c234b 2018-09-10 stsp
144 876c234b 2018-09-10 stsp while (n == 0) {
145 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
146 876c234b 2018-09-10 stsp if (err)
147 876c234b 2018-09-10 stsp return err;
148 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
149 876c234b 2018-09-10 stsp }
150 fe36cf76 2018-04-23 stsp
151 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
152 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
153 c4eae628 2018-04-23 stsp
154 73b7854a 2018-11-11 stsp if (imsg->hdr.type == GOT_IMSG_ERROR) {
155 73b7854a 2018-11-11 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
156 73b7854a 2018-11-11 stsp return recv_imsg_error(imsg, datalen);
157 c4eae628 2018-04-23 stsp }
158 c4eae628 2018-04-23 stsp
159 73b7854a 2018-11-11 stsp return NULL;
160 c4eae628 2018-04-23 stsp }
161 c4eae628 2018-04-23 stsp
162 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
163 2178c42e 2018-04-22 stsp void
164 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
165 2178c42e 2018-04-22 stsp {
166 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
167 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
168 2178c42e 2018-04-22 stsp int ret;
169 2178c42e 2018-04-22 stsp
170 2178c42e 2018-04-22 stsp ierr.code = err->code;
171 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
172 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
173 2178c42e 2018-04-22 stsp else
174 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
175 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
176 e93cd828 2018-11-11 stsp if (ret == -1) {
177 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
178 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
179 5d43e84d 2018-04-23 stsp return;
180 2178c42e 2018-04-22 stsp }
181 2178c42e 2018-04-22 stsp
182 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
183 5d43e84d 2018-04-23 stsp if (poll_err) {
184 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
185 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
186 5d43e84d 2018-04-23 stsp return;
187 5d43e84d 2018-04-23 stsp }
188 2178c42e 2018-04-22 stsp
189 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
190 5d43e84d 2018-04-23 stsp if (ret == -1) {
191 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
192 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
193 5d43e84d 2018-04-23 stsp return;
194 5d43e84d 2018-04-23 stsp }
195 e033d803 2018-04-23 stsp }
196 e033d803 2018-04-23 stsp
197 e033d803 2018-04-23 stsp static const struct got_error *
198 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
199 e033d803 2018-04-23 stsp {
200 e033d803 2018-04-23 stsp const struct got_error *err;
201 e033d803 2018-04-23 stsp
202 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
203 e033d803 2018-04-23 stsp if (err)
204 e033d803 2018-04-23 stsp return err;
205 e033d803 2018-04-23 stsp
206 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
207 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_flush");
208 e033d803 2018-04-23 stsp
209 e033d803 2018-04-23 stsp return NULL;
210 ad242220 2018-09-08 stsp }
211 ad242220 2018-09-08 stsp
212 ad242220 2018-09-08 stsp const struct got_error *
213 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
214 ad242220 2018-09-08 stsp {
215 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
216 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
217 ad242220 2018-09-08 stsp
218 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
219 ad242220 2018-09-08 stsp
220 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
221 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose STOP");
222 ad242220 2018-09-08 stsp
223 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
224 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
225 ad242220 2018-09-08 stsp return err;
226 7762fe12 2018-11-05 stsp }
227 7762fe12 2018-11-05 stsp
228 ad242220 2018-09-08 stsp const struct got_error *
229 aea5f015 2018-12-24 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
230 ad242220 2018-09-08 stsp {
231 aea5f015 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
232 aea5f015 2018-12-24 stsp == -1)
233 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT_REQUEST");
234 1785f84a 2018-12-23 stsp
235 1785f84a 2018-12-23 stsp return flush_imsg(ibuf);
236 1785f84a 2018-12-23 stsp }
237 1785f84a 2018-12-23 stsp
238 1785f84a 2018-12-23 stsp const struct got_error *
239 1785f84a 2018-12-23 stsp got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
240 1785f84a 2018-12-23 stsp struct got_object_id *id, int pack_idx)
241 1785f84a 2018-12-23 stsp {
242 41496140 2019-02-21 stsp const struct got_error *err = NULL;
243 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj, *iobjp;
244 1785f84a 2018-12-23 stsp size_t len;
245 1785f84a 2018-12-23 stsp
246 1785f84a 2018-12-23 stsp if (id) { /* commit is packed */
247 1785f84a 2018-12-23 stsp iobj.idx = pack_idx;
248 1785f84a 2018-12-23 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
249 1785f84a 2018-12-23 stsp iobjp = &iobj;
250 1785f84a 2018-12-23 stsp len = sizeof(iobj);
251 1785f84a 2018-12-23 stsp } else {
252 1785f84a 2018-12-23 stsp iobjp = NULL;
253 1785f84a 2018-12-23 stsp len = 0;
254 1785f84a 2018-12-23 stsp }
255 1785f84a 2018-12-23 stsp
256 1785f84a 2018-12-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
257 41496140 2019-02-21 stsp == -1) {
258 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
259 41496140 2019-02-21 stsp close(fd);
260 41496140 2019-02-21 stsp return err;
261 41496140 2019-02-21 stsp }
262 13c729f7 2018-12-24 stsp
263 13c729f7 2018-12-24 stsp return flush_imsg(ibuf);
264 13c729f7 2018-12-24 stsp }
265 13c729f7 2018-12-24 stsp
266 13c729f7 2018-12-24 stsp const struct got_error *
267 13c729f7 2018-12-24 stsp got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
268 13c729f7 2018-12-24 stsp struct got_object_id *id, int pack_idx)
269 13c729f7 2018-12-24 stsp {
270 41496140 2019-02-21 stsp const struct got_error *err = NULL;
271 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
272 13c729f7 2018-12-24 stsp size_t len;
273 13c729f7 2018-12-24 stsp
274 13c729f7 2018-12-24 stsp if (id) { /* tree is packed */
275 13c729f7 2018-12-24 stsp iobj.idx = pack_idx;
276 13c729f7 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
277 13c729f7 2018-12-24 stsp iobjp = &iobj;
278 13c729f7 2018-12-24 stsp len = sizeof(iobj);
279 13c729f7 2018-12-24 stsp } else {
280 13c729f7 2018-12-24 stsp iobjp = NULL;
281 13c729f7 2018-12-24 stsp len = 0;
282 13c729f7 2018-12-24 stsp }
283 13c729f7 2018-12-24 stsp
284 13c729f7 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
285 41496140 2019-02-21 stsp == -1) {
286 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TREE_REQUEST");
287 41496140 2019-02-21 stsp close(fd);
288 41496140 2019-02-21 stsp return err;
289 41496140 2019-02-21 stsp }
290 268f7291 2018-12-24 stsp
291 268f7291 2018-12-24 stsp return flush_imsg(ibuf);
292 268f7291 2018-12-24 stsp }
293 268f7291 2018-12-24 stsp
294 268f7291 2018-12-24 stsp const struct got_error *
295 268f7291 2018-12-24 stsp got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
296 268f7291 2018-12-24 stsp struct got_object_id *id, int pack_idx)
297 268f7291 2018-12-24 stsp {
298 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
299 268f7291 2018-12-24 stsp size_t len;
300 268f7291 2018-12-24 stsp
301 268f7291 2018-12-24 stsp if (id) { /* tag is packed */
302 268f7291 2018-12-24 stsp iobj.idx = pack_idx;
303 268f7291 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
304 268f7291 2018-12-24 stsp iobjp = &iobj;
305 268f7291 2018-12-24 stsp len = sizeof(iobj);
306 268f7291 2018-12-24 stsp } else {
307 268f7291 2018-12-24 stsp iobjp = NULL;
308 268f7291 2018-12-24 stsp len = 0;
309 268f7291 2018-12-24 stsp }
310 268f7291 2018-12-24 stsp
311 268f7291 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
312 1785f84a 2018-12-23 stsp == -1)
313 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TAG_REQUEST");
314 7762fe12 2018-11-05 stsp
315 7762fe12 2018-11-05 stsp return flush_imsg(ibuf);
316 7762fe12 2018-11-05 stsp }
317 7762fe12 2018-11-05 stsp
318 7762fe12 2018-11-05 stsp const struct got_error *
319 ebc55e2d 2018-12-24 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
320 ebc55e2d 2018-12-24 stsp struct got_object_id *id, int pack_idx)
321 55da3778 2018-09-10 stsp {
322 41496140 2019-02-21 stsp const struct got_error *err = NULL;
323 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
324 ebc55e2d 2018-12-24 stsp size_t len;
325 ebc55e2d 2018-12-24 stsp
326 ebc55e2d 2018-12-24 stsp if (id) { /* blob is packed */
327 ebc55e2d 2018-12-24 stsp iobj.idx = pack_idx;
328 ebc55e2d 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
329 ebc55e2d 2018-12-24 stsp iobjp = &iobj;
330 ebc55e2d 2018-12-24 stsp len = sizeof(iobj);
331 ebc55e2d 2018-12-24 stsp } else {
332 ebc55e2d 2018-12-24 stsp iobjp = NULL;
333 ebc55e2d 2018-12-24 stsp len = 0;
334 ebc55e2d 2018-12-24 stsp }
335 ebc55e2d 2018-12-24 stsp
336 ebc55e2d 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
337 41496140 2019-02-21 stsp == -1) {
338 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_REQUEST");
339 41496140 2019-02-21 stsp close(infd);
340 41496140 2019-02-21 stsp return err;
341 41496140 2019-02-21 stsp }
342 ad242220 2018-09-08 stsp
343 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
344 55da3778 2018-09-10 stsp }
345 ad242220 2018-09-08 stsp
346 55da3778 2018-09-10 stsp const struct got_error *
347 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
348 55da3778 2018-09-10 stsp {
349 41496140 2019-02-21 stsp const struct got_error *err = NULL;
350 41496140 2019-02-21 stsp
351 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
352 41496140 2019-02-21 stsp == -1) {
353 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_OUTFD");
354 41496140 2019-02-21 stsp close(outfd);
355 41496140 2019-02-21 stsp return err;
356 41496140 2019-02-21 stsp }
357 3840f4c9 2018-09-12 stsp
358 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
359 3840f4c9 2018-09-12 stsp }
360 3840f4c9 2018-09-12 stsp
361 3840f4c9 2018-09-12 stsp const struct got_error *
362 3840f4c9 2018-09-12 stsp got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
363 3840f4c9 2018-09-12 stsp {
364 41496140 2019-02-21 stsp const struct got_error *err = NULL;
365 41496140 2019-02-21 stsp
366 3840f4c9 2018-09-12 stsp if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
367 41496140 2019-02-21 stsp == -1) {
368 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TMPFD");
369 41496140 2019-02-21 stsp close(fd);
370 41496140 2019-02-21 stsp return err;
371 41496140 2019-02-21 stsp }
372 ad242220 2018-09-08 stsp
373 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
374 ad242220 2018-09-08 stsp }
375 ad242220 2018-09-08 stsp
376 ad242220 2018-09-08 stsp const struct got_error *
377 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
378 2178c42e 2018-04-22 stsp {
379 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
380 2178c42e 2018-04-22 stsp
381 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
382 2178c42e 2018-04-22 stsp iobj.type = obj->type;
383 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
384 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
385 2178c42e 2018-04-22 stsp iobj.size = obj->size;
386 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
387 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
388 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
389 c59b3346 2018-09-11 stsp }
390 2178c42e 2018-04-22 stsp
391 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
392 2178c42e 2018-04-22 stsp == -1)
393 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT");
394 2178c42e 2018-04-22 stsp
395 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
396 cfd633c2 2018-09-10 stsp }
397 cfd633c2 2018-09-10 stsp
398 cfd633c2 2018-09-10 stsp const struct got_error *
399 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
400 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
401 cfd633c2 2018-09-10 stsp {
402 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
403 291624d8 2018-11-07 stsp struct got_imsg_object *iobj;
404 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
405 cfd633c2 2018-09-10 stsp
406 291624d8 2018-11-07 stsp if (datalen != sizeof(*iobj))
407 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
408 291624d8 2018-11-07 stsp iobj = imsg->data;
409 cfd633c2 2018-09-10 stsp
410 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
411 cfd633c2 2018-09-10 stsp if (*obj == NULL)
412 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
413 cfd633c2 2018-09-10 stsp
414 291624d8 2018-11-07 stsp memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
415 291624d8 2018-11-07 stsp (*obj)->type = iobj->type;
416 291624d8 2018-11-07 stsp (*obj)->flags = iobj->flags;
417 291624d8 2018-11-07 stsp (*obj)->hdrlen = iobj->hdrlen;
418 291624d8 2018-11-07 stsp (*obj)->size = iobj->size;
419 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
420 291624d8 2018-11-07 stsp if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
421 291624d8 2018-11-07 stsp (*obj)->pack_offset = iobj->pack_offset;
422 291624d8 2018-11-07 stsp (*obj)->pack_idx = iobj->pack_idx;
423 876c234b 2018-09-10 stsp }
424 876c234b 2018-09-10 stsp
425 876c234b 2018-09-10 stsp return err;
426 876c234b 2018-09-10 stsp }
427 876c234b 2018-09-10 stsp
428 2178c42e 2018-04-22 stsp const struct got_error *
429 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
430 2178c42e 2018-04-22 stsp {
431 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
432 2178c42e 2018-04-22 stsp struct imsg imsg;
433 2178c42e 2018-04-22 stsp size_t datalen;
434 c4eae628 2018-04-23 stsp const size_t min_datalen =
435 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
436 2178c42e 2018-04-22 stsp
437 2178c42e 2018-04-22 stsp *obj = NULL;
438 2178c42e 2018-04-22 stsp
439 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
440 2178c42e 2018-04-22 stsp if (err)
441 2178c42e 2018-04-22 stsp return err;
442 2178c42e 2018-04-22 stsp
443 2178c42e 2018-04-22 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
444 2178c42e 2018-04-22 stsp
445 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
446 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
447 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
448 bff6ca00 2018-04-23 stsp break;
449 bff6ca00 2018-04-23 stsp default:
450 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
451 bff6ca00 2018-04-23 stsp break;
452 bff6ca00 2018-04-23 stsp }
453 bff6ca00 2018-04-23 stsp
454 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
455 bff6ca00 2018-04-23 stsp
456 bff6ca00 2018-04-23 stsp return err;
457 c75f7264 2018-09-11 stsp }
458 c75f7264 2018-09-11 stsp
459 c75f7264 2018-09-11 stsp static const struct got_error *
460 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
461 c75f7264 2018-09-11 stsp size_t logmsg_len)
462 c75f7264 2018-09-11 stsp {
463 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
464 c75f7264 2018-09-11 stsp size_t offset, remain;
465 c75f7264 2018-09-11 stsp
466 c75f7264 2018-09-11 stsp offset = 0;
467 c75f7264 2018-09-11 stsp remain = logmsg_len;
468 c75f7264 2018-09-11 stsp while (remain > 0) {
469 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
470 c75f7264 2018-09-11 stsp
471 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
472 fa4ffeb3 2018-11-04 stsp commit->logmsg + offset, n) == -1) {
473 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose "
474 230a42bd 2019-05-11 jcs "COMMIT_LOGMSG");
475 fa4ffeb3 2018-11-04 stsp break;
476 fa4ffeb3 2018-11-04 stsp }
477 c75f7264 2018-09-11 stsp
478 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
479 fa4ffeb3 2018-11-04 stsp if (err)
480 fa4ffeb3 2018-11-04 stsp break;
481 c75f7264 2018-09-11 stsp
482 c75f7264 2018-09-11 stsp offset += n;
483 c75f7264 2018-09-11 stsp remain -= n;
484 c75f7264 2018-09-11 stsp }
485 c75f7264 2018-09-11 stsp
486 fa4ffeb3 2018-11-04 stsp return err;
487 bff6ca00 2018-04-23 stsp }
488 bff6ca00 2018-04-23 stsp
489 bff6ca00 2018-04-23 stsp const struct got_error *
490 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
491 bff6ca00 2018-04-23 stsp {
492 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
493 b9c33926 2018-11-07 stsp struct got_imsg_commit_object *icommit;
494 bff6ca00 2018-04-23 stsp uint8_t *buf;
495 bff6ca00 2018-04-23 stsp size_t len, total;
496 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
497 b9c33926 2018-11-07 stsp size_t author_len = strlen(commit->author);
498 b9c33926 2018-11-07 stsp size_t committer_len = strlen(commit->committer);
499 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
500 bff6ca00 2018-04-23 stsp
501 b9c33926 2018-11-07 stsp total = sizeof(*icommit) + author_len + committer_len +
502 b9c33926 2018-11-07 stsp commit->nparents * SHA1_DIGEST_LENGTH;
503 bff6ca00 2018-04-23 stsp
504 bff6ca00 2018-04-23 stsp buf = malloc(total);
505 bff6ca00 2018-04-23 stsp if (buf == NULL)
506 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
507 bff6ca00 2018-04-23 stsp
508 b9c33926 2018-11-07 stsp icommit = (struct got_imsg_commit_object *)buf;
509 a7403916 2018-12-24 stsp memcpy(icommit->tree_id, commit->tree_id->sha1,
510 a7403916 2018-12-24 stsp sizeof(icommit->tree_id));
511 b9c33926 2018-11-07 stsp icommit->author_len = author_len;
512 b9c33926 2018-11-07 stsp icommit->author_time = commit->author_time;
513 b9c33926 2018-11-07 stsp icommit->author_gmtoff = commit->author_gmtoff;
514 b9c33926 2018-11-07 stsp icommit->committer_len = committer_len;
515 b9c33926 2018-11-07 stsp icommit->committer_time = commit->committer_time;
516 b9c33926 2018-11-07 stsp icommit->committer_gmtoff = commit->committer_gmtoff;
517 b9c33926 2018-11-07 stsp icommit->logmsg_len = logmsg_len;
518 b9c33926 2018-11-07 stsp icommit->nparents = commit->nparents;
519 b9c33926 2018-11-07 stsp
520 b9c33926 2018-11-07 stsp len = sizeof(*icommit);
521 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->author, author_len);
522 b9c33926 2018-11-07 stsp len += author_len;
523 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->committer, committer_len);
524 b9c33926 2018-11-07 stsp len += committer_len;
525 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
526 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
527 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
528 bff6ca00 2018-04-23 stsp }
529 bff6ca00 2018-04-23 stsp
530 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
531 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT");
532 bff6ca00 2018-04-23 stsp goto done;
533 bff6ca00 2018-04-23 stsp }
534 bff6ca00 2018-04-23 stsp
535 904df868 2018-11-04 stsp if (logmsg_len == 0 ||
536 904df868 2018-11-04 stsp logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
537 904df868 2018-11-04 stsp err = flush_imsg(ibuf);
538 904df868 2018-11-04 stsp if (err)
539 904df868 2018-11-04 stsp goto done;
540 904df868 2018-11-04 stsp }
541 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
542 bff6ca00 2018-04-23 stsp done:
543 bff6ca00 2018-04-23 stsp free(buf);
544 bff6ca00 2018-04-23 stsp return err;
545 bff6ca00 2018-04-23 stsp }
546 cfd633c2 2018-09-10 stsp
547 bff6ca00 2018-04-23 stsp const struct got_error *
548 068fd2bf 2018-04-24 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
549 bff6ca00 2018-04-23 stsp {
550 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
551 bff6ca00 2018-04-23 stsp struct imsg imsg;
552 291624d8 2018-11-07 stsp struct got_imsg_commit_object *icommit;
553 bff6ca00 2018-04-23 stsp size_t len, datalen;
554 bff6ca00 2018-04-23 stsp int i;
555 bff6ca00 2018-04-23 stsp const size_t min_datalen =
556 bff6ca00 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
557 bff6ca00 2018-04-23 stsp sizeof(struct got_imsg_commit_object));
558 bff6ca00 2018-04-23 stsp
559 bff6ca00 2018-04-23 stsp *commit = NULL;
560 bff6ca00 2018-04-23 stsp
561 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
562 bff6ca00 2018-04-23 stsp if (err)
563 bff6ca00 2018-04-23 stsp return err;
564 bff6ca00 2018-04-23 stsp
565 bff6ca00 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
566 bff6ca00 2018-04-23 stsp len = 0;
567 bff6ca00 2018-04-23 stsp
568 bff6ca00 2018-04-23 stsp switch (imsg.hdr.type) {
569 bff6ca00 2018-04-23 stsp case GOT_IMSG_COMMIT:
570 291624d8 2018-11-07 stsp if (datalen < sizeof(*icommit)) {
571 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
572 bff6ca00 2018-04-23 stsp break;
573 2178c42e 2018-04-22 stsp }
574 291624d8 2018-11-07 stsp icommit = imsg.data;
575 291624d8 2018-11-07 stsp if (datalen != sizeof(*icommit) + icommit->author_len +
576 291624d8 2018-11-07 stsp icommit->committer_len +
577 291624d8 2018-11-07 stsp icommit->nparents * SHA1_DIGEST_LENGTH) {
578 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
579 bff6ca00 2018-04-23 stsp break;
580 bff6ca00 2018-04-23 stsp }
581 291624d8 2018-11-07 stsp if (icommit->nparents < 0) {
582 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
583 bff6ca00 2018-04-23 stsp break;
584 bff6ca00 2018-04-23 stsp }
585 291624d8 2018-11-07 stsp len += sizeof(*icommit);
586 bff6ca00 2018-04-23 stsp
587 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
588 bff6ca00 2018-04-23 stsp if (*commit == NULL) {
589 638f9024 2019-05-13 stsp err = got_error_from_errno(
590 230a42bd 2019-05-11 jcs "got_object_commit_alloc_partial");
591 bff6ca00 2018-04-23 stsp break;
592 bff6ca00 2018-04-23 stsp }
593 bff6ca00 2018-04-23 stsp
594 291624d8 2018-11-07 stsp memcpy((*commit)->tree_id->sha1, icommit->tree_id,
595 86acc566 2018-04-23 stsp SHA1_DIGEST_LENGTH);
596 291624d8 2018-11-07 stsp (*commit)->author_time = icommit->author_time;
597 291624d8 2018-11-07 stsp (*commit)->author_gmtoff = icommit->author_gmtoff;
598 291624d8 2018-11-07 stsp (*commit)->committer_time = icommit->committer_time;
599 291624d8 2018-11-07 stsp (*commit)->committer_gmtoff = icommit->committer_gmtoff;
600 bff6ca00 2018-04-23 stsp
601 291624d8 2018-11-07 stsp if (icommit->author_len == 0) {
602 bff6ca00 2018-04-23 stsp (*commit)->author = strdup("");
603 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
604 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
605 bff6ca00 2018-04-23 stsp break;
606 bff6ca00 2018-04-23 stsp }
607 bff6ca00 2018-04-23 stsp } else {
608 291624d8 2018-11-07 stsp (*commit)->author = malloc(icommit->author_len + 1);
609 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
610 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
611 bff6ca00 2018-04-23 stsp break;
612 bff6ca00 2018-04-23 stsp }
613 291624d8 2018-11-07 stsp memcpy((*commit)->author, imsg.data + len,
614 291624d8 2018-11-07 stsp icommit->author_len);
615 291624d8 2018-11-07 stsp (*commit)->author[icommit->author_len] = '\0';
616 bff6ca00 2018-04-23 stsp }
617 291624d8 2018-11-07 stsp len += icommit->author_len;
618 6c281f94 2018-06-11 stsp
619 291624d8 2018-11-07 stsp if (icommit->committer_len == 0) {
620 bff6ca00 2018-04-23 stsp (*commit)->committer = strdup("");
621 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
622 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
623 bff6ca00 2018-04-23 stsp break;
624 bff6ca00 2018-04-23 stsp }
625 bff6ca00 2018-04-23 stsp } else {
626 bff6ca00 2018-04-23 stsp (*commit)->committer =
627 291624d8 2018-11-07 stsp malloc(icommit->committer_len + 1);
628 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
629 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
630 bff6ca00 2018-04-23 stsp break;
631 bff6ca00 2018-04-23 stsp }
632 291624d8 2018-11-07 stsp memcpy((*commit)->committer, imsg.data + len,
633 291624d8 2018-11-07 stsp icommit->committer_len);
634 291624d8 2018-11-07 stsp (*commit)->committer[icommit->committer_len] = '\0';
635 bff6ca00 2018-04-23 stsp }
636 291624d8 2018-11-07 stsp len += icommit->committer_len;
637 bff6ca00 2018-04-23 stsp
638 291624d8 2018-11-07 stsp if (icommit->logmsg_len == 0) {
639 bff6ca00 2018-04-23 stsp (*commit)->logmsg = strdup("");
640 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
641 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
642 bff6ca00 2018-04-23 stsp break;
643 bff6ca00 2018-04-23 stsp }
644 bff6ca00 2018-04-23 stsp } else {
645 291624d8 2018-11-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
646 c75f7264 2018-09-11 stsp
647 291624d8 2018-11-07 stsp (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
648 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
649 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
650 bff6ca00 2018-04-23 stsp break;
651 bff6ca00 2018-04-23 stsp }
652 c75f7264 2018-09-11 stsp while (remain > 0) {
653 c75f7264 2018-09-11 stsp struct imsg imsg_log;
654 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
655 c75f7264 2018-09-11 stsp remain);
656 c75f7264 2018-09-11 stsp
657 c75f7264 2018-09-11 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
658 c75f7264 2018-09-11 stsp if (err)
659 c75f7264 2018-09-11 stsp return err;
660 c75f7264 2018-09-11 stsp
661 c75f7264 2018-09-11 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
662 c75f7264 2018-09-11 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
663 c75f7264 2018-09-11 stsp
664 c75f7264 2018-09-11 stsp memcpy((*commit)->logmsg + offset,
665 c75f7264 2018-09-11 stsp imsg_log.data, n);
666 c75f7264 2018-09-11 stsp imsg_free(&imsg_log);
667 c75f7264 2018-09-11 stsp offset += n;
668 c75f7264 2018-09-11 stsp remain -= n;
669 c75f7264 2018-09-11 stsp }
670 291624d8 2018-11-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
671 bff6ca00 2018-04-23 stsp }
672 bff6ca00 2018-04-23 stsp
673 291624d8 2018-11-07 stsp for (i = 0; i < icommit->nparents; i++) {
674 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
675 86acc566 2018-04-23 stsp
676 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
677 5df4932d 2018-11-05 stsp if (err)
678 41fa1437 2018-11-05 stsp break;
679 291624d8 2018-11-07 stsp memcpy(qid->id, imsg.data + len +
680 291624d8 2018-11-07 stsp i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
681 7762fe12 2018-11-05 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
682 7762fe12 2018-11-05 stsp (*commit)->nparents++;
683 7762fe12 2018-11-05 stsp }
684 2178c42e 2018-04-22 stsp break;
685 8c580685 2018-04-22 stsp default:
686 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
687 8c580685 2018-04-22 stsp break;
688 2178c42e 2018-04-22 stsp }
689 2178c42e 2018-04-22 stsp
690 2178c42e 2018-04-22 stsp imsg_free(&imsg);
691 e033d803 2018-04-23 stsp
692 e033d803 2018-04-23 stsp return err;
693 e033d803 2018-04-23 stsp }
694 e033d803 2018-04-23 stsp
695 e033d803 2018-04-23 stsp const struct got_error *
696 068fd2bf 2018-04-24 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
697 e033d803 2018-04-23 stsp {
698 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
699 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
700 e033d803 2018-04-23 stsp struct got_tree_entry *te;
701 b00c9821 2018-11-04 stsp size_t totlen;
702 6eb07a17 2018-11-04 stsp int nimsg; /* number of imsg queued in ibuf */
703 e033d803 2018-04-23 stsp
704 883f0469 2018-06-23 stsp itree.nentries = tree->entries.nentries;
705 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
706 e033d803 2018-04-23 stsp == -1)
707 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TREE");
708 e033d803 2018-04-23 stsp
709 b00c9821 2018-11-04 stsp totlen = sizeof(itree);
710 6eb07a17 2018-11-04 stsp nimsg = 1;
711 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
712 a58a49db 2018-11-07 stsp struct got_imsg_tree_entry *ite;
713 e033d803 2018-04-23 stsp uint8_t *buf = NULL;
714 a58a49db 2018-11-07 stsp size_t len = sizeof(*ite) + strlen(te->name);
715 e033d803 2018-04-23 stsp
716 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
717 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
718 e033d803 2018-04-23 stsp
719 6eb07a17 2018-11-04 stsp nimsg++;
720 6eb07a17 2018-11-04 stsp if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
721 b00c9821 2018-11-04 stsp err = flush_imsg(ibuf);
722 b00c9821 2018-11-04 stsp if (err)
723 b00c9821 2018-11-04 stsp return err;
724 6eb07a17 2018-11-04 stsp nimsg = 0;
725 b00c9821 2018-11-04 stsp }
726 b00c9821 2018-11-04 stsp
727 e033d803 2018-04-23 stsp buf = malloc(len);
728 e033d803 2018-04-23 stsp if (buf == NULL)
729 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
730 e033d803 2018-04-23 stsp
731 a58a49db 2018-11-07 stsp ite = (struct got_imsg_tree_entry *)buf;
732 a58a49db 2018-11-07 stsp memcpy(ite->id, te->id->sha1, sizeof(ite->id));
733 a58a49db 2018-11-07 stsp ite->mode = te->mode;
734 a58a49db 2018-11-07 stsp memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
735 e033d803 2018-04-23 stsp
736 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
737 e033d803 2018-04-23 stsp buf, len) == -1)
738 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TREE_ENTRY");
739 e033d803 2018-04-23 stsp free(buf);
740 e033d803 2018-04-23 stsp if (err)
741 e033d803 2018-04-23 stsp return err;
742 b00c9821 2018-11-04 stsp totlen += len;
743 e033d803 2018-04-23 stsp }
744 e033d803 2018-04-23 stsp
745 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
746 e033d803 2018-04-23 stsp }
747 e033d803 2018-04-23 stsp
748 e033d803 2018-04-23 stsp const struct got_error *
749 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
750 e033d803 2018-04-23 stsp {
751 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
752 e033d803 2018-04-23 stsp const size_t min_datalen =
753 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
754 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
755 291624d8 2018-11-07 stsp struct got_imsg_tree_object *itree;
756 e033d803 2018-04-23 stsp int nentries = 0;
757 2178c42e 2018-04-22 stsp
758 e033d803 2018-04-23 stsp *tree = NULL;
759 e033d803 2018-04-23 stsp get_more:
760 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
761 e033d803 2018-04-23 stsp if (err)
762 1e51f5b9 2018-04-23 stsp goto done;
763 e033d803 2018-04-23 stsp
764 656b1f76 2019-05-11 jcs for (;;) {
765 e033d803 2018-04-23 stsp struct imsg imsg;
766 e033d803 2018-04-23 stsp size_t n;
767 e033d803 2018-04-23 stsp size_t datalen;
768 c0588d8d 2018-11-07 stsp struct got_imsg_tree_entry *ite;
769 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
770 e033d803 2018-04-23 stsp
771 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
772 e033d803 2018-04-23 stsp if (n == 0) {
773 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries)
774 e033d803 2018-04-23 stsp goto get_more;
775 e033d803 2018-04-23 stsp break;
776 e033d803 2018-04-23 stsp }
777 e033d803 2018-04-23 stsp
778 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
779 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
780 e033d803 2018-04-23 stsp
781 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
782 e033d803 2018-04-23 stsp
783 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
784 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
785 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
786 e033d803 2018-04-23 stsp break;
787 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
788 e033d803 2018-04-23 stsp /* This message should only appear once. */
789 e033d803 2018-04-23 stsp if (*tree != NULL) {
790 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
791 e033d803 2018-04-23 stsp break;
792 e033d803 2018-04-23 stsp }
793 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
794 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
795 e033d803 2018-04-23 stsp break;
796 e033d803 2018-04-23 stsp }
797 291624d8 2018-11-07 stsp itree = imsg.data;
798 c3b78ecc 2018-11-07 stsp *tree = malloc(sizeof(**tree));
799 e033d803 2018-04-23 stsp if (*tree == NULL) {
800 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
801 e033d803 2018-04-23 stsp break;
802 e033d803 2018-04-23 stsp }
803 291624d8 2018-11-07 stsp (*tree)->entries.nentries = itree->nentries;
804 883f0469 2018-06-23 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
805 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
806 e033d803 2018-04-23 stsp break;
807 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
808 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
809 e033d803 2018-04-23 stsp if (*tree == NULL) {
810 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
811 e033d803 2018-04-23 stsp break;
812 e033d803 2018-04-23 stsp }
813 c0588d8d 2018-11-07 stsp if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
814 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
815 e033d803 2018-04-23 stsp break;
816 e033d803 2018-04-23 stsp }
817 e033d803 2018-04-23 stsp
818 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
819 c0588d8d 2018-11-07 stsp datalen -= sizeof(*ite);
820 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
821 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
822 e033d803 2018-04-23 stsp break;
823 e033d803 2018-04-23 stsp }
824 c0588d8d 2018-11-07 stsp ite = imsg.data;
825 052d4dc3 2018-04-23 stsp
826 e033d803 2018-04-23 stsp te = got_alloc_tree_entry_partial();
827 e033d803 2018-04-23 stsp if (te == NULL) {
828 638f9024 2019-05-13 stsp err = got_error_from_errno(
829 230a42bd 2019-05-11 jcs "got_alloc_tree_entry_partial");
830 e033d803 2018-04-23 stsp break;
831 e033d803 2018-04-23 stsp }
832 e033d803 2018-04-23 stsp te->name = malloc(datalen + 1);
833 e033d803 2018-04-23 stsp if (te->name == NULL) {
834 e033d803 2018-04-23 stsp free(te);
835 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
836 e033d803 2018-04-23 stsp break;
837 e033d803 2018-04-23 stsp }
838 c0588d8d 2018-11-07 stsp memcpy(te->name, imsg.data + sizeof(*ite), datalen);
839 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
840 e033d803 2018-04-23 stsp
841 c0588d8d 2018-11-07 stsp memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
842 c0588d8d 2018-11-07 stsp te->mode = ite->mode;
843 883f0469 2018-06-23 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
844 e033d803 2018-04-23 stsp nentries++;
845 e033d803 2018-04-23 stsp break;
846 e033d803 2018-04-23 stsp default:
847 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
848 e033d803 2018-04-23 stsp break;
849 e033d803 2018-04-23 stsp }
850 e033d803 2018-04-23 stsp
851 e033d803 2018-04-23 stsp imsg_free(&imsg);
852 e033d803 2018-04-23 stsp }
853 1e51f5b9 2018-04-23 stsp done:
854 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries) {
855 1e51f5b9 2018-04-23 stsp if (err == NULL)
856 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
857 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
858 e033d803 2018-04-23 stsp *tree = NULL;
859 ff6b18f8 2018-04-24 stsp }
860 ff6b18f8 2018-04-24 stsp
861 ff6b18f8 2018-04-24 stsp return err;
862 ff6b18f8 2018-04-24 stsp }
863 ff6b18f8 2018-04-24 stsp
864 ff6b18f8 2018-04-24 stsp const struct got_error *
865 ac544f8c 2019-01-13 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
866 ac544f8c 2019-01-13 stsp const uint8_t *data)
867 ff6b18f8 2018-04-24 stsp {
868 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
869 2967a784 2018-04-24 stsp
870 2967a784 2018-04-24 stsp iblob.size = size;
871 ebc55e2d 2018-12-24 stsp iblob.hdrlen = hdrlen;
872 2967a784 2018-04-24 stsp
873 ac544f8c 2019-01-13 stsp if (data) {
874 ac544f8c 2019-01-13 stsp uint8_t *buf;
875 ac544f8c 2019-01-13 stsp
876 ac544f8c 2019-01-13 stsp if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
877 ac544f8c 2019-01-13 stsp return got_error(GOT_ERR_NO_SPACE);
878 ac544f8c 2019-01-13 stsp
879 ac544f8c 2019-01-13 stsp buf = malloc(sizeof(iblob) + size);
880 ac544f8c 2019-01-13 stsp if (buf == NULL)
881 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
882 ff6b18f8 2018-04-24 stsp
883 ac544f8c 2019-01-13 stsp memcpy(buf, &iblob, sizeof(iblob));
884 ac544f8c 2019-01-13 stsp memcpy(buf + sizeof(iblob), data, size);
885 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
886 ac544f8c 2019-01-13 stsp sizeof(iblob) + size) == -1) {
887 ac544f8c 2019-01-13 stsp free(buf);
888 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
889 ac544f8c 2019-01-13 stsp }
890 ac544f8c 2019-01-13 stsp free(buf);
891 ac544f8c 2019-01-13 stsp } else {
892 ac544f8c 2019-01-13 stsp /* Data has already been written to file descriptor. */
893 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
894 ac544f8c 2019-01-13 stsp sizeof(iblob)) == -1)
895 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
896 ac544f8c 2019-01-13 stsp }
897 ac544f8c 2019-01-13 stsp
898 ac544f8c 2019-01-13 stsp
899 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
900 ff6b18f8 2018-04-24 stsp }
901 ff6b18f8 2018-04-24 stsp
902 ff6b18f8 2018-04-24 stsp const struct got_error *
903 ac544f8c 2019-01-13 stsp got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
904 ac544f8c 2019-01-13 stsp struct imsgbuf *ibuf)
905 ff6b18f8 2018-04-24 stsp {
906 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
907 ff6b18f8 2018-04-24 stsp struct imsg imsg;
908 291624d8 2018-11-07 stsp struct got_imsg_blob *iblob;
909 ff6b18f8 2018-04-24 stsp size_t datalen;
910 ff6b18f8 2018-04-24 stsp
911 ac544f8c 2019-01-13 stsp *outbuf = NULL;
912 ac544f8c 2019-01-13 stsp
913 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
914 ff6b18f8 2018-04-24 stsp if (err)
915 ff6b18f8 2018-04-24 stsp return err;
916 ff6b18f8 2018-04-24 stsp
917 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
918 ff6b18f8 2018-04-24 stsp
919 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
920 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
921 ac544f8c 2019-01-13 stsp if (datalen < sizeof(*iblob)) {
922 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
923 18336eed 2018-11-04 stsp break;
924 18336eed 2018-11-04 stsp }
925 291624d8 2018-11-07 stsp iblob = imsg.data;
926 291624d8 2018-11-07 stsp *size = iblob->size;
927 ebc55e2d 2018-12-24 stsp *hdrlen = iblob->hdrlen;
928 ac544f8c 2019-01-13 stsp
929 ac544f8c 2019-01-13 stsp if (datalen == sizeof(*iblob)) {
930 ac544f8c 2019-01-13 stsp /* Data has been written to file descriptor. */
931 ac544f8c 2019-01-13 stsp break;
932 ac544f8c 2019-01-13 stsp }
933 ac544f8c 2019-01-13 stsp
934 ac544f8c 2019-01-13 stsp if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
935 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
936 ac544f8c 2019-01-13 stsp break;
937 ac544f8c 2019-01-13 stsp }
938 ac544f8c 2019-01-13 stsp
939 ac544f8c 2019-01-13 stsp *outbuf = malloc(*size);
940 ac544f8c 2019-01-13 stsp if (*outbuf == NULL) {
941 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
942 ac544f8c 2019-01-13 stsp break;
943 ac544f8c 2019-01-13 stsp }
944 ac544f8c 2019-01-13 stsp memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
945 f4a881ce 2018-11-17 stsp break;
946 f4a881ce 2018-11-17 stsp default:
947 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
948 f4a881ce 2018-11-17 stsp break;
949 f4a881ce 2018-11-17 stsp }
950 f4a881ce 2018-11-17 stsp
951 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
952 f4a881ce 2018-11-17 stsp
953 f4a881ce 2018-11-17 stsp return err;
954 f4a881ce 2018-11-17 stsp }
955 f4a881ce 2018-11-17 stsp
956 f4a881ce 2018-11-17 stsp static const struct got_error *
957 f4a881ce 2018-11-17 stsp send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
958 f4a881ce 2018-11-17 stsp {
959 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
960 f4a881ce 2018-11-17 stsp size_t offset, remain;
961 f4a881ce 2018-11-17 stsp
962 f4a881ce 2018-11-17 stsp offset = 0;
963 f4a881ce 2018-11-17 stsp remain = tagmsg_len;
964 f4a881ce 2018-11-17 stsp while (remain > 0) {
965 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
966 f4a881ce 2018-11-17 stsp
967 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
968 f4a881ce 2018-11-17 stsp tag->tagmsg + offset, n) == -1) {
969 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG_TAGMSG");
970 f4a881ce 2018-11-17 stsp break;
971 f4a881ce 2018-11-17 stsp }
972 f4a881ce 2018-11-17 stsp
973 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
974 f4a881ce 2018-11-17 stsp if (err)
975 f4a881ce 2018-11-17 stsp break;
976 f4a881ce 2018-11-17 stsp
977 f4a881ce 2018-11-17 stsp offset += n;
978 f4a881ce 2018-11-17 stsp remain -= n;
979 f4a881ce 2018-11-17 stsp }
980 f4a881ce 2018-11-17 stsp
981 f4a881ce 2018-11-17 stsp return err;
982 f4a881ce 2018-11-17 stsp }
983 f4a881ce 2018-11-17 stsp
984 f4a881ce 2018-11-17 stsp const struct got_error *
985 f4a881ce 2018-11-17 stsp got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
986 f4a881ce 2018-11-17 stsp {
987 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
988 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
989 f4a881ce 2018-11-17 stsp uint8_t *buf;
990 f4a881ce 2018-11-17 stsp size_t len, total;
991 f4a881ce 2018-11-17 stsp size_t tag_len = strlen(tag->tag);
992 f4a881ce 2018-11-17 stsp size_t tagger_len = strlen(tag->tagger);
993 f4a881ce 2018-11-17 stsp size_t tagmsg_len = strlen(tag->tagmsg);
994 f4a881ce 2018-11-17 stsp
995 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
996 f4a881ce 2018-11-17 stsp
997 f4a881ce 2018-11-17 stsp buf = malloc(total);
998 f4a881ce 2018-11-17 stsp if (buf == NULL)
999 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1000 f4a881ce 2018-11-17 stsp
1001 f4a881ce 2018-11-17 stsp itag = (struct got_imsg_tag_object *)buf;
1002 f4a881ce 2018-11-17 stsp memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1003 f4a881ce 2018-11-17 stsp itag->obj_type = tag->obj_type;
1004 f4a881ce 2018-11-17 stsp itag->tag_len = tag_len;
1005 f4a881ce 2018-11-17 stsp itag->tagger_len = tagger_len;
1006 f4a881ce 2018-11-17 stsp itag->tagger_time = tag->tagger_time;
1007 f4a881ce 2018-11-17 stsp itag->tagger_gmtoff = tag->tagger_gmtoff;
1008 f4a881ce 2018-11-17 stsp itag->tagmsg_len = tagmsg_len;
1009 f4a881ce 2018-11-17 stsp
1010 f4a881ce 2018-11-17 stsp len = sizeof(*itag);
1011 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tag, tag_len);
1012 f4a881ce 2018-11-17 stsp len += tag_len;
1013 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tagger, tagger_len);
1014 f4a881ce 2018-11-17 stsp len += tagger_len;
1015 f4a881ce 2018-11-17 stsp
1016 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1017 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG");
1018 f4a881ce 2018-11-17 stsp goto done;
1019 f4a881ce 2018-11-17 stsp }
1020 f4a881ce 2018-11-17 stsp
1021 f4a881ce 2018-11-17 stsp if (tagmsg_len == 0 ||
1022 f4a881ce 2018-11-17 stsp tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1023 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1024 f4a881ce 2018-11-17 stsp if (err)
1025 f4a881ce 2018-11-17 stsp goto done;
1026 f4a881ce 2018-11-17 stsp }
1027 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
1028 f4a881ce 2018-11-17 stsp done:
1029 f4a881ce 2018-11-17 stsp free(buf);
1030 f4a881ce 2018-11-17 stsp return err;
1031 f4a881ce 2018-11-17 stsp }
1032 f4a881ce 2018-11-17 stsp
1033 f4a881ce 2018-11-17 stsp const struct got_error *
1034 f4a881ce 2018-11-17 stsp got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1035 f4a881ce 2018-11-17 stsp {
1036 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1037 f4a881ce 2018-11-17 stsp struct imsg imsg;
1038 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1039 f4a881ce 2018-11-17 stsp size_t len, datalen;
1040 f4a881ce 2018-11-17 stsp const size_t min_datalen =
1041 f4a881ce 2018-11-17 stsp MIN(sizeof(struct got_imsg_error),
1042 f4a881ce 2018-11-17 stsp sizeof(struct got_imsg_tag_object));
1043 f4a881ce 2018-11-17 stsp
1044 f4a881ce 2018-11-17 stsp *tag = NULL;
1045 f4a881ce 2018-11-17 stsp
1046 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1047 f4a881ce 2018-11-17 stsp if (err)
1048 f4a881ce 2018-11-17 stsp return err;
1049 f4a881ce 2018-11-17 stsp
1050 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1051 f4a881ce 2018-11-17 stsp len = 0;
1052 f4a881ce 2018-11-17 stsp
1053 f4a881ce 2018-11-17 stsp switch (imsg.hdr.type) {
1054 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG:
1055 f4a881ce 2018-11-17 stsp if (datalen < sizeof(*itag)) {
1056 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1057 f4a881ce 2018-11-17 stsp break;
1058 f4a881ce 2018-11-17 stsp }
1059 f4a881ce 2018-11-17 stsp itag = imsg.data;
1060 f4a881ce 2018-11-17 stsp if (datalen != sizeof(*itag) + itag->tag_len +
1061 f4a881ce 2018-11-17 stsp itag->tagger_len) {
1062 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1063 f4a881ce 2018-11-17 stsp break;
1064 f4a881ce 2018-11-17 stsp }
1065 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
1066 f4a881ce 2018-11-17 stsp
1067 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
1068 f4a881ce 2018-11-17 stsp if (*tag == NULL) {
1069 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1070 f4a881ce 2018-11-17 stsp break;
1071 f4a881ce 2018-11-17 stsp }
1072 f4a881ce 2018-11-17 stsp
1073 f4a881ce 2018-11-17 stsp memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1074 f4a881ce 2018-11-17 stsp
1075 f4a881ce 2018-11-17 stsp if (itag->tag_len == 0) {
1076 f4a881ce 2018-11-17 stsp (*tag)->tag = strdup("");
1077 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1078 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1079 f4a881ce 2018-11-17 stsp break;
1080 f4a881ce 2018-11-17 stsp }
1081 f4a881ce 2018-11-17 stsp } else {
1082 f4a881ce 2018-11-17 stsp (*tag)->tag = malloc(itag->tag_len + 1);
1083 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1084 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1085 f4a881ce 2018-11-17 stsp break;
1086 f4a881ce 2018-11-17 stsp }
1087 f4a881ce 2018-11-17 stsp memcpy((*tag)->tag, imsg.data + len,
1088 f4a881ce 2018-11-17 stsp itag->tag_len);
1089 f4a881ce 2018-11-17 stsp (*tag)->tag[itag->tag_len] = '\0';
1090 f4a881ce 2018-11-17 stsp }
1091 f4a881ce 2018-11-17 stsp len += itag->tag_len;
1092 f4a881ce 2018-11-17 stsp
1093 f4a881ce 2018-11-17 stsp (*tag)->obj_type = itag->obj_type;
1094 f4a881ce 2018-11-17 stsp (*tag)->tagger_time = itag->tagger_time;
1095 f4a881ce 2018-11-17 stsp (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1096 f4a881ce 2018-11-17 stsp
1097 f4a881ce 2018-11-17 stsp if (itag->tagger_len == 0) {
1098 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup("");
1099 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1100 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1101 f4a881ce 2018-11-17 stsp break;
1102 f4a881ce 2018-11-17 stsp }
1103 f4a881ce 2018-11-17 stsp } else {
1104 f4a881ce 2018-11-17 stsp (*tag)->tagger = malloc(itag->tagger_len + 1);
1105 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1106 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1107 f4a881ce 2018-11-17 stsp break;
1108 f4a881ce 2018-11-17 stsp }
1109 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagger, imsg.data + len,
1110 f4a881ce 2018-11-17 stsp itag->tagger_len);
1111 f4a881ce 2018-11-17 stsp (*tag)->tagger[itag->tagger_len] = '\0';
1112 f4a881ce 2018-11-17 stsp }
1113 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1114 f4a881ce 2018-11-17 stsp
1115 f4a881ce 2018-11-17 stsp if (itag->tagmsg_len == 0) {
1116 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strdup("");
1117 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1118 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1119 f4a881ce 2018-11-17 stsp break;
1120 f4a881ce 2018-11-17 stsp }
1121 f4a881ce 2018-11-17 stsp } else {
1122 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1123 f4a881ce 2018-11-17 stsp
1124 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1125 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1126 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1127 f4a881ce 2018-11-17 stsp break;
1128 f4a881ce 2018-11-17 stsp }
1129 f4a881ce 2018-11-17 stsp while (remain > 0) {
1130 f4a881ce 2018-11-17 stsp struct imsg imsg_log;
1131 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1132 f4a881ce 2018-11-17 stsp remain);
1133 f4a881ce 2018-11-17 stsp
1134 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1135 f4a881ce 2018-11-17 stsp if (err)
1136 f4a881ce 2018-11-17 stsp return err;
1137 f4a881ce 2018-11-17 stsp
1138 f4a881ce 2018-11-17 stsp if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1139 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1140 f4a881ce 2018-11-17 stsp
1141 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1142 f4a881ce 2018-11-17 stsp n);
1143 f4a881ce 2018-11-17 stsp imsg_free(&imsg_log);
1144 f4a881ce 2018-11-17 stsp offset += n;
1145 f4a881ce 2018-11-17 stsp remain -= n;
1146 f4a881ce 2018-11-17 stsp }
1147 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1148 f4a881ce 2018-11-17 stsp }
1149 f4a881ce 2018-11-17 stsp
1150 ff6b18f8 2018-04-24 stsp break;
1151 ff6b18f8 2018-04-24 stsp default:
1152 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1153 ff6b18f8 2018-04-24 stsp break;
1154 e033d803 2018-04-23 stsp }
1155 e033d803 2018-04-23 stsp
1156 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1157 ff6b18f8 2018-04-24 stsp
1158 2178c42e 2018-04-22 stsp return err;
1159 2178c42e 2018-04-22 stsp }
1160 876c234b 2018-09-10 stsp
1161 876c234b 2018-09-10 stsp const struct got_error *
1162 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1163 876c234b 2018-09-10 stsp struct got_packidx *packidx)
1164 876c234b 2018-09-10 stsp {
1165 41496140 2019-02-21 stsp const struct got_error *err = NULL;
1166 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1167 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1168 876c234b 2018-09-10 stsp int fd;
1169 876c234b 2018-09-10 stsp
1170 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
1171 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
1172 876c234b 2018-09-10 stsp if (fd == -1)
1173 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1174 876c234b 2018-09-10 stsp
1175 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1176 41496140 2019-02-21 stsp sizeof(ipackidx)) == -1) {
1177 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACKIDX");
1178 41496140 2019-02-21 stsp close(fd);
1179 41496140 2019-02-21 stsp return err;
1180 41496140 2019-02-21 stsp }
1181 876c234b 2018-09-10 stsp
1182 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
1183 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1184 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
1185 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
1186 876c234b 2018-09-10 stsp
1187 876c234b 2018-09-10 stsp fd = dup(pack->fd);
1188 876c234b 2018-09-10 stsp if (fd == -1)
1189 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1190 876c234b 2018-09-10 stsp
1191 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1192 41496140 2019-02-21 stsp == -1) {
1193 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACK");
1194 41496140 2019-02-21 stsp close(fd);
1195 41496140 2019-02-21 stsp return err;
1196 41496140 2019-02-21 stsp }
1197 876c234b 2018-09-10 stsp
1198 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1199 876c234b 2018-09-10 stsp }
1200 876c234b 2018-09-10 stsp
1201 876c234b 2018-09-10 stsp const struct got_error *
1202 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1203 106807b4 2018-09-15 stsp struct got_object_id *id)
1204 876c234b 2018-09-10 stsp {
1205 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
1206 876c234b 2018-09-10 stsp
1207 876c234b 2018-09-10 stsp iobj.idx = idx;
1208 106807b4 2018-09-15 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1209 876c234b 2018-09-10 stsp
1210 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1211 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
1212 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose "
1213 230a42bd 2019-05-11 jcs "PACKED_OBJECT_REQUEST");
1214 876c234b 2018-09-10 stsp
1215 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1216 63219cd2 2019-01-04 stsp }
1217 63219cd2 2019-01-04 stsp
1218 63219cd2 2019-01-04 stsp const struct got_error *
1219 63219cd2 2019-01-04 stsp got_privsep_unveil_exec_helpers(void)
1220 63219cd2 2019-01-04 stsp {
1221 c39c25dd 2019-08-09 stsp const char *helpers[] = {
1222 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_PACK,
1223 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_OBJECT,
1224 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_COMMIT,
1225 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TREE,
1226 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_BLOB,
1227 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TAG,
1228 c39c25dd 2019-08-09 stsp };
1229 c39c25dd 2019-08-09 stsp int i;
1230 63219cd2 2019-01-04 stsp
1231 c39c25dd 2019-08-09 stsp for (i = 0; i < nitems(helpers); i++) {
1232 c39c25dd 2019-08-09 stsp if (unveil(helpers[i], "x") == 0)
1233 c39c25dd 2019-08-09 stsp continue;
1234 c39c25dd 2019-08-09 stsp return got_error_from_errno2("unveil", helpers[i]);
1235 c39c25dd 2019-08-09 stsp }
1236 c39c25dd 2019-08-09 stsp
1237 63219cd2 2019-01-04 stsp return NULL;
1238 876c234b 2018-09-10 stsp }