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 | |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 33 | #include <haproxy/action.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 34 | #include <haproxy/api.h> |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 35 | #include <common/cfgparse.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 36 | #include <haproxy/chunk.h> |
Willy Tarreau | eb92deb | 2020-06-04 10:53:16 +0200 | [diff] [blame] | 37 | #include <haproxy/dns.h> |
Willy Tarreau | 2741c8c | 2020-06-02 11:28:02 +0200 | [diff] [blame] | 38 | #include <haproxy/istbuf.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 39 | #include <haproxy/list.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 40 | #include <haproxy/regex.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 41 | #include <haproxy/tools.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 42 | #include <haproxy/time.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 43 | #include <haproxy/thread.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 44 | #include <haproxy/http.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 45 | #include <haproxy/http_htx.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 46 | #include <haproxy/h1.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 47 | #include <haproxy/htx.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame^] | 48 | #include <haproxy/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | |
| 50 | #include <types/global.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 51 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | |
Willy Tarreau | aa74c4e | 2020-06-04 10:19:23 +0200 | [diff] [blame] | 53 | #include <haproxy/arg.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/backend.h> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 55 | #include <proto/checks.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 56 | #include <proto/stats.h> |
Willy Tarreau | 0f6ffd6 | 2020-06-03 19:33:00 +0200 | [diff] [blame] | 57 | #include <haproxy/fd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/log.h> |
| 59 | #include <proto/queue.h> |
Willy Tarreau | fc8f6a8 | 2020-06-03 19:20:59 +0200 | [diff] [blame] | 60 | #include <haproxy/port_range.h> |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 61 | #include <proto/proto_tcp.h> |
Willy Tarreau | 2dd7c35 | 2020-06-03 15:26:55 +0200 | [diff] [blame] | 62 | #include <haproxy/protocol.h> |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 63 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | #include <proto/server.h> |
Willy Tarreau | 48d6bf2 | 2016-06-21 16:27:34 +0200 | [diff] [blame] | 65 | #include <proto/signal.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 66 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | #include <proto/task.h> |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 68 | #include <proto/log.h> |
Willy Tarreau | 832ce65 | 2020-06-04 08:36:05 +0200 | [diff] [blame] | 69 | #include <haproxy/proto_udp.h> |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 70 | #include <proto/ssl_sock.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 71 | #include <haproxy/sample.h> |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 72 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 73 | static int tcpcheck_get_step_id(struct check *, struct tcpcheck_rule *); |
Christopher Faulet | 31c30fd | 2020-03-26 21:10:03 +0100 | [diff] [blame] | 74 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 75 | static int wake_srv_chk(struct conn_stream *cs); |
| 76 | struct data_cb check_conn_cb = { |
| 77 | .wake = wake_srv_chk, |
| 78 | .name = "CHCK", |
| 79 | }; |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 80 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 81 | /* Global tree to share all tcp-checks */ |
| 82 | struct eb_root shared_tcpchecks = EB_ROOT; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 83 | |
| 84 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 85 | DECLARE_STATIC_POOL(pool_head_email_alert, "email_alert", sizeof(struct email_alert)); |
| 86 | 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] | 87 | |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 88 | /* Dummy frontend used to create all checks sessions. */ |
| 89 | static struct proxy checks_fe; |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 90 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 91 | /**************************************************************************/ |
| 92 | /************************ Handle check results ****************************/ |
| 93 | /**************************************************************************/ |
| 94 | struct check_status { |
| 95 | short result; /* one of SRV_CHK_* */ |
| 96 | char *info; /* human readable short info */ |
| 97 | char *desc; /* long description */ |
| 98 | }; |
| 99 | |
| 100 | struct analyze_status { |
| 101 | char *desc; /* description */ |
| 102 | unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */ |
| 103 | }; |
| 104 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 105 | static const struct check_status check_statuses[HCHK_STATUS_SIZE] = { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 106 | [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" }, |
| 107 | [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" }, |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 108 | [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 109 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 110 | /* Below we have finished checks */ |
| 111 | [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" }, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 112 | [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" }, |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 113 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 114 | [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 115 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 116 | [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" }, |
| 117 | [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" }, |
| 118 | [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 119 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 120 | [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" }, |
| 121 | [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" }, |
| 122 | [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 123 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 124 | [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" }, |
| 125 | [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 126 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 127 | [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 128 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 129 | [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" }, |
| 130 | [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" }, |
| 131 | [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" }, |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 132 | |
| 133 | [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" }, |
| 134 | [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" }, |
Cyril Bonté | 77010d8 | 2014-08-07 01:55:37 +0200 | [diff] [blame] | 135 | [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 136 | }; |
| 137 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 138 | 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] | 139 | [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }}, |
| 140 | |
| 141 | [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }}, |
| 142 | [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }}, |
| 143 | |
| 144 | [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }}, |
| 145 | [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }}, |
| 146 | [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }}, |
| 147 | [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }}, |
| 148 | |
| 149 | [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }}, |
| 150 | [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }}, |
| 151 | [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }}, |
| 152 | }; |
| 153 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 154 | /* checks if <err> is a real error for errno or one that can be ignored, and |
| 155 | * return 0 for these ones or <err> for real ones. |
| 156 | */ |
| 157 | static inline int unclean_errno(int err) |
| 158 | { |
| 159 | if (err == EAGAIN || err == EINPROGRESS || |
| 160 | err == EISCONN || err == EALREADY) |
| 161 | return 0; |
| 162 | return err; |
| 163 | } |
| 164 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 165 | /* Converts check_status code to description */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 166 | const char *get_check_status_description(short check_status) { |
| 167 | |
| 168 | const char *desc; |
| 169 | |
| 170 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 171 | desc = check_statuses[check_status].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 172 | else |
| 173 | desc = NULL; |
| 174 | |
| 175 | if (desc && *desc) |
| 176 | return desc; |
| 177 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 178 | return check_statuses[HCHK_STATUS_UNKNOWN].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 181 | /* Converts check_status code to short info */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 182 | const char *get_check_status_info(short check_status) { |
| 183 | |
| 184 | const char *info; |
| 185 | |
| 186 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 187 | info = check_statuses[check_status].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 188 | else |
| 189 | info = NULL; |
| 190 | |
| 191 | if (info && *info) |
| 192 | return info; |
| 193 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 194 | return check_statuses[HCHK_STATUS_UNKNOWN].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 197 | /* Convert analyze_status to description */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 198 | const char *get_analyze_status(short analyze_status) { |
| 199 | |
| 200 | const char *desc; |
| 201 | |
| 202 | if (analyze_status < HANA_STATUS_SIZE) |
| 203 | desc = analyze_statuses[analyze_status].desc; |
| 204 | else |
| 205 | desc = NULL; |
| 206 | |
| 207 | if (desc && *desc) |
| 208 | return desc; |
| 209 | else |
| 210 | return analyze_statuses[HANA_STATUS_UNKNOWN].desc; |
| 211 | } |
| 212 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 213 | /* Sets check->status, update check->duration and fill check->result with an |
| 214 | * adequate CHK_RES_* value. The new check->health is computed based on the |
| 215 | * result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 216 | * |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 217 | * Shows information in logs about failed health check if server is UP or |
| 218 | * succeeded health checks if server is DOWN. |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 219 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 220 | 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] | 221 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 222 | struct server *s = check->server; |
Willy Tarreau | bef1b32 | 2014-05-13 21:01:39 +0200 | [diff] [blame] | 223 | short prev_status = check->status; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 224 | int report = 0; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 225 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 226 | if (status == HCHK_STATUS_START) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 227 | check->result = CHK_RES_UNKNOWN; /* no result yet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 228 | check->desc[0] = '\0'; |
| 229 | check->start = now; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 230 | return; |
| 231 | } |
| 232 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 233 | if (!check->status) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 234 | return; |
| 235 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 236 | if (desc && *desc) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 237 | strncpy(check->desc, desc, HCHK_DESC_LEN-1); |
| 238 | check->desc[HCHK_DESC_LEN-1] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 239 | } else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 240 | check->desc[0] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 241 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 242 | check->status = status; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 243 | if (check_statuses[status].result) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 244 | check->result = check_statuses[status].result; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 245 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 246 | if (status == HCHK_STATUS_HANA) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 247 | check->duration = -1; |
| 248 | else if (!tv_iszero(&check->start)) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 249 | /* set_server_check_status() may be called more than once */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 250 | check->duration = tv_ms_elapsed(&check->start, &now); |
| 251 | tv_zero(&check->start); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 254 | /* no change is expected if no state change occurred */ |
| 255 | if (check->result == CHK_RES_NEUTRAL) |
| 256 | return; |
| 257 | |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 258 | /* If the check was really just sending a mail, it won't have an |
| 259 | * associated server, so we're done now. |
| 260 | */ |
| 261 | if (!s) |
| 262 | return; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 263 | report = 0; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 264 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 265 | switch (check->result) { |
| 266 | case CHK_RES_FAILED: |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 267 | /* Failure to connect to the agent as a secondary check should not |
| 268 | * cause the server to be marked down. |
| 269 | */ |
| 270 | if ((!(check->state & CHK_ST_AGENT) || |
Simon Horman | eaabd52 | 2015-02-26 11:26:17 +0900 | [diff] [blame] | 271 | (check->status >= HCHK_STATUS_L57DATA)) && |
Christopher Faulet | b119a79 | 2018-05-02 12:12:45 +0200 | [diff] [blame] | 272 | (check->health > 0)) { |
Olivier Houchard | 7059c55 | 2019-03-08 18:49:32 +0100 | [diff] [blame] | 273 | _HA_ATOMIC_ADD(&s->counters.failed_checks, 1); |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 274 | report = 1; |
| 275 | check->health--; |
| 276 | if (check->health < check->rise) |
| 277 | check->health = 0; |
| 278 | } |
| 279 | break; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 280 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 281 | case CHK_RES_PASSED: |
| 282 | case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */ |
| 283 | if ((check->health < check->rise + check->fall - 1) && |
| 284 | (check->result == CHK_RES_PASSED || check->health > 0)) { |
| 285 | report = 1; |
| 286 | check->health++; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 287 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 288 | if (check->health >= check->rise) |
| 289 | check->health = check->rise + check->fall - 1; /* OK now */ |
| 290 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 291 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 292 | /* clear consecutive_errors if observing is enabled */ |
| 293 | if (s->onerror) |
| 294 | s->consecutive_errors = 0; |
| 295 | break; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 296 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 297 | default: |
| 298 | break; |
| 299 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 300 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 301 | if (s->proxy->options2 & PR_O2_LOGHCHKS && |
| 302 | (status != prev_status || report)) { |
| 303 | chunk_printf(&trash, |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 304 | "%s check for %sserver %s/%s %s%s", |
| 305 | (check->state & CHK_ST_AGENT) ? "Agent" : "Health", |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 306 | s->flags & SRV_F_BACKUP ? "backup " : "", |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 307 | s->proxy->id, s->id, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 308 | (check->result == CHK_RES_CONDPASS) ? "conditionally ":"", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 309 | (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 310 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 311 | srv_append_status(&trash, s, check, -1, 0); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 312 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 313 | chunk_appendf(&trash, ", status: %d/%d %s", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 314 | (check->health >= check->rise) ? check->health - check->rise + 1 : check->health, |
| 315 | (check->health >= check->rise) ? check->fall : check->rise, |
| 316 | (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 317 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 318 | ha_warning("%s.\n", trash.area); |
| 319 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
| 320 | send_email_alert(s, LOG_INFO, "%s", trash.area); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 321 | } |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 324 | /* Marks the check <check>'s server down if the current check is already failed |
| 325 | * and the server is not down yet nor in maintenance. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 326 | */ |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 327 | static void check_notify_failure(struct check *check) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 328 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 329 | struct server *s = check->server; |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 330 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 331 | /* The agent secondary check should only cause a server to be marked |
| 332 | * as down if check->status is HCHK_STATUS_L7STS, which indicates |
| 333 | * that the agent returned "fail", "stopped" or "down". |
| 334 | * The implication here is that failure to connect to the agent |
| 335 | * as a secondary check should not cause the server to be marked |
| 336 | * down. */ |
| 337 | if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS) |
| 338 | return; |
| 339 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 340 | if (check->health > 0) |
| 341 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 342 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 343 | /* 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] | 344 | 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] | 345 | } |
| 346 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 347 | /* 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] | 348 | * it isn't in maintenance, it is not tracking a down server and other checks |
| 349 | * comply. The rule is simple : by default, a server is up, unless any of the |
| 350 | * following conditions is true : |
| 351 | * - health check failed (check->health < rise) |
| 352 | * - agent check failed (agent->health < rise) |
| 353 | * - the server tracks a down server (track && track->state == STOPPED) |
| 354 | * Note that if the server has a slowstart, it will switch to STARTING instead |
| 355 | * of RUNNING. Also, only the health checks support the nolb mode, so the |
| 356 | * agent's success may not take the server out of this mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 357 | */ |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 358 | static void check_notify_success(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 359 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 360 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 361 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 362 | if (s->next_admin & SRV_ADMF_MAINT) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 363 | return; |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 364 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 365 | if (s->track && s->track->next_state == SRV_ST_STOPPED) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 366 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 367 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 368 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 369 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 370 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 371 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 372 | return; |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 373 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 374 | if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 375 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 376 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 377 | 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] | 378 | } |
| 379 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 380 | /* Marks the check <check> as valid and tries to set its server into stopping mode |
| 381 | * if it was running or starting, and provided it isn't in maintenance and other |
| 382 | * checks comply. The conditions for the server to be marked in stopping mode are |
| 383 | * the same as for it to be turned up. Also, only the health checks support the |
| 384 | * nolb mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 385 | */ |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 386 | static void check_notify_stopping(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 387 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 388 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 389 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 390 | if (s->next_admin & SRV_ADMF_MAINT) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 391 | return; |
| 392 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 393 | if (check->state & CHK_ST_AGENT) |
| 394 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 395 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 396 | if (s->track && s->track->next_state == SRV_ST_STOPPED) |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 397 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 398 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 399 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 400 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 401 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 402 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 403 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 404 | |
Willy Tarreau | b26881a | 2017-12-23 11:16:49 +0100 | [diff] [blame] | 405 | 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] | 406 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 407 | |
Willy Tarreau | 9fe7aae | 2013-12-31 23:47:37 +0100 | [diff] [blame] | 408 | /* note: use health_adjust() only, which first checks that the observe mode is |
| 409 | * enabled. |
| 410 | */ |
| 411 | void __health_adjust(struct server *s, short status) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 412 | { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 413 | int failed; |
| 414 | int expire; |
| 415 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 416 | if (s->observe >= HANA_OBS_SIZE) |
| 417 | return; |
| 418 | |
Willy Tarreau | bb95666 | 2013-01-24 00:37:39 +0100 | [diff] [blame] | 419 | if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 420 | return; |
| 421 | |
| 422 | switch (analyze_statuses[status].lr[s->observe - 1]) { |
| 423 | case 1: |
| 424 | failed = 1; |
| 425 | break; |
| 426 | |
| 427 | case 2: |
| 428 | failed = 0; |
| 429 | break; |
| 430 | |
| 431 | default: |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | if (!failed) { |
| 436 | /* good: clear consecutive_errors */ |
| 437 | s->consecutive_errors = 0; |
| 438 | return; |
| 439 | } |
| 440 | |
Olivier Houchard | 7059c55 | 2019-03-08 18:49:32 +0100 | [diff] [blame] | 441 | _HA_ATOMIC_ADD(&s->consecutive_errors, 1); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 442 | |
| 443 | if (s->consecutive_errors < s->consecutive_errors_limit) |
| 444 | return; |
| 445 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 446 | chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s", |
| 447 | s->consecutive_errors, get_analyze_status(status)); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 448 | |
| 449 | switch (s->onerror) { |
| 450 | case HANA_ONERR_FASTINTER: |
| 451 | /* force fastinter - nothing to do here as all modes force it */ |
| 452 | break; |
| 453 | |
| 454 | case HANA_ONERR_SUDDTH: |
| 455 | /* simulate a pre-fatal failed health check */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 456 | if (s->check.health > s->check.rise) |
| 457 | s->check.health = s->check.rise + 1; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 458 | |
| 459 | /* no break - fall through */ |
| 460 | |
| 461 | case HANA_ONERR_FAILCHK: |
| 462 | /* simulate a failed health check */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 463 | set_server_check_status(&s->check, HCHK_STATUS_HANA, |
| 464 | trash.area); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 465 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 466 | break; |
| 467 | |
| 468 | case HANA_ONERR_MARKDWN: |
| 469 | /* mark server down */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 470 | s->check.health = s->check.rise; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 471 | set_server_check_status(&s->check, HCHK_STATUS_HANA, |
| 472 | trash.area); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 473 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 474 | break; |
| 475 | |
| 476 | default: |
| 477 | /* write a warning? */ |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | s->consecutive_errors = 0; |
Olivier Houchard | 7059c55 | 2019-03-08 18:49:32 +0100 | [diff] [blame] | 482 | _HA_ATOMIC_ADD(&s->counters.failed_hana, 1); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 483 | |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 484 | if (s->check.fastinter) { |
| 485 | expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter)); |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 486 | if (s->check.task->expire > expire) { |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 487 | s->check.task->expire = expire; |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 488 | /* requeue check task with new expire */ |
| 489 | task_queue(s->check.task); |
| 490 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 491 | } |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 492 | } |
| 493 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 494 | /* 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] | 495 | * closed, keep errno intact as it is supposed to contain the valid error code. |
| 496 | * If no error is reported, check the socket's error queue using getsockopt(). |
| 497 | * Warning, this must be done only once when returning from poll, and never |
| 498 | * after an I/O error was attempted, otherwise the error queue might contain |
| 499 | * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the |
| 500 | * socket. Returns non-zero if an error was reported, zero if everything is |
| 501 | * clean (including a properly closed socket). |
| 502 | */ |
| 503 | static int retrieve_errno_from_socket(struct connection *conn) |
| 504 | { |
| 505 | int skerr; |
| 506 | socklen_t lskerr = sizeof(skerr); |
| 507 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 508 | if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 509 | return 1; |
| 510 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 511 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 512 | return 0; |
| 513 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 514 | if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 515 | errno = skerr; |
| 516 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 517 | errno = unclean_errno(errno); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 518 | |
| 519 | if (!errno) { |
| 520 | /* we could not retrieve an error, that does not mean there is |
| 521 | * none. Just don't change anything and only report the prior |
| 522 | * error if any. |
| 523 | */ |
| 524 | if (conn->flags & CO_FL_ERROR) |
| 525 | return 1; |
| 526 | else |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH; |
| 531 | return 1; |
| 532 | } |
| 533 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 534 | /* Tries to collect as much information as possible on the connection status, |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 535 | * and adjust the server status accordingly. It may make use of <errno_bck> |
| 536 | * if non-null when the caller is absolutely certain of its validity (eg: |
| 537 | * checked just after a syscall). If the caller doesn't have a valid errno, |
| 538 | * it can pass zero, and retrieve_errno_from_socket() will be called to try |
| 539 | * to extract errno from the socket. If no error is reported, it will consider |
| 540 | * the <expired> flag. This is intended to be used when a connection error was |
| 541 | * reported in conn->flags or when a timeout was reported in <expired>. The |
| 542 | * function takes care of not updating a server status which was already set. |
| 543 | * All situations where at least one of <expired> or CO_FL_ERROR are set |
| 544 | * produce a status. |
| 545 | */ |
Willy Tarreau | b5259bf | 2017-10-04 14:47:29 +0200 | [diff] [blame] | 546 | 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] | 547 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 548 | struct conn_stream *cs = check->cs; |
| 549 | struct connection *conn = cs_conn(cs); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 550 | const char *err_msg; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 551 | struct buffer *chk; |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 552 | int step; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 553 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 554 | if (check->result != CHK_RES_UNKNOWN) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 555 | return; |
| 556 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 557 | errno = unclean_errno(errno_bck); |
| 558 | if (conn && errno) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 559 | retrieve_errno_from_socket(conn); |
| 560 | |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 561 | if (conn && !(conn->flags & CO_FL_ERROR) && |
| 562 | !(cs->flags & CS_FL_ERROR) && !expired) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 563 | return; |
| 564 | |
| 565 | /* we'll try to build a meaningful error message depending on the |
| 566 | * context of the error possibly present in conn->err_code, and the |
| 567 | * socket error possibly collected above. This is useful to know the |
| 568 | * exact step of the L6 layer (eg: SSL handshake). |
| 569 | */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 570 | chk = get_trash_chunk(); |
| 571 | |
Christopher Faulet | 799f3a4 | 2020-04-07 12:06:14 +0200 | [diff] [blame] | 572 | if (check->type == PR_O2_TCPCHK_CHK && |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 573 | (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) { |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 574 | step = tcpcheck_get_step_id(check, NULL); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 575 | if (!step) |
| 576 | chunk_printf(chk, " at initial connection step of tcp-check"); |
| 577 | else { |
| 578 | chunk_printf(chk, " at step %d of tcp-check", step); |
| 579 | /* we were looking for a string */ |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 580 | if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 581 | if (check->current_step->connect.port) |
| 582 | chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 583 | else |
| 584 | chunk_appendf(chk, " (connect)"); |
| 585 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 586 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) { |
| 587 | struct tcpcheck_expect *expect = &check->current_step->expect; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 588 | |
| 589 | switch (expect->type) { |
| 590 | case TCPCHK_EXPECT_STRING: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 591 | 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] | 592 | break; |
| 593 | case TCPCHK_EXPECT_BINARY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 594 | 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] | 595 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 596 | case TCPCHK_EXPECT_STRING_REGEX: |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 597 | chunk_appendf(chk, " (expect regex)"); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 598 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 599 | case TCPCHK_EXPECT_BINARY_REGEX: |
Gaetan Rivet | efab6c6 | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 600 | chunk_appendf(chk, " (expect binary regex)"); |
| 601 | break; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 602 | case TCPCHK_EXPECT_STRING_LF: |
| 603 | chunk_appendf(chk, " (expect log-format string)"); |
| 604 | break; |
| 605 | case TCPCHK_EXPECT_BINARY_LF: |
| 606 | chunk_appendf(chk, " (expect log-format binary)"); |
| 607 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 608 | case TCPCHK_EXPECT_HTTP_STATUS: |
Christopher Faulet | 8021a5f | 2020-04-24 13:53:12 +0200 | [diff] [blame] | 609 | chunk_appendf(chk, " (expect HTTP status codes)"); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 610 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 611 | case TCPCHK_EXPECT_HTTP_STATUS_REGEX: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 612 | chunk_appendf(chk, " (expect HTTP status regex)"); |
| 613 | break; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 614 | case TCPCHK_EXPECT_HTTP_HEADER: |
| 615 | chunk_appendf(chk, " (expect HTTP header pattern)"); |
| 616 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 617 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 618 | 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] | 619 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 620 | case TCPCHK_EXPECT_HTTP_BODY_REGEX: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 621 | chunk_appendf(chk, " (expect HTTP body regex)"); |
| 622 | break; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 623 | case TCPCHK_EXPECT_HTTP_BODY_LF: |
| 624 | chunk_appendf(chk, " (expect log-format HTTP body)"); |
| 625 | break; |
Christopher Faulet | 9e6ed15 | 2020-04-03 15:24:06 +0200 | [diff] [blame] | 626 | case TCPCHK_EXPECT_CUSTOM: |
| 627 | chunk_appendf(chk, " (expect custom function)"); |
| 628 | break; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 629 | case TCPCHK_EXPECT_UNDEF: |
| 630 | chunk_appendf(chk, " (undefined expect!)"); |
| 631 | break; |
| 632 | } |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 633 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 634 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) { |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 635 | chunk_appendf(chk, " (send)"); |
| 636 | } |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 637 | |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 638 | if (check->current_step && check->current_step->comment) |
| 639 | chunk_appendf(chk, " comment: '%s'", check->current_step->comment); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 643 | if (conn && conn->err_code) { |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 644 | if (unclean_errno(errno)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 645 | chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), |
| 646 | chk->area); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 647 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 648 | chunk_printf(&trash, "%s%s", conn_err_code_str(conn), |
| 649 | chk->area); |
| 650 | err_msg = trash.area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 651 | } |
| 652 | else { |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 653 | if (unclean_errno(errno)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 654 | chunk_printf(&trash, "%s%s", strerror(errno), |
| 655 | chk->area); |
| 656 | err_msg = trash.area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 657 | } |
| 658 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 659 | err_msg = chk->area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 663 | if (check->state & CHK_ST_PORT_MISS) { |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 664 | /* NOTE: this is reported after <fall> tries */ |
| 665 | chunk_printf(chk, "No port available for the TCP connection"); |
| 666 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 667 | } |
| 668 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 669 | if (!conn) { |
| 670 | /* connection allocation error before the connection was established */ |
| 671 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 672 | } |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 673 | else if (conn->flags & CO_FL_WAIT_L4_CONN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 674 | /* L4 not established (yet) */ |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 675 | if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 676 | set_server_check_status(check, HCHK_STATUS_L4CON, err_msg); |
| 677 | else if (expired) |
| 678 | set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 679 | |
| 680 | /* |
| 681 | * might be due to a server IP change. |
| 682 | * Let's trigger a DNS resolution if none are currently running. |
| 683 | */ |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 684 | if (check->server) |
| 685 | dns_trigger_resolution(check->server->dns_requester); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 686 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 687 | } |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 688 | else if (conn->flags & CO_FL_WAIT_L6_CONN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 689 | /* L6 not established (yet) */ |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 690 | if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 691 | set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg); |
| 692 | else if (expired) |
| 693 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 694 | } |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 695 | else if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 696 | /* I/O error after connection was established and before we could diagnose */ |
| 697 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 698 | } |
| 699 | else if (expired) { |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 700 | enum healthcheck_status tout = HCHK_STATUS_L7TOUT; |
| 701 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 702 | /* connection established but expired check */ |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 703 | if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT && |
| 704 | check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN) |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 705 | tout = check->current_step->expect.tout_status; |
| 706 | set_server_check_status(check, tout, err_msg); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | return; |
| 710 | } |
| 711 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 712 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 713 | /**************************************************************************/ |
| 714 | /*************** Init/deinit tcp-check rules and ruleset ******************/ |
| 715 | /**************************************************************************/ |
| 716 | /* Releases memory allocated for a log-format string */ |
| 717 | static void free_tcpcheck_fmt(struct list *fmt) |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 718 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 719 | struct logformat_node *lf, *lfb; |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 720 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 721 | list_for_each_entry_safe(lf, lfb, fmt, list) { |
| 722 | LIST_DEL(&lf->list); |
| 723 | release_sample_expr(lf->expr); |
| 724 | free(lf->arg); |
| 725 | free(lf); |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 726 | } |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 729 | /* Releases memory allocated for an HTTP header used in a tcp-check send rule */ |
| 730 | static void free_tcpcheck_http_hdr(struct tcpcheck_http_hdr *hdr) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 731 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 732 | if (!hdr) |
| 733 | return; |
Willy Tarreau | 4fc49a9 | 2019-05-05 06:54:22 +0200 | [diff] [blame] | 734 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 735 | free_tcpcheck_fmt(&hdr->value); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 736 | istfree(&hdr->name); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 737 | free(hdr); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 738 | } |
| 739 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 740 | /* Releases memory allocated for an HTTP header list used in a tcp-check send |
| 741 | * rule |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 742 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 743 | static void free_tcpcheck_http_hdrs(struct list *hdrs) |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 744 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 745 | struct tcpcheck_http_hdr *hdr, *bhdr; |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 746 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 747 | list_for_each_entry_safe(hdr, bhdr, hdrs, list) { |
| 748 | LIST_DEL(&hdr->list); |
| 749 | free_tcpcheck_http_hdr(hdr); |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 750 | } |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 751 | } |
| 752 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 753 | /* Releases memory allocated for a tcp-check. If in_pool is set, it means the |
| 754 | * tcp-check was allocated using a memory pool (it is used to instantiate email |
| 755 | * alerts). |
Christopher Faulet | 95226db | 2020-04-15 11:34:04 +0200 | [diff] [blame] | 756 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 757 | static void free_tcpcheck(struct tcpcheck_rule *rule, int in_pool) |
Christopher Faulet | 95226db | 2020-04-15 11:34:04 +0200 | [diff] [blame] | 758 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 759 | if (!rule) |
| 760 | return; |
Christopher Faulet | 95226db | 2020-04-15 11:34:04 +0200 | [diff] [blame] | 761 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 762 | free(rule->comment); |
| 763 | switch (rule->action) { |
| 764 | case TCPCHK_ACT_SEND: |
| 765 | switch (rule->send.type) { |
| 766 | case TCPCHK_SEND_STRING: |
| 767 | case TCPCHK_SEND_BINARY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 768 | istfree(&rule->send.data); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 769 | break; |
| 770 | case TCPCHK_SEND_STRING_LF: |
| 771 | case TCPCHK_SEND_BINARY_LF: |
| 772 | free_tcpcheck_fmt(&rule->send.fmt); |
| 773 | break; |
| 774 | case TCPCHK_SEND_HTTP: |
| 775 | free(rule->send.http.meth.str.area); |
| 776 | if (!(rule->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 777 | istfree(&rule->send.http.uri); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 778 | else |
| 779 | free_tcpcheck_fmt(&rule->send.http.uri_fmt); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 780 | istfree(&rule->send.http.vsn); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 781 | free_tcpcheck_http_hdrs(&rule->send.http.hdrs); |
| 782 | if (!(rule->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 783 | istfree(&rule->send.http.body); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 784 | else |
| 785 | free_tcpcheck_fmt(&rule->send.http.body_fmt); |
| 786 | break; |
| 787 | case TCPCHK_SEND_UNDEF: |
| 788 | break; |
| 789 | } |
| 790 | break; |
| 791 | case TCPCHK_ACT_EXPECT: |
| 792 | free_tcpcheck_fmt(&rule->expect.onerror_fmt); |
| 793 | free_tcpcheck_fmt(&rule->expect.onsuccess_fmt); |
| 794 | release_sample_expr(rule->expect.status_expr); |
| 795 | switch (rule->expect.type) { |
Christopher Faulet | 8021a5f | 2020-04-24 13:53:12 +0200 | [diff] [blame] | 796 | case TCPCHK_EXPECT_HTTP_STATUS: |
| 797 | free(rule->expect.codes.codes); |
| 798 | break; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 799 | case TCPCHK_EXPECT_STRING: |
| 800 | case TCPCHK_EXPECT_BINARY: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 801 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 802 | istfree(&rule->expect.data); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 803 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 804 | case TCPCHK_EXPECT_STRING_REGEX: |
| 805 | case TCPCHK_EXPECT_BINARY_REGEX: |
| 806 | case TCPCHK_EXPECT_HTTP_STATUS_REGEX: |
| 807 | case TCPCHK_EXPECT_HTTP_BODY_REGEX: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 808 | regex_free(rule->expect.regex); |
| 809 | break; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 810 | case TCPCHK_EXPECT_STRING_LF: |
| 811 | case TCPCHK_EXPECT_BINARY_LF: |
| 812 | case TCPCHK_EXPECT_HTTP_BODY_LF: |
| 813 | free_tcpcheck_fmt(&rule->expect.fmt); |
| 814 | break; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 815 | case TCPCHK_EXPECT_HTTP_HEADER: |
| 816 | if (rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HNAME_REG) |
| 817 | regex_free(rule->expect.hdr.name_re); |
| 818 | else if (rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HNAME_FMT) |
| 819 | free_tcpcheck_fmt(&rule->expect.hdr.name_fmt); |
| 820 | else |
| 821 | istfree(&rule->expect.hdr.name); |
| 822 | |
| 823 | if (rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_REG) |
| 824 | regex_free(rule->expect.hdr.value_re); |
| 825 | else if (rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_FMT) |
| 826 | free_tcpcheck_fmt(&rule->expect.hdr.value_fmt); |
| 827 | else if (!(rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_NONE)) |
| 828 | istfree(&rule->expect.hdr.value); |
| 829 | break; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 830 | case TCPCHK_EXPECT_CUSTOM: |
| 831 | case TCPCHK_EXPECT_UNDEF: |
| 832 | break; |
| 833 | } |
| 834 | break; |
| 835 | case TCPCHK_ACT_CONNECT: |
| 836 | free(rule->connect.sni); |
| 837 | free(rule->connect.alpn); |
| 838 | release_sample_expr(rule->connect.port_expr); |
| 839 | break; |
| 840 | case TCPCHK_ACT_COMMENT: |
| 841 | break; |
| 842 | case TCPCHK_ACT_ACTION_KW: |
| 843 | free(rule->action_kw.rule); |
| 844 | break; |
Christopher Faulet | 95226db | 2020-04-15 11:34:04 +0200 | [diff] [blame] | 845 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 846 | |
| 847 | if (in_pool) |
| 848 | pool_free(pool_head_tcpcheck_rule, rule); |
| 849 | else |
| 850 | free(rule); |
Christopher Faulet | 95226db | 2020-04-15 11:34:04 +0200 | [diff] [blame] | 851 | } |
| 852 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 853 | /* Creates a tcp-check variable used in preset variables before executing a |
| 854 | * tcp-check ruleset. |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 855 | */ |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 856 | static struct tcpcheck_var *create_tcpcheck_var(const struct ist name) |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 857 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 858 | struct tcpcheck_var *var = NULL; |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 859 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 860 | var = calloc(1, sizeof(*var)); |
| 861 | if (var == NULL) |
| 862 | return NULL; |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 863 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 864 | var->name = istdup(name); |
| 865 | if (!isttest(var->name)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 866 | free(var); |
| 867 | return NULL; |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 868 | } |
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_INIT(&var->list); |
| 871 | return var; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 872 | } |
| 873 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 874 | /* Releases memory allocated for a preset tcp-check variable */ |
| 875 | static void free_tcpcheck_var(struct tcpcheck_var *var) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 876 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 877 | if (!var) |
| 878 | return; |
| 879 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 880 | istfree(&var->name); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 881 | if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) |
| 882 | free(var->data.u.str.area); |
| 883 | else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) |
| 884 | free(var->data.u.meth.str.area); |
| 885 | free(var); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 886 | } |
| 887 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 888 | /* Releases a list of preset tcp-check variables */ |
| 889 | static void free_tcpcheck_vars(struct list *vars) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 890 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 891 | struct tcpcheck_var *var, *back; |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 892 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 893 | list_for_each_entry_safe(var, back, vars, list) { |
| 894 | LIST_DEL(&var->list); |
| 895 | free_tcpcheck_var(var); |
| 896 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 897 | } |
| 898 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 899 | /* Duplicate a list of preset tcp-check variables */ |
| 900 | int dup_tcpcheck_vars(struct list *dst, struct list *src) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 901 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 902 | struct tcpcheck_var *var, *new = NULL; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 903 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 904 | list_for_each_entry(var, src, list) { |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 905 | new = create_tcpcheck_var(var->name); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 906 | if (!new) |
| 907 | goto error; |
| 908 | new->data.type = var->data.type; |
| 909 | if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) { |
| 910 | if (chunk_dup(&new->data.u.str, &var->data.u.str) == NULL) |
| 911 | goto error; |
| 912 | if (var->data.type == SMP_T_STR) |
| 913 | new->data.u.str.area[new->data.u.str.data] = 0; |
| 914 | } |
| 915 | else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) { |
| 916 | if (chunk_dup(&new->data.u.str, &var->data.u.str) == NULL) |
| 917 | goto error; |
| 918 | new->data.u.str.area[new->data.u.str.data] = 0; |
| 919 | new->data.u.meth.meth = var->data.u.meth.meth; |
| 920 | } |
| 921 | else |
| 922 | new->data.u = var->data.u; |
| 923 | LIST_ADDQ(dst, &new->list); |
| 924 | } |
| 925 | return 1; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 926 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 927 | error: |
| 928 | free(new); |
| 929 | return 0; |
| 930 | } |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 931 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 932 | /* Looks for a shared tcp-check ruleset given its name. */ |
| 933 | static struct tcpcheck_ruleset *find_tcpcheck_ruleset(const char *name) |
| 934 | { |
| 935 | struct tcpcheck_ruleset *rs; |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 936 | struct ebpt_node *node; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 937 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 938 | node = ebis_lookup_len(&shared_tcpchecks, name, strlen(name)); |
| 939 | if (node) { |
| 940 | rs = container_of(node, typeof(*rs), node); |
| 941 | return rs; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 942 | } |
| 943 | return NULL; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 944 | } |
| 945 | |
Christopher Faulet | 56192cc | 2020-05-29 08:10:50 +0200 | [diff] [blame] | 946 | /* Creates a new shared tcp-check ruleset and insert it in shared_tcpchecks |
| 947 | * tree. |
| 948 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 949 | static struct tcpcheck_ruleset *create_tcpcheck_ruleset(const char *name) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 950 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 951 | struct tcpcheck_ruleset *rs; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 952 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 953 | rs = calloc(1, sizeof(*rs)); |
| 954 | if (rs == NULL) |
| 955 | return NULL; |
| 956 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 957 | rs->node.key = strdup(name); |
| 958 | if (rs->node.key == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 959 | free(rs); |
| 960 | return NULL; |
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 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 963 | LIST_INIT(&rs->rules); |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 964 | ebis_insert(&shared_tcpchecks, &rs->node); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 965 | return rs; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 966 | } |
| 967 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 968 | /* Releases memory allocated by a tcp-check ruleset. */ |
| 969 | static void free_tcpcheck_ruleset(struct tcpcheck_ruleset *rs) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 970 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 971 | struct tcpcheck_rule *r, *rb; |
| 972 | if (!rs) |
| 973 | return; |
Willy Tarreau | 48d6bf2 | 2016-06-21 16:27:34 +0200 | [diff] [blame] | 974 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 975 | ebpt_delete(&rs->node); |
| 976 | free(rs->node.key); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 977 | list_for_each_entry_safe(r, rb, &rs->rules, list) { |
| 978 | LIST_DEL(&r->list); |
| 979 | free_tcpcheck(r, 0); |
| 980 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 981 | free(rs); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 982 | } |
| 983 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 984 | |
| 985 | /**************************************************************************/ |
| 986 | /**************** Everything about tcp-checks execution *******************/ |
| 987 | /**************************************************************************/ |
| 988 | /* Returns the id of a step in a tcp-check ruleset */ |
| 989 | 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] | 990 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 991 | if (!rule) |
| 992 | rule = check->current_step; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 993 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 994 | /* no last started step => first step */ |
| 995 | if (!rule) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 996 | return 1; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 997 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 998 | /* last step is the first implicit connect */ |
| 999 | if (rule->index == 0 && |
| 1000 | rule->action == TCPCHK_ACT_CONNECT && |
| 1001 | (rule->connect.options & TCPCHK_OPT_IMPLICIT)) |
| 1002 | return 0; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1003 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1004 | return rule->index + 1; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1005 | } |
| 1006 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1007 | /* Returns the first non COMMENT/ACTION_KW tcp-check rule from list <list> or |
| 1008 | * NULL if none was found. |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1009 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1010 | static struct tcpcheck_rule *get_first_tcpcheck_rule(struct tcpcheck_rules *rules) |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1011 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1012 | struct tcpcheck_rule *r; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1013 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1014 | list_for_each_entry(r, rules->list, list) { |
| 1015 | if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW) |
| 1016 | return r; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1017 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1018 | return NULL; |
| 1019 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1020 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1021 | /* Returns the last non COMMENT/ACTION_KW tcp-check rule from list <list> or |
| 1022 | * NULL if none was found. |
| 1023 | */ |
| 1024 | static struct tcpcheck_rule *get_last_tcpcheck_rule(struct tcpcheck_rules *rules) |
| 1025 | { |
| 1026 | struct tcpcheck_rule *r; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1027 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1028 | list_for_each_entry_rev(r, rules->list, list) { |
| 1029 | if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW) |
| 1030 | return r; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1031 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1032 | return NULL; |
| 1033 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1034 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1035 | /* Returns the non COMMENT/ACTION_KW tcp-check rule from list <list> following |
| 1036 | * <start> or NULL if non was found. If <start> is NULL, it relies on |
| 1037 | * get_first_tcpcheck_rule(). |
| 1038 | */ |
| 1039 | static struct tcpcheck_rule *get_next_tcpcheck_rule(struct tcpcheck_rules *rules, struct tcpcheck_rule *start) |
| 1040 | { |
| 1041 | struct tcpcheck_rule *r; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1042 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1043 | if (!start) |
| 1044 | return get_first_tcpcheck_rule(rules); |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1045 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1046 | r = LIST_NEXT(&start->list, typeof(r), list); |
| 1047 | list_for_each_entry_from(r, rules->list, list) { |
| 1048 | if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW) |
| 1049 | return r; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1050 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1051 | return NULL; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1052 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1053 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1054 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1055 | /* Creates info message when a tcp-check healthcheck fails on an expect rule */ |
| 1056 | static void tcpcheck_expect_onerror_message(struct buffer *msg, struct check *check, struct tcpcheck_rule *rule, |
| 1057 | int match, struct ist info) |
| 1058 | { |
| 1059 | struct sample *smp; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1060 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1061 | /* Follows these step to produce the info message: |
| 1062 | * 1. if info field is already provided, copy it |
| 1063 | * 2. if the expect rule provides an onerror log-format string, |
| 1064 | * use it to produce the message |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1065 | * 3. the expect rule is part of a protocol check (http, redis, mysql...), do nothing |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1066 | * 4. Otherwise produce the generic tcp-check info message |
| 1067 | */ |
| 1068 | if (istlen(info)) { |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1069 | chunk_strncat(msg, istptr(info), istlen(info)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1070 | goto comment; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1071 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1072 | else if (!LIST_ISEMPTY(&rule->expect.onerror_fmt)) { |
| 1073 | msg->data += sess_build_logline(check->sess, NULL, b_tail(msg), b_room(msg), &rule->expect.onerror_fmt); |
| 1074 | goto comment; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1075 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1076 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1077 | if (check->type == PR_O2_TCPCHK_CHK && |
| 1078 | (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) != TCPCHK_RULES_TCP_CHK) |
| 1079 | goto comment; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1080 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1081 | chunk_strcat(msg, (match ? "TCPCHK matched unwanted content" : "TCPCHK did not match content")); |
| 1082 | switch (rule->expect.type) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1083 | case TCPCHK_EXPECT_HTTP_STATUS: |
Christopher Faulet | 8021a5f | 2020-04-24 13:53:12 +0200 | [diff] [blame] | 1084 | chunk_appendf(msg, "(status codes) at step %d", tcpcheck_get_step_id(check, rule)); |
| 1085 | break; |
| 1086 | case TCPCHK_EXPECT_STRING: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1087 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1088 | 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] | 1089 | tcpcheck_get_step_id(check, rule)); |
| 1090 | break; |
| 1091 | case TCPCHK_EXPECT_BINARY: |
| 1092 | chunk_appendf(msg, " (binary) at step %d", tcpcheck_get_step_id(check, rule)); |
| 1093 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 1094 | case TCPCHK_EXPECT_STRING_REGEX: |
| 1095 | case TCPCHK_EXPECT_HTTP_STATUS_REGEX: |
| 1096 | case TCPCHK_EXPECT_HTTP_BODY_REGEX: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1097 | chunk_appendf(msg, " (regex) at step %d", tcpcheck_get_step_id(check, rule)); |
| 1098 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 1099 | case TCPCHK_EXPECT_BINARY_REGEX: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1100 | chunk_appendf(msg, " (binary regex) at step %d", tcpcheck_get_step_id(check, rule)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1101 | break; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 1102 | case TCPCHK_EXPECT_STRING_LF: |
| 1103 | case TCPCHK_EXPECT_HTTP_BODY_LF: |
| 1104 | chunk_appendf(msg, " (log-format string) at step %d", tcpcheck_get_step_id(check, rule)); |
| 1105 | break; |
| 1106 | case TCPCHK_EXPECT_BINARY_LF: |
| 1107 | chunk_appendf(msg, " (log-format binary) at step %d", tcpcheck_get_step_id(check, rule)); |
| 1108 | break; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1109 | case TCPCHK_EXPECT_CUSTOM: |
| 1110 | chunk_appendf(msg, " (custom function) at step %d", tcpcheck_get_step_id(check, rule)); |
| 1111 | break; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 1112 | case TCPCHK_EXPECT_HTTP_HEADER: |
| 1113 | chunk_appendf(msg, " (header pattern) at step %d", tcpcheck_get_step_id(check, rule)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1114 | case TCPCHK_EXPECT_UNDEF: |
| 1115 | /* Should never happen. */ |
| 1116 | return; |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1117 | } |
| 1118 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1119 | comment: |
| 1120 | /* If the failing expect rule provides a comment, it is concatenated to |
| 1121 | * the info message. |
| 1122 | */ |
| 1123 | if (rule->comment) { |
| 1124 | chunk_strcat(msg, " comment: "); |
Christopher Faulet | 88d939c | 2020-04-22 15:32:11 +0200 | [diff] [blame] | 1125 | chunk_strcat(msg, rule->comment); |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1126 | } |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 1127 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1128 | /* Finally, the check status code is set if the failing expect rule |
| 1129 | * defines a status expression. |
| 1130 | */ |
| 1131 | if (rule->expect.status_expr) { |
| 1132 | smp = sample_fetch_as_type(check->proxy, check->sess, NULL, SMP_OPT_DIR_RES | SMP_OPT_FINAL, |
Christopher Faulet | 78f371e | 2020-04-30 09:38:08 +0200 | [diff] [blame] | 1133 | rule->expect.status_expr, SMP_T_STR); |
| 1134 | |
| 1135 | if (smp && sample_casts[smp->data.type][SMP_T_SINT] && |
| 1136 | sample_casts[smp->data.type][SMP_T_SINT](smp)) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1137 | check->code = smp->data.u.sint; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1138 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1139 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1140 | *(b_tail(msg)) = '\0'; |
| 1141 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1142 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1143 | /* Creates info message when a tcp-check healthcheck succeeds on an expect rule */ |
| 1144 | static void tcpcheck_expect_onsuccess_message(struct buffer *msg, struct check *check, struct tcpcheck_rule *rule, |
| 1145 | struct ist info) |
| 1146 | { |
| 1147 | struct sample *smp; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1148 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1149 | /* Follows these step to produce the info message: |
| 1150 | * 1. if info field is already provided, copy it |
| 1151 | * 2. if the expect rule provides an onsucces log-format string, |
| 1152 | * use it to produce the message |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1153 | * 3. the expect rule is part of a protocol check (http, redis, mysql...), do nothing |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1154 | * 4. Otherwise produce the generic tcp-check info message |
| 1155 | */ |
| 1156 | if (istlen(info)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1157 | chunk_strncat(msg, istptr(info), istlen(info)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1158 | if (!LIST_ISEMPTY(&rule->expect.onsuccess_fmt)) |
| 1159 | msg->data += sess_build_logline(check->sess, NULL, b_tail(msg), b_room(msg), |
| 1160 | &rule->expect.onsuccess_fmt); |
| 1161 | else if (check->type == PR_O2_TCPCHK_CHK && |
| 1162 | (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) |
| 1163 | chunk_strcat(msg, "(tcp-check)"); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1164 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1165 | /* Finally, the check status code is set if the expect rule defines a |
| 1166 | * status expression. |
| 1167 | */ |
| 1168 | if (rule->expect.status_expr) { |
| 1169 | smp = sample_fetch_as_type(check->proxy, check->sess, NULL, SMP_OPT_DIR_RES | SMP_OPT_FINAL, |
Christopher Faulet | 78f371e | 2020-04-30 09:38:08 +0200 | [diff] [blame] | 1170 | rule->expect.status_expr, SMP_T_STR); |
| 1171 | |
| 1172 | if (smp && sample_casts[smp->data.type][SMP_T_SINT] && |
| 1173 | sample_casts[smp->data.type][SMP_T_SINT](smp)) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1174 | check->code = smp->data.u.sint; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1175 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1176 | |
| 1177 | *(b_tail(msg)) = '\0'; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1178 | } |
| 1179 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1180 | /* Builds the server state header used by HTTP health-checks */ |
| 1181 | static int httpchk_build_status_header(struct server *s, struct buffer *buf) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1182 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1183 | int sv_state; |
| 1184 | int ratio; |
| 1185 | char addr[46]; |
| 1186 | char port[6]; |
| 1187 | const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d", |
| 1188 | "UP %d/%d", "UP", |
| 1189 | "NOLB %d/%d", "NOLB", |
| 1190 | "no check" }; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1191 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1192 | if (!(s->check.state & CHK_ST_ENABLED)) |
| 1193 | sv_state = 6; |
| 1194 | else if (s->cur_state != SRV_ST_STOPPED) { |
| 1195 | if (s->check.health == s->check.rise + s->check.fall - 1) |
| 1196 | sv_state = 3; /* UP */ |
| 1197 | else |
| 1198 | sv_state = 2; /* going down */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1199 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1200 | if (s->cur_state == SRV_ST_STOPPING) |
| 1201 | sv_state += 2; |
| 1202 | } else { |
| 1203 | if (s->check.health) |
| 1204 | sv_state = 1; /* going up */ |
| 1205 | else |
| 1206 | sv_state = 0; /* DOWN */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1207 | } |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 1208 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1209 | chunk_appendf(buf, srv_hlt_st[sv_state], |
| 1210 | (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health), |
| 1211 | (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise)); |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 1212 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1213 | addr_to_str(&s->addr, addr, sizeof(addr)); |
| 1214 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
| 1215 | snprintf(port, sizeof(port), "%u", s->svc_port); |
| 1216 | else |
| 1217 | *port = 0; |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 1218 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1219 | chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", |
| 1220 | addr, port, s->proxy->id, s->id, |
| 1221 | global.node, |
| 1222 | (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 1223 | (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 1224 | s->cur_sess, s->proxy->beconn - s->proxy->nbpend, |
| 1225 | s->nbpend); |
Willy Tarreau | 9f6dc72 | 2019-03-01 11:15:10 +0100 | [diff] [blame] | 1226 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1227 | if ((s->cur_state == SRV_ST_STARTING) && |
| 1228 | now.tv_sec < s->last_change + s->slowstart && |
| 1229 | now.tv_sec >= s->last_change) { |
| 1230 | ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); |
| 1231 | chunk_appendf(buf, "; throttle=%d%%", ratio); |
| 1232 | } |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1233 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1234 | return b_data(buf); |
| 1235 | } |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1236 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1237 | /* Internal functions to parse and validate a MySQL packet in the context of an |
| 1238 | * expect rule. It start to parse the input buffer at the offset <offset>. If |
| 1239 | * <last_read> is set, no more data are expected. |
| 1240 | */ |
| 1241 | static enum tcpcheck_eval_ret tcpcheck_mysql_expect_packet(struct check *check, struct tcpcheck_rule *rule, |
| 1242 | unsigned int offset, int last_read) |
| 1243 | { |
| 1244 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 1245 | enum healthcheck_status status; |
| 1246 | struct buffer *msg = NULL; |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1247 | struct ist desc = IST_NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1248 | unsigned int err = 0, plen = 0; |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1249 | |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1250 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1251 | /* 3 Bytes for the packet length and 1 byte for the sequence id */ |
Christopher Faulet | 733dd73 | 2020-04-28 10:24:23 +0200 | [diff] [blame] | 1252 | if (b_data(&check->bi) < offset+4) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1253 | if (!last_read) |
| 1254 | goto wait_more_data; |
| 1255 | |
| 1256 | /* invalid length or truncated response */ |
| 1257 | status = HCHK_STATUS_L7RSP; |
| 1258 | goto error; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1259 | } |
| 1260 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1261 | plen = ((unsigned char) *b_peek(&check->bi, offset)) + |
| 1262 | (((unsigned char) *(b_peek(&check->bi, offset+1))) << 8) + |
| 1263 | (((unsigned char) *(b_peek(&check->bi, offset+2))) << 16); |
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 | if (b_data(&check->bi) < offset+plen+4) { |
| 1266 | if (!last_read) |
| 1267 | goto wait_more_data; |
| 1268 | |
| 1269 | /* invalid length or truncated response */ |
| 1270 | status = HCHK_STATUS_L7RSP; |
| 1271 | goto error; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1272 | } |
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 | if (*b_peek(&check->bi, offset+4) == '\xff') { |
| 1275 | /* MySQL Error packet always begin with field_count = 0xff */ |
| 1276 | status = HCHK_STATUS_L7STS; |
| 1277 | err = ((unsigned char) *b_peek(&check->bi, offset+5)) + |
| 1278 | (((unsigned char) *(b_peek(&check->bi, offset+6))) << 8); |
| 1279 | desc = ist2(b_peek(&check->bi, offset+7), b_data(&check->bi) - offset - 7); |
| 1280 | goto error; |
| 1281 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1282 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1283 | if (get_next_tcpcheck_rule(check->tcpcheck_rules, rule) != NULL) { |
| 1284 | /* Not the last rule, continue */ |
| 1285 | goto out; |
| 1286 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1287 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1288 | /* We set the MySQL Version in description for information purpose |
| 1289 | * FIXME : it can be cool to use MySQL Version for other purpose, |
| 1290 | * like mark as down old MySQL server. |
| 1291 | */ |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 1292 | status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD); |
| 1293 | set_server_check_status(check, status, b_peek(&check->bi, 5)); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1294 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1295 | out: |
| 1296 | free_trash_chunk(msg); |
| 1297 | return ret; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1298 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1299 | error: |
| 1300 | ret = TCPCHK_EVAL_STOP; |
| 1301 | check->code = err; |
| 1302 | msg = alloc_trash_chunk(); |
| 1303 | if (msg) |
| 1304 | tcpcheck_expect_onerror_message(msg, check, rule, 0, desc); |
| 1305 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
| 1306 | goto out; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1307 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1308 | wait_more_data: |
| 1309 | ret = TCPCHK_EVAL_WAIT; |
| 1310 | goto out; |
| 1311 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1312 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1313 | /* Custom tcp-check expect function to parse and validate the MySQL initial |
| 1314 | * handshake packet. Returns TCPCHK_EVAL_WAIT to wait for more data, |
| 1315 | * TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if an |
| 1316 | * error occurred. |
| 1317 | */ |
| 1318 | static enum tcpcheck_eval_ret tcpcheck_mysql_expect_iniths(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 1319 | { |
| 1320 | return tcpcheck_mysql_expect_packet(check, rule, 0, last_read); |
| 1321 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1322 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1323 | /* Custom tcp-check expect function to parse and validate the MySQL OK packet |
| 1324 | * following the initial handshake. Returns TCPCHK_EVAL_WAIT to wait for more |
| 1325 | * data, TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if |
| 1326 | * an error occurred. |
| 1327 | */ |
| 1328 | static enum tcpcheck_eval_ret tcpcheck_mysql_expect_ok(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 1329 | { |
| 1330 | unsigned int hslen = 0; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1331 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1332 | hslen = 4 + ((unsigned char) *b_head(&check->bi)) + |
| 1333 | (((unsigned char) *(b_peek(&check->bi, 1))) << 8) + |
| 1334 | (((unsigned char) *(b_peek(&check->bi, 2))) << 16); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1335 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1336 | return tcpcheck_mysql_expect_packet(check, rule, hslen, last_read); |
| 1337 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1338 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1339 | /* Custom tcp-check expect function to parse and validate the LDAP bind response |
| 1340 | * package packet. Returns TCPCHK_EVAL_WAIT to wait for more data, |
| 1341 | * TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if an |
| 1342 | * error occurred. |
| 1343 | */ |
| 1344 | static enum tcpcheck_eval_ret tcpcheck_ldap_expect_bindrsp(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 1345 | { |
| 1346 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 1347 | enum healthcheck_status status; |
| 1348 | struct buffer *msg = NULL; |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1349 | struct ist desc = IST_NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1350 | unsigned short msglen = 0; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1351 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1352 | /* Check if the server speaks LDAP (ASN.1/BER) |
| 1353 | * http://en.wikipedia.org/wiki/Basic_Encoding_Rules |
| 1354 | * http://tools.ietf.org/html/rfc4511 |
| 1355 | */ |
| 1356 | /* size of LDAPMessage */ |
| 1357 | 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] | 1358 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1359 | /* http://tools.ietf.org/html/rfc4511#section-4.2.2 |
| 1360 | * messageID: 0x02 0x01 0x01: INTEGER 1 |
| 1361 | * protocolOp: 0x61: bindResponse |
| 1362 | */ |
| 1363 | if ((msglen > 2) || (memcmp(b_head(&check->bi) + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) { |
| 1364 | status = HCHK_STATUS_L7RSP; |
| 1365 | desc = ist("Not LDAPv3 protocol"); |
| 1366 | goto error; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1367 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1368 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1369 | /* size of bindResponse */ |
| 1370 | 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] | 1371 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1372 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1373 | * ldapResult: 0x0a 0x01: ENUMERATION |
| 1374 | */ |
| 1375 | if ((msglen > 4) || (memcmp(b_head(&check->bi) + 7 + msglen, "\x0a\x01", 2) != 0)) { |
| 1376 | status = HCHK_STATUS_L7RSP; |
| 1377 | desc = ist("Not LDAPv3 protocol"); |
| 1378 | goto error; |
| 1379 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1380 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1381 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1382 | * resultCode |
| 1383 | */ |
| 1384 | check->code = *(b_head(&check->bi) + msglen + 9); |
| 1385 | if (check->code) { |
| 1386 | status = HCHK_STATUS_L7STS; |
| 1387 | desc = ist("See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9"); |
| 1388 | goto error; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1389 | } |
| 1390 | |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 1391 | status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD); |
| 1392 | set_server_check_status(check, status, "Success"); |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 1393 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1394 | out: |
| 1395 | free_trash_chunk(msg); |
| 1396 | return ret; |
| 1397 | |
| 1398 | error: |
| 1399 | ret = TCPCHK_EVAL_STOP; |
| 1400 | msg = alloc_trash_chunk(); |
| 1401 | if (msg) |
| 1402 | tcpcheck_expect_onerror_message(msg, check, rule, 0, desc); |
| 1403 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
| 1404 | goto out; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1405 | } |
| 1406 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1407 | /* Custom tcp-check expect function to parse and validate the SPOP hello agent |
| 1408 | * frame. Returns TCPCHK_EVAL_WAIT to wait for more data, TCPCHK_EVAL_CONTINUE |
| 1409 | * 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] | 1410 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1411 | 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] | 1412 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1413 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 1414 | enum healthcheck_status status; |
| 1415 | struct buffer *msg = NULL; |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1416 | struct ist desc = IST_NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1417 | unsigned int framesz; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1418 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1419 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1420 | memcpy(&framesz, b_head(&check->bi), 4); |
| 1421 | framesz = ntohl(framesz); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1422 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1423 | if (!last_read && b_data(&check->bi) < (4+framesz)) |
| 1424 | goto wait_more_data; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1425 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1426 | memset(b_orig(&trash), 0, b_size(&trash)); |
| 1427 | if (spoe_handle_healthcheck_response(b_peek(&check->bi, 4), framesz, b_orig(&trash), HCHK_DESC_LEN) == -1) { |
| 1428 | status = HCHK_STATUS_L7RSP; |
| 1429 | desc = ist2(b_orig(&trash), strlen(b_orig(&trash))); |
| 1430 | goto error; |
| 1431 | } |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1432 | |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 1433 | status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD); |
| 1434 | set_server_check_status(check, status, "SPOA server is ok"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1435 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1436 | out: |
| 1437 | free_trash_chunk(msg); |
| 1438 | return ret; |
Christopher Faulet | a32a250 | 2020-04-20 09:04:37 +0200 | [diff] [blame] | 1439 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1440 | error: |
| 1441 | ret = TCPCHK_EVAL_STOP; |
| 1442 | msg = alloc_trash_chunk(); |
| 1443 | if (msg) |
| 1444 | tcpcheck_expect_onerror_message(msg, check, rule, 0, desc); |
| 1445 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
| 1446 | goto out; |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 1447 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1448 | wait_more_data: |
| 1449 | ret = TCPCHK_EVAL_WAIT; |
| 1450 | goto out; |
| 1451 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1452 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1453 | /* Custom tcp-check expect function to parse and validate the agent-check |
| 1454 | * reply. Returns TCPCHK_EVAL_WAIT to wait for more data, TCPCHK_EVAL_CONTINUE |
| 1455 | * to evaluate the next rule or TCPCHK_EVAL_STOP if an error occurred. |
| 1456 | */ |
| 1457 | static enum tcpcheck_eval_ret tcpcheck_agent_expect_reply(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 1458 | { |
| 1459 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_STOP; |
| 1460 | enum healthcheck_status status = HCHK_STATUS_CHECKED; |
| 1461 | const char *hs = NULL; /* health status */ |
| 1462 | const char *as = NULL; /* admin status */ |
| 1463 | const char *ps = NULL; /* performance status */ |
| 1464 | const char *cs = NULL; /* maxconn */ |
| 1465 | const char *err = NULL; /* first error to report */ |
| 1466 | const char *wrn = NULL; /* first warning to report */ |
| 1467 | char *cmd, *p; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1468 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1469 | /* We're getting an agent check response. The agent could |
| 1470 | * have been disabled in the mean time with a long check |
| 1471 | * still pending. It is important that we ignore the whole |
| 1472 | * response. |
| 1473 | */ |
| 1474 | if (!(check->state & CHK_ST_ENABLED)) |
| 1475 | goto out; |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1476 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1477 | /* The agent supports strings made of a single line ended by the |
| 1478 | * first CR ('\r') or LF ('\n'). This line is composed of words |
| 1479 | * delimited by spaces (' '), tabs ('\t'), or commas (','). The |
| 1480 | * line may optionally contained a description of a state change |
| 1481 | * after a sharp ('#'), which is only considered if a health state |
| 1482 | * is announced. |
| 1483 | * |
| 1484 | * Words may be composed of : |
| 1485 | * - a numeric weight suffixed by the percent character ('%'). |
| 1486 | * - a health status among "up", "down", "stopped", and "fail". |
| 1487 | * - an admin status among "ready", "drain", "maint". |
| 1488 | * |
| 1489 | * These words may appear in any order. If multiple words of the |
| 1490 | * same category appear, the last one wins. |
| 1491 | */ |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 1492 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1493 | p = b_head(&check->bi); |
| 1494 | while (*p && *p != '\n' && *p != '\r') |
| 1495 | p++; |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 1496 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1497 | if (!*p) { |
| 1498 | if (!last_read) |
| 1499 | goto wait_more_data; |
| 1500 | |
| 1501 | /* at least inform the admin that the agent is mis-behaving */ |
| 1502 | set_server_check_status(check, check->status, "Ignoring incomplete line from agent"); |
| 1503 | goto out; |
| 1504 | } |
| 1505 | |
| 1506 | *p = 0; |
| 1507 | cmd = b_head(&check->bi); |
| 1508 | |
| 1509 | while (*cmd) { |
| 1510 | /* look for next word */ |
| 1511 | if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') { |
| 1512 | cmd++; |
| 1513 | continue; |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 1514 | } |
| 1515 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1516 | if (*cmd == '#') { |
| 1517 | /* this is the beginning of a health status description, |
| 1518 | * skip the sharp and blanks. |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 1519 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1520 | cmd++; |
| 1521 | while (*cmd == '\t' || *cmd == ' ') |
| 1522 | cmd++; |
| 1523 | break; |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 1524 | } |
| 1525 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1526 | /* find the end of the word so that we have a null-terminated |
| 1527 | * word between <cmd> and <p>. |
| 1528 | */ |
| 1529 | p = cmd + 1; |
| 1530 | while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',') |
| 1531 | p++; |
| 1532 | if (*p) |
| 1533 | *p++ = 0; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1534 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1535 | /* first, health statuses */ |
| 1536 | if (strcasecmp(cmd, "up") == 0) { |
| 1537 | check->server->check.health = check->server->check.rise + check->server->check.fall - 1; |
| 1538 | status = HCHK_STATUS_L7OKD; |
| 1539 | hs = cmd; |
| 1540 | } |
| 1541 | else if (strcasecmp(cmd, "down") == 0) { |
| 1542 | check->server->check.health = 0; |
| 1543 | status = HCHK_STATUS_L7STS; |
| 1544 | hs = cmd; |
| 1545 | } |
| 1546 | else if (strcasecmp(cmd, "stopped") == 0) { |
| 1547 | check->server->check.health = 0; |
| 1548 | status = HCHK_STATUS_L7STS; |
| 1549 | hs = cmd; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1550 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1551 | else if (strcasecmp(cmd, "fail") == 0) { |
| 1552 | check->server->check.health = 0; |
| 1553 | status = HCHK_STATUS_L7STS; |
| 1554 | hs = cmd; |
| 1555 | } |
| 1556 | /* admin statuses */ |
| 1557 | else if (strcasecmp(cmd, "ready") == 0) { |
| 1558 | as = cmd; |
| 1559 | } |
| 1560 | else if (strcasecmp(cmd, "drain") == 0) { |
| 1561 | as = cmd; |
| 1562 | } |
| 1563 | else if (strcasecmp(cmd, "maint") == 0) { |
| 1564 | as = cmd; |
| 1565 | } |
| 1566 | /* try to parse a weight here and keep the last one */ |
| 1567 | else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) { |
| 1568 | ps = cmd; |
| 1569 | } |
| 1570 | /* try to parse a maxconn here */ |
| 1571 | else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) { |
| 1572 | cs = cmd; |
| 1573 | } |
| 1574 | else { |
| 1575 | /* keep a copy of the first error */ |
| 1576 | if (!err) |
| 1577 | err = cmd; |
| 1578 | } |
| 1579 | /* skip to next word */ |
| 1580 | cmd = p; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1581 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1582 | /* here, cmd points either to \0 or to the beginning of a |
| 1583 | * description. Skip possible leading spaces. |
| 1584 | */ |
| 1585 | while (*cmd == ' ' || *cmd == '\n') |
| 1586 | cmd++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1587 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1588 | /* First, update the admin status so that we avoid sending other |
| 1589 | * possibly useless warnings and can also update the health if |
| 1590 | * present after going back up. |
| 1591 | */ |
| 1592 | if (as) { |
| 1593 | if (strcasecmp(as, "drain") == 0) |
| 1594 | srv_adm_set_drain(check->server); |
| 1595 | else if (strcasecmp(as, "maint") == 0) |
| 1596 | srv_adm_set_maint(check->server); |
| 1597 | else |
| 1598 | srv_adm_set_ready(check->server); |
| 1599 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1600 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1601 | /* now change weights */ |
| 1602 | if (ps) { |
| 1603 | const char *msg; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 1604 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1605 | msg = server_parse_weight_change_request(check->server, ps); |
| 1606 | if (!wrn || !*wrn) |
| 1607 | wrn = msg; |
| 1608 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1609 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1610 | if (cs) { |
| 1611 | const char *msg; |
Willy Tarreau | 6dd4ac8 | 2019-09-03 18:55:02 +0200 | [diff] [blame] | 1612 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1613 | cs += strlen("maxconn:"); |
Willy Tarreau | 6dd4ac8 | 2019-09-03 18:55:02 +0200 | [diff] [blame] | 1614 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1615 | msg = server_parse_maxconn_change_request(check->server, cs); |
| 1616 | if (!wrn || !*wrn) |
| 1617 | wrn = msg; |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1618 | } |
| 1619 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1620 | /* and finally health status */ |
| 1621 | if (hs) { |
| 1622 | /* We'll report some of the warnings and errors we have |
| 1623 | * here. Down reports are critical, we leave them untouched. |
| 1624 | * Lack of report, or report of 'UP' leaves the room for |
| 1625 | * ERR first, then WARN. |
| 1626 | */ |
| 1627 | const char *msg = cmd; |
| 1628 | struct buffer *t; |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1629 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1630 | if (!*msg || status == HCHK_STATUS_L7OKD) { |
| 1631 | if (err && *err) |
| 1632 | msg = err; |
| 1633 | else if (wrn && *wrn) |
| 1634 | msg = wrn; |
| 1635 | } |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 1636 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1637 | t = get_trash_chunk(); |
| 1638 | chunk_printf(t, "via agent : %s%s%s%s", |
| 1639 | hs, *msg ? " (" : "", |
| 1640 | msg, *msg ? ")" : ""); |
| 1641 | set_server_check_status(check, status, t->area); |
| 1642 | } |
| 1643 | else if (err && *err) { |
| 1644 | /* No status change but we'd like to report something odd. |
| 1645 | * Just report the current state and copy the message. |
| 1646 | */ |
| 1647 | chunk_printf(&trash, "agent reports an error : %s", err); |
| 1648 | set_server_check_status(check, status/*check->status*/, trash.area); |
| 1649 | } |
| 1650 | else if (wrn && *wrn) { |
| 1651 | /* No status change but we'd like to report something odd. |
| 1652 | * Just report the current state and copy the message. |
| 1653 | */ |
| 1654 | chunk_printf(&trash, "agent warns : %s", wrn); |
| 1655 | set_server_check_status(check, status/*check->status*/, trash.area); |
| 1656 | } |
| 1657 | else |
| 1658 | set_server_check_status(check, status, NULL); |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 1659 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1660 | out: |
| 1661 | return ret; |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1662 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1663 | wait_more_data: |
| 1664 | ret = TCPCHK_EVAL_WAIT; |
| 1665 | goto out; |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1666 | } |
| 1667 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1668 | /* Evaluates a TCPCHK_ACT_CONNECT rule. Returns TCPCHK_EVAL_WAIT to wait the |
| 1669 | * connection establishment, TCPCHK_EVAL_CONTINUE to evaluate the next rule or |
| 1670 | * TCPCHK_EVAL_STOP if an error occurred. |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1671 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1672 | 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] | 1673 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1674 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 1675 | struct tcpcheck_connect *connect = &rule->connect; |
| 1676 | struct proxy *proxy = check->proxy; |
| 1677 | struct server *s = check->server; |
| 1678 | struct task *t = check->task; |
| 1679 | struct conn_stream *cs; |
| 1680 | struct connection *conn = NULL; |
| 1681 | struct protocol *proto; |
| 1682 | struct xprt_ops *xprt; |
Christopher Faulet | 2fabd9d | 2020-05-09 14:46:43 +0200 | [diff] [blame] | 1683 | struct tcpcheck_rule *next; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1684 | int status, port; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1685 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1686 | /* For a connect action we'll create a new connection. We may also have |
| 1687 | * to kill a previous one. But we don't want to leave *without* a |
| 1688 | * connection if we came here from the connection layer, hence with a |
| 1689 | * connection. Thus we'll proceed in the following order : |
| 1690 | * 1: close but not release previous connection (handled by the caller) |
| 1691 | * 2: try to get a new connection |
| 1692 | * 3: release and replace the old one on success |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 1693 | */ |
Willy Tarreau | e7b7348 | 2013-11-21 11:50:50 +0100 | [diff] [blame] | 1694 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1695 | /* 2- prepare new connection */ |
| 1696 | cs = cs_new(NULL); |
| 1697 | if (!cs) { |
| 1698 | chunk_printf(&trash, "TCPCHK error allocating connection at step %d", |
| 1699 | tcpcheck_get_step_id(check, rule)); |
| 1700 | if (rule->comment) |
| 1701 | chunk_appendf(&trash, " comment: '%s'", rule->comment); |
| 1702 | set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.area); |
| 1703 | ret = TCPCHK_EVAL_STOP; |
| 1704 | goto out; |
| 1705 | } |
Willy Tarreau | 15f3910 | 2013-12-11 20:41:18 +0100 | [diff] [blame] | 1706 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1707 | /* 3- release and replace the old one on success */ |
| 1708 | if (check->cs) { |
| 1709 | if (check->wait_list.events) |
Christopher Faulet | 06150e4 | 2020-04-27 11:22:56 +0200 | [diff] [blame] | 1710 | check->cs->conn->mux->unsubscribe(check->cs, check->wait_list.events, |
| 1711 | &check->wait_list); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1712 | |
| 1713 | /* We may have been scheduled to run, and the I/O handler |
| 1714 | * expects to have a cs, so remove the tasklet |
| 1715 | */ |
| 1716 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
| 1717 | cs_destroy(check->cs); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1718 | } |
| 1719 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1720 | tasklet_set_tid(check->wait_list.tasklet, tid); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1721 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1722 | check->cs = cs; |
| 1723 | conn = cs->conn; |
| 1724 | conn_set_owner(conn, check->sess, NULL); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1725 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1726 | /* Maybe there were an older connection we were waiting on */ |
| 1727 | check->wait_list.events = 0; |
| 1728 | conn->target = s ? &s->obj_type : &proxy->obj_type; |
| 1729 | |
| 1730 | /* no client address */ |
| 1731 | if (!sockaddr_alloc(&conn->dst)) { |
| 1732 | status = SF_ERR_RESOURCE; |
| 1733 | goto fail_check; |
| 1734 | } |
| 1735 | |
| 1736 | /* connect to the connect rule addr if specified, otherwise the check |
Christopher Faulet | 931ae5b | 2020-04-28 10:31:53 +0200 | [diff] [blame] | 1737 | * addr if specified on the server. otherwise, use the server addr (it |
| 1738 | * MUST exist at this step). |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1739 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1740 | *conn->dst = (is_addr(&connect->addr) |
| 1741 | ? connect->addr |
| 1742 | : (is_addr(&check->addr) ? check->addr : s->addr)); |
| 1743 | proto = protocol_by_family(conn->dst->ss_family); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1744 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1745 | port = 0; |
| 1746 | if (!port && connect->port) |
| 1747 | port = connect->port; |
| 1748 | if (!port && connect->port_expr) { |
| 1749 | struct sample *smp; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1750 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1751 | smp = sample_fetch_as_type(check->proxy, check->sess, NULL, |
| 1752 | SMP_OPT_DIR_REQ | SMP_OPT_FINAL, |
| 1753 | connect->port_expr, SMP_T_SINT); |
| 1754 | if (smp) |
| 1755 | port = smp->data.u.sint; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1756 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1757 | if (!port && is_inet_addr(&connect->addr)) |
| 1758 | port = get_host_port(&connect->addr); |
| 1759 | if (!port && check->port) |
| 1760 | port = check->port; |
| 1761 | if (!port && is_inet_addr(&check->addr)) |
| 1762 | port = get_host_port(&check->addr); |
Christopher Faulet | 931ae5b | 2020-04-28 10:31:53 +0200 | [diff] [blame] | 1763 | if (!port) { |
| 1764 | /* The server MUST exist here */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1765 | port = s->svc_port; |
Christopher Faulet | 931ae5b | 2020-04-28 10:31:53 +0200 | [diff] [blame] | 1766 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1767 | set_host_port(conn->dst, port); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 1768 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1769 | xprt = ((connect->options & TCPCHK_OPT_SSL) |
| 1770 | ? xprt_get(XPRT_SSL) |
| 1771 | : ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) ? check->xprt : xprt_get(XPRT_RAW))); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1772 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1773 | conn_prepare(conn, proto, xprt); |
| 1774 | cs_attach(cs, check, &check_conn_cb); |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 1775 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1776 | status = SF_ERR_INTERNAL; |
Christopher Faulet | 2fabd9d | 2020-05-09 14:46:43 +0200 | [diff] [blame] | 1777 | next = get_next_tcpcheck_rule(check->tcpcheck_rules, rule); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1778 | if (proto && proto->connect) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1779 | int flags = 0; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1780 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1781 | if (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) |
| 1782 | flags |= CONNECT_HAS_DATA; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1783 | if (!next || next->action != TCPCHK_ACT_EXPECT) |
| 1784 | flags |= CONNECT_DELACK_ALWAYS; |
| 1785 | status = proto->connect(conn, flags); |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1786 | } |
| 1787 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1788 | if (status != SF_ERR_NONE) |
| 1789 | goto fail_check; |
Christopher Faulet | 799f3a4 | 2020-04-07 12:06:14 +0200 | [diff] [blame] | 1790 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1791 | conn->flags |= CO_FL_PRIVATE; |
| 1792 | conn->ctx = cs; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1793 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1794 | /* The mux may be initialized now if there isn't server attached to the |
| 1795 | * check (email alerts) or if there is a mux proto specified or if there |
| 1796 | * is no alpn. |
| 1797 | */ |
Christopher Faulet | 12882cf | 2020-04-23 15:50:18 +0200 | [diff] [blame] | 1798 | if (!s || ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && check->mux_proto) || |
| 1799 | connect->mux_proto || (!connect->alpn && !check->alpn_str)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1800 | const struct mux_ops *mux_ops; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1801 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1802 | if (connect->mux_proto) |
| 1803 | mux_ops = connect->mux_proto->mux; |
Christopher Faulet | 12882cf | 2020-04-23 15:50:18 +0200 | [diff] [blame] | 1804 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && check->mux_proto) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1805 | mux_ops = check->mux_proto->mux; |
| 1806 | else { |
| 1807 | int mode = ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK |
| 1808 | ? PROTO_MODE_HTTP |
| 1809 | : PROTO_MODE_TCP); |
| 1810 | |
| 1811 | 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] | 1812 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1813 | if (mux_ops && conn_install_mux(conn, mux_ops, cs, proxy, check->sess) < 0) { |
| 1814 | status = SF_ERR_INTERNAL; |
| 1815 | goto fail_check; |
| 1816 | } |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1817 | } |
| 1818 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1819 | #ifdef USE_OPENSSL |
| 1820 | if (connect->sni) |
| 1821 | ssl_sock_set_servername(conn, connect->sni); |
Christopher Faulet | 931ae5b | 2020-04-28 10:31:53 +0200 | [diff] [blame] | 1822 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s && s->check.sni) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1823 | ssl_sock_set_servername(conn, s->check.sni); |
| 1824 | |
| 1825 | if (connect->alpn) |
| 1826 | ssl_sock_set_alpn(conn, (unsigned char *)connect->alpn, connect->alpn_len); |
Christopher Faulet | 931ae5b | 2020-04-28 10:31:53 +0200 | [diff] [blame] | 1827 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s && s->check.alpn_str) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1828 | ssl_sock_set_alpn(conn, (unsigned char *)s->check.alpn_str, s->check.alpn_len); |
| 1829 | #endif |
Christopher Faulet | 931ae5b | 2020-04-28 10:31:53 +0200 | [diff] [blame] | 1830 | if ((connect->options & TCPCHK_OPT_SOCKS4) && s && (s->flags & SRV_F_SOCKS4_PROXY)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1831 | conn->send_proxy_ofs = 1; |
| 1832 | conn->flags |= CO_FL_SOCKS4; |
| 1833 | } |
Christopher Faulet | 931ae5b | 2020-04-28 10:31:53 +0200 | [diff] [blame] | 1834 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s && s->check.via_socks4 && (s->flags & SRV_F_SOCKS4_PROXY)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1835 | conn->send_proxy_ofs = 1; |
| 1836 | conn->flags |= CO_FL_SOCKS4; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1837 | } |
| 1838 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1839 | if (connect->options & TCPCHK_OPT_SEND_PROXY) { |
| 1840 | conn->send_proxy_ofs = 1; |
| 1841 | conn->flags |= CO_FL_SEND_PROXY; |
| 1842 | } |
Christopher Faulet | 931ae5b | 2020-04-28 10:31:53 +0200 | [diff] [blame] | 1843 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s && s->check.send_proxy && !(check->state & CHK_ST_AGENT)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1844 | conn->send_proxy_ofs = 1; |
| 1845 | conn->flags |= CO_FL_SEND_PROXY; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1846 | } |
| 1847 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1848 | if (conn_ctrl_ready(conn) && (connect->options & TCPCHK_OPT_LINGER)) { |
| 1849 | /* Some servers don't like reset on close */ |
| 1850 | fdtab[cs->conn->handle.fd].linger_risk = 0; |
| 1851 | } |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1852 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1853 | if (conn_ctrl_ready(conn) && (conn->flags & (CO_FL_SEND_PROXY | CO_FL_SOCKS4))) { |
| 1854 | if (xprt_add_hs(conn) < 0) |
| 1855 | status = SF_ERR_RESOURCE; |
| 1856 | } |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1857 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1858 | fail_check: |
| 1859 | /* It can return one of : |
| 1860 | * - SF_ERR_NONE if everything's OK |
| 1861 | * - SF_ERR_SRVTO if there are no more servers |
| 1862 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 1863 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1864 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1865 | * - SF_ERR_INTERNAL for any other purely internal errors |
| 1866 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
| 1867 | * Note that we try to prevent the network stack from sending the ACK during the |
| 1868 | * connect() when a pure TCP check is used (without PROXY protocol). |
| 1869 | */ |
| 1870 | switch (status) { |
| 1871 | case SF_ERR_NONE: |
| 1872 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1873 | * to establish but only when timeout.check is set as it may be |
| 1874 | * to short for a full check otherwise |
| 1875 | */ |
| 1876 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1877 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1878 | if (proxy->timeout.check && proxy->timeout.connect) { |
| 1879 | int t_con = tick_add(now_ms, proxy->timeout.connect); |
| 1880 | t->expire = tick_first(t->expire, t_con); |
| 1881 | } |
| 1882 | break; |
| 1883 | case SF_ERR_SRVTO: /* ETIMEDOUT */ |
| 1884 | case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
| 1885 | case SF_ERR_PRXCOND: |
| 1886 | case SF_ERR_RESOURCE: |
| 1887 | case SF_ERR_INTERNAL: |
| 1888 | chk_report_conn_err(check, errno, 0); |
| 1889 | ret = TCPCHK_EVAL_STOP; |
| 1890 | goto out; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1891 | } |
| 1892 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1893 | /* don't do anything until the connection is established */ |
| 1894 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Christopher Faulet | 4b3a2df | 2020-05-12 15:05:43 +0200 | [diff] [blame] | 1895 | if (conn->mux) { |
| 1896 | if (next && next->action == TCPCHK_ACT_SEND) |
| 1897 | conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list); |
| 1898 | else |
| 1899 | conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list); |
| 1900 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1901 | ret = TCPCHK_EVAL_WAIT; |
| 1902 | goto out; |
| 1903 | } |
| 1904 | |
| 1905 | out: |
| 1906 | if (conn && check->result == CHK_RES_FAILED) |
| 1907 | conn->flags |= CO_FL_ERROR; |
| 1908 | return ret; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 1909 | } |
| 1910 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1911 | /* Evaluates a TCPCHK_ACT_SEND rule. Returns TCPCHK_EVAL_WAIT if outgoing data |
| 1912 | * were not fully sent, TCPCHK_EVAL_CONTINUE to evaluate the next rule or |
| 1913 | * TCPCHK_EVAL_STOP if an error occurred. |
| 1914 | */ |
| 1915 | 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] | 1916 | { |
| 1917 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1918 | struct tcpcheck_send *send = &rule->send; |
| 1919 | struct conn_stream *cs = check->cs; |
| 1920 | struct connection *conn = cs_conn(cs); |
| 1921 | struct buffer *tmp = NULL; |
| 1922 | struct htx *htx = NULL; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1923 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1924 | /* reset the read & write buffer */ |
| 1925 | b_reset(&check->bi); |
| 1926 | b_reset(&check->bo); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1927 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1928 | switch (send->type) { |
| 1929 | case TCPCHK_SEND_STRING: |
| 1930 | case TCPCHK_SEND_BINARY: |
| 1931 | if (istlen(send->data) >= b_size(&check->bo)) { |
| 1932 | chunk_printf(&trash, "tcp-check send : string too large (%u) for buffer size (%u) at step %d", |
| 1933 | (unsigned int)istlen(send->data), (unsigned int)b_size(&check->bo), |
| 1934 | tcpcheck_get_step_id(check, rule)); |
| 1935 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area); |
| 1936 | ret = TCPCHK_EVAL_STOP; |
| 1937 | goto out; |
| 1938 | } |
| 1939 | b_putist(&check->bo, send->data); |
| 1940 | break; |
| 1941 | case TCPCHK_SEND_STRING_LF: |
| 1942 | check->bo.data = sess_build_logline(check->sess, NULL, b_orig(&check->bo), b_size(&check->bo), &rule->send.fmt); |
| 1943 | if (!b_data(&check->bo)) |
| 1944 | goto out; |
| 1945 | break; |
Christopher Faulet | 2edcd4c | 2020-04-28 10:39:50 +0200 | [diff] [blame] | 1946 | case TCPCHK_SEND_BINARY_LF: { |
Christopher Faulet | 9c2cb2d | 2020-04-28 16:40:41 +0200 | [diff] [blame] | 1947 | int len = b_size(&check->bo); |
Christopher Faulet | 2edcd4c | 2020-04-28 10:39:50 +0200 | [diff] [blame] | 1948 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1949 | tmp = alloc_trash_chunk(); |
| 1950 | if (!tmp) |
| 1951 | goto error_lf; |
| 1952 | tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &rule->send.fmt); |
| 1953 | if (!b_data(tmp)) |
| 1954 | goto out; |
| 1955 | tmp->area[tmp->data] = '\0'; |
Christopher Faulet | 2edcd4c | 2020-04-28 10:39:50 +0200 | [diff] [blame] | 1956 | if (parse_binary(b_orig(tmp), &check->bo.area, &len, NULL) == 0) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1957 | goto error_lf; |
Christopher Faulet | 2edcd4c | 2020-04-28 10:39:50 +0200 | [diff] [blame] | 1958 | check->bo.data = len; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1959 | break; |
Christopher Faulet | 2edcd4c | 2020-04-28 10:39:50 +0200 | [diff] [blame] | 1960 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1961 | case TCPCHK_SEND_HTTP: { |
| 1962 | struct htx_sl *sl; |
| 1963 | struct ist meth, uri, vsn, clen, body; |
| 1964 | unsigned int slflags = 0; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1965 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1966 | tmp = alloc_trash_chunk(); |
| 1967 | if (!tmp) |
| 1968 | goto error_htx; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1969 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1970 | meth = ((send->http.meth.meth == HTTP_METH_OTHER) |
| 1971 | ? ist2(send->http.meth.str.area, send->http.meth.str.data) |
| 1972 | : http_known_methods[send->http.meth.meth]); |
Christopher Faulet | 7c95f5f | 2020-05-06 15:06:34 +0200 | [diff] [blame] | 1973 | if (send->http.flags & TCPCHK_SND_HTTP_FL_URI_FMT) { |
| 1974 | tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &send->http.uri_fmt); |
| 1975 | uri = (b_data(tmp) ? ist2(b_orig(tmp), b_data(tmp)) : ist("/")); |
| 1976 | } |
| 1977 | else |
| 1978 | uri = (isttest(send->http.uri) ? send->http.uri : ist("/")); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1979 | vsn = (isttest(send->http.vsn) ? send->http.vsn : ist("HTTP/1.0")); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1980 | |
Christopher Faulet | 8bf8fda | 2020-04-28 09:10:19 +0200 | [diff] [blame] | 1981 | if ((istlen(vsn) == 6 && *(vsn.ptr+5) == '2') || |
| 1982 | (istlen(vsn) == 8 && (*(vsn.ptr+5) > '1' || (*(vsn.ptr+5) == '1' && *(vsn.ptr+7) >= '1')))) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1983 | slflags |= HTX_SL_F_VER_11; |
| 1984 | slflags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 1985 | if (!isttest(send->http.body)) |
| 1986 | slflags |= HTX_SL_F_BODYLESS; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1987 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1988 | htx = htx_from_buf(&check->bo); |
| 1989 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, slflags, meth, uri, vsn); |
| 1990 | if (!sl) |
| 1991 | goto error_htx; |
| 1992 | sl->info.req.meth = send->http.meth.meth; |
Christopher Faulet | 39959a7 | 2020-04-29 13:21:37 +0200 | [diff] [blame] | 1993 | if (!http_update_host(htx, sl, uri)) |
| 1994 | goto error_htx; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 1995 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1996 | if (!LIST_ISEMPTY(&send->http.hdrs)) { |
| 1997 | struct tcpcheck_http_hdr *hdr; |
Christopher Faulet | 39959a7 | 2020-04-29 13:21:37 +0200 | [diff] [blame] | 1998 | struct ist hdr_value; |
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 | list_for_each_entry(hdr, &send->http.hdrs, list) { |
| 2001 | chunk_reset(tmp); |
| 2002 | tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &hdr->value); |
| 2003 | if (!b_data(tmp)) |
| 2004 | continue; |
Christopher Faulet | 39959a7 | 2020-04-29 13:21:37 +0200 | [diff] [blame] | 2005 | hdr_value = ist2(b_orig(tmp), b_data(tmp)); |
| 2006 | if (!htx_add_header(htx, hdr->name, hdr_value)) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2007 | goto error_htx; |
Christopher Faulet | 39959a7 | 2020-04-29 13:21:37 +0200 | [diff] [blame] | 2008 | if ((sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(hdr->name, ist("host"))) { |
| 2009 | if (!http_update_authority(htx, sl, hdr_value)) |
| 2010 | goto error_htx; |
| 2011 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2012 | } |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2013 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2014 | } |
| 2015 | if (check->proxy->options2 & PR_O2_CHK_SNDST) { |
| 2016 | chunk_reset(tmp); |
| 2017 | httpchk_build_status_header(check->server, tmp); |
| 2018 | if (!htx_add_header(htx, ist("X-Haproxy-Server-State"), ist2(b_orig(tmp), b_data(tmp)))) |
| 2019 | goto error_htx; |
| 2020 | } |
Christopher Faulet | 574e7bd | 2020-05-06 15:38:58 +0200 | [diff] [blame] | 2021 | |
| 2022 | |
| 2023 | if (send->http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT) { |
| 2024 | chunk_reset(tmp); |
| 2025 | tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &send->http.body_fmt); |
| 2026 | body = ist2(b_orig(tmp), b_data(tmp)); |
| 2027 | } |
| 2028 | else |
| 2029 | body = send->http.body; |
| 2030 | clen = ist((!istlen(body) ? "0" : ultoa(istlen(body)))); |
| 2031 | |
| 2032 | if (!htx_add_header(htx, ist("Connection"), ist("close")) || |
| 2033 | !htx_add_header(htx, ist("Content-length"), clen)) |
| 2034 | goto error_htx; |
| 2035 | |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2036 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2037 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | 574e7bd | 2020-05-06 15:38:58 +0200 | [diff] [blame] | 2038 | (istlen(body) && !htx_add_data_atonce(htx, body)) || |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2039 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 2040 | goto error_htx; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2041 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2042 | htx_to_buf(htx, &check->bo); |
| 2043 | break; |
| 2044 | } |
| 2045 | case TCPCHK_SEND_UNDEF: |
| 2046 | /* Should never happen. */ |
| 2047 | ret = TCPCHK_EVAL_STOP; |
| 2048 | goto out; |
| 2049 | }; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2050 | |
Christopher Faulet | 6d47121 | 2020-04-22 11:09:25 +0200 | [diff] [blame] | 2051 | |
| 2052 | if (conn->mux->snd_buf(cs, &check->bo, |
| 2053 | (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] | 2054 | if ((conn->flags & CO_FL_ERROR) || (cs->flags & CS_FL_ERROR)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2055 | ret = TCPCHK_EVAL_STOP; |
Christopher Faulet | 815516d | 2020-04-21 13:02:14 +0200 | [diff] [blame] | 2056 | goto out; |
| 2057 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2058 | } |
Christopher Faulet | 6d47121 | 2020-04-22 11:09:25 +0200 | [diff] [blame] | 2059 | 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] | 2060 | cs->conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list); |
| 2061 | ret = TCPCHK_EVAL_WAIT; |
| 2062 | goto out; |
| 2063 | } |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2064 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2065 | out: |
| 2066 | free_trash_chunk(tmp); |
| 2067 | return ret; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2068 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2069 | error_htx: |
| 2070 | if (htx) { |
| 2071 | htx_reset(htx); |
| 2072 | htx_to_buf(htx, &check->bo); |
| 2073 | } |
| 2074 | chunk_printf(&trash, "tcp-check send : failed to build HTTP request at step %d", |
| 2075 | tcpcheck_get_step_id(check, rule)); |
| 2076 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area); |
| 2077 | ret = TCPCHK_EVAL_STOP; |
| 2078 | goto out; |
| 2079 | |
| 2080 | error_lf: |
| 2081 | chunk_printf(&trash, "tcp-check send : failed to build log-format string at step %d", |
| 2082 | tcpcheck_get_step_id(check, rule)); |
| 2083 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area); |
| 2084 | ret = TCPCHK_EVAL_STOP; |
| 2085 | goto out; |
| 2086 | |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2087 | } |
| 2088 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 2089 | /* Try to receive data before evaluating a tcp-check expect rule. Returns |
| 2090 | * TCPCHK_EVAL_WAIT if it is already subscribed on receive events or if nothing |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2091 | * was received, TCPCHK_EVAL_CONTINUE to evaluate the expect rule or |
| 2092 | * TCPCHK_EVAL_STOP if an error occurred. |
| 2093 | */ |
| 2094 | 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] | 2095 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2096 | struct conn_stream *cs = check->cs; |
| 2097 | struct connection *conn = cs_conn(cs); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2098 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2099 | size_t max, read, cur_read = 0; |
| 2100 | int is_empty; |
| 2101 | int read_poll = MAX_READ_POLL_LOOPS; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2102 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2103 | if (check->wait_list.events & SUB_RETRY_RECV) |
| 2104 | goto wait_more_data; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2105 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2106 | if (cs->flags & CS_FL_EOS) |
| 2107 | goto end_recv; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2108 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2109 | /* errors on the connection and the conn-stream were already checked */ |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2110 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2111 | /* prepare to detect if the mux needs more room */ |
| 2112 | cs->flags &= ~CS_FL_WANT_ROOM; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2113 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2114 | while ((cs->flags & CS_FL_RCV_MORE) || |
| 2115 | (!(conn->flags & CO_FL_ERROR) && !(cs->flags & (CS_FL_ERROR|CS_FL_EOS)))) { |
| 2116 | max = (IS_HTX_CS(cs) ? htx_free_space(htxbuf(&check->bi)) : b_room(&check->bi)); |
| 2117 | read = conn->mux->rcv_buf(cs, &check->bi, max, 0); |
| 2118 | cur_read += read; |
| 2119 | if (!read || |
| 2120 | (cs->flags & CS_FL_WANT_ROOM) || |
| 2121 | (--read_poll <= 0) || |
| 2122 | (read < max && read >= global.tune.recv_enough)) |
| 2123 | break; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2124 | } |
| 2125 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2126 | end_recv: |
| 2127 | is_empty = (IS_HTX_CS(cs) ? htx_is_empty(htxbuf(&check->bi)) : !b_data(&check->bi)); |
| 2128 | if (is_empty && ((conn->flags & CO_FL_ERROR) || (cs->flags & CS_FL_ERROR))) { |
| 2129 | /* Report network errors only if we got no other data. Otherwise |
| 2130 | * we'll let the upper layers decide whether the response is OK |
| 2131 | * or not. It is very common that an RST sent by the server is |
| 2132 | * reported as an error just after the last data chunk. |
| 2133 | */ |
| 2134 | goto stop; |
| 2135 | } |
| 2136 | if (!cur_read) { |
| 2137 | if (!(cs->flags & (CS_FL_WANT_ROOM|CS_FL_ERROR|CS_FL_EOS))) { |
| 2138 | conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list); |
| 2139 | goto wait_more_data; |
| 2140 | } |
| 2141 | if (is_empty) { |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 2142 | int status; |
| 2143 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2144 | chunk_printf(&trash, "TCPCHK got an empty response at step %d", |
| 2145 | tcpcheck_get_step_id(check, rule)); |
| 2146 | if (rule->comment) |
| 2147 | chunk_appendf(&trash, " comment: '%s'", rule->comment); |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 2148 | |
| 2149 | status = ((rule->expect.err_status != HCHK_STATUS_UNKNOWN) ? rule->expect.err_status : HCHK_STATUS_L7RSP); |
| 2150 | set_server_check_status(check, status, trash.area); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2151 | goto stop; |
| 2152 | } |
| 2153 | } |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2154 | |
| 2155 | out: |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2156 | return ret; |
| 2157 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2158 | stop: |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2159 | ret = TCPCHK_EVAL_STOP; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2160 | goto out; |
| 2161 | |
| 2162 | wait_more_data: |
| 2163 | ret = TCPCHK_EVAL_WAIT; |
| 2164 | goto out; |
| 2165 | } |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2166 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2167 | /* Evaluates an HTTP TCPCHK_ACT_EXPECT rule. If <last_read> is set , no more data |
| 2168 | * are expected. Returns TCPCHK_EVAL_WAIT to wait for more data, |
| 2169 | * TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if an |
| 2170 | * error occurred. |
| 2171 | */ |
| 2172 | 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] | 2173 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2174 | struct htx *htx = htxbuf(&check->bi); |
| 2175 | struct htx_sl *sl; |
| 2176 | struct htx_blk *blk; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2177 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2178 | struct tcpcheck_expect *expect = &rule->expect; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 2179 | struct buffer *msg = NULL, *tmp = NULL, *nbuf = NULL, *vbuf = NULL; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 2180 | enum healthcheck_status status = HCHK_STATUS_L7RSP; |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2181 | struct ist desc = IST_NULL; |
Christopher Faulet | 8021a5f | 2020-04-24 13:53:12 +0200 | [diff] [blame] | 2182 | int i, match, inverse; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2183 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2184 | 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] | 2185 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2186 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 2187 | status = HCHK_STATUS_L7RSP; |
| 2188 | goto error; |
| 2189 | } |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2190 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2191 | if (htx_is_empty(htx)) { |
| 2192 | if (last_read) { |
| 2193 | status = HCHK_STATUS_L7RSP; |
| 2194 | goto error; |
| 2195 | } |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2196 | goto wait_more_data; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2197 | } |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2198 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2199 | sl = http_get_stline(htx); |
| 2200 | check->code = sl->info.res.status; |
| 2201 | |
| 2202 | if (check->server && |
| 2203 | (check->server->proxy->options & PR_O_DISABLE404) && |
| 2204 | (check->server->next_state != SRV_ST_STOPPED) && |
| 2205 | (check->code == 404)) { |
| 2206 | /* 404 may be accepted as "stopping" only if the server was up */ |
| 2207 | goto out; |
| 2208 | } |
| 2209 | |
| 2210 | inverse = !!(expect->flags & TCPCHK_EXPT_FL_INV); |
| 2211 | /* Make GCC happy ; initialize match to a failure state. */ |
| 2212 | match = inverse; |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2213 | status = expect->err_status; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2214 | |
| 2215 | switch (expect->type) { |
| 2216 | case TCPCHK_EXPECT_HTTP_STATUS: |
Christopher Faulet | 8021a5f | 2020-04-24 13:53:12 +0200 | [diff] [blame] | 2217 | match = 0; |
| 2218 | for (i = 0; i < expect->codes.num; i++) { |
| 2219 | if (sl->info.res.status >= expect->codes.codes[i][0] && |
| 2220 | sl->info.res.status <= expect->codes.codes[i][1]) { |
| 2221 | match = 1; |
| 2222 | break; |
| 2223 | } |
| 2224 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2225 | |
| 2226 | /* Set status and description in case of error */ |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2227 | status = ((status != HCHK_STATUS_UNKNOWN) ? status : HCHK_STATUS_L7STS); |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 2228 | if (LIST_ISEMPTY(&expect->onerror_fmt)) |
| 2229 | desc = htx_sl_res_reason(sl); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2230 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 2231 | case TCPCHK_EXPECT_HTTP_STATUS_REGEX: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2232 | match = regex_exec2(expect->regex, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 2233 | |
| 2234 | /* Set status and description in case of error */ |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2235 | status = ((status != HCHK_STATUS_UNKNOWN) ? status : HCHK_STATUS_L7STS); |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 2236 | if (LIST_ISEMPTY(&expect->onerror_fmt)) |
| 2237 | desc = htx_sl_res_reason(sl); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2238 | break; |
| 2239 | |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 2240 | case TCPCHK_EXPECT_HTTP_HEADER: { |
| 2241 | struct http_hdr_ctx ctx; |
| 2242 | struct ist npat, vpat, value; |
| 2243 | int full = (expect->flags & (TCPCHK_EXPT_FL_HTTP_HVAL_NONE|TCPCHK_EXPT_FL_HTTP_HVAL_FULL)); |
| 2244 | |
| 2245 | if (expect->flags & TCPCHK_EXPT_FL_HTTP_HNAME_FMT) { |
| 2246 | nbuf = alloc_trash_chunk(); |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2247 | if (!nbuf) { |
| 2248 | status = HCHK_STATUS_L7RSP; |
| 2249 | desc = ist("Failed to allocate buffer to eval log-format string"); |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 2250 | goto error; |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2251 | } |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 2252 | nbuf->data = sess_build_logline(check->sess, NULL, b_orig(nbuf), b_size(nbuf), &expect->hdr.name_fmt); |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2253 | if (!b_data(nbuf)) { |
| 2254 | status = HCHK_STATUS_L7RSP; |
| 2255 | desc = ist("log-format string evaluated to an empty string"); |
| 2256 | goto error; |
| 2257 | } |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 2258 | npat = ist2(b_orig(nbuf), b_data(nbuf)); |
| 2259 | } |
| 2260 | else if (!(expect->flags & TCPCHK_EXPT_FL_HTTP_HNAME_REG)) |
| 2261 | npat = expect->hdr.name; |
| 2262 | |
| 2263 | if (expect->flags & TCPCHK_EXPT_FL_HTTP_HVAL_FMT) { |
| 2264 | vbuf = alloc_trash_chunk(); |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2265 | if (!vbuf) { |
| 2266 | status = HCHK_STATUS_L7RSP; |
| 2267 | desc = ist("Failed to allocate buffer to eval log-format string"); |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 2268 | goto error; |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2269 | } |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 2270 | vbuf->data = sess_build_logline(check->sess, NULL, b_orig(vbuf), b_size(vbuf), &expect->hdr.value_fmt); |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2271 | if (!b_data(vbuf)) { |
| 2272 | status = HCHK_STATUS_L7RSP; |
| 2273 | desc = ist("log-format string evaluated to an empty string"); |
| 2274 | goto error; |
| 2275 | } |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 2276 | vpat = ist2(b_orig(vbuf), b_data(vbuf)); |
| 2277 | } |
| 2278 | else if (!(expect->flags & TCPCHK_EXPT_FL_HTTP_HVAL_REG)) |
| 2279 | vpat = expect->hdr.value; |
| 2280 | |
| 2281 | match = 0; |
| 2282 | ctx.blk = NULL; |
| 2283 | while (1) { |
| 2284 | switch (expect->flags & TCPCHK_EXPT_FL_HTTP_HNAME_TYPE) { |
| 2285 | case TCPCHK_EXPT_FL_HTTP_HNAME_STR: |
| 2286 | if (!http_find_str_header(htx, npat, &ctx, full)) |
| 2287 | goto end_of_match; |
| 2288 | break; |
| 2289 | case TCPCHK_EXPT_FL_HTTP_HNAME_BEG: |
| 2290 | if (!http_find_pfx_header(htx, npat, &ctx, full)) |
| 2291 | goto end_of_match; |
| 2292 | break; |
| 2293 | case TCPCHK_EXPT_FL_HTTP_HNAME_END: |
| 2294 | if (!http_find_sfx_header(htx, npat, &ctx, full)) |
| 2295 | goto end_of_match; |
| 2296 | break; |
| 2297 | case TCPCHK_EXPT_FL_HTTP_HNAME_SUB: |
| 2298 | if (!http_find_sub_header(htx, npat, &ctx, full)) |
| 2299 | goto end_of_match; |
| 2300 | break; |
| 2301 | case TCPCHK_EXPT_FL_HTTP_HNAME_REG: |
| 2302 | if (!http_match_header(htx, expect->hdr.name_re, &ctx, full)) |
| 2303 | goto end_of_match; |
| 2304 | break; |
Christopher Faulet | 083eff3 | 2020-05-07 15:41:39 +0200 | [diff] [blame] | 2305 | default: |
| 2306 | /* should never happen */ |
| 2307 | goto end_of_match; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 2308 | } |
| 2309 | |
Christopher Faulet | 083eff3 | 2020-05-07 15:41:39 +0200 | [diff] [blame] | 2310 | /* A header has matched the name pattern, let's test its |
| 2311 | * value now (always defined from there). If there is no |
| 2312 | * value pattern, it is a good match. |
| 2313 | */ |
| 2314 | |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 2315 | if (expect->flags & TCPCHK_EXPT_FL_HTTP_HVAL_NONE) { |
| 2316 | match = 1; |
| 2317 | goto end_of_match; |
| 2318 | } |
| 2319 | |
| 2320 | value = ctx.value; |
| 2321 | switch (expect->flags & TCPCHK_EXPT_FL_HTTP_HVAL_TYPE) { |
| 2322 | case TCPCHK_EXPT_FL_HTTP_HVAL_STR: |
| 2323 | if (isteq(value, vpat)) { |
| 2324 | match = 1; |
| 2325 | goto end_of_match; |
| 2326 | } |
| 2327 | break; |
| 2328 | case TCPCHK_EXPT_FL_HTTP_HVAL_BEG: |
| 2329 | if (istlen(value) < istlen(vpat)) |
| 2330 | break; |
| 2331 | value = ist2(istptr(value), istlen(vpat)); |
| 2332 | if (isteq(value, vpat)) { |
| 2333 | match = 1; |
| 2334 | goto end_of_match; |
| 2335 | } |
| 2336 | break; |
| 2337 | case TCPCHK_EXPT_FL_HTTP_HVAL_END: |
| 2338 | if (istlen(value) < istlen(vpat)) |
| 2339 | break; |
| 2340 | value = ist2(istptr(value) + istlen(value) - istlen(vpat), istlen(vpat)); |
| 2341 | if (isteq(value, vpat)) { |
| 2342 | match = 1; |
| 2343 | goto end_of_match; |
| 2344 | } |
| 2345 | break; |
| 2346 | case TCPCHK_EXPT_FL_HTTP_HVAL_SUB: |
| 2347 | if (isttest(istist(value, vpat))) { |
| 2348 | match = 1; |
| 2349 | goto end_of_match; |
| 2350 | } |
| 2351 | break; |
| 2352 | case TCPCHK_EXPT_FL_HTTP_HVAL_REG: |
| 2353 | if (regex_exec2(expect->hdr.value_re, istptr(value), istlen(value))) { |
| 2354 | match = 1; |
| 2355 | goto end_of_match; |
| 2356 | } |
| 2357 | break; |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | end_of_match: |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2362 | status = ((status != HCHK_STATUS_UNKNOWN) ? status : HCHK_STATUS_L7STS); |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 2363 | if (LIST_ISEMPTY(&expect->onerror_fmt)) |
| 2364 | desc = htx_sl_res_reason(sl); |
| 2365 | break; |
| 2366 | } |
| 2367 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2368 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 2369 | case TCPCHK_EXPECT_HTTP_BODY_REGEX: |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 2370 | case TCPCHK_EXPECT_HTTP_BODY_LF: |
| 2371 | match = 0; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2372 | chunk_reset(&trash); |
| 2373 | for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 2374 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 2375 | |
| 2376 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 2377 | break; |
| 2378 | if (type == HTX_BLK_DATA) { |
| 2379 | if (!chunk_istcat(&trash, htx_get_blk_value(htx, blk))) |
| 2380 | break; |
| 2381 | } |
| 2382 | } |
| 2383 | |
| 2384 | if (!b_data(&trash)) { |
| 2385 | if (!last_read) |
| 2386 | goto wait_more_data; |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2387 | status = ((status != HCHK_STATUS_UNKNOWN) ? status : HCHK_STATUS_L7RSP); |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 2388 | if (LIST_ISEMPTY(&expect->onerror_fmt)) |
| 2389 | desc = ist("HTTP content check could not find a response body"); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2390 | goto error; |
| 2391 | } |
| 2392 | |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 2393 | if (expect->type == TCPCHK_EXPECT_HTTP_BODY_LF) { |
| 2394 | tmp = alloc_trash_chunk(); |
| 2395 | if (!tmp) { |
| 2396 | status = HCHK_STATUS_L7RSP; |
| 2397 | desc = ist("Failed to allocate buffer to eval log-format string"); |
| 2398 | goto error; |
| 2399 | } |
| 2400 | tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &expect->fmt); |
| 2401 | if (!b_data(tmp)) { |
| 2402 | status = HCHK_STATUS_L7RSP; |
| 2403 | desc = ist("log-format string evaluated to an empty string"); |
| 2404 | goto error; |
| 2405 | } |
| 2406 | } |
| 2407 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2408 | if (!last_read && |
| 2409 | ((expect->type == TCPCHK_EXPECT_HTTP_BODY && b_data(&trash) < istlen(expect->data)) || |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 2410 | ((expect->type == TCPCHK_EXPECT_HTTP_BODY_LF && b_data(&trash) < b_data(tmp))) || |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2411 | (expect->min_recv > 0 && b_data(&trash) < expect->min_recv))) { |
| 2412 | ret = TCPCHK_EVAL_WAIT; |
| 2413 | goto out; |
| 2414 | } |
| 2415 | |
| 2416 | if (expect->type ==TCPCHK_EXPECT_HTTP_BODY) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2417 | match = my_memmem(b_orig(&trash), b_data(&trash), istptr(expect->data), istlen(expect->data)) != NULL; |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2418 | else if (expect->type ==TCPCHK_EXPECT_HTTP_BODY_LF) |
| 2419 | match = my_memmem(b_orig(&trash), b_data(&trash), b_orig(tmp), b_data(tmp)) != NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2420 | else |
| 2421 | match = regex_exec2(expect->regex, b_orig(&trash), b_data(&trash)); |
| 2422 | |
| 2423 | /* Set status and description in case of error */ |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2424 | status = ((status != HCHK_STATUS_UNKNOWN) ? status : HCHK_STATUS_L7RSP); |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 2425 | if (LIST_ISEMPTY(&expect->onerror_fmt)) |
| 2426 | desc = (inverse |
| 2427 | ? ist("HTTP check matched unwanted content") |
| 2428 | : ist("HTTP content check did not match")); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2429 | break; |
| 2430 | |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2431 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2432 | default: |
| 2433 | /* should never happen */ |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2434 | status = ((status != HCHK_STATUS_UNKNOWN) ? status : HCHK_STATUS_L7RSP); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2435 | goto error; |
| 2436 | } |
| 2437 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2438 | /* Wait for more data on mismatch only if no minimum is defined (-1), |
| 2439 | * otherwise the absence of match is already conclusive. |
| 2440 | */ |
| 2441 | if (!match && !last_read && (expect->min_recv == -1)) { |
| 2442 | ret = TCPCHK_EVAL_WAIT; |
| 2443 | goto out; |
| 2444 | } |
| 2445 | |
| 2446 | if (!(match ^ inverse)) |
| 2447 | goto error; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2448 | |
| 2449 | out: |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 2450 | free_trash_chunk(tmp); |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 2451 | free_trash_chunk(nbuf); |
| 2452 | free_trash_chunk(vbuf); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2453 | free_trash_chunk(msg); |
| 2454 | return ret; |
| 2455 | |
| 2456 | error: |
| 2457 | ret = TCPCHK_EVAL_STOP; |
| 2458 | msg = alloc_trash_chunk(); |
| 2459 | if (msg) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2460 | tcpcheck_expect_onerror_message(msg, check, rule, 0, desc); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2461 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
| 2462 | goto out; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2463 | |
| 2464 | wait_more_data: |
| 2465 | ret = TCPCHK_EVAL_WAIT; |
| 2466 | goto out; |
| 2467 | } |
| 2468 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2469 | /* Evaluates a TCP TCPCHK_ACT_EXPECT rule. Returns TCPCHK_EVAL_WAIT to wait for |
| 2470 | * more data, TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP |
| 2471 | * if an error occurred. |
| 2472 | */ |
| 2473 | static enum tcpcheck_eval_ret tcpcheck_eval_expect(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 2474 | { |
| 2475 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 2476 | struct tcpcheck_expect *expect = &rule->expect; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 2477 | struct buffer *msg = NULL, *tmp = NULL; |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2478 | struct ist desc = IST_NULL; |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 2479 | enum healthcheck_status status; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2480 | int match, inverse; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2481 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2482 | last_read |= b_full(&check->bi); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2483 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2484 | /* The current expect might need more data than the previous one, check again |
| 2485 | * that the minimum amount data required to match is respected. |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2486 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2487 | if (!last_read) { |
| 2488 | if ((expect->type == TCPCHK_EXPECT_STRING || expect->type == TCPCHK_EXPECT_BINARY) && |
| 2489 | (b_data(&check->bi) < istlen(expect->data))) { |
| 2490 | ret = TCPCHK_EVAL_WAIT; |
| 2491 | goto out; |
| 2492 | } |
| 2493 | if (expect->min_recv > 0 && (b_data(&check->bi) < expect->min_recv)) { |
| 2494 | ret = TCPCHK_EVAL_WAIT; |
| 2495 | goto out; |
| 2496 | } |
| 2497 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2498 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2499 | inverse = !!(expect->flags & TCPCHK_EXPT_FL_INV); |
| 2500 | /* Make GCC happy ; initialize match to a failure state. */ |
| 2501 | match = inverse; |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2502 | status = ((expect->err_status != HCHK_STATUS_UNKNOWN) ? expect->err_status : HCHK_STATUS_L7RSP); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2503 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2504 | switch (expect->type) { |
| 2505 | case TCPCHK_EXPECT_STRING: |
| 2506 | case TCPCHK_EXPECT_BINARY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2507 | 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] | 2508 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 2509 | case TCPCHK_EXPECT_STRING_REGEX: |
Christopher Faulet | 88d939c | 2020-04-22 15:32:11 +0200 | [diff] [blame] | 2510 | match = regex_exec2(expect->regex, b_head(&check->bi), MIN(b_data(&check->bi), b_size(&check->bi)-1)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2511 | break; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2512 | |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 2513 | case TCPCHK_EXPECT_BINARY_REGEX: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2514 | chunk_reset(&trash); |
| 2515 | dump_binary(&trash, b_head(&check->bi), b_data(&check->bi)); |
Christopher Faulet | 88d939c | 2020-04-22 15:32:11 +0200 | [diff] [blame] | 2516 | match = regex_exec2(expect->regex, b_head(&trash), MIN(b_data(&trash), b_size(&trash)-1)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2517 | break; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 2518 | |
| 2519 | case TCPCHK_EXPECT_STRING_LF: |
| 2520 | case TCPCHK_EXPECT_BINARY_LF: |
| 2521 | match = 0; |
| 2522 | tmp = alloc_trash_chunk(); |
| 2523 | if (!tmp) { |
| 2524 | status = HCHK_STATUS_L7RSP; |
| 2525 | desc = ist("Failed to allocate buffer to eval format string"); |
| 2526 | goto error; |
| 2527 | } |
| 2528 | tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &expect->fmt); |
| 2529 | if (!b_data(tmp)) { |
| 2530 | status = HCHK_STATUS_L7RSP; |
| 2531 | desc = ist("log-format string evaluated to an empty string"); |
| 2532 | goto error; |
| 2533 | } |
| 2534 | if (expect->type == TCPCHK_EXPECT_BINARY_LF) { |
| 2535 | int len = tmp->data; |
| 2536 | if (parse_binary(b_orig(tmp), &tmp->area, &len, NULL) == 0) { |
| 2537 | status = HCHK_STATUS_L7RSP; |
| 2538 | desc = ist("Failed to parse hexastring resulting of eval of a log-format string"); |
| 2539 | goto error; |
| 2540 | } |
| 2541 | tmp->data = len; |
| 2542 | } |
| 2543 | if (b_data(&check->bi) < tmp->data) { |
| 2544 | if (!last_read) { |
| 2545 | ret = TCPCHK_EVAL_WAIT; |
| 2546 | goto out; |
| 2547 | } |
| 2548 | break; |
| 2549 | } |
| 2550 | match = my_memmem(b_head(&check->bi), b_data(&check->bi), b_orig(tmp), b_data(tmp)) != NULL; |
| 2551 | break; |
| 2552 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2553 | case TCPCHK_EXPECT_CUSTOM: |
| 2554 | if (expect->custom) |
| 2555 | ret = expect->custom(check, rule, last_read); |
| 2556 | goto out; |
| 2557 | default: |
| 2558 | /* Should never happen. */ |
| 2559 | ret = TCPCHK_EVAL_STOP; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2560 | goto out; |
| 2561 | } |
| 2562 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2563 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2564 | /* Wait for more data on mismatch only if no minimum is defined (-1), |
| 2565 | * otherwise the absence of match is already conclusive. |
| 2566 | */ |
| 2567 | if (!match && !last_read && (expect->min_recv == -1)) { |
| 2568 | ret = TCPCHK_EVAL_WAIT; |
| 2569 | goto out; |
| 2570 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2571 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2572 | /* Result as expected, next rule. */ |
| 2573 | if (match ^ inverse) |
| 2574 | goto out; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2575 | |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 2576 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2577 | /* From this point on, we matched something we did not want, this is an error state. */ |
| 2578 | ret = TCPCHK_EVAL_STOP; |
| 2579 | msg = alloc_trash_chunk(); |
| 2580 | if (msg) |
Christopher Faulet | 0d6909b | 2020-05-05 15:50:37 +0200 | [diff] [blame] | 2581 | tcpcheck_expect_onerror_message(msg, check, rule, match, desc); |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 2582 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2583 | free_trash_chunk(msg); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2584 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2585 | out: |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 2586 | free_trash_chunk(tmp); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2587 | return ret; |
| 2588 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2589 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2590 | /* Evaluates a TCPCHK_ACT_ACTION_KW rule. Returns TCPCHK_EVAL_CONTINUE to |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 2591 | * evaluate the next rule or TCPCHK_EVAL_STOP if an error occurred. It never |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2592 | * waits. |
| 2593 | */ |
| 2594 | static enum tcpcheck_eval_ret tcpcheck_eval_action_kw(struct check *check, struct tcpcheck_rule *rule) |
| 2595 | { |
| 2596 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 2597 | struct act_rule *act_rule; |
| 2598 | enum act_return act_ret; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2599 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2600 | act_rule =rule->action_kw.rule; |
| 2601 | act_ret = act_rule->action_ptr(act_rule, check->proxy, check->sess, NULL, 0); |
| 2602 | if (act_ret != ACT_RET_CONT) { |
| 2603 | chunk_printf(&trash, "TCPCHK ACTION unexpected result at step %d\n", |
| 2604 | tcpcheck_get_step_id(check, rule)); |
| 2605 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area); |
| 2606 | ret = TCPCHK_EVAL_STOP; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2607 | } |
| 2608 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2609 | return ret; |
| 2610 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2611 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2612 | /* Executes a tcp-check ruleset. Note that this is called both from the |
| 2613 | * connection's wake() callback and from the check scheduling task. It returns |
| 2614 | * 0 on normal cases, or <0 if a close() has happened on an existing connection, |
| 2615 | * presenting the risk of an fd replacement. |
| 2616 | * |
| 2617 | * Please do NOT place any return statement in this function and only leave |
| 2618 | * via the out_end_tcpcheck label after setting retcode. |
| 2619 | */ |
| 2620 | static int tcpcheck_main(struct check *check) |
| 2621 | { |
| 2622 | struct tcpcheck_rule *rule; |
| 2623 | struct conn_stream *cs = check->cs; |
| 2624 | struct connection *conn = cs_conn(cs); |
| 2625 | int must_read = 1, last_read = 0; |
| 2626 | int ret, retcode = 0; |
Christopher Faulet | 030ed4b | 2020-05-28 13:58:34 +0200 | [diff] [blame] | 2627 | enum tcpcheck_eval_ret eval_ret; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2628 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2629 | /* here, we know that the check is complete or that it failed */ |
| 2630 | if (check->result != CHK_RES_UNKNOWN) |
Christopher Faulet | b693a0d | 2020-04-27 15:59:22 +0200 | [diff] [blame] | 2631 | goto out; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2632 | |
Christopher Faulet | b0b8b26 | 2020-05-26 12:14:22 +0200 | [diff] [blame] | 2633 | /* Note: the conn-stream and the connection may only be undefined before |
| 2634 | * the first rule evaluation (it is always a connect rule) or when the |
| 2635 | * conn-stream allocation failed on the first connect. |
| 2636 | */ |
| 2637 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2638 | /* 1- check for connection error, if any */ |
| 2639 | if ((conn && conn->flags & CO_FL_ERROR) || (cs && cs->flags & CS_FL_ERROR)) |
| 2640 | goto out_end_tcpcheck; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2641 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2642 | /* 2- check if we are waiting for the connection establishment. It only |
| 2643 | * happens during TCPCHK_ACT_CONNECT. */ |
| 2644 | if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) { |
Christopher Faulet | b0b8b26 | 2020-05-26 12:14:22 +0200 | [diff] [blame] | 2645 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Christopher Faulet | 97b2a8b | 2020-05-09 17:34:43 +0200 | [diff] [blame] | 2646 | struct tcpcheck_rule *next; |
| 2647 | |
| 2648 | next = get_next_tcpcheck_rule(check->tcpcheck_rules, check->current_step); |
| 2649 | if (next && next->action == TCPCHK_ACT_SEND) { |
Christopher Faulet | a2fb0c3 | 2020-05-09 17:27:43 +0200 | [diff] [blame] | 2650 | if (!(check->wait_list.events & SUB_RETRY_SEND)) |
| 2651 | conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list); |
Christopher Faulet | 030ed4b | 2020-05-28 13:58:34 +0200 | [diff] [blame] | 2652 | goto out; |
Christopher Faulet | a2fb0c3 | 2020-05-09 17:27:43 +0200 | [diff] [blame] | 2653 | } |
Christopher Faulet | 97b2a8b | 2020-05-09 17:34:43 +0200 | [diff] [blame] | 2654 | else { |
Christopher Faulet | 030ed4b | 2020-05-28 13:58:34 +0200 | [diff] [blame] | 2655 | eval_ret = tcpcheck_eval_recv(check, check->current_step); |
| 2656 | if (eval_ret == TCPCHK_EVAL_STOP) |
| 2657 | goto out_end_tcpcheck; |
| 2658 | else if (eval_ret == TCPCHK_EVAL_WAIT) |
| 2659 | goto out; |
| 2660 | last_read = ((conn->flags & CO_FL_ERROR) || (cs->flags & (CS_FL_ERROR|CS_FL_EOS))); |
| 2661 | must_read = 0; |
Christopher Faulet | a2fb0c3 | 2020-05-09 17:27:43 +0200 | [diff] [blame] | 2662 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2663 | } |
Christopher Faulet | 97b2a8b | 2020-05-09 17:34:43 +0200 | [diff] [blame] | 2664 | rule = LIST_NEXT(&check->current_step->list, typeof(rule), list); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2665 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2666 | |
| 2667 | /* 3- check for pending outgoing data. It only happens during |
| 2668 | * TCPCHK_ACT_SEND. */ |
| 2669 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) { |
Christopher Faulet | b0b8b26 | 2020-05-26 12:14:22 +0200 | [diff] [blame] | 2670 | if (b_data(&check->bo)) { |
Christopher Faulet | 0732134 | 2020-05-09 17:37:43 +0200 | [diff] [blame] | 2671 | /* We're already waiting to be able to send, give up */ |
| 2672 | if (check->wait_list.events & SUB_RETRY_SEND) |
| 2673 | goto out; |
| 2674 | |
Christopher Faulet | 6d47121 | 2020-04-22 11:09:25 +0200 | [diff] [blame] | 2675 | ret = conn->mux->snd_buf(cs, &check->bo, |
| 2676 | (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] | 2677 | if (ret <= 0) { |
Christopher Faulet | b0b8b26 | 2020-05-26 12:14:22 +0200 | [diff] [blame] | 2678 | if ((conn->flags & CO_FL_ERROR) || (cs->flags & CS_FL_ERROR)) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2679 | goto out_end_tcpcheck; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2680 | } |
Christopher Faulet | 6d47121 | 2020-04-22 11:09:25 +0200 | [diff] [blame] | 2681 | if ((IS_HTX_CONN(conn) && !htx_is_empty(htxbuf(&check->bo))) || b_data(&check->bo)) { |
Christopher Faulet | b0b8b26 | 2020-05-26 12:14:22 +0200 | [diff] [blame] | 2682 | conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2683 | goto out; |
| 2684 | } |
| 2685 | } |
| 2686 | rule = LIST_NEXT(&check->current_step->list, typeof(rule), list); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2687 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2688 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2689 | /* 4- check if a rule must be resume. It happens if check->current_step |
| 2690 | * is defined. */ |
| 2691 | else if (check->current_step) |
| 2692 | rule = check->current_step; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2693 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2694 | /* 5- It is the first evaluation. We must create a session and preset |
| 2695 | * tcp-check variables */ |
| 2696 | else { |
| 2697 | struct tcpcheck_var *var; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2698 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2699 | /* First evaluation, create a session */ |
| 2700 | check->sess = session_new(&checks_fe, NULL, &check->obj_type); |
| 2701 | if (!check->sess) { |
| 2702 | chunk_printf(&trash, "TCPCHK error allocating check session"); |
| 2703 | set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.area); |
| 2704 | goto out_end_tcpcheck; |
| 2705 | } |
| 2706 | vars_init(&check->vars, SCOPE_CHECK); |
| 2707 | rule = LIST_NEXT(check->tcpcheck_rules->list, typeof(rule), list); |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2708 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2709 | /* Preset tcp-check variables */ |
| 2710 | list_for_each_entry(var, &check->tcpcheck_rules->preset_vars, list) { |
| 2711 | struct sample smp; |
Willy Tarreau | 449f952 | 2015-05-13 15:39:48 +0200 | [diff] [blame] | 2712 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2713 | memset(&smp, 0, sizeof(smp)); |
| 2714 | smp_set_owner(&smp, check->proxy, check->sess, NULL, SMP_OPT_FINAL); |
| 2715 | smp.data = var->data; |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2716 | vars_set_by_name_ifexist(istptr(var->name), istlen(var->name), &smp); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2717 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2718 | } |
| 2719 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2720 | /* Now evaluate the tcp-check rules */ |
Willy Tarreau | deccd11 | 2018-06-14 18:38:55 +0200 | [diff] [blame] | 2721 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2722 | list_for_each_entry_from(rule, check->tcpcheck_rules->list, list) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2723 | check->code = 0; |
| 2724 | switch (rule->action) { |
| 2725 | case TCPCHK_ACT_CONNECT: |
| 2726 | check->current_step = rule; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2727 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2728 | /* close but not release yet previous connection */ |
| 2729 | if (check->cs) { |
| 2730 | cs_close(check->cs); |
| 2731 | retcode = -1; /* do not reuse the fd in the caller! */ |
| 2732 | } |
| 2733 | eval_ret = tcpcheck_eval_connect(check, rule); |
Christopher Faulet | 3cbdd22 | 2020-05-26 11:14:50 +0200 | [diff] [blame] | 2734 | |
| 2735 | /* Refresh conn-stream and connection */ |
| 2736 | cs = check->cs; |
| 2737 | conn = cs_conn(cs); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2738 | must_read = 1; last_read = 0; |
| 2739 | break; |
| 2740 | case TCPCHK_ACT_SEND: |
| 2741 | check->current_step = rule; |
| 2742 | eval_ret = tcpcheck_eval_send(check, rule); |
| 2743 | must_read = 1; |
| 2744 | break; |
| 2745 | case TCPCHK_ACT_EXPECT: |
| 2746 | check->current_step = rule; |
| 2747 | if (must_read) { |
| 2748 | if (check->proxy->timeout.check) |
| 2749 | check->task->expire = tick_add_ifset(now_ms, check->proxy->timeout.check); |
Willy Tarreau | f3d3482 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 2750 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2751 | eval_ret = tcpcheck_eval_recv(check, rule); |
| 2752 | if (eval_ret == TCPCHK_EVAL_STOP) |
| 2753 | goto out_end_tcpcheck; |
| 2754 | else if (eval_ret == TCPCHK_EVAL_WAIT) |
| 2755 | goto out; |
| 2756 | last_read = ((conn->flags & CO_FL_ERROR) || (cs->flags & (CS_FL_ERROR|CS_FL_EOS))); |
| 2757 | must_read = 0; |
| 2758 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2759 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2760 | eval_ret = ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK |
| 2761 | ? tcpcheck_eval_expect_http(check, rule, last_read) |
| 2762 | : tcpcheck_eval_expect(check, rule, last_read)); |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 2763 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2764 | if (eval_ret == TCPCHK_EVAL_WAIT) { |
| 2765 | check->current_step = rule->expect.head; |
Christopher Faulet | a2fb0c3 | 2020-05-09 17:27:43 +0200 | [diff] [blame] | 2766 | if (!(check->wait_list.events & SUB_RETRY_RECV)) |
| 2767 | conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2768 | } |
| 2769 | break; |
| 2770 | case TCPCHK_ACT_ACTION_KW: |
| 2771 | /* Don't update the current step */ |
| 2772 | eval_ret = tcpcheck_eval_action_kw(check, rule); |
| 2773 | break; |
| 2774 | default: |
| 2775 | /* Otherwise, just go to the next one and don't update |
| 2776 | * the current step |
| 2777 | */ |
| 2778 | eval_ret = TCPCHK_EVAL_CONTINUE; |
| 2779 | break; |
| 2780 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 2781 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2782 | switch (eval_ret) { |
| 2783 | case TCPCHK_EVAL_CONTINUE: |
| 2784 | break; |
| 2785 | case TCPCHK_EVAL_WAIT: |
| 2786 | goto out; |
| 2787 | case TCPCHK_EVAL_STOP: |
| 2788 | goto out_end_tcpcheck; |
| 2789 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 2790 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2791 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2792 | /* All rules was evaluated */ |
| 2793 | if (check->current_step) { |
| 2794 | rule = check->current_step; |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 2795 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2796 | if (rule->action == TCPCHK_ACT_EXPECT) { |
| 2797 | struct buffer *msg; |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 2798 | enum healthcheck_status status; |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 2799 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2800 | if (check->server && |
| 2801 | (check->server->proxy->options & PR_O_DISABLE404) && |
| 2802 | (check->server->next_state != SRV_ST_STOPPED) && |
| 2803 | (check->code == 404)) { |
| 2804 | set_server_check_status(check, HCHK_STATUS_L7OKCD, NULL); |
| 2805 | goto out_end_tcpcheck; |
| 2806 | } |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 2807 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2808 | msg = alloc_trash_chunk(); |
| 2809 | if (msg) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2810 | tcpcheck_expect_onsuccess_message(msg, check, rule, IST_NULL); |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 2811 | status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD); |
| 2812 | set_server_check_status(check, status, (msg ? b_head(msg) : "(tcp-check)")); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2813 | free_trash_chunk(msg); |
| 2814 | } |
| 2815 | else if (rule->action == TCPCHK_ACT_CONNECT) { |
| 2816 | const char *msg = ((rule->connect.options & TCPCHK_OPT_IMPLICIT) ? NULL : "(tcp-check)"); |
Christopher Faulet | f73f5cc | 2020-04-27 12:06:55 +0200 | [diff] [blame] | 2817 | enum healthcheck_status status = HCHK_STATUS_L4OK; |
| 2818 | #ifdef USE_OPENSSL |
Christopher Faulet | b0b8b26 | 2020-05-26 12:14:22 +0200 | [diff] [blame] | 2819 | if (ssl_sock_is_ssl(conn)) |
Christopher Faulet | f73f5cc | 2020-04-27 12:06:55 +0200 | [diff] [blame] | 2820 | status = HCHK_STATUS_L6OK; |
| 2821 | #endif |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2822 | set_server_check_status(check, status, msg); |
| 2823 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2824 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2825 | else |
| 2826 | set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)"); |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2827 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2828 | out_end_tcpcheck: |
| 2829 | if ((conn && conn->flags & CO_FL_ERROR) || (cs && cs->flags & CS_FL_ERROR)) |
| 2830 | chk_report_conn_err(check, errno, 0); |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2831 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2832 | out: |
| 2833 | return retcode; |
| 2834 | } |
Christopher Faulet | bb591a1 | 2020-04-01 16:52:17 +0200 | [diff] [blame] | 2835 | |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2836 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2837 | /**************************************************************************/ |
| 2838 | /************** Health-checks based on an external process ****************/ |
| 2839 | /**************************************************************************/ |
| 2840 | static struct list pid_list = LIST_HEAD_INIT(pid_list); |
| 2841 | static struct pool_head *pool_head_pid_list; |
| 2842 | __decl_spinlock(pid_list_lock); |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2843 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2844 | struct extcheck_env { |
| 2845 | char *name; /* environment variable name */ |
| 2846 | int vmaxlen; /* value maximum length, used to determine the required memory allocation */ |
| 2847 | }; |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2848 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2849 | /* environment variables memory requirement for different types of data */ |
| 2850 | #define EXTCHK_SIZE_EVAL_INIT 0 /* size determined during the init phase, |
| 2851 | * such environment variables are not updatable. */ |
| 2852 | #define EXTCHK_SIZE_ULONG 20 /* max string length for an unsigned long value */ |
| 2853 | #define EXTCHK_SIZE_UINT 11 /* max string length for an unsigned int value */ |
| 2854 | #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] | 2855 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2856 | /* external checks environment variables */ |
| 2857 | enum { |
| 2858 | EXTCHK_PATH = 0, |
Willy Tarreau | ca79f59 | 2019-07-17 19:04:47 +0200 | [diff] [blame] | 2859 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2860 | /* Proxy specific environment variables */ |
| 2861 | EXTCHK_HAPROXY_PROXY_NAME, /* the backend name */ |
| 2862 | EXTCHK_HAPROXY_PROXY_ID, /* the backend id */ |
| 2863 | EXTCHK_HAPROXY_PROXY_ADDR, /* the first bind address if available (or empty) */ |
| 2864 | EXTCHK_HAPROXY_PROXY_PORT, /* the first bind port if available (or empty) */ |
Christopher Faulet | bb591a1 | 2020-04-01 16:52:17 +0200 | [diff] [blame] | 2865 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2866 | /* Server specific environment variables */ |
| 2867 | EXTCHK_HAPROXY_SERVER_NAME, /* the server name */ |
| 2868 | EXTCHK_HAPROXY_SERVER_ID, /* the server id */ |
| 2869 | EXTCHK_HAPROXY_SERVER_ADDR, /* the server address */ |
| 2870 | EXTCHK_HAPROXY_SERVER_PORT, /* the server port if available (or empty) */ |
| 2871 | EXTCHK_HAPROXY_SERVER_MAXCONN, /* the server max connections */ |
| 2872 | EXTCHK_HAPROXY_SERVER_CURCONN, /* the current number of connections on the server */ |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2873 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2874 | EXTCHK_SIZE |
| 2875 | }; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2876 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2877 | const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = { |
| 2878 | [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT }, |
| 2879 | [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT }, |
| 2880 | [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT }, |
| 2881 | [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT }, |
| 2882 | [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT }, |
| 2883 | [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT }, |
| 2884 | [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT }, |
| 2885 | [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_ADDR }, |
| 2886 | [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_UINT }, |
| 2887 | [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT }, |
| 2888 | [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG }, |
| 2889 | }; |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 2890 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2891 | void block_sigchld(void) |
| 2892 | { |
| 2893 | sigset_t set; |
| 2894 | sigemptyset(&set); |
| 2895 | sigaddset(&set, SIGCHLD); |
| 2896 | assert(ha_sigmask(SIG_BLOCK, &set, NULL) == 0); |
| 2897 | } |
Willy Tarreau | be37315 | 2018-09-06 11:45:30 +0200 | [diff] [blame] | 2898 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2899 | void unblock_sigchld(void) |
| 2900 | { |
| 2901 | sigset_t set; |
| 2902 | sigemptyset(&set); |
| 2903 | sigaddset(&set, SIGCHLD); |
| 2904 | assert(ha_sigmask(SIG_UNBLOCK, &set, NULL) == 0); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2905 | } |
Willy Tarreau | 2ab5c38 | 2019-07-17 18:48:07 +0200 | [diff] [blame] | 2906 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2907 | 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] | 2908 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2909 | struct pid_list *elem; |
| 2910 | struct check *check = t->context; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2911 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2912 | elem = pool_alloc(pool_head_pid_list); |
| 2913 | if (!elem) |
| 2914 | return NULL; |
| 2915 | elem->pid = pid; |
| 2916 | elem->t = t; |
| 2917 | elem->exited = 0; |
| 2918 | check->curpid = elem; |
| 2919 | LIST_INIT(&elem->list); |
Gaetan Rivet | 08fdcb3 | 2020-02-28 11:04:21 +0100 | [diff] [blame] | 2920 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2921 | HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock); |
| 2922 | LIST_ADD(&pid_list, &elem->list); |
| 2923 | HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock); |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2924 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2925 | return elem; |
| 2926 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2927 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2928 | static void pid_list_del(struct pid_list *elem) |
| 2929 | { |
| 2930 | struct check *check; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2931 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2932 | if (!elem) |
| 2933 | return; |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2934 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2935 | HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock); |
| 2936 | LIST_DEL(&elem->list); |
| 2937 | HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock); |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2938 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2939 | if (!elem->exited) |
| 2940 | kill(elem->pid, SIGTERM); |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2941 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2942 | check = elem->t->context; |
| 2943 | check->curpid = NULL; |
| 2944 | pool_free(pool_head_pid_list, elem); |
| 2945 | } |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2946 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2947 | /* Called from inside SIGCHLD handler, SIGCHLD is blocked */ |
| 2948 | static void pid_list_expire(pid_t pid, int status) |
| 2949 | { |
| 2950 | struct pid_list *elem; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2951 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2952 | HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock); |
| 2953 | list_for_each_entry(elem, &pid_list, list) { |
| 2954 | if (elem->pid == pid) { |
| 2955 | elem->t->expire = now_ms; |
| 2956 | elem->status = status; |
| 2957 | elem->exited = 1; |
| 2958 | task_wakeup(elem->t, TASK_WOKEN_IO); |
| 2959 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2960 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2961 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2962 | HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock); |
| 2963 | } |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 2964 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2965 | static void sigchld_handler(struct sig_handler *sh) |
| 2966 | { |
| 2967 | pid_t pid; |
| 2968 | int status; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2969 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2970 | while ((pid = waitpid(0, &status, WNOHANG)) > 0) |
| 2971 | pid_list_expire(pid, status); |
| 2972 | } |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 2973 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2974 | static int init_pid_list(void) |
| 2975 | { |
| 2976 | if (pool_head_pid_list != NULL) |
| 2977 | /* Nothing to do */ |
| 2978 | return 0; |
| 2979 | |
| 2980 | if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) { |
| 2981 | ha_alert("Failed to set signal handler for external health checks: %s. Aborting.\n", |
| 2982 | strerror(errno)); |
| 2983 | return 1; |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2984 | } |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 2985 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2986 | pool_head_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED); |
| 2987 | if (pool_head_pid_list == NULL) { |
| 2988 | ha_alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n", |
| 2989 | strerror(errno)); |
| 2990 | return 1; |
| 2991 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2992 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2993 | return 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2994 | } |
| 2995 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2996 | /* helper macro to set an environment variable and jump to a specific label on failure. */ |
| 2997 | #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] | 2998 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2999 | /* |
| 3000 | * helper function to allocate enough memory to store an environment variable. |
| 3001 | * It will also check that the environment variable is updatable, and silently |
| 3002 | * fail if not. |
| 3003 | */ |
| 3004 | static int extchk_setenv(struct check *check, int idx, const char *value) |
| 3005 | { |
| 3006 | int len, ret; |
| 3007 | char *envname; |
| 3008 | int vmaxlen; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 3009 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3010 | if (idx < 0 || idx >= EXTCHK_SIZE) { |
| 3011 | ha_alert("Illegal environment variable index %d. Aborting.\n", idx); |
| 3012 | return 1; |
| 3013 | } |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 3014 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3015 | envname = extcheck_envs[idx].name; |
| 3016 | vmaxlen = extcheck_envs[idx].vmaxlen; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 3017 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3018 | /* Check if the environment variable is already set, and silently reject |
| 3019 | * the update if this one is not updatable. */ |
| 3020 | if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx])) |
| 3021 | return 0; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 3022 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3023 | /* Instead of sending NOT_USED, sending an empty value is preferable */ |
| 3024 | if (strcmp(value, "NOT_USED") == 0) { |
| 3025 | value = ""; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 3026 | } |
| 3027 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3028 | len = strlen(envname) + 1; |
| 3029 | if (vmaxlen == EXTCHK_SIZE_EVAL_INIT) |
| 3030 | len += strlen(value); |
| 3031 | else |
| 3032 | len += vmaxlen; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 3033 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3034 | if (!check->envp[idx]) |
| 3035 | check->envp[idx] = malloc(len + 1); |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 3036 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3037 | if (!check->envp[idx]) { |
| 3038 | ha_alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname); |
| 3039 | return 1; |
| 3040 | } |
| 3041 | ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value); |
| 3042 | if (ret < 0) { |
| 3043 | ha_alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno)); |
| 3044 | return 1; |
| 3045 | } |
| 3046 | else if (ret > len) { |
| 3047 | ha_alert("Environment variable '%s' was truncated. Aborting.\n", envname); |
| 3048 | return 1; |
| 3049 | } |
| 3050 | return 0; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 3051 | } |
| 3052 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3053 | static int prepare_external_check(struct check *check) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3054 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3055 | struct server *s = check->server; |
| 3056 | struct proxy *px = s->proxy; |
| 3057 | struct listener *listener = NULL, *l; |
| 3058 | int i; |
| 3059 | const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH; |
| 3060 | char buf[256]; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3061 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3062 | list_for_each_entry(l, &px->conf.listeners, by_fe) |
| 3063 | /* Use the first INET, INET6 or UNIX listener */ |
| 3064 | if (l->addr.ss_family == AF_INET || |
| 3065 | l->addr.ss_family == AF_INET6 || |
| 3066 | l->addr.ss_family == AF_UNIX) { |
| 3067 | listener = l; |
| 3068 | break; |
| 3069 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3070 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3071 | check->curpid = NULL; |
| 3072 | check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *)); |
| 3073 | if (!check->envp) { |
| 3074 | ha_alert("Failed to allocate memory for environment variables. Aborting\n"); |
| 3075 | goto err; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3076 | } |
| 3077 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3078 | check->argv = calloc(6, sizeof(char *)); |
| 3079 | if (!check->argv) { |
| 3080 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 3081 | goto err; |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 3082 | } |
| 3083 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3084 | check->argv[0] = px->check_command; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3085 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3086 | if (!listener) { |
| 3087 | check->argv[1] = strdup("NOT_USED"); |
| 3088 | check->argv[2] = strdup("NOT_USED"); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3089 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3090 | else if (listener->addr.ss_family == AF_INET || |
| 3091 | listener->addr.ss_family == AF_INET6) { |
| 3092 | addr_to_str(&listener->addr, buf, sizeof(buf)); |
| 3093 | check->argv[1] = strdup(buf); |
| 3094 | port_to_str(&listener->addr, buf, sizeof(buf)); |
| 3095 | check->argv[2] = strdup(buf); |
| 3096 | } |
| 3097 | else if (listener->addr.ss_family == AF_UNIX) { |
| 3098 | const struct sockaddr_un *un; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3099 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3100 | un = (struct sockaddr_un *)&listener->addr; |
| 3101 | check->argv[1] = strdup(un->sun_path); |
| 3102 | check->argv[2] = strdup("NOT_USED"); |
| 3103 | } |
| 3104 | else { |
| 3105 | ha_alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id); |
| 3106 | goto err; |
| 3107 | } |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 3108 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3109 | if (!check->argv[1] || !check->argv[2]) { |
| 3110 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 3111 | goto err; |
| 3112 | } |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 3113 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3114 | check->argv[3] = calloc(EXTCHK_SIZE_ADDR, sizeof(*check->argv[3])); |
| 3115 | check->argv[4] = calloc(EXTCHK_SIZE_UINT, sizeof(*check->argv[4])); |
| 3116 | if (!check->argv[3] || !check->argv[4]) { |
| 3117 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 3118 | goto err; |
| 3119 | } |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 3120 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3121 | addr_to_str(&s->addr, check->argv[3], EXTCHK_SIZE_ADDR); |
| 3122 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
| 3123 | snprintf(check->argv[4], EXTCHK_SIZE_UINT, "%u", s->svc_port); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3124 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3125 | for (i = 0; i < 5; i++) { |
| 3126 | if (!check->argv[i]) { |
| 3127 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 3128 | goto err; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3129 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3130 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3131 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3132 | EXTCHK_SETENV(check, EXTCHK_PATH, path, err); |
| 3133 | /* Add proxy environment variables */ |
| 3134 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err); |
| 3135 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err); |
| 3136 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err); |
| 3137 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err); |
| 3138 | /* Add server environment variables */ |
| 3139 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err); |
| 3140 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err); |
| 3141 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err); |
| 3142 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err); |
| 3143 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err); |
| 3144 | 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] | 3145 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3146 | /* Ensure that we don't leave any hole in check->envp */ |
| 3147 | for (i = 0; i < EXTCHK_SIZE; i++) |
| 3148 | if (!check->envp[i]) |
| 3149 | EXTCHK_SETENV(check, i, "", err); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3150 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3151 | return 1; |
| 3152 | err: |
| 3153 | if (check->envp) { |
| 3154 | for (i = 0; i < EXTCHK_SIZE; i++) |
| 3155 | free(check->envp[i]); |
| 3156 | free(check->envp); |
| 3157 | check->envp = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3158 | } |
| 3159 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3160 | if (check->argv) { |
| 3161 | for (i = 1; i < 5; i++) |
| 3162 | free(check->argv[i]); |
| 3163 | free(check->argv); |
| 3164 | check->argv = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3165 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3166 | return 0; |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3167 | } |
Gaetan Rivet | f8ba677 | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3168 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3169 | /* |
| 3170 | * establish a server health-check that makes use of a process. |
| 3171 | * |
| 3172 | * It can return one of : |
| 3173 | * - SF_ERR_NONE if everything's OK |
| 3174 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 3175 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
| 3176 | * |
| 3177 | * Blocks and then unblocks SIGCHLD |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3178 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3179 | static int connect_proc_chk(struct task *t) |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3180 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3181 | char buf[256]; |
| 3182 | struct check *check = t->context; |
| 3183 | struct server *s = check->server; |
| 3184 | struct proxy *px = s->proxy; |
| 3185 | int status; |
| 3186 | pid_t pid; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 3187 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3188 | status = SF_ERR_RESOURCE; |
Willy Tarreau | f2c8735 | 2015-05-13 12:08:21 +0200 | [diff] [blame] | 3189 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3190 | block_sigchld(); |
Willy Tarreau | 7df8ca6 | 2019-07-15 10:57:51 +0200 | [diff] [blame] | 3191 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3192 | pid = fork(); |
| 3193 | if (pid < 0) { |
| 3194 | ha_alert("Failed to fork process for external health check%s: %s. Aborting.\n", |
| 3195 | (global.tune.options & GTUNE_INSECURE_FORK) ? |
| 3196 | "" : " (likely caused by missing 'insecure-fork-wanted')", |
| 3197 | strerror(errno)); |
| 3198 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3199 | goto out; |
| 3200 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3201 | if (pid == 0) { |
| 3202 | /* Child */ |
| 3203 | extern char **environ; |
| 3204 | struct rlimit limit; |
| 3205 | int fd; |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3206 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3207 | /* close all FDs. Keep stdin/stdout/stderr in verbose mode */ |
| 3208 | fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 3209 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3210 | my_closefrom(fd); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3211 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3212 | /* restore the initial FD limits */ |
| 3213 | limit.rlim_cur = rlim_fd_cur_at_boot; |
| 3214 | limit.rlim_max = rlim_fd_max_at_boot; |
| 3215 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 3216 | getrlimit(RLIMIT_NOFILE, &limit); |
| 3217 | ha_warning("External check: failed to restore initial FD limits (cur=%u max=%u), using cur=%u max=%u\n", |
| 3218 | rlim_fd_cur_at_boot, rlim_fd_max_at_boot, |
| 3219 | (unsigned int)limit.rlim_cur, (unsigned int)limit.rlim_max); |
| 3220 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3221 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3222 | environ = check->envp; |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 3223 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3224 | /* Update some environment variables and command args: curconn, server addr and server port */ |
| 3225 | 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] | 3226 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3227 | addr_to_str(&s->addr, check->argv[3], EXTCHK_SIZE_ADDR); |
| 3228 | extchk_setenv(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3]); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3229 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3230 | *check->argv[4] = 0; |
| 3231 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
| 3232 | snprintf(check->argv[4], EXTCHK_SIZE_UINT, "%u", s->svc_port); |
| 3233 | extchk_setenv(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4]); |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 3234 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3235 | haproxy_unblock_signals(); |
| 3236 | execvp(px->check_command, check->argv); |
| 3237 | ha_alert("Failed to exec process for external health check: %s. Aborting.\n", |
| 3238 | strerror(errno)); |
| 3239 | exit(-1); |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 3240 | } |
| 3241 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3242 | /* Parent */ |
| 3243 | if (check->result == CHK_RES_UNKNOWN) { |
| 3244 | if (pid_list_add(pid, t) != NULL) { |
| 3245 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 3246 | |
| 3247 | if (px->timeout.check && px->timeout.connect) { |
| 3248 | int t_con = tick_add(now_ms, px->timeout.connect); |
| 3249 | t->expire = tick_first(t->expire, t_con); |
| 3250 | } |
| 3251 | status = SF_ERR_NONE; |
| 3252 | goto out; |
| 3253 | } |
| 3254 | else { |
| 3255 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 3256 | } |
| 3257 | kill(pid, SIGTERM); /* process creation error */ |
| 3258 | } |
| 3259 | else |
| 3260 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 3261 | |
| 3262 | out: |
| 3263 | unblock_sigchld(); |
| 3264 | return status; |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 3265 | } |
| 3266 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3267 | /* |
| 3268 | * manages a server health-check that uses an external process. Returns |
| 3269 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3270 | * |
| 3271 | * 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] | 3272 | * via the out_unlock label. |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3273 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3274 | 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] | 3275 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3276 | struct check *check = context; |
| 3277 | struct server *s = check->server; |
| 3278 | int rv; |
| 3279 | int ret; |
| 3280 | int expired = tick_is_expired(t->expire, now_ms); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3281 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3282 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 3283 | if (!(check->state & CHK_ST_INPROGRESS)) { |
| 3284 | /* no check currently running */ |
| 3285 | if (!expired) /* woke up too early */ |
| 3286 | goto out_unlock; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3287 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3288 | /* we don't send any health-checks when the proxy is |
| 3289 | * stopped, the server should not be checked or the check |
| 3290 | * is disabled. |
| 3291 | */ |
| 3292 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
| 3293 | s->proxy->state == PR_STSTOPPED) |
| 3294 | goto reschedule; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3295 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3296 | /* we'll initiate a new check */ |
| 3297 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Christopher Faulet | 370e0f1 | 2020-04-16 09:52:42 +0200 | [diff] [blame] | 3298 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3299 | check->state |= CHK_ST_INPROGRESS; |
| 3300 | |
| 3301 | ret = connect_proc_chk(t); |
| 3302 | if (ret == SF_ERR_NONE) { |
| 3303 | /* the process was forked, we allow up to min(inter, |
| 3304 | * timeout.connect) for it to report its status, but |
| 3305 | * only when timeout.check is set as it may be to short |
| 3306 | * for a full check otherwise. |
| 3307 | */ |
| 3308 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 3309 | |
| 3310 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 3311 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 3312 | t->expire = tick_first(t->expire, t_con); |
Christopher Faulet | 370e0f1 | 2020-04-16 09:52:42 +0200 | [diff] [blame] | 3313 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3314 | task_set_affinity(t, tid_bit); |
| 3315 | goto reschedule; |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3316 | } |
Gaetan Rivet | efab6c6 | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3317 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3318 | /* here, we failed to start the check */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3319 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3320 | check->state &= ~CHK_ST_INPROGRESS; |
| 3321 | check_notify_failure(check); |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3322 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3323 | /* we allow up to min(inter, timeout.connect) for a connection |
| 3324 | * to establish but only when timeout.check is set |
| 3325 | * as it may be to short for a full check otherwise |
| 3326 | */ |
| 3327 | while (tick_is_expired(t->expire, now_ms)) { |
| 3328 | int t_con; |
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 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 3331 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3332 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3333 | if (s->proxy->timeout.check) |
| 3334 | t->expire = tick_first(t->expire, t_con); |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3335 | } |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 3336 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3337 | else { |
| 3338 | /* there was a test running. |
| 3339 | * First, let's check whether there was an uncaught error, |
| 3340 | * which can happen on connect timeout or error. |
| 3341 | */ |
| 3342 | if (check->result == CHK_RES_UNKNOWN) { |
| 3343 | /* good connection is enough for pure TCP check */ |
| 3344 | struct pid_list *elem = check->curpid; |
| 3345 | int status = HCHK_STATUS_UNKNOWN; |
Christopher Faulet | 370e0f1 | 2020-04-16 09:52:42 +0200 | [diff] [blame] | 3346 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3347 | if (elem->exited) { |
| 3348 | status = elem->status; /* Save in case the process exits between use below */ |
| 3349 | if (!WIFEXITED(status)) |
| 3350 | check->code = -1; |
| 3351 | else |
| 3352 | check->code = WEXITSTATUS(status); |
| 3353 | if (!WIFEXITED(status) || WEXITSTATUS(status)) |
| 3354 | status = HCHK_STATUS_PROCERR; |
| 3355 | else |
| 3356 | status = HCHK_STATUS_PROCOK; |
| 3357 | } else if (expired) { |
| 3358 | status = HCHK_STATUS_PROCTOUT; |
| 3359 | ha_warning("kill %d\n", (int)elem->pid); |
| 3360 | kill(elem->pid, SIGTERM); |
| 3361 | } |
| 3362 | set_server_check_status(check, status, NULL); |
| 3363 | } |
Willy Tarreau | f2c8735 | 2015-05-13 12:08:21 +0200 | [diff] [blame] | 3364 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 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 | } |
| 3377 | task_set_affinity(t, 1); |
| 3378 | check->state &= ~CHK_ST_INPROGRESS; |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 3379 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3380 | pid_list_del(check->curpid); |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 3381 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 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)); |
| 3388 | } |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 3389 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3390 | reschedule: |
| 3391 | while (tick_is_expired(t->expire, now_ms)) |
| 3392 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3393 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3394 | out_unlock: |
| 3395 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
| 3396 | return t; |
| 3397 | } |
Baptiste Assmann | 248f117 | 2018-03-01 21:49:01 +0100 | [diff] [blame] | 3398 | |
Baptiste Assmann | 248f117 | 2018-03-01 21:49:01 +0100 | [diff] [blame] | 3399 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3400 | /**************************************************************************/ |
| 3401 | /***************** Health-checks based on connections *********************/ |
| 3402 | /**************************************************************************/ |
| 3403 | /* This function is used only for server health-checks. It handles connection |
| 3404 | * status updates including errors. If necessary, it wakes the check task up. |
| 3405 | * It returns 0 on normal cases, <0 if at least one close() has happened on the |
| 3406 | * connection (eg: reconnect). It relies on tcpcheck_main(). |
| 3407 | */ |
| 3408 | static int wake_srv_chk(struct conn_stream *cs) |
| 3409 | { |
| 3410 | struct connection *conn = cs->conn; |
| 3411 | struct check *check = cs->data; |
| 3412 | struct email_alertq *q = container_of(check, typeof(*q), check); |
| 3413 | int ret = 0; |
Christopher Faulet | df38f88 | 2020-04-07 16:04:38 +0200 | [diff] [blame] | 3414 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3415 | if (check->server) |
| 3416 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 3417 | else |
| 3418 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3419 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3420 | /* we may have to make progress on the TCP checks */ |
| 3421 | ret = tcpcheck_main(check); |
Christopher Faulet | df38f88 | 2020-04-07 16:04:38 +0200 | [diff] [blame] | 3422 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3423 | cs = check->cs; |
| 3424 | conn = cs->conn; |
Christopher Faulet | df38f88 | 2020-04-07 16:04:38 +0200 | [diff] [blame] | 3425 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3426 | if (unlikely(conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)) { |
| 3427 | /* We may get error reports bypassing the I/O handlers, typically |
| 3428 | * the case when sending a pure TCP check which fails, then the I/O |
| 3429 | * handlers above are not called. This is completely handled by the |
| 3430 | * main processing task so let's simply wake it up. If we get here, |
| 3431 | * we expect errno to still be valid. |
| 3432 | */ |
| 3433 | chk_report_conn_err(check, errno, 0); |
| 3434 | task_wakeup(check->task, TASK_WOKEN_IO); |
| 3435 | } |
| 3436 | |
| 3437 | if (check->result != CHK_RES_UNKNOWN) { |
| 3438 | /* Check complete or aborted. If connection not yet closed do it |
| 3439 | * now and wake the check task up to be sure the result is |
| 3440 | * handled ASAP. */ |
| 3441 | conn_sock_drain(conn); |
| 3442 | cs_close(cs); |
| 3443 | ret = -1; |
| 3444 | /* We may have been scheduled to run, and the |
| 3445 | * I/O handler expects to have a cs, so remove |
| 3446 | * the tasklet |
| 3447 | */ |
| 3448 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
| 3449 | task_wakeup(check->task, TASK_WOKEN_IO); |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 3450 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3451 | |
| 3452 | if (check->server) |
| 3453 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 3454 | else |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3455 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3456 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3457 | /* if a connection got replaced, we must absolutely prevent the connection |
| 3458 | * handler from touching its fd, and perform the FD polling updates ourselves |
| 3459 | */ |
| 3460 | if (ret < 0) |
| 3461 | conn_cond_update_polling(conn); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3462 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3463 | return ret; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3464 | } |
| 3465 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3466 | /* This function checks if any I/O is wanted, and if so, attempts to do so */ |
| 3467 | 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] | 3468 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3469 | struct check *check = ctx; |
| 3470 | struct conn_stream *cs = check->cs; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3471 | |
Christopher Faulet | 3d5e121 | 2020-05-28 14:34:02 +0200 | [diff] [blame] | 3472 | wake_srv_chk(cs); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3473 | return NULL; |
Simon Horman | bfb5d33 | 2015-01-30 11:22:55 +0900 | [diff] [blame] | 3474 | } |
| 3475 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3476 | /* manages a server health-check that uses a connection. Returns |
| 3477 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 3478 | * |
| 3479 | * Please do NOT place any return statement in this function and only leave |
| 3480 | * via the out_unlock label. |
| 3481 | */ |
| 3482 | 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] | 3483 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3484 | struct check *check = context; |
| 3485 | struct proxy *proxy = check->proxy; |
| 3486 | struct conn_stream *cs = check->cs; |
| 3487 | struct connection *conn = cs_conn(cs); |
| 3488 | int rv; |
| 3489 | int expired = tick_is_expired(t->expire, now_ms); |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 3490 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3491 | if (check->server) |
| 3492 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 3493 | if (!(check->state & CHK_ST_INPROGRESS)) { |
| 3494 | /* no check currently running */ |
| 3495 | if (!expired) /* woke up too early */ |
| 3496 | goto out_unlock; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3497 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3498 | /* we don't send any health-checks when the proxy is |
| 3499 | * stopped, the server should not be checked or the check |
| 3500 | * is disabled. |
| 3501 | */ |
| 3502 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
| 3503 | proxy->state == PR_STSTOPPED) |
| 3504 | goto reschedule; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3505 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3506 | /* we'll initiate a new check */ |
| 3507 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3508 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3509 | check->state |= CHK_ST_INPROGRESS; |
| 3510 | b_reset(&check->bi); |
| 3511 | b_reset(&check->bo); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3512 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3513 | task_set_affinity(t, tid_bit); |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3514 | |
Christopher Faulet | 99ff105 | 2020-05-25 07:32:01 +0200 | [diff] [blame] | 3515 | check->current_step = NULL; |
| 3516 | tcpcheck_main(check); |
| 3517 | goto out_unlock; |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3518 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3519 | else { |
| 3520 | /* there was a test running. |
| 3521 | * First, let's check whether there was an uncaught error, |
| 3522 | * which can happen on connect timeout or error. |
| 3523 | */ |
| 3524 | if (check->result == CHK_RES_UNKNOWN) { |
| 3525 | if ((conn->flags & CO_FL_ERROR) || cs->flags & CS_FL_ERROR || expired) { |
| 3526 | chk_report_conn_err(check, 0, expired); |
| 3527 | } |
| 3528 | else |
| 3529 | goto out_unlock; /* timeout not reached, wait again */ |
| 3530 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3531 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3532 | /* check complete or aborted */ |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3533 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3534 | check->current_step = NULL; |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3535 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3536 | if (conn && conn->xprt) { |
| 3537 | /* The check was aborted and the connection was not yet closed. |
| 3538 | * This can happen upon timeout, or when an external event such |
| 3539 | * as a failed response coupled with "observe layer7" caused the |
| 3540 | * server state to be suddenly changed. |
| 3541 | */ |
| 3542 | conn_sock_drain(conn); |
| 3543 | cs_close(cs); |
| 3544 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3545 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3546 | if (cs) { |
| 3547 | if (check->wait_list.events) |
Christopher Faulet | 0b9376a | 2020-04-24 16:20:49 +0200 | [diff] [blame] | 3548 | cs->conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3549 | /* We may have been scheduled to run, and the |
| 3550 | * I/O handler expects to have a cs, so remove |
| 3551 | * the tasklet |
| 3552 | */ |
| 3553 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
| 3554 | cs_destroy(cs); |
| 3555 | cs = check->cs = NULL; |
| 3556 | conn = NULL; |
| 3557 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3558 | |
Christopher Faulet | b693a0d | 2020-04-27 15:59:22 +0200 | [diff] [blame] | 3559 | if (check->sess != NULL) { |
| 3560 | vars_prune(&check->vars, check->sess, NULL); |
| 3561 | session_free(check->sess); |
| 3562 | check->sess = NULL; |
| 3563 | } |
| 3564 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3565 | if (check->server) { |
| 3566 | if (check->result == CHK_RES_FAILED) { |
| 3567 | /* a failure or timeout detected */ |
| 3568 | check_notify_failure(check); |
| 3569 | } |
| 3570 | else if (check->result == CHK_RES_CONDPASS) { |
| 3571 | /* check is OK but asks for stopping mode */ |
| 3572 | check_notify_stopping(check); |
| 3573 | } |
| 3574 | else if (check->result == CHK_RES_PASSED) { |
| 3575 | /* a success was detected */ |
| 3576 | check_notify_success(check); |
| 3577 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3578 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3579 | task_set_affinity(t, MAX_THREADS_MASK); |
| 3580 | check->state &= ~CHK_ST_INPROGRESS; |
| 3581 | |
| 3582 | if (check->server) { |
| 3583 | rv = 0; |
| 3584 | if (global.spread_checks > 0) { |
| 3585 | rv = srv_getinter(check) * global.spread_checks / 100; |
| 3586 | rv -= (int) (2 * rv * (ha_random32() / 4294967295.0)); |
| 3587 | } |
| 3588 | 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] | 3589 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3590 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3591 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3592 | reschedule: |
| 3593 | while (tick_is_expired(t->expire, now_ms)) |
| 3594 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 3595 | out_unlock: |
| 3596 | if (check->server) |
| 3597 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
| 3598 | return t; |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3599 | } |
| 3600 | |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3601 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3602 | /**************************************************************************/ |
| 3603 | /******************* Internals to parse tcp-check rules *******************/ |
| 3604 | /**************************************************************************/ |
| 3605 | struct action_kw_list tcp_check_keywords = { |
| 3606 | .list = LIST_HEAD_INIT(tcp_check_keywords.list), |
| 3607 | }; |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3608 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3609 | /* Return the struct action_kw associated to a keyword */ |
| 3610 | static struct action_kw *action_kw_tcp_check_lookup(const char *kw) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3611 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3612 | return action_lookup(&tcp_check_keywords.list, kw); |
| 3613 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3614 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3615 | static void action_kw_tcp_check_build_list(struct buffer *chk) |
| 3616 | { |
| 3617 | action_build_list(&tcp_check_keywords.list, chk); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3618 | } |
| 3619 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3620 | /* Creates a tcp-check rule resulting from parsing a custom keyword. NULL is |
| 3621 | * returned on error. |
| 3622 | */ |
| 3623 | static struct tcpcheck_rule *parse_tcpcheck_action(char **args, int cur_arg, struct proxy *px, |
| 3624 | struct list *rules, struct action_kw *kw, |
| 3625 | const char *file, int line, char **errmsg) |
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 | struct tcpcheck_rule *chk = NULL; |
| 3628 | struct act_rule *actrule = NULL; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3629 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3630 | actrule = calloc(1, sizeof(*actrule)); |
| 3631 | if (!actrule) { |
| 3632 | memprintf(errmsg, "out of memory"); |
| 3633 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3634 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3635 | actrule->kw = kw; |
| 3636 | actrule->from = ACT_F_TCP_CHK; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3637 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3638 | cur_arg++; |
| 3639 | if (kw->parse((const char **)args, &cur_arg, px, actrule, errmsg) == ACT_RET_PRS_ERR) { |
| 3640 | memprintf(errmsg, "'%s' : %s", kw->kw, *errmsg); |
| 3641 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3642 | } |
| 3643 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3644 | chk = calloc(1, sizeof(*chk)); |
| 3645 | if (!chk) { |
| 3646 | memprintf(errmsg, "out of memory"); |
| 3647 | goto error; |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3648 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3649 | chk->action = TCPCHK_ACT_ACTION_KW; |
| 3650 | chk->action_kw.rule = actrule; |
| 3651 | return chk; |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3652 | |
| 3653 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3654 | free(actrule); |
| 3655 | return NULL; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3656 | } |
| 3657 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3658 | /* Parses and creates a tcp-check connect or an http-check connect rule. NULL is |
| 3659 | * returned on error. |
| 3660 | */ |
| 3661 | static struct tcpcheck_rule *parse_tcpcheck_connect(char **args, int cur_arg, struct proxy *px, struct list *rules, |
| 3662 | const char *file, int line, char **errmsg) |
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 | struct tcpcheck_rule *chk = NULL; |
| 3665 | struct sockaddr_storage *sk = NULL; |
| 3666 | char *comment = NULL, *sni = NULL, *alpn = NULL; |
| 3667 | struct sample_expr *port_expr = NULL; |
Christopher Faulet | edc6ed9 | 2020-04-23 16:27:59 +0200 | [diff] [blame] | 3668 | const struct mux_proto_list *mux_proto = NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3669 | unsigned short conn_opts = 0; |
| 3670 | long port = 0; |
| 3671 | int alpn_len = 0; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3672 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3673 | list_for_each_entry(chk, rules, list) { |
| 3674 | if (chk->action == TCPCHK_ACT_CONNECT) |
| 3675 | break; |
| 3676 | if (chk->action == TCPCHK_ACT_COMMENT || |
| 3677 | chk->action == TCPCHK_ACT_ACTION_KW || |
| 3678 | (chk->action == TCPCHK_ACT_SEND && (chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT))) |
| 3679 | continue; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3680 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3681 | memprintf(errmsg, "first step MUST also be a 'connect', " |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 3682 | "optionally preceded by a 'set-var', an 'unset-var' or a 'comment', " |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3683 | "when there is a 'connect' step in the tcp-check ruleset"); |
| 3684 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3685 | } |
| 3686 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3687 | cur_arg++; |
| 3688 | while (*(args[cur_arg])) { |
| 3689 | if (strcmp(args[cur_arg], "default") == 0) |
| 3690 | conn_opts |= TCPCHK_OPT_DEFAULT_CONNECT; |
| 3691 | else if (strcmp(args[cur_arg], "addr") == 0) { |
| 3692 | int port1, port2; |
| 3693 | struct protocol *proto; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3694 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3695 | if (!*(args[cur_arg+1])) { |
| 3696 | memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[cur_arg]); |
| 3697 | goto error; |
| 3698 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3699 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3700 | sk = str2sa_range(args[cur_arg+1], NULL, &port1, &port2, errmsg, NULL, NULL, 1); |
| 3701 | if (!sk) { |
| 3702 | memprintf(errmsg, "'%s' : %s.", args[cur_arg], *errmsg); |
| 3703 | goto error; |
| 3704 | } |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3705 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3706 | proto = protocol_by_family(sk->ss_family); |
| 3707 | if (!proto || !proto->connect) { |
| 3708 | memprintf(errmsg, "'%s' : connect() not supported for this address family.\n", |
| 3709 | args[cur_arg]); |
| 3710 | goto error; |
| 3711 | } |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3712 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3713 | if (port1 != port2) { |
| 3714 | memprintf(errmsg, "'%s' : port ranges and offsets are not allowed in '%s'\n", |
| 3715 | args[cur_arg], args[cur_arg+1]); |
| 3716 | goto error; |
| 3717 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3718 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3719 | cur_arg++; |
| 3720 | } |
| 3721 | else if (strcmp(args[cur_arg], "port") == 0) { |
| 3722 | const char *p, *end; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3723 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3724 | if (!*(args[cur_arg+1])) { |
| 3725 | memprintf(errmsg, "'%s' expects a port number or a sample expression as argument.", args[cur_arg]); |
| 3726 | goto error; |
| 3727 | } |
| 3728 | cur_arg++; |
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 | port = 0; |
| 3731 | release_sample_expr(port_expr); |
| 3732 | p = args[cur_arg]; end = p + strlen(p); |
| 3733 | port = read_uint(&p, end); |
| 3734 | if (p != end) { |
| 3735 | int idx = 0; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3736 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3737 | px->conf.args.ctx = ARGC_SRV; |
| 3738 | port_expr = sample_parse_expr((char *[]){args[cur_arg], NULL}, &idx, |
| 3739 | file, line, errmsg, &px->conf.args, NULL); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3740 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3741 | if (!port_expr) { |
| 3742 | memprintf(errmsg, "error detected while parsing port expression : %s", *errmsg); |
| 3743 | goto error; |
| 3744 | } |
| 3745 | if (!(port_expr->fetch->val & SMP_VAL_BE_CHK_RUL)) { |
| 3746 | memprintf(errmsg, "error detected while parsing port expression : " |
| 3747 | " fetch method '%s' extracts information from '%s', " |
| 3748 | "none of which is available here.\n", |
| 3749 | args[cur_arg], sample_src_names(port_expr->fetch->use)); |
| 3750 | goto error; |
| 3751 | } |
| 3752 | px->http_needed |= !!(port_expr->fetch->use & SMP_USE_HTTP_ANY); |
| 3753 | } |
| 3754 | else if (port > 65535 || port < 1) { |
| 3755 | memprintf(errmsg, "expects a valid TCP port (from range 1 to 65535) or a sample expression, got %s.", |
| 3756 | args[cur_arg]); |
| 3757 | goto error; |
| 3758 | } |
| 3759 | } |
Christopher Faulet | edc6ed9 | 2020-04-23 16:27:59 +0200 | [diff] [blame] | 3760 | else if (strcmp(args[cur_arg], "proto") == 0) { |
| 3761 | if (!*(args[cur_arg+1])) { |
| 3762 | memprintf(errmsg, "'%s' expects a MUX protocol as argument.", args[cur_arg]); |
| 3763 | goto error; |
| 3764 | } |
| 3765 | mux_proto = get_mux_proto(ist2(args[cur_arg+1], strlen(args[cur_arg+1]))); |
| 3766 | if (!mux_proto) { |
| 3767 | memprintf(errmsg, "'%s' : unknown MUX protocol '%s'.", args[cur_arg], args[cur_arg+1]); |
| 3768 | goto error; |
| 3769 | } |
| 3770 | cur_arg++; |
| 3771 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3772 | else if (strcmp(args[cur_arg], "comment") == 0) { |
| 3773 | if (!*(args[cur_arg+1])) { |
| 3774 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3775 | goto error; |
| 3776 | } |
| 3777 | cur_arg++; |
| 3778 | free(comment); |
| 3779 | comment = strdup(args[cur_arg]); |
| 3780 | if (!comment) { |
| 3781 | memprintf(errmsg, "out of memory"); |
| 3782 | goto error; |
| 3783 | } |
| 3784 | } |
| 3785 | else if (strcmp(args[cur_arg], "send-proxy") == 0) |
| 3786 | conn_opts |= TCPCHK_OPT_SEND_PROXY; |
| 3787 | else if (strcmp(args[cur_arg], "via-socks4") == 0) |
| 3788 | conn_opts |= TCPCHK_OPT_SOCKS4; |
| 3789 | else if (strcmp(args[cur_arg], "linger") == 0) |
| 3790 | conn_opts |= TCPCHK_OPT_LINGER; |
| 3791 | #ifdef USE_OPENSSL |
| 3792 | else if (strcmp(args[cur_arg], "ssl") == 0) { |
| 3793 | px->options |= PR_O_TCPCHK_SSL; |
| 3794 | conn_opts |= TCPCHK_OPT_SSL; |
| 3795 | } |
| 3796 | else if (strcmp(args[cur_arg], "sni") == 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(sni); |
| 3803 | sni = strdup(args[cur_arg]); |
| 3804 | if (!sni) { |
| 3805 | memprintf(errmsg, "out of memory"); |
| 3806 | goto error; |
| 3807 | } |
| 3808 | } |
| 3809 | else if (strcmp(args[cur_arg], "alpn") == 0) { |
| 3810 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 3811 | free(alpn); |
| 3812 | if (ssl_sock_parse_alpn(args[cur_arg + 1], &alpn, &alpn_len, errmsg)) { |
| 3813 | memprintf(errmsg, "'%s' : %s", args[cur_arg], *errmsg); |
| 3814 | goto error; |
| 3815 | } |
| 3816 | cur_arg++; |
| 3817 | #else |
| 3818 | 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] | 3819 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3820 | #endif |
| 3821 | } |
| 3822 | #endif /* USE_OPENSSL */ |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3823 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3824 | else { |
| 3825 | memprintf(errmsg, "expects 'comment', 'port', 'addr', 'send-proxy'" |
| 3826 | #ifdef USE_OPENSSL |
| 3827 | ", 'ssl', 'sni', 'alpn'" |
| 3828 | #endif /* USE_OPENSSL */ |
| 3829 | " or 'via-socks4', 'linger', 'default' but got '%s' as argument.", |
| 3830 | args[cur_arg]); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3831 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3832 | } |
| 3833 | cur_arg++; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3834 | } |
| 3835 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3836 | chk = calloc(1, sizeof(*chk)); |
| 3837 | if (!chk) { |
| 3838 | memprintf(errmsg, "out of memory"); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3839 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3840 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3841 | chk->action = TCPCHK_ACT_CONNECT; |
| 3842 | chk->comment = comment; |
| 3843 | chk->connect.port = port; |
| 3844 | chk->connect.options = conn_opts; |
| 3845 | chk->connect.sni = sni; |
| 3846 | chk->connect.alpn = alpn; |
| 3847 | chk->connect.alpn_len= alpn_len; |
| 3848 | chk->connect.port_expr= port_expr; |
Christopher Faulet | edc6ed9 | 2020-04-23 16:27:59 +0200 | [diff] [blame] | 3849 | chk->connect.mux_proto= mux_proto; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3850 | if (sk) |
| 3851 | chk->connect.addr = *sk; |
| 3852 | return chk; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3853 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3854 | error: |
| 3855 | free(alpn); |
| 3856 | free(sni); |
| 3857 | free(comment); |
| 3858 | release_sample_expr(port_expr); |
| 3859 | return NULL; |
| 3860 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3861 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3862 | /* Parses and creates a tcp-check send rule. NULL is returned on error */ |
| 3863 | static struct tcpcheck_rule *parse_tcpcheck_send(char **args, int cur_arg, struct proxy *px, struct list *rules, |
| 3864 | const char *file, int line, char **errmsg) |
| 3865 | { |
| 3866 | struct tcpcheck_rule *chk = NULL; |
| 3867 | char *comment = NULL, *data = NULL; |
| 3868 | enum tcpcheck_send_type type = TCPCHK_SEND_UNDEF; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3869 | |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 3870 | if (strcmp(args[cur_arg], "send-binary-lf") == 0) |
| 3871 | type = TCPCHK_SEND_BINARY_LF; |
| 3872 | else if (strcmp(args[cur_arg], "send-binary") == 0) |
| 3873 | type = TCPCHK_SEND_BINARY; |
| 3874 | else if (strcmp(args[cur_arg], "send-lf") == 0) |
| 3875 | type = TCPCHK_SEND_STRING_LF; |
| 3876 | else if (strcmp(args[cur_arg], "send") == 0) |
| 3877 | type = TCPCHK_SEND_STRING; |
| 3878 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3879 | if (!*(args[cur_arg+1])) { |
| 3880 | memprintf(errmsg, "'%s' expects a %s as argument", |
| 3881 | (type == TCPCHK_SEND_BINARY ? "binary string": "string"), args[cur_arg]); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3882 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3883 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3884 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3885 | data = args[cur_arg+1]; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3886 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3887 | cur_arg += 2; |
| 3888 | while (*(args[cur_arg])) { |
| 3889 | if (strcmp(args[cur_arg], "comment") == 0) { |
| 3890 | if (!*(args[cur_arg+1])) { |
| 3891 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3892 | goto error; |
| 3893 | } |
| 3894 | cur_arg++; |
| 3895 | free(comment); |
| 3896 | comment = strdup(args[cur_arg]); |
| 3897 | if (!comment) { |
| 3898 | memprintf(errmsg, "out of memory"); |
| 3899 | goto error; |
| 3900 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3901 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3902 | else { |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 3903 | memprintf(errmsg, "expects 'comment' but got '%s' as argument.", |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3904 | args[cur_arg]); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3905 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3906 | } |
| 3907 | cur_arg++; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3908 | } |
| 3909 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3910 | chk = calloc(1, sizeof(*chk)); |
| 3911 | if (!chk) { |
| 3912 | memprintf(errmsg, "out of memory"); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3913 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3914 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3915 | chk->action = TCPCHK_ACT_SEND; |
| 3916 | chk->comment = comment; |
| 3917 | chk->send.type = type; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3918 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3919 | switch (chk->send.type) { |
| 3920 | case TCPCHK_SEND_STRING: |
| 3921 | chk->send.data = ist2(strdup(data), strlen(data)); |
| 3922 | if (!isttest(chk->send.data)) { |
| 3923 | memprintf(errmsg, "out of memory"); |
| 3924 | goto error; |
| 3925 | } |
| 3926 | break; |
Christopher Faulet | 2edcd4c | 2020-04-28 10:39:50 +0200 | [diff] [blame] | 3927 | case TCPCHK_SEND_BINARY: { |
Christopher Faulet | 9c2cb2d | 2020-04-28 16:40:41 +0200 | [diff] [blame] | 3928 | int len = chk->send.data.len; |
Christopher Faulet | 2edcd4c | 2020-04-28 10:39:50 +0200 | [diff] [blame] | 3929 | if (parse_binary(data, &chk->send.data.ptr, &len, errmsg) == 0) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3930 | memprintf(errmsg, "'%s' invalid binary string (%s).\n", data, *errmsg); |
| 3931 | goto error; |
| 3932 | } |
Christopher Faulet | 2edcd4c | 2020-04-28 10:39:50 +0200 | [diff] [blame] | 3933 | chk->send.data.len = len; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3934 | break; |
Christopher Faulet | 2edcd4c | 2020-04-28 10:39:50 +0200 | [diff] [blame] | 3935 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3936 | case TCPCHK_SEND_STRING_LF: |
| 3937 | case TCPCHK_SEND_BINARY_LF: |
| 3938 | LIST_INIT(&chk->send.fmt); |
| 3939 | px->conf.args.ctx = ARGC_SRV; |
| 3940 | if (!parse_logformat_string(data, px, &chk->send.fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 3941 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", data, *errmsg); |
| 3942 | goto error; |
| 3943 | } |
| 3944 | break; |
| 3945 | case TCPCHK_SEND_HTTP: |
| 3946 | case TCPCHK_SEND_UNDEF: |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3947 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3948 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3949 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3950 | return chk; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3951 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3952 | error: |
| 3953 | free(chk); |
| 3954 | free(comment); |
| 3955 | return NULL; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3956 | } |
| 3957 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3958 | /* Parses and creates a http-check send rule. NULL is returned on error */ |
| 3959 | static struct tcpcheck_rule *parse_tcpcheck_send_http(char **args, int cur_arg, struct proxy *px, struct list *rules, |
| 3960 | const char *file, int line, char **errmsg) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3961 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3962 | struct tcpcheck_rule *chk = NULL; |
| 3963 | struct tcpcheck_http_hdr *hdr = NULL; |
| 3964 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 3965 | char *meth = NULL, *uri = NULL, *vsn = NULL; |
| 3966 | char *body = NULL, *comment = NULL; |
| 3967 | unsigned int flags = 0; |
Christopher Faulet | 13ec87b | 2020-04-29 11:45:44 +0200 | [diff] [blame] | 3968 | int i = 0, host_hdr = -1; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3969 | |
| 3970 | cur_arg++; |
| 3971 | while (*(args[cur_arg])) { |
| 3972 | if (strcmp(args[cur_arg], "meth") == 0) { |
| 3973 | if (!*(args[cur_arg+1])) { |
| 3974 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3975 | goto error; |
| 3976 | } |
| 3977 | cur_arg++; |
| 3978 | meth = args[cur_arg]; |
| 3979 | } |
Christopher Faulet | 7c95f5f | 2020-05-06 15:06:34 +0200 | [diff] [blame] | 3980 | else if (strcmp(args[cur_arg], "uri") == 0 || strcmp(args[cur_arg], "uri-lf") == 0) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3981 | if (!*(args[cur_arg+1])) { |
| 3982 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3983 | goto error; |
| 3984 | } |
Christopher Faulet | 7c95f5f | 2020-05-06 15:06:34 +0200 | [diff] [blame] | 3985 | flags &= ~TCPCHK_SND_HTTP_FL_URI_FMT; |
| 3986 | if (strcmp(args[cur_arg], "uri-lf") == 0) |
| 3987 | flags |= TCPCHK_SND_HTTP_FL_URI_FMT; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3988 | cur_arg++; |
| 3989 | uri = args[cur_arg]; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3990 | } |
Christopher Faulet | 907701b | 2020-04-28 09:37:00 +0200 | [diff] [blame] | 3991 | else if (strcmp(args[cur_arg], "ver") == 0) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 3992 | if (!*(args[cur_arg+1])) { |
| 3993 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 3994 | goto error; |
| 3995 | } |
| 3996 | cur_arg++; |
| 3997 | vsn = args[cur_arg]; |
| 3998 | } |
| 3999 | else if (strcmp(args[cur_arg], "hdr") == 0) { |
| 4000 | if (!*args[cur_arg+1] || !*args[cur_arg+2]) { |
| 4001 | memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg]); |
| 4002 | goto error; |
| 4003 | } |
Christopher Faulet | 13ec87b | 2020-04-29 11:45:44 +0200 | [diff] [blame] | 4004 | |
| 4005 | if (strcasecmp(args[cur_arg+1], "host") == 0) { |
| 4006 | if (host_hdr >= 0) { |
| 4007 | memprintf(errmsg, "'%s' header already defined (previous value is '%s')", |
| 4008 | args[cur_arg+1], istptr(hdrs[host_hdr].v)); |
| 4009 | goto error; |
| 4010 | } |
| 4011 | host_hdr = i; |
| 4012 | } |
Christopher Faulet | 7d765db | 2020-04-29 11:50:01 +0200 | [diff] [blame] | 4013 | else if (strcasecmp(args[cur_arg+1], "connection") == 0 || |
| 4014 | strcasecmp(args[cur_arg+1], "content-length") == 0 || |
| 4015 | strcasecmp(args[cur_arg+1], "transfer-encoding") == 0) |
| 4016 | goto skip_hdr; |
Christopher Faulet | 13ec87b | 2020-04-29 11:45:44 +0200 | [diff] [blame] | 4017 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4018 | hdrs[i].n = ist2(args[cur_arg+1], strlen(args[cur_arg+1])); |
| 4019 | hdrs[i].v = ist2(args[cur_arg+2], strlen(args[cur_arg+2])); |
| 4020 | i++; |
Christopher Faulet | 7d765db | 2020-04-29 11:50:01 +0200 | [diff] [blame] | 4021 | skip_hdr: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4022 | cur_arg += 2; |
| 4023 | } |
Christopher Faulet | 574e7bd | 2020-05-06 15:38:58 +0200 | [diff] [blame] | 4024 | else if (strcmp(args[cur_arg], "body") == 0 || strcmp(args[cur_arg], "body-lf") == 0) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4025 | if (!*(args[cur_arg+1])) { |
| 4026 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 4027 | goto error; |
| 4028 | } |
Christopher Faulet | 574e7bd | 2020-05-06 15:38:58 +0200 | [diff] [blame] | 4029 | flags &= ~TCPCHK_SND_HTTP_FL_BODY_FMT; |
| 4030 | if (strcmp(args[cur_arg], "body-lf") == 0) |
| 4031 | flags |= TCPCHK_SND_HTTP_FL_BODY_FMT; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4032 | cur_arg++; |
| 4033 | body = args[cur_arg]; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4034 | } |
| 4035 | else if (strcmp(args[cur_arg], "comment") == 0) { |
| 4036 | if (!*(args[cur_arg+1])) { |
| 4037 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 4038 | goto error; |
| 4039 | } |
| 4040 | cur_arg++; |
| 4041 | free(comment); |
| 4042 | comment = strdup(args[cur_arg]); |
| 4043 | if (!comment) { |
| 4044 | memprintf(errmsg, "out of memory"); |
| 4045 | goto error; |
| 4046 | } |
| 4047 | } |
| 4048 | else { |
Christopher Faulet | 574e7bd | 2020-05-06 15:38:58 +0200 | [diff] [blame] | 4049 | memprintf(errmsg, "expects 'comment', 'meth', 'uri', 'uri-lf', 'ver', 'hdr', 'body' or 'body-lf'" |
Christopher Faulet | 7c95f5f | 2020-05-06 15:06:34 +0200 | [diff] [blame] | 4050 | " but got '%s' as argument.", args[cur_arg]); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4051 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4052 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4053 | cur_arg++; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4054 | } |
| 4055 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4056 | hdrs[i].n = hdrs[i].v = IST_NULL; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4057 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4058 | chk = calloc(1, sizeof(*chk)); |
| 4059 | if (!chk) { |
| 4060 | memprintf(errmsg, "out of memory"); |
| 4061 | goto error; |
| 4062 | } |
| 4063 | chk->action = TCPCHK_ACT_SEND; |
| 4064 | chk->comment = comment; comment = NULL; |
| 4065 | chk->send.type = TCPCHK_SEND_HTTP; |
| 4066 | chk->send.http.flags = flags; |
| 4067 | LIST_INIT(&chk->send.http.hdrs); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4068 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4069 | if (meth) { |
| 4070 | chk->send.http.meth.meth = find_http_meth(meth, strlen(meth)); |
| 4071 | chk->send.http.meth.str.area = strdup(meth); |
| 4072 | chk->send.http.meth.str.data = strlen(meth); |
| 4073 | if (!chk->send.http.meth.str.area) { |
| 4074 | memprintf(errmsg, "out of memory"); |
| 4075 | goto error; |
| 4076 | } |
| 4077 | } |
| 4078 | if (uri) { |
Christopher Faulet | 7c95f5f | 2020-05-06 15:06:34 +0200 | [diff] [blame] | 4079 | if (chk->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT) { |
| 4080 | LIST_INIT(&chk->send.http.uri_fmt); |
| 4081 | px->conf.args.ctx = ARGC_SRV; |
| 4082 | if (!parse_logformat_string(uri, px, &chk->send.http.uri_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 4083 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", uri, *errmsg); |
| 4084 | goto error; |
| 4085 | } |
| 4086 | } |
| 4087 | else { |
| 4088 | chk->send.http.uri = ist2(strdup(uri), strlen(uri)); |
| 4089 | if (!isttest(chk->send.http.uri)) { |
| 4090 | memprintf(errmsg, "out of memory"); |
| 4091 | goto error; |
| 4092 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4093 | } |
| 4094 | } |
| 4095 | if (vsn) { |
| 4096 | chk->send.http.vsn = ist2(strdup(vsn), strlen(vsn)); |
| 4097 | if (!isttest(chk->send.http.vsn)) { |
| 4098 | memprintf(errmsg, "out of memory"); |
| 4099 | goto error; |
| 4100 | } |
| 4101 | } |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4102 | for (i = 0; istlen(hdrs[i].n); i++) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4103 | hdr = calloc(1, sizeof(*hdr)); |
| 4104 | if (!hdr) { |
| 4105 | memprintf(errmsg, "out of memory"); |
| 4106 | goto error; |
| 4107 | } |
| 4108 | LIST_INIT(&hdr->value); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4109 | hdr->name = istdup(hdrs[i].n); |
| 4110 | if (!isttest(hdr->name)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4111 | memprintf(errmsg, "out of memory"); |
| 4112 | goto error; |
| 4113 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4114 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4115 | ist0(hdrs[i].v); |
| 4116 | 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] | 4117 | goto error; |
| 4118 | LIST_ADDQ(&chk->send.http.hdrs, &hdr->list); |
| 4119 | hdr = NULL; |
| 4120 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4121 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4122 | if (body) { |
Christopher Faulet | 574e7bd | 2020-05-06 15:38:58 +0200 | [diff] [blame] | 4123 | if (chk->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT) { |
| 4124 | LIST_INIT(&chk->send.http.body_fmt); |
| 4125 | px->conf.args.ctx = ARGC_SRV; |
| 4126 | if (!parse_logformat_string(body, px, &chk->send.http.body_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 4127 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", body, *errmsg); |
| 4128 | goto error; |
| 4129 | } |
| 4130 | } |
| 4131 | else { |
| 4132 | chk->send.http.body = ist2(strdup(body), strlen(body)); |
| 4133 | if (!isttest(chk->send.http.body)) { |
| 4134 | memprintf(errmsg, "out of memory"); |
| 4135 | goto error; |
| 4136 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4137 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4138 | } |
| 4139 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4140 | return chk; |
| 4141 | |
| 4142 | error: |
| 4143 | free_tcpcheck_http_hdr(hdr); |
| 4144 | free_tcpcheck(chk, 0); |
| 4145 | free(comment); |
| 4146 | return NULL; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4147 | } |
| 4148 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4149 | /* Parses and creates a http-check comment rule. NULL is returned on error */ |
| 4150 | static struct tcpcheck_rule *parse_tcpcheck_comment(char **args, int cur_arg, struct proxy *px, struct list *rules, |
| 4151 | const char *file, int line, char **errmsg) |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 4152 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4153 | struct tcpcheck_rule *chk = NULL; |
| 4154 | char *comment = NULL; |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 4155 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4156 | if (!*(args[cur_arg+1])) { |
| 4157 | memprintf(errmsg, "expects a string as argument"); |
| 4158 | goto error; |
| 4159 | } |
| 4160 | cur_arg++; |
| 4161 | comment = strdup(args[cur_arg]); |
| 4162 | if (!comment) { |
| 4163 | memprintf(errmsg, "out of memory"); |
| 4164 | goto error; |
| 4165 | } |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 4166 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4167 | chk = calloc(1, sizeof(*chk)); |
| 4168 | if (!chk) { |
| 4169 | memprintf(errmsg, "out of memory"); |
| 4170 | goto error; |
| 4171 | } |
| 4172 | chk->action = TCPCHK_ACT_COMMENT; |
| 4173 | chk->comment = comment; |
| 4174 | return chk; |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 4175 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4176 | error: |
| 4177 | free(comment); |
| 4178 | return NULL; |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 4179 | } |
| 4180 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4181 | /* Parses and creates a tcp-check or an http-check expect rule. NULL is returned |
| 4182 | * on error. <proto> is set to the right protocol flags (covered by the |
| 4183 | * TCPCHK_RULES_PROTO_CHK mask). |
| 4184 | */ |
| 4185 | static struct tcpcheck_rule *parse_tcpcheck_expect(char **args, int cur_arg, struct proxy *px, |
| 4186 | struct list *rules, unsigned int proto, |
| 4187 | const char *file, int line, char **errmsg) |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 4188 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4189 | struct tcpcheck_rule *prev_check, *chk = NULL; |
| 4190 | struct sample_expr *status_expr = NULL; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4191 | char *on_success_msg, *on_error_msg, *comment, *pattern, *npat, *vpat; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4192 | enum tcpcheck_expect_type type = TCPCHK_EXPECT_UNDEF; |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 4193 | enum healthcheck_status ok_st = HCHK_STATUS_UNKNOWN; |
| 4194 | enum healthcheck_status err_st = HCHK_STATUS_UNKNOWN; |
| 4195 | enum healthcheck_status tout_st = HCHK_STATUS_UNKNOWN; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4196 | unsigned int flags = 0; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4197 | long min_recv = -1; |
Christopher Faulet | 88d939c | 2020-04-22 15:32:11 +0200 | [diff] [blame] | 4198 | int inverse = 0; |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 4199 | |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4200 | on_success_msg = on_error_msg = comment = pattern = npat = vpat = NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4201 | if (!*(args[cur_arg+1])) { |
| 4202 | memprintf(errmsg, "expects at least a matching pattern as arguments"); |
| 4203 | goto error; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4204 | } |
| 4205 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4206 | cur_arg++; |
| 4207 | while (*(args[cur_arg])) { |
| 4208 | int in_pattern = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4209 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4210 | rescan: |
| 4211 | if (strcmp(args[cur_arg], "min-recv") == 0) { |
| 4212 | if (in_pattern) { |
| 4213 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4214 | goto error; |
| 4215 | } |
| 4216 | if (!*(args[cur_arg+1])) { |
| 4217 | memprintf(errmsg, "'%s' expects a integer as argument", args[cur_arg]); |
| 4218 | goto error; |
| 4219 | } |
| 4220 | /* Use an signed integer here because of chksize */ |
| 4221 | cur_arg++; |
| 4222 | min_recv = atol(args[cur_arg]); |
| 4223 | if (min_recv < -1 || min_recv > INT_MAX) { |
| 4224 | memprintf(errmsg, "'%s' expects -1 or an integer from 0 to INT_MAX" , args[cur_arg-1]); |
| 4225 | goto error; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4226 | } |
| 4227 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4228 | else if (*(args[cur_arg]) == '!') { |
| 4229 | in_pattern = 1; |
| 4230 | while (*(args[cur_arg]) == '!') { |
| 4231 | inverse = !inverse; |
| 4232 | args[cur_arg]++; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4233 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4234 | if (!*(args[cur_arg])) |
| 4235 | cur_arg++; |
| 4236 | goto rescan; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4237 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4238 | else if (strcmp(args[cur_arg], "string") == 0 || strcmp(args[cur_arg], "rstring") == 0) { |
| 4239 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 4240 | memprintf(errmsg, "only on pattern expected"); |
| 4241 | goto error; |
| 4242 | } |
| 4243 | if (proto != TCPCHK_RULES_HTTP_CHK) |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 4244 | type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_STRING : TCPCHK_EXPECT_STRING_REGEX); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4245 | else |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 4246 | type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_HTTP_BODY : TCPCHK_EXPECT_HTTP_BODY_REGEX); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4247 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4248 | if (!*(args[cur_arg+1])) { |
| 4249 | memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]); |
| 4250 | goto error; |
| 4251 | } |
| 4252 | cur_arg++; |
| 4253 | pattern = args[cur_arg]; |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 4254 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4255 | else if (strcmp(args[cur_arg], "binary") == 0 || strcmp(args[cur_arg], "rbinary") == 0) { |
| 4256 | if (proto == TCPCHK_RULES_HTTP_CHK) |
| 4257 | goto bad_http_kw; |
| 4258 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 4259 | memprintf(errmsg, "only on pattern expected"); |
| 4260 | goto error; |
| 4261 | } |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 4262 | type = ((*(args[cur_arg]) == 'b') ? TCPCHK_EXPECT_BINARY : TCPCHK_EXPECT_BINARY_REGEX); |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 4263 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4264 | if (!*(args[cur_arg+1])) { |
| 4265 | memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]); |
| 4266 | goto error; |
| 4267 | } |
| 4268 | cur_arg++; |
| 4269 | pattern = args[cur_arg]; |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 4270 | } |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 4271 | else if (strcmp(args[cur_arg], "string-lf") == 0 || strcmp(args[cur_arg], "binary-lf") == 0) { |
| 4272 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 4273 | memprintf(errmsg, "only on pattern expected"); |
| 4274 | goto error; |
| 4275 | } |
| 4276 | if (proto != TCPCHK_RULES_HTTP_CHK) |
| 4277 | type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_STRING_LF : TCPCHK_EXPECT_BINARY_LF); |
| 4278 | else { |
| 4279 | if (*(args[cur_arg]) != 's') |
| 4280 | goto bad_http_kw; |
| 4281 | type = TCPCHK_EXPECT_HTTP_BODY_LF; |
| 4282 | } |
| 4283 | |
| 4284 | if (!*(args[cur_arg+1])) { |
| 4285 | memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]); |
| 4286 | goto error; |
| 4287 | } |
| 4288 | cur_arg++; |
| 4289 | pattern = args[cur_arg]; |
| 4290 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4291 | else if (strcmp(args[cur_arg], "status") == 0 || strcmp(args[cur_arg], "rstatus") == 0) { |
| 4292 | if (proto != TCPCHK_RULES_HTTP_CHK) |
| 4293 | goto bad_tcp_kw; |
| 4294 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 4295 | memprintf(errmsg, "only on pattern expected"); |
| 4296 | goto error; |
| 4297 | } |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 4298 | type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_HTTP_STATUS : TCPCHK_EXPECT_HTTP_STATUS_REGEX); |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 4299 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4300 | if (!*(args[cur_arg+1])) { |
| 4301 | memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]); |
| 4302 | goto error; |
| 4303 | } |
| 4304 | cur_arg++; |
| 4305 | pattern = args[cur_arg]; |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 4306 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4307 | else if (strcmp(args[cur_arg], "custom") == 0) { |
| 4308 | if (in_pattern) { |
| 4309 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4310 | goto error; |
| 4311 | } |
| 4312 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 4313 | memprintf(errmsg, "only on pattern expected"); |
| 4314 | goto error; |
| 4315 | } |
| 4316 | type = TCPCHK_EXPECT_CUSTOM; |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4317 | } |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4318 | else if (strcmp(args[cur_arg], "hdr") == 0 || strcmp(args[cur_arg], "fhdr") == 0) { |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4319 | int orig_arg = cur_arg; |
| 4320 | |
| 4321 | if (proto != TCPCHK_RULES_HTTP_CHK) |
| 4322 | goto bad_tcp_kw; |
| 4323 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 4324 | memprintf(errmsg, "only on pattern expected"); |
| 4325 | goto error; |
| 4326 | } |
| 4327 | type = TCPCHK_EXPECT_HTTP_HEADER; |
| 4328 | |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4329 | if (strcmp(args[cur_arg], "fhdr") == 0) |
| 4330 | flags |= TCPCHK_EXPT_FL_HTTP_HVAL_FULL; |
| 4331 | |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4332 | /* Parse the name pattern, mandatory */ |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4333 | if (!*(args[cur_arg+1]) || !*(args[cur_arg+2]) || |
| 4334 | (strcmp(args[cur_arg+1], "name") != 0 && strcmp(args[cur_arg+1], "name-lf") != 0)) { |
| 4335 | memprintf(errmsg, "'%s' expects at the name keyword as first argument followed by a pattern", |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4336 | args[orig_arg]); |
| 4337 | goto error; |
| 4338 | } |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4339 | |
| 4340 | if (strcmp(args[cur_arg+1], "name-lf") == 0) |
| 4341 | flags |= TCPCHK_EXPT_FL_HTTP_HNAME_FMT; |
| 4342 | |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4343 | cur_arg += 2; |
| 4344 | if (strcmp(args[cur_arg], "-m") == 0) { |
| 4345 | if (!*(args[cur_arg+1])) { |
| 4346 | memprintf(errmsg, "'%s' : '%s' expects at a matching pattern ('str', 'beg', 'end', 'sub' or 'reg')", |
| 4347 | args[orig_arg], args[cur_arg]); |
| 4348 | goto error; |
| 4349 | } |
| 4350 | if (strcmp(args[cur_arg+1], "str") == 0) |
| 4351 | flags |= TCPCHK_EXPT_FL_HTTP_HNAME_STR; |
| 4352 | else if (strcmp(args[cur_arg+1], "beg") == 0) |
| 4353 | flags |= TCPCHK_EXPT_FL_HTTP_HNAME_BEG; |
| 4354 | else if (strcmp(args[cur_arg+1], "end") == 0) |
| 4355 | flags |= TCPCHK_EXPT_FL_HTTP_HNAME_END; |
| 4356 | else if (strcmp(args[cur_arg+1], "sub") == 0) |
| 4357 | flags |= TCPCHK_EXPT_FL_HTTP_HNAME_SUB; |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4358 | else if (strcmp(args[cur_arg+1], "reg") == 0) { |
| 4359 | if (flags & TCPCHK_EXPT_FL_HTTP_HNAME_FMT) { |
| 4360 | memprintf(errmsg, "'%s': log-format string is not supported with a regex matching method", |
| 4361 | args[orig_arg]); |
| 4362 | goto error; |
| 4363 | } |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4364 | flags |= TCPCHK_EXPT_FL_HTTP_HNAME_REG; |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4365 | } |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4366 | else { |
| 4367 | memprintf(errmsg, "'%s' : '%s' only supports 'str', 'beg', 'end', 'sub' or 'reg' (got '%s')", |
| 4368 | args[orig_arg], args[cur_arg], args[cur_arg+1]); |
| 4369 | goto error; |
| 4370 | } |
| 4371 | cur_arg += 2; |
| 4372 | } |
| 4373 | else |
| 4374 | flags |= TCPCHK_EXPT_FL_HTTP_HNAME_STR; |
| 4375 | npat = args[cur_arg]; |
| 4376 | |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4377 | if (!*(args[cur_arg+1]) || |
| 4378 | (strcmp(args[cur_arg+1], "value") != 0 && strcmp(args[cur_arg+1], "value-lf") != 0)) { |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4379 | flags |= TCPCHK_EXPT_FL_HTTP_HVAL_NONE; |
| 4380 | goto next; |
| 4381 | } |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4382 | if (strcmp(args[cur_arg+1], "value-lf") == 0) |
| 4383 | flags |= TCPCHK_EXPT_FL_HTTP_HVAL_FMT; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4384 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 4385 | /* Parse the value pattern, optional */ |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4386 | if (strcmp(args[cur_arg+2], "-m") == 0) { |
| 4387 | cur_arg += 2; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4388 | if (!*(args[cur_arg+1])) { |
| 4389 | memprintf(errmsg, "'%s' : '%s' expects at a matching pattern ('str', 'beg', 'end', 'sub' or 'reg')", |
| 4390 | args[orig_arg], args[cur_arg]); |
| 4391 | goto error; |
| 4392 | } |
| 4393 | if (strcmp(args[cur_arg+1], "str") == 0) |
| 4394 | flags |= TCPCHK_EXPT_FL_HTTP_HVAL_STR; |
| 4395 | else if (strcmp(args[cur_arg+1], "beg") == 0) |
| 4396 | flags |= TCPCHK_EXPT_FL_HTTP_HVAL_BEG; |
| 4397 | else if (strcmp(args[cur_arg+1], "end") == 0) |
| 4398 | flags |= TCPCHK_EXPT_FL_HTTP_HVAL_END; |
| 4399 | else if (strcmp(args[cur_arg+1], "sub") == 0) |
| 4400 | flags |= TCPCHK_EXPT_FL_HTTP_HVAL_SUB; |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4401 | else if (strcmp(args[cur_arg+1], "reg") == 0) { |
| 4402 | if (flags & TCPCHK_EXPT_FL_HTTP_HVAL_FMT) { |
| 4403 | memprintf(errmsg, "'%s': log-format string is not supported with a regex matching method", |
| 4404 | args[orig_arg]); |
| 4405 | goto error; |
| 4406 | } |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4407 | flags |= TCPCHK_EXPT_FL_HTTP_HVAL_REG; |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4408 | } |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4409 | else { |
| 4410 | memprintf(errmsg, "'%s' : '%s' only supports 'str', 'beg', 'end', 'sub' or 'reg' (got '%s')", |
| 4411 | args[orig_arg], args[cur_arg], args[cur_arg+1]); |
| 4412 | goto error; |
| 4413 | } |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4414 | } |
| 4415 | else |
| 4416 | flags |= TCPCHK_EXPT_FL_HTTP_HVAL_STR; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4417 | |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4418 | if (!*(args[cur_arg+2])) { |
| 4419 | memprintf(errmsg, "'%s' expect a pattern with the value keyword", args[orig_arg]); |
| 4420 | goto error; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4421 | } |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4422 | vpat = args[cur_arg+2]; |
| 4423 | cur_arg += 2; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4424 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4425 | else if (strcmp(args[cur_arg], "comment") == 0) { |
| 4426 | if (in_pattern) { |
| 4427 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4428 | goto error; |
| 4429 | } |
| 4430 | if (!*(args[cur_arg+1])) { |
| 4431 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 4432 | goto error; |
| 4433 | } |
| 4434 | cur_arg++; |
| 4435 | free(comment); |
| 4436 | comment = strdup(args[cur_arg]); |
| 4437 | if (!comment) { |
| 4438 | memprintf(errmsg, "out of memory"); |
| 4439 | goto error; |
| 4440 | } |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4441 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4442 | else if (strcmp(args[cur_arg], "on-success") == 0) { |
| 4443 | if (in_pattern) { |
| 4444 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4445 | goto error; |
| 4446 | } |
| 4447 | if (!*(args[cur_arg+1])) { |
| 4448 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 4449 | goto error; |
| 4450 | } |
| 4451 | cur_arg++; |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 4452 | on_success_msg = args[cur_arg]; |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 4453 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4454 | else if (strcmp(args[cur_arg], "on-error") == 0) { |
| 4455 | if (in_pattern) { |
| 4456 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4457 | goto error; |
| 4458 | } |
| 4459 | if (!*(args[cur_arg+1])) { |
| 4460 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 4461 | goto error; |
| 4462 | } |
| 4463 | cur_arg++; |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 4464 | on_error_msg = args[cur_arg]; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4465 | } |
| 4466 | else if (strcmp(args[cur_arg], "ok-status") == 0) { |
| 4467 | if (in_pattern) { |
| 4468 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4469 | goto error; |
| 4470 | } |
| 4471 | if (!*(args[cur_arg+1])) { |
| 4472 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 4473 | goto error; |
| 4474 | } |
| 4475 | if (strcasecmp(args[cur_arg+1], "L7OK") == 0) |
| 4476 | ok_st = HCHK_STATUS_L7OKD; |
| 4477 | else if (strcasecmp(args[cur_arg+1], "L7OKC") == 0) |
| 4478 | ok_st = HCHK_STATUS_L7OKCD; |
| 4479 | else if (strcasecmp(args[cur_arg+1], "L6OK") == 0) |
| 4480 | ok_st = HCHK_STATUS_L6OK; |
| 4481 | else if (strcasecmp(args[cur_arg+1], "L4OK") == 0) |
| 4482 | ok_st = HCHK_STATUS_L4OK; |
| 4483 | else { |
| 4484 | memprintf(errmsg, "'%s' only supports 'L4OK', 'L6OK', 'L7OK' or 'L7OKC' status (got '%s').", |
| 4485 | args[cur_arg], args[cur_arg+1]); |
| 4486 | goto error; |
| 4487 | } |
| 4488 | cur_arg++; |
| 4489 | } |
| 4490 | else if (strcmp(args[cur_arg], "error-status") == 0) { |
| 4491 | if (in_pattern) { |
| 4492 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4493 | goto error; |
| 4494 | } |
| 4495 | if (!*(args[cur_arg+1])) { |
| 4496 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 4497 | goto error; |
| 4498 | } |
| 4499 | if (strcasecmp(args[cur_arg+1], "L7RSP") == 0) |
| 4500 | err_st = HCHK_STATUS_L7RSP; |
| 4501 | else if (strcasecmp(args[cur_arg+1], "L7STS") == 0) |
| 4502 | err_st = HCHK_STATUS_L7STS; |
| 4503 | else if (strcasecmp(args[cur_arg+1], "L6RSP") == 0) |
| 4504 | err_st = HCHK_STATUS_L6RSP; |
| 4505 | else if (strcasecmp(args[cur_arg+1], "L4CON") == 0) |
| 4506 | err_st = HCHK_STATUS_L4CON; |
| 4507 | else { |
| 4508 | memprintf(errmsg, "'%s' only supports 'L4CON', 'L6RSP', 'L7RSP' or 'L7STS' status (got '%s').", |
| 4509 | args[cur_arg], args[cur_arg+1]); |
| 4510 | goto error; |
| 4511 | } |
| 4512 | cur_arg++; |
| 4513 | } |
| 4514 | else if (strcmp(args[cur_arg], "status-code") == 0) { |
| 4515 | int idx = 0; |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 4516 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4517 | if (in_pattern) { |
| 4518 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4519 | goto error; |
| 4520 | } |
| 4521 | if (!*(args[cur_arg+1])) { |
| 4522 | memprintf(errmsg, "'%s' expects an expression as argument", args[cur_arg]); |
| 4523 | goto error; |
| 4524 | } |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4525 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4526 | cur_arg++; |
| 4527 | release_sample_expr(status_expr); |
| 4528 | px->conf.args.ctx = ARGC_SRV; |
| 4529 | status_expr = sample_parse_expr((char *[]){args[cur_arg], NULL}, &idx, |
| 4530 | file, line, errmsg, &px->conf.args, NULL); |
| 4531 | if (!status_expr) { |
| 4532 | memprintf(errmsg, "error detected while parsing status-code expression : %s", *errmsg); |
| 4533 | goto error; |
| 4534 | } |
| 4535 | if (!(status_expr->fetch->val & SMP_VAL_BE_CHK_RUL)) { |
| 4536 | memprintf(errmsg, "error detected while parsing status-code expression : " |
| 4537 | " fetch method '%s' extracts information from '%s', " |
| 4538 | "none of which is available here.\n", |
| 4539 | args[cur_arg], sample_src_names(status_expr->fetch->use)); |
| 4540 | goto error; |
| 4541 | } |
| 4542 | px->http_needed |= !!(status_expr->fetch->use & SMP_USE_HTTP_ANY); |
| 4543 | } |
| 4544 | else if (strcmp(args[cur_arg], "tout-status") == 0) { |
| 4545 | if (in_pattern) { |
| 4546 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 4547 | goto error; |
| 4548 | } |
| 4549 | if (!*(args[cur_arg+1])) { |
| 4550 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 4551 | goto error; |
| 4552 | } |
| 4553 | if (strcasecmp(args[cur_arg+1], "L7TOUT") == 0) |
| 4554 | tout_st = HCHK_STATUS_L7TOUT; |
| 4555 | else if (strcasecmp(args[cur_arg+1], "L6TOUT") == 0) |
| 4556 | tout_st = HCHK_STATUS_L6TOUT; |
| 4557 | else if (strcasecmp(args[cur_arg+1], "L4TOUT") == 0) |
| 4558 | tout_st = HCHK_STATUS_L4TOUT; |
| 4559 | else { |
| 4560 | memprintf(errmsg, "'%s' only supports 'L4TOUT', 'L6TOUT' or 'L7TOUT' status (got '%s').", |
| 4561 | args[cur_arg], args[cur_arg+1]); |
| 4562 | goto error; |
| 4563 | } |
| 4564 | cur_arg++; |
| 4565 | } |
| 4566 | else { |
| 4567 | if (proto == TCPCHK_RULES_HTTP_CHK) { |
| 4568 | bad_http_kw: |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 4569 | memprintf(errmsg, "'only supports min-recv, [!]string', '[!]rstring', '[!]string-lf', '[!]status', " |
Christopher Faulet | b559426 | 2020-05-05 20:23:13 +0200 | [diff] [blame] | 4570 | "'[!]rstatus', [!]hdr, [!]fhdr or comment but got '%s' as argument.", args[cur_arg]); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4571 | } |
| 4572 | else { |
| 4573 | bad_tcp_kw: |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 4574 | memprintf(errmsg, "'only supports min-recv, '[!]binary', '[!]string', '[!]rstring', '[!]string-lf'" |
| 4575 | "'[!]rbinary', '[!]binary-lf' or comment but got '%s' as argument.", args[cur_arg]); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4576 | } |
| 4577 | goto error; |
| 4578 | } |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4579 | next: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4580 | cur_arg++; |
| 4581 | } |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4582 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4583 | chk = calloc(1, sizeof(*chk)); |
| 4584 | if (!chk) { |
| 4585 | memprintf(errmsg, "out of memory"); |
| 4586 | goto error; |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4587 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4588 | chk->action = TCPCHK_ACT_EXPECT; |
| 4589 | LIST_INIT(&chk->expect.onerror_fmt); |
| 4590 | LIST_INIT(&chk->expect.onsuccess_fmt); |
| 4591 | chk->comment = comment; comment = NULL; |
| 4592 | chk->expect.type = type; |
| 4593 | chk->expect.min_recv = min_recv; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4594 | chk->expect.flags = flags | (inverse ? TCPCHK_EXPT_FL_INV : 0); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4595 | chk->expect.ok_status = ok_st; |
| 4596 | chk->expect.err_status = err_st; |
| 4597 | chk->expect.tout_status = tout_st; |
| 4598 | chk->expect.status_expr = status_expr; status_expr = NULL; |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4599 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4600 | if (on_success_msg) { |
| 4601 | px->conf.args.ctx = ARGC_SRV; |
| 4602 | if (!parse_logformat_string(on_success_msg, px, &chk->expect.onsuccess_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 4603 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", on_success_msg, *errmsg); |
| 4604 | goto error; |
| 4605 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4606 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4607 | if (on_error_msg) { |
| 4608 | px->conf.args.ctx = ARGC_SRV; |
| 4609 | if (!parse_logformat_string(on_error_msg, px, &chk->expect.onerror_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 4610 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", on_error_msg, *errmsg); |
| 4611 | goto error; |
| 4612 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4613 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4614 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4615 | switch (chk->expect.type) { |
Christopher Faulet | 8021a5f | 2020-04-24 13:53:12 +0200 | [diff] [blame] | 4616 | case TCPCHK_EXPECT_HTTP_STATUS: { |
| 4617 | const char *p = pattern; |
| 4618 | unsigned int c1,c2; |
| 4619 | |
| 4620 | chk->expect.codes.codes = NULL; |
| 4621 | chk->expect.codes.num = 0; |
| 4622 | while (1) { |
| 4623 | c1 = c2 = read_uint(&p, pattern + strlen(pattern)); |
| 4624 | if (*p == '-') { |
| 4625 | p++; |
| 4626 | c2 = read_uint(&p, pattern + strlen(pattern)); |
| 4627 | } |
| 4628 | if (c1 > c2) { |
| 4629 | memprintf(errmsg, "invalid range of status codes '%s'", pattern); |
| 4630 | goto error; |
| 4631 | } |
| 4632 | |
| 4633 | chk->expect.codes.num++; |
| 4634 | chk->expect.codes.codes = my_realloc2(chk->expect.codes.codes, |
| 4635 | chk->expect.codes.num * sizeof(*chk->expect.codes.codes)); |
| 4636 | if (!chk->expect.codes.codes) { |
| 4637 | memprintf(errmsg, "out of memory"); |
| 4638 | goto error; |
| 4639 | } |
| 4640 | chk->expect.codes.codes[chk->expect.codes.num-1][0] = c1; |
| 4641 | chk->expect.codes.codes[chk->expect.codes.num-1][1] = c2; |
| 4642 | |
| 4643 | if (*p == '\0') |
| 4644 | break; |
| 4645 | if (*p != ',') { |
| 4646 | memprintf(errmsg, "invalid character '%c' in the list of status codes", *p); |
| 4647 | goto error; |
| 4648 | } |
| 4649 | p++; |
| 4650 | } |
| 4651 | break; |
| 4652 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4653 | case TCPCHK_EXPECT_STRING: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4654 | case TCPCHK_EXPECT_HTTP_BODY: |
| 4655 | chk->expect.data = ist2(strdup(pattern), strlen(pattern)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4656 | if (!isttest(chk->expect.data)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4657 | memprintf(errmsg, "out of memory"); |
| 4658 | goto error; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4659 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4660 | break; |
Christopher Faulet | 2edcd4c | 2020-04-28 10:39:50 +0200 | [diff] [blame] | 4661 | case TCPCHK_EXPECT_BINARY: { |
Christopher Faulet | 9c2cb2d | 2020-04-28 16:40:41 +0200 | [diff] [blame] | 4662 | int len = chk->expect.data.len; |
Christopher Faulet | 2edcd4c | 2020-04-28 10:39:50 +0200 | [diff] [blame] | 4663 | |
| 4664 | if (parse_binary(pattern, &chk->expect.data.ptr, &len, errmsg) == 0) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4665 | memprintf(errmsg, "invalid binary string (%s)", *errmsg); |
| 4666 | goto error; |
| 4667 | } |
Christopher Faulet | 2edcd4c | 2020-04-28 10:39:50 +0200 | [diff] [blame] | 4668 | chk->expect.data.len = len; |
| 4669 | break; |
| 4670 | } |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 4671 | case TCPCHK_EXPECT_STRING_REGEX: |
| 4672 | case TCPCHK_EXPECT_BINARY_REGEX: |
| 4673 | case TCPCHK_EXPECT_HTTP_STATUS_REGEX: |
| 4674 | case TCPCHK_EXPECT_HTTP_BODY_REGEX: |
Christopher Faulet | 88d939c | 2020-04-22 15:32:11 +0200 | [diff] [blame] | 4675 | chk->expect.regex = regex_comp(pattern, 1, 0, errmsg); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4676 | if (!chk->expect.regex) |
| 4677 | goto error; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4678 | break; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 4679 | |
| 4680 | case TCPCHK_EXPECT_STRING_LF: |
| 4681 | case TCPCHK_EXPECT_BINARY_LF: |
| 4682 | case TCPCHK_EXPECT_HTTP_BODY_LF: |
| 4683 | LIST_INIT(&chk->expect.fmt); |
| 4684 | px->conf.args.ctx = ARGC_SRV; |
| 4685 | if (!parse_logformat_string(pattern, px, &chk->expect.fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 4686 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", pattern, *errmsg); |
| 4687 | goto error; |
| 4688 | } |
| 4689 | break; |
| 4690 | |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 4691 | case TCPCHK_EXPECT_HTTP_HEADER: |
| 4692 | if (!npat) { |
| 4693 | memprintf(errmsg, "unexpected error, undefined header name pattern"); |
| 4694 | goto error; |
| 4695 | } |
| 4696 | if (chk->expect.flags & TCPCHK_EXPT_FL_HTTP_HNAME_REG) { |
| 4697 | chk->expect.hdr.name_re = regex_comp(npat, 0, 0, errmsg); |
| 4698 | if (!chk->expect.hdr.name_re) |
| 4699 | goto error; |
| 4700 | } |
| 4701 | else if (chk->expect.flags & TCPCHK_EXPT_FL_HTTP_HNAME_FMT) { |
| 4702 | px->conf.args.ctx = ARGC_SRV; |
| 4703 | LIST_INIT(&chk->expect.hdr.name_fmt); |
| 4704 | if (!parse_logformat_string(npat, px, &chk->expect.hdr.name_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 4705 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", npat, *errmsg); |
| 4706 | goto error; |
| 4707 | } |
| 4708 | } |
| 4709 | else { |
| 4710 | chk->expect.hdr.name = ist2(strdup(npat), strlen(npat)); |
| 4711 | if (!isttest(chk->expect.hdr.name)) { |
| 4712 | memprintf(errmsg, "out of memory"); |
| 4713 | goto error; |
| 4714 | } |
| 4715 | } |
| 4716 | |
| 4717 | if (chk->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_NONE) { |
| 4718 | chk->expect.hdr.value = IST_NULL; |
| 4719 | break; |
| 4720 | } |
| 4721 | |
| 4722 | if (!vpat) { |
| 4723 | memprintf(errmsg, "unexpected error, undefined header value pattern"); |
| 4724 | goto error; |
| 4725 | } |
| 4726 | else if (chk->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_REG) { |
| 4727 | chk->expect.hdr.value_re = regex_comp(vpat, 1, 0, errmsg); |
| 4728 | if (!chk->expect.hdr.value_re) |
| 4729 | goto error; |
| 4730 | } |
| 4731 | else if (chk->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_FMT) { |
| 4732 | px->conf.args.ctx = ARGC_SRV; |
| 4733 | LIST_INIT(&chk->expect.hdr.value_fmt); |
| 4734 | if (!parse_logformat_string(vpat, px, &chk->expect.hdr.value_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 4735 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", npat, *errmsg); |
| 4736 | goto error; |
| 4737 | } |
| 4738 | } |
| 4739 | else { |
| 4740 | chk->expect.hdr.value = ist2(strdup(vpat), strlen(vpat)); |
| 4741 | if (!isttest(chk->expect.hdr.value)) { |
| 4742 | memprintf(errmsg, "out of memory"); |
| 4743 | goto error; |
| 4744 | } |
| 4745 | } |
| 4746 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4747 | break; |
| 4748 | case TCPCHK_EXPECT_CUSTOM: |
| 4749 | chk->expect.custom = NULL; /* Must be defined by the caller ! */ |
| 4750 | break; |
| 4751 | case TCPCHK_EXPECT_UNDEF: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4752 | memprintf(errmsg, "pattern not found"); |
| 4753 | goto error; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4754 | } |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4755 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4756 | /* All tcp-check expect points back to the first inverse expect rule in |
| 4757 | * a chain of one or more expect rule, potentially itself. |
| 4758 | */ |
| 4759 | chk->expect.head = chk; |
| 4760 | list_for_each_entry_rev(prev_check, rules, list) { |
| 4761 | if (prev_check->action == TCPCHK_ACT_EXPECT) { |
| 4762 | if (prev_check->expect.flags & TCPCHK_EXPT_FL_INV) |
| 4763 | chk->expect.head = prev_check; |
| 4764 | continue; |
| 4765 | } |
| 4766 | if (prev_check->action != TCPCHK_ACT_COMMENT && prev_check->action != TCPCHK_ACT_ACTION_KW) |
| 4767 | break; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4768 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4769 | return chk; |
| 4770 | |
| 4771 | error: |
| 4772 | free_tcpcheck(chk, 0); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4773 | free(comment); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4774 | release_sample_expr(status_expr); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4775 | return NULL; |
| 4776 | } |
| 4777 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4778 | /* Overwrites fields of the old http send rule with those of the new one. When |
| 4779 | * replaced, old values are freed and replaced by the new ones. New values are |
| 4780 | * not copied but transferred. At the end <new> should be empty and can be |
| 4781 | * safely released. This function never fails. |
| 4782 | */ |
| 4783 | 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] | 4784 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4785 | struct logformat_node *lf, *lfb; |
| 4786 | struct tcpcheck_http_hdr *hdr, *bhdr; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4787 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4788 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4789 | if (new->send.http.meth.str.area) { |
| 4790 | free(old->send.http.meth.str.area); |
| 4791 | old->send.http.meth.meth = new->send.http.meth.meth; |
| 4792 | old->send.http.meth.str.area = new->send.http.meth.str.area; |
| 4793 | old->send.http.meth.str.data = new->send.http.meth.str.data; |
| 4794 | new->send.http.meth.str = BUF_NULL; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4795 | } |
| 4796 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4797 | if (!(new->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT) && isttest(new->send.http.uri)) { |
| 4798 | if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4799 | istfree(&old->send.http.uri); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4800 | else |
| 4801 | free_tcpcheck_fmt(&old->send.http.uri_fmt); |
| 4802 | old->send.http.flags &= ~TCPCHK_SND_HTTP_FL_URI_FMT; |
| 4803 | old->send.http.uri = new->send.http.uri; |
| 4804 | new->send.http.uri = IST_NULL; |
| 4805 | } |
| 4806 | else if ((new->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT) && !LIST_ISEMPTY(&new->send.http.uri_fmt)) { |
| 4807 | if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4808 | istfree(&old->send.http.uri); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4809 | else |
| 4810 | free_tcpcheck_fmt(&old->send.http.uri_fmt); |
| 4811 | old->send.http.flags |= TCPCHK_SND_HTTP_FL_URI_FMT; |
| 4812 | LIST_INIT(&old->send.http.uri_fmt); |
| 4813 | list_for_each_entry_safe(lf, lfb, &new->send.http.uri_fmt, list) { |
| 4814 | LIST_DEL(&lf->list); |
| 4815 | LIST_ADDQ(&old->send.http.uri_fmt, &lf->list); |
| 4816 | } |
| 4817 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4818 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4819 | if (isttest(new->send.http.vsn)) { |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4820 | istfree(&old->send.http.vsn); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4821 | old->send.http.vsn = new->send.http.vsn; |
| 4822 | new->send.http.vsn = IST_NULL; |
| 4823 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4824 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4825 | free_tcpcheck_http_hdrs(&old->send.http.hdrs); |
| 4826 | list_for_each_entry_safe(hdr, bhdr, &new->send.http.hdrs, list) { |
| 4827 | LIST_DEL(&hdr->list); |
| 4828 | LIST_ADDQ(&old->send.http.hdrs, &hdr->list); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4829 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4830 | |
| 4831 | if (!(new->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT) && isttest(new->send.http.body)) { |
| 4832 | if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4833 | istfree(&old->send.http.body); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4834 | else |
| 4835 | free_tcpcheck_fmt(&old->send.http.body_fmt); |
| 4836 | old->send.http.flags &= ~TCPCHK_SND_HTTP_FL_BODY_FMT; |
| 4837 | old->send.http.body = new->send.http.body; |
| 4838 | new->send.http.body = IST_NULL; |
| 4839 | } |
| 4840 | else if ((new->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT) && !LIST_ISEMPTY(&new->send.http.body_fmt)) { |
| 4841 | if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT)) |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 4842 | istfree(&old->send.http.body); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4843 | else |
| 4844 | free_tcpcheck_fmt(&old->send.http.body_fmt); |
| 4845 | old->send.http.flags |= TCPCHK_SND_HTTP_FL_BODY_FMT; |
| 4846 | LIST_INIT(&old->send.http.body_fmt); |
| 4847 | list_for_each_entry_safe(lf, lfb, &new->send.http.body_fmt, list) { |
| 4848 | LIST_DEL(&lf->list); |
| 4849 | LIST_ADDQ(&old->send.http.body_fmt, &lf->list); |
| 4850 | } |
| 4851 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4852 | } |
| 4853 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4854 | /* Internal function used to add an http-check rule in a list during the config |
| 4855 | * parsing step. Depending on its type, and the previously inserted rules, a |
| 4856 | * specific action may be performed or an error may be reported. This functions |
| 4857 | * returns 1 on success and 0 on error and <errmsg> is filled with the error |
| 4858 | * message. |
| 4859 | */ |
| 4860 | 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] | 4861 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4862 | struct tcpcheck_rule *r; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4863 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4864 | /* the implicit send rule coming from an "option httpchk" line must be |
| 4865 | * merged with the first explici http-check send rule, if |
| 4866 | * any. Depdending the declaration order some tests are required. |
| 4867 | * |
| 4868 | * Some tests is also required for other kinds of http-check rules to be |
| 4869 | * sure the ruleset remains valid. |
| 4870 | */ |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4871 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4872 | if (chk->action == TCPCHK_ACT_SEND && (chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) { |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 4873 | /* Tries to add an implicit http-check send rule from an "option httpchk" line. |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4874 | * First, the first rule is retrieved, skipping the first CONNECT, if any, and |
| 4875 | * following tests are performed : |
| 4876 | * |
| 4877 | * 1- If there is no such rule or if it is not a send rule, the implicit send |
| 4878 | * rule is pushed in front of the ruleset |
| 4879 | * |
| 4880 | * 2- If it is another implicit send rule, it is replaced with the new one. |
| 4881 | * |
| 4882 | * 3- Otherwise, it means it is an explicit send rule. In this case we merge |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 4883 | * both, overwriting the old send rule (the explicit one) with info of the |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4884 | * new send rule (the implicit one). |
| 4885 | */ |
| 4886 | r = get_first_tcpcheck_rule(rules); |
| 4887 | if (r && r->action == TCPCHK_ACT_CONNECT) |
| 4888 | r = get_next_tcpcheck_rule(rules, r); |
| 4889 | if (!r || r->action != TCPCHK_ACT_SEND) |
| 4890 | LIST_ADD(rules->list, &chk->list); |
| 4891 | else if (r->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT) { |
| 4892 | LIST_DEL(&r->list); |
| 4893 | free_tcpcheck(r, 0); |
| 4894 | LIST_ADD(rules->list, &chk->list); |
| 4895 | } |
| 4896 | else { |
| 4897 | tcpcheck_overwrite_send_http_rule(r, chk); |
| 4898 | free_tcpcheck(chk, 0); |
| 4899 | } |
| 4900 | } |
| 4901 | else { |
| 4902 | /* Tries to add an explicit http-check rule. First of all we check the typefo the |
| 4903 | * last inserted rule to be sure it is valid. Then for send rule, we try to merge it |
| 4904 | * with an existing implicit send rule, if any. At the end, if there is no error, |
| 4905 | * the rule is appended to the list. |
| 4906 | */ |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4907 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4908 | r = get_last_tcpcheck_rule(rules); |
| 4909 | if (!r || (r->action == TCPCHK_ACT_SEND && (r->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT))) |
| 4910 | /* no error */; |
| 4911 | else if (r->action != TCPCHK_ACT_CONNECT && chk->action == TCPCHK_ACT_SEND) { |
| 4912 | memprintf(errmsg, "unable to add http-check send rule at step %d (missing connect rule).", |
| 4913 | chk->index+1); |
| 4914 | return 0; |
| 4915 | } |
Christopher Faulet | 1a200d6 | 2020-05-05 07:59:23 +0200 | [diff] [blame] | 4916 | else if (r->action != TCPCHK_ACT_SEND && r->action != TCPCHK_ACT_EXPECT && chk->action == TCPCHK_ACT_EXPECT) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4917 | memprintf(errmsg, "unable to add http-check expect rule at step %d (missing send rule).", |
| 4918 | chk->index+1); |
| 4919 | return 0; |
| 4920 | } |
| 4921 | else if (r->action != TCPCHK_ACT_EXPECT && chk->action == TCPCHK_ACT_CONNECT) { |
| 4922 | memprintf(errmsg, "unable to add http-check connect rule at step %d (missing expect rule).", |
| 4923 | chk->index+1); |
| 4924 | return 0; |
| 4925 | } |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4926 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4927 | if (chk->action == TCPCHK_ACT_SEND) { |
| 4928 | r = get_first_tcpcheck_rule(rules); |
| 4929 | if (r && r->action == TCPCHK_ACT_SEND && (r->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) { |
| 4930 | tcpcheck_overwrite_send_http_rule(r, chk); |
| 4931 | free_tcpcheck(chk, 0); |
| 4932 | LIST_DEL(&r->list); |
| 4933 | r->send.http.flags &= ~TCPCHK_SND_HTTP_FROM_OPT; |
| 4934 | chk = r; |
| 4935 | } |
| 4936 | } |
| 4937 | LIST_ADDQ(rules->list, &chk->list); |
| 4938 | } |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4939 | return 1; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4940 | } |
| 4941 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4942 | /**************************************************************************/ |
| 4943 | /************************** Init/deinit checks ****************************/ |
| 4944 | /**************************************************************************/ |
| 4945 | static const char *init_check(struct check *check, int type) |
| 4946 | { |
| 4947 | check->type = type; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4948 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4949 | b_reset(&check->bi); check->bi.size = global.tune.chksize; |
| 4950 | b_reset(&check->bo); check->bo.size = global.tune.chksize; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4951 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4952 | check->bi.area = calloc(check->bi.size, sizeof(char)); |
| 4953 | check->bo.area = calloc(check->bo.size, sizeof(char)); |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4954 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4955 | if (!check->bi.area || !check->bo.area) |
| 4956 | return "out of memory while allocating check buffer"; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4957 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4958 | check->wait_list.tasklet = tasklet_new(); |
| 4959 | if (!check->wait_list.tasklet) |
| 4960 | return "out of memory while allocating check tasklet"; |
| 4961 | check->wait_list.events = 0; |
| 4962 | check->wait_list.tasklet->process = event_srv_chk_io; |
| 4963 | check->wait_list.tasklet->context = check; |
| 4964 | return NULL; |
| 4965 | } |
| 4966 | |
| 4967 | void free_check(struct check *check) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4968 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4969 | task_destroy(check->task); |
| 4970 | if (check->wait_list.tasklet) |
| 4971 | tasklet_free(check->wait_list.tasklet); |
| 4972 | |
| 4973 | free(check->bi.area); |
| 4974 | free(check->bo.area); |
| 4975 | if (check->cs) { |
| 4976 | free(check->cs->conn); |
| 4977 | check->cs->conn = NULL; |
| 4978 | cs_free(check->cs); |
| 4979 | check->cs = NULL; |
| 4980 | } |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4981 | } |
| 4982 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4983 | /* manages a server health-check. Returns the time the task accepts to wait, or |
| 4984 | * TIME_ETERNITY for infinity. |
| 4985 | */ |
| 4986 | 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] | 4987 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4988 | struct check *check = context; |
| 4989 | |
| 4990 | if (check->type == PR_O2_EXT_CHK) |
| 4991 | return process_chk_proc(t, context, state); |
| 4992 | return process_chk_conn(t, context, state); |
| 4993 | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4994 | } |
| 4995 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 4996 | |
| 4997 | static int start_check_task(struct check *check, int mininter, |
| 4998 | int nbcheck, int srvpos) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4999 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5000 | struct task *t; |
| 5001 | unsigned long thread_mask = MAX_THREADS_MASK; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 5002 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5003 | if (check->type == PR_O2_EXT_CHK) |
| 5004 | thread_mask = 1; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 5005 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5006 | /* task for the check */ |
| 5007 | if ((t = task_new(thread_mask)) == NULL) { |
| 5008 | ha_alert("Starting [%s:%s] check: out of memory.\n", |
| 5009 | check->server->proxy->id, check->server->id); |
| 5010 | return 0; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 5011 | } |
| 5012 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5013 | check->task = t; |
| 5014 | t->process = process_chk; |
| 5015 | t->context = check; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 5016 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5017 | if (mininter < srv_getinter(check)) |
| 5018 | mininter = srv_getinter(check); |
| 5019 | |
| 5020 | if (global.max_spread_checks && mininter > global.max_spread_checks) |
| 5021 | mininter = global.max_spread_checks; |
| 5022 | |
| 5023 | /* check this every ms */ |
| 5024 | t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck)); |
| 5025 | check->start = now; |
| 5026 | task_queue(t); |
| 5027 | |
| 5028 | return 1; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 5029 | } |
| 5030 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5031 | /* updates the server's weight during a warmup stage. Once the final weight is |
| 5032 | * reached, the task automatically stops. Note that any server status change |
| 5033 | * must have updated s->last_change accordingly. |
| 5034 | */ |
| 5035 | 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] | 5036 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5037 | struct server *s = context; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5038 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5039 | /* by default, plan on stopping the task */ |
| 5040 | t->expire = TICK_ETERNITY; |
| 5041 | if ((s->next_admin & SRV_ADMF_MAINT) || |
| 5042 | (s->next_state != SRV_ST_STARTING)) |
| 5043 | return t; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5044 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5045 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5046 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5047 | /* recalculate the weights and update the state */ |
| 5048 | server_recalc_eweight(s, 1); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 5049 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5050 | /* probably that we can refill this server with a bit more connections */ |
| 5051 | pendconn_grab_from_px(s); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 5052 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5053 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 5054 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5055 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 5056 | * whichever is greater, resulting in small 5% steps. |
| 5057 | */ |
| 5058 | if (s->next_state == SRV_ST_STARTING) |
| 5059 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 5060 | return t; |
| 5061 | } |
| 5062 | |
| 5063 | /* |
| 5064 | * Start health-check. |
| 5065 | * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case. |
| 5066 | */ |
| 5067 | static int start_checks() |
| 5068 | { |
| 5069 | |
| 5070 | struct proxy *px; |
| 5071 | struct server *s; |
| 5072 | struct task *t; |
| 5073 | int nbcheck=0, mininter=0, srvpos=0; |
| 5074 | |
| 5075 | /* 0- init the dummy frontend used to create all checks sessions */ |
| 5076 | init_new_proxy(&checks_fe); |
| 5077 | checks_fe.cap = PR_CAP_FE | PR_CAP_BE; |
| 5078 | checks_fe.mode = PR_MODE_TCP; |
| 5079 | checks_fe.maxconn = 0; |
| 5080 | checks_fe.conn_retries = CONN_RETRIES; |
| 5081 | checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC; |
| 5082 | checks_fe.timeout.client = TICK_ETERNITY; |
| 5083 | |
| 5084 | /* 1- count the checkers to run simultaneously. |
| 5085 | * We also determine the minimum interval among all of those which |
| 5086 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 5087 | * will be used to spread their start-up date. Those which have |
| 5088 | * a shorter interval will start independently and will not dictate |
| 5089 | * too short an interval for all others. |
| 5090 | */ |
| 5091 | for (px = proxies_list; px; px = px->next) { |
| 5092 | for (s = px->srv; s; s = s->next) { |
| 5093 | if (s->slowstart) { |
| 5094 | if ((t = task_new(MAX_THREADS_MASK)) == NULL) { |
| 5095 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 5096 | return ERR_ALERT | ERR_FATAL; |
| 5097 | } |
| 5098 | /* We need a warmup task that will be called when the server |
| 5099 | * state switches from down to up. |
| 5100 | */ |
| 5101 | s->warmup = t; |
| 5102 | t->process = server_warmup; |
| 5103 | t->context = s; |
| 5104 | /* server can be in this state only because of */ |
| 5105 | if (s->next_state == SRV_ST_STARTING) |
| 5106 | 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] | 5107 | } |
| 5108 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5109 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 5110 | nbcheck++; |
| 5111 | if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) && |
| 5112 | (!mininter || mininter > srv_getinter(&s->check))) |
| 5113 | mininter = srv_getinter(&s->check); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 5114 | } |
| 5115 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5116 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 5117 | nbcheck++; |
| 5118 | if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) && |
| 5119 | (!mininter || mininter > srv_getinter(&s->agent))) |
| 5120 | mininter = srv_getinter(&s->agent); |
| 5121 | } |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 5122 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5123 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 5124 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5125 | if (!nbcheck) |
| 5126 | return 0; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 5127 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5128 | srand((unsigned)time(NULL)); |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 5129 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5130 | /* |
| 5131 | * 2- start them as far as possible from each others. For this, we will |
| 5132 | * start them after their interval set to the min interval divided by |
| 5133 | * the number of servers, weighted by the server's position in the list. |
| 5134 | */ |
| 5135 | for (px = proxies_list; px; px = px->next) { |
| 5136 | if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) { |
| 5137 | if (init_pid_list()) { |
| 5138 | ha_alert("Starting [%s] check: out of memory.\n", px->id); |
| 5139 | return ERR_ALERT | ERR_FATAL; |
| 5140 | } |
| 5141 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 5142 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5143 | for (s = px->srv; s; s = s->next) { |
| 5144 | /* A task for the main check */ |
| 5145 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 5146 | if (s->check.type == PR_O2_EXT_CHK) { |
| 5147 | if (!prepare_external_check(&s->check)) |
| 5148 | return ERR_ALERT | ERR_FATAL; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 5149 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5150 | if (!start_check_task(&s->check, mininter, nbcheck, srvpos)) |
| 5151 | return ERR_ALERT | ERR_FATAL; |
| 5152 | srvpos++; |
Christopher Faulet | 9857232 | 2020-03-30 13:16:44 +0200 | [diff] [blame] | 5153 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5154 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5155 | /* A task for a auxiliary agent check */ |
| 5156 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 5157 | if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) { |
| 5158 | return ERR_ALERT | ERR_FATAL; |
| 5159 | } |
| 5160 | srvpos++; |
| 5161 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5162 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5163 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5164 | return 0; |
| 5165 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5166 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5167 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5168 | /* |
| 5169 | * Return value: |
| 5170 | * the port to be used for the health check |
| 5171 | * 0 in case no port could be found for the check |
| 5172 | */ |
| 5173 | static int srv_check_healthcheck_port(struct check *chk) |
| 5174 | { |
| 5175 | int i = 0; |
| 5176 | struct server *srv = NULL; |
| 5177 | |
| 5178 | srv = chk->server; |
| 5179 | |
| 5180 | /* by default, we use the health check port ocnfigured */ |
| 5181 | if (chk->port > 0) |
| 5182 | return chk->port; |
| 5183 | |
| 5184 | /* try to get the port from check_core.addr if check.port not set */ |
| 5185 | i = get_host_port(&chk->addr); |
| 5186 | if (i > 0) |
| 5187 | return i; |
| 5188 | |
| 5189 | /* try to get the port from server address */ |
| 5190 | /* prevent MAPPORTS from working at this point, since checks could |
| 5191 | * not be performed in such case (MAPPORTS impose a relative ports |
| 5192 | * based on live traffic) |
| 5193 | */ |
| 5194 | if (srv->flags & SRV_F_MAPPORTS) |
| 5195 | return 0; |
| 5196 | |
| 5197 | i = srv->svc_port; /* by default */ |
| 5198 | if (i > 0) |
| 5199 | return i; |
| 5200 | |
| 5201 | return 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5202 | } |
| 5203 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5204 | /* Initializes an health-check attached to the server <srv>. Non-zero is returned |
| 5205 | * if an error occurred. |
| 5206 | */ |
| 5207 | static int init_srv_check(struct server *srv) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5208 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5209 | const char *err; |
| 5210 | struct tcpcheck_rule *r; |
| 5211 | int ret = 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5212 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5213 | if (!srv->do_check) |
| 5214 | goto out; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5215 | |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 5216 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5217 | /* If neither a port nor an addr was specified and no check transport |
| 5218 | * layer is forced, then the transport layer used by the checks is the |
| 5219 | * same as for the production traffic. Otherwise we use raw_sock by |
| 5220 | * default, unless one is specified. |
| 5221 | */ |
| 5222 | if (!srv->check.port && !is_addr(&srv->check.addr)) { |
| 5223 | if (!srv->check.use_ssl && srv->use_ssl != -1) { |
| 5224 | srv->check.use_ssl = srv->use_ssl; |
| 5225 | srv->check.xprt = srv->xprt; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5226 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5227 | else if (srv->check.use_ssl == 1) |
| 5228 | srv->check.xprt = xprt_get(XPRT_SSL); |
| 5229 | srv->check.send_proxy |= (srv->pp_opts); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5230 | } |
Christopher Faulet | 66163ec | 2020-05-20 22:36:24 +0200 | [diff] [blame] | 5231 | else if (srv->check.use_ssl == 1) |
| 5232 | srv->check.xprt = xprt_get(XPRT_SSL); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5233 | |
Christopher Faulet | 12882cf | 2020-04-23 15:50:18 +0200 | [diff] [blame] | 5234 | /* Inherit the mux protocol from the server if not already defined for |
| 5235 | * the check |
| 5236 | */ |
| 5237 | if (srv->mux_proto && !srv->check.mux_proto) |
| 5238 | srv->check.mux_proto = srv->mux_proto; |
| 5239 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5240 | /* validate <srv> server health-check settings */ |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 5241 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5242 | /* We need at least a service port, a check port or the first tcp-check |
| 5243 | * rule must be a 'connect' one when checking an IPv4/IPv6 server. |
| 5244 | */ |
| 5245 | if ((srv_check_healthcheck_port(&srv->check) != 0) || |
| 5246 | (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr)))) |
| 5247 | goto init; |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 5248 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5249 | if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) { |
| 5250 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n", |
| 5251 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 5252 | ret |= ERR_ALERT | ERR_ABORT; |
| 5253 | goto out; |
| 5254 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5255 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5256 | /* search the first action (connect / send / expect) in the list */ |
| 5257 | r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules); |
| 5258 | if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) { |
| 5259 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port " |
| 5260 | "nor tcp_check rule 'connect' with port information.\n", |
| 5261 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 5262 | ret |= ERR_ALERT | ERR_ABORT; |
| 5263 | goto out; |
| 5264 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5265 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5266 | /* scan the tcp-check ruleset to ensure a port has been configured */ |
| 5267 | list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) { |
| 5268 | if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port || !get_host_port(&r->connect.addr))) { |
| 5269 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port, " |
| 5270 | "and a tcp_check rule 'connect' with no port information.\n", |
| 5271 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 5272 | ret |= ERR_ALERT | ERR_ABORT; |
| 5273 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5274 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5275 | } |
| 5276 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5277 | init: |
| 5278 | if (!(srv->proxy->options2 & PR_O2_CHK_ANY)) { |
| 5279 | struct tcpcheck_ruleset *rs = NULL; |
| 5280 | struct tcpcheck_rules *rules = &srv->proxy->tcpcheck_rules; |
| 5281 | //char *errmsg = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5282 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5283 | srv->proxy->options2 &= ~PR_O2_CHK_ANY; |
| 5284 | srv->proxy->options2 |= PR_O2_TCPCHK_CHK; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5285 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5286 | rs = find_tcpcheck_ruleset("*tcp-check"); |
| 5287 | if (!rs) { |
| 5288 | rs = create_tcpcheck_ruleset("*tcp-check"); |
| 5289 | if (rs == NULL) { |
| 5290 | ha_alert("config: %s '%s': out of memory.\n", |
| 5291 | proxy_type_str(srv->proxy), srv->proxy->id); |
| 5292 | ret |= ERR_ALERT | ERR_FATAL; |
| 5293 | goto out; |
| 5294 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5295 | } |
| 5296 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5297 | free_tcpcheck_vars(&rules->preset_vars); |
| 5298 | rules->list = &rs->rules; |
| 5299 | rules->flags = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5300 | } |
| 5301 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5302 | err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY); |
| 5303 | if (err) { |
| 5304 | ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n", |
| 5305 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err); |
| 5306 | ret |= ERR_ALERT | ERR_ABORT; |
| 5307 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5308 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5309 | srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED; |
| 5310 | global.maxsock++; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5311 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5312 | out: |
| 5313 | return ret; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5314 | } |
| 5315 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5316 | /* Initializes an agent-check attached to the server <srv>. Non-zero is returned |
| 5317 | * if an error occurred. |
| 5318 | */ |
| 5319 | static int init_srv_agent_check(struct server *srv) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5320 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5321 | struct tcpcheck_rule *chk; |
| 5322 | const char *err; |
| 5323 | int ret = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5324 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5325 | if (!srv->do_agent) |
| 5326 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5327 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 5328 | /* If there is no connect rule preceding all send / expect rules, an |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5329 | * implicit one is inserted before all others. |
| 5330 | */ |
| 5331 | chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules); |
| 5332 | if (!chk || chk->action != TCPCHK_ACT_CONNECT) { |
| 5333 | chk = calloc(1, sizeof(*chk)); |
| 5334 | if (!chk) { |
| 5335 | ha_alert("config : %s '%s': unable to add implicit tcp-check connect rule" |
| 5336 | " to agent-check for server '%s' (out of memory).\n", |
| 5337 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 5338 | ret |= ERR_ALERT | ERR_FATAL; |
| 5339 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5340 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5341 | chk->action = TCPCHK_ACT_CONNECT; |
| 5342 | chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT); |
| 5343 | LIST_ADD(srv->agent.tcpcheck_rules->list, &chk->list); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5344 | } |
| 5345 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5346 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5347 | err = init_check(&srv->agent, PR_O2_TCPCHK_CHK); |
| 5348 | if (err) { |
| 5349 | ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n", |
| 5350 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err); |
| 5351 | ret |= ERR_ALERT | ERR_ABORT; |
| 5352 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5353 | } |
| 5354 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5355 | if (!srv->agent.inter) |
| 5356 | srv->agent.inter = srv->check.inter; |
| 5357 | |
| 5358 | srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT; |
| 5359 | global.maxsock++; |
| 5360 | |
| 5361 | out: |
| 5362 | return ret; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5363 | } |
| 5364 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5365 | /* Check tcp-check health-check configuration for the proxy <px>. */ |
| 5366 | static int check_proxy_tcpcheck(struct proxy *px) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5367 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5368 | struct tcpcheck_rule *chk, *back; |
| 5369 | char *comment = NULL, *errmsg = NULL; |
| 5370 | enum tcpcheck_rule_type prev_action = TCPCHK_ACT_COMMENT; |
| 5371 | int ret = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5372 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5373 | if (!(px->cap & PR_CAP_BE) || (px->options2 & PR_O2_CHK_ANY) != PR_O2_TCPCHK_CHK) { |
| 5374 | deinit_proxy_tcpcheck(px); |
| 5375 | goto out; |
| 5376 | } |
| 5377 | |
| 5378 | free(px->check_command); |
| 5379 | free(px->check_path); |
| 5380 | px->check_command = px->check_path = NULL; |
| 5381 | |
| 5382 | if (!px->tcpcheck_rules.list) { |
| 5383 | ha_alert("config : proxy '%s' : tcp-check configured but no ruleset defined.\n", px->id); |
| 5384 | ret |= ERR_ALERT | ERR_FATAL; |
| 5385 | goto out; |
| 5386 | } |
| 5387 | |
| 5388 | /* HTTP ruleset only : */ |
| 5389 | if ((px->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) { |
| 5390 | struct tcpcheck_rule *next; |
| 5391 | |
| 5392 | /* move remaining implicit send rule from "option httpchk" line to the right place. |
| 5393 | * If such rule exists, it must be the first one. In this case, the rule is moved |
| 5394 | * after the first connect rule, if any. Otherwise, nothing is done. |
| 5395 | */ |
| 5396 | chk = get_first_tcpcheck_rule(&px->tcpcheck_rules); |
| 5397 | if (chk && chk->action == TCPCHK_ACT_SEND && (chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) { |
| 5398 | next = get_next_tcpcheck_rule(&px->tcpcheck_rules, chk); |
| 5399 | if (next && next->action == TCPCHK_ACT_CONNECT) { |
| 5400 | LIST_DEL(&chk->list); |
| 5401 | LIST_ADD(&next->list, &chk->list); |
| 5402 | chk->index = next->index; |
| 5403 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5404 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5405 | |
| 5406 | /* add implicit expect rule if the last one is a send. It is inherited from previous |
| 5407 | * versions where the http expect rule was optional. Now it is possible to chained |
| 5408 | * send/expect rules but the last expect may still be implicit. |
| 5409 | */ |
| 5410 | chk = get_last_tcpcheck_rule(&px->tcpcheck_rules); |
| 5411 | if (chk && chk->action == TCPCHK_ACT_SEND) { |
Christopher Faulet | 8021a5f | 2020-04-24 13:53:12 +0200 | [diff] [blame] | 5412 | next = parse_tcpcheck_expect((char *[]){"http-check", "expect", "status", "200-399", ""}, |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5413 | 1, px, px->tcpcheck_rules.list, TCPCHK_RULES_HTTP_CHK, |
| 5414 | px->conf.file, px->conf.line, &errmsg); |
| 5415 | if (!next) { |
| 5416 | ha_alert("config : proxy '%s': unable to add implicit http-check expect rule " |
| 5417 | "(%s).\n", px->id, errmsg); |
| 5418 | free(errmsg); |
| 5419 | ret |= ERR_ALERT | ERR_FATAL; |
| 5420 | goto out; |
| 5421 | } |
| 5422 | LIST_ADDQ(px->tcpcheck_rules.list, &next->list); |
| 5423 | next->index = chk->index; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5424 | } |
| 5425 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5426 | |
| 5427 | /* For all ruleset: */ |
| 5428 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 5429 | /* If there is no connect rule preceding all send / expect rules, an |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5430 | * implicit one is inserted before all others. |
| 5431 | */ |
| 5432 | chk = get_first_tcpcheck_rule(&px->tcpcheck_rules); |
| 5433 | if (!chk || chk->action != TCPCHK_ACT_CONNECT) { |
| 5434 | chk = calloc(1, sizeof(*chk)); |
| 5435 | if (!chk) { |
| 5436 | ha_alert("config : proxy '%s': unable to add implicit tcp-check connect rule " |
| 5437 | "(out of memory).\n", px->id); |
| 5438 | ret |= ERR_ALERT | ERR_FATAL; |
| 5439 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5440 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5441 | chk->action = TCPCHK_ACT_CONNECT; |
| 5442 | chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT); |
| 5443 | LIST_ADD(px->tcpcheck_rules.list, &chk->list); |
| 5444 | } |
| 5445 | |
| 5446 | /* Remove all comment rules. To do so, when a such rule is found, the |
| 5447 | * comment is assigned to the following rule(s). |
| 5448 | */ |
| 5449 | list_for_each_entry_safe(chk, back, px->tcpcheck_rules.list, list) { |
| 5450 | if (chk->action != prev_action && prev_action != TCPCHK_ACT_COMMENT) { |
| 5451 | free(comment); |
| 5452 | comment = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5453 | } |
| 5454 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5455 | prev_action = chk->action; |
| 5456 | switch (chk->action) { |
| 5457 | case TCPCHK_ACT_COMMENT: |
| 5458 | free(comment); |
| 5459 | comment = chk->comment; |
| 5460 | LIST_DEL(&chk->list); |
| 5461 | free(chk); |
| 5462 | break; |
| 5463 | case TCPCHK_ACT_CONNECT: |
| 5464 | if (!chk->comment && comment) |
| 5465 | chk->comment = strdup(comment); |
| 5466 | /* fall though */ |
| 5467 | case TCPCHK_ACT_ACTION_KW: |
| 5468 | free(comment); |
| 5469 | comment = NULL; |
| 5470 | break; |
| 5471 | case TCPCHK_ACT_SEND: |
| 5472 | case TCPCHK_ACT_EXPECT: |
| 5473 | if (!chk->comment && comment) |
| 5474 | chk->comment = strdup(comment); |
| 5475 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5476 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5477 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5478 | free(comment); |
| 5479 | comment = NULL; |
| 5480 | |
| 5481 | out: |
| 5482 | return ret; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5483 | } |
| 5484 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5485 | void deinit_proxy_tcpcheck(struct proxy *px) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5486 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5487 | free_tcpcheck_vars(&px->tcpcheck_rules.preset_vars); |
| 5488 | px->tcpcheck_rules.flags = 0; |
| 5489 | px->tcpcheck_rules.list = NULL; |
| 5490 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5491 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5492 | static void deinit_srv_check(struct server *srv) |
| 5493 | { |
| 5494 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 5495 | free_check(&srv->check); |
| 5496 | srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED; |
| 5497 | srv->do_check = 0; |
| 5498 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5499 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5500 | |
| 5501 | static void deinit_srv_agent_check(struct server *srv) |
| 5502 | { |
| 5503 | if (srv->agent.tcpcheck_rules) { |
| 5504 | free_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars); |
| 5505 | free(srv->agent.tcpcheck_rules); |
| 5506 | srv->agent.tcpcheck_rules = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5507 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5508 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5509 | if (srv->agent.state & CHK_ST_CONFIGURED) |
| 5510 | free_check(&srv->agent); |
| 5511 | |
| 5512 | srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT; |
| 5513 | srv->do_agent = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5514 | } |
| 5515 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5516 | static void deinit_tcpchecks() |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5517 | { |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 5518 | struct tcpcheck_ruleset *rs; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5519 | struct tcpcheck_rule *r, *rb; |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 5520 | struct ebpt_node *node, *next; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5521 | |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 5522 | node = ebpt_first(&shared_tcpchecks); |
| 5523 | while (node) { |
| 5524 | next = ebpt_next(node); |
| 5525 | ebpt_delete(node); |
| 5526 | free(node->key); |
| 5527 | rs = container_of(node, typeof(*rs), node); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5528 | list_for_each_entry_safe(r, rb, &rs->rules, list) { |
| 5529 | LIST_DEL(&r->list); |
| 5530 | free_tcpcheck(r, 0); |
| 5531 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5532 | free(rs); |
Christopher Faulet | d7cee71 | 2020-04-21 13:45:00 +0200 | [diff] [blame] | 5533 | node = next; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5534 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5535 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5536 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5537 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5538 | REGISTER_POST_SERVER_CHECK(init_srv_check); |
| 5539 | REGISTER_POST_SERVER_CHECK(init_srv_agent_check); |
| 5540 | REGISTER_POST_PROXY_CHECK(check_proxy_tcpcheck); |
| 5541 | REGISTER_POST_CHECK(start_checks); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5542 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5543 | REGISTER_SERVER_DEINIT(deinit_srv_check); |
| 5544 | REGISTER_SERVER_DEINIT(deinit_srv_agent_check); |
| 5545 | REGISTER_PROXY_DEINIT(deinit_proxy_tcpcheck); |
| 5546 | REGISTER_POST_DEINIT(deinit_tcpchecks); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5547 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5548 | /**************************************************************************/ |
| 5549 | /****************************** Email alerts ******************************/ |
| 5550 | /* NOTE: It may be pertinent to use an applet to handle email alerts */ |
| 5551 | /* instead of a tcp-check ruleset */ |
| 5552 | /**************************************************************************/ |
| 5553 | void email_alert_free(struct email_alert *alert) |
| 5554 | { |
| 5555 | struct tcpcheck_rule *rule, *back; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5556 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5557 | if (!alert) |
| 5558 | return; |
| 5559 | |
| 5560 | if (alert->rules.list) { |
| 5561 | list_for_each_entry_safe(rule, back, alert->rules.list, list) { |
| 5562 | LIST_DEL(&rule->list); |
| 5563 | free_tcpcheck(rule, 1); |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 5564 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5565 | free_tcpcheck_vars(&alert->rules.preset_vars); |
| 5566 | free(alert->rules.list); |
| 5567 | alert->rules.list = NULL; |
| 5568 | } |
| 5569 | pool_free(pool_head_email_alert, alert); |
| 5570 | } |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 5571 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5572 | static struct task *process_email_alert(struct task *t, void *context, unsigned short state) |
| 5573 | { |
| 5574 | struct check *check = context; |
| 5575 | struct email_alertq *q; |
| 5576 | struct email_alert *alert; |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 5577 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5578 | q = container_of(check, typeof(*q), check); |
| 5579 | |
| 5580 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
| 5581 | while (1) { |
| 5582 | if (!(check->state & CHK_ST_ENABLED)) { |
| 5583 | if (LIST_ISEMPTY(&q->email_alerts)) { |
| 5584 | /* All alerts processed, queue the task */ |
| 5585 | t->expire = TICK_ETERNITY; |
| 5586 | task_queue(t); |
| 5587 | goto end; |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 5588 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5589 | |
| 5590 | alert = LIST_NEXT(&q->email_alerts, typeof(alert), list); |
| 5591 | LIST_DEL(&alert->list); |
| 5592 | t->expire = now_ms; |
| 5593 | check->tcpcheck_rules = &alert->rules; |
| 5594 | check->status = HCHK_STATUS_INI; |
| 5595 | check->state |= CHK_ST_ENABLED; |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 5596 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5597 | |
| 5598 | process_chk(t, context, state); |
| 5599 | if (check->state & CHK_ST_INPROGRESS) |
| 5600 | break; |
| 5601 | |
| 5602 | alert = container_of(check->tcpcheck_rules, typeof(*alert), rules); |
| 5603 | email_alert_free(alert); |
| 5604 | check->tcpcheck_rules = NULL; |
| 5605 | check->server = NULL; |
| 5606 | check->state &= ~CHK_ST_ENABLED; |
| 5607 | } |
| 5608 | end: |
| 5609 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
| 5610 | return t; |
| 5611 | } |
| 5612 | |
| 5613 | /* Initializes mailer alerts for the proxy <p> using <mls> parameters. |
| 5614 | * |
| 5615 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 5616 | * filled. |
| 5617 | */ |
| 5618 | int init_email_alert(struct mailers *mls, struct proxy *p, char **err) |
| 5619 | { |
| 5620 | struct mailer *mailer; |
| 5621 | struct email_alertq *queues; |
| 5622 | const char *err_str; |
| 5623 | int i = 0; |
| 5624 | |
| 5625 | if ((queues = calloc(mls->count, sizeof(*queues))) == NULL) { |
| 5626 | memprintf(err, "out of memory while allocating mailer alerts queues"); |
| 5627 | goto fail_no_queue; |
| 5628 | } |
| 5629 | |
| 5630 | for (mailer = mls->mailer_list; mailer; i++, mailer = mailer->next) { |
| 5631 | struct email_alertq *q = &queues[i]; |
| 5632 | struct check *check = &q->check; |
| 5633 | struct task *t; |
| 5634 | |
| 5635 | LIST_INIT(&q->email_alerts); |
| 5636 | HA_SPIN_INIT(&q->lock); |
| 5637 | check->inter = mls->timeout.mail; |
| 5638 | check->rise = DEF_AGENT_RISETIME; |
| 5639 | check->proxy = p; |
| 5640 | check->fall = DEF_AGENT_FALLTIME; |
| 5641 | if ((err_str = init_check(check, PR_O2_TCPCHK_CHK))) { |
| 5642 | memprintf(err, "%s", err_str); |
| 5643 | goto error; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5644 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5645 | |
| 5646 | check->xprt = mailer->xprt; |
| 5647 | check->addr = mailer->addr; |
| 5648 | check->port = get_host_port(&mailer->addr); |
| 5649 | |
| 5650 | if ((t = task_new(MAX_THREADS_MASK)) == NULL) { |
| 5651 | memprintf(err, "out of memory while allocating mailer alerts task"); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5652 | goto error; |
| 5653 | } |
| 5654 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5655 | check->task = t; |
| 5656 | t->process = process_email_alert; |
| 5657 | t->context = check; |
| 5658 | |
| 5659 | /* check this in one ms */ |
| 5660 | t->expire = TICK_ETERNITY; |
| 5661 | check->start = now; |
| 5662 | task_queue(t); |
| 5663 | } |
| 5664 | |
| 5665 | mls->users++; |
| 5666 | free(p->email_alert.mailers.name); |
| 5667 | p->email_alert.mailers.m = mls; |
| 5668 | p->email_alert.queues = queues; |
| 5669 | return 0; |
| 5670 | |
| 5671 | error: |
| 5672 | for (i = 0; i < mls->count; i++) { |
| 5673 | struct email_alertq *q = &queues[i]; |
| 5674 | struct check *check = &q->check; |
| 5675 | |
| 5676 | free_check(check); |
| 5677 | } |
| 5678 | free(queues); |
| 5679 | fail_no_queue: |
| 5680 | return 1; |
| 5681 | } |
| 5682 | |
| 5683 | static int add_tcpcheck_expect_str(struct tcpcheck_rules *rules, const char *str) |
| 5684 | { |
| 5685 | struct tcpcheck_rule *tcpcheck, *prev_check; |
| 5686 | struct tcpcheck_expect *expect; |
| 5687 | |
| 5688 | if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL) |
| 5689 | return 0; |
| 5690 | memset(tcpcheck, 0, sizeof(*tcpcheck)); |
| 5691 | tcpcheck->action = TCPCHK_ACT_EXPECT; |
| 5692 | |
| 5693 | expect = &tcpcheck->expect; |
| 5694 | expect->type = TCPCHK_EXPECT_STRING; |
| 5695 | LIST_INIT(&expect->onerror_fmt); |
| 5696 | LIST_INIT(&expect->onsuccess_fmt); |
| 5697 | expect->ok_status = HCHK_STATUS_L7OKD; |
| 5698 | expect->err_status = HCHK_STATUS_L7RSP; |
| 5699 | expect->tout_status = HCHK_STATUS_L7TOUT; |
| 5700 | expect->data = ist2(strdup(str), strlen(str)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 5701 | if (!isttest(expect->data)) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5702 | pool_free(pool_head_tcpcheck_rule, tcpcheck); |
| 5703 | return 0; |
| 5704 | } |
| 5705 | |
| 5706 | /* All tcp-check expect points back to the first inverse expect rule |
| 5707 | * in a chain of one or more expect rule, potentially itself. |
| 5708 | */ |
| 5709 | tcpcheck->expect.head = tcpcheck; |
| 5710 | list_for_each_entry_rev(prev_check, rules->list, list) { |
| 5711 | if (prev_check->action == TCPCHK_ACT_EXPECT) { |
| 5712 | if (prev_check->expect.flags & TCPCHK_EXPT_FL_INV) |
| 5713 | tcpcheck->expect.head = prev_check; |
| 5714 | continue; |
| 5715 | } |
| 5716 | if (prev_check->action != TCPCHK_ACT_COMMENT && prev_check->action != TCPCHK_ACT_ACTION_KW) |
| 5717 | break; |
| 5718 | } |
| 5719 | LIST_ADDQ(rules->list, &tcpcheck->list); |
| 5720 | return 1; |
| 5721 | } |
| 5722 | |
| 5723 | static int add_tcpcheck_send_strs(struct tcpcheck_rules *rules, const char * const *strs) |
| 5724 | { |
| 5725 | struct tcpcheck_rule *tcpcheck; |
| 5726 | struct tcpcheck_send *send; |
| 5727 | const char *in; |
| 5728 | char *dst; |
| 5729 | int i; |
| 5730 | |
| 5731 | if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL) |
| 5732 | return 0; |
| 5733 | memset(tcpcheck, 0, sizeof(*tcpcheck)); |
| 5734 | tcpcheck->action = TCPCHK_ACT_SEND; |
| 5735 | |
| 5736 | send = &tcpcheck->send; |
| 5737 | send->type = TCPCHK_SEND_STRING; |
| 5738 | |
| 5739 | for (i = 0; strs[i]; i++) |
| 5740 | send->data.len += strlen(strs[i]); |
| 5741 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 5742 | send->data.ptr = malloc(istlen(send->data) + 1); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5743 | if (!isttest(send->data)) { |
| 5744 | pool_free(pool_head_tcpcheck_rule, tcpcheck); |
| 5745 | return 0; |
| 5746 | } |
| 5747 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 5748 | dst = istptr(send->data); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5749 | for (i = 0; strs[i]; i++) |
| 5750 | for (in = strs[i]; (*dst = *in++); dst++); |
| 5751 | *dst = 0; |
| 5752 | |
| 5753 | LIST_ADDQ(rules->list, &tcpcheck->list); |
| 5754 | return 1; |
| 5755 | } |
| 5756 | |
| 5757 | static int enqueue_one_email_alert(struct proxy *p, struct server *s, |
| 5758 | struct email_alertq *q, const char *msg) |
| 5759 | { |
| 5760 | struct email_alert *alert; |
| 5761 | struct tcpcheck_rule *tcpcheck; |
| 5762 | struct check *check = &q->check; |
| 5763 | |
| 5764 | if ((alert = pool_alloc(pool_head_email_alert)) == NULL) |
| 5765 | goto error; |
| 5766 | LIST_INIT(&alert->list); |
| 5767 | alert->rules.flags = TCPCHK_RULES_TCP_CHK; |
| 5768 | alert->rules.list = calloc(1, sizeof(*alert->rules.list)); |
| 5769 | if (!alert->rules.list) |
| 5770 | goto error; |
| 5771 | LIST_INIT(alert->rules.list); |
| 5772 | LIST_INIT(&alert->rules.preset_vars); /* unused for email alerts */ |
| 5773 | alert->srv = s; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5774 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5775 | if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL) |
| 5776 | goto error; |
| 5777 | memset(tcpcheck, 0, sizeof(*tcpcheck)); |
| 5778 | tcpcheck->action = TCPCHK_ACT_CONNECT; |
| 5779 | tcpcheck->comment = NULL; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5780 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5781 | LIST_ADDQ(alert->rules.list, &tcpcheck->list); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5782 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5783 | if (!add_tcpcheck_expect_str(&alert->rules, "220 ")) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5784 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5785 | |
| 5786 | { |
| 5787 | const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" }; |
| 5788 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
| 5789 | goto error; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5790 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5791 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5792 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
| 5793 | goto error; |
| 5794 | |
| 5795 | { |
| 5796 | const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" }; |
| 5797 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5798 | goto error; |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5799 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5800 | |
| 5801 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
| 5802 | goto error; |
| 5803 | |
| 5804 | { |
| 5805 | const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" }; |
| 5806 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5807 | goto error; |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5808 | } |
| 5809 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5810 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
| 5811 | goto error; |
| 5812 | |
| 5813 | { |
| 5814 | const char * const strs[2] = { "DATA\r\n" }; |
| 5815 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5816 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5817 | } |
| 5818 | |
| 5819 | if (!add_tcpcheck_expect_str(&alert->rules, "354 ")) |
| 5820 | goto error; |
| 5821 | |
| 5822 | { |
| 5823 | struct tm tm; |
| 5824 | char datestr[48]; |
| 5825 | const char * const strs[18] = { |
| 5826 | "From: ", p->email_alert.from, "\r\n", |
| 5827 | "To: ", p->email_alert.to, "\r\n", |
| 5828 | "Date: ", datestr, "\r\n", |
| 5829 | "Subject: [HAproxy Alert] ", msg, "\r\n", |
| 5830 | "\r\n", |
| 5831 | msg, "\r\n", |
| 5832 | "\r\n", |
| 5833 | ".\r\n", |
| 5834 | NULL |
| 5835 | }; |
| 5836 | |
| 5837 | get_localtime(date.tv_sec, &tm); |
| 5838 | |
| 5839 | 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] | 5840 | goto error; |
| 5841 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5842 | |
| 5843 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5844 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5845 | } |
| 5846 | |
| 5847 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5848 | goto error; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5849 | |
| 5850 | { |
| 5851 | const char * const strs[2] = { "QUIT\r\n" }; |
| 5852 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
| 5853 | goto error; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5854 | } |
| 5855 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5856 | if (!add_tcpcheck_expect_str(&alert->rules, "221 ")) |
| 5857 | goto error; |
| 5858 | |
| 5859 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
| 5860 | task_wakeup(check->task, TASK_WOKEN_MSG); |
| 5861 | LIST_ADDQ(&q->email_alerts, &alert->list); |
| 5862 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
| 5863 | return 1; |
| 5864 | |
| 5865 | error: |
| 5866 | email_alert_free(alert); |
| 5867 | return 0; |
| 5868 | } |
| 5869 | |
| 5870 | static void enqueue_email_alert(struct proxy *p, struct server *s, const char *msg) |
| 5871 | { |
| 5872 | int i; |
| 5873 | struct mailer *mailer; |
| 5874 | |
| 5875 | for (i = 0, mailer = p->email_alert.mailers.m->mailer_list; |
| 5876 | i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) { |
| 5877 | if (!enqueue_one_email_alert(p, s, &p->email_alert.queues[i], msg)) { |
| 5878 | ha_alert("Email alert [%s] could not be enqueued: out of memory\n", p->id); |
| 5879 | return; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5880 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5881 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5882 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5883 | return; |
| 5884 | } |
| 5885 | |
| 5886 | /* |
| 5887 | * Send email alert if configured. |
| 5888 | */ |
| 5889 | void send_email_alert(struct server *s, int level, const char *format, ...) |
| 5890 | { |
| 5891 | va_list argp; |
| 5892 | char buf[1024]; |
| 5893 | int len; |
| 5894 | struct proxy *p = s->proxy; |
| 5895 | |
| 5896 | if (!p->email_alert.mailers.m || level > p->email_alert.level || format == NULL) |
| 5897 | return; |
| 5898 | |
| 5899 | va_start(argp, format); |
| 5900 | len = vsnprintf(buf, sizeof(buf), format, argp); |
| 5901 | va_end(argp); |
| 5902 | |
| 5903 | if (len < 0 || len >= sizeof(buf)) { |
| 5904 | ha_alert("Email alert [%s] could not format message\n", p->id); |
| 5905 | return; |
| 5906 | } |
| 5907 | |
| 5908 | enqueue_email_alert(p, s, buf); |
| 5909 | } |
| 5910 | |
| 5911 | /**************************************************************************/ |
| 5912 | /************************** Check sample fetches **************************/ |
| 5913 | /**************************************************************************/ |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5914 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5915 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5916 | { /* END */ }, |
| 5917 | }}; |
| 5918 | |
| 5919 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
| 5920 | |
| 5921 | |
| 5922 | /**************************************************************************/ |
| 5923 | /************************ Check's parsing functions ***********************/ |
| 5924 | /**************************************************************************/ |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5925 | /* Parses the "tcp-check" proxy keyword */ |
| 5926 | static int proxy_parse_tcpcheck(char **args, int section, struct proxy *curpx, |
| 5927 | struct proxy *defpx, const char *file, int line, |
| 5928 | char **errmsg) |
| 5929 | { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5930 | struct tcpcheck_ruleset *rs = NULL; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5931 | struct tcpcheck_rule *chk = NULL; |
Gaetan Rivet | 5301b01 | 2020-02-25 17:19:17 +0100 | [diff] [blame] | 5932 | int index, cur_arg, ret = 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5933 | |
| 5934 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[0], NULL)) |
| 5935 | ret = 1; |
| 5936 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5937 | /* Deduce the ruleset name from the proxy info */ |
| 5938 | chunk_printf(&trash, "*tcp-check-%s_%s-%d", |
| 5939 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 5940 | curpx->conf.file, curpx->conf.line); |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 5941 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5942 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5943 | if (rs == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 5944 | rs = create_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5945 | if (rs == NULL) { |
| 5946 | memprintf(errmsg, "out of memory.\n"); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5947 | goto error; |
| 5948 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5949 | } |
| 5950 | |
Gaetan Rivet | 5301b01 | 2020-02-25 17:19:17 +0100 | [diff] [blame] | 5951 | index = 0; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5952 | if (!LIST_ISEMPTY(&rs->rules)) { |
| 5953 | chk = LIST_PREV(&rs->rules, typeof(chk), list); |
Gaetan Rivet | 5301b01 | 2020-02-25 17:19:17 +0100 | [diff] [blame] | 5954 | index = chk->index + 1; |
| 5955 | } |
| 5956 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5957 | cur_arg = 1; |
| 5958 | if (strcmp(args[cur_arg], "connect") == 0) |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5959 | chk = parse_tcpcheck_connect(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 5960 | else if (strcmp(args[cur_arg], "send") == 0 || strcmp(args[cur_arg], "send-binary") == 0 || |
| 5961 | strcmp(args[cur_arg], "send-lf") == 0 || strcmp(args[cur_arg], "send-binary-lf") == 0) |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5962 | 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] | 5963 | else if (strcmp(args[cur_arg], "expect") == 0) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5964 | 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] | 5965 | else if (strcmp(args[cur_arg], "comment") == 0) |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5966 | 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] | 5967 | else { |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 5968 | struct action_kw *kw = action_kw_tcp_check_lookup(args[cur_arg]); |
| 5969 | |
| 5970 | if (!kw) { |
| 5971 | action_kw_tcp_check_build_list(&trash); |
| 5972 | memprintf(errmsg, "'%s' only supports 'comment', 'connect', 'send', 'send-binary', 'expect'" |
| 5973 | "%s%s. but got '%s'", |
| 5974 | args[0], (*trash.area ? ", " : ""), trash.area, args[1]); |
| 5975 | goto error; |
| 5976 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5977 | 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] | 5978 | } |
| 5979 | |
| 5980 | if (!chk) { |
| 5981 | memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg); |
| 5982 | goto error; |
| 5983 | } |
Christopher Faulet | 528f481 | 2020-04-28 10:47:28 +0200 | [diff] [blame] | 5984 | ret = (ret || (*errmsg != NULL)); /* Handle warning */ |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5985 | |
| 5986 | /* No error: add the tcp-check rule in the list */ |
Gaetan Rivet | 5301b01 | 2020-02-25 17:19:17 +0100 | [diff] [blame] | 5987 | chk->index = index; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5988 | LIST_ADDQ(&rs->rules, &chk->list); |
| 5989 | |
| 5990 | if ((curpx->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK && |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 5991 | (curpx->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5992 | /* Use this ruleset if the proxy already has tcp-check enabled */ |
| 5993 | curpx->tcpcheck_rules.list = &rs->rules; |
| 5994 | curpx->tcpcheck_rules.flags &= ~TCPCHK_RULES_UNUSED_TCP_RS; |
| 5995 | } |
| 5996 | else { |
| 5997 | /* mark this ruleset as unused for now */ |
| 5998 | curpx->tcpcheck_rules.flags |= TCPCHK_RULES_UNUSED_TCP_RS; |
| 5999 | } |
| 6000 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 6001 | return ret; |
| 6002 | |
| 6003 | error: |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6004 | free_tcpcheck(chk, 0); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6005 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 6006 | return -1; |
| 6007 | } |
| 6008 | |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6009 | /* Parses the "http-check" proxy keyword */ |
| 6010 | static int proxy_parse_httpcheck(char **args, int section, struct proxy *curpx, |
| 6011 | struct proxy *defpx, const char *file, int line, |
| 6012 | char **errmsg) |
| 6013 | { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6014 | struct tcpcheck_ruleset *rs = NULL; |
| 6015 | struct tcpcheck_rule *chk = NULL; |
| 6016 | int index, cur_arg, ret = 0; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6017 | |
| 6018 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[0], NULL)) |
| 6019 | ret = 1; |
| 6020 | |
| 6021 | cur_arg = 1; |
| 6022 | if (strcmp(args[cur_arg], "disable-on-404") == 0) { |
| 6023 | /* enable a graceful server shutdown on an HTTP 404 response */ |
| 6024 | curpx->options |= PR_O_DISABLE404; |
| 6025 | if (too_many_args(1, args, errmsg, NULL)) |
| 6026 | goto error; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6027 | goto out; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6028 | } |
| 6029 | else if (strcmp(args[cur_arg], "send-state") == 0) { |
| 6030 | /* enable emission of the apparent state of a server in HTTP checks */ |
| 6031 | curpx->options2 |= PR_O2_CHK_SNDST; |
| 6032 | if (too_many_args(1, args, errmsg, NULL)) |
| 6033 | goto error; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6034 | goto out; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6035 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6036 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6037 | /* Deduce the ruleset name from the proxy info */ |
| 6038 | chunk_printf(&trash, "*http-check-%s_%s-%d", |
| 6039 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 6040 | curpx->conf.file, curpx->conf.line); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6041 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6042 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6043 | if (rs == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6044 | rs = create_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6045 | if (rs == NULL) { |
| 6046 | memprintf(errmsg, "out of memory.\n"); |
| 6047 | goto error; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6048 | } |
| 6049 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6050 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6051 | index = 0; |
| 6052 | if (!LIST_ISEMPTY(&rs->rules)) { |
| 6053 | chk = LIST_PREV(&rs->rules, typeof(chk), list); |
| 6054 | if (chk->action != TCPCHK_ACT_SEND || !(chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) |
| 6055 | index = chk->index + 1; |
| 6056 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6057 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6058 | if (strcmp(args[cur_arg], "connect") == 0) |
| 6059 | chk = parse_tcpcheck_connect(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
| 6060 | else if (strcmp(args[cur_arg], "send") == 0) |
| 6061 | chk = parse_tcpcheck_send_http(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
| 6062 | else if (strcmp(args[cur_arg], "expect") == 0) |
| 6063 | chk = parse_tcpcheck_expect(args, cur_arg, curpx, &rs->rules, TCPCHK_RULES_HTTP_CHK, |
| 6064 | file, line, errmsg); |
| 6065 | else if (strcmp(args[cur_arg], "comment") == 0) |
| 6066 | chk = parse_tcpcheck_comment(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
| 6067 | else { |
| 6068 | struct action_kw *kw = action_kw_tcp_check_lookup(args[cur_arg]); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6069 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6070 | if (!kw) { |
| 6071 | action_kw_tcp_check_build_list(&trash); |
| 6072 | memprintf(errmsg, "'%s' only supports 'disable-on-404', 'send-state', 'comment', 'connect'," |
| 6073 | " 'send', 'expect'%s%s. but got '%s'", |
| 6074 | args[0], (*trash.area ? ", " : ""), trash.area, args[1]); |
| 6075 | goto error; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6076 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6077 | chk = parse_tcpcheck_action(args, cur_arg, curpx, &rs->rules, kw, file, line, errmsg); |
| 6078 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6079 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6080 | if (!chk) { |
| 6081 | memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg); |
| 6082 | goto error; |
| 6083 | } |
| 6084 | ret = (*errmsg != NULL); /* Handle warning */ |
| 6085 | |
| 6086 | chk->index = index; |
| 6087 | if ((curpx->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK && |
| 6088 | (curpx->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) { |
| 6089 | /* Use this ruleset if the proxy already has http-check enabled */ |
| 6090 | curpx->tcpcheck_rules.list = &rs->rules; |
| 6091 | curpx->tcpcheck_rules.flags &= ~TCPCHK_RULES_UNUSED_HTTP_RS; |
| 6092 | if (!tcpcheck_add_http_rule(chk, &curpx->tcpcheck_rules, errmsg)) { |
| 6093 | memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg); |
| 6094 | curpx->tcpcheck_rules.list = NULL; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6095 | goto error; |
| 6096 | } |
| 6097 | } |
| 6098 | else { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6099 | /* mark this ruleset as unused for now */ |
| 6100 | curpx->tcpcheck_rules.flags |= TCPCHK_RULES_UNUSED_HTTP_RS; |
| 6101 | LIST_ADDQ(&rs->rules, &chk->list); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6102 | } |
| 6103 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6104 | out: |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6105 | return ret; |
| 6106 | |
| 6107 | error: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6108 | free_tcpcheck(chk, 0); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6109 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 6110 | return -1; |
| 6111 | } |
| 6112 | |
Christopher Faulet | e9111b6 | 2020-04-09 18:12:08 +0200 | [diff] [blame] | 6113 | /* Parses the "external-check" proxy keyword */ |
| 6114 | static int proxy_parse_extcheck(char **args, int section, struct proxy *curpx, |
| 6115 | struct proxy *defpx, const char *file, int line, |
| 6116 | char **errmsg) |
| 6117 | { |
| 6118 | int cur_arg, ret = 0; |
| 6119 | |
| 6120 | cur_arg = 1; |
| 6121 | if (!*(args[cur_arg])) { |
| 6122 | memprintf(errmsg, "missing argument after '%s'.\n", args[0]); |
| 6123 | goto error; |
| 6124 | } |
| 6125 | |
| 6126 | if (strcmp(args[cur_arg], "command") == 0) { |
| 6127 | if (too_many_args(2, args, errmsg, NULL)) |
| 6128 | goto error; |
| 6129 | if (!*(args[cur_arg+1])) { |
| 6130 | memprintf(errmsg, "missing argument after '%s'.", args[cur_arg]); |
| 6131 | goto error; |
| 6132 | } |
| 6133 | free(curpx->check_command); |
| 6134 | curpx->check_command = strdup(args[cur_arg+1]); |
| 6135 | } |
| 6136 | else if (strcmp(args[cur_arg], "path") == 0) { |
| 6137 | if (too_many_args(2, args, errmsg, NULL)) |
| 6138 | goto error; |
| 6139 | if (!*(args[cur_arg+1])) { |
| 6140 | memprintf(errmsg, "missing argument after '%s'.", args[cur_arg]); |
| 6141 | goto error; |
| 6142 | } |
| 6143 | free(curpx->check_path); |
| 6144 | curpx->check_path = strdup(args[cur_arg+1]); |
| 6145 | } |
| 6146 | else { |
| 6147 | memprintf(errmsg, "'%s' only supports 'command' and 'path'. but got '%s'.", |
| 6148 | args[0], args[1]); |
| 6149 | goto error; |
| 6150 | } |
| 6151 | |
| 6152 | ret = (*errmsg != NULL); /* Handle warning */ |
| 6153 | return ret; |
| 6154 | |
| 6155 | error: |
| 6156 | return -1; |
| 6157 | } |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 6158 | |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 6159 | /* Parses the "option tcp-check" proxy keyword */ |
| 6160 | int proxy_parse_tcp_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6161 | const char *file, int line) |
| 6162 | { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6163 | struct tcpcheck_ruleset *rs = NULL; |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 6164 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6165 | int err_code = 0; |
| 6166 | |
| 6167 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6168 | err_code |= ERR_WARN; |
| 6169 | |
| 6170 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 6171 | goto out; |
| 6172 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6173 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6174 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6175 | |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 6176 | if ((rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6177 | /* If a tcp-check rulesset is already set, do nothing */ |
| 6178 | if (rules->list) |
| 6179 | goto out; |
| 6180 | |
| 6181 | /* If a tcp-check ruleset is waiting to be used for the current proxy, |
| 6182 | * get it. |
| 6183 | */ |
| 6184 | if (rules->flags & TCPCHK_RULES_UNUSED_TCP_RS) |
| 6185 | goto curpx_ruleset; |
| 6186 | |
| 6187 | /* Otherwise, try to get the tcp-check ruleset of the default proxy */ |
| 6188 | 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] | 6189 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6190 | if (rs) |
| 6191 | goto ruleset_found; |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 6192 | } |
| 6193 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6194 | curpx_ruleset: |
| 6195 | /* Deduce the ruleset name from the proxy info */ |
| 6196 | chunk_printf(&trash, "*tcp-check-%s_%s-%d", |
| 6197 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 6198 | curpx->conf.file, curpx->conf.line); |
| 6199 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6200 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6201 | if (rs == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6202 | rs = create_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6203 | if (rs == NULL) { |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 6204 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6205 | goto error; |
| 6206 | } |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 6207 | } |
| 6208 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6209 | ruleset_found: |
| 6210 | free_tcpcheck_vars(&rules->preset_vars); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6211 | rules->list = &rs->rules; |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 6212 | rules->flags |= TCPCHK_RULES_TCP_CHK; |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 6213 | |
| 6214 | out: |
| 6215 | return err_code; |
| 6216 | |
| 6217 | error: |
| 6218 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6219 | goto out; |
| 6220 | } |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 6221 | |
| 6222 | /* Parses the "option redis-check" proxy keyword */ |
| 6223 | int proxy_parse_redis_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6224 | const char *file, int line) |
| 6225 | { |
| 6226 | static char *redis_req = "*1\r\n$4\r\nPING\r\n"; |
| 6227 | static char *redis_res = "+PONG\r\n"; |
| 6228 | |
| 6229 | struct tcpcheck_ruleset *rs = NULL; |
| 6230 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6231 | struct tcpcheck_rule *chk; |
| 6232 | char *errmsg = NULL; |
| 6233 | int err_code = 0; |
| 6234 | |
| 6235 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6236 | err_code |= ERR_WARN; |
| 6237 | |
| 6238 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 6239 | goto out; |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 6240 | |
| 6241 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6242 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6243 | |
| 6244 | free_tcpcheck_vars(&rules->preset_vars); |
| 6245 | rules->list = NULL; |
| 6246 | rules->flags = 0; |
| 6247 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6248 | rs = find_tcpcheck_ruleset("*redis-check"); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 6249 | if (rs) |
| 6250 | goto ruleset_found; |
| 6251 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6252 | rs = create_tcpcheck_ruleset("*redis-check"); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 6253 | if (rs == NULL) { |
| 6254 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6255 | goto error; |
| 6256 | } |
| 6257 | |
| 6258 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send", redis_req, ""}, |
| 6259 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6260 | if (!chk) { |
| 6261 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6262 | goto error; |
| 6263 | } |
| 6264 | chk->index = 0; |
| 6265 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6266 | |
| 6267 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "string", redis_res, |
| 6268 | "error-status", "L7STS", |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 6269 | "on-error", "%[res.payload(0,0),cut_crlf]", |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 6270 | "on-success", "Redis server is ok", |
| 6271 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6272 | 1, curpx, &rs->rules, TCPCHK_RULES_REDIS_CHK, file, line, &errmsg); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 6273 | if (!chk) { |
| 6274 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6275 | goto error; |
| 6276 | } |
| 6277 | chk->index = 1; |
| 6278 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6279 | |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 6280 | ruleset_found: |
| 6281 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6282 | rules->flags |= TCPCHK_RULES_REDIS_CHK; |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 6283 | |
| 6284 | out: |
| 6285 | free(errmsg); |
| 6286 | return err_code; |
| 6287 | |
| 6288 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6289 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 6290 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6291 | goto out; |
| 6292 | } |
| 6293 | |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6294 | |
| 6295 | /* Parses the "option ssl-hello-chk" proxy keyword */ |
| 6296 | int proxy_parse_ssl_hello_chk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6297 | const char *file, int line) |
| 6298 | { |
| 6299 | /* This is the SSLv3 CLIENT HELLO packet used in conjunction with the |
| 6300 | * ssl-hello-chk option to ensure that the remote server speaks SSL. |
| 6301 | * |
| 6302 | * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details. |
| 6303 | */ |
| 6304 | static char sslv3_client_hello[] = { |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 6305 | "16" /* ContentType : 0x16 = Handshake */ |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6306 | "0300" /* ProtocolVersion : 0x0300 = SSLv3 */ |
| 6307 | "0079" /* ContentLength : 0x79 bytes after this one */ |
| 6308 | "01" /* HanshakeType : 0x01 = CLIENT HELLO */ |
| 6309 | "000075" /* HandshakeLength : 0x75 bytes after this one */ |
| 6310 | "0300" /* Hello Version : 0x0300 = v3 */ |
| 6311 | "%[date(),htonl,hex]" /* Unix GMT Time (s) : filled with <now> (@0x0B) */ |
| 6312 | "%[str(HAPROXYSSLCHK\nHAPROXYSSLCHK\n),hex]" /* Random : must be exactly 28 bytes */ |
| 6313 | "00" /* Session ID length : empty (no session ID) */ |
| 6314 | "004E" /* Cipher Suite Length : 78 bytes after this one */ |
| 6315 | "0001" "0002" "0003" "0004" /* 39 most common ciphers : */ |
| 6316 | "0005" "0006" "0007" "0008" /* 0x01...0x1B, 0x2F...0x3A */ |
| 6317 | "0009" "000A" "000B" "000C" /* This covers RSA/DH, */ |
| 6318 | "000D" "000E" "000F" "0010" /* various bit lengths, */ |
| 6319 | "0011" "0012" "0013" "0014" /* SHA1/MD5, DES/3DES/AES... */ |
| 6320 | "0015" "0016" "0017" "0018" |
| 6321 | "0019" "001A" "001B" "002F" |
| 6322 | "0030" "0031" "0032" "0033" |
| 6323 | "0034" "0035" "0036" "0037" |
| 6324 | "0038" "0039" "003A" |
| 6325 | "01" /* Compression Length : 0x01 = 1 byte for types */ |
| 6326 | "00" /* Compression Type : 0x00 = NULL compression */ |
| 6327 | }; |
| 6328 | |
| 6329 | struct tcpcheck_ruleset *rs = NULL; |
| 6330 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6331 | struct tcpcheck_rule *chk; |
| 6332 | char *errmsg = NULL; |
| 6333 | int err_code = 0; |
| 6334 | |
| 6335 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6336 | err_code |= ERR_WARN; |
| 6337 | |
| 6338 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 6339 | goto out; |
| 6340 | |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6341 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6342 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6343 | |
| 6344 | free_tcpcheck_vars(&rules->preset_vars); |
| 6345 | rules->list = NULL; |
| 6346 | rules->flags = 0; |
| 6347 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6348 | rs = find_tcpcheck_ruleset("*ssl-hello-check"); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6349 | if (rs) |
| 6350 | goto ruleset_found; |
| 6351 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6352 | rs = create_tcpcheck_ruleset("*ssl-hello-check"); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6353 | if (rs == NULL) { |
| 6354 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6355 | goto error; |
| 6356 | } |
| 6357 | |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 6358 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary-lf", sslv3_client_hello, ""}, |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6359 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6360 | if (!chk) { |
| 6361 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6362 | goto error; |
| 6363 | } |
| 6364 | chk->index = 0; |
| 6365 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6366 | |
| 6367 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^1[56]", |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 6368 | "min-recv", "5", "ok-status", "L6OK", |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6369 | "error-status", "L6RSP", "tout-status", "L6TOUT", |
| 6370 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6371 | 1, curpx, &rs->rules, TCPCHK_RULES_SSL3_CHK, file, line, &errmsg); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6372 | if (!chk) { |
| 6373 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6374 | goto error; |
| 6375 | } |
| 6376 | chk->index = 1; |
| 6377 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6378 | |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6379 | ruleset_found: |
| 6380 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6381 | rules->flags |= TCPCHK_RULES_SSL3_CHK; |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6382 | |
| 6383 | out: |
| 6384 | free(errmsg); |
| 6385 | return err_code; |
| 6386 | |
| 6387 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6388 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6389 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6390 | goto out; |
| 6391 | } |
| 6392 | |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6393 | /* Parses the "option smtpchk" proxy keyword */ |
| 6394 | int proxy_parse_smtpchk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6395 | const char *file, int line) |
| 6396 | { |
| 6397 | static char *smtp_req = "%[var(check.smtp_cmd)]\r\n"; |
| 6398 | |
| 6399 | struct tcpcheck_ruleset *rs = NULL; |
| 6400 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6401 | struct tcpcheck_rule *chk; |
| 6402 | struct tcpcheck_var *var = NULL; |
| 6403 | char *cmd = NULL, *errmsg = NULL; |
| 6404 | int err_code = 0; |
| 6405 | |
| 6406 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6407 | err_code |= ERR_WARN; |
| 6408 | |
| 6409 | if (alertif_too_many_args_idx(2, 1, file, line, args, &err_code)) |
| 6410 | goto out; |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6411 | |
| 6412 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6413 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6414 | |
| 6415 | free_tcpcheck_vars(&rules->preset_vars); |
| 6416 | rules->list = NULL; |
| 6417 | rules->flags = 0; |
| 6418 | |
| 6419 | cur_arg += 2; |
| 6420 | if (*args[cur_arg] && *args[cur_arg+1] && |
| 6421 | (strcmp(args[cur_arg], "EHLO") == 0 || strcmp(args[cur_arg], "HELO") == 0)) { |
| 6422 | cmd = calloc(strlen(args[cur_arg]) + strlen(args[cur_arg+1]) + 1, sizeof(*cmd)); |
| 6423 | if (cmd) |
| 6424 | sprintf(cmd, "%s %s", args[cur_arg], args[cur_arg+1]); |
| 6425 | } |
| 6426 | else { |
| 6427 | /* this just hits the default for now, but you could potentially expand it to allow for other stuff |
| 6428 | though, it's unlikely you'd want to send anything other than an EHLO or HELO */ |
| 6429 | cmd = strdup("HELO localhost"); |
| 6430 | } |
| 6431 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6432 | var = create_tcpcheck_var(ist("check.smtp_cmd")); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6433 | if (cmd == NULL || var == NULL) { |
| 6434 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6435 | goto error; |
| 6436 | } |
| 6437 | var->data.type = SMP_T_STR; |
| 6438 | var->data.u.str.area = cmd; |
| 6439 | var->data.u.str.data = strlen(cmd); |
| 6440 | LIST_INIT(&var->list); |
| 6441 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6442 | cmd = NULL; |
| 6443 | var = NULL; |
| 6444 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6445 | rs = find_tcpcheck_ruleset("*smtp-check"); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6446 | if (rs) |
| 6447 | goto ruleset_found; |
| 6448 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6449 | rs = create_tcpcheck_ruleset("*smtp-check"); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6450 | if (rs == NULL) { |
| 6451 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6452 | goto error; |
| 6453 | } |
| 6454 | |
| 6455 | chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""}, |
| 6456 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6457 | if (!chk) { |
| 6458 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6459 | goto error; |
| 6460 | } |
| 6461 | chk->index = 0; |
| 6462 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6463 | |
| 6464 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^[0-9]{3}[ \r]", |
| 6465 | "min-recv", "4", |
| 6466 | "error-status", "L7RSP", |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 6467 | "on-error", "%[res.payload(0,0),cut_crlf]", |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6468 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6469 | 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6470 | if (!chk) { |
| 6471 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6472 | goto error; |
| 6473 | } |
| 6474 | chk->index = 1; |
| 6475 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6476 | |
| 6477 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^2[0-9]{2}[ \r]", |
| 6478 | "min-recv", "4", |
| 6479 | "error-status", "L7STS", |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 6480 | "on-error", "%[res.payload(4,0),ltrim(' '),cut_crlf]", |
| 6481 | "status-code", "res.payload(0,3)", |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6482 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6483 | 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6484 | if (!chk) { |
| 6485 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6486 | goto error; |
| 6487 | } |
| 6488 | chk->index = 2; |
| 6489 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6490 | |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 6491 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", smtp_req, ""}, |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6492 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6493 | if (!chk) { |
| 6494 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6495 | goto error; |
| 6496 | } |
| 6497 | chk->index = 3; |
| 6498 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6499 | |
| 6500 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^2[0-9]{2}[- \r]", |
| 6501 | "min-recv", "4", |
| 6502 | "error-status", "L7STS", |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 6503 | "on-error", "%[res.payload(4,0),ltrim(' '),cut_crlf]", |
| 6504 | "on-success", "%[res.payload(4,0),ltrim(' '),cut_crlf]", |
| 6505 | "status-code", "res.payload(0,3)", |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6506 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6507 | 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6508 | if (!chk) { |
| 6509 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6510 | goto error; |
| 6511 | } |
| 6512 | chk->index = 4; |
| 6513 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6514 | |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6515 | ruleset_found: |
| 6516 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6517 | rules->flags |= TCPCHK_RULES_SMTP_CHK; |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6518 | |
| 6519 | out: |
| 6520 | free(errmsg); |
| 6521 | return err_code; |
| 6522 | |
| 6523 | error: |
| 6524 | free(cmd); |
| 6525 | free(var); |
| 6526 | free_tcpcheck_vars(&rules->preset_vars); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6527 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6528 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6529 | goto out; |
| 6530 | } |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6531 | |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6532 | /* Parses the "option pgsql-check" proxy keyword */ |
| 6533 | int proxy_parse_pgsql_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6534 | const char *file, int line) |
| 6535 | { |
| 6536 | static char pgsql_req[] = { |
| 6537 | "%[var(check.plen),htonl,hex]" /* The packet length*/ |
| 6538 | "00030000" /* the version 3.0 */ |
| 6539 | "7573657200" /* "user" key */ |
| 6540 | "%[var(check.username),hex]00" /* the username */ |
| 6541 | "00" |
| 6542 | }; |
| 6543 | |
| 6544 | struct tcpcheck_ruleset *rs = NULL; |
| 6545 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6546 | struct tcpcheck_rule *chk; |
| 6547 | struct tcpcheck_var *var = NULL; |
| 6548 | char *user = NULL, *errmsg = NULL; |
| 6549 | size_t packetlen = 0; |
| 6550 | int err_code = 0; |
| 6551 | |
| 6552 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6553 | err_code |= ERR_WARN; |
| 6554 | |
| 6555 | if (alertif_too_many_args_idx(2, 1, file, line, args, &err_code)) |
| 6556 | goto out; |
| 6557 | |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6558 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6559 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6560 | |
| 6561 | free_tcpcheck_vars(&rules->preset_vars); |
| 6562 | rules->list = NULL; |
| 6563 | rules->flags = 0; |
| 6564 | |
| 6565 | cur_arg += 2; |
| 6566 | if (!*args[cur_arg] || !*args[cur_arg+1]) { |
| 6567 | ha_alert("parsing [%s:%d] : '%s %s' expects 'user <username>' as argument.\n", |
| 6568 | file, line, args[0], args[1]); |
| 6569 | goto error; |
| 6570 | } |
| 6571 | if (strcmp(args[cur_arg], "user") == 0) { |
| 6572 | packetlen = 15 + strlen(args[cur_arg+1]); |
| 6573 | user = strdup(args[cur_arg+1]); |
| 6574 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6575 | var = create_tcpcheck_var(ist("check.username")); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6576 | if (user == NULL || var == NULL) { |
| 6577 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6578 | goto error; |
| 6579 | } |
| 6580 | var->data.type = SMP_T_STR; |
| 6581 | var->data.u.str.area = user; |
| 6582 | var->data.u.str.data = strlen(user); |
| 6583 | LIST_INIT(&var->list); |
| 6584 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6585 | user = NULL; |
| 6586 | var = NULL; |
| 6587 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6588 | var = create_tcpcheck_var(ist("check.plen")); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6589 | if (var == NULL) { |
| 6590 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6591 | goto error; |
| 6592 | } |
| 6593 | var->data.type = SMP_T_SINT; |
| 6594 | var->data.u.sint = packetlen; |
| 6595 | LIST_INIT(&var->list); |
| 6596 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6597 | var = NULL; |
| 6598 | } |
| 6599 | else { |
| 6600 | ha_alert("parsing [%s:%d] : '%s %s' only supports optional values: 'user'.\n", |
| 6601 | file, line, args[0], args[1]); |
| 6602 | goto error; |
| 6603 | } |
| 6604 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6605 | rs = find_tcpcheck_ruleset("*pgsql-check"); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6606 | if (rs) |
| 6607 | goto ruleset_found; |
| 6608 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6609 | rs = create_tcpcheck_ruleset("*pgsql-check"); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6610 | if (rs == NULL) { |
| 6611 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6612 | goto error; |
| 6613 | } |
| 6614 | |
| 6615 | chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""}, |
| 6616 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6617 | if (!chk) { |
| 6618 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6619 | goto error; |
| 6620 | } |
| 6621 | chk->index = 0; |
| 6622 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6623 | |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 6624 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary-lf", pgsql_req, ""}, |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6625 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6626 | if (!chk) { |
| 6627 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6628 | goto error; |
| 6629 | } |
| 6630 | chk->index = 1; |
| 6631 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6632 | |
| 6633 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "!rstring", "^E", |
| 6634 | "min-recv", "5", |
| 6635 | "error-status", "L7RSP", |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 6636 | "on-error", "%[res.payload(6,0)]", |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6637 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6638 | 1, curpx, &rs->rules, TCPCHK_RULES_PGSQL_CHK, file, line, &errmsg); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6639 | if (!chk) { |
| 6640 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6641 | goto error; |
| 6642 | } |
| 6643 | chk->index = 2; |
| 6644 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6645 | |
Christopher Faulet | b841c74 | 2020-04-27 18:29:49 +0200 | [diff] [blame] | 6646 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^52000000(08|0A|0C)000000(00|02|03|04|05|06)", |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6647 | "min-recv", "9", |
| 6648 | "error-status", "L7STS", |
| 6649 | "on-success", "PostgreSQL server is ok", |
| 6650 | "on-error", "PostgreSQL unknown error", |
| 6651 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6652 | 1, curpx, &rs->rules, TCPCHK_RULES_PGSQL_CHK, file, line, &errmsg); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6653 | if (!chk) { |
| 6654 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6655 | goto error; |
| 6656 | } |
| 6657 | chk->index = 3; |
| 6658 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6659 | |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6660 | ruleset_found: |
| 6661 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6662 | rules->flags |= TCPCHK_RULES_PGSQL_CHK; |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6663 | |
| 6664 | out: |
| 6665 | free(errmsg); |
| 6666 | return err_code; |
| 6667 | |
| 6668 | error: |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6669 | free(user); |
| 6670 | free(var); |
| 6671 | free_tcpcheck_vars(&rules->preset_vars); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6672 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6673 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6674 | goto out; |
| 6675 | } |
| 6676 | |
| 6677 | |
| 6678 | /* Parses the "option mysql-check" proxy keyword */ |
| 6679 | int proxy_parse_mysql_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6680 | const char *file, int line) |
| 6681 | { |
| 6682 | /* This is an example of a MySQL >=4.0 client Authentication packet kindly provided by Cyril Bonte. |
| 6683 | * const char mysql40_client_auth_pkt[] = { |
| 6684 | * "\x0e\x00\x00" // packet length |
| 6685 | * "\x01" // packet number |
| 6686 | * "\x00\x00" // client capabilities |
| 6687 | * "\x00\x00\x01" // max packet |
| 6688 | * "haproxy\x00" // username (null terminated string) |
| 6689 | * "\x00" // filler (always 0x00) |
| 6690 | * "\x01\x00\x00" // packet length |
| 6691 | * "\x00" // packet number |
| 6692 | * "\x01" // COM_QUIT command |
| 6693 | * }; |
| 6694 | */ |
| 6695 | static char mysql40_rsname[] = "*mysql40-check"; |
| 6696 | static char mysql40_req[] = { |
| 6697 | "%[var(check.header),hex]" /* 3 bytes for the packet length and 1 byte for the sequence ID */ |
| 6698 | "0080" /* client capabilities */ |
| 6699 | "000001" /* max packet */ |
| 6700 | "%[var(check.username),hex]00" /* the username */ |
| 6701 | "00" /* filler (always 0x00) */ |
| 6702 | "010000" /* packet length*/ |
| 6703 | "00" /* sequence ID */ |
| 6704 | "01" /* COM_QUIT command */ |
| 6705 | }; |
| 6706 | |
| 6707 | /* This is an example of a MySQL >=4.1 client Authentication packet provided by Nenad Merdanovic. |
| 6708 | * const char mysql41_client_auth_pkt[] = { |
| 6709 | * "\x0e\x00\x00\" // packet length |
| 6710 | * "\x01" // packet number |
| 6711 | * "\x00\x00\x00\x00" // client capabilities |
| 6712 | * "\x00\x00\x00\x01" // max packet |
| 6713 | * "\x21" // character set (UTF-8) |
| 6714 | * char[23] // All zeroes |
| 6715 | * "haproxy\x00" // username (null terminated string) |
| 6716 | * "\x00" // filler (always 0x00) |
| 6717 | * "\x01\x00\x00" // packet length |
| 6718 | * "\x00" // packet number |
| 6719 | * "\x01" // COM_QUIT command |
| 6720 | * }; |
| 6721 | */ |
| 6722 | static char mysql41_rsname[] = "*mysql41-check"; |
| 6723 | static char mysql41_req[] = { |
| 6724 | "%[var(check.header),hex]" /* 3 bytes for the packet length and 1 byte for the sequence ID */ |
| 6725 | "00820000" /* client capabilities */ |
| 6726 | "00800001" /* max packet */ |
| 6727 | "21" /* character set (UTF-8) */ |
| 6728 | "000000000000000000000000" /* 23 bytes, al zeroes */ |
| 6729 | "0000000000000000000000" |
| 6730 | "%[var(check.username),hex]00" /* the username */ |
| 6731 | "00" /* filler (always 0x00) */ |
| 6732 | "010000" /* packet length*/ |
| 6733 | "00" /* sequence ID */ |
| 6734 | "01" /* COM_QUIT command */ |
| 6735 | }; |
| 6736 | |
| 6737 | struct tcpcheck_ruleset *rs = NULL; |
| 6738 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6739 | struct tcpcheck_rule *chk; |
| 6740 | struct tcpcheck_var *var = NULL; |
| 6741 | char *mysql_rsname = "*mysql-check"; |
| 6742 | char *mysql_req = NULL, *hdr = NULL, *user = NULL, *errmsg = NULL; |
| 6743 | int index = 0, err_code = 0; |
| 6744 | |
| 6745 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6746 | err_code |= ERR_WARN; |
| 6747 | |
| 6748 | if (alertif_too_many_args_idx(3, 1, file, line, args, &err_code)) |
| 6749 | goto out; |
| 6750 | |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6751 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6752 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6753 | |
| 6754 | free_tcpcheck_vars(&rules->preset_vars); |
| 6755 | rules->list = NULL; |
| 6756 | rules->flags = 0; |
| 6757 | |
| 6758 | cur_arg += 2; |
| 6759 | if (*args[cur_arg]) { |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6760 | int packetlen, userlen; |
| 6761 | |
| 6762 | if (strcmp(args[cur_arg], "user") != 0) { |
| 6763 | ha_alert("parsing [%s:%d] : '%s %s' only supports optional values: 'user' (got '%s').\n", |
| 6764 | file, line, args[0], args[1], args[cur_arg]); |
| 6765 | goto error; |
| 6766 | } |
| 6767 | |
| 6768 | if (*(args[cur_arg+1]) == 0) { |
| 6769 | ha_alert("parsing [%s:%d] : '%s %s %s' expects <username> as argument.\n", |
| 6770 | file, line, args[0], args[1], args[cur_arg]); |
| 6771 | goto error; |
| 6772 | } |
| 6773 | |
| 6774 | hdr = calloc(4, sizeof(*hdr)); |
| 6775 | user = strdup(args[cur_arg+1]); |
| 6776 | userlen = strlen(args[cur_arg+1]); |
| 6777 | |
| 6778 | if (hdr == NULL || user == NULL) { |
| 6779 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6780 | goto error; |
| 6781 | } |
| 6782 | |
Christopher Faulet | 62f79fe | 2020-05-18 18:13:03 +0200 | [diff] [blame] | 6783 | if (!*args[cur_arg+2] || strcmp(args[cur_arg+2], "post-41") == 0) { |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6784 | packetlen = userlen + 7 + 27; |
| 6785 | mysql_req = mysql41_req; |
| 6786 | mysql_rsname = mysql41_rsname; |
| 6787 | } |
Christopher Faulet | 62f79fe | 2020-05-18 18:13:03 +0200 | [diff] [blame] | 6788 | else if (strcmp(args[cur_arg+2], "pre-41") == 0) { |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6789 | packetlen = userlen + 7; |
| 6790 | mysql_req = mysql40_req; |
| 6791 | mysql_rsname = mysql40_rsname; |
| 6792 | } |
Christopher Faulet | 62f79fe | 2020-05-18 18:13:03 +0200 | [diff] [blame] | 6793 | else { |
| 6794 | ha_alert("parsing [%s:%d] : keyword '%s' only supports 'post-41' and 'pre-41' (got '%s').\n", |
| 6795 | file, line, args[cur_arg], args[cur_arg+2]); |
| 6796 | goto error; |
| 6797 | } |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6798 | |
| 6799 | hdr[0] = (unsigned char)(packetlen & 0xff); |
| 6800 | hdr[1] = (unsigned char)((packetlen >> 8) & 0xff); |
| 6801 | hdr[2] = (unsigned char)((packetlen >> 16) & 0xff); |
| 6802 | hdr[3] = 1; |
| 6803 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6804 | var = create_tcpcheck_var(ist("check.header")); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6805 | if (var == NULL) { |
| 6806 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6807 | goto error; |
| 6808 | } |
| 6809 | var->data.type = SMP_T_STR; |
| 6810 | var->data.u.str.area = hdr; |
| 6811 | var->data.u.str.data = 4; |
| 6812 | LIST_INIT(&var->list); |
| 6813 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6814 | hdr = NULL; |
| 6815 | var = NULL; |
| 6816 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 6817 | var = create_tcpcheck_var(ist("check.username")); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6818 | if (var == NULL) { |
| 6819 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6820 | goto error; |
| 6821 | } |
| 6822 | var->data.type = SMP_T_STR; |
| 6823 | var->data.u.str.area = user; |
| 6824 | var->data.u.str.data = strlen(user); |
| 6825 | LIST_INIT(&var->list); |
| 6826 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6827 | user = NULL; |
| 6828 | var = NULL; |
| 6829 | } |
| 6830 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6831 | rs = find_tcpcheck_ruleset(mysql_rsname); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6832 | if (rs) |
| 6833 | goto ruleset_found; |
| 6834 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6835 | rs = create_tcpcheck_ruleset(mysql_rsname); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6836 | if (rs == NULL) { |
| 6837 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6838 | goto error; |
| 6839 | } |
| 6840 | |
| 6841 | chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""}, |
| 6842 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6843 | if (!chk) { |
| 6844 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6845 | goto error; |
| 6846 | } |
| 6847 | chk->index = index++; |
| 6848 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6849 | |
| 6850 | if (mysql_req) { |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 6851 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary-lf", mysql_req, ""}, |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6852 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6853 | if (!chk) { |
| 6854 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6855 | goto error; |
| 6856 | } |
| 6857 | chk->index = index++; |
| 6858 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6859 | } |
| 6860 | |
| 6861 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6862 | 1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6863 | if (!chk) { |
| 6864 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6865 | goto error; |
| 6866 | } |
| 6867 | chk->expect.custom = tcpcheck_mysql_expect_iniths; |
| 6868 | chk->index = index++; |
| 6869 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6870 | |
| 6871 | if (mysql_req) { |
| 6872 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6873 | 1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6874 | if (!chk) { |
| 6875 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6876 | goto error; |
| 6877 | } |
| 6878 | chk->expect.custom = tcpcheck_mysql_expect_ok; |
| 6879 | chk->index = index++; |
| 6880 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6881 | } |
| 6882 | |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6883 | ruleset_found: |
| 6884 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6885 | rules->flags |= TCPCHK_RULES_MYSQL_CHK; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6886 | |
| 6887 | out: |
| 6888 | free(errmsg); |
| 6889 | return err_code; |
| 6890 | |
| 6891 | error: |
| 6892 | free(hdr); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6893 | free(user); |
| 6894 | free(var); |
| 6895 | free_tcpcheck_vars(&rules->preset_vars); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6896 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6897 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6898 | goto out; |
| 6899 | } |
| 6900 | |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6901 | int proxy_parse_ldap_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6902 | const char *file, int line) |
| 6903 | { |
| 6904 | static char *ldap_req = "300C020101600702010304008000"; |
| 6905 | |
| 6906 | struct tcpcheck_ruleset *rs = NULL; |
| 6907 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6908 | struct tcpcheck_rule *chk; |
| 6909 | char *errmsg = NULL; |
| 6910 | int err_code = 0; |
| 6911 | |
| 6912 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6913 | err_code |= ERR_WARN; |
| 6914 | |
| 6915 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 6916 | goto out; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6917 | |
| 6918 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6919 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6920 | |
| 6921 | free_tcpcheck_vars(&rules->preset_vars); |
| 6922 | rules->list = NULL; |
| 6923 | rules->flags = 0; |
| 6924 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6925 | rs = find_tcpcheck_ruleset("*ldap-check"); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6926 | if (rs) |
| 6927 | goto ruleset_found; |
| 6928 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6929 | rs = create_tcpcheck_ruleset("*ldap-check"); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6930 | if (rs == NULL) { |
| 6931 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6932 | goto error; |
| 6933 | } |
| 6934 | |
| 6935 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", ldap_req, ""}, |
| 6936 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6937 | if (!chk) { |
| 6938 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6939 | goto error; |
| 6940 | } |
| 6941 | chk->index = 0; |
| 6942 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6943 | |
| 6944 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^30", |
| 6945 | "min-recv", "14", |
| 6946 | "on-error", "Not LDAPv3 protocol", |
| 6947 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6948 | 1, curpx, &rs->rules, TCPCHK_RULES_LDAP_CHK, file, line, &errmsg); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6949 | if (!chk) { |
| 6950 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6951 | goto error; |
| 6952 | } |
| 6953 | chk->index = 1; |
| 6954 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6955 | |
| 6956 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6957 | 1, curpx, &rs->rules, TCPCHK_RULES_LDAP_CHK, file, line, &errmsg); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6958 | if (!chk) { |
| 6959 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6960 | goto error; |
| 6961 | } |
| 6962 | chk->expect.custom = tcpcheck_ldap_expect_bindrsp; |
| 6963 | chk->index = 2; |
| 6964 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6965 | |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6966 | ruleset_found: |
| 6967 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6968 | rules->flags |= TCPCHK_RULES_LDAP_CHK; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6969 | |
| 6970 | out: |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6971 | free(errmsg); |
| 6972 | return err_code; |
| 6973 | |
| 6974 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 6975 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6976 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6977 | goto out; |
| 6978 | } |
| 6979 | |
| 6980 | int proxy_parse_spop_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6981 | const char *file, int line) |
| 6982 | { |
| 6983 | struct tcpcheck_ruleset *rs = NULL; |
| 6984 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6985 | struct tcpcheck_rule *chk; |
| 6986 | char *spop_req = NULL; |
| 6987 | char *errmsg = NULL; |
| 6988 | int spop_len = 0, err_code = 0; |
| 6989 | |
| 6990 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6991 | err_code |= ERR_WARN; |
| 6992 | |
| 6993 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 6994 | goto out; |
| 6995 | |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6996 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6997 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6998 | |
| 6999 | free_tcpcheck_vars(&rules->preset_vars); |
| 7000 | rules->list = NULL; |
| 7001 | rules->flags = 0; |
| 7002 | |
| 7003 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 7004 | rs = find_tcpcheck_ruleset("*spop-check"); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 7005 | if (rs) |
| 7006 | goto ruleset_found; |
| 7007 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 7008 | rs = create_tcpcheck_ruleset("*spop-check"); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 7009 | if (rs == NULL) { |
| 7010 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 7011 | goto error; |
| 7012 | } |
| 7013 | |
| 7014 | if (spoe_prepare_healthcheck_request(&spop_req, &spop_len) == -1) { |
| 7015 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 7016 | goto error; |
| 7017 | } |
| 7018 | chunk_reset(&trash); |
| 7019 | dump_binary(&trash, spop_req, spop_len); |
| 7020 | trash.area[trash.data] = '\0'; |
| 7021 | |
| 7022 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", b_head(&trash), ""}, |
| 7023 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 7024 | if (!chk) { |
| 7025 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 7026 | goto error; |
| 7027 | } |
| 7028 | chk->index = 0; |
| 7029 | LIST_ADDQ(&rs->rules, &chk->list); |
| 7030 | |
| 7031 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", "min-recv", "4", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7032 | 1, curpx, &rs->rules, TCPCHK_RULES_SPOP_CHK, file, line, &errmsg); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 7033 | if (!chk) { |
| 7034 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 7035 | goto error; |
| 7036 | } |
| 7037 | chk->expect.custom = tcpcheck_spop_expect_agenthello; |
| 7038 | chk->index = 1; |
| 7039 | LIST_ADDQ(&rs->rules, &chk->list); |
| 7040 | |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 7041 | ruleset_found: |
| 7042 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 7043 | rules->flags |= TCPCHK_RULES_SPOP_CHK; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 7044 | |
| 7045 | out: |
| 7046 | free(spop_req); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 7047 | free(errmsg); |
| 7048 | return err_code; |
| 7049 | |
| 7050 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 7051 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 7052 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7053 | goto out; |
| 7054 | } |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 7055 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7056 | |
| 7057 | struct tcpcheck_rule *proxy_parse_httpchk_req(char **args, int cur_arg, struct proxy *px, char **errmsg) |
| 7058 | { |
| 7059 | struct tcpcheck_rule *chk = NULL; |
| 7060 | struct tcpcheck_http_hdr *hdr = NULL; |
| 7061 | char *meth = NULL, *uri = NULL, *vsn = NULL; |
| 7062 | char *hdrs, *body; |
| 7063 | |
| 7064 | hdrs = (*args[cur_arg+2] ? strstr(args[cur_arg+2], "\r\n") : NULL); |
| 7065 | body = (*args[cur_arg+2] ? strstr(args[cur_arg+2], "\r\n\r\n") : NULL); |
| 7066 | if (hdrs == body) |
| 7067 | hdrs = NULL; |
| 7068 | if (hdrs) { |
| 7069 | *hdrs = '\0'; |
| 7070 | hdrs +=2; |
| 7071 | } |
| 7072 | if (body) { |
| 7073 | *body = '\0'; |
| 7074 | body += 4; |
| 7075 | } |
| 7076 | if (hdrs || body) { |
| 7077 | memprintf(errmsg, "hiding headers or body at the end of the version string is deprecated." |
| 7078 | " Please, consider to use 'http-check send' directive instead."); |
| 7079 | } |
| 7080 | |
| 7081 | chk = calloc(1, sizeof(*chk)); |
| 7082 | if (!chk) { |
| 7083 | memprintf(errmsg, "out of memory"); |
| 7084 | goto error; |
| 7085 | } |
| 7086 | chk->action = TCPCHK_ACT_SEND; |
| 7087 | chk->send.type = TCPCHK_SEND_HTTP; |
| 7088 | chk->send.http.flags |= TCPCHK_SND_HTTP_FROM_OPT; |
| 7089 | chk->send.http.meth.meth = HTTP_METH_OPTIONS; |
| 7090 | LIST_INIT(&chk->send.http.hdrs); |
| 7091 | |
| 7092 | /* Copy the method, uri and version */ |
| 7093 | if (*args[cur_arg]) { |
| 7094 | if (!*args[cur_arg+1]) |
| 7095 | uri = args[cur_arg]; |
| 7096 | else |
| 7097 | meth = args[cur_arg]; |
| 7098 | } |
| 7099 | if (*args[cur_arg+1]) |
| 7100 | uri = args[cur_arg+1]; |
| 7101 | if (*args[cur_arg+2]) |
| 7102 | vsn = args[cur_arg+2]; |
| 7103 | |
| 7104 | if (meth) { |
| 7105 | chk->send.http.meth.meth = find_http_meth(meth, strlen(meth)); |
| 7106 | chk->send.http.meth.str.area = strdup(meth); |
| 7107 | chk->send.http.meth.str.data = strlen(meth); |
| 7108 | if (!chk->send.http.meth.str.area) { |
| 7109 | memprintf(errmsg, "out of memory"); |
| 7110 | goto error; |
| 7111 | } |
| 7112 | } |
| 7113 | if (uri) { |
| 7114 | chk->send.http.uri = ist2(strdup(uri), strlen(uri)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 7115 | if (!isttest(chk->send.http.uri)) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7116 | memprintf(errmsg, "out of memory"); |
| 7117 | goto error; |
| 7118 | } |
| 7119 | } |
| 7120 | if (vsn) { |
| 7121 | chk->send.http.vsn = ist2(strdup(vsn), strlen(vsn)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 7122 | if (!isttest(chk->send.http.vsn)) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7123 | memprintf(errmsg, "out of memory"); |
| 7124 | goto error; |
| 7125 | } |
| 7126 | } |
| 7127 | |
| 7128 | /* Copy the header */ |
| 7129 | if (hdrs) { |
| 7130 | struct http_hdr tmp_hdrs[global.tune.max_http_hdr]; |
| 7131 | struct h1m h1m; |
| 7132 | int i, ret; |
| 7133 | |
| 7134 | /* Build and parse the request */ |
| 7135 | chunk_printf(&trash, "%s\r\n\r\n", hdrs); |
| 7136 | |
| 7137 | h1m.flags = H1_MF_HDRS_ONLY; |
| 7138 | ret = h1_headers_to_hdr_list(b_orig(&trash), b_tail(&trash), |
| 7139 | tmp_hdrs, sizeof(tmp_hdrs)/sizeof(tmp_hdrs[0]), |
| 7140 | &h1m, NULL); |
| 7141 | if (ret <= 0) { |
| 7142 | memprintf(errmsg, "unable to parse the request '%s'.", b_orig(&trash)); |
| 7143 | goto error; |
| 7144 | } |
| 7145 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 7146 | for (i = 0; istlen(tmp_hdrs[i].n); i++) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7147 | hdr = calloc(1, sizeof(*hdr)); |
| 7148 | if (!hdr) { |
| 7149 | memprintf(errmsg, "out of memory"); |
| 7150 | goto error; |
| 7151 | } |
| 7152 | LIST_INIT(&hdr->value); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 7153 | hdr->name = istdup(tmp_hdrs[i].n); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7154 | if (!hdr->name.ptr) { |
| 7155 | memprintf(errmsg, "out of memory"); |
| 7156 | goto error; |
| 7157 | } |
| 7158 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 7159 | ist0(tmp_hdrs[i].v); |
| 7160 | 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] | 7161 | goto error; |
| 7162 | LIST_ADDQ(&chk->send.http.hdrs, &hdr->list); |
| 7163 | } |
| 7164 | } |
| 7165 | |
| 7166 | /* Copy the body */ |
| 7167 | if (body) { |
| 7168 | chk->send.http.body = ist2(strdup(body), strlen(body)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 7169 | if (!isttest(chk->send.http.body)) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7170 | memprintf(errmsg, "out of memory"); |
| 7171 | goto error; |
| 7172 | } |
| 7173 | } |
| 7174 | |
| 7175 | return chk; |
| 7176 | |
| 7177 | error: |
| 7178 | free_tcpcheck_http_hdr(hdr); |
| 7179 | free_tcpcheck(chk, 0); |
| 7180 | return NULL; |
| 7181 | } |
| 7182 | |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7183 | int proxy_parse_httpchk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 7184 | const char *file, int line) |
| 7185 | { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7186 | struct tcpcheck_ruleset *rs = NULL; |
| 7187 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 7188 | struct tcpcheck_rule *chk; |
| 7189 | char *errmsg = NULL; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7190 | int err_code = 0; |
| 7191 | |
| 7192 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 7193 | err_code |= ERR_WARN; |
| 7194 | |
| 7195 | if (alertif_too_many_args_idx(3, 1, file, line, args, &err_code)) |
| 7196 | goto out; |
| 7197 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7198 | chk = proxy_parse_httpchk_req(args, cur_arg+2, curpx, &errmsg); |
| 7199 | if (!chk) { |
| 7200 | ha_alert("parsing [%s:%d] : '%s %s' : %s.\n", file, line, args[0], args[1], errmsg); |
| 7201 | goto error; |
| 7202 | } |
| 7203 | if (errmsg) { |
| 7204 | ha_warning("parsing [%s:%d]: '%s %s' : %s\n", file, line, args[0], args[1], errmsg); |
| 7205 | err_code |= ERR_WARN; |
| 7206 | free(errmsg); |
| 7207 | errmsg = NULL; |
| 7208 | } |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7209 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7210 | no_request: |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7211 | curpx->options2 &= ~PR_O2_CHK_ANY; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7212 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7213 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7214 | free_tcpcheck_vars(&rules->preset_vars); |
| 7215 | rules->list = NULL; |
| 7216 | rules->flags |= TCPCHK_SND_HTTP_FROM_OPT; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7217 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7218 | /* Deduce the ruleset name from the proxy info */ |
| 7219 | chunk_printf(&trash, "*http-check-%s_%s-%d", |
| 7220 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 7221 | curpx->conf.file, curpx->conf.line); |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7222 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 7223 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7224 | if (rs == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 7225 | rs = create_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7226 | if (rs == NULL) { |
| 7227 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 7228 | goto error; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7229 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7230 | } |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7231 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7232 | rules->list = &rs->rules; |
| 7233 | rules->flags |= TCPCHK_RULES_HTTP_CHK; |
| 7234 | if (!tcpcheck_add_http_rule(chk, rules, &errmsg)) { |
| 7235 | ha_alert("parsing [%s:%d] : '%s %s' : %s.\n", file, line, args[0], args[1], errmsg); |
| 7236 | rules->list = NULL; |
| 7237 | goto error; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7238 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7239 | |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7240 | out: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7241 | free(errmsg); |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7242 | return err_code; |
| 7243 | |
| 7244 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 7245 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7246 | free_tcpcheck(chk, 0); |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 7247 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7248 | goto out; |
| 7249 | } |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7250 | |
Christopher Faulet | 6f55791 | 2020-04-09 15:58:50 +0200 | [diff] [blame] | 7251 | int proxy_parse_external_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 7252 | const char *file, int line) |
| 7253 | { |
| 7254 | int err_code = 0; |
| 7255 | |
Christopher Faulet | 6f55791 | 2020-04-09 15:58:50 +0200 | [diff] [blame] | 7256 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 7257 | curpx->options2 |= PR_O2_EXT_CHK; |
| 7258 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 7259 | goto out; |
| 7260 | |
| 7261 | out: |
| 7262 | return err_code; |
| 7263 | } |
| 7264 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 7265 | /* Parse the "addr" server keyword */ |
| 7266 | static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7267 | char **errmsg) |
| 7268 | { |
| 7269 | struct sockaddr_storage *sk; |
| 7270 | struct protocol *proto; |
| 7271 | int port1, port2, err_code = 0; |
| 7272 | |
| 7273 | |
| 7274 | if (!*args[*cur_arg+1]) { |
| 7275 | memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]); |
| 7276 | goto error; |
| 7277 | } |
| 7278 | |
| 7279 | sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, errmsg, NULL, NULL, 1); |
| 7280 | if (!sk) { |
| 7281 | memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg); |
| 7282 | goto error; |
| 7283 | } |
| 7284 | |
| 7285 | proto = protocol_by_family(sk->ss_family); |
| 7286 | if (!proto || !proto->connect) { |
| 7287 | memprintf(errmsg, "'%s %s' : connect() not supported for this address family.", |
| 7288 | args[*cur_arg], args[*cur_arg+1]); |
| 7289 | goto error; |
| 7290 | } |
| 7291 | |
| 7292 | if (port1 != port2) { |
| 7293 | memprintf(errmsg, "'%s' : port ranges and offsets are not allowed in '%s'.", |
| 7294 | args[*cur_arg], args[*cur_arg+1]); |
| 7295 | goto error; |
| 7296 | } |
| 7297 | |
| 7298 | srv->check.addr = srv->agent.addr = *sk; |
| 7299 | srv->flags |= SRV_F_CHECKADDR; |
| 7300 | srv->flags |= SRV_F_AGENTADDR; |
| 7301 | |
| 7302 | out: |
| 7303 | return err_code; |
| 7304 | |
| 7305 | error: |
| 7306 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7307 | goto out; |
| 7308 | } |
| 7309 | |
| 7310 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7311 | /* Parse the "agent-addr" server keyword */ |
| 7312 | static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7313 | char **errmsg) |
| 7314 | { |
| 7315 | int err_code = 0; |
| 7316 | |
| 7317 | if (!*(args[*cur_arg+1])) { |
| 7318 | memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]); |
| 7319 | goto error; |
| 7320 | } |
| 7321 | if(str2ip(args[*cur_arg+1], &srv->agent.addr) == NULL) { |
| 7322 | memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]); |
| 7323 | goto error; |
| 7324 | } |
| 7325 | |
| 7326 | out: |
| 7327 | return err_code; |
| 7328 | |
| 7329 | error: |
| 7330 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7331 | goto out; |
| 7332 | } |
| 7333 | |
| 7334 | /* Parse the "agent-check" server keyword */ |
| 7335 | static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7336 | char **errmsg) |
| 7337 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7338 | struct tcpcheck_ruleset *rs = NULL; |
| 7339 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
| 7340 | struct tcpcheck_rule *chk; |
| 7341 | int err_code = 0; |
| 7342 | |
| 7343 | if (srv->do_agent) |
| 7344 | goto out; |
| 7345 | |
| 7346 | if (!rules) { |
| 7347 | rules = calloc(1, sizeof(*rules)); |
| 7348 | if (!rules) { |
| 7349 | memprintf(errmsg, "out of memory."); |
| 7350 | goto error; |
| 7351 | } |
| 7352 | LIST_INIT(&rules->preset_vars); |
| 7353 | srv->agent.tcpcheck_rules = rules; |
| 7354 | } |
| 7355 | rules->list = NULL; |
| 7356 | rules->flags = 0; |
| 7357 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 7358 | rs = find_tcpcheck_ruleset("*agent-check"); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7359 | if (rs) |
| 7360 | goto ruleset_found; |
| 7361 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 7362 | rs = create_tcpcheck_ruleset("*agent-check"); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7363 | if (rs == NULL) { |
| 7364 | memprintf(errmsg, "out of memory."); |
| 7365 | goto error; |
| 7366 | } |
| 7367 | |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 7368 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""}, |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7369 | 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg); |
| 7370 | if (!chk) { |
| 7371 | memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg); |
| 7372 | goto error; |
| 7373 | } |
| 7374 | chk->index = 0; |
| 7375 | LIST_ADDQ(&rs->rules, &chk->list); |
| 7376 | |
| 7377 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 7378 | 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK, |
| 7379 | srv->conf.file, srv->conf.line, errmsg); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7380 | if (!chk) { |
| 7381 | memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg); |
| 7382 | goto error; |
| 7383 | } |
| 7384 | chk->expect.custom = tcpcheck_agent_expect_reply; |
| 7385 | chk->index = 1; |
| 7386 | LIST_ADDQ(&rs->rules, &chk->list); |
| 7387 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7388 | ruleset_found: |
| 7389 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 7390 | rules->flags |= TCPCHK_RULES_AGENT_CHK; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7391 | srv->do_agent = 1; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7392 | |
| 7393 | out: |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7394 | return 0; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7395 | |
| 7396 | error: |
| 7397 | deinit_srv_agent_check(srv); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 7398 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7399 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7400 | goto out; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7401 | } |
| 7402 | |
| 7403 | /* Parse the "agent-inter" server keyword */ |
| 7404 | static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7405 | char **errmsg) |
| 7406 | { |
| 7407 | const char *err = NULL; |
| 7408 | unsigned int delay; |
| 7409 | int err_code = 0; |
| 7410 | |
| 7411 | if (!*(args[*cur_arg+1])) { |
| 7412 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 7413 | goto error; |
| 7414 | } |
| 7415 | |
| 7416 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 7417 | if (err == PARSE_TIME_OVER) { |
| 7418 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 7419 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7420 | goto error; |
| 7421 | } |
| 7422 | else if (err == PARSE_TIME_UNDER) { |
| 7423 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 7424 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7425 | goto error; |
| 7426 | } |
| 7427 | else if (err) { |
| 7428 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 7429 | *err, srv->id); |
| 7430 | goto error; |
| 7431 | } |
| 7432 | if (delay <= 0) { |
| 7433 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 7434 | delay, args[*cur_arg], srv->id); |
| 7435 | goto error; |
| 7436 | } |
| 7437 | srv->agent.inter = delay; |
| 7438 | |
| 7439 | out: |
| 7440 | return err_code; |
| 7441 | |
| 7442 | error: |
| 7443 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7444 | goto out; |
| 7445 | } |
| 7446 | |
| 7447 | /* Parse the "agent-port" server keyword */ |
| 7448 | static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7449 | char **errmsg) |
| 7450 | { |
| 7451 | int err_code = 0; |
| 7452 | |
| 7453 | if (!*(args[*cur_arg+1])) { |
| 7454 | memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]); |
| 7455 | goto error; |
| 7456 | } |
| 7457 | |
| 7458 | global.maxsock++; |
| 7459 | srv->agent.port = atol(args[*cur_arg+1]); |
| 7460 | |
| 7461 | out: |
| 7462 | return err_code; |
| 7463 | |
| 7464 | error: |
| 7465 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7466 | goto out; |
| 7467 | } |
| 7468 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7469 | int set_srv_agent_send(struct server *srv, const char *send) |
| 7470 | { |
| 7471 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
| 7472 | struct tcpcheck_var *var = NULL; |
| 7473 | char *str; |
| 7474 | |
| 7475 | str = strdup(send); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 7476 | var = create_tcpcheck_var(ist("check.agent_string")); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7477 | if (str == NULL || var == NULL) |
| 7478 | goto error; |
| 7479 | |
| 7480 | free_tcpcheck_vars(&rules->preset_vars); |
| 7481 | |
| 7482 | var->data.type = SMP_T_STR; |
| 7483 | var->data.u.str.area = str; |
| 7484 | var->data.u.str.data = strlen(str); |
| 7485 | LIST_INIT(&var->list); |
| 7486 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 7487 | |
| 7488 | return 1; |
| 7489 | |
| 7490 | error: |
| 7491 | free(str); |
| 7492 | free(var); |
| 7493 | return 0; |
| 7494 | } |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7495 | |
| 7496 | /* Parse the "agent-send" server keyword */ |
| 7497 | static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7498 | char **errmsg) |
| 7499 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7500 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7501 | int err_code = 0; |
| 7502 | |
| 7503 | if (!*(args[*cur_arg+1])) { |
| 7504 | memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]); |
| 7505 | goto error; |
| 7506 | } |
| 7507 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7508 | if (!rules) { |
| 7509 | rules = calloc(1, sizeof(*rules)); |
| 7510 | if (!rules) { |
| 7511 | memprintf(errmsg, "out of memory."); |
| 7512 | goto error; |
| 7513 | } |
| 7514 | LIST_INIT(&rules->preset_vars); |
| 7515 | srv->agent.tcpcheck_rules = rules; |
| 7516 | } |
| 7517 | |
| 7518 | if (!set_srv_agent_send(srv, args[*cur_arg+1])) { |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7519 | memprintf(errmsg, "out of memory."); |
| 7520 | goto error; |
| 7521 | } |
| 7522 | |
| 7523 | out: |
| 7524 | return err_code; |
| 7525 | |
| 7526 | error: |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7527 | deinit_srv_agent_check(srv); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7528 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7529 | goto out; |
| 7530 | } |
| 7531 | |
| 7532 | /* Parse the "no-agent-send" server keyword */ |
| 7533 | static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7534 | char **errmsg) |
| 7535 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7536 | deinit_srv_agent_check(srv); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7537 | return 0; |
| 7538 | } |
| 7539 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 7540 | /* Parse the "check" server keyword */ |
| 7541 | static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7542 | char **errmsg) |
| 7543 | { |
| 7544 | srv->do_check = 1; |
| 7545 | return 0; |
| 7546 | } |
| 7547 | |
| 7548 | /* Parse the "check-send-proxy" server keyword */ |
| 7549 | static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7550 | char **errmsg) |
| 7551 | { |
| 7552 | srv->check.send_proxy = 1; |
| 7553 | return 0; |
| 7554 | } |
| 7555 | |
| 7556 | /* Parse the "check-via-socks4" server keyword */ |
| 7557 | static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7558 | char **errmsg) |
| 7559 | { |
| 7560 | srv->check.via_socks4 = 1; |
| 7561 | return 0; |
| 7562 | } |
| 7563 | |
| 7564 | /* Parse the "no-check" server keyword */ |
| 7565 | static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7566 | char **errmsg) |
| 7567 | { |
| 7568 | deinit_srv_check(srv); |
| 7569 | return 0; |
| 7570 | } |
| 7571 | |
| 7572 | /* Parse the "no-check-send-proxy" server keyword */ |
| 7573 | static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7574 | char **errmsg) |
| 7575 | { |
| 7576 | srv->check.send_proxy = 0; |
| 7577 | return 0; |
| 7578 | } |
| 7579 | |
Christopher Faulet | edc6ed9 | 2020-04-23 16:27:59 +0200 | [diff] [blame] | 7580 | /* parse the "check-proto" server keyword */ |
| 7581 | static int srv_parse_check_proto(char **args, int *cur_arg, |
| 7582 | struct proxy *px, struct server *newsrv, char **err) |
| 7583 | { |
| 7584 | int err_code = 0; |
| 7585 | |
| 7586 | if (!*args[*cur_arg + 1]) { |
| 7587 | memprintf(err, "'%s' : missing value", args[*cur_arg]); |
| 7588 | goto error; |
| 7589 | } |
| 7590 | newsrv->check.mux_proto = get_mux_proto(ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]))); |
| 7591 | if (!newsrv->check.mux_proto) { |
| 7592 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]); |
| 7593 | goto error; |
| 7594 | } |
| 7595 | |
| 7596 | out: |
| 7597 | return err_code; |
| 7598 | |
| 7599 | error: |
| 7600 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7601 | goto out; |
| 7602 | } |
| 7603 | |
| 7604 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 7605 | /* Parse the "rise" server keyword */ |
| 7606 | static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7607 | char **errmsg) |
| 7608 | { |
| 7609 | int err_code = 0; |
| 7610 | |
| 7611 | if (!*args[*cur_arg + 1]) { |
| 7612 | memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]); |
| 7613 | goto error; |
| 7614 | } |
| 7615 | |
| 7616 | srv->check.rise = atol(args[*cur_arg+1]); |
| 7617 | if (srv->check.rise <= 0) { |
| 7618 | memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]); |
| 7619 | goto error; |
| 7620 | } |
| 7621 | |
| 7622 | if (srv->check.health) |
| 7623 | srv->check.health = srv->check.rise; |
| 7624 | |
| 7625 | out: |
| 7626 | return err_code; |
| 7627 | |
| 7628 | error: |
| 7629 | deinit_srv_agent_check(srv); |
| 7630 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7631 | goto out; |
| 7632 | return 0; |
| 7633 | } |
| 7634 | |
| 7635 | /* Parse the "fall" server keyword */ |
| 7636 | static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7637 | char **errmsg) |
| 7638 | { |
| 7639 | int err_code = 0; |
| 7640 | |
| 7641 | if (!*args[*cur_arg + 1]) { |
| 7642 | memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]); |
| 7643 | goto error; |
| 7644 | } |
| 7645 | |
| 7646 | srv->check.fall = atol(args[*cur_arg+1]); |
| 7647 | if (srv->check.fall <= 0) { |
| 7648 | memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]); |
| 7649 | goto error; |
| 7650 | } |
| 7651 | |
| 7652 | out: |
| 7653 | return err_code; |
| 7654 | |
| 7655 | error: |
| 7656 | deinit_srv_agent_check(srv); |
| 7657 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7658 | goto out; |
| 7659 | return 0; |
| 7660 | } |
| 7661 | |
| 7662 | /* Parse the "inter" server keyword */ |
| 7663 | static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7664 | char **errmsg) |
| 7665 | { |
| 7666 | const char *err = NULL; |
| 7667 | unsigned int delay; |
| 7668 | int err_code = 0; |
| 7669 | |
| 7670 | if (!*(args[*cur_arg+1])) { |
| 7671 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 7672 | goto error; |
| 7673 | } |
| 7674 | |
| 7675 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 7676 | if (err == PARSE_TIME_OVER) { |
| 7677 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 7678 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7679 | goto error; |
| 7680 | } |
| 7681 | else if (err == PARSE_TIME_UNDER) { |
| 7682 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 7683 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7684 | goto error; |
| 7685 | } |
| 7686 | else if (err) { |
| 7687 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 7688 | *err, srv->id); |
| 7689 | goto error; |
| 7690 | } |
| 7691 | if (delay <= 0) { |
| 7692 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 7693 | delay, args[*cur_arg], srv->id); |
| 7694 | goto error; |
| 7695 | } |
| 7696 | srv->check.inter = delay; |
| 7697 | |
| 7698 | out: |
| 7699 | return err_code; |
| 7700 | |
| 7701 | error: |
| 7702 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7703 | goto out; |
| 7704 | } |
| 7705 | |
| 7706 | |
| 7707 | /* Parse the "fastinter" server keyword */ |
| 7708 | static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7709 | char **errmsg) |
| 7710 | { |
| 7711 | const char *err = NULL; |
| 7712 | unsigned int delay; |
| 7713 | int err_code = 0; |
| 7714 | |
| 7715 | if (!*(args[*cur_arg+1])) { |
| 7716 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 7717 | goto error; |
| 7718 | } |
| 7719 | |
| 7720 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 7721 | if (err == PARSE_TIME_OVER) { |
| 7722 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 7723 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7724 | goto error; |
| 7725 | } |
| 7726 | else if (err == PARSE_TIME_UNDER) { |
| 7727 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 7728 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7729 | goto error; |
| 7730 | } |
| 7731 | else if (err) { |
| 7732 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 7733 | *err, srv->id); |
| 7734 | goto error; |
| 7735 | } |
| 7736 | if (delay <= 0) { |
| 7737 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 7738 | delay, args[*cur_arg], srv->id); |
| 7739 | goto error; |
| 7740 | } |
| 7741 | srv->check.fastinter = delay; |
| 7742 | |
| 7743 | out: |
| 7744 | return err_code; |
| 7745 | |
| 7746 | error: |
| 7747 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7748 | goto out; |
| 7749 | } |
| 7750 | |
| 7751 | |
| 7752 | /* Parse the "downinter" server keyword */ |
| 7753 | static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7754 | char **errmsg) |
| 7755 | { |
| 7756 | const char *err = NULL; |
| 7757 | unsigned int delay; |
| 7758 | int err_code = 0; |
| 7759 | |
| 7760 | if (!*(args[*cur_arg+1])) { |
| 7761 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 7762 | goto error; |
| 7763 | } |
| 7764 | |
| 7765 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 7766 | if (err == PARSE_TIME_OVER) { |
| 7767 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 7768 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7769 | goto error; |
| 7770 | } |
| 7771 | else if (err == PARSE_TIME_UNDER) { |
| 7772 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 7773 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7774 | goto error; |
| 7775 | } |
| 7776 | else if (err) { |
| 7777 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 7778 | *err, srv->id); |
| 7779 | goto error; |
| 7780 | } |
| 7781 | if (delay <= 0) { |
| 7782 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 7783 | delay, args[*cur_arg], srv->id); |
| 7784 | goto error; |
| 7785 | } |
| 7786 | srv->check.downinter = delay; |
| 7787 | |
| 7788 | out: |
| 7789 | return err_code; |
| 7790 | |
| 7791 | error: |
| 7792 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7793 | goto out; |
| 7794 | } |
| 7795 | |
| 7796 | /* Parse the "port" server keyword */ |
| 7797 | static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7798 | char **errmsg) |
| 7799 | { |
| 7800 | int err_code = 0; |
| 7801 | |
| 7802 | if (!*(args[*cur_arg+1])) { |
| 7803 | memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]); |
| 7804 | goto error; |
| 7805 | } |
| 7806 | |
| 7807 | global.maxsock++; |
| 7808 | srv->check.port = atol(args[*cur_arg+1]); |
| 7809 | srv->flags |= SRV_F_CHECKPORT; |
| 7810 | |
| 7811 | out: |
| 7812 | return err_code; |
| 7813 | |
| 7814 | error: |
| 7815 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7816 | goto out; |
| 7817 | } |
| 7818 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 7819 | static struct cfg_kw_list cfg_kws = {ILH, { |
Christopher Faulet | e9111b6 | 2020-04-09 18:12:08 +0200 | [diff] [blame] | 7820 | { CFG_LISTEN, "tcp-check", proxy_parse_tcpcheck }, |
| 7821 | { CFG_LISTEN, "http-check", proxy_parse_httpcheck }, |
| 7822 | { CFG_LISTEN, "external-check", proxy_parse_extcheck }, |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 7823 | { 0, NULL, NULL }, |
| 7824 | }}; |
| 7825 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7826 | static struct srv_kw_list srv_kws = { "CHK", { }, { |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 7827 | { "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] | 7828 | { "agent-addr", srv_parse_agent_addr, 1, 1 }, /* Enable an auxiliary agent check */ |
| 7829 | { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable agent checks */ |
| 7830 | { "agent-inter", srv_parse_agent_inter, 1, 1 }, /* Set the interval between two agent checks */ |
| 7831 | { "agent-port", srv_parse_agent_port, 1, 1 }, /* Set the TCP port used for agent checks. */ |
| 7832 | { "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] | 7833 | { "check", srv_parse_check, 0, 1 }, /* Enable health checks */ |
Christopher Faulet | edc6ed9 | 2020-04-23 16:27:59 +0200 | [diff] [blame] | 7834 | { "check-proto", srv_parse_check_proto, 1, 1 }, /* Set the mux protocol for health checks */ |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 7835 | { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* Enable PROXY protocol for health checks */ |
| 7836 | { "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] | 7837 | { "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] | 7838 | { "no-check", srv_parse_no_check, 0, 1 }, /* Disable health checks */ |
| 7839 | { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* Disable PROXY protol for health checks */ |
| 7840 | { "rise", srv_parse_check_rise, 1, 1 }, /* Set rise value for health checks */ |
| 7841 | { "fall", srv_parse_check_fall, 1, 1 }, /* Set fall value for health checks */ |
| 7842 | { "inter", srv_parse_check_inter, 1, 1 }, /* Set inter value for health checks */ |
| 7843 | { "fastinter", srv_parse_check_fastinter, 1, 1 }, /* Set fastinter value for health checks */ |
| 7844 | { "downinter", srv_parse_check_downinter, 1, 1 }, /* Set downinter value for health checks */ |
| 7845 | { "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] | 7846 | { NULL, NULL, 0 }, |
| 7847 | }}; |
| 7848 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 7849 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7850 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 7851 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 7852 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7853 | * Local variables: |
| 7854 | * c-indent-level: 8 |
| 7855 | * c-basic-offset: 8 |
| 7856 | * End: |
| 7857 | */ |