Blame


1 5e11c00c 2021-03-02 op /*
2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 5e11c00c 2021-03-02 op *
4 5e11c00c 2021-03-02 op * Permission to use, copy, modify, and distribute this software for any
5 5e11c00c 2021-03-02 op * purpose with or without fee is hereby granted, provided that the above
6 5e11c00c 2021-03-02 op * copyright notice and this permission notice appear in all copies.
7 5e11c00c 2021-03-02 op *
8 5e11c00c 2021-03-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5e11c00c 2021-03-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5e11c00c 2021-03-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5e11c00c 2021-03-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5e11c00c 2021-03-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5e11c00c 2021-03-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5e11c00c 2021-03-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 5e11c00c 2021-03-02 op */
16 5e11c00c 2021-03-02 op
17 5e11c00c 2021-03-02 op /*
18 5e11c00c 2021-03-02 op * TODO:
19 5e11c00c 2021-03-02 op * - move the various
20 5e11c00c 2021-03-02 op * imsg_compose(...);
21 5e11c00c 2021-03-02 op * imsg_flush(...);
22 5e11c00c 2021-03-02 op * to something more asynchronous
23 5e11c00c 2021-03-02 op */
24 5e11c00c 2021-03-02 op
25 5e11c00c 2021-03-02 op #include <telescope.h>
26 5e11c00c 2021-03-02 op
27 5e11c00c 2021-03-02 op #include <sys/types.h>
28 5e11c00c 2021-03-02 op #include <sys/socket.h>
29 5e11c00c 2021-03-02 op
30 5e11c00c 2021-03-02 op #include <netinet/in.h>
31 5e11c00c 2021-03-02 op
32 5e11c00c 2021-03-02 op #include <assert.h>
33 5e11c00c 2021-03-02 op #include <ctype.h>
34 5e11c00c 2021-03-02 op #include <errno.h>
35 5e11c00c 2021-03-02 op #include <netdb.h>
36 5e11c00c 2021-03-02 op #include <stdarg.h>
37 5e11c00c 2021-03-02 op #include <stdio.h>
38 5e11c00c 2021-03-02 op #include <stdlib.h>
39 5e11c00c 2021-03-02 op #include <string.h>
40 5e11c00c 2021-03-02 op #include <tls.h>
41 5e11c00c 2021-03-02 op #include <unistd.h>
42 5e11c00c 2021-03-02 op
43 cc4953ef 2021-03-16 op #if HAVE_ASR_RUN
44 cc4953ef 2021-03-16 op # include <asr.h>
45 cc4953ef 2021-03-16 op #endif
46 cc4953ef 2021-03-16 op
47 35e1f40a 2021-03-14 op static struct event imsgev;
48 5e11c00c 2021-03-02 op static struct tls_config *tlsconf;
49 5e11c00c 2021-03-02 op static struct imsgbuf *ibuf;
50 5e11c00c 2021-03-02 op
51 5e11c00c 2021-03-02 op struct req;
52 5e11c00c 2021-03-02 op
53 5e11c00c 2021-03-02 op static void die(void) __attribute__((__noreturn__));
54 cc4953ef 2021-03-16 op
55 cc4953ef 2021-03-16 op #if HAVE_ASR_RUN
56 cc4953ef 2021-03-16 op static void try_to_connect(int, short, void*);
57 cc4953ef 2021-03-16 op static void query_done(struct asr_result*, void*);
58 cc4953ef 2021-03-16 op static void async_conn_towards(struct req*);
59 cc4953ef 2021-03-16 op #else
60 5e11c00c 2021-03-02 op static char *xasprintf(const char*, ...);
61 cc4953ef 2021-03-16 op static int blocking_conn_towards(struct url*, char**);
62 cc4953ef 2021-03-16 op #endif
63 5e11c00c 2021-03-02 op
64 a237223e 2021-03-16 op static void close_with_err(struct req*, const char*);
65 a237223e 2021-03-16 op static void close_with_errf(struct req*, const char*, ...) __attribute__((format(printf, 2, 3)));
66 5e11c00c 2021-03-02 op static struct req *req_by_id(uint32_t);
67 15354eed 2021-03-10 op static struct req *req_by_id_try(uint32_t);
68 5e11c00c 2021-03-02 op
69 cc4953ef 2021-03-16 op static void setup_tls(struct req*);
70 5e11c00c 2021-03-02 op static void do_handshake(int, short, void*);
71 5e11c00c 2021-03-02 op static void write_request(int, short, void*);
72 5e11c00c 2021-03-02 op static void read_reply(int, short, void*);
73 5e11c00c 2021-03-02 op static void parse_reply(struct req*);
74 5e11c00c 2021-03-02 op static void copy_body(int, short, void*);
75 5e11c00c 2021-03-02 op
76 5e11c00c 2021-03-02 op static void handle_get(struct imsg*, size_t);
77 5e11c00c 2021-03-02 op static void handle_cert_status(struct imsg*, size_t);
78 0972d8b2 2021-03-02 op static void handle_proceed(struct imsg*, size_t);
79 5e11c00c 2021-03-02 op static void handle_stop(struct imsg*, size_t);
80 5e11c00c 2021-03-02 op static void handle_quit(struct imsg*, size_t);
81 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
82 5e11c00c 2021-03-02 op
83 723bee54 2021-03-09 op /* TODO: making this customizable */
84 723bee54 2021-03-09 op struct timeval timeout_for_handshake = { 5, 0 };
85 723bee54 2021-03-09 op
86 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
87 5e11c00c 2021-03-02 op [IMSG_GET] = handle_get,
88 5e11c00c 2021-03-02 op [IMSG_CERT_STATUS] = handle_cert_status,
89 0972d8b2 2021-03-02 op [IMSG_PROCEED] = handle_proceed,
90 5e11c00c 2021-03-02 op [IMSG_STOP] = handle_stop,
91 5e11c00c 2021-03-02 op [IMSG_QUIT] = handle_quit,
92 5e11c00c 2021-03-02 op };
93 5e11c00c 2021-03-02 op
94 5e11c00c 2021-03-02 op typedef void (*statefn)(int, short, void*);
95 5e11c00c 2021-03-02 op
96 5e11c00c 2021-03-02 op TAILQ_HEAD(, req) reqhead;
97 5e11c00c 2021-03-02 op /* a pending request */
98 5e11c00c 2021-03-02 op struct req {
99 5e11c00c 2021-03-02 op struct event ev;
100 5e11c00c 2021-03-02 op struct url url;
101 5e11c00c 2021-03-02 op uint32_t id;
102 5e11c00c 2021-03-02 op int fd;
103 5e11c00c 2021-03-02 op struct tls *ctx;
104 5e11c00c 2021-03-02 op char buf[1024];
105 5e11c00c 2021-03-02 op size_t off;
106 cc4953ef 2021-03-16 op
107 cc4953ef 2021-03-16 op #if HAVE_ASR_RUN
108 cc4953ef 2021-03-16 op struct addrinfo hints, *servinfo, *p;
109 cc4953ef 2021-03-16 op struct event_asr *asrev;
110 cc4953ef 2021-03-16 op #endif
111 cc4953ef 2021-03-16 op
112 5e11c00c 2021-03-02 op TAILQ_ENTRY(req) reqs;
113 5e11c00c 2021-03-02 op };
114 5e11c00c 2021-03-02 op
115 5e11c00c 2021-03-02 op static inline void
116 5e11c00c 2021-03-02 op yield_r(struct req *req, statefn fn, struct timeval *tv)
117 5e11c00c 2021-03-02 op {
118 5e11c00c 2021-03-02 op event_once(req->fd, EV_READ, fn, req, tv);
119 5e11c00c 2021-03-02 op }
120 5e11c00c 2021-03-02 op
121 5e11c00c 2021-03-02 op static inline void
122 5e11c00c 2021-03-02 op yield_w(struct req *req, statefn fn, struct timeval *tv)
123 5e11c00c 2021-03-02 op {
124 5e11c00c 2021-03-02 op event_once(req->fd, EV_WRITE, fn, req, tv);
125 5e11c00c 2021-03-02 op }
126 5e11c00c 2021-03-02 op
127 5e11c00c 2021-03-02 op static inline void
128 5e11c00c 2021-03-02 op advance_buf(struct req *req, size_t len)
129 5e11c00c 2021-03-02 op {
130 5e11c00c 2021-03-02 op assert(len <= req->off);
131 5e11c00c 2021-03-02 op
132 5e11c00c 2021-03-02 op req->off -= len;
133 5e11c00c 2021-03-02 op memmove(req->buf, req->buf + len, req->off);
134 5e11c00c 2021-03-02 op }
135 5e11c00c 2021-03-02 op
136 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
137 5e11c00c 2021-03-02 op die(void)
138 5e11c00c 2021-03-02 op {
139 5e11c00c 2021-03-02 op abort(); /* TODO */
140 cc4953ef 2021-03-16 op }
141 cc4953ef 2021-03-16 op
142 cc4953ef 2021-03-16 op #if HAVE_ASR_RUN
143 cc4953ef 2021-03-16 op static void
144 cc4953ef 2021-03-16 op try_to_connect(int fd, short ev, void *d)
145 cc4953ef 2021-03-16 op {
146 cc4953ef 2021-03-16 op struct req *req = d;
147 cc4953ef 2021-03-16 op int error = 0;
148 cc4953ef 2021-03-16 op socklen_t len = sizeof(error);
149 cc4953ef 2021-03-16 op
150 cc4953ef 2021-03-16 op if (req->p == NULL)
151 cc4953ef 2021-03-16 op goto err;
152 cc4953ef 2021-03-16 op
153 cc4953ef 2021-03-16 op if (req->fd != -1) {
154 cc4953ef 2021-03-16 op if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error, &len) == -1)
155 cc4953ef 2021-03-16 op goto err;
156 cc4953ef 2021-03-16 op if (error != 0) {
157 cc4953ef 2021-03-16 op errno = error;
158 cc4953ef 2021-03-16 op goto err;
159 cc4953ef 2021-03-16 op }
160 cc4953ef 2021-03-16 op goto done;
161 cc4953ef 2021-03-16 op }
162 cc4953ef 2021-03-16 op
163 cc4953ef 2021-03-16 op req->fd = socket(req->p->ai_family, req->p->ai_socktype, req->p->ai_protocol);
164 20d2d786 2021-03-16 op if (req->fd == -1) {
165 cc4953ef 2021-03-16 op req->p = req->p->ai_next;
166 20d2d786 2021-03-16 op try_to_connect(fd, ev, req);
167 20d2d786 2021-03-16 op } else {
168 cc4953ef 2021-03-16 op mark_nonblock(req->fd);
169 cc4953ef 2021-03-16 op if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
170 cc4953ef 2021-03-16 op goto done;
171 20d2d786 2021-03-16 op yield_w(req, try_to_connect, NULL);
172 cc4953ef 2021-03-16 op }
173 cc4953ef 2021-03-16 op return;
174 cc4953ef 2021-03-16 op
175 cc4953ef 2021-03-16 op err:
176 cc4953ef 2021-03-16 op freeaddrinfo(req->servinfo);
177 cc4953ef 2021-03-16 op close_with_errf(req, "failed to connect to %s",
178 cc4953ef 2021-03-16 op req->url.host);
179 cc4953ef 2021-03-16 op return;
180 cc4953ef 2021-03-16 op
181 cc4953ef 2021-03-16 op done:
182 cc4953ef 2021-03-16 op freeaddrinfo(req->servinfo);
183 cc4953ef 2021-03-16 op setup_tls(req);
184 cc4953ef 2021-03-16 op }
185 cc4953ef 2021-03-16 op
186 cc4953ef 2021-03-16 op static void
187 cc4953ef 2021-03-16 op query_done(struct asr_result *res, void *d)
188 cc4953ef 2021-03-16 op {
189 cc4953ef 2021-03-16 op struct req *req = d;
190 cc4953ef 2021-03-16 op
191 cc4953ef 2021-03-16 op req->asrev = NULL;
192 cc4953ef 2021-03-16 op if (res->ar_gai_errno != 0) {
193 cc4953ef 2021-03-16 op close_with_errf(req, "failed to resolve %s: %s",
194 cc4953ef 2021-03-16 op req->url.host, gai_strerror(res->ar_gai_errno));
195 cc4953ef 2021-03-16 op return;
196 cc4953ef 2021-03-16 op }
197 cc4953ef 2021-03-16 op
198 cc4953ef 2021-03-16 op req->fd = -1;
199 cc4953ef 2021-03-16 op req->servinfo = res->ar_addrinfo;
200 cc4953ef 2021-03-16 op req->p = res->ar_addrinfo;
201 cc4953ef 2021-03-16 op try_to_connect(0, 0, req);
202 5e11c00c 2021-03-02 op }
203 5e11c00c 2021-03-02 op
204 cc4953ef 2021-03-16 op static void
205 cc4953ef 2021-03-16 op async_conn_towards(struct req *req)
206 cc4953ef 2021-03-16 op {
207 cc4953ef 2021-03-16 op struct asr_query *q;
208 cc4953ef 2021-03-16 op const char *proto = "1965";
209 cc4953ef 2021-03-16 op
210 cc4953ef 2021-03-16 op if (*req->url.port != '\0')
211 cc4953ef 2021-03-16 op proto = req->url.port;
212 cc4953ef 2021-03-16 op
213 cc4953ef 2021-03-16 op req->hints.ai_family = AF_UNSPEC;
214 cc4953ef 2021-03-16 op req->hints.ai_socktype = SOCK_STREAM;
215 cc4953ef 2021-03-16 op q = getaddrinfo_async(req->url.host, proto, &req->hints, NULL);
216 cc4953ef 2021-03-16 op req->asrev = event_asr_run(q, query_done, req);
217 cc4953ef 2021-03-16 op }
218 cc4953ef 2021-03-16 op #else
219 5e11c00c 2021-03-02 op static char *
220 5e11c00c 2021-03-02 op xasprintf(const char *fmt, ...)
221 5e11c00c 2021-03-02 op {
222 5e11c00c 2021-03-02 op va_list ap;
223 5e11c00c 2021-03-02 op char *s;
224 5e11c00c 2021-03-02 op
225 5e11c00c 2021-03-02 op va_start(ap, fmt);
226 5e11c00c 2021-03-02 op if (vasprintf(&s, fmt, ap) == -1)
227 5e11c00c 2021-03-02 op s = NULL;
228 5e11c00c 2021-03-02 op va_end(ap);
229 5e11c00c 2021-03-02 op
230 5e11c00c 2021-03-02 op return s;
231 5e11c00c 2021-03-02 op }
232 5e11c00c 2021-03-02 op
233 5e11c00c 2021-03-02 op static int
234 cc4953ef 2021-03-16 op blocking_conn_towards(struct url *url, char **err)
235 5e11c00c 2021-03-02 op {
236 5e11c00c 2021-03-02 op struct addrinfo hints, *servinfo, *p;
237 5e11c00c 2021-03-02 op int status, sock;
238 5e11c00c 2021-03-02 op const char *proto = "1965";
239 5e11c00c 2021-03-02 op
240 5e11c00c 2021-03-02 op *err = NULL;
241 5e11c00c 2021-03-02 op
242 5e11c00c 2021-03-02 op if (*url->port != '\0')
243 5e11c00c 2021-03-02 op proto = url->port;
244 5e11c00c 2021-03-02 op
245 5e11c00c 2021-03-02 op memset(&hints, 0, sizeof(hints));
246 5e11c00c 2021-03-02 op hints.ai_family = AF_UNSPEC;
247 5e11c00c 2021-03-02 op hints.ai_socktype = SOCK_STREAM;
248 5e11c00c 2021-03-02 op
249 5e11c00c 2021-03-02 op if ((status = getaddrinfo(url->host, proto, &hints, &servinfo))) {
250 5e11c00c 2021-03-02 op *err = xasprintf("failed to resolve %s: %s",
251 5e11c00c 2021-03-02 op url->host, gai_strerror(status));
252 5e11c00c 2021-03-02 op return -1;
253 5e11c00c 2021-03-02 op }
254 5e11c00c 2021-03-02 op
255 5e11c00c 2021-03-02 op sock = -1;
256 5e11c00c 2021-03-02 op for (p = servinfo; p != NULL; p = p->ai_next) {
257 5e11c00c 2021-03-02 op if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
258 5e11c00c 2021-03-02 op continue;
259 5e11c00c 2021-03-02 op if (connect(sock, p->ai_addr, p->ai_addrlen) != -1)
260 5e11c00c 2021-03-02 op break;
261 5e11c00c 2021-03-02 op close(sock);
262 5e11c00c 2021-03-02 op }
263 5e11c00c 2021-03-02 op
264 5e11c00c 2021-03-02 op if (sock == -1)
265 5e11c00c 2021-03-02 op *err = xasprintf("couldn't connect to %s", url->host);
266 5e11c00c 2021-03-02 op else
267 5e11c00c 2021-03-02 op mark_nonblock(sock);
268 5e11c00c 2021-03-02 op
269 5e11c00c 2021-03-02 op freeaddrinfo(servinfo);
270 5e11c00c 2021-03-02 op return sock;
271 5e11c00c 2021-03-02 op }
272 cc4953ef 2021-03-16 op #endif
273 5e11c00c 2021-03-02 op
274 5e11c00c 2021-03-02 op static struct req *
275 5e11c00c 2021-03-02 op req_by_id(uint32_t id)
276 15354eed 2021-03-10 op {
277 15354eed 2021-03-10 op struct req *r;
278 15354eed 2021-03-10 op
279 15354eed 2021-03-10 op if ((r = req_by_id_try(id)) == NULL)
280 15354eed 2021-03-10 op die();
281 15354eed 2021-03-10 op return r;
282 15354eed 2021-03-10 op }
283 15354eed 2021-03-10 op
284 15354eed 2021-03-10 op static struct req *
285 15354eed 2021-03-10 op req_by_id_try(uint32_t id)
286 5e11c00c 2021-03-02 op {
287 5e11c00c 2021-03-02 op struct req *r;
288 5e11c00c 2021-03-02 op
289 5e11c00c 2021-03-02 op TAILQ_FOREACH(r, &reqhead, reqs) {
290 5e11c00c 2021-03-02 op if (r->id == id)
291 5e11c00c 2021-03-02 op return r;
292 5e11c00c 2021-03-02 op }
293 5e11c00c 2021-03-02 op
294 15354eed 2021-03-10 op return NULL;
295 5e11c00c 2021-03-02 op }
296 5e11c00c 2021-03-02 op
297 5e11c00c 2021-03-02 op static void
298 5e11c00c 2021-03-02 op close_conn(int fd, short ev, void *d)
299 5e11c00c 2021-03-02 op {
300 5e11c00c 2021-03-02 op struct req *req = d;
301 5e11c00c 2021-03-02 op
302 cc4953ef 2021-03-16 op #if HAVE_ASR_RUN
303 cc4953ef 2021-03-16 op if (req->asrev != NULL)
304 cc4953ef 2021-03-16 op event_asr_abort(req->asrev);
305 cc4953ef 2021-03-16 op #endif
306 cc4953ef 2021-03-16 op
307 5e11c00c 2021-03-02 op if (req->ctx != NULL) {
308 5e11c00c 2021-03-02 op switch (tls_close(req->ctx)) {
309 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
310 5e11c00c 2021-03-02 op yield_r(req, close_conn, NULL);
311 5e11c00c 2021-03-02 op return;
312 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
313 5e11c00c 2021-03-02 op yield_w(req, close_conn, NULL);
314 5e11c00c 2021-03-02 op return;
315 5e11c00c 2021-03-02 op }
316 5e11c00c 2021-03-02 op
317 5e11c00c 2021-03-02 op tls_free(req->ctx);
318 5e11c00c 2021-03-02 op }
319 5e11c00c 2021-03-02 op
320 5e11c00c 2021-03-02 op TAILQ_REMOVE(&reqhead, req, reqs);
321 5e11c00c 2021-03-02 op if (req->fd != -1)
322 5e11c00c 2021-03-02 op close(req->fd);
323 5e11c00c 2021-03-02 op free(req);
324 5e11c00c 2021-03-02 op }
325 5e11c00c 2021-03-02 op
326 5e11c00c 2021-03-02 op static void
327 5e11c00c 2021-03-02 op close_with_err(struct req *req, const char *err)
328 5e11c00c 2021-03-02 op {
329 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_ERR, req->id, 0, -1, err, strlen(err)+1);
330 5e11c00c 2021-03-02 op imsg_flush(ibuf);
331 5e11c00c 2021-03-02 op close_conn(0, 0, req);
332 5e11c00c 2021-03-02 op }
333 5e11c00c 2021-03-02 op
334 5e11c00c 2021-03-02 op static void
335 a237223e 2021-03-16 op close_with_errf(struct req *req, const char *fmt, ...)
336 a237223e 2021-03-16 op {
337 a237223e 2021-03-16 op va_list ap;
338 a237223e 2021-03-16 op char *s;
339 a237223e 2021-03-16 op
340 a237223e 2021-03-16 op va_start(ap, fmt);
341 a237223e 2021-03-16 op if (vasprintf(&s, fmt, ap) == -1)
342 a237223e 2021-03-16 op abort();
343 a237223e 2021-03-16 op va_end(ap);
344 a237223e 2021-03-16 op
345 a237223e 2021-03-16 op close_with_err(req, s);
346 a237223e 2021-03-16 op free(s);
347 a237223e 2021-03-16 op }
348 a237223e 2021-03-16 op
349 a237223e 2021-03-16 op static void
350 cc4953ef 2021-03-16 op setup_tls(struct req *req)
351 cc4953ef 2021-03-16 op {
352 cc4953ef 2021-03-16 op if ((req->ctx = tls_client()) == NULL) {
353 cc4953ef 2021-03-16 op close_with_errf(req, "tls_client: %s", strerror(errno));
354 cc4953ef 2021-03-16 op return;
355 cc4953ef 2021-03-16 op }
356 cc4953ef 2021-03-16 op if (tls_configure(req->ctx, tlsconf) == -1) {
357 cc4953ef 2021-03-16 op close_with_errf(req, "tls_configure: %s", tls_error(req->ctx));
358 cc4953ef 2021-03-16 op return;
359 cc4953ef 2021-03-16 op }
360 cc4953ef 2021-03-16 op if (tls_connect_socket(req->ctx, req->fd, req->url.host) == -1) {
361 cc4953ef 2021-03-16 op close_with_errf(req, "tls_connect_socket: %s", tls_error(req->ctx));
362 cc4953ef 2021-03-16 op return;
363 cc4953ef 2021-03-16 op }
364 cc4953ef 2021-03-16 op yield_w(req, do_handshake, &timeout_for_handshake);
365 cc4953ef 2021-03-16 op }
366 cc4953ef 2021-03-16 op
367 cc4953ef 2021-03-16 op static void
368 5e11c00c 2021-03-02 op do_handshake(int fd, short ev, void *d)
369 5e11c00c 2021-03-02 op {
370 5e11c00c 2021-03-02 op struct req *req = d;
371 5e11c00c 2021-03-02 op const char *hash;
372 5e11c00c 2021-03-02 op
373 723bee54 2021-03-09 op if (ev == EV_TIMEOUT) {
374 723bee54 2021-03-09 op close_with_err(req, "Timeout loading page");
375 723bee54 2021-03-09 op return;
376 723bee54 2021-03-09 op }
377 723bee54 2021-03-09 op
378 5e11c00c 2021-03-02 op switch (tls_handshake(req->ctx)) {
379 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
380 5e11c00c 2021-03-02 op yield_r(req, do_handshake, NULL);
381 5e11c00c 2021-03-02 op return;
382 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
383 5e11c00c 2021-03-02 op yield_w(req, do_handshake, NULL);
384 5e11c00c 2021-03-02 op return;
385 5e11c00c 2021-03-02 op }
386 5e11c00c 2021-03-02 op
387 5e11c00c 2021-03-02 op hash = tls_peer_cert_hash(req->ctx);
388 282a1d0a 2021-03-16 op if (hash == NULL) {
389 a237223e 2021-03-16 op close_with_errf(req, "handshake failed: %s", tls_error(req->ctx));
390 282a1d0a 2021-03-16 op return;
391 282a1d0a 2021-03-16 op }
392 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_CHECK_CERT, req->id, 0, -1, hash, strlen(hash)+1);
393 5e11c00c 2021-03-02 op imsg_flush(ibuf);
394 5e11c00c 2021-03-02 op }
395 5e11c00c 2021-03-02 op
396 5e11c00c 2021-03-02 op static void
397 5e11c00c 2021-03-02 op write_request(int fd, short ev, void *d)
398 5e11c00c 2021-03-02 op {
399 5e11c00c 2021-03-02 op struct req *req = d;
400 5e11c00c 2021-03-02 op ssize_t r;
401 5e11c00c 2021-03-02 op size_t len;
402 a237223e 2021-03-16 op char buf[1024];
403 5e11c00c 2021-03-02 op
404 5e11c00c 2021-03-02 op strlcpy(buf, "gemini://", sizeof(buf));
405 5e11c00c 2021-03-02 op strlcat(buf, req->url.host, sizeof(buf));
406 5e11c00c 2021-03-02 op strlcat(buf, "/", sizeof(buf));
407 5e11c00c 2021-03-02 op strlcat(buf, req->url.path, sizeof(buf));
408 5e11c00c 2021-03-02 op
409 5e11c00c 2021-03-02 op if (req->url.query[0] != '\0') {
410 5e11c00c 2021-03-02 op strlcat(buf, "?", sizeof(buf));
411 5e11c00c 2021-03-02 op strlcat(buf, req->url.query, sizeof(buf));
412 5e11c00c 2021-03-02 op }
413 5e11c00c 2021-03-02 op
414 5e11c00c 2021-03-02 op len = strlcat(buf, "\r\n", sizeof(buf));
415 5e11c00c 2021-03-02 op
416 5e11c00c 2021-03-02 op assert(len <= sizeof(buf));
417 5e11c00c 2021-03-02 op
418 5e11c00c 2021-03-02 op switch (r = tls_write(req->ctx, buf, len)) {
419 5e11c00c 2021-03-02 op case -1:
420 a237223e 2021-03-16 op close_with_errf(req, "tls_write: %s", tls_error(req->ctx));
421 5e11c00c 2021-03-02 op break;
422 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
423 5e11c00c 2021-03-02 op yield_r(req, write_request, NULL);
424 5e11c00c 2021-03-02 op break;
425 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
426 5e11c00c 2021-03-02 op yield_w(req, write_request, NULL);
427 5e11c00c 2021-03-02 op break;
428 5e11c00c 2021-03-02 op default:
429 5e11c00c 2021-03-02 op /* assume r == len */
430 5e11c00c 2021-03-02 op (void)r;
431 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
432 5e11c00c 2021-03-02 op break;
433 5e11c00c 2021-03-02 op }
434 5e11c00c 2021-03-02 op }
435 5e11c00c 2021-03-02 op
436 5e11c00c 2021-03-02 op static void
437 5e11c00c 2021-03-02 op read_reply(int fd, short ev, void *d)
438 5e11c00c 2021-03-02 op {
439 5e11c00c 2021-03-02 op struct req *req = d;
440 5e11c00c 2021-03-02 op size_t len;
441 5e11c00c 2021-03-02 op ssize_t r;
442 a237223e 2021-03-16 op char *buf;
443 5e11c00c 2021-03-02 op
444 5e11c00c 2021-03-02 op buf = req->buf + req->off;
445 5e11c00c 2021-03-02 op len = sizeof(req->buf) - req->off;
446 5e11c00c 2021-03-02 op
447 5e11c00c 2021-03-02 op switch (r = tls_read(req->ctx, buf, len)) {
448 5e11c00c 2021-03-02 op case -1:
449 a237223e 2021-03-16 op close_with_errf(req, "tls_read: %s", tls_error(req->ctx));
450 5e11c00c 2021-03-02 op break;
451 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
452 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
453 5e11c00c 2021-03-02 op break;
454 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
455 5e11c00c 2021-03-02 op yield_w(req, read_reply, NULL);
456 5e11c00c 2021-03-02 op break;
457 5e11c00c 2021-03-02 op default:
458 5e11c00c 2021-03-02 op req->off += r;
459 5e11c00c 2021-03-02 op
460 e62289e7 2021-03-16 op if (memmem(req->buf, req->off, "\r\n", 2) != NULL)
461 5e11c00c 2021-03-02 op parse_reply(req);
462 5e11c00c 2021-03-02 op else if (req->off == sizeof(req->buf))
463 5e11c00c 2021-03-02 op close_with_err(req, "invalid response");
464 5e11c00c 2021-03-02 op else
465 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
466 5e11c00c 2021-03-02 op break;
467 5e11c00c 2021-03-02 op }
468 5e11c00c 2021-03-02 op }
469 5e11c00c 2021-03-02 op
470 5e11c00c 2021-03-02 op static void
471 5e11c00c 2021-03-02 op parse_reply(struct req *req)
472 5e11c00c 2021-03-02 op {
473 5e11c00c 2021-03-02 op int code;
474 5e11c00c 2021-03-02 op char *e;
475 5e11c00c 2021-03-02 op size_t len;
476 5e11c00c 2021-03-02 op
477 5e11c00c 2021-03-02 op if (req->off < 4)
478 5e11c00c 2021-03-02 op goto err;
479 5e11c00c 2021-03-02 op
480 5e11c00c 2021-03-02 op if (!isdigit(req->buf[0]) || !isdigit(req->buf[1]))
481 5e11c00c 2021-03-02 op goto err;
482 5e11c00c 2021-03-02 op
483 5e11c00c 2021-03-02 op code = (req->buf[0] - '0')*10 + (req->buf[1] - '0');
484 5e11c00c 2021-03-02 op
485 5e11c00c 2021-03-02 op if (!isspace(req->buf[2]))
486 5e11c00c 2021-03-02 op goto err;
487 5e11c00c 2021-03-02 op
488 5e11c00c 2021-03-02 op advance_buf(req, 3);
489 e62289e7 2021-03-16 op if ((e = memmem(req->buf, req->off, "\r\n", 2)) == NULL)
490 5e11c00c 2021-03-02 op goto err;
491 5e11c00c 2021-03-02 op
492 5e11c00c 2021-03-02 op *e = '\0';
493 5e11c00c 2021-03-02 op e++;
494 5e11c00c 2021-03-02 op len = e - req->buf;
495 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_GOT_CODE, req->id, 0, -1, &code, sizeof(code));
496 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_GOT_META, req->id, 0, -1,
497 5e11c00c 2021-03-02 op req->buf, len);
498 5e11c00c 2021-03-02 op imsg_flush(ibuf);
499 5e11c00c 2021-03-02 op
500 723bee54 2021-03-09 op if (code != 20)
501 723bee54 2021-03-09 op close_conn(0, 0, req);
502 58ba20cd 2021-03-18 op else
503 58ba20cd 2021-03-18 op advance_buf(req, len+1); /* skip \n too */
504 723bee54 2021-03-09 op
505 5e11c00c 2021-03-02 op return;
506 5e11c00c 2021-03-02 op
507 5e11c00c 2021-03-02 op err:
508 5e11c00c 2021-03-02 op close_with_err(req, "malformed request");
509 5e11c00c 2021-03-02 op }
510 5e11c00c 2021-03-02 op
511 5e11c00c 2021-03-02 op static void
512 5e11c00c 2021-03-02 op copy_body(int fd, short ev, void *d)
513 5e11c00c 2021-03-02 op {
514 5e11c00c 2021-03-02 op struct req *req = d;
515 5e11c00c 2021-03-02 op ssize_t r;
516 5e11c00c 2021-03-02 op
517 e4304954 2021-03-14 op for (;;) {
518 72a79cfb 2021-03-13 op if (req->off != 0) {
519 72a79cfb 2021-03-13 op imsg_compose(ibuf, IMSG_BUF, req->id, 0, -1,
520 72a79cfb 2021-03-13 op req->buf, req->off);
521 72a79cfb 2021-03-13 op imsg_flush(ibuf);
522 1deaf397 2021-03-14 op req->off = 0;
523 72a79cfb 2021-03-13 op }
524 72a79cfb 2021-03-13 op
525 72a79cfb 2021-03-13 op switch (r = tls_read(req->ctx, req->buf, sizeof(req->buf))) {
526 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
527 5e11c00c 2021-03-02 op yield_r(req, copy_body, NULL);
528 5e11c00c 2021-03-02 op return;
529 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
530 5e11c00c 2021-03-02 op yield_w(req, copy_body, NULL);
531 5e11c00c 2021-03-02 op return;
532 5e11c00c 2021-03-02 op case 0:
533 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_EOF, req->id, 0, -1, NULL, 0);
534 5e11c00c 2021-03-02 op imsg_flush(ibuf);
535 5e11c00c 2021-03-02 op close_conn(0, 0, req);
536 5e11c00c 2021-03-02 op return;
537 5e11c00c 2021-03-02 op default:
538 72a79cfb 2021-03-13 op req->off = r;
539 5e11c00c 2021-03-02 op }
540 e4304954 2021-03-14 op }
541 5e11c00c 2021-03-02 op }
542 5e11c00c 2021-03-02 op
543 5e11c00c 2021-03-02 op static void
544 5e11c00c 2021-03-02 op handle_get(struct imsg *imsg, size_t datalen)
545 5e11c00c 2021-03-02 op {
546 5e11c00c 2021-03-02 op struct req *req;
547 5e11c00c 2021-03-02 op const char *e;
548 cc4953ef 2021-03-16 op char *data;
549 5e11c00c 2021-03-02 op
550 5e11c00c 2021-03-02 op data = imsg->data;
551 5e11c00c 2021-03-02 op
552 5e11c00c 2021-03-02 op if (data[datalen-1] != '\0')
553 5e11c00c 2021-03-02 op die();
554 5e11c00c 2021-03-02 op
555 5e11c00c 2021-03-02 op if ((req = calloc(1, sizeof(*req))) == NULL)
556 5e11c00c 2021-03-02 op die();
557 5e11c00c 2021-03-02 op
558 5e11c00c 2021-03-02 op req->id = imsg->hdr.peerid;
559 71a2feab 2021-03-10 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
560 5e11c00c 2021-03-02 op
561 5e11c00c 2021-03-02 op if (!url_parse(imsg->data, &req->url, &e)) {
562 5e11c00c 2021-03-02 op close_with_err(req, e);
563 5e11c00c 2021-03-02 op return;
564 5e11c00c 2021-03-02 op }
565 5e11c00c 2021-03-02 op
566 cc4953ef 2021-03-16 op #if HAVE_ASR_RUN
567 cc4953ef 2021-03-16 op async_conn_towards(req);
568 cc4953ef 2021-03-16 op #else
569 cc4953ef 2021-03-16 op {
570 0bbc0ba9 2021-03-17 op char *err = NULL;
571 5e11c00c 2021-03-02 op
572 cc4953ef 2021-03-16 op if ((req->fd = blocking_conn_towards(&req->url, &err)) == -1) {
573 cc4953ef 2021-03-16 op close_with_err(req, err);
574 cc4953ef 2021-03-16 op free(err);
575 58ba20cd 2021-03-18 op return;
576 cc4953ef 2021-03-16 op }
577 cc4953ef 2021-03-16 op setup_tls(req);
578 cc4953ef 2021-03-16 op }
579 cc4953ef 2021-03-16 op #endif
580 5e11c00c 2021-03-02 op }
581 5e11c00c 2021-03-02 op
582 5e11c00c 2021-03-02 op static void
583 5e11c00c 2021-03-02 op handle_cert_status(struct imsg *imsg, size_t datalen)
584 5e11c00c 2021-03-02 op {
585 5e11c00c 2021-03-02 op struct req *req;
586 5e11c00c 2021-03-02 op int is_ok;
587 5e11c00c 2021-03-02 op
588 5e11c00c 2021-03-02 op req = req_by_id(imsg->hdr.peerid);
589 5e11c00c 2021-03-02 op
590 5e11c00c 2021-03-02 op if (datalen < sizeof(is_ok))
591 5e11c00c 2021-03-02 op die();
592 5e11c00c 2021-03-02 op memcpy(&is_ok, imsg->data, sizeof(is_ok));
593 5e11c00c 2021-03-02 op
594 5e11c00c 2021-03-02 op if (is_ok)
595 5e11c00c 2021-03-02 op yield_w(req, write_request, NULL);
596 5e11c00c 2021-03-02 op else
597 5e11c00c 2021-03-02 op close_conn(0, 0, req);
598 5e11c00c 2021-03-02 op }
599 5e11c00c 2021-03-02 op
600 5e11c00c 2021-03-02 op static void
601 0972d8b2 2021-03-02 op handle_proceed(struct imsg *imsg, size_t datalen)
602 0972d8b2 2021-03-02 op {
603 e4304954 2021-03-14 op yield_r(req_by_id(imsg->hdr.peerid),
604 e4304954 2021-03-14 op copy_body, NULL);
605 0972d8b2 2021-03-02 op }
606 0972d8b2 2021-03-02 op
607 0972d8b2 2021-03-02 op static void
608 5e11c00c 2021-03-02 op handle_stop(struct imsg *imsg, size_t datalen)
609 5e11c00c 2021-03-02 op {
610 5e11c00c 2021-03-02 op struct req *req;
611 5e11c00c 2021-03-02 op
612 15354eed 2021-03-10 op if ((req = req_by_id_try(imsg->hdr.peerid)) == NULL)
613 15354eed 2021-03-10 op return;
614 5e11c00c 2021-03-02 op close_conn(0, 0, req);
615 5e11c00c 2021-03-02 op }
616 5e11c00c 2021-03-02 op
617 5e11c00c 2021-03-02 op static void
618 5e11c00c 2021-03-02 op handle_quit(struct imsg *imsg, size_t datalen)
619 5e11c00c 2021-03-02 op {
620 5e11c00c 2021-03-02 op event_loopbreak();
621 5e11c00c 2021-03-02 op }
622 5e11c00c 2021-03-02 op
623 5e11c00c 2021-03-02 op static void
624 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
625 5e11c00c 2021-03-02 op {
626 5e11c00c 2021-03-02 op struct imsgbuf *ibuf = d;
627 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
628 5e11c00c 2021-03-02 op }
629 5e11c00c 2021-03-02 op
630 5e11c00c 2021-03-02 op int
631 5e11c00c 2021-03-02 op client_main(struct imsgbuf *b)
632 5e11c00c 2021-03-02 op {
633 5e11c00c 2021-03-02 op ibuf = b;
634 5e11c00c 2021-03-02 op
635 5e11c00c 2021-03-02 op TAILQ_INIT(&reqhead);
636 5e11c00c 2021-03-02 op
637 5e11c00c 2021-03-02 op if ((tlsconf = tls_config_new()) == NULL)
638 5e11c00c 2021-03-02 op die();
639 5e11c00c 2021-03-02 op tls_config_insecure_noverifycert(tlsconf);
640 d6cd2fc1 2021-03-25 op tls_config_insecure_noverifyname(tlsconf);
641 5e11c00c 2021-03-02 op
642 5e11c00c 2021-03-02 op event_init();
643 5e11c00c 2021-03-02 op
644 1304bbdd 2021-03-15 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
645 5e11c00c 2021-03-02 op event_add(&imsgev, NULL);
646 ebdcadbf 2021-03-12 op
647 ebdcadbf 2021-03-12 op sandbox_network_process();
648 5e11c00c 2021-03-02 op
649 5e11c00c 2021-03-02 op event_dispatch();
650 5e11c00c 2021-03-02 op return 0;
651 5e11c00c 2021-03-02 op }