xref: /openbsd/usr.sbin/relayd/check_tcp.c (revision 9b7c3dbb)
1 /*	$OpenBSD: check_tcp.c,v 1.51 2016/01/11 21:31:42 benno Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <sys/socket.h>
22 
23 #include <netinet/in.h>
24 
25 #include <limits.h>
26 #include <event.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <fnmatch.h>
33 #include <sha1.h>
34 #include <imsg.h>
35 
36 #include "relayd.h"
37 
38 void	tcp_write(int, short, void *);
39 void	tcp_host_up(struct ctl_tcp_event *);
40 void	tcp_close(struct ctl_tcp_event *, int);
41 void	tcp_send_req(int, short, void *);
42 void	tcp_read_buf(int, short, void *);
43 
44 int	check_http_code(struct ctl_tcp_event *);
45 int	check_http_digest(struct ctl_tcp_event *);
46 int	check_send_expect(struct ctl_tcp_event *);
47 
48 void
49 check_tcp(struct ctl_tcp_event *cte)
50 {
51 	int			 s;
52 	socklen_t		 len;
53 	struct timeval		 tv;
54 	struct linger		 lng;
55 	int			 he = HCE_TCP_SOCKET_OPTION;
56 
57 	switch (cte->host->conf.ss.ss_family) {
58 	case AF_INET:
59 		((struct sockaddr_in *)&cte->host->conf.ss)->sin_port =
60 			cte->table->conf.port;
61 		break;
62 	case AF_INET6:
63 		((struct sockaddr_in6 *)&cte->host->conf.ss)->sin6_port =
64 			cte->table->conf.port;
65 		break;
66 	}
67 
68 	len = ((struct sockaddr *)&cte->host->conf.ss)->sa_len;
69 
70 	if ((s = socket(cte->host->conf.ss.ss_family,
71 	    SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) {
72 		if (errno == EMFILE || errno == ENFILE)
73 			he = HCE_TCP_SOCKET_LIMIT;
74 		else
75 			he = HCE_TCP_SOCKET_ERROR;
76 		goto bad;
77 	}
78 
79 	cte->s = s;
80 
81 	bzero(&lng, sizeof(lng));
82 	if (setsockopt(s, SOL_SOCKET, SO_LINGER, &lng, sizeof(lng)) == -1)
83 		goto bad;
84 
85 	if (cte->host->conf.ttl > 0) {
86 		if (setsockopt(s, IPPROTO_IP, IP_TTL,
87 		    &cte->host->conf.ttl, sizeof(int)) == -1)
88 			goto bad;
89 	}
90 
91 	bcopy(&cte->table->conf.timeout, &tv, sizeof(tv));
92 	if (connect(s, (struct sockaddr *)&cte->host->conf.ss, len) == -1) {
93 		if (errno != EINPROGRESS) {
94 			he = HCE_TCP_CONNECT_FAIL;
95 			goto bad;
96 		}
97 	}
98 
99 	cte->buf = NULL;
100 	cte->host->up = HOST_UP;
101 	event_del(&cte->ev);
102 	event_set(&cte->ev, s, EV_TIMEOUT|EV_WRITE, tcp_write, cte);
103 	event_add(&cte->ev, &tv);
104 	return;
105 
106 bad:
107 	tcp_close(cte, HOST_DOWN);
108 	hce_notify_done(cte->host, he);
109 }
110 
111 void
112 tcp_write(int s, short event, void *arg)
113 {
114 	struct ctl_tcp_event	*cte = arg;
115 	int			 err;
116 	socklen_t		 len;
117 
118 	if (event == EV_TIMEOUT) {
119 		tcp_close(cte, HOST_DOWN);
120 		hce_notify_done(cte->host, HCE_TCP_CONNECT_TIMEOUT);
121 		return;
122 	}
123 
124 	len = sizeof(err);
125 	if (getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len))
126 		fatal("tcp_write: getsockopt");
127 	if (err != 0) {
128 		tcp_close(cte, HOST_DOWN);
129 		hce_notify_done(cte->host, HCE_TCP_CONNECT_FAIL);
130 		return;
131 	}
132 
133 	cte->host->up = HOST_UP;
134 	tcp_host_up(cte);
135 }
136 
137 void
138 tcp_close(struct ctl_tcp_event *cte, int status)
139 {
140 	close(cte->s);
141 	cte->s = -1;
142 	if (status != 0)
143 		cte->host->up = status;
144 	ibuf_free(cte->buf);
145 	cte->buf = NULL;
146 }
147 
148 void
149 tcp_host_up(struct ctl_tcp_event *cte)
150 {
151 	switch (cte->table->conf.check) {
152 	case CHECK_TCP:
153 		if (cte->table->conf.flags & F_TLS)
154 			break;
155 		tcp_close(cte, 0);
156 		hce_notify_done(cte->host, HCE_TCP_CONNECT_OK);
157 		return;
158 	case CHECK_HTTP_CODE:
159 		cte->validate_read = NULL;
160 		cte->validate_close = check_http_code;
161 		break;
162 	case CHECK_HTTP_DIGEST:
163 		cte->validate_read = NULL;
164 		cte->validate_close = check_http_digest;
165 		break;
166 	case CHECK_SEND_EXPECT:
167 		cte->validate_read = check_send_expect;
168 		cte->validate_close = check_send_expect;
169 		break;
170 	}
171 
172 	if (cte->table->conf.flags & F_TLS) {
173 		ssl_transaction(cte);
174 		return;
175 	}
176 
177 	if (cte->table->sendbuf != NULL) {
178 		cte->req = cte->table->sendbuf;
179 		event_again(&cte->ev, cte->s, EV_TIMEOUT|EV_WRITE, tcp_send_req,
180 		    &cte->tv_start, &cte->table->conf.timeout, cte);
181 		return;
182 	}
183 
184 	if ((cte->buf = ibuf_dynamic(SMALL_READ_BUF_SIZE, UINT_MAX)) == NULL)
185 		fatalx("tcp_host_up: cannot create dynamic buffer");
186 	event_again(&cte->ev, cte->s, EV_TIMEOUT|EV_READ, tcp_read_buf,
187 	    &cte->tv_start, &cte->table->conf.timeout, cte);
188 }
189 
190 void
191 tcp_send_req(int s, short event, void *arg)
192 {
193 	struct ctl_tcp_event	*cte = arg;
194 	int			 bs;
195 	int			 len;
196 
197 	if (event == EV_TIMEOUT) {
198 		tcp_close(cte, HOST_DOWN);
199 		hce_notify_done(cte->host, HCE_TCP_WRITE_TIMEOUT);
200 		return;
201 	}
202 	len = strlen(cte->req);
203 	do {
204 		bs = write(s, cte->req, len);
205 		if (bs == -1) {
206 			if (errno == EAGAIN || errno == EINTR)
207 				goto retry;
208 			log_warn("%s: cannot send request", __func__);
209 			tcp_close(cte, HOST_DOWN);
210 			hce_notify_done(cte->host, HCE_TCP_WRITE_FAIL);
211 			return;
212 		}
213 		cte->req += bs;
214 		len -= bs;
215 	} while (len > 0);
216 
217 	if ((cte->buf = ibuf_dynamic(SMALL_READ_BUF_SIZE, UINT_MAX)) == NULL)
218 		fatalx("tcp_send_req: cannot create dynamic buffer");
219 	event_again(&cte->ev, s, EV_TIMEOUT|EV_READ, tcp_read_buf,
220 	    &cte->tv_start, &cte->table->conf.timeout, cte);
221 	return;
222 
223  retry:
224 	event_again(&cte->ev, s, EV_TIMEOUT|EV_WRITE, tcp_send_req,
225 	    &cte->tv_start, &cte->table->conf.timeout, cte);
226 }
227 
228 void
229 tcp_read_buf(int s, short event, void *arg)
230 {
231 	ssize_t			 br;
232 	char			 rbuf[SMALL_READ_BUF_SIZE];
233 	struct ctl_tcp_event	*cte = arg;
234 
235 	if (event == EV_TIMEOUT) {
236 		tcp_close(cte, HOST_DOWN);
237 		hce_notify_done(cte->host, HCE_TCP_READ_TIMEOUT);
238 		return;
239 	}
240 
241 	bzero(rbuf, sizeof(rbuf));
242 	br = read(s, rbuf, sizeof(rbuf) - 1);
243 	switch (br) {
244 	case -1:
245 		if (errno == EAGAIN || errno == EINTR)
246 			goto retry;
247 		tcp_close(cte, HOST_DOWN);
248 		hce_notify_done(cte->host, HCE_TCP_READ_FAIL);
249 		return;
250 	case 0:
251 		cte->host->up = HOST_DOWN;
252 		(void)cte->validate_close(cte);
253 		tcp_close(cte, 0);
254 		hce_notify_done(cte->host, cte->host->he);
255 		return;
256 	default:
257 		if (ibuf_add(cte->buf, rbuf, br) == -1)
258 			fatal("tcp_read_buf: buf_add error");
259 		if (cte->validate_read != NULL) {
260 			if (cte->validate_read(cte) != 0)
261 				goto retry;
262 			tcp_close(cte, 0);
263 			hce_notify_done(cte->host, cte->host->he);
264 			return;
265 		}
266 		break; /* retry */
267 	}
268 retry:
269 	event_again(&cte->ev, s, EV_TIMEOUT|EV_READ, tcp_read_buf,
270 	    &cte->tv_start, &cte->table->conf.timeout, cte);
271 }
272 
273 int
274 check_send_expect(struct ctl_tcp_event *cte)
275 {
276 	u_char	*b;
277 
278 	/*
279 	 * ensure string is nul-terminated.
280 	 */
281 	b = ibuf_reserve(cte->buf, 1);
282 	if (b == NULL)
283 		fatal("out of memory");
284 	*b = '\0';
285 	if (fnmatch(cte->table->conf.exbuf, cte->buf->buf, 0) == 0) {
286 		cte->host->he = HCE_SEND_EXPECT_OK;
287 		cte->host->up = HOST_UP;
288 		return (0);
289 	}
290 	cte->host->he = HCE_SEND_EXPECT_FAIL;
291 	cte->host->up = HOST_UNKNOWN;
292 
293 	/*
294 	 * go back to original position.
295 	 */
296 	cte->buf->wpos--;
297 	return (1);
298 }
299 
300 int
301 check_http_code(struct ctl_tcp_event *cte)
302 {
303 	char		*head;
304 	char		 scode[4];
305 	const char	*estr;
306 	u_char		*b;
307 	int		 code;
308 	struct host	*host;
309 
310 	/*
311 	 * ensure string is nul-terminated.
312 	 */
313 	b = ibuf_reserve(cte->buf, 1);
314 	if (b == NULL)
315 		fatal("out of memory");
316 	*b = '\0';
317 
318 	head = cte->buf->buf;
319 	host = cte->host;
320 	host->he = HCE_HTTP_CODE_ERROR;
321 	host->code = 0;
322 
323 	if (strncmp(head, "HTTP/1.1 ", strlen("HTTP/1.1 ")) &&
324 	    strncmp(head, "HTTP/1.0 ", strlen("HTTP/1.0 "))) {
325 		log_debug("%s: %s failed (cannot parse HTTP version)",
326 		    __func__, host->conf.name);
327 		host->up = HOST_DOWN;
328 		return (1);
329 	}
330 	head += strlen("HTTP/1.1 ");
331 	if (strlen(head) < 5) /* code + \r\n */ {
332 		host->up = HOST_DOWN;
333 		return (1);
334 	}
335 	(void)strlcpy(scode, head, sizeof(scode));
336 	code = strtonum(scode, 100, 999, &estr);
337 	if (estr != NULL) {
338 		log_debug("%s: %s failed (cannot parse HTTP code)",
339 		    __func__, host->conf.name);
340 		host->up = HOST_DOWN;
341 		return (1);
342 	}
343 	if (code != cte->table->conf.retcode) {
344 		log_debug("%s: %s failed (invalid HTTP code %d returned)",
345 		    __func__, host->conf.name, code);
346 		host->he = HCE_HTTP_CODE_FAIL;
347 		host->up = HOST_DOWN;
348 		host->code = code;
349 	} else {
350 		host->he = HCE_HTTP_CODE_OK;
351 		host->up = HOST_UP;
352 	}
353 	return (!(host->up == HOST_UP));
354 }
355 
356 int
357 check_http_digest(struct ctl_tcp_event *cte)
358 {
359 	char		*head;
360 	u_char		*b;
361 	char		 digest[SHA1_DIGEST_STRING_LENGTH];
362 	struct host	*host;
363 
364 	/*
365 	 * ensure string is nul-terminated.
366 	 */
367 	b = ibuf_reserve(cte->buf, 1);
368 	if (b == NULL)
369 		fatal("out of memory");
370 	*b = '\0';
371 
372 	head = cte->buf->buf;
373 	host = cte->host;
374 	host->he = HCE_HTTP_DIGEST_ERROR;
375 
376 	if ((head = strstr(head, "\r\n\r\n")) == NULL) {
377 		log_debug("%s: %s failed (no end of headers)",
378 		    __func__, host->conf.name);
379 		host->up = HOST_DOWN;
380 		return (1);
381 	}
382 	head += strlen("\r\n\r\n");
383 
384 	digeststr(cte->table->conf.digest_type, head, strlen(head), digest);
385 
386 	if (strcmp(cte->table->conf.digest, digest)) {
387 		log_warnx("%s: %s failed (wrong digest)",
388 		    __func__, host->conf.name);
389 		host->he = HCE_HTTP_DIGEST_FAIL;
390 		host->up = HOST_DOWN;
391 	} else {
392 		host->he = HCE_HTTP_DIGEST_OK;
393 		host->up = HOST_UP;
394 	}
395 	return (!(host->up == HOST_UP));
396 }
397