Blame


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