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