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 recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
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);
85 m = imsg_get(ibuf, imsg);
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);
95 static const struct got_error *
96 recv_imsg_error(struct imsg *imsg, size_t datalen)
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);
111 return got_error(ierr.code);
114 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
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;
122 ierr.code = err->code;
123 if (err->code == GOT_ERR_ERRNO)
124 ierr.errno_code = errno;
127 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
129 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
130 getprogname(), err->code, err->msg, strerror(errno));
134 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
136 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
137 getprogname(), err->code, err->msg, poll_err->msg);
141 ret = imsg_flush(ibuf);
143 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
144 getprogname(), err->code, err->msg, strerror(errno));
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;
162 /* TODO: Handle deltas */
165 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
167 return got_error_from_errno();
169 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
173 if (imsg_flush(ibuf) == -1)
174 return got_error_from_errno();
179 const struct got_error *
180 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
182 const struct got_error *err = NULL;
184 struct got_imsg_object iobj;
187 const size_t min_datalen =
188 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
192 err = recv_one_imsg(&imsg, ibuf, min_datalen);
196 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
198 switch (imsg.hdr.type) {
200 err = recv_imsg_error(&imsg, datalen);
202 case GOT_IMSG_OBJECT:
203 if (datalen != sizeof(iobj)) {
204 err = got_error(GOT_ERR_PRIVSEP_LEN);
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);
215 *obj = calloc(1, sizeof(**obj));
217 err = got_error_from_errno();
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 */
229 err = got_error(GOT_ERR_PRIVSEP_MSG);
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;
245 struct got_parent_id *pid;
247 if (got_sha1_digest_to_str(commit->tree_id->sha1, icommit.tree_id,
248 sizeof(icommit.tree_id)) == NULL)
249 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
250 icommit.author_len = strlen(commit->author);
251 icommit.committer_len = strlen(commit->committer);
252 icommit.logmsg_len = strlen(commit->logmsg);
253 icommit.nparents = commit->nparents;
255 total = sizeof(icommit) + icommit.author_len +
256 icommit.committer_len + icommit.logmsg_len +
257 icommit.nparents * (SHA1_DIGEST_STRING_LENGTH);
258 /* XXX TODO support very large log messages properly */
259 if (total > MAX_IMSGSIZE)
260 return got_error(GOT_ERR_NO_SPACE);
264 return got_error_from_errno();
267 memcpy(buf + len, &icommit, sizeof(icommit));
268 len += sizeof(icommit);
269 memcpy(buf + len, commit->author, icommit.author_len);
270 len += icommit.author_len;
271 memcpy(buf + len, commit->committer, icommit.committer_len);
272 len += icommit.committer_len;
273 memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
274 len += icommit.logmsg_len;
275 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
276 char id_str[SHA1_DIGEST_STRING_LENGTH];
277 if (got_sha1_digest_to_str(pid->id->sha1, id_str,
278 sizeof(id_str)) == NULL) {
279 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
282 memcpy(buf + len, id_str, SHA1_DIGEST_STRING_LENGTH);
283 len += SHA1_DIGEST_STRING_LENGTH;
286 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
287 err = got_error_from_errno();
291 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
295 if (imsg_flush(ibuf) == -1) {
296 err = got_error_from_errno();
304 const struct got_error *
305 got_privsep_recv_commit_obj(struct got_commit_object **commit,
306 struct imsgbuf *ibuf)
308 const struct got_error *err = NULL;
310 struct got_imsg_commit_object icommit;
313 const size_t min_datalen =
314 MIN(sizeof(struct got_imsg_error),
315 sizeof(struct got_imsg_commit_object));
320 err = recv_one_imsg(&imsg, ibuf, min_datalen);
325 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
328 switch (imsg.hdr.type) {
330 err = recv_imsg_error(&imsg, datalen);
332 case GOT_IMSG_COMMIT:
333 if (datalen < sizeof(icommit)) {
334 err = got_error(GOT_ERR_PRIVSEP_LEN);
338 memcpy(&icommit, data, sizeof(icommit));
339 if (datalen != sizeof(icommit) + icommit.author_len +
340 icommit.committer_len + icommit.logmsg_len +
341 icommit.nparents * (SHA1_DIGEST_STRING_LENGTH)) {
342 err = got_error(GOT_ERR_PRIVSEP_LEN);
345 if (icommit.nparents < 0) {
346 err = got_error(GOT_ERR_PRIVSEP_LEN);
349 len += sizeof(icommit);
351 *commit = got_object_commit_alloc_partial();
352 if (*commit == NULL) {
353 err = got_error_from_errno();
357 if (!got_parse_sha1_digest((*commit)->tree_id->sha1,
359 err = got_error(GOT_ERR_BAD_OBJ_DATA);
363 if (icommit.author_len == 0) {
364 (*commit)->author = strdup("");
365 if ((*commit)->author == NULL) {
366 err = got_error_from_errno();
370 (*commit)->author = malloc(icommit.author_len + 1);
371 if ((*commit)->author == NULL) {
372 err = got_error_from_errno();
375 memcpy((*commit)->author, data + len,
377 (*commit)->author[icommit.author_len] = '\0';
379 len += icommit.author_len;
381 if (icommit.committer_len == 0) {
382 (*commit)->committer = strdup("");
383 if ((*commit)->committer == NULL) {
384 err = got_error_from_errno();
388 (*commit)->committer =
389 malloc(icommit.committer_len + 1);
390 if ((*commit)->committer == NULL) {
391 err = got_error_from_errno();
394 memcpy((*commit)->committer, data + len,
395 icommit.committer_len);
396 (*commit)->committer[icommit.committer_len] = '\0';
398 len += icommit.committer_len;
400 if (icommit.logmsg_len == 0) {
401 (*commit)->logmsg = strdup("");
402 if ((*commit)->logmsg == NULL) {
403 err = got_error_from_errno();
407 (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
408 if ((*commit)->logmsg == NULL) {
409 err = got_error_from_errno();
412 memcpy((*commit)->logmsg, data + len,
414 (*commit)->logmsg[icommit.logmsg_len] = '\0';
416 len += icommit.logmsg_len;
418 for (i = 0; i < icommit.nparents; i++) {
419 char id_str[SHA1_DIGEST_STRING_LENGTH];
420 memcpy(id_str, data + len +
421 i * SHA1_DIGEST_STRING_LENGTH, sizeof(id_str));
422 id_str[SHA1_DIGEST_STRING_LENGTH - 1] = '\0';
423 err = got_object_commit_add_parent(*commit, id_str);
429 err = got_error(GOT_ERR_PRIVSEP_MSG);