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 2178c42e 2018-04-22 stsp
21 2178c42e 2018-04-22 stsp #include <stdio.h>
22 2178c42e 2018-04-22 stsp #include <stdlib.h>
23 2178c42e 2018-04-22 stsp #include <string.h>
24 2178c42e 2018-04-22 stsp #include <errno.h>
25 2178c42e 2018-04-22 stsp #include <stdint.h>
26 2178c42e 2018-04-22 stsp #include <poll.h>
27 2178c42e 2018-04-22 stsp #include <imsg.h>
28 2178c42e 2018-04-22 stsp #include <sha1.h>
29 2178c42e 2018-04-22 stsp #include <zlib.h>
30 2178c42e 2018-04-22 stsp
31 2178c42e 2018-04-22 stsp #include "got_object.h"
32 2178c42e 2018-04-22 stsp #include "got_error.h"
33 2178c42e 2018-04-22 stsp
34 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
35 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
36 2178c42e 2018-04-22 stsp #include "got_lib_zbuf.h"
37 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
38 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
39 2178c42e 2018-04-22 stsp
40 2178c42e 2018-04-22 stsp #ifndef MIN
41 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 2178c42e 2018-04-22 stsp #endif
43 2178c42e 2018-04-22 stsp
44 2178c42e 2018-04-22 stsp static const struct got_error *
45 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
46 2178c42e 2018-04-22 stsp {
47 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
48 2178c42e 2018-04-22 stsp int n;
49 2178c42e 2018-04-22 stsp
50 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
51 2178c42e 2018-04-22 stsp pfd[0].events = events;
52 2178c42e 2018-04-22 stsp
53 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
54 2178c42e 2018-04-22 stsp if (n == -1)
55 2178c42e 2018-04-22 stsp return got_error_from_errno();
56 2178c42e 2018-04-22 stsp if (n == 0)
57 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
58 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
59 2178c42e 2018-04-22 stsp return got_error_from_errno();
60 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
61 2178c42e 2018-04-22 stsp return NULL;
62 2178c42e 2018-04-22 stsp
63 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
64 2178c42e 2018-04-22 stsp }
65 2178c42e 2018-04-22 stsp
66 c4eae628 2018-04-23 stsp static const struct got_error *
67 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
68 fe36cf76 2018-04-23 stsp {
69 fe36cf76 2018-04-23 stsp const struct got_error *err;
70 e033d803 2018-04-23 stsp size_t n;
71 fe36cf76 2018-04-23 stsp
72 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
73 fe36cf76 2018-04-23 stsp if (err)
74 fe36cf76 2018-04-23 stsp return err;
75 fe36cf76 2018-04-23 stsp
76 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
77 fe36cf76 2018-04-23 stsp if (n == -1) {
78 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
79 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
80 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
81 fe36cf76 2018-04-23 stsp }
82 fe36cf76 2018-04-23 stsp if (n == 0)
83 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
84 fe36cf76 2018-04-23 stsp
85 e033d803 2018-04-23 stsp return NULL;
86 e033d803 2018-04-23 stsp }
87 e033d803 2018-04-23 stsp
88 e033d803 2018-04-23 stsp static const struct got_error *
89 e033d803 2018-04-23 stsp recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
90 e033d803 2018-04-23 stsp {
91 e033d803 2018-04-23 stsp const struct got_error *err;
92 e033d803 2018-04-23 stsp ssize_t n;
93 e033d803 2018-04-23 stsp
94 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
95 e033d803 2018-04-23 stsp if (err)
96 e033d803 2018-04-23 stsp return err;
97 e033d803 2018-04-23 stsp
98 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
99 e033d803 2018-04-23 stsp if (n == 0)
100 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
101 fe36cf76 2018-04-23 stsp
102 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
103 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
104 fe36cf76 2018-04-23 stsp
105 fe36cf76 2018-04-23 stsp return NULL;
106 fe36cf76 2018-04-23 stsp }
107 fe36cf76 2018-04-23 stsp
108 fe36cf76 2018-04-23 stsp static const struct got_error *
109 c4eae628 2018-04-23 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
110 c4eae628 2018-04-23 stsp {
111 c4eae628 2018-04-23 stsp struct got_imsg_error ierr;
112 c4eae628 2018-04-23 stsp
113 c4eae628 2018-04-23 stsp if (datalen != sizeof(ierr))
114 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
115 c4eae628 2018-04-23 stsp
116 c4eae628 2018-04-23 stsp memcpy(&ierr, imsg->data, sizeof(ierr));
117 c4eae628 2018-04-23 stsp if (ierr.code == GOT_ERR_ERRNO) {
118 c4eae628 2018-04-23 stsp static struct got_error serr;
119 c4eae628 2018-04-23 stsp serr.code = GOT_ERR_ERRNO;
120 c4eae628 2018-04-23 stsp serr.msg = strerror(ierr.errno_code);
121 c4eae628 2018-04-23 stsp return &serr;
122 c4eae628 2018-04-23 stsp }
123 c4eae628 2018-04-23 stsp
124 c4eae628 2018-04-23 stsp return got_error(ierr.code);
125 c4eae628 2018-04-23 stsp }
126 c4eae628 2018-04-23 stsp
127 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
128 2178c42e 2018-04-22 stsp void
129 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
130 2178c42e 2018-04-22 stsp {
131 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
132 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
133 2178c42e 2018-04-22 stsp int ret;
134 2178c42e 2018-04-22 stsp
135 2178c42e 2018-04-22 stsp ierr.code = err->code;
136 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
137 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
138 2178c42e 2018-04-22 stsp else
139 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
140 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
141 2178c42e 2018-04-22 stsp if (ret != -1) {
142 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
143 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
144 5d43e84d 2018-04-23 stsp return;
145 2178c42e 2018-04-22 stsp }
146 2178c42e 2018-04-22 stsp
147 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
148 5d43e84d 2018-04-23 stsp if (poll_err) {
149 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
150 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
151 5d43e84d 2018-04-23 stsp return;
152 5d43e84d 2018-04-23 stsp }
153 2178c42e 2018-04-22 stsp
154 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
155 5d43e84d 2018-04-23 stsp if (ret == -1) {
156 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
157 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
158 5d43e84d 2018-04-23 stsp return;
159 5d43e84d 2018-04-23 stsp }
160 e033d803 2018-04-23 stsp }
161 e033d803 2018-04-23 stsp
162 e033d803 2018-04-23 stsp static const struct got_error *
163 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
164 e033d803 2018-04-23 stsp {
165 e033d803 2018-04-23 stsp const struct got_error *err;
166 e033d803 2018-04-23 stsp
167 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
168 e033d803 2018-04-23 stsp if (err)
169 e033d803 2018-04-23 stsp return err;
170 e033d803 2018-04-23 stsp
171 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
172 e033d803 2018-04-23 stsp return got_error_from_errno();
173 e033d803 2018-04-23 stsp
174 e033d803 2018-04-23 stsp return NULL;
175 2178c42e 2018-04-22 stsp }
176 2178c42e 2018-04-22 stsp
177 2178c42e 2018-04-22 stsp const struct got_error *
178 2178c42e 2018-04-22 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
179 2178c42e 2018-04-22 stsp {
180 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
181 2178c42e 2018-04-22 stsp
182 2178c42e 2018-04-22 stsp iobj.type = obj->type;
183 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
184 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
185 2178c42e 2018-04-22 stsp iobj.size = obj->size;
186 2178c42e 2018-04-22 stsp iobj.ndeltas = ndeltas;
187 2178c42e 2018-04-22 stsp
188 2178c42e 2018-04-22 stsp if (ndeltas > 0) {
189 2178c42e 2018-04-22 stsp /* TODO: Handle deltas */
190 2178c42e 2018-04-22 stsp }
191 2178c42e 2018-04-22 stsp
192 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
193 2178c42e 2018-04-22 stsp == -1)
194 2178c42e 2018-04-22 stsp return got_error_from_errno();
195 2178c42e 2018-04-22 stsp
196 e033d803 2018-04-23 stsp return flush_imsg(ibuf);
197 2178c42e 2018-04-22 stsp }
198 2178c42e 2018-04-22 stsp
199 2178c42e 2018-04-22 stsp const struct got_error *
200 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
201 2178c42e 2018-04-22 stsp {
202 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
203 2178c42e 2018-04-22 stsp struct imsg imsg;
204 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
205 2178c42e 2018-04-22 stsp size_t datalen;
206 2178c42e 2018-04-22 stsp int i;
207 c4eae628 2018-04-23 stsp const size_t min_datalen =
208 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
209 2178c42e 2018-04-22 stsp
210 2178c42e 2018-04-22 stsp *obj = NULL;
211 2178c42e 2018-04-22 stsp
212 fe36cf76 2018-04-23 stsp err = recv_one_imsg(&imsg, ibuf, min_datalen);
213 2178c42e 2018-04-22 stsp if (err)
214 2178c42e 2018-04-22 stsp return err;
215 2178c42e 2018-04-22 stsp
216 2178c42e 2018-04-22 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
217 2178c42e 2018-04-22 stsp
218 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
219 2178c42e 2018-04-22 stsp case GOT_IMSG_ERROR:
220 c4eae628 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
221 2178c42e 2018-04-22 stsp break;
222 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
223 2178c42e 2018-04-22 stsp if (datalen != sizeof(iobj)) {
224 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
225 2178c42e 2018-04-22 stsp break;
226 2178c42e 2018-04-22 stsp }
227 2178c42e 2018-04-22 stsp
228 2178c42e 2018-04-22 stsp memcpy(&iobj, imsg.data, sizeof(iobj));
229 2178c42e 2018-04-22 stsp if (iobj.ndeltas < 0 ||
230 2178c42e 2018-04-22 stsp iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
231 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
232 2178c42e 2018-04-22 stsp break;
233 2178c42e 2018-04-22 stsp }
234 2178c42e 2018-04-22 stsp
235 2178c42e 2018-04-22 stsp *obj = calloc(1, sizeof(**obj));
236 2178c42e 2018-04-22 stsp if (*obj == NULL) {
237 2178c42e 2018-04-22 stsp err = got_error_from_errno();
238 2178c42e 2018-04-22 stsp break;
239 2178c42e 2018-04-22 stsp }
240 2178c42e 2018-04-22 stsp
241 2178c42e 2018-04-22 stsp (*obj)->type = iobj.type;
242 2178c42e 2018-04-22 stsp (*obj)->hdrlen = iobj.hdrlen;
243 2178c42e 2018-04-22 stsp (*obj)->size = iobj.size;
244 2178c42e 2018-04-22 stsp for (i = 0; i < iobj.ndeltas; i++) {
245 2178c42e 2018-04-22 stsp /* TODO: Handle deltas */
246 bff6ca00 2018-04-23 stsp }
247 bff6ca00 2018-04-23 stsp break;
248 bff6ca00 2018-04-23 stsp default:
249 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
250 bff6ca00 2018-04-23 stsp break;
251 bff6ca00 2018-04-23 stsp }
252 bff6ca00 2018-04-23 stsp
253 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
254 bff6ca00 2018-04-23 stsp
255 bff6ca00 2018-04-23 stsp return err;
256 bff6ca00 2018-04-23 stsp }
257 bff6ca00 2018-04-23 stsp
258 bff6ca00 2018-04-23 stsp const struct got_error *
259 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
260 bff6ca00 2018-04-23 stsp {
261 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
262 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
263 bff6ca00 2018-04-23 stsp uint8_t *buf;
264 bff6ca00 2018-04-23 stsp size_t len, total;
265 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
266 bff6ca00 2018-04-23 stsp
267 86acc566 2018-04-23 stsp memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
268 bff6ca00 2018-04-23 stsp icommit.author_len = strlen(commit->author);
269 ef530fe5 2018-06-10 stsp icommit.author_time = commit->author_time;
270 6c281f94 2018-06-11 stsp icommit.author_tzoff_len = strlen(commit->author_tzoff);
271 bff6ca00 2018-04-23 stsp icommit.committer_len = strlen(commit->committer);
272 ef530fe5 2018-06-10 stsp icommit.committer_time = commit->committer_time;
273 6c281f94 2018-06-11 stsp icommit.committer_tzoff_len = strlen(commit->committer_tzoff);
274 bff6ca00 2018-04-23 stsp icommit.logmsg_len = strlen(commit->logmsg);
275 bff6ca00 2018-04-23 stsp icommit.nparents = commit->nparents;
276 bff6ca00 2018-04-23 stsp
277 bff6ca00 2018-04-23 stsp total = sizeof(icommit) + icommit.author_len +
278 6c281f94 2018-06-11 stsp icommit.author_tzoff_len + icommit.committer_len +
279 6c281f94 2018-06-11 stsp icommit.committer_tzoff_len + icommit.logmsg_len +
280 86acc566 2018-04-23 stsp icommit.nparents * SHA1_DIGEST_LENGTH;
281 bff6ca00 2018-04-23 stsp /* XXX TODO support very large log messages properly */
282 bff6ca00 2018-04-23 stsp if (total > MAX_IMSGSIZE)
283 bff6ca00 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
284 bff6ca00 2018-04-23 stsp
285 bff6ca00 2018-04-23 stsp buf = malloc(total);
286 bff6ca00 2018-04-23 stsp if (buf == NULL)
287 bff6ca00 2018-04-23 stsp return got_error_from_errno();
288 bff6ca00 2018-04-23 stsp
289 bff6ca00 2018-04-23 stsp len = 0;
290 bff6ca00 2018-04-23 stsp memcpy(buf + len, &icommit, sizeof(icommit));
291 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
292 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->author, icommit.author_len);
293 bff6ca00 2018-04-23 stsp len += icommit.author_len;
294 6c281f94 2018-06-11 stsp memcpy(buf + len, commit->author_tzoff, icommit.author_tzoff_len);
295 6c281f94 2018-06-11 stsp len += icommit.author_tzoff_len;
296 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->committer, icommit.committer_len);
297 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
298 6c281f94 2018-06-11 stsp memcpy(buf + len, commit->committer_tzoff, icommit.committer_tzoff_len);
299 6c281f94 2018-06-11 stsp len += icommit.committer_tzoff_len;
300 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
301 bff6ca00 2018-04-23 stsp len += icommit.logmsg_len;
302 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
303 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
304 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
305 bff6ca00 2018-04-23 stsp }
306 bff6ca00 2018-04-23 stsp
307 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
308 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
309 bff6ca00 2018-04-23 stsp goto done;
310 bff6ca00 2018-04-23 stsp }
311 bff6ca00 2018-04-23 stsp
312 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
313 bff6ca00 2018-04-23 stsp done:
314 bff6ca00 2018-04-23 stsp free(buf);
315 bff6ca00 2018-04-23 stsp return err;
316 bff6ca00 2018-04-23 stsp }
317 bff6ca00 2018-04-23 stsp const struct got_error *
318 068fd2bf 2018-04-24 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
319 bff6ca00 2018-04-23 stsp {
320 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
321 bff6ca00 2018-04-23 stsp struct imsg imsg;
322 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
323 bff6ca00 2018-04-23 stsp size_t len, datalen;
324 bff6ca00 2018-04-23 stsp int i;
325 bff6ca00 2018-04-23 stsp const size_t min_datalen =
326 bff6ca00 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
327 bff6ca00 2018-04-23 stsp sizeof(struct got_imsg_commit_object));
328 bff6ca00 2018-04-23 stsp uint8_t *data;
329 bff6ca00 2018-04-23 stsp
330 bff6ca00 2018-04-23 stsp *commit = NULL;
331 bff6ca00 2018-04-23 stsp
332 bff6ca00 2018-04-23 stsp err = recv_one_imsg(&imsg, ibuf, min_datalen);
333 bff6ca00 2018-04-23 stsp if (err)
334 bff6ca00 2018-04-23 stsp return err;
335 bff6ca00 2018-04-23 stsp
336 bff6ca00 2018-04-23 stsp data = imsg.data;
337 bff6ca00 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
338 bff6ca00 2018-04-23 stsp len = 0;
339 bff6ca00 2018-04-23 stsp
340 bff6ca00 2018-04-23 stsp switch (imsg.hdr.type) {
341 bff6ca00 2018-04-23 stsp case GOT_IMSG_ERROR:
342 bff6ca00 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
343 bff6ca00 2018-04-23 stsp break;
344 bff6ca00 2018-04-23 stsp case GOT_IMSG_COMMIT:
345 bff6ca00 2018-04-23 stsp if (datalen < sizeof(icommit)) {
346 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
347 bff6ca00 2018-04-23 stsp break;
348 2178c42e 2018-04-22 stsp }
349 bff6ca00 2018-04-23 stsp
350 bff6ca00 2018-04-23 stsp memcpy(&icommit, data, sizeof(icommit));
351 bff6ca00 2018-04-23 stsp if (datalen != sizeof(icommit) + icommit.author_len +
352 6c281f94 2018-06-11 stsp icommit.author_tzoff_len + icommit.committer_len +
353 6c281f94 2018-06-11 stsp icommit.committer_tzoff_len + icommit.logmsg_len +
354 86acc566 2018-04-23 stsp icommit.nparents * SHA1_DIGEST_LENGTH) {
355 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
356 bff6ca00 2018-04-23 stsp break;
357 bff6ca00 2018-04-23 stsp }
358 bff6ca00 2018-04-23 stsp if (icommit.nparents < 0) {
359 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
360 bff6ca00 2018-04-23 stsp break;
361 bff6ca00 2018-04-23 stsp }
362 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
363 bff6ca00 2018-04-23 stsp
364 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
365 bff6ca00 2018-04-23 stsp if (*commit == NULL) {
366 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
367 bff6ca00 2018-04-23 stsp break;
368 bff6ca00 2018-04-23 stsp }
369 bff6ca00 2018-04-23 stsp
370 86acc566 2018-04-23 stsp memcpy((*commit)->tree_id->sha1, icommit.tree_id,
371 86acc566 2018-04-23 stsp SHA1_DIGEST_LENGTH);
372 bff6ca00 2018-04-23 stsp
373 bff6ca00 2018-04-23 stsp if (icommit.author_len == 0) {
374 bff6ca00 2018-04-23 stsp (*commit)->author = strdup("");
375 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
376 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
377 bff6ca00 2018-04-23 stsp break;
378 bff6ca00 2018-04-23 stsp }
379 ef530fe5 2018-06-10 stsp (*commit)->author_time = 0;
380 bff6ca00 2018-04-23 stsp } else {
381 bff6ca00 2018-04-23 stsp (*commit)->author = malloc(icommit.author_len + 1);
382 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
383 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
384 bff6ca00 2018-04-23 stsp break;
385 bff6ca00 2018-04-23 stsp }
386 bff6ca00 2018-04-23 stsp memcpy((*commit)->author, data + len,
387 bff6ca00 2018-04-23 stsp icommit.author_len);
388 bff6ca00 2018-04-23 stsp (*commit)->author[icommit.author_len] = '\0';
389 bff6ca00 2018-04-23 stsp }
390 bff6ca00 2018-04-23 stsp len += icommit.author_len;
391 6c281f94 2018-06-11 stsp
392 6c281f94 2018-06-11 stsp (*commit)->author_time = icommit.author_time;
393 6c281f94 2018-06-11 stsp if (icommit.author_tzoff_len == 0) {
394 6c281f94 2018-06-11 stsp (*commit)->author_tzoff = strdup("");
395 6c281f94 2018-06-11 stsp if ((*commit)->author_tzoff == NULL) {
396 6c281f94 2018-06-11 stsp err = got_error_from_errno();
397 6c281f94 2018-06-11 stsp break;
398 6c281f94 2018-06-11 stsp }
399 6c281f94 2018-06-11 stsp } else {
400 6c281f94 2018-06-11 stsp (*commit)->author_tzoff =
401 6c281f94 2018-06-11 stsp malloc(icommit.author_tzoff_len + 1);
402 6c281f94 2018-06-11 stsp if ((*commit)->author_tzoff == NULL) {
403 6c281f94 2018-06-11 stsp err = got_error_from_errno();
404 6c281f94 2018-06-11 stsp break;
405 6c281f94 2018-06-11 stsp }
406 6c281f94 2018-06-11 stsp memcpy((*commit)->author_tzoff, data + len,
407 6c281f94 2018-06-11 stsp icommit.author_tzoff_len);
408 6c281f94 2018-06-11 stsp (*commit)->author_tzoff[icommit.author_tzoff_len] =
409 6c281f94 2018-06-11 stsp '\0';
410 6c281f94 2018-06-11 stsp }
411 6c281f94 2018-06-11 stsp len += icommit.author_tzoff_len;
412 bff6ca00 2018-04-23 stsp
413 bff6ca00 2018-04-23 stsp if (icommit.committer_len == 0) {
414 bff6ca00 2018-04-23 stsp (*commit)->committer = strdup("");
415 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
416 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
417 bff6ca00 2018-04-23 stsp break;
418 bff6ca00 2018-04-23 stsp }
419 ef530fe5 2018-06-10 stsp (*commit)->committer_time = 0;
420 bff6ca00 2018-04-23 stsp } else {
421 bff6ca00 2018-04-23 stsp (*commit)->committer =
422 bff6ca00 2018-04-23 stsp malloc(icommit.committer_len + 1);
423 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
424 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
425 bff6ca00 2018-04-23 stsp break;
426 bff6ca00 2018-04-23 stsp }
427 bff6ca00 2018-04-23 stsp memcpy((*commit)->committer, data + len,
428 bff6ca00 2018-04-23 stsp icommit.committer_len);
429 bff6ca00 2018-04-23 stsp (*commit)->committer[icommit.committer_len] = '\0';
430 bff6ca00 2018-04-23 stsp }
431 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
432 bff6ca00 2018-04-23 stsp
433 6c281f94 2018-06-11 stsp (*commit)->committer_time = icommit.committer_time;
434 6c281f94 2018-06-11 stsp if (icommit.committer_tzoff_len == 0) {
435 6c281f94 2018-06-11 stsp (*commit)->committer_tzoff = strdup("");
436 6c281f94 2018-06-11 stsp if ((*commit)->committer_tzoff == NULL) {
437 6c281f94 2018-06-11 stsp err = got_error_from_errno();
438 6c281f94 2018-06-11 stsp break;
439 6c281f94 2018-06-11 stsp }
440 6c281f94 2018-06-11 stsp } else {
441 6c281f94 2018-06-11 stsp (*commit)->committer_tzoff =
442 6c281f94 2018-06-11 stsp malloc(icommit.committer_tzoff_len + 1);
443 6c281f94 2018-06-11 stsp if ((*commit)->committer_tzoff == NULL) {
444 6c281f94 2018-06-11 stsp err = got_error_from_errno();
445 6c281f94 2018-06-11 stsp break;
446 6c281f94 2018-06-11 stsp }
447 6c281f94 2018-06-11 stsp memcpy((*commit)->committer_tzoff, data + len,
448 6c281f94 2018-06-11 stsp icommit.committer_tzoff_len);
449 6c281f94 2018-06-11 stsp (*commit)->committer_tzoff[icommit.committer_tzoff_len]
450 6c281f94 2018-06-11 stsp = '\0';
451 6c281f94 2018-06-11 stsp }
452 6c281f94 2018-06-11 stsp len += icommit.committer_tzoff_len;
453 6c281f94 2018-06-11 stsp
454 bff6ca00 2018-04-23 stsp if (icommit.logmsg_len == 0) {
455 bff6ca00 2018-04-23 stsp (*commit)->logmsg = strdup("");
456 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
457 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
458 bff6ca00 2018-04-23 stsp break;
459 bff6ca00 2018-04-23 stsp }
460 bff6ca00 2018-04-23 stsp } else {
461 bff6ca00 2018-04-23 stsp (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
462 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
463 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
464 bff6ca00 2018-04-23 stsp break;
465 bff6ca00 2018-04-23 stsp }
466 bff6ca00 2018-04-23 stsp memcpy((*commit)->logmsg, data + len,
467 bff6ca00 2018-04-23 stsp icommit.logmsg_len);
468 bff6ca00 2018-04-23 stsp (*commit)->logmsg[icommit.logmsg_len] = '\0';
469 bff6ca00 2018-04-23 stsp }
470 bff6ca00 2018-04-23 stsp len += icommit.logmsg_len;
471 bff6ca00 2018-04-23 stsp
472 bff6ca00 2018-04-23 stsp for (i = 0; i < icommit.nparents; i++) {
473 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
474 86acc566 2018-04-23 stsp
475 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
476 79f35eb3 2018-06-11 stsp if (qid == NULL) {
477 86acc566 2018-04-23 stsp err = got_error_from_errno();
478 bff6ca00 2018-04-23 stsp break;
479 86acc566 2018-04-23 stsp }
480 79f35eb3 2018-06-11 stsp qid->id = calloc(1, sizeof(*qid->id));
481 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
482 86acc566 2018-04-23 stsp err = got_error_from_errno();
483 79f35eb3 2018-06-11 stsp free(qid);
484 86acc566 2018-04-23 stsp break;
485 86acc566 2018-04-23 stsp }
486 86acc566 2018-04-23 stsp
487 79f35eb3 2018-06-11 stsp memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
488 79f35eb3 2018-06-11 stsp sizeof(*qid->id));
489 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
490 86acc566 2018-04-23 stsp (*commit)->nparents++;
491 bff6ca00 2018-04-23 stsp }
492 2178c42e 2018-04-22 stsp break;
493 8c580685 2018-04-22 stsp default:
494 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
495 8c580685 2018-04-22 stsp break;
496 2178c42e 2018-04-22 stsp }
497 2178c42e 2018-04-22 stsp
498 2178c42e 2018-04-22 stsp imsg_free(&imsg);
499 e033d803 2018-04-23 stsp
500 e033d803 2018-04-23 stsp return err;
501 e033d803 2018-04-23 stsp }
502 e033d803 2018-04-23 stsp
503 e033d803 2018-04-23 stsp const struct got_error *
504 068fd2bf 2018-04-24 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
505 e033d803 2018-04-23 stsp {
506 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
507 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
508 e033d803 2018-04-23 stsp struct got_tree_entry *te;
509 e033d803 2018-04-23 stsp
510 e033d803 2018-04-23 stsp itree.nentries = tree->nentries;
511 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
512 e033d803 2018-04-23 stsp == -1)
513 e033d803 2018-04-23 stsp return got_error_from_errno();
514 e033d803 2018-04-23 stsp
515 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
516 e033d803 2018-04-23 stsp if (err)
517 e033d803 2018-04-23 stsp return err;
518 e033d803 2018-04-23 stsp
519 e033d803 2018-04-23 stsp SIMPLEQ_FOREACH(te, &tree->entries, entry) {
520 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
521 e033d803 2018-04-23 stsp uint8_t *buf = NULL;
522 e033d803 2018-04-23 stsp size_t len = sizeof(ite) + strlen(te->name);
523 e033d803 2018-04-23 stsp
524 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
525 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
526 e033d803 2018-04-23 stsp
527 e033d803 2018-04-23 stsp buf = malloc(len);
528 e033d803 2018-04-23 stsp if (buf == NULL)
529 e033d803 2018-04-23 stsp return got_error_from_errno();
530 e033d803 2018-04-23 stsp
531 e033d803 2018-04-23 stsp memcpy(ite.id, te->id->sha1, sizeof(ite.id));
532 e033d803 2018-04-23 stsp ite.mode = te->mode;
533 e033d803 2018-04-23 stsp memcpy(buf, &ite, sizeof(ite));
534 e033d803 2018-04-23 stsp memcpy(buf + sizeof(ite), te->name, strlen(te->name));
535 e033d803 2018-04-23 stsp
536 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
537 e033d803 2018-04-23 stsp buf, len) == -1)
538 e033d803 2018-04-23 stsp err = got_error_from_errno();
539 e033d803 2018-04-23 stsp free(buf);
540 e033d803 2018-04-23 stsp if (err)
541 e033d803 2018-04-23 stsp return err;
542 e033d803 2018-04-23 stsp
543 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
544 e033d803 2018-04-23 stsp if (err)
545 e033d803 2018-04-23 stsp return err;
546 e033d803 2018-04-23 stsp }
547 e033d803 2018-04-23 stsp
548 e033d803 2018-04-23 stsp return NULL;
549 e033d803 2018-04-23 stsp }
550 e033d803 2018-04-23 stsp
551 e033d803 2018-04-23 stsp const struct got_error *
552 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
553 e033d803 2018-04-23 stsp {
554 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
555 e033d803 2018-04-23 stsp const size_t min_datalen =
556 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
557 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
558 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree = { 0 };
559 e033d803 2018-04-23 stsp int nentries = 0;
560 2178c42e 2018-04-22 stsp
561 e033d803 2018-04-23 stsp *tree = NULL;
562 e033d803 2018-04-23 stsp get_more:
563 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
564 e033d803 2018-04-23 stsp if (err)
565 1e51f5b9 2018-04-23 stsp goto done;
566 e033d803 2018-04-23 stsp
567 e033d803 2018-04-23 stsp while (1) {
568 e033d803 2018-04-23 stsp struct imsg imsg;
569 e033d803 2018-04-23 stsp size_t n;
570 e033d803 2018-04-23 stsp size_t datalen;
571 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
572 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
573 e033d803 2018-04-23 stsp
574 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
575 e033d803 2018-04-23 stsp if (n == 0) {
576 e033d803 2018-04-23 stsp if (*tree && (*tree)->nentries != nentries)
577 e033d803 2018-04-23 stsp goto get_more;
578 e033d803 2018-04-23 stsp break;
579 e033d803 2018-04-23 stsp }
580 e033d803 2018-04-23 stsp
581 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
582 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
583 e033d803 2018-04-23 stsp
584 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
585 e033d803 2018-04-23 stsp
586 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
587 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
588 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
589 e033d803 2018-04-23 stsp break;
590 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
591 e033d803 2018-04-23 stsp /* This message should only appear once. */
592 e033d803 2018-04-23 stsp if (*tree != NULL) {
593 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
594 e033d803 2018-04-23 stsp break;
595 e033d803 2018-04-23 stsp }
596 e033d803 2018-04-23 stsp if (datalen != sizeof(itree)) {
597 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
598 e033d803 2018-04-23 stsp break;
599 e033d803 2018-04-23 stsp }
600 e033d803 2018-04-23 stsp memcpy(&itree, imsg.data, sizeof(itree));
601 e033d803 2018-04-23 stsp *tree = calloc(1, sizeof(**tree));
602 e033d803 2018-04-23 stsp if (*tree == NULL) {
603 e033d803 2018-04-23 stsp err = got_error_from_errno();
604 e033d803 2018-04-23 stsp break;
605 e033d803 2018-04-23 stsp }
606 e033d803 2018-04-23 stsp (*tree)->nentries = itree.nentries;
607 e033d803 2018-04-23 stsp SIMPLEQ_INIT(&(*tree)->entries);
608 e033d803 2018-04-23 stsp break;
609 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
610 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
611 e033d803 2018-04-23 stsp if (*tree == NULL) {
612 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
613 e033d803 2018-04-23 stsp break;
614 e033d803 2018-04-23 stsp }
615 e033d803 2018-04-23 stsp if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
616 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
617 e033d803 2018-04-23 stsp break;
618 e033d803 2018-04-23 stsp }
619 e033d803 2018-04-23 stsp
620 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
621 e033d803 2018-04-23 stsp datalen -= sizeof(ite);
622 e033d803 2018-04-23 stsp memcpy(&ite, imsg.data, sizeof(ite));
623 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
624 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
625 e033d803 2018-04-23 stsp break;
626 e033d803 2018-04-23 stsp }
627 052d4dc3 2018-04-23 stsp
628 e033d803 2018-04-23 stsp te = got_alloc_tree_entry_partial();
629 e033d803 2018-04-23 stsp if (te == NULL) {
630 e033d803 2018-04-23 stsp err = got_error_from_errno();
631 e033d803 2018-04-23 stsp break;
632 e033d803 2018-04-23 stsp }
633 e033d803 2018-04-23 stsp te->name = malloc(datalen + 1);
634 e033d803 2018-04-23 stsp if (te->name == NULL) {
635 e033d803 2018-04-23 stsp free(te);
636 e033d803 2018-04-23 stsp err = got_error_from_errno();
637 e033d803 2018-04-23 stsp break;
638 e033d803 2018-04-23 stsp }
639 052d4dc3 2018-04-23 stsp memcpy(te->name, imsg.data + sizeof(ite), datalen);
640 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
641 e033d803 2018-04-23 stsp
642 e033d803 2018-04-23 stsp memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
643 e033d803 2018-04-23 stsp te->mode = ite.mode;
644 e033d803 2018-04-23 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
645 e033d803 2018-04-23 stsp nentries++;
646 e033d803 2018-04-23 stsp break;
647 e033d803 2018-04-23 stsp default:
648 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
649 e033d803 2018-04-23 stsp break;
650 e033d803 2018-04-23 stsp }
651 e033d803 2018-04-23 stsp
652 e033d803 2018-04-23 stsp imsg_free(&imsg);
653 e033d803 2018-04-23 stsp }
654 1e51f5b9 2018-04-23 stsp done:
655 e033d803 2018-04-23 stsp if (*tree && (*tree)->nentries != nentries) {
656 1e51f5b9 2018-04-23 stsp if (err == NULL)
657 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
658 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
659 e033d803 2018-04-23 stsp *tree = NULL;
660 ff6b18f8 2018-04-24 stsp }
661 ff6b18f8 2018-04-24 stsp
662 ff6b18f8 2018-04-24 stsp return err;
663 ff6b18f8 2018-04-24 stsp }
664 ff6b18f8 2018-04-24 stsp
665 ff6b18f8 2018-04-24 stsp const struct got_error *
666 2967a784 2018-04-24 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
667 ff6b18f8 2018-04-24 stsp {
668 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
669 2967a784 2018-04-24 stsp
670 2967a784 2018-04-24 stsp iblob.size = size;
671 ff6b18f8 2018-04-24 stsp /* Data has already been written to file descriptor. */
672 2967a784 2018-04-24 stsp
673 2967a784 2018-04-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
674 2967a784 2018-04-24 stsp == -1)
675 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
676 ff6b18f8 2018-04-24 stsp
677 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
678 ff6b18f8 2018-04-24 stsp }
679 ff6b18f8 2018-04-24 stsp
680 ff6b18f8 2018-04-24 stsp const struct got_error *
681 2967a784 2018-04-24 stsp got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
682 ff6b18f8 2018-04-24 stsp {
683 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
684 ff6b18f8 2018-04-24 stsp struct imsg imsg;
685 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
686 ff6b18f8 2018-04-24 stsp size_t datalen;
687 ff6b18f8 2018-04-24 stsp
688 ff6b18f8 2018-04-24 stsp err = recv_one_imsg(&imsg, ibuf, 0);
689 ff6b18f8 2018-04-24 stsp if (err)
690 ff6b18f8 2018-04-24 stsp return err;
691 ff6b18f8 2018-04-24 stsp
692 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
693 ff6b18f8 2018-04-24 stsp
694 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
695 ff6b18f8 2018-04-24 stsp case GOT_IMSG_ERROR:
696 ff6b18f8 2018-04-24 stsp err = recv_imsg_error(&imsg, datalen);
697 ff6b18f8 2018-04-24 stsp break;
698 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
699 2967a784 2018-04-24 stsp if (datalen != sizeof(iblob))
700 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
701 2967a784 2018-04-24 stsp memcpy(&iblob, imsg.data, sizeof(iblob));
702 2967a784 2018-04-24 stsp *size = iblob.size;
703 ff6b18f8 2018-04-24 stsp /* Data has been written to file descriptor. */
704 ff6b18f8 2018-04-24 stsp break;
705 ff6b18f8 2018-04-24 stsp default:
706 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
707 ff6b18f8 2018-04-24 stsp break;
708 e033d803 2018-04-23 stsp }
709 e033d803 2018-04-23 stsp
710 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
711 ff6b18f8 2018-04-24 stsp
712 2178c42e 2018-04-22 stsp return err;
713 2178c42e 2018-04-22 stsp }