Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <stdint.h>
26 #include <poll.h>
27 #include <imsg.h>
28 #include <sha1.h>
29 #include <zlib.h>
31 #include "got_object.h"
32 #include "got_error.h"
34 #include "got_lib_sha1.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_zbuf.h"
37 #include "got_lib_object.h"
38 #include "got_lib_privsep.h"
40 #ifndef MIN
41 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 #endif
44 static const struct got_error *
45 poll_fd(int fd, int events, int timeout)
46 {
47 struct pollfd pfd[1];
48 int n;
50 pfd[0].fd = fd;
51 pfd[0].events = events;
53 n = poll(pfd, 1, timeout);
54 if (n == -1)
55 return got_error_from_errno();
56 if (n == 0)
57 return got_error(GOT_ERR_TIMEOUT);
58 if (pfd[0].revents & (POLLERR | POLLNVAL))
59 return got_error_from_errno();
60 if (pfd[0].revents & (events | POLLHUP))
61 return NULL;
63 return got_error(GOT_ERR_INTERRUPT);
64 }
66 static const struct got_error *
67 recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
68 {
69 const struct got_error *err;
70 ssize_t n, m;
72 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
73 if (err)
74 return err;
76 n = imsg_read(ibuf);
77 if (n == -1) {
78 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
79 return got_error(GOT_ERR_PRIVSEP_NO_FD);
80 return got_error(GOT_ERR_PRIVSEP_READ);
81 }
82 if (n == 0)
83 return got_error(GOT_ERR_PRIVSEP_PIPE);
85 m = imsg_get(ibuf, imsg);
86 if (m == 0)
87 return got_error(GOT_ERR_PRIVSEP_READ);
89 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
90 return got_error(GOT_ERR_PRIVSEP_LEN);
92 return NULL;
93 }
95 static const struct got_error *
96 recv_imsg_error(struct imsg *imsg, size_t datalen)
97 {
98 struct got_imsg_error ierr;
100 if (datalen != sizeof(ierr))
101 return got_error(GOT_ERR_PRIVSEP_LEN);
103 memcpy(&ierr, imsg->data, sizeof(ierr));
104 if (ierr.code == GOT_ERR_ERRNO) {
105 static struct got_error serr;
106 serr.code = GOT_ERR_ERRNO;
107 serr.msg = strerror(ierr.errno_code);
108 return &serr;
111 return got_error(ierr.code);
114 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
115 void
116 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
118 const struct got_error *poll_err;
119 struct got_imsg_error ierr;
120 int ret;
122 ierr.code = err->code;
123 if (err->code == GOT_ERR_ERRNO)
124 ierr.errno_code = errno;
125 else
126 ierr.errno_code = 0;
127 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
128 if (ret != -1) {
129 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
130 getprogname(), err->code, err->msg, strerror(errno));
131 return;
134 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
135 if (poll_err) {
136 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
137 getprogname(), err->code, err->msg, poll_err->msg);
138 return;
141 ret = imsg_flush(ibuf);
142 if (ret == -1) {
143 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
144 getprogname(), err->code, err->msg, strerror(errno));
145 return;
149 const struct got_error *
150 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
152 const struct got_error *err = NULL;
153 struct got_imsg_object iobj;
155 iobj.type = obj->type;
156 iobj.flags = obj->flags;
157 iobj.hdrlen = obj->hdrlen;
158 iobj.size = obj->size;
159 iobj.ndeltas = ndeltas;
161 if (ndeltas > 0) {
162 /* TODO: Handle deltas */
165 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
166 == -1)
167 return got_error_from_errno();
169 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
170 if (err)
171 return err;
173 if (imsg_flush(ibuf) == -1)
174 return got_error_from_errno();
176 return NULL;
179 const struct got_error *
180 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
182 const struct got_error *err = NULL;
183 struct imsg imsg;
184 struct got_imsg_object iobj;
185 size_t datalen;
186 int i;
187 const size_t min_datalen =
188 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
190 *obj = NULL;
192 err = recv_one_imsg(&imsg, ibuf, min_datalen);
193 if (err)
194 return err;
196 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
198 switch (imsg.hdr.type) {
199 case GOT_IMSG_ERROR:
200 err = recv_imsg_error(&imsg, datalen);
201 break;
202 case GOT_IMSG_OBJECT:
203 if (datalen != sizeof(iobj)) {
204 err = got_error(GOT_ERR_PRIVSEP_LEN);
205 break;
208 memcpy(&iobj, imsg.data, sizeof(iobj));
209 if (iobj.ndeltas < 0 ||
210 iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
211 err = got_error(GOT_ERR_PRIVSEP_LEN);
212 break;
215 *obj = calloc(1, sizeof(**obj));
216 if (*obj == NULL) {
217 err = got_error_from_errno();
218 break;
221 (*obj)->type = iobj.type;
222 (*obj)->hdrlen = iobj.hdrlen;
223 (*obj)->size = iobj.size;
224 for (i = 0; i < iobj.ndeltas; i++) {
225 /* TODO: Handle deltas */
227 break;
228 default:
229 err = got_error(GOT_ERR_PRIVSEP_MSG);
230 break;
233 imsg_free(&imsg);
235 return err;
238 const struct got_error *
239 got_privsep_send_commit_obj(struct imsgbuf *ibuf, struct got_commit_object *commit)
241 const struct got_error *err = NULL;
242 struct got_imsg_commit_object icommit;
243 uint8_t *buf;
244 size_t len, total;
245 struct got_parent_id *pid;
247 memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
248 icommit.author_len = strlen(commit->author);
249 icommit.committer_len = strlen(commit->committer);
250 icommit.logmsg_len = strlen(commit->logmsg);
251 icommit.nparents = commit->nparents;
253 total = sizeof(icommit) + icommit.author_len +
254 icommit.committer_len + icommit.logmsg_len +
255 icommit.nparents * SHA1_DIGEST_LENGTH;
256 /* XXX TODO support very large log messages properly */
257 if (total > MAX_IMSGSIZE)
258 return got_error(GOT_ERR_NO_SPACE);
260 buf = malloc(total);
261 if (buf == NULL)
262 return got_error_from_errno();
264 len = 0;
265 memcpy(buf + len, &icommit, sizeof(icommit));
266 len += sizeof(icommit);
267 memcpy(buf + len, commit->author, icommit.author_len);
268 len += icommit.author_len;
269 memcpy(buf + len, commit->committer, icommit.committer_len);
270 len += icommit.committer_len;
271 memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
272 len += icommit.logmsg_len;
273 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
274 memcpy(buf + len, pid->id, SHA1_DIGEST_LENGTH);
275 len += SHA1_DIGEST_LENGTH;
278 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
279 err = got_error_from_errno();
280 goto done;
283 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
284 if (err)
285 goto done;
287 if (imsg_flush(ibuf) == -1) {
288 err = got_error_from_errno();
289 goto done;
292 done:
293 free(buf);
294 return err;
296 const struct got_error *
297 got_privsep_recv_commit_obj(struct got_commit_object **commit,
298 struct imsgbuf *ibuf)
300 const struct got_error *err = NULL;
301 struct imsg imsg;
302 struct got_imsg_commit_object icommit;
303 size_t len, datalen;
304 int i;
305 const size_t min_datalen =
306 MIN(sizeof(struct got_imsg_error),
307 sizeof(struct got_imsg_commit_object));
308 uint8_t *data;
310 *commit = NULL;
312 err = recv_one_imsg(&imsg, ibuf, min_datalen);
313 if (err)
314 return err;
316 data = imsg.data;
317 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
318 len = 0;
320 switch (imsg.hdr.type) {
321 case GOT_IMSG_ERROR:
322 err = recv_imsg_error(&imsg, datalen);
323 break;
324 case GOT_IMSG_COMMIT:
325 if (datalen < sizeof(icommit)) {
326 err = got_error(GOT_ERR_PRIVSEP_LEN);
327 break;
330 memcpy(&icommit, data, sizeof(icommit));
331 if (datalen != sizeof(icommit) + icommit.author_len +
332 icommit.committer_len + icommit.logmsg_len +
333 icommit.nparents * SHA1_DIGEST_LENGTH) {
334 err = got_error(GOT_ERR_PRIVSEP_LEN);
335 break;
337 if (icommit.nparents < 0) {
338 err = got_error(GOT_ERR_PRIVSEP_LEN);
339 break;
341 len += sizeof(icommit);
343 *commit = got_object_commit_alloc_partial();
344 if (*commit == NULL) {
345 err = got_error_from_errno();
346 break;
349 memcpy((*commit)->tree_id->sha1, icommit.tree_id,
350 SHA1_DIGEST_LENGTH);
352 if (icommit.author_len == 0) {
353 (*commit)->author = strdup("");
354 if ((*commit)->author == NULL) {
355 err = got_error_from_errno();
356 break;
358 } else {
359 (*commit)->author = malloc(icommit.author_len + 1);
360 if ((*commit)->author == NULL) {
361 err = got_error_from_errno();
362 break;
364 memcpy((*commit)->author, data + len,
365 icommit.author_len);
366 (*commit)->author[icommit.author_len] = '\0';
368 len += icommit.author_len;
370 if (icommit.committer_len == 0) {
371 (*commit)->committer = strdup("");
372 if ((*commit)->committer == NULL) {
373 err = got_error_from_errno();
374 break;
376 } else {
377 (*commit)->committer =
378 malloc(icommit.committer_len + 1);
379 if ((*commit)->committer == NULL) {
380 err = got_error_from_errno();
381 break;
383 memcpy((*commit)->committer, data + len,
384 icommit.committer_len);
385 (*commit)->committer[icommit.committer_len] = '\0';
387 len += icommit.committer_len;
389 if (icommit.logmsg_len == 0) {
390 (*commit)->logmsg = strdup("");
391 if ((*commit)->logmsg == NULL) {
392 err = got_error_from_errno();
393 break;
395 } else {
396 (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
397 if ((*commit)->logmsg == NULL) {
398 err = got_error_from_errno();
399 break;
401 memcpy((*commit)->logmsg, data + len,
402 icommit.logmsg_len);
403 (*commit)->logmsg[icommit.logmsg_len] = '\0';
405 len += icommit.logmsg_len;
407 for (i = 0; i < icommit.nparents; i++) {
408 struct got_parent_id *pid;
410 pid = calloc(1, sizeof(*pid));
411 if (pid == NULL) {
412 err = got_error_from_errno();
413 break;
415 pid->id = calloc(1, sizeof(*pid->id));
416 if (pid->id == NULL) {
417 err = got_error_from_errno();
418 free(pid);
419 break;
422 memcpy(pid->id, data + len + i * SHA1_DIGEST_LENGTH,
423 sizeof(*pid->id));
424 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
425 (*commit)->nparents++;
427 break;
428 default:
429 err = got_error(GOT_ERR_PRIVSEP_MSG);
430 break;
433 imsg_free(&imsg);
435 return err;