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