Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Health-checks functions. |
| 3 | * |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 4 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 5 | * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
Willy Tarreau | b881608 | 2008-01-18 12:18:15 +0100 | [diff] [blame] | 14 | #include <assert.h> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 15 | #include <ctype.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | #include <errno.h> |
| 17 | #include <fcntl.h> |
Willy Tarreau | 9b39dc5 | 2014-07-08 00:54:10 +0200 | [diff] [blame] | 18 | #include <signal.h> |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 19 | #include <stdarg.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | #include <stdio.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 21 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #include <string.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 23 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | #include <unistd.h> |
Willy Tarreau | 9f6dc72 | 2019-03-01 11:15:10 +0100 | [diff] [blame] | 25 | #include <sys/resource.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | #include <sys/socket.h> |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 27 | #include <sys/types.h> |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 28 | #include <sys/wait.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | #include <netinet/in.h> |
Willy Tarreau | 1274bc4 | 2009-07-15 07:16:31 +0200 | [diff] [blame] | 30 | #include <netinet/tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <arpa/inet.h> |
| 32 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 33 | #include <common/cfgparse.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 34 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 35 | #include <common/compat.h> |
| 36 | #include <common/config.h> |
| 37 | #include <common/mini-clist.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 38 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 39 | #include <common/time.h> |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 40 | #include <common/hathreads.h> |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 41 | #include <common/http.h> |
| 42 | #include <common/h1.h> |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 43 | #include <common/htx.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
| 45 | #include <types/global.h> |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 46 | #include <types/dns.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 47 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 49 | #include <proto/action.h> |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 50 | #include <proto/arg.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | #include <proto/backend.h> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 52 | #include <proto/checks.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 53 | #include <proto/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/fd.h> |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 55 | #include <proto/http_htx.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/log.h> |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 57 | #include <proto/mux_pt.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/queue.h> |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 59 | #include <proto/port_range.h> |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 60 | #include <proto/proto_tcp.h> |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 61 | #include <proto/protocol.h> |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 62 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | #include <proto/server.h> |
Willy Tarreau | 48d6bf2 | 2016-06-21 16:27:34 +0200 | [diff] [blame] | 64 | #include <proto/signal.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 65 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | #include <proto/task.h> |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 67 | #include <proto/vars.h> |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 68 | #include <proto/log.h> |
| 69 | #include <proto/dns.h> |
| 70 | #include <proto/proto_udp.h> |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 71 | #include <proto/ssl_sock.h> |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 72 | #include <proto/sample.h> |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 73 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 74 | static int tcpcheck_get_step_id(struct check *, struct tcpcheck_rule *); |
Christopher Faulet | 31c30fd | 2020-03-26 21:10:03 +0100 | [diff] [blame] | 75 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 76 | static int wake_srv_chk(struct conn_stream *cs); |
| 77 | struct data_cb check_conn_cb = { |
| 78 | .wake = wake_srv_chk, |
| 79 | .name = "CHCK", |
| 80 | }; |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 81 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 82 | /* Global tree to share all tcp-checks */ |
| 83 | struct eb_root shared_tcpchecks = EB_ROOT; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 84 | |
| 85 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 86 | DECLARE_STATIC_POOL(pool_head_email_alert, "email_alert", sizeof(struct email_alert)); |
| 87 | DECLARE_STATIC_POOL(pool_head_tcpcheck_rule, "tcpcheck_rule", sizeof(struct tcpcheck_rule)); |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 88 | |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 89 | /* Dummy frontend used to create all checks sessions. */ |
| 90 | static struct proxy checks_fe; |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 91 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 92 | /**************************************************************************/ |
| 93 | /************************ Handle check results ****************************/ |
| 94 | /**************************************************************************/ |
| 95 | struct check_status { |
| 96 | short result; /* one of SRV_CHK_* */ |
| 97 | char *info; /* human readable short info */ |
| 98 | char *desc; /* long description */ |
| 99 | }; |
| 100 | |
| 101 | struct analyze_status { |
| 102 | char *desc; /* description */ |
| 103 | unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */ |
| 104 | }; |
| 105 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 106 | static const struct check_status check_statuses[HCHK_STATUS_SIZE] = { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 107 | [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" }, |
| 108 | [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" }, |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 109 | [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 110 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 111 | /* Below we have finished checks */ |
| 112 | [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" }, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 113 | [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" }, |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 114 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 115 | [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 117 | [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" }, |
| 118 | [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" }, |
| 119 | [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 120 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 121 | [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" }, |
| 122 | [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" }, |
| 123 | [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 124 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 125 | [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" }, |
| 126 | [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 127 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 128 | [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 129 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 130 | [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" }, |
| 131 | [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" }, |
| 132 | [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" }, |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 133 | |
| 134 | [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" }, |
| 135 | [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" }, |
Cyril Bonté | 77010d8 | 2014-08-07 01:55:37 +0200 | [diff] [blame] | 136 | [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 137 | }; |
| 138 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 139 | static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 140 | [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }}, |
| 141 | |
| 142 | [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }}, |
| 143 | [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }}, |
| 144 | |
| 145 | [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }}, |
| 146 | [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }}, |
| 147 | [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }}, |
| 148 | [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }}, |
| 149 | |
| 150 | [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }}, |
| 151 | [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }}, |
| 152 | [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }}, |
| 153 | }; |
| 154 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 155 | /* checks if <err> is a real error for errno or one that can be ignored, and |
| 156 | * return 0 for these ones or <err> for real ones. |
| 157 | */ |
| 158 | static inline int unclean_errno(int err) |
| 159 | { |
| 160 | if (err == EAGAIN || err == EINPROGRESS || |
| 161 | err == EISCONN || err == EALREADY) |
| 162 | return 0; |
| 163 | return err; |
| 164 | } |
| 165 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 166 | /* Converts check_status code to description */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 167 | const char *get_check_status_description(short check_status) { |
| 168 | |
| 169 | const char *desc; |
| 170 | |
| 171 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 172 | desc = check_statuses[check_status].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 173 | else |
| 174 | desc = NULL; |
| 175 | |
| 176 | if (desc && *desc) |
| 177 | return desc; |
| 178 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 179 | return check_statuses[HCHK_STATUS_UNKNOWN].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 182 | /* Converts check_status code to short info */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 183 | const char *get_check_status_info(short check_status) { |
| 184 | |
| 185 | const char *info; |
| 186 | |
| 187 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 188 | info = check_statuses[check_status].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 189 | else |
| 190 | info = NULL; |
| 191 | |
| 192 | if (info && *info) |
| 193 | return info; |
| 194 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 195 | return check_statuses[HCHK_STATUS_UNKNOWN].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 198 | /* Convert analyze_status to description */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 199 | const char *get_analyze_status(short analyze_status) { |
| 200 | |
| 201 | const char *desc; |
| 202 | |
| 203 | if (analyze_status < HANA_STATUS_SIZE) |
| 204 | desc = analyze_statuses[analyze_status].desc; |
| 205 | else |
| 206 | desc = NULL; |
| 207 | |
| 208 | if (desc && *desc) |
| 209 | return desc; |
| 210 | else |
| 211 | return analyze_statuses[HANA_STATUS_UNKNOWN].desc; |
| 212 | } |
| 213 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 214 | /* Sets check->status, update check->duration and fill check->result with an |
| 215 | * adequate CHK_RES_* value. The new check->health is computed based on the |
| 216 | * result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 217 | * |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 218 | * Shows information in logs about failed health check if server is UP or |
| 219 | * succeeded health checks if server is DOWN. |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 220 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 221 | static void set_server_check_status(struct check *check, short status, const char *desc) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 222 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 223 | struct server *s = check->server; |
Willy Tarreau | bef1b32 | 2014-05-13 21:01:39 +0200 | [diff] [blame] | 224 | short prev_status = check->status; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 225 | int report = 0; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 226 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 227 | if (status == HCHK_STATUS_START) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 228 | check->result = CHK_RES_UNKNOWN; /* no result yet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 229 | check->desc[0] = '\0'; |
| 230 | check->start = now; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 234 | if (!check->status) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 235 | return; |
| 236 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 237 | if (desc && *desc) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 238 | strncpy(check->desc, desc, HCHK_DESC_LEN-1); |
| 239 | check->desc[HCHK_DESC_LEN-1] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 240 | } else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 241 | check->desc[0] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 242 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 243 | check->status = status; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 244 | if (check_statuses[status].result) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 245 | check->result = check_statuses[status].result; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 246 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 247 | if (status == HCHK_STATUS_HANA) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 248 | check->duration = -1; |
| 249 | else if (!tv_iszero(&check->start)) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 250 | /* set_server_check_status() may be called more than once */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 251 | check->duration = tv_ms_elapsed(&check->start, &now); |
| 252 | tv_zero(&check->start); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 255 | /* no change is expected if no state change occurred */ |
| 256 | if (check->result == CHK_RES_NEUTRAL) |
| 257 | return; |
| 258 | |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 259 | /* If the check was really just sending a mail, it won't have an |
| 260 | * associated server, so we're done now. |
| 261 | */ |
| 262 | if (!s) |
| 263 | return; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 264 | report = 0; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 265 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 266 | switch (check->result) { |
| 267 | case CHK_RES_FAILED: |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 268 | /* Failure to connect to the agent as a secondary check should not |
| 269 | * cause the server to be marked down. |
| 270 | */ |
| 271 | if ((!(check->state & CHK_ST_AGENT) || |
Simon Horman | eaabd52 | 2015-02-26 11:26:17 +0900 | [diff] [blame] | 272 | (check->status >= HCHK_STATUS_L57DATA)) && |
Christopher Faulet | b119a79 | 2018-05-02 12:12:45 +0200 | [diff] [blame] | 273 | (check->health > 0)) { |
Olivier Houchard | 7059c55 | 2019-03-08 18:49:32 +0100 | [diff] [blame] | 274 | _HA_ATOMIC_ADD(&s->counters.failed_checks, 1); |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 275 | report = 1; |
| 276 | check->health--; |
| 277 | if (check->health < check->rise) |
| 278 | check->health = 0; |
| 279 | } |
| 280 | break; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 281 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 282 | case CHK_RES_PASSED: |
| 283 | case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */ |
| 284 | if ((check->health < check->rise + check->fall - 1) && |
| 285 | (check->result == CHK_RES_PASSED || check->health > 0)) { |
| 286 | report = 1; |
| 287 | check->health++; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 288 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 289 | if (check->health >= check->rise) |
| 290 | check->health = check->rise + check->fall - 1; /* OK now */ |
| 291 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 292 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 293 | /* clear consecutive_errors if observing is enabled */ |
| 294 | if (s->onerror) |
| 295 | s->consecutive_errors = 0; |
| 296 | break; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 297 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 298 | default: |
| 299 | break; |
| 300 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 301 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 302 | if (s->proxy->options2 & PR_O2_LOGHCHKS && |
| 303 | (status != prev_status || report)) { |
| 304 | chunk_printf(&trash, |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 305 | "%s check for %sserver %s/%s %s%s", |
| 306 | (check->state & CHK_ST_AGENT) ? "Agent" : "Health", |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 307 | s->flags & SRV_F_BACKUP ? "backup " : "", |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 308 | s->proxy->id, s->id, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 309 | (check->result == CHK_RES_CONDPASS) ? "conditionally ":"", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 310 | (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 311 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 312 | srv_append_status(&trash, s, check, -1, 0); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 313 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 314 | chunk_appendf(&trash, ", status: %d/%d %s", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 315 | (check->health >= check->rise) ? check->health - check->rise + 1 : check->health, |
| 316 | (check->health >= check->rise) ? check->fall : check->rise, |
| 317 | (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 318 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 319 | ha_warning("%s.\n", trash.area); |
| 320 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
| 321 | send_email_alert(s, LOG_INFO, "%s", trash.area); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 322 | } |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 325 | /* Marks the check <check>'s server down if the current check is already failed |
| 326 | * and the server is not down yet nor in maintenance. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 327 | */ |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 328 | static void check_notify_failure(struct check *check) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 329 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 330 | struct server *s = check->server; |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 331 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 332 | /* The agent secondary check should only cause a server to be marked |
| 333 | * as down if check->status is HCHK_STATUS_L7STS, which indicates |
| 334 | * that the agent returned "fail", "stopped" or "down". |
| 335 | * The implication here is that failure to connect to the agent |
| 336 | * as a secondary check should not cause the server to be marked |
| 337 | * down. */ |
| 338 | if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS) |
| 339 | return; |
| 340 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 341 | if (check->health > 0) |
| 342 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 343 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 344 | /* We only report a reason for the check if we did not do so previously */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 345 | srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 348 | /* Marks the check <check> as valid and tries to set its server up, provided |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 349 | * it isn't in maintenance, it is not tracking a down server and other checks |
| 350 | * comply. The rule is simple : by default, a server is up, unless any of the |
| 351 | * following conditions is true : |
| 352 | * - health check failed (check->health < rise) |
| 353 | * - agent check failed (agent->health < rise) |
| 354 | * - the server tracks a down server (track && track->state == STOPPED) |
| 355 | * Note that if the server has a slowstart, it will switch to STARTING instead |
| 356 | * of RUNNING. Also, only the health checks support the nolb mode, so the |
| 357 | * agent's success may not take the server out of this mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 358 | */ |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 359 | static void check_notify_success(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 360 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 361 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 362 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 363 | if (s->next_admin & SRV_ADMF_MAINT) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 364 | return; |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 365 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 366 | if (s->track && s->track->next_state == SRV_ST_STOPPED) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 367 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 368 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 369 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 370 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 371 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 372 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 373 | return; |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 374 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 375 | if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 376 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 377 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 378 | srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 379 | } |
| 380 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 381 | /* Marks the check <check> as valid and tries to set its server into stopping mode |
| 382 | * if it was running or starting, and provided it isn't in maintenance and other |
| 383 | * checks comply. The conditions for the server to be marked in stopping mode are |
| 384 | * the same as for it to be turned up. Also, only the health checks support the |
| 385 | * nolb mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 386 | */ |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 387 | static void check_notify_stopping(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 388 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 389 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 390 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 391 | if (s->next_admin & SRV_ADMF_MAINT) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 392 | return; |
| 393 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 394 | if (check->state & CHK_ST_AGENT) |
| 395 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 396 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 397 | if (s->track && s->track->next_state == SRV_ST_STOPPED) |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 398 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 399 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 400 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 401 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 402 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 403 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 404 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 405 | |
Willy Tarreau | b26881a | 2017-12-23 11:16:49 +0100 | [diff] [blame] | 406 | srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 407 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 408 | |
Willy Tarreau | 9fe7aae | 2013-12-31 23:47:37 +0100 | [diff] [blame] | 409 | /* note: use health_adjust() only, which first checks that the observe mode is |
| 410 | * enabled. |
| 411 | */ |
| 412 | void __health_adjust(struct server *s, short status) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 413 | { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 414 | int failed; |
| 415 | int expire; |
| 416 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 417 | if (s->observe >= HANA_OBS_SIZE) |
| 418 | return; |
| 419 | |
Willy Tarreau | bb95666 | 2013-01-24 00:37:39 +0100 | [diff] [blame] | 420 | if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 421 | return; |
| 422 | |
| 423 | switch (analyze_statuses[status].lr[s->observe - 1]) { |
| 424 | case 1: |
| 425 | failed = 1; |
| 426 | break; |
| 427 | |
| 428 | case 2: |
| 429 | failed = 0; |
| 430 | break; |
| 431 | |
| 432 | default: |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | if (!failed) { |
| 437 | /* good: clear consecutive_errors */ |
| 438 | s->consecutive_errors = 0; |
| 439 | return; |
| 440 | } |
| 441 | |
Olivier Houchard | 7059c55 | 2019-03-08 18:49:32 +0100 | [diff] [blame] | 442 | _HA_ATOMIC_ADD(&s->consecutive_errors, 1); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 443 | |
| 444 | if (s->consecutive_errors < s->consecutive_errors_limit) |
| 445 | return; |
| 446 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 447 | chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s", |
| 448 | s->consecutive_errors, get_analyze_status(status)); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 449 | |
| 450 | switch (s->onerror) { |
| 451 | case HANA_ONERR_FASTINTER: |
| 452 | /* force fastinter - nothing to do here as all modes force it */ |
| 453 | break; |
| 454 | |
| 455 | case HANA_ONERR_SUDDTH: |
| 456 | /* simulate a pre-fatal failed health check */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 457 | if (s->check.health > s->check.rise) |
| 458 | s->check.health = s->check.rise + 1; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 459 | |
| 460 | /* no break - fall through */ |
| 461 | |
| 462 | case HANA_ONERR_FAILCHK: |
| 463 | /* simulate a failed health check */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 464 | set_server_check_status(&s->check, HCHK_STATUS_HANA, |
| 465 | trash.area); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 466 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 467 | break; |
| 468 | |
| 469 | case HANA_ONERR_MARKDWN: |
| 470 | /* mark server down */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 471 | s->check.health = s->check.rise; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 472 | set_server_check_status(&s->check, HCHK_STATUS_HANA, |
| 473 | trash.area); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 474 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 475 | break; |
| 476 | |
| 477 | default: |
| 478 | /* write a warning? */ |
| 479 | break; |
| 480 | } |
| 481 | |
| 482 | s->consecutive_errors = 0; |
Olivier Houchard | 7059c55 | 2019-03-08 18:49:32 +0100 | [diff] [blame] | 483 | _HA_ATOMIC_ADD(&s->counters.failed_hana, 1); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 484 | |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 485 | if (s->check.fastinter) { |
| 486 | expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter)); |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 487 | if (s->check.task->expire > expire) { |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 488 | s->check.task->expire = expire; |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 489 | /* requeue check task with new expire */ |
| 490 | task_queue(s->check.task); |
| 491 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 492 | } |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 493 | } |
| 494 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 495 | /* Checks the connection. If an error has already been reported or the socket is |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 496 | * closed, keep errno intact as it is supposed to contain the valid error code. |
| 497 | * If no error is reported, check the socket's error queue using getsockopt(). |
| 498 | * Warning, this must be done only once when returning from poll, and never |
| 499 | * after an I/O error was attempted, otherwise the error queue might contain |
| 500 | * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the |
| 501 | * socket. Returns non-zero if an error was reported, zero if everything is |
| 502 | * clean (including a properly closed socket). |
| 503 | */ |
| 504 | static int retrieve_errno_from_socket(struct connection *conn) |
| 505 | { |
| 506 | int skerr; |
| 507 | socklen_t lskerr = sizeof(skerr); |
| 508 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 509 | if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 510 | return 1; |
| 511 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 512 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 513 | return 0; |
| 514 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 515 | if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 516 | errno = skerr; |
| 517 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 518 | errno = unclean_errno(errno); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 519 | |
| 520 | if (!errno) { |
| 521 | /* we could not retrieve an error, that does not mean there is |
| 522 | * none. Just don't change anything and only report the prior |
| 523 | * error if any. |
| 524 | */ |
| 525 | if (conn->flags & CO_FL_ERROR) |
| 526 | return 1; |
| 527 | else |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH; |
| 532 | return 1; |
| 533 | } |
| 534 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 535 | /* Tries to collect as much information as possible on the connection status, |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 536 | * and adjust the server status accordingly. It may make use of <errno_bck> |
| 537 | * if non-null when the caller is absolutely certain of its validity (eg: |
| 538 | * checked just after a syscall). If the caller doesn't have a valid errno, |
| 539 | * it can pass zero, and retrieve_errno_from_socket() will be called to try |
| 540 | * to extract errno from the socket. If no error is reported, it will consider |
| 541 | * the <expired> flag. This is intended to be used when a connection error was |
| 542 | * reported in conn->flags or when a timeout was reported in <expired>. The |
| 543 | * function takes care of not updating a server status which was already set. |
| 544 | * All situations where at least one of <expired> or CO_FL_ERROR are set |
| 545 | * produce a status. |
| 546 | */ |
Willy Tarreau | b5259bf | 2017-10-04 14:47:29 +0200 | [diff] [blame] | 547 | static void chk_report_conn_err(struct check *check, int errno_bck, int expired) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 548 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 549 | struct conn_stream *cs = check->cs; |
| 550 | struct connection *conn = cs_conn(cs); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 551 | const char *err_msg; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 552 | struct buffer *chk; |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 553 | int step; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 554 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 555 | if (check->result != CHK_RES_UNKNOWN) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 556 | return; |
| 557 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 558 | errno = unclean_errno(errno_bck); |
| 559 | if (conn && errno) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 560 | retrieve_errno_from_socket(conn); |
| 561 | |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 562 | if (conn && !(conn->flags & CO_FL_ERROR) && |
| 563 | !(cs->flags & CS_FL_ERROR) && !expired) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 564 | return; |
| 565 | |
| 566 | /* we'll try to build a meaningful error message depending on the |
| 567 | * context of the error possibly present in conn->err_code, and the |
| 568 | * socket error possibly collected above. This is useful to know the |
| 569 | * exact step of the L6 layer (eg: SSL handshake). |
| 570 | */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 571 | chk = get_trash_chunk(); |
| 572 | |
Christopher Faulet | 799f3a4 | 2020-04-07 12:06:14 +0200 | [diff] [blame] | 573 | if (check->type == PR_O2_TCPCHK_CHK && |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 574 | (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) { |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 575 | step = tcpcheck_get_step_id(check, NULL); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 576 | if (!step) |
| 577 | chunk_printf(chk, " at initial connection step of tcp-check"); |
| 578 | else { |
| 579 | chunk_printf(chk, " at step %d of tcp-check", step); |
| 580 | /* we were looking for a string */ |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 581 | if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 582 | if (check->current_step->connect.port) |
| 583 | chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 584 | else |
| 585 | chunk_appendf(chk, " (connect)"); |
| 586 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 587 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) { |
| 588 | struct tcpcheck_expect *expect = &check->current_step->expect; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 589 | |
| 590 | switch (expect->type) { |
| 591 | case TCPCHK_EXPECT_STRING: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 592 | chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data)); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 593 | break; |
| 594 | case TCPCHK_EXPECT_BINARY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 595 | chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data)); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 596 | break; |
| 597 | case TCPCHK_EXPECT_REGEX: |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 598 | chunk_appendf(chk, " (expect regex)"); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 599 | break; |
Gaetan Rivet | efab6c6 | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 600 | case TCPCHK_EXPECT_REGEX_BINARY: |
| 601 | chunk_appendf(chk, " (expect binary regex)"); |
| 602 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 603 | case TCPCHK_EXPECT_HTTP_STATUS: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 604 | chunk_appendf(chk, " (expect HTTP status '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 605 | break; |
| 606 | case TCPCHK_EXPECT_HTTP_REGEX_STATUS: |
| 607 | chunk_appendf(chk, " (expect HTTP status regex)"); |
| 608 | break; |
| 609 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 610 | chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 611 | break; |
| 612 | case TCPCHK_EXPECT_HTTP_REGEX_BODY: |
| 613 | chunk_appendf(chk, " (expect HTTP body regex)"); |
| 614 | break; |
Christopher Faulet | 9e6ed15 | 2020-04-03 15:24:06 +0200 | [diff] [blame] | 615 | case TCPCHK_EXPECT_CUSTOM: |
| 616 | chunk_appendf(chk, " (expect custom function)"); |
| 617 | break; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 618 | case TCPCHK_EXPECT_UNDEF: |
| 619 | chunk_appendf(chk, " (undefined expect!)"); |
| 620 | break; |
| 621 | } |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 622 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 623 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) { |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 624 | chunk_appendf(chk, " (send)"); |
| 625 | } |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 626 | |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 627 | if (check->current_step && check->current_step->comment) |
| 628 | chunk_appendf(chk, " comment: '%s'", check->current_step->comment); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 632 | if (conn && conn->err_code) { |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 633 | if (unclean_errno(errno)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 634 | chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), |
| 635 | chk->area); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 636 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 637 | chunk_printf(&trash, "%s%s", conn_err_code_str(conn), |
| 638 | chk->area); |
| 639 | err_msg = trash.area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 640 | } |
| 641 | else { |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 642 | if (unclean_errno(errno)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 643 | chunk_printf(&trash, "%s%s", strerror(errno), |
| 644 | chk->area); |
| 645 | err_msg = trash.area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 646 | } |
| 647 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 648 | err_msg = chk->area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 652 | if (check->state & CHK_ST_PORT_MISS) { |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 653 | /* NOTE: this is reported after <fall> tries */ |
| 654 | chunk_printf(chk, "No port available for the TCP connection"); |
| 655 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 656 | } |
| 657 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 658 | if (!conn) { |
| 659 | /* connection allocation error before the connection was established */ |
| 660 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 661 | } |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 662 | else if (conn->flags & CO_FL_WAIT_L4_CONN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 663 | /* L4 not established (yet) */ |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 664 | if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 665 | set_server_check_status(check, HCHK_STATUS_L4CON, err_msg); |
| 666 | else if (expired) |
| 667 | set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 668 | |
| 669 | /* |
| 670 | * might be due to a server IP change. |
| 671 | * Let's trigger a DNS resolution if none are currently running. |
| 672 | */ |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 673 | if (check->server) |
| 674 | dns_trigger_resolution(check->server->dns_requester); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 675 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 676 | } |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 677 | else if (conn->flags & CO_FL_WAIT_L6_CONN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 678 | /* L6 not established (yet) */ |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 679 | if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 680 | set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg); |
| 681 | else if (expired) |
| 682 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 683 | } |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 684 | else if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 685 | /* I/O error after connection was established and before we could diagnose */ |
| 686 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 687 | } |
| 688 | else if (expired) { |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 689 | enum healthcheck_status tout = HCHK_STATUS_L7TOUT; |
| 690 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 691 | /* connection established but expired check */ |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 692 | if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) |
| 693 | tout = check->current_step->expect.tout_status; |
| 694 | set_server_check_status(check, tout, err_msg); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | return; |
| 698 | } |
| 699 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 700 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 701 | /**************************************************************************/ |
| 702 | /*************** Init/deinit tcp-check rules and ruleset ******************/ |
| 703 | /**************************************************************************/ |
| 704 | /* Releases memory allocated for a log-format string */ |
| 705 | static void free_tcpcheck_fmt(struct list *fmt) |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 706 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 707 | struct logformat_node *lf, *lfb; |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 708 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 709 | list_for_each_entry_safe(lf, lfb, fmt, list) { |
| 710 | LIST_DEL(&lf->list); |
| 711 | release_sample_expr(lf->expr); |
| 712 | free(lf->arg); |
| 713 | free(lf); |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 714 | } |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 715 | } |
| 716 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 717 | /* Releases memory allocated for an HTTP header used in a tcp-check send rule */ |
| 718 | static void free_tcpcheck_http_hdr(struct tcpcheck_http_hdr *hdr) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 719 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 720 | if (!hdr) |
| 721 | return; |
Willy Tarreau | 4fc49a9 | 2019-05-05 06:54:22 +0200 | [diff] [blame] | 722 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 723 | free_tcpcheck_fmt(&hdr->value); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 724 | istfree(&hdr->name); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 725 | free(hdr); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 726 | } |
| 727 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 728 | /* Releases memory allocated for an HTTP header list used in a tcp-check send |
| 729 | * rule |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 730 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 731 | static void free_tcpcheck_http_hdrs(struct list *hdrs) |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 732 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 733 | struct tcpcheck_http_hdr *hdr, *bhdr; |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 734 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 735 | list_for_each_entry_safe(hdr, bhdr, hdrs, list) { |
| 736 | LIST_DEL(&hdr->list); |
| 737 | free_tcpcheck_http_hdr(hdr); |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 738 | } |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 741 | /* Releases memory allocated for a tcp-check. If in_pool is set, it means the |
| 742 | * tcp-check was allocated using a memory pool (it is used to instantiate email |
| 743 | * alerts). |
Christopher Faulet | 95226db | 2020-04-15 11:34:04 +0200 | [diff] [blame] | 744 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 745 | static void free_tcpcheck(struct tcpcheck_rule *rule, int in_pool) |
Christopher Faulet | 95226db | 2020-04-15 11:34:04 +0200 | [diff] [blame] | 746 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 747 | if (!rule) |
| 748 | return; |
Christopher Faulet | 95226db | 2020-04-15 11:34:04 +0200 | [diff] [blame] | 749 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 750 | free(rule->comment); |
| 751 | switch (rule->action) { |
| 752 | case TCPCHK_ACT_SEND: |
| 753 | switch (rule->send.type) { |
| 754 | case TCPCHK_SEND_STRING: |
| 755 | case TCPCHK_SEND_BINARY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 756 | istfree(&rule->send.data); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 757 | break; |
| 758 | case TCPCHK_SEND_STRING_LF: |
| 759 | case TCPCHK_SEND_BINARY_LF: |
| 760 | free_tcpcheck_fmt(&rule->send.fmt); |
| 761 | break; |
| 762 | case TCPCHK_SEND_HTTP: |
| 763 | free(rule->send.http.meth.str.area); |
| 764 | if (!(rule->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 765 | istfree(&rule->send.http.uri); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 766 | else |
| 767 | free_tcpcheck_fmt(&rule->send.http.uri_fmt); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 768 | istfree(&rule->send.http.vsn); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 769 | free_tcpcheck_http_hdrs(&rule->send.http.hdrs); |
| 770 | if (!(rule->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 771 | istfree(&rule->send.http.body); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 772 | else |
| 773 | free_tcpcheck_fmt(&rule->send.http.body_fmt); |
| 774 | break; |
| 775 | case TCPCHK_SEND_UNDEF: |
| 776 | break; |
| 777 | } |
| 778 | break; |
| 779 | case TCPCHK_ACT_EXPECT: |
| 780 | free_tcpcheck_fmt(&rule->expect.onerror_fmt); |
| 781 | free_tcpcheck_fmt(&rule->expect.onsuccess_fmt); |
| 782 | release_sample_expr(rule->expect.status_expr); |
| 783 | switch (rule->expect.type) { |
| 784 | case TCPCHK_EXPECT_STRING: |
| 785 | case TCPCHK_EXPECT_BINARY: |
| 786 | case TCPCHK_EXPECT_HTTP_STATUS: |
| 787 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 788 | istfree(&rule->expect.data); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 789 | break; |
| 790 | case TCPCHK_EXPECT_REGEX: |
| 791 | case TCPCHK_EXPECT_REGEX_BINARY: |
| 792 | case TCPCHK_EXPECT_HTTP_REGEX_STATUS: |
| 793 | case TCPCHK_EXPECT_HTTP_REGEX_BODY: |
| 794 | regex_free(rule->expect.regex); |
| 795 | break; |
| 796 | case TCPCHK_EXPECT_CUSTOM: |
| 797 | case TCPCHK_EXPECT_UNDEF: |
| 798 | break; |
| 799 | } |
| 800 | break; |
| 801 | case TCPCHK_ACT_CONNECT: |
| 802 | free(rule->connect.sni); |
| 803 | free(rule->connect.alpn); |
| 804 | release_sample_expr(rule->connect.port_expr); |
| 805 | break; |
| 806 | case TCPCHK_ACT_COMMENT: |
| 807 | break; |
| 808 | case TCPCHK_ACT_ACTION_KW: |
| 809 | free(rule->action_kw.rule); |
| 810 | break; |
Christopher Faulet | 95226db | 2020-04-15 11:34:04 +0200 | [diff] [blame] | 811 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 812 | |
| 813 | if (in_pool) |
| 814 | pool_free(pool_head_tcpcheck_rule, rule); |
| 815 | else |
| 816 | free(rule); |
Christopher Faulet | 95226db | 2020-04-15 11:34:04 +0200 | [diff] [blame] | 817 | } |
| 818 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 819 | /* Creates a tcp-check variable used in preset variables before executing a |
| 820 | * tcp-check ruleset. |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 821 | */ |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 822 | static struct tcpcheck_var *create_tcpcheck_var(const struct ist name) |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 823 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 824 | struct tcpcheck_var *var = NULL; |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 825 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 826 | var = calloc(1, sizeof(*var)); |
| 827 | if (var == NULL) |
| 828 | return NULL; |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 829 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 830 | var->name = istdup(name); |
| 831 | if (!isttest(var->name)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 832 | free(var); |
| 833 | return NULL; |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 834 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 835 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 836 | LIST_INIT(&var->list); |
| 837 | return var; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 838 | } |
| 839 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 840 | /* Releases memory allocated for a preset tcp-check variable */ |
| 841 | static void free_tcpcheck_var(struct tcpcheck_var *var) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 842 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 843 | if (!var) |
| 844 | return; |
| 845 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 846 | istfree(&var->name); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 847 | if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) |
| 848 | free(var->data.u.str.area); |
| 849 | else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) |
| 850 | free(var->data.u.meth.str.area); |
| 851 | free(var); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 852 | } |
| 853 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 854 | /* Releases a list of preset tcp-check variables */ |
| 855 | static void free_tcpcheck_vars(struct list *vars) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 856 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 857 | struct tcpcheck_var *var, *back; |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 858 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 859 | list_for_each_entry_safe(var, back, vars, list) { |
| 860 | LIST_DEL(&var->list); |
| 861 | free_tcpcheck_var(var); |
| 862 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 863 | } |
| 864 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 865 | /* Duplicate a list of preset tcp-check variables */ |
| 866 | int dup_tcpcheck_vars(struct list *dst, struct list *src) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 867 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 868 | struct tcpcheck_var *var, *new = NULL; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 869 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 870 | list_for_each_entry(var, src, list) { |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 871 | new = create_tcpcheck_var(var->name); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 872 | if (!new) |
| 873 | goto error; |
| 874 | new->data.type = var->data.type; |
| 875 | if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) { |
| 876 | if (chunk_dup(&new->data.u.str, &var->data.u.str) == NULL) |
| 877 | goto error; |
| 878 | if (var->data.type == SMP_T_STR) |
| 879 | new->data.u.str.area[new->data.u.str.data] = 0; |
| 880 | } |
| 881 | else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) { |
| 882 | if (chunk_dup(&new->data.u.str, &var->data.u.str) == NULL) |
| 883 | goto error; |
| 884 | new->data.u.str.area[new->data.u.str.data] = 0; |
| 885 | new->data.u.meth.meth = var->data.u.meth.meth; |
| 886 | } |
| 887 | else |
| 888 | new->data.u = var->data.u; |
| 889 | LIST_ADDQ(dst, &new->list); |
| 890 | } |
| 891 | return 1; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 892 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 893 | error: |
| 894 | free(new); |
| 895 | return 0; |
| 896 | } |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 897 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 898 | /* Looks for a shared tcp-check ruleset given its name. */ |
| 899 | static struct tcpcheck_ruleset *find_tcpcheck_ruleset(const char *name) |
| 900 | { |
| 901 | struct tcpcheck_ruleset *rs; |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 902 | struct ebpt_node *node; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 903 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 904 | node = ebis_lookup_len(&shared_tcpchecks, name, strlen(name)); |
| 905 | if (node) { |
| 906 | rs = container_of(node, typeof(*rs), node); |
| 907 | return rs; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 908 | } |
| 909 | return NULL; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 910 | } |
| 911 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 912 | /* Creates a new shared tcp-check ruleset */ |
| 913 | static struct tcpcheck_ruleset *create_tcpcheck_ruleset(const char *name) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 914 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 915 | struct tcpcheck_ruleset *rs; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 916 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 917 | rs = calloc(1, sizeof(*rs)); |
| 918 | if (rs == NULL) |
| 919 | return NULL; |
| 920 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 921 | rs->node.key = strdup(name); |
| 922 | if (rs->node.key == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 923 | free(rs); |
| 924 | return NULL; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 925 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 926 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 927 | LIST_INIT(&rs->rules); |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 928 | ebis_insert(&shared_tcpchecks, &rs->node); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 929 | return rs; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 930 | } |
| 931 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 932 | /* Releases memory allocated by a tcp-check ruleset. */ |
| 933 | static void free_tcpcheck_ruleset(struct tcpcheck_ruleset *rs) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 934 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 935 | struct tcpcheck_rule *r, *rb; |
| 936 | if (!rs) |
| 937 | return; |
Willy Tarreau | 48d6bf2 | 2016-06-21 16:27:34 +0200 | [diff] [blame] | 938 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 939 | ebpt_delete(&rs->node); |
| 940 | free(rs->node.key); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 941 | list_for_each_entry_safe(r, rb, &rs->rules, list) { |
| 942 | LIST_DEL(&r->list); |
| 943 | free_tcpcheck(r, 0); |
| 944 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 945 | free(rs); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 946 | } |
| 947 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 948 | |
| 949 | /**************************************************************************/ |
| 950 | /**************** Everything about tcp-checks execution *******************/ |
| 951 | /**************************************************************************/ |
| 952 | /* Returns the id of a step in a tcp-check ruleset */ |
| 953 | static int tcpcheck_get_step_id(struct check *check, struct tcpcheck_rule *rule) |
Willy Tarreau | 48d6bf2 | 2016-06-21 16:27:34 +0200 | [diff] [blame] | 954 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 955 | if (!rule) |
| 956 | rule = check->current_step; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 957 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 958 | /* no last started step => first step */ |
| 959 | if (!rule) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 960 | return 1; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 961 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 962 | /* last step is the first implicit connect */ |
| 963 | if (rule->index == 0 && |
| 964 | rule->action == TCPCHK_ACT_CONNECT && |
| 965 | (rule->connect.options & TCPCHK_OPT_IMPLICIT)) |
| 966 | return 0; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 967 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 968 | return rule->index + 1; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 969 | } |
| 970 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 971 | /* Returns the first non COMMENT/ACTION_KW tcp-check rule from list <list> or |
| 972 | * NULL if none was found. |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 973 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 974 | static struct tcpcheck_rule *get_first_tcpcheck_rule(struct tcpcheck_rules *rules) |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 975 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 976 | struct tcpcheck_rule *r; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 977 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 978 | list_for_each_entry(r, rules->list, list) { |
| 979 | if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW) |
| 980 | return r; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 981 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 982 | return NULL; |
| 983 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 984 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 985 | /* Returns the last non COMMENT/ACTION_KW tcp-check rule from list <list> or |
| 986 | * NULL if none was found. |
| 987 | */ |
| 988 | static struct tcpcheck_rule *get_last_tcpcheck_rule(struct tcpcheck_rules *rules) |
| 989 | { |
| 990 | struct tcpcheck_rule *r; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 991 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 992 | list_for_each_entry_rev(r, rules->list, list) { |
| 993 | if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW) |
| 994 | return r; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 995 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 996 | return NULL; |
| 997 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 998 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 999 | /* Returns the non COMMENT/ACTION_KW tcp-check rule from list <list> following |
| 1000 | * <start> or NULL if non was found. If <start> is NULL, it relies on |
| 1001 | * get_first_tcpcheck_rule(). |
| 1002 | */ |
| 1003 | static struct tcpcheck_rule *get_next_tcpcheck_rule(struct tcpcheck_rules *rules, struct tcpcheck_rule *start) |
| 1004 | { |
| 1005 | struct tcpcheck_rule *r; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1006 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1007 | if (!start) |
| 1008 | return get_first_tcpcheck_rule(rules); |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1009 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1010 | r = LIST_NEXT(&start->list, typeof(r), list); |
| 1011 | list_for_each_entry_from(r, rules->list, list) { |
| 1012 | if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW) |
| 1013 | return r; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1014 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1015 | return NULL; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1016 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1017 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1018 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1019 | /* Creates info message when a tcp-check healthcheck fails on an expect rule */ |
| 1020 | static void tcpcheck_expect_onerror_message(struct buffer *msg, struct check *check, struct tcpcheck_rule *rule, |
| 1021 | int match, struct ist info) |
| 1022 | { |
| 1023 | struct sample *smp; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1024 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1025 | /* Follows these step to produce the info message: |
| 1026 | * 1. if info field is already provided, copy it |
| 1027 | * 2. if the expect rule provides an onerror log-format string, |
| 1028 | * use it to produce the message |
| 1029 | * 3. the expect rule is part of a protcol check (http, redis, mysql...), do nothing |
| 1030 | * 4. Otherwise produce the generic tcp-check info message |
| 1031 | */ |
| 1032 | if (istlen(info)) { |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1033 | chunk_strncat(msg, istptr(info), istlen(info)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1034 | goto comment; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1035 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1036 | else if (!LIST_ISEMPTY(&rule->expect.onerror_fmt)) { |
| 1037 | msg->data += sess_build_logline(check->sess, NULL, b_tail(msg), b_room(msg), &rule->expect.onerror_fmt); |
| 1038 | goto comment; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1039 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1040 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1041 | if (check->type == PR_O2_TCPCHK_CHK && |
| 1042 | (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) != TCPCHK_RULES_TCP_CHK) |
| 1043 | goto comment; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1044 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1045 | chunk_strcat(msg, (match ? "TCPCHK matched unwanted content" : "TCPCHK did not match content")); |
| 1046 | switch (rule->expect.type) { |
| 1047 | case TCPCHK_EXPECT_STRING: |
| 1048 | case TCPCHK_EXPECT_HTTP_STATUS: |
| 1049 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1050 | chunk_appendf(msg, " '%.*s' at step %d", (unsigned int)istlen(rule->expect.data), istptr(rule->expect.data), |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1051 | tcpcheck_get_step_id(check, rule)); |
| 1052 | break; |
| 1053 | case TCPCHK_EXPECT_BINARY: |
| 1054 | chunk_appendf(msg, " (binary) at step %d", tcpcheck_get_step_id(check, rule)); |
| 1055 | break; |
| 1056 | case TCPCHK_EXPECT_REGEX: |
| 1057 | case TCPCHK_EXPECT_HTTP_REGEX_STATUS: |
| 1058 | case TCPCHK_EXPECT_HTTP_REGEX_BODY: |
| 1059 | chunk_appendf(msg, " (regex) at step %d", tcpcheck_get_step_id(check, rule)); |
| 1060 | break; |
| 1061 | case TCPCHK_EXPECT_REGEX_BINARY: |
| 1062 | chunk_appendf(msg, " (binary regex) at step %d", tcpcheck_get_step_id(check, rule)); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1063 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1064 | /* If references to the matched text were made, divide the |
| 1065 | * offsets by 2 to match offset of the original response buffer. |
| 1066 | */ |
| 1067 | if (rule->expect.flags & TCPCHK_EXPT_FL_CAP) { |
| 1068 | int i; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1069 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1070 | for (i = 1; i < MAX_MATCH && pmatch[i].rm_so != -1; i++) { |
| 1071 | pmatch[i].rm_so /= 2; /* at first matched char. */ |
| 1072 | pmatch[i].rm_eo /= 2; /* at last matched char. */ |
| 1073 | } |
| 1074 | } |
| 1075 | break; |
| 1076 | case TCPCHK_EXPECT_CUSTOM: |
| 1077 | chunk_appendf(msg, " (custom function) at step %d", tcpcheck_get_step_id(check, rule)); |
| 1078 | break; |
| 1079 | case TCPCHK_EXPECT_UNDEF: |
| 1080 | /* Should never happen. */ |
| 1081 | return; |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1084 | comment: |
| 1085 | /* If the failing expect rule provides a comment, it is concatenated to |
| 1086 | * the info message. |
| 1087 | */ |
| 1088 | if (rule->comment) { |
| 1089 | chunk_strcat(msg, " comment: "); |
| 1090 | if (rule->expect.flags & TCPCHK_EXPT_FL_CAP) { |
| 1091 | int ret = exp_replace(b_tail(msg), b_room(msg), b_head(&check->bi), rule->comment, pmatch); |
| 1092 | if (ret != -1) /* ignore comment if too large */ |
| 1093 | msg->data += ret; |
| 1094 | } |
| 1095 | else |
| 1096 | chunk_strcat(msg, rule->comment); |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1097 | } |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 1098 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1099 | /* Finally, the check status code is set if the failing expect rule |
| 1100 | * defines a status expression. |
| 1101 | */ |
| 1102 | if (rule->expect.status_expr) { |
| 1103 | smp = sample_fetch_as_type(check->proxy, check->sess, NULL, SMP_OPT_DIR_RES | SMP_OPT_FINAL, |
| 1104 | rule->expect.status_expr, SMP_T_SINT); |
| 1105 | if (smp) |
| 1106 | check->code = smp->data.u.sint; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1107 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1108 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1109 | *(b_tail(msg)) = '\0'; |
| 1110 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1111 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1112 | /* Creates info message when a tcp-check healthcheck succeeds on an expect rule */ |
| 1113 | static void tcpcheck_expect_onsuccess_message(struct buffer *msg, struct check *check, struct tcpcheck_rule *rule, |
| 1114 | struct ist info) |
| 1115 | { |
| 1116 | struct sample *smp; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1117 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1118 | /* Follows these step to produce the info message: |
| 1119 | * 1. if info field is already provided, copy it |
| 1120 | * 2. if the expect rule provides an onsucces log-format string, |
| 1121 | * use it to produce the message |
| 1122 | * 3. the expect rule is part of a protcol check (http, redis, mysql...), do nothing |
| 1123 | * 4. Otherwise produce the generic tcp-check info message |
| 1124 | */ |
| 1125 | if (istlen(info)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1126 | chunk_strncat(msg, istptr(info), istlen(info)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1127 | if (!LIST_ISEMPTY(&rule->expect.onsuccess_fmt)) |
| 1128 | msg->data += sess_build_logline(check->sess, NULL, b_tail(msg), b_room(msg), |
| 1129 | &rule->expect.onsuccess_fmt); |
| 1130 | else if (check->type == PR_O2_TCPCHK_CHK && |
| 1131 | (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) |
| 1132 | chunk_strcat(msg, "(tcp-check)"); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1133 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1134 | /* Finally, the check status code is set if the expect rule defines a |
| 1135 | * status expression. |
| 1136 | */ |
| 1137 | if (rule->expect.status_expr) { |
| 1138 | smp = sample_fetch_as_type(check->proxy, check->sess, NULL, SMP_OPT_DIR_RES | SMP_OPT_FINAL, |
| 1139 | rule->expect.status_expr, SMP_T_SINT); |
| 1140 | if (smp) |
| 1141 | check->code = smp->data.u.sint; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1142 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1143 | |
| 1144 | *(b_tail(msg)) = '\0'; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1145 | } |
| 1146 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1147 | /* Builds the server state header used by HTTP health-checks */ |
| 1148 | static int httpchk_build_status_header(struct server *s, struct buffer *buf) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1149 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1150 | int sv_state; |
| 1151 | int ratio; |
| 1152 | char addr[46]; |
| 1153 | char port[6]; |
| 1154 | const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d", |
| 1155 | "UP %d/%d", "UP", |
| 1156 | "NOLB %d/%d", "NOLB", |
| 1157 | "no check" }; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1158 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1159 | if (!(s->check.state & CHK_ST_ENABLED)) |
| 1160 | sv_state = 6; |
| 1161 | else if (s->cur_state != SRV_ST_STOPPED) { |
| 1162 | if (s->check.health == s->check.rise + s->check.fall - 1) |
| 1163 | sv_state = 3; /* UP */ |
| 1164 | else |
| 1165 | sv_state = 2; /* going down */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1166 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1167 | if (s->cur_state == SRV_ST_STOPPING) |
| 1168 | sv_state += 2; |
| 1169 | } else { |
| 1170 | if (s->check.health) |
| 1171 | sv_state = 1; /* going up */ |
| 1172 | else |
| 1173 | sv_state = 0; /* DOWN */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1174 | } |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 1175 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1176 | chunk_appendf(buf, srv_hlt_st[sv_state], |
| 1177 | (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health), |
| 1178 | (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise)); |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 1179 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1180 | addr_to_str(&s->addr, addr, sizeof(addr)); |
| 1181 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
| 1182 | snprintf(port, sizeof(port), "%u", s->svc_port); |
| 1183 | else |
| 1184 | *port = 0; |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 1185 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1186 | chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", |
| 1187 | addr, port, s->proxy->id, s->id, |
| 1188 | global.node, |
| 1189 | (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 1190 | (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 1191 | s->cur_sess, s->proxy->beconn - s->proxy->nbpend, |
| 1192 | s->nbpend); |
Willy Tarreau | 9f6dc72 | 2019-03-01 11:15:10 +0100 | [diff] [blame] | 1193 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1194 | if ((s->cur_state == SRV_ST_STARTING) && |
| 1195 | now.tv_sec < s->last_change + s->slowstart && |
| 1196 | now.tv_sec >= s->last_change) { |
| 1197 | ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); |
| 1198 | chunk_appendf(buf, "; throttle=%d%%", ratio); |
| 1199 | } |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1200 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1201 | return b_data(buf); |
| 1202 | } |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1203 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1204 | /* Internal functions to parse and validate a MySQL packet in the context of an |
| 1205 | * expect rule. It start to parse the input buffer at the offset <offset>. If |
| 1206 | * <last_read> is set, no more data are expected. |
| 1207 | */ |
| 1208 | static enum tcpcheck_eval_ret tcpcheck_mysql_expect_packet(struct check *check, struct tcpcheck_rule *rule, |
| 1209 | unsigned int offset, int last_read) |
| 1210 | { |
| 1211 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 1212 | enum healthcheck_status status; |
| 1213 | struct buffer *msg = NULL; |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1214 | struct ist desc = IST_NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1215 | unsigned int err = 0, plen = 0; |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1216 | |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1217 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1218 | /* 3 Bytes for the packet length and 1 byte for the sequence id */ |
| 1219 | if (!last_read && b_data(&check->bi) < offset+4) { |
| 1220 | if (!last_read) |
| 1221 | goto wait_more_data; |
| 1222 | |
| 1223 | /* invalid length or truncated response */ |
| 1224 | status = HCHK_STATUS_L7RSP; |
| 1225 | goto error; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1226 | } |
| 1227 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1228 | plen = ((unsigned char) *b_peek(&check->bi, offset)) + |
| 1229 | (((unsigned char) *(b_peek(&check->bi, offset+1))) << 8) + |
| 1230 | (((unsigned char) *(b_peek(&check->bi, offset+2))) << 16); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1231 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1232 | if (b_data(&check->bi) < offset+plen+4) { |
| 1233 | if (!last_read) |
| 1234 | goto wait_more_data; |
| 1235 | |
| 1236 | /* invalid length or truncated response */ |
| 1237 | status = HCHK_STATUS_L7RSP; |
| 1238 | goto error; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1239 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1240 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1241 | if (*b_peek(&check->bi, offset+4) == '\xff') { |
| 1242 | /* MySQL Error packet always begin with field_count = 0xff */ |
| 1243 | status = HCHK_STATUS_L7STS; |
| 1244 | err = ((unsigned char) *b_peek(&check->bi, offset+5)) + |
| 1245 | (((unsigned char) *(b_peek(&check->bi, offset+6))) << 8); |
| 1246 | desc = ist2(b_peek(&check->bi, offset+7), b_data(&check->bi) - offset - 7); |
| 1247 | goto error; |
| 1248 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1249 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1250 | if (get_next_tcpcheck_rule(check->tcpcheck_rules, rule) != NULL) { |
| 1251 | /* Not the last rule, continue */ |
| 1252 | goto out; |
| 1253 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1254 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1255 | /* We set the MySQL Version in description for information purpose |
| 1256 | * FIXME : it can be cool to use MySQL Version for other purpose, |
| 1257 | * like mark as down old MySQL server. |
| 1258 | */ |
| 1259 | set_server_check_status(check, rule->expect.ok_status, b_peek(&check->bi, 5)); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1260 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1261 | out: |
| 1262 | free_trash_chunk(msg); |
| 1263 | return ret; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1264 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1265 | error: |
| 1266 | ret = TCPCHK_EVAL_STOP; |
| 1267 | check->code = err; |
| 1268 | msg = alloc_trash_chunk(); |
| 1269 | if (msg) |
| 1270 | tcpcheck_expect_onerror_message(msg, check, rule, 0, desc); |
| 1271 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
| 1272 | goto out; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1273 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1274 | wait_more_data: |
| 1275 | ret = TCPCHK_EVAL_WAIT; |
| 1276 | goto out; |
| 1277 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1278 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1279 | /* Custom tcp-check expect function to parse and validate the MySQL initial |
| 1280 | * handshake packet. Returns TCPCHK_EVAL_WAIT to wait for more data, |
| 1281 | * TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if an |
| 1282 | * error occurred. |
| 1283 | */ |
| 1284 | static enum tcpcheck_eval_ret tcpcheck_mysql_expect_iniths(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 1285 | { |
| 1286 | return tcpcheck_mysql_expect_packet(check, rule, 0, last_read); |
| 1287 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1288 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1289 | /* Custom tcp-check expect function to parse and validate the MySQL OK packet |
| 1290 | * following the initial handshake. Returns TCPCHK_EVAL_WAIT to wait for more |
| 1291 | * data, TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if |
| 1292 | * an error occurred. |
| 1293 | */ |
| 1294 | static enum tcpcheck_eval_ret tcpcheck_mysql_expect_ok(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 1295 | { |
| 1296 | unsigned int hslen = 0; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1297 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1298 | hslen = 4 + ((unsigned char) *b_head(&check->bi)) + |
| 1299 | (((unsigned char) *(b_peek(&check->bi, 1))) << 8) + |
| 1300 | (((unsigned char) *(b_peek(&check->bi, 2))) << 16); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1301 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1302 | return tcpcheck_mysql_expect_packet(check, rule, hslen, last_read); |
| 1303 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1304 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1305 | /* Custom tcp-check expect function to parse and validate the LDAP bind response |
| 1306 | * package packet. Returns TCPCHK_EVAL_WAIT to wait for more data, |
| 1307 | * TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if an |
| 1308 | * error occurred. |
| 1309 | */ |
| 1310 | static enum tcpcheck_eval_ret tcpcheck_ldap_expect_bindrsp(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 1311 | { |
| 1312 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 1313 | enum healthcheck_status status; |
| 1314 | struct buffer *msg = NULL; |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1315 | struct ist desc = IST_NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1316 | unsigned short msglen = 0; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1317 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1318 | /* Check if the server speaks LDAP (ASN.1/BER) |
| 1319 | * http://en.wikipedia.org/wiki/Basic_Encoding_Rules |
| 1320 | * http://tools.ietf.org/html/rfc4511 |
| 1321 | */ |
| 1322 | /* size of LDAPMessage */ |
| 1323 | msglen = (*(b_head(&check->bi) + 1) & 0x80) ? (*(b_head(&check->bi) + 1) & 0x7f) : 0; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1324 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1325 | /* http://tools.ietf.org/html/rfc4511#section-4.2.2 |
| 1326 | * messageID: 0x02 0x01 0x01: INTEGER 1 |
| 1327 | * protocolOp: 0x61: bindResponse |
| 1328 | */ |
| 1329 | if ((msglen > 2) || (memcmp(b_head(&check->bi) + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) { |
| 1330 | status = HCHK_STATUS_L7RSP; |
| 1331 | desc = ist("Not LDAPv3 protocol"); |
| 1332 | goto error; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1333 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1334 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1335 | /* size of bindResponse */ |
| 1336 | msglen += (*(b_head(&check->bi) + msglen + 6) & 0x80) ? (*(b_head(&check->bi) + msglen + 6) & 0x7f) : 0; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1337 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1338 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1339 | * ldapResult: 0x0a 0x01: ENUMERATION |
| 1340 | */ |
| 1341 | if ((msglen > 4) || (memcmp(b_head(&check->bi) + 7 + msglen, "\x0a\x01", 2) != 0)) { |
| 1342 | status = HCHK_STATUS_L7RSP; |
| 1343 | desc = ist("Not LDAPv3 protocol"); |
| 1344 | goto error; |
| 1345 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1346 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1347 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1348 | * resultCode |
| 1349 | */ |
| 1350 | check->code = *(b_head(&check->bi) + msglen + 9); |
| 1351 | if (check->code) { |
| 1352 | status = HCHK_STATUS_L7STS; |
| 1353 | desc = ist("See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9"); |
| 1354 | goto error; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1355 | } |
| 1356 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1357 | set_server_check_status(check, rule->expect.ok_status, "Success"); |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 1358 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1359 | out: |
| 1360 | free_trash_chunk(msg); |
| 1361 | return ret; |
| 1362 | |
| 1363 | error: |
| 1364 | ret = TCPCHK_EVAL_STOP; |
| 1365 | msg = alloc_trash_chunk(); |
| 1366 | if (msg) |
| 1367 | tcpcheck_expect_onerror_message(msg, check, rule, 0, desc); |
| 1368 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
| 1369 | goto out; |
| 1370 | |
| 1371 | wait_more_data: |
| 1372 | ret = TCPCHK_EVAL_WAIT; |
| 1373 | goto out; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1374 | } |
| 1375 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1376 | /* Custom tcp-check expect function to parse and validate the SPOP hello agent |
| 1377 | * frame. Returns TCPCHK_EVAL_WAIT to wait for more data, TCPCHK_EVAL_CONTINUE |
| 1378 | * to evaluate the next rule or TCPCHK_EVAL_STOP if an error occurred. |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1379 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1380 | static enum tcpcheck_eval_ret tcpcheck_spop_expect_agenthello(struct check *check, struct tcpcheck_rule *rule, int last_read) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1381 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1382 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 1383 | enum healthcheck_status status; |
| 1384 | struct buffer *msg = NULL; |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1385 | struct ist desc = IST_NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1386 | unsigned int framesz; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1387 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1388 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1389 | memcpy(&framesz, b_head(&check->bi), 4); |
| 1390 | framesz = ntohl(framesz); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1391 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1392 | if (!last_read && b_data(&check->bi) < (4+framesz)) |
| 1393 | goto wait_more_data; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1394 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1395 | memset(b_orig(&trash), 0, b_size(&trash)); |
| 1396 | if (spoe_handle_healthcheck_response(b_peek(&check->bi, 4), framesz, b_orig(&trash), HCHK_DESC_LEN) == -1) { |
| 1397 | status = HCHK_STATUS_L7RSP; |
| 1398 | desc = ist2(b_orig(&trash), strlen(b_orig(&trash))); |
| 1399 | goto error; |
| 1400 | } |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1401 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1402 | set_server_check_status(check, rule->expect.ok_status, "SPOA server is ok"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1403 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1404 | out: |
| 1405 | free_trash_chunk(msg); |
| 1406 | return ret; |
Christopher Faulet | a32a250 | 2020-04-20 09:04:37 +0200 | [diff] [blame] | 1407 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1408 | error: |
| 1409 | ret = TCPCHK_EVAL_STOP; |
| 1410 | msg = alloc_trash_chunk(); |
| 1411 | if (msg) |
| 1412 | tcpcheck_expect_onerror_message(msg, check, rule, 0, desc); |
| 1413 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
| 1414 | goto out; |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 1415 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1416 | wait_more_data: |
| 1417 | ret = TCPCHK_EVAL_WAIT; |
| 1418 | goto out; |
| 1419 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1420 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1421 | /* Custom tcp-check expect function to parse and validate the agent-check |
| 1422 | * reply. Returns TCPCHK_EVAL_WAIT to wait for more data, TCPCHK_EVAL_CONTINUE |
| 1423 | * to evaluate the next rule or TCPCHK_EVAL_STOP if an error occurred. |
| 1424 | */ |
| 1425 | static enum tcpcheck_eval_ret tcpcheck_agent_expect_reply(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 1426 | { |
| 1427 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_STOP; |
| 1428 | enum healthcheck_status status = HCHK_STATUS_CHECKED; |
| 1429 | const char *hs = NULL; /* health status */ |
| 1430 | const char *as = NULL; /* admin status */ |
| 1431 | const char *ps = NULL; /* performance status */ |
| 1432 | const char *cs = NULL; /* maxconn */ |
| 1433 | const char *err = NULL; /* first error to report */ |
| 1434 | const char *wrn = NULL; /* first warning to report */ |
| 1435 | char *cmd, *p; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1436 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1437 | /* We're getting an agent check response. The agent could |
| 1438 | * have been disabled in the mean time with a long check |
| 1439 | * still pending. It is important that we ignore the whole |
| 1440 | * response. |
| 1441 | */ |
| 1442 | if (!(check->state & CHK_ST_ENABLED)) |
| 1443 | goto out; |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1444 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1445 | /* The agent supports strings made of a single line ended by the |
| 1446 | * first CR ('\r') or LF ('\n'). This line is composed of words |
| 1447 | * delimited by spaces (' '), tabs ('\t'), or commas (','). The |
| 1448 | * line may optionally contained a description of a state change |
| 1449 | * after a sharp ('#'), which is only considered if a health state |
| 1450 | * is announced. |
| 1451 | * |
| 1452 | * Words may be composed of : |
| 1453 | * - a numeric weight suffixed by the percent character ('%'). |
| 1454 | * - a health status among "up", "down", "stopped", and "fail". |
| 1455 | * - an admin status among "ready", "drain", "maint". |
| 1456 | * |
| 1457 | * These words may appear in any order. If multiple words of the |
| 1458 | * same category appear, the last one wins. |
| 1459 | */ |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 1460 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1461 | p = b_head(&check->bi); |
| 1462 | while (*p && *p != '\n' && *p != '\r') |
| 1463 | p++; |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 1464 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1465 | if (!*p) { |
| 1466 | if (!last_read) |
| 1467 | goto wait_more_data; |
| 1468 | |
| 1469 | /* at least inform the admin that the agent is mis-behaving */ |
| 1470 | set_server_check_status(check, check->status, "Ignoring incomplete line from agent"); |
| 1471 | goto out; |
| 1472 | } |
| 1473 | |
| 1474 | *p = 0; |
| 1475 | cmd = b_head(&check->bi); |
| 1476 | |
| 1477 | while (*cmd) { |
| 1478 | /* look for next word */ |
| 1479 | if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') { |
| 1480 | cmd++; |
| 1481 | continue; |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 1482 | } |
| 1483 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1484 | if (*cmd == '#') { |
| 1485 | /* this is the beginning of a health status description, |
| 1486 | * skip the sharp and blanks. |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 1487 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1488 | cmd++; |
| 1489 | while (*cmd == '\t' || *cmd == ' ') |
| 1490 | cmd++; |
| 1491 | break; |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 1492 | } |
| 1493 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1494 | /* find the end of the word so that we have a null-terminated |
| 1495 | * word between <cmd> and <p>. |
| 1496 | */ |
| 1497 | p = cmd + 1; |
| 1498 | while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',') |
| 1499 | p++; |
| 1500 | if (*p) |
| 1501 | *p++ = 0; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1502 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1503 | /* first, health statuses */ |
| 1504 | if (strcasecmp(cmd, "up") == 0) { |
| 1505 | check->server->check.health = check->server->check.rise + check->server->check.fall - 1; |
| 1506 | status = HCHK_STATUS_L7OKD; |
| 1507 | hs = cmd; |
| 1508 | } |
| 1509 | else if (strcasecmp(cmd, "down") == 0) { |
| 1510 | check->server->check.health = 0; |
| 1511 | status = HCHK_STATUS_L7STS; |
| 1512 | hs = cmd; |
| 1513 | } |
| 1514 | else if (strcasecmp(cmd, "stopped") == 0) { |
| 1515 | check->server->check.health = 0; |
| 1516 | status = HCHK_STATUS_L7STS; |
| 1517 | hs = cmd; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1518 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1519 | else if (strcasecmp(cmd, "fail") == 0) { |
| 1520 | check->server->check.health = 0; |
| 1521 | status = HCHK_STATUS_L7STS; |
| 1522 | hs = cmd; |
| 1523 | } |
| 1524 | /* admin statuses */ |
| 1525 | else if (strcasecmp(cmd, "ready") == 0) { |
| 1526 | as = cmd; |
| 1527 | } |
| 1528 | else if (strcasecmp(cmd, "drain") == 0) { |
| 1529 | as = cmd; |
| 1530 | } |
| 1531 | else if (strcasecmp(cmd, "maint") == 0) { |
| 1532 | as = cmd; |
| 1533 | } |
| 1534 | /* try to parse a weight here and keep the last one */ |
| 1535 | else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) { |
| 1536 | ps = cmd; |
| 1537 | } |
| 1538 | /* try to parse a maxconn here */ |
| 1539 | else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) { |
| 1540 | cs = cmd; |
| 1541 | } |
| 1542 | else { |
| 1543 | /* keep a copy of the first error */ |
| 1544 | if (!err) |
| 1545 | err = cmd; |
| 1546 | } |
| 1547 | /* skip to next word */ |
| 1548 | cmd = p; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1549 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1550 | /* here, cmd points either to \0 or to the beginning of a |
| 1551 | * description. Skip possible leading spaces. |
| 1552 | */ |
| 1553 | while (*cmd == ' ' || *cmd == '\n') |
| 1554 | cmd++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1555 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1556 | /* First, update the admin status so that we avoid sending other |
| 1557 | * possibly useless warnings and can also update the health if |
| 1558 | * present after going back up. |
| 1559 | */ |
| 1560 | if (as) { |
| 1561 | if (strcasecmp(as, "drain") == 0) |
| 1562 | srv_adm_set_drain(check->server); |
| 1563 | else if (strcasecmp(as, "maint") == 0) |
| 1564 | srv_adm_set_maint(check->server); |
| 1565 | else |
| 1566 | srv_adm_set_ready(check->server); |
| 1567 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1568 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1569 | /* now change weights */ |
| 1570 | if (ps) { |
| 1571 | const char *msg; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 1572 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1573 | msg = server_parse_weight_change_request(check->server, ps); |
| 1574 | if (!wrn || !*wrn) |
| 1575 | wrn = msg; |
| 1576 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1577 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1578 | if (cs) { |
| 1579 | const char *msg; |
Willy Tarreau | 6dd4ac8 | 2019-09-03 18:55:02 +0200 | [diff] [blame] | 1580 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1581 | cs += strlen("maxconn:"); |
Willy Tarreau | 6dd4ac8 | 2019-09-03 18:55:02 +0200 | [diff] [blame] | 1582 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1583 | msg = server_parse_maxconn_change_request(check->server, cs); |
| 1584 | if (!wrn || !*wrn) |
| 1585 | wrn = msg; |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1586 | } |
| 1587 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1588 | /* and finally health status */ |
| 1589 | if (hs) { |
| 1590 | /* We'll report some of the warnings and errors we have |
| 1591 | * here. Down reports are critical, we leave them untouched. |
| 1592 | * Lack of report, or report of 'UP' leaves the room for |
| 1593 | * ERR first, then WARN. |
| 1594 | */ |
| 1595 | const char *msg = cmd; |
| 1596 | struct buffer *t; |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1597 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1598 | if (!*msg || status == HCHK_STATUS_L7OKD) { |
| 1599 | if (err && *err) |
| 1600 | msg = err; |
| 1601 | else if (wrn && *wrn) |
| 1602 | msg = wrn; |
| 1603 | } |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 1604 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1605 | t = get_trash_chunk(); |
| 1606 | chunk_printf(t, "via agent : %s%s%s%s", |
| 1607 | hs, *msg ? " (" : "", |
| 1608 | msg, *msg ? ")" : ""); |
| 1609 | set_server_check_status(check, status, t->area); |
| 1610 | } |
| 1611 | else if (err && *err) { |
| 1612 | /* No status change but we'd like to report something odd. |
| 1613 | * Just report the current state and copy the message. |
| 1614 | */ |
| 1615 | chunk_printf(&trash, "agent reports an error : %s", err); |
| 1616 | set_server_check_status(check, status/*check->status*/, trash.area); |
| 1617 | } |
| 1618 | else if (wrn && *wrn) { |
| 1619 | /* No status change but we'd like to report something odd. |
| 1620 | * Just report the current state and copy the message. |
| 1621 | */ |
| 1622 | chunk_printf(&trash, "agent warns : %s", wrn); |
| 1623 | set_server_check_status(check, status/*check->status*/, trash.area); |
| 1624 | } |
| 1625 | else |
| 1626 | set_server_check_status(check, status, NULL); |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 1627 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1628 | out: |
| 1629 | return ret; |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1630 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1631 | wait_more_data: |
| 1632 | ret = TCPCHK_EVAL_WAIT; |
| 1633 | goto out; |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1634 | } |
| 1635 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1636 | /* Evaluates a TCPCHK_ACT_CONNECT rule. Returns TCPCHK_EVAL_WAIT to wait the |
| 1637 | * connection establishment, TCPCHK_EVAL_CONTINUE to evaluate the next rule or |
| 1638 | * TCPCHK_EVAL_STOP if an error occurred. |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1639 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1640 | static enum tcpcheck_eval_ret tcpcheck_eval_connect(struct check *check, struct tcpcheck_rule *rule) |
Willy Tarreau | 865c514 | 2016-12-21 20:04:48 +0100 | [diff] [blame] | 1641 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1642 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 1643 | struct tcpcheck_connect *connect = &rule->connect; |
| 1644 | struct proxy *proxy = check->proxy; |
| 1645 | struct server *s = check->server; |
| 1646 | struct task *t = check->task; |
| 1647 | struct conn_stream *cs; |
| 1648 | struct connection *conn = NULL; |
| 1649 | struct protocol *proto; |
| 1650 | struct xprt_ops *xprt; |
| 1651 | int status, port; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1652 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1653 | /* For a connect action we'll create a new connection. We may also have |
| 1654 | * to kill a previous one. But we don't want to leave *without* a |
| 1655 | * connection if we came here from the connection layer, hence with a |
| 1656 | * connection. Thus we'll proceed in the following order : |
| 1657 | * 1: close but not release previous connection (handled by the caller) |
| 1658 | * 2: try to get a new connection |
| 1659 | * 3: release and replace the old one on success |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 1660 | */ |
Willy Tarreau | e7b7348 | 2013-11-21 11:50:50 +0100 | [diff] [blame] | 1661 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1662 | /* 2- prepare new connection */ |
| 1663 | cs = cs_new(NULL); |
| 1664 | if (!cs) { |
| 1665 | chunk_printf(&trash, "TCPCHK error allocating connection at step %d", |
| 1666 | tcpcheck_get_step_id(check, rule)); |
| 1667 | if (rule->comment) |
| 1668 | chunk_appendf(&trash, " comment: '%s'", rule->comment); |
| 1669 | set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.area); |
| 1670 | ret = TCPCHK_EVAL_STOP; |
| 1671 | goto out; |
| 1672 | } |
Willy Tarreau | 15f3910 | 2013-12-11 20:41:18 +0100 | [diff] [blame] | 1673 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1674 | /* 3- release and replace the old one on success */ |
| 1675 | if (check->cs) { |
| 1676 | if (check->wait_list.events) |
| 1677 | cs->conn->xprt->unsubscribe(cs->conn, cs->conn->xprt_ctx, |
| 1678 | check->wait_list.events, &check->wait_list); |
| 1679 | |
| 1680 | /* We may have been scheduled to run, and the I/O handler |
| 1681 | * expects to have a cs, so remove the tasklet |
| 1682 | */ |
| 1683 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
| 1684 | cs_destroy(check->cs); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1685 | } |
| 1686 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1687 | tasklet_set_tid(check->wait_list.tasklet, tid); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1688 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1689 | check->cs = cs; |
| 1690 | conn = cs->conn; |
| 1691 | conn_set_owner(conn, check->sess, NULL); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1692 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1693 | /* Maybe there were an older connection we were waiting on */ |
| 1694 | check->wait_list.events = 0; |
| 1695 | conn->target = s ? &s->obj_type : &proxy->obj_type; |
| 1696 | |
| 1697 | /* no client address */ |
| 1698 | if (!sockaddr_alloc(&conn->dst)) { |
| 1699 | status = SF_ERR_RESOURCE; |
| 1700 | goto fail_check; |
| 1701 | } |
| 1702 | |
| 1703 | /* connect to the connect rule addr if specified, otherwise the check |
| 1704 | * addr if specified on the server. otherwise, use the server addr |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1705 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1706 | *conn->dst = (is_addr(&connect->addr) |
| 1707 | ? connect->addr |
| 1708 | : (is_addr(&check->addr) ? check->addr : s->addr)); |
| 1709 | proto = protocol_by_family(conn->dst->ss_family); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1710 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1711 | port = 0; |
| 1712 | if (!port && connect->port) |
| 1713 | port = connect->port; |
| 1714 | if (!port && connect->port_expr) { |
| 1715 | struct sample *smp; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1716 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1717 | smp = sample_fetch_as_type(check->proxy, check->sess, NULL, |
| 1718 | SMP_OPT_DIR_REQ | SMP_OPT_FINAL, |
| 1719 | connect->port_expr, SMP_T_SINT); |
| 1720 | if (smp) |
| 1721 | port = smp->data.u.sint; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1722 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1723 | if (!port && is_inet_addr(&connect->addr)) |
| 1724 | port = get_host_port(&connect->addr); |
| 1725 | if (!port && check->port) |
| 1726 | port = check->port; |
| 1727 | if (!port && is_inet_addr(&check->addr)) |
| 1728 | port = get_host_port(&check->addr); |
| 1729 | if (!port) |
| 1730 | port = s->svc_port; |
| 1731 | set_host_port(conn->dst, port); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 1732 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1733 | xprt = ((connect->options & TCPCHK_OPT_SSL) |
| 1734 | ? xprt_get(XPRT_SSL) |
| 1735 | : ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) ? check->xprt : xprt_get(XPRT_RAW))); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1736 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1737 | conn_prepare(conn, proto, xprt); |
| 1738 | cs_attach(cs, check, &check_conn_cb); |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 1739 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1740 | status = SF_ERR_INTERNAL; |
| 1741 | if (proto && proto->connect) { |
| 1742 | struct tcpcheck_rule *next; |
| 1743 | int flags = 0; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1744 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1745 | if (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) |
| 1746 | flags |= CONNECT_HAS_DATA; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1747 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1748 | next = get_next_tcpcheck_rule(check->tcpcheck_rules, rule); |
| 1749 | if (!next || next->action != TCPCHK_ACT_EXPECT) |
| 1750 | flags |= CONNECT_DELACK_ALWAYS; |
| 1751 | status = proto->connect(conn, flags); |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1752 | } |
| 1753 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1754 | if (status != SF_ERR_NONE) |
| 1755 | goto fail_check; |
Christopher Faulet | 799f3a4 | 2020-04-07 12:06:14 +0200 | [diff] [blame] | 1756 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1757 | conn->flags |= CO_FL_PRIVATE; |
| 1758 | conn->ctx = cs; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1759 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1760 | /* The mux may be initialized now if there isn't server attached to the |
| 1761 | * check (email alerts) or if there is a mux proto specified or if there |
| 1762 | * is no alpn. |
| 1763 | */ |
| 1764 | if (!s || connect->mux_proto || check->mux_proto || (!connect->alpn && !check->alpn_str)) { |
| 1765 | const struct mux_ops *mux_ops; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1766 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1767 | if (connect->mux_proto) |
| 1768 | mux_ops = connect->mux_proto->mux; |
| 1769 | else if (check->mux_proto) |
| 1770 | mux_ops = check->mux_proto->mux; |
| 1771 | else { |
| 1772 | int mode = ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK |
| 1773 | ? PROTO_MODE_HTTP |
| 1774 | : PROTO_MODE_TCP); |
| 1775 | |
| 1776 | mux_ops = conn_get_best_mux(conn, IST_NULL, PROTO_SIDE_BE, mode); |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1777 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1778 | if (mux_ops && conn_install_mux(conn, mux_ops, cs, proxy, check->sess) < 0) { |
| 1779 | status = SF_ERR_INTERNAL; |
| 1780 | goto fail_check; |
| 1781 | } |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1782 | } |
| 1783 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1784 | #ifdef USE_OPENSSL |
| 1785 | if (connect->sni) |
| 1786 | ssl_sock_set_servername(conn, connect->sni); |
| 1787 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s->check.sni) |
| 1788 | ssl_sock_set_servername(conn, s->check.sni); |
| 1789 | |
| 1790 | if (connect->alpn) |
| 1791 | ssl_sock_set_alpn(conn, (unsigned char *)connect->alpn, connect->alpn_len); |
| 1792 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s->check.alpn_str) |
| 1793 | ssl_sock_set_alpn(conn, (unsigned char *)s->check.alpn_str, s->check.alpn_len); |
| 1794 | #endif |
| 1795 | if ((connect->options & TCPCHK_OPT_SOCKS4) && (s->flags & SRV_F_SOCKS4_PROXY)) { |
| 1796 | conn->send_proxy_ofs = 1; |
| 1797 | conn->flags |= CO_FL_SOCKS4; |
| 1798 | } |
| 1799 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s->check.via_socks4 && (s->flags & SRV_F_SOCKS4_PROXY)) { |
| 1800 | conn->send_proxy_ofs = 1; |
| 1801 | conn->flags |= CO_FL_SOCKS4; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1802 | } |
| 1803 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1804 | if (connect->options & TCPCHK_OPT_SEND_PROXY) { |
| 1805 | conn->send_proxy_ofs = 1; |
| 1806 | conn->flags |= CO_FL_SEND_PROXY; |
| 1807 | } |
| 1808 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s->check.send_proxy && !(check->state & CHK_ST_AGENT)) { |
| 1809 | conn->send_proxy_ofs = 1; |
| 1810 | conn->flags |= CO_FL_SEND_PROXY; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1811 | } |
| 1812 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1813 | if (conn_ctrl_ready(conn) && (connect->options & TCPCHK_OPT_LINGER)) { |
| 1814 | /* Some servers don't like reset on close */ |
| 1815 | fdtab[cs->conn->handle.fd].linger_risk = 0; |
| 1816 | } |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1817 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1818 | if (conn_ctrl_ready(conn) && (conn->flags & (CO_FL_SEND_PROXY | CO_FL_SOCKS4))) { |
| 1819 | if (xprt_add_hs(conn) < 0) |
| 1820 | status = SF_ERR_RESOURCE; |
| 1821 | } |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1822 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1823 | fail_check: |
| 1824 | /* It can return one of : |
| 1825 | * - SF_ERR_NONE if everything's OK |
| 1826 | * - SF_ERR_SRVTO if there are no more servers |
| 1827 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 1828 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1829 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1830 | * - SF_ERR_INTERNAL for any other purely internal errors |
| 1831 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
| 1832 | * Note that we try to prevent the network stack from sending the ACK during the |
| 1833 | * connect() when a pure TCP check is used (without PROXY protocol). |
| 1834 | */ |
| 1835 | switch (status) { |
| 1836 | case SF_ERR_NONE: |
| 1837 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1838 | * to establish but only when timeout.check is set as it may be |
| 1839 | * to short for a full check otherwise |
| 1840 | */ |
| 1841 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1842 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1843 | if (proxy->timeout.check && proxy->timeout.connect) { |
| 1844 | int t_con = tick_add(now_ms, proxy->timeout.connect); |
| 1845 | t->expire = tick_first(t->expire, t_con); |
| 1846 | } |
| 1847 | break; |
| 1848 | case SF_ERR_SRVTO: /* ETIMEDOUT */ |
| 1849 | case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
| 1850 | case SF_ERR_PRXCOND: |
| 1851 | case SF_ERR_RESOURCE: |
| 1852 | case SF_ERR_INTERNAL: |
| 1853 | chk_report_conn_err(check, errno, 0); |
| 1854 | ret = TCPCHK_EVAL_STOP; |
| 1855 | goto out; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1856 | } |
| 1857 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1858 | /* don't do anything until the connection is established */ |
| 1859 | if (conn->flags & CO_FL_WAIT_XPRT) { |
| 1860 | ret = TCPCHK_EVAL_WAIT; |
| 1861 | goto out; |
| 1862 | } |
| 1863 | |
| 1864 | out: |
| 1865 | if (conn && check->result == CHK_RES_FAILED) |
| 1866 | conn->flags |= CO_FL_ERROR; |
| 1867 | return ret; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1868 | } |
| 1869 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1870 | /* Evaluates a TCPCHK_ACT_SEND rule. Returns TCPCHK_EVAL_WAIT if outgoing data |
| 1871 | * were not fully sent, TCPCHK_EVAL_CONTINUE to evaluate the next rule or |
| 1872 | * TCPCHK_EVAL_STOP if an error occurred. |
| 1873 | */ |
| 1874 | static enum tcpcheck_eval_ret tcpcheck_eval_send(struct check *check, struct tcpcheck_rule *rule) |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1875 | { |
| 1876 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1877 | struct tcpcheck_send *send = &rule->send; |
| 1878 | struct conn_stream *cs = check->cs; |
| 1879 | struct connection *conn = cs_conn(cs); |
| 1880 | struct buffer *tmp = NULL; |
| 1881 | struct htx *htx = NULL; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1882 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1883 | /* reset the read & write buffer */ |
| 1884 | b_reset(&check->bi); |
| 1885 | b_reset(&check->bo); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1886 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1887 | switch (send->type) { |
| 1888 | case TCPCHK_SEND_STRING: |
| 1889 | case TCPCHK_SEND_BINARY: |
| 1890 | if (istlen(send->data) >= b_size(&check->bo)) { |
| 1891 | chunk_printf(&trash, "tcp-check send : string too large (%u) for buffer size (%u) at step %d", |
| 1892 | (unsigned int)istlen(send->data), (unsigned int)b_size(&check->bo), |
| 1893 | tcpcheck_get_step_id(check, rule)); |
| 1894 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area); |
| 1895 | ret = TCPCHK_EVAL_STOP; |
| 1896 | goto out; |
| 1897 | } |
| 1898 | b_putist(&check->bo, send->data); |
| 1899 | break; |
| 1900 | case TCPCHK_SEND_STRING_LF: |
| 1901 | check->bo.data = sess_build_logline(check->sess, NULL, b_orig(&check->bo), b_size(&check->bo), &rule->send.fmt); |
| 1902 | if (!b_data(&check->bo)) |
| 1903 | goto out; |
| 1904 | break; |
| 1905 | case TCPCHK_SEND_BINARY_LF: |
| 1906 | tmp = alloc_trash_chunk(); |
| 1907 | if (!tmp) |
| 1908 | goto error_lf; |
| 1909 | tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &rule->send.fmt); |
| 1910 | if (!b_data(tmp)) |
| 1911 | goto out; |
| 1912 | tmp->area[tmp->data] = '\0'; |
| 1913 | b_set_data(&check->bo, b_size(&check->bo)); |
| 1914 | if (parse_binary(b_orig(tmp), &check->bo.area, (int *)&check->bo.data, NULL) == 0) |
| 1915 | goto error_lf; |
| 1916 | break; |
| 1917 | case TCPCHK_SEND_HTTP: { |
| 1918 | struct htx_sl *sl; |
| 1919 | struct ist meth, uri, vsn, clen, body; |
| 1920 | unsigned int slflags = 0; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1921 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1922 | tmp = alloc_trash_chunk(); |
| 1923 | if (!tmp) |
| 1924 | goto error_htx; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1925 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1926 | meth = ((send->http.meth.meth == HTTP_METH_OTHER) |
| 1927 | ? ist2(send->http.meth.str.area, send->http.meth.str.data) |
| 1928 | : http_known_methods[send->http.meth.meth]); |
| 1929 | uri = (isttest(send->http.uri) ? send->http.uri : ist("/")); // TODO: handle uri_fmt |
| 1930 | vsn = (isttest(send->http.vsn) ? send->http.vsn : ist("HTTP/1.0")); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1931 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1932 | if (istlen(vsn) == 8 && |
| 1933 | (*(vsn.ptr+5) > '1' || (*(vsn.ptr+5) == '1' && *(vsn.ptr+7) >= '1'))) |
| 1934 | slflags |= HTX_SL_F_VER_11; |
| 1935 | slflags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 1936 | if (!isttest(send->http.body)) |
| 1937 | slflags |= HTX_SL_F_BODYLESS; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1938 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1939 | htx = htx_from_buf(&check->bo); |
| 1940 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, slflags, meth, uri, vsn); |
| 1941 | if (!sl) |
| 1942 | goto error_htx; |
| 1943 | sl->info.req.meth = send->http.meth.meth; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1944 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1945 | body = send->http.body; // TODO: handle body_fmt |
| 1946 | clen = ist((!istlen(body) ? "0" : ultoa(istlen(body)))); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1947 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1948 | if (!htx_add_header(htx, ist("Connection"), ist("close")) || |
| 1949 | !htx_add_header(htx, ist("Content-length"), clen)) |
| 1950 | goto error_htx; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1951 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1952 | if (!LIST_ISEMPTY(&send->http.hdrs)) { |
| 1953 | struct tcpcheck_http_hdr *hdr; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1954 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1955 | list_for_each_entry(hdr, &send->http.hdrs, list) { |
| 1956 | chunk_reset(tmp); |
| 1957 | tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &hdr->value); |
| 1958 | if (!b_data(tmp)) |
| 1959 | continue; |
| 1960 | if (!htx_add_header(htx, hdr->name, ist2(b_orig(tmp), b_data(tmp)))) |
| 1961 | goto error_htx; |
| 1962 | } |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1963 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1964 | } |
| 1965 | if (check->proxy->options2 & PR_O2_CHK_SNDST) { |
| 1966 | chunk_reset(tmp); |
| 1967 | httpchk_build_status_header(check->server, tmp); |
| 1968 | if (!htx_add_header(htx, ist("X-Haproxy-Server-State"), ist2(b_orig(tmp), b_data(tmp)))) |
| 1969 | goto error_htx; |
| 1970 | } |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1971 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1972 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 1973 | (istlen(body) && !htx_add_data_atonce(htx, send->http.body)) || |
| 1974 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 1975 | goto error_htx; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1976 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1977 | htx_to_buf(htx, &check->bo); |
| 1978 | break; |
| 1979 | } |
| 1980 | case TCPCHK_SEND_UNDEF: |
| 1981 | /* Should never happen. */ |
| 1982 | ret = TCPCHK_EVAL_STOP; |
| 1983 | goto out; |
| 1984 | }; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1985 | |
Christopher Faulet | 6d47121 | 2020-04-22 11:09:25 +0200 | [diff] [blame] | 1986 | |
| 1987 | if (conn->mux->snd_buf(cs, &check->bo, |
| 1988 | (IS_HTX_CONN(conn) ? (htxbuf(&check->bo))->data: b_data(&check->bo)), 0) <= 0) { |
Christopher Faulet | 815516d | 2020-04-21 13:02:14 +0200 | [diff] [blame] | 1989 | if ((conn->flags & CO_FL_ERROR) || (cs->flags & CS_FL_ERROR)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1990 | ret = TCPCHK_EVAL_STOP; |
Christopher Faulet | 815516d | 2020-04-21 13:02:14 +0200 | [diff] [blame] | 1991 | goto out; |
| 1992 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1993 | } |
Christopher Faulet | 6d47121 | 2020-04-22 11:09:25 +0200 | [diff] [blame] | 1994 | if ((IS_HTX_CONN(conn) && !htx_is_empty(htxbuf(&check->bo))) || b_data(&check->bo)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1995 | cs->conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list); |
| 1996 | ret = TCPCHK_EVAL_WAIT; |
| 1997 | goto out; |
| 1998 | } |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1999 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2000 | out: |
| 2001 | free_trash_chunk(tmp); |
| 2002 | return ret; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2003 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2004 | error_htx: |
| 2005 | if (htx) { |
| 2006 | htx_reset(htx); |
| 2007 | htx_to_buf(htx, &check->bo); |
| 2008 | } |
| 2009 | chunk_printf(&trash, "tcp-check send : failed to build HTTP request at step %d", |
| 2010 | tcpcheck_get_step_id(check, rule)); |
| 2011 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area); |
| 2012 | ret = TCPCHK_EVAL_STOP; |
| 2013 | goto out; |
| 2014 | |
| 2015 | error_lf: |
| 2016 | chunk_printf(&trash, "tcp-check send : failed to build log-format string at step %d", |
| 2017 | tcpcheck_get_step_id(check, rule)); |
| 2018 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area); |
| 2019 | ret = TCPCHK_EVAL_STOP; |
| 2020 | goto out; |
| 2021 | |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2022 | } |
| 2023 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2024 | /* Try to reveice data before evaluting a tcp-check expect rule. Returns |
| 2025 | * TCPCHK_EVAL_WAIT if it is already subcribed on receive events or if nothing |
| 2026 | * was received, TCPCHK_EVAL_CONTINUE to evaluate the expect rule or |
| 2027 | * TCPCHK_EVAL_STOP if an error occurred. |
| 2028 | */ |
| 2029 | static enum tcpcheck_eval_ret tcpcheck_eval_recv(struct check *check, struct tcpcheck_rule *rule) |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2030 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2031 | struct conn_stream *cs = check->cs; |
| 2032 | struct connection *conn = cs_conn(cs); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2033 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2034 | size_t max, read, cur_read = 0; |
| 2035 | int is_empty; |
| 2036 | int read_poll = MAX_READ_POLL_LOOPS; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2037 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2038 | if (check->wait_list.events & SUB_RETRY_RECV) |
| 2039 | goto wait_more_data; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2040 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2041 | if (cs->flags & CS_FL_EOS) |
| 2042 | goto end_recv; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2043 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2044 | /* errors on the connection and the conn-stream were already checked */ |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2045 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2046 | /* prepare to detect if the mux needs more room */ |
| 2047 | cs->flags &= ~CS_FL_WANT_ROOM; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2048 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2049 | while ((cs->flags & CS_FL_RCV_MORE) || |
| 2050 | (!(conn->flags & CO_FL_ERROR) && !(cs->flags & (CS_FL_ERROR|CS_FL_EOS)))) { |
| 2051 | max = (IS_HTX_CS(cs) ? htx_free_space(htxbuf(&check->bi)) : b_room(&check->bi)); |
| 2052 | read = conn->mux->rcv_buf(cs, &check->bi, max, 0); |
| 2053 | cur_read += read; |
| 2054 | if (!read || |
| 2055 | (cs->flags & CS_FL_WANT_ROOM) || |
| 2056 | (--read_poll <= 0) || |
| 2057 | (read < max && read >= global.tune.recv_enough)) |
| 2058 | break; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2059 | } |
| 2060 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2061 | end_recv: |
| 2062 | is_empty = (IS_HTX_CS(cs) ? htx_is_empty(htxbuf(&check->bi)) : !b_data(&check->bi)); |
| 2063 | if (is_empty && ((conn->flags & CO_FL_ERROR) || (cs->flags & CS_FL_ERROR))) { |
| 2064 | /* Report network errors only if we got no other data. Otherwise |
| 2065 | * we'll let the upper layers decide whether the response is OK |
| 2066 | * or not. It is very common that an RST sent by the server is |
| 2067 | * reported as an error just after the last data chunk. |
| 2068 | */ |
| 2069 | goto stop; |
| 2070 | } |
| 2071 | if (!cur_read) { |
| 2072 | if (!(cs->flags & (CS_FL_WANT_ROOM|CS_FL_ERROR|CS_FL_EOS))) { |
| 2073 | conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list); |
| 2074 | goto wait_more_data; |
| 2075 | } |
| 2076 | if (is_empty) { |
| 2077 | chunk_printf(&trash, "TCPCHK got an empty response at step %d", |
| 2078 | tcpcheck_get_step_id(check, rule)); |
| 2079 | if (rule->comment) |
| 2080 | chunk_appendf(&trash, " comment: '%s'", rule->comment); |
| 2081 | set_server_check_status(check, rule->expect.err_status, trash.area); |
| 2082 | goto stop; |
| 2083 | } |
| 2084 | } |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2085 | |
| 2086 | out: |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2087 | return ret; |
| 2088 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2089 | stop: |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2090 | ret = TCPCHK_EVAL_STOP; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2091 | goto out; |
| 2092 | |
| 2093 | wait_more_data: |
| 2094 | ret = TCPCHK_EVAL_WAIT; |
| 2095 | goto out; |
| 2096 | } |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2097 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2098 | /* Evaluates an HTTP TCPCHK_ACT_EXPECT rule. If <last_read> is set , no more data |
| 2099 | * are expected. Returns TCPCHK_EVAL_WAIT to wait for more data, |
| 2100 | * TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if an |
| 2101 | * error occurred. |
| 2102 | */ |
| 2103 | static enum tcpcheck_eval_ret tcpcheck_eval_expect_http(struct check *check, struct tcpcheck_rule *rule, int last_read) |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2104 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2105 | struct htx *htx = htxbuf(&check->bi); |
| 2106 | struct htx_sl *sl; |
| 2107 | struct htx_blk *blk; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2108 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2109 | struct tcpcheck_expect *expect = &rule->expect; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2110 | struct buffer *msg = NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2111 | enum healthcheck_status status; |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2112 | struct ist desc = IST_NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2113 | int match, inverse; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2114 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2115 | last_read |= (!htx_free_space(htx) || (htx_get_tail_type(htx) == HTX_BLK_EOM)); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2116 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2117 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 2118 | status = HCHK_STATUS_L7RSP; |
| 2119 | goto error; |
| 2120 | } |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2121 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2122 | if (htx_is_empty(htx)) { |
| 2123 | if (last_read) { |
| 2124 | status = HCHK_STATUS_L7RSP; |
| 2125 | goto error; |
| 2126 | } |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2127 | goto wait_more_data; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2128 | } |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2129 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2130 | sl = http_get_stline(htx); |
| 2131 | check->code = sl->info.res.status; |
| 2132 | |
| 2133 | if (check->server && |
| 2134 | (check->server->proxy->options & PR_O_DISABLE404) && |
| 2135 | (check->server->next_state != SRV_ST_STOPPED) && |
| 2136 | (check->code == 404)) { |
| 2137 | /* 404 may be accepted as "stopping" only if the server was up */ |
| 2138 | goto out; |
| 2139 | } |
| 2140 | |
| 2141 | inverse = !!(expect->flags & TCPCHK_EXPT_FL_INV); |
| 2142 | /* Make GCC happy ; initialize match to a failure state. */ |
| 2143 | match = inverse; |
| 2144 | |
| 2145 | switch (expect->type) { |
| 2146 | case TCPCHK_EXPECT_HTTP_STATUS: |
| 2147 | match = isteq(htx_sl_res_code(sl), expect->data); |
| 2148 | |
| 2149 | /* Set status and description in case of error */ |
| 2150 | status = HCHK_STATUS_L7STS; |
| 2151 | desc = htx_sl_res_reason(sl); |
| 2152 | break; |
| 2153 | case TCPCHK_EXPECT_HTTP_REGEX_STATUS: |
| 2154 | match = regex_exec2(expect->regex, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 2155 | |
| 2156 | /* Set status and description in case of error */ |
| 2157 | status = HCHK_STATUS_L7STS; |
| 2158 | desc = htx_sl_res_reason(sl); |
| 2159 | break; |
| 2160 | |
| 2161 | case TCPCHK_EXPECT_HTTP_BODY: |
| 2162 | case TCPCHK_EXPECT_HTTP_REGEX_BODY: |
| 2163 | chunk_reset(&trash); |
| 2164 | for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 2165 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 2166 | |
| 2167 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 2168 | break; |
| 2169 | if (type == HTX_BLK_DATA) { |
| 2170 | if (!chunk_istcat(&trash, htx_get_blk_value(htx, blk))) |
| 2171 | break; |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | if (!b_data(&trash)) { |
| 2176 | if (!last_read) |
| 2177 | goto wait_more_data; |
| 2178 | status = HCHK_STATUS_L7RSP; |
| 2179 | desc = ist("HTTP content check could not find a response body"); |
| 2180 | goto error; |
| 2181 | } |
| 2182 | |
| 2183 | if (!last_read && |
| 2184 | ((expect->type == TCPCHK_EXPECT_HTTP_BODY && b_data(&trash) < istlen(expect->data)) || |
| 2185 | (expect->min_recv > 0 && b_data(&trash) < expect->min_recv))) { |
| 2186 | ret = TCPCHK_EVAL_WAIT; |
| 2187 | goto out; |
| 2188 | } |
| 2189 | |
| 2190 | if (expect->type ==TCPCHK_EXPECT_HTTP_BODY) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2191 | match = my_memmem(b_orig(&trash), b_data(&trash), istptr(expect->data), istlen(expect->data)) != NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2192 | else |
| 2193 | match = regex_exec2(expect->regex, b_orig(&trash), b_data(&trash)); |
| 2194 | |
| 2195 | /* Set status and description in case of error */ |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2196 | status = HCHK_STATUS_L7RSP; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2197 | desc = (inverse |
| 2198 | ? ist("HTTP check matched unwanted content") |
| 2199 | : ist("HTTP content check did not match")); |
| 2200 | break; |
| 2201 | |
| 2202 | default: |
| 2203 | /* should never happen */ |
| 2204 | status = HCHK_STATUS_L7RSP; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2205 | goto error; |
| 2206 | } |
| 2207 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2208 | /* Wait for more data on mismatch only if no minimum is defined (-1), |
| 2209 | * otherwise the absence of match is already conclusive. |
| 2210 | */ |
| 2211 | if (!match && !last_read && (expect->min_recv == -1)) { |
| 2212 | ret = TCPCHK_EVAL_WAIT; |
| 2213 | goto out; |
| 2214 | } |
| 2215 | |
| 2216 | if (!(match ^ inverse)) |
| 2217 | goto error; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2218 | |
| 2219 | out: |
| 2220 | free_trash_chunk(msg); |
| 2221 | return ret; |
| 2222 | |
| 2223 | error: |
| 2224 | ret = TCPCHK_EVAL_STOP; |
| 2225 | msg = alloc_trash_chunk(); |
| 2226 | if (msg) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2227 | tcpcheck_expect_onerror_message(msg, check, rule, 0, desc); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2228 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
| 2229 | goto out; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2230 | |
| 2231 | wait_more_data: |
| 2232 | ret = TCPCHK_EVAL_WAIT; |
| 2233 | goto out; |
| 2234 | } |
| 2235 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2236 | /* Evaluates a TCP TCPCHK_ACT_EXPECT rule. Returns TCPCHK_EVAL_WAIT to wait for |
| 2237 | * more data, TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP |
| 2238 | * if an error occurred. |
| 2239 | */ |
| 2240 | static enum tcpcheck_eval_ret tcpcheck_eval_expect(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 2241 | { |
| 2242 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 2243 | struct tcpcheck_expect *expect = &rule->expect; |
| 2244 | struct buffer *msg = NULL; |
| 2245 | int match, inverse; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2246 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2247 | last_read |= b_full(&check->bi); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2248 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2249 | /* The current expect might need more data than the previous one, check again |
| 2250 | * that the minimum amount data required to match is respected. |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2251 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2252 | if (!last_read) { |
| 2253 | if ((expect->type == TCPCHK_EXPECT_STRING || expect->type == TCPCHK_EXPECT_BINARY) && |
| 2254 | (b_data(&check->bi) < istlen(expect->data))) { |
| 2255 | ret = TCPCHK_EVAL_WAIT; |
| 2256 | goto out; |
| 2257 | } |
| 2258 | if (expect->min_recv > 0 && (b_data(&check->bi) < expect->min_recv)) { |
| 2259 | ret = TCPCHK_EVAL_WAIT; |
| 2260 | goto out; |
| 2261 | } |
| 2262 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2263 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2264 | inverse = !!(expect->flags & TCPCHK_EXPT_FL_INV); |
| 2265 | /* Make GCC happy ; initialize match to a failure state. */ |
| 2266 | match = inverse; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2267 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2268 | switch (expect->type) { |
| 2269 | case TCPCHK_EXPECT_STRING: |
| 2270 | case TCPCHK_EXPECT_BINARY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2271 | match = my_memmem(b_head(&check->bi), b_data(&check->bi), istptr(expect->data), istlen(expect->data)) != NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2272 | break; |
| 2273 | case TCPCHK_EXPECT_REGEX: |
| 2274 | if (expect->flags & TCPCHK_EXPT_FL_CAP) |
| 2275 | match = regex_exec_match2(expect->regex, b_head(&check->bi), MIN(b_data(&check->bi), b_size(&check->bi)-1), |
| 2276 | MAX_MATCH, pmatch, 0); |
| 2277 | else |
| 2278 | match = regex_exec2(expect->regex, b_head(&check->bi), MIN(b_data(&check->bi), b_size(&check->bi)-1)); |
| 2279 | break; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2280 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2281 | case TCPCHK_EXPECT_REGEX_BINARY: |
| 2282 | chunk_reset(&trash); |
| 2283 | dump_binary(&trash, b_head(&check->bi), b_data(&check->bi)); |
| 2284 | if (expect->flags & TCPCHK_EXPT_FL_CAP) |
| 2285 | match = regex_exec_match2(expect->regex, b_head(&trash), MIN(b_data(&trash), b_size(&trash)-1), |
| 2286 | MAX_MATCH, pmatch, 0); |
| 2287 | else |
| 2288 | match = regex_exec2(expect->regex, b_head(&trash), MIN(b_data(&trash), b_size(&trash)-1)); |
| 2289 | break; |
| 2290 | case TCPCHK_EXPECT_CUSTOM: |
| 2291 | if (expect->custom) |
| 2292 | ret = expect->custom(check, rule, last_read); |
| 2293 | goto out; |
| 2294 | default: |
| 2295 | /* Should never happen. */ |
| 2296 | ret = TCPCHK_EVAL_STOP; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2297 | goto out; |
| 2298 | } |
| 2299 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2300 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2301 | /* Wait for more data on mismatch only if no minimum is defined (-1), |
| 2302 | * otherwise the absence of match is already conclusive. |
| 2303 | */ |
| 2304 | if (!match && !last_read && (expect->min_recv == -1)) { |
| 2305 | ret = TCPCHK_EVAL_WAIT; |
| 2306 | goto out; |
| 2307 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2308 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2309 | /* Result as expected, next rule. */ |
| 2310 | if (match ^ inverse) |
| 2311 | goto out; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2312 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2313 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2314 | /* From this point on, we matched something we did not want, this is an error state. */ |
| 2315 | ret = TCPCHK_EVAL_STOP; |
| 2316 | msg = alloc_trash_chunk(); |
| 2317 | if (msg) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2318 | tcpcheck_expect_onerror_message(msg, check, rule, match, IST_NULL); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2319 | set_server_check_status(check, expect->err_status, (msg ? b_head(msg) : NULL)); |
| 2320 | free_trash_chunk(msg); |
| 2321 | ret = TCPCHK_EVAL_STOP; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2322 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2323 | out: |
| 2324 | return ret; |
| 2325 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2326 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2327 | /* Evaluates a TCPCHK_ACT_ACTION_KW rule. Returns TCPCHK_EVAL_CONTINUE to |
| 2328 | * evaluate the next rule or TCPCHK_EVAL_STOP if an error occurred. It nevers |
| 2329 | * waits. |
| 2330 | */ |
| 2331 | static enum tcpcheck_eval_ret tcpcheck_eval_action_kw(struct check *check, struct tcpcheck_rule *rule) |
| 2332 | { |
| 2333 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 2334 | struct act_rule *act_rule; |
| 2335 | enum act_return act_ret; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2336 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2337 | act_rule =rule->action_kw.rule; |
| 2338 | act_ret = act_rule->action_ptr(act_rule, check->proxy, check->sess, NULL, 0); |
| 2339 | if (act_ret != ACT_RET_CONT) { |
| 2340 | chunk_printf(&trash, "TCPCHK ACTION unexpected result at step %d\n", |
| 2341 | tcpcheck_get_step_id(check, rule)); |
| 2342 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area); |
| 2343 | ret = TCPCHK_EVAL_STOP; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2344 | } |
| 2345 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2346 | return ret; |
| 2347 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2348 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2349 | /* Executes a tcp-check ruleset. Note that this is called both from the |
| 2350 | * connection's wake() callback and from the check scheduling task. It returns |
| 2351 | * 0 on normal cases, or <0 if a close() has happened on an existing connection, |
| 2352 | * presenting the risk of an fd replacement. |
| 2353 | * |
| 2354 | * Please do NOT place any return statement in this function and only leave |
| 2355 | * via the out_end_tcpcheck label after setting retcode. |
| 2356 | */ |
| 2357 | static int tcpcheck_main(struct check *check) |
| 2358 | { |
| 2359 | struct tcpcheck_rule *rule; |
| 2360 | struct conn_stream *cs = check->cs; |
| 2361 | struct connection *conn = cs_conn(cs); |
| 2362 | int must_read = 1, last_read = 0; |
| 2363 | int ret, retcode = 0; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2364 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2365 | /* here, we know that the check is complete or that it failed */ |
| 2366 | if (check->result != CHK_RES_UNKNOWN) |
| 2367 | goto out_end_tcpcheck; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2368 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2369 | /* 1- check for connection error, if any */ |
| 2370 | if ((conn && conn->flags & CO_FL_ERROR) || (cs && cs->flags & CS_FL_ERROR)) |
| 2371 | goto out_end_tcpcheck; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2372 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2373 | /* 2- check if we are waiting for the connection establishment. It only |
| 2374 | * happens during TCPCHK_ACT_CONNECT. */ |
| 2375 | if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 2376 | rule = LIST_NEXT(&check->current_step->list, typeof(rule), list); |
| 2377 | if (conn && (conn->flags & CO_FL_WAIT_XPRT)) { |
| 2378 | if (rule->action == TCPCHK_ACT_SEND) |
| 2379 | conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list); |
| 2380 | else if (rule->action == TCPCHK_ACT_EXPECT) |
| 2381 | conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list); |
| 2382 | goto out; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2383 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2384 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2385 | |
| 2386 | /* 3- check for pending outgoing data. It only happens during |
| 2387 | * TCPCHK_ACT_SEND. */ |
| 2388 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) { |
| 2389 | if (conn && b_data(&check->bo)) { |
Christopher Faulet | 6d47121 | 2020-04-22 11:09:25 +0200 | [diff] [blame] | 2390 | ret = conn->mux->snd_buf(cs, &check->bo, |
| 2391 | (IS_HTX_CONN(conn) ? (htxbuf(&check->bo))->data: b_data(&check->bo)), 0); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2392 | if (ret <= 0) { |
| 2393 | if ((conn && conn->flags & CO_FL_ERROR) || (cs && cs->flags & CS_FL_ERROR)) |
| 2394 | goto out_end_tcpcheck; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2395 | } |
Christopher Faulet | 6d47121 | 2020-04-22 11:09:25 +0200 | [diff] [blame] | 2396 | if ((IS_HTX_CONN(conn) && !htx_is_empty(htxbuf(&check->bo))) || b_data(&check->bo)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2397 | cs->conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list); |
| 2398 | goto out; |
| 2399 | } |
| 2400 | } |
| 2401 | rule = LIST_NEXT(&check->current_step->list, typeof(rule), list); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2402 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2403 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2404 | /* 4- check if a rule must be resume. It happens if check->current_step |
| 2405 | * is defined. */ |
| 2406 | else if (check->current_step) |
| 2407 | rule = check->current_step; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2408 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2409 | /* 5- It is the first evaluation. We must create a session and preset |
| 2410 | * tcp-check variables */ |
| 2411 | else { |
| 2412 | struct tcpcheck_var *var; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2413 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2414 | /* First evaluation, create a session */ |
| 2415 | check->sess = session_new(&checks_fe, NULL, &check->obj_type); |
| 2416 | if (!check->sess) { |
| 2417 | chunk_printf(&trash, "TCPCHK error allocating check session"); |
| 2418 | set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.area); |
| 2419 | goto out_end_tcpcheck; |
| 2420 | } |
| 2421 | vars_init(&check->vars, SCOPE_CHECK); |
| 2422 | rule = LIST_NEXT(check->tcpcheck_rules->list, typeof(rule), list); |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2423 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2424 | /* Preset tcp-check variables */ |
| 2425 | list_for_each_entry(var, &check->tcpcheck_rules->preset_vars, list) { |
| 2426 | struct sample smp; |
Willy Tarreau | 449f952 | 2015-05-13 15:39:48 +0200 | [diff] [blame] | 2427 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2428 | memset(&smp, 0, sizeof(smp)); |
| 2429 | smp_set_owner(&smp, check->proxy, check->sess, NULL, SMP_OPT_FINAL); |
| 2430 | smp.data = var->data; |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2431 | vars_set_by_name_ifexist(istptr(var->name), istlen(var->name), &smp); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2432 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2433 | } |
| 2434 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2435 | /* Now evaluate the tcp-check rules */ |
Willy Tarreau | deccd11 | 2018-06-14 18:38:55 +0200 | [diff] [blame] | 2436 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2437 | list_for_each_entry_from(rule, check->tcpcheck_rules->list, list) { |
| 2438 | enum tcpcheck_eval_ret eval_ret; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2439 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2440 | check->code = 0; |
| 2441 | switch (rule->action) { |
| 2442 | case TCPCHK_ACT_CONNECT: |
| 2443 | check->current_step = rule; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2444 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2445 | /* close but not release yet previous connection */ |
| 2446 | if (check->cs) { |
| 2447 | cs_close(check->cs); |
| 2448 | retcode = -1; /* do not reuse the fd in the caller! */ |
| 2449 | } |
| 2450 | eval_ret = tcpcheck_eval_connect(check, rule); |
| 2451 | must_read = 1; last_read = 0; |
| 2452 | break; |
| 2453 | case TCPCHK_ACT_SEND: |
| 2454 | check->current_step = rule; |
| 2455 | eval_ret = tcpcheck_eval_send(check, rule); |
| 2456 | must_read = 1; |
| 2457 | break; |
| 2458 | case TCPCHK_ACT_EXPECT: |
| 2459 | check->current_step = rule; |
| 2460 | if (must_read) { |
| 2461 | if (check->proxy->timeout.check) |
| 2462 | check->task->expire = tick_add_ifset(now_ms, check->proxy->timeout.check); |
Willy Tarreau | f3d3482 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 2463 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2464 | eval_ret = tcpcheck_eval_recv(check, rule); |
| 2465 | if (eval_ret == TCPCHK_EVAL_STOP) |
| 2466 | goto out_end_tcpcheck; |
| 2467 | else if (eval_ret == TCPCHK_EVAL_WAIT) |
| 2468 | goto out; |
| 2469 | last_read = ((conn->flags & CO_FL_ERROR) || (cs->flags & (CS_FL_ERROR|CS_FL_EOS))); |
| 2470 | must_read = 0; |
| 2471 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2472 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2473 | eval_ret = ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK |
| 2474 | ? tcpcheck_eval_expect_http(check, rule, last_read) |
| 2475 | : tcpcheck_eval_expect(check, rule, last_read)); |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 2476 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2477 | if (eval_ret == TCPCHK_EVAL_WAIT) { |
| 2478 | check->current_step = rule->expect.head; |
| 2479 | conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list); |
| 2480 | } |
| 2481 | break; |
| 2482 | case TCPCHK_ACT_ACTION_KW: |
| 2483 | /* Don't update the current step */ |
| 2484 | eval_ret = tcpcheck_eval_action_kw(check, rule); |
| 2485 | break; |
| 2486 | default: |
| 2487 | /* Otherwise, just go to the next one and don't update |
| 2488 | * the current step |
| 2489 | */ |
| 2490 | eval_ret = TCPCHK_EVAL_CONTINUE; |
| 2491 | break; |
| 2492 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 2493 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2494 | switch (eval_ret) { |
| 2495 | case TCPCHK_EVAL_CONTINUE: |
| 2496 | break; |
| 2497 | case TCPCHK_EVAL_WAIT: |
| 2498 | goto out; |
| 2499 | case TCPCHK_EVAL_STOP: |
| 2500 | goto out_end_tcpcheck; |
| 2501 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 2502 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2503 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2504 | /* All rules was evaluated */ |
| 2505 | if (check->current_step) { |
| 2506 | rule = check->current_step; |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 2507 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2508 | if (rule->action == TCPCHK_ACT_EXPECT) { |
| 2509 | struct buffer *msg; |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 2510 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2511 | if (check->server && |
| 2512 | (check->server->proxy->options & PR_O_DISABLE404) && |
| 2513 | (check->server->next_state != SRV_ST_STOPPED) && |
| 2514 | (check->code == 404)) { |
| 2515 | set_server_check_status(check, HCHK_STATUS_L7OKCD, NULL); |
| 2516 | goto out_end_tcpcheck; |
| 2517 | } |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 2518 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2519 | msg = alloc_trash_chunk(); |
| 2520 | if (msg) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2521 | tcpcheck_expect_onsuccess_message(msg, check, rule, IST_NULL); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2522 | set_server_check_status(check, rule->expect.ok_status, |
| 2523 | (msg ? b_head(msg) : "(tcp-check)")); |
| 2524 | free_trash_chunk(msg); |
| 2525 | } |
| 2526 | else if (rule->action == TCPCHK_ACT_CONNECT) { |
| 2527 | const char *msg = ((rule->connect.options & TCPCHK_OPT_IMPLICIT) ? NULL : "(tcp-check)"); |
| 2528 | enum healthcheck_status status = ((conn && ssl_sock_is_ssl(conn)) ? HCHK_STATUS_L6OK : HCHK_STATUS_L4OK); |
Olivier Houchard | ff1e9f3 | 2019-09-20 17:18:35 +0200 | [diff] [blame] | 2529 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2530 | set_server_check_status(check, status, msg); |
| 2531 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2532 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2533 | else |
| 2534 | set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)"); |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2535 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2536 | out_end_tcpcheck: |
| 2537 | if ((conn && conn->flags & CO_FL_ERROR) || (cs && cs->flags & CS_FL_ERROR)) |
| 2538 | chk_report_conn_err(check, errno, 0); |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2539 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2540 | /* cleanup before leaving */ |
| 2541 | check->current_step = NULL; |
| 2542 | if (check->sess != NULL) { |
| 2543 | vars_prune(&check->vars, check->sess, NULL); |
| 2544 | session_free(check->sess); |
| 2545 | check->sess = NULL; |
| 2546 | } |
| 2547 | out: |
| 2548 | return retcode; |
| 2549 | } |
Christopher Faulet | bb591a1 | 2020-04-01 16:52:17 +0200 | [diff] [blame] | 2550 | |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2551 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2552 | /**************************************************************************/ |
| 2553 | /************** Health-checks based on an external process ****************/ |
| 2554 | /**************************************************************************/ |
| 2555 | static struct list pid_list = LIST_HEAD_INIT(pid_list); |
| 2556 | static struct pool_head *pool_head_pid_list; |
| 2557 | __decl_spinlock(pid_list_lock); |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2558 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2559 | struct extcheck_env { |
| 2560 | char *name; /* environment variable name */ |
| 2561 | int vmaxlen; /* value maximum length, used to determine the required memory allocation */ |
| 2562 | }; |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2563 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2564 | /* environment variables memory requirement for different types of data */ |
| 2565 | #define EXTCHK_SIZE_EVAL_INIT 0 /* size determined during the init phase, |
| 2566 | * such environment variables are not updatable. */ |
| 2567 | #define EXTCHK_SIZE_ULONG 20 /* max string length for an unsigned long value */ |
| 2568 | #define EXTCHK_SIZE_UINT 11 /* max string length for an unsigned int value */ |
| 2569 | #define EXTCHK_SIZE_ADDR INET6_ADDRSTRLEN+1 /* max string length for an address */ |
Christopher Faulet | bb591a1 | 2020-04-01 16:52:17 +0200 | [diff] [blame] | 2570 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2571 | /* external checks environment variables */ |
| 2572 | enum { |
| 2573 | EXTCHK_PATH = 0, |
Willy Tarreau | ca79f59 | 2019-07-17 19:04:47 +0200 | [diff] [blame] | 2574 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2575 | /* Proxy specific environment variables */ |
| 2576 | EXTCHK_HAPROXY_PROXY_NAME, /* the backend name */ |
| 2577 | EXTCHK_HAPROXY_PROXY_ID, /* the backend id */ |
| 2578 | EXTCHK_HAPROXY_PROXY_ADDR, /* the first bind address if available (or empty) */ |
| 2579 | EXTCHK_HAPROXY_PROXY_PORT, /* the first bind port if available (or empty) */ |
Christopher Faulet | bb591a1 | 2020-04-01 16:52:17 +0200 | [diff] [blame] | 2580 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2581 | /* Server specific environment variables */ |
| 2582 | EXTCHK_HAPROXY_SERVER_NAME, /* the server name */ |
| 2583 | EXTCHK_HAPROXY_SERVER_ID, /* the server id */ |
| 2584 | EXTCHK_HAPROXY_SERVER_ADDR, /* the server address */ |
| 2585 | EXTCHK_HAPROXY_SERVER_PORT, /* the server port if available (or empty) */ |
| 2586 | EXTCHK_HAPROXY_SERVER_MAXCONN, /* the server max connections */ |
| 2587 | EXTCHK_HAPROXY_SERVER_CURCONN, /* the current number of connections on the server */ |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2588 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2589 | EXTCHK_SIZE |
| 2590 | }; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2591 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2592 | const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = { |
| 2593 | [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT }, |
| 2594 | [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT }, |
| 2595 | [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT }, |
| 2596 | [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT }, |
| 2597 | [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT }, |
| 2598 | [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT }, |
| 2599 | [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT }, |
| 2600 | [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_ADDR }, |
| 2601 | [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_UINT }, |
| 2602 | [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT }, |
| 2603 | [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG }, |
| 2604 | }; |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 2605 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2606 | void block_sigchld(void) |
| 2607 | { |
| 2608 | sigset_t set; |
| 2609 | sigemptyset(&set); |
| 2610 | sigaddset(&set, SIGCHLD); |
| 2611 | assert(ha_sigmask(SIG_BLOCK, &set, NULL) == 0); |
| 2612 | } |
Willy Tarreau | be37315 | 2018-09-06 11:45:30 +0200 | [diff] [blame] | 2613 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2614 | void unblock_sigchld(void) |
| 2615 | { |
| 2616 | sigset_t set; |
| 2617 | sigemptyset(&set); |
| 2618 | sigaddset(&set, SIGCHLD); |
| 2619 | assert(ha_sigmask(SIG_UNBLOCK, &set, NULL) == 0); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2620 | } |
Willy Tarreau | 2ab5c38 | 2019-07-17 18:48:07 +0200 | [diff] [blame] | 2621 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2622 | static struct pid_list *pid_list_add(pid_t pid, struct task *t) |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2623 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2624 | struct pid_list *elem; |
| 2625 | struct check *check = t->context; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2626 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2627 | elem = pool_alloc(pool_head_pid_list); |
| 2628 | if (!elem) |
| 2629 | return NULL; |
| 2630 | elem->pid = pid; |
| 2631 | elem->t = t; |
| 2632 | elem->exited = 0; |
| 2633 | check->curpid = elem; |
| 2634 | LIST_INIT(&elem->list); |
Gaetan Rivet | 08fdcb3 | 2020-02-28 11:04:21 +0100 | [diff] [blame] | 2635 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2636 | HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock); |
| 2637 | LIST_ADD(&pid_list, &elem->list); |
| 2638 | HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock); |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2639 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2640 | return elem; |
| 2641 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2642 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2643 | static void pid_list_del(struct pid_list *elem) |
| 2644 | { |
| 2645 | struct check *check; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2646 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2647 | if (!elem) |
| 2648 | return; |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2649 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2650 | HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock); |
| 2651 | LIST_DEL(&elem->list); |
| 2652 | HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock); |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2653 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2654 | if (!elem->exited) |
| 2655 | kill(elem->pid, SIGTERM); |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2656 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2657 | check = elem->t->context; |
| 2658 | check->curpid = NULL; |
| 2659 | pool_free(pool_head_pid_list, elem); |
| 2660 | } |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2661 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2662 | /* Called from inside SIGCHLD handler, SIGCHLD is blocked */ |
| 2663 | static void pid_list_expire(pid_t pid, int status) |
| 2664 | { |
| 2665 | struct pid_list *elem; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2666 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2667 | HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock); |
| 2668 | list_for_each_entry(elem, &pid_list, list) { |
| 2669 | if (elem->pid == pid) { |
| 2670 | elem->t->expire = now_ms; |
| 2671 | elem->status = status; |
| 2672 | elem->exited = 1; |
| 2673 | task_wakeup(elem->t, TASK_WOKEN_IO); |
| 2674 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2675 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2676 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2677 | HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock); |
| 2678 | } |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 2679 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2680 | static void sigchld_handler(struct sig_handler *sh) |
| 2681 | { |
| 2682 | pid_t pid; |
| 2683 | int status; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2684 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2685 | while ((pid = waitpid(0, &status, WNOHANG)) > 0) |
| 2686 | pid_list_expire(pid, status); |
| 2687 | } |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 2688 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2689 | static int init_pid_list(void) |
| 2690 | { |
| 2691 | if (pool_head_pid_list != NULL) |
| 2692 | /* Nothing to do */ |
| 2693 | return 0; |
| 2694 | |
| 2695 | if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) { |
| 2696 | ha_alert("Failed to set signal handler for external health checks: %s. Aborting.\n", |
| 2697 | strerror(errno)); |
| 2698 | return 1; |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2699 | } |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2700 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2701 | pool_head_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED); |
| 2702 | if (pool_head_pid_list == NULL) { |
| 2703 | ha_alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n", |
| 2704 | strerror(errno)); |
| 2705 | return 1; |
| 2706 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2707 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2708 | return 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2709 | } |
| 2710 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2711 | /* helper macro to set an environment variable and jump to a specific label on failure. */ |
| 2712 | #define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; } |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 2713 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2714 | /* |
| 2715 | * helper function to allocate enough memory to store an environment variable. |
| 2716 | * It will also check that the environment variable is updatable, and silently |
| 2717 | * fail if not. |
| 2718 | */ |
| 2719 | static int extchk_setenv(struct check *check, int idx, const char *value) |
| 2720 | { |
| 2721 | int len, ret; |
| 2722 | char *envname; |
| 2723 | int vmaxlen; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 2724 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2725 | if (idx < 0 || idx >= EXTCHK_SIZE) { |
| 2726 | ha_alert("Illegal environment variable index %d. Aborting.\n", idx); |
| 2727 | return 1; |
| 2728 | } |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 2729 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2730 | envname = extcheck_envs[idx].name; |
| 2731 | vmaxlen = extcheck_envs[idx].vmaxlen; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 2732 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2733 | /* Check if the environment variable is already set, and silently reject |
| 2734 | * the update if this one is not updatable. */ |
| 2735 | if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx])) |
| 2736 | return 0; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 2737 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2738 | /* Instead of sending NOT_USED, sending an empty value is preferable */ |
| 2739 | if (strcmp(value, "NOT_USED") == 0) { |
| 2740 | value = ""; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 2741 | } |
| 2742 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2743 | len = strlen(envname) + 1; |
| 2744 | if (vmaxlen == EXTCHK_SIZE_EVAL_INIT) |
| 2745 | len += strlen(value); |
| 2746 | else |
| 2747 | len += vmaxlen; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 2748 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2749 | if (!check->envp[idx]) |
| 2750 | check->envp[idx] = malloc(len + 1); |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 2751 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2752 | if (!check->envp[idx]) { |
| 2753 | ha_alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname); |
| 2754 | return 1; |
| 2755 | } |
| 2756 | ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value); |
| 2757 | if (ret < 0) { |
| 2758 | ha_alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno)); |
| 2759 | return 1; |
| 2760 | } |
| 2761 | else if (ret > len) { |
| 2762 | ha_alert("Environment variable '%s' was truncated. Aborting.\n", envname); |
| 2763 | return 1; |
| 2764 | } |
| 2765 | return 0; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 2766 | } |
| 2767 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2768 | static int prepare_external_check(struct check *check) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2769 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2770 | struct server *s = check->server; |
| 2771 | struct proxy *px = s->proxy; |
| 2772 | struct listener *listener = NULL, *l; |
| 2773 | int i; |
| 2774 | const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH; |
| 2775 | char buf[256]; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2776 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2777 | list_for_each_entry(l, &px->conf.listeners, by_fe) |
| 2778 | /* Use the first INET, INET6 or UNIX listener */ |
| 2779 | if (l->addr.ss_family == AF_INET || |
| 2780 | l->addr.ss_family == AF_INET6 || |
| 2781 | l->addr.ss_family == AF_UNIX) { |
| 2782 | listener = l; |
| 2783 | break; |
| 2784 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2785 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2786 | check->curpid = NULL; |
| 2787 | check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *)); |
| 2788 | if (!check->envp) { |
| 2789 | ha_alert("Failed to allocate memory for environment variables. Aborting\n"); |
| 2790 | goto err; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2791 | } |
| 2792 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2793 | check->argv = calloc(6, sizeof(char *)); |
| 2794 | if (!check->argv) { |
| 2795 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 2796 | goto err; |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2797 | } |
| 2798 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2799 | check->argv[0] = px->check_command; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2800 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2801 | if (!listener) { |
| 2802 | check->argv[1] = strdup("NOT_USED"); |
| 2803 | check->argv[2] = strdup("NOT_USED"); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2804 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2805 | else if (listener->addr.ss_family == AF_INET || |
| 2806 | listener->addr.ss_family == AF_INET6) { |
| 2807 | addr_to_str(&listener->addr, buf, sizeof(buf)); |
| 2808 | check->argv[1] = strdup(buf); |
| 2809 | port_to_str(&listener->addr, buf, sizeof(buf)); |
| 2810 | check->argv[2] = strdup(buf); |
| 2811 | } |
| 2812 | else if (listener->addr.ss_family == AF_UNIX) { |
| 2813 | const struct sockaddr_un *un; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2814 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2815 | un = (struct sockaddr_un *)&listener->addr; |
| 2816 | check->argv[1] = strdup(un->sun_path); |
| 2817 | check->argv[2] = strdup("NOT_USED"); |
| 2818 | } |
| 2819 | else { |
| 2820 | ha_alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id); |
| 2821 | goto err; |
| 2822 | } |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2823 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2824 | if (!check->argv[1] || !check->argv[2]) { |
| 2825 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 2826 | goto err; |
| 2827 | } |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2828 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2829 | check->argv[3] = calloc(EXTCHK_SIZE_ADDR, sizeof(*check->argv[3])); |
| 2830 | check->argv[4] = calloc(EXTCHK_SIZE_UINT, sizeof(*check->argv[4])); |
| 2831 | if (!check->argv[3] || !check->argv[4]) { |
| 2832 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 2833 | goto err; |
| 2834 | } |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2835 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2836 | addr_to_str(&s->addr, check->argv[3], EXTCHK_SIZE_ADDR); |
| 2837 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
| 2838 | snprintf(check->argv[4], EXTCHK_SIZE_UINT, "%u", s->svc_port); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2839 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2840 | for (i = 0; i < 5; i++) { |
| 2841 | if (!check->argv[i]) { |
| 2842 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 2843 | goto err; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2844 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2845 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2846 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2847 | EXTCHK_SETENV(check, EXTCHK_PATH, path, err); |
| 2848 | /* Add proxy environment variables */ |
| 2849 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err); |
| 2850 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err); |
| 2851 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err); |
| 2852 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err); |
| 2853 | /* Add server environment variables */ |
| 2854 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err); |
| 2855 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err); |
| 2856 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err); |
| 2857 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err); |
| 2858 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err); |
| 2859 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2860 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2861 | /* Ensure that we don't leave any hole in check->envp */ |
| 2862 | for (i = 0; i < EXTCHK_SIZE; i++) |
| 2863 | if (!check->envp[i]) |
| 2864 | EXTCHK_SETENV(check, i, "", err); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2865 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2866 | return 1; |
| 2867 | err: |
| 2868 | if (check->envp) { |
| 2869 | for (i = 0; i < EXTCHK_SIZE; i++) |
| 2870 | free(check->envp[i]); |
| 2871 | free(check->envp); |
| 2872 | check->envp = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2873 | } |
| 2874 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2875 | if (check->argv) { |
| 2876 | for (i = 1; i < 5; i++) |
| 2877 | free(check->argv[i]); |
| 2878 | free(check->argv); |
| 2879 | check->argv = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2880 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2881 | return 0; |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2882 | } |
Gaetan Rivet | f8ba677 | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 2883 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2884 | /* |
| 2885 | * establish a server health-check that makes use of a process. |
| 2886 | * |
| 2887 | * It can return one of : |
| 2888 | * - SF_ERR_NONE if everything's OK |
| 2889 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 2890 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
| 2891 | * |
| 2892 | * Blocks and then unblocks SIGCHLD |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2893 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2894 | static int connect_proc_chk(struct task *t) |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2895 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2896 | char buf[256]; |
| 2897 | struct check *check = t->context; |
| 2898 | struct server *s = check->server; |
| 2899 | struct proxy *px = s->proxy; |
| 2900 | int status; |
| 2901 | pid_t pid; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2902 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2903 | status = SF_ERR_RESOURCE; |
Willy Tarreau | f2c8735 | 2015-05-13 12:08:21 +0200 | [diff] [blame] | 2904 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2905 | block_sigchld(); |
Willy Tarreau | 7df8ca6 | 2019-07-15 10:57:51 +0200 | [diff] [blame] | 2906 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2907 | pid = fork(); |
| 2908 | if (pid < 0) { |
| 2909 | ha_alert("Failed to fork process for external health check%s: %s. Aborting.\n", |
| 2910 | (global.tune.options & GTUNE_INSECURE_FORK) ? |
| 2911 | "" : " (likely caused by missing 'insecure-fork-wanted')", |
| 2912 | strerror(errno)); |
| 2913 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2914 | goto out; |
| 2915 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2916 | if (pid == 0) { |
| 2917 | /* Child */ |
| 2918 | extern char **environ; |
| 2919 | struct rlimit limit; |
| 2920 | int fd; |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 2921 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2922 | /* close all FDs. Keep stdin/stdout/stderr in verbose mode */ |
| 2923 | fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2924 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2925 | my_closefrom(fd); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2926 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2927 | /* restore the initial FD limits */ |
| 2928 | limit.rlim_cur = rlim_fd_cur_at_boot; |
| 2929 | limit.rlim_max = rlim_fd_max_at_boot; |
| 2930 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 2931 | getrlimit(RLIMIT_NOFILE, &limit); |
| 2932 | ha_warning("External check: failed to restore initial FD limits (cur=%u max=%u), using cur=%u max=%u\n", |
| 2933 | rlim_fd_cur_at_boot, rlim_fd_max_at_boot, |
| 2934 | (unsigned int)limit.rlim_cur, (unsigned int)limit.rlim_max); |
| 2935 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2936 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2937 | environ = check->envp; |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2938 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2939 | /* Update some environment variables and command args: curconn, server addr and server port */ |
| 2940 | extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf))); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2941 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2942 | addr_to_str(&s->addr, check->argv[3], EXTCHK_SIZE_ADDR); |
| 2943 | extchk_setenv(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3]); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2944 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2945 | *check->argv[4] = 0; |
| 2946 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
| 2947 | snprintf(check->argv[4], EXTCHK_SIZE_UINT, "%u", s->svc_port); |
| 2948 | extchk_setenv(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4]); |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 2949 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2950 | haproxy_unblock_signals(); |
| 2951 | execvp(px->check_command, check->argv); |
| 2952 | ha_alert("Failed to exec process for external health check: %s. Aborting.\n", |
| 2953 | strerror(errno)); |
| 2954 | exit(-1); |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 2955 | } |
| 2956 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2957 | /* Parent */ |
| 2958 | if (check->result == CHK_RES_UNKNOWN) { |
| 2959 | if (pid_list_add(pid, t) != NULL) { |
| 2960 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 2961 | |
| 2962 | if (px->timeout.check && px->timeout.connect) { |
| 2963 | int t_con = tick_add(now_ms, px->timeout.connect); |
| 2964 | t->expire = tick_first(t->expire, t_con); |
| 2965 | } |
| 2966 | status = SF_ERR_NONE; |
| 2967 | goto out; |
| 2968 | } |
| 2969 | else { |
| 2970 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 2971 | } |
| 2972 | kill(pid, SIGTERM); /* process creation error */ |
| 2973 | } |
| 2974 | else |
| 2975 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 2976 | |
| 2977 | out: |
| 2978 | unblock_sigchld(); |
| 2979 | return status; |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 2980 | } |
| 2981 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2982 | /* |
| 2983 | * manages a server health-check that uses an external process. Returns |
| 2984 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2985 | * |
| 2986 | * Please do NOT place any return statement in this function and only leave |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2987 | * via the out_unlock label. |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2988 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2989 | static struct task *process_chk_proc(struct task *t, void *context, unsigned short state) |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2990 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2991 | struct check *check = context; |
| 2992 | struct server *s = check->server; |
| 2993 | int rv; |
| 2994 | int ret; |
| 2995 | int expired = tick_is_expired(t->expire, now_ms); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2996 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2997 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 2998 | if (!(check->state & CHK_ST_INPROGRESS)) { |
| 2999 | /* no check currently running */ |
| 3000 | if (!expired) /* woke up too early */ |
| 3001 | goto out_unlock; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3002 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3003 | /* we don't send any health-checks when the proxy is |
| 3004 | * stopped, the server should not be checked or the check |
| 3005 | * is disabled. |
| 3006 | */ |
| 3007 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
| 3008 | s->proxy->state == PR_STSTOPPED) |
| 3009 | goto reschedule; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3010 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3011 | /* we'll initiate a new check */ |
| 3012 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Christopher Faulet | 370e0f1 | 2020-04-16 09:52:42 +0200 | [diff] [blame] | 3013 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3014 | check->state |= CHK_ST_INPROGRESS; |
| 3015 | |
| 3016 | ret = connect_proc_chk(t); |
| 3017 | if (ret == SF_ERR_NONE) { |
| 3018 | /* the process was forked, we allow up to min(inter, |
| 3019 | * timeout.connect) for it to report its status, but |
| 3020 | * only when timeout.check is set as it may be to short |
| 3021 | * for a full check otherwise. |
| 3022 | */ |
| 3023 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 3024 | |
| 3025 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 3026 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 3027 | t->expire = tick_first(t->expire, t_con); |
Christopher Faulet | 370e0f1 | 2020-04-16 09:52:42 +0200 | [diff] [blame] | 3028 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3029 | task_set_affinity(t, tid_bit); |
| 3030 | goto reschedule; |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3031 | } |
Gaetan Rivet | efab6c6 | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3032 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3033 | /* here, we failed to start the check */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3034 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3035 | check->state &= ~CHK_ST_INPROGRESS; |
| 3036 | check_notify_failure(check); |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3037 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3038 | /* we allow up to min(inter, timeout.connect) for a connection |
| 3039 | * to establish but only when timeout.check is set |
| 3040 | * as it may be to short for a full check otherwise |
| 3041 | */ |
| 3042 | while (tick_is_expired(t->expire, now_ms)) { |
| 3043 | int t_con; |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3044 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3045 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 3046 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3047 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3048 | if (s->proxy->timeout.check) |
| 3049 | t->expire = tick_first(t->expire, t_con); |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3050 | } |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 3051 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3052 | else { |
| 3053 | /* there was a test running. |
| 3054 | * First, let's check whether there was an uncaught error, |
| 3055 | * which can happen on connect timeout or error. |
| 3056 | */ |
| 3057 | if (check->result == CHK_RES_UNKNOWN) { |
| 3058 | /* good connection is enough for pure TCP check */ |
| 3059 | struct pid_list *elem = check->curpid; |
| 3060 | int status = HCHK_STATUS_UNKNOWN; |
Christopher Faulet | 370e0f1 | 2020-04-16 09:52:42 +0200 | [diff] [blame] | 3061 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3062 | if (elem->exited) { |
| 3063 | status = elem->status; /* Save in case the process exits between use below */ |
| 3064 | if (!WIFEXITED(status)) |
| 3065 | check->code = -1; |
| 3066 | else |
| 3067 | check->code = WEXITSTATUS(status); |
| 3068 | if (!WIFEXITED(status) || WEXITSTATUS(status)) |
| 3069 | status = HCHK_STATUS_PROCERR; |
| 3070 | else |
| 3071 | status = HCHK_STATUS_PROCOK; |
| 3072 | } else if (expired) { |
| 3073 | status = HCHK_STATUS_PROCTOUT; |
| 3074 | ha_warning("kill %d\n", (int)elem->pid); |
| 3075 | kill(elem->pid, SIGTERM); |
| 3076 | } |
| 3077 | set_server_check_status(check, status, NULL); |
| 3078 | } |
Willy Tarreau | f2c8735 | 2015-05-13 12:08:21 +0200 | [diff] [blame] | 3079 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3080 | if (check->result == CHK_RES_FAILED) { |
| 3081 | /* a failure or timeout detected */ |
| 3082 | check_notify_failure(check); |
| 3083 | } |
| 3084 | else if (check->result == CHK_RES_CONDPASS) { |
| 3085 | /* check is OK but asks for stopping mode */ |
| 3086 | check_notify_stopping(check); |
| 3087 | } |
| 3088 | else if (check->result == CHK_RES_PASSED) { |
| 3089 | /* a success was detected */ |
| 3090 | check_notify_success(check); |
| 3091 | } |
| 3092 | task_set_affinity(t, 1); |
| 3093 | check->state &= ~CHK_ST_INPROGRESS; |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 3094 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3095 | pid_list_del(check->curpid); |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 3096 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3097 | rv = 0; |
| 3098 | if (global.spread_checks > 0) { |
| 3099 | rv = srv_getinter(check) * global.spread_checks / 100; |
| 3100 | rv -= (int) (2 * rv * (ha_random32() / 4294967295.0)); |
| 3101 | } |
| 3102 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); |
| 3103 | } |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 3104 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3105 | reschedule: |
| 3106 | while (tick_is_expired(t->expire, now_ms)) |
| 3107 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3108 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3109 | out_unlock: |
| 3110 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
| 3111 | return t; |
| 3112 | } |
Baptiste Assmann | 248f117 | 2018-03-01 21:49:01 +0100 | [diff] [blame] | 3113 | |
Baptiste Assmann | 248f117 | 2018-03-01 21:49:01 +0100 | [diff] [blame] | 3114 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3115 | /**************************************************************************/ |
| 3116 | /***************** Health-checks based on connections *********************/ |
| 3117 | /**************************************************************************/ |
| 3118 | /* This function is used only for server health-checks. It handles connection |
| 3119 | * status updates including errors. If necessary, it wakes the check task up. |
| 3120 | * It returns 0 on normal cases, <0 if at least one close() has happened on the |
| 3121 | * connection (eg: reconnect). It relies on tcpcheck_main(). |
| 3122 | */ |
| 3123 | static int wake_srv_chk(struct conn_stream *cs) |
| 3124 | { |
| 3125 | struct connection *conn = cs->conn; |
| 3126 | struct check *check = cs->data; |
| 3127 | struct email_alertq *q = container_of(check, typeof(*q), check); |
| 3128 | int ret = 0; |
Christopher Faulet | df38f88 | 2020-04-07 16:04:38 +0200 | [diff] [blame] | 3129 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3130 | if (check->server) |
| 3131 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 3132 | else |
| 3133 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3134 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3135 | /* we may have to make progress on the TCP checks */ |
| 3136 | ret = tcpcheck_main(check); |
Christopher Faulet | df38f88 | 2020-04-07 16:04:38 +0200 | [diff] [blame] | 3137 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3138 | cs = check->cs; |
| 3139 | conn = cs->conn; |
Christopher Faulet | df38f88 | 2020-04-07 16:04:38 +0200 | [diff] [blame] | 3140 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3141 | if (unlikely(conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)) { |
| 3142 | /* We may get error reports bypassing the I/O handlers, typically |
| 3143 | * the case when sending a pure TCP check which fails, then the I/O |
| 3144 | * handlers above are not called. This is completely handled by the |
| 3145 | * main processing task so let's simply wake it up. If we get here, |
| 3146 | * we expect errno to still be valid. |
| 3147 | */ |
| 3148 | chk_report_conn_err(check, errno, 0); |
| 3149 | task_wakeup(check->task, TASK_WOKEN_IO); |
| 3150 | } |
| 3151 | |
| 3152 | if (check->result != CHK_RES_UNKNOWN) { |
| 3153 | /* Check complete or aborted. If connection not yet closed do it |
| 3154 | * now and wake the check task up to be sure the result is |
| 3155 | * handled ASAP. */ |
| 3156 | conn_sock_drain(conn); |
| 3157 | cs_close(cs); |
| 3158 | ret = -1; |
| 3159 | /* We may have been scheduled to run, and the |
| 3160 | * I/O handler expects to have a cs, so remove |
| 3161 | * the tasklet |
| 3162 | */ |
| 3163 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
| 3164 | task_wakeup(check->task, TASK_WOKEN_IO); |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 3165 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3166 | |
| 3167 | if (check->server) |
| 3168 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 3169 | else |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3170 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3171 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3172 | /* if a connection got replaced, we must absolutely prevent the connection |
| 3173 | * handler from touching its fd, and perform the FD polling updates ourselves |
| 3174 | */ |
| 3175 | if (ret < 0) |
| 3176 | conn_cond_update_polling(conn); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3177 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3178 | return ret; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3179 | } |
| 3180 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3181 | /* This function checks if any I/O is wanted, and if so, attempts to do so */ |
| 3182 | static struct task *event_srv_chk_io(struct task *t, void *ctx, unsigned short state) |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 3183 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3184 | struct check *check = ctx; |
| 3185 | struct conn_stream *cs = check->cs; |
| 3186 | struct email_alertq *q = container_of(check, typeof(*q), check); |
| 3187 | int ret = 0; |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 3188 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3189 | if (!(check->wait_list.events & SUB_RETRY_SEND)) |
| 3190 | ret = wake_srv_chk(cs); |
| 3191 | if (ret == 0 && !(check->wait_list.events & SUB_RETRY_RECV)) { |
| 3192 | if (check->server) |
| 3193 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 3194 | else |
| 3195 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 3196 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3197 | if (unlikely(check->result == CHK_RES_FAILED)) { |
| 3198 | /* collect possible new errors */ |
| 3199 | if (cs->conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) |
| 3200 | chk_report_conn_err(check, 0, 0); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3201 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3202 | /* Reset the check buffer... */ |
| 3203 | b_reset(&check->bi); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3204 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3205 | /* Close the connection... We still attempt to nicely close if, |
| 3206 | * for instance, SSL needs to send a "close notify." Later, we perform |
| 3207 | * a hard close and reset the connection if some data are pending, |
| 3208 | * otherwise we end up with many TIME_WAITs and eat all the source port |
| 3209 | * range quickly. To avoid sending RSTs all the time, we first try to |
| 3210 | * drain pending data. |
| 3211 | */ |
| 3212 | /* Call cs_shutr() first, to add the CO_FL_SOCK_RD_SH flag on the |
| 3213 | * connection, to make sure cs_shutw() will not lead to a shutdown() |
| 3214 | * that would provoke TIME_WAITs. |
| 3215 | */ |
| 3216 | cs_shutr(cs, CS_SHR_DRAIN); |
| 3217 | cs_shutw(cs, CS_SHW_NORMAL); |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 3218 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3219 | /* OK, let's not stay here forever */ |
| 3220 | if (check->result == CHK_RES_FAILED) |
| 3221 | cs->conn->flags |= CO_FL_ERROR; |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 3222 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3223 | task_wakeup(t, TASK_WOKEN_IO); |
| 3224 | } |
| 3225 | |
| 3226 | if (check->server) |
| 3227 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
| 3228 | else |
| 3229 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Christopher Faulet | 23d86d1 | 2018-01-25 11:36:35 +0100 | [diff] [blame] | 3230 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3231 | return NULL; |
Simon Horman | bfb5d33 | 2015-01-30 11:22:55 +0900 | [diff] [blame] | 3232 | } |
| 3233 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3234 | /* manages a server health-check that uses a connection. Returns |
| 3235 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 3236 | * |
| 3237 | * Please do NOT place any return statement in this function and only leave |
| 3238 | * via the out_unlock label. |
| 3239 | */ |
| 3240 | static struct task *process_chk_conn(struct task *t, void *context, unsigned short state) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3241 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3242 | struct check *check = context; |
| 3243 | struct proxy *proxy = check->proxy; |
| 3244 | struct conn_stream *cs = check->cs; |
| 3245 | struct connection *conn = cs_conn(cs); |
| 3246 | int rv; |
| 3247 | int expired = tick_is_expired(t->expire, now_ms); |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 3248 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3249 | if (check->server) |
| 3250 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 3251 | if (!(check->state & CHK_ST_INPROGRESS)) { |
| 3252 | /* no check currently running */ |
| 3253 | if (!expired) /* woke up too early */ |
| 3254 | goto out_unlock; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3255 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3256 | /* we don't send any health-checks when the proxy is |
| 3257 | * stopped, the server should not be checked or the check |
| 3258 | * is disabled. |
| 3259 | */ |
| 3260 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
| 3261 | proxy->state == PR_STSTOPPED) |
| 3262 | goto reschedule; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3263 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3264 | /* we'll initiate a new check */ |
| 3265 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3266 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3267 | check->state |= CHK_ST_INPROGRESS; |
| 3268 | b_reset(&check->bi); |
| 3269 | b_reset(&check->bo); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3270 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3271 | task_set_affinity(t, tid_bit); |
| 3272 | cs = check->cs; |
| 3273 | conn = cs_conn(cs); |
| 3274 | if (!conn) { |
| 3275 | check->current_step = NULL; |
| 3276 | tcpcheck_main(check); |
| 3277 | goto out_unlock; |
| 3278 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3279 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3280 | conn->flags |= CO_FL_ERROR; |
| 3281 | chk_report_conn_err(check, 0, 0); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3282 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3283 | /* here, we have seen a synchronous error, no fd was allocated */ |
| 3284 | task_set_affinity(t, MAX_THREADS_MASK); |
| 3285 | if (cs) { |
| 3286 | if (check->wait_list.events) |
| 3287 | cs->conn->xprt->unsubscribe(cs->conn, |
| 3288 | cs->conn->xprt_ctx, |
| 3289 | check->wait_list.events, |
| 3290 | &check->wait_list); |
| 3291 | /* We may have been scheduled to run, and the |
| 3292 | * I/O handler expects to have a cs, so remove |
| 3293 | * the tasklet |
| 3294 | */ |
| 3295 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
| 3296 | cs_destroy(cs); |
| 3297 | cs = check->cs = NULL; |
| 3298 | conn = NULL; |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3299 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3300 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3301 | check->state &= ~CHK_ST_INPROGRESS; |
| 3302 | check_notify_failure(check); |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3303 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3304 | /* we allow up to min(inter, timeout.connect) for a connection |
| 3305 | * to establish but only when timeout.check is set |
| 3306 | * as it may be to short for a full check otherwise |
| 3307 | */ |
| 3308 | while (tick_is_expired(t->expire, now_ms)) { |
| 3309 | int t_con; |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3310 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3311 | t_con = tick_add(t->expire, proxy->timeout.connect); |
| 3312 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 3313 | if (proxy->timeout.check) |
| 3314 | t->expire = tick_first(t->expire, t_con); |
| 3315 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3316 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3317 | else { |
| 3318 | /* there was a test running. |
| 3319 | * First, let's check whether there was an uncaught error, |
| 3320 | * which can happen on connect timeout or error. |
| 3321 | */ |
| 3322 | if (check->result == CHK_RES_UNKNOWN) { |
| 3323 | if ((conn->flags & CO_FL_ERROR) || cs->flags & CS_FL_ERROR || expired) { |
| 3324 | chk_report_conn_err(check, 0, expired); |
| 3325 | } |
| 3326 | else |
| 3327 | goto out_unlock; /* timeout not reached, wait again */ |
| 3328 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3329 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3330 | /* check complete or aborted */ |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3331 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3332 | check->current_step = NULL; |
| 3333 | if (check->sess != NULL) { |
| 3334 | session_free(check->sess); |
| 3335 | check->sess = NULL; |
| 3336 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3337 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3338 | if (conn && conn->xprt) { |
| 3339 | /* The check was aborted and the connection was not yet closed. |
| 3340 | * This can happen upon timeout, or when an external event such |
| 3341 | * as a failed response coupled with "observe layer7" caused the |
| 3342 | * server state to be suddenly changed. |
| 3343 | */ |
| 3344 | conn_sock_drain(conn); |
| 3345 | cs_close(cs); |
| 3346 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3347 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3348 | if (cs) { |
| 3349 | if (check->wait_list.events) |
| 3350 | cs->conn->xprt->unsubscribe(cs->conn, |
| 3351 | cs->conn->xprt_ctx, |
| 3352 | check->wait_list.events, |
| 3353 | &check->wait_list); |
| 3354 | /* We may have been scheduled to run, and the |
| 3355 | * I/O handler expects to have a cs, so remove |
| 3356 | * the tasklet |
| 3357 | */ |
| 3358 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
| 3359 | cs_destroy(cs); |
| 3360 | cs = check->cs = NULL; |
| 3361 | conn = NULL; |
| 3362 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3363 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3364 | if (check->server) { |
| 3365 | if (check->result == CHK_RES_FAILED) { |
| 3366 | /* a failure or timeout detected */ |
| 3367 | check_notify_failure(check); |
| 3368 | } |
| 3369 | else if (check->result == CHK_RES_CONDPASS) { |
| 3370 | /* check is OK but asks for stopping mode */ |
| 3371 | check_notify_stopping(check); |
| 3372 | } |
| 3373 | else if (check->result == CHK_RES_PASSED) { |
| 3374 | /* a success was detected */ |
| 3375 | check_notify_success(check); |
| 3376 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3377 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3378 | task_set_affinity(t, MAX_THREADS_MASK); |
| 3379 | check->state &= ~CHK_ST_INPROGRESS; |
| 3380 | |
| 3381 | if (check->server) { |
| 3382 | rv = 0; |
| 3383 | if (global.spread_checks > 0) { |
| 3384 | rv = srv_getinter(check) * global.spread_checks / 100; |
| 3385 | rv -= (int) (2 * rv * (ha_random32() / 4294967295.0)); |
| 3386 | } |
| 3387 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3388 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3389 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3390 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3391 | reschedule: |
| 3392 | while (tick_is_expired(t->expire, now_ms)) |
| 3393 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 3394 | out_unlock: |
| 3395 | if (check->server) |
| 3396 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
| 3397 | return t; |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3398 | } |
| 3399 | |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3400 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3401 | /**************************************************************************/ |
| 3402 | /******************* Internals to parse tcp-check rules *******************/ |
| 3403 | /**************************************************************************/ |
| 3404 | struct action_kw_list tcp_check_keywords = { |
| 3405 | .list = LIST_HEAD_INIT(tcp_check_keywords.list), |
| 3406 | }; |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3407 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3408 | /* Return the struct action_kw associated to a keyword */ |
| 3409 | static struct action_kw *action_kw_tcp_check_lookup(const char *kw) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3410 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3411 | return action_lookup(&tcp_check_keywords.list, kw); |
| 3412 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3413 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3414 | static void action_kw_tcp_check_build_list(struct buffer *chk) |
| 3415 | { |
| 3416 | action_build_list(&tcp_check_keywords.list, chk); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3417 | } |
| 3418 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3419 | /* Creates a tcp-check rule resulting from parsing a custom keyword. NULL is |
| 3420 | * returned on error. |
| 3421 | */ |
| 3422 | static struct tcpcheck_rule *parse_tcpcheck_action(char **args, int cur_arg, struct proxy *px, |
| 3423 | struct list *rules, struct action_kw *kw, |
| 3424 | const char *file, int line, char **errmsg) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3425 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3426 | struct tcpcheck_rule *chk = NULL; |
| 3427 | struct act_rule *actrule = NULL; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3428 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3429 | actrule = calloc(1, sizeof(*actrule)); |
| 3430 | if (!actrule) { |
| 3431 | memprintf(errmsg, "out of memory"); |
| 3432 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3433 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3434 | actrule->kw = kw; |
| 3435 | actrule->from = ACT_F_TCP_CHK; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3436 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3437 | cur_arg++; |
| 3438 | if (kw->parse((const char **)args, &cur_arg, px, actrule, errmsg) == ACT_RET_PRS_ERR) { |
| 3439 | memprintf(errmsg, "'%s' : %s", kw->kw, *errmsg); |
| 3440 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3441 | } |
| 3442 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3443 | chk = calloc(1, sizeof(*chk)); |
| 3444 | if (!chk) { |
| 3445 | memprintf(errmsg, "out of memory"); |
| 3446 | goto error; |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3447 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3448 | chk->action = TCPCHK_ACT_ACTION_KW; |
| 3449 | chk->action_kw.rule = actrule; |
| 3450 | return chk; |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3451 | |
| 3452 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3453 | free(actrule); |
| 3454 | return NULL; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3455 | } |
| 3456 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3457 | /* Parses and creates a tcp-check connect or an http-check connect rule. NULL is |
| 3458 | * returned on error. |
| 3459 | */ |
| 3460 | static struct tcpcheck_rule *parse_tcpcheck_connect(char **args, int cur_arg, struct proxy *px, struct list *rules, |
| 3461 | const char *file, int line, char **errmsg) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3462 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3463 | struct tcpcheck_rule *chk = NULL; |
| 3464 | struct sockaddr_storage *sk = NULL; |
| 3465 | char *comment = NULL, *sni = NULL, *alpn = NULL; |
| 3466 | struct sample_expr *port_expr = NULL; |
| 3467 | unsigned short conn_opts = 0; |
| 3468 | long port = 0; |
| 3469 | int alpn_len = 0; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3470 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3471 | list_for_each_entry(chk, rules, list) { |
| 3472 | if (chk->action == TCPCHK_ACT_CONNECT) |
| 3473 | break; |
| 3474 | if (chk->action == TCPCHK_ACT_COMMENT || |
| 3475 | chk->action == TCPCHK_ACT_ACTION_KW || |
| 3476 | (chk->action == TCPCHK_ACT_SEND && (chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT))) |
| 3477 | continue; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3478 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3479 | memprintf(errmsg, "first step MUST also be a 'connect', " |
| 3480 | "optionnaly preceded by a 'set-var', an 'unset-var' or a 'comment', " |
| 3481 | "when there is a 'connect' step in the tcp-check ruleset"); |
| 3482 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3483 | } |
| 3484 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3485 | cur_arg++; |
| 3486 | while (*(args[cur_arg])) { |
| 3487 | if (strcmp(args[cur_arg], "default") == 0) |
| 3488 | conn_opts |= TCPCHK_OPT_DEFAULT_CONNECT; |
| 3489 | else if (strcmp(args[cur_arg], "addr") == 0) { |
| 3490 | int port1, port2; |
| 3491 | struct protocol *proto; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3492 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3493 | if (!*(args[cur_arg+1])) { |
| 3494 | memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[cur_arg]); |
| 3495 | goto error; |
| 3496 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3497 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3498 | sk = str2sa_range(args[cur_arg+1], NULL, &port1, &port2, errmsg, NULL, NULL, 1); |
| 3499 | if (!sk) { |
| 3500 | memprintf(errmsg, "'%s' : %s.", args[cur_arg], *errmsg); |
| 3501 | goto error; |
| 3502 | } |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3503 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3504 | proto = protocol_by_family(sk->ss_family); |
| 3505 | if (!proto || !proto->connect) { |
| 3506 | memprintf(errmsg, "'%s' : connect() not supported for this address family.\n", |
| 3507 | args[cur_arg]); |
| 3508 | goto error; |
| 3509 | } |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3510 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3511 | if (port1 != port2) { |
| 3512 | memprintf(errmsg, "'%s' : port ranges and offsets are not allowed in '%s'\n", |
| 3513 | args[cur_arg], args[cur_arg+1]); |
| 3514 | goto error; |
| 3515 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3516 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3517 | cur_arg++; |
| 3518 | } |
| 3519 | else if (strcmp(args[cur_arg], "port") == 0) { |
| 3520 | const char *p, *end; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3521 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3522 | if (!*(args[cur_arg+1])) { |
| 3523 | memprintf(errmsg, "'%s' expects a port number or a sample expression as argument.", args[cur_arg]); |
| 3524 | goto error; |
| 3525 | } |
| 3526 | cur_arg++; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3527 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3528 | port = 0; |
| 3529 | release_sample_expr(port_expr); |
| 3530 | p = args[cur_arg]; end = p + strlen(p); |
| 3531 | port = read_uint(&p, end); |
| 3532 | if (p != end) { |
| 3533 | int idx = 0; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3534 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3535 | px->conf.args.ctx = ARGC_SRV; |
| 3536 | port_expr = sample_parse_expr((char *[]){args[cur_arg], NULL}, &idx, |
| 3537 | file, line, errmsg, &px->conf.args, NULL); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3538 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3539 | if (!port_expr) { |
| 3540 | memprintf(errmsg, "error detected while parsing port expression : %s", *errmsg); |
| 3541 | goto error; |
| 3542 | } |
| 3543 | if (!(port_expr->fetch->val & SMP_VAL_BE_CHK_RUL)) { |
| 3544 | memprintf(errmsg, "error detected while parsing port expression : " |
| 3545 | " fetch method '%s' extracts information from '%s', " |
| 3546 | "none of which is available here.\n", |
| 3547 | args[cur_arg], sample_src_names(port_expr->fetch->use)); |
| 3548 | goto error; |
| 3549 | } |
| 3550 | px->http_needed |= !!(port_expr->fetch->use & SMP_USE_HTTP_ANY); |
| 3551 | } |
| 3552 | else if (port > 65535 || port < 1) { |
| 3553 | memprintf(errmsg, "expects a valid TCP port (from range 1 to 65535) or a sample expression, got %s.", |
| 3554 | args[cur_arg]); |
| 3555 | goto error; |
| 3556 | } |
| 3557 | } |
| 3558 | else if (strcmp(args[cur_arg], "comment") == 0) { |
| 3559 | if (!*(args[cur_arg+1])) { |
| 3560 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3561 | goto error; |
| 3562 | } |
| 3563 | cur_arg++; |
| 3564 | free(comment); |
| 3565 | comment = strdup(args[cur_arg]); |
| 3566 | if (!comment) { |
| 3567 | memprintf(errmsg, "out of memory"); |
| 3568 | goto error; |
| 3569 | } |
| 3570 | } |
| 3571 | else if (strcmp(args[cur_arg], "send-proxy") == 0) |
| 3572 | conn_opts |= TCPCHK_OPT_SEND_PROXY; |
| 3573 | else if (strcmp(args[cur_arg], "via-socks4") == 0) |
| 3574 | conn_opts |= TCPCHK_OPT_SOCKS4; |
| 3575 | else if (strcmp(args[cur_arg], "linger") == 0) |
| 3576 | conn_opts |= TCPCHK_OPT_LINGER; |
| 3577 | #ifdef USE_OPENSSL |
| 3578 | else if (strcmp(args[cur_arg], "ssl") == 0) { |
| 3579 | px->options |= PR_O_TCPCHK_SSL; |
| 3580 | conn_opts |= TCPCHK_OPT_SSL; |
| 3581 | } |
| 3582 | else if (strcmp(args[cur_arg], "sni") == 0) { |
| 3583 | if (!*(args[cur_arg+1])) { |
| 3584 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3585 | goto error; |
| 3586 | } |
| 3587 | cur_arg++; |
| 3588 | free(sni); |
| 3589 | sni = strdup(args[cur_arg]); |
| 3590 | if (!sni) { |
| 3591 | memprintf(errmsg, "out of memory"); |
| 3592 | goto error; |
| 3593 | } |
| 3594 | } |
| 3595 | else if (strcmp(args[cur_arg], "alpn") == 0) { |
| 3596 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 3597 | free(alpn); |
| 3598 | if (ssl_sock_parse_alpn(args[cur_arg + 1], &alpn, &alpn_len, errmsg)) { |
| 3599 | memprintf(errmsg, "'%s' : %s", args[cur_arg], *errmsg); |
| 3600 | goto error; |
| 3601 | } |
| 3602 | cur_arg++; |
| 3603 | #else |
| 3604 | memprintf(errmsg, "'%s' : library does not support TLS ALPN extension.", args[cur_arg]); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3605 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3606 | #endif |
| 3607 | } |
| 3608 | #endif /* USE_OPENSSL */ |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3609 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3610 | else { |
| 3611 | memprintf(errmsg, "expects 'comment', 'port', 'addr', 'send-proxy'" |
| 3612 | #ifdef USE_OPENSSL |
| 3613 | ", 'ssl', 'sni', 'alpn'" |
| 3614 | #endif /* USE_OPENSSL */ |
| 3615 | " or 'via-socks4', 'linger', 'default' but got '%s' as argument.", |
| 3616 | args[cur_arg]); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3617 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3618 | } |
| 3619 | cur_arg++; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3620 | } |
| 3621 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3622 | chk = calloc(1, sizeof(*chk)); |
| 3623 | if (!chk) { |
| 3624 | memprintf(errmsg, "out of memory"); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3625 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3626 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3627 | chk->action = TCPCHK_ACT_CONNECT; |
| 3628 | chk->comment = comment; |
| 3629 | chk->connect.port = port; |
| 3630 | chk->connect.options = conn_opts; |
| 3631 | chk->connect.sni = sni; |
| 3632 | chk->connect.alpn = alpn; |
| 3633 | chk->connect.alpn_len= alpn_len; |
| 3634 | chk->connect.port_expr= port_expr; |
| 3635 | if (sk) |
| 3636 | chk->connect.addr = *sk; |
| 3637 | return chk; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3638 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3639 | error: |
| 3640 | free(alpn); |
| 3641 | free(sni); |
| 3642 | free(comment); |
| 3643 | release_sample_expr(port_expr); |
| 3644 | return NULL; |
| 3645 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3646 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3647 | /* Parses and creates a tcp-check send rule. NULL is returned on error */ |
| 3648 | static struct tcpcheck_rule *parse_tcpcheck_send(char **args, int cur_arg, struct proxy *px, struct list *rules, |
| 3649 | const char *file, int line, char **errmsg) |
| 3650 | { |
| 3651 | struct tcpcheck_rule *chk = NULL; |
| 3652 | char *comment = NULL, *data = NULL; |
| 3653 | enum tcpcheck_send_type type = TCPCHK_SEND_UNDEF; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3654 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3655 | type = ((strcmp(args[cur_arg], "send-binary") == 0) ? TCPCHK_SEND_BINARY : TCPCHK_SEND_STRING); |
| 3656 | if (!*(args[cur_arg+1])) { |
| 3657 | memprintf(errmsg, "'%s' expects a %s as argument", |
| 3658 | (type == TCPCHK_SEND_BINARY ? "binary string": "string"), args[cur_arg]); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3659 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3660 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3661 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3662 | data = args[cur_arg+1]; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3663 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3664 | cur_arg += 2; |
| 3665 | while (*(args[cur_arg])) { |
| 3666 | if (strcmp(args[cur_arg], "comment") == 0) { |
| 3667 | if (!*(args[cur_arg+1])) { |
| 3668 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3669 | goto error; |
| 3670 | } |
| 3671 | cur_arg++; |
| 3672 | free(comment); |
| 3673 | comment = strdup(args[cur_arg]); |
| 3674 | if (!comment) { |
| 3675 | memprintf(errmsg, "out of memory"); |
| 3676 | goto error; |
| 3677 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3678 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3679 | else if (strcmp(args[cur_arg], "log-format") == 0) { |
| 3680 | if (type == TCPCHK_SEND_BINARY) |
| 3681 | type = TCPCHK_SEND_BINARY_LF; |
| 3682 | else if (type == TCPCHK_SEND_STRING) |
| 3683 | type = TCPCHK_SEND_STRING_LF; |
| 3684 | } |
| 3685 | else { |
| 3686 | memprintf(errmsg, "expects 'comment', 'log-format' but got '%s' as argument.", |
| 3687 | args[cur_arg]); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3688 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3689 | } |
| 3690 | cur_arg++; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3691 | } |
| 3692 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3693 | chk = calloc(1, sizeof(*chk)); |
| 3694 | if (!chk) { |
| 3695 | memprintf(errmsg, "out of memory"); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3696 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3697 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3698 | chk->action = TCPCHK_ACT_SEND; |
| 3699 | chk->comment = comment; |
| 3700 | chk->send.type = type; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3701 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3702 | switch (chk->send.type) { |
| 3703 | case TCPCHK_SEND_STRING: |
| 3704 | chk->send.data = ist2(strdup(data), strlen(data)); |
| 3705 | if (!isttest(chk->send.data)) { |
| 3706 | memprintf(errmsg, "out of memory"); |
| 3707 | goto error; |
| 3708 | } |
| 3709 | break; |
| 3710 | case TCPCHK_SEND_BINARY: |
| 3711 | if (parse_binary(data, &chk->send.data.ptr, (int *)&chk->send.data.len, errmsg) == 0) { |
| 3712 | memprintf(errmsg, "'%s' invalid binary string (%s).\n", data, *errmsg); |
| 3713 | goto error; |
| 3714 | } |
| 3715 | break; |
| 3716 | case TCPCHK_SEND_STRING_LF: |
| 3717 | case TCPCHK_SEND_BINARY_LF: |
| 3718 | LIST_INIT(&chk->send.fmt); |
| 3719 | px->conf.args.ctx = ARGC_SRV; |
| 3720 | if (!parse_logformat_string(data, px, &chk->send.fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 3721 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", data, *errmsg); |
| 3722 | goto error; |
| 3723 | } |
| 3724 | break; |
| 3725 | case TCPCHK_SEND_HTTP: |
| 3726 | case TCPCHK_SEND_UNDEF: |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3727 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3728 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3729 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3730 | return chk; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3731 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3732 | error: |
| 3733 | free(chk); |
| 3734 | free(comment); |
| 3735 | return NULL; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3736 | } |
| 3737 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3738 | /* Parses and creates a http-check send rule. NULL is returned on error */ |
| 3739 | static struct tcpcheck_rule *parse_tcpcheck_send_http(char **args, int cur_arg, struct proxy *px, struct list *rules, |
| 3740 | const char *file, int line, char **errmsg) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3741 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3742 | struct tcpcheck_rule *chk = NULL; |
| 3743 | struct tcpcheck_http_hdr *hdr = NULL; |
| 3744 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 3745 | char *meth = NULL, *uri = NULL, *vsn = NULL; |
| 3746 | char *body = NULL, *comment = NULL; |
| 3747 | unsigned int flags = 0; |
| 3748 | int i = 0; |
| 3749 | |
| 3750 | cur_arg++; |
| 3751 | while (*(args[cur_arg])) { |
| 3752 | if (strcmp(args[cur_arg], "meth") == 0) { |
| 3753 | if (!*(args[cur_arg+1])) { |
| 3754 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3755 | goto error; |
| 3756 | } |
| 3757 | cur_arg++; |
| 3758 | meth = args[cur_arg]; |
| 3759 | } |
| 3760 | else if (strcmp(args[cur_arg], "uri") == 0) { |
| 3761 | if (!*(args[cur_arg+1])) { |
| 3762 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3763 | goto error; |
| 3764 | } |
| 3765 | cur_arg++; |
| 3766 | uri = args[cur_arg]; |
| 3767 | // TODO: log-format uri |
| 3768 | } |
| 3769 | else if (strcmp(args[cur_arg], "vsn") == 0) { |
| 3770 | if (!*(args[cur_arg+1])) { |
| 3771 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3772 | goto error; |
| 3773 | } |
| 3774 | cur_arg++; |
| 3775 | vsn = args[cur_arg]; |
| 3776 | } |
| 3777 | else if (strcmp(args[cur_arg], "hdr") == 0) { |
| 3778 | if (!*args[cur_arg+1] || !*args[cur_arg+2]) { |
| 3779 | memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg]); |
| 3780 | goto error; |
| 3781 | } |
| 3782 | hdrs[i].n = ist2(args[cur_arg+1], strlen(args[cur_arg+1])); |
| 3783 | hdrs[i].v = ist2(args[cur_arg+2], strlen(args[cur_arg+2])); |
| 3784 | i++; |
| 3785 | cur_arg += 2; |
| 3786 | } |
| 3787 | else if (strcmp(args[cur_arg], "body") == 0) { |
| 3788 | if (!*(args[cur_arg+1])) { |
| 3789 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3790 | goto error; |
| 3791 | } |
| 3792 | cur_arg++; |
| 3793 | body = args[cur_arg]; |
| 3794 | // TODO: log-format body |
| 3795 | } |
| 3796 | else if (strcmp(args[cur_arg], "comment") == 0) { |
| 3797 | if (!*(args[cur_arg+1])) { |
| 3798 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3799 | goto error; |
| 3800 | } |
| 3801 | cur_arg++; |
| 3802 | free(comment); |
| 3803 | comment = strdup(args[cur_arg]); |
| 3804 | if (!comment) { |
| 3805 | memprintf(errmsg, "out of memory"); |
| 3806 | goto error; |
| 3807 | } |
| 3808 | } |
| 3809 | else { |
| 3810 | memprintf(errmsg, "expects 'comment', 'meth', 'uri', 'hdr' and 'body' but got '%s' as argument.", |
| 3811 | args[cur_arg]); |
| 3812 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3813 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3814 | cur_arg++; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3815 | } |
| 3816 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3817 | hdrs[i].n = hdrs[i].v = IST_NULL; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3818 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3819 | chk = calloc(1, sizeof(*chk)); |
| 3820 | if (!chk) { |
| 3821 | memprintf(errmsg, "out of memory"); |
| 3822 | goto error; |
| 3823 | } |
| 3824 | chk->action = TCPCHK_ACT_SEND; |
| 3825 | chk->comment = comment; comment = NULL; |
| 3826 | chk->send.type = TCPCHK_SEND_HTTP; |
| 3827 | chk->send.http.flags = flags; |
| 3828 | LIST_INIT(&chk->send.http.hdrs); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3829 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3830 | if (meth) { |
| 3831 | chk->send.http.meth.meth = find_http_meth(meth, strlen(meth)); |
| 3832 | chk->send.http.meth.str.area = strdup(meth); |
| 3833 | chk->send.http.meth.str.data = strlen(meth); |
| 3834 | if (!chk->send.http.meth.str.area) { |
| 3835 | memprintf(errmsg, "out of memory"); |
| 3836 | goto error; |
| 3837 | } |
| 3838 | } |
| 3839 | if (uri) { |
| 3840 | chk->send.http.uri = ist2(strdup(uri), strlen(uri)); |
| 3841 | if (!isttest(chk->send.http.uri)) { |
| 3842 | memprintf(errmsg, "out of memory"); |
| 3843 | goto error; |
| 3844 | } |
| 3845 | } |
| 3846 | if (vsn) { |
| 3847 | chk->send.http.vsn = ist2(strdup(vsn), strlen(vsn)); |
| 3848 | if (!isttest(chk->send.http.vsn)) { |
| 3849 | memprintf(errmsg, "out of memory"); |
| 3850 | goto error; |
| 3851 | } |
| 3852 | } |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 3853 | for (i = 0; istlen(hdrs[i].n); i++) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3854 | hdr = calloc(1, sizeof(*hdr)); |
| 3855 | if (!hdr) { |
| 3856 | memprintf(errmsg, "out of memory"); |
| 3857 | goto error; |
| 3858 | } |
| 3859 | LIST_INIT(&hdr->value); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 3860 | hdr->name = istdup(hdrs[i].n); |
| 3861 | if (!isttest(hdr->name)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3862 | memprintf(errmsg, "out of memory"); |
| 3863 | goto error; |
| 3864 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3865 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 3866 | ist0(hdrs[i].v); |
| 3867 | if (!parse_logformat_string(istptr(hdrs[i].v), px, &hdr->value, 0, SMP_VAL_BE_CHK_RUL, errmsg)) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3868 | goto error; |
| 3869 | LIST_ADDQ(&chk->send.http.hdrs, &hdr->list); |
| 3870 | hdr = NULL; |
| 3871 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3872 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3873 | if (body) { |
| 3874 | chk->send.http.body = ist2(strdup(body), strlen(body)); |
| 3875 | if (!isttest(chk->send.http.body)) { |
| 3876 | memprintf(errmsg, "out of memory"); |
| 3877 | goto error; |
| 3878 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3879 | } |
| 3880 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3881 | return chk; |
| 3882 | |
| 3883 | error: |
| 3884 | free_tcpcheck_http_hdr(hdr); |
| 3885 | free_tcpcheck(chk, 0); |
| 3886 | free(comment); |
| 3887 | return NULL; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3888 | } |
| 3889 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3890 | /* Parses and creates a http-check comment rule. NULL is returned on error */ |
| 3891 | static struct tcpcheck_rule *parse_tcpcheck_comment(char **args, int cur_arg, struct proxy *px, struct list *rules, |
| 3892 | const char *file, int line, char **errmsg) |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 3893 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3894 | struct tcpcheck_rule *chk = NULL; |
| 3895 | char *comment = NULL; |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 3896 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3897 | if (!*(args[cur_arg+1])) { |
| 3898 | memprintf(errmsg, "expects a string as argument"); |
| 3899 | goto error; |
| 3900 | } |
| 3901 | cur_arg++; |
| 3902 | comment = strdup(args[cur_arg]); |
| 3903 | if (!comment) { |
| 3904 | memprintf(errmsg, "out of memory"); |
| 3905 | goto error; |
| 3906 | } |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3907 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3908 | chk = calloc(1, sizeof(*chk)); |
| 3909 | if (!chk) { |
| 3910 | memprintf(errmsg, "out of memory"); |
| 3911 | goto error; |
| 3912 | } |
| 3913 | chk->action = TCPCHK_ACT_COMMENT; |
| 3914 | chk->comment = comment; |
| 3915 | return chk; |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 3916 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3917 | error: |
| 3918 | free(comment); |
| 3919 | return NULL; |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 3920 | } |
| 3921 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3922 | /* Parses and creates a tcp-check or an http-check expect rule. NULL is returned |
| 3923 | * on error. <proto> is set to the right protocol flags (covered by the |
| 3924 | * TCPCHK_RULES_PROTO_CHK mask). |
| 3925 | */ |
| 3926 | static struct tcpcheck_rule *parse_tcpcheck_expect(char **args, int cur_arg, struct proxy *px, |
| 3927 | struct list *rules, unsigned int proto, |
| 3928 | const char *file, int line, char **errmsg) |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 3929 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3930 | struct tcpcheck_rule *prev_check, *chk = NULL; |
| 3931 | struct sample_expr *status_expr = NULL; |
| 3932 | char *str, *on_success_msg, *on_error_msg, *comment, *pattern; |
| 3933 | enum tcpcheck_expect_type type = TCPCHK_EXPECT_UNDEF; |
| 3934 | enum healthcheck_status ok_st = HCHK_STATUS_L7OKD; |
| 3935 | enum healthcheck_status err_st = HCHK_STATUS_L7RSP; |
| 3936 | enum healthcheck_status tout_st = HCHK_STATUS_L7TOUT; |
| 3937 | long min_recv = -1; |
| 3938 | int inverse = 0, with_capture = 0; |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 3939 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3940 | str = on_success_msg = on_error_msg = comment = pattern = NULL; |
| 3941 | if (!*(args[cur_arg+1])) { |
| 3942 | memprintf(errmsg, "expects at least a matching pattern as arguments"); |
| 3943 | goto error; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3944 | } |
| 3945 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3946 | cur_arg++; |
| 3947 | while (*(args[cur_arg])) { |
| 3948 | int in_pattern = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3949 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3950 | rescan: |
| 3951 | if (strcmp(args[cur_arg], "min-recv") == 0) { |
| 3952 | if (in_pattern) { |
| 3953 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 3954 | goto error; |
| 3955 | } |
| 3956 | if (!*(args[cur_arg+1])) { |
| 3957 | memprintf(errmsg, "'%s' expects a integer as argument", args[cur_arg]); |
| 3958 | goto error; |
| 3959 | } |
| 3960 | /* Use an signed integer here because of chksize */ |
| 3961 | cur_arg++; |
| 3962 | min_recv = atol(args[cur_arg]); |
| 3963 | if (min_recv < -1 || min_recv > INT_MAX) { |
| 3964 | memprintf(errmsg, "'%s' expects -1 or an integer from 0 to INT_MAX" , args[cur_arg-1]); |
| 3965 | goto error; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3966 | } |
| 3967 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3968 | else if (*(args[cur_arg]) == '!') { |
| 3969 | in_pattern = 1; |
| 3970 | while (*(args[cur_arg]) == '!') { |
| 3971 | inverse = !inverse; |
| 3972 | args[cur_arg]++; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3973 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3974 | if (!*(args[cur_arg])) |
| 3975 | cur_arg++; |
| 3976 | goto rescan; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3977 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3978 | else if (strcmp(args[cur_arg], "string") == 0 || strcmp(args[cur_arg], "rstring") == 0) { |
| 3979 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 3980 | memprintf(errmsg, "only on pattern expected"); |
| 3981 | goto error; |
| 3982 | } |
| 3983 | if (proto != TCPCHK_RULES_HTTP_CHK) |
| 3984 | type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_STRING : TCPCHK_EXPECT_REGEX); |
| 3985 | else |
| 3986 | type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_HTTP_BODY : TCPCHK_EXPECT_HTTP_REGEX_BODY); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3987 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3988 | if (!*(args[cur_arg+1])) { |
| 3989 | memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]); |
| 3990 | goto error; |
| 3991 | } |
| 3992 | cur_arg++; |
| 3993 | pattern = args[cur_arg]; |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 3994 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3995 | else if (strcmp(args[cur_arg], "binary") == 0 || strcmp(args[cur_arg], "rbinary") == 0) { |
| 3996 | if (proto == TCPCHK_RULES_HTTP_CHK) |
| 3997 | goto bad_http_kw; |
| 3998 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 3999 | memprintf(errmsg, "only on pattern expected"); |
| 4000 | goto error; |
| 4001 | } |
| 4002 | type = ((*(args[cur_arg]) == 'b') ? TCPCHK_EXPECT_BINARY : TCPCHK_EXPECT_REGEX_BINARY); |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 4003 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4004 | if (!*(args[cur_arg+1])) { |
| 4005 | memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]); |
| 4006 | goto error; |
| 4007 | } |
| 4008 | cur_arg++; |
| 4009 | pattern = args[cur_arg]; |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 4010 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4011 | else if (strcmp(args[cur_arg], "status") == 0 || strcmp(args[cur_arg], "rstatus") == 0) { |
| 4012 | if (proto != TCPCHK_RULES_HTTP_CHK) |
| 4013 | goto bad_tcp_kw; |
| 4014 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 4015 | memprintf(errmsg, "only on pattern expected"); |
| 4016 | goto error; |
| 4017 | } |
| 4018 | type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_HTTP_STATUS : TCPCHK_EXPECT_HTTP_REGEX_STATUS); |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 4019 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4020 | if (!*(args[cur_arg+1])) { |
| 4021 | memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]); |
| 4022 | goto error; |
| 4023 | } |
| 4024 | cur_arg++; |
| 4025 | pattern = args[cur_arg]; |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 4026 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4027 | else if (strcmp(args[cur_arg], "custom") == 0) { |
| 4028 | if (in_pattern) { |
| 4029 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4030 | goto error; |
| 4031 | } |
| 4032 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 4033 | memprintf(errmsg, "only on pattern expected"); |
| 4034 | goto error; |
| 4035 | } |
| 4036 | type = TCPCHK_EXPECT_CUSTOM; |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4037 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4038 | else if (strcmp(args[cur_arg], "comment") == 0) { |
| 4039 | if (in_pattern) { |
| 4040 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4041 | goto error; |
| 4042 | } |
| 4043 | if (!*(args[cur_arg+1])) { |
| 4044 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 4045 | goto error; |
| 4046 | } |
| 4047 | cur_arg++; |
| 4048 | free(comment); |
| 4049 | comment = strdup(args[cur_arg]); |
| 4050 | if (!comment) { |
| 4051 | memprintf(errmsg, "out of memory"); |
| 4052 | goto error; |
| 4053 | } |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4054 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4055 | else if (strcmp(args[cur_arg], "on-success") == 0) { |
| 4056 | if (in_pattern) { |
| 4057 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4058 | goto error; |
| 4059 | } |
| 4060 | if (!*(args[cur_arg+1])) { |
| 4061 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 4062 | goto error; |
| 4063 | } |
| 4064 | cur_arg++; |
| 4065 | free(on_success_msg); |
| 4066 | on_success_msg = strdup(args[cur_arg]); |
| 4067 | if (!on_success_msg) { |
| 4068 | memprintf(errmsg, "out of memory"); |
| 4069 | goto error; |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 4070 | } |
| 4071 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4072 | else if (strcmp(args[cur_arg], "on-error") == 0) { |
| 4073 | if (in_pattern) { |
| 4074 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4075 | goto error; |
| 4076 | } |
| 4077 | if (!*(args[cur_arg+1])) { |
| 4078 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 4079 | goto error; |
| 4080 | } |
| 4081 | cur_arg++; |
| 4082 | free(on_error_msg); |
| 4083 | on_error_msg = strdup(args[cur_arg]); |
| 4084 | if (!on_error_msg) { |
| 4085 | memprintf(errmsg, "out of memory"); |
| 4086 | goto error; |
| 4087 | } |
| 4088 | } |
| 4089 | else if (strcmp(args[cur_arg], "ok-status") == 0) { |
| 4090 | if (in_pattern) { |
| 4091 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4092 | goto error; |
| 4093 | } |
| 4094 | if (!*(args[cur_arg+1])) { |
| 4095 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 4096 | goto error; |
| 4097 | } |
| 4098 | if (strcasecmp(args[cur_arg+1], "L7OK") == 0) |
| 4099 | ok_st = HCHK_STATUS_L7OKD; |
| 4100 | else if (strcasecmp(args[cur_arg+1], "L7OKC") == 0) |
| 4101 | ok_st = HCHK_STATUS_L7OKCD; |
| 4102 | else if (strcasecmp(args[cur_arg+1], "L6OK") == 0) |
| 4103 | ok_st = HCHK_STATUS_L6OK; |
| 4104 | else if (strcasecmp(args[cur_arg+1], "L4OK") == 0) |
| 4105 | ok_st = HCHK_STATUS_L4OK; |
| 4106 | else { |
| 4107 | memprintf(errmsg, "'%s' only supports 'L4OK', 'L6OK', 'L7OK' or 'L7OKC' status (got '%s').", |
| 4108 | args[cur_arg], args[cur_arg+1]); |
| 4109 | goto error; |
| 4110 | } |
| 4111 | cur_arg++; |
| 4112 | } |
| 4113 | else if (strcmp(args[cur_arg], "error-status") == 0) { |
| 4114 | if (in_pattern) { |
| 4115 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4116 | goto error; |
| 4117 | } |
| 4118 | if (!*(args[cur_arg+1])) { |
| 4119 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 4120 | goto error; |
| 4121 | } |
| 4122 | if (strcasecmp(args[cur_arg+1], "L7RSP") == 0) |
| 4123 | err_st = HCHK_STATUS_L7RSP; |
| 4124 | else if (strcasecmp(args[cur_arg+1], "L7STS") == 0) |
| 4125 | err_st = HCHK_STATUS_L7STS; |
| 4126 | else if (strcasecmp(args[cur_arg+1], "L6RSP") == 0) |
| 4127 | err_st = HCHK_STATUS_L6RSP; |
| 4128 | else if (strcasecmp(args[cur_arg+1], "L4CON") == 0) |
| 4129 | err_st = HCHK_STATUS_L4CON; |
| 4130 | else { |
| 4131 | memprintf(errmsg, "'%s' only supports 'L4CON', 'L6RSP', 'L7RSP' or 'L7STS' status (got '%s').", |
| 4132 | args[cur_arg], args[cur_arg+1]); |
| 4133 | goto error; |
| 4134 | } |
| 4135 | cur_arg++; |
| 4136 | } |
| 4137 | else if (strcmp(args[cur_arg], "status-code") == 0) { |
| 4138 | int idx = 0; |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 4139 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4140 | if (in_pattern) { |
| 4141 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4142 | goto error; |
| 4143 | } |
| 4144 | if (!*(args[cur_arg+1])) { |
| 4145 | memprintf(errmsg, "'%s' expects an expression as argument", args[cur_arg]); |
| 4146 | goto error; |
| 4147 | } |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4148 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4149 | cur_arg++; |
| 4150 | release_sample_expr(status_expr); |
| 4151 | px->conf.args.ctx = ARGC_SRV; |
| 4152 | status_expr = sample_parse_expr((char *[]){args[cur_arg], NULL}, &idx, |
| 4153 | file, line, errmsg, &px->conf.args, NULL); |
| 4154 | if (!status_expr) { |
| 4155 | memprintf(errmsg, "error detected while parsing status-code expression : %s", *errmsg); |
| 4156 | goto error; |
| 4157 | } |
| 4158 | if (!(status_expr->fetch->val & SMP_VAL_BE_CHK_RUL)) { |
| 4159 | memprintf(errmsg, "error detected while parsing status-code expression : " |
| 4160 | " fetch method '%s' extracts information from '%s', " |
| 4161 | "none of which is available here.\n", |
| 4162 | args[cur_arg], sample_src_names(status_expr->fetch->use)); |
| 4163 | goto error; |
| 4164 | } |
| 4165 | px->http_needed |= !!(status_expr->fetch->use & SMP_USE_HTTP_ANY); |
| 4166 | } |
| 4167 | else if (strcmp(args[cur_arg], "tout-status") == 0) { |
| 4168 | if (in_pattern) { |
| 4169 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4170 | goto error; |
| 4171 | } |
| 4172 | if (!*(args[cur_arg+1])) { |
| 4173 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 4174 | goto error; |
| 4175 | } |
| 4176 | if (strcasecmp(args[cur_arg+1], "L7TOUT") == 0) |
| 4177 | tout_st = HCHK_STATUS_L7TOUT; |
| 4178 | else if (strcasecmp(args[cur_arg+1], "L6TOUT") == 0) |
| 4179 | tout_st = HCHK_STATUS_L6TOUT; |
| 4180 | else if (strcasecmp(args[cur_arg+1], "L4TOUT") == 0) |
| 4181 | tout_st = HCHK_STATUS_L4TOUT; |
| 4182 | else { |
| 4183 | memprintf(errmsg, "'%s' only supports 'L4TOUT', 'L6TOUT' or 'L7TOUT' status (got '%s').", |
| 4184 | args[cur_arg], args[cur_arg+1]); |
| 4185 | goto error; |
| 4186 | } |
| 4187 | cur_arg++; |
| 4188 | } |
| 4189 | else { |
| 4190 | if (proto == TCPCHK_RULES_HTTP_CHK) { |
| 4191 | bad_http_kw: |
| 4192 | memprintf(errmsg, "'only supports min-recv, [!]string', '[!]rstring', '[!]status', '[!]rstatus'" |
| 4193 | " or comment but got '%s' as argument.", args[cur_arg]); |
| 4194 | } |
| 4195 | else { |
| 4196 | bad_tcp_kw: |
| 4197 | memprintf(errmsg, "'only supports min-recv, '[!]binary', '[!]string', '[!]rstring', '[!]rbinary'" |
| 4198 | " or comment but got '%s' as argument.", args[cur_arg]); |
| 4199 | } |
| 4200 | goto error; |
| 4201 | } |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4202 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4203 | cur_arg++; |
| 4204 | } |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4205 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4206 | if (comment) { |
| 4207 | char *p = comment; |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4208 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4209 | while (*p) { |
| 4210 | if (*p == '\\') { |
| 4211 | p++; |
| 4212 | if (!*p || !isdigit((unsigned char)*p) || |
| 4213 | (*p == 'x' && (!*(p+1) || !*(p+2) || !ishex(*(p+1)) || !ishex(*(p+2))))) { |
| 4214 | memprintf(errmsg, "invalid backreference in 'comment' argument"); |
| 4215 | goto error; |
| 4216 | } |
| 4217 | with_capture = 1; |
| 4218 | } |
| 4219 | p++; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4220 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4221 | if (with_capture && !inverse) |
| 4222 | memprintf(errmsg, "using backreference in a positive expect comment is useless"); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4223 | } |
| 4224 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4225 | chk = calloc(1, sizeof(*chk)); |
| 4226 | if (!chk) { |
| 4227 | memprintf(errmsg, "out of memory"); |
| 4228 | goto error; |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4229 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4230 | chk->action = TCPCHK_ACT_EXPECT; |
| 4231 | LIST_INIT(&chk->expect.onerror_fmt); |
| 4232 | LIST_INIT(&chk->expect.onsuccess_fmt); |
| 4233 | chk->comment = comment; comment = NULL; |
| 4234 | chk->expect.type = type; |
| 4235 | chk->expect.min_recv = min_recv; |
| 4236 | chk->expect.flags |= (inverse ? TCPCHK_EXPT_FL_INV : 0); |
| 4237 | chk->expect.flags |= (with_capture ? TCPCHK_EXPT_FL_CAP : 0); |
| 4238 | chk->expect.ok_status = ok_st; |
| 4239 | chk->expect.err_status = err_st; |
| 4240 | chk->expect.tout_status = tout_st; |
| 4241 | chk->expect.status_expr = status_expr; status_expr = NULL; |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4242 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4243 | if (on_success_msg) { |
| 4244 | px->conf.args.ctx = ARGC_SRV; |
| 4245 | if (!parse_logformat_string(on_success_msg, px, &chk->expect.onsuccess_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 4246 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", on_success_msg, *errmsg); |
| 4247 | goto error; |
| 4248 | } |
| 4249 | free(on_success_msg); |
| 4250 | on_success_msg = NULL; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4251 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4252 | if (on_error_msg) { |
| 4253 | px->conf.args.ctx = ARGC_SRV; |
| 4254 | if (!parse_logformat_string(on_error_msg, px, &chk->expect.onerror_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 4255 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", on_error_msg, *errmsg); |
| 4256 | goto error; |
| 4257 | } |
| 4258 | free(on_error_msg); |
| 4259 | on_error_msg = NULL; |
| 4260 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4261 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4262 | switch (chk->expect.type) { |
| 4263 | case TCPCHK_EXPECT_STRING: |
| 4264 | case TCPCHK_EXPECT_HTTP_STATUS: |
| 4265 | case TCPCHK_EXPECT_HTTP_BODY: |
| 4266 | chk->expect.data = ist2(strdup(pattern), strlen(pattern)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4267 | if (!isttest(chk->expect.data)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4268 | memprintf(errmsg, "out of memory"); |
| 4269 | goto error; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4270 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4271 | break; |
| 4272 | case TCPCHK_EXPECT_BINARY: |
| 4273 | if (parse_binary(pattern, &chk->expect.data.ptr, (int *)&chk->expect.data.len, errmsg) == 0) { |
| 4274 | memprintf(errmsg, "invalid binary string (%s)", *errmsg); |
| 4275 | goto error; |
| 4276 | } |
| 4277 | case TCPCHK_EXPECT_REGEX: |
| 4278 | case TCPCHK_EXPECT_REGEX_BINARY: |
| 4279 | case TCPCHK_EXPECT_HTTP_REGEX_STATUS: |
| 4280 | case TCPCHK_EXPECT_HTTP_REGEX_BODY: |
| 4281 | chk->expect.regex = regex_comp(pattern, 1, with_capture, errmsg); |
| 4282 | if (!chk->expect.regex) |
| 4283 | goto error; |
| 4284 | break; |
| 4285 | case TCPCHK_EXPECT_CUSTOM: |
| 4286 | chk->expect.custom = NULL; /* Must be defined by the caller ! */ |
| 4287 | break; |
| 4288 | case TCPCHK_EXPECT_UNDEF: |
| 4289 | free(chk); |
| 4290 | memprintf(errmsg, "pattern not found"); |
| 4291 | goto error; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4292 | } |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4293 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4294 | /* All tcp-check expect points back to the first inverse expect rule in |
| 4295 | * a chain of one or more expect rule, potentially itself. |
| 4296 | */ |
| 4297 | chk->expect.head = chk; |
| 4298 | list_for_each_entry_rev(prev_check, rules, list) { |
| 4299 | if (prev_check->action == TCPCHK_ACT_EXPECT) { |
| 4300 | if (prev_check->expect.flags & TCPCHK_EXPT_FL_INV) |
| 4301 | chk->expect.head = prev_check; |
| 4302 | continue; |
| 4303 | } |
| 4304 | if (prev_check->action != TCPCHK_ACT_COMMENT && prev_check->action != TCPCHK_ACT_ACTION_KW) |
| 4305 | break; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4306 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4307 | return chk; |
| 4308 | |
| 4309 | error: |
| 4310 | free_tcpcheck(chk, 0); |
| 4311 | free(str); |
| 4312 | free(comment); |
| 4313 | free(on_success_msg); |
| 4314 | free(on_error_msg); |
| 4315 | release_sample_expr(status_expr); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4316 | return NULL; |
| 4317 | } |
| 4318 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4319 | /* Overwrites fields of the old http send rule with those of the new one. When |
| 4320 | * replaced, old values are freed and replaced by the new ones. New values are |
| 4321 | * not copied but transferred. At the end <new> should be empty and can be |
| 4322 | * safely released. This function never fails. |
| 4323 | */ |
| 4324 | static void tcpcheck_overwrite_send_http_rule(struct tcpcheck_rule *old, struct tcpcheck_rule *new) |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4325 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4326 | struct logformat_node *lf, *lfb; |
| 4327 | struct tcpcheck_http_hdr *hdr, *bhdr; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4328 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4329 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4330 | if (new->send.http.meth.str.area) { |
| 4331 | free(old->send.http.meth.str.area); |
| 4332 | old->send.http.meth.meth = new->send.http.meth.meth; |
| 4333 | old->send.http.meth.str.area = new->send.http.meth.str.area; |
| 4334 | old->send.http.meth.str.data = new->send.http.meth.str.data; |
| 4335 | new->send.http.meth.str = BUF_NULL; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4336 | } |
| 4337 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4338 | if (!(new->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT) && isttest(new->send.http.uri)) { |
| 4339 | if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4340 | istfree(&old->send.http.uri); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4341 | else |
| 4342 | free_tcpcheck_fmt(&old->send.http.uri_fmt); |
| 4343 | old->send.http.flags &= ~TCPCHK_SND_HTTP_FL_URI_FMT; |
| 4344 | old->send.http.uri = new->send.http.uri; |
| 4345 | new->send.http.uri = IST_NULL; |
| 4346 | } |
| 4347 | else if ((new->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT) && !LIST_ISEMPTY(&new->send.http.uri_fmt)) { |
| 4348 | if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4349 | istfree(&old->send.http.uri); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4350 | else |
| 4351 | free_tcpcheck_fmt(&old->send.http.uri_fmt); |
| 4352 | old->send.http.flags |= TCPCHK_SND_HTTP_FL_URI_FMT; |
| 4353 | LIST_INIT(&old->send.http.uri_fmt); |
| 4354 | list_for_each_entry_safe(lf, lfb, &new->send.http.uri_fmt, list) { |
| 4355 | LIST_DEL(&lf->list); |
| 4356 | LIST_ADDQ(&old->send.http.uri_fmt, &lf->list); |
| 4357 | } |
| 4358 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4359 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4360 | if (isttest(new->send.http.vsn)) { |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4361 | istfree(&old->send.http.vsn); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4362 | old->send.http.vsn = new->send.http.vsn; |
| 4363 | new->send.http.vsn = IST_NULL; |
| 4364 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4365 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4366 | free_tcpcheck_http_hdrs(&old->send.http.hdrs); |
| 4367 | list_for_each_entry_safe(hdr, bhdr, &new->send.http.hdrs, list) { |
| 4368 | LIST_DEL(&hdr->list); |
| 4369 | LIST_ADDQ(&old->send.http.hdrs, &hdr->list); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4370 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4371 | |
| 4372 | if (!(new->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT) && isttest(new->send.http.body)) { |
| 4373 | if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4374 | istfree(&old->send.http.body); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4375 | else |
| 4376 | free_tcpcheck_fmt(&old->send.http.body_fmt); |
| 4377 | old->send.http.flags &= ~TCPCHK_SND_HTTP_FL_BODY_FMT; |
| 4378 | old->send.http.body = new->send.http.body; |
| 4379 | new->send.http.body = IST_NULL; |
| 4380 | } |
| 4381 | else if ((new->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT) && !LIST_ISEMPTY(&new->send.http.body_fmt)) { |
| 4382 | if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4383 | istfree(&old->send.http.body); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4384 | else |
| 4385 | free_tcpcheck_fmt(&old->send.http.body_fmt); |
| 4386 | old->send.http.flags |= TCPCHK_SND_HTTP_FL_BODY_FMT; |
| 4387 | LIST_INIT(&old->send.http.body_fmt); |
| 4388 | list_for_each_entry_safe(lf, lfb, &new->send.http.body_fmt, list) { |
| 4389 | LIST_DEL(&lf->list); |
| 4390 | LIST_ADDQ(&old->send.http.body_fmt, &lf->list); |
| 4391 | } |
| 4392 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4393 | } |
| 4394 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4395 | /* Internal function used to add an http-check rule in a list during the config |
| 4396 | * parsing step. Depending on its type, and the previously inserted rules, a |
| 4397 | * specific action may be performed or an error may be reported. This functions |
| 4398 | * returns 1 on success and 0 on error and <errmsg> is filled with the error |
| 4399 | * message. |
| 4400 | */ |
| 4401 | static int tcpcheck_add_http_rule(struct tcpcheck_rule *chk, struct tcpcheck_rules *rules, char **errmsg) |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4402 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4403 | struct tcpcheck_rule *r; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4404 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4405 | /* the implicit send rule coming from an "option httpchk" line must be |
| 4406 | * merged with the first explici http-check send rule, if |
| 4407 | * any. Depdending the declaration order some tests are required. |
| 4408 | * |
| 4409 | * Some tests is also required for other kinds of http-check rules to be |
| 4410 | * sure the ruleset remains valid. |
| 4411 | */ |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4412 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4413 | if (chk->action == TCPCHK_ACT_SEND && (chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) { |
| 4414 | /* Tries to add an implcit http-check send rule from an "option httpchk" line. |
| 4415 | * First, the first rule is retrieved, skipping the first CONNECT, if any, and |
| 4416 | * following tests are performed : |
| 4417 | * |
| 4418 | * 1- If there is no such rule or if it is not a send rule, the implicit send |
| 4419 | * rule is pushed in front of the ruleset |
| 4420 | * |
| 4421 | * 2- If it is another implicit send rule, it is replaced with the new one. |
| 4422 | * |
| 4423 | * 3- Otherwise, it means it is an explicit send rule. In this case we merge |
| 4424 | * both, overwritting the old send rule (the explicit one) with info of the |
| 4425 | * new send rule (the implicit one). |
| 4426 | */ |
| 4427 | r = get_first_tcpcheck_rule(rules); |
| 4428 | if (r && r->action == TCPCHK_ACT_CONNECT) |
| 4429 | r = get_next_tcpcheck_rule(rules, r); |
| 4430 | if (!r || r->action != TCPCHK_ACT_SEND) |
| 4431 | LIST_ADD(rules->list, &chk->list); |
| 4432 | else if (r->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT) { |
| 4433 | LIST_DEL(&r->list); |
| 4434 | free_tcpcheck(r, 0); |
| 4435 | LIST_ADD(rules->list, &chk->list); |
| 4436 | } |
| 4437 | else { |
| 4438 | tcpcheck_overwrite_send_http_rule(r, chk); |
| 4439 | free_tcpcheck(chk, 0); |
| 4440 | } |
| 4441 | } |
| 4442 | else { |
| 4443 | /* Tries to add an explicit http-check rule. First of all we check the typefo the |
| 4444 | * last inserted rule to be sure it is valid. Then for send rule, we try to merge it |
| 4445 | * with an existing implicit send rule, if any. At the end, if there is no error, |
| 4446 | * the rule is appended to the list. |
| 4447 | */ |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4448 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4449 | r = get_last_tcpcheck_rule(rules); |
| 4450 | if (!r || (r->action == TCPCHK_ACT_SEND && (r->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT))) |
| 4451 | /* no error */; |
| 4452 | else if (r->action != TCPCHK_ACT_CONNECT && chk->action == TCPCHK_ACT_SEND) { |
| 4453 | memprintf(errmsg, "unable to add http-check send rule at step %d (missing connect rule).", |
| 4454 | chk->index+1); |
| 4455 | return 0; |
| 4456 | } |
| 4457 | else if (r->action != TCPCHK_ACT_SEND && chk->action == TCPCHK_ACT_EXPECT) { |
| 4458 | memprintf(errmsg, "unable to add http-check expect rule at step %d (missing send rule).", |
| 4459 | chk->index+1); |
| 4460 | return 0; |
| 4461 | } |
| 4462 | else if (r->action != TCPCHK_ACT_EXPECT && chk->action == TCPCHK_ACT_CONNECT) { |
| 4463 | memprintf(errmsg, "unable to add http-check connect rule at step %d (missing expect rule).", |
| 4464 | chk->index+1); |
| 4465 | return 0; |
| 4466 | } |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4467 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4468 | if (chk->action == TCPCHK_ACT_SEND) { |
| 4469 | r = get_first_tcpcheck_rule(rules); |
| 4470 | if (r && r->action == TCPCHK_ACT_SEND && (r->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) { |
| 4471 | tcpcheck_overwrite_send_http_rule(r, chk); |
| 4472 | free_tcpcheck(chk, 0); |
| 4473 | LIST_DEL(&r->list); |
| 4474 | r->send.http.flags &= ~TCPCHK_SND_HTTP_FROM_OPT; |
| 4475 | chk = r; |
| 4476 | } |
| 4477 | } |
| 4478 | LIST_ADDQ(rules->list, &chk->list); |
| 4479 | } |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4480 | return 1; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4481 | } |
| 4482 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4483 | /**************************************************************************/ |
| 4484 | /************************** Init/deinit checks ****************************/ |
| 4485 | /**************************************************************************/ |
| 4486 | static const char *init_check(struct check *check, int type) |
| 4487 | { |
| 4488 | check->type = type; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4489 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4490 | b_reset(&check->bi); check->bi.size = global.tune.chksize; |
| 4491 | b_reset(&check->bo); check->bo.size = global.tune.chksize; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4492 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4493 | check->bi.area = calloc(check->bi.size, sizeof(char)); |
| 4494 | check->bo.area = calloc(check->bo.size, sizeof(char)); |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4495 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4496 | if (!check->bi.area || !check->bo.area) |
| 4497 | return "out of memory while allocating check buffer"; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4498 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4499 | check->wait_list.tasklet = tasklet_new(); |
| 4500 | if (!check->wait_list.tasklet) |
| 4501 | return "out of memory while allocating check tasklet"; |
| 4502 | check->wait_list.events = 0; |
| 4503 | check->wait_list.tasklet->process = event_srv_chk_io; |
| 4504 | check->wait_list.tasklet->context = check; |
| 4505 | return NULL; |
| 4506 | } |
| 4507 | |
| 4508 | void free_check(struct check *check) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4509 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4510 | task_destroy(check->task); |
| 4511 | if (check->wait_list.tasklet) |
| 4512 | tasklet_free(check->wait_list.tasklet); |
| 4513 | |
| 4514 | free(check->bi.area); |
| 4515 | free(check->bo.area); |
| 4516 | if (check->cs) { |
| 4517 | free(check->cs->conn); |
| 4518 | check->cs->conn = NULL; |
| 4519 | cs_free(check->cs); |
| 4520 | check->cs = NULL; |
| 4521 | } |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4522 | } |
| 4523 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4524 | /* manages a server health-check. Returns the time the task accepts to wait, or |
| 4525 | * TIME_ETERNITY for infinity. |
| 4526 | */ |
| 4527 | static struct task *process_chk(struct task *t, void *context, unsigned short state) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4528 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4529 | struct check *check = context; |
| 4530 | |
| 4531 | if (check->type == PR_O2_EXT_CHK) |
| 4532 | return process_chk_proc(t, context, state); |
| 4533 | return process_chk_conn(t, context, state); |
| 4534 | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4535 | } |
| 4536 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4537 | |
| 4538 | static int start_check_task(struct check *check, int mininter, |
| 4539 | int nbcheck, int srvpos) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4540 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4541 | struct task *t; |
| 4542 | unsigned long thread_mask = MAX_THREADS_MASK; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4543 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4544 | if (check->type == PR_O2_EXT_CHK) |
| 4545 | thread_mask = 1; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4546 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4547 | /* task for the check */ |
| 4548 | if ((t = task_new(thread_mask)) == NULL) { |
| 4549 | ha_alert("Starting [%s:%s] check: out of memory.\n", |
| 4550 | check->server->proxy->id, check->server->id); |
| 4551 | return 0; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4552 | } |
| 4553 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4554 | check->task = t; |
| 4555 | t->process = process_chk; |
| 4556 | t->context = check; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4557 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4558 | if (mininter < srv_getinter(check)) |
| 4559 | mininter = srv_getinter(check); |
| 4560 | |
| 4561 | if (global.max_spread_checks && mininter > global.max_spread_checks) |
| 4562 | mininter = global.max_spread_checks; |
| 4563 | |
| 4564 | /* check this every ms */ |
| 4565 | t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck)); |
| 4566 | check->start = now; |
| 4567 | task_queue(t); |
| 4568 | |
| 4569 | return 1; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4570 | } |
| 4571 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4572 | /* updates the server's weight during a warmup stage. Once the final weight is |
| 4573 | * reached, the task automatically stops. Note that any server status change |
| 4574 | * must have updated s->last_change accordingly. |
| 4575 | */ |
| 4576 | static struct task *server_warmup(struct task *t, void *context, unsigned short state) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4577 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4578 | struct server *s = context; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4579 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4580 | /* by default, plan on stopping the task */ |
| 4581 | t->expire = TICK_ETERNITY; |
| 4582 | if ((s->next_admin & SRV_ADMF_MAINT) || |
| 4583 | (s->next_state != SRV_ST_STARTING)) |
| 4584 | return t; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4585 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4586 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4587 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4588 | /* recalculate the weights and update the state */ |
| 4589 | server_recalc_eweight(s, 1); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 4590 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4591 | /* probably that we can refill this server with a bit more connections */ |
| 4592 | pendconn_grab_from_px(s); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 4593 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4594 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 4595 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4596 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 4597 | * whichever is greater, resulting in small 5% steps. |
| 4598 | */ |
| 4599 | if (s->next_state == SRV_ST_STARTING) |
| 4600 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 4601 | return t; |
| 4602 | } |
| 4603 | |
| 4604 | /* |
| 4605 | * Start health-check. |
| 4606 | * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case. |
| 4607 | */ |
| 4608 | static int start_checks() |
| 4609 | { |
| 4610 | |
| 4611 | struct proxy *px; |
| 4612 | struct server *s; |
| 4613 | struct task *t; |
| 4614 | int nbcheck=0, mininter=0, srvpos=0; |
| 4615 | |
| 4616 | /* 0- init the dummy frontend used to create all checks sessions */ |
| 4617 | init_new_proxy(&checks_fe); |
| 4618 | checks_fe.cap = PR_CAP_FE | PR_CAP_BE; |
| 4619 | checks_fe.mode = PR_MODE_TCP; |
| 4620 | checks_fe.maxconn = 0; |
| 4621 | checks_fe.conn_retries = CONN_RETRIES; |
| 4622 | checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC; |
| 4623 | checks_fe.timeout.client = TICK_ETERNITY; |
| 4624 | |
| 4625 | /* 1- count the checkers to run simultaneously. |
| 4626 | * We also determine the minimum interval among all of those which |
| 4627 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 4628 | * will be used to spread their start-up date. Those which have |
| 4629 | * a shorter interval will start independently and will not dictate |
| 4630 | * too short an interval for all others. |
| 4631 | */ |
| 4632 | for (px = proxies_list; px; px = px->next) { |
| 4633 | for (s = px->srv; s; s = s->next) { |
| 4634 | if (s->slowstart) { |
| 4635 | if ((t = task_new(MAX_THREADS_MASK)) == NULL) { |
| 4636 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 4637 | return ERR_ALERT | ERR_FATAL; |
| 4638 | } |
| 4639 | /* We need a warmup task that will be called when the server |
| 4640 | * state switches from down to up. |
| 4641 | */ |
| 4642 | s->warmup = t; |
| 4643 | t->process = server_warmup; |
| 4644 | t->context = s; |
| 4645 | /* server can be in this state only because of */ |
| 4646 | if (s->next_state == SRV_ST_STARTING) |
| 4647 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, (now.tv_sec - s->last_change)) / 20))); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 4648 | } |
| 4649 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4650 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 4651 | nbcheck++; |
| 4652 | if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) && |
| 4653 | (!mininter || mininter > srv_getinter(&s->check))) |
| 4654 | mininter = srv_getinter(&s->check); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 4655 | } |
| 4656 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4657 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 4658 | nbcheck++; |
| 4659 | if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) && |
| 4660 | (!mininter || mininter > srv_getinter(&s->agent))) |
| 4661 | mininter = srv_getinter(&s->agent); |
| 4662 | } |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 4663 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4664 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 4665 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4666 | if (!nbcheck) |
| 4667 | return 0; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 4668 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4669 | srand((unsigned)time(NULL)); |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 4670 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4671 | /* |
| 4672 | * 2- start them as far as possible from each others. For this, we will |
| 4673 | * start them after their interval set to the min interval divided by |
| 4674 | * the number of servers, weighted by the server's position in the list. |
| 4675 | */ |
| 4676 | for (px = proxies_list; px; px = px->next) { |
| 4677 | if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) { |
| 4678 | if (init_pid_list()) { |
| 4679 | ha_alert("Starting [%s] check: out of memory.\n", px->id); |
| 4680 | return ERR_ALERT | ERR_FATAL; |
| 4681 | } |
| 4682 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 4683 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4684 | for (s = px->srv; s; s = s->next) { |
| 4685 | /* A task for the main check */ |
| 4686 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 4687 | if (s->check.type == PR_O2_EXT_CHK) { |
| 4688 | if (!prepare_external_check(&s->check)) |
| 4689 | return ERR_ALERT | ERR_FATAL; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 4690 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4691 | if (!start_check_task(&s->check, mininter, nbcheck, srvpos)) |
| 4692 | return ERR_ALERT | ERR_FATAL; |
| 4693 | srvpos++; |
Christopher Faulet | 9857232 | 2020-03-30 13:16:44 +0200 | [diff] [blame] | 4694 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4695 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4696 | /* A task for a auxiliary agent check */ |
| 4697 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 4698 | if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) { |
| 4699 | return ERR_ALERT | ERR_FATAL; |
| 4700 | } |
| 4701 | srvpos++; |
| 4702 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4703 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4704 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4705 | return 0; |
| 4706 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4707 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4708 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4709 | /* |
| 4710 | * Return value: |
| 4711 | * the port to be used for the health check |
| 4712 | * 0 in case no port could be found for the check |
| 4713 | */ |
| 4714 | static int srv_check_healthcheck_port(struct check *chk) |
| 4715 | { |
| 4716 | int i = 0; |
| 4717 | struct server *srv = NULL; |
| 4718 | |
| 4719 | srv = chk->server; |
| 4720 | |
| 4721 | /* by default, we use the health check port ocnfigured */ |
| 4722 | if (chk->port > 0) |
| 4723 | return chk->port; |
| 4724 | |
| 4725 | /* try to get the port from check_core.addr if check.port not set */ |
| 4726 | i = get_host_port(&chk->addr); |
| 4727 | if (i > 0) |
| 4728 | return i; |
| 4729 | |
| 4730 | /* try to get the port from server address */ |
| 4731 | /* prevent MAPPORTS from working at this point, since checks could |
| 4732 | * not be performed in such case (MAPPORTS impose a relative ports |
| 4733 | * based on live traffic) |
| 4734 | */ |
| 4735 | if (srv->flags & SRV_F_MAPPORTS) |
| 4736 | return 0; |
| 4737 | |
| 4738 | i = srv->svc_port; /* by default */ |
| 4739 | if (i > 0) |
| 4740 | return i; |
| 4741 | |
| 4742 | return 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4743 | } |
| 4744 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4745 | /* Initializes an health-check attached to the server <srv>. Non-zero is returned |
| 4746 | * if an error occurred. |
| 4747 | */ |
| 4748 | static int init_srv_check(struct server *srv) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4749 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4750 | const char *err; |
| 4751 | struct tcpcheck_rule *r; |
| 4752 | int ret = 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4753 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4754 | if (!srv->do_check) |
| 4755 | goto out; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4756 | |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 4757 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4758 | /* If neither a port nor an addr was specified and no check transport |
| 4759 | * layer is forced, then the transport layer used by the checks is the |
| 4760 | * same as for the production traffic. Otherwise we use raw_sock by |
| 4761 | * default, unless one is specified. |
| 4762 | */ |
| 4763 | if (!srv->check.port && !is_addr(&srv->check.addr)) { |
| 4764 | if (!srv->check.use_ssl && srv->use_ssl != -1) { |
| 4765 | srv->check.use_ssl = srv->use_ssl; |
| 4766 | srv->check.xprt = srv->xprt; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4767 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4768 | else if (srv->check.use_ssl == 1) |
| 4769 | srv->check.xprt = xprt_get(XPRT_SSL); |
| 4770 | srv->check.send_proxy |= (srv->pp_opts); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4771 | } |
| 4772 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4773 | /* validate <srv> server health-check settings */ |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 4774 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4775 | /* We need at least a service port, a check port or the first tcp-check |
| 4776 | * rule must be a 'connect' one when checking an IPv4/IPv6 server. |
| 4777 | */ |
| 4778 | if ((srv_check_healthcheck_port(&srv->check) != 0) || |
| 4779 | (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr)))) |
| 4780 | goto init; |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 4781 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4782 | if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) { |
| 4783 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n", |
| 4784 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 4785 | ret |= ERR_ALERT | ERR_ABORT; |
| 4786 | goto out; |
| 4787 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4788 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4789 | /* search the first action (connect / send / expect) in the list */ |
| 4790 | r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules); |
| 4791 | if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) { |
| 4792 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port " |
| 4793 | "nor tcp_check rule 'connect' with port information.\n", |
| 4794 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 4795 | ret |= ERR_ALERT | ERR_ABORT; |
| 4796 | goto out; |
| 4797 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4798 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4799 | /* scan the tcp-check ruleset to ensure a port has been configured */ |
| 4800 | list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) { |
| 4801 | if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port || !get_host_port(&r->connect.addr))) { |
| 4802 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port, " |
| 4803 | "and a tcp_check rule 'connect' with no port information.\n", |
| 4804 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 4805 | ret |= ERR_ALERT | ERR_ABORT; |
| 4806 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4807 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4808 | } |
| 4809 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4810 | init: |
| 4811 | if (!(srv->proxy->options2 & PR_O2_CHK_ANY)) { |
| 4812 | struct tcpcheck_ruleset *rs = NULL; |
| 4813 | struct tcpcheck_rules *rules = &srv->proxy->tcpcheck_rules; |
| 4814 | //char *errmsg = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4815 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4816 | srv->proxy->options2 &= ~PR_O2_CHK_ANY; |
| 4817 | srv->proxy->options2 |= PR_O2_TCPCHK_CHK; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4818 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4819 | rs = find_tcpcheck_ruleset("*tcp-check"); |
| 4820 | if (!rs) { |
| 4821 | rs = create_tcpcheck_ruleset("*tcp-check"); |
| 4822 | if (rs == NULL) { |
| 4823 | ha_alert("config: %s '%s': out of memory.\n", |
| 4824 | proxy_type_str(srv->proxy), srv->proxy->id); |
| 4825 | ret |= ERR_ALERT | ERR_FATAL; |
| 4826 | goto out; |
| 4827 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4828 | } |
| 4829 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4830 | free_tcpcheck_vars(&rules->preset_vars); |
| 4831 | rules->list = &rs->rules; |
| 4832 | rules->flags = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4833 | } |
| 4834 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4835 | err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY); |
| 4836 | if (err) { |
| 4837 | ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n", |
| 4838 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err); |
| 4839 | ret |= ERR_ALERT | ERR_ABORT; |
| 4840 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4841 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4842 | srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED; |
| 4843 | global.maxsock++; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4844 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4845 | out: |
| 4846 | return ret; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4847 | } |
| 4848 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4849 | /* Initializes an agent-check attached to the server <srv>. Non-zero is returned |
| 4850 | * if an error occurred. |
| 4851 | */ |
| 4852 | static int init_srv_agent_check(struct server *srv) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4853 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4854 | struct tcpcheck_rule *chk; |
| 4855 | const char *err; |
| 4856 | int ret = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4857 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4858 | if (!srv->do_agent) |
| 4859 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4860 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4861 | /* If there is no connect rule preceeding all send / expect rules, an |
| 4862 | * implicit one is inserted before all others. |
| 4863 | */ |
| 4864 | chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules); |
| 4865 | if (!chk || chk->action != TCPCHK_ACT_CONNECT) { |
| 4866 | chk = calloc(1, sizeof(*chk)); |
| 4867 | if (!chk) { |
| 4868 | ha_alert("config : %s '%s': unable to add implicit tcp-check connect rule" |
| 4869 | " to agent-check for server '%s' (out of memory).\n", |
| 4870 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 4871 | ret |= ERR_ALERT | ERR_FATAL; |
| 4872 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4873 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4874 | chk->action = TCPCHK_ACT_CONNECT; |
| 4875 | chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT); |
| 4876 | LIST_ADD(srv->agent.tcpcheck_rules->list, &chk->list); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4877 | } |
| 4878 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4879 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4880 | err = init_check(&srv->agent, PR_O2_TCPCHK_CHK); |
| 4881 | if (err) { |
| 4882 | ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n", |
| 4883 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err); |
| 4884 | ret |= ERR_ALERT | ERR_ABORT; |
| 4885 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4886 | } |
| 4887 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4888 | if (!srv->agent.inter) |
| 4889 | srv->agent.inter = srv->check.inter; |
| 4890 | |
| 4891 | srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT; |
| 4892 | global.maxsock++; |
| 4893 | |
| 4894 | out: |
| 4895 | return ret; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4896 | } |
| 4897 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4898 | /* Check tcp-check health-check configuration for the proxy <px>. */ |
| 4899 | static int check_proxy_tcpcheck(struct proxy *px) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4900 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4901 | struct tcpcheck_rule *chk, *back; |
| 4902 | char *comment = NULL, *errmsg = NULL; |
| 4903 | enum tcpcheck_rule_type prev_action = TCPCHK_ACT_COMMENT; |
| 4904 | int ret = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4905 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4906 | if (!(px->cap & PR_CAP_BE) || (px->options2 & PR_O2_CHK_ANY) != PR_O2_TCPCHK_CHK) { |
| 4907 | deinit_proxy_tcpcheck(px); |
| 4908 | goto out; |
| 4909 | } |
| 4910 | |
| 4911 | free(px->check_command); |
| 4912 | free(px->check_path); |
| 4913 | px->check_command = px->check_path = NULL; |
| 4914 | |
| 4915 | if (!px->tcpcheck_rules.list) { |
| 4916 | ha_alert("config : proxy '%s' : tcp-check configured but no ruleset defined.\n", px->id); |
| 4917 | ret |= ERR_ALERT | ERR_FATAL; |
| 4918 | goto out; |
| 4919 | } |
| 4920 | |
| 4921 | /* HTTP ruleset only : */ |
| 4922 | if ((px->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) { |
| 4923 | struct tcpcheck_rule *next; |
| 4924 | |
| 4925 | /* move remaining implicit send rule from "option httpchk" line to the right place. |
| 4926 | * If such rule exists, it must be the first one. In this case, the rule is moved |
| 4927 | * after the first connect rule, if any. Otherwise, nothing is done. |
| 4928 | */ |
| 4929 | chk = get_first_tcpcheck_rule(&px->tcpcheck_rules); |
| 4930 | if (chk && chk->action == TCPCHK_ACT_SEND && (chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) { |
| 4931 | next = get_next_tcpcheck_rule(&px->tcpcheck_rules, chk); |
| 4932 | if (next && next->action == TCPCHK_ACT_CONNECT) { |
| 4933 | LIST_DEL(&chk->list); |
| 4934 | LIST_ADD(&next->list, &chk->list); |
| 4935 | chk->index = next->index; |
| 4936 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4937 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4938 | |
| 4939 | /* add implicit expect rule if the last one is a send. It is inherited from previous |
| 4940 | * versions where the http expect rule was optional. Now it is possible to chained |
| 4941 | * send/expect rules but the last expect may still be implicit. |
| 4942 | */ |
| 4943 | chk = get_last_tcpcheck_rule(&px->tcpcheck_rules); |
| 4944 | if (chk && chk->action == TCPCHK_ACT_SEND) { |
| 4945 | next = parse_tcpcheck_expect((char *[]){"http-check", "expect", "rstatus", "^[23]", ""}, |
| 4946 | 1, px, px->tcpcheck_rules.list, TCPCHK_RULES_HTTP_CHK, |
| 4947 | px->conf.file, px->conf.line, &errmsg); |
| 4948 | if (!next) { |
| 4949 | ha_alert("config : proxy '%s': unable to add implicit http-check expect rule " |
| 4950 | "(%s).\n", px->id, errmsg); |
| 4951 | free(errmsg); |
| 4952 | ret |= ERR_ALERT | ERR_FATAL; |
| 4953 | goto out; |
| 4954 | } |
| 4955 | LIST_ADDQ(px->tcpcheck_rules.list, &next->list); |
| 4956 | next->index = chk->index; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4957 | } |
| 4958 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4959 | |
| 4960 | /* For all ruleset: */ |
| 4961 | |
| 4962 | /* If there is no connect rule preceeding all send / expect rules, an |
| 4963 | * implicit one is inserted before all others. |
| 4964 | */ |
| 4965 | chk = get_first_tcpcheck_rule(&px->tcpcheck_rules); |
| 4966 | if (!chk || chk->action != TCPCHK_ACT_CONNECT) { |
| 4967 | chk = calloc(1, sizeof(*chk)); |
| 4968 | if (!chk) { |
| 4969 | ha_alert("config : proxy '%s': unable to add implicit tcp-check connect rule " |
| 4970 | "(out of memory).\n", px->id); |
| 4971 | ret |= ERR_ALERT | ERR_FATAL; |
| 4972 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4973 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4974 | chk->action = TCPCHK_ACT_CONNECT; |
| 4975 | chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT); |
| 4976 | LIST_ADD(px->tcpcheck_rules.list, &chk->list); |
| 4977 | } |
| 4978 | |
| 4979 | /* Remove all comment rules. To do so, when a such rule is found, the |
| 4980 | * comment is assigned to the following rule(s). |
| 4981 | */ |
| 4982 | list_for_each_entry_safe(chk, back, px->tcpcheck_rules.list, list) { |
| 4983 | if (chk->action != prev_action && prev_action != TCPCHK_ACT_COMMENT) { |
| 4984 | free(comment); |
| 4985 | comment = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4986 | } |
| 4987 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4988 | prev_action = chk->action; |
| 4989 | switch (chk->action) { |
| 4990 | case TCPCHK_ACT_COMMENT: |
| 4991 | free(comment); |
| 4992 | comment = chk->comment; |
| 4993 | LIST_DEL(&chk->list); |
| 4994 | free(chk); |
| 4995 | break; |
| 4996 | case TCPCHK_ACT_CONNECT: |
| 4997 | if (!chk->comment && comment) |
| 4998 | chk->comment = strdup(comment); |
| 4999 | /* fall though */ |
| 5000 | case TCPCHK_ACT_ACTION_KW: |
| 5001 | free(comment); |
| 5002 | comment = NULL; |
| 5003 | break; |
| 5004 | case TCPCHK_ACT_SEND: |
| 5005 | case TCPCHK_ACT_EXPECT: |
| 5006 | if (!chk->comment && comment) |
| 5007 | chk->comment = strdup(comment); |
| 5008 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5009 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5010 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5011 | free(comment); |
| 5012 | comment = NULL; |
| 5013 | |
| 5014 | out: |
| 5015 | return ret; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5016 | } |
| 5017 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5018 | void deinit_proxy_tcpcheck(struct proxy *px) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5019 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5020 | free_tcpcheck_vars(&px->tcpcheck_rules.preset_vars); |
| 5021 | px->tcpcheck_rules.flags = 0; |
| 5022 | px->tcpcheck_rules.list = NULL; |
| 5023 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5024 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5025 | static void deinit_srv_check(struct server *srv) |
| 5026 | { |
| 5027 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 5028 | free_check(&srv->check); |
| 5029 | srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED; |
| 5030 | srv->do_check = 0; |
| 5031 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5032 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5033 | |
| 5034 | static void deinit_srv_agent_check(struct server *srv) |
| 5035 | { |
| 5036 | if (srv->agent.tcpcheck_rules) { |
| 5037 | free_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars); |
| 5038 | free(srv->agent.tcpcheck_rules); |
| 5039 | srv->agent.tcpcheck_rules = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5040 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5041 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5042 | if (srv->agent.state & CHK_ST_CONFIGURED) |
| 5043 | free_check(&srv->agent); |
| 5044 | |
| 5045 | srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT; |
| 5046 | srv->do_agent = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5047 | } |
| 5048 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5049 | static void deinit_tcpchecks() |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5050 | { |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 5051 | struct tcpcheck_ruleset *rs; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5052 | struct tcpcheck_rule *r, *rb; |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 5053 | struct ebpt_node *node, *next; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5054 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 5055 | node = ebpt_first(&shared_tcpchecks); |
| 5056 | while (node) { |
| 5057 | next = ebpt_next(node); |
| 5058 | ebpt_delete(node); |
| 5059 | free(node->key); |
| 5060 | rs = container_of(node, typeof(*rs), node); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5061 | list_for_each_entry_safe(r, rb, &rs->rules, list) { |
| 5062 | LIST_DEL(&r->list); |
| 5063 | free_tcpcheck(r, 0); |
| 5064 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5065 | free(rs); |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 5066 | node = next; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5067 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5068 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5069 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5070 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5071 | REGISTER_POST_SERVER_CHECK(init_srv_check); |
| 5072 | REGISTER_POST_SERVER_CHECK(init_srv_agent_check); |
| 5073 | REGISTER_POST_PROXY_CHECK(check_proxy_tcpcheck); |
| 5074 | REGISTER_POST_CHECK(start_checks); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5075 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5076 | REGISTER_SERVER_DEINIT(deinit_srv_check); |
| 5077 | REGISTER_SERVER_DEINIT(deinit_srv_agent_check); |
| 5078 | REGISTER_PROXY_DEINIT(deinit_proxy_tcpcheck); |
| 5079 | REGISTER_POST_DEINIT(deinit_tcpchecks); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5080 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5081 | /**************************************************************************/ |
| 5082 | /****************************** Email alerts ******************************/ |
| 5083 | /* NOTE: It may be pertinent to use an applet to handle email alerts */ |
| 5084 | /* instead of a tcp-check ruleset */ |
| 5085 | /**************************************************************************/ |
| 5086 | void email_alert_free(struct email_alert *alert) |
| 5087 | { |
| 5088 | struct tcpcheck_rule *rule, *back; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5089 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5090 | if (!alert) |
| 5091 | return; |
| 5092 | |
| 5093 | if (alert->rules.list) { |
| 5094 | list_for_each_entry_safe(rule, back, alert->rules.list, list) { |
| 5095 | LIST_DEL(&rule->list); |
| 5096 | free_tcpcheck(rule, 1); |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 5097 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5098 | free_tcpcheck_vars(&alert->rules.preset_vars); |
| 5099 | free(alert->rules.list); |
| 5100 | alert->rules.list = NULL; |
| 5101 | } |
| 5102 | pool_free(pool_head_email_alert, alert); |
| 5103 | } |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 5104 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5105 | static struct task *process_email_alert(struct task *t, void *context, unsigned short state) |
| 5106 | { |
| 5107 | struct check *check = context; |
| 5108 | struct email_alertq *q; |
| 5109 | struct email_alert *alert; |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 5110 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5111 | q = container_of(check, typeof(*q), check); |
| 5112 | |
| 5113 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
| 5114 | while (1) { |
| 5115 | if (!(check->state & CHK_ST_ENABLED)) { |
| 5116 | if (LIST_ISEMPTY(&q->email_alerts)) { |
| 5117 | /* All alerts processed, queue the task */ |
| 5118 | t->expire = TICK_ETERNITY; |
| 5119 | task_queue(t); |
| 5120 | goto end; |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 5121 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5122 | |
| 5123 | alert = LIST_NEXT(&q->email_alerts, typeof(alert), list); |
| 5124 | LIST_DEL(&alert->list); |
| 5125 | t->expire = now_ms; |
| 5126 | check->tcpcheck_rules = &alert->rules; |
| 5127 | check->status = HCHK_STATUS_INI; |
| 5128 | check->state |= CHK_ST_ENABLED; |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 5129 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5130 | |
| 5131 | process_chk(t, context, state); |
| 5132 | if (check->state & CHK_ST_INPROGRESS) |
| 5133 | break; |
| 5134 | |
| 5135 | alert = container_of(check->tcpcheck_rules, typeof(*alert), rules); |
| 5136 | email_alert_free(alert); |
| 5137 | check->tcpcheck_rules = NULL; |
| 5138 | check->server = NULL; |
| 5139 | check->state &= ~CHK_ST_ENABLED; |
| 5140 | } |
| 5141 | end: |
| 5142 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
| 5143 | return t; |
| 5144 | } |
| 5145 | |
| 5146 | /* Initializes mailer alerts for the proxy <p> using <mls> parameters. |
| 5147 | * |
| 5148 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 5149 | * filled. |
| 5150 | */ |
| 5151 | int init_email_alert(struct mailers *mls, struct proxy *p, char **err) |
| 5152 | { |
| 5153 | struct mailer *mailer; |
| 5154 | struct email_alertq *queues; |
| 5155 | const char *err_str; |
| 5156 | int i = 0; |
| 5157 | |
| 5158 | if ((queues = calloc(mls->count, sizeof(*queues))) == NULL) { |
| 5159 | memprintf(err, "out of memory while allocating mailer alerts queues"); |
| 5160 | goto fail_no_queue; |
| 5161 | } |
| 5162 | |
| 5163 | for (mailer = mls->mailer_list; mailer; i++, mailer = mailer->next) { |
| 5164 | struct email_alertq *q = &queues[i]; |
| 5165 | struct check *check = &q->check; |
| 5166 | struct task *t; |
| 5167 | |
| 5168 | LIST_INIT(&q->email_alerts); |
| 5169 | HA_SPIN_INIT(&q->lock); |
| 5170 | check->inter = mls->timeout.mail; |
| 5171 | check->rise = DEF_AGENT_RISETIME; |
| 5172 | check->proxy = p; |
| 5173 | check->fall = DEF_AGENT_FALLTIME; |
| 5174 | if ((err_str = init_check(check, PR_O2_TCPCHK_CHK))) { |
| 5175 | memprintf(err, "%s", err_str); |
| 5176 | goto error; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5177 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5178 | |
| 5179 | check->xprt = mailer->xprt; |
| 5180 | check->addr = mailer->addr; |
| 5181 | check->port = get_host_port(&mailer->addr); |
| 5182 | |
| 5183 | if ((t = task_new(MAX_THREADS_MASK)) == NULL) { |
| 5184 | memprintf(err, "out of memory while allocating mailer alerts task"); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5185 | goto error; |
| 5186 | } |
| 5187 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5188 | check->task = t; |
| 5189 | t->process = process_email_alert; |
| 5190 | t->context = check; |
| 5191 | |
| 5192 | /* check this in one ms */ |
| 5193 | t->expire = TICK_ETERNITY; |
| 5194 | check->start = now; |
| 5195 | task_queue(t); |
| 5196 | } |
| 5197 | |
| 5198 | mls->users++; |
| 5199 | free(p->email_alert.mailers.name); |
| 5200 | p->email_alert.mailers.m = mls; |
| 5201 | p->email_alert.queues = queues; |
| 5202 | return 0; |
| 5203 | |
| 5204 | error: |
| 5205 | for (i = 0; i < mls->count; i++) { |
| 5206 | struct email_alertq *q = &queues[i]; |
| 5207 | struct check *check = &q->check; |
| 5208 | |
| 5209 | free_check(check); |
| 5210 | } |
| 5211 | free(queues); |
| 5212 | fail_no_queue: |
| 5213 | return 1; |
| 5214 | } |
| 5215 | |
| 5216 | static int add_tcpcheck_expect_str(struct tcpcheck_rules *rules, const char *str) |
| 5217 | { |
| 5218 | struct tcpcheck_rule *tcpcheck, *prev_check; |
| 5219 | struct tcpcheck_expect *expect; |
| 5220 | |
| 5221 | if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL) |
| 5222 | return 0; |
| 5223 | memset(tcpcheck, 0, sizeof(*tcpcheck)); |
| 5224 | tcpcheck->action = TCPCHK_ACT_EXPECT; |
| 5225 | |
| 5226 | expect = &tcpcheck->expect; |
| 5227 | expect->type = TCPCHK_EXPECT_STRING; |
| 5228 | LIST_INIT(&expect->onerror_fmt); |
| 5229 | LIST_INIT(&expect->onsuccess_fmt); |
| 5230 | expect->ok_status = HCHK_STATUS_L7OKD; |
| 5231 | expect->err_status = HCHK_STATUS_L7RSP; |
| 5232 | expect->tout_status = HCHK_STATUS_L7TOUT; |
| 5233 | expect->data = ist2(strdup(str), strlen(str)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 5234 | if (!isttest(expect->data)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5235 | pool_free(pool_head_tcpcheck_rule, tcpcheck); |
| 5236 | return 0; |
| 5237 | } |
| 5238 | |
| 5239 | /* All tcp-check expect points back to the first inverse expect rule |
| 5240 | * in a chain of one or more expect rule, potentially itself. |
| 5241 | */ |
| 5242 | tcpcheck->expect.head = tcpcheck; |
| 5243 | list_for_each_entry_rev(prev_check, rules->list, list) { |
| 5244 | if (prev_check->action == TCPCHK_ACT_EXPECT) { |
| 5245 | if (prev_check->expect.flags & TCPCHK_EXPT_FL_INV) |
| 5246 | tcpcheck->expect.head = prev_check; |
| 5247 | continue; |
| 5248 | } |
| 5249 | if (prev_check->action != TCPCHK_ACT_COMMENT && prev_check->action != TCPCHK_ACT_ACTION_KW) |
| 5250 | break; |
| 5251 | } |
| 5252 | LIST_ADDQ(rules->list, &tcpcheck->list); |
| 5253 | return 1; |
| 5254 | } |
| 5255 | |
| 5256 | static int add_tcpcheck_send_strs(struct tcpcheck_rules *rules, const char * const *strs) |
| 5257 | { |
| 5258 | struct tcpcheck_rule *tcpcheck; |
| 5259 | struct tcpcheck_send *send; |
| 5260 | const char *in; |
| 5261 | char *dst; |
| 5262 | int i; |
| 5263 | |
| 5264 | if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL) |
| 5265 | return 0; |
| 5266 | memset(tcpcheck, 0, sizeof(*tcpcheck)); |
| 5267 | tcpcheck->action = TCPCHK_ACT_SEND; |
| 5268 | |
| 5269 | send = &tcpcheck->send; |
| 5270 | send->type = TCPCHK_SEND_STRING; |
| 5271 | |
| 5272 | for (i = 0; strs[i]; i++) |
| 5273 | send->data.len += strlen(strs[i]); |
| 5274 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 5275 | send->data.ptr = malloc(istlen(send->data) + 1); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5276 | if (!isttest(send->data)) { |
| 5277 | pool_free(pool_head_tcpcheck_rule, tcpcheck); |
| 5278 | return 0; |
| 5279 | } |
| 5280 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 5281 | dst = istptr(send->data); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5282 | for (i = 0; strs[i]; i++) |
| 5283 | for (in = strs[i]; (*dst = *in++); dst++); |
| 5284 | *dst = 0; |
| 5285 | |
| 5286 | LIST_ADDQ(rules->list, &tcpcheck->list); |
| 5287 | return 1; |
| 5288 | } |
| 5289 | |
| 5290 | static int enqueue_one_email_alert(struct proxy *p, struct server *s, |
| 5291 | struct email_alertq *q, const char *msg) |
| 5292 | { |
| 5293 | struct email_alert *alert; |
| 5294 | struct tcpcheck_rule *tcpcheck; |
| 5295 | struct check *check = &q->check; |
| 5296 | |
| 5297 | if ((alert = pool_alloc(pool_head_email_alert)) == NULL) |
| 5298 | goto error; |
| 5299 | LIST_INIT(&alert->list); |
| 5300 | alert->rules.flags = TCPCHK_RULES_TCP_CHK; |
| 5301 | alert->rules.list = calloc(1, sizeof(*alert->rules.list)); |
| 5302 | if (!alert->rules.list) |
| 5303 | goto error; |
| 5304 | LIST_INIT(alert->rules.list); |
| 5305 | LIST_INIT(&alert->rules.preset_vars); /* unused for email alerts */ |
| 5306 | alert->srv = s; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5307 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5308 | if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL) |
| 5309 | goto error; |
| 5310 | memset(tcpcheck, 0, sizeof(*tcpcheck)); |
| 5311 | tcpcheck->action = TCPCHK_ACT_CONNECT; |
| 5312 | tcpcheck->comment = NULL; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5313 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5314 | LIST_ADDQ(alert->rules.list, &tcpcheck->list); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5315 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5316 | if (!add_tcpcheck_expect_str(&alert->rules, "220 ")) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5317 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5318 | |
| 5319 | { |
| 5320 | const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" }; |
| 5321 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
| 5322 | goto error; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5323 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5324 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5325 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
| 5326 | goto error; |
| 5327 | |
| 5328 | { |
| 5329 | const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" }; |
| 5330 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5331 | goto error; |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5332 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5333 | |
| 5334 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
| 5335 | goto error; |
| 5336 | |
| 5337 | { |
| 5338 | const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" }; |
| 5339 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5340 | goto error; |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5341 | } |
| 5342 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5343 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
| 5344 | goto error; |
| 5345 | |
| 5346 | { |
| 5347 | const char * const strs[2] = { "DATA\r\n" }; |
| 5348 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5349 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5350 | } |
| 5351 | |
| 5352 | if (!add_tcpcheck_expect_str(&alert->rules, "354 ")) |
| 5353 | goto error; |
| 5354 | |
| 5355 | { |
| 5356 | struct tm tm; |
| 5357 | char datestr[48]; |
| 5358 | const char * const strs[18] = { |
| 5359 | "From: ", p->email_alert.from, "\r\n", |
| 5360 | "To: ", p->email_alert.to, "\r\n", |
| 5361 | "Date: ", datestr, "\r\n", |
| 5362 | "Subject: [HAproxy Alert] ", msg, "\r\n", |
| 5363 | "\r\n", |
| 5364 | msg, "\r\n", |
| 5365 | "\r\n", |
| 5366 | ".\r\n", |
| 5367 | NULL |
| 5368 | }; |
| 5369 | |
| 5370 | get_localtime(date.tv_sec, &tm); |
| 5371 | |
| 5372 | if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) { |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5373 | goto error; |
| 5374 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5375 | |
| 5376 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5377 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5381 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5382 | |
| 5383 | { |
| 5384 | const char * const strs[2] = { "QUIT\r\n" }; |
| 5385 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
| 5386 | goto error; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5387 | } |
| 5388 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5389 | if (!add_tcpcheck_expect_str(&alert->rules, "221 ")) |
| 5390 | goto error; |
| 5391 | |
| 5392 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
| 5393 | task_wakeup(check->task, TASK_WOKEN_MSG); |
| 5394 | LIST_ADDQ(&q->email_alerts, &alert->list); |
| 5395 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
| 5396 | return 1; |
| 5397 | |
| 5398 | error: |
| 5399 | email_alert_free(alert); |
| 5400 | return 0; |
| 5401 | } |
| 5402 | |
| 5403 | static void enqueue_email_alert(struct proxy *p, struct server *s, const char *msg) |
| 5404 | { |
| 5405 | int i; |
| 5406 | struct mailer *mailer; |
| 5407 | |
| 5408 | for (i = 0, mailer = p->email_alert.mailers.m->mailer_list; |
| 5409 | i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) { |
| 5410 | if (!enqueue_one_email_alert(p, s, &p->email_alert.queues[i], msg)) { |
| 5411 | ha_alert("Email alert [%s] could not be enqueued: out of memory\n", p->id); |
| 5412 | return; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5413 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5414 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5415 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5416 | return; |
| 5417 | } |
| 5418 | |
| 5419 | /* |
| 5420 | * Send email alert if configured. |
| 5421 | */ |
| 5422 | void send_email_alert(struct server *s, int level, const char *format, ...) |
| 5423 | { |
| 5424 | va_list argp; |
| 5425 | char buf[1024]; |
| 5426 | int len; |
| 5427 | struct proxy *p = s->proxy; |
| 5428 | |
| 5429 | if (!p->email_alert.mailers.m || level > p->email_alert.level || format == NULL) |
| 5430 | return; |
| 5431 | |
| 5432 | va_start(argp, format); |
| 5433 | len = vsnprintf(buf, sizeof(buf), format, argp); |
| 5434 | va_end(argp); |
| 5435 | |
| 5436 | if (len < 0 || len >= sizeof(buf)) { |
| 5437 | ha_alert("Email alert [%s] could not format message\n", p->id); |
| 5438 | return; |
| 5439 | } |
| 5440 | |
| 5441 | enqueue_email_alert(p, s, buf); |
| 5442 | } |
| 5443 | |
| 5444 | /**************************************************************************/ |
| 5445 | /************************** Check sample fetches **************************/ |
| 5446 | /**************************************************************************/ |
| 5447 | /* extracts check payload at a fixed position and length */ |
| 5448 | static int |
| 5449 | smp_fetch_chk_payload(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 5450 | { |
| 5451 | unsigned int buf_offset = ((arg_p[0].type == ARGT_SINT) ? arg_p[0].data.sint : 0); |
| 5452 | unsigned int buf_size = ((arg_p[1].type == ARGT_SINT) ? arg_p[1].data.sint : 0); |
| 5453 | struct check *check = (smp->sess ? objt_check(smp->sess->origin) : NULL); |
| 5454 | struct buffer *buf; |
| 5455 | |
| 5456 | if (!check) |
| 5457 | return 0; |
| 5458 | |
| 5459 | buf = &check->bi; |
| 5460 | if (buf_offset > b_data(buf)) |
| 5461 | goto no_match; |
| 5462 | if (buf_offset + buf_size > b_data(buf)) |
| 5463 | buf_size = 0; |
| 5464 | |
| 5465 | /* init chunk as read only */ |
| 5466 | smp->data.type = SMP_T_STR; |
| 5467 | smp->flags = SMP_F_VOLATILE | SMP_F_CONST; |
| 5468 | chunk_initlen(&smp->data.u.str, b_head(buf) + buf_offset, 0, (buf_size ? buf_size : (b_data(buf) - buf_offset))); |
| 5469 | |
| 5470 | return 1; |
| 5471 | |
| 5472 | no_match: |
| 5473 | smp->flags = 0; |
| 5474 | return 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5475 | } |
| 5476 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5477 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
| 5478 | { "check.payload", smp_fetch_chk_payload, ARG2(0,SINT,SINT), NULL, SMP_T_STR, SMP_USE_INTRN }, |
| 5479 | { /* END */ }, |
| 5480 | }}; |
| 5481 | |
| 5482 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
| 5483 | |
| 5484 | |
| 5485 | /**************************************************************************/ |
| 5486 | /************************ Check's parsing functions ***********************/ |
| 5487 | /**************************************************************************/ |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5488 | /* Parses the "tcp-check" proxy keyword */ |
| 5489 | static int proxy_parse_tcpcheck(char **args, int section, struct proxy *curpx, |
| 5490 | struct proxy *defpx, const char *file, int line, |
| 5491 | char **errmsg) |
| 5492 | { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5493 | struct tcpcheck_ruleset *rs = NULL; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5494 | struct tcpcheck_rule *chk = NULL; |
Gaetan Rivet | 5301b01 | 2020-02-25 17:19:17 +0100 | [diff] [blame] | 5495 | int index, cur_arg, ret = 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5496 | |
| 5497 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[0], NULL)) |
| 5498 | ret = 1; |
| 5499 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5500 | /* Deduce the ruleset name from the proxy info */ |
| 5501 | chunk_printf(&trash, "*tcp-check-%s_%s-%d", |
| 5502 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 5503 | curpx->conf.file, curpx->conf.line); |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 5504 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5505 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5506 | if (rs == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5507 | rs = create_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5508 | if (rs == NULL) { |
| 5509 | memprintf(errmsg, "out of memory.\n"); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5510 | goto error; |
| 5511 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5512 | } |
| 5513 | |
Gaetan Rivet | 5301b01 | 2020-02-25 17:19:17 +0100 | [diff] [blame] | 5514 | index = 0; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5515 | if (!LIST_ISEMPTY(&rs->rules)) { |
| 5516 | chk = LIST_PREV(&rs->rules, typeof(chk), list); |
Gaetan Rivet | 5301b01 | 2020-02-25 17:19:17 +0100 | [diff] [blame] | 5517 | index = chk->index + 1; |
| 5518 | } |
| 5519 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5520 | cur_arg = 1; |
| 5521 | if (strcmp(args[cur_arg], "connect") == 0) |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5522 | chk = parse_tcpcheck_connect(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5523 | else if (strcmp(args[cur_arg], "send") == 0 || strcmp(args[cur_arg], "send-binary") == 0) |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5524 | chk = parse_tcpcheck_send(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5525 | else if (strcmp(args[cur_arg], "expect") == 0) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5526 | chk = parse_tcpcheck_expect(args, cur_arg, curpx, &rs->rules, 0, file, line, errmsg); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5527 | else if (strcmp(args[cur_arg], "comment") == 0) |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5528 | chk = parse_tcpcheck_comment(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5529 | else { |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 5530 | struct action_kw *kw = action_kw_tcp_check_lookup(args[cur_arg]); |
| 5531 | |
| 5532 | if (!kw) { |
| 5533 | action_kw_tcp_check_build_list(&trash); |
| 5534 | memprintf(errmsg, "'%s' only supports 'comment', 'connect', 'send', 'send-binary', 'expect'" |
| 5535 | "%s%s. but got '%s'", |
| 5536 | args[0], (*trash.area ? ", " : ""), trash.area, args[1]); |
| 5537 | goto error; |
| 5538 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5539 | chk = parse_tcpcheck_action(args, cur_arg, curpx, &rs->rules, kw, file, line, errmsg); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | if (!chk) { |
| 5543 | memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg); |
| 5544 | goto error; |
| 5545 | } |
| 5546 | ret = (*errmsg != NULL); /* Handle warning */ |
| 5547 | |
| 5548 | /* No error: add the tcp-check rule in the list */ |
Gaetan Rivet | 5301b01 | 2020-02-25 17:19:17 +0100 | [diff] [blame] | 5549 | chk->index = index; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5550 | LIST_ADDQ(&rs->rules, &chk->list); |
| 5551 | |
| 5552 | if ((curpx->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK && |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 5553 | (curpx->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5554 | /* Use this ruleset if the proxy already has tcp-check enabled */ |
| 5555 | curpx->tcpcheck_rules.list = &rs->rules; |
| 5556 | curpx->tcpcheck_rules.flags &= ~TCPCHK_RULES_UNUSED_TCP_RS; |
| 5557 | } |
| 5558 | else { |
| 5559 | /* mark this ruleset as unused for now */ |
| 5560 | curpx->tcpcheck_rules.flags |= TCPCHK_RULES_UNUSED_TCP_RS; |
| 5561 | } |
| 5562 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5563 | return ret; |
| 5564 | |
| 5565 | error: |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5566 | free_tcpcheck(chk, 0); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5567 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5568 | return -1; |
| 5569 | } |
| 5570 | |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5571 | /* Parses the "http-check" proxy keyword */ |
| 5572 | static int proxy_parse_httpcheck(char **args, int section, struct proxy *curpx, |
| 5573 | struct proxy *defpx, const char *file, int line, |
| 5574 | char **errmsg) |
| 5575 | { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5576 | struct tcpcheck_ruleset *rs = NULL; |
| 5577 | struct tcpcheck_rule *chk = NULL; |
| 5578 | int index, cur_arg, ret = 0; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5579 | |
| 5580 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[0], NULL)) |
| 5581 | ret = 1; |
| 5582 | |
| 5583 | cur_arg = 1; |
| 5584 | if (strcmp(args[cur_arg], "disable-on-404") == 0) { |
| 5585 | /* enable a graceful server shutdown on an HTTP 404 response */ |
| 5586 | curpx->options |= PR_O_DISABLE404; |
| 5587 | if (too_many_args(1, args, errmsg, NULL)) |
| 5588 | goto error; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5589 | goto out; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5590 | } |
| 5591 | else if (strcmp(args[cur_arg], "send-state") == 0) { |
| 5592 | /* enable emission of the apparent state of a server in HTTP checks */ |
| 5593 | curpx->options2 |= PR_O2_CHK_SNDST; |
| 5594 | if (too_many_args(1, args, errmsg, NULL)) |
| 5595 | goto error; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5596 | goto out; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5597 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5598 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5599 | /* Deduce the ruleset name from the proxy info */ |
| 5600 | chunk_printf(&trash, "*http-check-%s_%s-%d", |
| 5601 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 5602 | curpx->conf.file, curpx->conf.line); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5603 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5604 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5605 | if (rs == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5606 | rs = create_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5607 | if (rs == NULL) { |
| 5608 | memprintf(errmsg, "out of memory.\n"); |
| 5609 | goto error; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5610 | } |
| 5611 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5612 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5613 | index = 0; |
| 5614 | if (!LIST_ISEMPTY(&rs->rules)) { |
| 5615 | chk = LIST_PREV(&rs->rules, typeof(chk), list); |
| 5616 | if (chk->action != TCPCHK_ACT_SEND || !(chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) |
| 5617 | index = chk->index + 1; |
| 5618 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5619 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5620 | if (strcmp(args[cur_arg], "connect") == 0) |
| 5621 | chk = parse_tcpcheck_connect(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
| 5622 | else if (strcmp(args[cur_arg], "send") == 0) |
| 5623 | chk = parse_tcpcheck_send_http(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
| 5624 | else if (strcmp(args[cur_arg], "expect") == 0) |
| 5625 | chk = parse_tcpcheck_expect(args, cur_arg, curpx, &rs->rules, TCPCHK_RULES_HTTP_CHK, |
| 5626 | file, line, errmsg); |
| 5627 | else if (strcmp(args[cur_arg], "comment") == 0) |
| 5628 | chk = parse_tcpcheck_comment(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
| 5629 | else { |
| 5630 | struct action_kw *kw = action_kw_tcp_check_lookup(args[cur_arg]); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5631 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5632 | if (!kw) { |
| 5633 | action_kw_tcp_check_build_list(&trash); |
| 5634 | memprintf(errmsg, "'%s' only supports 'disable-on-404', 'send-state', 'comment', 'connect'," |
| 5635 | " 'send', 'expect'%s%s. but got '%s'", |
| 5636 | args[0], (*trash.area ? ", " : ""), trash.area, args[1]); |
| 5637 | goto error; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5638 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5639 | chk = parse_tcpcheck_action(args, cur_arg, curpx, &rs->rules, kw, file, line, errmsg); |
| 5640 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5641 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5642 | if (!chk) { |
| 5643 | memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg); |
| 5644 | goto error; |
| 5645 | } |
| 5646 | ret = (*errmsg != NULL); /* Handle warning */ |
| 5647 | |
| 5648 | chk->index = index; |
| 5649 | if ((curpx->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK && |
| 5650 | (curpx->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) { |
| 5651 | /* Use this ruleset if the proxy already has http-check enabled */ |
| 5652 | curpx->tcpcheck_rules.list = &rs->rules; |
| 5653 | curpx->tcpcheck_rules.flags &= ~TCPCHK_RULES_UNUSED_HTTP_RS; |
| 5654 | if (!tcpcheck_add_http_rule(chk, &curpx->tcpcheck_rules, errmsg)) { |
| 5655 | memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg); |
| 5656 | curpx->tcpcheck_rules.list = NULL; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5657 | goto error; |
| 5658 | } |
| 5659 | } |
| 5660 | else { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5661 | /* mark this ruleset as unused for now */ |
| 5662 | curpx->tcpcheck_rules.flags |= TCPCHK_RULES_UNUSED_HTTP_RS; |
| 5663 | LIST_ADDQ(&rs->rules, &chk->list); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5664 | } |
| 5665 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5666 | out: |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5667 | return ret; |
| 5668 | |
| 5669 | error: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5670 | free_tcpcheck(chk, 0); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5671 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5672 | return -1; |
| 5673 | } |
| 5674 | |
Christopher Faulet | e9111b6 | 2020-04-09 18:12:08 +0200 | [diff] [blame] | 5675 | /* Parses the "external-check" proxy keyword */ |
| 5676 | static int proxy_parse_extcheck(char **args, int section, struct proxy *curpx, |
| 5677 | struct proxy *defpx, const char *file, int line, |
| 5678 | char **errmsg) |
| 5679 | { |
| 5680 | int cur_arg, ret = 0; |
| 5681 | |
| 5682 | cur_arg = 1; |
| 5683 | if (!*(args[cur_arg])) { |
| 5684 | memprintf(errmsg, "missing argument after '%s'.\n", args[0]); |
| 5685 | goto error; |
| 5686 | } |
| 5687 | |
| 5688 | if (strcmp(args[cur_arg], "command") == 0) { |
| 5689 | if (too_many_args(2, args, errmsg, NULL)) |
| 5690 | goto error; |
| 5691 | if (!*(args[cur_arg+1])) { |
| 5692 | memprintf(errmsg, "missing argument after '%s'.", args[cur_arg]); |
| 5693 | goto error; |
| 5694 | } |
| 5695 | free(curpx->check_command); |
| 5696 | curpx->check_command = strdup(args[cur_arg+1]); |
| 5697 | } |
| 5698 | else if (strcmp(args[cur_arg], "path") == 0) { |
| 5699 | if (too_many_args(2, args, errmsg, NULL)) |
| 5700 | goto error; |
| 5701 | if (!*(args[cur_arg+1])) { |
| 5702 | memprintf(errmsg, "missing argument after '%s'.", args[cur_arg]); |
| 5703 | goto error; |
| 5704 | } |
| 5705 | free(curpx->check_path); |
| 5706 | curpx->check_path = strdup(args[cur_arg+1]); |
| 5707 | } |
| 5708 | else { |
| 5709 | memprintf(errmsg, "'%s' only supports 'command' and 'path'. but got '%s'.", |
| 5710 | args[0], args[1]); |
| 5711 | goto error; |
| 5712 | } |
| 5713 | |
| 5714 | ret = (*errmsg != NULL); /* Handle warning */ |
| 5715 | return ret; |
| 5716 | |
| 5717 | error: |
| 5718 | return -1; |
| 5719 | } |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5720 | |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 5721 | /* Parses the "option tcp-check" proxy keyword */ |
| 5722 | int proxy_parse_tcp_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 5723 | const char *file, int line) |
| 5724 | { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5725 | struct tcpcheck_ruleset *rs = NULL; |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 5726 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 5727 | int err_code = 0; |
| 5728 | |
| 5729 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 5730 | err_code |= ERR_WARN; |
| 5731 | |
| 5732 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 5733 | goto out; |
| 5734 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5735 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 5736 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 5737 | |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 5738 | if ((rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5739 | /* If a tcp-check rulesset is already set, do nothing */ |
| 5740 | if (rules->list) |
| 5741 | goto out; |
| 5742 | |
| 5743 | /* If a tcp-check ruleset is waiting to be used for the current proxy, |
| 5744 | * get it. |
| 5745 | */ |
| 5746 | if (rules->flags & TCPCHK_RULES_UNUSED_TCP_RS) |
| 5747 | goto curpx_ruleset; |
| 5748 | |
| 5749 | /* Otherwise, try to get the tcp-check ruleset of the default proxy */ |
| 5750 | chunk_printf(&trash, "*tcp-check-defaults_%s-%d", defpx->conf.file, defpx->conf.line); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5751 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5752 | if (rs) |
| 5753 | goto ruleset_found; |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 5754 | } |
| 5755 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5756 | curpx_ruleset: |
| 5757 | /* Deduce the ruleset name from the proxy info */ |
| 5758 | chunk_printf(&trash, "*tcp-check-%s_%s-%d", |
| 5759 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 5760 | curpx->conf.file, curpx->conf.line); |
| 5761 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5762 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5763 | if (rs == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5764 | rs = create_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5765 | if (rs == NULL) { |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 5766 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 5767 | goto error; |
| 5768 | } |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 5769 | } |
| 5770 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5771 | ruleset_found: |
| 5772 | free_tcpcheck_vars(&rules->preset_vars); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5773 | rules->list = &rs->rules; |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 5774 | rules->flags |= TCPCHK_RULES_TCP_CHK; |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 5775 | |
| 5776 | out: |
| 5777 | return err_code; |
| 5778 | |
| 5779 | error: |
| 5780 | err_code |= ERR_ALERT | ERR_FATAL; |
| 5781 | goto out; |
| 5782 | } |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5783 | |
| 5784 | /* Parses the "option redis-check" proxy keyword */ |
| 5785 | int proxy_parse_redis_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 5786 | const char *file, int line) |
| 5787 | { |
| 5788 | static char *redis_req = "*1\r\n$4\r\nPING\r\n"; |
| 5789 | static char *redis_res = "+PONG\r\n"; |
| 5790 | |
| 5791 | struct tcpcheck_ruleset *rs = NULL; |
| 5792 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 5793 | struct tcpcheck_rule *chk; |
| 5794 | char *errmsg = NULL; |
| 5795 | int err_code = 0; |
| 5796 | |
| 5797 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 5798 | err_code |= ERR_WARN; |
| 5799 | |
| 5800 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 5801 | goto out; |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5802 | |
| 5803 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 5804 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 5805 | |
| 5806 | free_tcpcheck_vars(&rules->preset_vars); |
| 5807 | rules->list = NULL; |
| 5808 | rules->flags = 0; |
| 5809 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5810 | rs = find_tcpcheck_ruleset("*redis-check"); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5811 | if (rs) |
| 5812 | goto ruleset_found; |
| 5813 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5814 | rs = create_tcpcheck_ruleset("*redis-check"); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5815 | if (rs == NULL) { |
| 5816 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 5817 | goto error; |
| 5818 | } |
| 5819 | |
| 5820 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send", redis_req, ""}, |
| 5821 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 5822 | if (!chk) { |
| 5823 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 5824 | goto error; |
| 5825 | } |
| 5826 | chk->index = 0; |
| 5827 | LIST_ADDQ(&rs->rules, &chk->list); |
| 5828 | |
| 5829 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "string", redis_res, |
| 5830 | "error-status", "L7STS", |
| 5831 | "on-error", "%[check.payload(),cut_crlf]", |
| 5832 | "on-success", "Redis server is ok", |
| 5833 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5834 | 1, curpx, &rs->rules, TCPCHK_RULES_REDIS_CHK, file, line, &errmsg); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5835 | if (!chk) { |
| 5836 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 5837 | goto error; |
| 5838 | } |
| 5839 | chk->index = 1; |
| 5840 | LIST_ADDQ(&rs->rules, &chk->list); |
| 5841 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 5842 | ebis_insert(&shared_tcpchecks, &rs->node); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5843 | |
| 5844 | ruleset_found: |
| 5845 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5846 | rules->flags |= TCPCHK_RULES_REDIS_CHK; |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5847 | |
| 5848 | out: |
| 5849 | free(errmsg); |
| 5850 | return err_code; |
| 5851 | |
| 5852 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5853 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5854 | err_code |= ERR_ALERT | ERR_FATAL; |
| 5855 | goto out; |
| 5856 | } |
| 5857 | |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5858 | |
| 5859 | /* Parses the "option ssl-hello-chk" proxy keyword */ |
| 5860 | int proxy_parse_ssl_hello_chk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 5861 | const char *file, int line) |
| 5862 | { |
| 5863 | /* This is the SSLv3 CLIENT HELLO packet used in conjunction with the |
| 5864 | * ssl-hello-chk option to ensure that the remote server speaks SSL. |
| 5865 | * |
| 5866 | * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details. |
| 5867 | */ |
| 5868 | static char sslv3_client_hello[] = { |
| 5869 | "16" /* ContentType : 0x16 = Hanshake */ |
| 5870 | "0300" /* ProtocolVersion : 0x0300 = SSLv3 */ |
| 5871 | "0079" /* ContentLength : 0x79 bytes after this one */ |
| 5872 | "01" /* HanshakeType : 0x01 = CLIENT HELLO */ |
| 5873 | "000075" /* HandshakeLength : 0x75 bytes after this one */ |
| 5874 | "0300" /* Hello Version : 0x0300 = v3 */ |
| 5875 | "%[date(),htonl,hex]" /* Unix GMT Time (s) : filled with <now> (@0x0B) */ |
| 5876 | "%[str(HAPROXYSSLCHK\nHAPROXYSSLCHK\n),hex]" /* Random : must be exactly 28 bytes */ |
| 5877 | "00" /* Session ID length : empty (no session ID) */ |
| 5878 | "004E" /* Cipher Suite Length : 78 bytes after this one */ |
| 5879 | "0001" "0002" "0003" "0004" /* 39 most common ciphers : */ |
| 5880 | "0005" "0006" "0007" "0008" /* 0x01...0x1B, 0x2F...0x3A */ |
| 5881 | "0009" "000A" "000B" "000C" /* This covers RSA/DH, */ |
| 5882 | "000D" "000E" "000F" "0010" /* various bit lengths, */ |
| 5883 | "0011" "0012" "0013" "0014" /* SHA1/MD5, DES/3DES/AES... */ |
| 5884 | "0015" "0016" "0017" "0018" |
| 5885 | "0019" "001A" "001B" "002F" |
| 5886 | "0030" "0031" "0032" "0033" |
| 5887 | "0034" "0035" "0036" "0037" |
| 5888 | "0038" "0039" "003A" |
| 5889 | "01" /* Compression Length : 0x01 = 1 byte for types */ |
| 5890 | "00" /* Compression Type : 0x00 = NULL compression */ |
| 5891 | }; |
| 5892 | |
| 5893 | struct tcpcheck_ruleset *rs = NULL; |
| 5894 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 5895 | struct tcpcheck_rule *chk; |
| 5896 | char *errmsg = NULL; |
| 5897 | int err_code = 0; |
| 5898 | |
| 5899 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 5900 | err_code |= ERR_WARN; |
| 5901 | |
| 5902 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 5903 | goto out; |
| 5904 | |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5905 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 5906 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 5907 | |
| 5908 | free_tcpcheck_vars(&rules->preset_vars); |
| 5909 | rules->list = NULL; |
| 5910 | rules->flags = 0; |
| 5911 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5912 | rs = find_tcpcheck_ruleset("*ssl-hello-check"); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5913 | if (rs) |
| 5914 | goto ruleset_found; |
| 5915 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5916 | rs = create_tcpcheck_ruleset("*ssl-hello-check"); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5917 | if (rs == NULL) { |
| 5918 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 5919 | goto error; |
| 5920 | } |
| 5921 | |
| 5922 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", sslv3_client_hello, "log-format", ""}, |
| 5923 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 5924 | if (!chk) { |
| 5925 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 5926 | goto error; |
| 5927 | } |
| 5928 | chk->index = 0; |
| 5929 | LIST_ADDQ(&rs->rules, &chk->list); |
| 5930 | |
| 5931 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^1[56]", |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 5932 | "min-recv", "5", "ok-status", "L6OK", |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5933 | "error-status", "L6RSP", "tout-status", "L6TOUT", |
| 5934 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5935 | 1, curpx, &rs->rules, TCPCHK_RULES_SSL3_CHK, file, line, &errmsg); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5936 | if (!chk) { |
| 5937 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 5938 | goto error; |
| 5939 | } |
| 5940 | chk->index = 1; |
| 5941 | LIST_ADDQ(&rs->rules, &chk->list); |
| 5942 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 5943 | ebis_insert(&shared_tcpchecks, &rs->node); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5944 | |
| 5945 | ruleset_found: |
| 5946 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5947 | rules->flags |= TCPCHK_RULES_SSL3_CHK; |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5948 | |
| 5949 | out: |
| 5950 | free(errmsg); |
| 5951 | return err_code; |
| 5952 | |
| 5953 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5954 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5955 | err_code |= ERR_ALERT | ERR_FATAL; |
| 5956 | goto out; |
| 5957 | } |
| 5958 | |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 5959 | /* Parses the "option smtpchk" proxy keyword */ |
| 5960 | int proxy_parse_smtpchk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 5961 | const char *file, int line) |
| 5962 | { |
| 5963 | static char *smtp_req = "%[var(check.smtp_cmd)]\r\n"; |
| 5964 | |
| 5965 | struct tcpcheck_ruleset *rs = NULL; |
| 5966 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 5967 | struct tcpcheck_rule *chk; |
| 5968 | struct tcpcheck_var *var = NULL; |
| 5969 | char *cmd = NULL, *errmsg = NULL; |
| 5970 | int err_code = 0; |
| 5971 | |
| 5972 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 5973 | err_code |= ERR_WARN; |
| 5974 | |
| 5975 | if (alertif_too_many_args_idx(2, 1, file, line, args, &err_code)) |
| 5976 | goto out; |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 5977 | |
| 5978 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 5979 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 5980 | |
| 5981 | free_tcpcheck_vars(&rules->preset_vars); |
| 5982 | rules->list = NULL; |
| 5983 | rules->flags = 0; |
| 5984 | |
| 5985 | cur_arg += 2; |
| 5986 | if (*args[cur_arg] && *args[cur_arg+1] && |
| 5987 | (strcmp(args[cur_arg], "EHLO") == 0 || strcmp(args[cur_arg], "HELO") == 0)) { |
| 5988 | cmd = calloc(strlen(args[cur_arg]) + strlen(args[cur_arg+1]) + 1, sizeof(*cmd)); |
| 5989 | if (cmd) |
| 5990 | sprintf(cmd, "%s %s", args[cur_arg], args[cur_arg+1]); |
| 5991 | } |
| 5992 | else { |
| 5993 | /* this just hits the default for now, but you could potentially expand it to allow for other stuff |
| 5994 | though, it's unlikely you'd want to send anything other than an EHLO or HELO */ |
| 5995 | cmd = strdup("HELO localhost"); |
| 5996 | } |
| 5997 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 5998 | var = create_tcpcheck_var(ist("check.smtp_cmd")); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 5999 | if (cmd == NULL || var == NULL) { |
| 6000 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6001 | goto error; |
| 6002 | } |
| 6003 | var->data.type = SMP_T_STR; |
| 6004 | var->data.u.str.area = cmd; |
| 6005 | var->data.u.str.data = strlen(cmd); |
| 6006 | LIST_INIT(&var->list); |
| 6007 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6008 | cmd = NULL; |
| 6009 | var = NULL; |
| 6010 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6011 | rs = find_tcpcheck_ruleset("*smtp-check"); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6012 | if (rs) |
| 6013 | goto ruleset_found; |
| 6014 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6015 | rs = create_tcpcheck_ruleset("*smtp-check"); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6016 | if (rs == NULL) { |
| 6017 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6018 | goto error; |
| 6019 | } |
| 6020 | |
| 6021 | chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""}, |
| 6022 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6023 | if (!chk) { |
| 6024 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6025 | goto error; |
| 6026 | } |
| 6027 | chk->index = 0; |
| 6028 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6029 | |
| 6030 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^[0-9]{3}[ \r]", |
| 6031 | "min-recv", "4", |
| 6032 | "error-status", "L7RSP", |
| 6033 | "on-error", "%[check.payload(),cut_crlf]", |
| 6034 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6035 | 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6036 | if (!chk) { |
| 6037 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6038 | goto error; |
| 6039 | } |
| 6040 | chk->index = 1; |
| 6041 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6042 | |
| 6043 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^2[0-9]{2}[ \r]", |
| 6044 | "min-recv", "4", |
| 6045 | "error-status", "L7STS", |
| 6046 | "on-error", "%[check.payload(4,0),ltrim(' '),cut_crlf]", |
| 6047 | "status-code", "check.payload(0,3)", |
| 6048 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6049 | 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6050 | if (!chk) { |
| 6051 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6052 | goto error; |
| 6053 | } |
| 6054 | chk->index = 2; |
| 6055 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6056 | |
| 6057 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send", smtp_req, "log-format", ""}, |
| 6058 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6059 | if (!chk) { |
| 6060 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6061 | goto error; |
| 6062 | } |
| 6063 | chk->index = 3; |
| 6064 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6065 | |
| 6066 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^2[0-9]{2}[- \r]", |
| 6067 | "min-recv", "4", |
| 6068 | "error-status", "L7STS", |
| 6069 | "on-error", "%[check.payload(4,0),ltrim(' '),cut_crlf]", |
| 6070 | "on-success", "%[check.payload(4,0),ltrim(' '),cut_crlf]", |
| 6071 | "status-code", "check.payload(0,3)", |
| 6072 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6073 | 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6074 | if (!chk) { |
| 6075 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6076 | goto error; |
| 6077 | } |
| 6078 | chk->index = 4; |
| 6079 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6080 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 6081 | ebis_insert(&shared_tcpchecks, &rs->node); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6082 | |
| 6083 | ruleset_found: |
| 6084 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6085 | rules->flags |= TCPCHK_RULES_SMTP_CHK; |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6086 | |
| 6087 | out: |
| 6088 | free(errmsg); |
| 6089 | return err_code; |
| 6090 | |
| 6091 | error: |
| 6092 | free(cmd); |
| 6093 | free(var); |
| 6094 | free_tcpcheck_vars(&rules->preset_vars); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6095 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6096 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6097 | goto out; |
| 6098 | } |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6099 | |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6100 | /* Parses the "option pgsql-check" proxy keyword */ |
| 6101 | int proxy_parse_pgsql_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6102 | const char *file, int line) |
| 6103 | { |
| 6104 | static char pgsql_req[] = { |
| 6105 | "%[var(check.plen),htonl,hex]" /* The packet length*/ |
| 6106 | "00030000" /* the version 3.0 */ |
| 6107 | "7573657200" /* "user" key */ |
| 6108 | "%[var(check.username),hex]00" /* the username */ |
| 6109 | "00" |
| 6110 | }; |
| 6111 | |
| 6112 | struct tcpcheck_ruleset *rs = NULL; |
| 6113 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6114 | struct tcpcheck_rule *chk; |
| 6115 | struct tcpcheck_var *var = NULL; |
| 6116 | char *user = NULL, *errmsg = NULL; |
| 6117 | size_t packetlen = 0; |
| 6118 | int err_code = 0; |
| 6119 | |
| 6120 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6121 | err_code |= ERR_WARN; |
| 6122 | |
| 6123 | if (alertif_too_many_args_idx(2, 1, file, line, args, &err_code)) |
| 6124 | goto out; |
| 6125 | |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6126 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6127 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6128 | |
| 6129 | free_tcpcheck_vars(&rules->preset_vars); |
| 6130 | rules->list = NULL; |
| 6131 | rules->flags = 0; |
| 6132 | |
| 6133 | cur_arg += 2; |
| 6134 | if (!*args[cur_arg] || !*args[cur_arg+1]) { |
| 6135 | ha_alert("parsing [%s:%d] : '%s %s' expects 'user <username>' as argument.\n", |
| 6136 | file, line, args[0], args[1]); |
| 6137 | goto error; |
| 6138 | } |
| 6139 | if (strcmp(args[cur_arg], "user") == 0) { |
| 6140 | packetlen = 15 + strlen(args[cur_arg+1]); |
| 6141 | user = strdup(args[cur_arg+1]); |
| 6142 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6143 | var = create_tcpcheck_var(ist("check.username")); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6144 | if (user == NULL || var == NULL) { |
| 6145 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6146 | goto error; |
| 6147 | } |
| 6148 | var->data.type = SMP_T_STR; |
| 6149 | var->data.u.str.area = user; |
| 6150 | var->data.u.str.data = strlen(user); |
| 6151 | LIST_INIT(&var->list); |
| 6152 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6153 | user = NULL; |
| 6154 | var = NULL; |
| 6155 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6156 | var = create_tcpcheck_var(ist("check.plen")); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6157 | if (var == NULL) { |
| 6158 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6159 | goto error; |
| 6160 | } |
| 6161 | var->data.type = SMP_T_SINT; |
| 6162 | var->data.u.sint = packetlen; |
| 6163 | LIST_INIT(&var->list); |
| 6164 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6165 | var = NULL; |
| 6166 | } |
| 6167 | else { |
| 6168 | ha_alert("parsing [%s:%d] : '%s %s' only supports optional values: 'user'.\n", |
| 6169 | file, line, args[0], args[1]); |
| 6170 | goto error; |
| 6171 | } |
| 6172 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6173 | rs = find_tcpcheck_ruleset("*pgsql-check"); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6174 | if (rs) |
| 6175 | goto ruleset_found; |
| 6176 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6177 | rs = create_tcpcheck_ruleset("*pgsql-check"); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6178 | if (rs == NULL) { |
| 6179 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6180 | goto error; |
| 6181 | } |
| 6182 | |
| 6183 | chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""}, |
| 6184 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6185 | if (!chk) { |
| 6186 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6187 | goto error; |
| 6188 | } |
| 6189 | chk->index = 0; |
| 6190 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6191 | |
| 6192 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", pgsql_req, "log-format", ""}, |
| 6193 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6194 | if (!chk) { |
| 6195 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6196 | goto error; |
| 6197 | } |
| 6198 | chk->index = 1; |
| 6199 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6200 | |
| 6201 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "!rstring", "^E", |
| 6202 | "min-recv", "5", |
| 6203 | "error-status", "L7RSP", |
| 6204 | "on-error", "%[check.payload(6,0)]", |
| 6205 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6206 | 1, curpx, &rs->rules, TCPCHK_RULES_PGSQL_CHK, file, line, &errmsg); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6207 | if (!chk) { |
| 6208 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6209 | goto error; |
| 6210 | } |
| 6211 | chk->index = 2; |
| 6212 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6213 | |
| 6214 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^520000000800000000", |
| 6215 | "min-recv", "9", |
| 6216 | "error-status", "L7STS", |
| 6217 | "on-success", "PostgreSQL server is ok", |
| 6218 | "on-error", "PostgreSQL unknown error", |
| 6219 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6220 | 1, curpx, &rs->rules, TCPCHK_RULES_PGSQL_CHK, file, line, &errmsg); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6221 | if (!chk) { |
| 6222 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6223 | goto error; |
| 6224 | } |
| 6225 | chk->index = 3; |
| 6226 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6227 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 6228 | ebis_insert(&shared_tcpchecks, &rs->node); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6229 | |
| 6230 | ruleset_found: |
| 6231 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6232 | rules->flags |= TCPCHK_RULES_PGSQL_CHK; |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6233 | |
| 6234 | out: |
| 6235 | free(errmsg); |
| 6236 | return err_code; |
| 6237 | |
| 6238 | error: |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6239 | free(user); |
| 6240 | free(var); |
| 6241 | free_tcpcheck_vars(&rules->preset_vars); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6242 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6243 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6244 | goto out; |
| 6245 | } |
| 6246 | |
| 6247 | |
| 6248 | /* Parses the "option mysql-check" proxy keyword */ |
| 6249 | int proxy_parse_mysql_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6250 | const char *file, int line) |
| 6251 | { |
| 6252 | /* This is an example of a MySQL >=4.0 client Authentication packet kindly provided by Cyril Bonte. |
| 6253 | * const char mysql40_client_auth_pkt[] = { |
| 6254 | * "\x0e\x00\x00" // packet length |
| 6255 | * "\x01" // packet number |
| 6256 | * "\x00\x00" // client capabilities |
| 6257 | * "\x00\x00\x01" // max packet |
| 6258 | * "haproxy\x00" // username (null terminated string) |
| 6259 | * "\x00" // filler (always 0x00) |
| 6260 | * "\x01\x00\x00" // packet length |
| 6261 | * "\x00" // packet number |
| 6262 | * "\x01" // COM_QUIT command |
| 6263 | * }; |
| 6264 | */ |
| 6265 | static char mysql40_rsname[] = "*mysql40-check"; |
| 6266 | static char mysql40_req[] = { |
| 6267 | "%[var(check.header),hex]" /* 3 bytes for the packet length and 1 byte for the sequence ID */ |
| 6268 | "0080" /* client capabilities */ |
| 6269 | "000001" /* max packet */ |
| 6270 | "%[var(check.username),hex]00" /* the username */ |
| 6271 | "00" /* filler (always 0x00) */ |
| 6272 | "010000" /* packet length*/ |
| 6273 | "00" /* sequence ID */ |
| 6274 | "01" /* COM_QUIT command */ |
| 6275 | }; |
| 6276 | |
| 6277 | /* This is an example of a MySQL >=4.1 client Authentication packet provided by Nenad Merdanovic. |
| 6278 | * const char mysql41_client_auth_pkt[] = { |
| 6279 | * "\x0e\x00\x00\" // packet length |
| 6280 | * "\x01" // packet number |
| 6281 | * "\x00\x00\x00\x00" // client capabilities |
| 6282 | * "\x00\x00\x00\x01" // max packet |
| 6283 | * "\x21" // character set (UTF-8) |
| 6284 | * char[23] // All zeroes |
| 6285 | * "haproxy\x00" // username (null terminated string) |
| 6286 | * "\x00" // filler (always 0x00) |
| 6287 | * "\x01\x00\x00" // packet length |
| 6288 | * "\x00" // packet number |
| 6289 | * "\x01" // COM_QUIT command |
| 6290 | * }; |
| 6291 | */ |
| 6292 | static char mysql41_rsname[] = "*mysql41-check"; |
| 6293 | static char mysql41_req[] = { |
| 6294 | "%[var(check.header),hex]" /* 3 bytes for the packet length and 1 byte for the sequence ID */ |
| 6295 | "00820000" /* client capabilities */ |
| 6296 | "00800001" /* max packet */ |
| 6297 | "21" /* character set (UTF-8) */ |
| 6298 | "000000000000000000000000" /* 23 bytes, al zeroes */ |
| 6299 | "0000000000000000000000" |
| 6300 | "%[var(check.username),hex]00" /* the username */ |
| 6301 | "00" /* filler (always 0x00) */ |
| 6302 | "010000" /* packet length*/ |
| 6303 | "00" /* sequence ID */ |
| 6304 | "01" /* COM_QUIT command */ |
| 6305 | }; |
| 6306 | |
| 6307 | struct tcpcheck_ruleset *rs = NULL; |
| 6308 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6309 | struct tcpcheck_rule *chk; |
| 6310 | struct tcpcheck_var *var = NULL; |
| 6311 | char *mysql_rsname = "*mysql-check"; |
| 6312 | char *mysql_req = NULL, *hdr = NULL, *user = NULL, *errmsg = NULL; |
| 6313 | int index = 0, err_code = 0; |
| 6314 | |
| 6315 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6316 | err_code |= ERR_WARN; |
| 6317 | |
| 6318 | if (alertif_too_many_args_idx(3, 1, file, line, args, &err_code)) |
| 6319 | goto out; |
| 6320 | |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6321 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6322 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6323 | |
| 6324 | free_tcpcheck_vars(&rules->preset_vars); |
| 6325 | rules->list = NULL; |
| 6326 | rules->flags = 0; |
| 6327 | |
| 6328 | cur_arg += 2; |
| 6329 | if (*args[cur_arg]) { |
| 6330 | char *user; |
| 6331 | int packetlen, userlen; |
| 6332 | |
| 6333 | if (strcmp(args[cur_arg], "user") != 0) { |
| 6334 | ha_alert("parsing [%s:%d] : '%s %s' only supports optional values: 'user' (got '%s').\n", |
| 6335 | file, line, args[0], args[1], args[cur_arg]); |
| 6336 | goto error; |
| 6337 | } |
| 6338 | |
| 6339 | if (*(args[cur_arg+1]) == 0) { |
| 6340 | ha_alert("parsing [%s:%d] : '%s %s %s' expects <username> as argument.\n", |
| 6341 | file, line, args[0], args[1], args[cur_arg]); |
| 6342 | goto error; |
| 6343 | } |
| 6344 | |
| 6345 | hdr = calloc(4, sizeof(*hdr)); |
| 6346 | user = strdup(args[cur_arg+1]); |
| 6347 | userlen = strlen(args[cur_arg+1]); |
| 6348 | |
| 6349 | if (hdr == NULL || user == NULL) { |
| 6350 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6351 | goto error; |
| 6352 | } |
| 6353 | |
| 6354 | if (*args[cur_arg+2]) { |
| 6355 | if (strcmp(args[cur_arg+2], "post-41") != 0) { |
| 6356 | ha_alert("parsing [%s:%d] : keyword '%s' only supports option 'post-41' (got '%s').\n", |
| 6357 | file, line, args[cur_arg], args[cur_arg+2]); |
| 6358 | goto error; |
| 6359 | } |
| 6360 | packetlen = userlen + 7 + 27; |
| 6361 | mysql_req = mysql41_req; |
| 6362 | mysql_rsname = mysql41_rsname; |
| 6363 | } |
| 6364 | else { |
| 6365 | packetlen = userlen + 7; |
| 6366 | mysql_req = mysql40_req; |
| 6367 | mysql_rsname = mysql40_rsname; |
| 6368 | } |
| 6369 | |
| 6370 | hdr[0] = (unsigned char)(packetlen & 0xff); |
| 6371 | hdr[1] = (unsigned char)((packetlen >> 8) & 0xff); |
| 6372 | hdr[2] = (unsigned char)((packetlen >> 16) & 0xff); |
| 6373 | hdr[3] = 1; |
| 6374 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6375 | var = create_tcpcheck_var(ist("check.header")); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6376 | if (var == NULL) { |
| 6377 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6378 | goto error; |
| 6379 | } |
| 6380 | var->data.type = SMP_T_STR; |
| 6381 | var->data.u.str.area = hdr; |
| 6382 | var->data.u.str.data = 4; |
| 6383 | LIST_INIT(&var->list); |
| 6384 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6385 | hdr = NULL; |
| 6386 | var = NULL; |
| 6387 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6388 | var = create_tcpcheck_var(ist("check.username")); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6389 | if (var == NULL) { |
| 6390 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6391 | goto error; |
| 6392 | } |
| 6393 | var->data.type = SMP_T_STR; |
| 6394 | var->data.u.str.area = user; |
| 6395 | var->data.u.str.data = strlen(user); |
| 6396 | LIST_INIT(&var->list); |
| 6397 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6398 | user = NULL; |
| 6399 | var = NULL; |
| 6400 | } |
| 6401 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6402 | rs = find_tcpcheck_ruleset(mysql_rsname); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6403 | if (rs) |
| 6404 | goto ruleset_found; |
| 6405 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6406 | rs = create_tcpcheck_ruleset(mysql_rsname); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6407 | if (rs == NULL) { |
| 6408 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6409 | goto error; |
| 6410 | } |
| 6411 | |
| 6412 | chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""}, |
| 6413 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6414 | if (!chk) { |
| 6415 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6416 | goto error; |
| 6417 | } |
| 6418 | chk->index = index++; |
| 6419 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6420 | |
| 6421 | if (mysql_req) { |
| 6422 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", mysql_req, "log-format", ""}, |
| 6423 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6424 | if (!chk) { |
| 6425 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6426 | goto error; |
| 6427 | } |
| 6428 | chk->index = index++; |
| 6429 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6430 | } |
| 6431 | |
| 6432 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6433 | 1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6434 | if (!chk) { |
| 6435 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6436 | goto error; |
| 6437 | } |
| 6438 | chk->expect.custom = tcpcheck_mysql_expect_iniths; |
| 6439 | chk->index = index++; |
| 6440 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6441 | |
| 6442 | if (mysql_req) { |
| 6443 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6444 | 1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6445 | if (!chk) { |
| 6446 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6447 | goto error; |
| 6448 | } |
| 6449 | chk->expect.custom = tcpcheck_mysql_expect_ok; |
| 6450 | chk->index = index++; |
| 6451 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6452 | } |
| 6453 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 6454 | ebis_insert(&shared_tcpchecks, &rs->node); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6455 | |
| 6456 | ruleset_found: |
| 6457 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6458 | rules->flags |= TCPCHK_RULES_MYSQL_CHK; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6459 | |
| 6460 | out: |
| 6461 | free(errmsg); |
| 6462 | return err_code; |
| 6463 | |
| 6464 | error: |
| 6465 | free(hdr); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6466 | free(user); |
| 6467 | free(var); |
| 6468 | free_tcpcheck_vars(&rules->preset_vars); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6469 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6470 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6471 | goto out; |
| 6472 | } |
| 6473 | |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6474 | int proxy_parse_ldap_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6475 | const char *file, int line) |
| 6476 | { |
| 6477 | static char *ldap_req = "300C020101600702010304008000"; |
| 6478 | |
| 6479 | struct tcpcheck_ruleset *rs = NULL; |
| 6480 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6481 | struct tcpcheck_rule *chk; |
| 6482 | char *errmsg = NULL; |
| 6483 | int err_code = 0; |
| 6484 | |
| 6485 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6486 | err_code |= ERR_WARN; |
| 6487 | |
| 6488 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 6489 | goto out; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6490 | |
| 6491 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6492 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6493 | |
| 6494 | free_tcpcheck_vars(&rules->preset_vars); |
| 6495 | rules->list = NULL; |
| 6496 | rules->flags = 0; |
| 6497 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6498 | rs = find_tcpcheck_ruleset("*ldap-check"); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6499 | if (rs) |
| 6500 | goto ruleset_found; |
| 6501 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6502 | rs = create_tcpcheck_ruleset("*ldap-check"); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6503 | if (rs == NULL) { |
| 6504 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6505 | goto error; |
| 6506 | } |
| 6507 | |
| 6508 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", ldap_req, ""}, |
| 6509 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6510 | if (!chk) { |
| 6511 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6512 | goto error; |
| 6513 | } |
| 6514 | chk->index = 0; |
| 6515 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6516 | |
| 6517 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^30", |
| 6518 | "min-recv", "14", |
| 6519 | "on-error", "Not LDAPv3 protocol", |
| 6520 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6521 | 1, curpx, &rs->rules, TCPCHK_RULES_LDAP_CHK, file, line, &errmsg); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6522 | if (!chk) { |
| 6523 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6524 | goto error; |
| 6525 | } |
| 6526 | chk->index = 1; |
| 6527 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6528 | |
| 6529 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6530 | 1, curpx, &rs->rules, TCPCHK_RULES_LDAP_CHK, file, line, &errmsg); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6531 | if (!chk) { |
| 6532 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6533 | goto error; |
| 6534 | } |
| 6535 | chk->expect.custom = tcpcheck_ldap_expect_bindrsp; |
| 6536 | chk->index = 2; |
| 6537 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6538 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 6539 | ebis_insert(&shared_tcpchecks, &rs->node); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6540 | |
| 6541 | ruleset_found: |
| 6542 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6543 | rules->flags |= TCPCHK_RULES_LDAP_CHK; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6544 | |
| 6545 | out: |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6546 | free(errmsg); |
| 6547 | return err_code; |
| 6548 | |
| 6549 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6550 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6551 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6552 | goto out; |
| 6553 | } |
| 6554 | |
| 6555 | int proxy_parse_spop_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6556 | const char *file, int line) |
| 6557 | { |
| 6558 | struct tcpcheck_ruleset *rs = NULL; |
| 6559 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6560 | struct tcpcheck_rule *chk; |
| 6561 | char *spop_req = NULL; |
| 6562 | char *errmsg = NULL; |
| 6563 | int spop_len = 0, err_code = 0; |
| 6564 | |
| 6565 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6566 | err_code |= ERR_WARN; |
| 6567 | |
| 6568 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 6569 | goto out; |
| 6570 | |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6571 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6572 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6573 | |
| 6574 | free_tcpcheck_vars(&rules->preset_vars); |
| 6575 | rules->list = NULL; |
| 6576 | rules->flags = 0; |
| 6577 | |
| 6578 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6579 | rs = find_tcpcheck_ruleset("*spop-check"); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6580 | if (rs) |
| 6581 | goto ruleset_found; |
| 6582 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6583 | rs = create_tcpcheck_ruleset("*spop-check"); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6584 | if (rs == NULL) { |
| 6585 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6586 | goto error; |
| 6587 | } |
| 6588 | |
| 6589 | if (spoe_prepare_healthcheck_request(&spop_req, &spop_len) == -1) { |
| 6590 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6591 | goto error; |
| 6592 | } |
| 6593 | chunk_reset(&trash); |
| 6594 | dump_binary(&trash, spop_req, spop_len); |
| 6595 | trash.area[trash.data] = '\0'; |
| 6596 | |
| 6597 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", b_head(&trash), ""}, |
| 6598 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6599 | if (!chk) { |
| 6600 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6601 | goto error; |
| 6602 | } |
| 6603 | chk->index = 0; |
| 6604 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6605 | |
| 6606 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", "min-recv", "4", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6607 | 1, curpx, &rs->rules, TCPCHK_RULES_SPOP_CHK, file, line, &errmsg); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6608 | if (!chk) { |
| 6609 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6610 | goto error; |
| 6611 | } |
| 6612 | chk->expect.custom = tcpcheck_spop_expect_agenthello; |
| 6613 | chk->index = 1; |
| 6614 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6615 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 6616 | ebis_insert(&shared_tcpchecks, &rs->node); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6617 | |
| 6618 | ruleset_found: |
| 6619 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6620 | rules->flags |= TCPCHK_RULES_SPOP_CHK; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6621 | |
| 6622 | out: |
| 6623 | free(spop_req); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6624 | free(errmsg); |
| 6625 | return err_code; |
| 6626 | |
| 6627 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6628 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6629 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6630 | goto out; |
| 6631 | } |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6632 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6633 | |
| 6634 | struct tcpcheck_rule *proxy_parse_httpchk_req(char **args, int cur_arg, struct proxy *px, char **errmsg) |
| 6635 | { |
| 6636 | struct tcpcheck_rule *chk = NULL; |
| 6637 | struct tcpcheck_http_hdr *hdr = NULL; |
| 6638 | char *meth = NULL, *uri = NULL, *vsn = NULL; |
| 6639 | char *hdrs, *body; |
| 6640 | |
| 6641 | hdrs = (*args[cur_arg+2] ? strstr(args[cur_arg+2], "\r\n") : NULL); |
| 6642 | body = (*args[cur_arg+2] ? strstr(args[cur_arg+2], "\r\n\r\n") : NULL); |
| 6643 | if (hdrs == body) |
| 6644 | hdrs = NULL; |
| 6645 | if (hdrs) { |
| 6646 | *hdrs = '\0'; |
| 6647 | hdrs +=2; |
| 6648 | } |
| 6649 | if (body) { |
| 6650 | *body = '\0'; |
| 6651 | body += 4; |
| 6652 | } |
| 6653 | if (hdrs || body) { |
| 6654 | memprintf(errmsg, "hiding headers or body at the end of the version string is deprecated." |
| 6655 | " Please, consider to use 'http-check send' directive instead."); |
| 6656 | } |
| 6657 | |
| 6658 | chk = calloc(1, sizeof(*chk)); |
| 6659 | if (!chk) { |
| 6660 | memprintf(errmsg, "out of memory"); |
| 6661 | goto error; |
| 6662 | } |
| 6663 | chk->action = TCPCHK_ACT_SEND; |
| 6664 | chk->send.type = TCPCHK_SEND_HTTP; |
| 6665 | chk->send.http.flags |= TCPCHK_SND_HTTP_FROM_OPT; |
| 6666 | chk->send.http.meth.meth = HTTP_METH_OPTIONS; |
| 6667 | LIST_INIT(&chk->send.http.hdrs); |
| 6668 | |
| 6669 | /* Copy the method, uri and version */ |
| 6670 | if (*args[cur_arg]) { |
| 6671 | if (!*args[cur_arg+1]) |
| 6672 | uri = args[cur_arg]; |
| 6673 | else |
| 6674 | meth = args[cur_arg]; |
| 6675 | } |
| 6676 | if (*args[cur_arg+1]) |
| 6677 | uri = args[cur_arg+1]; |
| 6678 | if (*args[cur_arg+2]) |
| 6679 | vsn = args[cur_arg+2]; |
| 6680 | |
| 6681 | if (meth) { |
| 6682 | chk->send.http.meth.meth = find_http_meth(meth, strlen(meth)); |
| 6683 | chk->send.http.meth.str.area = strdup(meth); |
| 6684 | chk->send.http.meth.str.data = strlen(meth); |
| 6685 | if (!chk->send.http.meth.str.area) { |
| 6686 | memprintf(errmsg, "out of memory"); |
| 6687 | goto error; |
| 6688 | } |
| 6689 | } |
| 6690 | if (uri) { |
| 6691 | chk->send.http.uri = ist2(strdup(uri), strlen(uri)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6692 | if (!isttest(chk->send.http.uri)) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6693 | memprintf(errmsg, "out of memory"); |
| 6694 | goto error; |
| 6695 | } |
| 6696 | } |
| 6697 | if (vsn) { |
| 6698 | chk->send.http.vsn = ist2(strdup(vsn), strlen(vsn)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6699 | if (!isttest(chk->send.http.vsn)) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6700 | memprintf(errmsg, "out of memory"); |
| 6701 | goto error; |
| 6702 | } |
| 6703 | } |
| 6704 | |
| 6705 | /* Copy the header */ |
| 6706 | if (hdrs) { |
| 6707 | struct http_hdr tmp_hdrs[global.tune.max_http_hdr]; |
| 6708 | struct h1m h1m; |
| 6709 | int i, ret; |
| 6710 | |
| 6711 | /* Build and parse the request */ |
| 6712 | chunk_printf(&trash, "%s\r\n\r\n", hdrs); |
| 6713 | |
| 6714 | h1m.flags = H1_MF_HDRS_ONLY; |
| 6715 | ret = h1_headers_to_hdr_list(b_orig(&trash), b_tail(&trash), |
| 6716 | tmp_hdrs, sizeof(tmp_hdrs)/sizeof(tmp_hdrs[0]), |
| 6717 | &h1m, NULL); |
| 6718 | if (ret <= 0) { |
| 6719 | memprintf(errmsg, "unable to parse the request '%s'.", b_orig(&trash)); |
| 6720 | goto error; |
| 6721 | } |
| 6722 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6723 | for (i = 0; istlen(tmp_hdrs[i].n); i++) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6724 | hdr = calloc(1, sizeof(*hdr)); |
| 6725 | if (!hdr) { |
| 6726 | memprintf(errmsg, "out of memory"); |
| 6727 | goto error; |
| 6728 | } |
| 6729 | LIST_INIT(&hdr->value); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6730 | hdr->name = istdup(tmp_hdrs[i].n); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6731 | if (!hdr->name.ptr) { |
| 6732 | memprintf(errmsg, "out of memory"); |
| 6733 | goto error; |
| 6734 | } |
| 6735 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6736 | ist0(tmp_hdrs[i].v); |
| 6737 | if (!parse_logformat_string(istptr(tmp_hdrs[i].v), px, &hdr->value, 0, SMP_VAL_BE_CHK_RUL, errmsg)) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6738 | goto error; |
| 6739 | LIST_ADDQ(&chk->send.http.hdrs, &hdr->list); |
| 6740 | } |
| 6741 | } |
| 6742 | |
| 6743 | /* Copy the body */ |
| 6744 | if (body) { |
| 6745 | chk->send.http.body = ist2(strdup(body), strlen(body)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6746 | if (!isttest(chk->send.http.body)) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6747 | memprintf(errmsg, "out of memory"); |
| 6748 | goto error; |
| 6749 | } |
| 6750 | } |
| 6751 | |
| 6752 | return chk; |
| 6753 | |
| 6754 | error: |
| 6755 | free_tcpcheck_http_hdr(hdr); |
| 6756 | free_tcpcheck(chk, 0); |
| 6757 | return NULL; |
| 6758 | } |
| 6759 | |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6760 | int proxy_parse_httpchk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6761 | const char *file, int line) |
| 6762 | { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6763 | struct tcpcheck_ruleset *rs = NULL; |
| 6764 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6765 | struct tcpcheck_rule *chk; |
| 6766 | char *errmsg = NULL; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6767 | int err_code = 0; |
| 6768 | |
| 6769 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6770 | err_code |= ERR_WARN; |
| 6771 | |
| 6772 | if (alertif_too_many_args_idx(3, 1, file, line, args, &err_code)) |
| 6773 | goto out; |
| 6774 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6775 | chk = proxy_parse_httpchk_req(args, cur_arg+2, curpx, &errmsg); |
| 6776 | if (!chk) { |
| 6777 | ha_alert("parsing [%s:%d] : '%s %s' : %s.\n", file, line, args[0], args[1], errmsg); |
| 6778 | goto error; |
| 6779 | } |
| 6780 | if (errmsg) { |
| 6781 | ha_warning("parsing [%s:%d]: '%s %s' : %s\n", file, line, args[0], args[1], errmsg); |
| 6782 | err_code |= ERR_WARN; |
| 6783 | free(errmsg); |
| 6784 | errmsg = NULL; |
| 6785 | } |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6786 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6787 | no_request: |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6788 | curpx->options2 &= ~PR_O2_CHK_ANY; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6789 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6790 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6791 | free_tcpcheck_vars(&rules->preset_vars); |
| 6792 | rules->list = NULL; |
| 6793 | rules->flags |= TCPCHK_SND_HTTP_FROM_OPT; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6794 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6795 | /* Deduce the ruleset name from the proxy info */ |
| 6796 | chunk_printf(&trash, "*http-check-%s_%s-%d", |
| 6797 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 6798 | curpx->conf.file, curpx->conf.line); |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6799 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6800 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6801 | if (rs == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6802 | rs = create_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6803 | if (rs == NULL) { |
| 6804 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6805 | goto error; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6806 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6807 | } |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6808 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6809 | rules->list = &rs->rules; |
| 6810 | rules->flags |= TCPCHK_RULES_HTTP_CHK; |
| 6811 | if (!tcpcheck_add_http_rule(chk, rules, &errmsg)) { |
| 6812 | ha_alert("parsing [%s:%d] : '%s %s' : %s.\n", file, line, args[0], args[1], errmsg); |
| 6813 | rules->list = NULL; |
| 6814 | goto error; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6815 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6816 | |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6817 | out: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6818 | free(errmsg); |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6819 | return err_code; |
| 6820 | |
| 6821 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6822 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6823 | free_tcpcheck(chk, 0); |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6824 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6825 | goto out; |
| 6826 | } |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 6827 | |
Christopher Faulet | 6f55791 | 2020-04-09 15:58:50 +0200 | [diff] [blame] | 6828 | int proxy_parse_external_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6829 | const char *file, int line) |
| 6830 | { |
| 6831 | int err_code = 0; |
| 6832 | |
Christopher Faulet | 6f55791 | 2020-04-09 15:58:50 +0200 | [diff] [blame] | 6833 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6834 | curpx->options2 |= PR_O2_EXT_CHK; |
| 6835 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 6836 | goto out; |
| 6837 | |
| 6838 | out: |
| 6839 | return err_code; |
| 6840 | } |
| 6841 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 6842 | /* Parse the "addr" server keyword */ |
| 6843 | static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 6844 | char **errmsg) |
| 6845 | { |
| 6846 | struct sockaddr_storage *sk; |
| 6847 | struct protocol *proto; |
| 6848 | int port1, port2, err_code = 0; |
| 6849 | |
| 6850 | |
| 6851 | if (!*args[*cur_arg+1]) { |
| 6852 | memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]); |
| 6853 | goto error; |
| 6854 | } |
| 6855 | |
| 6856 | sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, errmsg, NULL, NULL, 1); |
| 6857 | if (!sk) { |
| 6858 | memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg); |
| 6859 | goto error; |
| 6860 | } |
| 6861 | |
| 6862 | proto = protocol_by_family(sk->ss_family); |
| 6863 | if (!proto || !proto->connect) { |
| 6864 | memprintf(errmsg, "'%s %s' : connect() not supported for this address family.", |
| 6865 | args[*cur_arg], args[*cur_arg+1]); |
| 6866 | goto error; |
| 6867 | } |
| 6868 | |
| 6869 | if (port1 != port2) { |
| 6870 | memprintf(errmsg, "'%s' : port ranges and offsets are not allowed in '%s'.", |
| 6871 | args[*cur_arg], args[*cur_arg+1]); |
| 6872 | goto error; |
| 6873 | } |
| 6874 | |
| 6875 | srv->check.addr = srv->agent.addr = *sk; |
| 6876 | srv->flags |= SRV_F_CHECKADDR; |
| 6877 | srv->flags |= SRV_F_AGENTADDR; |
| 6878 | |
| 6879 | out: |
| 6880 | return err_code; |
| 6881 | |
| 6882 | error: |
| 6883 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6884 | goto out; |
| 6885 | } |
| 6886 | |
| 6887 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 6888 | /* Parse the "agent-addr" server keyword */ |
| 6889 | static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 6890 | char **errmsg) |
| 6891 | { |
| 6892 | int err_code = 0; |
| 6893 | |
| 6894 | if (!*(args[*cur_arg+1])) { |
| 6895 | memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]); |
| 6896 | goto error; |
| 6897 | } |
| 6898 | if(str2ip(args[*cur_arg+1], &srv->agent.addr) == NULL) { |
| 6899 | memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]); |
| 6900 | goto error; |
| 6901 | } |
| 6902 | |
| 6903 | out: |
| 6904 | return err_code; |
| 6905 | |
| 6906 | error: |
| 6907 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6908 | goto out; |
| 6909 | } |
| 6910 | |
| 6911 | /* Parse the "agent-check" server keyword */ |
| 6912 | static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 6913 | char **errmsg) |
| 6914 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 6915 | struct tcpcheck_ruleset *rs = NULL; |
| 6916 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
| 6917 | struct tcpcheck_rule *chk; |
| 6918 | int err_code = 0; |
| 6919 | |
| 6920 | if (srv->do_agent) |
| 6921 | goto out; |
| 6922 | |
| 6923 | if (!rules) { |
| 6924 | rules = calloc(1, sizeof(*rules)); |
| 6925 | if (!rules) { |
| 6926 | memprintf(errmsg, "out of memory."); |
| 6927 | goto error; |
| 6928 | } |
| 6929 | LIST_INIT(&rules->preset_vars); |
| 6930 | srv->agent.tcpcheck_rules = rules; |
| 6931 | } |
| 6932 | rules->list = NULL; |
| 6933 | rules->flags = 0; |
| 6934 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6935 | rs = find_tcpcheck_ruleset("*agent-check"); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 6936 | if (rs) |
| 6937 | goto ruleset_found; |
| 6938 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6939 | rs = create_tcpcheck_ruleset("*agent-check"); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 6940 | if (rs == NULL) { |
| 6941 | memprintf(errmsg, "out of memory."); |
| 6942 | goto error; |
| 6943 | } |
| 6944 | |
| 6945 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send", "%[var(check.agent_string)]", "log-format", ""}, |
| 6946 | 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg); |
| 6947 | if (!chk) { |
| 6948 | memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg); |
| 6949 | goto error; |
| 6950 | } |
| 6951 | chk->index = 0; |
| 6952 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6953 | |
| 6954 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6955 | 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK, |
| 6956 | srv->conf.file, srv->conf.line, errmsg); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 6957 | if (!chk) { |
| 6958 | memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg); |
| 6959 | goto error; |
| 6960 | } |
| 6961 | chk->expect.custom = tcpcheck_agent_expect_reply; |
| 6962 | chk->index = 1; |
| 6963 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6964 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 6965 | ebis_insert(&shared_tcpchecks, &rs->node); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 6966 | |
| 6967 | ruleset_found: |
| 6968 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6969 | rules->flags |= TCPCHK_RULES_AGENT_CHK; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 6970 | srv->do_agent = 1; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 6971 | |
| 6972 | out: |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 6973 | return 0; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 6974 | |
| 6975 | error: |
| 6976 | deinit_srv_agent_check(srv); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6977 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 6978 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6979 | goto out; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 6980 | } |
| 6981 | |
| 6982 | /* Parse the "agent-inter" server keyword */ |
| 6983 | static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 6984 | char **errmsg) |
| 6985 | { |
| 6986 | const char *err = NULL; |
| 6987 | unsigned int delay; |
| 6988 | int err_code = 0; |
| 6989 | |
| 6990 | if (!*(args[*cur_arg+1])) { |
| 6991 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 6992 | goto error; |
| 6993 | } |
| 6994 | |
| 6995 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 6996 | if (err == PARSE_TIME_OVER) { |
| 6997 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 6998 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 6999 | goto error; |
| 7000 | } |
| 7001 | else if (err == PARSE_TIME_UNDER) { |
| 7002 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 7003 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7004 | goto error; |
| 7005 | } |
| 7006 | else if (err) { |
| 7007 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 7008 | *err, srv->id); |
| 7009 | goto error; |
| 7010 | } |
| 7011 | if (delay <= 0) { |
| 7012 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 7013 | delay, args[*cur_arg], srv->id); |
| 7014 | goto error; |
| 7015 | } |
| 7016 | srv->agent.inter = delay; |
| 7017 | |
| 7018 | out: |
| 7019 | return err_code; |
| 7020 | |
| 7021 | error: |
| 7022 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7023 | goto out; |
| 7024 | } |
| 7025 | |
| 7026 | /* Parse the "agent-port" server keyword */ |
| 7027 | static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7028 | char **errmsg) |
| 7029 | { |
| 7030 | int err_code = 0; |
| 7031 | |
| 7032 | if (!*(args[*cur_arg+1])) { |
| 7033 | memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]); |
| 7034 | goto error; |
| 7035 | } |
| 7036 | |
| 7037 | global.maxsock++; |
| 7038 | srv->agent.port = atol(args[*cur_arg+1]); |
| 7039 | |
| 7040 | out: |
| 7041 | return err_code; |
| 7042 | |
| 7043 | error: |
| 7044 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7045 | goto out; |
| 7046 | } |
| 7047 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7048 | int set_srv_agent_send(struct server *srv, const char *send) |
| 7049 | { |
| 7050 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
| 7051 | struct tcpcheck_var *var = NULL; |
| 7052 | char *str; |
| 7053 | |
| 7054 | str = strdup(send); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 7055 | var = create_tcpcheck_var(ist("check.agent_string")); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7056 | if (str == NULL || var == NULL) |
| 7057 | goto error; |
| 7058 | |
| 7059 | free_tcpcheck_vars(&rules->preset_vars); |
| 7060 | |
| 7061 | var->data.type = SMP_T_STR; |
| 7062 | var->data.u.str.area = str; |
| 7063 | var->data.u.str.data = strlen(str); |
| 7064 | LIST_INIT(&var->list); |
| 7065 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 7066 | |
| 7067 | return 1; |
| 7068 | |
| 7069 | error: |
| 7070 | free(str); |
| 7071 | free(var); |
| 7072 | return 0; |
| 7073 | } |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7074 | |
| 7075 | /* Parse the "agent-send" server keyword */ |
| 7076 | static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7077 | char **errmsg) |
| 7078 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7079 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7080 | int err_code = 0; |
| 7081 | |
| 7082 | if (!*(args[*cur_arg+1])) { |
| 7083 | memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]); |
| 7084 | goto error; |
| 7085 | } |
| 7086 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7087 | if (!rules) { |
| 7088 | rules = calloc(1, sizeof(*rules)); |
| 7089 | if (!rules) { |
| 7090 | memprintf(errmsg, "out of memory."); |
| 7091 | goto error; |
| 7092 | } |
| 7093 | LIST_INIT(&rules->preset_vars); |
| 7094 | srv->agent.tcpcheck_rules = rules; |
| 7095 | } |
| 7096 | |
| 7097 | if (!set_srv_agent_send(srv, args[*cur_arg+1])) { |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7098 | memprintf(errmsg, "out of memory."); |
| 7099 | goto error; |
| 7100 | } |
| 7101 | |
| 7102 | out: |
| 7103 | return err_code; |
| 7104 | |
| 7105 | error: |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7106 | deinit_srv_agent_check(srv); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7107 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7108 | goto out; |
| 7109 | } |
| 7110 | |
| 7111 | /* Parse the "no-agent-send" server keyword */ |
| 7112 | static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7113 | char **errmsg) |
| 7114 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7115 | deinit_srv_agent_check(srv); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7116 | return 0; |
| 7117 | } |
| 7118 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 7119 | /* Parse the "check" server keyword */ |
| 7120 | static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7121 | char **errmsg) |
| 7122 | { |
| 7123 | srv->do_check = 1; |
| 7124 | return 0; |
| 7125 | } |
| 7126 | |
| 7127 | /* Parse the "check-send-proxy" server keyword */ |
| 7128 | static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7129 | char **errmsg) |
| 7130 | { |
| 7131 | srv->check.send_proxy = 1; |
| 7132 | return 0; |
| 7133 | } |
| 7134 | |
| 7135 | /* Parse the "check-via-socks4" server keyword */ |
| 7136 | static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7137 | char **errmsg) |
| 7138 | { |
| 7139 | srv->check.via_socks4 = 1; |
| 7140 | return 0; |
| 7141 | } |
| 7142 | |
| 7143 | /* Parse the "no-check" server keyword */ |
| 7144 | static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7145 | char **errmsg) |
| 7146 | { |
| 7147 | deinit_srv_check(srv); |
| 7148 | return 0; |
| 7149 | } |
| 7150 | |
| 7151 | /* Parse the "no-check-send-proxy" server keyword */ |
| 7152 | static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7153 | char **errmsg) |
| 7154 | { |
| 7155 | srv->check.send_proxy = 0; |
| 7156 | return 0; |
| 7157 | } |
| 7158 | |
| 7159 | /* Parse the "rise" server keyword */ |
| 7160 | static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7161 | char **errmsg) |
| 7162 | { |
| 7163 | int err_code = 0; |
| 7164 | |
| 7165 | if (!*args[*cur_arg + 1]) { |
| 7166 | memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]); |
| 7167 | goto error; |
| 7168 | } |
| 7169 | |
| 7170 | srv->check.rise = atol(args[*cur_arg+1]); |
| 7171 | if (srv->check.rise <= 0) { |
| 7172 | memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]); |
| 7173 | goto error; |
| 7174 | } |
| 7175 | |
| 7176 | if (srv->check.health) |
| 7177 | srv->check.health = srv->check.rise; |
| 7178 | |
| 7179 | out: |
| 7180 | return err_code; |
| 7181 | |
| 7182 | error: |
| 7183 | deinit_srv_agent_check(srv); |
| 7184 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7185 | goto out; |
| 7186 | return 0; |
| 7187 | } |
| 7188 | |
| 7189 | /* Parse the "fall" server keyword */ |
| 7190 | static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7191 | char **errmsg) |
| 7192 | { |
| 7193 | int err_code = 0; |
| 7194 | |
| 7195 | if (!*args[*cur_arg + 1]) { |
| 7196 | memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]); |
| 7197 | goto error; |
| 7198 | } |
| 7199 | |
| 7200 | srv->check.fall = atol(args[*cur_arg+1]); |
| 7201 | if (srv->check.fall <= 0) { |
| 7202 | memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]); |
| 7203 | goto error; |
| 7204 | } |
| 7205 | |
| 7206 | out: |
| 7207 | return err_code; |
| 7208 | |
| 7209 | error: |
| 7210 | deinit_srv_agent_check(srv); |
| 7211 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7212 | goto out; |
| 7213 | return 0; |
| 7214 | } |
| 7215 | |
| 7216 | /* Parse the "inter" server keyword */ |
| 7217 | static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7218 | char **errmsg) |
| 7219 | { |
| 7220 | const char *err = NULL; |
| 7221 | unsigned int delay; |
| 7222 | int err_code = 0; |
| 7223 | |
| 7224 | if (!*(args[*cur_arg+1])) { |
| 7225 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 7226 | goto error; |
| 7227 | } |
| 7228 | |
| 7229 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 7230 | if (err == PARSE_TIME_OVER) { |
| 7231 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 7232 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7233 | goto error; |
| 7234 | } |
| 7235 | else if (err == PARSE_TIME_UNDER) { |
| 7236 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 7237 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7238 | goto error; |
| 7239 | } |
| 7240 | else if (err) { |
| 7241 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 7242 | *err, srv->id); |
| 7243 | goto error; |
| 7244 | } |
| 7245 | if (delay <= 0) { |
| 7246 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 7247 | delay, args[*cur_arg], srv->id); |
| 7248 | goto error; |
| 7249 | } |
| 7250 | srv->check.inter = delay; |
| 7251 | |
| 7252 | out: |
| 7253 | return err_code; |
| 7254 | |
| 7255 | error: |
| 7256 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7257 | goto out; |
| 7258 | } |
| 7259 | |
| 7260 | |
| 7261 | /* Parse the "fastinter" server keyword */ |
| 7262 | static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7263 | char **errmsg) |
| 7264 | { |
| 7265 | const char *err = NULL; |
| 7266 | unsigned int delay; |
| 7267 | int err_code = 0; |
| 7268 | |
| 7269 | if (!*(args[*cur_arg+1])) { |
| 7270 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 7271 | goto error; |
| 7272 | } |
| 7273 | |
| 7274 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 7275 | if (err == PARSE_TIME_OVER) { |
| 7276 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 7277 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7278 | goto error; |
| 7279 | } |
| 7280 | else if (err == PARSE_TIME_UNDER) { |
| 7281 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 7282 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7283 | goto error; |
| 7284 | } |
| 7285 | else if (err) { |
| 7286 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 7287 | *err, srv->id); |
| 7288 | goto error; |
| 7289 | } |
| 7290 | if (delay <= 0) { |
| 7291 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 7292 | delay, args[*cur_arg], srv->id); |
| 7293 | goto error; |
| 7294 | } |
| 7295 | srv->check.fastinter = delay; |
| 7296 | |
| 7297 | out: |
| 7298 | return err_code; |
| 7299 | |
| 7300 | error: |
| 7301 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7302 | goto out; |
| 7303 | } |
| 7304 | |
| 7305 | |
| 7306 | /* Parse the "downinter" server keyword */ |
| 7307 | static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7308 | char **errmsg) |
| 7309 | { |
| 7310 | const char *err = NULL; |
| 7311 | unsigned int delay; |
| 7312 | int err_code = 0; |
| 7313 | |
| 7314 | if (!*(args[*cur_arg+1])) { |
| 7315 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 7316 | goto error; |
| 7317 | } |
| 7318 | |
| 7319 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 7320 | if (err == PARSE_TIME_OVER) { |
| 7321 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 7322 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7323 | goto error; |
| 7324 | } |
| 7325 | else if (err == PARSE_TIME_UNDER) { |
| 7326 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 7327 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7328 | goto error; |
| 7329 | } |
| 7330 | else if (err) { |
| 7331 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 7332 | *err, srv->id); |
| 7333 | goto error; |
| 7334 | } |
| 7335 | if (delay <= 0) { |
| 7336 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 7337 | delay, args[*cur_arg], srv->id); |
| 7338 | goto error; |
| 7339 | } |
| 7340 | srv->check.downinter = delay; |
| 7341 | |
| 7342 | out: |
| 7343 | return err_code; |
| 7344 | |
| 7345 | error: |
| 7346 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7347 | goto out; |
| 7348 | } |
| 7349 | |
| 7350 | /* Parse the "port" server keyword */ |
| 7351 | static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7352 | char **errmsg) |
| 7353 | { |
| 7354 | int err_code = 0; |
| 7355 | |
| 7356 | if (!*(args[*cur_arg+1])) { |
| 7357 | memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]); |
| 7358 | goto error; |
| 7359 | } |
| 7360 | |
| 7361 | global.maxsock++; |
| 7362 | srv->check.port = atol(args[*cur_arg+1]); |
| 7363 | srv->flags |= SRV_F_CHECKPORT; |
| 7364 | |
| 7365 | out: |
| 7366 | return err_code; |
| 7367 | |
| 7368 | error: |
| 7369 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7370 | goto out; |
| 7371 | } |
| 7372 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 7373 | static struct cfg_kw_list cfg_kws = {ILH, { |
Christopher Faulet | e9111b6 | 2020-04-09 18:12:08 +0200 | [diff] [blame] | 7374 | { CFG_LISTEN, "tcp-check", proxy_parse_tcpcheck }, |
| 7375 | { CFG_LISTEN, "http-check", proxy_parse_httpcheck }, |
| 7376 | { CFG_LISTEN, "external-check", proxy_parse_extcheck }, |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 7377 | { 0, NULL, NULL }, |
| 7378 | }}; |
| 7379 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7380 | static struct srv_kw_list srv_kws = { "CHK", { }, { |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 7381 | { "addr", srv_parse_addr, 1, 1 }, /* IP address to send health to or to probe from agent-check */ |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7382 | { "agent-addr", srv_parse_agent_addr, 1, 1 }, /* Enable an auxiliary agent check */ |
| 7383 | { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable agent checks */ |
| 7384 | { "agent-inter", srv_parse_agent_inter, 1, 1 }, /* Set the interval between two agent checks */ |
| 7385 | { "agent-port", srv_parse_agent_port, 1, 1 }, /* Set the TCP port used for agent checks. */ |
| 7386 | { "agent-send", srv_parse_agent_send, 1, 1 }, /* Set string to send to agent. */ |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 7387 | { "check", srv_parse_check, 0, 1 }, /* Enable health checks */ |
| 7388 | { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* Enable PROXY protocol for health checks */ |
| 7389 | { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* Enable socks4 proxy for health checks */ |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7390 | { "no-agent-check", srv_parse_no_agent_check, 0, 1 }, /* Do not enable any auxiliary agent check */ |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 7391 | { "no-check", srv_parse_no_check, 0, 1 }, /* Disable health checks */ |
| 7392 | { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* Disable PROXY protol for health checks */ |
| 7393 | { "rise", srv_parse_check_rise, 1, 1 }, /* Set rise value for health checks */ |
| 7394 | { "fall", srv_parse_check_fall, 1, 1 }, /* Set fall value for health checks */ |
| 7395 | { "inter", srv_parse_check_inter, 1, 1 }, /* Set inter value for health checks */ |
| 7396 | { "fastinter", srv_parse_check_fastinter, 1, 1 }, /* Set fastinter value for health checks */ |
| 7397 | { "downinter", srv_parse_check_downinter, 1, 1 }, /* Set downinter value for health checks */ |
| 7398 | { "port", srv_parse_check_port, 1, 1 }, /* Set the TCP port used for health checks. */ |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7399 | { NULL, NULL, 0 }, |
| 7400 | }}; |
| 7401 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 7402 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7403 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 7404 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 7405 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7406 | * Local variables: |
| 7407 | * c-indent-level: 8 |
| 7408 | * c-basic-offset: 8 |
| 7409 | * End: |
| 7410 | */ |