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/wait.h>
22 2178c42e 2018-04-22 stsp
23 531c3985 2020-03-18 stsp #include <ctype.h>
24 23c57b28 2020-09-11 naddy #include <limits.h>
25 2178c42e 2018-04-22 stsp #include <stdio.h>
26 2178c42e 2018-04-22 stsp #include <stdlib.h>
27 2178c42e 2018-04-22 stsp #include <string.h>
28 2178c42e 2018-04-22 stsp #include <errno.h>
29 2178c42e 2018-04-22 stsp #include <stdint.h>
30 2178c42e 2018-04-22 stsp #include <poll.h>
31 2178c42e 2018-04-22 stsp #include <imsg.h>
32 2178c42e 2018-04-22 stsp #include <sha1.h>
33 81a12da5 2020-09-09 naddy #include <unistd.h>
34 2178c42e 2018-04-22 stsp #include <zlib.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 2178c42e 2018-04-22 stsp
37 2178c42e 2018-04-22 stsp #include "got_object.h"
38 2178c42e 2018-04-22 stsp #include "got_error.h"
39 3022d272 2019-11-14 stsp #include "got_path.h"
40 cd95becd 2019-11-29 stsp #include "got_repository.h"
41 2178c42e 2018-04-22 stsp
42 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
43 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
44 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
45 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
46 a440fac0 2018-09-06 stsp #include "got_lib_object_parse.h"
47 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
48 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
49 2178c42e 2018-04-22 stsp
50 2178c42e 2018-04-22 stsp #ifndef MIN
51 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 c39c25dd 2019-08-09 stsp #endif
53 c39c25dd 2019-08-09 stsp
54 c39c25dd 2019-08-09 stsp #ifndef nitems
55 c39c25dd 2019-08-09 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 2178c42e 2018-04-22 stsp #endif
57 2178c42e 2018-04-22 stsp
58 2178c42e 2018-04-22 stsp static const struct got_error *
59 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
60 2178c42e 2018-04-22 stsp {
61 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
62 2178c42e 2018-04-22 stsp int n;
63 2178c42e 2018-04-22 stsp
64 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
65 2178c42e 2018-04-22 stsp pfd[0].events = events;
66 2178c42e 2018-04-22 stsp
67 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
68 2178c42e 2018-04-22 stsp if (n == -1)
69 638f9024 2019-05-13 stsp return got_error_from_errno("poll");
70 2178c42e 2018-04-22 stsp if (n == 0)
71 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
72 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
73 638f9024 2019-05-13 stsp return got_error_from_errno("poll error");
74 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
75 2178c42e 2018-04-22 stsp return NULL;
76 2178c42e 2018-04-22 stsp
77 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
78 2178c42e 2018-04-22 stsp }
79 2178c42e 2018-04-22 stsp
80 c4eae628 2018-04-23 stsp static const struct got_error *
81 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
82 fe36cf76 2018-04-23 stsp {
83 fe36cf76 2018-04-23 stsp const struct got_error *err;
84 e033d803 2018-04-23 stsp size_t n;
85 fe36cf76 2018-04-23 stsp
86 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
87 fe36cf76 2018-04-23 stsp if (err)
88 fe36cf76 2018-04-23 stsp return err;
89 fe36cf76 2018-04-23 stsp
90 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
91 fe36cf76 2018-04-23 stsp if (n == -1) {
92 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
93 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
94 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
95 fe36cf76 2018-04-23 stsp }
96 fe36cf76 2018-04-23 stsp if (n == 0)
97 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
98 fe36cf76 2018-04-23 stsp
99 e033d803 2018-04-23 stsp return NULL;
100 e033d803 2018-04-23 stsp }
101 e033d803 2018-04-23 stsp
102 ad242220 2018-09-08 stsp const struct got_error *
103 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
104 876c234b 2018-09-10 stsp {
105 876c234b 2018-09-10 stsp int child_status;
106 876c234b 2018-09-10 stsp
107 2cb49fa8 2019-05-10 stsp if (waitpid(pid, &child_status, 0) == -1)
108 638f9024 2019-05-13 stsp return got_error_from_errno("waitpid");
109 876c234b 2018-09-10 stsp
110 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
111 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
112 876c234b 2018-09-10 stsp
113 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
114 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
115 876c234b 2018-09-10 stsp
116 876c234b 2018-09-10 stsp return NULL;
117 876c234b 2018-09-10 stsp }
118 876c234b 2018-09-10 stsp
119 73b7854a 2018-11-11 stsp static const struct got_error *
120 73b7854a 2018-11-11 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
121 73b7854a 2018-11-11 stsp {
122 73b7854a 2018-11-11 stsp struct got_imsg_error *ierr;
123 73b7854a 2018-11-11 stsp
124 73b7854a 2018-11-11 stsp if (datalen != sizeof(*ierr))
125 73b7854a 2018-11-11 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
126 73b7854a 2018-11-11 stsp
127 73b7854a 2018-11-11 stsp ierr = imsg->data;
128 73b7854a 2018-11-11 stsp if (ierr->code == GOT_ERR_ERRNO) {
129 73b7854a 2018-11-11 stsp static struct got_error serr;
130 73b7854a 2018-11-11 stsp serr.code = GOT_ERR_ERRNO;
131 73b7854a 2018-11-11 stsp serr.msg = strerror(ierr->errno_code);
132 73b7854a 2018-11-11 stsp return &serr;
133 73b7854a 2018-11-11 stsp }
134 73b7854a 2018-11-11 stsp
135 73b7854a 2018-11-11 stsp return got_error(ierr->code);
136 73b7854a 2018-11-11 stsp }
137 73b7854a 2018-11-11 stsp
138 876c234b 2018-09-10 stsp const struct got_error *
139 46de5bfd 2018-11-11 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
140 46de5bfd 2018-11-11 stsp size_t min_datalen)
141 e033d803 2018-04-23 stsp {
142 e033d803 2018-04-23 stsp const struct got_error *err;
143 e033d803 2018-04-23 stsp ssize_t n;
144 e033d803 2018-04-23 stsp
145 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
146 876c234b 2018-09-10 stsp if (n == -1)
147 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_get");
148 876c234b 2018-09-10 stsp
149 876c234b 2018-09-10 stsp while (n == 0) {
150 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
151 876c234b 2018-09-10 stsp if (err)
152 876c234b 2018-09-10 stsp return err;
153 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
154 cbfaaf20 2020-01-06 stsp if (n == -1)
155 cbfaaf20 2020-01-06 stsp return got_error_from_errno("imsg_get");
156 876c234b 2018-09-10 stsp }
157 fe36cf76 2018-04-23 stsp
158 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
159 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
160 c4eae628 2018-04-23 stsp
161 73b7854a 2018-11-11 stsp if (imsg->hdr.type == GOT_IMSG_ERROR) {
162 73b7854a 2018-11-11 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
163 73b7854a 2018-11-11 stsp return recv_imsg_error(imsg, datalen);
164 c4eae628 2018-04-23 stsp }
165 c4eae628 2018-04-23 stsp
166 73b7854a 2018-11-11 stsp return NULL;
167 c4eae628 2018-04-23 stsp }
168 c4eae628 2018-04-23 stsp
169 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
170 2178c42e 2018-04-22 stsp void
171 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
172 2178c42e 2018-04-22 stsp {
173 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
174 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
175 2178c42e 2018-04-22 stsp int ret;
176 2178c42e 2018-04-22 stsp
177 2178c42e 2018-04-22 stsp ierr.code = err->code;
178 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
179 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
180 2178c42e 2018-04-22 stsp else
181 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
182 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
183 e93cd828 2018-11-11 stsp if (ret == -1) {
184 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
185 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
186 5d43e84d 2018-04-23 stsp return;
187 2178c42e 2018-04-22 stsp }
188 2178c42e 2018-04-22 stsp
189 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
190 5d43e84d 2018-04-23 stsp if (poll_err) {
191 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
192 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
193 5d43e84d 2018-04-23 stsp return;
194 5d43e84d 2018-04-23 stsp }
195 2178c42e 2018-04-22 stsp
196 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
197 5d43e84d 2018-04-23 stsp if (ret == -1) {
198 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
199 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
200 5d43e84d 2018-04-23 stsp return;
201 5d43e84d 2018-04-23 stsp }
202 e033d803 2018-04-23 stsp }
203 e033d803 2018-04-23 stsp
204 e033d803 2018-04-23 stsp static const struct got_error *
205 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
206 e033d803 2018-04-23 stsp {
207 e033d803 2018-04-23 stsp const struct got_error *err;
208 e033d803 2018-04-23 stsp
209 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
210 e033d803 2018-04-23 stsp if (err)
211 e033d803 2018-04-23 stsp return err;
212 e033d803 2018-04-23 stsp
213 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
214 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_flush");
215 e033d803 2018-04-23 stsp
216 e033d803 2018-04-23 stsp return NULL;
217 ad242220 2018-09-08 stsp }
218 ad242220 2018-09-08 stsp
219 ad242220 2018-09-08 stsp const struct got_error *
220 e70bf110 2020-03-22 stsp got_privsep_flush_imsg(struct imsgbuf *ibuf)
221 e70bf110 2020-03-22 stsp {
222 e70bf110 2020-03-22 stsp return flush_imsg(ibuf);
223 e70bf110 2020-03-22 stsp }
224 e70bf110 2020-03-22 stsp
225 e70bf110 2020-03-22 stsp const struct got_error *
226 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
227 ad242220 2018-09-08 stsp {
228 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
229 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
230 ad242220 2018-09-08 stsp
231 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
232 ad242220 2018-09-08 stsp
233 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
234 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose STOP");
235 ad242220 2018-09-08 stsp
236 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
237 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
238 ad242220 2018-09-08 stsp return err;
239 7762fe12 2018-11-05 stsp }
240 93658fb9 2020-03-18 stsp
241 93658fb9 2020-03-18 stsp const struct got_error *
242 aea5f015 2018-12-24 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
243 ad242220 2018-09-08 stsp {
244 aea5f015 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
245 aea5f015 2018-12-24 stsp == -1)
246 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT_REQUEST");
247 1785f84a 2018-12-23 stsp
248 1785f84a 2018-12-23 stsp return flush_imsg(ibuf);
249 1785f84a 2018-12-23 stsp }
250 1785f84a 2018-12-23 stsp
251 1785f84a 2018-12-23 stsp const struct got_error *
252 1785f84a 2018-12-23 stsp got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
253 1785f84a 2018-12-23 stsp struct got_object_id *id, int pack_idx)
254 1785f84a 2018-12-23 stsp {
255 41496140 2019-02-21 stsp const struct got_error *err = NULL;
256 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj, *iobjp;
257 1785f84a 2018-12-23 stsp size_t len;
258 1785f84a 2018-12-23 stsp
259 1785f84a 2018-12-23 stsp if (id) { /* commit is packed */
260 1785f84a 2018-12-23 stsp iobj.idx = pack_idx;
261 1785f84a 2018-12-23 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
262 1785f84a 2018-12-23 stsp iobjp = &iobj;
263 1785f84a 2018-12-23 stsp len = sizeof(iobj);
264 1785f84a 2018-12-23 stsp } else {
265 1785f84a 2018-12-23 stsp iobjp = NULL;
266 1785f84a 2018-12-23 stsp len = 0;
267 1785f84a 2018-12-23 stsp }
268 1785f84a 2018-12-23 stsp
269 1785f84a 2018-12-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
270 41496140 2019-02-21 stsp == -1) {
271 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
272 41496140 2019-02-21 stsp close(fd);
273 41496140 2019-02-21 stsp return err;
274 41496140 2019-02-21 stsp }
275 13c729f7 2018-12-24 stsp
276 13c729f7 2018-12-24 stsp return flush_imsg(ibuf);
277 13c729f7 2018-12-24 stsp }
278 13c729f7 2018-12-24 stsp
279 13c729f7 2018-12-24 stsp const struct got_error *
280 13c729f7 2018-12-24 stsp got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
281 13c729f7 2018-12-24 stsp struct got_object_id *id, int pack_idx)
282 13c729f7 2018-12-24 stsp {
283 41496140 2019-02-21 stsp const struct got_error *err = NULL;
284 7f358e3b 2019-11-23 stsp struct ibuf *wbuf;
285 7f358e3b 2019-11-23 stsp size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
286 7f358e3b 2019-11-23 stsp
287 7f358e3b 2019-11-23 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
288 7f358e3b 2019-11-23 stsp if (wbuf == NULL)
289 7f358e3b 2019-11-23 stsp return got_error_from_errno("imsg_create TREE_REQUEST");
290 13c729f7 2018-12-24 stsp
291 13c729f7 2018-12-24 stsp if (id) { /* tree is packed */
292 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
293 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
294 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
295 7f358e3b 2019-11-23 stsp return err;
296 7f358e3b 2019-11-23 stsp }
297 13c729f7 2018-12-24 stsp
298 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
299 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
300 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
301 7f358e3b 2019-11-23 stsp return err;
302 7f358e3b 2019-11-23 stsp }
303 41496140 2019-02-21 stsp }
304 268f7291 2018-12-24 stsp
305 7f358e3b 2019-11-23 stsp wbuf->fd = fd;
306 7f358e3b 2019-11-23 stsp imsg_close(ibuf, wbuf);
307 7f358e3b 2019-11-23 stsp
308 268f7291 2018-12-24 stsp return flush_imsg(ibuf);
309 268f7291 2018-12-24 stsp }
310 268f7291 2018-12-24 stsp
311 268f7291 2018-12-24 stsp const struct got_error *
312 268f7291 2018-12-24 stsp got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
313 268f7291 2018-12-24 stsp struct got_object_id *id, int pack_idx)
314 268f7291 2018-12-24 stsp {
315 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
316 268f7291 2018-12-24 stsp size_t len;
317 268f7291 2018-12-24 stsp
318 268f7291 2018-12-24 stsp if (id) { /* tag is packed */
319 268f7291 2018-12-24 stsp iobj.idx = pack_idx;
320 268f7291 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
321 268f7291 2018-12-24 stsp iobjp = &iobj;
322 268f7291 2018-12-24 stsp len = sizeof(iobj);
323 268f7291 2018-12-24 stsp } else {
324 268f7291 2018-12-24 stsp iobjp = NULL;
325 268f7291 2018-12-24 stsp len = 0;
326 268f7291 2018-12-24 stsp }
327 268f7291 2018-12-24 stsp
328 268f7291 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
329 1785f84a 2018-12-23 stsp == -1)
330 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TAG_REQUEST");
331 7762fe12 2018-11-05 stsp
332 7762fe12 2018-11-05 stsp return flush_imsg(ibuf);
333 7762fe12 2018-11-05 stsp }
334 7762fe12 2018-11-05 stsp
335 7762fe12 2018-11-05 stsp const struct got_error *
336 ebc55e2d 2018-12-24 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
337 ebc55e2d 2018-12-24 stsp struct got_object_id *id, int pack_idx)
338 55da3778 2018-09-10 stsp {
339 41496140 2019-02-21 stsp const struct got_error *err = NULL;
340 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
341 ebc55e2d 2018-12-24 stsp size_t len;
342 ebc55e2d 2018-12-24 stsp
343 ebc55e2d 2018-12-24 stsp if (id) { /* blob is packed */
344 ebc55e2d 2018-12-24 stsp iobj.idx = pack_idx;
345 ebc55e2d 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
346 ebc55e2d 2018-12-24 stsp iobjp = &iobj;
347 ebc55e2d 2018-12-24 stsp len = sizeof(iobj);
348 ebc55e2d 2018-12-24 stsp } else {
349 ebc55e2d 2018-12-24 stsp iobjp = NULL;
350 ebc55e2d 2018-12-24 stsp len = 0;
351 ebc55e2d 2018-12-24 stsp }
352 ebc55e2d 2018-12-24 stsp
353 ebc55e2d 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
354 41496140 2019-02-21 stsp == -1) {
355 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_REQUEST");
356 41496140 2019-02-21 stsp close(infd);
357 41496140 2019-02-21 stsp return err;
358 41496140 2019-02-21 stsp }
359 ad242220 2018-09-08 stsp
360 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
361 55da3778 2018-09-10 stsp }
362 ad242220 2018-09-08 stsp
363 55da3778 2018-09-10 stsp const struct got_error *
364 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
365 55da3778 2018-09-10 stsp {
366 41496140 2019-02-21 stsp const struct got_error *err = NULL;
367 41496140 2019-02-21 stsp
368 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
369 41496140 2019-02-21 stsp == -1) {
370 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_OUTFD");
371 41496140 2019-02-21 stsp close(outfd);
372 41496140 2019-02-21 stsp return err;
373 41496140 2019-02-21 stsp }
374 3840f4c9 2018-09-12 stsp
375 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
376 3840f4c9 2018-09-12 stsp }
377 3840f4c9 2018-09-12 stsp
378 73ab1060 2020-03-18 stsp static const struct got_error *
379 73ab1060 2020-03-18 stsp send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
380 3840f4c9 2018-09-12 stsp {
381 41496140 2019-02-21 stsp const struct got_error *err = NULL;
382 41496140 2019-02-21 stsp
383 73ab1060 2020-03-18 stsp if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
384 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TMPFD");
385 41496140 2019-02-21 stsp close(fd);
386 41496140 2019-02-21 stsp return err;
387 41496140 2019-02-21 stsp }
388 ad242220 2018-09-08 stsp
389 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
390 ad242220 2018-09-08 stsp }
391 ad242220 2018-09-08 stsp
392 ad242220 2018-09-08 stsp const struct got_error *
393 73ab1060 2020-03-18 stsp got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
394 73ab1060 2020-03-18 stsp {
395 73ab1060 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
396 73ab1060 2020-03-18 stsp }
397 73ab1060 2020-03-18 stsp
398 73ab1060 2020-03-18 stsp const struct got_error *
399 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
400 2178c42e 2018-04-22 stsp {
401 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
402 2178c42e 2018-04-22 stsp
403 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
404 2178c42e 2018-04-22 stsp iobj.type = obj->type;
405 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
406 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
407 2178c42e 2018-04-22 stsp iobj.size = obj->size;
408 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
409 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
410 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
411 c59b3346 2018-09-11 stsp }
412 2178c42e 2018-04-22 stsp
413 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
414 2178c42e 2018-04-22 stsp == -1)
415 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT");
416 2178c42e 2018-04-22 stsp
417 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
418 cfd633c2 2018-09-10 stsp }
419 cfd633c2 2018-09-10 stsp
420 cfd633c2 2018-09-10 stsp const struct got_error *
421 33501562 2020-03-18 stsp got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
422 4ba14133 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
423 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
424 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
425 93658fb9 2020-03-18 stsp {
426 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
427 33501562 2020-03-18 stsp struct ibuf *wbuf;
428 4ba14133 2020-03-20 stsp size_t len;
429 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
430 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetchreq;
431 93658fb9 2020-03-18 stsp
432 4ba14133 2020-03-20 stsp memset(&fetchreq, 0, sizeof(fetchreq));
433 4ba14133 2020-03-20 stsp fetchreq.fetch_all_branches = fetch_all_branches;
434 41b0de12 2020-03-21 stsp fetchreq.list_refs_only = list_refs_only;
435 2690194b 2020-03-21 stsp fetchreq.verbosity = verbosity;
436 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, have_refs, entry)
437 4ba14133 2020-03-20 stsp fetchreq.n_have_refs++;
438 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry)
439 4ba14133 2020-03-20 stsp fetchreq.n_wanted_branches++;
440 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry)
441 0e4002ca 2020-03-21 stsp fetchreq.n_wanted_refs++;
442 659e7fbd 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_request);
443 33501562 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
444 33501562 2020-03-18 stsp close(fd);
445 33501562 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
446 33501562 2020-03-18 stsp }
447 33501562 2020-03-18 stsp
448 4ba14133 2020-03-20 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
449 4ba14133 2020-03-20 stsp &fetchreq, sizeof(fetchreq)) == -1)
450 4ba14133 2020-03-20 stsp return got_error_from_errno(
451 4ba14133 2020-03-20 stsp "imsg_compose FETCH_SERVER_PROGRESS");
452 33501562 2020-03-18 stsp
453 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
454 4ba14133 2020-03-20 stsp if (err) {
455 659e7fbd 2020-03-20 stsp close(fd);
456 659e7fbd 2020-03-20 stsp return err;
457 659e7fbd 2020-03-20 stsp }
458 4ba14133 2020-03-20 stsp fd = -1;
459 33501562 2020-03-18 stsp
460 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
461 33501562 2020-03-18 stsp const char *name = pe->path;
462 33501562 2020-03-18 stsp size_t name_len = pe->path_len;
463 33501562 2020-03-18 stsp struct got_object_id *id = pe->data;
464 33501562 2020-03-18 stsp
465 4ba14133 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
466 4ba14133 2020-03-20 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
467 4ba14133 2020-03-20 stsp if (wbuf == NULL)
468 4ba14133 2020-03-20 stsp return got_error_from_errno("imsg_create FETCH_HAVE_REF");
469 4ba14133 2020-03-20 stsp
470 33501562 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_have_ref! */
471 33501562 2020-03-18 stsp if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
472 4ba14133 2020-03-20 stsp err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
473 33501562 2020-03-18 stsp ibuf_free(wbuf);
474 33501562 2020-03-18 stsp return err;
475 33501562 2020-03-18 stsp }
476 33501562 2020-03-18 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
477 4ba14133 2020-03-20 stsp err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
478 33501562 2020-03-18 stsp ibuf_free(wbuf);
479 33501562 2020-03-18 stsp return err;
480 33501562 2020-03-18 stsp }
481 33501562 2020-03-18 stsp if (imsg_add(wbuf, name, name_len) == -1) {
482 4ba14133 2020-03-20 stsp err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
483 33501562 2020-03-18 stsp ibuf_free(wbuf);
484 33501562 2020-03-18 stsp return err;
485 33501562 2020-03-18 stsp }
486 4ba14133 2020-03-20 stsp
487 4ba14133 2020-03-20 stsp wbuf->fd = -1;
488 4ba14133 2020-03-20 stsp imsg_close(ibuf, wbuf);
489 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
490 4ba14133 2020-03-20 stsp if (err)
491 4ba14133 2020-03-20 stsp return err;
492 33501562 2020-03-18 stsp }
493 33501562 2020-03-18 stsp
494 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
495 4ba14133 2020-03-20 stsp const char *name = pe->path;
496 4ba14133 2020-03-20 stsp size_t name_len = pe->path_len;
497 4ba14133 2020-03-20 stsp
498 4ba14133 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
499 4ba14133 2020-03-20 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
500 4ba14133 2020-03-20 stsp len);
501 4ba14133 2020-03-20 stsp if (wbuf == NULL)
502 4ba14133 2020-03-20 stsp return got_error_from_errno(
503 62d463ca 2020-10-20 naddy "imsg_create FETCH_WANTED_BRANCH");
504 4ba14133 2020-03-20 stsp
505 4ba14133 2020-03-20 stsp /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
506 4ba14133 2020-03-20 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
507 4ba14133 2020-03-20 stsp err = got_error_from_errno(
508 4ba14133 2020-03-20 stsp "imsg_add FETCH_WANTED_BRANCH");
509 4ba14133 2020-03-20 stsp ibuf_free(wbuf);
510 4ba14133 2020-03-20 stsp return err;
511 4ba14133 2020-03-20 stsp }
512 4ba14133 2020-03-20 stsp if (imsg_add(wbuf, name, name_len) == -1) {
513 4ba14133 2020-03-20 stsp err = got_error_from_errno(
514 62d463ca 2020-10-20 naddy "imsg_add FETCH_WANTED_BRANCH");
515 4ba14133 2020-03-20 stsp ibuf_free(wbuf);
516 4ba14133 2020-03-20 stsp return err;
517 4ba14133 2020-03-20 stsp }
518 4ba14133 2020-03-20 stsp
519 4ba14133 2020-03-20 stsp wbuf->fd = -1;
520 4ba14133 2020-03-20 stsp imsg_close(ibuf, wbuf);
521 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
522 4ba14133 2020-03-20 stsp if (err)
523 4ba14133 2020-03-20 stsp return err;
524 4ba14133 2020-03-20 stsp }
525 4ba14133 2020-03-20 stsp
526 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
527 0e4002ca 2020-03-21 stsp const char *name = pe->path;
528 0e4002ca 2020-03-21 stsp size_t name_len = pe->path_len;
529 0e4002ca 2020-03-21 stsp
530 0e4002ca 2020-03-21 stsp len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
531 0e4002ca 2020-03-21 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
532 0e4002ca 2020-03-21 stsp len);
533 0e4002ca 2020-03-21 stsp if (wbuf == NULL)
534 0e4002ca 2020-03-21 stsp return got_error_from_errno(
535 62d463ca 2020-10-20 naddy "imsg_create FETCH_WANTED_REF");
536 0e4002ca 2020-03-21 stsp
537 0e4002ca 2020-03-21 stsp /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
538 0e4002ca 2020-03-21 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
539 0e4002ca 2020-03-21 stsp err = got_error_from_errno(
540 0e4002ca 2020-03-21 stsp "imsg_add FETCH_WANTED_REF");
541 0e4002ca 2020-03-21 stsp ibuf_free(wbuf);
542 0e4002ca 2020-03-21 stsp return err;
543 0e4002ca 2020-03-21 stsp }
544 0e4002ca 2020-03-21 stsp if (imsg_add(wbuf, name, name_len) == -1) {
545 0e4002ca 2020-03-21 stsp err = got_error_from_errno(
546 62d463ca 2020-10-20 naddy "imsg_add FETCH_WANTED_REF");
547 0e4002ca 2020-03-21 stsp ibuf_free(wbuf);
548 0e4002ca 2020-03-21 stsp return err;
549 0e4002ca 2020-03-21 stsp }
550 0e4002ca 2020-03-21 stsp
551 0e4002ca 2020-03-21 stsp wbuf->fd = -1;
552 0e4002ca 2020-03-21 stsp imsg_close(ibuf, wbuf);
553 0e4002ca 2020-03-21 stsp err = flush_imsg(ibuf);
554 0e4002ca 2020-03-21 stsp if (err)
555 0e4002ca 2020-03-21 stsp return err;
556 0e4002ca 2020-03-21 stsp }
557 0e4002ca 2020-03-21 stsp
558 0e4002ca 2020-03-21 stsp
559 4ba14133 2020-03-20 stsp return NULL;
560 4ba14133 2020-03-20 stsp
561 93658fb9 2020-03-18 stsp }
562 93658fb9 2020-03-18 stsp
563 93658fb9 2020-03-18 stsp const struct got_error *
564 f826addf 2020-03-18 stsp got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
565 f826addf 2020-03-18 stsp {
566 f826addf 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
567 531c3985 2020-03-18 stsp }
568 531c3985 2020-03-18 stsp
569 531c3985 2020-03-18 stsp const struct got_error *
570 8f2d01a6 2020-03-18 stsp got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
571 531c3985 2020-03-18 stsp char **refname, struct got_pathlist_head *symrefs, char **server_progress,
572 1d72a2a0 2020-03-24 stsp off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
573 b9f99abf 2020-03-18 stsp {
574 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
575 b9f99abf 2020-03-18 stsp struct imsg imsg;
576 b9f99abf 2020-03-18 stsp size_t datalen;
577 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symrefs *isymrefs = NULL;
578 abe0f35f 2020-03-18 stsp size_t n, remain;
579 abe0f35f 2020-03-18 stsp off_t off;
580 531c3985 2020-03-18 stsp int i;
581 b9f99abf 2020-03-18 stsp
582 8f2d01a6 2020-03-18 stsp *done = 0;
583 8f2d01a6 2020-03-18 stsp *id = NULL;
584 b9f99abf 2020-03-18 stsp *refname = NULL;
585 531c3985 2020-03-18 stsp *server_progress = NULL;
586 5a489642 2020-03-19 stsp *packfile_size = 0;
587 1d72a2a0 2020-03-24 stsp memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
588 b9f99abf 2020-03-18 stsp
589 531c3985 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
590 b9f99abf 2020-03-18 stsp if (err)
591 b9f99abf 2020-03-18 stsp return err;
592 b9f99abf 2020-03-18 stsp
593 b9f99abf 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
594 b9f99abf 2020-03-18 stsp switch (imsg.hdr.type) {
595 b9f99abf 2020-03-18 stsp case GOT_IMSG_ERROR:
596 531c3985 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_error)) {
597 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
598 531c3985 2020-03-18 stsp break;
599 531c3985 2020-03-18 stsp }
600 b9f99abf 2020-03-18 stsp err = recv_imsg_error(&imsg, datalen);
601 b9f99abf 2020-03-18 stsp break;
602 abe0f35f 2020-03-18 stsp case GOT_IMSG_FETCH_SYMREFS:
603 abe0f35f 2020-03-18 stsp if (datalen < sizeof(*isymrefs)) {
604 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
605 abe0f35f 2020-03-18 stsp break;
606 abe0f35f 2020-03-18 stsp }
607 abe0f35f 2020-03-18 stsp if (isymrefs != NULL) {
608 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
609 abe0f35f 2020-03-18 stsp break;
610 abe0f35f 2020-03-18 stsp }
611 abe0f35f 2020-03-18 stsp isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
612 abe0f35f 2020-03-18 stsp off = sizeof(*isymrefs);
613 abe0f35f 2020-03-18 stsp remain = datalen - off;
614 abe0f35f 2020-03-18 stsp for (n = 0; n < isymrefs->nsymrefs; n++) {
615 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symref *s;
616 abe0f35f 2020-03-18 stsp char *name, *target;
617 abe0f35f 2020-03-18 stsp if (remain < sizeof(struct got_imsg_fetch_symref)) {
618 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
619 abe0f35f 2020-03-18 stsp goto done;
620 abe0f35f 2020-03-18 stsp }
621 abe0f35f 2020-03-18 stsp s = (struct got_imsg_fetch_symref *)(imsg.data + off);
622 abe0f35f 2020-03-18 stsp off += sizeof(*s);
623 abe0f35f 2020-03-18 stsp remain -= sizeof(*s);
624 abe0f35f 2020-03-18 stsp if (remain < s->name_len) {
625 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
626 abe0f35f 2020-03-18 stsp goto done;
627 abe0f35f 2020-03-18 stsp }
628 abe0f35f 2020-03-18 stsp name = strndup(imsg.data + off, s->name_len);
629 abe0f35f 2020-03-18 stsp if (name == NULL) {
630 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
631 abe0f35f 2020-03-18 stsp goto done;
632 abe0f35f 2020-03-18 stsp }
633 abe0f35f 2020-03-18 stsp off += s->name_len;
634 abe0f35f 2020-03-18 stsp remain -= s->name_len;
635 abe0f35f 2020-03-18 stsp if (remain < s->target_len) {
636 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
637 abe0f35f 2020-03-18 stsp free(name);
638 abe0f35f 2020-03-18 stsp goto done;
639 abe0f35f 2020-03-18 stsp }
640 abe0f35f 2020-03-18 stsp target = strndup(imsg.data + off, s->target_len);
641 abe0f35f 2020-03-18 stsp if (target == NULL) {
642 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
643 abe0f35f 2020-03-18 stsp free(name);
644 abe0f35f 2020-03-18 stsp goto done;
645 abe0f35f 2020-03-18 stsp }
646 abe0f35f 2020-03-18 stsp off += s->target_len;
647 abe0f35f 2020-03-18 stsp remain -= s->target_len;
648 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
649 abe0f35f 2020-03-18 stsp if (err) {
650 abe0f35f 2020-03-18 stsp free(name);
651 abe0f35f 2020-03-18 stsp free(target);
652 abe0f35f 2020-03-18 stsp goto done;
653 abe0f35f 2020-03-18 stsp }
654 abe0f35f 2020-03-18 stsp }
655 abe0f35f 2020-03-18 stsp break;
656 ea7396b9 2020-03-18 stsp case GOT_IMSG_FETCH_REF:
657 531c3985 2020-03-18 stsp if (datalen <= SHA1_DIGEST_LENGTH) {
658 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
659 531c3985 2020-03-18 stsp break;
660 531c3985 2020-03-18 stsp }
661 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
662 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
663 b9f99abf 2020-03-18 stsp err = got_error_from_errno("malloc");
664 b9f99abf 2020-03-18 stsp break;
665 b9f99abf 2020-03-18 stsp }
666 8f2d01a6 2020-03-18 stsp memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
667 b9f99abf 2020-03-18 stsp *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
668 b9f99abf 2020-03-18 stsp datalen - SHA1_DIGEST_LENGTH);
669 b9f99abf 2020-03-18 stsp if (*refname == NULL) {
670 b9f99abf 2020-03-18 stsp err = got_error_from_errno("strndup");
671 8f2d01a6 2020-03-18 stsp break;
672 8f2d01a6 2020-03-18 stsp }
673 8f2d01a6 2020-03-18 stsp break;
674 531c3985 2020-03-18 stsp case GOT_IMSG_FETCH_SERVER_PROGRESS:
675 531c3985 2020-03-18 stsp if (datalen == 0) {
676 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
677 531c3985 2020-03-18 stsp break;
678 531c3985 2020-03-18 stsp }
679 531c3985 2020-03-18 stsp *server_progress = strndup(imsg.data, datalen);
680 531c3985 2020-03-18 stsp if (*server_progress == NULL) {
681 531c3985 2020-03-18 stsp err = got_error_from_errno("strndup");
682 531c3985 2020-03-18 stsp break;
683 531c3985 2020-03-18 stsp }
684 531c3985 2020-03-18 stsp for (i = 0; i < datalen; i++) {
685 531c3985 2020-03-18 stsp if (!isprint((unsigned char)(*server_progress)[i]) &&
686 531c3985 2020-03-18 stsp !isspace((unsigned char)(*server_progress)[i])) {
687 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
688 531c3985 2020-03-18 stsp free(*server_progress);
689 531c3985 2020-03-18 stsp *server_progress = NULL;
690 531c3985 2020-03-18 stsp goto done;
691 531c3985 2020-03-18 stsp }
692 531c3985 2020-03-18 stsp }
693 531c3985 2020-03-18 stsp break;
694 d2cdc636 2020-03-18 stsp case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
695 d2cdc636 2020-03-18 stsp if (datalen < sizeof(*packfile_size)) {
696 d2cdc636 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
697 d2cdc636 2020-03-18 stsp break;
698 d2cdc636 2020-03-18 stsp }
699 d2cdc636 2020-03-18 stsp memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
700 d2cdc636 2020-03-18 stsp break;
701 8f2d01a6 2020-03-18 stsp case GOT_IMSG_FETCH_DONE:
702 8f2d01a6 2020-03-18 stsp if (datalen != SHA1_DIGEST_LENGTH) {
703 8f2d01a6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
704 8f2d01a6 2020-03-18 stsp break;
705 8f2d01a6 2020-03-18 stsp }
706 1d72a2a0 2020-03-24 stsp memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
707 8f2d01a6 2020-03-18 stsp *done = 1;
708 b9f99abf 2020-03-18 stsp break;
709 b9f99abf 2020-03-18 stsp default:
710 b887aab6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
711 b887aab6 2020-03-18 stsp break;
712 b9f99abf 2020-03-18 stsp }
713 abe0f35f 2020-03-18 stsp done:
714 b887aab6 2020-03-18 stsp if (err) {
715 8f2d01a6 2020-03-18 stsp free(*id);
716 8f2d01a6 2020-03-18 stsp *id = NULL;
717 b887aab6 2020-03-18 stsp free(*refname);
718 b887aab6 2020-03-18 stsp *refname = NULL;
719 b887aab6 2020-03-18 stsp }
720 b9f99abf 2020-03-18 stsp imsg_free(&imsg);
721 b9f99abf 2020-03-18 stsp return err;
722 93658fb9 2020-03-18 stsp }
723 93658fb9 2020-03-18 stsp
724 93658fb9 2020-03-18 stsp const struct got_error *
725 fd251256 2020-03-24 stsp got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
726 668a20f6 2020-03-18 stsp int fd)
727 93658fb9 2020-03-18 stsp {
728 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
729 93658fb9 2020-03-18 stsp
730 668a20f6 2020-03-18 stsp /* Keep in sync with struct got_imsg_index_pack_request */
731 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
732 fd251256 2020-03-24 stsp pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
733 93658fb9 2020-03-18 stsp err = got_error_from_errno("imsg_compose INDEX_REQUEST");
734 93658fb9 2020-03-18 stsp close(fd);
735 93658fb9 2020-03-18 stsp return err;
736 93658fb9 2020-03-18 stsp }
737 baa9fea0 2020-03-18 stsp return flush_imsg(ibuf);
738 baa9fea0 2020-03-18 stsp }
739 baa9fea0 2020-03-18 stsp
740 baa9fea0 2020-03-18 stsp const struct got_error *
741 73ab1060 2020-03-18 stsp got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
742 73ab1060 2020-03-18 stsp {
743 73ab1060 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
744 73ab1060 2020-03-18 stsp }
745 73ab1060 2020-03-18 stsp
746 73ab1060 2020-03-18 stsp const struct got_error *
747 668a20f6 2020-03-18 stsp got_privsep_recv_index_progress(int *done, int *nobj_total,
748 668a20f6 2020-03-18 stsp int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
749 668a20f6 2020-03-18 stsp struct imsgbuf *ibuf)
750 93658fb9 2020-03-18 stsp {
751 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
752 93658fb9 2020-03-18 stsp struct imsg imsg;
753 baa9fea0 2020-03-18 stsp struct got_imsg_index_pack_progress *iprogress;
754 baa9fea0 2020-03-18 stsp size_t datalen;
755 93658fb9 2020-03-18 stsp
756 baa9fea0 2020-03-18 stsp *done = 0;
757 668a20f6 2020-03-18 stsp *nobj_total = 0;
758 668a20f6 2020-03-18 stsp *nobj_indexed = 0;
759 668a20f6 2020-03-18 stsp *nobj_resolved = 0;
760 baa9fea0 2020-03-18 stsp
761 93658fb9 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
762 93658fb9 2020-03-18 stsp if (err)
763 93658fb9 2020-03-18 stsp return err;
764 baa9fea0 2020-03-18 stsp
765 baa9fea0 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
766 baa9fea0 2020-03-18 stsp switch (imsg.hdr.type) {
767 baa9fea0 2020-03-18 stsp case GOT_IMSG_ERROR:
768 baa9fea0 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_error)) {
769 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
770 baa9fea0 2020-03-18 stsp break;
771 baa9fea0 2020-03-18 stsp }
772 baa9fea0 2020-03-18 stsp err = recv_imsg_error(&imsg, datalen);
773 baa9fea0 2020-03-18 stsp break;
774 baa9fea0 2020-03-18 stsp case GOT_IMSG_IDXPACK_PROGRESS:
775 baa9fea0 2020-03-18 stsp if (datalen < sizeof(*iprogress)) {
776 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
777 baa9fea0 2020-03-18 stsp break;
778 baa9fea0 2020-03-18 stsp }
779 baa9fea0 2020-03-18 stsp iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
780 668a20f6 2020-03-18 stsp *nobj_total = iprogress->nobj_total;
781 668a20f6 2020-03-18 stsp *nobj_indexed = iprogress->nobj_indexed;
782 668a20f6 2020-03-18 stsp *nobj_loose = iprogress->nobj_loose;
783 668a20f6 2020-03-18 stsp *nobj_resolved = iprogress->nobj_resolved;
784 baa9fea0 2020-03-18 stsp break;
785 baa9fea0 2020-03-18 stsp case GOT_IMSG_IDXPACK_DONE:
786 baa9fea0 2020-03-18 stsp if (datalen != 0) {
787 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
788 baa9fea0 2020-03-18 stsp break;
789 baa9fea0 2020-03-18 stsp }
790 baa9fea0 2020-03-18 stsp *done = 1;
791 baa9fea0 2020-03-18 stsp break;
792 baa9fea0 2020-03-18 stsp default:
793 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
794 baa9fea0 2020-03-18 stsp break;
795 baa9fea0 2020-03-18 stsp }
796 baa9fea0 2020-03-18 stsp
797 93658fb9 2020-03-18 stsp imsg_free(&imsg);
798 baa9fea0 2020-03-18 stsp return err;
799 93658fb9 2020-03-18 stsp }
800 93658fb9 2020-03-18 stsp
801 93658fb9 2020-03-18 stsp const struct got_error *
802 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
803 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
804 cfd633c2 2018-09-10 stsp {
805 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
806 291624d8 2018-11-07 stsp struct got_imsg_object *iobj;
807 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
808 cfd633c2 2018-09-10 stsp
809 291624d8 2018-11-07 stsp if (datalen != sizeof(*iobj))
810 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
811 291624d8 2018-11-07 stsp iobj = imsg->data;
812 cfd633c2 2018-09-10 stsp
813 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
814 cfd633c2 2018-09-10 stsp if (*obj == NULL)
815 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
816 cfd633c2 2018-09-10 stsp
817 291624d8 2018-11-07 stsp memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
818 291624d8 2018-11-07 stsp (*obj)->type = iobj->type;
819 291624d8 2018-11-07 stsp (*obj)->flags = iobj->flags;
820 291624d8 2018-11-07 stsp (*obj)->hdrlen = iobj->hdrlen;
821 291624d8 2018-11-07 stsp (*obj)->size = iobj->size;
822 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
823 291624d8 2018-11-07 stsp if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
824 291624d8 2018-11-07 stsp (*obj)->pack_offset = iobj->pack_offset;
825 291624d8 2018-11-07 stsp (*obj)->pack_idx = iobj->pack_idx;
826 876c234b 2018-09-10 stsp }
827 876c234b 2018-09-10 stsp
828 876c234b 2018-09-10 stsp return err;
829 876c234b 2018-09-10 stsp }
830 876c234b 2018-09-10 stsp
831 2178c42e 2018-04-22 stsp const struct got_error *
832 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
833 2178c42e 2018-04-22 stsp {
834 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
835 2178c42e 2018-04-22 stsp struct imsg imsg;
836 c4eae628 2018-04-23 stsp const size_t min_datalen =
837 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
838 2178c42e 2018-04-22 stsp
839 2178c42e 2018-04-22 stsp *obj = NULL;
840 2178c42e 2018-04-22 stsp
841 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
842 2178c42e 2018-04-22 stsp if (err)
843 2178c42e 2018-04-22 stsp return err;
844 2178c42e 2018-04-22 stsp
845 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
846 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
847 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
848 bff6ca00 2018-04-23 stsp break;
849 bff6ca00 2018-04-23 stsp default:
850 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
851 bff6ca00 2018-04-23 stsp break;
852 bff6ca00 2018-04-23 stsp }
853 bff6ca00 2018-04-23 stsp
854 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
855 bff6ca00 2018-04-23 stsp
856 bff6ca00 2018-04-23 stsp return err;
857 c75f7264 2018-09-11 stsp }
858 c75f7264 2018-09-11 stsp
859 c75f7264 2018-09-11 stsp static const struct got_error *
860 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
861 c75f7264 2018-09-11 stsp size_t logmsg_len)
862 c75f7264 2018-09-11 stsp {
863 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
864 c75f7264 2018-09-11 stsp size_t offset, remain;
865 c75f7264 2018-09-11 stsp
866 c75f7264 2018-09-11 stsp offset = 0;
867 c75f7264 2018-09-11 stsp remain = logmsg_len;
868 c75f7264 2018-09-11 stsp while (remain > 0) {
869 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
870 c75f7264 2018-09-11 stsp
871 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
872 fa4ffeb3 2018-11-04 stsp commit->logmsg + offset, n) == -1) {
873 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose "
874 230a42bd 2019-05-11 jcs "COMMIT_LOGMSG");
875 fa4ffeb3 2018-11-04 stsp break;
876 fa4ffeb3 2018-11-04 stsp }
877 c75f7264 2018-09-11 stsp
878 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
879 fa4ffeb3 2018-11-04 stsp if (err)
880 fa4ffeb3 2018-11-04 stsp break;
881 c75f7264 2018-09-11 stsp
882 c75f7264 2018-09-11 stsp offset += n;
883 c75f7264 2018-09-11 stsp remain -= n;
884 c75f7264 2018-09-11 stsp }
885 c75f7264 2018-09-11 stsp
886 fa4ffeb3 2018-11-04 stsp return err;
887 bff6ca00 2018-04-23 stsp }
888 bff6ca00 2018-04-23 stsp
889 bff6ca00 2018-04-23 stsp const struct got_error *
890 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
891 bff6ca00 2018-04-23 stsp {
892 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
893 b9c33926 2018-11-07 stsp struct got_imsg_commit_object *icommit;
894 bff6ca00 2018-04-23 stsp uint8_t *buf;
895 bff6ca00 2018-04-23 stsp size_t len, total;
896 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
897 b9c33926 2018-11-07 stsp size_t author_len = strlen(commit->author);
898 b9c33926 2018-11-07 stsp size_t committer_len = strlen(commit->committer);
899 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
900 bff6ca00 2018-04-23 stsp
901 b9c33926 2018-11-07 stsp total = sizeof(*icommit) + author_len + committer_len +
902 b9c33926 2018-11-07 stsp commit->nparents * SHA1_DIGEST_LENGTH;
903 bff6ca00 2018-04-23 stsp
904 bff6ca00 2018-04-23 stsp buf = malloc(total);
905 bff6ca00 2018-04-23 stsp if (buf == NULL)
906 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
907 bff6ca00 2018-04-23 stsp
908 b9c33926 2018-11-07 stsp icommit = (struct got_imsg_commit_object *)buf;
909 a7403916 2018-12-24 stsp memcpy(icommit->tree_id, commit->tree_id->sha1,
910 a7403916 2018-12-24 stsp sizeof(icommit->tree_id));
911 b9c33926 2018-11-07 stsp icommit->author_len = author_len;
912 b9c33926 2018-11-07 stsp icommit->author_time = commit->author_time;
913 b9c33926 2018-11-07 stsp icommit->author_gmtoff = commit->author_gmtoff;
914 b9c33926 2018-11-07 stsp icommit->committer_len = committer_len;
915 b9c33926 2018-11-07 stsp icommit->committer_time = commit->committer_time;
916 b9c33926 2018-11-07 stsp icommit->committer_gmtoff = commit->committer_gmtoff;
917 b9c33926 2018-11-07 stsp icommit->logmsg_len = logmsg_len;
918 b9c33926 2018-11-07 stsp icommit->nparents = commit->nparents;
919 b9c33926 2018-11-07 stsp
920 b9c33926 2018-11-07 stsp len = sizeof(*icommit);
921 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->author, author_len);
922 b9c33926 2018-11-07 stsp len += author_len;
923 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->committer, committer_len);
924 b9c33926 2018-11-07 stsp len += committer_len;
925 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
926 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
927 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
928 bff6ca00 2018-04-23 stsp }
929 bff6ca00 2018-04-23 stsp
930 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
931 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT");
932 bff6ca00 2018-04-23 stsp goto done;
933 bff6ca00 2018-04-23 stsp }
934 bff6ca00 2018-04-23 stsp
935 904df868 2018-11-04 stsp if (logmsg_len == 0 ||
936 904df868 2018-11-04 stsp logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
937 904df868 2018-11-04 stsp err = flush_imsg(ibuf);
938 904df868 2018-11-04 stsp if (err)
939 904df868 2018-11-04 stsp goto done;
940 904df868 2018-11-04 stsp }
941 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
942 bff6ca00 2018-04-23 stsp done:
943 bff6ca00 2018-04-23 stsp free(buf);
944 bff6ca00 2018-04-23 stsp return err;
945 bff6ca00 2018-04-23 stsp }
946 cfd633c2 2018-09-10 stsp
947 ca6e02ac 2020-01-07 stsp static const struct got_error *
948 ca6e02ac 2020-01-07 stsp get_commit_from_imsg(struct got_commit_object **commit,
949 ca6e02ac 2020-01-07 stsp struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
950 bff6ca00 2018-04-23 stsp {
951 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
952 291624d8 2018-11-07 stsp struct got_imsg_commit_object *icommit;
953 ca6e02ac 2020-01-07 stsp size_t len = 0;
954 bff6ca00 2018-04-23 stsp int i;
955 bff6ca00 2018-04-23 stsp
956 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(*icommit))
957 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
958 bff6ca00 2018-04-23 stsp
959 ca6e02ac 2020-01-07 stsp icommit = imsg->data;
960 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommit) + icommit->author_len +
961 ca6e02ac 2020-01-07 stsp icommit->committer_len +
962 ca6e02ac 2020-01-07 stsp icommit->nparents * SHA1_DIGEST_LENGTH)
963 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
964 bff6ca00 2018-04-23 stsp
965 ca6e02ac 2020-01-07 stsp if (icommit->nparents < 0)
966 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
967 ca6e02ac 2020-01-07 stsp
968 ca6e02ac 2020-01-07 stsp len += sizeof(*icommit);
969 bff6ca00 2018-04-23 stsp
970 ca6e02ac 2020-01-07 stsp *commit = got_object_commit_alloc_partial();
971 ca6e02ac 2020-01-07 stsp if (*commit == NULL)
972 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
973 ca6e02ac 2020-01-07 stsp "got_object_commit_alloc_partial");
974 ca6e02ac 2020-01-07 stsp
975 ca6e02ac 2020-01-07 stsp memcpy((*commit)->tree_id->sha1, icommit->tree_id,
976 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
977 ca6e02ac 2020-01-07 stsp (*commit)->author_time = icommit->author_time;
978 ca6e02ac 2020-01-07 stsp (*commit)->author_gmtoff = icommit->author_gmtoff;
979 ca6e02ac 2020-01-07 stsp (*commit)->committer_time = icommit->committer_time;
980 ca6e02ac 2020-01-07 stsp (*commit)->committer_gmtoff = icommit->committer_gmtoff;
981 ca6e02ac 2020-01-07 stsp
982 ca6e02ac 2020-01-07 stsp if (icommit->author_len == 0) {
983 ca6e02ac 2020-01-07 stsp (*commit)->author = strdup("");
984 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
985 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
986 ca6e02ac 2020-01-07 stsp goto done;
987 bff6ca00 2018-04-23 stsp }
988 ca6e02ac 2020-01-07 stsp } else {
989 ca6e02ac 2020-01-07 stsp (*commit)->author = malloc(icommit->author_len + 1);
990 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
991 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
992 ca6e02ac 2020-01-07 stsp goto done;
993 ca6e02ac 2020-01-07 stsp }
994 ca6e02ac 2020-01-07 stsp memcpy((*commit)->author, imsg->data + len,
995 ca6e02ac 2020-01-07 stsp icommit->author_len);
996 ca6e02ac 2020-01-07 stsp (*commit)->author[icommit->author_len] = '\0';
997 ca6e02ac 2020-01-07 stsp }
998 ca6e02ac 2020-01-07 stsp len += icommit->author_len;
999 bff6ca00 2018-04-23 stsp
1000 ca6e02ac 2020-01-07 stsp if (icommit->committer_len == 0) {
1001 ca6e02ac 2020-01-07 stsp (*commit)->committer = strdup("");
1002 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
1003 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1004 ca6e02ac 2020-01-07 stsp goto done;
1005 ca6e02ac 2020-01-07 stsp }
1006 ca6e02ac 2020-01-07 stsp } else {
1007 ca6e02ac 2020-01-07 stsp (*commit)->committer =
1008 ca6e02ac 2020-01-07 stsp malloc(icommit->committer_len + 1);
1009 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
1010 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1011 ca6e02ac 2020-01-07 stsp goto done;
1012 ca6e02ac 2020-01-07 stsp }
1013 ca6e02ac 2020-01-07 stsp memcpy((*commit)->committer, imsg->data + len,
1014 ca6e02ac 2020-01-07 stsp icommit->committer_len);
1015 ca6e02ac 2020-01-07 stsp (*commit)->committer[icommit->committer_len] = '\0';
1016 ca6e02ac 2020-01-07 stsp }
1017 ca6e02ac 2020-01-07 stsp len += icommit->committer_len;
1018 ca6e02ac 2020-01-07 stsp
1019 ca6e02ac 2020-01-07 stsp if (icommit->logmsg_len == 0) {
1020 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = strdup("");
1021 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
1022 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1023 ca6e02ac 2020-01-07 stsp goto done;
1024 ca6e02ac 2020-01-07 stsp }
1025 ca6e02ac 2020-01-07 stsp } else {
1026 ca6e02ac 2020-01-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
1027 ca6e02ac 2020-01-07 stsp
1028 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1029 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
1030 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1031 ca6e02ac 2020-01-07 stsp goto done;
1032 bff6ca00 2018-04-23 stsp }
1033 ca6e02ac 2020-01-07 stsp while (remain > 0) {
1034 ca6e02ac 2020-01-07 stsp struct imsg imsg_log;
1035 ca6e02ac 2020-01-07 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1036 ca6e02ac 2020-01-07 stsp remain);
1037 6c281f94 2018-06-11 stsp
1038 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1039 ca6e02ac 2020-01-07 stsp if (err)
1040 ca6e02ac 2020-01-07 stsp goto done;
1041 bff6ca00 2018-04-23 stsp
1042 ca6e02ac 2020-01-07 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1043 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1044 ca6e02ac 2020-01-07 stsp goto done;
1045 bff6ca00 2018-04-23 stsp }
1046 c75f7264 2018-09-11 stsp
1047 ca6e02ac 2020-01-07 stsp memcpy((*commit)->logmsg + offset,
1048 ca6e02ac 2020-01-07 stsp imsg_log.data, n);
1049 ca6e02ac 2020-01-07 stsp imsg_free(&imsg_log);
1050 ca6e02ac 2020-01-07 stsp offset += n;
1051 ca6e02ac 2020-01-07 stsp remain -= n;
1052 bff6ca00 2018-04-23 stsp }
1053 ca6e02ac 2020-01-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
1054 ca6e02ac 2020-01-07 stsp }
1055 bff6ca00 2018-04-23 stsp
1056 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommit->nparents; i++) {
1057 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
1058 86acc566 2018-04-23 stsp
1059 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
1060 ca6e02ac 2020-01-07 stsp if (err)
1061 ca6e02ac 2020-01-07 stsp break;
1062 ca6e02ac 2020-01-07 stsp memcpy(qid->id, imsg->data + len +
1063 ca6e02ac 2020-01-07 stsp i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1064 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1065 ca6e02ac 2020-01-07 stsp (*commit)->nparents++;
1066 ca6e02ac 2020-01-07 stsp }
1067 ca6e02ac 2020-01-07 stsp done:
1068 ca6e02ac 2020-01-07 stsp if (err) {
1069 ca6e02ac 2020-01-07 stsp got_object_commit_close(*commit);
1070 ca6e02ac 2020-01-07 stsp *commit = NULL;
1071 ca6e02ac 2020-01-07 stsp }
1072 ca6e02ac 2020-01-07 stsp return err;
1073 ca6e02ac 2020-01-07 stsp }
1074 ca6e02ac 2020-01-07 stsp
1075 ca6e02ac 2020-01-07 stsp const struct got_error *
1076 ca6e02ac 2020-01-07 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1077 ca6e02ac 2020-01-07 stsp {
1078 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1079 ca6e02ac 2020-01-07 stsp struct imsg imsg;
1080 ca6e02ac 2020-01-07 stsp size_t datalen;
1081 ca6e02ac 2020-01-07 stsp const size_t min_datalen =
1082 ca6e02ac 2020-01-07 stsp MIN(sizeof(struct got_imsg_error),
1083 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_object));
1084 ca6e02ac 2020-01-07 stsp
1085 ca6e02ac 2020-01-07 stsp *commit = NULL;
1086 ca6e02ac 2020-01-07 stsp
1087 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1088 ca6e02ac 2020-01-07 stsp if (err)
1089 ca6e02ac 2020-01-07 stsp return err;
1090 ca6e02ac 2020-01-07 stsp
1091 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1092 ca6e02ac 2020-01-07 stsp
1093 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
1094 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
1095 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1096 2178c42e 2018-04-22 stsp break;
1097 8c580685 2018-04-22 stsp default:
1098 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1099 8c580685 2018-04-22 stsp break;
1100 2178c42e 2018-04-22 stsp }
1101 2178c42e 2018-04-22 stsp
1102 2178c42e 2018-04-22 stsp imsg_free(&imsg);
1103 e033d803 2018-04-23 stsp
1104 e033d803 2018-04-23 stsp return err;
1105 e033d803 2018-04-23 stsp }
1106 e033d803 2018-04-23 stsp
1107 e033d803 2018-04-23 stsp const struct got_error *
1108 3022d272 2019-11-14 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1109 3022d272 2019-11-14 stsp int nentries)
1110 e033d803 2018-04-23 stsp {
1111 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1112 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
1113 3022d272 2019-11-14 stsp struct got_pathlist_entry *pe;
1114 b00c9821 2018-11-04 stsp size_t totlen;
1115 6eb07a17 2018-11-04 stsp int nimsg; /* number of imsg queued in ibuf */
1116 e033d803 2018-04-23 stsp
1117 3022d272 2019-11-14 stsp itree.nentries = nentries;
1118 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1119 e033d803 2018-04-23 stsp == -1)
1120 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TREE");
1121 e033d803 2018-04-23 stsp
1122 b00c9821 2018-11-04 stsp totlen = sizeof(itree);
1123 6eb07a17 2018-11-04 stsp nimsg = 1;
1124 3022d272 2019-11-14 stsp TAILQ_FOREACH(pe, entries, entry) {
1125 3022d272 2019-11-14 stsp const char *name = pe->path;
1126 3022d272 2019-11-14 stsp struct got_parsed_tree_entry *pte = pe->data;
1127 3022d272 2019-11-14 stsp struct ibuf *wbuf;
1128 3022d272 2019-11-14 stsp size_t namelen = strlen(name);
1129 cd9e913a 2019-11-27 stsp size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1130 e033d803 2018-04-23 stsp
1131 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
1132 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
1133 e033d803 2018-04-23 stsp
1134 6eb07a17 2018-11-04 stsp nimsg++;
1135 6eb07a17 2018-11-04 stsp if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1136 b00c9821 2018-11-04 stsp err = flush_imsg(ibuf);
1137 b00c9821 2018-11-04 stsp if (err)
1138 b00c9821 2018-11-04 stsp return err;
1139 6eb07a17 2018-11-04 stsp nimsg = 0;
1140 b00c9821 2018-11-04 stsp }
1141 b00c9821 2018-11-04 stsp
1142 3022d272 2019-11-14 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1143 3022d272 2019-11-14 stsp if (wbuf == NULL)
1144 3022d272 2019-11-14 stsp return got_error_from_errno("imsg_create TREE_ENTRY");
1145 e033d803 2018-04-23 stsp
1146 3022d272 2019-11-14 stsp /* Keep in sync with struct got_imsg_tree_object definition! */
1147 3b647085 2019-11-23 stsp if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1148 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1149 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1150 e033d803 2018-04-23 stsp return err;
1151 3b647085 2019-11-23 stsp }
1152 3b647085 2019-11-23 stsp if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1153 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1154 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1155 3022d272 2019-11-14 stsp return err;
1156 3b647085 2019-11-23 stsp }
1157 3022d272 2019-11-14 stsp
1158 3b647085 2019-11-23 stsp if (imsg_add(wbuf, name, namelen) == -1) {
1159 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1160 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1161 3022d272 2019-11-14 stsp return err;
1162 3b647085 2019-11-23 stsp }
1163 3022d272 2019-11-14 stsp
1164 3022d272 2019-11-14 stsp wbuf->fd = -1;
1165 3022d272 2019-11-14 stsp imsg_close(ibuf, wbuf);
1166 3022d272 2019-11-14 stsp
1167 b00c9821 2018-11-04 stsp totlen += len;
1168 e033d803 2018-04-23 stsp }
1169 e033d803 2018-04-23 stsp
1170 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
1171 e033d803 2018-04-23 stsp }
1172 e033d803 2018-04-23 stsp
1173 e033d803 2018-04-23 stsp const struct got_error *
1174 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1175 e033d803 2018-04-23 stsp {
1176 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1177 e033d803 2018-04-23 stsp const size_t min_datalen =
1178 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
1179 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
1180 291624d8 2018-11-07 stsp struct got_imsg_tree_object *itree;
1181 e033d803 2018-04-23 stsp int nentries = 0;
1182 2178c42e 2018-04-22 stsp
1183 e033d803 2018-04-23 stsp *tree = NULL;
1184 e033d803 2018-04-23 stsp get_more:
1185 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
1186 e033d803 2018-04-23 stsp if (err)
1187 1e51f5b9 2018-04-23 stsp goto done;
1188 e033d803 2018-04-23 stsp
1189 656b1f76 2019-05-11 jcs for (;;) {
1190 e033d803 2018-04-23 stsp struct imsg imsg;
1191 e033d803 2018-04-23 stsp size_t n;
1192 e033d803 2018-04-23 stsp size_t datalen;
1193 c0588d8d 2018-11-07 stsp struct got_imsg_tree_entry *ite;
1194 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
1195 e033d803 2018-04-23 stsp
1196 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
1197 e033d803 2018-04-23 stsp if (n == 0) {
1198 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries)
1199 e033d803 2018-04-23 stsp goto get_more;
1200 e033d803 2018-04-23 stsp break;
1201 e033d803 2018-04-23 stsp }
1202 e033d803 2018-04-23 stsp
1203 fca1f6ad 2020-08-27 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1204 fca1f6ad 2020-08-27 stsp imsg_free(&imsg);
1205 fca1f6ad 2020-08-27 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1206 fca1f6ad 2020-08-27 stsp break;
1207 fca1f6ad 2020-08-27 stsp }
1208 e033d803 2018-04-23 stsp
1209 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1210 e033d803 2018-04-23 stsp
1211 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
1212 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
1213 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
1214 e033d803 2018-04-23 stsp break;
1215 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
1216 e033d803 2018-04-23 stsp /* This message should only appear once. */
1217 e033d803 2018-04-23 stsp if (*tree != NULL) {
1218 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1219 e033d803 2018-04-23 stsp break;
1220 e033d803 2018-04-23 stsp }
1221 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
1222 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1223 e033d803 2018-04-23 stsp break;
1224 e033d803 2018-04-23 stsp }
1225 291624d8 2018-11-07 stsp itree = imsg.data;
1226 c3b78ecc 2018-11-07 stsp *tree = malloc(sizeof(**tree));
1227 e033d803 2018-04-23 stsp if (*tree == NULL) {
1228 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1229 e033d803 2018-04-23 stsp break;
1230 e033d803 2018-04-23 stsp }
1231 56e0773d 2019-11-28 stsp (*tree)->entries = calloc(itree->nentries,
1232 56e0773d 2019-11-28 stsp sizeof(struct got_tree_entry));
1233 56e0773d 2019-11-28 stsp if ((*tree)->entries == NULL) {
1234 56e0773d 2019-11-28 stsp err = got_error_from_errno("malloc");
1235 56e0773d 2019-11-28 stsp break;
1236 56e0773d 2019-11-28 stsp }
1237 56e0773d 2019-11-28 stsp (*tree)->nentries = itree->nentries;
1238 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
1239 e033d803 2018-04-23 stsp break;
1240 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
1241 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
1242 e033d803 2018-04-23 stsp if (*tree == NULL) {
1243 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1244 e033d803 2018-04-23 stsp break;
1245 e033d803 2018-04-23 stsp }
1246 c0588d8d 2018-11-07 stsp if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1247 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1248 e033d803 2018-04-23 stsp break;
1249 e033d803 2018-04-23 stsp }
1250 e033d803 2018-04-23 stsp
1251 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
1252 c0588d8d 2018-11-07 stsp datalen -= sizeof(*ite);
1253 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1254 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1255 e033d803 2018-04-23 stsp break;
1256 e033d803 2018-04-23 stsp }
1257 c0588d8d 2018-11-07 stsp ite = imsg.data;
1258 052d4dc3 2018-04-23 stsp
1259 56e0773d 2019-11-28 stsp if (datalen + 1 > sizeof(te->name)) {
1260 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1261 e033d803 2018-04-23 stsp break;
1262 e033d803 2018-04-23 stsp }
1263 56e0773d 2019-11-28 stsp te = &(*tree)->entries[nentries];
1264 c0588d8d 2018-11-07 stsp memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1265 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
1266 e033d803 2018-04-23 stsp
1267 56e0773d 2019-11-28 stsp memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1268 c0588d8d 2018-11-07 stsp te->mode = ite->mode;
1269 56e0773d 2019-11-28 stsp te->idx = nentries;
1270 e033d803 2018-04-23 stsp nentries++;
1271 e033d803 2018-04-23 stsp break;
1272 e033d803 2018-04-23 stsp default:
1273 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1274 e033d803 2018-04-23 stsp break;
1275 e033d803 2018-04-23 stsp }
1276 e033d803 2018-04-23 stsp
1277 e033d803 2018-04-23 stsp imsg_free(&imsg);
1278 d6b7d054 2020-08-27 stsp if (err)
1279 d6b7d054 2020-08-27 stsp break;
1280 e033d803 2018-04-23 stsp }
1281 1e51f5b9 2018-04-23 stsp done:
1282 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries) {
1283 1e51f5b9 2018-04-23 stsp if (err == NULL)
1284 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1285 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
1286 e033d803 2018-04-23 stsp *tree = NULL;
1287 ff6b18f8 2018-04-24 stsp }
1288 ff6b18f8 2018-04-24 stsp
1289 ff6b18f8 2018-04-24 stsp return err;
1290 ff6b18f8 2018-04-24 stsp }
1291 ff6b18f8 2018-04-24 stsp
1292 ff6b18f8 2018-04-24 stsp const struct got_error *
1293 ac544f8c 2019-01-13 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1294 ac544f8c 2019-01-13 stsp const uint8_t *data)
1295 ff6b18f8 2018-04-24 stsp {
1296 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
1297 2967a784 2018-04-24 stsp
1298 2967a784 2018-04-24 stsp iblob.size = size;
1299 ebc55e2d 2018-12-24 stsp iblob.hdrlen = hdrlen;
1300 2967a784 2018-04-24 stsp
1301 ac544f8c 2019-01-13 stsp if (data) {
1302 ac544f8c 2019-01-13 stsp uint8_t *buf;
1303 ac544f8c 2019-01-13 stsp
1304 ac544f8c 2019-01-13 stsp if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1305 ac544f8c 2019-01-13 stsp return got_error(GOT_ERR_NO_SPACE);
1306 ac544f8c 2019-01-13 stsp
1307 ac544f8c 2019-01-13 stsp buf = malloc(sizeof(iblob) + size);
1308 ac544f8c 2019-01-13 stsp if (buf == NULL)
1309 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1310 ff6b18f8 2018-04-24 stsp
1311 ac544f8c 2019-01-13 stsp memcpy(buf, &iblob, sizeof(iblob));
1312 ac544f8c 2019-01-13 stsp memcpy(buf + sizeof(iblob), data, size);
1313 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1314 62d463ca 2020-10-20 naddy sizeof(iblob) + size) == -1) {
1315 ac544f8c 2019-01-13 stsp free(buf);
1316 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1317 ac544f8c 2019-01-13 stsp }
1318 ac544f8c 2019-01-13 stsp free(buf);
1319 ac544f8c 2019-01-13 stsp } else {
1320 ac544f8c 2019-01-13 stsp /* Data has already been written to file descriptor. */
1321 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1322 ac544f8c 2019-01-13 stsp sizeof(iblob)) == -1)
1323 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1324 ac544f8c 2019-01-13 stsp }
1325 ac544f8c 2019-01-13 stsp
1326 ac544f8c 2019-01-13 stsp
1327 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
1328 ff6b18f8 2018-04-24 stsp }
1329 ff6b18f8 2018-04-24 stsp
1330 ff6b18f8 2018-04-24 stsp const struct got_error *
1331 ac544f8c 2019-01-13 stsp got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1332 ac544f8c 2019-01-13 stsp struct imsgbuf *ibuf)
1333 ff6b18f8 2018-04-24 stsp {
1334 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1335 ff6b18f8 2018-04-24 stsp struct imsg imsg;
1336 291624d8 2018-11-07 stsp struct got_imsg_blob *iblob;
1337 ff6b18f8 2018-04-24 stsp size_t datalen;
1338 ff6b18f8 2018-04-24 stsp
1339 ac544f8c 2019-01-13 stsp *outbuf = NULL;
1340 ac544f8c 2019-01-13 stsp
1341 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1342 ff6b18f8 2018-04-24 stsp if (err)
1343 ff6b18f8 2018-04-24 stsp return err;
1344 ff6b18f8 2018-04-24 stsp
1345 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1346 ff6b18f8 2018-04-24 stsp
1347 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
1348 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
1349 ac544f8c 2019-01-13 stsp if (datalen < sizeof(*iblob)) {
1350 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1351 18336eed 2018-11-04 stsp break;
1352 18336eed 2018-11-04 stsp }
1353 291624d8 2018-11-07 stsp iblob = imsg.data;
1354 291624d8 2018-11-07 stsp *size = iblob->size;
1355 ebc55e2d 2018-12-24 stsp *hdrlen = iblob->hdrlen;
1356 ac544f8c 2019-01-13 stsp
1357 ac544f8c 2019-01-13 stsp if (datalen == sizeof(*iblob)) {
1358 ac544f8c 2019-01-13 stsp /* Data has been written to file descriptor. */
1359 ac544f8c 2019-01-13 stsp break;
1360 ac544f8c 2019-01-13 stsp }
1361 ac544f8c 2019-01-13 stsp
1362 ac544f8c 2019-01-13 stsp if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1363 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1364 ac544f8c 2019-01-13 stsp break;
1365 ac544f8c 2019-01-13 stsp }
1366 ac544f8c 2019-01-13 stsp
1367 ac544f8c 2019-01-13 stsp *outbuf = malloc(*size);
1368 ac544f8c 2019-01-13 stsp if (*outbuf == NULL) {
1369 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1370 ac544f8c 2019-01-13 stsp break;
1371 ac544f8c 2019-01-13 stsp }
1372 ac544f8c 2019-01-13 stsp memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1373 f4a881ce 2018-11-17 stsp break;
1374 f4a881ce 2018-11-17 stsp default:
1375 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1376 f4a881ce 2018-11-17 stsp break;
1377 f4a881ce 2018-11-17 stsp }
1378 f4a881ce 2018-11-17 stsp
1379 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
1380 f4a881ce 2018-11-17 stsp
1381 f4a881ce 2018-11-17 stsp return err;
1382 f4a881ce 2018-11-17 stsp }
1383 f4a881ce 2018-11-17 stsp
1384 f4a881ce 2018-11-17 stsp static const struct got_error *
1385 f4a881ce 2018-11-17 stsp send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1386 f4a881ce 2018-11-17 stsp {
1387 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1388 f4a881ce 2018-11-17 stsp size_t offset, remain;
1389 f4a881ce 2018-11-17 stsp
1390 f4a881ce 2018-11-17 stsp offset = 0;
1391 f4a881ce 2018-11-17 stsp remain = tagmsg_len;
1392 f4a881ce 2018-11-17 stsp while (remain > 0) {
1393 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1394 f4a881ce 2018-11-17 stsp
1395 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1396 f4a881ce 2018-11-17 stsp tag->tagmsg + offset, n) == -1) {
1397 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1398 f4a881ce 2018-11-17 stsp break;
1399 f4a881ce 2018-11-17 stsp }
1400 f4a881ce 2018-11-17 stsp
1401 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1402 f4a881ce 2018-11-17 stsp if (err)
1403 f4a881ce 2018-11-17 stsp break;
1404 f4a881ce 2018-11-17 stsp
1405 f4a881ce 2018-11-17 stsp offset += n;
1406 f4a881ce 2018-11-17 stsp remain -= n;
1407 f4a881ce 2018-11-17 stsp }
1408 f4a881ce 2018-11-17 stsp
1409 f4a881ce 2018-11-17 stsp return err;
1410 f4a881ce 2018-11-17 stsp }
1411 f4a881ce 2018-11-17 stsp
1412 f4a881ce 2018-11-17 stsp const struct got_error *
1413 f4a881ce 2018-11-17 stsp got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1414 f4a881ce 2018-11-17 stsp {
1415 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1416 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1417 f4a881ce 2018-11-17 stsp uint8_t *buf;
1418 f4a881ce 2018-11-17 stsp size_t len, total;
1419 f4a881ce 2018-11-17 stsp size_t tag_len = strlen(tag->tag);
1420 f4a881ce 2018-11-17 stsp size_t tagger_len = strlen(tag->tagger);
1421 f4a881ce 2018-11-17 stsp size_t tagmsg_len = strlen(tag->tagmsg);
1422 f4a881ce 2018-11-17 stsp
1423 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1424 f4a881ce 2018-11-17 stsp
1425 f4a881ce 2018-11-17 stsp buf = malloc(total);
1426 f4a881ce 2018-11-17 stsp if (buf == NULL)
1427 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1428 f4a881ce 2018-11-17 stsp
1429 f4a881ce 2018-11-17 stsp itag = (struct got_imsg_tag_object *)buf;
1430 f4a881ce 2018-11-17 stsp memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1431 f4a881ce 2018-11-17 stsp itag->obj_type = tag->obj_type;
1432 f4a881ce 2018-11-17 stsp itag->tag_len = tag_len;
1433 f4a881ce 2018-11-17 stsp itag->tagger_len = tagger_len;
1434 f4a881ce 2018-11-17 stsp itag->tagger_time = tag->tagger_time;
1435 f4a881ce 2018-11-17 stsp itag->tagger_gmtoff = tag->tagger_gmtoff;
1436 f4a881ce 2018-11-17 stsp itag->tagmsg_len = tagmsg_len;
1437 f4a881ce 2018-11-17 stsp
1438 f4a881ce 2018-11-17 stsp len = sizeof(*itag);
1439 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tag, tag_len);
1440 f4a881ce 2018-11-17 stsp len += tag_len;
1441 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tagger, tagger_len);
1442 f4a881ce 2018-11-17 stsp len += tagger_len;
1443 f4a881ce 2018-11-17 stsp
1444 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1445 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG");
1446 f4a881ce 2018-11-17 stsp goto done;
1447 f4a881ce 2018-11-17 stsp }
1448 f4a881ce 2018-11-17 stsp
1449 f4a881ce 2018-11-17 stsp if (tagmsg_len == 0 ||
1450 f4a881ce 2018-11-17 stsp tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1451 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1452 f4a881ce 2018-11-17 stsp if (err)
1453 f4a881ce 2018-11-17 stsp goto done;
1454 f4a881ce 2018-11-17 stsp }
1455 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
1456 f4a881ce 2018-11-17 stsp done:
1457 f4a881ce 2018-11-17 stsp free(buf);
1458 f4a881ce 2018-11-17 stsp return err;
1459 f4a881ce 2018-11-17 stsp }
1460 f4a881ce 2018-11-17 stsp
1461 f4a881ce 2018-11-17 stsp const struct got_error *
1462 f4a881ce 2018-11-17 stsp got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1463 f4a881ce 2018-11-17 stsp {
1464 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1465 f4a881ce 2018-11-17 stsp struct imsg imsg;
1466 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1467 f4a881ce 2018-11-17 stsp size_t len, datalen;
1468 f4a881ce 2018-11-17 stsp const size_t min_datalen =
1469 f4a881ce 2018-11-17 stsp MIN(sizeof(struct got_imsg_error),
1470 f4a881ce 2018-11-17 stsp sizeof(struct got_imsg_tag_object));
1471 f4a881ce 2018-11-17 stsp
1472 f4a881ce 2018-11-17 stsp *tag = NULL;
1473 f4a881ce 2018-11-17 stsp
1474 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1475 f4a881ce 2018-11-17 stsp if (err)
1476 f4a881ce 2018-11-17 stsp return err;
1477 f4a881ce 2018-11-17 stsp
1478 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1479 f4a881ce 2018-11-17 stsp len = 0;
1480 f4a881ce 2018-11-17 stsp
1481 f4a881ce 2018-11-17 stsp switch (imsg.hdr.type) {
1482 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG:
1483 f4a881ce 2018-11-17 stsp if (datalen < sizeof(*itag)) {
1484 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1485 f4a881ce 2018-11-17 stsp break;
1486 f4a881ce 2018-11-17 stsp }
1487 f4a881ce 2018-11-17 stsp itag = imsg.data;
1488 f4a881ce 2018-11-17 stsp if (datalen != sizeof(*itag) + itag->tag_len +
1489 f4a881ce 2018-11-17 stsp itag->tagger_len) {
1490 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1491 f4a881ce 2018-11-17 stsp break;
1492 f4a881ce 2018-11-17 stsp }
1493 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
1494 f4a881ce 2018-11-17 stsp
1495 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
1496 f4a881ce 2018-11-17 stsp if (*tag == NULL) {
1497 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1498 f4a881ce 2018-11-17 stsp break;
1499 f4a881ce 2018-11-17 stsp }
1500 f4a881ce 2018-11-17 stsp
1501 f4a881ce 2018-11-17 stsp memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1502 f4a881ce 2018-11-17 stsp
1503 f4a881ce 2018-11-17 stsp if (itag->tag_len == 0) {
1504 f4a881ce 2018-11-17 stsp (*tag)->tag = strdup("");
1505 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1506 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1507 f4a881ce 2018-11-17 stsp break;
1508 f4a881ce 2018-11-17 stsp }
1509 f4a881ce 2018-11-17 stsp } else {
1510 f4a881ce 2018-11-17 stsp (*tag)->tag = malloc(itag->tag_len + 1);
1511 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1512 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1513 f4a881ce 2018-11-17 stsp break;
1514 f4a881ce 2018-11-17 stsp }
1515 f4a881ce 2018-11-17 stsp memcpy((*tag)->tag, imsg.data + len,
1516 f4a881ce 2018-11-17 stsp itag->tag_len);
1517 f4a881ce 2018-11-17 stsp (*tag)->tag[itag->tag_len] = '\0';
1518 f4a881ce 2018-11-17 stsp }
1519 f4a881ce 2018-11-17 stsp len += itag->tag_len;
1520 f4a881ce 2018-11-17 stsp
1521 f4a881ce 2018-11-17 stsp (*tag)->obj_type = itag->obj_type;
1522 f4a881ce 2018-11-17 stsp (*tag)->tagger_time = itag->tagger_time;
1523 f4a881ce 2018-11-17 stsp (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1524 f4a881ce 2018-11-17 stsp
1525 f4a881ce 2018-11-17 stsp if (itag->tagger_len == 0) {
1526 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup("");
1527 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1528 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1529 f4a881ce 2018-11-17 stsp break;
1530 f4a881ce 2018-11-17 stsp }
1531 f4a881ce 2018-11-17 stsp } else {
1532 f4a881ce 2018-11-17 stsp (*tag)->tagger = malloc(itag->tagger_len + 1);
1533 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1534 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1535 f4a881ce 2018-11-17 stsp break;
1536 f4a881ce 2018-11-17 stsp }
1537 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagger, imsg.data + len,
1538 f4a881ce 2018-11-17 stsp itag->tagger_len);
1539 f4a881ce 2018-11-17 stsp (*tag)->tagger[itag->tagger_len] = '\0';
1540 f4a881ce 2018-11-17 stsp }
1541 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1542 f4a881ce 2018-11-17 stsp
1543 f4a881ce 2018-11-17 stsp if (itag->tagmsg_len == 0) {
1544 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strdup("");
1545 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1546 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1547 f4a881ce 2018-11-17 stsp break;
1548 f4a881ce 2018-11-17 stsp }
1549 f4a881ce 2018-11-17 stsp } else {
1550 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1551 f4a881ce 2018-11-17 stsp
1552 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1553 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1554 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1555 f4a881ce 2018-11-17 stsp break;
1556 f4a881ce 2018-11-17 stsp }
1557 f4a881ce 2018-11-17 stsp while (remain > 0) {
1558 f4a881ce 2018-11-17 stsp struct imsg imsg_log;
1559 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1560 f4a881ce 2018-11-17 stsp remain);
1561 f4a881ce 2018-11-17 stsp
1562 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1563 f4a881ce 2018-11-17 stsp if (err)
1564 f4a881ce 2018-11-17 stsp return err;
1565 f4a881ce 2018-11-17 stsp
1566 f4a881ce 2018-11-17 stsp if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1567 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1568 f4a881ce 2018-11-17 stsp
1569 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1570 f4a881ce 2018-11-17 stsp n);
1571 f4a881ce 2018-11-17 stsp imsg_free(&imsg_log);
1572 f4a881ce 2018-11-17 stsp offset += n;
1573 f4a881ce 2018-11-17 stsp remain -= n;
1574 f4a881ce 2018-11-17 stsp }
1575 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1576 f4a881ce 2018-11-17 stsp }
1577 f4a881ce 2018-11-17 stsp
1578 ff6b18f8 2018-04-24 stsp break;
1579 ff6b18f8 2018-04-24 stsp default:
1580 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1581 ff6b18f8 2018-04-24 stsp break;
1582 e033d803 2018-04-23 stsp }
1583 e033d803 2018-04-23 stsp
1584 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1585 ff6b18f8 2018-04-24 stsp
1586 2178c42e 2018-04-22 stsp return err;
1587 2178c42e 2018-04-22 stsp }
1588 876c234b 2018-09-10 stsp
1589 876c234b 2018-09-10 stsp const struct got_error *
1590 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1591 876c234b 2018-09-10 stsp struct got_packidx *packidx)
1592 876c234b 2018-09-10 stsp {
1593 41496140 2019-02-21 stsp const struct got_error *err = NULL;
1594 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1595 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1596 876c234b 2018-09-10 stsp int fd;
1597 876c234b 2018-09-10 stsp
1598 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
1599 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
1600 876c234b 2018-09-10 stsp if (fd == -1)
1601 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1602 876c234b 2018-09-10 stsp
1603 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1604 41496140 2019-02-21 stsp sizeof(ipackidx)) == -1) {
1605 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACKIDX");
1606 41496140 2019-02-21 stsp close(fd);
1607 41496140 2019-02-21 stsp return err;
1608 41496140 2019-02-21 stsp }
1609 876c234b 2018-09-10 stsp
1610 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
1611 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1612 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
1613 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
1614 876c234b 2018-09-10 stsp
1615 876c234b 2018-09-10 stsp fd = dup(pack->fd);
1616 876c234b 2018-09-10 stsp if (fd == -1)
1617 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1618 876c234b 2018-09-10 stsp
1619 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1620 41496140 2019-02-21 stsp == -1) {
1621 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACK");
1622 41496140 2019-02-21 stsp close(fd);
1623 41496140 2019-02-21 stsp return err;
1624 41496140 2019-02-21 stsp }
1625 876c234b 2018-09-10 stsp
1626 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1627 876c234b 2018-09-10 stsp }
1628 876c234b 2018-09-10 stsp
1629 876c234b 2018-09-10 stsp const struct got_error *
1630 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1631 106807b4 2018-09-15 stsp struct got_object_id *id)
1632 876c234b 2018-09-10 stsp {
1633 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
1634 876c234b 2018-09-10 stsp
1635 876c234b 2018-09-10 stsp iobj.idx = idx;
1636 106807b4 2018-09-15 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1637 876c234b 2018-09-10 stsp
1638 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1639 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
1640 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose "
1641 230a42bd 2019-05-11 jcs "PACKED_OBJECT_REQUEST");
1642 aba9c984 2019-09-08 stsp
1643 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1644 aba9c984 2019-09-08 stsp }
1645 aba9c984 2019-09-08 stsp
1646 aba9c984 2019-09-08 stsp const struct got_error *
1647 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1648 aba9c984 2019-09-08 stsp {
1649 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1650 aba9c984 2019-09-08 stsp
1651 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1652 aba9c984 2019-09-08 stsp NULL, 0) == -1) {
1653 aba9c984 2019-09-08 stsp err = got_error_from_errno("imsg_compose "
1654 aba9c984 2019-09-08 stsp "GITCONFIG_PARSE_REQUEST");
1655 aba9c984 2019-09-08 stsp close(fd);
1656 aba9c984 2019-09-08 stsp return err;
1657 aba9c984 2019-09-08 stsp }
1658 aba9c984 2019-09-08 stsp
1659 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1660 aba9c984 2019-09-08 stsp }
1661 aba9c984 2019-09-08 stsp
1662 aba9c984 2019-09-08 stsp const struct got_error *
1663 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1664 aba9c984 2019-09-08 stsp {
1665 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1666 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1667 aba9c984 2019-09-08 stsp NULL, 0) == -1)
1668 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1669 aba9c984 2019-09-08 stsp "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1670 aba9c984 2019-09-08 stsp
1671 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1672 aba9c984 2019-09-08 stsp }
1673 aba9c984 2019-09-08 stsp
1674 aba9c984 2019-09-08 stsp const struct got_error *
1675 20b7abb3 2020-10-22 stsp got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
1676 20b7abb3 2020-10-22 stsp {
1677 20b7abb3 2020-10-22 stsp if (imsg_compose(ibuf,
1678 20b7abb3 2020-10-22 stsp GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
1679 20b7abb3 2020-10-22 stsp NULL, 0) == -1)
1680 20b7abb3 2020-10-22 stsp return got_error_from_errno("imsg_compose "
1681 20b7abb3 2020-10-22 stsp "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
1682 20b7abb3 2020-10-22 stsp
1683 20b7abb3 2020-10-22 stsp return flush_imsg(ibuf);
1684 20b7abb3 2020-10-22 stsp }
1685 20b7abb3 2020-10-22 stsp
1686 20b7abb3 2020-10-22 stsp
1687 20b7abb3 2020-10-22 stsp const struct got_error *
1688 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1689 aba9c984 2019-09-08 stsp {
1690 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1691 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1692 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1693 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_NAME_REQUEST");
1694 aba9c984 2019-09-08 stsp
1695 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1696 aba9c984 2019-09-08 stsp }
1697 aba9c984 2019-09-08 stsp
1698 aba9c984 2019-09-08 stsp const struct got_error *
1699 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1700 aba9c984 2019-09-08 stsp {
1701 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1702 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1703 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1704 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1705 cd95becd 2019-11-29 stsp
1706 cd95becd 2019-11-29 stsp return flush_imsg(ibuf);
1707 cd95becd 2019-11-29 stsp }
1708 cd95becd 2019-11-29 stsp
1709 cd95becd 2019-11-29 stsp const struct got_error *
1710 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1711 cd95becd 2019-11-29 stsp {
1712 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf,
1713 cd95becd 2019-11-29 stsp GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1714 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose "
1715 cd95becd 2019-11-29 stsp "GITCONFIG_REMOTE_REQUEST");
1716 aba9c984 2019-09-08 stsp
1717 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1718 aba9c984 2019-09-08 stsp }
1719 aba9c984 2019-09-08 stsp
1720 aba9c984 2019-09-08 stsp const struct got_error *
1721 9a1cc63f 2020-02-03 stsp got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1722 9a1cc63f 2020-02-03 stsp {
1723 9a1cc63f 2020-02-03 stsp if (imsg_compose(ibuf,
1724 9a1cc63f 2020-02-03 stsp GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1725 9a1cc63f 2020-02-03 stsp return got_error_from_errno("imsg_compose "
1726 9a1cc63f 2020-02-03 stsp "GITCONFIG_OWNER_REQUEST");
1727 9a1cc63f 2020-02-03 stsp
1728 9a1cc63f 2020-02-03 stsp return flush_imsg(ibuf);
1729 9a1cc63f 2020-02-03 stsp }
1730 9a1cc63f 2020-02-03 stsp
1731 9a1cc63f 2020-02-03 stsp const struct got_error *
1732 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1733 aba9c984 2019-09-08 stsp {
1734 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1735 aba9c984 2019-09-08 stsp struct imsg imsg;
1736 aba9c984 2019-09-08 stsp size_t datalen;
1737 aba9c984 2019-09-08 stsp const size_t min_datalen = 0;
1738 aba9c984 2019-09-08 stsp
1739 aba9c984 2019-09-08 stsp *str = NULL;
1740 aba9c984 2019-09-08 stsp
1741 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1742 aba9c984 2019-09-08 stsp if (err)
1743 aba9c984 2019-09-08 stsp return err;
1744 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1745 aba9c984 2019-09-08 stsp
1746 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1747 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_STR_VAL:
1748 aba9c984 2019-09-08 stsp if (datalen == 0)
1749 aba9c984 2019-09-08 stsp break;
1750 6c13dcd2 2020-09-18 stsp /* datalen does not include terminating \0 */
1751 6c13dcd2 2020-09-18 stsp *str = malloc(datalen + 1);
1752 aba9c984 2019-09-08 stsp if (*str == NULL) {
1753 aba9c984 2019-09-08 stsp err = got_error_from_errno("malloc");
1754 aba9c984 2019-09-08 stsp break;
1755 aba9c984 2019-09-08 stsp }
1756 6c13dcd2 2020-09-18 stsp memcpy(*str, imsg.data, datalen);
1757 6c13dcd2 2020-09-18 stsp (*str)[datalen] = '\0';
1758 aba9c984 2019-09-08 stsp break;
1759 aba9c984 2019-09-08 stsp default:
1760 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1761 aba9c984 2019-09-08 stsp break;
1762 aba9c984 2019-09-08 stsp }
1763 876c234b 2018-09-10 stsp
1764 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1765 aba9c984 2019-09-08 stsp return err;
1766 aba9c984 2019-09-08 stsp }
1767 aba9c984 2019-09-08 stsp
1768 aba9c984 2019-09-08 stsp const struct got_error *
1769 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1770 aba9c984 2019-09-08 stsp {
1771 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1772 aba9c984 2019-09-08 stsp struct imsg imsg;
1773 aba9c984 2019-09-08 stsp size_t datalen;
1774 aba9c984 2019-09-08 stsp const size_t min_datalen =
1775 aba9c984 2019-09-08 stsp MIN(sizeof(struct got_imsg_error), sizeof(int));
1776 aba9c984 2019-09-08 stsp
1777 aba9c984 2019-09-08 stsp *val = 0;
1778 aba9c984 2019-09-08 stsp
1779 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1780 aba9c984 2019-09-08 stsp if (err)
1781 aba9c984 2019-09-08 stsp return err;
1782 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1783 aba9c984 2019-09-08 stsp
1784 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1785 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_INT_VAL:
1786 aba9c984 2019-09-08 stsp if (datalen != sizeof(*val)) {
1787 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1788 aba9c984 2019-09-08 stsp break;
1789 aba9c984 2019-09-08 stsp }
1790 aba9c984 2019-09-08 stsp memcpy(val, imsg.data, sizeof(*val));
1791 cd95becd 2019-11-29 stsp break;
1792 cd95becd 2019-11-29 stsp default:
1793 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1794 cd95becd 2019-11-29 stsp break;
1795 cd95becd 2019-11-29 stsp }
1796 cd95becd 2019-11-29 stsp
1797 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1798 cd95becd 2019-11-29 stsp return err;
1799 b8adfa55 2020-09-25 stsp }
1800 b8adfa55 2020-09-25 stsp
1801 b8adfa55 2020-09-25 stsp static void
1802 b8adfa55 2020-09-25 stsp free_remote_data(struct got_remote_repo *remote)
1803 b8adfa55 2020-09-25 stsp {
1804 b8adfa55 2020-09-25 stsp int i;
1805 b8adfa55 2020-09-25 stsp
1806 b8adfa55 2020-09-25 stsp free(remote->name);
1807 b8adfa55 2020-09-25 stsp free(remote->url);
1808 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++)
1809 b8adfa55 2020-09-25 stsp free(remote->branches[i]);
1810 b8adfa55 2020-09-25 stsp free(remote->branches);
1811 99495ddb 2021-01-10 stsp for (i = 0; i < remote->nrefs; i++)
1812 99495ddb 2021-01-10 stsp free(remote->refs[i]);
1813 99495ddb 2021-01-10 stsp free(remote->refs);
1814 cd95becd 2019-11-29 stsp }
1815 cd95becd 2019-11-29 stsp
1816 cd95becd 2019-11-29 stsp const struct got_error *
1817 cd95becd 2019-11-29 stsp got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1818 cd95becd 2019-11-29 stsp int *nremotes, struct imsgbuf *ibuf)
1819 cd95becd 2019-11-29 stsp {
1820 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1821 cd95becd 2019-11-29 stsp struct imsg imsg;
1822 cd95becd 2019-11-29 stsp size_t datalen;
1823 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
1824 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
1825 cd95becd 2019-11-29 stsp
1826 cd95becd 2019-11-29 stsp *remotes = NULL;
1827 cd95becd 2019-11-29 stsp *nremotes = 0;
1828 d669b9c9 2020-02-22 stsp iremotes.nremotes = 0;
1829 cd95becd 2019-11-29 stsp
1830 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1831 cd95becd 2019-11-29 stsp if (err)
1832 cd95becd 2019-11-29 stsp return err;
1833 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1834 cd95becd 2019-11-29 stsp
1835 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
1836 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES:
1837 cd95becd 2019-11-29 stsp if (datalen != sizeof(iremotes)) {
1838 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1839 cd95becd 2019-11-29 stsp break;
1840 cd95becd 2019-11-29 stsp }
1841 cd95becd 2019-11-29 stsp memcpy(&iremotes, imsg.data, sizeof(iremotes));
1842 cd95becd 2019-11-29 stsp if (iremotes.nremotes == 0) {
1843 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1844 cd95becd 2019-11-29 stsp return NULL;
1845 cd95becd 2019-11-29 stsp }
1846 aba9c984 2019-09-08 stsp break;
1847 aba9c984 2019-09-08 stsp default:
1848 54b1c5b5 2020-02-22 stsp imsg_free(&imsg);
1849 54b1c5b5 2020-02-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1850 aba9c984 2019-09-08 stsp }
1851 aba9c984 2019-09-08 stsp
1852 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1853 cd95becd 2019-11-29 stsp
1854 5146eb39 2020-03-20 stsp *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
1855 cd95becd 2019-11-29 stsp if (*remotes == NULL)
1856 cd95becd 2019-11-29 stsp return got_error_from_errno("recallocarray");
1857 cd95becd 2019-11-29 stsp
1858 cd95becd 2019-11-29 stsp while (*nremotes < iremotes.nremotes) {
1859 cd95becd 2019-11-29 stsp struct got_remote_repo *remote;
1860 3168e5da 2020-09-10 stsp
1861 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1862 cd95becd 2019-11-29 stsp if (err)
1863 cd95becd 2019-11-29 stsp break;
1864 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1865 cd95becd 2019-11-29 stsp
1866 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
1867 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTE:
1868 cd95becd 2019-11-29 stsp remote = &(*remotes)[*nremotes];
1869 b8adfa55 2020-09-25 stsp memset(remote, 0, sizeof(*remote));
1870 cd95becd 2019-11-29 stsp if (datalen < sizeof(iremote)) {
1871 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1872 cd95becd 2019-11-29 stsp break;
1873 cd95becd 2019-11-29 stsp }
1874 cd95becd 2019-11-29 stsp memcpy(&iremote, imsg.data, sizeof(iremote));
1875 cd95becd 2019-11-29 stsp if (iremote.name_len == 0 || iremote.url_len == 0 ||
1876 cd95becd 2019-11-29 stsp (sizeof(iremote) + iremote.name_len +
1877 cd95becd 2019-11-29 stsp iremote.url_len) > datalen) {
1878 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1879 cd95becd 2019-11-29 stsp break;
1880 cd95becd 2019-11-29 stsp }
1881 cd95becd 2019-11-29 stsp remote->name = strndup(imsg.data + sizeof(iremote),
1882 cd95becd 2019-11-29 stsp iremote.name_len);
1883 cd95becd 2019-11-29 stsp if (remote->name == NULL) {
1884 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
1885 cd95becd 2019-11-29 stsp break;
1886 cd95becd 2019-11-29 stsp }
1887 cd95becd 2019-11-29 stsp remote->url = strndup(imsg.data + sizeof(iremote) +
1888 cd95becd 2019-11-29 stsp iremote.name_len, iremote.url_len);
1889 cd95becd 2019-11-29 stsp if (remote->url == NULL) {
1890 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
1891 b8adfa55 2020-09-25 stsp free_remote_data(remote);
1892 cd95becd 2019-11-29 stsp break;
1893 cd95becd 2019-11-29 stsp }
1894 469dd726 2020-03-20 stsp remote->mirror_references = iremote.mirror_references;
1895 0c8b29c5 2021-01-05 stsp remote->fetch_all_branches = iremote.fetch_all_branches;
1896 b8adfa55 2020-09-25 stsp remote->nbranches = 0;
1897 b8adfa55 2020-09-25 stsp remote->branches = NULL;
1898 99495ddb 2021-01-10 stsp remote->nrefs = 0;
1899 99495ddb 2021-01-10 stsp remote->refs = NULL;
1900 cd95becd 2019-11-29 stsp (*nremotes)++;
1901 cd95becd 2019-11-29 stsp break;
1902 cd95becd 2019-11-29 stsp default:
1903 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1904 cd95becd 2019-11-29 stsp break;
1905 cd95becd 2019-11-29 stsp }
1906 cd95becd 2019-11-29 stsp
1907 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1908 cd95becd 2019-11-29 stsp if (err)
1909 cd95becd 2019-11-29 stsp break;
1910 cd95becd 2019-11-29 stsp }
1911 cd95becd 2019-11-29 stsp
1912 cd95becd 2019-11-29 stsp if (err) {
1913 cd95becd 2019-11-29 stsp int i;
1914 b8adfa55 2020-09-25 stsp for (i = 0; i < *nremotes; i++)
1915 b8adfa55 2020-09-25 stsp free_remote_data(&(*remotes)[i]);
1916 cd95becd 2019-11-29 stsp free(*remotes);
1917 cd95becd 2019-11-29 stsp *remotes = NULL;
1918 cd95becd 2019-11-29 stsp *nremotes = 0;
1919 cd95becd 2019-11-29 stsp }
1920 aba9c984 2019-09-08 stsp return err;
1921 257add31 2020-09-09 stsp }
1922 257add31 2020-09-09 stsp
1923 257add31 2020-09-09 stsp const struct got_error *
1924 257add31 2020-09-09 stsp got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
1925 257add31 2020-09-09 stsp {
1926 257add31 2020-09-09 stsp const struct got_error *err = NULL;
1927 257add31 2020-09-09 stsp
1928 257add31 2020-09-09 stsp if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
1929 257add31 2020-09-09 stsp NULL, 0) == -1) {
1930 257add31 2020-09-09 stsp err = got_error_from_errno("imsg_compose "
1931 257add31 2020-09-09 stsp "GOTCONFIG_PARSE_REQUEST");
1932 257add31 2020-09-09 stsp close(fd);
1933 257add31 2020-09-09 stsp return err;
1934 257add31 2020-09-09 stsp }
1935 257add31 2020-09-09 stsp
1936 257add31 2020-09-09 stsp return flush_imsg(ibuf);
1937 257add31 2020-09-09 stsp }
1938 257add31 2020-09-09 stsp
1939 257add31 2020-09-09 stsp const struct got_error *
1940 257add31 2020-09-09 stsp got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
1941 257add31 2020-09-09 stsp {
1942 257add31 2020-09-09 stsp if (imsg_compose(ibuf,
1943 257add31 2020-09-09 stsp GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
1944 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose "
1945 257add31 2020-09-09 stsp "GOTCONFIG_AUTHOR_REQUEST");
1946 257add31 2020-09-09 stsp
1947 257add31 2020-09-09 stsp return flush_imsg(ibuf);
1948 ca6e02ac 2020-01-07 stsp }
1949 ca6e02ac 2020-01-07 stsp
1950 ca6e02ac 2020-01-07 stsp const struct got_error *
1951 257add31 2020-09-09 stsp got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
1952 257add31 2020-09-09 stsp {
1953 257add31 2020-09-09 stsp if (imsg_compose(ibuf,
1954 257add31 2020-09-09 stsp GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1955 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose "
1956 257add31 2020-09-09 stsp "GOTCONFIG_REMOTE_REQUEST");
1957 257add31 2020-09-09 stsp
1958 257add31 2020-09-09 stsp return flush_imsg(ibuf);
1959 257add31 2020-09-09 stsp }
1960 257add31 2020-09-09 stsp
1961 257add31 2020-09-09 stsp const struct got_error *
1962 257add31 2020-09-09 stsp got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
1963 257add31 2020-09-09 stsp {
1964 257add31 2020-09-09 stsp const struct got_error *err = NULL;
1965 257add31 2020-09-09 stsp struct imsg imsg;
1966 257add31 2020-09-09 stsp size_t datalen;
1967 257add31 2020-09-09 stsp const size_t min_datalen = 0;
1968 257add31 2020-09-09 stsp
1969 257add31 2020-09-09 stsp *str = NULL;
1970 257add31 2020-09-09 stsp
1971 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1972 257add31 2020-09-09 stsp if (err)
1973 257add31 2020-09-09 stsp return err;
1974 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1975 257add31 2020-09-09 stsp
1976 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
1977 257add31 2020-09-09 stsp case GOT_IMSG_ERROR:
1978 257add31 2020-09-09 stsp if (datalen < sizeof(struct got_imsg_error)) {
1979 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1980 257add31 2020-09-09 stsp break;
1981 257add31 2020-09-09 stsp }
1982 257add31 2020-09-09 stsp err = recv_imsg_error(&imsg, datalen);
1983 257add31 2020-09-09 stsp break;
1984 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_STR_VAL:
1985 257add31 2020-09-09 stsp if (datalen == 0)
1986 257add31 2020-09-09 stsp break;
1987 5874ea87 2020-09-18 stsp /* datalen does not include terminating \0 */
1988 5874ea87 2020-09-18 stsp *str = malloc(datalen + 1);
1989 257add31 2020-09-09 stsp if (*str == NULL) {
1990 257add31 2020-09-09 stsp err = got_error_from_errno("malloc");
1991 257add31 2020-09-09 stsp break;
1992 257add31 2020-09-09 stsp }
1993 5874ea87 2020-09-18 stsp memcpy(*str, imsg.data, datalen);
1994 5874ea87 2020-09-18 stsp (*str)[datalen] = '\0';
1995 257add31 2020-09-09 stsp break;
1996 257add31 2020-09-09 stsp default:
1997 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1998 257add31 2020-09-09 stsp break;
1999 257add31 2020-09-09 stsp }
2000 257add31 2020-09-09 stsp
2001 257add31 2020-09-09 stsp imsg_free(&imsg);
2002 257add31 2020-09-09 stsp return err;
2003 257add31 2020-09-09 stsp }
2004 257add31 2020-09-09 stsp
2005 b8adfa55 2020-09-25 stsp
2006 257add31 2020-09-09 stsp const struct got_error *
2007 257add31 2020-09-09 stsp got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2008 257add31 2020-09-09 stsp int *nremotes, struct imsgbuf *ibuf)
2009 257add31 2020-09-09 stsp {
2010 257add31 2020-09-09 stsp const struct got_error *err = NULL;
2011 257add31 2020-09-09 stsp struct imsg imsg;
2012 257add31 2020-09-09 stsp size_t datalen;
2013 257add31 2020-09-09 stsp struct got_imsg_remotes iremotes;
2014 257add31 2020-09-09 stsp struct got_imsg_remote iremote;
2015 257add31 2020-09-09 stsp const size_t min_datalen =
2016 257add31 2020-09-09 stsp MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2017 257add31 2020-09-09 stsp
2018 257add31 2020-09-09 stsp *remotes = NULL;
2019 257add31 2020-09-09 stsp *nremotes = 0;
2020 257add31 2020-09-09 stsp iremotes.nremotes = 0;
2021 257add31 2020-09-09 stsp
2022 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2023 257add31 2020-09-09 stsp if (err)
2024 257add31 2020-09-09 stsp return err;
2025 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2026 257add31 2020-09-09 stsp
2027 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
2028 257add31 2020-09-09 stsp case GOT_IMSG_ERROR:
2029 257add31 2020-09-09 stsp if (datalen < sizeof(struct got_imsg_error)) {
2030 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2031 257add31 2020-09-09 stsp break;
2032 257add31 2020-09-09 stsp }
2033 257add31 2020-09-09 stsp err = recv_imsg_error(&imsg, datalen);
2034 257add31 2020-09-09 stsp break;
2035 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_REMOTES:
2036 257add31 2020-09-09 stsp if (datalen != sizeof(iremotes)) {
2037 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2038 257add31 2020-09-09 stsp break;
2039 257add31 2020-09-09 stsp }
2040 257add31 2020-09-09 stsp memcpy(&iremotes, imsg.data, sizeof(iremotes));
2041 257add31 2020-09-09 stsp if (iremotes.nremotes == 0) {
2042 257add31 2020-09-09 stsp imsg_free(&imsg);
2043 257add31 2020-09-09 stsp return NULL;
2044 257add31 2020-09-09 stsp }
2045 257add31 2020-09-09 stsp break;
2046 257add31 2020-09-09 stsp default:
2047 257add31 2020-09-09 stsp imsg_free(&imsg);
2048 257add31 2020-09-09 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
2049 257add31 2020-09-09 stsp }
2050 257add31 2020-09-09 stsp
2051 257add31 2020-09-09 stsp imsg_free(&imsg);
2052 257add31 2020-09-09 stsp
2053 257add31 2020-09-09 stsp *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2054 257add31 2020-09-09 stsp if (*remotes == NULL)
2055 257add31 2020-09-09 stsp return got_error_from_errno("recallocarray");
2056 257add31 2020-09-09 stsp
2057 257add31 2020-09-09 stsp while (*nremotes < iremotes.nremotes) {
2058 257add31 2020-09-09 stsp struct got_remote_repo *remote;
2059 257add31 2020-09-09 stsp const size_t min_datalen =
2060 257add31 2020-09-09 stsp MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2061 b8adfa55 2020-09-25 stsp int i;
2062 257add31 2020-09-09 stsp
2063 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2064 257add31 2020-09-09 stsp if (err)
2065 257add31 2020-09-09 stsp break;
2066 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2067 257add31 2020-09-09 stsp
2068 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
2069 257add31 2020-09-09 stsp case GOT_IMSG_ERROR:
2070 257add31 2020-09-09 stsp if (datalen < sizeof(struct got_imsg_error)) {
2071 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2072 257add31 2020-09-09 stsp break;
2073 257add31 2020-09-09 stsp }
2074 257add31 2020-09-09 stsp err = recv_imsg_error(&imsg, datalen);
2075 257add31 2020-09-09 stsp break;
2076 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_REMOTE:
2077 257add31 2020-09-09 stsp remote = &(*remotes)[*nremotes];
2078 b8adfa55 2020-09-25 stsp memset(remote, 0, sizeof(*remote));
2079 257add31 2020-09-09 stsp if (datalen < sizeof(iremote)) {
2080 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2081 257add31 2020-09-09 stsp break;
2082 257add31 2020-09-09 stsp }
2083 257add31 2020-09-09 stsp memcpy(&iremote, imsg.data, sizeof(iremote));
2084 257add31 2020-09-09 stsp if (iremote.name_len == 0 || iremote.url_len == 0 ||
2085 257add31 2020-09-09 stsp (sizeof(iremote) + iremote.name_len +
2086 257add31 2020-09-09 stsp iremote.url_len) > datalen) {
2087 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2088 257add31 2020-09-09 stsp break;
2089 257add31 2020-09-09 stsp }
2090 257add31 2020-09-09 stsp remote->name = strndup(imsg.data + sizeof(iremote),
2091 257add31 2020-09-09 stsp iremote.name_len);
2092 257add31 2020-09-09 stsp if (remote->name == NULL) {
2093 257add31 2020-09-09 stsp err = got_error_from_errno("strndup");
2094 257add31 2020-09-09 stsp break;
2095 257add31 2020-09-09 stsp }
2096 257add31 2020-09-09 stsp remote->url = strndup(imsg.data + sizeof(iremote) +
2097 257add31 2020-09-09 stsp iremote.name_len, iremote.url_len);
2098 257add31 2020-09-09 stsp if (remote->url == NULL) {
2099 257add31 2020-09-09 stsp err = got_error_from_errno("strndup");
2100 b8adfa55 2020-09-25 stsp free_remote_data(remote);
2101 257add31 2020-09-09 stsp break;
2102 257add31 2020-09-09 stsp }
2103 257add31 2020-09-09 stsp remote->mirror_references = iremote.mirror_references;
2104 0c8b29c5 2021-01-05 stsp remote->fetch_all_branches = iremote.fetch_all_branches;
2105 b8adfa55 2020-09-25 stsp if (iremote.nbranches > 0) {
2106 b8adfa55 2020-09-25 stsp remote->branches = recallocarray(NULL, 0,
2107 b8adfa55 2020-09-25 stsp iremote.nbranches, sizeof(char *));
2108 b8adfa55 2020-09-25 stsp if (remote->branches == NULL) {
2109 b8adfa55 2020-09-25 stsp err = got_error_from_errno("calloc");
2110 b8adfa55 2020-09-25 stsp free_remote_data(remote);
2111 b8adfa55 2020-09-25 stsp break;
2112 b8adfa55 2020-09-25 stsp }
2113 b8adfa55 2020-09-25 stsp }
2114 b8adfa55 2020-09-25 stsp remote->nbranches = 0;
2115 b8adfa55 2020-09-25 stsp for (i = 0; i < iremote.nbranches; i++) {
2116 b8adfa55 2020-09-25 stsp char *branch;
2117 b8adfa55 2020-09-25 stsp err = got_privsep_recv_gotconfig_str(&branch,
2118 b8adfa55 2020-09-25 stsp ibuf);
2119 b8adfa55 2020-09-25 stsp if (err) {
2120 b8adfa55 2020-09-25 stsp free_remote_data(remote);
2121 b8adfa55 2020-09-25 stsp goto done;
2122 b8adfa55 2020-09-25 stsp }
2123 b8adfa55 2020-09-25 stsp remote->branches[i] = branch;
2124 b8adfa55 2020-09-25 stsp remote->nbranches++;
2125 99495ddb 2021-01-10 stsp }
2126 99495ddb 2021-01-10 stsp if (iremote.nrefs > 0) {
2127 99495ddb 2021-01-10 stsp remote->refs = recallocarray(NULL, 0,
2128 99495ddb 2021-01-10 stsp iremote.nrefs, sizeof(char *));
2129 99495ddb 2021-01-10 stsp if (remote->refs == NULL) {
2130 99495ddb 2021-01-10 stsp err = got_error_from_errno("calloc");
2131 99495ddb 2021-01-10 stsp free_remote_data(remote);
2132 99495ddb 2021-01-10 stsp break;
2133 99495ddb 2021-01-10 stsp }
2134 b8adfa55 2020-09-25 stsp }
2135 99495ddb 2021-01-10 stsp remote->nrefs = 0;
2136 99495ddb 2021-01-10 stsp for (i = 0; i < iremote.nrefs; i++) {
2137 99495ddb 2021-01-10 stsp char *ref;
2138 99495ddb 2021-01-10 stsp err = got_privsep_recv_gotconfig_str(&ref,
2139 99495ddb 2021-01-10 stsp ibuf);
2140 99495ddb 2021-01-10 stsp if (err) {
2141 99495ddb 2021-01-10 stsp free_remote_data(remote);
2142 99495ddb 2021-01-10 stsp goto done;
2143 99495ddb 2021-01-10 stsp }
2144 99495ddb 2021-01-10 stsp remote->refs[i] = ref;
2145 99495ddb 2021-01-10 stsp remote->nrefs++;
2146 99495ddb 2021-01-10 stsp }
2147 257add31 2020-09-09 stsp (*nremotes)++;
2148 257add31 2020-09-09 stsp break;
2149 257add31 2020-09-09 stsp default:
2150 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2151 257add31 2020-09-09 stsp break;
2152 257add31 2020-09-09 stsp }
2153 257add31 2020-09-09 stsp
2154 257add31 2020-09-09 stsp imsg_free(&imsg);
2155 257add31 2020-09-09 stsp if (err)
2156 257add31 2020-09-09 stsp break;
2157 257add31 2020-09-09 stsp }
2158 b8adfa55 2020-09-25 stsp done:
2159 257add31 2020-09-09 stsp if (err) {
2160 257add31 2020-09-09 stsp int i;
2161 b8adfa55 2020-09-25 stsp for (i = 0; i < *nremotes; i++)
2162 b8adfa55 2020-09-25 stsp free_remote_data(&(*remotes)[i]);
2163 257add31 2020-09-09 stsp free(*remotes);
2164 257add31 2020-09-09 stsp *remotes = NULL;
2165 257add31 2020-09-09 stsp *nremotes = 0;
2166 257add31 2020-09-09 stsp }
2167 257add31 2020-09-09 stsp return err;
2168 257add31 2020-09-09 stsp }
2169 257add31 2020-09-09 stsp
2170 257add31 2020-09-09 stsp const struct got_error *
2171 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2172 ca6e02ac 2020-01-07 stsp struct got_object_id *id, int idx, const char *path)
2173 ca6e02ac 2020-01-07 stsp {
2174 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2175 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
2176 ca6e02ac 2020-01-07 stsp size_t path_len = strlen(path) + 1;
2177 ca6e02ac 2020-01-07 stsp
2178 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2179 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_traversal_request) + path_len);
2180 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
2181 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
2182 ca6e02ac 2020-01-07 stsp "imsg_create COMMIT_TRAVERSAL_REQUEST");
2183 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2184 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2185 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2186 ca6e02ac 2020-01-07 stsp return err;
2187 ca6e02ac 2020-01-07 stsp }
2188 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2189 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2190 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2191 ca6e02ac 2020-01-07 stsp return err;
2192 ca6e02ac 2020-01-07 stsp }
2193 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, path, path_len) == -1) {
2194 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2195 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2196 ca6e02ac 2020-01-07 stsp return err;
2197 ca6e02ac 2020-01-07 stsp }
2198 ca6e02ac 2020-01-07 stsp
2199 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
2200 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
2201 ca6e02ac 2020-01-07 stsp
2202 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2203 ca6e02ac 2020-01-07 stsp }
2204 ca6e02ac 2020-01-07 stsp
2205 ca6e02ac 2020-01-07 stsp const struct got_error *
2206 ca6e02ac 2020-01-07 stsp got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2207 ca6e02ac 2020-01-07 stsp struct got_object_id **changed_commit_id,
2208 ca6e02ac 2020-01-07 stsp struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2209 ca6e02ac 2020-01-07 stsp {
2210 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2211 ca6e02ac 2020-01-07 stsp struct imsg imsg;
2212 ca6e02ac 2020-01-07 stsp struct got_imsg_traversed_commits *icommits;
2213 ca6e02ac 2020-01-07 stsp size_t datalen;
2214 ca6e02ac 2020-01-07 stsp int i, done = 0;
2215 ca6e02ac 2020-01-07 stsp
2216 ca6e02ac 2020-01-07 stsp *changed_commit = NULL;
2217 ca6e02ac 2020-01-07 stsp *changed_commit_id = NULL;
2218 ca6e02ac 2020-01-07 stsp
2219 ca6e02ac 2020-01-07 stsp while (!done) {
2220 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2221 ca6e02ac 2020-01-07 stsp if (err)
2222 ca6e02ac 2020-01-07 stsp return err;
2223 ca6e02ac 2020-01-07 stsp
2224 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2225 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
2226 ca6e02ac 2020-01-07 stsp case GOT_IMSG_TRAVERSED_COMMITS:
2227 ca6e02ac 2020-01-07 stsp icommits = imsg.data;
2228 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommits) +
2229 ca6e02ac 2020-01-07 stsp icommits->ncommits * SHA1_DIGEST_LENGTH) {
2230 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2231 ca6e02ac 2020-01-07 stsp break;
2232 ca6e02ac 2020-01-07 stsp }
2233 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommits->ncommits; i++) {
2234 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
2235 ca6e02ac 2020-01-07 stsp uint8_t *sha1 = (uint8_t *)imsg.data +
2236 ca6e02ac 2020-01-07 stsp sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2237 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
2238 ca6e02ac 2020-01-07 stsp if (err)
2239 ca6e02ac 2020-01-07 stsp break;
2240 ca6e02ac 2020-01-07 stsp memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2241 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2242 ca6e02ac 2020-01-07 stsp
2243 ca6e02ac 2020-01-07 stsp /* The last commit may contain a change. */
2244 ca6e02ac 2020-01-07 stsp if (i == icommits->ncommits - 1) {
2245 ca6e02ac 2020-01-07 stsp *changed_commit_id =
2246 ca6e02ac 2020-01-07 stsp got_object_id_dup(qid->id);
2247 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2248 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
2249 ca6e02ac 2020-01-07 stsp "got_object_id_dup");
2250 ca6e02ac 2020-01-07 stsp break;
2251 ca6e02ac 2020-01-07 stsp }
2252 ca6e02ac 2020-01-07 stsp }
2253 ca6e02ac 2020-01-07 stsp }
2254 ca6e02ac 2020-01-07 stsp break;
2255 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
2256 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2257 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2258 ca6e02ac 2020-01-07 stsp break;
2259 ca6e02ac 2020-01-07 stsp }
2260 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(changed_commit, &imsg,
2261 ca6e02ac 2020-01-07 stsp datalen, ibuf);
2262 ca6e02ac 2020-01-07 stsp break;
2263 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2264 ca6e02ac 2020-01-07 stsp done = 1;
2265 ca6e02ac 2020-01-07 stsp break;
2266 ca6e02ac 2020-01-07 stsp default:
2267 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2268 ca6e02ac 2020-01-07 stsp break;
2269 ca6e02ac 2020-01-07 stsp }
2270 ca6e02ac 2020-01-07 stsp
2271 ca6e02ac 2020-01-07 stsp imsg_free(&imsg);
2272 ca6e02ac 2020-01-07 stsp if (err)
2273 ca6e02ac 2020-01-07 stsp break;
2274 ca6e02ac 2020-01-07 stsp }
2275 ca6e02ac 2020-01-07 stsp
2276 ca6e02ac 2020-01-07 stsp if (err)
2277 ca6e02ac 2020-01-07 stsp got_object_id_queue_free(commit_ids);
2278 ca6e02ac 2020-01-07 stsp return err;
2279 ca6e02ac 2020-01-07 stsp }
2280 ca6e02ac 2020-01-07 stsp
2281 ca6e02ac 2020-01-07 stsp const struct got_error *
2282 63219cd2 2019-01-04 stsp got_privsep_unveil_exec_helpers(void)
2283 63219cd2 2019-01-04 stsp {
2284 c39c25dd 2019-08-09 stsp const char *helpers[] = {
2285 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_PACK,
2286 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_OBJECT,
2287 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_COMMIT,
2288 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TREE,
2289 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_BLOB,
2290 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TAG,
2291 aba9c984 2019-09-08 stsp GOT_PATH_PROG_READ_GITCONFIG,
2292 257add31 2020-09-09 stsp GOT_PATH_PROG_READ_GOTCONFIG,
2293 ee448f5f 2020-03-18 stsp GOT_PATH_PROG_FETCH_PACK,
2294 ee448f5f 2020-03-18 stsp GOT_PATH_PROG_INDEX_PACK,
2295 c39c25dd 2019-08-09 stsp };
2296 16aeacf7 2020-11-26 stsp size_t i;
2297 63219cd2 2019-01-04 stsp
2298 c39c25dd 2019-08-09 stsp for (i = 0; i < nitems(helpers); i++) {
2299 c39c25dd 2019-08-09 stsp if (unveil(helpers[i], "x") == 0)
2300 c39c25dd 2019-08-09 stsp continue;
2301 c39c25dd 2019-08-09 stsp return got_error_from_errno2("unveil", helpers[i]);
2302 c39c25dd 2019-08-09 stsp }
2303 c39c25dd 2019-08-09 stsp
2304 63219cd2 2019-01-04 stsp return NULL;
2305 876c234b 2018-09-10 stsp }
2306 aba9c984 2019-09-08 stsp
2307 aba9c984 2019-09-08 stsp void
2308 aba9c984 2019-09-08 stsp got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2309 aba9c984 2019-09-08 stsp {
2310 08578a35 2021-01-22 stsp if (close(imsg_fds[0]) == -1) {
2311 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2312 aba9c984 2019-09-08 stsp _exit(1);
2313 aba9c984 2019-09-08 stsp }
2314 aba9c984 2019-09-08 stsp
2315 aba9c984 2019-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2316 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2317 aba9c984 2019-09-08 stsp _exit(1);
2318 aba9c984 2019-09-08 stsp }
2319 aba9c984 2019-09-08 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2320 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2321 aba9c984 2019-09-08 stsp _exit(1);
2322 aba9c984 2019-09-08 stsp }
2323 aba9c984 2019-09-08 stsp
2324 aba9c984 2019-09-08 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
2325 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2326 aba9c984 2019-09-08 stsp strerror(errno));
2327 aba9c984 2019-09-08 stsp _exit(1);
2328 aba9c984 2019-09-08 stsp }
2329 aba9c984 2019-09-08 stsp }