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