2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
18 3efd8e31 2022-10-23 thomas #include <sys/types.h>
19 3efd8e31 2022-10-23 thomas #include <sys/uio.h>
21 3efd8e31 2022-10-23 thomas #include <errno.h>
22 3efd8e31 2022-10-23 thomas #include <event.h>
23 3efd8e31 2022-10-23 thomas #include <imsg.h>
24 3efd8e31 2022-10-23 thomas #include <limits.h>
25 3efd8e31 2022-10-23 thomas #include <poll.h>
26 3efd8e31 2022-10-23 thomas #include <sha1.h>
27 3efd8e31 2022-10-23 thomas #include <stdio.h>
28 3efd8e31 2022-10-23 thomas #include <string.h>
29 3efd8e31 2022-10-23 thomas #include <unistd.h>
31 3efd8e31 2022-10-23 thomas #include "got_error.h"
33 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
35 3efd8e31 2022-10-23 thomas #include "gotd.h"
37 3efd8e31 2022-10-23 thomas const struct got_error *
38 3efd8e31 2022-10-23 thomas gotd_imsg_recv_error(uint32_t *client_id, struct imsg *imsg)
40 3efd8e31 2022-10-23 thomas struct gotd_imsg_error ierr;
41 3efd8e31 2022-10-23 thomas size_t datalen;
43 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
44 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ierr))
45 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
46 3efd8e31 2022-10-23 thomas memcpy(&ierr, imsg->data, sizeof(ierr));
48 3efd8e31 2022-10-23 thomas if (client_id)
49 3efd8e31 2022-10-23 thomas *client_id = ierr.client_id;
51 3efd8e31 2022-10-23 thomas if (ierr.code == GOT_ERR_ERRNO)
52 3efd8e31 2022-10-23 thomas errno = ierr.errno_code;
54 3efd8e31 2022-10-23 thomas return got_error_msg(ierr.code, ierr.msg);
57 3efd8e31 2022-10-23 thomas const struct got_error *
58 3efd8e31 2022-10-23 thomas gotd_imsg_flush(struct imsgbuf *ibuf)
60 3efd8e31 2022-10-23 thomas const struct got_error *err;
62 3efd8e31 2022-10-23 thomas err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
64 3efd8e31 2022-10-23 thomas return err;
66 3efd8e31 2022-10-23 thomas if (imsg_flush(ibuf) == -1) {
67 3efd8e31 2022-10-23 thomas imsg_clear(ibuf);
68 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_flush");
71 3efd8e31 2022-10-23 thomas return NULL;
74 3efd8e31 2022-10-23 thomas const struct got_error *
75 3efd8e31 2022-10-23 thomas gotd_imsg_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
77 3efd8e31 2022-10-23 thomas ssize_t n;
79 3efd8e31 2022-10-23 thomas n = imsg_get(ibuf, imsg);
80 3efd8e31 2022-10-23 thomas if (n == -1)
81 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_get");
83 3efd8e31 2022-10-23 thomas if (n == 0) {
84 3efd8e31 2022-10-23 thomas n = imsg_read(ibuf);
85 3efd8e31 2022-10-23 thomas if (n == -1) {
86 3efd8e31 2022-10-23 thomas if (errno == EAGAIN)
87 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_READ);
88 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_read");
90 3efd8e31 2022-10-23 thomas if (n == 0)
91 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_EOF);
92 3efd8e31 2022-10-23 thomas n = imsg_get(ibuf, imsg);
93 3efd8e31 2022-10-23 thomas if (n == -1)
94 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_get");
97 3efd8e31 2022-10-23 thomas if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
98 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
100 3efd8e31 2022-10-23 thomas return NULL;
103 3efd8e31 2022-10-23 thomas const struct got_error *
104 3efd8e31 2022-10-23 thomas gotd_imsg_poll_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
106 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
108 3efd8e31 2022-10-23 thomas for (;;) {
109 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv(imsg, ibuf, min_datalen);
110 3efd8e31 2022-10-23 thomas if (err == NULL || err->code != GOT_ERR_PRIVSEP_READ)
111 3efd8e31 2022-10-23 thomas return err;
113 3efd8e31 2022-10-23 thomas err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
118 3efd8e31 2022-10-23 thomas return err;
122 3efd8e31 2022-10-23 thomas gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t peerid,
123 3efd8e31 2022-10-23 thomas uint32_t client_id, const struct got_error *err)
125 3efd8e31 2022-10-23 thomas const struct got_error *flush_err;
126 3efd8e31 2022-10-23 thomas struct gotd_imsg_error ierr;
129 3efd8e31 2022-10-23 thomas ierr.code = err->code;
130 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_ERRNO)
131 3efd8e31 2022-10-23 thomas ierr.errno_code = errno;
133 3efd8e31 2022-10-23 thomas ierr.errno_code = 0;
134 3efd8e31 2022-10-23 thomas ierr.client_id = client_id;
135 3efd8e31 2022-10-23 thomas strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
137 3efd8e31 2022-10-23 thomas ret = imsg_compose(ibuf, GOTD_IMSG_ERROR, peerid, getpid(), -1,
138 3efd8e31 2022-10-23 thomas &ierr, sizeof(ierr));
139 3efd8e31 2022-10-23 thomas if (ret == -1)
140 3efd8e31 2022-10-23 thomas return -1;
142 3efd8e31 2022-10-23 thomas flush_err = gotd_imsg_flush(ibuf);
143 3efd8e31 2022-10-23 thomas if (flush_err)
144 3efd8e31 2022-10-23 thomas return -1;
146 3efd8e31 2022-10-23 thomas return 0;
150 3efd8e31 2022-10-23 thomas gotd_imsg_send_error_event(struct gotd_imsgev *iev, uint32_t peerid,
151 3efd8e31 2022-10-23 thomas uint32_t client_id, const struct got_error *err)
153 3efd8e31 2022-10-23 thomas struct gotd_imsg_error ierr;
156 3efd8e31 2022-10-23 thomas ierr.code = err->code;
157 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_ERRNO)
158 3efd8e31 2022-10-23 thomas ierr.errno_code = errno;
160 3efd8e31 2022-10-23 thomas ierr.errno_code = 0;
161 3efd8e31 2022-10-23 thomas ierr.client_id = client_id;
162 3efd8e31 2022-10-23 thomas strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
164 3efd8e31 2022-10-23 thomas ret = gotd_imsg_compose_event(iev, GOTD_IMSG_ERROR, peerid, -1,
165 3efd8e31 2022-10-23 thomas &ierr, sizeof(ierr));
166 3efd8e31 2022-10-23 thomas if (ret == -1)
167 3efd8e31 2022-10-23 thomas return -1;
169 3efd8e31 2022-10-23 thomas return 0;
173 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(struct gotd_imsgev *iev)
175 3efd8e31 2022-10-23 thomas iev->events = EV_READ;
176 3efd8e31 2022-10-23 thomas if (iev->ibuf.w.queued)
177 3efd8e31 2022-10-23 thomas iev->events |= EV_WRITE;
179 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
180 3efd8e31 2022-10-23 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
181 3efd8e31 2022-10-23 thomas event_add(&iev->ev, NULL);
185 3efd8e31 2022-10-23 thomas gotd_imsg_compose_event(struct gotd_imsgev *iev, uint16_t type, uint32_t peerid,
186 3efd8e31 2022-10-23 thomas int fd, void *data, uint16_t datalen)
190 3efd8e31 2022-10-23 thomas ret = imsg_compose(&iev->ibuf, type, peerid, getpid(), fd,
191 3efd8e31 2022-10-23 thomas data, datalen);
192 3efd8e31 2022-10-23 thomas if (ret != -1)
193 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
195 3efd8e31 2022-10-23 thomas return ret;
199 3efd8e31 2022-10-23 thomas gotd_imsg_forward(struct gotd_imsgev *iev, struct imsg *imsg, int fd)
201 3efd8e31 2022-10-23 thomas return gotd_imsg_compose_event(iev, imsg->hdr.type, imsg->hdr.peerid,
202 3efd8e31 2022-10-23 thomas fd, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);