blob: 5fdee40f132a757e4c7c2c9dc27ddff4f19e1c19 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Configuration parser
3 *
Willy Tarreauff011f22011-01-06 17:51:27 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaue5733232019-05-22 19:24:06 +020013#ifdef USE_LIBCRYPT
Cyril Bonté1a0191d2014-08-29 20:20:02 +020014/* This is to have crypt() defined on Linux */
15#define _GNU_SOURCE
16
Willy Tarreaue5733232019-05-22 19:24:06 +020017#ifdef USE_CRYPT_H
Cyril Bonté1a0191d2014-08-29 20:20:02 +020018/* some platforms such as Solaris need this */
19#include <crypt.h>
20#endif
Willy Tarreaue5733232019-05-22 19:24:06 +020021#endif /* USE_LIBCRYPT */
Cyril Bonté1a0191d2014-08-29 20:20:02 +020022
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +010023#include <dirent.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <netdb.h>
28#include <ctype.h>
Willy Tarreau95c20ac2007-03-25 15:39:23 +020029#include <pwd.h>
30#include <grp.h>
Willy Tarreau0b4ed902007-03-26 00:18:40 +020031#include <errno.h>
Willy Tarreau3f49b302007-06-11 00:29:26 +020032#include <sys/types.h>
33#include <sys/stat.h>
34#include <fcntl.h>
35#include <unistd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreaudcc048a2020-06-04 19:11:43 +020037#include <haproxy/acl.h>
Christopher Faulet42c6cf92021-03-25 17:19:04 +010038#include <haproxy/action.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/api.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020040#include <haproxy/auth.h>
Willy Tarreau49801602020-06-04 22:50:02 +020041#include <haproxy/backend.h>
Willy Tarreau278161c2020-06-04 11:18:28 +020042#include <haproxy/capture.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020043#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020044#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020045#include <haproxy/check.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020046#include <haproxy/chunk.h>
Amaury Denoyellea6f9c5d2021-04-23 16:58:08 +020047#ifdef USE_CPU_AFFINITY
Amaury Denoyellec90932b2021-04-14 16:16:03 +020048#include <haproxy/cpuset.h>
Amaury Denoyellea6f9c5d2021-04-23 16:58:08 +020049#endif
Willy Tarreau7ea393d2020-06-04 18:02:10 +020050#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020051#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020052#include <haproxy/filters.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020053#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020054#include <haproxy/global.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020055#include <haproxy/http_ana.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020056#include <haproxy/http_rules.h>
Willy Tarreaufbe8da32020-06-04 14:34:27 +020057#include <haproxy/lb_chash.h>
Willy Tarreaub5fc3bf2020-06-04 14:37:38 +020058#include <haproxy/lb_fas.h>
Willy Tarreau02549412020-06-04 14:41:04 +020059#include <haproxy/lb_fwlc.h>
Willy Tarreau546ba422020-06-04 14:45:03 +020060#include <haproxy/lb_fwrr.h>
Willy Tarreau28671592020-06-04 20:22:59 +020061#include <haproxy/lb_map.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020062#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020063#include <haproxy/log.h>
Willy Tarreaucee013e2020-06-05 11:40:38 +020064#include <haproxy/mailers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/namespace.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020066#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020067#include <haproxy/peers-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020068#include <haproxy/peers.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020069#include <haproxy/pool.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020070#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020071#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010072#include <haproxy/resolvers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020073#include <haproxy/sample.h>
74#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020075#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020076#include <haproxy/stats-t.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020077#include <haproxy/stick_table.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020078#include <haproxy/stream.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020079#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020080#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020081#include <haproxy/thread.h>
82#include <haproxy/time.h>
83#include <haproxy/tools.h>
84#include <haproxy/uri_auth-t.h>
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010085#include <haproxy/xprt_quic.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
87
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +010088/* Used to chain configuration sections definitions. This list
89 * stores struct cfg_section
90 */
91struct list sections = LIST_HEAD_INIT(sections);
92
William Lallemand48b4bb42017-10-23 14:36:34 +020093struct list postparsers = LIST_HEAD_INIT(postparsers);
94
Eric Salama1b8dacc2021-03-16 15:12:17 +010095extern struct proxy *mworker_proxy;
96
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010097char *cursection = NULL;
Willy Tarreauc8d5b952019-02-27 17:25:52 +010098int cfg_maxpconn = 0; /* # of simultaneous connections per proxy (-N) */
Willy Tarreau5af24ef2009-03-15 15:23:16 +010099int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */
Christopher Faulet79bdef32016-11-04 22:36:15 +0100100char *cfg_scope = NULL; /* the current scope during the configuration parsing */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101
Willy Tarreau5b2c3362008-07-09 19:39:06 +0200102/* List head of all known configuration keywords */
Willy Tarreau36b9e222018-11-11 15:19:52 +0100103struct cfg_kw_list cfg_keywords = {
Willy Tarreau5b2c3362008-07-09 19:39:06 +0200104 .list = LIST_HEAD_INIT(cfg_keywords.list)
105};
106
Willy Tarreau4b103022021-02-12 17:59:10 +0100107/* nested if/elif/else/endif block states */
108enum nested_cond_state {
109 NESTED_COND_IF_TAKE, // "if" with a true condition
110 NESTED_COND_IF_DROP, // "if" with a false condition
111 NESTED_COND_IF_SKIP, // "if" masked by an outer false condition
112
113 NESTED_COND_ELIF_TAKE, // "elif" with a true condition from a false one
114 NESTED_COND_ELIF_DROP, // "elif" with a false condition from a false one
115 NESTED_COND_ELIF_SKIP, // "elif" masked by an outer false condition or a previously taken if
116
117 NESTED_COND_ELSE_TAKE, // taken "else" after an if false condition
118 NESTED_COND_ELSE_DROP, // "else" masked by outer false condition or an if true condition
119};
120
121/* 100 levels of nested conditions should already be sufficient */
122#define MAXNESTEDCONDS 100
123
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124/*
125 * converts <str> to a list of listeners which are dynamically allocated.
126 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
127 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
128 * - <port> is a numerical port from 1 to 65535 ;
129 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
130 * This can be repeated as many times as necessary, separated by a coma.
Willy Tarreau4fbb2282012-09-20 20:01:39 +0200131 * Function returns 1 for success or 0 if error. In case of errors, if <err> is
132 * not NULL, it must be a valid pointer to either NULL or a freeable area that
133 * will be replaced with an error message.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134 */
Willy Tarreau4fbb2282012-09-20 20:01:39 +0200135int str2listener(char *str, struct proxy *curproxy, struct bind_conf *bind_conf, const char *file, int line, char **err)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136{
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200137 struct protocol *proto;
Willy Tarreau2dff0c22011-03-04 15:43:13 +0100138 char *next, *dupstr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139 int port, end;
140
141 next = dupstr = strdup(str);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200142
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143 while (next && *next) {
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200144 struct sockaddr_storage *ss2;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100145 int fd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200146
147 str = next;
148 /* 1) look for the end of the first address */
Krzysztof Piotr Oledzki52d522b2009-01-27 16:57:08 +0100149 if ((next = strchr(str, ',')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200150 *next++ = 0;
151 }
152
Willy Tarreau5fc93282020-09-16 18:25:03 +0200153 ss2 = str2sa_range(str, NULL, &port, &end, &fd, &proto, err,
Eric Salama1b8dacc2021-03-16 15:12:17 +0100154 (curproxy == global.cli_fe || curproxy == mworker_proxy) ? NULL : global.unix_bind.prefix,
Willy Tarreau32819932020-09-04 15:53:16 +0200155 NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
Willy Tarreau5e1779a2020-09-16 16:28:08 +0200156 PA_O_SOCKET_FD | PA_O_STREAM | PA_O_XPRT);
Willy Tarreau12eb2a62013-03-06 15:45:03 +0100157 if (!ss2)
158 goto fail;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159
Willy Tarreau12eb2a62013-03-06 15:45:03 +0100160 /* OK the address looks correct */
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100161
162#ifdef USE_QUIC
163 /* The transport layer automatically switches to QUIC when QUIC
164 * is selected, regardless of bind_conf settings. We then need
165 * to initialize QUIC params.
166 */
167 if (proto->sock_type == SOCK_DGRAM && proto->ctrl_type == SOCK_STREAM) {
168 bind_conf->xprt = xprt_get(XPRT_QUIC);
169 quic_transport_params_init(&bind_conf->quic_params, 1);
170 }
171#endif
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200172 if (!create_listeners(bind_conf, ss2, port, end, fd, proto, err)) {
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200173 memprintf(err, "%s for address '%s'.\n", *err, str);
174 goto fail;
175 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200176 } /* end while(next) */
177 free(dupstr);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200178 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200179 fail:
180 free(dupstr);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200181 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182}
183
William Lallemand6e62fb62015-04-28 16:55:23 +0200184/*
Willy Tarreauaa333122020-09-16 15:13:04 +0200185 * converts <str> to a list of datagram-oriented listeners which are dynamically
186 * allocated.
187 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
188 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
189 * - <port> is a numerical port from 1 to 65535 ;
190 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
191 * This can be repeated as many times as necessary, separated by a coma.
192 * Function returns 1 for success or 0 if error. In case of errors, if <err> is
193 * not NULL, it must be a valid pointer to either NULL or a freeable area that
194 * will be replaced with an error message.
195 */
196int str2receiver(char *str, struct proxy *curproxy, struct bind_conf *bind_conf, const char *file, int line, char **err)
197{
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200198 struct protocol *proto;
Willy Tarreauaa333122020-09-16 15:13:04 +0200199 char *next, *dupstr;
200 int port, end;
201
202 next = dupstr = strdup(str);
203
204 while (next && *next) {
205 struct sockaddr_storage *ss2;
206 int fd = -1;
207
208 str = next;
209 /* 1) look for the end of the first address */
210 if ((next = strchr(str, ',')) != NULL) {
211 *next++ = 0;
212 }
213
Willy Tarreau5fc93282020-09-16 18:25:03 +0200214 ss2 = str2sa_range(str, NULL, &port, &end, &fd, &proto, err,
Willy Tarreau4975d142021-03-13 11:00:33 +0100215 curproxy == global.cli_fe ? NULL : global.unix_bind.prefix,
Willy Tarreauaa333122020-09-16 15:13:04 +0200216 NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
217 PA_O_SOCKET_FD | PA_O_DGRAM | PA_O_XPRT);
218 if (!ss2)
219 goto fail;
220
221 /* OK the address looks correct */
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200222 if (!create_listeners(bind_conf, ss2, port, end, fd, proto, err)) {
Willy Tarreauaa333122020-09-16 15:13:04 +0200223 memprintf(err, "%s for address '%s'.\n", *err, str);
224 goto fail;
225 }
226 } /* end while(next) */
227 free(dupstr);
228 return 1;
229 fail:
230 free(dupstr);
231 return 0;
232}
233
234/*
Willy Tarreauece9b072016-12-21 22:41:44 +0100235 * Report an error in <msg> when there are too many arguments. This version is
236 * intended to be used by keyword parsers so that the message will be included
237 * into the general error message. The index is the current keyword in args.
238 * Return 0 if the number of argument is correct, otherwise build a message and
239 * return 1. Fill err_code with an ERR_ALERT and an ERR_FATAL if not null. The
240 * message may also be null, it will simply not be produced (useful to check only).
241 * <msg> and <err_code> are only affected on error.
242 */
243int too_many_args_idx(int maxarg, int index, char **args, char **msg, int *err_code)
244{
245 int i;
246
247 if (!*args[index + maxarg + 1])
248 return 0;
249
250 if (msg) {
251 *msg = NULL;
252 memprintf(msg, "%s", args[0]);
253 for (i = 1; i <= index; i++)
254 memprintf(msg, "%s %s", *msg, args[i]);
255
256 memprintf(msg, "'%s' cannot handle unexpected argument '%s'.", *msg, args[index + maxarg + 1]);
257 }
258 if (err_code)
259 *err_code |= ERR_ALERT | ERR_FATAL;
260
261 return 1;
262}
263
264/*
265 * same as too_many_args_idx with a 0 index
266 */
267int too_many_args(int maxarg, char **args, char **msg, int *err_code)
268{
269 return too_many_args_idx(maxarg, 0, args, msg, err_code);
270}
271
272/*
William Lallemand6e62fb62015-04-28 16:55:23 +0200273 * Report a fatal Alert when there is too much arguments
274 * The index is the current keyword in args
275 * Return 0 if the number of argument is correct, otherwise emit an alert and return 1
276 * Fill err_code with an ERR_ALERT and an ERR_FATAL
277 */
278int alertif_too_many_args_idx(int maxarg, int index, const char *file, int linenum, char **args, int *err_code)
279{
280 char *kw = NULL;
281 int i;
282
283 if (!*args[index + maxarg + 1])
284 return 0;
285
286 memprintf(&kw, "%s", args[0]);
287 for (i = 1; i <= index; i++) {
288 memprintf(&kw, "%s %s", kw, args[i]);
289 }
290
Christopher Faulet767a84b2017-11-24 16:50:31 +0100291 ha_alert("parsing [%s:%d] : '%s' cannot handle unexpected argument '%s'.\n", file, linenum, kw, args[index + maxarg + 1]);
William Lallemand6e62fb62015-04-28 16:55:23 +0200292 free(kw);
293 *err_code |= ERR_ALERT | ERR_FATAL;
294 return 1;
295}
296
297/*
298 * same as alertif_too_many_args_idx with a 0 index
299 */
300int alertif_too_many_args(int maxarg, const char *file, int linenum, char **args, int *err_code)
301{
302 return alertif_too_many_args_idx(maxarg, 0, file, linenum, args, err_code);
303}
304
Willy Tarreau61d18892009-03-31 10:49:21 +0200305
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100306/* Report it if a request ACL condition uses some keywords that are incompatible
307 * with the place where the ACL is used. It returns either 0 or ERR_WARN so that
308 * its result can be or'ed with err_code. Note that <cond> may be NULL and then
309 * will be ignored.
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100310 */
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100311int warnif_cond_conflicts(const struct acl_cond *cond, unsigned int where, const char *file, int line)
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100312{
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100313 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +0200314 const char *kw;
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100315
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100316 if (!cond)
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100317 return 0;
318
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100319 acl = acl_cond_conflicts(cond, where);
320 if (acl) {
321 if (acl->name && *acl->name)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100322 ha_warning("parsing [%s:%d] : acl '%s' will never match because it only involves keywords that are incompatible with '%s'\n",
323 file, line, acl->name, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100324 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100325 ha_warning("parsing [%s:%d] : anonymous acl will never match because it uses keyword '%s' which is incompatible with '%s'\n",
326 file, line, LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100327 return ERR_WARN;
328 }
329 if (!acl_cond_kw_conflicts(cond, where, &acl, &kw))
Willy Tarreaufdb563c2010-01-31 15:43:27 +0100330 return 0;
331
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100332 if (acl->name && *acl->name)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100333 ha_warning("parsing [%s:%d] : acl '%s' involves keywords '%s' which is incompatible with '%s'\n",
334 file, line, acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100335 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100336 ha_warning("parsing [%s:%d] : anonymous acl involves keyword '%s' which is incompatible with '%s'\n",
337 file, line, kw, sample_ckp_names(where));
Willy Tarreaufdb563c2010-01-31 15:43:27 +0100338 return ERR_WARN;
339}
340
Christopher Faulet581db2b2021-03-26 10:02:46 +0100341/* Report it if an ACL uses a L6 sample fetch from an HTTP proxy. It returns
342 * either 0 or ERR_WARN so that its result can be or'ed with err_code. Note that
343 * <cond> may be NULL and then will be ignored.
344*/
345int warnif_tcp_http_cond(const struct proxy *px, const struct acl_cond *cond)
346{
347 if (!cond || px->mode != PR_MODE_HTTP)
348 return 0;
349
350 if (cond->use & (SMP_USE_L6REQ|SMP_USE_L6RES)) {
351 ha_warning("Proxy '%s': L6 sample fetches ignored on HTTP proxies (declared at %s:%d).\n",
352 px->id, cond->file, cond->line);
353 return ERR_WARN;
354 }
355 return 0;
356}
357
Willy Tarreaue2afcc42021-03-12 09:08:04 +0100358/* try to find in <list> the word that looks closest to <word> by counting
359 * transitions between letters, digits and other characters. Will return the
360 * best matching word if found, otherwise NULL. An optional array of extra
361 * words to compare may be passed in <extra>, but it must then be terminated
362 * by a NULL entry. If unused it may be NULL.
363 */
364const char *cfg_find_best_match(const char *word, const struct list *list, int section, const char **extra)
365{
366 uint8_t word_sig[1024]; // 0..25=letter, 26=digit, 27=other, 28=begin, 29=end
367 uint8_t list_sig[1024];
368 const struct cfg_kw_list *kwl;
369 int index;
370 const char *best_ptr = NULL;
371 int dist, best_dist = INT_MAX;
372
373 make_word_fingerprint(word_sig, word);
374 list_for_each_entry(kwl, list, list) {
375 for (index = 0; kwl->kw[index].kw != NULL; index++) {
376 if (kwl->kw[index].section != section)
377 continue;
378
379 make_word_fingerprint(list_sig, kwl->kw[index].kw);
380 dist = word_fingerprint_distance(word_sig, list_sig);
381 if (dist < best_dist) {
382 best_dist = dist;
383 best_ptr = kwl->kw[index].kw;
384 }
385 }
386 }
387
388 while (extra && *extra) {
389 make_word_fingerprint(list_sig, *extra);
390 dist = word_fingerprint_distance(word_sig, list_sig);
391 if (dist < best_dist) {
392 best_dist = dist;
393 best_ptr = *extra;
394 }
395 extra++;
396 }
397
398 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
399 best_ptr = NULL;
400 return best_ptr;
401}
402
Christopher Faulet62519022017-10-16 15:49:32 +0200403/* Parse a string representing a process number or a set of processes. It must
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100404 * be "all", "odd", "even", a number between 1 and <max> or a range with
Christopher Faulet5ab51772017-11-22 11:21:58 +0100405 * two such numbers delimited by a dash ('-'). On success, it returns
406 * 0. otherwise it returns 1 with an error message in <err>.
Christopher Faulet62519022017-10-16 15:49:32 +0200407 *
408 * Note: this function can also be used to parse a thread number or a set of
409 * threads.
410 */
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100411int parse_process_number(const char *arg, unsigned long *proc, int max, int *autoinc, char **err)
Christopher Faulet62519022017-10-16 15:49:32 +0200412{
Christopher Faulet26028f62017-11-22 15:01:51 +0100413 if (autoinc) {
414 *autoinc = 0;
415 if (strncmp(arg, "auto:", 5) == 0) {
416 arg += 5;
417 *autoinc = 1;
418 }
419 }
420
Christopher Faulet62519022017-10-16 15:49:32 +0200421 if (strcmp(arg, "all") == 0)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100422 *proc |= ~0UL;
Christopher Faulet62519022017-10-16 15:49:32 +0200423 else if (strcmp(arg, "odd") == 0)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100424 *proc |= ~0UL/3UL; /* 0x555....555 */
Christopher Faulet62519022017-10-16 15:49:32 +0200425 else if (strcmp(arg, "even") == 0)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100426 *proc |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Christopher Faulet62519022017-10-16 15:49:32 +0200427 else {
Christopher Faulet18cca782019-02-07 16:29:41 +0100428 const char *p, *dash = NULL;
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100429 unsigned int low, high;
430
Christopher Faulet18cca782019-02-07 16:29:41 +0100431 for (p = arg; *p; p++) {
432 if (*p == '-' && !dash)
433 dash = p;
Willy Tarreau90807112020-02-25 08:16:33 +0100434 else if (!isdigit((unsigned char)*p)) {
Christopher Faulet18cca782019-02-07 16:29:41 +0100435 memprintf(err, "'%s' is not a valid number/range.", arg);
436 return -1;
437 }
Christopher Faulet5ab51772017-11-22 11:21:58 +0100438 }
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100439
440 low = high = str2uic(arg);
Christopher Faulet18cca782019-02-07 16:29:41 +0100441 if (dash)
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100442 high = ((!*(dash+1)) ? max : str2uic(dash + 1));
Christopher Fauletff4121f2017-11-22 16:38:49 +0100443
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100444 if (high < low) {
445 unsigned int swap = low;
446 low = high;
447 high = swap;
448 }
449
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100450 if (low < 1 || low > max || high > max) {
Christopher Fauletcb6a9452017-11-22 16:50:41 +0100451 memprintf(err, "'%s' is not a valid number/range."
452 " It supports numbers from 1 to %d.\n",
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100453 arg, max);
Christopher Faulet5ab51772017-11-22 11:21:58 +0100454 return 1;
455 }
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100456
457 for (;low <= high; low++)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100458 *proc |= 1UL << (low-1);
Christopher Faulet62519022017-10-16 15:49:32 +0200459 }
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100460 *proc &= ~0UL >> (LONGBITS - max);
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100461
Christopher Faulet5ab51772017-11-22 11:21:58 +0100462 return 0;
Christopher Faulet62519022017-10-16 15:49:32 +0200463}
464
David Carlier7e351ee2017-12-01 09:14:02 +0000465#ifdef USE_CPU_AFFINITY
Christopher Faulet62519022017-10-16 15:49:32 +0200466/* Parse cpu sets. Each CPU set is either a unique number between 0 and
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200467 * ha_cpuset_size() - 1 or a range with two such numbers delimited by a dash
Amaury Denoyellea8082352021-04-06 16:46:15 +0200468 * ('-'). If <comma_allowed> is set, each CPU set can be a list of unique
469 * numbers or ranges separated by a comma. It is also possible to specify
470 * multiple cpu numbers or ranges in distinct argument in <args>. On success,
471 * it returns 0, otherwise it returns 1 with an error message in <err>.
Christopher Faulet62519022017-10-16 15:49:32 +0200472 */
Amaury Denoyellea8082352021-04-06 16:46:15 +0200473unsigned long parse_cpu_set(const char **args, struct hap_cpuset *cpu_set,
474 int comma_allowed, char **err)
Christopher Faulet62519022017-10-16 15:49:32 +0200475{
476 int cur_arg = 0;
Amaury Denoyellea8082352021-04-06 16:46:15 +0200477 const char *arg;
Christopher Faulet62519022017-10-16 15:49:32 +0200478
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200479 ha_cpuset_zero(cpu_set);
480
Amaury Denoyellea8082352021-04-06 16:46:15 +0200481 arg = args[cur_arg];
482 while (*arg) {
483 const char *dash, *comma;
Christopher Faulet62519022017-10-16 15:49:32 +0200484 unsigned int low, high;
485
Willy Tarreau90807112020-02-25 08:16:33 +0100486 if (!isdigit((unsigned char)*args[cur_arg])) {
Amaury Denoyellea8082352021-04-06 16:46:15 +0200487 memprintf(err, "'%s' is not a CPU range.", arg);
Christopher Faulet62519022017-10-16 15:49:32 +0200488 return -1;
489 }
490
Amaury Denoyellea8082352021-04-06 16:46:15 +0200491 low = high = str2uic(arg);
492
493 comma = comma_allowed ? strchr(arg, ',') : NULL;
494 dash = strchr(arg, '-');
495
496 if (dash && (!comma || dash < comma))
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200497 high = *(dash+1) ? str2uic(dash + 1) : ha_cpuset_size() - 1;
Christopher Faulet62519022017-10-16 15:49:32 +0200498
499 if (high < low) {
500 unsigned int swap = low;
501 low = high;
502 high = swap;
503 }
504
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200505 if (high >= ha_cpuset_size()) {
506 memprintf(err, "supports CPU numbers from 0 to %d.",
507 ha_cpuset_size() - 1);
Christopher Faulet62519022017-10-16 15:49:32 +0200508 return 1;
509 }
510
511 while (low <= high)
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200512 ha_cpuset_set(cpu_set, low++);
Christopher Faulet62519022017-10-16 15:49:32 +0200513
Amaury Denoyellea8082352021-04-06 16:46:15 +0200514 /* if a comma is present, parse the rest of the arg, else
515 * skip to the next arg */
516 arg = comma ? comma + 1 : args[++cur_arg];
Christopher Faulet62519022017-10-16 15:49:32 +0200517 }
518 return 0;
519}
David Carlier7e351ee2017-12-01 09:14:02 +0000520#endif
521
Frédéric Lécaille18251032019-01-11 11:07:15 +0100522/* Allocate and initialize the frontend of a "peers" section found in
523 * file <file> at line <linenum> with <id> as ID.
524 * Return 0 if succeeded, -1 if not.
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200525 * Note that this function may be called from "default-server"
526 * or "peer" lines.
Frédéric Lécaille18251032019-01-11 11:07:15 +0100527 */
528static int init_peers_frontend(const char *file, int linenum,
529 const char *id, struct peers *peers)
530{
531 struct proxy *p;
532
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200533 if (peers->peers_fe) {
534 p = peers->peers_fe;
535 goto out;
536 }
Frédéric Lécaille9492c4e2019-01-11 11:47:12 +0100537
Frédéric Lécaille18251032019-01-11 11:07:15 +0100538 p = calloc(1, sizeof *p);
539 if (!p) {
540 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
541 return -1;
542 }
543
544 init_new_proxy(p);
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200545 peers_setup_frontend(p);
Frédéric Lécaille18251032019-01-11 11:07:15 +0100546 p->parent = peers;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200547 /* Finally store this frontend. */
548 peers->peers_fe = p;
549
550 out:
551 if (id && !p->id)
552 p->id = strdup(id);
Frédéric Lécaille1055e682018-04-26 14:35:21 +0200553 free(p->conf.file);
Frédéric Lécaille18251032019-01-11 11:07:15 +0100554 p->conf.args.file = p->conf.file = strdup(file);
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100555 if (linenum != -1)
556 p->conf.args.line = p->conf.line = linenum;
Frédéric Lécaille18251032019-01-11 11:07:15 +0100557
558 return 0;
559}
Willy Tarreauade5ec42010-01-28 19:33:49 +0100560
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100561/* Only change ->file, ->line and ->arg struct bind_conf member values
562 * if already present.
563 */
564static struct bind_conf *bind_conf_uniq_alloc(struct proxy *p,
565 const char *file, int line,
566 const char *arg, struct xprt_ops *xprt)
567{
568 struct bind_conf *bind_conf;
569
570 if (!LIST_ISEMPTY(&p->conf.bind)) {
571 bind_conf = LIST_ELEM((&p->conf.bind)->n, typeof(bind_conf), by_fe);
572 free(bind_conf->file);
573 bind_conf->file = strdup(file);
574 bind_conf->line = line;
575 if (arg) {
576 free(bind_conf->arg);
577 bind_conf->arg = strdup(arg);
578 }
579 }
580 else {
581 bind_conf = bind_conf_alloc(p, file, line, arg, xprt);
582 }
583
584 return bind_conf;
585}
586
587/*
588 * Allocate a new struct peer parsed at line <linenum> in file <file>
589 * to be added to <peers>.
590 * Returns the new allocated structure if succeeded, NULL if not.
591 */
592static struct peer *cfg_peers_add_peer(struct peers *peers,
593 const char *file, int linenum,
594 const char *id, int local)
595{
596 struct peer *p;
597
598 p = calloc(1, sizeof *p);
599 if (!p) {
600 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
601 return NULL;
602 }
603
604 /* the peers are linked backwards first */
605 peers->count++;
606 p->next = peers->remote;
607 peers->remote = p;
608 p->conf.file = strdup(file);
609 p->conf.line = linenum;
610 p->last_change = now.tv_sec;
611 p->xprt = xprt_get(XPRT_RAW);
612 p->sock_init_arg = NULL;
613 HA_SPIN_INIT(&p->lock);
614 if (id)
615 p->id = strdup(id);
616 if (local) {
617 p->local = 1;
618 peers->local = p;
619 }
620
621 return p;
622}
623
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624/*
William Lallemand51097192015-04-14 16:35:22 +0200625 * Parse a line in a <listen>, <frontend> or <backend> section.
Willy Tarreau93893792009-07-23 13:19:11 +0200626 * Returns the error code, 0 if OK, or any combination of :
627 * - ERR_ABORT: must abort ASAP
628 * - ERR_FATAL: we can continue parsing but not start the service
629 * - ERR_WARN: a warning has been emitted
630 * - ERR_ALERT: an alert has been emitted
631 * Only the two first ones can stop processing, the two others are just
632 * indicators.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200633 */
Emeric Brun32da3c42010-09-23 18:39:19 +0200634int cfg_parse_peers(const char *file, int linenum, char **args, int kwm)
635{
636 static struct peers *curpeers = NULL;
637 struct peer *newpeer = NULL;
638 const char *err;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200639 struct bind_conf *bind_conf;
640 struct listener *l;
Emeric Brun32da3c42010-09-23 18:39:19 +0200641 int err_code = 0;
Willy Tarreau902636f2013-03-10 19:44:48 +0100642 char *errmsg = NULL;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100643 static int bind_line, peer_line;
644
645 if (strcmp(args[0], "bind") == 0 || strcmp(args[0], "default-bind") == 0) {
646 int cur_arg;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100647 struct bind_conf *bind_conf;
648 struct bind_kw *kw;
Emeric Brun32da3c42010-09-23 18:39:19 +0200649
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100650 cur_arg = 1;
651
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200652 if (init_peers_frontend(file, linenum, NULL, curpeers) != 0) {
653 err_code |= ERR_ALERT | ERR_ABORT;
654 goto out;
655 }
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100656
657 bind_conf = bind_conf_uniq_alloc(curpeers->peers_fe, file, linenum,
658 NULL, xprt_get(XPRT_RAW));
659 if (*args[0] == 'b') {
660 struct listener *l;
661
662 if (peer_line) {
663 ha_alert("parsing [%s:%d] : mixing \"peer\" and \"bind\" line is forbidden\n", file, linenum);
664 err_code |= ERR_ALERT | ERR_FATAL;
665 goto out;
666 }
667
668 if (!str2listener(args[1], curpeers->peers_fe, bind_conf, file, linenum, &errmsg)) {
669 if (errmsg && *errmsg) {
670 indent_msg(&errmsg, 2);
671 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
672 }
673 else
674 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
675 file, linenum, args[0], args[1], args[2]);
676 err_code |= ERR_FATAL;
677 goto out;
678 }
679 l = LIST_ELEM(bind_conf->listeners.n, typeof(l), by_bind);
680 l->maxaccept = 1;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100681 l->accept = session_accept_fd;
682 l->analysers |= curpeers->peers_fe->fe_req_ana;
683 l->default_target = curpeers->peers_fe->default_target;
684 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
Willy Tarreau18215cb2019-02-27 16:25:28 +0100685 global.maxsock++; /* for the listening socket */
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100686
687 bind_line = 1;
688 if (cfg_peers->local) {
689 newpeer = cfg_peers->local;
690 }
691 else {
692 /* This peer is local.
693 * Note that we do not set the peer ID. This latter is initialized
694 * when parsing "peer" or "server" line.
695 */
696 newpeer = cfg_peers_add_peer(curpeers, file, linenum, NULL, 1);
697 if (!newpeer) {
698 err_code |= ERR_ALERT | ERR_ABORT;
699 goto out;
700 }
701 }
Willy Tarreau37159062020-08-27 07:48:42 +0200702 newpeer->addr = l->rx.addr;
Willy Tarreau5fc93282020-09-16 18:25:03 +0200703 newpeer->proto = l->rx.proto;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100704 cur_arg++;
705 }
706
707 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
708 int ret;
709
710 ret = kw->parse(args, cur_arg, curpeers->peers_fe, bind_conf, &errmsg);
711 err_code |= ret;
712 if (ret) {
713 if (errmsg && *errmsg) {
714 indent_msg(&errmsg, 2);
715 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
716 }
717 else
718 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
719 file, linenum, args[cur_arg]);
720 if (ret & ERR_FATAL)
721 goto out;
722 }
723 cur_arg += 1 + kw->skip;
724 }
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100725 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +0100726 const char *best = bind_find_best_kw(args[cur_arg]);
727 if (best)
728 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
729 file, linenum, args[cur_arg], cursection, best);
730 else
731 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
732 file, linenum, args[cur_arg], cursection);
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100733 err_code |= ERR_ALERT | ERR_FATAL;
734 goto out;
735 }
736 }
737 else if (strcmp(args[0], "default-server") == 0) {
738 if (init_peers_frontend(file, -1, NULL, curpeers) != 0) {
739 err_code |= ERR_ALERT | ERR_ABORT;
740 goto out;
741 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100742 err_code |= parse_server(file, linenum, args, curpeers->peers_fe, NULL,
743 SRV_PARSE_DEFAULT_SERVER|SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_INITIAL_RESOLVE);
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200744 }
Frédéric Lécailleb6f759b2019-11-05 09:57:45 +0100745 else if (strcmp(args[0], "log") == 0) {
746 if (init_peers_frontend(file, linenum, NULL, curpeers) != 0) {
747 err_code |= ERR_ALERT | ERR_ABORT;
748 goto out;
749 }
Emeric Brun9533a702021-04-02 10:13:43 +0200750 if (!parse_logsrv(args, &curpeers->peers_fe->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Frédéric Lécailleb6f759b2019-11-05 09:57:45 +0100751 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
752 err_code |= ERR_ALERT | ERR_FATAL;
753 goto out;
754 }
755 }
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200756 else if (strcmp(args[0], "peers") == 0) { /* new peers section */
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100757 /* Initialize these static variables when entering a new "peers" section*/
758 bind_line = peer_line = 0;
Willy Tarreau0dbbf312013-03-05 11:31:55 +0100759 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100760 ha_alert("parsing [%s:%d] : missing name for peers section.\n", file, linenum);
Willy Tarreau54984722014-02-16 08:20:13 +0100761 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau0dbbf312013-03-05 11:31:55 +0100762 goto out;
763 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200764
William Lallemand6e62fb62015-04-28 16:55:23 +0200765 if (alertif_too_many_args(1, file, linenum, args, &err_code))
766 goto out;
767
Emeric Brun32da3c42010-09-23 18:39:19 +0200768 err = invalid_char(args[1]);
769 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100770 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
771 file, linenum, *err, args[0], args[1]);
Willy Tarreau54984722014-02-16 08:20:13 +0100772 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau0dbbf312013-03-05 11:31:55 +0100773 goto out;
Emeric Brun32da3c42010-09-23 18:39:19 +0200774 }
775
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200776 for (curpeers = cfg_peers; curpeers != NULL; curpeers = curpeers->next) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200777 /*
778 * If there are two proxies with the same name only following
779 * combinations are allowed:
780 */
781 if (strcmp(curpeers->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100782 ha_alert("Parsing [%s:%d]: peers section '%s' has the same name as another peers section declared at %s:%d.\n",
783 file, linenum, args[1], curpeers->conf.file, curpeers->conf.line);
Willy Tarreau911fa2e2015-05-26 10:35:50 +0200784 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun32da3c42010-09-23 18:39:19 +0200785 }
786 }
787
Vincent Bernat02779b62016-04-03 13:48:43 +0200788 if ((curpeers = calloc(1, sizeof(*curpeers))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100789 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Emeric Brun32da3c42010-09-23 18:39:19 +0200790 err_code |= ERR_ALERT | ERR_ABORT;
791 goto out;
792 }
793
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200794 curpeers->next = cfg_peers;
795 cfg_peers = curpeers;
Willy Tarreau8113a5d2012-10-04 08:01:43 +0200796 curpeers->conf.file = strdup(file);
Emeric Brun32da3c42010-09-23 18:39:19 +0200797 curpeers->conf.line = linenum;
798 curpeers->last_change = now.tv_sec;
799 curpeers->id = strdup(args[1]);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +0200800 curpeers->disabled = 0;
Emeric Brun32da3c42010-09-23 18:39:19 +0200801 }
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200802 else if (strcmp(args[0], "peer") == 0 ||
803 strcmp(args[0], "server") == 0) { /* peer or server definition */
Frédéric Lécaille04636b72019-01-31 06:48:16 +0100804 int local_peer, peer;
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100805 int parse_addr = 0;
Emeric Brun32da3c42010-09-23 18:39:19 +0200806
Frédéric Lécaille04636b72019-01-31 06:48:16 +0100807 peer = *args[0] == 'p';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100808 local_peer = strcmp(args[1], localpeer) == 0;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100809 /* The local peer may have already partially been parsed on a "bind" line. */
810 if (*args[0] == 'p') {
811 if (bind_line) {
812 ha_alert("parsing [%s:%d] : mixing \"peer\" and \"bind\" line is forbidden\n", file, linenum);
813 err_code |= ERR_ALERT | ERR_FATAL;
814 goto out;
815 }
816 peer_line = 1;
817 }
818 if (cfg_peers->local && !cfg_peers->local->id && local_peer) {
819 /* The local peer has already been initialized on a "bind" line.
820 * Let's use it and store its ID.
821 */
822 newpeer = cfg_peers->local;
823 newpeer->id = strdup(localpeer);
824 }
825 else {
826 if (local_peer && cfg_peers->local) {
827 ha_alert("parsing [%s:%d] : '%s %s' : local peer name already referenced at %s:%d. %s\n",
828 file, linenum, args[0], args[1],
829 curpeers->peers_fe->conf.file, curpeers->peers_fe->conf.line, cfg_peers->local->id);
830 err_code |= ERR_FATAL;
831 goto out;
832 }
833 newpeer = cfg_peers_add_peer(curpeers, file, linenum, args[1], local_peer);
834 if (!newpeer) {
835 err_code |= ERR_ALERT | ERR_ABORT;
836 goto out;
837 }
838 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200839
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100840 /* Line number and peer ID are updated only if this peer is the local one. */
841 if (init_peers_frontend(file,
842 newpeer->local ? linenum: -1,
843 newpeer->local ? newpeer->id : NULL,
844 curpeers) != 0) {
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200845 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreaub36487e2013-03-10 18:37:42 +0100846 goto out;
847 }
Willy Tarreau2aa38802013-02-20 19:20:59 +0100848
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100849 /* This initializes curpeer->peers->peers_fe->srv.
850 * The server address is parsed only if we are parsing a "peer" line,
851 * or if we are parsing a "server" line and the current peer is not the local one.
852 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100853 parse_addr = (peer || !local_peer) ? SRV_PARSE_PARSE_ADDR : 0;
854 err_code |= parse_server(file, linenum, args, curpeers->peers_fe, NULL,
855 SRV_PARSE_IN_PEER_SECTION|parse_addr|SRV_PARSE_INITIAL_RESOLVE);
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +0200856 if (!curpeers->peers_fe->srv) {
857 /* Remove the newly allocated peer. */
858 if (newpeer != curpeers->local) {
859 struct peer *p;
860
861 p = curpeers->remote;
862 curpeers->remote = curpeers->remote->next;
863 free(p->id);
864 free(p);
865 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200866 goto out;
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +0200867 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200868
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100869 /* If the peer address has just been parsed, let's copy it to <newpeer>
870 * and initializes ->proto.
871 */
872 if (peer || !local_peer) {
873 newpeer->addr = curpeers->peers_fe->srv->addr;
874 newpeer->proto = protocol_by_family(newpeer->addr.ss_family);
875 }
876
Willy Tarreaua261e9b2016-12-22 20:44:00 +0100877 newpeer->xprt = xprt_get(XPRT_RAW);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200878 newpeer->sock_init_arg = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100879 HA_SPIN_INIT(&newpeer->lock);
Willy Tarreau26d8c592012-05-07 18:12:14 +0200880
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100881 newpeer->srv = curpeers->peers_fe->srv;
882 if (!newpeer->local)
Frédéric Lécaille6617e762018-04-25 15:13:38 +0200883 goto out;
884
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100885 /* The lines above are reserved to "peer" lines. */
886 if (*args[0] == 's')
Frédéric Lécaille4ba51982018-04-25 15:32:18 +0200887 goto out;
Emeric Brun32da3c42010-09-23 18:39:19 +0200888
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100889 bind_conf = bind_conf_uniq_alloc(curpeers->peers_fe, file, linenum, args[2], xprt_get(XPRT_RAW));
Frédéric Lécaille16e49102019-01-11 11:27:16 +0100890
Frédéric Lécaille91694d52019-01-11 11:43:53 +0100891 if (!str2listener(args[2], curpeers->peers_fe, bind_conf, file, linenum, &errmsg)) {
892 if (errmsg && *errmsg) {
893 indent_msg(&errmsg, 2);
894 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Frédéric Lécaille16e49102019-01-11 11:27:16 +0100895 }
Frédéric Lécaille91694d52019-01-11 11:43:53 +0100896 else
897 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
898 file, linenum, args[0], args[1], args[2]);
899 err_code |= ERR_FATAL;
900 goto out;
901 }
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100902
903 l = LIST_ELEM(bind_conf->listeners.n, typeof(l), by_bind);
904 l->maxaccept = 1;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100905 l->accept = session_accept_fd;
906 l->analysers |= curpeers->peers_fe->fe_req_ana;
907 l->default_target = curpeers->peers_fe->default_target;
908 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
Willy Tarreau18215cb2019-02-27 16:25:28 +0100909 global.maxsock++; /* for the listening socket */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100910 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100911 else if (strcmp(args[0], "table") == 0) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100912 struct stktable *t, *other;
913 char *id;
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100914 size_t prefix_len;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100915
916 /* Line number and peer ID are updated only if this peer is the local one. */
917 if (init_peers_frontend(file, -1, NULL, curpeers) != 0) {
918 err_code |= ERR_ALERT | ERR_ABORT;
919 goto out;
920 }
921
922 other = stktable_find_by_name(args[1]);
923 if (other) {
924 ha_alert("parsing [%s:%d] : stick-table name '%s' conflicts with table declared in %s '%s' at %s:%d.\n",
925 file, linenum, args[1],
926 other->proxy ? proxy_cap_str(other->proxy->cap) : "peers",
927 other->proxy ? other->id : other->peers.p->id,
928 other->conf.file, other->conf.line);
929 err_code |= ERR_ALERT | ERR_FATAL;
930 goto out;
931 }
932
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100933 /* Build the stick-table name, concatenating the "peers" section name
934 * followed by a '/' character and the table name argument.
935 */
936 chunk_reset(&trash);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100937 if (!chunk_strcpy(&trash, curpeers->id)) {
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100938 ha_alert("parsing [%s:%d]: '%s %s' : stick-table name too long.\n",
939 file, linenum, args[0], args[1]);
940 err_code |= ERR_ALERT | ERR_FATAL;
941 goto out;
942 }
943
944 prefix_len = trash.data;
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100945 if (!chunk_memcat(&trash, "/", 1) || !chunk_strcat(&trash, args[1])) {
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100946 ha_alert("parsing [%s:%d]: '%s %s' : stick-table name too long.\n",
947 file, linenum, args[0], args[1]);
948 err_code |= ERR_ALERT | ERR_FATAL;
949 goto out;
950 }
951
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100952 t = calloc(1, sizeof *t);
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100953 id = strdup(trash.area);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100954 if (!t || !id) {
955 ha_alert("parsing [%s:%d]: '%s %s' : memory allocation failed\n",
956 file, linenum, args[0], args[1]);
Eric Salama1aab9112020-09-18 11:55:17 +0200957 free(t);
958 free(id);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100959 err_code |= ERR_ALERT | ERR_FATAL;
960 goto out;
961 }
962
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100963 err_code |= parse_stick_table(file, linenum, args, t, id, id + prefix_len, curpeers);
Eric Salama1aab9112020-09-18 11:55:17 +0200964 if (err_code & ERR_FATAL) {
965 free(t);
966 free(id);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100967 goto out;
Eric Salama1aab9112020-09-18 11:55:17 +0200968 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100969
970 stktable_store_name(t);
971 t->next = stktables_list;
972 stktables_list = t;
973 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100974 else if (strcmp(args[0], "disabled") == 0) { /* disables this peers section */
Willy Tarreau1ad64ac2020-09-24 08:48:08 +0200975 curpeers->disabled = 1;
Willy Tarreau77e4bd12015-05-01 20:02:17 +0200976 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100977 else if (strcmp(args[0], "enabled") == 0) { /* enables this peers section (used to revert a disabled default) */
Willy Tarreau1ad64ac2020-09-24 08:48:08 +0200978 curpeers->disabled = 0;
Willy Tarreau77e4bd12015-05-01 20:02:17 +0200979 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200980 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100981 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
Emeric Brun32da3c42010-09-23 18:39:19 +0200982 err_code |= ERR_ALERT | ERR_FATAL;
983 goto out;
984 }
985
986out:
Willy Tarreau902636f2013-03-10 19:44:48 +0100987 free(errmsg);
Emeric Brun32da3c42010-09-23 18:39:19 +0200988 return err_code;
989}
990
Baptiste Assmann325137d2015-04-13 23:40:55 +0200991/*
William Lallemand51097192015-04-14 16:35:22 +0200992 * Parse a line in a <listen>, <frontend> or <backend> section.
Simon Horman0d16a402015-01-30 11:22:58 +0900993 * Returns the error code, 0 if OK, or any combination of :
994 * - ERR_ABORT: must abort ASAP
995 * - ERR_FATAL: we can continue parsing but not start the service
996 * - ERR_WARN: a warning has been emitted
997 * - ERR_ALERT: an alert has been emitted
998 * Only the two first ones can stop processing, the two others are just
999 * indicators.
1000 */
1001int cfg_parse_mailers(const char *file, int linenum, char **args, int kwm)
1002{
1003 static struct mailers *curmailers = NULL;
1004 struct mailer *newmailer = NULL;
1005 const char *err;
1006 int err_code = 0;
1007 char *errmsg = NULL;
1008
1009 if (strcmp(args[0], "mailers") == 0) { /* new mailers section */
1010 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001011 ha_alert("parsing [%s:%d] : missing name for mailers section.\n", file, linenum);
Simon Horman0d16a402015-01-30 11:22:58 +09001012 err_code |= ERR_ALERT | ERR_ABORT;
1013 goto out;
1014 }
1015
1016 err = invalid_char(args[1]);
1017 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001018 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1019 file, linenum, *err, args[0], args[1]);
Simon Horman0d16a402015-01-30 11:22:58 +09001020 err_code |= ERR_ALERT | ERR_ABORT;
1021 goto out;
1022 }
1023
1024 for (curmailers = mailers; curmailers != NULL; curmailers = curmailers->next) {
1025 /*
1026 * If there are two proxies with the same name only following
1027 * combinations are allowed:
1028 */
1029 if (strcmp(curmailers->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001030 ha_alert("Parsing [%s:%d]: mailers section '%s' has the same name as another mailers section declared at %s:%d.\n",
1031 file, linenum, args[1], curmailers->conf.file, curmailers->conf.line);
Willy Tarreau911fa2e2015-05-26 10:35:50 +02001032 err_code |= ERR_ALERT | ERR_FATAL;
Simon Horman0d16a402015-01-30 11:22:58 +09001033 }
1034 }
1035
Vincent Bernat02779b62016-04-03 13:48:43 +02001036 if ((curmailers = calloc(1, sizeof(*curmailers))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001037 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Simon Horman0d16a402015-01-30 11:22:58 +09001038 err_code |= ERR_ALERT | ERR_ABORT;
1039 goto out;
1040 }
1041
1042 curmailers->next = mailers;
1043 mailers = curmailers;
1044 curmailers->conf.file = strdup(file);
1045 curmailers->conf.line = linenum;
1046 curmailers->id = strdup(args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001047 curmailers->timeout.mail = DEF_MAILALERTTIME;/* XXX: Would like to Skip to the next alert, if any, ASAP.
1048 * But need enough time so that timeouts don't occur
1049 * during tcp procssing. For now just us an arbitrary default. */
Simon Horman0d16a402015-01-30 11:22:58 +09001050 }
1051 else if (strcmp(args[0], "mailer") == 0) { /* mailer definition */
1052 struct sockaddr_storage *sk;
1053 int port1, port2;
1054 struct protocol *proto;
1055
1056 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001057 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1058 file, linenum, args[0]);
Simon Horman0d16a402015-01-30 11:22:58 +09001059 err_code |= ERR_ALERT | ERR_FATAL;
1060 goto out;
1061 }
1062
1063 err = invalid_char(args[1]);
1064 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001065 ha_alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1066 file, linenum, *err, args[1]);
Simon Horman0d16a402015-01-30 11:22:58 +09001067 err_code |= ERR_ALERT | ERR_FATAL;
1068 goto out;
1069 }
1070
Vincent Bernat02779b62016-04-03 13:48:43 +02001071 if ((newmailer = calloc(1, sizeof(*newmailer))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001072 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Simon Horman0d16a402015-01-30 11:22:58 +09001073 err_code |= ERR_ALERT | ERR_ABORT;
1074 goto out;
1075 }
1076
1077 /* the mailers are linked backwards first */
1078 curmailers->count++;
1079 newmailer->next = curmailers->mailer_list;
1080 curmailers->mailer_list = newmailer;
1081 newmailer->mailers = curmailers;
1082 newmailer->conf.file = strdup(file);
1083 newmailer->conf.line = linenum;
1084
1085 newmailer->id = strdup(args[1]);
1086
Willy Tarreau5fc93282020-09-16 18:25:03 +02001087 sk = str2sa_range(args[2], NULL, &port1, &port2, NULL, &proto,
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001088 &errmsg, NULL, NULL,
1089 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
Simon Horman0d16a402015-01-30 11:22:58 +09001090 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001091 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Simon Horman0d16a402015-01-30 11:22:58 +09001092 err_code |= ERR_ALERT | ERR_FATAL;
1093 goto out;
1094 }
1095
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001096 if (proto->sock_prot != IPPROTO_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001097 ha_alert("parsing [%s:%d] : '%s %s' : TCP not supported for this address family.\n",
1098 file, linenum, args[0], args[1]);
Simon Horman0d16a402015-01-30 11:22:58 +09001099 err_code |= ERR_ALERT | ERR_FATAL;
1100 goto out;
1101 }
1102
Simon Horman0d16a402015-01-30 11:22:58 +09001103 newmailer->addr = *sk;
1104 newmailer->proto = proto;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001105 newmailer->xprt = xprt_get(XPRT_RAW);
Simon Horman0d16a402015-01-30 11:22:58 +09001106 newmailer->sock_init_arg = NULL;
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001107 }
1108 else if (strcmp(args[0], "timeout") == 0) {
1109 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001110 ha_alert("parsing [%s:%d] : '%s' expects 'mail' and <time> as arguments.\n",
1111 file, linenum, args[0]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001112 err_code |= ERR_ALERT | ERR_FATAL;
1113 goto out;
1114 }
1115 else if (strcmp(args[1], "mail") == 0) {
1116 const char *res;
1117 unsigned int timeout_mail;
1118 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001119 ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
1120 file, linenum, args[0], args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001121 err_code |= ERR_ALERT | ERR_FATAL;
1122 goto out;
1123 }
1124 res = parse_time_err(args[2], &timeout_mail, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001125 if (res == PARSE_TIME_OVER) {
1126 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
1127 file, linenum, args[2], args[0], args[1]);
1128 err_code |= ERR_ALERT | ERR_FATAL;
1129 goto out;
1130 }
1131 else if (res == PARSE_TIME_UNDER) {
1132 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
1133 file, linenum, args[2], args[0], args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001134 err_code |= ERR_ALERT | ERR_FATAL;
1135 goto out;
1136 }
Willy Tarreau9faebe32019-06-07 19:00:37 +02001137 else if (res) {
1138 ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
1139 file, linenum, *res, args[0], args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001140 err_code |= ERR_ALERT | ERR_FATAL;
1141 goto out;
1142 }
1143 curmailers->timeout.mail = timeout_mail;
1144 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001145 ha_alert("parsing [%s:%d] : '%s' expects 'mail' and <time> as arguments got '%s'.\n",
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001146 file, linenum, args[0], args[1]);
1147 err_code |= ERR_ALERT | ERR_FATAL;
1148 goto out;
1149 }
1150 }
Simon Horman0d16a402015-01-30 11:22:58 +09001151 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001152 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
Simon Horman0d16a402015-01-30 11:22:58 +09001153 err_code |= ERR_ALERT | ERR_FATAL;
1154 goto out;
1155 }
1156
1157out:
1158 free(errmsg);
1159 return err_code;
1160}
1161
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +01001162void free_email_alert(struct proxy *p)
Simon Horman9dc49962015-01-30 11:22:59 +09001163{
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001164 ha_free(&p->email_alert.mailers.name);
1165 ha_free(&p->email_alert.from);
1166 ha_free(&p->email_alert.to);
1167 ha_free(&p->email_alert.myhostname);
Simon Horman9dc49962015-01-30 11:22:59 +09001168}
1169
Willy Tarreaubaaee002006-06-26 02:48:02 +02001170
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001171int
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001172cfg_parse_netns(const char *file, int linenum, char **args, int kwm)
1173{
Willy Tarreaue5733232019-05-22 19:24:06 +02001174#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001175 const char *err;
1176 const char *item = args[0];
1177
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001178 if (strcmp(item, "namespace_list") == 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001179 return 0;
1180 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001181 else if (strcmp(item, "namespace") == 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001182 size_t idx = 1;
1183 const char *current;
1184 while (*(current = args[idx++])) {
1185 err = invalid_char(current);
1186 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001187 ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1188 file, linenum, *err, item, current);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001189 return ERR_ALERT | ERR_FATAL;
1190 }
1191
1192 if (netns_store_lookup(current, strlen(current))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001193 ha_alert("parsing [%s:%d]: Namespace '%s' is already added.\n",
1194 file, linenum, current);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001195 return ERR_ALERT | ERR_FATAL;
1196 }
1197 if (!netns_store_insert(current)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001198 ha_alert("parsing [%s:%d]: Cannot open namespace '%s'.\n",
1199 file, linenum, current);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001200 return ERR_ALERT | ERR_FATAL;
1201 }
1202 }
1203 }
1204
1205 return 0;
1206#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001207 ha_alert("parsing [%s:%d]: namespace support is not compiled in.",
1208 file, linenum);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001209 return ERR_ALERT | ERR_FATAL;
1210#endif
1211}
1212
1213int
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001214cfg_parse_users(const char *file, int linenum, char **args, int kwm)
1215{
1216
1217 int err_code = 0;
1218 const char *err;
1219
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001220 if (strcmp(args[0], "userlist") == 0) { /* new userlist */
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001221 struct userlist *newul;
1222
1223 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001224 ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1225 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001226 err_code |= ERR_ALERT | ERR_FATAL;
1227 goto out;
1228 }
William Lallemand6e62fb62015-04-28 16:55:23 +02001229 if (alertif_too_many_args(1, file, linenum, args, &err_code))
1230 goto out;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001231
1232 err = invalid_char(args[1]);
1233 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001234 ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1235 file, linenum, *err, args[0], args[1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001236 err_code |= ERR_ALERT | ERR_FATAL;
1237 goto out;
1238 }
1239
1240 for (newul = userlist; newul; newul = newul->next)
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001241 if (strcmp(newul->name, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001242 ha_warning("parsing [%s:%d]: ignoring duplicated userlist '%s'.\n",
1243 file, linenum, args[1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001244 err_code |= ERR_WARN;
1245 goto out;
1246 }
1247
Vincent Bernat02779b62016-04-03 13:48:43 +02001248 newul = calloc(1, sizeof(*newul));
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001249 if (!newul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001250 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001251 err_code |= ERR_ALERT | ERR_ABORT;
1252 goto out;
1253 }
1254
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001255 newul->name = strdup(args[1]);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001256 if (!newul->name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001257 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001258 err_code |= ERR_ALERT | ERR_ABORT;
David Carlier97880bb2016-04-08 10:35:26 +01001259 free(newul);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001260 goto out;
1261 }
1262
1263 newul->next = userlist;
1264 userlist = newul;
1265
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001266 } else if (strcmp(args[0], "group") == 0) { /* new group */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001267 int cur_arg;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001268 const char *err;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001269 struct auth_groups *ag;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001270
1271 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001272 ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1273 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001274 err_code |= ERR_ALERT | ERR_FATAL;
1275 goto out;
1276 }
1277
1278 err = invalid_char(args[1]);
1279 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001280 ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1281 file, linenum, *err, args[0], args[1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001282 err_code |= ERR_ALERT | ERR_FATAL;
1283 goto out;
1284 }
1285
William Lallemand4ac9f542015-05-28 18:03:51 +02001286 if (!userlist)
1287 goto out;
1288
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001289 for (ag = userlist->groups; ag; ag = ag->next)
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001290 if (strcmp(ag->name, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001291 ha_warning("parsing [%s:%d]: ignoring duplicated group '%s' in userlist '%s'.\n",
1292 file, linenum, args[1], userlist->name);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001293 err_code |= ERR_ALERT;
1294 goto out;
1295 }
1296
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001297 ag = calloc(1, sizeof(*ag));
1298 if (!ag) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001299 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001300 err_code |= ERR_ALERT | ERR_ABORT;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001301 goto out;
1302 }
1303
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001304 ag->name = strdup(args[1]);
David Carlier70d60452016-08-22 23:27:42 +01001305 if (!ag->name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001306 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001307 err_code |= ERR_ALERT | ERR_ABORT;
David Carlier70d60452016-08-22 23:27:42 +01001308 free(ag);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001309 goto out;
1310 }
1311
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001312 cur_arg = 2;
1313
1314 while (*args[cur_arg]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001315 if (strcmp(args[cur_arg], "users") == 0) {
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001316 ag->groupusers = strdup(args[cur_arg + 1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001317 cur_arg += 2;
1318 continue;
1319 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001320 ha_alert("parsing [%s:%d]: '%s' only supports 'users' option.\n",
1321 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001322 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier70d60452016-08-22 23:27:42 +01001323 free(ag->groupusers);
1324 free(ag->name);
1325 free(ag);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001326 goto out;
1327 }
1328 }
1329
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001330 ag->next = userlist->groups;
1331 userlist->groups = ag;
1332
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001333 } else if (strcmp(args[0], "user") == 0) { /* new user */
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001334 struct auth_users *newuser;
1335 int cur_arg;
1336
1337 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001338 ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1339 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001340 err_code |= ERR_ALERT | ERR_FATAL;
1341 goto out;
1342 }
William Lallemand4ac9f542015-05-28 18:03:51 +02001343 if (!userlist)
1344 goto out;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001345
1346 for (newuser = userlist->users; newuser; newuser = newuser->next)
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001347 if (strcmp(newuser->user, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001348 ha_warning("parsing [%s:%d]: ignoring duplicated user '%s' in userlist '%s'.\n",
1349 file, linenum, args[1], userlist->name);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001350 err_code |= ERR_ALERT;
1351 goto out;
1352 }
1353
Vincent Bernat02779b62016-04-03 13:48:43 +02001354 newuser = calloc(1, sizeof(*newuser));
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001355 if (!newuser) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001356 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001357 err_code |= ERR_ALERT | ERR_ABORT;
1358 goto out;
1359 }
1360
1361 newuser->user = strdup(args[1]);
1362
1363 newuser->next = userlist->users;
1364 userlist->users = newuser;
1365
1366 cur_arg = 2;
1367
1368 while (*args[cur_arg]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001369 if (strcmp(args[cur_arg], "password") == 0) {
Willy Tarreaue5733232019-05-22 19:24:06 +02001370#ifdef USE_LIBCRYPT
Cyril Bonté1a0191d2014-08-29 20:20:02 +02001371 if (!crypt("", args[cur_arg + 1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001372 ha_alert("parsing [%s:%d]: the encrypted password used for user '%s' is not supported by crypt(3).\n",
1373 file, linenum, newuser->user);
Cyril Bonté1a0191d2014-08-29 20:20:02 +02001374 err_code |= ERR_ALERT | ERR_FATAL;
1375 goto out;
1376 }
1377#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001378 ha_warning("parsing [%s:%d]: no crypt(3) support compiled, encrypted passwords will not work.\n",
1379 file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001380 err_code |= ERR_ALERT;
1381#endif
1382 newuser->pass = strdup(args[cur_arg + 1]);
1383 cur_arg += 2;
1384 continue;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001385 } else if (strcmp(args[cur_arg], "insecure-password") == 0) {
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001386 newuser->pass = strdup(args[cur_arg + 1]);
1387 newuser->flags |= AU_O_INSECURE;
1388 cur_arg += 2;
1389 continue;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001390 } else if (strcmp(args[cur_arg], "groups") == 0) {
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001391 newuser->u.groups_names = strdup(args[cur_arg + 1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001392 cur_arg += 2;
1393 continue;
1394 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001395 ha_alert("parsing [%s:%d]: '%s' only supports 'password', 'insecure-password' and 'groups' options.\n",
1396 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001397 err_code |= ERR_ALERT | ERR_FATAL;
1398 goto out;
1399 }
1400 }
1401 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001402 ha_alert("parsing [%s:%d]: unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "users");
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001403 err_code |= ERR_ALERT | ERR_FATAL;
1404 }
1405
1406out:
1407 return err_code;
1408}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001409
Christopher Faulet79bdef32016-11-04 22:36:15 +01001410int
1411cfg_parse_scope(const char *file, int linenum, char *line)
1412{
1413 char *beg, *end, *scope = NULL;
1414 int err_code = 0;
1415 const char *err;
1416
1417 beg = line + 1;
1418 end = strchr(beg, ']');
1419
1420 /* Detect end of scope declaration */
1421 if (!end || end == beg) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001422 ha_alert("parsing [%s:%d] : empty scope name is forbidden.\n",
1423 file, linenum);
Christopher Faulet79bdef32016-11-04 22:36:15 +01001424 err_code |= ERR_ALERT | ERR_FATAL;
1425 goto out;
1426 }
1427
1428 /* Get scope name and check its validity */
1429 scope = my_strndup(beg, end-beg);
1430 err = invalid_char(scope);
1431 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001432 ha_alert("parsing [%s:%d] : character '%c' is not permitted in a scope name.\n",
1433 file, linenum, *err);
Christopher Faulet79bdef32016-11-04 22:36:15 +01001434 err_code |= ERR_ALERT | ERR_ABORT;
1435 goto out;
1436 }
1437
1438 /* Be sure to have a scope declaration alone on its line */
1439 line = end+1;
1440 while (isspace((unsigned char)*line))
1441 line++;
1442 if (*line && *line != '#' && *line != '\n' && *line != '\r') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001443 ha_alert("parsing [%s:%d] : character '%c' is not permitted after scope declaration.\n",
1444 file, linenum, *line);
Christopher Faulet79bdef32016-11-04 22:36:15 +01001445 err_code |= ERR_ALERT | ERR_ABORT;
1446 goto out;
1447 }
1448
1449 /* We have a valid scope declaration, save it */
1450 free(cfg_scope);
1451 cfg_scope = scope;
1452 scope = NULL;
1453
1454 out:
1455 free(scope);
1456 return err_code;
1457}
1458
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01001459int
1460cfg_parse_track_sc_num(unsigned int *track_sc_num,
1461 const char *arg, const char *end, char **errmsg)
1462{
1463 const char *p;
1464 unsigned int num;
1465
1466 p = arg;
1467 num = read_uint64(&arg, end);
1468
1469 if (arg != end) {
1470 memprintf(errmsg, "Wrong track-sc number '%s'", p);
1471 return -1;
1472 }
1473
1474 if (num >= MAX_SESS_STKCTR) {
1475 memprintf(errmsg, "%u track-sc number exceeding "
1476 "%d (MAX_SESS_STKCTR-1) value", num, MAX_SESS_STKCTR - 1);
1477 return -1;
1478 }
1479
1480 *track_sc_num = num;
1481 return 0;
1482}
1483
Willy Tarreaubaaee002006-06-26 02:48:02 +02001484/*
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001485 * Detect a global section after a non-global one and output a diagnostic
1486 * warning.
1487 */
1488static void check_section_position(char *section_name,
1489 const char *file, int linenum,
1490 int *non_global_parsed)
1491{
1492 if (!strcmp(section_name, "global")) {
1493 if (*non_global_parsed == 1)
1494 _ha_diag_warning("parsing [%s:%d] : global section detected after a non-global one, the prevalence of their statements is unspecified\n", file, linenum);
1495 }
1496 else if (*non_global_parsed == 0) {
1497 *non_global_parsed = 1;
1498 }
1499}
1500
1501/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001502 * This function reads and parses the configuration file given in the argument.
Willy Tarreau058e9072009-07-20 09:30:05 +02001503 * Returns the error code, 0 if OK, or any combination of :
1504 * - ERR_ABORT: must abort ASAP
1505 * - ERR_FATAL: we can continue parsing but not start the service
1506 * - ERR_WARN: a warning has been emitted
1507 * - ERR_ALERT: an alert has been emitted
1508 * Only the two first ones can stop processing, the two others are just
1509 * indicators.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001510 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001511int readcfgfile(const char *file)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001512{
William Lallemand64e84512015-05-12 14:25:37 +02001513 char *thisline;
1514 int linesize = LINESIZE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001515 FILE *f;
1516 int linenum = 0;
Willy Tarreau058e9072009-07-20 09:30:05 +02001517 int err_code = 0;
William Lallemandd2ff56d2017-10-16 11:06:50 +02001518 struct cfg_section *cs = NULL, *pcs = NULL;
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001519 struct cfg_section *ics;
William Lallemand64e84512015-05-12 14:25:37 +02001520 int readbytes = 0;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001521 char *outline = NULL;
1522 size_t outlen = 0;
1523 size_t outlinesize = 0;
Willy Tarreau32234e72020-06-16 17:14:33 +02001524 int fatal = 0;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001525 int missing_lf = -1;
Willy Tarreau4b103022021-02-12 17:59:10 +01001526 int nested_cond_lvl = 0;
1527 enum nested_cond_state nested_conds[MAXNESTEDCONDS];
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001528 int non_global_section_parsed = 0;
William Lallemand64e84512015-05-12 14:25:37 +02001529
1530 if ((thisline = malloc(sizeof(*thisline) * linesize)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001531 ha_alert("parsing [%s] : out of memory.\n", file);
William Lallemand64e84512015-05-12 14:25:37 +02001532 return -1;
1533 }
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001534
David Carlier97880bb2016-04-08 10:35:26 +01001535 if ((f=fopen(file,"r")) == NULL) {
1536 free(thisline);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001537 return -1;
David Carlier97880bb2016-04-08 10:35:26 +01001538 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001539
William Lallemandb2f07452015-05-12 14:27:13 +02001540next_line:
William Lallemand64e84512015-05-12 14:25:37 +02001541 while (fgets(thisline + readbytes, linesize - readbytes, f) != NULL) {
Willy Tarreau3842f002009-06-14 11:39:52 +02001542 int arg, kwm = KWM_STD;
Krzysztof Piotr Oledzkie6bbd742007-11-01 00:33:12 +01001543 char *end;
1544 char *args[MAX_LINE_ARGS + 1];
1545 char *line = thisline;
1546
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001547 if (missing_lf != -1) {
Tim Duesterhusf92afb72020-08-18 22:00:04 +02001548 ha_alert("parsing [%s:%d]: Stray NUL character at position %d.\n",
1549 file, linenum, (missing_lf + 1));
1550 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001551 missing_lf = -1;
Tim Duesterhusf92afb72020-08-18 22:00:04 +02001552 break;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001553 }
1554
Willy Tarreaubaaee002006-06-26 02:48:02 +02001555 linenum++;
1556
Willy Tarreau32234e72020-06-16 17:14:33 +02001557 if (fatal >= 50) {
1558 ha_alert("parsing [%s:%d]: too many fatal errors (%d), stopping now.\n", file, linenum, fatal);
1559 break;
1560 }
1561
Willy Tarreaubaaee002006-06-26 02:48:02 +02001562 end = line + strlen(line);
1563
William Lallemand64e84512015-05-12 14:25:37 +02001564 if (end-line == linesize-1 && *(end-1) != '\n') {
Krzysztof Piotr Oledzkie6bbd742007-11-01 00:33:12 +01001565 /* Check if we reached the limit and the last char is not \n.
1566 * Watch out for the last line without the terminating '\n'!
1567 */
William Lallemand64e84512015-05-12 14:25:37 +02001568 char *newline;
1569 int newlinesize = linesize * 2;
1570
1571 newline = realloc(thisline, sizeof(*thisline) * newlinesize);
1572 if (newline == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001573 ha_alert("parsing [%s:%d]: line too long, cannot allocate memory.\n",
1574 file, linenum);
William Lallemand64e84512015-05-12 14:25:37 +02001575 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001576 fatal++;
Willy Tarreau40cb26f2020-06-25 09:37:54 +02001577 linenum--;
William Lallemand64e84512015-05-12 14:25:37 +02001578 continue;
1579 }
1580
1581 readbytes = linesize - 1;
1582 linesize = newlinesize;
1583 thisline = newline;
Willy Tarreau40cb26f2020-06-25 09:37:54 +02001584 linenum--;
William Lallemand64e84512015-05-12 14:25:37 +02001585 continue;
Krzysztof Piotr Oledzkie6bbd742007-11-01 00:33:12 +01001586 }
1587
William Lallemand64e84512015-05-12 14:25:37 +02001588 readbytes = 0;
1589
Willy Tarreau08488f62020-06-26 17:24:54 +02001590 if (end > line && *(end-1) == '\n') {
Tim Duesterhus70f58992020-06-22 22:57:44 +02001591 /* kill trailing LF */
1592 *(end - 1) = 0;
1593 }
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001594 else {
1595 /* mark this line as truncated */
1596 missing_lf = end - line;
1597 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001598
Willy Tarreaubaaee002006-06-26 02:48:02 +02001599 /* skip leading spaces */
Willy Tarreau8f8e6452007-06-17 21:51:38 +02001600 while (isspace((unsigned char)*line))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001601 line++;
William Lallemandf9873ba2015-05-05 17:37:14 +02001602
Joseph Herlanta14c03e2018-11-15 14:04:19 -08001603 if (*line == '[') {/* This is the beginning if a scope */
Christopher Faulet79bdef32016-11-04 22:36:15 +01001604 err_code |= cfg_parse_scope(file, linenum, line);
1605 goto next_line;
1606 }
1607
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001608 while (1) {
1609 uint32_t err;
1610 char *errptr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001611
Willy Tarreau61dd44b2020-06-25 07:35:42 +02001612 arg = MAX_LINE_ARGS + 1;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001613 outlen = outlinesize;
1614 err = parse_line(line, outline, &outlen, args, &arg,
1615 PARSE_OPT_ENV | PARSE_OPT_DQUOTE | PARSE_OPT_SQUOTE |
Amaury Denoyellefa41cb62020-10-01 14:32:35 +02001616 PARSE_OPT_BKSLASH | PARSE_OPT_SHARP | PARSE_OPT_WORD_EXPAND,
1617 &errptr);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001618
1619 if (err & PARSE_ERR_QUOTE) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001620 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1621
1622 ha_alert("parsing [%s:%d]: unmatched quote at position %d:\n"
1623 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001624 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001625 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001626 goto next_line;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001627 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001628
1629 if (err & PARSE_ERR_BRACE) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001630 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1631
1632 ha_alert("parsing [%s:%d]: unmatched brace in environment variable name at position %d:\n"
1633 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001634 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001635 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001636 goto next_line;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001637 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001638
1639 if (err & PARSE_ERR_VARNAME) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001640 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1641
1642 ha_alert("parsing [%s:%d]: forbidden first char in environment variable name at position %d:\n"
1643 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001644 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001645 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001646 goto next_line;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001647 }
William Lallemandb2f07452015-05-12 14:27:13 +02001648
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001649 if (err & PARSE_ERR_HEX) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001650 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1651
1652 ha_alert("parsing [%s:%d]: truncated or invalid hexadecimal sequence at position %d:\n"
1653 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001654 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus9f658a52020-06-16 18:14:21 +02001655 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001656 goto next_line;
1657 }
William Lallemandb2f07452015-05-12 14:27:13 +02001658
Amaury Denoyellefa41cb62020-10-01 14:32:35 +02001659 if (err & PARSE_ERR_WRONG_EXPAND) {
1660 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1661
1662 ha_alert("parsing [%s:%d]: truncated or invalid word expansion sequence at position %d:\n"
1663 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
1664 err_code |= ERR_ALERT | ERR_FATAL;
1665 fatal++;
1666 goto next_line;
1667 }
1668
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001669 if (err & (PARSE_ERR_TOOLARGE|PARSE_ERR_OVERLAP)) {
1670 outlinesize = (outlen + 1023) & -1024;
Ilya Shipitsin76837bc2021-01-07 22:45:13 +05001671 outline = my_realloc2(outline, outlinesize);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001672 if (outline == NULL) {
1673 ha_alert("parsing [%s:%d]: line too long, cannot allocate memory.\n",
1674 file, linenum);
William Lallemandb2f07452015-05-12 14:27:13 +02001675 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001676 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001677 goto next_line;
William Lallemandb2f07452015-05-12 14:27:13 +02001678 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001679 /* try again */
1680 continue;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001681 }
Willy Tarreau07d47062020-06-25 07:41:22 +02001682
1683 if (err & PARSE_ERR_TOOMANY) {
1684 /* only check this *after* being sure the output is allocated */
1685 ha_alert("parsing [%s:%d]: too many words, truncating after word %d, position %ld: <%s>.\n",
1686 file, linenum, MAX_LINE_ARGS, (long)(args[MAX_LINE_ARGS-1] - outline + 1), args[MAX_LINE_ARGS-1]);
1687 err_code |= ERR_ALERT | ERR_FATAL;
1688 fatal++;
1689 goto next_line;
1690 }
1691
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001692 /* everything's OK */
1693 break;
William Lallemandf9873ba2015-05-05 17:37:14 +02001694 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001695
1696 /* empty line */
1697 if (!**args)
1698 continue;
1699
Willy Tarreau4b103022021-02-12 17:59:10 +01001700 /* check for config macros */
1701 if (*args[0] == '.') {
1702 if (strcmp(args[0], ".if") == 0) {
1703 nested_cond_lvl++;
1704 if (nested_cond_lvl >= MAXNESTEDCONDS) {
1705 ha_alert("parsing [%s:%d]: too many nested '.if', max is %d.\n", file, linenum, MAXNESTEDCONDS);
1706 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1707 goto err;
1708 }
1709
1710 if (nested_conds[nested_cond_lvl - 1] == NESTED_COND_IF_DROP ||
1711 nested_conds[nested_cond_lvl - 1] == NESTED_COND_IF_SKIP ||
1712 nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELIF_DROP ||
1713 nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELIF_SKIP ||
1714 nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELSE_DROP) {
1715 nested_conds[nested_cond_lvl] = NESTED_COND_IF_SKIP;
1716 } else if (!*args[1] || *args[1] == '0') {
1717 /* empty = false */
1718 nested_conds[nested_cond_lvl] = NESTED_COND_IF_DROP;
1719 } else if (atoi(args[1]) > 0) {
1720 /* true */
1721 nested_conds[nested_cond_lvl] = NESTED_COND_IF_TAKE;
1722 } else {
1723 ha_alert("parsing [%s:%d]: unparsable conditional expression '%s'.\n", file, linenum, args[1]);
1724 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1725 goto err;
1726 }
1727 goto next_line;
1728 }
1729 else if (strcmp(args[0], ".elif") == 0) {
1730 if (!nested_cond_lvl) {
1731 ha_alert("parsing [%s:%d]: lone '.elif' with no matching '.if'.\n", file, linenum);
1732 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1733 goto err;
1734 }
1735
1736 if (nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_TAKE ||
1737 nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP) {
1738 ha_alert("parsing [%s:%d]: '.elif' after '.else' is not permitted.\n", file, linenum);
1739 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1740 goto err;
1741 }
1742
1743 if (nested_conds[nested_cond_lvl] == NESTED_COND_IF_TAKE ||
1744 nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
1745 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP) {
1746 nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_SKIP;
1747 } else if (!*args[1] || *args[1] == '0') {
1748 /* empty = false */
1749 nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_DROP;
1750 } else if (atoi(args[1]) > 0) {
1751 /* true */
1752 nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_TAKE;
1753 } else {
1754 ha_alert("parsing [%s:%d]: unparsable conditional expression '%s'.\n", file, linenum, args[1]);
1755 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1756 goto err;
1757 }
1758 goto next_line;
1759 }
1760 else if (strcmp(args[0], ".else") == 0) {
1761 if (!nested_cond_lvl) {
1762 ha_alert("parsing [%s:%d]: lone '.else' with no matching '.if'.\n", file, linenum);
1763 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1764 goto err;
1765 }
1766
1767 if (nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_TAKE ||
1768 nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP) {
1769 ha_alert("parsing [%s:%d]: '.else' after '.else' is not permitted.\n", file, linenum);
1770 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1771 goto err;
1772 }
1773
1774 if (nested_conds[nested_cond_lvl] == NESTED_COND_IF_TAKE ||
1775 nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
1776 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_TAKE ||
1777 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP) {
1778 nested_conds[nested_cond_lvl] = NESTED_COND_ELSE_DROP;
1779 } else {
1780 /* otherwise we take the "else" */
1781 nested_conds[nested_cond_lvl] = NESTED_COND_ELSE_TAKE;
1782 }
1783 goto next_line;
1784 }
1785 else if (strcmp(args[0], ".endif") == 0) {
1786 if (!nested_cond_lvl) {
1787 ha_alert("parsing [%s:%d]: lone '.endif' with no matching '.if'.\n", file, linenum);
1788 err_code |= ERR_ALERT | ERR_FATAL;
1789 fatal++;
1790 break;
1791 }
1792 nested_cond_lvl--;
1793 goto next_line;
1794 }
1795 }
1796
1797 if (nested_cond_lvl &&
1798 (nested_conds[nested_cond_lvl] == NESTED_COND_IF_DROP ||
1799 nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
1800 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_DROP ||
1801 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP ||
1802 nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP)) {
1803 /* The current block is masked out by the conditions */
1804 goto next_line;
1805 }
1806
1807 /* .warning/.error/.notice */
1808 if (*args[0] == '.') {
1809 if (strcmp(args[0], ".alert") == 0) {
1810 ha_alert("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
1811 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1812 goto err;
1813 }
1814 else if (strcmp(args[0], ".warning") == 0) {
1815 ha_warning("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
1816 err_code |= ERR_WARN;
1817 goto next_line;
1818 }
1819 else if (strcmp(args[0], ".notice") == 0) {
1820 ha_notice("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
1821 goto next_line;
1822 }
1823 else {
1824 ha_alert("parsing [%s:%d]: unknown directive '%s'.\n", file, linenum, args[0]);
1825 err_code |= ERR_ALERT | ERR_FATAL;
1826 fatal++;
1827 break;
1828 }
1829 }
1830
Willy Tarreau3842f002009-06-14 11:39:52 +02001831 /* check for keyword modifiers "no" and "default" */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001832 if (strcmp(args[0], "no") == 0) {
William Lallemand0f99e342011-10-12 17:50:54 +02001833 char *tmp;
1834
Willy Tarreau3842f002009-06-14 11:39:52 +02001835 kwm = KWM_NO;
William Lallemand0f99e342011-10-12 17:50:54 +02001836 tmp = args[0];
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01001837 for (arg=0; *args[arg+1]; arg++)
1838 args[arg] = args[arg+1]; // shift args after inversion
William Lallemand0f99e342011-10-12 17:50:54 +02001839 *tmp = '\0'; // fix the next arg to \0
1840 args[arg] = tmp;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01001841 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001842 else if (strcmp(args[0], "default") == 0) {
Willy Tarreau3842f002009-06-14 11:39:52 +02001843 kwm = KWM_DEF;
1844 for (arg=0; *args[arg+1]; arg++)
1845 args[arg] = args[arg+1]; // shift args after inversion
1846 }
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01001847
William Dauchyec730982019-10-27 20:08:10 +01001848 if (kwm != KWM_STD && strcmp(args[0], "option") != 0 &&
1849 strcmp(args[0], "log") != 0 && strcmp(args[0], "busy-polling") != 0 &&
Willy Tarreaud96f1122019-12-03 07:07:36 +01001850 strcmp(args[0], "set-dumpable") != 0 && strcmp(args[0], "strict-limits") != 0 &&
Amaury Denoyelle0f50cb92021-03-26 18:50:33 +01001851 strcmp(args[0], "insecure-fork-wanted") != 0 &&
1852 strcmp(args[0], "numa-cpu-mapping") != 0) {
William Dauchyec730982019-10-27 20:08:10 +01001853 ha_alert("parsing [%s:%d]: negation/default currently "
William Dauchy0fec3ab2019-10-27 20:08:11 +01001854 "supported only for options, log, busy-polling, "
Amaury Denoyelle0f50cb92021-03-26 18:50:33 +01001855 "set-dumpable, strict-limits, insecure-fork-wanted "
1856 "and numa-cpu-mapping.\n", file, linenum);
Willy Tarreau058e9072009-07-20 09:30:05 +02001857 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001858 fatal++;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01001859 }
1860
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001861 /* detect section start */
1862 list_for_each_entry(ics, &sections, list) {
1863 if (strcmp(args[0], ics->section_name) == 0) {
1864 cursection = ics->section_name;
Christopher Faulet7805e2b2018-11-30 13:50:47 +01001865 pcs = cs;
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001866 cs = ics;
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001867
1868 if (global.mode & MODE_DIAG) {
1869 check_section_position(args[0], file, linenum,
1870 &non_global_section_parsed);
1871 }
1872
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001873 break;
1874 }
Emeric Brun32da3c42010-09-23 18:39:19 +02001875 }
1876
Christopher Faulet7805e2b2018-11-30 13:50:47 +01001877 if (pcs && pcs->post_section_parser) {
Willy Tarreau32234e72020-06-16 17:14:33 +02001878 int status;
1879
1880 status = pcs->post_section_parser();
1881 err_code |= status;
1882 if (status & ERR_FATAL)
1883 fatal++;
1884
Christopher Faulet7805e2b2018-11-30 13:50:47 +01001885 if (err_code & ERR_ABORT)
1886 goto err;
Christopher Faulet7805e2b2018-11-30 13:50:47 +01001887 }
Christopher Fauletbf7a9592018-12-02 09:37:38 +01001888 pcs = NULL;
Christopher Faulet7805e2b2018-11-30 13:50:47 +01001889
William Lallemandd2ff56d2017-10-16 11:06:50 +02001890 if (!cs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001891 ha_alert("parsing [%s:%d]: unknown keyword '%s' out of section.\n", file, linenum, args[0]);
Willy Tarreau058e9072009-07-20 09:30:05 +02001892 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001893 fatal++;
William Lallemandd2ff56d2017-10-16 11:06:50 +02001894 } else {
Willy Tarreau32234e72020-06-16 17:14:33 +02001895 int status;
1896
1897 status = cs->section_parser(file, linenum, args, kwm);
1898 err_code |= status;
1899 if (status & ERR_FATAL)
1900 fatal++;
1901
William Lallemandd2ff56d2017-10-16 11:06:50 +02001902 if (err_code & ERR_ABORT)
1903 goto err;
1904 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001905 }
William Lallemandd2ff56d2017-10-16 11:06:50 +02001906
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001907 if (missing_lf != -1) {
Tim Duesterhusf92afb72020-08-18 22:00:04 +02001908 ha_alert("parsing [%s:%d]: Missing LF on last line, file might have been truncated at position %d.\n",
1909 file, linenum, (missing_lf + 1));
1910 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001911 }
1912
Christopher Fauletbf7a9592018-12-02 09:37:38 +01001913 if (cs && cs->post_section_parser)
Christopher Faulet7805e2b2018-11-30 13:50:47 +01001914 err_code |= cs->post_section_parser();
William Lallemandd2ff56d2017-10-16 11:06:50 +02001915
Willy Tarreau4b103022021-02-12 17:59:10 +01001916 if (nested_cond_lvl) {
1917 ha_alert("parsing [%s:%d]: non-terminated '.if' block.\n", file, linenum);
1918 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1919 }
William Lallemandd2ff56d2017-10-16 11:06:50 +02001920err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001921 ha_free(&cfg_scope);
Willy Tarreau6daf3432008-01-22 16:44:08 +01001922 cursection = NULL;
William Lallemand64e84512015-05-12 14:25:37 +02001923 free(thisline);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001924 free(outline);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001925 fclose(f);
Willy Tarreau058e9072009-07-20 09:30:05 +02001926 return err_code;
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001927}
1928
Willy Tarreau64ab6072014-09-16 12:17:36 +02001929/* This function propagates processes from frontend <from> to backend <to> so
1930 * that it is always guaranteed that a backend pointed to by a frontend is
1931 * bound to all of its processes. After that, if the target is a "listen"
1932 * instance, the function recursively descends the target's own targets along
Willy Tarreau98d04852015-05-26 12:18:29 +02001933 * default_backend and use_backend rules. Since the bits are
Willy Tarreau64ab6072014-09-16 12:17:36 +02001934 * checked first to ensure that <to> is already bound to all processes of
1935 * <from>, there is no risk of looping and we ensure to follow the shortest
1936 * path to the destination.
1937 *
1938 * It is possible to set <to> to NULL for the first call so that the function
1939 * takes care of visiting the initial frontend in <from>.
1940 *
1941 * It is important to note that the function relies on the fact that all names
1942 * have already been resolved.
1943 */
1944void propagate_processes(struct proxy *from, struct proxy *to)
1945{
1946 struct switching_rule *rule;
Willy Tarreau64ab6072014-09-16 12:17:36 +02001947
1948 if (to) {
1949 /* check whether we need to go down */
1950 if (from->bind_proc &&
1951 (from->bind_proc & to->bind_proc) == from->bind_proc)
1952 return;
1953
1954 if (!from->bind_proc && !to->bind_proc)
1955 return;
1956
1957 to->bind_proc = from->bind_proc ?
1958 (to->bind_proc | from->bind_proc) : 0;
1959
1960 /* now propagate down */
1961 from = to;
1962 }
1963
Willy Tarreau8a95d8c2014-12-18 13:56:26 +01001964 if (!(from->cap & PR_CAP_FE))
Willy Tarreau64ab6072014-09-16 12:17:36 +02001965 return;
1966
Willy Tarreauc3914d42020-09-24 08:39:22 +02001967 if (from->disabled)
Willy Tarreauf6b70012014-12-18 14:00:43 +01001968 return;
1969
Willy Tarreau64ab6072014-09-16 12:17:36 +02001970 /* default_backend */
1971 if (from->defbe.be)
1972 propagate_processes(from, from->defbe.be);
1973
1974 /* use_backend */
1975 list_for_each_entry(rule, &from->switching_rules, list) {
Cyril Bonté51639692014-10-02 19:56:25 +02001976 if (rule->dynamic)
1977 continue;
Willy Tarreau64ab6072014-09-16 12:17:36 +02001978 to = rule->be.backend;
1979 propagate_processes(from, to);
1980 }
Willy Tarreau64ab6072014-09-16 12:17:36 +02001981}
1982
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01001983#if defined(__linux__) && defined USE_CPU_AFFINITY
1984/* filter directory name of the pattern node<X> */
1985static int numa_filter(const struct dirent *dir)
1986{
1987 char *endptr;
1988
1989 /* dir name must start with "node" prefix */
1990 if (strncmp(dir->d_name, "node", 4))
1991 return 0;
1992
1993 /* dir name must be at least 5 characters long */
1994 if (!dir->d_name[4])
1995 return 0;
1996
1997 /* dir name must end with a numeric id */
1998 if (strtol(&dir->d_name[4], &endptr, 10) < 0 || *endptr)
1999 return 0;
2000
2001 /* all tests succeeded */
2002 return 1;
2003}
2004
2005/* Parse a linux cpu map string representing to a numeric cpu mask map
2006 * The cpu map string is a list of 4-byte hex strings separated by commas, with
2007 * most-significant byte first, one bit per cpu number.
2008 */
2009static void parse_cpumap(char *cpumap_str, struct hap_cpuset *cpu_set)
2010{
2011 unsigned long cpumap;
2012 char *start, *endptr, *comma;
2013 int i, j;
2014
2015 ha_cpuset_zero(cpu_set);
2016
2017 i = 0;
2018 do {
2019 /* reverse-search for a comma, parse the string after the comma
2020 * or at the beginning if no comma found
2021 */
2022 comma = strrchr(cpumap_str, ',');
2023 start = comma ? comma + 1 : cpumap_str;
2024
2025 cpumap = strtoul(start, &endptr, 16);
2026 for (j = 0; cpumap; cpumap >>= 1, ++j) {
2027 if (cpumap & 0x1)
2028 ha_cpuset_set(cpu_set, j + i * 32);
2029 }
2030
2031 if (comma)
2032 *comma = '\0';
2033 ++i;
2034 } while (comma);
2035}
2036
2037/* Read the first line of a file from <path> into the trash buffer.
2038 * Returns 0 on success, otherwise non-zero.
2039 */
2040static int read_file_to_trash(const char *path)
2041{
2042 FILE *file;
2043 int ret = 1;
2044
2045 file = fopen(path, "r");
2046 if (file) {
2047 if (fgets(trash.area, trash.size, file))
2048 ret = 0;
2049
2050 fclose(file);
2051 }
2052
2053 return ret;
2054}
2055
2056/* Inspect the cpu topology of the machine on startup. If a multi-socket
2057 * machine is detected, try to bind on the first node with active cpu. This is
2058 * done to prevent an impact on the overall performance when the topology of
2059 * the machine is unknown. This function is not called if one of the conditions
2060 * is met :
2061 * - a non-null nbthread directive is active
2062 * - a restrictive cpu-map directive is active
2063 * - a restrictive affinity is already applied, for example via taskset
2064 *
2065 * Returns the count of cpus selected. If no automatic binding was required or
2066 * an error occurred and the topology is unknown, 0 is returned.
2067 */
2068static int numa_detect_topology()
2069{
2070 struct dirent **node_dirlist;
2071 int node_dirlist_size;
2072
2073 struct hap_cpuset active_cpus, node_cpu_set;
2074 const char *parse_cpu_set_args[2];
2075 char cpumap_path[PATH_MAX];
2076 char *err = NULL;
2077
2078 /* node_cpu_set count is used as return value */
2079 ha_cpuset_zero(&node_cpu_set);
2080
2081 /* 1. count the sysfs node<X> directories */
Willy Tarreau07bf21c2021-04-23 19:09:16 +02002082 node_dirlist = NULL;
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002083 node_dirlist_size = scandir(NUMA_DETECT_SYSTEM_SYSFS_PATH"/node", &node_dirlist, numa_filter, alphasort);
2084 if (node_dirlist_size <= 1)
2085 goto free_scandir_entries;
2086
2087 /* 2. read and parse the list of currently online cpu */
2088 if (read_file_to_trash(NUMA_DETECT_SYSTEM_SYSFS_PATH"/cpu/online")) {
2089 ha_notice("Cannot read online CPUs list, will not try to refine binding\n");
2090 goto free_scandir_entries;
2091 }
2092
2093 parse_cpu_set_args[0] = trash.area;
2094 parse_cpu_set_args[1] = "\0";
2095 if (parse_cpu_set(parse_cpu_set_args, &active_cpus, 1, &err)) {
2096 ha_notice("Cannot read online CPUs list: '%s'. Will not try to refine binding\n", err);
2097 free(err);
2098 goto free_scandir_entries;
2099 }
2100
2101 /* 3. loop through nodes dirs and find the first one with active cpus */
2102 while (node_dirlist_size--) {
2103 const char *node = node_dirlist[node_dirlist_size]->d_name;
2104 ha_cpuset_zero(&node_cpu_set);
2105
2106 snprintf(cpumap_path, PATH_MAX, "%s/node/%s/cpumap",
2107 NUMA_DETECT_SYSTEM_SYSFS_PATH, node);
2108
2109 if (read_file_to_trash(cpumap_path)) {
2110 ha_notice("Cannot read CPUs list of '%s', will not select them to refine binding\n", node);
2111 free(node_dirlist[node_dirlist_size]);
2112 continue;
2113 }
2114
2115 parse_cpumap(trash.area, &node_cpu_set);
2116 ha_cpuset_and(&node_cpu_set, &active_cpus);
2117
2118 /* 5. set affinity on the first found node with active cpus */
2119 if (!ha_cpuset_count(&node_cpu_set)) {
2120 free(node_dirlist[node_dirlist_size]);
2121 continue;
2122 }
2123
2124 ha_diag_warning("Multi-socket cpu detected, automatically binding on active CPUs of '%s' (%u active cpu(s))\n", node, ha_cpuset_count(&node_cpu_set));
2125 if (sched_setaffinity(getpid(), sizeof(node_cpu_set.cpuset), &node_cpu_set.cpuset) == -1) {
2126 ha_warning("Cannot set the cpu affinity for this multi-cpu machine\n");
2127
2128 /* clear the cpuset used as return value */
2129 ha_cpuset_zero(&node_cpu_set);
2130 }
2131
2132 free(node_dirlist[node_dirlist_size]);
2133 break;
2134 }
2135
2136 free_scandir_entries:
2137 while (node_dirlist_size-- > 0)
2138 free(node_dirlist[node_dirlist_size]);
2139 free(node_dirlist);
2140
2141 return ha_cpuset_count(&node_cpu_set);
2142}
2143#endif /* __linux__ && USE_CPU_AFFINITY */
2144
Willy Tarreaubb925012009-07-23 13:36:36 +02002145/*
2146 * Returns the error code, 0 if OK, or any combination of :
2147 * - ERR_ABORT: must abort ASAP
2148 * - ERR_FATAL: we can continue parsing but not start the service
2149 * - ERR_WARN: a warning has been emitted
2150 * - ERR_ALERT: an alert has been emitted
2151 * Only the two first ones can stop processing, the two others are just
2152 * indicators.
2153 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002154int check_config_validity()
2155{
2156 int cfgerr = 0;
2157 struct proxy *curproxy = NULL;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002158 struct stktable *t;
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002159 struct server *newsrv = NULL;
Willy Tarreaubb925012009-07-23 13:36:36 +02002160 int err_code = 0;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002161 unsigned int next_pxid = 1;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002162 struct bind_conf *bind_conf;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002163 char *err;
William Lallemand48b4bb42017-10-23 14:36:34 +02002164 struct cfg_postparser *postparser;
Emeric Brun750fe792020-12-23 16:51:12 +01002165 struct resolvers *curr_resolvers = NULL;
Amaury Denoyelle3efee652021-03-08 17:31:39 +01002166 int i;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002167
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002168 bind_conf = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002169 /*
2170 * Now, check for the integrity of all that we have collected.
2171 */
2172
2173 /* will be needed further to delay some tasks */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002174 tv_update_date(0,1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002175
Willy Tarreau193b8c62012-11-22 00:17:38 +01002176 if (!global.tune.max_http_hdr)
2177 global.tune.max_http_hdr = MAX_HTTP_HDR;
2178
2179 if (!global.tune.cookie_len)
2180 global.tune.cookie_len = CAPTURE_LEN;
2181
Stéphane Cottin23e9e932017-05-18 08:58:41 +02002182 if (!global.tune.requri_len)
2183 global.tune.requri_len = REQURI_LEN;
2184
Willy Tarreau149ab772019-01-26 14:27:06 +01002185 if (!global.nbthread) {
2186 /* nbthread not set, thus automatic. In this case, and only if
2187 * running on a single process, we enable the same number of
2188 * threads as the number of CPUs the process is bound to. This
2189 * allows to easily control the number of threads using taskset.
2190 */
2191 global.nbthread = 1;
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002192
Willy Tarreau149ab772019-01-26 14:27:06 +01002193#if defined(USE_THREAD)
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002194 if (global.nbproc == 1) {
2195 int numa_cores = 0;
2196#if defined(__linux__) && defined USE_CPU_AFFINITY
Amaury Denoyelle0f50cb92021-03-26 18:50:33 +01002197 if (global.numa_cpu_mapping && !thread_cpu_mask_forced())
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002198 numa_cores = numa_detect_topology();
2199#endif
2200 global.nbthread = numa_cores ? numa_cores :
2201 thread_cpus_enabled_at_boot;
2202 }
Willy Tarreau149ab772019-01-26 14:27:06 +01002203 all_threads_mask = nbits(global.nbthread);
2204#endif
2205 }
2206
Willy Tarreau26f6ae12019-02-02 12:56:15 +01002207 if (global.nbproc > 1 && global.nbthread > 1) {
2208 ha_alert("config : cannot enable multiple processes if multiple threads are configured. Please use either nbproc or nbthread but not both.\n");
2209 err_code |= ERR_ALERT | ERR_FATAL;
2210 goto out;
2211 }
2212
Willy Tarreaubafbe012017-11-24 17:34:44 +01002213 pool_head_requri = create_pool("requri", global.tune.requri_len , MEM_F_SHARED);
Emeric Brun96fd9262017-07-05 13:33:16 +02002214
Willy Tarreaubafbe012017-11-24 17:34:44 +01002215 pool_head_capture = create_pool("capture", global.tune.cookie_len, MEM_F_SHARED);
Willy Tarreau193b8c62012-11-22 00:17:38 +01002216
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01002217 /* Post initialisation of the users and groups lists. */
2218 err_code = userlist_postinit();
2219 if (err_code != ERR_NONE)
2220 goto out;
2221
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002222 /* first, we will invert the proxy list order */
2223 curproxy = NULL;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002224 while (proxies_list) {
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002225 struct proxy *next;
2226
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002227 next = proxies_list->next;
2228 proxies_list->next = curproxy;
2229 curproxy = proxies_list;
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002230 if (!next)
2231 break;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002232 proxies_list = next;
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002233 }
2234
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002235 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreau55ea7572007-06-17 19:56:27 +02002236 struct switching_rule *rule;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002237 struct server_rule *srule;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002238 struct sticking_rule *mrule;
Dragan Dosen1322d092015-09-22 16:05:32 +02002239 struct logsrv *tmplogsrv;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002240 unsigned int next_id;
Willy Tarreau16a21472012-11-19 12:39:59 +01002241 int nbproc;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002242
Willy Tarreau050536d2012-10-04 08:47:34 +02002243 if (curproxy->uuid < 0) {
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002244 /* proxy ID not set, use automatic numbering with first
2245 * spare entry starting with next_pxid.
2246 */
2247 next_pxid = get_next_id(&used_proxy_id, next_pxid);
2248 curproxy->conf.id.key = curproxy->uuid = next_pxid;
2249 eb32_insert(&used_proxy_id, &curproxy->conf.id);
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002250 }
Krzysztof Piotr Oledzkidf5cb9f2010-02-05 20:58:27 +01002251 next_pxid++;
2252
Willy Tarreau55ea7572007-06-17 19:56:27 +02002253
Willy Tarreauc3914d42020-09-24 08:39:22 +02002254 if (curproxy->disabled) {
Willy Tarreau02b092f2020-10-07 18:36:54 +02002255 /* ensure we don't keep listeners uselessly bound. We
2256 * can't disable their listeners yet (fdtab not
2257 * allocated yet) but let's skip them.
2258 */
Dragan Dosen7d61a332019-05-07 14:16:18 +02002259 if (curproxy->table) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002260 ha_free(&curproxy->table->peers.name);
Dragan Dosen7d61a332019-05-07 14:16:18 +02002261 curproxy->table->peers.p = NULL;
2262 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002263 continue;
2264 }
2265
Willy Tarreau102df612014-05-07 23:56:38 +02002266 /* Check multi-process mode compatibility for the current proxy */
2267
2268 if (curproxy->bind_proc) {
2269 /* an explicit bind-process was specified, let's check how many
2270 * processes remain.
2271 */
David Carliere6c39412015-07-02 07:00:17 +00002272 nbproc = my_popcountl(curproxy->bind_proc);
Willy Tarreau102df612014-05-07 23:56:38 +02002273
Willy Tarreaua38a7172019-02-02 17:11:28 +01002274 curproxy->bind_proc &= all_proc_mask;
Willy Tarreau102df612014-05-07 23:56:38 +02002275 if (!curproxy->bind_proc && nbproc == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002276 ha_warning("Proxy '%s': the process specified on the 'bind-process' directive refers to a process number that is higher than global.nbproc. The proxy has been forced to run on process 1 only.\n", curproxy->id);
Willy Tarreau102df612014-05-07 23:56:38 +02002277 curproxy->bind_proc = 1;
2278 }
2279 else if (!curproxy->bind_proc && nbproc > 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002280 ha_warning("Proxy '%s': all processes specified on the 'bind-process' directive refer to numbers that are all higher than global.nbproc. The directive was ignored and the proxy will run on all processes.\n", curproxy->id);
Willy Tarreau102df612014-05-07 23:56:38 +02002281 curproxy->bind_proc = 0;
2282 }
2283 }
2284
Willy Tarreau3d209582014-05-09 17:06:11 +02002285 /* check and reduce the bind-proc of each listener */
2286 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
2287 unsigned long mask;
2288
Willy Tarreau45a66cc2017-11-24 11:28:00 +01002289 /* HTTP frontends with "h2" as ALPN/NPN will work in
2290 * HTTP/2 and absolutely require buffers 16kB or larger.
2291 */
2292#ifdef USE_OPENSSL
2293 if (curproxy->mode == PR_MODE_HTTP && global.tune.bufsize < 16384) {
2294#ifdef OPENSSL_NPN_NEGOTIATED
2295 /* check NPN */
Willy Tarreau4db49c02018-11-11 10:36:25 +01002296 if (bind_conf->ssl_conf.npn_str && strstr(bind_conf->ssl_conf.npn_str, "\002h2")) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002297 ha_alert("config : HTTP frontend '%s' enables HTTP/2 via NPN at [%s:%d], so global.tune.bufsize must be at least 16384 bytes (%d now).\n",
2298 curproxy->id, bind_conf->file, bind_conf->line, global.tune.bufsize);
Willy Tarreau45a66cc2017-11-24 11:28:00 +01002299 cfgerr++;
2300 }
2301#endif
2302#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2303 /* check ALPN */
Willy Tarreau4db49c02018-11-11 10:36:25 +01002304 if (bind_conf->ssl_conf.alpn_str && strstr(bind_conf->ssl_conf.alpn_str, "\002h2")) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002305 ha_alert("config : HTTP frontend '%s' enables HTTP/2 via ALPN at [%s:%d], so global.tune.bufsize must be at least 16384 bytes (%d now).\n",
2306 curproxy->id, bind_conf->file, bind_conf->line, global.tune.bufsize);
Willy Tarreau45a66cc2017-11-24 11:28:00 +01002307 cfgerr++;
2308 }
2309#endif
2310 } /* HTTP && bufsize < 16384 */
2311#endif
2312
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002313 /* detect and address thread affinity inconsistencies */
Willy Tarreaue26993c2020-09-03 07:18:55 +02002314 mask = thread_mask(bind_conf->settings.bind_thread);
Willy Tarreau3d957172019-02-02 18:00:17 +01002315 if (!(mask & all_threads_mask)) {
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002316 unsigned long new_mask = 0;
2317
2318 while (mask) {
Willy Tarreau0c026f42018-08-01 19:12:20 +02002319 new_mask |= mask & all_threads_mask;
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002320 mask >>= global.nbthread;
2321 }
2322
Willy Tarreaue26993c2020-09-03 07:18:55 +02002323 bind_conf->settings.bind_thread = new_mask;
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002324 ha_warning("Proxy '%s': the thread range specified on the 'process' directive of 'bind %s' at [%s:%d] only refers to thread numbers out of the range defined by the global 'nbthread' directive. The thread numbers were remapped to existing threads instead (mask 0x%lx).\n",
2325 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line, new_mask);
2326 }
2327
2328 /* detect process and nbproc affinity inconsistencies */
Willy Tarreaue26993c2020-09-03 07:18:55 +02002329 mask = proc_mask(bind_conf->settings.bind_proc) & proc_mask(curproxy->bind_proc);
Willy Tarreaua36b3242019-02-02 13:14:34 +01002330 if (!(mask & all_proc_mask)) {
2331 mask = proc_mask(curproxy->bind_proc) & all_proc_mask;
Willy Tarreaue26993c2020-09-03 07:18:55 +02002332 nbproc = my_popcountl(bind_conf->settings.bind_proc);
2333 bind_conf->settings.bind_proc = proc_mask(bind_conf->settings.bind_proc) & mask;
Willy Tarreau3d209582014-05-09 17:06:11 +02002334
Willy Tarreaue26993c2020-09-03 07:18:55 +02002335 if (!bind_conf->settings.bind_proc && nbproc == 1) {
Willy Tarreaua36b3242019-02-02 13:14:34 +01002336 ha_warning("Proxy '%s': the process number specified on the 'process' directive of 'bind %s' at [%s:%d] refers to a process not covered by the proxy. This has been fixed by forcing it to run on the proxy's first process only.\n",
2337 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreaue26993c2020-09-03 07:18:55 +02002338 bind_conf->settings.bind_proc = mask & ~(mask - 1);
Willy Tarreaua36b3242019-02-02 13:14:34 +01002339 }
Willy Tarreaue26993c2020-09-03 07:18:55 +02002340 else if (!bind_conf->settings.bind_proc && nbproc > 1) {
Willy Tarreaua36b3242019-02-02 13:14:34 +01002341 ha_warning("Proxy '%s': the process range specified on the 'process' directive of 'bind %s' at [%s:%d] only refers to processes not covered by the proxy. The directive was ignored so that all of the proxy's processes are used.\n",
2342 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreaue26993c2020-09-03 07:18:55 +02002343 bind_conf->settings.bind_proc = 0;
Willy Tarreaua36b3242019-02-02 13:14:34 +01002344 }
Willy Tarreau3d209582014-05-09 17:06:11 +02002345 }
2346 }
2347
Willy Tarreauff01a212009-03-15 13:46:16 +01002348 switch (curproxy->mode) {
Willy Tarreauff01a212009-03-15 13:46:16 +01002349 case PR_MODE_TCP:
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002350 cfgerr += proxy_cfg_ensure_no_http(curproxy);
Willy Tarreauff01a212009-03-15 13:46:16 +01002351 break;
2352
2353 case PR_MODE_HTTP:
Willy Tarreau25320b22013-03-24 07:22:08 +01002354 curproxy->http_needed = 1;
Willy Tarreauff01a212009-03-15 13:46:16 +01002355 break;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002356
2357 case PR_MODE_CLI:
2358 cfgerr += proxy_cfg_ensure_no_http(curproxy);
2359 break;
Emeric Brun54932b42020-07-07 09:43:24 +02002360 case PR_MODE_SYSLOG:
Willy Tarreaua389c9e2020-10-07 17:49:42 +02002361 case PR_MODE_PEERS:
Emeric Brun54932b42020-07-07 09:43:24 +02002362 case PR_MODES:
2363 /* should not happen, bug gcc warn missing switch statement */
Willy Tarreaua389c9e2020-10-07 17:49:42 +02002364 ha_alert("config : %s '%s' cannot use peers or syslog mode for this proxy. NOTE: PLEASE REPORT THIS TO DEVELOPERS AS YOU'RE NOT SUPPOSED TO BE ABLE TO CREATE A CONFIGURATION TRIGGERING THIS!\n",
Emeric Brun54932b42020-07-07 09:43:24 +02002365 proxy_type_str(curproxy), curproxy->id);
2366 cfgerr++;
2367 break;
Willy Tarreauff01a212009-03-15 13:46:16 +01002368 }
2369
Willy Tarreau4975d142021-03-13 11:00:33 +01002370 if (curproxy != global.cli_fe && (curproxy->cap & PR_CAP_FE) && LIST_ISEMPTY(&curproxy->conf.listeners)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002371 ha_warning("config : %s '%s' has no 'bind' directive. Please declare it as a backend if this was intended.\n",
2372 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauf3934b82015-08-11 11:36:45 +02002373 err_code |= ERR_WARN;
2374 }
2375
Willy Tarreau77e0dae2020-10-14 15:44:27 +02002376 if (curproxy->cap & PR_CAP_BE) {
Willy Tarreauf3e49f92009-10-03 12:21:20 +02002377 if (curproxy->lbprm.algo & BE_LB_KIND) {
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002378 if (curproxy->options & PR_O_TRANSP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002379 ha_alert("config : %s '%s' cannot use both transparent and balance mode.\n",
2380 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002381 cfgerr++;
2382 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002383#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002384 else if (curproxy->srv == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002385 ha_alert("config : %s '%s' needs at least 1 server in balance mode.\n",
2386 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002387 cfgerr++;
2388 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002389#endif
Willy Tarreau1620ec32011-08-06 17:05:02 +02002390 else if (curproxy->options & PR_O_DISPATCH) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002391 ha_warning("config : dispatch address of %s '%s' will be ignored in balance mode.\n",
2392 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubb925012009-07-23 13:36:36 +02002393 err_code |= ERR_WARN;
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002394 }
2395 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02002396 else if (!(curproxy->options & (PR_O_TRANSP | PR_O_DISPATCH | PR_O_HTTP_PROXY))) {
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002397 /* If no LB algo is set in a backend, and we're not in
2398 * transparent mode, dispatch mode nor proxy mode, we
2399 * want to use balance roundrobin by default.
2400 */
2401 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2402 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002403 }
2404 }
Willy Tarreau193cf932007-09-17 10:17:23 +02002405
Willy Tarreau1620ec32011-08-06 17:05:02 +02002406 if (curproxy->options & PR_O_DISPATCH)
2407 curproxy->options &= ~(PR_O_TRANSP | PR_O_HTTP_PROXY);
2408 else if (curproxy->options & PR_O_HTTP_PROXY)
2409 curproxy->options &= ~(PR_O_DISPATCH | PR_O_TRANSP);
2410 else if (curproxy->options & PR_O_TRANSP)
2411 curproxy->options &= ~(PR_O_DISPATCH | PR_O_HTTP_PROXY);
Willy Tarreau82936582007-11-30 15:20:09 +01002412
Christopher Faulete5870d82020-04-15 11:32:03 +02002413 if ((curproxy->tcpcheck_rules.flags & TCPCHK_RULES_UNUSED_HTTP_RS)) {
2414 ha_warning("config : %s '%s' uses http-check rules without 'option httpchk', so the rules are ignored.\n",
2415 proxy_type_str(curproxy), curproxy->id);
2416 err_code |= ERR_WARN;
2417 }
2418
2419 if ((curproxy->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK &&
Christopher Fauletc0fcbe42020-06-03 19:00:42 +02002420 (curproxy->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) != TCPCHK_RULES_HTTP_CHK) {
Willy Tarreau1620ec32011-08-06 17:05:02 +02002421 if (curproxy->options & PR_O_DISABLE404) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002422 ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option httpchk').\n",
2423 "disable-on-404", proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1620ec32011-08-06 17:05:02 +02002424 err_code |= ERR_WARN;
2425 curproxy->options &= ~PR_O_DISABLE404;
2426 }
2427 if (curproxy->options2 & PR_O2_CHK_SNDST) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002428 ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option httpchk').\n",
2429 "send-state", proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1620ec32011-08-06 17:05:02 +02002430 err_code |= ERR_WARN;
2431 curproxy->options &= ~PR_O2_CHK_SNDST;
2432 }
Willy Tarreauef781042010-01-27 11:53:01 +01002433 }
2434
Simon Horman98637e52014-06-20 12:30:16 +09002435 if ((curproxy->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2436 if (!global.external_check) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002437 ha_alert("Proxy '%s' : '%s' unable to find required 'global.external-check'.\n",
2438 curproxy->id, "option external-check");
Simon Horman98637e52014-06-20 12:30:16 +09002439 cfgerr++;
2440 }
2441 if (!curproxy->check_command) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002442 ha_alert("Proxy '%s' : '%s' unable to find required 'external-check command'.\n",
2443 curproxy->id, "option external-check");
Simon Horman98637e52014-06-20 12:30:16 +09002444 cfgerr++;
2445 }
Willy Tarreaud96f1122019-12-03 07:07:36 +01002446 if (!(global.tune.options & GTUNE_INSECURE_FORK)) {
2447 ha_warning("Proxy '%s' : 'insecure-fork-wanted' not enabled in the global section, '%s' will likely fail.\n",
2448 curproxy->id, "option external-check");
2449 err_code |= ERR_WARN;
2450 }
Simon Horman98637e52014-06-20 12:30:16 +09002451 }
2452
Simon Horman64e34162015-02-06 11:11:57 +09002453 if (curproxy->email_alert.set) {
Simon Horman0ba0e4a2015-01-30 11:23:00 +09002454 if (!(curproxy->email_alert.mailers.name && curproxy->email_alert.from && curproxy->email_alert.to)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002455 ha_warning("config : 'email-alert' will be ignored for %s '%s' (the presence any of "
2456 "'email-alert from', 'email-alert level' 'email-alert mailers', "
2457 "'email-alert myhostname', or 'email-alert to' "
2458 "requires each of 'email-alert from', 'email-alert mailers' and 'email-alert to' "
2459 "to be present).\n",
2460 proxy_type_str(curproxy), curproxy->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09002461 err_code |= ERR_WARN;
2462 free_email_alert(curproxy);
2463 }
2464 if (!curproxy->email_alert.myhostname)
Cyril Bontée22bfd62015-12-04 03:07:07 +01002465 curproxy->email_alert.myhostname = strdup(hostname);
Simon Horman9dc49962015-01-30 11:22:59 +09002466 }
2467
Simon Horman98637e52014-06-20 12:30:16 +09002468 if (curproxy->check_command) {
2469 int clear = 0;
2470 if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_EXT_CHK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002471 ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option external-check').\n",
2472 "external-check command", proxy_type_str(curproxy), curproxy->id);
Simon Horman98637e52014-06-20 12:30:16 +09002473 err_code |= ERR_WARN;
2474 clear = 1;
2475 }
2476 if (curproxy->check_command[0] != '/' && !curproxy->check_path) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002477 ha_alert("Proxy '%s': '%s' does not have a leading '/' and 'external-check path' is not set.\n",
2478 curproxy->id, "external-check command");
Simon Horman98637e52014-06-20 12:30:16 +09002479 cfgerr++;
2480 }
2481 if (clear) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002482 ha_free(&curproxy->check_command);
Simon Horman98637e52014-06-20 12:30:16 +09002483 }
2484 }
2485
2486 if (curproxy->check_path) {
2487 if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_EXT_CHK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002488 ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option external-check').\n",
2489 "external-check path", proxy_type_str(curproxy), curproxy->id);
Simon Horman98637e52014-06-20 12:30:16 +09002490 err_code |= ERR_WARN;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002491 ha_free(&curproxy->check_path);
Simon Horman98637e52014-06-20 12:30:16 +09002492 }
2493 }
2494
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002495 /* if a default backend was specified, let's find it */
2496 if (curproxy->defbe.name) {
2497 struct proxy *target;
2498
Willy Tarreauafb39922015-05-26 12:04:09 +02002499 target = proxy_be_by_name(curproxy->defbe.name);
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +01002500 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002501 ha_alert("Proxy '%s': unable to find required default_backend: '%s'.\n",
2502 curproxy->id, curproxy->defbe.name);
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002503 cfgerr++;
2504 } else if (target == curproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002505 ha_alert("Proxy '%s': loop detected for default_backend: '%s'.\n",
2506 curproxy->id, curproxy->defbe.name);
Willy Tarreaubb925012009-07-23 13:36:36 +02002507 cfgerr++;
Willy Tarreauafb39922015-05-26 12:04:09 +02002508 } else if (target->mode != curproxy->mode &&
2509 !(curproxy->mode == PR_MODE_TCP && target->mode == PR_MODE_HTTP)) {
2510
Christopher Faulet767a84b2017-11-24 16:50:31 +01002511 ha_alert("%s %s '%s' (%s:%d) tries to use incompatible %s %s '%s' (%s:%d) as its default backend (see 'mode').\n",
2512 proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
2513 curproxy->conf.file, curproxy->conf.line,
2514 proxy_mode_str(target->mode), proxy_type_str(target), target->id,
2515 target->conf.file, target->conf.line);
Willy Tarreauafb39922015-05-26 12:04:09 +02002516 cfgerr++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002517 } else {
2518 free(curproxy->defbe.name);
2519 curproxy->defbe.be = target;
Willy Tarreauff678132012-02-13 14:32:34 +01002520 /* Emit a warning if this proxy also has some servers */
2521 if (curproxy->srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002522 ha_warning("In proxy '%s', the 'default_backend' rule always has precedence over the servers, which will never be used.\n",
2523 curproxy->id);
Willy Tarreauff678132012-02-13 14:32:34 +01002524 err_code |= ERR_WARN;
2525 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002526 }
2527 }
2528
Willy Tarreau55ea7572007-06-17 19:56:27 +02002529 /* find the target proxy for 'use_backend' rules */
2530 list_for_each_entry(rule, &curproxy->switching_rules, list) {
Willy Tarreau55ea7572007-06-17 19:56:27 +02002531 struct proxy *target;
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01002532 struct logformat_node *node;
2533 char *pxname;
2534
2535 /* Try to parse the string as a log format expression. If the result
2536 * of the parsing is only one entry containing a simple string, then
2537 * it's a standard string corresponding to a static rule, thus the
2538 * parsing is cancelled and be.name is restored to be resolved.
2539 */
2540 pxname = rule->be.name;
2541 LIST_INIT(&rule->be.expr);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01002542 curproxy->conf.args.ctx = ARGC_UBK;
2543 curproxy->conf.args.file = rule->file;
2544 curproxy->conf.args.line = rule->line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002545 err = NULL;
2546 if (!parse_logformat_string(pxname, curproxy, &rule->be.expr, 0, SMP_VAL_FE_HRQ_HDR, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002547 ha_alert("Parsing [%s:%d]: failed to parse use_backend rule '%s' : %s.\n",
2548 rule->file, rule->line, pxname, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002549 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01002550 cfgerr++;
2551 continue;
2552 }
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01002553 node = LIST_NEXT(&rule->be.expr, struct logformat_node *, list);
2554
2555 if (!LIST_ISEMPTY(&rule->be.expr)) {
2556 if (node->type != LOG_FMT_TEXT || node->list.n != &rule->be.expr) {
2557 rule->dynamic = 1;
2558 free(pxname);
2559 continue;
2560 }
Christopher Fauletf82ea4a2020-05-07 15:59:33 +02002561 /* Only one element in the list, a simple string: free the expression and
2562 * fall back to static rule
2563 */
Willy Tarreau2b718102021-04-21 07:32:39 +02002564 LIST_DELETE(&node->list);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01002565 free(node->arg);
2566 free(node);
2567 }
2568
2569 rule->dynamic = 0;
2570 rule->be.name = pxname;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002571
Willy Tarreauafb39922015-05-26 12:04:09 +02002572 target = proxy_be_by_name(rule->be.name);
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +01002573 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002574 ha_alert("Proxy '%s': unable to find required use_backend: '%s'.\n",
2575 curproxy->id, rule->be.name);
Willy Tarreau55ea7572007-06-17 19:56:27 +02002576 cfgerr++;
2577 } else if (target == curproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002578 ha_alert("Proxy '%s': loop detected for use_backend: '%s'.\n",
2579 curproxy->id, rule->be.name);
Willy Tarreau55ea7572007-06-17 19:56:27 +02002580 cfgerr++;
Willy Tarreauafb39922015-05-26 12:04:09 +02002581 } else if (target->mode != curproxy->mode &&
2582 !(curproxy->mode == PR_MODE_TCP && target->mode == PR_MODE_HTTP)) {
2583
Christopher Faulet767a84b2017-11-24 16:50:31 +01002584 ha_alert("%s %s '%s' (%s:%d) tries to use incompatible %s %s '%s' (%s:%d) in a 'use_backend' rule (see 'mode').\n",
2585 proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
2586 curproxy->conf.file, curproxy->conf.line,
2587 proxy_mode_str(target->mode), proxy_type_str(target), target->id,
2588 target->conf.file, target->conf.line);
Willy Tarreauafb39922015-05-26 12:04:09 +02002589 cfgerr++;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002590 } else {
Willy Tarreau35cd7342021-02-26 20:51:47 +01002591 ha_free(&rule->be.name);
Willy Tarreau55ea7572007-06-17 19:56:27 +02002592 rule->be.backend = target;
2593 }
Christopher Faulet581db2b2021-03-26 10:02:46 +01002594 err_code |= warnif_tcp_http_cond(curproxy, rule->cond);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002595 }
2596
Willy Tarreau64ab6072014-09-16 12:17:36 +02002597 /* find the target server for 'use_server' rules */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002598 list_for_each_entry(srule, &curproxy->server_rules, list) {
Jerome Magnin824186b2020-03-29 09:37:12 +02002599 struct server *target;
2600 struct logformat_node *node;
2601 char *server_name;
2602
2603 /* We try to parse the string as a log format expression. If the result of the parsing
2604 * is only one entry containing a single string, then it's a standard string corresponding
2605 * to a static rule, thus the parsing is cancelled and we fall back to setting srv.ptr.
2606 */
2607 server_name = srule->srv.name;
2608 LIST_INIT(&srule->expr);
2609 curproxy->conf.args.ctx = ARGC_USRV;
2610 err = NULL;
2611 if (!parse_logformat_string(server_name, curproxy, &srule->expr, 0, SMP_VAL_FE_HRQ_HDR, &err)) {
2612 ha_alert("Parsing [%s:%d]; use-server rule failed to parse log-format '%s' : %s.\n",
2613 srule->file, srule->line, server_name, err);
2614 free(err);
2615 cfgerr++;
2616 continue;
2617 }
2618 node = LIST_NEXT(&srule->expr, struct logformat_node *, list);
2619
2620 if (!LIST_ISEMPTY(&srule->expr)) {
2621 if (node->type != LOG_FMT_TEXT || node->list.n != &srule->expr) {
2622 srule->dynamic = 1;
2623 free(server_name);
2624 continue;
2625 }
Christopher Fauletf82ea4a2020-05-07 15:59:33 +02002626 /* Only one element in the list, a simple string: free the expression and
2627 * fall back to static rule
2628 */
Willy Tarreau2b718102021-04-21 07:32:39 +02002629 LIST_DELETE(&node->list);
Jerome Magnin824186b2020-03-29 09:37:12 +02002630 free(node->arg);
2631 free(node);
2632 }
2633
2634 srule->dynamic = 0;
2635 srule->srv.name = server_name;
2636 target = findserver(curproxy, srule->srv.name);
Christopher Faulet581db2b2021-03-26 10:02:46 +01002637 err_code |= warnif_tcp_http_cond(curproxy, srule->cond);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002638
2639 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002640 ha_alert("config : %s '%s' : unable to find server '%s' referenced in a 'use-server' rule.\n",
2641 proxy_type_str(curproxy), curproxy->id, srule->srv.name);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002642 cfgerr++;
2643 continue;
2644 }
Willy Tarreau35cd7342021-02-26 20:51:47 +01002645 ha_free(&srule->srv.name);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002646 srule->srv.ptr = target;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002647 }
2648
Emeric Brunb982a3d2010-01-04 15:45:53 +01002649 /* find the target table for 'stick' rules */
2650 list_for_each_entry(mrule, &curproxy->sticking_rules, list) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002651 struct stktable *target;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002652
Emeric Brun1d33b292010-01-04 15:47:17 +01002653 curproxy->be_req_ana |= AN_REQ_STICKING_RULES;
2654 if (mrule->flags & STK_IS_STORE)
2655 curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
2656
Emeric Brunb982a3d2010-01-04 15:45:53 +01002657 if (mrule->table.name)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002658 target = stktable_find_by_name(mrule->table.name);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002659 else
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002660 target = curproxy->table;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002661
2662 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002663 ha_alert("Proxy '%s': unable to find stick-table '%s'.\n",
Willy Tarreau508d2322020-01-24 07:19:34 +01002664 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002665 cfgerr++;
2666 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002667 else if (!stktable_compatible_sample(mrule->expr, target->type)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002668 ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
2669 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002670 cfgerr++;
2671 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002672 else if (target->proxy && curproxy->bind_proc & ~target->proxy->bind_proc) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01002673 ha_alert("Proxy '%s': stick-table '%s' referenced 'stick-store' rule not present on all processes covered by proxy '%s'.\n",
2674 curproxy->id, target->id, curproxy->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01002675 cfgerr++;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01002676 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01002677 else {
Willy Tarreau35cd7342021-02-26 20:51:47 +01002678 ha_free(&mrule->table.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002679 mrule->table.t = target;
2680 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL);
Thayne McCombs92149f92020-11-20 01:28:26 -07002681 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_KEY, NULL);
Frédéric Lécaille9c3a0ce2019-09-02 14:02:28 +02002682 if (!in_proxies_list(target->proxies_list, curproxy)) {
2683 curproxy->next_stkt_ref = target->proxies_list;
2684 target->proxies_list = curproxy;
2685 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01002686 }
Christopher Faulet581db2b2021-03-26 10:02:46 +01002687 err_code |= warnif_tcp_http_cond(curproxy, mrule->cond);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002688 }
2689
2690 /* find the target table for 'store response' rules */
2691 list_for_each_entry(mrule, &curproxy->storersp_rules, list) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002692 struct stktable *target;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002693
Emeric Brun1d33b292010-01-04 15:47:17 +01002694 curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
2695
Emeric Brunb982a3d2010-01-04 15:45:53 +01002696 if (mrule->table.name)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002697 target = stktable_find_by_name(mrule->table.name);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002698 else
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002699 target = curproxy->table;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002700
2701 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002702 ha_alert("Proxy '%s': unable to find store table '%s'.\n",
Willy Tarreau508d2322020-01-24 07:19:34 +01002703 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002704 cfgerr++;
2705 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002706 else if (!stktable_compatible_sample(mrule->expr, target->type)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002707 ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
2708 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002709 cfgerr++;
2710 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002711 else if (target->proxy && (curproxy->bind_proc & ~target->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01002712 ha_alert("Proxy '%s': stick-table '%s' referenced 'stick-store' rule not present on all processes covered by proxy '%s'.\n",
2713 curproxy->id, target->id, curproxy->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01002714 cfgerr++;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01002715 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01002716 else {
Willy Tarreau35cd7342021-02-26 20:51:47 +01002717 ha_free(&mrule->table.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002718 mrule->table.t = target;
2719 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL);
Thayne McCombs92149f92020-11-20 01:28:26 -07002720 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_KEY, NULL);
Frédéric Lécaille9c3a0ce2019-09-02 14:02:28 +02002721 if (!in_proxies_list(target->proxies_list, curproxy)) {
2722 curproxy->next_stkt_ref = target->proxies_list;
2723 target->proxies_list = curproxy;
2724 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01002725 }
2726 }
2727
Christopher Faulet42c6cf92021-03-25 17:19:04 +01002728 /* check validity for 'tcp-request' layer 4/5/6/7 rules */
2729 cfgerr += check_action_rules(&curproxy->tcp_req.l4_rules, curproxy, &err_code);
2730 cfgerr += check_action_rules(&curproxy->tcp_req.l5_rules, curproxy, &err_code);
2731 cfgerr += check_action_rules(&curproxy->tcp_req.inspect_rules, curproxy, &err_code);
2732 cfgerr += check_action_rules(&curproxy->tcp_rep.inspect_rules, curproxy, &err_code);
2733 cfgerr += check_action_rules(&curproxy->http_req_rules, curproxy, &err_code);
2734 cfgerr += check_action_rules(&curproxy->http_res_rules, curproxy, &err_code);
2735 cfgerr += check_action_rules(&curproxy->http_after_res_rules, curproxy, &err_code);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002736
Christopher Faulet5eef0182021-03-31 17:13:49 +02002737 /* Warn is a switch-mode http is used on a TCP listener with servers but no backend */
2738 if (!curproxy->defbe.name && LIST_ISEMPTY(&curproxy->switching_rules) && curproxy->srv) {
2739 if ((curproxy->options & PR_O_HTTP_UPG) && curproxy->mode == PR_MODE_TCP)
2740 ha_warning("Proxy '%s' : 'switch-mode http' configured for a %s %s with no backend. "
2741 "Incoming connections upgraded to HTTP cannot be routed to TCP servers\n",
2742 curproxy->id, proxy_mode_str(curproxy->mode), proxy_type_str(curproxy));
2743 }
2744
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002745 if (curproxy->table && curproxy->table->peers.name) {
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002746 struct peers *curpeers;
Emeric Brun32da3c42010-09-23 18:39:19 +02002747
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002748 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002749 if (strcmp(curpeers->id, curproxy->table->peers.name) == 0) {
Willy Tarreau35cd7342021-02-26 20:51:47 +01002750 ha_free(&curproxy->table->peers.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002751 curproxy->table->peers.p = curpeers;
Emeric Brun32da3c42010-09-23 18:39:19 +02002752 break;
2753 }
2754 }
2755
2756 if (!curpeers) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002757 ha_alert("Proxy '%s': unable to find sync peers '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002758 curproxy->id, curproxy->table->peers.name);
Willy Tarreau35cd7342021-02-26 20:51:47 +01002759 ha_free(&curproxy->table->peers.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002760 curproxy->table->peers.p = NULL;
Emeric Brun32da3c42010-09-23 18:39:19 +02002761 cfgerr++;
2762 }
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02002763 else if (curpeers->disabled) {
Willy Tarreau77e4bd12015-05-01 20:02:17 +02002764 /* silently disable this peers section */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002765 curproxy->table->peers.p = NULL;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02002766 }
Emeric Brun32da3c42010-09-23 18:39:19 +02002767 else if (!curpeers->peers_fe) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002768 ha_alert("Proxy '%s': unable to find local peer '%s' in peers section '%s'.\n",
2769 curproxy->id, localpeer, curpeers->id);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002770 curproxy->table->peers.p = NULL;
Emeric Brun32da3c42010-09-23 18:39:19 +02002771 cfgerr++;
2772 }
2773 }
2774
Simon Horman9dc49962015-01-30 11:22:59 +09002775
2776 if (curproxy->email_alert.mailers.name) {
2777 struct mailers *curmailers = mailers;
2778
2779 for (curmailers = mailers; curmailers; curmailers = curmailers->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002780 if (strcmp(curmailers->id, curproxy->email_alert.mailers.name) == 0)
Simon Horman9dc49962015-01-30 11:22:59 +09002781 break;
Simon Horman9dc49962015-01-30 11:22:59 +09002782 }
Simon Horman9dc49962015-01-30 11:22:59 +09002783 if (!curmailers) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002784 ha_alert("Proxy '%s': unable to find mailers '%s'.\n",
2785 curproxy->id, curproxy->email_alert.mailers.name);
Simon Horman9dc49962015-01-30 11:22:59 +09002786 free_email_alert(curproxy);
2787 cfgerr++;
2788 }
Christopher Faulet0108bb32017-10-20 21:34:32 +02002789 else {
2790 err = NULL;
2791 if (init_email_alert(curmailers, curproxy, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002792 ha_alert("Proxy '%s': %s.\n", curproxy->id, err);
Christopher Faulet0108bb32017-10-20 21:34:32 +02002793 free(err);
2794 cfgerr++;
2795 }
2796 }
Simon Horman9dc49962015-01-30 11:22:59 +09002797 }
2798
Willy Tarreau5bbc6762021-02-12 11:49:25 +01002799 if (curproxy->uri_auth && !(curproxy->uri_auth->flags & STAT_CONVDONE) &&
Willy Tarreauff011f22011-01-06 17:51:27 +01002800 !LIST_ISEMPTY(&curproxy->uri_auth->http_req_rules) &&
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002801 (curproxy->uri_auth->userlist || curproxy->uri_auth->auth_realm )) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002802 ha_alert("%s '%s': stats 'auth'/'realm' and 'http-request' can't be used at the same time.\n",
2803 "proxy", curproxy->id);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002804 cfgerr++;
2805 goto out_uri_auth_compat;
2806 }
2807
Willy Tarreauee4f5f82019-10-09 09:59:22 +02002808 if (curproxy->uri_auth && curproxy->uri_auth->userlist &&
Willy Tarreau5bbc6762021-02-12 11:49:25 +01002809 (!(curproxy->uri_auth->flags & STAT_CONVDONE) ||
Willy Tarreauee4f5f82019-10-09 09:59:22 +02002810 LIST_ISEMPTY(&curproxy->uri_auth->http_req_rules))) {
Willy Tarreau95fa4692010-02-01 13:05:50 +01002811 const char *uri_auth_compat_req[10];
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002812 struct act_rule *rule;
Amaury Denoyelle3efee652021-03-08 17:31:39 +01002813 i = 0;
2814
Willy Tarreau95fa4692010-02-01 13:05:50 +01002815 /* build the ACL condition from scratch. We're relying on anonymous ACLs for that */
2816 uri_auth_compat_req[i++] = "auth";
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002817
2818 if (curproxy->uri_auth->auth_realm) {
Willy Tarreau95fa4692010-02-01 13:05:50 +01002819 uri_auth_compat_req[i++] = "realm";
2820 uri_auth_compat_req[i++] = curproxy->uri_auth->auth_realm;
2821 }
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002822
Willy Tarreau95fa4692010-02-01 13:05:50 +01002823 uri_auth_compat_req[i++] = "unless";
2824 uri_auth_compat_req[i++] = "{";
2825 uri_auth_compat_req[i++] = "http_auth(.internal-stats-userlist)";
2826 uri_auth_compat_req[i++] = "}";
2827 uri_auth_compat_req[i++] = "";
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002828
Willy Tarreauff011f22011-01-06 17:51:27 +01002829 rule = parse_http_req_cond(uri_auth_compat_req, "internal-stats-auth-compat", 0, curproxy);
2830 if (!rule) {
Willy Tarreau95fa4692010-02-01 13:05:50 +01002831 cfgerr++;
2832 break;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002833 }
2834
Willy Tarreau2b718102021-04-21 07:32:39 +02002835 LIST_APPEND(&curproxy->uri_auth->http_req_rules, &rule->list);
Willy Tarreau95fa4692010-02-01 13:05:50 +01002836
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002837 if (curproxy->uri_auth->auth_realm) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002838 ha_free(&curproxy->uri_auth->auth_realm);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002839 }
Willy Tarreau5bbc6762021-02-12 11:49:25 +01002840 curproxy->uri_auth->flags |= STAT_CONVDONE;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002841 }
2842out_uri_auth_compat:
2843
Dragan Dosen43885c72015-10-01 13:18:13 +02002844 /* check whether we have a log server that uses RFC5424 log format */
Dragan Dosen1322d092015-09-22 16:05:32 +02002845 list_for_each_entry(tmplogsrv, &curproxy->logsrvs, list) {
Dragan Dosen43885c72015-10-01 13:18:13 +02002846 if (tmplogsrv->format == LOG_FORMAT_RFC5424) {
2847 if (!curproxy->conf.logformat_sd_string) {
2848 /* set the default logformat_sd_string */
2849 curproxy->conf.logformat_sd_string = default_rfc5424_sd_log_format;
2850 }
Dragan Dosen1322d092015-09-22 16:05:32 +02002851 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02002852 }
Dragan Dosen1322d092015-09-22 16:05:32 +02002853 }
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02002854
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002855 /* compile the log format */
2856 if (!(curproxy->cap & PR_CAP_FE)) {
Willy Tarreau62a61232013-04-12 18:13:46 +02002857 if (curproxy->conf.logformat_string != default_http_log_format &&
2858 curproxy->conf.logformat_string != default_tcp_log_format &&
2859 curproxy->conf.logformat_string != clf_http_log_format)
2860 free(curproxy->conf.logformat_string);
2861 curproxy->conf.logformat_string = NULL;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002862 ha_free(&curproxy->conf.lfs_file);
Willy Tarreau62a61232013-04-12 18:13:46 +02002863 curproxy->conf.lfs_line = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002864
2865 if (curproxy->conf.logformat_sd_string != default_rfc5424_sd_log_format)
2866 free(curproxy->conf.logformat_sd_string);
2867 curproxy->conf.logformat_sd_string = NULL;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002868 ha_free(&curproxy->conf.lfsd_file);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002869 curproxy->conf.lfsd_line = 0;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002870 }
2871
Willy Tarreau62a61232013-04-12 18:13:46 +02002872 if (curproxy->conf.logformat_string) {
2873 curproxy->conf.args.ctx = ARGC_LOG;
2874 curproxy->conf.args.file = curproxy->conf.lfs_file;
2875 curproxy->conf.args.line = curproxy->conf.lfs_line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002876 err = NULL;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +02002877 if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat,
2878 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002879 SMP_VAL_FE_LOG_END, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002880 ha_alert("Parsing [%s:%d]: failed to parse log-format : %s.\n",
2881 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002882 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01002883 cfgerr++;
2884 }
Willy Tarreau62a61232013-04-12 18:13:46 +02002885 curproxy->conf.args.file = NULL;
2886 curproxy->conf.args.line = 0;
2887 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002888
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002889 if (curproxy->conf.logformat_sd_string) {
2890 curproxy->conf.args.ctx = ARGC_LOGSD;
2891 curproxy->conf.args.file = curproxy->conf.lfsd_file;
2892 curproxy->conf.args.line = curproxy->conf.lfsd_line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002893 err = NULL;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +02002894 if (!parse_logformat_string(curproxy->conf.logformat_sd_string, curproxy, &curproxy->logformat_sd,
2895 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002896 SMP_VAL_FE_LOG_END, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002897 ha_alert("Parsing [%s:%d]: failed to parse log-format-sd : %s.\n",
2898 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002899 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01002900 cfgerr++;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002901 } else if (!add_to_logformat_list(NULL, NULL, LF_SEPARATOR, &curproxy->logformat_sd, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002902 ha_alert("Parsing [%s:%d]: failed to parse log-format-sd : %s.\n",
2903 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002904 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01002905 cfgerr++;
2906 }
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002907 curproxy->conf.args.file = NULL;
2908 curproxy->conf.args.line = 0;
2909 }
2910
Willy Tarreau62a61232013-04-12 18:13:46 +02002911 if (curproxy->conf.uniqueid_format_string) {
2912 curproxy->conf.args.ctx = ARGC_UIF;
2913 curproxy->conf.args.file = curproxy->conf.uif_file;
2914 curproxy->conf.args.line = curproxy->conf.uif_line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002915 err = NULL;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +02002916 if (!parse_logformat_string(curproxy->conf.uniqueid_format_string, curproxy, &curproxy->format_unique_id,
2917 LOG_OPT_HTTP|LOG_OPT_MERGE_SPACES,
2918 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR
2919 : SMP_VAL_BE_HRQ_HDR,
2920 &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002921 ha_alert("Parsing [%s:%d]: failed to parse unique-id : %s.\n",
2922 curproxy->conf.uif_file, curproxy->conf.uif_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002923 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01002924 cfgerr++;
2925 }
Willy Tarreau62a61232013-04-12 18:13:46 +02002926 curproxy->conf.args.file = NULL;
2927 curproxy->conf.args.line = 0;
2928 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002929
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01002930 /* only now we can check if some args remain unresolved.
2931 * This must be done after the users and groups resolution.
2932 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01002933 err = NULL;
2934 i = smp_resolve_args(curproxy, &err);
2935 cfgerr += i;
2936 if (i) {
2937 indent_msg(&err, 8);
2938 ha_alert("%s%s\n", i > 1 ? "multiple argument resolution errors:" : "", err);
2939 ha_free(&err);
2940 } else
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002941 cfgerr += acl_find_targets(curproxy);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002942
Willy Tarreau2738a142006-07-08 17:28:09 +02002943 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002944 (((curproxy->cap & PR_CAP_FE) && !curproxy->timeout.client) ||
Willy Tarreaud825eef2007-05-12 22:35:00 +02002945 ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) &&
Willy Tarreauce887fd2012-05-12 12:50:00 +02002946 (!curproxy->timeout.connect ||
2947 (!curproxy->timeout.server && (curproxy->mode == PR_MODE_HTTP || !curproxy->timeout.tunnel)))))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002948 ha_warning("config : missing timeouts for %s '%s'.\n"
2949 " | While not properly invalid, you will certainly encounter various problems\n"
2950 " | with such a configuration. To fix this, please ensure that all following\n"
2951 " | timeouts are set to a non-zero value: 'client', 'connect', 'server'.\n",
2952 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubb925012009-07-23 13:36:36 +02002953 err_code |= ERR_WARN;
Willy Tarreau2738a142006-07-08 17:28:09 +02002954 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02002955
Willy Tarreau1fa31262007-12-03 00:36:16 +01002956 /* Historically, the tarpit and queue timeouts were inherited from contimeout.
2957 * We must still support older configurations, so let's find out whether those
2958 * parameters have been set or must be copied from contimeouts.
2959 */
Willy Tarreau937c3ea2021-02-12 11:14:35 +01002960 if (!curproxy->timeout.tarpit)
2961 curproxy->timeout.tarpit = curproxy->timeout.connect;
2962 if ((curproxy->cap & PR_CAP_BE) && !curproxy->timeout.queue)
2963 curproxy->timeout.queue = curproxy->timeout.connect;
Willy Tarreau1fa31262007-12-03 00:36:16 +01002964
Christopher Faulete5870d82020-04-15 11:32:03 +02002965 if ((curproxy->tcpcheck_rules.flags & TCPCHK_RULES_UNUSED_TCP_RS)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002966 ha_warning("config : %s '%s' uses tcp-check rules without 'option tcp-check', so the rules are ignored.\n",
2967 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau215663d2014-06-13 18:30:23 +02002968 err_code |= ERR_WARN;
2969 }
2970
Willy Tarreau193b8c62012-11-22 00:17:38 +01002971 /* ensure that cookie capture length is not too large */
2972 if (curproxy->capture_len >= global.tune.cookie_len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002973 ha_warning("config : truncating capture length to %d bytes for %s '%s'.\n",
2974 global.tune.cookie_len - 1, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau193b8c62012-11-22 00:17:38 +01002975 err_code |= ERR_WARN;
2976 curproxy->capture_len = global.tune.cookie_len - 1;
2977 }
2978
Willy Tarreaucf7f3202007-05-13 22:46:04 +02002979 /* The small pools required for the capture lists */
Willy Tarreau9a54e132012-03-24 08:33:05 +01002980 if (curproxy->nb_req_cap) {
Willy Tarreaud9ed3d22014-06-13 12:23:06 +02002981 curproxy->req_cap_pool = create_pool("ptrcap",
2982 curproxy->nb_req_cap * sizeof(char *),
2983 MEM_F_SHARED);
Willy Tarreau9a54e132012-03-24 08:33:05 +01002984 }
2985
2986 if (curproxy->nb_rsp_cap) {
Willy Tarreaud9ed3d22014-06-13 12:23:06 +02002987 curproxy->rsp_cap_pool = create_pool("ptrcap",
2988 curproxy->nb_rsp_cap * sizeof(char *),
2989 MEM_F_SHARED);
Willy Tarreau9a54e132012-03-24 08:33:05 +01002990 }
Willy Tarreaucf7f3202007-05-13 22:46:04 +02002991
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002992 switch (curproxy->load_server_state_from_file) {
2993 case PR_SRV_STATE_FILE_UNSPEC:
2994 curproxy->load_server_state_from_file = PR_SRV_STATE_FILE_NONE;
2995 break;
2996 case PR_SRV_STATE_FILE_GLOBAL:
2997 if (!global.server_state_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002998 ha_warning("config : backend '%s' configured to load server state file from global section 'server-state-file' directive. Unfortunately, 'server-state-file' is not set!\n",
2999 curproxy->id);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003000 err_code |= ERR_WARN;
3001 }
3002 break;
3003 }
3004
Willy Tarreaubaaee002006-06-26 02:48:02 +02003005 /* first, we will invert the servers list order */
3006 newsrv = NULL;
3007 while (curproxy->srv) {
3008 struct server *next;
3009
3010 next = curproxy->srv->next;
3011 curproxy->srv->next = newsrv;
3012 newsrv = curproxy->srv;
3013 if (!next)
3014 break;
3015 curproxy->srv = next;
3016 }
3017
Willy Tarreau17edc812014-01-03 12:14:34 +01003018 /* Check that no server name conflicts. This causes trouble in the stats.
3019 * We only emit a warning for the first conflict affecting each server,
3020 * in order to avoid combinatory explosion if all servers have the same
3021 * name. We do that only for servers which do not have an explicit ID,
3022 * because these IDs were made also for distinguishing them and we don't
3023 * want to annoy people who correctly manage them.
3024 */
3025 for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
3026 struct server *other_srv;
3027
3028 if (newsrv->puid)
3029 continue;
3030
3031 for (other_srv = curproxy->srv; other_srv && other_srv != newsrv; other_srv = other_srv->next) {
3032 if (!other_srv->puid && strcmp(other_srv->id, newsrv->id) == 0) {
Willy Tarreaub01302f2019-05-27 19:31:06 +02003033 ha_alert("parsing [%s:%d] : %s '%s', another server named '%s' was already defined at line %d, please use distinct names.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003034 newsrv->conf.file, newsrv->conf.line,
3035 proxy_type_str(curproxy), curproxy->id,
3036 newsrv->id, other_srv->conf.line);
Willy Tarreaub01302f2019-05-27 19:31:06 +02003037 cfgerr++;
Willy Tarreau17edc812014-01-03 12:14:34 +01003038 break;
3039 }
3040 }
3041 }
3042
Willy Tarreaudd701652010-05-25 23:03:02 +02003043 /* assign automatic UIDs to servers which don't have one yet */
3044 next_id = 1;
3045 newsrv = curproxy->srv;
3046 while (newsrv != NULL) {
3047 if (!newsrv->puid) {
3048 /* server ID not set, use automatic numbering with first
3049 * spare entry starting with next_svid.
3050 */
3051 next_id = get_next_id(&curproxy->conf.used_server_id, next_id);
3052 newsrv->conf.id.key = newsrv->puid = next_id;
3053 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Frédéric Lécaille84d60462019-05-17 14:29:15 +02003054 newsrv->conf.name.key = newsrv->id;
3055 ebis_insert(&curproxy->conf.used_server_name, &newsrv->conf.name);
Willy Tarreaudd701652010-05-25 23:03:02 +02003056 }
3057 next_id++;
3058 newsrv = newsrv->next;
3059 }
3060
Willy Tarreau20697042007-11-15 23:26:18 +01003061 curproxy->lbprm.wmult = 1; /* default weight multiplier */
Willy Tarreau5dc2fa62007-11-19 19:10:18 +01003062 curproxy->lbprm.wdiv = 1; /* default weight divider */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003063
Willy Tarreau62c3be22012-01-20 13:12:32 +01003064 /*
3065 * If this server supports a maxconn parameter, it needs a dedicated
3066 * tasks to fill the emptied slots when a connection leaves.
3067 * Also, resolve deferred tracking dependency if needed.
3068 */
3069 newsrv = curproxy->srv;
3070 while (newsrv != NULL) {
3071 if (newsrv->minconn > newsrv->maxconn) {
3072 /* Only 'minconn' was specified, or it was higher than or equal
3073 * to 'maxconn'. Let's turn this into maxconn and clean it, as
3074 * this will avoid further useless expensive computations.
3075 */
3076 newsrv->maxconn = newsrv->minconn;
3077 } else if (newsrv->maxconn && !newsrv->minconn) {
3078 /* minconn was not specified, so we set it to maxconn */
3079 newsrv->minconn = newsrv->maxconn;
3080 }
3081
William Dauchyf6370442020-11-14 19:25:33 +01003082 /* this will also properly set the transport layer for
3083 * prod and checks
3084 * if default-server have use_ssl, prerare ssl init
3085 * without activating it */
3086 if (newsrv->use_ssl == 1 || newsrv->check.use_ssl == 1 ||
3087 (newsrv->proxy->options & PR_O_TCPCHK_SSL) ||
3088 (newsrv->use_ssl != 1 && curproxy->defsrv.use_ssl == 1)) {
Willy Tarreau17d45382016-12-22 21:16:08 +01003089 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv)
3090 cfgerr += xprt_get(XPRT_SSL)->prepare_srv(newsrv);
3091 }
Emeric Brun94324a42012-10-11 14:00:19 +02003092
Willy Tarreau034c88c2017-01-23 23:36:45 +01003093 if ((newsrv->flags & SRV_F_FASTOPEN) &&
3094 ((curproxy->retry_type & (PR_RE_DISCONNECTED | PR_RE_TIMEOUT)) !=
3095 (PR_RE_DISCONNECTED | PR_RE_TIMEOUT)))
3096 ha_warning("parsing [%s:%d] : %s '%s': server '%s' has tfo activated, the backend should be configured with at least 'conn-failure', 'empty-response' and 'response-timeout' or we wouldn't be able to retry the connection on failure.\n",
3097 newsrv->conf.file, newsrv->conf.line,
3098 proxy_type_str(curproxy), curproxy->id,
3099 newsrv->id);
3100
Willy Tarreau62c3be22012-01-20 13:12:32 +01003101 if (newsrv->trackit) {
3102 struct proxy *px;
Willy Tarreau32091232014-05-16 13:52:00 +02003103 struct server *srv, *loop;
Willy Tarreau62c3be22012-01-20 13:12:32 +01003104 char *pname, *sname;
3105
3106 pname = newsrv->trackit;
3107 sname = strrchr(pname, '/');
3108
3109 if (sname)
3110 *sname++ = '\0';
3111 else {
3112 sname = pname;
3113 pname = NULL;
3114 }
3115
3116 if (pname) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02003117 px = proxy_be_by_name(pname);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003118 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003119 ha_alert("config : %s '%s', server '%s': unable to find required proxy '%s' for tracking.\n",
3120 proxy_type_str(curproxy), curproxy->id,
3121 newsrv->id, pname);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003122 cfgerr++;
3123 goto next_srv;
3124 }
3125 } else
3126 px = curproxy;
3127
3128 srv = findserver(px, sname);
3129 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003130 ha_alert("config : %s '%s', server '%s': unable to find required server '%s' for tracking.\n",
3131 proxy_type_str(curproxy), curproxy->id,
3132 newsrv->id, sname);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003133 cfgerr++;
3134 goto next_srv;
3135 }
3136
Christopher Faulet8892e5d2020-03-26 19:48:20 +01003137 if (!srv->do_check && !srv->do_agent && !srv->track && !srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003138 ha_alert("config : %s '%s', server '%s': unable to use %s/%s for "
3139 "tracking as it does not have any check nor agent enabled.\n",
3140 proxy_type_str(curproxy), curproxy->id,
3141 newsrv->id, px->id, srv->id);
Willy Tarreau32091232014-05-16 13:52:00 +02003142 cfgerr++;
3143 goto next_srv;
3144 }
3145
3146 for (loop = srv->track; loop && loop != newsrv; loop = loop->track);
3147
Frédéric Lécaille2efc6492017-03-14 14:32:17 +01003148 if (newsrv == srv || loop) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003149 ha_alert("config : %s '%s', server '%s': unable to track %s/%s as it "
3150 "belongs to a tracking chain looping back to %s/%s.\n",
3151 proxy_type_str(curproxy), curproxy->id,
3152 newsrv->id, px->id, srv->id, px->id,
3153 newsrv == srv ? srv->id : loop->id);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003154 cfgerr++;
3155 goto next_srv;
3156 }
3157
3158 if (curproxy != px &&
3159 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003160 ha_alert("config : %s '%s', server '%s': unable to use %s/%s for"
3161 "tracking: disable-on-404 option inconsistency.\n",
3162 proxy_type_str(curproxy), curproxy->id,
3163 newsrv->id, px->id, srv->id);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003164 cfgerr++;
3165 goto next_srv;
3166 }
3167
Willy Tarreau62c3be22012-01-20 13:12:32 +01003168 newsrv->track = srv;
Willy Tarreau1a53a3a2013-12-11 15:27:05 +01003169 newsrv->tracknext = srv->trackers;
3170 srv->trackers = newsrv;
Willy Tarreau62c3be22012-01-20 13:12:32 +01003171
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003172 ha_free(&newsrv->trackit);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003173 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003174
Willy Tarreau62c3be22012-01-20 13:12:32 +01003175 next_srv:
3176 newsrv = newsrv->next;
3177 }
3178
Olivier Houchard4e694042017-03-14 20:01:29 +01003179 /*
3180 * Try to generate dynamic cookies for servers now.
3181 * It couldn't be done earlier, since at the time we parsed
3182 * the server line, we may not have known yet that we
3183 * should use dynamic cookies, or the secret key may not
3184 * have been provided yet.
3185 */
3186 if (curproxy->ck_opts & PR_CK_DYNAMIC) {
3187 newsrv = curproxy->srv;
3188 while (newsrv != NULL) {
3189 srv_set_dyncookie(newsrv);
3190 newsrv = newsrv->next;
3191 }
3192
3193 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003194 /* We have to initialize the server lookup mechanism depending
Joseph Herlanta14c03e2018-11-15 14:04:19 -08003195 * on what LB algorithm was chosen.
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003196 */
3197
3198 curproxy->lbprm.algo &= ~(BE_LB_LKUP | BE_LB_PROP_DYN);
3199 switch (curproxy->lbprm.algo & BE_LB_KIND) {
3200 case BE_LB_KIND_RR:
Willy Tarreau9757a382009-10-03 12:56:50 +02003201 if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_RR_STATIC) {
3202 curproxy->lbprm.algo |= BE_LB_LKUP_MAP;
3203 init_server_map(curproxy);
Willy Tarreau760e81d2018-05-03 07:20:40 +02003204 } else if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_RR_RANDOM) {
3205 curproxy->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
3206 chash_init_server_tree(curproxy);
Willy Tarreau9757a382009-10-03 12:56:50 +02003207 } else {
3208 curproxy->lbprm.algo |= BE_LB_LKUP_RRTREE | BE_LB_PROP_DYN;
3209 fwrr_init_server_groups(curproxy);
3210 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003211 break;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003212
Willy Tarreau3ebb1162012-02-13 16:57:44 +01003213 case BE_LB_KIND_CB:
Willy Tarreauf09c6602012-02-13 17:12:08 +01003214 if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_CB_LC) {
3215 curproxy->lbprm.algo |= BE_LB_LKUP_LCTREE | BE_LB_PROP_DYN;
3216 fwlc_init_server_tree(curproxy);
3217 } else {
3218 curproxy->lbprm.algo |= BE_LB_LKUP_FSTREE | BE_LB_PROP_DYN;
3219 fas_init_server_tree(curproxy);
3220 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003221 break;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003222
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003223 case BE_LB_KIND_HI:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003224 if ((curproxy->lbprm.algo & BE_LB_HASH_TYPE) == BE_LB_HASH_CONS) {
3225 curproxy->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
3226 chash_init_server_tree(curproxy);
3227 } else {
3228 curproxy->lbprm.algo |= BE_LB_LKUP_MAP;
3229 init_server_map(curproxy);
3230 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003231 break;
3232 }
Willy Tarreaucd10def2020-10-17 18:48:47 +02003233 HA_RWLOCK_INIT(&curproxy->lbprm.lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003234
3235 if (curproxy->options & PR_O_LOGASAP)
3236 curproxy->to_log &= ~LW_BYTES;
3237
Willy Tarreaue7ded1f2009-08-09 10:11:45 +02003238 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003239 (curproxy->cap & PR_CAP_FE) && LIST_ISEMPTY(&curproxy->logsrvs) &&
3240 (!LIST_ISEMPTY(&curproxy->logformat) || !LIST_ISEMPTY(&curproxy->logformat_sd))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003241 ha_warning("config : log format ignored for %s '%s' since it has no log address.\n",
3242 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue7ded1f2009-08-09 10:11:45 +02003243 err_code |= ERR_WARN;
3244 }
3245
Christopher Faulet3b6446f2021-03-15 15:10:38 +01003246 if (curproxy->mode != PR_MODE_HTTP && !(curproxy->options & PR_O_HTTP_UPG)) {
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003247 int optnum;
3248
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003249 if (curproxy->uri_auth) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003250 ha_warning("config : 'stats' statement ignored for %s '%s' as it requires HTTP mode.\n",
3251 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003252 err_code |= ERR_WARN;
3253 curproxy->uri_auth = NULL;
3254 }
3255
Willy Tarreaude7dc882017-03-10 11:49:21 +01003256 if (curproxy->capture_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003257 ha_warning("config : 'capture' statement ignored for %s '%s' as it requires HTTP mode.\n",
3258 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003259 err_code |= ERR_WARN;
3260 }
3261
3262 if (!LIST_ISEMPTY(&curproxy->http_req_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003263 ha_warning("config : 'http-request' rules ignored for %s '%s' as they require HTTP mode.\n",
3264 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003265 err_code |= ERR_WARN;
3266 }
3267
3268 if (!LIST_ISEMPTY(&curproxy->http_res_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003269 ha_warning("config : 'http-response' rules ignored for %s '%s' as they require HTTP mode.\n",
3270 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003271 err_code |= ERR_WARN;
3272 }
3273
Christopher Fauletbb7abed2021-03-15 15:09:21 +01003274 if (!LIST_ISEMPTY(&curproxy->http_after_res_rules)) {
3275 ha_warning("config : 'http-after-response' rules ignored for %s '%s' as they require HTTP mode.\n",
3276 proxy_type_str(curproxy), curproxy->id);
3277 err_code |= ERR_WARN;
3278 }
3279
Willy Tarreaude7dc882017-03-10 11:49:21 +01003280 if (!LIST_ISEMPTY(&curproxy->redirect_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003281 ha_warning("config : 'redirect' rules ignored for %s '%s' as they require HTTP mode.\n",
3282 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003283 err_code |= ERR_WARN;
3284 }
3285
Willy Tarreau87cf5142011-08-19 22:57:24 +02003286 if (curproxy->options & (PR_O_FWDFOR | PR_O_FF_ALWAYS)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003287 ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3288 "forwardfor", proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003289 err_code |= ERR_WARN;
Willy Tarreau87cf5142011-08-19 22:57:24 +02003290 curproxy->options &= ~(PR_O_FWDFOR | PR_O_FF_ALWAYS);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003291 }
3292
3293 if (curproxy->options & PR_O_ORGTO) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003294 ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3295 "originalto", proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003296 err_code |= ERR_WARN;
3297 curproxy->options &= ~PR_O_ORGTO;
3298 }
3299
3300 for (optnum = 0; cfg_opts[optnum].name; optnum++) {
3301 if (cfg_opts[optnum].mode == PR_MODE_HTTP &&
3302 (curproxy->cap & cfg_opts[optnum].cap) &&
3303 (curproxy->options & cfg_opts[optnum].val)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003304 ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3305 cfg_opts[optnum].name, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003306 err_code |= ERR_WARN;
3307 curproxy->options &= ~cfg_opts[optnum].val;
3308 }
3309 }
3310
3311 for (optnum = 0; cfg_opts2[optnum].name; optnum++) {
3312 if (cfg_opts2[optnum].mode == PR_MODE_HTTP &&
3313 (curproxy->cap & cfg_opts2[optnum].cap) &&
3314 (curproxy->options2 & cfg_opts2[optnum].val)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003315 ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3316 cfg_opts2[optnum].name, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003317 err_code |= ERR_WARN;
3318 curproxy->options2 &= ~cfg_opts2[optnum].val;
3319 }
3320 }
Willy Tarreaubce70882009-09-07 11:51:47 +02003321
Willy Tarreau29fbe512015-08-20 19:35:14 +02003322#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreauef9a3602012-12-08 22:29:20 +01003323 if (curproxy->conn_src.bind_hdr_occ) {
3324 curproxy->conn_src.bind_hdr_occ = 0;
Christopher Faulet767a84b2017-11-24 16:50:31 +01003325 ha_warning("config : %s '%s' : ignoring use of header %s as source IP in non-HTTP mode.\n",
3326 proxy_type_str(curproxy), curproxy->id, curproxy->conn_src.bind_hdr_name);
Willy Tarreaubce70882009-09-07 11:51:47 +02003327 err_code |= ERR_WARN;
3328 }
Willy Tarreauefa5f512010-03-30 20:13:29 +02003329#endif
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003330 }
3331
Willy Tarreaubaaee002006-06-26 02:48:02 +02003332 /*
Willy Tarreau21d2af32008-02-14 20:25:24 +01003333 * ensure that we're not cross-dressing a TCP server into HTTP.
3334 */
3335 newsrv = curproxy->srv;
3336 while (newsrv != NULL) {
Willy Tarreau0cec3312011-10-31 13:49:26 +01003337 if ((curproxy->mode != PR_MODE_HTTP) && newsrv->rdr_len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003338 ha_alert("config : %s '%s' : server cannot have cookie or redirect prefix in non-HTTP mode.\n",
3339 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubb925012009-07-23 13:36:36 +02003340 cfgerr++;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003341 }
Willy Tarreaubce70882009-09-07 11:51:47 +02003342
Willy Tarreau0cec3312011-10-31 13:49:26 +01003343 if ((curproxy->mode != PR_MODE_HTTP) && newsrv->cklen) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003344 ha_warning("config : %s '%s' : ignoring cookie for server '%s' as HTTP mode is disabled.\n",
3345 proxy_type_str(curproxy), curproxy->id, newsrv->id);
Willy Tarreau0cec3312011-10-31 13:49:26 +01003346 err_code |= ERR_WARN;
3347 }
3348
Willy Tarreauc93cd162014-05-13 15:54:22 +02003349 if ((newsrv->flags & SRV_F_MAPPORTS) && (curproxy->options2 & PR_O2_RDPC_PRST)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003350 ha_warning("config : %s '%s' : RDP cookie persistence will not work for server '%s' because it lacks an explicit port number.\n",
3351 proxy_type_str(curproxy), curproxy->id, newsrv->id);
Willy Tarreau82ffa392013-08-13 17:19:08 +02003352 err_code |= ERR_WARN;
3353 }
3354
Willy Tarreau29fbe512015-08-20 19:35:14 +02003355#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreauef9a3602012-12-08 22:29:20 +01003356 if (curproxy->mode != PR_MODE_HTTP && newsrv->conn_src.bind_hdr_occ) {
3357 newsrv->conn_src.bind_hdr_occ = 0;
Christopher Faulet767a84b2017-11-24 16:50:31 +01003358 ha_warning("config : %s '%s' : server %s cannot use header %s as source IP in non-HTTP mode.\n",
3359 proxy_type_str(curproxy), curproxy->id, newsrv->id, newsrv->conn_src.bind_hdr_name);
Willy Tarreaubce70882009-09-07 11:51:47 +02003360 err_code |= ERR_WARN;
3361 }
Willy Tarreauefa5f512010-03-30 20:13:29 +02003362#endif
Willy Tarreau4c183462017-01-06 12:21:38 +01003363
Willy Tarreau46deab62018-04-28 07:18:15 +02003364 if ((curproxy->mode != PR_MODE_HTTP) && (curproxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR)
3365 curproxy->options &= ~PR_O_REUSE_MASK;
3366
Willy Tarreau21d2af32008-02-14 20:25:24 +01003367 newsrv = newsrv->next;
3368 }
3369
Christopher Fauletd7c91962015-04-30 11:48:27 +02003370 /* Check filter configuration, if any */
3371 cfgerr += flt_check(curproxy);
3372
Willy Tarreauc1a21672009-08-16 22:37:44 +02003373 if (curproxy->cap & PR_CAP_FE) {
Willy Tarreau050536d2012-10-04 08:47:34 +02003374 if (!curproxy->accept)
3375 curproxy->accept = frontend_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +02003376
Willy Tarreauc1a21672009-08-16 22:37:44 +02003377 if (curproxy->tcp_req.inspect_delay ||
3378 !LIST_ISEMPTY(&curproxy->tcp_req.inspect_rules))
Willy Tarreaufb356202010-08-03 14:02:05 +02003379 curproxy->fe_req_ana |= AN_REQ_INSPECT_FE;
Willy Tarreauc1a21672009-08-16 22:37:44 +02003380
Willy Tarreau4e5b8282009-08-16 22:57:50 +02003381 if (curproxy->mode == PR_MODE_HTTP) {
Willy Tarreauc1a21672009-08-16 22:37:44 +02003382 curproxy->fe_req_ana |= AN_REQ_WAIT_HTTP | AN_REQ_HTTP_PROCESS_FE;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003383 curproxy->fe_rsp_ana |= AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_FE;
Willy Tarreau4e5b8282009-08-16 22:57:50 +02003384 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02003385
William Lallemandcf62f7e2018-10-26 14:47:40 +02003386 if (curproxy->mode == PR_MODE_CLI) {
3387 curproxy->fe_req_ana |= AN_REQ_WAIT_CLI;
3388 curproxy->fe_rsp_ana |= AN_RES_WAIT_CLI;
3389 }
3390
Willy Tarreauc1a21672009-08-16 22:37:44 +02003391 /* both TCP and HTTP must check switching rules */
3392 curproxy->fe_req_ana |= AN_REQ_SWITCHING_RULES;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003393
3394 /* Add filters analyzers if needed */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01003395 if (!LIST_ISEMPTY(&curproxy->filter_configs)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003396 curproxy->fe_req_ana |= AN_REQ_FLT_START_FE | AN_REQ_FLT_XFER_DATA | AN_REQ_FLT_END;
3397 curproxy->fe_rsp_ana |= AN_RES_FLT_START_FE | AN_RES_FLT_XFER_DATA | AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003398 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02003399 }
3400
3401 if (curproxy->cap & PR_CAP_BE) {
Willy Tarreaufb356202010-08-03 14:02:05 +02003402 if (curproxy->tcp_req.inspect_delay ||
3403 !LIST_ISEMPTY(&curproxy->tcp_req.inspect_rules))
3404 curproxy->be_req_ana |= AN_REQ_INSPECT_BE;
3405
Emeric Brun97679e72010-09-23 17:56:44 +02003406 if (!LIST_ISEMPTY(&curproxy->tcp_rep.inspect_rules))
3407 curproxy->be_rsp_ana |= AN_RES_INSPECT;
3408
Willy Tarreau4e5b8282009-08-16 22:57:50 +02003409 if (curproxy->mode == PR_MODE_HTTP) {
Willy Tarreauc1a21672009-08-16 22:37:44 +02003410 curproxy->be_req_ana |= AN_REQ_WAIT_HTTP | AN_REQ_HTTP_INNER | AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003411 curproxy->be_rsp_ana |= AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE;
Willy Tarreau4e5b8282009-08-16 22:57:50 +02003412 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02003413
3414 /* If the backend does requires RDP cookie persistence, we have to
3415 * enable the corresponding analyser.
3416 */
3417 if (curproxy->options2 & PR_O2_RDPC_PRST)
3418 curproxy->be_req_ana |= AN_REQ_PRST_RDP_COOKIE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003419
3420 /* Add filters analyzers if needed */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01003421 if (!LIST_ISEMPTY(&curproxy->filter_configs)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003422 curproxy->be_req_ana |= AN_REQ_FLT_START_BE | AN_REQ_FLT_XFER_DATA | AN_REQ_FLT_END;
3423 curproxy->be_rsp_ana |= AN_RES_FLT_START_BE | AN_RES_FLT_XFER_DATA | AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003424 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02003425 }
Christopher Fauleta717b992018-04-10 14:43:00 +02003426
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003427 /* Check the mux protocols, if any, for each listener and server
Christopher Fauleta717b992018-04-10 14:43:00 +02003428 * attached to the current proxy */
3429 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
3430 int mode = (1 << (curproxy->mode == PR_MODE_HTTP));
Willy Tarreau76a551d2018-12-02 13:09:09 +01003431 const struct mux_proto_list *mux_ent;
Christopher Fauleta717b992018-04-10 14:43:00 +02003432
3433 if (!bind_conf->mux_proto)
3434 continue;
Willy Tarreau76a551d2018-12-02 13:09:09 +01003435
3436 /* it is possible that an incorrect mux was referenced
3437 * due to the proxy's mode not being taken into account
3438 * on first pass. Let's adjust it now.
3439 */
3440 mux_ent = conn_get_best_mux_entry(bind_conf->mux_proto->token, PROTO_SIDE_FE, mode);
3441
3442 if (!mux_ent || !isteq(mux_ent->token, bind_conf->mux_proto->token)) {
Christopher Fauleta717b992018-04-10 14:43:00 +02003443 ha_alert("config : %s '%s' : MUX protocol '%.*s' is not usable for 'bind %s' at [%s:%d].\n",
3444 proxy_type_str(curproxy), curproxy->id,
3445 (int)bind_conf->mux_proto->token.len,
3446 bind_conf->mux_proto->token.ptr,
3447 bind_conf->arg, bind_conf->file, bind_conf->line);
3448 cfgerr++;
3449 }
Willy Tarreau76a551d2018-12-02 13:09:09 +01003450
3451 /* update the mux */
3452 bind_conf->mux_proto = mux_ent;
Christopher Fauleta717b992018-04-10 14:43:00 +02003453 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003454 for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
3455 int mode = (1 << (curproxy->mode == PR_MODE_HTTP));
Willy Tarreau76a551d2018-12-02 13:09:09 +01003456 const struct mux_proto_list *mux_ent;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003457
3458 if (!newsrv->mux_proto)
3459 continue;
Willy Tarreau76a551d2018-12-02 13:09:09 +01003460
3461 /* it is possible that an incorrect mux was referenced
3462 * due to the proxy's mode not being taken into account
3463 * on first pass. Let's adjust it now.
3464 */
3465 mux_ent = conn_get_best_mux_entry(newsrv->mux_proto->token, PROTO_SIDE_BE, mode);
3466
3467 if (!mux_ent || !isteq(mux_ent->token, newsrv->mux_proto->token)) {
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003468 ha_alert("config : %s '%s' : MUX protocol '%.*s' is not usable for server '%s' at [%s:%d].\n",
3469 proxy_type_str(curproxy), curproxy->id,
3470 (int)newsrv->mux_proto->token.len,
3471 newsrv->mux_proto->token.ptr,
3472 newsrv->id, newsrv->conf.file, newsrv->conf.line);
3473 cfgerr++;
3474 }
Willy Tarreau76a551d2018-12-02 13:09:09 +01003475
3476 /* update the mux */
3477 newsrv->mux_proto = mux_ent;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003478 }
Willy Tarreau4cdac162021-03-05 10:48:42 +01003479 }
Willy Tarreau835daa12019-02-07 14:46:29 +01003480
Willy Tarreau4cdac162021-03-05 10:48:42 +01003481 /***********************************************************/
3482 /* At this point, target names have already been resolved. */
3483 /***********************************************************/
Willy Tarreau835daa12019-02-07 14:46:29 +01003484
Willy Tarreau4cdac162021-03-05 10:48:42 +01003485 /* we must finish to initialize certain things on the servers */
Willy Tarreau835daa12019-02-07 14:46:29 +01003486
Willy Tarreau4cdac162021-03-05 10:48:42 +01003487 list_for_each_entry(newsrv, &servers_list, global_list) {
3488 /* initialize idle conns lists */
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003489 newsrv->per_thr = calloc(global.nbthread, sizeof(*newsrv->per_thr));
3490 if (!newsrv->per_thr) {
3491 ha_alert("parsing [%s:%d] : failed to allocate per-thread lists for server '%s'.\n",
3492 newsrv->conf.file, newsrv->conf.line, newsrv->id);
Willy Tarreau4cdac162021-03-05 10:48:42 +01003493 cfgerr++;
3494 continue;
3495 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003496
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003497 for (i = 0; i < global.nbthread; i++) {
3498 newsrv->per_thr[i].idle_conns = EB_ROOT;
3499 newsrv->per_thr[i].safe_conns = EB_ROOT;
3500 newsrv->per_thr[i].avail_conns = EB_ROOT;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01003501 MT_LIST_INIT(&newsrv->per_thr[i].streams);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003502 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003503
Willy Tarreau4cdac162021-03-05 10:48:42 +01003504 if (newsrv->max_idle_conns != 0) {
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003505 newsrv->curr_idle_thr = calloc(global.nbthread, sizeof(*newsrv->curr_idle_thr));
3506 if (!newsrv->curr_idle_thr) {
3507 ha_alert("parsing [%s:%d] : failed to allocate idle connection tasks for server '%s'.\n",
3508 newsrv->conf.file, newsrv->conf.line, newsrv->id);
3509 cfgerr++;
3510 continue;
3511 }
3512 }
3513 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003514
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003515 idle_conn_task = task_new(MAX_THREADS_MASK);
3516 if (!idle_conn_task) {
3517 ha_alert("parsing : failed to allocate global idle connection task.\n");
3518 cfgerr++;
3519 }
3520 else {
3521 idle_conn_task->process = srv_cleanup_idle_conns;
3522 idle_conn_task->context = NULL;
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003523
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003524 for (i = 0; i < global.nbthread; i++) {
3525 idle_conns[i].cleanup_task = task_new(1UL << i);
3526 if (!idle_conns[i].cleanup_task) {
3527 ha_alert("parsing : failed to allocate idle connection tasks for thread '%d'.\n", i);
3528 cfgerr++;
3529 break;
Willy Tarreau4cdac162021-03-05 10:48:42 +01003530 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003531
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003532 idle_conns[i].cleanup_task->process = srv_cleanup_toremove_conns;
3533 idle_conns[i].cleanup_task->context = NULL;
3534 HA_SPIN_INIT(&idle_conns[i].idle_conns_lock);
3535 MT_LIST_INIT(&idle_conns[i].toremove_conns);
Willy Tarreau835daa12019-02-07 14:46:29 +01003536 }
Willy Tarreau419ead82014-09-16 13:41:21 +02003537 }
3538
Willy Tarreau419ead82014-09-16 13:41:21 +02003539 /* Check multi-process mode compatibility */
3540
Willy Tarreau4975d142021-03-13 11:00:33 +01003541 if (global.nbproc > 1 && global.cli_fe) {
3542 list_for_each_entry(bind_conf, &global.cli_fe->conf.bind, by_fe) {
Willy Tarreau419ead82014-09-16 13:41:21 +02003543 unsigned long mask;
3544
Willy Tarreau4975d142021-03-13 11:00:33 +01003545 mask = proc_mask(global.cli_fe->bind_proc) && all_proc_mask;
Willy Tarreaue26993c2020-09-03 07:18:55 +02003546 mask &= proc_mask(bind_conf->settings.bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003547
3548 /* stop here if more than one process is used */
Willy Tarreau9504dd62018-10-15 09:37:03 +02003549 if (atleast2(mask))
Willy Tarreau419ead82014-09-16 13:41:21 +02003550 break;
3551 }
Willy Tarreau4975d142021-03-13 11:00:33 +01003552 if (&bind_conf->by_fe != &global.cli_fe->conf.bind) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003553 ha_warning("stats socket will not work as expected in multi-process mode (nbproc > 1), you should force process binding globally using 'stats bind-process' or per socket using the 'process' attribute.\n");
Willy Tarreau419ead82014-09-16 13:41:21 +02003554 }
3555 }
3556
3557 /* Make each frontend inherit bind-process from its listeners when not specified. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003558 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreau419ead82014-09-16 13:41:21 +02003559 if (curproxy->bind_proc)
3560 continue;
3561
3562 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
3563 unsigned long mask;
3564
Willy Tarreaue26993c2020-09-03 07:18:55 +02003565 mask = proc_mask(bind_conf->settings.bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003566 curproxy->bind_proc |= mask;
3567 }
Willy Tarreau6daac192019-02-02 17:39:53 +01003568 curproxy->bind_proc = proc_mask(curproxy->bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003569 }
3570
Willy Tarreau4975d142021-03-13 11:00:33 +01003571 if (global.cli_fe) {
3572 list_for_each_entry(bind_conf, &global.cli_fe->conf.bind, by_fe) {
Willy Tarreau419ead82014-09-16 13:41:21 +02003573 unsigned long mask;
3574
Willy Tarreaue26993c2020-09-03 07:18:55 +02003575 mask = bind_conf->settings.bind_proc ? bind_conf->settings.bind_proc : 0;
Willy Tarreau4975d142021-03-13 11:00:33 +01003576 global.cli_fe->bind_proc |= mask;
Willy Tarreau419ead82014-09-16 13:41:21 +02003577 }
Willy Tarreau4975d142021-03-13 11:00:33 +01003578 global.cli_fe->bind_proc = proc_mask(global.cli_fe->bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003579 }
3580
Willy Tarreauacbe8ab2014-10-01 20:50:17 +02003581 /* propagate bindings from frontends to backends. Don't do it if there
3582 * are any fatal errors as we must not call it with unresolved proxies.
3583 */
3584 if (!cfgerr) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003585 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreauacbe8ab2014-10-01 20:50:17 +02003586 if (curproxy->cap & PR_CAP_FE)
3587 propagate_processes(curproxy, NULL);
3588 }
Willy Tarreau419ead82014-09-16 13:41:21 +02003589 }
3590
3591 /* Bind each unbound backend to all processes when not specified. */
Willy Tarreau6daac192019-02-02 17:39:53 +01003592 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next)
3593 curproxy->bind_proc = proc_mask(curproxy->bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003594
3595 /*******************************************************/
3596 /* At this step, all proxies have a non-null bind_proc */
3597 /*******************************************************/
3598
3599 /* perform the final checks before creating tasks */
3600
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003601 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreau419ead82014-09-16 13:41:21 +02003602 struct listener *listener;
3603 unsigned int next_id;
Willy Tarreauc1a21672009-08-16 22:37:44 +02003604
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02003605 /* Configure SSL for each bind line.
3606 * Note: if configuration fails at some point, the ->ctx member
3607 * remains NULL so that listeners can later detach.
3608 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003609 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
Willy Tarreau55d37912016-12-21 23:38:39 +01003610 if (bind_conf->xprt->prepare_bind_conf &&
3611 bind_conf->xprt->prepare_bind_conf(bind_conf) < 0)
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02003612 cfgerr++;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003613 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02003614
Willy Tarreaue6b98942007-10-29 01:09:36 +01003615 /* adjust this proxy's listeners */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02003616 next_id = 1;
Willy Tarreau4348fad2012-09-20 16:48:07 +02003617 list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) {
Willy Tarreau7c0ffd22016-04-14 11:47:38 +02003618 int nbproc;
3619
3620 nbproc = my_popcountl(curproxy->bind_proc &
Willy Tarreaue26993c2020-09-03 07:18:55 +02003621 (listener->bind_conf->settings.bind_proc ? listener->bind_conf->settings.bind_proc : curproxy->bind_proc) &
Willy Tarreaua38a7172019-02-02 17:11:28 +01003622 all_proc_mask);
Willy Tarreau7c0ffd22016-04-14 11:47:38 +02003623
3624 if (!nbproc) /* no intersection between listener and frontend */
3625 nbproc = 1;
3626
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02003627 if (!listener->luid) {
3628 /* listener ID not set, use automatic numbering with first
3629 * spare entry starting with next_luid.
3630 */
3631 next_id = get_next_id(&curproxy->conf.used_listener_id, next_id);
3632 listener->conf.id.key = listener->luid = next_id;
3633 eb32_insert(&curproxy->conf.used_listener_id, &listener->conf.id);
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02003634 }
Krzysztof Piotr Oledzkidf5cb9f2010-02-05 20:58:27 +01003635 next_id++;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02003636
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003637 /* enable separate counters */
3638 if (curproxy->options2 & PR_O2_SOCKSTAT) {
Willy Tarreauae9bea02016-11-25 14:44:52 +01003639 listener->counters = calloc(1, sizeof(*listener->counters));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003640 if (!listener->name)
3641 memprintf(&listener->name, "sock-%d", listener->luid);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003642 }
Willy Tarreau81796be2012-09-22 19:11:47 +02003643
Willy Tarreaue6b98942007-10-29 01:09:36 +01003644 if (curproxy->options & PR_O_TCP_NOLING)
3645 listener->options |= LI_O_NOLINGER;
Willy Tarreau16a21472012-11-19 12:39:59 +01003646 if (!listener->maxaccept)
Willy Tarreau66161322021-02-19 15:50:27 +01003647 listener->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreau16a21472012-11-19 12:39:59 +01003648
3649 /* we want to have an optimal behaviour on single process mode to
3650 * maximize the work at once, but in multi-process we want to keep
3651 * some fairness between processes, so we target half of the max
3652 * number of events to be balanced over all the processes the proxy
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003653 * is bound to. Remember that maxaccept = -1 must be kept as it is
Willy Tarreau16a21472012-11-19 12:39:59 +01003654 * used to disable the limit.
3655 */
Christopher Faulet02f3cf12019-04-30 14:08:41 +02003656 if (listener->maxaccept > 0 && nbproc > 1) {
3657 listener->maxaccept = (listener->maxaccept + 1) / 2;
Willy Tarreau16a21472012-11-19 12:39:59 +01003658 listener->maxaccept = (listener->maxaccept + nbproc - 1) / nbproc;
3659 }
3660
Willy Tarreau9903f0e2015-04-04 18:50:31 +02003661 listener->accept = session_accept_fd;
Willy Tarreauc1a21672009-08-16 22:37:44 +02003662 listener->analysers |= curproxy->fe_req_ana;
Willy Tarreau10b688f2015-03-13 16:43:12 +01003663 listener->default_target = curproxy->default_target;
Willy Tarreau3bc13772008-12-07 11:50:35 +01003664
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02003665 if (!LIST_ISEMPTY(&curproxy->tcp_req.l4_rules))
Willy Tarreau7d9736f2016-10-21 16:34:21 +02003666 listener->options |= LI_O_TCP_L4_RULES;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02003667
Willy Tarreau620408f2016-10-21 16:37:51 +02003668 if (!LIST_ISEMPTY(&curproxy->tcp_req.l5_rules))
3669 listener->options |= LI_O_TCP_L5_RULES;
3670
Willy Tarreau9ea05a72009-06-14 12:07:01 +02003671 /* smart accept mode is automatic in HTTP mode */
3672 if ((curproxy->options2 & PR_O2_SMARTACC) ||
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003673 ((curproxy->mode == PR_MODE_HTTP || listener->bind_conf->is_ssl) &&
Willy Tarreau9ea05a72009-06-14 12:07:01 +02003674 !(curproxy->no_options2 & PR_O2_SMARTACC)))
3675 listener->options |= LI_O_NOQUICKACK;
Willy Tarreaue6b98942007-10-29 01:09:36 +01003676 }
3677
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003678 /* Release unused SSL configs */
3679 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01003680 if (!bind_conf->is_ssl && bind_conf->xprt->destroy_bind_conf)
3681 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003682 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02003683
Willy Tarreaua38a7172019-02-02 17:11:28 +01003684 if (atleast2(curproxy->bind_proc & all_proc_mask)) {
Willy Tarreau102df612014-05-07 23:56:38 +02003685 if (curproxy->uri_auth) {
Willy Tarreaueb791e02014-09-16 15:11:04 +02003686 int count, maxproc = 0;
3687
3688 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
Willy Tarreaue26993c2020-09-03 07:18:55 +02003689 count = my_popcountl(bind_conf->settings.bind_proc);
Willy Tarreaueb791e02014-09-16 15:11:04 +02003690 if (count > maxproc)
3691 maxproc = count;
3692 }
3693 /* backends have 0, frontends have 1 or more */
3694 if (maxproc != 1)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003695 ha_warning("Proxy '%s': in multi-process mode, stats will be"
3696 " limited to process assigned to the current request.\n",
3697 curproxy->id);
Willy Tarreaueb791e02014-09-16 15:11:04 +02003698
Willy Tarreau102df612014-05-07 23:56:38 +02003699 if (!LIST_ISEMPTY(&curproxy->uri_auth->admin_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003700 ha_warning("Proxy '%s': stats admin will not work correctly in multi-process mode.\n",
3701 curproxy->id);
Cyril Bonté02ff8ef2010-12-14 22:48:49 +01003702 }
Cyril Bonté02ff8ef2010-12-14 22:48:49 +01003703 }
Willy Tarreau102df612014-05-07 23:56:38 +02003704 if (!LIST_ISEMPTY(&curproxy->sticking_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003705 ha_warning("Proxy '%s': sticking rules will not work correctly in multi-process mode.\n",
3706 curproxy->id);
Cyril Bonté02ff8ef2010-12-14 22:48:49 +01003707 }
3708 }
Willy Tarreau918ff602011-07-25 16:33:49 +02003709
3710 /* create the task associated with the proxy */
Emeric Brunc60def82017-09-27 14:59:38 +02003711 curproxy->task = task_new(MAX_THREADS_MASK);
Willy Tarreau918ff602011-07-25 16:33:49 +02003712 if (curproxy->task) {
3713 curproxy->task->context = curproxy;
3714 curproxy->task->process = manage_proxy;
Willy Tarreau918ff602011-07-25 16:33:49 +02003715 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003716 ha_alert("Proxy '%s': no more memory when trying to allocate the management task\n",
3717 curproxy->id);
Willy Tarreau918ff602011-07-25 16:33:49 +02003718 cfgerr++;
3719 }
Willy Tarreaub369a042014-09-16 13:21:03 +02003720 }
3721
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003722 /*
3723 * Recount currently required checks.
3724 */
3725
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003726 for (curproxy=proxies_list; curproxy; curproxy=curproxy->next) {
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003727 int optnum;
3728
Willy Tarreau66aa61f2009-01-18 21:44:07 +01003729 for (optnum = 0; cfg_opts[optnum].name; optnum++)
3730 if (curproxy->options & cfg_opts[optnum].val)
3731 global.last_checks |= cfg_opts[optnum].checks;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003732
Willy Tarreau66aa61f2009-01-18 21:44:07 +01003733 for (optnum = 0; cfg_opts2[optnum].name; optnum++)
3734 if (curproxy->options2 & cfg_opts2[optnum].val)
3735 global.last_checks |= cfg_opts2[optnum].checks;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003736 }
3737
Willy Tarreau0fca4832015-05-01 19:12:05 +02003738 /* compute the required process bindings for the peers */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003739 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003740 if (curproxy->table && curproxy->table->peers.p)
3741 curproxy->table->peers.p->peers_fe->bind_proc |= curproxy->bind_proc;
Willy Tarreau0fca4832015-05-01 19:12:05 +02003742
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01003743 /* compute the required process bindings for the peers from <stktables_list>
3744 * for all the stick-tables, the ones coming with "peers" sections included.
3745 */
3746 for (t = stktables_list; t; t = t->next) {
3747 struct proxy *p;
3748
3749 for (p = t->proxies_list; p; p = p->next_stkt_ref) {
3750 if (t->peers.p && t->peers.p->peers_fe) {
3751 t->peers.p->peers_fe->bind_proc |= p->bind_proc;
3752 }
3753 }
3754 }
3755
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003756 if (cfg_peers) {
3757 struct peers *curpeers = cfg_peers, **last;
Willy Tarreau122541c2011-09-07 21:24:49 +02003758 struct peer *p, *pb;
3759
William Lallemanda2cfd7e2020-03-24 16:02:48 +01003760 /* In the case the peers frontend was not initialized by a
3761 stick-table used in the configuration, set its bind_proc
3762 by default to the first process. */
3763 while (curpeers) {
William Lallemand3ef2d562020-03-24 16:42:15 +01003764 if (curpeers->peers_fe) {
3765 if (curpeers->peers_fe->bind_proc == 0)
3766 curpeers->peers_fe->bind_proc = 1;
3767 }
William Lallemanda2cfd7e2020-03-24 16:02:48 +01003768 curpeers = curpeers->next;
3769 }
3770
3771 curpeers = cfg_peers;
Willy Tarreau1e273012015-05-01 19:15:17 +02003772 /* Remove all peers sections which don't have a valid listener,
3773 * which are not used by any table, or which are bound to more
3774 * than one process.
Willy Tarreau122541c2011-09-07 21:24:49 +02003775 */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003776 last = &cfg_peers;
Willy Tarreau122541c2011-09-07 21:24:49 +02003777 while (*last) {
Frédéric Lécaille87eacbb2020-03-24 20:08:30 +01003778 struct stktable *t;
Willy Tarreau122541c2011-09-07 21:24:49 +02003779 curpeers = *last;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003780
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003781 if (curpeers->disabled) {
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003782 /* the "disabled" keyword was present */
3783 if (curpeers->peers_fe)
3784 stop_proxy(curpeers->peers_fe);
3785 curpeers->peers_fe = NULL;
3786 }
Frédéric Lécaille5a4fe5a2019-10-04 08:30:04 +02003787 else if (!curpeers->peers_fe || !curpeers->peers_fe->id) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003788 ha_warning("Removing incomplete section 'peers %s' (no peer named '%s').\n",
3789 curpeers->id, localpeer);
Frédéric Lécaille87eacbb2020-03-24 20:08:30 +01003790 if (curpeers->peers_fe)
3791 stop_proxy(curpeers->peers_fe);
3792 curpeers->peers_fe = NULL;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003793 }
Willy Tarreau9504dd62018-10-15 09:37:03 +02003794 else if (atleast2(curpeers->peers_fe->bind_proc)) {
Willy Tarreau1e273012015-05-01 19:15:17 +02003795 /* either it's totally stopped or too much used */
3796 if (curpeers->peers_fe->bind_proc) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003797 ha_alert("Peers section '%s': peers referenced by sections "
3798 "running in different processes (%d different ones). "
3799 "Check global.nbproc and all tables' bind-process "
3800 "settings.\n", curpeers->id, my_popcountl(curpeers->peers_fe->bind_proc));
Willy Tarreau1e273012015-05-01 19:15:17 +02003801 cfgerr++;
3802 }
3803 stop_proxy(curpeers->peers_fe);
3804 curpeers->peers_fe = NULL;
3805 }
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003806 else {
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +01003807 /* Initializes the transport layer of the server part of all the peers belonging to
3808 * <curpeers> section if required.
3809 * Note that ->srv is used by the local peer of a new process to connect to the local peer
3810 * of an old process.
3811 */
Frédéric Lécaille1055e682018-04-26 14:35:21 +02003812 p = curpeers->remote;
3813 while (p) {
Frédéric Lécaille355b2032019-01-11 14:06:12 +01003814 if (p->srv) {
Christopher Fauletf61f33a2020-03-27 18:55:49 +01003815 if (p->srv->use_ssl == 1 && xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv)
Frédéric Lécaille355b2032019-01-11 14:06:12 +01003816 cfgerr += xprt_get(XPRT_SSL)->prepare_srv(p->srv);
3817 }
Frédéric Lécaille1055e682018-04-26 14:35:21 +02003818 p = p->next;
3819 }
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +01003820 /* Configure the SSL bindings of the local peer if required. */
3821 if (!LIST_ISEMPTY(&curpeers->peers_fe->conf.bind)) {
3822 struct list *l;
3823 struct bind_conf *bind_conf;
3824
3825 l = &curpeers->peers_fe->conf.bind;
3826 bind_conf = LIST_ELEM(l->n, typeof(bind_conf), by_fe);
3827 if (bind_conf->xprt->prepare_bind_conf &&
3828 bind_conf->xprt->prepare_bind_conf(bind_conf) < 0)
3829 cfgerr++;
3830 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003831 if (!peers_init_sync(curpeers) || !peers_alloc_dcache(curpeers)) {
Willy Tarreaud9443442018-10-15 11:18:03 +02003832 ha_alert("Peers section '%s': out of memory, giving up on peers.\n",
3833 curpeers->id);
3834 cfgerr++;
3835 break;
3836 }
Willy Tarreau122541c2011-09-07 21:24:49 +02003837 last = &curpeers->next;
3838 continue;
3839 }
3840
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003841 /* clean what has been detected above */
Willy Tarreau122541c2011-09-07 21:24:49 +02003842 p = curpeers->remote;
3843 while (p) {
3844 pb = p->next;
3845 free(p->id);
3846 free(p);
3847 p = pb;
3848 }
3849
3850 /* Destroy and unlink this curpeers section.
3851 * Note: curpeers is backed up into *last.
3852 */
3853 free(curpeers->id);
3854 curpeers = curpeers->next;
Frédéric Lécaille87eacbb2020-03-24 20:08:30 +01003855 /* Reset any refereance to this peers section in the list of stick-tables */
3856 for (t = stktables_list; t; t = t->next) {
3857 if (t->peers.p && t->peers.p == *last)
3858 t->peers.p = NULL;
3859 }
Willy Tarreau122541c2011-09-07 21:24:49 +02003860 free(*last);
3861 *last = curpeers;
3862 }
3863 }
3864
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003865 for (t = stktables_list; t; t = t->next) {
3866 if (t->proxy)
3867 continue;
3868 if (!stktable_init(t)) {
3869 ha_alert("Proxy '%s': failed to initialize stick-table.\n", t->id);
3870 cfgerr++;
3871 }
3872 }
3873
Willy Tarreau6866f3f2015-05-01 19:09:08 +02003874 /* initialize stick-tables on backend capable proxies. This must not
3875 * be done earlier because the data size may be discovered while parsing
3876 * other proxies.
3877 */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003878 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02003879 if (curproxy->disabled || !curproxy->table)
Willy Tarreau6866f3f2015-05-01 19:09:08 +02003880 continue;
3881
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003882 if (!stktable_init(curproxy->table)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003883 ha_alert("Proxy '%s': failed to initialize stick-table.\n", curproxy->id);
Willy Tarreau6866f3f2015-05-01 19:09:08 +02003884 cfgerr++;
3885 }
3886 }
3887
Simon Horman0d16a402015-01-30 11:22:58 +09003888 if (mailers) {
3889 struct mailers *curmailers = mailers, **last;
3890 struct mailer *m, *mb;
3891
3892 /* Remove all mailers sections which don't have a valid listener.
3893 * This can happen when a mailers section is never referenced.
3894 */
3895 last = &mailers;
3896 while (*last) {
3897 curmailers = *last;
3898 if (curmailers->users) {
3899 last = &curmailers->next;
3900 continue;
3901 }
3902
Christopher Faulet767a84b2017-11-24 16:50:31 +01003903 ha_warning("Removing incomplete section 'mailers %s'.\n",
3904 curmailers->id);
Simon Horman0d16a402015-01-30 11:22:58 +09003905
3906 m = curmailers->mailer_list;
3907 while (m) {
3908 mb = m->next;
3909 free(m->id);
3910 free(m);
3911 m = mb;
3912 }
3913
3914 /* Destroy and unlink this curmailers section.
3915 * Note: curmailers is backed up into *last.
3916 */
3917 free(curmailers->id);
3918 curmailers = curmailers->next;
3919 free(*last);
3920 *last = curmailers;
3921 }
3922 }
3923
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003924 /* Update server_state_file_name to backend name if backend is supposed to use
3925 * a server-state file locally defined and none has been provided */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003926 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003927 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_LOCAL &&
3928 curproxy->server_state_file_name == NULL)
3929 curproxy->server_state_file_name = strdup(curproxy->id);
3930 }
3931
Emeric Brun750fe792020-12-23 16:51:12 +01003932 list_for_each_entry(curr_resolvers, &sec_resolvers, list) {
Ben Draut054fbee2018-04-13 15:43:04 -06003933 if (LIST_ISEMPTY(&curr_resolvers->nameservers)) {
3934 ha_warning("config : resolvers '%s' [%s:%d] has no nameservers configured!\n",
3935 curr_resolvers->id, curr_resolvers->conf.file,
3936 curr_resolvers->conf.line);
3937 err_code |= ERR_WARN;
3938 }
3939 }
3940
William Lallemand48b4bb42017-10-23 14:36:34 +02003941 list_for_each_entry(postparser, &postparsers, list) {
3942 if (postparser->func)
3943 cfgerr += postparser->func();
3944 }
3945
Willy Tarreaubb925012009-07-23 13:36:36 +02003946 if (cfgerr > 0)
3947 err_code |= ERR_ALERT | ERR_FATAL;
3948 out:
3949 return err_code;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003950}
3951
Willy Tarreau5b2c3362008-07-09 19:39:06 +02003952/*
3953 * Registers the CFG keyword list <kwl> as a list of valid keywords for next
3954 * parsing sessions.
3955 */
3956void cfg_register_keywords(struct cfg_kw_list *kwl)
3957{
Willy Tarreau2b718102021-04-21 07:32:39 +02003958 LIST_APPEND(&cfg_keywords.list, &kwl->list);
Willy Tarreau5b2c3362008-07-09 19:39:06 +02003959}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003960
Willy Tarreau5b2c3362008-07-09 19:39:06 +02003961/*
3962 * Unregisters the CFG keyword list <kwl> from the list of valid keywords.
3963 */
3964void cfg_unregister_keywords(struct cfg_kw_list *kwl)
3965{
Willy Tarreau2b718102021-04-21 07:32:39 +02003966 LIST_DELETE(&kwl->list);
Willy Tarreau5b2c3362008-07-09 19:39:06 +02003967 LIST_INIT(&kwl->list);
3968}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003969
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01003970/* this function register new section in the haproxy configuration file.
3971 * <section_name> is the name of this new section and <section_parser>
3972 * is the called parser. If two section declaration have the same name,
3973 * only the first declared is used.
3974 */
3975int cfg_register_section(char *section_name,
William Lallemandd2ff56d2017-10-16 11:06:50 +02003976 int (*section_parser)(const char *, int, char **, int),
3977 int (*post_section_parser)())
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01003978{
3979 struct cfg_section *cs;
3980
Willy Tarreau5e4261b2016-05-17 16:16:09 +02003981 list_for_each_entry(cs, &sections, list) {
3982 if (strcmp(cs->section_name, section_name) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003983 ha_alert("register section '%s': already registered.\n", section_name);
Willy Tarreau5e4261b2016-05-17 16:16:09 +02003984 return 0;
3985 }
3986 }
3987
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01003988 cs = calloc(1, sizeof(*cs));
3989 if (!cs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003990 ha_alert("register section '%s': out of memory.\n", section_name);
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01003991 return 0;
3992 }
3993
3994 cs->section_name = section_name;
3995 cs->section_parser = section_parser;
William Lallemandd2ff56d2017-10-16 11:06:50 +02003996 cs->post_section_parser = post_section_parser;
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01003997
Willy Tarreau2b718102021-04-21 07:32:39 +02003998 LIST_APPEND(&sections, &cs->list);
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01003999
4000 return 1;
4001}
4002
William Lallemand48b4bb42017-10-23 14:36:34 +02004003/* this function register a new function which will be called once the haproxy
4004 * configuration file has been parsed. It's useful to check dependencies
4005 * between sections or to resolve items once everything is parsed.
4006 */
4007int cfg_register_postparser(char *name, int (*func)())
4008{
4009 struct cfg_postparser *cp;
4010
4011 cp = calloc(1, sizeof(*cp));
4012 if (!cp) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004013 ha_alert("register postparser '%s': out of memory.\n", name);
William Lallemand48b4bb42017-10-23 14:36:34 +02004014 return 0;
4015 }
4016 cp->name = name;
4017 cp->func = func;
4018
Willy Tarreau2b718102021-04-21 07:32:39 +02004019 LIST_APPEND(&postparsers, &cp->list);
William Lallemand48b4bb42017-10-23 14:36:34 +02004020
4021 return 1;
4022}
4023
Willy Tarreaubaaee002006-06-26 02:48:02 +02004024/*
David Carlier845efb52015-09-25 11:49:18 +01004025 * free all config section entries
4026 */
4027void cfg_unregister_sections(void)
4028{
4029 struct cfg_section *cs, *ics;
4030
4031 list_for_each_entry_safe(cs, ics, &sections, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004032 LIST_DELETE(&cs->list);
David Carlier845efb52015-09-25 11:49:18 +01004033 free(cs);
4034 }
4035}
4036
Christopher Faulet7110b402016-10-26 11:09:44 +02004037void cfg_backup_sections(struct list *backup_sections)
4038{
4039 struct cfg_section *cs, *ics;
4040
4041 list_for_each_entry_safe(cs, ics, &sections, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004042 LIST_DELETE(&cs->list);
4043 LIST_APPEND(backup_sections, &cs->list);
Christopher Faulet7110b402016-10-26 11:09:44 +02004044 }
4045}
4046
4047void cfg_restore_sections(struct list *backup_sections)
4048{
4049 struct cfg_section *cs, *ics;
4050
4051 list_for_each_entry_safe(cs, ics, backup_sections, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004052 LIST_DELETE(&cs->list);
4053 LIST_APPEND(&sections, &cs->list);
Christopher Faulet7110b402016-10-26 11:09:44 +02004054 }
4055}
4056
Willy Tarreaue6552512018-11-26 11:33:13 +01004057/* these are the config sections handled by default */
4058REGISTER_CONFIG_SECTION("listen", cfg_parse_listen, NULL);
4059REGISTER_CONFIG_SECTION("frontend", cfg_parse_listen, NULL);
4060REGISTER_CONFIG_SECTION("backend", cfg_parse_listen, NULL);
4061REGISTER_CONFIG_SECTION("defaults", cfg_parse_listen, NULL);
4062REGISTER_CONFIG_SECTION("global", cfg_parse_global, NULL);
4063REGISTER_CONFIG_SECTION("userlist", cfg_parse_users, NULL);
4064REGISTER_CONFIG_SECTION("peers", cfg_parse_peers, NULL);
4065REGISTER_CONFIG_SECTION("mailers", cfg_parse_mailers, NULL);
4066REGISTER_CONFIG_SECTION("namespace_list", cfg_parse_netns, NULL);
Willy Tarreau659fbf02016-05-26 17:55:28 +02004067
David Carlier845efb52015-09-25 11:49:18 +01004068/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004069 * Local variables:
4070 * c-indent-level: 8
4071 * c-basic-offset: 8
4072 * End:
4073 */