Blame


1 2178c42e 2018-04-22 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
4 2178c42e 2018-04-22 stsp *
5 2178c42e 2018-04-22 stsp * Permission to use, copy, modify, and distribute this software for any
6 2178c42e 2018-04-22 stsp * purpose with or without fee is hereby granted, provided that the above
7 2178c42e 2018-04-22 stsp * copyright notice and this permission notice appear in all copies.
8 2178c42e 2018-04-22 stsp *
9 2178c42e 2018-04-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 2178c42e 2018-04-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 2178c42e 2018-04-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 2178c42e 2018-04-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 2178c42e 2018-04-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 2178c42e 2018-04-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 2178c42e 2018-04-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 2178c42e 2018-04-22 stsp */
17 2178c42e 2018-04-22 stsp
18 2178c42e 2018-04-22 stsp #include <sys/types.h>
19 2178c42e 2018-04-22 stsp #include <sys/queue.h>
20 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
22 876c234b 2018-09-10 stsp #include <sys/wait.h>
23 2178c42e 2018-04-22 stsp
24 2178c42e 2018-04-22 stsp #include <stdio.h>
25 2178c42e 2018-04-22 stsp #include <stdlib.h>
26 2178c42e 2018-04-22 stsp #include <string.h>
27 2178c42e 2018-04-22 stsp #include <errno.h>
28 2178c42e 2018-04-22 stsp #include <stdint.h>
29 2178c42e 2018-04-22 stsp #include <poll.h>
30 2178c42e 2018-04-22 stsp #include <imsg.h>
31 2178c42e 2018-04-22 stsp #include <sha1.h>
32 2178c42e 2018-04-22 stsp #include <zlib.h>
33 788c352e 2018-06-16 stsp #include <time.h>
34 2178c42e 2018-04-22 stsp
35 2178c42e 2018-04-22 stsp #include "got_object.h"
36 2178c42e 2018-04-22 stsp #include "got_error.h"
37 3022d272 2019-11-14 stsp #include "got_path.h"
38 cd95becd 2019-11-29 stsp #include "got_repository.h"
39 2178c42e 2018-04-22 stsp
40 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
41 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
42 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
43 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
44 a440fac0 2018-09-06 stsp #include "got_lib_object_parse.h"
45 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
46 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
47 2178c42e 2018-04-22 stsp
48 2178c42e 2018-04-22 stsp #ifndef MIN
49 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 c39c25dd 2019-08-09 stsp #endif
51 c39c25dd 2019-08-09 stsp
52 c39c25dd 2019-08-09 stsp #ifndef nitems
53 c39c25dd 2019-08-09 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 2178c42e 2018-04-22 stsp #endif
55 2178c42e 2018-04-22 stsp
56 2178c42e 2018-04-22 stsp static const struct got_error *
57 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
58 2178c42e 2018-04-22 stsp {
59 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
60 2178c42e 2018-04-22 stsp int n;
61 2178c42e 2018-04-22 stsp
62 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
63 2178c42e 2018-04-22 stsp pfd[0].events = events;
64 2178c42e 2018-04-22 stsp
65 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
66 2178c42e 2018-04-22 stsp if (n == -1)
67 638f9024 2019-05-13 stsp return got_error_from_errno("poll");
68 2178c42e 2018-04-22 stsp if (n == 0)
69 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
70 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
71 638f9024 2019-05-13 stsp return got_error_from_errno("poll error");
72 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
73 2178c42e 2018-04-22 stsp return NULL;
74 2178c42e 2018-04-22 stsp
75 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
76 2178c42e 2018-04-22 stsp }
77 2178c42e 2018-04-22 stsp
78 c4eae628 2018-04-23 stsp static const struct got_error *
79 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
80 fe36cf76 2018-04-23 stsp {
81 fe36cf76 2018-04-23 stsp const struct got_error *err;
82 e033d803 2018-04-23 stsp size_t n;
83 fe36cf76 2018-04-23 stsp
84 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
85 fe36cf76 2018-04-23 stsp if (err)
86 fe36cf76 2018-04-23 stsp return err;
87 fe36cf76 2018-04-23 stsp
88 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
89 fe36cf76 2018-04-23 stsp if (n == -1) {
90 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
91 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
92 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
93 fe36cf76 2018-04-23 stsp }
94 fe36cf76 2018-04-23 stsp if (n == 0)
95 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
96 fe36cf76 2018-04-23 stsp
97 e033d803 2018-04-23 stsp return NULL;
98 e033d803 2018-04-23 stsp }
99 e033d803 2018-04-23 stsp
100 ad242220 2018-09-08 stsp const struct got_error *
101 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
102 876c234b 2018-09-10 stsp {
103 876c234b 2018-09-10 stsp int child_status;
104 876c234b 2018-09-10 stsp
105 2cb49fa8 2019-05-10 stsp if (waitpid(pid, &child_status, 0) == -1)
106 638f9024 2019-05-13 stsp return got_error_from_errno("waitpid");
107 876c234b 2018-09-10 stsp
108 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
109 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
110 876c234b 2018-09-10 stsp
111 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
112 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
113 876c234b 2018-09-10 stsp
114 876c234b 2018-09-10 stsp return NULL;
115 876c234b 2018-09-10 stsp }
116 876c234b 2018-09-10 stsp
117 73b7854a 2018-11-11 stsp static const struct got_error *
118 73b7854a 2018-11-11 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
119 73b7854a 2018-11-11 stsp {
120 73b7854a 2018-11-11 stsp struct got_imsg_error *ierr;
121 73b7854a 2018-11-11 stsp
122 73b7854a 2018-11-11 stsp if (datalen != sizeof(*ierr))
123 73b7854a 2018-11-11 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
124 73b7854a 2018-11-11 stsp
125 73b7854a 2018-11-11 stsp ierr = imsg->data;
126 73b7854a 2018-11-11 stsp if (ierr->code == GOT_ERR_ERRNO) {
127 73b7854a 2018-11-11 stsp static struct got_error serr;
128 73b7854a 2018-11-11 stsp serr.code = GOT_ERR_ERRNO;
129 73b7854a 2018-11-11 stsp serr.msg = strerror(ierr->errno_code);
130 73b7854a 2018-11-11 stsp return &serr;
131 73b7854a 2018-11-11 stsp }
132 73b7854a 2018-11-11 stsp
133 73b7854a 2018-11-11 stsp return got_error(ierr->code);
134 73b7854a 2018-11-11 stsp }
135 73b7854a 2018-11-11 stsp
136 876c234b 2018-09-10 stsp const struct got_error *
137 46de5bfd 2018-11-11 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
138 46de5bfd 2018-11-11 stsp size_t min_datalen)
139 e033d803 2018-04-23 stsp {
140 e033d803 2018-04-23 stsp const struct got_error *err;
141 e033d803 2018-04-23 stsp ssize_t n;
142 e033d803 2018-04-23 stsp
143 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
144 876c234b 2018-09-10 stsp if (n == -1)
145 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_get");
146 876c234b 2018-09-10 stsp
147 876c234b 2018-09-10 stsp while (n == 0) {
148 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
149 876c234b 2018-09-10 stsp if (err)
150 876c234b 2018-09-10 stsp return err;
151 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
152 cbfaaf20 2020-01-06 stsp if (n == -1)
153 cbfaaf20 2020-01-06 stsp return got_error_from_errno("imsg_get");
154 876c234b 2018-09-10 stsp }
155 fe36cf76 2018-04-23 stsp
156 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
157 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
158 c4eae628 2018-04-23 stsp
159 73b7854a 2018-11-11 stsp if (imsg->hdr.type == GOT_IMSG_ERROR) {
160 73b7854a 2018-11-11 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
161 73b7854a 2018-11-11 stsp return recv_imsg_error(imsg, datalen);
162 c4eae628 2018-04-23 stsp }
163 c4eae628 2018-04-23 stsp
164 73b7854a 2018-11-11 stsp return NULL;
165 c4eae628 2018-04-23 stsp }
166 c4eae628 2018-04-23 stsp
167 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
168 2178c42e 2018-04-22 stsp void
169 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
170 2178c42e 2018-04-22 stsp {
171 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
172 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
173 2178c42e 2018-04-22 stsp int ret;
174 2178c42e 2018-04-22 stsp
175 2178c42e 2018-04-22 stsp ierr.code = err->code;
176 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
177 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
178 2178c42e 2018-04-22 stsp else
179 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
180 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
181 e93cd828 2018-11-11 stsp if (ret == -1) {
182 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
183 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
184 5d43e84d 2018-04-23 stsp return;
185 2178c42e 2018-04-22 stsp }
186 2178c42e 2018-04-22 stsp
187 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
188 5d43e84d 2018-04-23 stsp if (poll_err) {
189 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
190 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
191 5d43e84d 2018-04-23 stsp return;
192 5d43e84d 2018-04-23 stsp }
193 2178c42e 2018-04-22 stsp
194 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
195 5d43e84d 2018-04-23 stsp if (ret == -1) {
196 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
197 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
198 5d43e84d 2018-04-23 stsp return;
199 5d43e84d 2018-04-23 stsp }
200 e033d803 2018-04-23 stsp }
201 e033d803 2018-04-23 stsp
202 e033d803 2018-04-23 stsp static const struct got_error *
203 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
204 e033d803 2018-04-23 stsp {
205 e033d803 2018-04-23 stsp const struct got_error *err;
206 e033d803 2018-04-23 stsp
207 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
208 e033d803 2018-04-23 stsp if (err)
209 e033d803 2018-04-23 stsp return err;
210 e033d803 2018-04-23 stsp
211 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
212 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_flush");
213 e033d803 2018-04-23 stsp
214 e033d803 2018-04-23 stsp return NULL;
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_stop(int fd)
219 ad242220 2018-09-08 stsp {
220 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
221 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
222 ad242220 2018-09-08 stsp
223 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
224 ad242220 2018-09-08 stsp
225 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
226 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose STOP");
227 ad242220 2018-09-08 stsp
228 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
229 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
230 ad242220 2018-09-08 stsp return err;
231 7762fe12 2018-11-05 stsp }
232 93658fb9 2020-03-18 stsp
233 93658fb9 2020-03-18 stsp const struct got_error *
234 aea5f015 2018-12-24 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
235 ad242220 2018-09-08 stsp {
236 aea5f015 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
237 aea5f015 2018-12-24 stsp == -1)
238 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT_REQUEST");
239 1785f84a 2018-12-23 stsp
240 1785f84a 2018-12-23 stsp return flush_imsg(ibuf);
241 1785f84a 2018-12-23 stsp }
242 1785f84a 2018-12-23 stsp
243 1785f84a 2018-12-23 stsp const struct got_error *
244 1785f84a 2018-12-23 stsp got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
245 1785f84a 2018-12-23 stsp struct got_object_id *id, int pack_idx)
246 1785f84a 2018-12-23 stsp {
247 41496140 2019-02-21 stsp const struct got_error *err = NULL;
248 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj, *iobjp;
249 1785f84a 2018-12-23 stsp size_t len;
250 1785f84a 2018-12-23 stsp
251 1785f84a 2018-12-23 stsp if (id) { /* commit is packed */
252 1785f84a 2018-12-23 stsp iobj.idx = pack_idx;
253 1785f84a 2018-12-23 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
254 1785f84a 2018-12-23 stsp iobjp = &iobj;
255 1785f84a 2018-12-23 stsp len = sizeof(iobj);
256 1785f84a 2018-12-23 stsp } else {
257 1785f84a 2018-12-23 stsp iobjp = NULL;
258 1785f84a 2018-12-23 stsp len = 0;
259 1785f84a 2018-12-23 stsp }
260 1785f84a 2018-12-23 stsp
261 1785f84a 2018-12-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
262 41496140 2019-02-21 stsp == -1) {
263 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
264 41496140 2019-02-21 stsp close(fd);
265 41496140 2019-02-21 stsp return err;
266 41496140 2019-02-21 stsp }
267 13c729f7 2018-12-24 stsp
268 13c729f7 2018-12-24 stsp return flush_imsg(ibuf);
269 13c729f7 2018-12-24 stsp }
270 13c729f7 2018-12-24 stsp
271 13c729f7 2018-12-24 stsp const struct got_error *
272 13c729f7 2018-12-24 stsp got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
273 13c729f7 2018-12-24 stsp struct got_object_id *id, int pack_idx)
274 13c729f7 2018-12-24 stsp {
275 41496140 2019-02-21 stsp const struct got_error *err = NULL;
276 7f358e3b 2019-11-23 stsp struct ibuf *wbuf;
277 7f358e3b 2019-11-23 stsp size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
278 7f358e3b 2019-11-23 stsp
279 7f358e3b 2019-11-23 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
280 7f358e3b 2019-11-23 stsp if (wbuf == NULL)
281 7f358e3b 2019-11-23 stsp return got_error_from_errno("imsg_create TREE_REQUEST");
282 13c729f7 2018-12-24 stsp
283 13c729f7 2018-12-24 stsp if (id) { /* tree is packed */
284 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
285 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
286 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
287 7f358e3b 2019-11-23 stsp return err;
288 7f358e3b 2019-11-23 stsp }
289 13c729f7 2018-12-24 stsp
290 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
291 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
292 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
293 7f358e3b 2019-11-23 stsp return err;
294 7f358e3b 2019-11-23 stsp }
295 41496140 2019-02-21 stsp }
296 268f7291 2018-12-24 stsp
297 7f358e3b 2019-11-23 stsp wbuf->fd = fd;
298 7f358e3b 2019-11-23 stsp imsg_close(ibuf, wbuf);
299 7f358e3b 2019-11-23 stsp
300 268f7291 2018-12-24 stsp return flush_imsg(ibuf);
301 268f7291 2018-12-24 stsp }
302 268f7291 2018-12-24 stsp
303 268f7291 2018-12-24 stsp const struct got_error *
304 268f7291 2018-12-24 stsp got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
305 268f7291 2018-12-24 stsp struct got_object_id *id, int pack_idx)
306 268f7291 2018-12-24 stsp {
307 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
308 268f7291 2018-12-24 stsp size_t len;
309 268f7291 2018-12-24 stsp
310 268f7291 2018-12-24 stsp if (id) { /* tag is packed */
311 268f7291 2018-12-24 stsp iobj.idx = pack_idx;
312 268f7291 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
313 268f7291 2018-12-24 stsp iobjp = &iobj;
314 268f7291 2018-12-24 stsp len = sizeof(iobj);
315 268f7291 2018-12-24 stsp } else {
316 268f7291 2018-12-24 stsp iobjp = NULL;
317 268f7291 2018-12-24 stsp len = 0;
318 268f7291 2018-12-24 stsp }
319 268f7291 2018-12-24 stsp
320 268f7291 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
321 1785f84a 2018-12-23 stsp == -1)
322 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TAG_REQUEST");
323 7762fe12 2018-11-05 stsp
324 7762fe12 2018-11-05 stsp return flush_imsg(ibuf);
325 7762fe12 2018-11-05 stsp }
326 7762fe12 2018-11-05 stsp
327 7762fe12 2018-11-05 stsp const struct got_error *
328 ebc55e2d 2018-12-24 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
329 ebc55e2d 2018-12-24 stsp struct got_object_id *id, int pack_idx)
330 55da3778 2018-09-10 stsp {
331 41496140 2019-02-21 stsp const struct got_error *err = NULL;
332 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
333 ebc55e2d 2018-12-24 stsp size_t len;
334 ebc55e2d 2018-12-24 stsp
335 ebc55e2d 2018-12-24 stsp if (id) { /* blob is packed */
336 ebc55e2d 2018-12-24 stsp iobj.idx = pack_idx;
337 ebc55e2d 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
338 ebc55e2d 2018-12-24 stsp iobjp = &iobj;
339 ebc55e2d 2018-12-24 stsp len = sizeof(iobj);
340 ebc55e2d 2018-12-24 stsp } else {
341 ebc55e2d 2018-12-24 stsp iobjp = NULL;
342 ebc55e2d 2018-12-24 stsp len = 0;
343 ebc55e2d 2018-12-24 stsp }
344 ebc55e2d 2018-12-24 stsp
345 ebc55e2d 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
346 41496140 2019-02-21 stsp == -1) {
347 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_REQUEST");
348 41496140 2019-02-21 stsp close(infd);
349 41496140 2019-02-21 stsp return err;
350 41496140 2019-02-21 stsp }
351 ad242220 2018-09-08 stsp
352 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
353 55da3778 2018-09-10 stsp }
354 ad242220 2018-09-08 stsp
355 55da3778 2018-09-10 stsp const struct got_error *
356 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
357 55da3778 2018-09-10 stsp {
358 41496140 2019-02-21 stsp const struct got_error *err = NULL;
359 41496140 2019-02-21 stsp
360 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
361 41496140 2019-02-21 stsp == -1) {
362 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_OUTFD");
363 41496140 2019-02-21 stsp close(outfd);
364 41496140 2019-02-21 stsp return err;
365 41496140 2019-02-21 stsp }
366 3840f4c9 2018-09-12 stsp
367 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
368 3840f4c9 2018-09-12 stsp }
369 3840f4c9 2018-09-12 stsp
370 3840f4c9 2018-09-12 stsp const struct got_error *
371 3840f4c9 2018-09-12 stsp got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
372 3840f4c9 2018-09-12 stsp {
373 41496140 2019-02-21 stsp const struct got_error *err = NULL;
374 41496140 2019-02-21 stsp
375 3840f4c9 2018-09-12 stsp if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
376 41496140 2019-02-21 stsp == -1) {
377 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TMPFD");
378 41496140 2019-02-21 stsp close(fd);
379 41496140 2019-02-21 stsp return err;
380 41496140 2019-02-21 stsp }
381 ad242220 2018-09-08 stsp
382 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
383 ad242220 2018-09-08 stsp }
384 ad242220 2018-09-08 stsp
385 ad242220 2018-09-08 stsp const struct got_error *
386 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
387 2178c42e 2018-04-22 stsp {
388 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
389 2178c42e 2018-04-22 stsp
390 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
391 2178c42e 2018-04-22 stsp iobj.type = obj->type;
392 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
393 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
394 2178c42e 2018-04-22 stsp iobj.size = obj->size;
395 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
396 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
397 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
398 c59b3346 2018-09-11 stsp }
399 2178c42e 2018-04-22 stsp
400 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
401 2178c42e 2018-04-22 stsp == -1)
402 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT");
403 2178c42e 2018-04-22 stsp
404 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
405 cfd633c2 2018-09-10 stsp }
406 cfd633c2 2018-09-10 stsp
407 cfd633c2 2018-09-10 stsp const struct got_error *
408 93658fb9 2020-03-18 stsp got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd)
409 93658fb9 2020-03-18 stsp {
410 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
411 93658fb9 2020-03-18 stsp
412 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
413 93658fb9 2020-03-18 stsp NULL, 0) == -1) {
414 93658fb9 2020-03-18 stsp err = got_error_from_errno("imsg_compose FETCH_REQUEST");
415 93658fb9 2020-03-18 stsp close(fd);
416 93658fb9 2020-03-18 stsp return err;
417 93658fb9 2020-03-18 stsp }
418 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
419 93658fb9 2020-03-18 stsp }
420 93658fb9 2020-03-18 stsp
421 93658fb9 2020-03-18 stsp const struct got_error *
422 b9f99abf 2020-03-18 stsp got_privsep_send_fetch_progress(struct imsgbuf *ibuf,
423 b9f99abf 2020-03-18 stsp struct got_object_id *refid, const char *refname)
424 b9f99abf 2020-03-18 stsp {
425 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
426 b9f99abf 2020-03-18 stsp struct ibuf *wbuf;
427 b9f99abf 2020-03-18 stsp size_t len, reflen = strlen(refname);
428 b9f99abf 2020-03-18 stsp
429 b9f99abf 2020-03-18 stsp len = sizeof(struct got_imsg_fetch_progress) + reflen;
430 b9f99abf 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
431 b9f99abf 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
432 b9f99abf 2020-03-18 stsp
433 b9f99abf 2020-03-18 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_PROGRESS, 0, 0, len);
434 b9f99abf 2020-03-18 stsp if (wbuf == NULL)
435 b9f99abf 2020-03-18 stsp return got_error_from_errno("imsg_create FETCH_PROGRESS");
436 b9f99abf 2020-03-18 stsp
437 b9f99abf 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_progress definition! */
438 b9f99abf 2020-03-18 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
439 b9f99abf 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_PROGRESS");
440 b9f99abf 2020-03-18 stsp ibuf_free(wbuf);
441 b9f99abf 2020-03-18 stsp return err;
442 b9f99abf 2020-03-18 stsp }
443 b9f99abf 2020-03-18 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
444 b9f99abf 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_PROGRESS");
445 b9f99abf 2020-03-18 stsp ibuf_free(wbuf);
446 b9f99abf 2020-03-18 stsp return err;
447 b9f99abf 2020-03-18 stsp }
448 b9f99abf 2020-03-18 stsp
449 b9f99abf 2020-03-18 stsp wbuf->fd = -1;
450 b9f99abf 2020-03-18 stsp imsg_close(ibuf, wbuf);
451 b9f99abf 2020-03-18 stsp return flush_imsg(ibuf);
452 b9f99abf 2020-03-18 stsp }
453 b9f99abf 2020-03-18 stsp
454 b9f99abf 2020-03-18 stsp const struct got_error *
455 93658fb9 2020-03-18 stsp got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
456 93658fb9 2020-03-18 stsp {
457 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
458 93658fb9 2020-03-18 stsp hash.sha1, SHA1_DIGEST_LENGTH) == -1)
459 93658fb9 2020-03-18 stsp return got_error_from_errno("imsg_compose FETCH");
460 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
461 93658fb9 2020-03-18 stsp }
462 93658fb9 2020-03-18 stsp
463 b9f99abf 2020-03-18 stsp
464 93658fb9 2020-03-18 stsp const struct got_error *
465 8f2d01a6 2020-03-18 stsp got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
466 b9f99abf 2020-03-18 stsp char **refname, struct imsgbuf *ibuf)
467 b9f99abf 2020-03-18 stsp {
468 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
469 b9f99abf 2020-03-18 stsp struct imsg imsg;
470 b9f99abf 2020-03-18 stsp size_t datalen;
471 b9f99abf 2020-03-18 stsp const size_t min_datalen =
472 b9f99abf 2020-03-18 stsp MIN(sizeof(struct got_imsg_error),
473 b9f99abf 2020-03-18 stsp sizeof(struct got_imsg_fetch_progress));
474 b9f99abf 2020-03-18 stsp
475 8f2d01a6 2020-03-18 stsp *done = 0;
476 8f2d01a6 2020-03-18 stsp *id = NULL;
477 b9f99abf 2020-03-18 stsp *refname = NULL;
478 b9f99abf 2020-03-18 stsp
479 b9f99abf 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
480 b9f99abf 2020-03-18 stsp if (err)
481 b9f99abf 2020-03-18 stsp return err;
482 b9f99abf 2020-03-18 stsp
483 b9f99abf 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
484 b9f99abf 2020-03-18 stsp switch (imsg.hdr.type) {
485 b9f99abf 2020-03-18 stsp case GOT_IMSG_ERROR:
486 b9f99abf 2020-03-18 stsp err = recv_imsg_error(&imsg, datalen);
487 b9f99abf 2020-03-18 stsp break;
488 b9f99abf 2020-03-18 stsp case GOT_IMSG_FETCH_PROGRESS:
489 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
490 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
491 b9f99abf 2020-03-18 stsp err = got_error_from_errno("malloc");
492 b9f99abf 2020-03-18 stsp break;
493 b9f99abf 2020-03-18 stsp }
494 8f2d01a6 2020-03-18 stsp memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
495 b9f99abf 2020-03-18 stsp if (datalen <= SHA1_DIGEST_LENGTH) {
496 b9f99abf 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
497 b9f99abf 2020-03-18 stsp break;
498 b9f99abf 2020-03-18 stsp }
499 b9f99abf 2020-03-18 stsp *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
500 b9f99abf 2020-03-18 stsp datalen - SHA1_DIGEST_LENGTH);
501 b9f99abf 2020-03-18 stsp if (*refname == NULL) {
502 b9f99abf 2020-03-18 stsp err = got_error_from_errno("strndup");
503 8f2d01a6 2020-03-18 stsp break;
504 8f2d01a6 2020-03-18 stsp }
505 8f2d01a6 2020-03-18 stsp break;
506 8f2d01a6 2020-03-18 stsp case GOT_IMSG_FETCH_DONE:
507 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
508 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
509 8f2d01a6 2020-03-18 stsp err = got_error_from_errno("malloc");
510 b9f99abf 2020-03-18 stsp break;
511 b9f99abf 2020-03-18 stsp }
512 8f2d01a6 2020-03-18 stsp if (datalen != SHA1_DIGEST_LENGTH) {
513 8f2d01a6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
514 8f2d01a6 2020-03-18 stsp break;
515 8f2d01a6 2020-03-18 stsp }
516 8f2d01a6 2020-03-18 stsp memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
517 8f2d01a6 2020-03-18 stsp *done = 1;
518 b9f99abf 2020-03-18 stsp break;
519 b9f99abf 2020-03-18 stsp default:
520 b887aab6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
521 b887aab6 2020-03-18 stsp break;
522 b9f99abf 2020-03-18 stsp }
523 b9f99abf 2020-03-18 stsp
524 b887aab6 2020-03-18 stsp if (err) {
525 8f2d01a6 2020-03-18 stsp free(*id);
526 8f2d01a6 2020-03-18 stsp *id = NULL;
527 b887aab6 2020-03-18 stsp free(*refname);
528 b887aab6 2020-03-18 stsp *refname = NULL;
529 b887aab6 2020-03-18 stsp }
530 b9f99abf 2020-03-18 stsp imsg_free(&imsg);
531 b9f99abf 2020-03-18 stsp return err;
532 93658fb9 2020-03-18 stsp }
533 93658fb9 2020-03-18 stsp
534 93658fb9 2020-03-18 stsp const struct got_error *
535 279090e1 2020-03-18 stsp got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id *hash)
536 93658fb9 2020-03-18 stsp {
537 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
538 93658fb9 2020-03-18 stsp
539 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
540 279090e1 2020-03-18 stsp hash->sha1, SHA1_DIGEST_LENGTH) == -1) {
541 93658fb9 2020-03-18 stsp err = got_error_from_errno("imsg_compose INDEX_REQUEST");
542 93658fb9 2020-03-18 stsp close(fd);
543 93658fb9 2020-03-18 stsp return err;
544 93658fb9 2020-03-18 stsp }
545 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
546 93658fb9 2020-03-18 stsp }
547 93658fb9 2020-03-18 stsp
548 93658fb9 2020-03-18 stsp const struct got_error *
549 93658fb9 2020-03-18 stsp got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
550 93658fb9 2020-03-18 stsp {
551 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
552 93658fb9 2020-03-18 stsp return got_error_from_errno("imsg_compose FETCH");
553 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
554 93658fb9 2020-03-18 stsp }
555 93658fb9 2020-03-18 stsp
556 93658fb9 2020-03-18 stsp const struct got_error *
557 93658fb9 2020-03-18 stsp got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
558 93658fb9 2020-03-18 stsp {
559 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
560 93658fb9 2020-03-18 stsp struct imsg imsg;
561 93658fb9 2020-03-18 stsp
562 93658fb9 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
563 93658fb9 2020-03-18 stsp if (err)
564 93658fb9 2020-03-18 stsp return err;
565 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
566 93658fb9 2020-03-18 stsp return NULL;
567 93658fb9 2020-03-18 stsp else
568 93658fb9 2020-03-18 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
569 93658fb9 2020-03-18 stsp imsg_free(&imsg);
570 93658fb9 2020-03-18 stsp }
571 93658fb9 2020-03-18 stsp
572 93658fb9 2020-03-18 stsp const struct got_error *
573 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
574 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
575 cfd633c2 2018-09-10 stsp {
576 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
577 291624d8 2018-11-07 stsp struct got_imsg_object *iobj;
578 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
579 cfd633c2 2018-09-10 stsp
580 291624d8 2018-11-07 stsp if (datalen != sizeof(*iobj))
581 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
582 291624d8 2018-11-07 stsp iobj = imsg->data;
583 cfd633c2 2018-09-10 stsp
584 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
585 cfd633c2 2018-09-10 stsp if (*obj == NULL)
586 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
587 cfd633c2 2018-09-10 stsp
588 291624d8 2018-11-07 stsp memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
589 291624d8 2018-11-07 stsp (*obj)->type = iobj->type;
590 291624d8 2018-11-07 stsp (*obj)->flags = iobj->flags;
591 291624d8 2018-11-07 stsp (*obj)->hdrlen = iobj->hdrlen;
592 291624d8 2018-11-07 stsp (*obj)->size = iobj->size;
593 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
594 291624d8 2018-11-07 stsp if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
595 291624d8 2018-11-07 stsp (*obj)->pack_offset = iobj->pack_offset;
596 291624d8 2018-11-07 stsp (*obj)->pack_idx = iobj->pack_idx;
597 876c234b 2018-09-10 stsp }
598 876c234b 2018-09-10 stsp
599 876c234b 2018-09-10 stsp return err;
600 876c234b 2018-09-10 stsp }
601 876c234b 2018-09-10 stsp
602 2178c42e 2018-04-22 stsp const struct got_error *
603 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
604 2178c42e 2018-04-22 stsp {
605 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
606 2178c42e 2018-04-22 stsp struct imsg imsg;
607 c4eae628 2018-04-23 stsp const size_t min_datalen =
608 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
609 2178c42e 2018-04-22 stsp
610 2178c42e 2018-04-22 stsp *obj = NULL;
611 2178c42e 2018-04-22 stsp
612 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
613 2178c42e 2018-04-22 stsp if (err)
614 2178c42e 2018-04-22 stsp return err;
615 2178c42e 2018-04-22 stsp
616 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
617 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
618 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
619 bff6ca00 2018-04-23 stsp break;
620 bff6ca00 2018-04-23 stsp default:
621 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
622 bff6ca00 2018-04-23 stsp break;
623 bff6ca00 2018-04-23 stsp }
624 bff6ca00 2018-04-23 stsp
625 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
626 bff6ca00 2018-04-23 stsp
627 bff6ca00 2018-04-23 stsp return err;
628 c75f7264 2018-09-11 stsp }
629 c75f7264 2018-09-11 stsp
630 c75f7264 2018-09-11 stsp static const struct got_error *
631 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
632 c75f7264 2018-09-11 stsp size_t logmsg_len)
633 c75f7264 2018-09-11 stsp {
634 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
635 c75f7264 2018-09-11 stsp size_t offset, remain;
636 c75f7264 2018-09-11 stsp
637 c75f7264 2018-09-11 stsp offset = 0;
638 c75f7264 2018-09-11 stsp remain = logmsg_len;
639 c75f7264 2018-09-11 stsp while (remain > 0) {
640 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
641 c75f7264 2018-09-11 stsp
642 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
643 fa4ffeb3 2018-11-04 stsp commit->logmsg + offset, n) == -1) {
644 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose "
645 230a42bd 2019-05-11 jcs "COMMIT_LOGMSG");
646 fa4ffeb3 2018-11-04 stsp break;
647 fa4ffeb3 2018-11-04 stsp }
648 c75f7264 2018-09-11 stsp
649 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
650 fa4ffeb3 2018-11-04 stsp if (err)
651 fa4ffeb3 2018-11-04 stsp break;
652 c75f7264 2018-09-11 stsp
653 c75f7264 2018-09-11 stsp offset += n;
654 c75f7264 2018-09-11 stsp remain -= n;
655 c75f7264 2018-09-11 stsp }
656 c75f7264 2018-09-11 stsp
657 fa4ffeb3 2018-11-04 stsp return err;
658 bff6ca00 2018-04-23 stsp }
659 bff6ca00 2018-04-23 stsp
660 bff6ca00 2018-04-23 stsp const struct got_error *
661 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
662 bff6ca00 2018-04-23 stsp {
663 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
664 b9c33926 2018-11-07 stsp struct got_imsg_commit_object *icommit;
665 bff6ca00 2018-04-23 stsp uint8_t *buf;
666 bff6ca00 2018-04-23 stsp size_t len, total;
667 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
668 b9c33926 2018-11-07 stsp size_t author_len = strlen(commit->author);
669 b9c33926 2018-11-07 stsp size_t committer_len = strlen(commit->committer);
670 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
671 bff6ca00 2018-04-23 stsp
672 b9c33926 2018-11-07 stsp total = sizeof(*icommit) + author_len + committer_len +
673 b9c33926 2018-11-07 stsp commit->nparents * SHA1_DIGEST_LENGTH;
674 bff6ca00 2018-04-23 stsp
675 bff6ca00 2018-04-23 stsp buf = malloc(total);
676 bff6ca00 2018-04-23 stsp if (buf == NULL)
677 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
678 bff6ca00 2018-04-23 stsp
679 b9c33926 2018-11-07 stsp icommit = (struct got_imsg_commit_object *)buf;
680 a7403916 2018-12-24 stsp memcpy(icommit->tree_id, commit->tree_id->sha1,
681 a7403916 2018-12-24 stsp sizeof(icommit->tree_id));
682 b9c33926 2018-11-07 stsp icommit->author_len = author_len;
683 b9c33926 2018-11-07 stsp icommit->author_time = commit->author_time;
684 b9c33926 2018-11-07 stsp icommit->author_gmtoff = commit->author_gmtoff;
685 b9c33926 2018-11-07 stsp icommit->committer_len = committer_len;
686 b9c33926 2018-11-07 stsp icommit->committer_time = commit->committer_time;
687 b9c33926 2018-11-07 stsp icommit->committer_gmtoff = commit->committer_gmtoff;
688 b9c33926 2018-11-07 stsp icommit->logmsg_len = logmsg_len;
689 b9c33926 2018-11-07 stsp icommit->nparents = commit->nparents;
690 b9c33926 2018-11-07 stsp
691 b9c33926 2018-11-07 stsp len = sizeof(*icommit);
692 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->author, author_len);
693 b9c33926 2018-11-07 stsp len += author_len;
694 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->committer, committer_len);
695 b9c33926 2018-11-07 stsp len += committer_len;
696 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
697 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
698 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
699 bff6ca00 2018-04-23 stsp }
700 bff6ca00 2018-04-23 stsp
701 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
702 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT");
703 bff6ca00 2018-04-23 stsp goto done;
704 bff6ca00 2018-04-23 stsp }
705 bff6ca00 2018-04-23 stsp
706 904df868 2018-11-04 stsp if (logmsg_len == 0 ||
707 904df868 2018-11-04 stsp logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
708 904df868 2018-11-04 stsp err = flush_imsg(ibuf);
709 904df868 2018-11-04 stsp if (err)
710 904df868 2018-11-04 stsp goto done;
711 904df868 2018-11-04 stsp }
712 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
713 bff6ca00 2018-04-23 stsp done:
714 bff6ca00 2018-04-23 stsp free(buf);
715 bff6ca00 2018-04-23 stsp return err;
716 bff6ca00 2018-04-23 stsp }
717 cfd633c2 2018-09-10 stsp
718 ca6e02ac 2020-01-07 stsp static const struct got_error *
719 ca6e02ac 2020-01-07 stsp get_commit_from_imsg(struct got_commit_object **commit,
720 ca6e02ac 2020-01-07 stsp struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
721 bff6ca00 2018-04-23 stsp {
722 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
723 291624d8 2018-11-07 stsp struct got_imsg_commit_object *icommit;
724 ca6e02ac 2020-01-07 stsp size_t len = 0;
725 bff6ca00 2018-04-23 stsp int i;
726 bff6ca00 2018-04-23 stsp
727 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(*icommit))
728 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
729 bff6ca00 2018-04-23 stsp
730 ca6e02ac 2020-01-07 stsp icommit = imsg->data;
731 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommit) + icommit->author_len +
732 ca6e02ac 2020-01-07 stsp icommit->committer_len +
733 ca6e02ac 2020-01-07 stsp icommit->nparents * SHA1_DIGEST_LENGTH)
734 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
735 bff6ca00 2018-04-23 stsp
736 ca6e02ac 2020-01-07 stsp if (icommit->nparents < 0)
737 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
738 ca6e02ac 2020-01-07 stsp
739 ca6e02ac 2020-01-07 stsp len += sizeof(*icommit);
740 bff6ca00 2018-04-23 stsp
741 ca6e02ac 2020-01-07 stsp *commit = got_object_commit_alloc_partial();
742 ca6e02ac 2020-01-07 stsp if (*commit == NULL)
743 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
744 ca6e02ac 2020-01-07 stsp "got_object_commit_alloc_partial");
745 ca6e02ac 2020-01-07 stsp
746 ca6e02ac 2020-01-07 stsp memcpy((*commit)->tree_id->sha1, icommit->tree_id,
747 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
748 ca6e02ac 2020-01-07 stsp (*commit)->author_time = icommit->author_time;
749 ca6e02ac 2020-01-07 stsp (*commit)->author_gmtoff = icommit->author_gmtoff;
750 ca6e02ac 2020-01-07 stsp (*commit)->committer_time = icommit->committer_time;
751 ca6e02ac 2020-01-07 stsp (*commit)->committer_gmtoff = icommit->committer_gmtoff;
752 ca6e02ac 2020-01-07 stsp
753 ca6e02ac 2020-01-07 stsp if (icommit->author_len == 0) {
754 ca6e02ac 2020-01-07 stsp (*commit)->author = strdup("");
755 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
756 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
757 ca6e02ac 2020-01-07 stsp goto done;
758 bff6ca00 2018-04-23 stsp }
759 ca6e02ac 2020-01-07 stsp } else {
760 ca6e02ac 2020-01-07 stsp (*commit)->author = malloc(icommit->author_len + 1);
761 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
762 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
763 ca6e02ac 2020-01-07 stsp goto done;
764 ca6e02ac 2020-01-07 stsp }
765 ca6e02ac 2020-01-07 stsp memcpy((*commit)->author, imsg->data + len,
766 ca6e02ac 2020-01-07 stsp icommit->author_len);
767 ca6e02ac 2020-01-07 stsp (*commit)->author[icommit->author_len] = '\0';
768 ca6e02ac 2020-01-07 stsp }
769 ca6e02ac 2020-01-07 stsp len += icommit->author_len;
770 bff6ca00 2018-04-23 stsp
771 ca6e02ac 2020-01-07 stsp if (icommit->committer_len == 0) {
772 ca6e02ac 2020-01-07 stsp (*commit)->committer = strdup("");
773 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
774 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
775 ca6e02ac 2020-01-07 stsp goto done;
776 ca6e02ac 2020-01-07 stsp }
777 ca6e02ac 2020-01-07 stsp } else {
778 ca6e02ac 2020-01-07 stsp (*commit)->committer =
779 ca6e02ac 2020-01-07 stsp malloc(icommit->committer_len + 1);
780 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
781 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
782 ca6e02ac 2020-01-07 stsp goto done;
783 ca6e02ac 2020-01-07 stsp }
784 ca6e02ac 2020-01-07 stsp memcpy((*commit)->committer, imsg->data + len,
785 ca6e02ac 2020-01-07 stsp icommit->committer_len);
786 ca6e02ac 2020-01-07 stsp (*commit)->committer[icommit->committer_len] = '\0';
787 ca6e02ac 2020-01-07 stsp }
788 ca6e02ac 2020-01-07 stsp len += icommit->committer_len;
789 ca6e02ac 2020-01-07 stsp
790 ca6e02ac 2020-01-07 stsp if (icommit->logmsg_len == 0) {
791 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = strdup("");
792 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
793 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
794 ca6e02ac 2020-01-07 stsp goto done;
795 ca6e02ac 2020-01-07 stsp }
796 ca6e02ac 2020-01-07 stsp } else {
797 ca6e02ac 2020-01-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
798 ca6e02ac 2020-01-07 stsp
799 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
800 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
801 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
802 ca6e02ac 2020-01-07 stsp goto done;
803 bff6ca00 2018-04-23 stsp }
804 ca6e02ac 2020-01-07 stsp while (remain > 0) {
805 ca6e02ac 2020-01-07 stsp struct imsg imsg_log;
806 ca6e02ac 2020-01-07 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
807 ca6e02ac 2020-01-07 stsp remain);
808 6c281f94 2018-06-11 stsp
809 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
810 ca6e02ac 2020-01-07 stsp if (err)
811 ca6e02ac 2020-01-07 stsp goto done;
812 bff6ca00 2018-04-23 stsp
813 ca6e02ac 2020-01-07 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
814 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
815 ca6e02ac 2020-01-07 stsp goto done;
816 bff6ca00 2018-04-23 stsp }
817 c75f7264 2018-09-11 stsp
818 ca6e02ac 2020-01-07 stsp memcpy((*commit)->logmsg + offset,
819 ca6e02ac 2020-01-07 stsp imsg_log.data, n);
820 ca6e02ac 2020-01-07 stsp imsg_free(&imsg_log);
821 ca6e02ac 2020-01-07 stsp offset += n;
822 ca6e02ac 2020-01-07 stsp remain -= n;
823 bff6ca00 2018-04-23 stsp }
824 ca6e02ac 2020-01-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
825 ca6e02ac 2020-01-07 stsp }
826 bff6ca00 2018-04-23 stsp
827 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommit->nparents; i++) {
828 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
829 86acc566 2018-04-23 stsp
830 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
831 ca6e02ac 2020-01-07 stsp if (err)
832 ca6e02ac 2020-01-07 stsp break;
833 ca6e02ac 2020-01-07 stsp memcpy(qid->id, imsg->data + len +
834 ca6e02ac 2020-01-07 stsp i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
835 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
836 ca6e02ac 2020-01-07 stsp (*commit)->nparents++;
837 ca6e02ac 2020-01-07 stsp }
838 ca6e02ac 2020-01-07 stsp done:
839 ca6e02ac 2020-01-07 stsp if (err) {
840 ca6e02ac 2020-01-07 stsp got_object_commit_close(*commit);
841 ca6e02ac 2020-01-07 stsp *commit = NULL;
842 ca6e02ac 2020-01-07 stsp }
843 ca6e02ac 2020-01-07 stsp return err;
844 ca6e02ac 2020-01-07 stsp }
845 ca6e02ac 2020-01-07 stsp
846 ca6e02ac 2020-01-07 stsp const struct got_error *
847 ca6e02ac 2020-01-07 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
848 ca6e02ac 2020-01-07 stsp {
849 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
850 ca6e02ac 2020-01-07 stsp struct imsg imsg;
851 ca6e02ac 2020-01-07 stsp size_t datalen;
852 ca6e02ac 2020-01-07 stsp const size_t min_datalen =
853 ca6e02ac 2020-01-07 stsp MIN(sizeof(struct got_imsg_error),
854 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_object));
855 ca6e02ac 2020-01-07 stsp
856 ca6e02ac 2020-01-07 stsp *commit = NULL;
857 ca6e02ac 2020-01-07 stsp
858 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
859 ca6e02ac 2020-01-07 stsp if (err)
860 ca6e02ac 2020-01-07 stsp return err;
861 ca6e02ac 2020-01-07 stsp
862 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
863 ca6e02ac 2020-01-07 stsp
864 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
865 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
866 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
867 2178c42e 2018-04-22 stsp break;
868 8c580685 2018-04-22 stsp default:
869 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
870 8c580685 2018-04-22 stsp break;
871 2178c42e 2018-04-22 stsp }
872 2178c42e 2018-04-22 stsp
873 2178c42e 2018-04-22 stsp imsg_free(&imsg);
874 e033d803 2018-04-23 stsp
875 e033d803 2018-04-23 stsp return err;
876 e033d803 2018-04-23 stsp }
877 e033d803 2018-04-23 stsp
878 e033d803 2018-04-23 stsp const struct got_error *
879 3022d272 2019-11-14 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
880 3022d272 2019-11-14 stsp int nentries)
881 e033d803 2018-04-23 stsp {
882 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
883 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
884 3022d272 2019-11-14 stsp struct got_pathlist_entry *pe;
885 b00c9821 2018-11-04 stsp size_t totlen;
886 6eb07a17 2018-11-04 stsp int nimsg; /* number of imsg queued in ibuf */
887 e033d803 2018-04-23 stsp
888 3022d272 2019-11-14 stsp itree.nentries = nentries;
889 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
890 e033d803 2018-04-23 stsp == -1)
891 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TREE");
892 e033d803 2018-04-23 stsp
893 b00c9821 2018-11-04 stsp totlen = sizeof(itree);
894 6eb07a17 2018-11-04 stsp nimsg = 1;
895 3022d272 2019-11-14 stsp TAILQ_FOREACH(pe, entries, entry) {
896 3022d272 2019-11-14 stsp const char *name = pe->path;
897 3022d272 2019-11-14 stsp struct got_parsed_tree_entry *pte = pe->data;
898 3022d272 2019-11-14 stsp struct ibuf *wbuf;
899 3022d272 2019-11-14 stsp size_t namelen = strlen(name);
900 cd9e913a 2019-11-27 stsp size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
901 e033d803 2018-04-23 stsp
902 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
903 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
904 e033d803 2018-04-23 stsp
905 6eb07a17 2018-11-04 stsp nimsg++;
906 6eb07a17 2018-11-04 stsp if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
907 b00c9821 2018-11-04 stsp err = flush_imsg(ibuf);
908 b00c9821 2018-11-04 stsp if (err)
909 b00c9821 2018-11-04 stsp return err;
910 6eb07a17 2018-11-04 stsp nimsg = 0;
911 b00c9821 2018-11-04 stsp }
912 b00c9821 2018-11-04 stsp
913 3022d272 2019-11-14 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
914 3022d272 2019-11-14 stsp if (wbuf == NULL)
915 3022d272 2019-11-14 stsp return got_error_from_errno("imsg_create TREE_ENTRY");
916 e033d803 2018-04-23 stsp
917 3022d272 2019-11-14 stsp /* Keep in sync with struct got_imsg_tree_object definition! */
918 3b647085 2019-11-23 stsp if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
919 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
920 3b647085 2019-11-23 stsp ibuf_free(wbuf);
921 e033d803 2018-04-23 stsp return err;
922 3b647085 2019-11-23 stsp }
923 3b647085 2019-11-23 stsp if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
924 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
925 3b647085 2019-11-23 stsp ibuf_free(wbuf);
926 3022d272 2019-11-14 stsp return err;
927 3b647085 2019-11-23 stsp }
928 3022d272 2019-11-14 stsp
929 3b647085 2019-11-23 stsp if (imsg_add(wbuf, name, namelen) == -1) {
930 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
931 3b647085 2019-11-23 stsp ibuf_free(wbuf);
932 3022d272 2019-11-14 stsp return err;
933 3b647085 2019-11-23 stsp }
934 3022d272 2019-11-14 stsp
935 3022d272 2019-11-14 stsp wbuf->fd = -1;
936 3022d272 2019-11-14 stsp imsg_close(ibuf, wbuf);
937 3022d272 2019-11-14 stsp
938 b00c9821 2018-11-04 stsp totlen += len;
939 e033d803 2018-04-23 stsp }
940 e033d803 2018-04-23 stsp
941 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
942 e033d803 2018-04-23 stsp }
943 e033d803 2018-04-23 stsp
944 e033d803 2018-04-23 stsp const struct got_error *
945 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
946 e033d803 2018-04-23 stsp {
947 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
948 e033d803 2018-04-23 stsp const size_t min_datalen =
949 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
950 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
951 291624d8 2018-11-07 stsp struct got_imsg_tree_object *itree;
952 e033d803 2018-04-23 stsp int nentries = 0;
953 2178c42e 2018-04-22 stsp
954 e033d803 2018-04-23 stsp *tree = NULL;
955 e033d803 2018-04-23 stsp get_more:
956 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
957 e033d803 2018-04-23 stsp if (err)
958 1e51f5b9 2018-04-23 stsp goto done;
959 e033d803 2018-04-23 stsp
960 656b1f76 2019-05-11 jcs for (;;) {
961 e033d803 2018-04-23 stsp struct imsg imsg;
962 e033d803 2018-04-23 stsp size_t n;
963 e033d803 2018-04-23 stsp size_t datalen;
964 c0588d8d 2018-11-07 stsp struct got_imsg_tree_entry *ite;
965 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
966 e033d803 2018-04-23 stsp
967 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
968 e033d803 2018-04-23 stsp if (n == 0) {
969 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries)
970 e033d803 2018-04-23 stsp goto get_more;
971 e033d803 2018-04-23 stsp break;
972 e033d803 2018-04-23 stsp }
973 e033d803 2018-04-23 stsp
974 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
975 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
976 e033d803 2018-04-23 stsp
977 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
978 e033d803 2018-04-23 stsp
979 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
980 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
981 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
982 e033d803 2018-04-23 stsp break;
983 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
984 e033d803 2018-04-23 stsp /* This message should only appear once. */
985 e033d803 2018-04-23 stsp if (*tree != NULL) {
986 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
987 e033d803 2018-04-23 stsp break;
988 e033d803 2018-04-23 stsp }
989 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
990 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
991 e033d803 2018-04-23 stsp break;
992 e033d803 2018-04-23 stsp }
993 291624d8 2018-11-07 stsp itree = imsg.data;
994 c3b78ecc 2018-11-07 stsp *tree = malloc(sizeof(**tree));
995 e033d803 2018-04-23 stsp if (*tree == NULL) {
996 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
997 e033d803 2018-04-23 stsp break;
998 e033d803 2018-04-23 stsp }
999 56e0773d 2019-11-28 stsp (*tree)->entries = calloc(itree->nentries,
1000 56e0773d 2019-11-28 stsp sizeof(struct got_tree_entry));
1001 56e0773d 2019-11-28 stsp if ((*tree)->entries == NULL) {
1002 56e0773d 2019-11-28 stsp err = got_error_from_errno("malloc");
1003 56e0773d 2019-11-28 stsp break;
1004 56e0773d 2019-11-28 stsp }
1005 56e0773d 2019-11-28 stsp (*tree)->nentries = itree->nentries;
1006 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
1007 e033d803 2018-04-23 stsp break;
1008 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
1009 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
1010 e033d803 2018-04-23 stsp if (*tree == NULL) {
1011 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1012 e033d803 2018-04-23 stsp break;
1013 e033d803 2018-04-23 stsp }
1014 c0588d8d 2018-11-07 stsp if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1015 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1016 e033d803 2018-04-23 stsp break;
1017 e033d803 2018-04-23 stsp }
1018 e033d803 2018-04-23 stsp
1019 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
1020 c0588d8d 2018-11-07 stsp datalen -= sizeof(*ite);
1021 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1022 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1023 e033d803 2018-04-23 stsp break;
1024 e033d803 2018-04-23 stsp }
1025 c0588d8d 2018-11-07 stsp ite = imsg.data;
1026 052d4dc3 2018-04-23 stsp
1027 56e0773d 2019-11-28 stsp if (datalen + 1 > sizeof(te->name)) {
1028 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1029 e033d803 2018-04-23 stsp break;
1030 e033d803 2018-04-23 stsp }
1031 56e0773d 2019-11-28 stsp te = &(*tree)->entries[nentries];
1032 c0588d8d 2018-11-07 stsp memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1033 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
1034 e033d803 2018-04-23 stsp
1035 56e0773d 2019-11-28 stsp memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1036 c0588d8d 2018-11-07 stsp te->mode = ite->mode;
1037 56e0773d 2019-11-28 stsp te->idx = nentries;
1038 e033d803 2018-04-23 stsp nentries++;
1039 e033d803 2018-04-23 stsp break;
1040 e033d803 2018-04-23 stsp default:
1041 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1042 e033d803 2018-04-23 stsp break;
1043 e033d803 2018-04-23 stsp }
1044 e033d803 2018-04-23 stsp
1045 e033d803 2018-04-23 stsp imsg_free(&imsg);
1046 e033d803 2018-04-23 stsp }
1047 1e51f5b9 2018-04-23 stsp done:
1048 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries) {
1049 1e51f5b9 2018-04-23 stsp if (err == NULL)
1050 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1051 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
1052 e033d803 2018-04-23 stsp *tree = NULL;
1053 ff6b18f8 2018-04-24 stsp }
1054 ff6b18f8 2018-04-24 stsp
1055 ff6b18f8 2018-04-24 stsp return err;
1056 ff6b18f8 2018-04-24 stsp }
1057 ff6b18f8 2018-04-24 stsp
1058 ff6b18f8 2018-04-24 stsp const struct got_error *
1059 ac544f8c 2019-01-13 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1060 ac544f8c 2019-01-13 stsp const uint8_t *data)
1061 ff6b18f8 2018-04-24 stsp {
1062 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
1063 2967a784 2018-04-24 stsp
1064 2967a784 2018-04-24 stsp iblob.size = size;
1065 ebc55e2d 2018-12-24 stsp iblob.hdrlen = hdrlen;
1066 2967a784 2018-04-24 stsp
1067 ac544f8c 2019-01-13 stsp if (data) {
1068 ac544f8c 2019-01-13 stsp uint8_t *buf;
1069 ac544f8c 2019-01-13 stsp
1070 ac544f8c 2019-01-13 stsp if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1071 ac544f8c 2019-01-13 stsp return got_error(GOT_ERR_NO_SPACE);
1072 ac544f8c 2019-01-13 stsp
1073 ac544f8c 2019-01-13 stsp buf = malloc(sizeof(iblob) + size);
1074 ac544f8c 2019-01-13 stsp if (buf == NULL)
1075 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1076 ff6b18f8 2018-04-24 stsp
1077 ac544f8c 2019-01-13 stsp memcpy(buf, &iblob, sizeof(iblob));
1078 ac544f8c 2019-01-13 stsp memcpy(buf + sizeof(iblob), data, size);
1079 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1080 ac544f8c 2019-01-13 stsp sizeof(iblob) + size) == -1) {
1081 ac544f8c 2019-01-13 stsp free(buf);
1082 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1083 ac544f8c 2019-01-13 stsp }
1084 ac544f8c 2019-01-13 stsp free(buf);
1085 ac544f8c 2019-01-13 stsp } else {
1086 ac544f8c 2019-01-13 stsp /* Data has already been written to file descriptor. */
1087 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1088 ac544f8c 2019-01-13 stsp sizeof(iblob)) == -1)
1089 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1090 ac544f8c 2019-01-13 stsp }
1091 ac544f8c 2019-01-13 stsp
1092 ac544f8c 2019-01-13 stsp
1093 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
1094 ff6b18f8 2018-04-24 stsp }
1095 ff6b18f8 2018-04-24 stsp
1096 ff6b18f8 2018-04-24 stsp const struct got_error *
1097 ac544f8c 2019-01-13 stsp got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1098 ac544f8c 2019-01-13 stsp struct imsgbuf *ibuf)
1099 ff6b18f8 2018-04-24 stsp {
1100 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1101 ff6b18f8 2018-04-24 stsp struct imsg imsg;
1102 291624d8 2018-11-07 stsp struct got_imsg_blob *iblob;
1103 ff6b18f8 2018-04-24 stsp size_t datalen;
1104 ff6b18f8 2018-04-24 stsp
1105 ac544f8c 2019-01-13 stsp *outbuf = NULL;
1106 ac544f8c 2019-01-13 stsp
1107 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1108 ff6b18f8 2018-04-24 stsp if (err)
1109 ff6b18f8 2018-04-24 stsp return err;
1110 ff6b18f8 2018-04-24 stsp
1111 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1112 ff6b18f8 2018-04-24 stsp
1113 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
1114 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
1115 ac544f8c 2019-01-13 stsp if (datalen < sizeof(*iblob)) {
1116 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1117 18336eed 2018-11-04 stsp break;
1118 18336eed 2018-11-04 stsp }
1119 291624d8 2018-11-07 stsp iblob = imsg.data;
1120 291624d8 2018-11-07 stsp *size = iblob->size;
1121 ebc55e2d 2018-12-24 stsp *hdrlen = iblob->hdrlen;
1122 ac544f8c 2019-01-13 stsp
1123 ac544f8c 2019-01-13 stsp if (datalen == sizeof(*iblob)) {
1124 ac544f8c 2019-01-13 stsp /* Data has been written to file descriptor. */
1125 ac544f8c 2019-01-13 stsp break;
1126 ac544f8c 2019-01-13 stsp }
1127 ac544f8c 2019-01-13 stsp
1128 ac544f8c 2019-01-13 stsp if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1129 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1130 ac544f8c 2019-01-13 stsp break;
1131 ac544f8c 2019-01-13 stsp }
1132 ac544f8c 2019-01-13 stsp
1133 ac544f8c 2019-01-13 stsp *outbuf = malloc(*size);
1134 ac544f8c 2019-01-13 stsp if (*outbuf == NULL) {
1135 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1136 ac544f8c 2019-01-13 stsp break;
1137 ac544f8c 2019-01-13 stsp }
1138 ac544f8c 2019-01-13 stsp memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1139 f4a881ce 2018-11-17 stsp break;
1140 f4a881ce 2018-11-17 stsp default:
1141 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1142 f4a881ce 2018-11-17 stsp break;
1143 f4a881ce 2018-11-17 stsp }
1144 f4a881ce 2018-11-17 stsp
1145 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
1146 f4a881ce 2018-11-17 stsp
1147 f4a881ce 2018-11-17 stsp return err;
1148 f4a881ce 2018-11-17 stsp }
1149 f4a881ce 2018-11-17 stsp
1150 f4a881ce 2018-11-17 stsp static const struct got_error *
1151 f4a881ce 2018-11-17 stsp send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1152 f4a881ce 2018-11-17 stsp {
1153 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1154 f4a881ce 2018-11-17 stsp size_t offset, remain;
1155 f4a881ce 2018-11-17 stsp
1156 f4a881ce 2018-11-17 stsp offset = 0;
1157 f4a881ce 2018-11-17 stsp remain = tagmsg_len;
1158 f4a881ce 2018-11-17 stsp while (remain > 0) {
1159 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1160 f4a881ce 2018-11-17 stsp
1161 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1162 f4a881ce 2018-11-17 stsp tag->tagmsg + offset, n) == -1) {
1163 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1164 f4a881ce 2018-11-17 stsp break;
1165 f4a881ce 2018-11-17 stsp }
1166 f4a881ce 2018-11-17 stsp
1167 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1168 f4a881ce 2018-11-17 stsp if (err)
1169 f4a881ce 2018-11-17 stsp break;
1170 f4a881ce 2018-11-17 stsp
1171 f4a881ce 2018-11-17 stsp offset += n;
1172 f4a881ce 2018-11-17 stsp remain -= n;
1173 f4a881ce 2018-11-17 stsp }
1174 f4a881ce 2018-11-17 stsp
1175 f4a881ce 2018-11-17 stsp return err;
1176 f4a881ce 2018-11-17 stsp }
1177 f4a881ce 2018-11-17 stsp
1178 f4a881ce 2018-11-17 stsp const struct got_error *
1179 f4a881ce 2018-11-17 stsp got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1180 f4a881ce 2018-11-17 stsp {
1181 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1182 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1183 f4a881ce 2018-11-17 stsp uint8_t *buf;
1184 f4a881ce 2018-11-17 stsp size_t len, total;
1185 f4a881ce 2018-11-17 stsp size_t tag_len = strlen(tag->tag);
1186 f4a881ce 2018-11-17 stsp size_t tagger_len = strlen(tag->tagger);
1187 f4a881ce 2018-11-17 stsp size_t tagmsg_len = strlen(tag->tagmsg);
1188 f4a881ce 2018-11-17 stsp
1189 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1190 f4a881ce 2018-11-17 stsp
1191 f4a881ce 2018-11-17 stsp buf = malloc(total);
1192 f4a881ce 2018-11-17 stsp if (buf == NULL)
1193 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1194 f4a881ce 2018-11-17 stsp
1195 f4a881ce 2018-11-17 stsp itag = (struct got_imsg_tag_object *)buf;
1196 f4a881ce 2018-11-17 stsp memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1197 f4a881ce 2018-11-17 stsp itag->obj_type = tag->obj_type;
1198 f4a881ce 2018-11-17 stsp itag->tag_len = tag_len;
1199 f4a881ce 2018-11-17 stsp itag->tagger_len = tagger_len;
1200 f4a881ce 2018-11-17 stsp itag->tagger_time = tag->tagger_time;
1201 f4a881ce 2018-11-17 stsp itag->tagger_gmtoff = tag->tagger_gmtoff;
1202 f4a881ce 2018-11-17 stsp itag->tagmsg_len = tagmsg_len;
1203 f4a881ce 2018-11-17 stsp
1204 f4a881ce 2018-11-17 stsp len = sizeof(*itag);
1205 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tag, tag_len);
1206 f4a881ce 2018-11-17 stsp len += tag_len;
1207 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tagger, tagger_len);
1208 f4a881ce 2018-11-17 stsp len += tagger_len;
1209 f4a881ce 2018-11-17 stsp
1210 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1211 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG");
1212 f4a881ce 2018-11-17 stsp goto done;
1213 f4a881ce 2018-11-17 stsp }
1214 f4a881ce 2018-11-17 stsp
1215 f4a881ce 2018-11-17 stsp if (tagmsg_len == 0 ||
1216 f4a881ce 2018-11-17 stsp tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1217 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1218 f4a881ce 2018-11-17 stsp if (err)
1219 f4a881ce 2018-11-17 stsp goto done;
1220 f4a881ce 2018-11-17 stsp }
1221 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
1222 f4a881ce 2018-11-17 stsp done:
1223 f4a881ce 2018-11-17 stsp free(buf);
1224 f4a881ce 2018-11-17 stsp return err;
1225 f4a881ce 2018-11-17 stsp }
1226 f4a881ce 2018-11-17 stsp
1227 f4a881ce 2018-11-17 stsp const struct got_error *
1228 f4a881ce 2018-11-17 stsp got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1229 f4a881ce 2018-11-17 stsp {
1230 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1231 f4a881ce 2018-11-17 stsp struct imsg imsg;
1232 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1233 f4a881ce 2018-11-17 stsp size_t len, datalen;
1234 f4a881ce 2018-11-17 stsp const size_t min_datalen =
1235 f4a881ce 2018-11-17 stsp MIN(sizeof(struct got_imsg_error),
1236 f4a881ce 2018-11-17 stsp sizeof(struct got_imsg_tag_object));
1237 f4a881ce 2018-11-17 stsp
1238 f4a881ce 2018-11-17 stsp *tag = NULL;
1239 f4a881ce 2018-11-17 stsp
1240 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1241 f4a881ce 2018-11-17 stsp if (err)
1242 f4a881ce 2018-11-17 stsp return err;
1243 f4a881ce 2018-11-17 stsp
1244 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1245 f4a881ce 2018-11-17 stsp len = 0;
1246 f4a881ce 2018-11-17 stsp
1247 f4a881ce 2018-11-17 stsp switch (imsg.hdr.type) {
1248 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG:
1249 f4a881ce 2018-11-17 stsp if (datalen < sizeof(*itag)) {
1250 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1251 f4a881ce 2018-11-17 stsp break;
1252 f4a881ce 2018-11-17 stsp }
1253 f4a881ce 2018-11-17 stsp itag = imsg.data;
1254 f4a881ce 2018-11-17 stsp if (datalen != sizeof(*itag) + itag->tag_len +
1255 f4a881ce 2018-11-17 stsp itag->tagger_len) {
1256 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1257 f4a881ce 2018-11-17 stsp break;
1258 f4a881ce 2018-11-17 stsp }
1259 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
1260 f4a881ce 2018-11-17 stsp
1261 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
1262 f4a881ce 2018-11-17 stsp if (*tag == NULL) {
1263 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1264 f4a881ce 2018-11-17 stsp break;
1265 f4a881ce 2018-11-17 stsp }
1266 f4a881ce 2018-11-17 stsp
1267 f4a881ce 2018-11-17 stsp memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1268 f4a881ce 2018-11-17 stsp
1269 f4a881ce 2018-11-17 stsp if (itag->tag_len == 0) {
1270 f4a881ce 2018-11-17 stsp (*tag)->tag = strdup("");
1271 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1272 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1273 f4a881ce 2018-11-17 stsp break;
1274 f4a881ce 2018-11-17 stsp }
1275 f4a881ce 2018-11-17 stsp } else {
1276 f4a881ce 2018-11-17 stsp (*tag)->tag = malloc(itag->tag_len + 1);
1277 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1278 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1279 f4a881ce 2018-11-17 stsp break;
1280 f4a881ce 2018-11-17 stsp }
1281 f4a881ce 2018-11-17 stsp memcpy((*tag)->tag, imsg.data + len,
1282 f4a881ce 2018-11-17 stsp itag->tag_len);
1283 f4a881ce 2018-11-17 stsp (*tag)->tag[itag->tag_len] = '\0';
1284 f4a881ce 2018-11-17 stsp }
1285 f4a881ce 2018-11-17 stsp len += itag->tag_len;
1286 f4a881ce 2018-11-17 stsp
1287 f4a881ce 2018-11-17 stsp (*tag)->obj_type = itag->obj_type;
1288 f4a881ce 2018-11-17 stsp (*tag)->tagger_time = itag->tagger_time;
1289 f4a881ce 2018-11-17 stsp (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1290 f4a881ce 2018-11-17 stsp
1291 f4a881ce 2018-11-17 stsp if (itag->tagger_len == 0) {
1292 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup("");
1293 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1294 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1295 f4a881ce 2018-11-17 stsp break;
1296 f4a881ce 2018-11-17 stsp }
1297 f4a881ce 2018-11-17 stsp } else {
1298 f4a881ce 2018-11-17 stsp (*tag)->tagger = malloc(itag->tagger_len + 1);
1299 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1300 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1301 f4a881ce 2018-11-17 stsp break;
1302 f4a881ce 2018-11-17 stsp }
1303 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagger, imsg.data + len,
1304 f4a881ce 2018-11-17 stsp itag->tagger_len);
1305 f4a881ce 2018-11-17 stsp (*tag)->tagger[itag->tagger_len] = '\0';
1306 f4a881ce 2018-11-17 stsp }
1307 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1308 f4a881ce 2018-11-17 stsp
1309 f4a881ce 2018-11-17 stsp if (itag->tagmsg_len == 0) {
1310 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strdup("");
1311 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1312 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1313 f4a881ce 2018-11-17 stsp break;
1314 f4a881ce 2018-11-17 stsp }
1315 f4a881ce 2018-11-17 stsp } else {
1316 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1317 f4a881ce 2018-11-17 stsp
1318 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1319 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1320 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1321 f4a881ce 2018-11-17 stsp break;
1322 f4a881ce 2018-11-17 stsp }
1323 f4a881ce 2018-11-17 stsp while (remain > 0) {
1324 f4a881ce 2018-11-17 stsp struct imsg imsg_log;
1325 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1326 f4a881ce 2018-11-17 stsp remain);
1327 f4a881ce 2018-11-17 stsp
1328 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1329 f4a881ce 2018-11-17 stsp if (err)
1330 f4a881ce 2018-11-17 stsp return err;
1331 f4a881ce 2018-11-17 stsp
1332 f4a881ce 2018-11-17 stsp if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1333 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1334 f4a881ce 2018-11-17 stsp
1335 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1336 f4a881ce 2018-11-17 stsp n);
1337 f4a881ce 2018-11-17 stsp imsg_free(&imsg_log);
1338 f4a881ce 2018-11-17 stsp offset += n;
1339 f4a881ce 2018-11-17 stsp remain -= n;
1340 f4a881ce 2018-11-17 stsp }
1341 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1342 f4a881ce 2018-11-17 stsp }
1343 f4a881ce 2018-11-17 stsp
1344 ff6b18f8 2018-04-24 stsp break;
1345 ff6b18f8 2018-04-24 stsp default:
1346 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1347 ff6b18f8 2018-04-24 stsp break;
1348 e033d803 2018-04-23 stsp }
1349 e033d803 2018-04-23 stsp
1350 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1351 ff6b18f8 2018-04-24 stsp
1352 2178c42e 2018-04-22 stsp return err;
1353 2178c42e 2018-04-22 stsp }
1354 876c234b 2018-09-10 stsp
1355 876c234b 2018-09-10 stsp const struct got_error *
1356 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1357 876c234b 2018-09-10 stsp struct got_packidx *packidx)
1358 876c234b 2018-09-10 stsp {
1359 41496140 2019-02-21 stsp const struct got_error *err = NULL;
1360 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1361 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1362 876c234b 2018-09-10 stsp int fd;
1363 876c234b 2018-09-10 stsp
1364 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
1365 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
1366 876c234b 2018-09-10 stsp if (fd == -1)
1367 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1368 876c234b 2018-09-10 stsp
1369 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1370 41496140 2019-02-21 stsp sizeof(ipackidx)) == -1) {
1371 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACKIDX");
1372 41496140 2019-02-21 stsp close(fd);
1373 41496140 2019-02-21 stsp return err;
1374 41496140 2019-02-21 stsp }
1375 876c234b 2018-09-10 stsp
1376 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
1377 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1378 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
1379 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
1380 876c234b 2018-09-10 stsp
1381 876c234b 2018-09-10 stsp fd = dup(pack->fd);
1382 876c234b 2018-09-10 stsp if (fd == -1)
1383 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1384 876c234b 2018-09-10 stsp
1385 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1386 41496140 2019-02-21 stsp == -1) {
1387 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACK");
1388 41496140 2019-02-21 stsp close(fd);
1389 41496140 2019-02-21 stsp return err;
1390 41496140 2019-02-21 stsp }
1391 876c234b 2018-09-10 stsp
1392 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1393 876c234b 2018-09-10 stsp }
1394 876c234b 2018-09-10 stsp
1395 876c234b 2018-09-10 stsp const struct got_error *
1396 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1397 106807b4 2018-09-15 stsp struct got_object_id *id)
1398 876c234b 2018-09-10 stsp {
1399 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
1400 876c234b 2018-09-10 stsp
1401 876c234b 2018-09-10 stsp iobj.idx = idx;
1402 106807b4 2018-09-15 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1403 876c234b 2018-09-10 stsp
1404 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1405 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
1406 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose "
1407 230a42bd 2019-05-11 jcs "PACKED_OBJECT_REQUEST");
1408 aba9c984 2019-09-08 stsp
1409 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1410 aba9c984 2019-09-08 stsp }
1411 aba9c984 2019-09-08 stsp
1412 aba9c984 2019-09-08 stsp const struct got_error *
1413 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1414 aba9c984 2019-09-08 stsp {
1415 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1416 aba9c984 2019-09-08 stsp
1417 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1418 aba9c984 2019-09-08 stsp NULL, 0) == -1) {
1419 aba9c984 2019-09-08 stsp err = got_error_from_errno("imsg_compose "
1420 aba9c984 2019-09-08 stsp "GITCONFIG_PARSE_REQUEST");
1421 aba9c984 2019-09-08 stsp close(fd);
1422 aba9c984 2019-09-08 stsp return err;
1423 aba9c984 2019-09-08 stsp }
1424 aba9c984 2019-09-08 stsp
1425 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1426 aba9c984 2019-09-08 stsp }
1427 aba9c984 2019-09-08 stsp
1428 aba9c984 2019-09-08 stsp const struct got_error *
1429 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1430 aba9c984 2019-09-08 stsp {
1431 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1432 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1433 aba9c984 2019-09-08 stsp NULL, 0) == -1)
1434 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1435 aba9c984 2019-09-08 stsp "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1436 aba9c984 2019-09-08 stsp
1437 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1438 aba9c984 2019-09-08 stsp }
1439 aba9c984 2019-09-08 stsp
1440 aba9c984 2019-09-08 stsp const struct got_error *
1441 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1442 aba9c984 2019-09-08 stsp {
1443 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1444 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1445 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1446 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_NAME_REQUEST");
1447 aba9c984 2019-09-08 stsp
1448 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1449 aba9c984 2019-09-08 stsp }
1450 aba9c984 2019-09-08 stsp
1451 aba9c984 2019-09-08 stsp const struct got_error *
1452 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1453 aba9c984 2019-09-08 stsp {
1454 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1455 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1456 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1457 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1458 cd95becd 2019-11-29 stsp
1459 cd95becd 2019-11-29 stsp return flush_imsg(ibuf);
1460 cd95becd 2019-11-29 stsp }
1461 cd95becd 2019-11-29 stsp
1462 cd95becd 2019-11-29 stsp const struct got_error *
1463 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1464 cd95becd 2019-11-29 stsp {
1465 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf,
1466 cd95becd 2019-11-29 stsp GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1467 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose "
1468 cd95becd 2019-11-29 stsp "GITCONFIG_REMOTE_REQUEST");
1469 aba9c984 2019-09-08 stsp
1470 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1471 aba9c984 2019-09-08 stsp }
1472 aba9c984 2019-09-08 stsp
1473 aba9c984 2019-09-08 stsp const struct got_error *
1474 9a1cc63f 2020-02-03 stsp got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1475 9a1cc63f 2020-02-03 stsp {
1476 9a1cc63f 2020-02-03 stsp if (imsg_compose(ibuf,
1477 9a1cc63f 2020-02-03 stsp GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1478 9a1cc63f 2020-02-03 stsp return got_error_from_errno("imsg_compose "
1479 9a1cc63f 2020-02-03 stsp "GITCONFIG_OWNER_REQUEST");
1480 9a1cc63f 2020-02-03 stsp
1481 9a1cc63f 2020-02-03 stsp return flush_imsg(ibuf);
1482 9a1cc63f 2020-02-03 stsp }
1483 9a1cc63f 2020-02-03 stsp
1484 9a1cc63f 2020-02-03 stsp const struct got_error *
1485 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1486 aba9c984 2019-09-08 stsp {
1487 aba9c984 2019-09-08 stsp size_t len = value ? strlen(value) + 1 : 0;
1488 aba9c984 2019-09-08 stsp
1489 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1490 aba9c984 2019-09-08 stsp value, len) == -1)
1491 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1492 aba9c984 2019-09-08 stsp
1493 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1494 aba9c984 2019-09-08 stsp }
1495 aba9c984 2019-09-08 stsp
1496 aba9c984 2019-09-08 stsp const struct got_error *
1497 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1498 aba9c984 2019-09-08 stsp {
1499 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1500 aba9c984 2019-09-08 stsp struct imsg imsg;
1501 aba9c984 2019-09-08 stsp size_t datalen;
1502 aba9c984 2019-09-08 stsp const size_t min_datalen = 0;
1503 aba9c984 2019-09-08 stsp
1504 aba9c984 2019-09-08 stsp *str = NULL;
1505 aba9c984 2019-09-08 stsp
1506 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1507 aba9c984 2019-09-08 stsp if (err)
1508 aba9c984 2019-09-08 stsp return err;
1509 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1510 aba9c984 2019-09-08 stsp
1511 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1512 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_STR_VAL:
1513 aba9c984 2019-09-08 stsp if (datalen == 0)
1514 aba9c984 2019-09-08 stsp break;
1515 aba9c984 2019-09-08 stsp *str = malloc(datalen);
1516 aba9c984 2019-09-08 stsp if (*str == NULL) {
1517 aba9c984 2019-09-08 stsp err = got_error_from_errno("malloc");
1518 aba9c984 2019-09-08 stsp break;
1519 aba9c984 2019-09-08 stsp }
1520 aba9c984 2019-09-08 stsp if (strlcpy(*str, imsg.data, datalen) >= datalen)
1521 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_NO_SPACE);
1522 aba9c984 2019-09-08 stsp break;
1523 aba9c984 2019-09-08 stsp default:
1524 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1525 aba9c984 2019-09-08 stsp break;
1526 aba9c984 2019-09-08 stsp }
1527 876c234b 2018-09-10 stsp
1528 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1529 aba9c984 2019-09-08 stsp return err;
1530 aba9c984 2019-09-08 stsp }
1531 aba9c984 2019-09-08 stsp
1532 aba9c984 2019-09-08 stsp const struct got_error *
1533 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1534 aba9c984 2019-09-08 stsp {
1535 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1536 aba9c984 2019-09-08 stsp &value, sizeof(value)) == -1)
1537 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1538 aba9c984 2019-09-08 stsp
1539 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1540 63219cd2 2019-01-04 stsp }
1541 63219cd2 2019-01-04 stsp
1542 63219cd2 2019-01-04 stsp const struct got_error *
1543 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1544 aba9c984 2019-09-08 stsp {
1545 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1546 aba9c984 2019-09-08 stsp struct imsg imsg;
1547 aba9c984 2019-09-08 stsp size_t datalen;
1548 aba9c984 2019-09-08 stsp const size_t min_datalen =
1549 aba9c984 2019-09-08 stsp MIN(sizeof(struct got_imsg_error), sizeof(int));
1550 aba9c984 2019-09-08 stsp
1551 aba9c984 2019-09-08 stsp *val = 0;
1552 aba9c984 2019-09-08 stsp
1553 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1554 aba9c984 2019-09-08 stsp if (err)
1555 aba9c984 2019-09-08 stsp return err;
1556 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1557 aba9c984 2019-09-08 stsp
1558 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1559 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_INT_VAL:
1560 aba9c984 2019-09-08 stsp if (datalen != sizeof(*val)) {
1561 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1562 aba9c984 2019-09-08 stsp break;
1563 aba9c984 2019-09-08 stsp }
1564 aba9c984 2019-09-08 stsp memcpy(val, imsg.data, sizeof(*val));
1565 cd95becd 2019-11-29 stsp break;
1566 cd95becd 2019-11-29 stsp default:
1567 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1568 cd95becd 2019-11-29 stsp break;
1569 cd95becd 2019-11-29 stsp }
1570 cd95becd 2019-11-29 stsp
1571 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1572 cd95becd 2019-11-29 stsp return err;
1573 cd95becd 2019-11-29 stsp }
1574 cd95becd 2019-11-29 stsp
1575 cd95becd 2019-11-29 stsp const struct got_error *
1576 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1577 cd95becd 2019-11-29 stsp struct got_remote_repo *remotes, int nremotes)
1578 cd95becd 2019-11-29 stsp {
1579 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1580 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
1581 cd95becd 2019-11-29 stsp int i;
1582 cd95becd 2019-11-29 stsp
1583 cd95becd 2019-11-29 stsp iremotes.nremotes = nremotes;
1584 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1585 cd95becd 2019-11-29 stsp &iremotes, sizeof(iremotes)) == -1)
1586 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1587 cd95becd 2019-11-29 stsp
1588 cd95becd 2019-11-29 stsp err = flush_imsg(ibuf);
1589 cd95becd 2019-11-29 stsp imsg_clear(ibuf);
1590 cd95becd 2019-11-29 stsp if (err)
1591 cd95becd 2019-11-29 stsp return err;
1592 cd95becd 2019-11-29 stsp
1593 cd95becd 2019-11-29 stsp for (i = 0; i < nremotes; i++) {
1594 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
1595 cd95becd 2019-11-29 stsp size_t len = sizeof(iremote);
1596 cd95becd 2019-11-29 stsp struct ibuf *wbuf;
1597 cd95becd 2019-11-29 stsp
1598 cd95becd 2019-11-29 stsp iremote.name_len = strlen(remotes[i].name);
1599 cd95becd 2019-11-29 stsp len += iremote.name_len;
1600 cd95becd 2019-11-29 stsp iremote.url_len = strlen(remotes[i].url);
1601 cd95becd 2019-11-29 stsp len += iremote.url_len;
1602 cd95becd 2019-11-29 stsp
1603 cd95becd 2019-11-29 stsp wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1604 cd95becd 2019-11-29 stsp if (wbuf == NULL)
1605 cd95becd 2019-11-29 stsp return got_error_from_errno(
1606 cd95becd 2019-11-29 stsp "imsg_create GITCONFIG_REMOTE");
1607 cd95becd 2019-11-29 stsp
1608 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1609 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1610 cd95becd 2019-11-29 stsp "imsg_add GIITCONFIG_REMOTE");
1611 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1612 cd95becd 2019-11-29 stsp return err;
1613 cd95becd 2019-11-29 stsp }
1614 cd95becd 2019-11-29 stsp
1615 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1616 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1617 cd95becd 2019-11-29 stsp "imsg_add GIITCONFIG_REMOTE");
1618 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1619 cd95becd 2019-11-29 stsp return err;
1620 cd95becd 2019-11-29 stsp }
1621 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1622 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1623 cd95becd 2019-11-29 stsp "imsg_add GIITCONFIG_REMOTE");
1624 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1625 cd95becd 2019-11-29 stsp return err;
1626 cd95becd 2019-11-29 stsp }
1627 cd95becd 2019-11-29 stsp
1628 cd95becd 2019-11-29 stsp wbuf->fd = -1;
1629 cd95becd 2019-11-29 stsp imsg_close(ibuf, wbuf);
1630 cd95becd 2019-11-29 stsp err = flush_imsg(ibuf);
1631 cd95becd 2019-11-29 stsp if (err)
1632 cd95becd 2019-11-29 stsp return err;
1633 cd95becd 2019-11-29 stsp }
1634 cd95becd 2019-11-29 stsp
1635 cd95becd 2019-11-29 stsp return NULL;
1636 cd95becd 2019-11-29 stsp }
1637 cd95becd 2019-11-29 stsp
1638 cd95becd 2019-11-29 stsp const struct got_error *
1639 cd95becd 2019-11-29 stsp got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1640 cd95becd 2019-11-29 stsp int *nremotes, struct imsgbuf *ibuf)
1641 cd95becd 2019-11-29 stsp {
1642 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1643 cd95becd 2019-11-29 stsp struct imsg imsg;
1644 cd95becd 2019-11-29 stsp size_t datalen;
1645 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
1646 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
1647 cd95becd 2019-11-29 stsp
1648 cd95becd 2019-11-29 stsp *remotes = NULL;
1649 cd95becd 2019-11-29 stsp *nremotes = 0;
1650 d669b9c9 2020-02-22 stsp iremotes.nremotes = 0;
1651 cd95becd 2019-11-29 stsp
1652 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1653 cd95becd 2019-11-29 stsp if (err)
1654 cd95becd 2019-11-29 stsp return err;
1655 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1656 cd95becd 2019-11-29 stsp
1657 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
1658 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES:
1659 cd95becd 2019-11-29 stsp if (datalen != sizeof(iremotes)) {
1660 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1661 cd95becd 2019-11-29 stsp break;
1662 cd95becd 2019-11-29 stsp }
1663 cd95becd 2019-11-29 stsp memcpy(&iremotes, imsg.data, sizeof(iremotes));
1664 cd95becd 2019-11-29 stsp if (iremotes.nremotes == 0) {
1665 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1666 cd95becd 2019-11-29 stsp return NULL;
1667 cd95becd 2019-11-29 stsp }
1668 aba9c984 2019-09-08 stsp break;
1669 aba9c984 2019-09-08 stsp default:
1670 54b1c5b5 2020-02-22 stsp imsg_free(&imsg);
1671 54b1c5b5 2020-02-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1672 aba9c984 2019-09-08 stsp }
1673 aba9c984 2019-09-08 stsp
1674 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1675 cd95becd 2019-11-29 stsp
1676 cd95becd 2019-11-29 stsp *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1677 cd95becd 2019-11-29 stsp if (*remotes == NULL)
1678 cd95becd 2019-11-29 stsp return got_error_from_errno("recallocarray");
1679 cd95becd 2019-11-29 stsp
1680 cd95becd 2019-11-29 stsp while (*nremotes < iremotes.nremotes) {
1681 cd95becd 2019-11-29 stsp struct got_remote_repo *remote;
1682 cd95becd 2019-11-29 stsp
1683 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1684 cd95becd 2019-11-29 stsp if (err)
1685 cd95becd 2019-11-29 stsp break;
1686 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1687 cd95becd 2019-11-29 stsp
1688 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
1689 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTE:
1690 cd95becd 2019-11-29 stsp remote = &(*remotes)[*nremotes];
1691 cd95becd 2019-11-29 stsp if (datalen < sizeof(iremote)) {
1692 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1693 cd95becd 2019-11-29 stsp break;
1694 cd95becd 2019-11-29 stsp }
1695 cd95becd 2019-11-29 stsp memcpy(&iremote, imsg.data, sizeof(iremote));
1696 cd95becd 2019-11-29 stsp if (iremote.name_len == 0 || iremote.url_len == 0 ||
1697 cd95becd 2019-11-29 stsp (sizeof(iremote) + iremote.name_len +
1698 cd95becd 2019-11-29 stsp iremote.url_len) > datalen) {
1699 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1700 cd95becd 2019-11-29 stsp break;
1701 cd95becd 2019-11-29 stsp }
1702 cd95becd 2019-11-29 stsp remote->name = strndup(imsg.data + sizeof(iremote),
1703 cd95becd 2019-11-29 stsp iremote.name_len);
1704 cd95becd 2019-11-29 stsp if (remote->name == NULL) {
1705 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
1706 cd95becd 2019-11-29 stsp break;
1707 cd95becd 2019-11-29 stsp }
1708 cd95becd 2019-11-29 stsp remote->url = strndup(imsg.data + sizeof(iremote) +
1709 cd95becd 2019-11-29 stsp iremote.name_len, iremote.url_len);
1710 cd95becd 2019-11-29 stsp if (remote->url == NULL) {
1711 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
1712 cd95becd 2019-11-29 stsp free(remote->name);
1713 cd95becd 2019-11-29 stsp break;
1714 cd95becd 2019-11-29 stsp }
1715 cd95becd 2019-11-29 stsp (*nremotes)++;
1716 cd95becd 2019-11-29 stsp break;
1717 cd95becd 2019-11-29 stsp default:
1718 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1719 cd95becd 2019-11-29 stsp break;
1720 cd95becd 2019-11-29 stsp }
1721 cd95becd 2019-11-29 stsp
1722 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1723 cd95becd 2019-11-29 stsp if (err)
1724 cd95becd 2019-11-29 stsp break;
1725 cd95becd 2019-11-29 stsp }
1726 cd95becd 2019-11-29 stsp
1727 cd95becd 2019-11-29 stsp if (err) {
1728 cd95becd 2019-11-29 stsp int i;
1729 cd95becd 2019-11-29 stsp for (i = 0; i < *nremotes; i++) {
1730 cd95becd 2019-11-29 stsp free((*remotes)[i].name);
1731 cd95becd 2019-11-29 stsp free((*remotes)[i].url);
1732 cd95becd 2019-11-29 stsp }
1733 cd95becd 2019-11-29 stsp free(*remotes);
1734 cd95becd 2019-11-29 stsp *remotes = NULL;
1735 cd95becd 2019-11-29 stsp *nremotes = 0;
1736 cd95becd 2019-11-29 stsp }
1737 aba9c984 2019-09-08 stsp return err;
1738 ca6e02ac 2020-01-07 stsp }
1739 ca6e02ac 2020-01-07 stsp
1740 ca6e02ac 2020-01-07 stsp const struct got_error *
1741 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1742 ca6e02ac 2020-01-07 stsp struct got_object_id *id, int idx, const char *path)
1743 ca6e02ac 2020-01-07 stsp {
1744 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1745 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
1746 ca6e02ac 2020-01-07 stsp size_t path_len = strlen(path) + 1;
1747 ca6e02ac 2020-01-07 stsp
1748 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1749 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_traversal_request) + path_len);
1750 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
1751 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
1752 ca6e02ac 2020-01-07 stsp "imsg_create COMMIT_TRAVERSAL_REQUEST");
1753 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1754 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1755 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1756 ca6e02ac 2020-01-07 stsp return err;
1757 ca6e02ac 2020-01-07 stsp }
1758 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1759 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1760 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1761 ca6e02ac 2020-01-07 stsp return err;
1762 ca6e02ac 2020-01-07 stsp }
1763 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, path, path_len) == -1) {
1764 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1765 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1766 ca6e02ac 2020-01-07 stsp return err;
1767 ca6e02ac 2020-01-07 stsp }
1768 ca6e02ac 2020-01-07 stsp
1769 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
1770 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
1771 ca6e02ac 2020-01-07 stsp
1772 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
1773 aba9c984 2019-09-08 stsp }
1774 aba9c984 2019-09-08 stsp
1775 aba9c984 2019-09-08 stsp const struct got_error *
1776 ca6e02ac 2020-01-07 stsp got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1777 ca6e02ac 2020-01-07 stsp size_t ncommits, struct imsgbuf *ibuf)
1778 ca6e02ac 2020-01-07 stsp {
1779 ca6e02ac 2020-01-07 stsp const struct got_error *err;
1780 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
1781 ca6e02ac 2020-01-07 stsp int i;
1782 ca6e02ac 2020-01-07 stsp
1783 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
1784 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_traversed_commits) +
1785 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH);
1786 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
1787 ca6e02ac 2020-01-07 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
1788 ca6e02ac 2020-01-07 stsp
1789 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
1790 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
1791 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1792 ca6e02ac 2020-01-07 stsp return err;
1793 ca6e02ac 2020-01-07 stsp }
1794 ca6e02ac 2020-01-07 stsp for (i = 0; i < ncommits; i++) {
1795 ca6e02ac 2020-01-07 stsp struct got_object_id *id = &commit_ids[i];
1796 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1797 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
1798 ca6e02ac 2020-01-07 stsp "imsg_add TRAVERSED_COMMITS");
1799 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1800 ca6e02ac 2020-01-07 stsp return err;
1801 ca6e02ac 2020-01-07 stsp }
1802 ca6e02ac 2020-01-07 stsp }
1803 ca6e02ac 2020-01-07 stsp
1804 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
1805 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
1806 ca6e02ac 2020-01-07 stsp
1807 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
1808 ca6e02ac 2020-01-07 stsp }
1809 ca6e02ac 2020-01-07 stsp
1810 ca6e02ac 2020-01-07 stsp const struct got_error *
1811 ca6e02ac 2020-01-07 stsp got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1812 ca6e02ac 2020-01-07 stsp struct got_object_id **changed_commit_id,
1813 ca6e02ac 2020-01-07 stsp struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1814 ca6e02ac 2020-01-07 stsp {
1815 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1816 ca6e02ac 2020-01-07 stsp struct imsg imsg;
1817 ca6e02ac 2020-01-07 stsp struct got_imsg_traversed_commits *icommits;
1818 ca6e02ac 2020-01-07 stsp size_t datalen;
1819 ca6e02ac 2020-01-07 stsp int i, done = 0;
1820 ca6e02ac 2020-01-07 stsp
1821 ca6e02ac 2020-01-07 stsp *changed_commit = NULL;
1822 ca6e02ac 2020-01-07 stsp *changed_commit_id = NULL;
1823 ca6e02ac 2020-01-07 stsp
1824 ca6e02ac 2020-01-07 stsp while (!done) {
1825 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1826 ca6e02ac 2020-01-07 stsp if (err)
1827 ca6e02ac 2020-01-07 stsp return err;
1828 ca6e02ac 2020-01-07 stsp
1829 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1830 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
1831 ca6e02ac 2020-01-07 stsp case GOT_IMSG_TRAVERSED_COMMITS:
1832 ca6e02ac 2020-01-07 stsp icommits = imsg.data;
1833 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommits) +
1834 ca6e02ac 2020-01-07 stsp icommits->ncommits * SHA1_DIGEST_LENGTH) {
1835 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1836 ca6e02ac 2020-01-07 stsp break;
1837 ca6e02ac 2020-01-07 stsp }
1838 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommits->ncommits; i++) {
1839 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
1840 ca6e02ac 2020-01-07 stsp uint8_t *sha1 = (uint8_t *)imsg.data +
1841 ca6e02ac 2020-01-07 stsp sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1842 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
1843 ca6e02ac 2020-01-07 stsp if (err)
1844 ca6e02ac 2020-01-07 stsp break;
1845 ca6e02ac 2020-01-07 stsp memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1846 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1847 ca6e02ac 2020-01-07 stsp
1848 ca6e02ac 2020-01-07 stsp /* The last commit may contain a change. */
1849 ca6e02ac 2020-01-07 stsp if (i == icommits->ncommits - 1) {
1850 ca6e02ac 2020-01-07 stsp *changed_commit_id =
1851 ca6e02ac 2020-01-07 stsp got_object_id_dup(qid->id);
1852 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
1853 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
1854 ca6e02ac 2020-01-07 stsp "got_object_id_dup");
1855 ca6e02ac 2020-01-07 stsp break;
1856 ca6e02ac 2020-01-07 stsp }
1857 ca6e02ac 2020-01-07 stsp }
1858 ca6e02ac 2020-01-07 stsp }
1859 ca6e02ac 2020-01-07 stsp break;
1860 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
1861 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
1862 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1863 ca6e02ac 2020-01-07 stsp break;
1864 ca6e02ac 2020-01-07 stsp }
1865 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(changed_commit, &imsg,
1866 ca6e02ac 2020-01-07 stsp datalen, ibuf);
1867 ca6e02ac 2020-01-07 stsp break;
1868 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1869 ca6e02ac 2020-01-07 stsp done = 1;
1870 ca6e02ac 2020-01-07 stsp break;
1871 ca6e02ac 2020-01-07 stsp default:
1872 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1873 ca6e02ac 2020-01-07 stsp break;
1874 ca6e02ac 2020-01-07 stsp }
1875 ca6e02ac 2020-01-07 stsp
1876 ca6e02ac 2020-01-07 stsp imsg_free(&imsg);
1877 ca6e02ac 2020-01-07 stsp if (err)
1878 ca6e02ac 2020-01-07 stsp break;
1879 ca6e02ac 2020-01-07 stsp }
1880 ca6e02ac 2020-01-07 stsp
1881 ca6e02ac 2020-01-07 stsp if (err)
1882 ca6e02ac 2020-01-07 stsp got_object_id_queue_free(commit_ids);
1883 ca6e02ac 2020-01-07 stsp return err;
1884 ca6e02ac 2020-01-07 stsp }
1885 ca6e02ac 2020-01-07 stsp
1886 ca6e02ac 2020-01-07 stsp const struct got_error *
1887 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
1888 ca6e02ac 2020-01-07 stsp {
1889 ca6e02ac 2020-01-07 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
1890 ca6e02ac 2020-01-07 stsp NULL, 0) == -1)
1891 ca6e02ac 2020-01-07 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
1892 ca6e02ac 2020-01-07 stsp
1893 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
1894 ca6e02ac 2020-01-07 stsp }
1895 ca6e02ac 2020-01-07 stsp
1896 ca6e02ac 2020-01-07 stsp const struct got_error *
1897 63219cd2 2019-01-04 stsp got_privsep_unveil_exec_helpers(void)
1898 63219cd2 2019-01-04 stsp {
1899 c39c25dd 2019-08-09 stsp const char *helpers[] = {
1900 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_PACK,
1901 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_OBJECT,
1902 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_COMMIT,
1903 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TREE,
1904 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_BLOB,
1905 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TAG,
1906 aba9c984 2019-09-08 stsp GOT_PATH_PROG_READ_GITCONFIG,
1907 c39c25dd 2019-08-09 stsp };
1908 c39c25dd 2019-08-09 stsp int i;
1909 63219cd2 2019-01-04 stsp
1910 c39c25dd 2019-08-09 stsp for (i = 0; i < nitems(helpers); i++) {
1911 c39c25dd 2019-08-09 stsp if (unveil(helpers[i], "x") == 0)
1912 c39c25dd 2019-08-09 stsp continue;
1913 c39c25dd 2019-08-09 stsp return got_error_from_errno2("unveil", helpers[i]);
1914 c39c25dd 2019-08-09 stsp }
1915 c39c25dd 2019-08-09 stsp
1916 63219cd2 2019-01-04 stsp return NULL;
1917 876c234b 2018-09-10 stsp }
1918 aba9c984 2019-09-08 stsp
1919 aba9c984 2019-09-08 stsp void
1920 aba9c984 2019-09-08 stsp got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1921 aba9c984 2019-09-08 stsp {
1922 aba9c984 2019-09-08 stsp if (close(imsg_fds[0]) != 0) {
1923 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1924 aba9c984 2019-09-08 stsp _exit(1);
1925 aba9c984 2019-09-08 stsp }
1926 aba9c984 2019-09-08 stsp
1927 aba9c984 2019-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1928 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1929 aba9c984 2019-09-08 stsp _exit(1);
1930 aba9c984 2019-09-08 stsp }
1931 aba9c984 2019-09-08 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1932 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1933 aba9c984 2019-09-08 stsp _exit(1);
1934 aba9c984 2019-09-08 stsp }
1935 aba9c984 2019-09-08 stsp
1936 aba9c984 2019-09-08 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
1937 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1938 aba9c984 2019-09-08 stsp strerror(errno));
1939 aba9c984 2019-09-08 stsp _exit(1);
1940 aba9c984 2019-09-08 stsp }
1941 aba9c984 2019-09-08 stsp }