2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.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"
41 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
44 static const struct got_error *
45 poll_fd(int fd, int events, int timeout)
51 pfd[0].events = events;
53 n = poll(pfd, 1, timeout);
55 return got_error_from_errno();
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))
63 return got_error(GOT_ERR_INTERRUPT);
66 static const struct got_error *
67 read_imsg(struct imsgbuf *ibuf)
69 const struct got_error *err;
72 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
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);
83 return got_error(GOT_ERR_PRIVSEP_PIPE);
88 static const struct got_error *
89 recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
91 const struct got_error *err;
94 err = read_imsg(ibuf);
98 n = imsg_get(ibuf, imsg);
100 return got_error(GOT_ERR_PRIVSEP_READ);
102 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
103 return got_error(GOT_ERR_PRIVSEP_LEN);
108 static const struct got_error *
109 recv_imsg_error(struct imsg *imsg, size_t datalen)
111 struct got_imsg_error ierr;
113 if (datalen != sizeof(ierr))
114 return got_error(GOT_ERR_PRIVSEP_LEN);
116 memcpy(&ierr, imsg->data, sizeof(ierr));
117 if (ierr.code == GOT_ERR_ERRNO) {
118 static struct got_error serr;
119 serr.code = GOT_ERR_ERRNO;
120 serr.msg = strerror(ierr.errno_code);
124 return got_error(ierr.code);
127 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
129 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
131 const struct got_error *poll_err;
132 struct got_imsg_error ierr;
135 ierr.code = err->code;
136 if (err->code == GOT_ERR_ERRNO)
137 ierr.errno_code = errno;
140 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
142 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
143 getprogname(), err->code, err->msg, strerror(errno));
147 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
149 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
150 getprogname(), err->code, err->msg, poll_err->msg);
154 ret = imsg_flush(ibuf);
156 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
157 getprogname(), err->code, err->msg, strerror(errno));
162 static const struct got_error *
163 flush_imsg(struct imsgbuf *ibuf)
165 const struct got_error *err;
167 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
171 if (imsg_flush(ibuf) == -1)
172 return got_error_from_errno();
177 const struct got_error *
178 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
180 struct got_imsg_object iobj;
182 iobj.type = obj->type;
183 iobj.flags = obj->flags;
184 iobj.hdrlen = obj->hdrlen;
185 iobj.size = obj->size;
186 iobj.ndeltas = ndeltas;
189 /* TODO: Handle deltas */
192 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
194 return got_error_from_errno();
196 return flush_imsg(ibuf);
199 const struct got_error *
200 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
202 const struct got_error *err = NULL;
204 struct got_imsg_object iobj;
207 const size_t min_datalen =
208 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
212 err = recv_one_imsg(&imsg, ibuf, min_datalen);
216 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
218 switch (imsg.hdr.type) {
220 err = recv_imsg_error(&imsg, datalen);
222 case GOT_IMSG_OBJECT:
223 if (datalen != sizeof(iobj)) {
224 err = got_error(GOT_ERR_PRIVSEP_LEN);
228 memcpy(&iobj, imsg.data, sizeof(iobj));
229 if (iobj.ndeltas < 0 ||
230 iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
231 err = got_error(GOT_ERR_PRIVSEP_LEN);
235 *obj = calloc(1, sizeof(**obj));
237 err = got_error_from_errno();
241 (*obj)->type = iobj.type;
242 (*obj)->hdrlen = iobj.hdrlen;
243 (*obj)->size = iobj.size;
244 for (i = 0; i < iobj.ndeltas; i++) {
245 /* TODO: Handle deltas */
249 err = got_error(GOT_ERR_PRIVSEP_MSG);
258 const struct got_error *
259 got_privsep_send_commit_obj(struct imsgbuf *ibuf, struct got_commit_object *commit)
261 const struct got_error *err = NULL;
262 struct got_imsg_commit_object icommit;
265 struct got_parent_id *pid;
267 memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
268 icommit.author_len = strlen(commit->author);
269 icommit.committer_len = strlen(commit->committer);
270 icommit.logmsg_len = strlen(commit->logmsg);
271 icommit.nparents = commit->nparents;
273 total = sizeof(icommit) + icommit.author_len +
274 icommit.committer_len + icommit.logmsg_len +
275 icommit.nparents * SHA1_DIGEST_LENGTH;
276 /* XXX TODO support very large log messages properly */
277 if (total > MAX_IMSGSIZE)
278 return got_error(GOT_ERR_NO_SPACE);
282 return got_error_from_errno();
285 memcpy(buf + len, &icommit, sizeof(icommit));
286 len += sizeof(icommit);
287 memcpy(buf + len, commit->author, icommit.author_len);
288 len += icommit.author_len;
289 memcpy(buf + len, commit->committer, icommit.committer_len);
290 len += icommit.committer_len;
291 memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
292 len += icommit.logmsg_len;
293 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
294 memcpy(buf + len, pid->id, SHA1_DIGEST_LENGTH);
295 len += SHA1_DIGEST_LENGTH;
298 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
299 err = got_error_from_errno();
303 err = flush_imsg(ibuf);
308 const struct got_error *
309 got_privsep_recv_commit_obj(struct got_commit_object **commit,
310 struct imsgbuf *ibuf)
312 const struct got_error *err = NULL;
314 struct got_imsg_commit_object icommit;
317 const size_t min_datalen =
318 MIN(sizeof(struct got_imsg_error),
319 sizeof(struct got_imsg_commit_object));
324 err = recv_one_imsg(&imsg, ibuf, min_datalen);
329 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
332 switch (imsg.hdr.type) {
334 err = recv_imsg_error(&imsg, datalen);
336 case GOT_IMSG_COMMIT:
337 if (datalen < sizeof(icommit)) {
338 err = got_error(GOT_ERR_PRIVSEP_LEN);
342 memcpy(&icommit, data, sizeof(icommit));
343 if (datalen != sizeof(icommit) + icommit.author_len +
344 icommit.committer_len + icommit.logmsg_len +
345 icommit.nparents * SHA1_DIGEST_LENGTH) {
346 err = got_error(GOT_ERR_PRIVSEP_LEN);
349 if (icommit.nparents < 0) {
350 err = got_error(GOT_ERR_PRIVSEP_LEN);
353 len += sizeof(icommit);
355 *commit = got_object_commit_alloc_partial();
356 if (*commit == NULL) {
357 err = got_error_from_errno();
361 memcpy((*commit)->tree_id->sha1, icommit.tree_id,
364 if (icommit.author_len == 0) {
365 (*commit)->author = strdup("");
366 if ((*commit)->author == NULL) {
367 err = got_error_from_errno();
371 (*commit)->author = malloc(icommit.author_len + 1);
372 if ((*commit)->author == NULL) {
373 err = got_error_from_errno();
376 memcpy((*commit)->author, data + len,
378 (*commit)->author[icommit.author_len] = '\0';
380 len += icommit.author_len;
382 if (icommit.committer_len == 0) {
383 (*commit)->committer = strdup("");
384 if ((*commit)->committer == NULL) {
385 err = got_error_from_errno();
389 (*commit)->committer =
390 malloc(icommit.committer_len + 1);
391 if ((*commit)->committer == NULL) {
392 err = got_error_from_errno();
395 memcpy((*commit)->committer, data + len,
396 icommit.committer_len);
397 (*commit)->committer[icommit.committer_len] = '\0';
399 len += icommit.committer_len;
401 if (icommit.logmsg_len == 0) {
402 (*commit)->logmsg = strdup("");
403 if ((*commit)->logmsg == NULL) {
404 err = got_error_from_errno();
408 (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
409 if ((*commit)->logmsg == NULL) {
410 err = got_error_from_errno();
413 memcpy((*commit)->logmsg, data + len,
415 (*commit)->logmsg[icommit.logmsg_len] = '\0';
417 len += icommit.logmsg_len;
419 for (i = 0; i < icommit.nparents; i++) {
420 struct got_parent_id *pid;
422 pid = calloc(1, sizeof(*pid));
424 err = got_error_from_errno();
427 pid->id = calloc(1, sizeof(*pid->id));
428 if (pid->id == NULL) {
429 err = got_error_from_errno();
434 memcpy(pid->id, data + len + i * SHA1_DIGEST_LENGTH,
436 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
437 (*commit)->nparents++;
441 err = got_error(GOT_ERR_PRIVSEP_MSG);
450 const struct got_error *
451 got_privsep_send_tree_obj(struct imsgbuf *ibuf, struct got_tree_object *tree)
453 const struct got_error *err = NULL;
454 struct got_imsg_tree_object itree;
455 struct got_tree_entry *te;
457 itree.nentries = tree->nentries;
458 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
460 return got_error_from_errno();
462 err = flush_imsg(ibuf);
466 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
467 struct got_imsg_tree_entry ite;
469 size_t len = sizeof(ite) + strlen(te->name);
471 if (len > MAX_IMSGSIZE)
472 return got_error(GOT_ERR_NO_SPACE);
476 return got_error_from_errno();
478 memcpy(ite.id, te->id->sha1, sizeof(ite.id));
480 memcpy(buf, &ite, sizeof(ite));
481 memcpy(buf + sizeof(ite), te->name, strlen(te->name));
483 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
485 err = got_error_from_errno();
490 err = flush_imsg(ibuf);
498 const struct got_error *
499 got_privsep_recv_tree_obj(struct got_tree_object **tree, struct imsgbuf *ibuf)
501 const struct got_error *err = NULL;
502 const size_t min_datalen =
503 MIN(sizeof(struct got_imsg_error),
504 sizeof(struct got_imsg_tree_object));
505 struct got_imsg_tree_object itree = { 0 };
510 err = read_imsg(ibuf);
518 struct got_imsg_tree_entry ite;
519 struct got_tree_entry *te = NULL;
521 n = imsg_get(ibuf, &imsg);
523 if (*tree && (*tree)->nentries != nentries)
528 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
529 return got_error(GOT_ERR_PRIVSEP_LEN);
531 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
533 switch (imsg.hdr.type) {
535 err = recv_imsg_error(&imsg, datalen);
538 /* This message should only appear once. */
540 err = got_error(GOT_ERR_PRIVSEP_MSG);
543 if (datalen != sizeof(itree)) {
544 err = got_error(GOT_ERR_PRIVSEP_LEN);
547 memcpy(&itree, imsg.data, sizeof(itree));
548 *tree = calloc(1, sizeof(**tree));
550 err = got_error_from_errno();
553 (*tree)->nentries = itree.nentries;
554 SIMPLEQ_INIT(&(*tree)->entries);
556 case GOT_IMSG_TREE_ENTRY:
557 /* This message should be preceeded by GOT_IMSG_TREE. */
559 err = got_error(GOT_ERR_PRIVSEP_MSG);
562 if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
563 err = got_error(GOT_ERR_PRIVSEP_LEN);
567 /* Remaining data contains the entry's name. */
568 datalen -= sizeof(ite);
569 memcpy(&ite, imsg.data, sizeof(ite));
570 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
571 err = got_error(GOT_ERR_PRIVSEP_LEN);
575 te = got_alloc_tree_entry_partial();
577 err = got_error_from_errno();
580 te->name = malloc(datalen + 1);
581 if (te->name == NULL) {
583 err = got_error_from_errno();
586 memcpy(te->name, imsg.data + sizeof(ite), datalen);
587 te->name[datalen] = '\0';
589 memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
591 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
595 err = got_error(GOT_ERR_PRIVSEP_MSG);
602 if (*tree && (*tree)->nentries != nentries) {
604 err = got_error(GOT_ERR_PRIVSEP_LEN);
605 got_object_tree_close(*tree);