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 ad461650 2021-06-13 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 static void try_to_connect(int, short, void*);
56 58cfcb90 2021-06-18 op
57 58cfcb90 2021-06-18 op #if HAVE_ASR_RUN
58 cc4953ef 2021-03-16 op static void query_done(struct asr_result*, void*);
59 cc4953ef 2021-03-16 op static void async_conn_towards(struct req*);
60 cc4953ef 2021-03-16 op #else
61 4e600d30 2021-03-29 op static void blocking_conn_towards(struct req*);
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 984245ce 2021-06-23 op static void handle_get_raw(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 984245ce 2021-06-23 op [IMSG_GET_RAW] = handle_get_raw,
88 848779c2 2021-03-29 op [IMSG_CERT_STATUS] = handle_cert_status,
89 848779c2 2021-03-29 op [IMSG_PROCEED] = handle_proceed,
90 848779c2 2021-03-29 op [IMSG_STOP] = handle_stop,
91 848779c2 2021-03-29 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 31f1a758 2021-04-22 op struct phos_uri 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 58cfcb90 2021-06-18 op struct addrinfo *servinfo, *p;
108 cc4953ef 2021-03-16 op #if HAVE_ASR_RUN
109 58cfcb90 2021-06-18 op struct addrinfo hints;
110 cc4953ef 2021-03-16 op struct event_asr *asrev;
111 cc4953ef 2021-03-16 op #endif
112 cc4953ef 2021-03-16 op
113 5e11c00c 2021-03-02 op TAILQ_ENTRY(req) reqs;
114 5e11c00c 2021-03-02 op };
115 5e11c00c 2021-03-02 op
116 5e11c00c 2021-03-02 op static inline void
117 5e11c00c 2021-03-02 op yield_r(struct req *req, statefn fn, struct timeval *tv)
118 5e11c00c 2021-03-02 op {
119 5e11c00c 2021-03-02 op event_once(req->fd, EV_READ, fn, req, tv);
120 5e11c00c 2021-03-02 op }
121 5e11c00c 2021-03-02 op
122 5e11c00c 2021-03-02 op static inline void
123 5e11c00c 2021-03-02 op yield_w(struct req *req, statefn fn, struct timeval *tv)
124 5e11c00c 2021-03-02 op {
125 5e11c00c 2021-03-02 op event_once(req->fd, EV_WRITE, fn, req, tv);
126 5e11c00c 2021-03-02 op }
127 5e11c00c 2021-03-02 op
128 5e11c00c 2021-03-02 op static inline void
129 5e11c00c 2021-03-02 op advance_buf(struct req *req, size_t len)
130 5e11c00c 2021-03-02 op {
131 5e11c00c 2021-03-02 op assert(len <= req->off);
132 5e11c00c 2021-03-02 op
133 5e11c00c 2021-03-02 op req->off -= len;
134 5e11c00c 2021-03-02 op memmove(req->buf, req->buf + len, req->off);
135 5e11c00c 2021-03-02 op }
136 5e11c00c 2021-03-02 op
137 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
138 5e11c00c 2021-03-02 op die(void)
139 5e11c00c 2021-03-02 op {
140 5e11c00c 2021-03-02 op abort(); /* TODO */
141 cc4953ef 2021-03-16 op }
142 cc4953ef 2021-03-16 op
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 848779c2 2021-03-29 op again:
151 cc4953ef 2021-03-16 op if (req->p == NULL)
152 cc4953ef 2021-03-16 op goto err;
153 cc4953ef 2021-03-16 op
154 cc4953ef 2021-03-16 op if (req->fd != -1) {
155 cc4953ef 2021-03-16 op if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error, &len) == -1)
156 cc4953ef 2021-03-16 op goto err;
157 cc4953ef 2021-03-16 op if (error != 0) {
158 cc4953ef 2021-03-16 op errno = error;
159 cc4953ef 2021-03-16 op goto err;
160 cc4953ef 2021-03-16 op }
161 cc4953ef 2021-03-16 op goto done;
162 cc4953ef 2021-03-16 op }
163 cc4953ef 2021-03-16 op
164 cc4953ef 2021-03-16 op req->fd = socket(req->p->ai_family, req->p->ai_socktype, req->p->ai_protocol);
165 20d2d786 2021-03-16 op if (req->fd == -1) {
166 cc4953ef 2021-03-16 op req->p = req->p->ai_next;
167 848779c2 2021-03-29 op goto again;
168 20d2d786 2021-03-16 op } else {
169 cc4953ef 2021-03-16 op mark_nonblock(req->fd);
170 cc4953ef 2021-03-16 op if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
171 cc4953ef 2021-03-16 op goto done;
172 20d2d786 2021-03-16 op yield_w(req, try_to_connect, NULL);
173 cc4953ef 2021-03-16 op }
174 cc4953ef 2021-03-16 op return;
175 cc4953ef 2021-03-16 op
176 cc4953ef 2021-03-16 op err:
177 cc4953ef 2021-03-16 op freeaddrinfo(req->servinfo);
178 cc4953ef 2021-03-16 op close_with_errf(req, "failed to connect to %s",
179 cc4953ef 2021-03-16 op req->url.host);
180 cc4953ef 2021-03-16 op return;
181 cc4953ef 2021-03-16 op
182 cc4953ef 2021-03-16 op done:
183 cc4953ef 2021-03-16 op freeaddrinfo(req->servinfo);
184 cc4953ef 2021-03-16 op setup_tls(req);
185 cc4953ef 2021-03-16 op }
186 cc4953ef 2021-03-16 op
187 58cfcb90 2021-06-18 op #if HAVE_ASR_RUN
188 cc4953ef 2021-03-16 op static void
189 cc4953ef 2021-03-16 op query_done(struct asr_result *res, void *d)
190 cc4953ef 2021-03-16 op {
191 cc4953ef 2021-03-16 op struct req *req = d;
192 cc4953ef 2021-03-16 op
193 cc4953ef 2021-03-16 op req->asrev = NULL;
194 cc4953ef 2021-03-16 op if (res->ar_gai_errno != 0) {
195 cc4953ef 2021-03-16 op close_with_errf(req, "failed to resolve %s: %s",
196 cc4953ef 2021-03-16 op req->url.host, gai_strerror(res->ar_gai_errno));
197 cc4953ef 2021-03-16 op return;
198 cc4953ef 2021-03-16 op }
199 cc4953ef 2021-03-16 op
200 cc4953ef 2021-03-16 op req->fd = -1;
201 cc4953ef 2021-03-16 op req->servinfo = res->ar_addrinfo;
202 cc4953ef 2021-03-16 op req->p = res->ar_addrinfo;
203 cc4953ef 2021-03-16 op try_to_connect(0, 0, req);
204 5e11c00c 2021-03-02 op }
205 5e11c00c 2021-03-02 op
206 cc4953ef 2021-03-16 op static void
207 cc4953ef 2021-03-16 op async_conn_towards(struct req *req)
208 cc4953ef 2021-03-16 op {
209 cc4953ef 2021-03-16 op struct asr_query *q;
210 cc4953ef 2021-03-16 op const char *proto = "1965";
211 cc4953ef 2021-03-16 op
212 cc4953ef 2021-03-16 op if (*req->url.port != '\0')
213 cc4953ef 2021-03-16 op proto = req->url.port;
214 cc4953ef 2021-03-16 op
215 cc4953ef 2021-03-16 op req->hints.ai_family = AF_UNSPEC;
216 cc4953ef 2021-03-16 op req->hints.ai_socktype = SOCK_STREAM;
217 cc4953ef 2021-03-16 op q = getaddrinfo_async(req->url.host, proto, &req->hints, NULL);
218 cc4953ef 2021-03-16 op req->asrev = event_asr_run(q, query_done, req);
219 cc4953ef 2021-03-16 op }
220 cc4953ef 2021-03-16 op #else
221 4e600d30 2021-03-29 op static void
222 4e600d30 2021-03-29 op blocking_conn_towards(struct req *req)
223 5e11c00c 2021-03-02 op {
224 58cfcb90 2021-06-18 op struct addrinfo hints;
225 a13419ca 2021-04-25 op struct phos_uri *url = &req->url;
226 58cfcb90 2021-06-18 op int status;
227 5e11c00c 2021-03-02 op const char *proto = "1965";
228 5e11c00c 2021-03-02 op
229 5e11c00c 2021-03-02 op if (*url->port != '\0')
230 5e11c00c 2021-03-02 op proto = url->port;
231 5e11c00c 2021-03-02 op
232 5e11c00c 2021-03-02 op memset(&hints, 0, sizeof(hints));
233 5e11c00c 2021-03-02 op hints.ai_family = AF_UNSPEC;
234 5e11c00c 2021-03-02 op hints.ai_socktype = SOCK_STREAM;
235 5e11c00c 2021-03-02 op
236 58cfcb90 2021-06-18 op if ((status = getaddrinfo(url->host, proto, &hints, &req->servinfo))) {
237 4e600d30 2021-03-29 op close_with_errf(req, "failed to resolve %s: %s",
238 5e11c00c 2021-03-02 op url->host, gai_strerror(status));
239 4e600d30 2021-03-29 op return;
240 5e11c00c 2021-03-02 op }
241 5e11c00c 2021-03-02 op
242 58cfcb90 2021-06-18 op req->fd = -1;
243 58cfcb90 2021-06-18 op req->p = req->servinfo;
244 58cfcb90 2021-06-18 op try_to_connect(0, 0, req);
245 5e11c00c 2021-03-02 op }
246 cc4953ef 2021-03-16 op #endif
247 5e11c00c 2021-03-02 op
248 5e11c00c 2021-03-02 op static struct req *
249 5e11c00c 2021-03-02 op req_by_id(uint32_t id)
250 15354eed 2021-03-10 op {
251 15354eed 2021-03-10 op struct req *r;
252 15354eed 2021-03-10 op
253 15354eed 2021-03-10 op if ((r = req_by_id_try(id)) == NULL)
254 15354eed 2021-03-10 op die();
255 15354eed 2021-03-10 op return r;
256 15354eed 2021-03-10 op }
257 15354eed 2021-03-10 op
258 15354eed 2021-03-10 op static struct req *
259 15354eed 2021-03-10 op req_by_id_try(uint32_t id)
260 5e11c00c 2021-03-02 op {
261 5e11c00c 2021-03-02 op struct req *r;
262 5e11c00c 2021-03-02 op
263 5e11c00c 2021-03-02 op TAILQ_FOREACH(r, &reqhead, reqs) {
264 5e11c00c 2021-03-02 op if (r->id == id)
265 5e11c00c 2021-03-02 op return r;
266 5e11c00c 2021-03-02 op }
267 5e11c00c 2021-03-02 op
268 15354eed 2021-03-10 op return NULL;
269 5e11c00c 2021-03-02 op }
270 5e11c00c 2021-03-02 op
271 5e11c00c 2021-03-02 op static void
272 5e11c00c 2021-03-02 op close_conn(int fd, short ev, void *d)
273 5e11c00c 2021-03-02 op {
274 5e11c00c 2021-03-02 op struct req *req = d;
275 5e11c00c 2021-03-02 op
276 cc4953ef 2021-03-16 op #if HAVE_ASR_RUN
277 cc4953ef 2021-03-16 op if (req->asrev != NULL)
278 cc4953ef 2021-03-16 op event_asr_abort(req->asrev);
279 cc4953ef 2021-03-16 op #endif
280 cc4953ef 2021-03-16 op
281 5e11c00c 2021-03-02 op if (req->ctx != NULL) {
282 5e11c00c 2021-03-02 op switch (tls_close(req->ctx)) {
283 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
284 5e11c00c 2021-03-02 op yield_r(req, close_conn, NULL);
285 5e11c00c 2021-03-02 op return;
286 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
287 5e11c00c 2021-03-02 op yield_w(req, close_conn, NULL);
288 5e11c00c 2021-03-02 op return;
289 5e11c00c 2021-03-02 op }
290 5e11c00c 2021-03-02 op
291 5e11c00c 2021-03-02 op tls_free(req->ctx);
292 5e11c00c 2021-03-02 op }
293 5e11c00c 2021-03-02 op
294 5e11c00c 2021-03-02 op TAILQ_REMOVE(&reqhead, req, reqs);
295 5e11c00c 2021-03-02 op if (req->fd != -1)
296 5e11c00c 2021-03-02 op close(req->fd);
297 5e11c00c 2021-03-02 op free(req);
298 5e11c00c 2021-03-02 op }
299 5e11c00c 2021-03-02 op
300 5e11c00c 2021-03-02 op static void
301 5e11c00c 2021-03-02 op close_with_err(struct req *req, const char *err)
302 5e11c00c 2021-03-02 op {
303 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_ERR, req->id, 0, -1, err, strlen(err)+1);
304 5e11c00c 2021-03-02 op imsg_flush(ibuf);
305 5e11c00c 2021-03-02 op close_conn(0, 0, req);
306 5e11c00c 2021-03-02 op }
307 5e11c00c 2021-03-02 op
308 5e11c00c 2021-03-02 op static void
309 a237223e 2021-03-16 op close_with_errf(struct req *req, const char *fmt, ...)
310 a237223e 2021-03-16 op {
311 a237223e 2021-03-16 op va_list ap;
312 a237223e 2021-03-16 op char *s;
313 a237223e 2021-03-16 op
314 a237223e 2021-03-16 op va_start(ap, fmt);
315 a237223e 2021-03-16 op if (vasprintf(&s, fmt, ap) == -1)
316 a237223e 2021-03-16 op abort();
317 a237223e 2021-03-16 op va_end(ap);
318 a237223e 2021-03-16 op
319 a237223e 2021-03-16 op close_with_err(req, s);
320 a237223e 2021-03-16 op free(s);
321 a237223e 2021-03-16 op }
322 a237223e 2021-03-16 op
323 a237223e 2021-03-16 op static void
324 cc4953ef 2021-03-16 op setup_tls(struct req *req)
325 cc4953ef 2021-03-16 op {
326 cc4953ef 2021-03-16 op if ((req->ctx = tls_client()) == NULL) {
327 cc4953ef 2021-03-16 op close_with_errf(req, "tls_client: %s", strerror(errno));
328 cc4953ef 2021-03-16 op return;
329 cc4953ef 2021-03-16 op }
330 cc4953ef 2021-03-16 op if (tls_configure(req->ctx, tlsconf) == -1) {
331 cc4953ef 2021-03-16 op close_with_errf(req, "tls_configure: %s", tls_error(req->ctx));
332 cc4953ef 2021-03-16 op return;
333 cc4953ef 2021-03-16 op }
334 cc4953ef 2021-03-16 op if (tls_connect_socket(req->ctx, req->fd, req->url.host) == -1) {
335 cc4953ef 2021-03-16 op close_with_errf(req, "tls_connect_socket: %s", tls_error(req->ctx));
336 cc4953ef 2021-03-16 op return;
337 cc4953ef 2021-03-16 op }
338 cc4953ef 2021-03-16 op yield_w(req, do_handshake, &timeout_for_handshake);
339 cc4953ef 2021-03-16 op }
340 cc4953ef 2021-03-16 op
341 cc4953ef 2021-03-16 op static void
342 5e11c00c 2021-03-02 op do_handshake(int fd, short ev, void *d)
343 5e11c00c 2021-03-02 op {
344 5e11c00c 2021-03-02 op struct req *req = d;
345 5e11c00c 2021-03-02 op const char *hash;
346 5e11c00c 2021-03-02 op
347 723bee54 2021-03-09 op if (ev == EV_TIMEOUT) {
348 723bee54 2021-03-09 op close_with_err(req, "Timeout loading page");
349 723bee54 2021-03-09 op return;
350 723bee54 2021-03-09 op }
351 723bee54 2021-03-09 op
352 5e11c00c 2021-03-02 op switch (tls_handshake(req->ctx)) {
353 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
354 5e11c00c 2021-03-02 op yield_r(req, do_handshake, NULL);
355 5e11c00c 2021-03-02 op return;
356 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
357 5e11c00c 2021-03-02 op yield_w(req, do_handshake, NULL);
358 5e11c00c 2021-03-02 op return;
359 5e11c00c 2021-03-02 op }
360 5e11c00c 2021-03-02 op
361 5e11c00c 2021-03-02 op hash = tls_peer_cert_hash(req->ctx);
362 282a1d0a 2021-03-16 op if (hash == NULL) {
363 a237223e 2021-03-16 op close_with_errf(req, "handshake failed: %s", tls_error(req->ctx));
364 282a1d0a 2021-03-16 op return;
365 282a1d0a 2021-03-16 op }
366 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_CHECK_CERT, req->id, 0, -1, hash, strlen(hash)+1);
367 5e11c00c 2021-03-02 op imsg_flush(ibuf);
368 5e11c00c 2021-03-02 op }
369 5e11c00c 2021-03-02 op
370 5e11c00c 2021-03-02 op static void
371 5e11c00c 2021-03-02 op write_request(int fd, short ev, void *d)
372 5e11c00c 2021-03-02 op {
373 5e11c00c 2021-03-02 op struct req *req = d;
374 5e11c00c 2021-03-02 op ssize_t r;
375 5e11c00c 2021-03-02 op size_t len;
376 984245ce 2021-06-23 op
377 984245ce 2021-06-23 op len = strlen(req->buf);
378 5e11c00c 2021-03-02 op
379 984245ce 2021-06-23 op switch (r = tls_write(req->ctx, req->buf, len)) {
380 5e11c00c 2021-03-02 op case -1:
381 a237223e 2021-03-16 op close_with_errf(req, "tls_write: %s", tls_error(req->ctx));
382 5e11c00c 2021-03-02 op break;
383 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
384 5e11c00c 2021-03-02 op yield_r(req, write_request, NULL);
385 5e11c00c 2021-03-02 op break;
386 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
387 5e11c00c 2021-03-02 op yield_w(req, write_request, NULL);
388 5e11c00c 2021-03-02 op break;
389 5e11c00c 2021-03-02 op default:
390 5e11c00c 2021-03-02 op /* assume r == len */
391 5e11c00c 2021-03-02 op (void)r;
392 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
393 5e11c00c 2021-03-02 op break;
394 5e11c00c 2021-03-02 op }
395 5e11c00c 2021-03-02 op }
396 5e11c00c 2021-03-02 op
397 5e11c00c 2021-03-02 op static void
398 5e11c00c 2021-03-02 op read_reply(int fd, short ev, void *d)
399 5e11c00c 2021-03-02 op {
400 5e11c00c 2021-03-02 op struct req *req = d;
401 5e11c00c 2021-03-02 op size_t len;
402 5e11c00c 2021-03-02 op ssize_t r;
403 a237223e 2021-03-16 op char *buf;
404 5e11c00c 2021-03-02 op
405 5e11c00c 2021-03-02 op buf = req->buf + req->off;
406 5e11c00c 2021-03-02 op len = sizeof(req->buf) - req->off;
407 5e11c00c 2021-03-02 op
408 5e11c00c 2021-03-02 op switch (r = tls_read(req->ctx, buf, len)) {
409 5e11c00c 2021-03-02 op case -1:
410 a237223e 2021-03-16 op close_with_errf(req, "tls_read: %s", tls_error(req->ctx));
411 5e11c00c 2021-03-02 op break;
412 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
413 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
414 5e11c00c 2021-03-02 op break;
415 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
416 5e11c00c 2021-03-02 op yield_w(req, read_reply, NULL);
417 5e11c00c 2021-03-02 op break;
418 5e11c00c 2021-03-02 op default:
419 5e11c00c 2021-03-02 op req->off += r;
420 5e11c00c 2021-03-02 op
421 e62289e7 2021-03-16 op if (memmem(req->buf, req->off, "\r\n", 2) != NULL)
422 5e11c00c 2021-03-02 op parse_reply(req);
423 5e11c00c 2021-03-02 op else if (req->off == sizeof(req->buf))
424 5e11c00c 2021-03-02 op close_with_err(req, "invalid response");
425 5e11c00c 2021-03-02 op else
426 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
427 5e11c00c 2021-03-02 op break;
428 5e11c00c 2021-03-02 op }
429 5e11c00c 2021-03-02 op }
430 5e11c00c 2021-03-02 op
431 5e11c00c 2021-03-02 op static void
432 5e11c00c 2021-03-02 op parse_reply(struct req *req)
433 5e11c00c 2021-03-02 op {
434 5e11c00c 2021-03-02 op int code;
435 5e11c00c 2021-03-02 op char *e;
436 5e11c00c 2021-03-02 op size_t len;
437 5e11c00c 2021-03-02 op
438 5e11c00c 2021-03-02 op if (req->off < 4)
439 5e11c00c 2021-03-02 op goto err;
440 5e11c00c 2021-03-02 op
441 5e11c00c 2021-03-02 op if (!isdigit(req->buf[0]) || !isdigit(req->buf[1]))
442 5e11c00c 2021-03-02 op goto err;
443 5e11c00c 2021-03-02 op
444 5e11c00c 2021-03-02 op code = (req->buf[0] - '0')*10 + (req->buf[1] - '0');
445 5e11c00c 2021-03-02 op
446 5e11c00c 2021-03-02 op if (!isspace(req->buf[2]))
447 5e11c00c 2021-03-02 op goto err;
448 5e11c00c 2021-03-02 op
449 5e11c00c 2021-03-02 op advance_buf(req, 3);
450 e62289e7 2021-03-16 op if ((e = memmem(req->buf, req->off, "\r\n", 2)) == NULL)
451 5e11c00c 2021-03-02 op goto err;
452 5e11c00c 2021-03-02 op
453 5e11c00c 2021-03-02 op *e = '\0';
454 5e11c00c 2021-03-02 op e++;
455 5e11c00c 2021-03-02 op len = e - req->buf;
456 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_GOT_CODE, req->id, 0, -1, &code, sizeof(code));
457 155dac7f 2021-03-29 op imsg_compose(ibuf, IMSG_GOT_META, req->id, 0, -1, req->buf, len);
458 5e11c00c 2021-03-02 op imsg_flush(ibuf);
459 5e11c00c 2021-03-02 op
460 d55d6174 2021-04-01 op if (20 <= code && code < 30)
461 58ba20cd 2021-03-18 op advance_buf(req, len+1); /* skip \n too */
462 2d8fdde4 2021-04-01 op else
463 2d8fdde4 2021-04-01 op close_conn(0, 0, req);
464 723bee54 2021-03-09 op
465 5e11c00c 2021-03-02 op return;
466 5e11c00c 2021-03-02 op
467 5e11c00c 2021-03-02 op err:
468 5e11c00c 2021-03-02 op close_with_err(req, "malformed request");
469 5e11c00c 2021-03-02 op }
470 5e11c00c 2021-03-02 op
471 5e11c00c 2021-03-02 op static void
472 5e11c00c 2021-03-02 op copy_body(int fd, short ev, void *d)
473 5e11c00c 2021-03-02 op {
474 5e11c00c 2021-03-02 op struct req *req = d;
475 5e11c00c 2021-03-02 op ssize_t r;
476 5e11c00c 2021-03-02 op
477 e4304954 2021-03-14 op for (;;) {
478 72a79cfb 2021-03-13 op if (req->off != 0) {
479 72a79cfb 2021-03-13 op imsg_compose(ibuf, IMSG_BUF, req->id, 0, -1,
480 72a79cfb 2021-03-13 op req->buf, req->off);
481 72a79cfb 2021-03-13 op imsg_flush(ibuf);
482 1deaf397 2021-03-14 op req->off = 0;
483 72a79cfb 2021-03-13 op }
484 72a79cfb 2021-03-13 op
485 72a79cfb 2021-03-13 op switch (r = tls_read(req->ctx, req->buf, sizeof(req->buf))) {
486 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
487 5e11c00c 2021-03-02 op yield_r(req, copy_body, NULL);
488 5e11c00c 2021-03-02 op return;
489 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
490 5e11c00c 2021-03-02 op yield_w(req, copy_body, NULL);
491 5e11c00c 2021-03-02 op return;
492 5e11c00c 2021-03-02 op case 0:
493 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_EOF, req->id, 0, -1, NULL, 0);
494 5e11c00c 2021-03-02 op imsg_flush(ibuf);
495 5e11c00c 2021-03-02 op close_conn(0, 0, req);
496 5e11c00c 2021-03-02 op return;
497 5e11c00c 2021-03-02 op default:
498 72a79cfb 2021-03-13 op req->off = r;
499 5e11c00c 2021-03-02 op }
500 e4304954 2021-03-14 op }
501 5e11c00c 2021-03-02 op }
502 5e11c00c 2021-03-02 op
503 5e11c00c 2021-03-02 op static void
504 984245ce 2021-06-23 op handle_get_raw(struct imsg *imsg, size_t datalen)
505 5e11c00c 2021-03-02 op {
506 5e11c00c 2021-03-02 op struct req *req;
507 984245ce 2021-06-23 op struct get_req *r;
508 5e11c00c 2021-03-02 op
509 984245ce 2021-06-23 op r = imsg->data;
510 5e11c00c 2021-03-02 op
511 984245ce 2021-06-23 op if (datalen != sizeof(*r))
512 5e11c00c 2021-03-02 op die();
513 5e11c00c 2021-03-02 op
514 5e11c00c 2021-03-02 op if ((req = calloc(1, sizeof(*req))) == NULL)
515 5e11c00c 2021-03-02 op die();
516 5e11c00c 2021-03-02 op
517 5e11c00c 2021-03-02 op req->id = imsg->hdr.peerid;
518 71a2feab 2021-03-10 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
519 5e11c00c 2021-03-02 op
520 984245ce 2021-06-23 op strlcpy(req->url.host, r->host, sizeof(req->url.host));
521 984245ce 2021-06-23 op strlcpy(req->url.port, r->port, sizeof(req->url.port));
522 984245ce 2021-06-23 op strlcpy(req->buf, r->req, sizeof(req->buf));
523 5e11c00c 2021-03-02 op
524 cc4953ef 2021-03-16 op #if HAVE_ASR_RUN
525 cc4953ef 2021-03-16 op async_conn_towards(req);
526 cc4953ef 2021-03-16 op #else
527 4e600d30 2021-03-29 op blocking_conn_towards(req);
528 cc4953ef 2021-03-16 op #endif
529 5e11c00c 2021-03-02 op }
530 5e11c00c 2021-03-02 op
531 5e11c00c 2021-03-02 op static void
532 5e11c00c 2021-03-02 op handle_cert_status(struct imsg *imsg, size_t datalen)
533 5e11c00c 2021-03-02 op {
534 5e11c00c 2021-03-02 op struct req *req;
535 5e11c00c 2021-03-02 op int is_ok;
536 5e11c00c 2021-03-02 op
537 5e11c00c 2021-03-02 op req = req_by_id(imsg->hdr.peerid);
538 5e11c00c 2021-03-02 op
539 5e11c00c 2021-03-02 op if (datalen < sizeof(is_ok))
540 5e11c00c 2021-03-02 op die();
541 5e11c00c 2021-03-02 op memcpy(&is_ok, imsg->data, sizeof(is_ok));
542 5e11c00c 2021-03-02 op
543 5e11c00c 2021-03-02 op if (is_ok)
544 5e11c00c 2021-03-02 op yield_w(req, write_request, NULL);
545 5e11c00c 2021-03-02 op else
546 5e11c00c 2021-03-02 op close_conn(0, 0, req);
547 5e11c00c 2021-03-02 op }
548 5e11c00c 2021-03-02 op
549 5e11c00c 2021-03-02 op static void
550 0972d8b2 2021-03-02 op handle_proceed(struct imsg *imsg, size_t datalen)
551 0972d8b2 2021-03-02 op {
552 e4304954 2021-03-14 op yield_r(req_by_id(imsg->hdr.peerid),
553 e4304954 2021-03-14 op copy_body, NULL);
554 0972d8b2 2021-03-02 op }
555 0972d8b2 2021-03-02 op
556 0972d8b2 2021-03-02 op static void
557 5e11c00c 2021-03-02 op handle_stop(struct imsg *imsg, size_t datalen)
558 5e11c00c 2021-03-02 op {
559 5e11c00c 2021-03-02 op struct req *req;
560 5e11c00c 2021-03-02 op
561 15354eed 2021-03-10 op if ((req = req_by_id_try(imsg->hdr.peerid)) == NULL)
562 15354eed 2021-03-10 op return;
563 5e11c00c 2021-03-02 op close_conn(0, 0, req);
564 5e11c00c 2021-03-02 op }
565 5e11c00c 2021-03-02 op
566 5e11c00c 2021-03-02 op static void
567 5e11c00c 2021-03-02 op handle_quit(struct imsg *imsg, size_t datalen)
568 5e11c00c 2021-03-02 op {
569 5e11c00c 2021-03-02 op event_loopbreak();
570 5e11c00c 2021-03-02 op }
571 5e11c00c 2021-03-02 op
572 5e11c00c 2021-03-02 op static void
573 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
574 5e11c00c 2021-03-02 op {
575 5e11c00c 2021-03-02 op struct imsgbuf *ibuf = d;
576 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
577 5e11c00c 2021-03-02 op }
578 5e11c00c 2021-03-02 op
579 5e11c00c 2021-03-02 op int
580 5e11c00c 2021-03-02 op client_main(struct imsgbuf *b)
581 5e11c00c 2021-03-02 op {
582 5e11c00c 2021-03-02 op ibuf = b;
583 5e11c00c 2021-03-02 op
584 5e11c00c 2021-03-02 op TAILQ_INIT(&reqhead);
585 5e11c00c 2021-03-02 op
586 5e11c00c 2021-03-02 op if ((tlsconf = tls_config_new()) == NULL)
587 5e11c00c 2021-03-02 op die();
588 5e11c00c 2021-03-02 op tls_config_insecure_noverifycert(tlsconf);
589 d6cd2fc1 2021-03-25 op tls_config_insecure_noverifyname(tlsconf);
590 5e11c00c 2021-03-02 op
591 5e11c00c 2021-03-02 op event_init();
592 5e11c00c 2021-03-02 op
593 1304bbdd 2021-03-15 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
594 5e11c00c 2021-03-02 op event_add(&imsgev, NULL);
595 ebdcadbf 2021-03-12 op
596 ebdcadbf 2021-03-12 op sandbox_network_process();
597 5e11c00c 2021-03-02 op
598 5e11c00c 2021-03-02 op event_dispatch();
599 5e11c00c 2021-03-02 op return 0;
600 5e11c00c 2021-03-02 op }