1 /* $OpenBSD: check_tcp.c,v 1.56 2018/04/14 20:42:41 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 switch (cte->host->conf.ss.ss_family) { 87 case AF_INET: 88 if (setsockopt(s, IPPROTO_IP, IP_TTL, 89 &cte->host->conf.ttl, sizeof(int)) == -1) 90 goto bad; 91 break; 92 case AF_INET6: 93 if (setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 94 &cte->host->conf.ttl, sizeof(int)) == -1) 95 goto bad; 96 break; 97 } 98 99 bcopy(&cte->table->conf.timeout, &tv, sizeof(tv)); 100 if (connect(s, (struct sockaddr *)&cte->host->conf.ss, len) == -1) { 101 if (errno != EINPROGRESS) { 102 he = HCE_TCP_CONNECT_FAIL; 103 goto bad; 104 } 105 } 106 107 cte->buf = NULL; 108 cte->host->up = HOST_UP; 109 event_del(&cte->ev); 110 event_set(&cte->ev, s, EV_TIMEOUT|EV_WRITE, tcp_write, cte); 111 event_add(&cte->ev, &tv); 112 return; 113 114 bad: 115 tcp_close(cte, HOST_DOWN); 116 hce_notify_done(cte->host, he); 117 } 118 119 void 120 tcp_write(int s, short event, void *arg) 121 { 122 struct ctl_tcp_event *cte = arg; 123 int err; 124 socklen_t len; 125 126 if (event == EV_TIMEOUT) { 127 tcp_close(cte, HOST_DOWN); 128 hce_notify_done(cte->host, HCE_TCP_CONNECT_TIMEOUT); 129 return; 130 } 131 132 len = sizeof(err); 133 if (getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len)) 134 fatal("%s: getsockopt", __func__); 135 if (err != 0) { 136 tcp_close(cte, HOST_DOWN); 137 hce_notify_done(cte->host, HCE_TCP_CONNECT_FAIL); 138 return; 139 } 140 141 cte->host->up = HOST_UP; 142 tcp_host_up(cte); 143 } 144 145 void 146 tcp_close(struct ctl_tcp_event *cte, int status) 147 { 148 close(cte->s); 149 cte->s = -1; 150 if (status != 0) 151 cte->host->up = status; 152 ibuf_free(cte->buf); 153 cte->buf = NULL; 154 } 155 156 void 157 tcp_host_up(struct ctl_tcp_event *cte) 158 { 159 switch (cte->table->conf.check) { 160 case CHECK_TCP: 161 if (cte->table->conf.flags & F_TLS) 162 break; 163 tcp_close(cte, 0); 164 hce_notify_done(cte->host, HCE_TCP_CONNECT_OK); 165 return; 166 case CHECK_HTTP_CODE: 167 cte->validate_read = NULL; 168 cte->validate_close = check_http_code; 169 break; 170 case CHECK_HTTP_DIGEST: 171 cte->validate_read = NULL; 172 cte->validate_close = check_http_digest; 173 break; 174 case CHECK_SEND_EXPECT: 175 cte->validate_read = check_send_expect; 176 cte->validate_close = check_send_expect; 177 break; 178 } 179 180 if (cte->table->conf.flags & F_TLS) { 181 check_tls(cte); 182 return; 183 } 184 185 if (cte->table->sendbuf != NULL) { 186 cte->req = cte->table->sendbuf; 187 event_again(&cte->ev, cte->s, EV_TIMEOUT|EV_WRITE, tcp_send_req, 188 &cte->tv_start, &cte->table->conf.timeout, cte); 189 return; 190 } 191 192 if ((cte->buf = ibuf_dynamic(SMALL_READ_BUF_SIZE, UINT_MAX)) == NULL) 193 fatalx("%s: cannot create dynamic buffer", __func__); 194 event_again(&cte->ev, cte->s, EV_TIMEOUT|EV_READ, tcp_read_buf, 195 &cte->tv_start, &cte->table->conf.timeout, cte); 196 } 197 198 void 199 tcp_send_req(int s, short event, void *arg) 200 { 201 struct ctl_tcp_event *cte = arg; 202 int bs; 203 int len; 204 205 if (event == EV_TIMEOUT) { 206 tcp_close(cte, HOST_DOWN); 207 hce_notify_done(cte->host, HCE_TCP_WRITE_TIMEOUT); 208 return; 209 } 210 len = strlen(cte->req); 211 do { 212 bs = write(s, cte->req, len); 213 if (bs == -1) { 214 if (errno == EAGAIN || errno == EINTR) 215 goto retry; 216 log_warn("%s: cannot send request", __func__); 217 tcp_close(cte, HOST_DOWN); 218 hce_notify_done(cte->host, HCE_TCP_WRITE_FAIL); 219 return; 220 } 221 cte->req += bs; 222 len -= bs; 223 } while (len > 0); 224 225 if ((cte->buf = ibuf_dynamic(SMALL_READ_BUF_SIZE, UINT_MAX)) == NULL) 226 fatalx("%s: cannot create dynamic buffer", __func__); 227 event_again(&cte->ev, s, EV_TIMEOUT|EV_READ, tcp_read_buf, 228 &cte->tv_start, &cte->table->conf.timeout, cte); 229 return; 230 231 retry: 232 event_again(&cte->ev, s, EV_TIMEOUT|EV_WRITE, tcp_send_req, 233 &cte->tv_start, &cte->table->conf.timeout, cte); 234 } 235 236 void 237 tcp_read_buf(int s, short event, void *arg) 238 { 239 ssize_t br; 240 char rbuf[SMALL_READ_BUF_SIZE]; 241 struct ctl_tcp_event *cte = arg; 242 243 if (event == EV_TIMEOUT) { 244 if (ibuf_size(cte->buf)) 245 (void)cte->validate_close(cte); 246 else { 247 cte->host->he = HCE_TCP_READ_TIMEOUT; 248 cte->host->up = HOST_DOWN; 249 } 250 tcp_close(cte, cte->host->up == HOST_UP ? 0 : HOST_DOWN); 251 hce_notify_done(cte->host, cte->host->he); 252 return; 253 } 254 255 bzero(rbuf, sizeof(rbuf)); 256 br = read(s, rbuf, sizeof(rbuf) - 1); 257 switch (br) { 258 case -1: 259 if (errno == EAGAIN || errno == EINTR) 260 goto retry; 261 tcp_close(cte, HOST_DOWN); 262 hce_notify_done(cte->host, HCE_TCP_READ_FAIL); 263 return; 264 case 0: 265 cte->host->up = HOST_DOWN; 266 (void)cte->validate_close(cte); 267 tcp_close(cte, 0); 268 hce_notify_done(cte->host, cte->host->he); 269 return; 270 default: 271 if (ibuf_add(cte->buf, rbuf, br) == -1) 272 fatal("%s: buf_add error", __func__); 273 if (cte->validate_read != NULL) { 274 if (cte->validate_read(cte) != 0) 275 goto retry; 276 tcp_close(cte, 0); 277 hce_notify_done(cte->host, cte->host->he); 278 return; 279 } 280 break; /* retry */ 281 } 282 retry: 283 event_again(&cte->ev, s, EV_TIMEOUT|EV_READ, tcp_read_buf, 284 &cte->tv_start, &cte->table->conf.timeout, cte); 285 } 286 287 int 288 check_send_expect(struct ctl_tcp_event *cte) 289 { 290 u_char *b; 291 292 /* 293 * ensure string is nul-terminated. 294 */ 295 b = ibuf_reserve(cte->buf, 1); 296 if (b == NULL) 297 fatal("out of memory"); 298 *b = '\0'; 299 if (fnmatch(cte->table->conf.exbuf, cte->buf->buf, 0) == 0) { 300 cte->host->he = HCE_SEND_EXPECT_OK; 301 cte->host->up = HOST_UP; 302 return (0); 303 } 304 cte->host->he = HCE_SEND_EXPECT_FAIL; 305 cte->host->up = HOST_UNKNOWN; 306 307 /* 308 * go back to original position. 309 */ 310 cte->buf->wpos--; 311 return (1); 312 } 313 314 int 315 check_http_code(struct ctl_tcp_event *cte) 316 { 317 char *head; 318 char scode[4]; 319 const char *estr; 320 u_char *b; 321 int code; 322 struct host *host; 323 324 /* 325 * ensure string is nul-terminated. 326 */ 327 b = ibuf_reserve(cte->buf, 1); 328 if (b == NULL) 329 fatal("out of memory"); 330 *b = '\0'; 331 332 head = cte->buf->buf; 333 host = cte->host; 334 host->he = HCE_HTTP_CODE_ERROR; 335 host->code = 0; 336 337 if (strncmp(head, "HTTP/1.1 ", strlen("HTTP/1.1 ")) && 338 strncmp(head, "HTTP/1.0 ", strlen("HTTP/1.0 "))) { 339 log_debug("%s: %s failed (cannot parse HTTP version)", 340 __func__, host->conf.name); 341 host->up = HOST_DOWN; 342 return (1); 343 } 344 head += strlen("HTTP/1.1 "); 345 if (strlen(head) < 5) /* code + \r\n */ { 346 host->up = HOST_DOWN; 347 return (1); 348 } 349 (void)strlcpy(scode, head, sizeof(scode)); 350 code = strtonum(scode, 100, 999, &estr); 351 if (estr != NULL) { 352 log_debug("%s: %s failed (cannot parse HTTP code)", 353 __func__, host->conf.name); 354 host->up = HOST_DOWN; 355 return (1); 356 } 357 if (code != cte->table->conf.retcode) { 358 log_debug("%s: %s failed (invalid HTTP code %d returned)", 359 __func__, host->conf.name, code); 360 host->he = HCE_HTTP_CODE_FAIL; 361 host->up = HOST_DOWN; 362 host->code = code; 363 } else { 364 host->he = HCE_HTTP_CODE_OK; 365 host->up = HOST_UP; 366 } 367 return (!(host->up == HOST_UP)); 368 } 369 370 int 371 check_http_digest(struct ctl_tcp_event *cte) 372 { 373 char *head; 374 u_char *b; 375 char digest[SHA1_DIGEST_STRING_LENGTH]; 376 struct host *host; 377 378 /* 379 * ensure string is nul-terminated. 380 */ 381 b = ibuf_reserve(cte->buf, 1); 382 if (b == NULL) 383 fatal("out of memory"); 384 *b = '\0'; 385 386 head = cte->buf->buf; 387 host = cte->host; 388 host->he = HCE_HTTP_DIGEST_ERROR; 389 390 if ((head = strstr(head, "\r\n\r\n")) == NULL) { 391 log_debug("%s: %s failed (no end of headers)", 392 __func__, host->conf.name); 393 host->up = HOST_DOWN; 394 return (1); 395 } 396 head += strlen("\r\n\r\n"); 397 398 digeststr(cte->table->conf.digest_type, head, strlen(head), digest); 399 400 if (strcmp(cte->table->conf.digest, digest)) { 401 log_warnx("%s: %s failed (wrong digest)", 402 __func__, host->conf.name); 403 host->he = HCE_HTTP_DIGEST_FAIL; 404 host->up = HOST_DOWN; 405 } else { 406 host->he = HCE_HTTP_DIGEST_OK; 407 host->up = HOST_UP; 408 } 409 return (!(host->up == HOST_UP)); 410 } 411