Blame


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