Blame


1 3efd8e31 2022-10-23 thomas /*
2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas *
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.
7 3efd8e31 2022-10-23 thomas *
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.
15 3efd8e31 2022-10-23 thomas */
16 3efd8e31 2022-10-23 thomas
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>
20 3efd8e31 2022-10-23 thomas
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>
30 3efd8e31 2022-10-23 thomas
31 3efd8e31 2022-10-23 thomas #include "got_error.h"
32 3efd8e31 2022-10-23 thomas
33 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
34 3efd8e31 2022-10-23 thomas
35 3efd8e31 2022-10-23 thomas #include "gotd.h"
36 3efd8e31 2022-10-23 thomas
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)
39 3efd8e31 2022-10-23 thomas {
40 3efd8e31 2022-10-23 thomas struct gotd_imsg_error ierr;
41 3efd8e31 2022-10-23 thomas size_t datalen;
42 3efd8e31 2022-10-23 thomas
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));
47 3efd8e31 2022-10-23 thomas
48 3efd8e31 2022-10-23 thomas if (client_id)
49 3efd8e31 2022-10-23 thomas *client_id = ierr.client_id;
50 3efd8e31 2022-10-23 thomas
51 3efd8e31 2022-10-23 thomas if (ierr.code == GOT_ERR_ERRNO)
52 3efd8e31 2022-10-23 thomas errno = ierr.errno_code;
53 3efd8e31 2022-10-23 thomas
54 3efd8e31 2022-10-23 thomas return got_error_msg(ierr.code, ierr.msg);
55 3efd8e31 2022-10-23 thomas }
56 3efd8e31 2022-10-23 thomas
57 3efd8e31 2022-10-23 thomas const struct got_error *
58 3efd8e31 2022-10-23 thomas gotd_imsg_flush(struct imsgbuf *ibuf)
59 3efd8e31 2022-10-23 thomas {
60 eac60ea1 2022-12-04 thomas const struct got_error *err = NULL;
61 3efd8e31 2022-10-23 thomas
62 eac60ea1 2022-12-04 thomas while (ibuf->w.queued > 0) {
63 eac60ea1 2022-12-04 thomas err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
64 eac60ea1 2022-12-04 thomas if (err)
65 eac60ea1 2022-12-04 thomas break;
66 3efd8e31 2022-10-23 thomas
67 eac60ea1 2022-12-04 thomas if (imsg_flush(ibuf) == -1) {
68 eac60ea1 2022-12-04 thomas if (errno != EAGAIN) {
69 eac60ea1 2022-12-04 thomas imsg_clear(ibuf);
70 eac60ea1 2022-12-04 thomas err = got_error_from_errno("imsg_flush");
71 eac60ea1 2022-12-04 thomas break;
72 eac60ea1 2022-12-04 thomas }
73 eac60ea1 2022-12-04 thomas }
74 eac60ea1 2022-12-04 thomas }
75 3efd8e31 2022-10-23 thomas
76 eac60ea1 2022-12-04 thomas return err;
77 3efd8e31 2022-10-23 thomas }
78 3efd8e31 2022-10-23 thomas
79 3efd8e31 2022-10-23 thomas const struct got_error *
80 3efd8e31 2022-10-23 thomas gotd_imsg_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
81 3efd8e31 2022-10-23 thomas {
82 3efd8e31 2022-10-23 thomas ssize_t n;
83 3efd8e31 2022-10-23 thomas
84 3efd8e31 2022-10-23 thomas n = imsg_get(ibuf, imsg);
85 3efd8e31 2022-10-23 thomas if (n == -1)
86 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_get");
87 3efd8e31 2022-10-23 thomas
88 3efd8e31 2022-10-23 thomas if (n == 0) {
89 3efd8e31 2022-10-23 thomas n = imsg_read(ibuf);
90 3efd8e31 2022-10-23 thomas if (n == -1) {
91 3efd8e31 2022-10-23 thomas if (errno == EAGAIN)
92 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_READ);
93 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_read");
94 3efd8e31 2022-10-23 thomas }
95 3efd8e31 2022-10-23 thomas if (n == 0)
96 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_EOF);
97 3efd8e31 2022-10-23 thomas n = imsg_get(ibuf, imsg);
98 3efd8e31 2022-10-23 thomas if (n == -1)
99 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_get");
100 3efd8e31 2022-10-23 thomas }
101 3efd8e31 2022-10-23 thomas
102 3efd8e31 2022-10-23 thomas if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
103 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
104 3efd8e31 2022-10-23 thomas
105 3efd8e31 2022-10-23 thomas return NULL;
106 3efd8e31 2022-10-23 thomas }
107 3efd8e31 2022-10-23 thomas
108 3efd8e31 2022-10-23 thomas const struct got_error *
109 3efd8e31 2022-10-23 thomas gotd_imsg_poll_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
110 3efd8e31 2022-10-23 thomas {
111 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
112 3efd8e31 2022-10-23 thomas
113 3efd8e31 2022-10-23 thomas for (;;) {
114 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv(imsg, ibuf, min_datalen);
115 3efd8e31 2022-10-23 thomas if (err == NULL || err->code != GOT_ERR_PRIVSEP_READ)
116 3efd8e31 2022-10-23 thomas return err;
117 3efd8e31 2022-10-23 thomas
118 3efd8e31 2022-10-23 thomas err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
119 3efd8e31 2022-10-23 thomas if (err)
120 3efd8e31 2022-10-23 thomas break;
121 3efd8e31 2022-10-23 thomas }
122 3efd8e31 2022-10-23 thomas
123 3efd8e31 2022-10-23 thomas return err;
124 3efd8e31 2022-10-23 thomas }
125 3efd8e31 2022-10-23 thomas
126 3efd8e31 2022-10-23 thomas int
127 3efd8e31 2022-10-23 thomas gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t peerid,
128 3efd8e31 2022-10-23 thomas uint32_t client_id, const struct got_error *err)
129 3efd8e31 2022-10-23 thomas {
130 3efd8e31 2022-10-23 thomas const struct got_error *flush_err;
131 3efd8e31 2022-10-23 thomas struct gotd_imsg_error ierr;
132 3efd8e31 2022-10-23 thomas int ret;
133 3efd8e31 2022-10-23 thomas
134 3efd8e31 2022-10-23 thomas ierr.code = err->code;
135 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_ERRNO)
136 3efd8e31 2022-10-23 thomas ierr.errno_code = errno;
137 3efd8e31 2022-10-23 thomas else
138 3efd8e31 2022-10-23 thomas ierr.errno_code = 0;
139 3efd8e31 2022-10-23 thomas ierr.client_id = client_id;
140 3efd8e31 2022-10-23 thomas strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
141 3efd8e31 2022-10-23 thomas
142 3efd8e31 2022-10-23 thomas ret = imsg_compose(ibuf, GOTD_IMSG_ERROR, peerid, getpid(), -1,
143 3efd8e31 2022-10-23 thomas &ierr, sizeof(ierr));
144 3efd8e31 2022-10-23 thomas if (ret == -1)
145 3efd8e31 2022-10-23 thomas return -1;
146 3efd8e31 2022-10-23 thomas
147 3efd8e31 2022-10-23 thomas flush_err = gotd_imsg_flush(ibuf);
148 3efd8e31 2022-10-23 thomas if (flush_err)
149 3efd8e31 2022-10-23 thomas return -1;
150 3efd8e31 2022-10-23 thomas
151 3efd8e31 2022-10-23 thomas return 0;
152 3efd8e31 2022-10-23 thomas }
153 3efd8e31 2022-10-23 thomas
154 3efd8e31 2022-10-23 thomas int
155 3efd8e31 2022-10-23 thomas gotd_imsg_send_error_event(struct gotd_imsgev *iev, uint32_t peerid,
156 3efd8e31 2022-10-23 thomas uint32_t client_id, const struct got_error *err)
157 3efd8e31 2022-10-23 thomas {
158 3efd8e31 2022-10-23 thomas struct gotd_imsg_error ierr;
159 3efd8e31 2022-10-23 thomas int ret;
160 3efd8e31 2022-10-23 thomas
161 3efd8e31 2022-10-23 thomas ierr.code = err->code;
162 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_ERRNO)
163 3efd8e31 2022-10-23 thomas ierr.errno_code = errno;
164 3efd8e31 2022-10-23 thomas else
165 3efd8e31 2022-10-23 thomas ierr.errno_code = 0;
166 3efd8e31 2022-10-23 thomas ierr.client_id = client_id;
167 3efd8e31 2022-10-23 thomas strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
168 3efd8e31 2022-10-23 thomas
169 3efd8e31 2022-10-23 thomas ret = gotd_imsg_compose_event(iev, GOTD_IMSG_ERROR, peerid, -1,
170 3efd8e31 2022-10-23 thomas &ierr, sizeof(ierr));
171 3efd8e31 2022-10-23 thomas if (ret == -1)
172 3efd8e31 2022-10-23 thomas return -1;
173 3efd8e31 2022-10-23 thomas
174 3efd8e31 2022-10-23 thomas return 0;
175 3efd8e31 2022-10-23 thomas }
176 3efd8e31 2022-10-23 thomas
177 3efd8e31 2022-10-23 thomas void
178 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(struct gotd_imsgev *iev)
179 3efd8e31 2022-10-23 thomas {
180 3efd8e31 2022-10-23 thomas iev->events = EV_READ;
181 3efd8e31 2022-10-23 thomas if (iev->ibuf.w.queued)
182 3efd8e31 2022-10-23 thomas iev->events |= EV_WRITE;
183 3efd8e31 2022-10-23 thomas
184 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
185 3efd8e31 2022-10-23 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
186 3efd8e31 2022-10-23 thomas event_add(&iev->ev, NULL);
187 3efd8e31 2022-10-23 thomas }
188 3efd8e31 2022-10-23 thomas
189 3efd8e31 2022-10-23 thomas int
190 3efd8e31 2022-10-23 thomas gotd_imsg_compose_event(struct gotd_imsgev *iev, uint16_t type, uint32_t peerid,
191 3efd8e31 2022-10-23 thomas int fd, void *data, uint16_t datalen)
192 3efd8e31 2022-10-23 thomas {
193 3efd8e31 2022-10-23 thomas int ret;
194 3efd8e31 2022-10-23 thomas
195 3efd8e31 2022-10-23 thomas ret = imsg_compose(&iev->ibuf, type, peerid, getpid(), fd,
196 3efd8e31 2022-10-23 thomas data, datalen);
197 3efd8e31 2022-10-23 thomas if (ret != -1)
198 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
199 3efd8e31 2022-10-23 thomas
200 3efd8e31 2022-10-23 thomas return ret;
201 3efd8e31 2022-10-23 thomas }
202 3efd8e31 2022-10-23 thomas
203 3efd8e31 2022-10-23 thomas int
204 3efd8e31 2022-10-23 thomas gotd_imsg_forward(struct gotd_imsgev *iev, struct imsg *imsg, int fd)
205 3efd8e31 2022-10-23 thomas {
206 3efd8e31 2022-10-23 thomas return gotd_imsg_compose_event(iev, imsg->hdr.type, imsg->hdr.peerid,
207 3efd8e31 2022-10-23 thomas fd, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);
208 3efd8e31 2022-10-23 thomas }