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 <event.h>
36 5e11c00c 2021-03-02 op #include <netdb.h>
37 5e11c00c 2021-03-02 op #include <stdarg.h>
38 5e11c00c 2021-03-02 op #include <stdio.h>
39 5e11c00c 2021-03-02 op #include <stdlib.h>
40 5e11c00c 2021-03-02 op #include <string.h>
41 5e11c00c 2021-03-02 op #include <tls.h>
42 5e11c00c 2021-03-02 op #include <unistd.h>
43 5e11c00c 2021-03-02 op
44 5e11c00c 2021-03-02 op static struct tls_config *tlsconf;
45 5e11c00c 2021-03-02 op static struct imsgbuf *ibuf;
46 5e11c00c 2021-03-02 op
47 5e11c00c 2021-03-02 op struct req;
48 5e11c00c 2021-03-02 op
49 5e11c00c 2021-03-02 op static void die(void) __attribute__((__noreturn__));
50 5e11c00c 2021-03-02 op static char *xasprintf(const char*, ...);
51 5e11c00c 2021-03-02 op static int conn_towards(struct url*, char**);
52 5e11c00c 2021-03-02 op
53 5e11c00c 2021-03-02 op static void close_with_err(struct req*, const char *err);
54 5e11c00c 2021-03-02 op static struct req *req_by_id(uint32_t);
55 15354eed 2021-03-10 op static struct req *req_by_id_try(uint32_t);
56 5e11c00c 2021-03-02 op
57 5e11c00c 2021-03-02 op static void do_handshake(int, short, void*);
58 5e11c00c 2021-03-02 op static void write_request(int, short, void*);
59 5e11c00c 2021-03-02 op static void read_reply(int, short, void*);
60 5e11c00c 2021-03-02 op static void parse_reply(struct req*);
61 5e11c00c 2021-03-02 op static void copy_body(int, short, void*);
62 5e11c00c 2021-03-02 op
63 5e11c00c 2021-03-02 op static void check_special_page(struct req*, const char*);
64 5e11c00c 2021-03-02 op
65 5e11c00c 2021-03-02 op static void handle_get(struct imsg*, size_t);
66 5e11c00c 2021-03-02 op static void handle_cert_status(struct imsg*, size_t);
67 0972d8b2 2021-03-02 op static void handle_proceed(struct imsg*, size_t);
68 5e11c00c 2021-03-02 op static void handle_stop(struct imsg*, size_t);
69 5e11c00c 2021-03-02 op static void handle_quit(struct imsg*, size_t);
70 5e11c00c 2021-03-02 op
71 723bee54 2021-03-09 op /* TODO: making this customizable */
72 723bee54 2021-03-09 op struct timeval timeout_for_handshake = { 5, 0 };
73 723bee54 2021-03-09 op
74 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
75 5e11c00c 2021-03-02 op [IMSG_GET] = handle_get,
76 5e11c00c 2021-03-02 op [IMSG_CERT_STATUS] = handle_cert_status,
77 0972d8b2 2021-03-02 op [IMSG_PROCEED] = handle_proceed,
78 5e11c00c 2021-03-02 op [IMSG_STOP] = handle_stop,
79 5e11c00c 2021-03-02 op [IMSG_QUIT] = handle_quit,
80 5e11c00c 2021-03-02 op };
81 5e11c00c 2021-03-02 op
82 5e11c00c 2021-03-02 op typedef void (*statefn)(int, short, void*);
83 5e11c00c 2021-03-02 op
84 5e11c00c 2021-03-02 op TAILQ_HEAD(, req) reqhead;
85 5e11c00c 2021-03-02 op /* a pending request */
86 5e11c00c 2021-03-02 op struct req {
87 5e11c00c 2021-03-02 op struct event ev;
88 5e11c00c 2021-03-02 op struct url url;
89 5e11c00c 2021-03-02 op uint32_t id;
90 5e11c00c 2021-03-02 op int fd;
91 5e11c00c 2021-03-02 op struct tls *ctx;
92 5e11c00c 2021-03-02 op char buf[1024];
93 5e11c00c 2021-03-02 op size_t off;
94 5e11c00c 2021-03-02 op TAILQ_ENTRY(req) reqs;
95 5e11c00c 2021-03-02 op };
96 5e11c00c 2021-03-02 op
97 5e11c00c 2021-03-02 op static inline void
98 5e11c00c 2021-03-02 op yield_r(struct req *req, statefn fn, struct timeval *tv)
99 5e11c00c 2021-03-02 op {
100 5e11c00c 2021-03-02 op event_once(req->fd, EV_READ, fn, req, tv);
101 5e11c00c 2021-03-02 op }
102 5e11c00c 2021-03-02 op
103 5e11c00c 2021-03-02 op static inline void
104 5e11c00c 2021-03-02 op yield_w(struct req *req, statefn fn, struct timeval *tv)
105 5e11c00c 2021-03-02 op {
106 5e11c00c 2021-03-02 op event_once(req->fd, EV_WRITE, fn, req, tv);
107 5e11c00c 2021-03-02 op }
108 5e11c00c 2021-03-02 op
109 5e11c00c 2021-03-02 op static inline void
110 5e11c00c 2021-03-02 op advance_buf(struct req *req, size_t len)
111 5e11c00c 2021-03-02 op {
112 5e11c00c 2021-03-02 op assert(len <= req->off);
113 5e11c00c 2021-03-02 op
114 5e11c00c 2021-03-02 op req->off -= len;
115 5e11c00c 2021-03-02 op memmove(req->buf, req->buf + len, req->off);
116 5e11c00c 2021-03-02 op }
117 5e11c00c 2021-03-02 op
118 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
119 5e11c00c 2021-03-02 op die(void)
120 5e11c00c 2021-03-02 op {
121 5e11c00c 2021-03-02 op abort(); /* TODO */
122 5e11c00c 2021-03-02 op }
123 5e11c00c 2021-03-02 op
124 5e11c00c 2021-03-02 op static char *
125 5e11c00c 2021-03-02 op xasprintf(const char *fmt, ...)
126 5e11c00c 2021-03-02 op {
127 5e11c00c 2021-03-02 op va_list ap;
128 5e11c00c 2021-03-02 op char *s;
129 5e11c00c 2021-03-02 op
130 5e11c00c 2021-03-02 op va_start(ap, fmt);
131 5e11c00c 2021-03-02 op if (vasprintf(&s, fmt, ap) == -1)
132 5e11c00c 2021-03-02 op s = NULL;
133 5e11c00c 2021-03-02 op va_end(ap);
134 5e11c00c 2021-03-02 op
135 5e11c00c 2021-03-02 op return s;
136 5e11c00c 2021-03-02 op }
137 5e11c00c 2021-03-02 op
138 5e11c00c 2021-03-02 op static int
139 5e11c00c 2021-03-02 op conn_towards(struct url *url, char **err)
140 5e11c00c 2021-03-02 op {
141 5e11c00c 2021-03-02 op struct addrinfo hints, *servinfo, *p;
142 5e11c00c 2021-03-02 op int status, sock;
143 5e11c00c 2021-03-02 op const char *proto = "1965";
144 5e11c00c 2021-03-02 op
145 5e11c00c 2021-03-02 op *err = NULL;
146 5e11c00c 2021-03-02 op
147 5e11c00c 2021-03-02 op if (*url->port != '\0')
148 5e11c00c 2021-03-02 op proto = url->port;
149 5e11c00c 2021-03-02 op
150 5e11c00c 2021-03-02 op memset(&hints, 0, sizeof(hints));
151 5e11c00c 2021-03-02 op hints.ai_family = AF_UNSPEC;
152 5e11c00c 2021-03-02 op hints.ai_socktype = SOCK_STREAM;
153 5e11c00c 2021-03-02 op
154 5e11c00c 2021-03-02 op if ((status = getaddrinfo(url->host, proto, &hints, &servinfo))) {
155 5e11c00c 2021-03-02 op *err = xasprintf("failed to resolve %s: %s",
156 5e11c00c 2021-03-02 op url->host, gai_strerror(status));
157 5e11c00c 2021-03-02 op return -1;
158 5e11c00c 2021-03-02 op }
159 5e11c00c 2021-03-02 op
160 5e11c00c 2021-03-02 op sock = -1;
161 5e11c00c 2021-03-02 op for (p = servinfo; p != NULL; p = p->ai_next) {
162 5e11c00c 2021-03-02 op if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
163 5e11c00c 2021-03-02 op continue;
164 5e11c00c 2021-03-02 op if (connect(sock, p->ai_addr, p->ai_addrlen) != -1)
165 5e11c00c 2021-03-02 op break;
166 5e11c00c 2021-03-02 op close(sock);
167 5e11c00c 2021-03-02 op }
168 5e11c00c 2021-03-02 op
169 5e11c00c 2021-03-02 op if (sock == -1)
170 5e11c00c 2021-03-02 op *err = xasprintf("couldn't connect to %s", url->host);
171 5e11c00c 2021-03-02 op else
172 5e11c00c 2021-03-02 op mark_nonblock(sock);
173 5e11c00c 2021-03-02 op
174 5e11c00c 2021-03-02 op freeaddrinfo(servinfo);
175 5e11c00c 2021-03-02 op return sock;
176 5e11c00c 2021-03-02 op }
177 5e11c00c 2021-03-02 op
178 5e11c00c 2021-03-02 op static struct req *
179 5e11c00c 2021-03-02 op req_by_id(uint32_t id)
180 15354eed 2021-03-10 op {
181 15354eed 2021-03-10 op struct req *r;
182 15354eed 2021-03-10 op
183 15354eed 2021-03-10 op if ((r = req_by_id_try(id)) == NULL)
184 15354eed 2021-03-10 op die();
185 15354eed 2021-03-10 op return r;
186 15354eed 2021-03-10 op }
187 15354eed 2021-03-10 op
188 15354eed 2021-03-10 op static struct req *
189 15354eed 2021-03-10 op req_by_id_try(uint32_t id)
190 5e11c00c 2021-03-02 op {
191 5e11c00c 2021-03-02 op struct req *r;
192 5e11c00c 2021-03-02 op
193 5e11c00c 2021-03-02 op TAILQ_FOREACH(r, &reqhead, reqs) {
194 5e11c00c 2021-03-02 op if (r->id == id)
195 5e11c00c 2021-03-02 op return r;
196 5e11c00c 2021-03-02 op }
197 5e11c00c 2021-03-02 op
198 15354eed 2021-03-10 op return NULL;
199 5e11c00c 2021-03-02 op }
200 5e11c00c 2021-03-02 op
201 5e11c00c 2021-03-02 op static void
202 5e11c00c 2021-03-02 op close_conn(int fd, short ev, void *d)
203 5e11c00c 2021-03-02 op {
204 5e11c00c 2021-03-02 op struct req *req = d;
205 5e11c00c 2021-03-02 op
206 5e11c00c 2021-03-02 op if (req->ctx != NULL) {
207 5e11c00c 2021-03-02 op switch (tls_close(req->ctx)) {
208 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
209 5e11c00c 2021-03-02 op yield_r(req, close_conn, NULL);
210 5e11c00c 2021-03-02 op return;
211 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
212 5e11c00c 2021-03-02 op yield_w(req, close_conn, NULL);
213 5e11c00c 2021-03-02 op return;
214 5e11c00c 2021-03-02 op }
215 5e11c00c 2021-03-02 op
216 5e11c00c 2021-03-02 op tls_free(req->ctx);
217 5e11c00c 2021-03-02 op }
218 5e11c00c 2021-03-02 op
219 5e11c00c 2021-03-02 op TAILQ_REMOVE(&reqhead, req, reqs);
220 5e11c00c 2021-03-02 op if (req->fd != -1)
221 5e11c00c 2021-03-02 op close(req->fd);
222 5e11c00c 2021-03-02 op free(req);
223 5e11c00c 2021-03-02 op }
224 5e11c00c 2021-03-02 op
225 5e11c00c 2021-03-02 op static void
226 5e11c00c 2021-03-02 op close_with_err(struct req *req, const char *err)
227 5e11c00c 2021-03-02 op {
228 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_ERR, req->id, 0, -1, err, strlen(err)+1);
229 5e11c00c 2021-03-02 op imsg_flush(ibuf);
230 5e11c00c 2021-03-02 op close_conn(0, 0, req);
231 5e11c00c 2021-03-02 op }
232 5e11c00c 2021-03-02 op
233 5e11c00c 2021-03-02 op static void
234 5e11c00c 2021-03-02 op do_handshake(int fd, short ev, void *d)
235 5e11c00c 2021-03-02 op {
236 5e11c00c 2021-03-02 op struct req *req = d;
237 5e11c00c 2021-03-02 op const char *hash;
238 5e11c00c 2021-03-02 op
239 723bee54 2021-03-09 op if (ev == EV_TIMEOUT) {
240 723bee54 2021-03-09 op close_with_err(req, "Timeout loading page");
241 723bee54 2021-03-09 op return;
242 723bee54 2021-03-09 op }
243 723bee54 2021-03-09 op
244 5e11c00c 2021-03-02 op switch (tls_handshake(req->ctx)) {
245 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
246 5e11c00c 2021-03-02 op yield_r(req, do_handshake, NULL);
247 5e11c00c 2021-03-02 op return;
248 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
249 5e11c00c 2021-03-02 op yield_w(req, do_handshake, NULL);
250 5e11c00c 2021-03-02 op return;
251 5e11c00c 2021-03-02 op }
252 5e11c00c 2021-03-02 op
253 5e11c00c 2021-03-02 op hash = tls_peer_cert_hash(req->ctx);
254 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_CHECK_CERT, req->id, 0, -1, hash, strlen(hash)+1);
255 5e11c00c 2021-03-02 op imsg_flush(ibuf);
256 5e11c00c 2021-03-02 op }
257 5e11c00c 2021-03-02 op
258 5e11c00c 2021-03-02 op static void
259 5e11c00c 2021-03-02 op write_request(int fd, short ev, void *d)
260 5e11c00c 2021-03-02 op {
261 5e11c00c 2021-03-02 op struct req *req = d;
262 5e11c00c 2021-03-02 op ssize_t r;
263 5e11c00c 2021-03-02 op size_t len;
264 5e11c00c 2021-03-02 op char buf[1024], *err;
265 5e11c00c 2021-03-02 op
266 5e11c00c 2021-03-02 op strlcpy(buf, "gemini://", sizeof(buf));
267 5e11c00c 2021-03-02 op strlcat(buf, req->url.host, sizeof(buf));
268 5e11c00c 2021-03-02 op strlcat(buf, "/", sizeof(buf));
269 5e11c00c 2021-03-02 op strlcat(buf, req->url.path, sizeof(buf));
270 5e11c00c 2021-03-02 op
271 5e11c00c 2021-03-02 op if (req->url.query[0] != '\0') {
272 5e11c00c 2021-03-02 op strlcat(buf, "?", sizeof(buf));
273 5e11c00c 2021-03-02 op strlcat(buf, req->url.query, sizeof(buf));
274 5e11c00c 2021-03-02 op }
275 5e11c00c 2021-03-02 op
276 5e11c00c 2021-03-02 op len = strlcat(buf, "\r\n", sizeof(buf));
277 5e11c00c 2021-03-02 op
278 5e11c00c 2021-03-02 op assert(len <= sizeof(buf));
279 5e11c00c 2021-03-02 op
280 5e11c00c 2021-03-02 op switch (r = tls_write(req->ctx, buf, len)) {
281 5e11c00c 2021-03-02 op case -1:
282 5e11c00c 2021-03-02 op err = xasprintf("tls_write: %s", tls_error(req->ctx));
283 5e11c00c 2021-03-02 op close_with_err(req, err);
284 5e11c00c 2021-03-02 op free(err);
285 5e11c00c 2021-03-02 op break;
286 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
287 5e11c00c 2021-03-02 op yield_r(req, write_request, NULL);
288 5e11c00c 2021-03-02 op break;
289 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
290 5e11c00c 2021-03-02 op yield_w(req, write_request, NULL);
291 5e11c00c 2021-03-02 op break;
292 5e11c00c 2021-03-02 op default:
293 5e11c00c 2021-03-02 op /* assume r == len */
294 5e11c00c 2021-03-02 op (void)r;
295 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
296 5e11c00c 2021-03-02 op break;
297 5e11c00c 2021-03-02 op }
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 read_reply(int fd, short ev, void *d)
302 5e11c00c 2021-03-02 op {
303 5e11c00c 2021-03-02 op struct req *req = d;
304 5e11c00c 2021-03-02 op size_t len;
305 5e11c00c 2021-03-02 op ssize_t r;
306 5e11c00c 2021-03-02 op char *buf, *e;
307 5e11c00c 2021-03-02 op
308 5e11c00c 2021-03-02 op buf = req->buf + req->off;
309 5e11c00c 2021-03-02 op len = sizeof(req->buf) - req->off;
310 5e11c00c 2021-03-02 op
311 5e11c00c 2021-03-02 op switch (r = tls_read(req->ctx, buf, len)) {
312 5e11c00c 2021-03-02 op case -1:
313 5e11c00c 2021-03-02 op e = xasprintf("tls_read: %s", tls_error(req->ctx));
314 5e11c00c 2021-03-02 op close_with_err(req, e);
315 5e11c00c 2021-03-02 op free(e);
316 5e11c00c 2021-03-02 op break;
317 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
318 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
319 5e11c00c 2021-03-02 op break;
320 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
321 5e11c00c 2021-03-02 op yield_w(req, read_reply, NULL);
322 5e11c00c 2021-03-02 op break;
323 5e11c00c 2021-03-02 op default:
324 5e11c00c 2021-03-02 op req->off += r;
325 5e11c00c 2021-03-02 op
326 5e11c00c 2021-03-02 op /* TODO: really watch for \r\n not \n alone */
327 5e11c00c 2021-03-02 op if ((e = telescope_strnchr(req->buf, '\n', req->off)) != NULL)
328 5e11c00c 2021-03-02 op parse_reply(req);
329 5e11c00c 2021-03-02 op else if (req->off == sizeof(req->buf))
330 5e11c00c 2021-03-02 op close_with_err(req, "invalid response");
331 5e11c00c 2021-03-02 op else
332 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
333 5e11c00c 2021-03-02 op break;
334 5e11c00c 2021-03-02 op }
335 5e11c00c 2021-03-02 op }
336 5e11c00c 2021-03-02 op
337 5e11c00c 2021-03-02 op static void
338 5e11c00c 2021-03-02 op parse_reply(struct req *req)
339 5e11c00c 2021-03-02 op {
340 5e11c00c 2021-03-02 op int code;
341 5e11c00c 2021-03-02 op char *e;
342 5e11c00c 2021-03-02 op size_t len;
343 5e11c00c 2021-03-02 op
344 5e11c00c 2021-03-02 op if (req->off < 4)
345 5e11c00c 2021-03-02 op goto err;
346 5e11c00c 2021-03-02 op
347 5e11c00c 2021-03-02 op if (!isdigit(req->buf[0]) || !isdigit(req->buf[1]))
348 5e11c00c 2021-03-02 op goto err;
349 5e11c00c 2021-03-02 op
350 5e11c00c 2021-03-02 op code = (req->buf[0] - '0')*10 + (req->buf[1] - '0');
351 5e11c00c 2021-03-02 op
352 5e11c00c 2021-03-02 op if (!isspace(req->buf[2]))
353 5e11c00c 2021-03-02 op goto err;
354 5e11c00c 2021-03-02 op
355 5e11c00c 2021-03-02 op advance_buf(req, 3);
356 5e11c00c 2021-03-02 op if ((e = telescope_strnchr(req->buf, '\r', req->off)) == NULL)
357 5e11c00c 2021-03-02 op goto err;
358 5e11c00c 2021-03-02 op
359 5e11c00c 2021-03-02 op *e = '\0';
360 5e11c00c 2021-03-02 op e++;
361 5e11c00c 2021-03-02 op len = e - req->buf;
362 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_GOT_CODE, req->id, 0, -1, &code, sizeof(code));
363 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_GOT_META, req->id, 0, -1,
364 5e11c00c 2021-03-02 op req->buf, len);
365 5e11c00c 2021-03-02 op imsg_flush(ibuf);
366 5e11c00c 2021-03-02 op
367 723bee54 2021-03-09 op if (code != 20)
368 723bee54 2021-03-09 op close_conn(0, 0, req);
369 72a79cfb 2021-03-13 op advance_buf(req, len+1); /* skip \n too */
370 723bee54 2021-03-09 op
371 5e11c00c 2021-03-02 op return;
372 5e11c00c 2021-03-02 op
373 5e11c00c 2021-03-02 op err:
374 5e11c00c 2021-03-02 op close_with_err(req, "malformed request");
375 5e11c00c 2021-03-02 op }
376 5e11c00c 2021-03-02 op
377 5e11c00c 2021-03-02 op static void
378 5e11c00c 2021-03-02 op copy_body(int fd, short ev, void *d)
379 5e11c00c 2021-03-02 op {
380 5e11c00c 2021-03-02 op struct req *req = d;
381 5e11c00c 2021-03-02 op ssize_t r;
382 5e11c00c 2021-03-02 op
383 72a79cfb 2021-03-13 op do {
384 72a79cfb 2021-03-13 op if (req->off != 0) {
385 72a79cfb 2021-03-13 op imsg_compose(ibuf, IMSG_BUF, req->id, 0, -1,
386 72a79cfb 2021-03-13 op req->buf, req->off);
387 72a79cfb 2021-03-13 op imsg_flush(ibuf);
388 72a79cfb 2021-03-13 op }
389 72a79cfb 2021-03-13 op
390 72a79cfb 2021-03-13 op switch (r = tls_read(req->ctx, req->buf, sizeof(req->buf))) {
391 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
392 5e11c00c 2021-03-02 op yield_r(req, copy_body, NULL);
393 5e11c00c 2021-03-02 op return;
394 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
395 5e11c00c 2021-03-02 op yield_w(req, copy_body, NULL);
396 5e11c00c 2021-03-02 op return;
397 5e11c00c 2021-03-02 op case 0:
398 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_EOF, req->id, 0, -1, NULL, 0);
399 5e11c00c 2021-03-02 op imsg_flush(ibuf);
400 5e11c00c 2021-03-02 op close_conn(0, 0, req);
401 5e11c00c 2021-03-02 op return;
402 5e11c00c 2021-03-02 op default:
403 72a79cfb 2021-03-13 op req->off = r;
404 5e11c00c 2021-03-02 op }
405 72a79cfb 2021-03-13 op } while(1);
406 5e11c00c 2021-03-02 op }
407 5e11c00c 2021-03-02 op
408 5e11c00c 2021-03-02 op static void
409 5e11c00c 2021-03-02 op handle_get(struct imsg *imsg, size_t datalen)
410 5e11c00c 2021-03-02 op {
411 5e11c00c 2021-03-02 op struct req *req;
412 5e11c00c 2021-03-02 op const char *e;
413 5e11c00c 2021-03-02 op char *data, *err = NULL;
414 5e11c00c 2021-03-02 op
415 5e11c00c 2021-03-02 op data = imsg->data;
416 5e11c00c 2021-03-02 op
417 5e11c00c 2021-03-02 op if (data[datalen-1] != '\0')
418 5e11c00c 2021-03-02 op die();
419 5e11c00c 2021-03-02 op
420 5e11c00c 2021-03-02 op if ((req = calloc(1, sizeof(*req))) == NULL)
421 5e11c00c 2021-03-02 op die();
422 5e11c00c 2021-03-02 op
423 5e11c00c 2021-03-02 op req->id = imsg->hdr.peerid;
424 71a2feab 2021-03-10 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
425 5e11c00c 2021-03-02 op
426 5e11c00c 2021-03-02 op if (!url_parse(imsg->data, &req->url, &e)) {
427 5e11c00c 2021-03-02 op fprintf(stderr, "failed to parse url: %s\n", e);
428 5e11c00c 2021-03-02 op close_with_err(req, e);
429 5e11c00c 2021-03-02 op return;
430 5e11c00c 2021-03-02 op }
431 5e11c00c 2021-03-02 op
432 5e11c00c 2021-03-02 op if ((req->fd = conn_towards(&req->url, &err)) == -1)
433 5e11c00c 2021-03-02 op goto err;
434 5e11c00c 2021-03-02 op if ((req->ctx = tls_client()) == NULL)
435 5e11c00c 2021-03-02 op goto err;
436 5e11c00c 2021-03-02 op if (tls_configure(req->ctx, tlsconf) == -1) {
437 5e11c00c 2021-03-02 op err = xasprintf("tls_configure: %s", tls_error(req->ctx));
438 5e11c00c 2021-03-02 op goto err;
439 5e11c00c 2021-03-02 op }
440 5e11c00c 2021-03-02 op if (tls_connect_socket(req->ctx, req->fd, req->url.host) == -1) {
441 5e11c00c 2021-03-02 op err = xasprintf("tls_connect_socket: %s", tls_error(req->ctx));
442 5e11c00c 2021-03-02 op goto err;
443 5e11c00c 2021-03-02 op }
444 5e11c00c 2021-03-02 op
445 723bee54 2021-03-09 op yield_w(req, do_handshake, &timeout_for_handshake);
446 5e11c00c 2021-03-02 op return;
447 5e11c00c 2021-03-02 op
448 5e11c00c 2021-03-02 op err:
449 5e11c00c 2021-03-02 op close_with_err(req, err);
450 5e11c00c 2021-03-02 op free(err);
451 5e11c00c 2021-03-02 op }
452 5e11c00c 2021-03-02 op
453 5e11c00c 2021-03-02 op static void
454 5e11c00c 2021-03-02 op handle_cert_status(struct imsg *imsg, size_t datalen)
455 5e11c00c 2021-03-02 op {
456 5e11c00c 2021-03-02 op struct req *req;
457 5e11c00c 2021-03-02 op int is_ok;
458 5e11c00c 2021-03-02 op
459 5e11c00c 2021-03-02 op req = req_by_id(imsg->hdr.peerid);
460 5e11c00c 2021-03-02 op
461 5e11c00c 2021-03-02 op if (datalen < sizeof(is_ok))
462 5e11c00c 2021-03-02 op die();
463 5e11c00c 2021-03-02 op memcpy(&is_ok, imsg->data, sizeof(is_ok));
464 5e11c00c 2021-03-02 op
465 5e11c00c 2021-03-02 op if (is_ok)
466 5e11c00c 2021-03-02 op yield_w(req, write_request, NULL);
467 5e11c00c 2021-03-02 op else
468 5e11c00c 2021-03-02 op close_conn(0, 0, req);
469 5e11c00c 2021-03-02 op }
470 5e11c00c 2021-03-02 op
471 5e11c00c 2021-03-02 op static void
472 0972d8b2 2021-03-02 op handle_proceed(struct imsg *imsg, size_t datalen)
473 0972d8b2 2021-03-02 op {
474 0972d8b2 2021-03-02 op struct req *req;
475 0972d8b2 2021-03-02 op
476 0972d8b2 2021-03-02 op req = req_by_id(imsg->hdr.peerid);
477 0972d8b2 2021-03-02 op yield_r(req, copy_body, NULL);
478 0972d8b2 2021-03-02 op }
479 0972d8b2 2021-03-02 op
480 0972d8b2 2021-03-02 op static void
481 5e11c00c 2021-03-02 op handle_stop(struct imsg *imsg, size_t datalen)
482 5e11c00c 2021-03-02 op {
483 5e11c00c 2021-03-02 op struct req *req;
484 5e11c00c 2021-03-02 op
485 15354eed 2021-03-10 op if ((req = req_by_id_try(imsg->hdr.peerid)) == NULL)
486 15354eed 2021-03-10 op return;
487 5e11c00c 2021-03-02 op close_conn(0, 0, req);
488 5e11c00c 2021-03-02 op }
489 5e11c00c 2021-03-02 op
490 5e11c00c 2021-03-02 op static void
491 5e11c00c 2021-03-02 op handle_quit(struct imsg *imsg, size_t datalen)
492 5e11c00c 2021-03-02 op {
493 5e11c00c 2021-03-02 op event_loopbreak();
494 5e11c00c 2021-03-02 op }
495 5e11c00c 2021-03-02 op
496 5e11c00c 2021-03-02 op static void
497 5e11c00c 2021-03-02 op dispatch_imsg(int fd, short ev, void *d)
498 5e11c00c 2021-03-02 op {
499 5e11c00c 2021-03-02 op struct imsgbuf *ibuf = d;
500 5e11c00c 2021-03-02 op struct imsg imsg;
501 5e11c00c 2021-03-02 op ssize_t n;
502 5e11c00c 2021-03-02 op size_t datalen;
503 5e11c00c 2021-03-02 op
504 5e11c00c 2021-03-02 op if ((n = imsg_read(ibuf)) == -1) {
505 5e11c00c 2021-03-02 op if (errno == EAGAIN || errno == EWOULDBLOCK)
506 5e11c00c 2021-03-02 op return;
507 723bee54 2021-03-09 op _exit(1);
508 5e11c00c 2021-03-02 op }
509 5e11c00c 2021-03-02 op
510 5e11c00c 2021-03-02 op if (n == 0)
511 723bee54 2021-03-09 op _exit(1);
512 5e11c00c 2021-03-02 op
513 5e11c00c 2021-03-02 op for (;;) {
514 5e11c00c 2021-03-02 op if ((n = imsg_get(ibuf, &imsg)) == -1)
515 723bee54 2021-03-09 op _exit(1);
516 5e11c00c 2021-03-02 op if (n == 0)
517 5e11c00c 2021-03-02 op return;
518 5e11c00c 2021-03-02 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
519 5e11c00c 2021-03-02 op handlers[imsg.hdr.type](&imsg, datalen);
520 5e11c00c 2021-03-02 op imsg_free(&imsg);
521 5e11c00c 2021-03-02 op }
522 5e11c00c 2021-03-02 op }
523 5e11c00c 2021-03-02 op
524 5e11c00c 2021-03-02 op int
525 5e11c00c 2021-03-02 op client_main(struct imsgbuf *b)
526 5e11c00c 2021-03-02 op {
527 5e11c00c 2021-03-02 op ibuf = b;
528 5e11c00c 2021-03-02 op
529 5e11c00c 2021-03-02 op TAILQ_INIT(&reqhead);
530 5e11c00c 2021-03-02 op
531 5e11c00c 2021-03-02 op if ((tlsconf = tls_config_new()) == NULL)
532 5e11c00c 2021-03-02 op die();
533 5e11c00c 2021-03-02 op tls_config_insecure_noverifycert(tlsconf);
534 5e11c00c 2021-03-02 op
535 5e11c00c 2021-03-02 op event_init();
536 5e11c00c 2021-03-02 op
537 5e11c00c 2021-03-02 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
538 5e11c00c 2021-03-02 op event_add(&imsgev, NULL);
539 ebdcadbf 2021-03-12 op
540 ebdcadbf 2021-03-12 op sandbox_network_process();
541 5e11c00c 2021-03-02 op
542 5e11c00c 2021-03-02 op event_dispatch();
543 5e11c00c 2021-03-02 op return 0;
544 5e11c00c 2021-03-02 op }