Blame


1 c8dba1e6 2021-07-24 op /*
2 5657662f 2024-01-16 op * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
3 c8dba1e6 2021-07-24 op *
4 c8dba1e6 2021-07-24 op * Permission to use, copy, modify, and distribute this software for any
5 c8dba1e6 2021-07-24 op * purpose with or without fee is hereby granted, provided that the above
6 c8dba1e6 2021-07-24 op * copyright notice and this permission notice appear in all copies.
7 c8dba1e6 2021-07-24 op *
8 c8dba1e6 2021-07-24 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c8dba1e6 2021-07-24 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c8dba1e6 2021-07-24 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c8dba1e6 2021-07-24 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c8dba1e6 2021-07-24 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c8dba1e6 2021-07-24 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c8dba1e6 2021-07-24 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c8dba1e6 2021-07-24 op */
16 c8dba1e6 2021-07-24 op
17 c8dba1e6 2021-07-24 op #include "compat.h"
18 c8dba1e6 2021-07-24 op
19 d35e18b3 2024-02-04 op #include <sys/mman.h>
20 c8dba1e6 2021-07-24 op #include <sys/types.h>
21 c8dba1e6 2021-07-24 op #include <sys/socket.h>
22 d35e18b3 2024-02-04 op #include <sys/stat.h>
23 c8dba1e6 2021-07-24 op
24 c8dba1e6 2021-07-24 op #include <netinet/in.h>
25 c8dba1e6 2021-07-24 op
26 c8dba1e6 2021-07-24 op #include <assert.h>
27 c8dba1e6 2021-07-24 op #include <ctype.h>
28 c8dba1e6 2021-07-24 op #include <errno.h>
29 c8dba1e6 2021-07-24 op #include <netdb.h>
30 c8dba1e6 2021-07-24 op #include <stdarg.h>
31 c8dba1e6 2021-07-24 op #include <stdio.h>
32 c8dba1e6 2021-07-24 op #include <stdlib.h>
33 c8dba1e6 2021-07-24 op #include <string.h>
34 c8dba1e6 2021-07-24 op #include <tls.h>
35 c8dba1e6 2021-07-24 op #include <unistd.h>
36 c8dba1e6 2021-07-24 op
37 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
38 c8dba1e6 2021-07-24 op # include <asr.h>
39 c8dba1e6 2021-07-24 op #endif
40 c8dba1e6 2021-07-24 op
41 98d3e6c1 2024-02-18 op #include "bufio.h"
42 98d3e6c1 2024-02-18 op #include "ev.h"
43 87d297d1 2024-02-22 op #include "imsgev.h"
44 c8dba1e6 2021-07-24 op #include "telescope.h"
45 9d65b1d9 2022-01-11 op #include "utils.h"
46 c8dba1e6 2021-07-24 op
47 c8dba1e6 2021-07-24 op static struct imsgev *iev_ui;
48 c8dba1e6 2021-07-24 op
49 98d3e6c1 2024-02-18 op enum conn_state {
50 98d3e6c1 2024-02-18 op CONN_CONNECTING,
51 98d3e6c1 2024-02-18 op CONN_HANDSHAKE,
52 98d3e6c1 2024-02-18 op CONN_HEADER,
53 98d3e6c1 2024-02-18 op CONN_BODY,
54 98d3e6c1 2024-02-18 op CONN_CLOSE,
55 98d3e6c1 2024-02-18 op CONN_ERROR,
56 98d3e6c1 2024-02-18 op };
57 98d3e6c1 2024-02-18 op
58 edfe87be 2021-07-24 op /* a pending request */
59 edfe87be 2021-07-24 op struct req {
60 edfe87be 2021-07-24 op uint32_t id;
61 98d3e6c1 2024-02-18 op enum conn_state state;
62 5fb52fc0 2021-07-25 op int proto;
63 edfe87be 2021-07-24 op int fd;
64 5657662f 2024-01-16 op char *host;
65 5657662f 2024-01-16 op char *port;
66 5657662f 2024-01-16 op char *req;
67 edfe87be 2021-07-24 op size_t len;
68 d35e18b3 2024-02-04 op void *ccert;
69 d35e18b3 2024-02-04 op size_t ccert_len;
70 d35e18b3 2024-02-04 op int ccert_fd;
71 c8dba1e6 2021-07-24 op
72 98d3e6c1 2024-02-18 op int eof;
73 62d3cd29 2024-02-22 op unsigned int timer;
74 98d3e6c1 2024-02-18 op struct bufio bio;
75 98d3e6c1 2024-02-18 op
76 c6287992 2024-02-13 op int conn_error;
77 c6287992 2024-02-13 op const char *cause;
78 c6287992 2024-02-13 op
79 edfe87be 2021-07-24 op struct addrinfo *servinfo, *p;
80 edfe87be 2021-07-24 op #if HAVE_ASR_RUN
81 10884685 2024-02-22 op struct asr_query *q;
82 10884685 2024-02-22 op int ar_fd;
83 edfe87be 2021-07-24 op #endif
84 edfe87be 2021-07-24 op
85 edfe87be 2021-07-24 op TAILQ_ENTRY(req) reqs;
86 edfe87be 2021-07-24 op };
87 edfe87be 2021-07-24 op
88 e2a349f7 2021-07-24 op static struct req *req_by_id(uint32_t);
89 e2a349f7 2021-07-24 op
90 77ae5e91 2021-07-24 op static void die(void) __attribute__((__noreturn__));
91 c8dba1e6 2021-07-24 op
92 77ae5e91 2021-07-24 op static void close_with_err(struct req*, const char*);
93 77ae5e91 2021-07-24 op static void close_with_errf(struct req*, const char*, ...)
94 bfb9acb0 2021-07-24 op __attribute__((format(printf, 2, 3)));
95 c8dba1e6 2021-07-24 op
96 98d3e6c1 2024-02-18 op static int try_to_connect(struct req *);
97 5f285272 2024-03-25 op static int gemini_parse_reply(struct req *, const char *);
98 98d3e6c1 2024-02-18 op static void net_ev(int, int, void *);
99 98d3e6c1 2024-02-18 op static void handle_dispatch_imsg(int, int, void*);
100 5b762f01 2021-07-24 op
101 77ae5e91 2021-07-24 op static int net_send_ui(int, uint32_t, const void *, uint16_t);
102 c8dba1e6 2021-07-24 op
103 c8dba1e6 2021-07-24 op /* TODO: making this customizable */
104 c8dba1e6 2021-07-24 op struct timeval timeout_for_handshake = { 5, 0 };
105 c8dba1e6 2021-07-24 op
106 c8dba1e6 2021-07-24 op TAILQ_HEAD(, req) reqhead;
107 c8dba1e6 2021-07-24 op
108 e2a349f7 2021-07-24 op static struct req *
109 e2a349f7 2021-07-24 op req_by_id(uint32_t id)
110 e2a349f7 2021-07-24 op {
111 e2a349f7 2021-07-24 op struct req *r;
112 e2a349f7 2021-07-24 op
113 e2a349f7 2021-07-24 op TAILQ_FOREACH(r, &reqhead, reqs) {
114 e2a349f7 2021-07-24 op if (r->id == id)
115 e2a349f7 2021-07-24 op return r;
116 e2a349f7 2021-07-24 op }
117 e2a349f7 2021-07-24 op
118 e2a349f7 2021-07-24 op return NULL;
119 e2a349f7 2021-07-24 op }
120 e2a349f7 2021-07-24 op
121 c8dba1e6 2021-07-24 op static void __attribute__((__noreturn__))
122 c8dba1e6 2021-07-24 op die(void)
123 c8dba1e6 2021-07-24 op {
124 c8dba1e6 2021-07-24 op abort(); /* TODO */
125 c8dba1e6 2021-07-24 op }
126 c8dba1e6 2021-07-24 op
127 ce023433 2024-02-22 op static inline int
128 ce023433 2024-02-22 op req_bio_ev(struct req *req)
129 ce023433 2024-02-22 op {
130 ce023433 2024-02-22 op int ret, ev;
131 ce023433 2024-02-22 op
132 ce023433 2024-02-22 op ret = 0;
133 ce023433 2024-02-22 op ev = bufio_ev(&req->bio);
134 ce023433 2024-02-22 op if (ev & BUFIO_WANT_READ)
135 ce023433 2024-02-22 op ret |= EV_READ;
136 ce023433 2024-02-22 op if (ev & BUFIO_WANT_WRITE)
137 ce023433 2024-02-22 op ret |= EV_WRITE;
138 ce023433 2024-02-22 op return (ret);
139 ce023433 2024-02-22 op }
140 ce023433 2024-02-22 op
141 c8dba1e6 2021-07-24 op static void
142 98d3e6c1 2024-02-18 op close_conn(int fd, int ev, void *d)
143 c8dba1e6 2021-07-24 op {
144 c8dba1e6 2021-07-24 op struct req *req = d;
145 c8dba1e6 2021-07-24 op
146 98d3e6c1 2024-02-18 op if (req->state != CONN_ERROR)
147 98d3e6c1 2024-02-18 op req->state = CONN_CLOSE;
148 98d3e6c1 2024-02-18 op
149 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
150 10884685 2024-02-22 op if (req->q) {
151 10884685 2024-02-22 op asr_abort(req->q);
152 10884685 2024-02-22 op ev_del(req->ar_fd);
153 10884685 2024-02-22 op }
154 c8dba1e6 2021-07-24 op #endif
155 c8dba1e6 2021-07-24 op
156 62d3cd29 2024-02-22 op if (req->timer != 0) {
157 62d3cd29 2024-02-22 op ev_timer_cancel(req->timer);
158 62d3cd29 2024-02-22 op req->timer = 0;
159 5b762f01 2021-07-24 op }
160 5b762f01 2021-07-24 op
161 98d3e6c1 2024-02-18 op if (req->state == CONN_CLOSE &&
162 99ebdacb 2024-02-18 op req->fd != -1 &&
163 98d3e6c1 2024-02-18 op bufio_close(&req->bio) == -1 &&
164 98d3e6c1 2024-02-18 op errno == EAGAIN) {
165 ce023433 2024-02-22 op ev_add(req->fd, req_bio_ev(req), close_conn, req);
166 98d3e6c1 2024-02-18 op return;
167 d35e18b3 2024-02-04 op }
168 d35e18b3 2024-02-04 op
169 98d3e6c1 2024-02-18 op if (req->servinfo)
170 98d3e6c1 2024-02-18 op freeaddrinfo(req->servinfo);
171 98d3e6c1 2024-02-18 op
172 98d3e6c1 2024-02-18 op bufio_free(&req->bio);
173 98d3e6c1 2024-02-18 op
174 d35e18b3 2024-02-04 op if (req->ccert != NULL) {
175 d35e18b3 2024-02-04 op munmap(req->ccert, req->ccert_len);
176 d35e18b3 2024-02-04 op close(req->ccert_fd);
177 c8dba1e6 2021-07-24 op }
178 c8dba1e6 2021-07-24 op
179 5657662f 2024-01-16 op free(req->host);
180 5657662f 2024-01-16 op free(req->port);
181 5657662f 2024-01-16 op free(req->req);
182 5657662f 2024-01-16 op
183 c8dba1e6 2021-07-24 op TAILQ_REMOVE(&reqhead, req, reqs);
184 98d3e6c1 2024-02-18 op if (req->fd != -1) {
185 98d3e6c1 2024-02-18 op ev_del(req->fd);
186 c8dba1e6 2021-07-24 op close(req->fd);
187 98d3e6c1 2024-02-18 op }
188 c8dba1e6 2021-07-24 op free(req);
189 c8dba1e6 2021-07-24 op }
190 c8dba1e6 2021-07-24 op
191 c8dba1e6 2021-07-24 op static void
192 c8dba1e6 2021-07-24 op close_with_err(struct req *req, const char *err)
193 c8dba1e6 2021-07-24 op {
194 98d3e6c1 2024-02-18 op req->state = CONN_ERROR;
195 c8dba1e6 2021-07-24 op net_send_ui(IMSG_ERR, req->id, err, strlen(err)+1);
196 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
197 c8dba1e6 2021-07-24 op }
198 c8dba1e6 2021-07-24 op
199 c8dba1e6 2021-07-24 op static void
200 c8dba1e6 2021-07-24 op close_with_errf(struct req *req, const char *fmt, ...)
201 c8dba1e6 2021-07-24 op {
202 c8dba1e6 2021-07-24 op va_list ap;
203 c8dba1e6 2021-07-24 op char *s;
204 c8dba1e6 2021-07-24 op
205 c8dba1e6 2021-07-24 op va_start(ap, fmt);
206 c8dba1e6 2021-07-24 op if (vasprintf(&s, fmt, ap) == -1)
207 c8dba1e6 2021-07-24 op abort();
208 c8dba1e6 2021-07-24 op va_end(ap);
209 c8dba1e6 2021-07-24 op
210 c8dba1e6 2021-07-24 op close_with_err(req, s);
211 c8dba1e6 2021-07-24 op free(s);
212 c8dba1e6 2021-07-24 op }
213 c8dba1e6 2021-07-24 op
214 10884685 2024-02-22 op #if HAVE_ASR_RUN
215 10884685 2024-02-22 op static void
216 10884685 2024-02-22 op req_resolve(int fd, int ev, void *d)
217 10884685 2024-02-22 op {
218 10884685 2024-02-22 op struct req *req = d;
219 10884685 2024-02-22 op struct addrinfo hints;
220 10884685 2024-02-22 op struct asr_result ar;
221 10884685 2024-02-22 op struct timeval tv;
222 10884685 2024-02-22 op
223 10884685 2024-02-22 op if (req->q == NULL) {
224 10884685 2024-02-22 op memset(&hints, 0, sizeof(hints));
225 10884685 2024-02-22 op hints.ai_family = AF_UNSPEC;
226 10884685 2024-02-22 op hints.ai_socktype = SOCK_STREAM;
227 10884685 2024-02-22 op
228 10884685 2024-02-22 op req->q = getaddrinfo_async(req->host, req->port, &hints, NULL);
229 10884685 2024-02-22 op if (req->q == NULL) {
230 10884685 2024-02-22 op close_with_errf(req, "getaddrinfo_async: %s",
231 10884685 2024-02-22 op strerror(errno));
232 10884685 2024-02-22 op return;
233 10884685 2024-02-22 op }
234 10884685 2024-02-22 op }
235 10884685 2024-02-22 op
236 10884685 2024-02-22 op if (fd != -1)
237 10884685 2024-02-22 op ev_del(fd);
238 62d3cd29 2024-02-22 op if (req->timer) {
239 62d3cd29 2024-02-22 op ev_timer_cancel(req->timer);
240 62d3cd29 2024-02-22 op req->timer = 0;
241 10884685 2024-02-22 op }
242 10884685 2024-02-22 op
243 10884685 2024-02-22 op if (asr_run(req->q, &ar) == 0) {
244 10884685 2024-02-22 op ev = 0;
245 10884685 2024-02-22 op if (ar.ar_cond & ASR_WANT_READ)
246 10884685 2024-02-22 op ev |= EV_READ;
247 10884685 2024-02-22 op if (ar.ar_cond & ASR_WANT_WRITE)
248 10884685 2024-02-22 op ev |= EV_WRITE;
249 10884685 2024-02-22 op
250 10884685 2024-02-22 op req->ar_fd = ar.ar_fd;
251 10884685 2024-02-22 op if (ev_add(req->ar_fd, ev, req_resolve, req) == -1) {
252 10884685 2024-02-22 op close_with_errf(req, "ev_add failure: %s",
253 10884685 2024-02-22 op strerror(errno));
254 10884685 2024-02-22 op return;
255 10884685 2024-02-22 op }
256 10884685 2024-02-22 op
257 10884685 2024-02-22 op tv.tv_sec = ar.ar_timeout / 1000;
258 10884685 2024-02-22 op tv.tv_usec = (ar.ar_timeout % 1000) * 1000;
259 62d3cd29 2024-02-22 op req->timer = ev_timer(&tv, req_resolve, req);
260 62d3cd29 2024-02-22 op if (req->timer == 0)
261 10884685 2024-02-22 op close_with_errf(req, "ev_timer failure: %s",
262 10884685 2024-02-22 op strerror(errno));
263 10884685 2024-02-22 op return;
264 10884685 2024-02-22 op }
265 10884685 2024-02-22 op
266 10884685 2024-02-22 op req->ar_fd = -1;
267 10884685 2024-02-22 op req->q = NULL;
268 10884685 2024-02-22 op
269 10884685 2024-02-22 op if (ar.ar_gai_errno) {
270 10884685 2024-02-22 op close_with_errf(req, "failed to resolve %s: %s",
271 10884685 2024-02-22 op req->host, gai_strerror(ar.ar_gai_errno));
272 10884685 2024-02-22 op return;
273 10884685 2024-02-22 op }
274 10884685 2024-02-22 op
275 10884685 2024-02-22 op req->servinfo = ar.ar_addrinfo;
276 10884685 2024-02-22 op
277 10884685 2024-02-22 op req->p = req->servinfo;
278 10884685 2024-02-22 op net_ev(-1, EV_READ, req);
279 10884685 2024-02-22 op }
280 10884685 2024-02-22 op #else
281 10884685 2024-02-22 op static void
282 10884685 2024-02-22 op req_resolve(int fd, int ev, struct req *req)
283 10884685 2024-02-22 op {
284 10884685 2024-02-22 op struct addrinfo hints;
285 10884685 2024-02-22 op int s;
286 10884685 2024-02-22 op
287 10884685 2024-02-22 op memset(&hints, 0, sizeof(hints));
288 10884685 2024-02-22 op hints.ai_family = AF_UNSPEC;
289 10884685 2024-02-22 op hints.ai_socktype = SOCK_STREAM;
290 10884685 2024-02-22 op
291 10884685 2024-02-22 op s = getaddrinfo(req->host, req->port, &hints, &req->servinfo);
292 10884685 2024-02-22 op if (s != 0) {
293 10884685 2024-02-22 op close_with_errf(req, "failed to resolve %s: %s",
294 10884685 2024-02-22 op req->host, gai_strerror(s));
295 10884685 2024-02-22 op return;
296 10884685 2024-02-22 op }
297 10884685 2024-02-22 op
298 10884685 2024-02-22 op req->fd = -1;
299 10884685 2024-02-22 op req->p = req->servinfo;
300 10884685 2024-02-22 op net_ev(-1, EV_READ, req);
301 10884685 2024-02-22 op }
302 10884685 2024-02-22 op #endif
303 10884685 2024-02-22 op
304 98d3e6c1 2024-02-18 op static int
305 98d3e6c1 2024-02-18 op try_to_connect(struct req *req)
306 c8dba1e6 2021-07-24 op {
307 98d3e6c1 2024-02-18 op int error;
308 98d3e6c1 2024-02-18 op socklen_t len = sizeof(error);
309 c8dba1e6 2021-07-24 op
310 98d3e6c1 2024-02-18 op again:
311 98d3e6c1 2024-02-18 op if (req->p == NULL)
312 98d3e6c1 2024-02-18 op return (-1);
313 c8dba1e6 2021-07-24 op
314 98d3e6c1 2024-02-18 op if (req->fd != -1) {
315 98d3e6c1 2024-02-18 op if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error,
316 98d3e6c1 2024-02-18 op &len) == -1) {
317 98d3e6c1 2024-02-18 op req->conn_error = errno;
318 98d3e6c1 2024-02-18 op req->cause = "getsockopt";
319 98d3e6c1 2024-02-18 op return (-1);
320 98d3e6c1 2024-02-18 op }
321 c8dba1e6 2021-07-24 op
322 98d3e6c1 2024-02-18 op if (error == 0) /* connected */
323 98d3e6c1 2024-02-18 op return (0);
324 c8dba1e6 2021-07-24 op
325 98d3e6c1 2024-02-18 op req->conn_error = error;
326 98d3e6c1 2024-02-18 op req->cause = "connect";
327 98d3e6c1 2024-02-18 op close(req->fd);
328 98d3e6c1 2024-02-18 op req->fd = -1;
329 98d3e6c1 2024-02-18 op req->p = req->p->ai_next;
330 98d3e6c1 2024-02-18 op goto again;
331 5b762f01 2021-07-24 op }
332 5b762f01 2021-07-24 op
333 98d3e6c1 2024-02-18 op req->fd = socket(req->p->ai_family, req->p->ai_socktype,
334 98d3e6c1 2024-02-18 op req->p->ai_protocol);
335 98d3e6c1 2024-02-18 op if (req->fd == -1) {
336 98d3e6c1 2024-02-18 op req->conn_error = errno;
337 98d3e6c1 2024-02-18 op req->cause = "socket";
338 98d3e6c1 2024-02-18 op req->p = req->p->ai_next;
339 98d3e6c1 2024-02-18 op goto again;
340 5b762f01 2021-07-24 op }
341 c8dba1e6 2021-07-24 op
342 98d3e6c1 2024-02-18 op if (!mark_nonblock_cloexec(req->fd)) {
343 98d3e6c1 2024-02-18 op req->conn_error = errno;
344 98d3e6c1 2024-02-18 op req->cause = "setsockopt";
345 98d3e6c1 2024-02-18 op return (-1);
346 5b762f01 2021-07-24 op }
347 c8dba1e6 2021-07-24 op
348 98d3e6c1 2024-02-18 op if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
349 98d3e6c1 2024-02-18 op return (0);
350 98d3e6c1 2024-02-18 op errno = EAGAIN;
351 98d3e6c1 2024-02-18 op return (-1);
352 5b762f01 2021-07-24 op }
353 5b762f01 2021-07-24 op
354 5b762f01 2021-07-24 op static int
355 5f285272 2024-03-25 op gemini_parse_reply(struct req *req, const char *header)
356 5b762f01 2021-07-24 op {
357 54764e41 2024-02-02 op struct ibuf *ibuf;
358 5f285272 2024-03-25 op size_t len;
359 5b762f01 2021-07-24 op int code;
360 5b762f01 2021-07-24 op
361 5b762f01 2021-07-24 op if (!isdigit(header[0]) || !isdigit(header[1]))
362 1fa94674 2024-03-25 op return -1;
363 5b762f01 2021-07-24 op
364 5b762f01 2021-07-24 op code = (header[0] - '0')*10 + (header[1] - '0');
365 5b762f01 2021-07-24 op if (header[2] != ' ')
366 1fa94674 2024-03-25 op return -1;
367 5b762f01 2021-07-24 op
368 5f285272 2024-03-25 op header += 3;
369 5f285272 2024-03-25 op len = strlen(header) + 1;
370 5b762f01 2021-07-24 op
371 54764e41 2024-02-02 op if ((ibuf = imsg_create(&iev_ui->ibuf, IMSG_REPLY, req->id, 0,
372 54764e41 2024-02-02 op sizeof(code) + len)) == NULL)
373 54764e41 2024-02-02 op die();
374 54764e41 2024-02-02 op if (imsg_add(ibuf, &code, sizeof(code)) == -1 ||
375 5f285272 2024-03-25 op imsg_add(ibuf, header, len) == -1)
376 54764e41 2024-02-02 op die();
377 54764e41 2024-02-02 op imsg_close(&iev_ui->ibuf, ibuf);
378 54764e41 2024-02-02 op imsg_event_add(iev_ui);
379 0e49c9bb 2022-01-21 op return code;
380 5b762f01 2021-07-24 op }
381 c8dba1e6 2021-07-24 op
382 98d3e6c1 2024-02-18 op static inline int
383 98d3e6c1 2024-02-18 op net_send_req(struct req *req)
384 5b762f01 2021-07-24 op {
385 98d3e6c1 2024-02-18 op return (bufio_compose(&req->bio, req->req, req->len));
386 5b762f01 2021-07-24 op }
387 5b762f01 2021-07-24 op
388 5b762f01 2021-07-24 op static void
389 98d3e6c1 2024-02-18 op net_ev(int fd, int ev, void *d)
390 5b762f01 2021-07-24 op {
391 b07d2757 2024-01-31 op static char buf[4096];
392 5b762f01 2021-07-24 op struct req *req = d;
393 98d3e6c1 2024-02-18 op const char *hash;
394 98d3e6c1 2024-02-18 op ssize_t read;
395 b07d2757 2024-01-31 op size_t len;
396 f2a80e94 2024-03-22 op char *header;
397 140a1abd 2024-03-25 op int code;
398 5b762f01 2021-07-24 op
399 98d3e6c1 2024-02-18 op if (ev == EV_TIMEOUT) {
400 98d3e6c1 2024-02-18 op close_with_err(req, "Timeout loading page");
401 98d3e6c1 2024-02-18 op return;
402 98d3e6c1 2024-02-18 op }
403 98d3e6c1 2024-02-18 op
404 98d3e6c1 2024-02-18 op if (req->state == CONN_CONNECTING) {
405 98d3e6c1 2024-02-18 op ev_del(req->fd);
406 98d3e6c1 2024-02-18 op if (try_to_connect(req) == -1) {
407 98d3e6c1 2024-02-18 op if (req->fd != -1 && errno == EAGAIN) {
408 98d3e6c1 2024-02-18 op ev_add(req->fd, EV_WRITE, net_ev, req);
409 98d3e6c1 2024-02-18 op return;
410 98d3e6c1 2024-02-18 op }
411 98d3e6c1 2024-02-18 op close_with_errf(req, "failed to connect to %s"
412 98d3e6c1 2024-02-18 op " (%s: %s)", req->host, req->cause,
413 98d3e6c1 2024-02-18 op strerror(req->conn_error));
414 186a1eec 2024-01-31 op return;
415 186a1eec 2024-01-31 op }
416 98d3e6c1 2024-02-18 op
417 98d3e6c1 2024-02-18 op bufio_set_fd(&req->bio, req->fd);
418 98d3e6c1 2024-02-18 op
419 98d3e6c1 2024-02-18 op switch (req->proto) {
420 98d3e6c1 2024-02-18 op case PROTO_FINGER:
421 98d3e6c1 2024-02-18 op case PROTO_GOPHER:
422 98d3e6c1 2024-02-18 op /* finger and gopher don't have a header nor TLS */
423 98d3e6c1 2024-02-18 op req->state = CONN_BODY;
424 98d3e6c1 2024-02-18 op if (net_send_req(req) == -1) {
425 98d3e6c1 2024-02-18 op close_with_err(req, "failed to send request");
426 98d3e6c1 2024-02-18 op return;
427 98d3e6c1 2024-02-18 op }
428 98d3e6c1 2024-02-18 op break;
429 98d3e6c1 2024-02-18 op case PROTO_GEMINI:
430 98d3e6c1 2024-02-18 op req->state = CONN_HANDSHAKE;
431 98d3e6c1 2024-02-18 op if (bufio_starttls(&req->bio, req->host, 1,
432 98d3e6c1 2024-02-18 op req->ccert, req->ccert_len,
433 98d3e6c1 2024-02-18 op req->ccert, req->ccert_len) == -1) {
434 98d3e6c1 2024-02-18 op close_with_err(req, "failed to setup TLS");
435 98d3e6c1 2024-02-18 op return;
436 98d3e6c1 2024-02-18 op }
437 62d3cd29 2024-02-22 op req->timer = ev_timer(&timeout_for_handshake,
438 98d3e6c1 2024-02-18 op net_ev, req);
439 62d3cd29 2024-02-22 op if (req->timer == 0) {
440 98d3e6c1 2024-02-18 op close_with_err(req, "failed to setup"
441 98d3e6c1 2024-02-18 op " handshake timer");
442 98d3e6c1 2024-02-18 op return;
443 98d3e6c1 2024-02-18 op }
444 98d3e6c1 2024-02-18 op break;
445 98d3e6c1 2024-02-18 op }
446 98d3e6c1 2024-02-18 op }
447 98d3e6c1 2024-02-18 op
448 98d3e6c1 2024-02-18 op if (req->state == CONN_HANDSHAKE) {
449 98d3e6c1 2024-02-18 op if (bufio_handshake(&req->bio) == -1 && errno == EAGAIN) {
450 ce023433 2024-02-22 op ev_add(req->fd, req_bio_ev(req), net_ev, req);
451 5b762f01 2021-07-24 op return;
452 186a1eec 2024-01-31 op }
453 98d3e6c1 2024-02-18 op
454 62d3cd29 2024-02-22 op ev_timer_cancel(req->timer);
455 62d3cd29 2024-02-22 op req->timer = 0;
456 98d3e6c1 2024-02-18 op
457 98d3e6c1 2024-02-18 op req->state = CONN_HEADER;
458 98d3e6c1 2024-02-18 op
459 98d3e6c1 2024-02-18 op /* pause until we've told the certificate is OK */
460 98d3e6c1 2024-02-18 op ev_del(req->fd);
461 98d3e6c1 2024-02-18 op
462 98d3e6c1 2024-02-18 op hash = tls_peer_cert_hash(req->bio.ctx);
463 98d3e6c1 2024-02-18 op if (hash == NULL) {
464 98d3e6c1 2024-02-18 op close_with_errf(req, "handshake failed: %s",
465 98d3e6c1 2024-02-18 op tls_error(req->bio.ctx));
466 b07d2757 2024-01-31 op return;
467 b07d2757 2024-01-31 op }
468 0e8f22ed 2021-09-12 op
469 98d3e6c1 2024-02-18 op net_send_ui(IMSG_CHECK_CERT, req->id, hash, strlen(hash)+1);
470 98d3e6c1 2024-02-18 op return;
471 0e8f22ed 2021-09-12 op }
472 c8dba1e6 2021-07-24 op
473 98d3e6c1 2024-02-18 op if (ev & EV_READ) {
474 98d3e6c1 2024-02-18 op read = bufio_read(&req->bio);
475 98d3e6c1 2024-02-18 op if (read == -1 && errno != EAGAIN) {
476 98d3e6c1 2024-02-18 op close_with_errf(req, "Read error");
477 98d3e6c1 2024-02-18 op return;
478 98d3e6c1 2024-02-18 op }
479 98d3e6c1 2024-02-18 op if (read == 0)
480 98d3e6c1 2024-02-18 op req->eof = 1;
481 5b762f01 2021-07-24 op }
482 c8dba1e6 2021-07-24 op
483 98d3e6c1 2024-02-18 op if ((ev & EV_WRITE) && bufio_write(&req->bio) == -1 &&
484 98d3e6c1 2024-02-18 op errno != EAGAIN) {
485 98d3e6c1 2024-02-18 op close_with_errf(req, "bufio_write: %s", strerror(errno));
486 5b762f01 2021-07-24 op return;
487 c8dba1e6 2021-07-24 op }
488 5b762f01 2021-07-24 op
489 98d3e6c1 2024-02-18 op if (req->state == CONN_HEADER) {
490 f2a80e94 2024-03-22 op header = buf_getdelim(&req->bio.rbuf, "\r\n", &len);
491 f2a80e94 2024-03-22 op if (header == NULL && req->bio.rbuf.len >= 1024) {
492 98d3e6c1 2024-02-18 op close_with_err(req, "Invalid gemini reply (too long)");
493 98cba69e 2021-11-10 op return;
494 98cba69e 2021-11-10 op }
495 f2a80e94 2024-03-22 op if (header == NULL && req->eof) {
496 98d3e6c1 2024-02-18 op close_with_err(req, "Invalid gemini reply.");
497 98d3e6c1 2024-02-18 op return;
498 98d3e6c1 2024-02-18 op }
499 f2a80e94 2024-03-22 op if (header == NULL) {
500 ce023433 2024-02-22 op ev_add(req->fd, req_bio_ev(req), net_ev, req);
501 98d3e6c1 2024-02-18 op return;
502 98d3e6c1 2024-02-18 op }
503 98d3e6c1 2024-02-18 op req->state = CONN_BODY;
504 140a1abd 2024-03-25 op if ((code = gemini_parse_reply(req, header)) == -1) {
505 98d3e6c1 2024-02-18 op close_with_err(req, "Malformed gemini reply");
506 98d3e6c1 2024-02-18 op return;
507 98d3e6c1 2024-02-18 op }
508 140a1abd 2024-03-25 op if (code < 20 || code >= 30) {
509 98d3e6c1 2024-02-18 op close_conn(0, 0, req);
510 81df72e1 2024-03-25 op return;
511 81df72e1 2024-03-25 op }
512 81df72e1 2024-03-25 op
513 140a1abd 2024-03-25 op buf_drain(&req->bio.rbuf, len);
514 140a1abd 2024-03-25 op
515 9659786c 2024-03-25 op /* pause until we've been told to go ahead */
516 81df72e1 2024-03-25 op ev_del(req->fd);
517 5b762f01 2021-07-24 op return;
518 5b762f01 2021-07-24 op }
519 98d3e6c1 2024-02-18 op
520 98d3e6c1 2024-02-18 op /*
521 98d3e6c1 2024-02-18 op * Split data into chunks before sending. imsg can't handle
522 98d3e6c1 2024-02-18 op * message that are "too big".
523 98d3e6c1 2024-02-18 op */
524 98d3e6c1 2024-02-18 op for (;;) {
525 98d3e6c1 2024-02-18 op if ((len = bufio_drain(&req->bio, buf, sizeof(buf))) == 0)
526 98d3e6c1 2024-02-18 op break;
527 98d3e6c1 2024-02-18 op net_send_ui(IMSG_BUF, req->id, buf, len);
528 5b762f01 2021-07-24 op }
529 5b762f01 2021-07-24 op
530 98d3e6c1 2024-02-18 op if (req->eof) {
531 98d3e6c1 2024-02-18 op net_send_ui(IMSG_EOF, req->id, NULL, 0);
532 98d3e6c1 2024-02-18 op close_conn(0, 0, req);
533 cc300b25 2024-02-18 op return;
534 5b762f01 2021-07-24 op }
535 5b762f01 2021-07-24 op
536 ce023433 2024-02-22 op ev_add(req->fd, req_bio_ev(req), net_ev, req);
537 d35e18b3 2024-02-04 op }
538 d35e18b3 2024-02-04 op
539 d35e18b3 2024-02-04 op static int
540 d35e18b3 2024-02-04 op load_cert(struct imsg *imsg, struct req *req)
541 d35e18b3 2024-02-04 op {
542 d35e18b3 2024-02-04 op struct stat sb;
543 d35e18b3 2024-02-04 op int fd;
544 d35e18b3 2024-02-04 op
545 d35e18b3 2024-02-04 op if ((fd = imsg_get_fd(imsg)) == -1)
546 d35e18b3 2024-02-04 op return (0);
547 d35e18b3 2024-02-04 op
548 d35e18b3 2024-02-04 op if (fstat(fd, &sb) == -1)
549 d35e18b3 2024-02-04 op return (-1);
550 d35e18b3 2024-02-04 op
551 d35e18b3 2024-02-04 op #if 0
552 d35e18b3 2024-02-04 op if (sb.st_size >= (off_t)SIZE_MAX) {
553 d35e18b3 2024-02-04 op close(fd);
554 d35e18b3 2024-02-04 op return (-1);
555 d35e18b3 2024-02-04 op }
556 d35e18b3 2024-02-04 op #endif
557 d35e18b3 2024-02-04 op
558 d35e18b3 2024-02-04 op req->ccert = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
559 d35e18b3 2024-02-04 op if (req->ccert == MAP_FAILED) {
560 d35e18b3 2024-02-04 op req->ccert = NULL;
561 d35e18b3 2024-02-04 op close(fd);
562 d35e18b3 2024-02-04 op return (-1);
563 d35e18b3 2024-02-04 op }
564 d35e18b3 2024-02-04 op
565 d35e18b3 2024-02-04 op req->ccert_len = sb.st_size;
566 d35e18b3 2024-02-04 op req->ccert_fd = fd;
567 d35e18b3 2024-02-04 op
568 d35e18b3 2024-02-04 op return (0);
569 c8dba1e6 2021-07-24 op }
570 c8dba1e6 2021-07-24 op
571 c8dba1e6 2021-07-24 op static void
572 98d3e6c1 2024-02-18 op handle_dispatch_imsg(int fd, int event, void *d)
573 c8dba1e6 2021-07-24 op {
574 0640b555 2022-05-27 op struct imsgev *iev = d;
575 0640b555 2022-05-27 op struct imsgbuf *ibuf = &iev->ibuf;
576 0640b555 2022-05-27 op struct imsg imsg;
577 c8dba1e6 2021-07-24 op struct req *req;
578 0388b56b 2024-01-31 op struct get_req r;
579 0640b555 2022-05-27 op ssize_t n;
580 0640b555 2022-05-27 op int certok;
581 c8dba1e6 2021-07-24 op
582 0640b555 2022-05-27 op if (event & EV_READ) {
583 0640b555 2022-05-27 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
584 0640b555 2022-05-27 op err(1, "imsg_read");
585 0640b555 2022-05-27 op if (n == 0)
586 0640b555 2022-05-27 op err(1, "connection closed");
587 0640b555 2022-05-27 op }
588 0640b555 2022-05-27 op if (event & EV_WRITE) {
589 0640b555 2022-05-27 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
590 0640b555 2022-05-27 op err(1, "msgbuf_write");
591 0640b555 2022-05-27 op if (n == 0)
592 0640b555 2022-05-27 op err(1, "connection closed");
593 0640b555 2022-05-27 op }
594 c8dba1e6 2021-07-24 op
595 0640b555 2022-05-27 op for (;;) {
596 0640b555 2022-05-27 op if ((n = imsg_get(ibuf, &imsg)) == -1)
597 0640b555 2022-05-27 op err(1, "imsg_get");
598 0640b555 2022-05-27 op if (n == 0)
599 0640b555 2022-05-27 op break;
600 0388b56b 2024-01-31 op switch (imsg_get_type(&imsg)) {
601 c6e37ffd 2022-05-27 op case IMSG_GET:
602 0388b56b 2024-01-31 op if (imsg_get_data(&imsg, &r, sizeof(r)) == -1 ||
603 0388b56b 2024-01-31 op r.host[sizeof(r.host) - 1] != '\0' ||
604 0388b56b 2024-01-31 op r.port[sizeof(r.port) - 1] != '\0' ||
605 0388b56b 2024-01-31 op r.req[sizeof(r.req) - 1] != '\0')
606 0640b555 2022-05-27 op die();
607 0388b56b 2024-01-31 op if (r.proto != PROTO_FINGER &&
608 0388b56b 2024-01-31 op r.proto != PROTO_GEMINI &&
609 0388b56b 2024-01-31 op r.proto != PROTO_GOPHER)
610 0640b555 2022-05-27 op die();
611 c8dba1e6 2021-07-24 op
612 0640b555 2022-05-27 op if ((req = calloc(1, sizeof(*req))) == NULL)
613 0640b555 2022-05-27 op die();
614 5b762f01 2021-07-24 op
615 51ea137b 2024-02-22 op req->fd = -1;
616 51ea137b 2024-02-22 op #if HAVE_ASR_RUN
617 51ea137b 2024-02-22 op req->ar_fd = -1;
618 51ea137b 2024-02-22 op #endif
619 d35e18b3 2024-02-04 op req->ccert_fd = -1;
620 0388b56b 2024-01-31 op req->id = imsg_get_id(&imsg);
621 0640b555 2022-05-27 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
622 5fb52fc0 2021-07-25 op
623 0388b56b 2024-01-31 op if ((req->host = strdup(r.host)) == NULL)
624 5657662f 2024-01-16 op die();
625 0388b56b 2024-01-31 op if ((req->port = strdup(r.port)) == NULL)
626 5657662f 2024-01-16 op die();
627 0388b56b 2024-01-31 op if ((req->req = strdup(r.req)) == NULL)
628 5657662f 2024-01-16 op die();
629 d35e18b3 2024-02-04 op if (load_cert(&imsg, req) == -1)
630 d35e18b3 2024-02-04 op die();
631 98d3e6c1 2024-02-18 op if (bufio_init(&req->bio) == -1)
632 98d3e6c1 2024-02-18 op die();
633 5657662f 2024-01-16 op
634 5657662f 2024-01-16 op req->len = strlen(req->req);
635 0388b56b 2024-01-31 op req->proto = r.proto;
636 10884685 2024-02-22 op req_resolve(-1, 0, req);
637 0640b555 2022-05-27 op break;
638 0640b555 2022-05-27 op
639 0640b555 2022-05-27 op case IMSG_CERT_STATUS:
640 0388b56b 2024-01-31 op if ((req = req_by_id(imsg_get_id(&imsg))) == NULL)
641 0640b555 2022-05-27 op break;
642 0640b555 2022-05-27 op
643 c24b14ad 2024-01-22 op if (imsg_get_data(&imsg, &certok, sizeof(certok)) ==
644 c24b14ad 2024-01-22 op -1)
645 0640b555 2022-05-27 op die();
646 98d3e6c1 2024-02-18 op if (!certok) {
647 0640b555 2022-05-27 op close_conn(0, 0, req);
648 98d3e6c1 2024-02-18 op break;
649 98d3e6c1 2024-02-18 op }
650 98d3e6c1 2024-02-18 op
651 98d3e6c1 2024-02-18 op if (net_send_req(req) == -1) {
652 98d3e6c1 2024-02-18 op close_with_err(req, "failed to send request");
653 98d3e6c1 2024-02-18 op break;
654 98d3e6c1 2024-02-18 op }
655 98d3e6c1 2024-02-18 op
656 98d3e6c1 2024-02-18 op if (ev_add(req->fd, EV_WRITE, net_ev, req) == -1) {
657 98d3e6c1 2024-02-18 op close_with_err(req,
658 98d3e6c1 2024-02-18 op "failed to register event.");
659 98d3e6c1 2024-02-18 op break;
660 98d3e6c1 2024-02-18 op }
661 0640b555 2022-05-27 op break;
662 c8dba1e6 2021-07-24 op
663 0640b555 2022-05-27 op case IMSG_PROCEED:
664 0388b56b 2024-01-31 op if ((req = req_by_id(imsg_get_id(&imsg))) == NULL)
665 0640b555 2022-05-27 op break;
666 98d3e6c1 2024-02-18 op ev_add(req->fd, EV_READ, net_ev, req);
667 98d3e6c1 2024-02-18 op net_ev(req->fd, 0, req);
668 0640b555 2022-05-27 op break;
669 c8dba1e6 2021-07-24 op
670 0640b555 2022-05-27 op case IMSG_STOP:
671 0388b56b 2024-01-31 op if ((req = req_by_id(imsg_get_id(&imsg))) == NULL)
672 0640b555 2022-05-27 op break;
673 0640b555 2022-05-27 op close_conn(0, 0, req);
674 0640b555 2022-05-27 op break;
675 c8dba1e6 2021-07-24 op
676 0640b555 2022-05-27 op case IMSG_QUIT:
677 98d3e6c1 2024-02-18 op ev_break();
678 0640b555 2022-05-27 op imsg_free(&imsg);
679 0640b555 2022-05-27 op return;
680 c8dba1e6 2021-07-24 op
681 0640b555 2022-05-27 op default:
682 0388b56b 2024-01-31 op errx(1, "got unknown imsg %d", imsg_get_type(&imsg));
683 0640b555 2022-05-27 op }
684 c8dba1e6 2021-07-24 op
685 0640b555 2022-05-27 op imsg_free(&imsg);
686 0640b555 2022-05-27 op }
687 5b762f01 2021-07-24 op
688 0640b555 2022-05-27 op imsg_event_add(iev);
689 c8dba1e6 2021-07-24 op }
690 c8dba1e6 2021-07-24 op
691 c8dba1e6 2021-07-24 op static int
692 c8dba1e6 2021-07-24 op net_send_ui(int type, uint32_t peerid, const void *data,
693 c8dba1e6 2021-07-24 op uint16_t datalen)
694 c8dba1e6 2021-07-24 op {
695 c8dba1e6 2021-07-24 op return imsg_compose_event(iev_ui, type, peerid, 0, -1,
696 c8dba1e6 2021-07-24 op data, datalen);
697 c8dba1e6 2021-07-24 op }
698 c8dba1e6 2021-07-24 op
699 c8dba1e6 2021-07-24 op int
700 f45bd2e3 2021-07-24 op net_main(void)
701 c8dba1e6 2021-07-24 op {
702 c8dba1e6 2021-07-24 op setproctitle("net");
703 c8dba1e6 2021-07-24 op
704 c8dba1e6 2021-07-24 op TAILQ_INIT(&reqhead);
705 c8dba1e6 2021-07-24 op
706 98d3e6c1 2024-02-18 op if (ev_init() == -1)
707 98d3e6c1 2024-02-18 op exit(1);
708 c8dba1e6 2021-07-24 op
709 c8dba1e6 2021-07-24 op /* Setup pipe and event handler to the main process */
710 c8dba1e6 2021-07-24 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
711 c8dba1e6 2021-07-24 op die();
712 c8dba1e6 2021-07-24 op imsg_init(&iev_ui->ibuf, 3);
713 c8dba1e6 2021-07-24 op iev_ui->handler = handle_dispatch_imsg;
714 c8dba1e6 2021-07-24 op iev_ui->events = EV_READ;
715 98d3e6c1 2024-02-18 op ev_add(iev_ui->ibuf.fd, iev_ui->events, iev_ui->handler, iev_ui);
716 c8dba1e6 2021-07-24 op
717 c8dba1e6 2021-07-24 op sandbox_net_process();
718 c8dba1e6 2021-07-24 op
719 98d3e6c1 2024-02-18 op ev_loop();
720 74c0f6ba 2021-07-24 op
721 74c0f6ba 2021-07-24 op msgbuf_clear(&iev_ui->ibuf.w);
722 74c0f6ba 2021-07-24 op close(iev_ui->ibuf.fd);
723 74c0f6ba 2021-07-24 op free(iev_ui);
724 74c0f6ba 2021-07-24 op
725 c8dba1e6 2021-07-24 op return 0;
726 c8dba1e6 2021-07-24 op }