Hash :
94fbf93a
Author :
Date :
2018-04-22T15:48:01
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/*
* Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <poll.h>
#include <imsg.h>
#include <sha1.h>
#include <zlib.h>
#include "got_object.h"
#include "got_error.h"
#include "got_lib_sha1.h"
#include "got_lib_delta.h"
#include "got_lib_zbuf.h"
#include "got_lib_object.h"
#include "got_lib_privsep.h"
#ifndef MIN
#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
#endif
static const struct got_error *
poll_fd(int fd, int events, int timeout)
{
struct pollfd pfd[1];
int n;
pfd[0].fd = fd;
pfd[0].events = events;
n = poll(pfd, 1, timeout);
if (n == -1)
return got_error_from_errno();
if (n == 0)
return got_error(GOT_ERR_TIMEOUT);
if (pfd[0].revents & (POLLERR | POLLNVAL))
return got_error_from_errno();
if (pfd[0].revents & (events | POLLHUP))
return NULL;
return got_error(GOT_ERR_INTERRUPT);
}
/* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
void
got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
{
const struct got_error *poll_err;
struct got_imsg_error ierr;
int ret;
ierr.code = err->code;
if (err->code == GOT_ERR_ERRNO)
ierr.errno_code = errno;
else
ierr.errno_code = 0;
ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
if (ret != -1) {
fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
getprogname(), err->code, err->msg, strerror(errno));
}
poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
if (poll_err)
fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
getprogname(), err->code, err->msg, poll_err->msg);
ret = imsg_flush(ibuf);
if (ret == -1)
fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
getprogname(), err->code, err->msg, strerror(errno));
}
const struct got_error *
got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
{
const struct got_error *err = NULL;
struct got_imsg_object iobj;
iobj.type = obj->type;
iobj.flags = obj->flags;
iobj.hdrlen = obj->hdrlen;
iobj.size = obj->size;
iobj.ndeltas = ndeltas;
if (ndeltas > 0) {
/* TODO: Handle deltas */
}
if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
== -1)
return got_error_from_errno();
err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
if (err)
return err;
if (imsg_flush(ibuf) == -1)
return got_error_from_errno();
return NULL;
}
const struct got_error *
got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
struct got_imsg_error ierr;
struct imsg imsg;
struct got_imsg_object iobj;
ssize_t n, m;
size_t datalen;
int i;
*obj = NULL;
err = poll_fd(ibuf->fd, POLLIN, INFTIM);
if (err)
return err;
n = imsg_read(ibuf);
if (n == -1) {
if (errno == EAGAIN) /* Could be a file-descriptor leak. */
return got_error(GOT_ERR_PRIVSEP_NO_FD);
return got_error(GOT_ERR_PRIVSEP_READ);
}
if (n == 0)
return got_error(GOT_ERR_PRIVSEP_PIPE);
m = imsg_get(ibuf, &imsg);
if (m == 0)
return got_error(GOT_ERR_PRIVSEP_READ);
if (imsg.hdr.len < IMSG_HEADER_SIZE + MIN(sizeof(ierr), sizeof(obj)))
return got_error(GOT_ERR_PRIVSEP_LEN);
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
switch (imsg.hdr.type) {
case GOT_IMSG_ERROR:
if (datalen != sizeof(ierr)) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
memcpy(&ierr, imsg.data, sizeof(ierr));
if (ierr.code == GOT_ERR_ERRNO) {
static struct got_error serr;
serr.code = GOT_ERR_ERRNO;
serr.msg = strerror(ierr.errno_code);
err = &serr;
} else
err = got_error(ierr.code);
break;
case GOT_IMSG_OBJECT:
if (datalen != sizeof(iobj)) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
memcpy(&iobj, imsg.data, sizeof(iobj));
if (iobj.ndeltas < 0 ||
iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
*obj = calloc(1, sizeof(**obj));
if (*obj == NULL) {
err = got_error_from_errno();
break;
}
(*obj)->type = iobj.type;
(*obj)->hdrlen = iobj.hdrlen;
(*obj)->size = iobj.size;
for (i = 0; i < iobj.ndeltas; i++) {
/* TODO: Handle deltas */
}
break;
default:
err = got_error(GOT_ERR_PRIVSEP_MSG);
break;
}
imsg_free(&imsg);
return err;
}