blob: d8ba76a8b460c442e1ff3f92959567d951292084 [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 Tarreau732525f2021-05-06 15:49:04 +020040#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020041#include <haproxy/auth.h>
Willy Tarreau49801602020-06-04 22:50:02 +020042#include <haproxy/backend.h>
Willy Tarreau278161c2020-06-04 11:18:28 +020043#include <haproxy/capture.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020044#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020045#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020046#include <haproxy/check.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020047#include <haproxy/chunk.h>
Amaury Denoyellea6f9c5d2021-04-23 16:58:08 +020048#ifdef USE_CPU_AFFINITY
Amaury Denoyellec90932b2021-04-14 16:16:03 +020049#include <haproxy/cpuset.h>
Amaury Denoyellea6f9c5d2021-04-23 16:58:08 +020050#endif
Willy Tarreau7ea393d2020-06-04 18:02:10 +020051#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020052#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020053#include <haproxy/filters.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020054#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020055#include <haproxy/global.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020056#include <haproxy/http_ana.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020057#include <haproxy/http_rules.h>
Willy Tarreaufbe8da32020-06-04 14:34:27 +020058#include <haproxy/lb_chash.h>
Willy Tarreaub5fc3bf2020-06-04 14:37:38 +020059#include <haproxy/lb_fas.h>
Willy Tarreau02549412020-06-04 14:41:04 +020060#include <haproxy/lb_fwlc.h>
Willy Tarreau546ba422020-06-04 14:45:03 +020061#include <haproxy/lb_fwrr.h>
Willy Tarreau28671592020-06-04 20:22:59 +020062#include <haproxy/lb_map.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020063#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020064#include <haproxy/log.h>
Willy Tarreaucee013e2020-06-05 11:40:38 +020065#include <haproxy/mailers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020066#include <haproxy/namespace.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020067#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020068#include <haproxy/peers-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020069#include <haproxy/peers.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020070#include <haproxy/pool.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020071#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020072#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010073#include <haproxy/resolvers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020074#include <haproxy/sample.h>
75#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020076#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020077#include <haproxy/stats-t.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020078#include <haproxy/stick_table.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020079#include <haproxy/stream.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020080#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020081#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020082#include <haproxy/thread.h>
83#include <haproxy/time.h>
84#include <haproxy/tools.h>
85#include <haproxy/uri_auth-t.h>
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010086#include <haproxy/xprt_quic.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020087
88
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +010089/* Used to chain configuration sections definitions. This list
90 * stores struct cfg_section
91 */
92struct list sections = LIST_HEAD_INIT(sections);
93
William Lallemand48b4bb42017-10-23 14:36:34 +020094struct list postparsers = LIST_HEAD_INIT(postparsers);
95
Eric Salama1b8dacc2021-03-16 15:12:17 +010096extern struct proxy *mworker_proxy;
97
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010098char *cursection = NULL;
Willy Tarreauc8d5b952019-02-27 17:25:52 +010099int cfg_maxpconn = 0; /* # of simultaneous connections per proxy (-N) */
Willy Tarreau5af24ef2009-03-15 15:23:16 +0100100int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */
Christopher Faulet79bdef32016-11-04 22:36:15 +0100101char *cfg_scope = NULL; /* the current scope during the configuration parsing */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102
Willy Tarreau8a022d52021-04-27 20:29:11 +0200103/* how to handle default paths */
104static enum default_path_mode {
105 DEFAULT_PATH_CURRENT = 0, /* "current": paths are relative to CWD (this is the default) */
106 DEFAULT_PATH_CONFIG, /* "config": paths are relative to config file */
107 DEFAULT_PATH_PARENT, /* "parent": paths are relative to config file's ".." */
108 DEFAULT_PATH_ORIGIN, /* "origin": paths are relative to default_path_origin */
109} default_path_mode;
110
111static char initial_cwd[PATH_MAX];
112static char current_cwd[PATH_MAX];
113
Willy Tarreau5b2c3362008-07-09 19:39:06 +0200114/* List head of all known configuration keywords */
Willy Tarreau36b9e222018-11-11 15:19:52 +0100115struct cfg_kw_list cfg_keywords = {
Willy Tarreau5b2c3362008-07-09 19:39:06 +0200116 .list = LIST_HEAD_INIT(cfg_keywords.list)
117};
118
Willy Tarreau4b103022021-02-12 17:59:10 +0100119/* nested if/elif/else/endif block states */
120enum nested_cond_state {
121 NESTED_COND_IF_TAKE, // "if" with a true condition
122 NESTED_COND_IF_DROP, // "if" with a false condition
123 NESTED_COND_IF_SKIP, // "if" masked by an outer false condition
124
125 NESTED_COND_ELIF_TAKE, // "elif" with a true condition from a false one
126 NESTED_COND_ELIF_DROP, // "elif" with a false condition from a false one
127 NESTED_COND_ELIF_SKIP, // "elif" masked by an outer false condition or a previously taken if
128
129 NESTED_COND_ELSE_TAKE, // taken "else" after an if false condition
130 NESTED_COND_ELSE_DROP, // "else" masked by outer false condition or an if true condition
131};
132
133/* 100 levels of nested conditions should already be sufficient */
134#define MAXNESTEDCONDS 100
135
Willy Tarreau732525f2021-05-06 15:49:04 +0200136/* supported conditional predicates for .if/.elif */
137enum cond_predicate {
138 CFG_PRED_NONE, // none
Willy Tarreau42ed14b2021-05-06 15:55:14 +0200139 CFG_PRED_DEFINED, // "defined"
Willy Tarreau732525f2021-05-06 15:49:04 +0200140};
141
142struct cond_pred_kw {
143 const char *word; // NULL marks the end of the list
144 enum cond_predicate prd; // one of the CFG_PRED_* above
145 uint64_t arg_mask; // mask of supported arguments (strings only)
146};
147
148/* supported condition predicates */
149const struct cond_pred_kw cond_predicates[] = {
Willy Tarreau42ed14b2021-05-06 15:55:14 +0200150 { "defined", CFG_PRED_DEFINED, ARG1(1, STR) },
Willy Tarreau732525f2021-05-06 15:49:04 +0200151 { NULL, CFG_PRED_NONE, 0 }
152};
153
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154/*
155 * converts <str> to a list of listeners which are dynamically allocated.
156 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
157 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
158 * - <port> is a numerical port from 1 to 65535 ;
159 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
160 * This can be repeated as many times as necessary, separated by a coma.
Willy Tarreau4fbb2282012-09-20 20:01:39 +0200161 * Function returns 1 for success or 0 if error. In case of errors, if <err> is
162 * not NULL, it must be a valid pointer to either NULL or a freeable area that
163 * will be replaced with an error message.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164 */
Willy Tarreau4fbb2282012-09-20 20:01:39 +0200165int 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 +0200166{
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200167 struct protocol *proto;
Willy Tarreau2dff0c22011-03-04 15:43:13 +0100168 char *next, *dupstr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169 int port, end;
170
171 next = dupstr = strdup(str);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200172
Willy Tarreaubaaee002006-06-26 02:48:02 +0200173 while (next && *next) {
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200174 struct sockaddr_storage *ss2;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100175 int fd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200176
177 str = next;
178 /* 1) look for the end of the first address */
Krzysztof Piotr Oledzki52d522b2009-01-27 16:57:08 +0100179 if ((next = strchr(str, ',')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200180 *next++ = 0;
181 }
182
Willy Tarreau5fc93282020-09-16 18:25:03 +0200183 ss2 = str2sa_range(str, NULL, &port, &end, &fd, &proto, err,
Eric Salama1b8dacc2021-03-16 15:12:17 +0100184 (curproxy == global.cli_fe || curproxy == mworker_proxy) ? NULL : global.unix_bind.prefix,
Willy Tarreau32819932020-09-04 15:53:16 +0200185 NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
Willy Tarreau5e1779a2020-09-16 16:28:08 +0200186 PA_O_SOCKET_FD | PA_O_STREAM | PA_O_XPRT);
Willy Tarreau12eb2a62013-03-06 15:45:03 +0100187 if (!ss2)
188 goto fail;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189
Willy Tarreau12eb2a62013-03-06 15:45:03 +0100190 /* OK the address looks correct */
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100191
192#ifdef USE_QUIC
193 /* The transport layer automatically switches to QUIC when QUIC
194 * is selected, regardless of bind_conf settings. We then need
195 * to initialize QUIC params.
196 */
197 if (proto->sock_type == SOCK_DGRAM && proto->ctrl_type == SOCK_STREAM) {
198 bind_conf->xprt = xprt_get(XPRT_QUIC);
199 quic_transport_params_init(&bind_conf->quic_params, 1);
200 }
201#endif
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200202 if (!create_listeners(bind_conf, ss2, port, end, fd, proto, err)) {
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200203 memprintf(err, "%s for address '%s'.\n", *err, str);
204 goto fail;
205 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200206 } /* end while(next) */
207 free(dupstr);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200208 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200209 fail:
210 free(dupstr);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200211 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200212}
213
William Lallemand6e62fb62015-04-28 16:55:23 +0200214/*
Willy Tarreauaa333122020-09-16 15:13:04 +0200215 * converts <str> to a list of datagram-oriented listeners which are dynamically
216 * allocated.
217 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
218 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
219 * - <port> is a numerical port from 1 to 65535 ;
220 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
221 * This can be repeated as many times as necessary, separated by a coma.
222 * Function returns 1 for success or 0 if error. In case of errors, if <err> is
223 * not NULL, it must be a valid pointer to either NULL or a freeable area that
224 * will be replaced with an error message.
225 */
226int str2receiver(char *str, struct proxy *curproxy, struct bind_conf *bind_conf, const char *file, int line, char **err)
227{
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200228 struct protocol *proto;
Willy Tarreauaa333122020-09-16 15:13:04 +0200229 char *next, *dupstr;
230 int port, end;
231
232 next = dupstr = strdup(str);
233
234 while (next && *next) {
235 struct sockaddr_storage *ss2;
236 int fd = -1;
237
238 str = next;
239 /* 1) look for the end of the first address */
240 if ((next = strchr(str, ',')) != NULL) {
241 *next++ = 0;
242 }
243
Willy Tarreau5fc93282020-09-16 18:25:03 +0200244 ss2 = str2sa_range(str, NULL, &port, &end, &fd, &proto, err,
Willy Tarreau4975d142021-03-13 11:00:33 +0100245 curproxy == global.cli_fe ? NULL : global.unix_bind.prefix,
Willy Tarreauaa333122020-09-16 15:13:04 +0200246 NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
247 PA_O_SOCKET_FD | PA_O_DGRAM | PA_O_XPRT);
248 if (!ss2)
249 goto fail;
250
251 /* OK the address looks correct */
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200252 if (!create_listeners(bind_conf, ss2, port, end, fd, proto, err)) {
Willy Tarreauaa333122020-09-16 15:13:04 +0200253 memprintf(err, "%s for address '%s'.\n", *err, str);
254 goto fail;
255 }
256 } /* end while(next) */
257 free(dupstr);
258 return 1;
259 fail:
260 free(dupstr);
261 return 0;
262}
263
264/*
Willy Tarreauece9b072016-12-21 22:41:44 +0100265 * Report an error in <msg> when there are too many arguments. This version is
266 * intended to be used by keyword parsers so that the message will be included
267 * into the general error message. The index is the current keyword in args.
268 * Return 0 if the number of argument is correct, otherwise build a message and
269 * return 1. Fill err_code with an ERR_ALERT and an ERR_FATAL if not null. The
270 * message may also be null, it will simply not be produced (useful to check only).
271 * <msg> and <err_code> are only affected on error.
272 */
273int too_many_args_idx(int maxarg, int index, char **args, char **msg, int *err_code)
274{
275 int i;
276
277 if (!*args[index + maxarg + 1])
278 return 0;
279
280 if (msg) {
281 *msg = NULL;
282 memprintf(msg, "%s", args[0]);
283 for (i = 1; i <= index; i++)
284 memprintf(msg, "%s %s", *msg, args[i]);
285
286 memprintf(msg, "'%s' cannot handle unexpected argument '%s'.", *msg, args[index + maxarg + 1]);
287 }
288 if (err_code)
289 *err_code |= ERR_ALERT | ERR_FATAL;
290
291 return 1;
292}
293
294/*
295 * same as too_many_args_idx with a 0 index
296 */
297int too_many_args(int maxarg, char **args, char **msg, int *err_code)
298{
299 return too_many_args_idx(maxarg, 0, args, msg, err_code);
300}
301
302/*
William Lallemand6e62fb62015-04-28 16:55:23 +0200303 * Report a fatal Alert when there is too much arguments
304 * The index is the current keyword in args
305 * Return 0 if the number of argument is correct, otherwise emit an alert and return 1
306 * Fill err_code with an ERR_ALERT and an ERR_FATAL
307 */
308int alertif_too_many_args_idx(int maxarg, int index, const char *file, int linenum, char **args, int *err_code)
309{
310 char *kw = NULL;
311 int i;
312
313 if (!*args[index + maxarg + 1])
314 return 0;
315
316 memprintf(&kw, "%s", args[0]);
317 for (i = 1; i <= index; i++) {
318 memprintf(&kw, "%s %s", kw, args[i]);
319 }
320
Christopher Faulet767a84b2017-11-24 16:50:31 +0100321 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 +0200322 free(kw);
323 *err_code |= ERR_ALERT | ERR_FATAL;
324 return 1;
325}
326
327/*
328 * same as alertif_too_many_args_idx with a 0 index
329 */
330int alertif_too_many_args(int maxarg, const char *file, int linenum, char **args, int *err_code)
331{
332 return alertif_too_many_args_idx(maxarg, 0, file, linenum, args, err_code);
333}
334
Willy Tarreau61d18892009-03-31 10:49:21 +0200335
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100336/* Report it if a request ACL condition uses some keywords that are incompatible
337 * with the place where the ACL is used. It returns either 0 or ERR_WARN so that
338 * its result can be or'ed with err_code. Note that <cond> may be NULL and then
339 * will be ignored.
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100340 */
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100341int warnif_cond_conflicts(const struct acl_cond *cond, unsigned int where, const char *file, int line)
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100342{
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100343 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +0200344 const char *kw;
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100345
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100346 if (!cond)
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100347 return 0;
348
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100349 acl = acl_cond_conflicts(cond, where);
350 if (acl) {
351 if (acl->name && *acl->name)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100352 ha_warning("parsing [%s:%d] : acl '%s' will never match because it only involves keywords that are incompatible with '%s'\n",
353 file, line, acl->name, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100354 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100355 ha_warning("parsing [%s:%d] : anonymous acl will never match because it uses keyword '%s' which is incompatible with '%s'\n",
356 file, line, LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100357 return ERR_WARN;
358 }
359 if (!acl_cond_kw_conflicts(cond, where, &acl, &kw))
Willy Tarreaufdb563c2010-01-31 15:43:27 +0100360 return 0;
361
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100362 if (acl->name && *acl->name)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100363 ha_warning("parsing [%s:%d] : acl '%s' involves keywords '%s' which is incompatible with '%s'\n",
364 file, line, acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100365 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100366 ha_warning("parsing [%s:%d] : anonymous acl involves keyword '%s' which is incompatible with '%s'\n",
367 file, line, kw, sample_ckp_names(where));
Willy Tarreaufdb563c2010-01-31 15:43:27 +0100368 return ERR_WARN;
369}
370
Christopher Faulet581db2b2021-03-26 10:02:46 +0100371/* Report it if an ACL uses a L6 sample fetch from an HTTP proxy. It returns
372 * either 0 or ERR_WARN so that its result can be or'ed with err_code. Note that
373 * <cond> may be NULL and then will be ignored.
374*/
375int warnif_tcp_http_cond(const struct proxy *px, const struct acl_cond *cond)
376{
377 if (!cond || px->mode != PR_MODE_HTTP)
378 return 0;
379
380 if (cond->use & (SMP_USE_L6REQ|SMP_USE_L6RES)) {
381 ha_warning("Proxy '%s': L6 sample fetches ignored on HTTP proxies (declared at %s:%d).\n",
382 px->id, cond->file, cond->line);
383 return ERR_WARN;
384 }
385 return 0;
386}
387
Willy Tarreaue2afcc42021-03-12 09:08:04 +0100388/* try to find in <list> the word that looks closest to <word> by counting
389 * transitions between letters, digits and other characters. Will return the
390 * best matching word if found, otherwise NULL. An optional array of extra
391 * words to compare may be passed in <extra>, but it must then be terminated
392 * by a NULL entry. If unused it may be NULL.
393 */
394const char *cfg_find_best_match(const char *word, const struct list *list, int section, const char **extra)
395{
396 uint8_t word_sig[1024]; // 0..25=letter, 26=digit, 27=other, 28=begin, 29=end
397 uint8_t list_sig[1024];
398 const struct cfg_kw_list *kwl;
399 int index;
400 const char *best_ptr = NULL;
401 int dist, best_dist = INT_MAX;
402
403 make_word_fingerprint(word_sig, word);
404 list_for_each_entry(kwl, list, list) {
405 for (index = 0; kwl->kw[index].kw != NULL; index++) {
406 if (kwl->kw[index].section != section)
407 continue;
408
409 make_word_fingerprint(list_sig, kwl->kw[index].kw);
410 dist = word_fingerprint_distance(word_sig, list_sig);
411 if (dist < best_dist) {
412 best_dist = dist;
413 best_ptr = kwl->kw[index].kw;
414 }
415 }
416 }
417
418 while (extra && *extra) {
419 make_word_fingerprint(list_sig, *extra);
420 dist = word_fingerprint_distance(word_sig, list_sig);
421 if (dist < best_dist) {
422 best_dist = dist;
423 best_ptr = *extra;
424 }
425 extra++;
426 }
427
428 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
429 best_ptr = NULL;
430 return best_ptr;
431}
432
Christopher Faulet62519022017-10-16 15:49:32 +0200433/* Parse a string representing a process number or a set of processes. It must
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100434 * be "all", "odd", "even", a number between 1 and <max> or a range with
Christopher Faulet5ab51772017-11-22 11:21:58 +0100435 * two such numbers delimited by a dash ('-'). On success, it returns
436 * 0. otherwise it returns 1 with an error message in <err>.
Christopher Faulet62519022017-10-16 15:49:32 +0200437 *
438 * Note: this function can also be used to parse a thread number or a set of
439 * threads.
440 */
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100441int parse_process_number(const char *arg, unsigned long *proc, int max, int *autoinc, char **err)
Christopher Faulet62519022017-10-16 15:49:32 +0200442{
Christopher Faulet26028f62017-11-22 15:01:51 +0100443 if (autoinc) {
444 *autoinc = 0;
445 if (strncmp(arg, "auto:", 5) == 0) {
446 arg += 5;
447 *autoinc = 1;
448 }
449 }
450
Christopher Faulet62519022017-10-16 15:49:32 +0200451 if (strcmp(arg, "all") == 0)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100452 *proc |= ~0UL;
Christopher Faulet62519022017-10-16 15:49:32 +0200453 else if (strcmp(arg, "odd") == 0)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100454 *proc |= ~0UL/3UL; /* 0x555....555 */
Christopher Faulet62519022017-10-16 15:49:32 +0200455 else if (strcmp(arg, "even") == 0)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100456 *proc |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Christopher Faulet62519022017-10-16 15:49:32 +0200457 else {
Christopher Faulet18cca782019-02-07 16:29:41 +0100458 const char *p, *dash = NULL;
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100459 unsigned int low, high;
460
Christopher Faulet18cca782019-02-07 16:29:41 +0100461 for (p = arg; *p; p++) {
462 if (*p == '-' && !dash)
463 dash = p;
Willy Tarreau90807112020-02-25 08:16:33 +0100464 else if (!isdigit((unsigned char)*p)) {
Christopher Faulet18cca782019-02-07 16:29:41 +0100465 memprintf(err, "'%s' is not a valid number/range.", arg);
466 return -1;
467 }
Christopher Faulet5ab51772017-11-22 11:21:58 +0100468 }
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100469
470 low = high = str2uic(arg);
Christopher Faulet18cca782019-02-07 16:29:41 +0100471 if (dash)
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100472 high = ((!*(dash+1)) ? max : str2uic(dash + 1));
Christopher Fauletff4121f2017-11-22 16:38:49 +0100473
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100474 if (high < low) {
475 unsigned int swap = low;
476 low = high;
477 high = swap;
478 }
479
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100480 if (low < 1 || low > max || high > max) {
Christopher Fauletcb6a9452017-11-22 16:50:41 +0100481 memprintf(err, "'%s' is not a valid number/range."
482 " It supports numbers from 1 to %d.\n",
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100483 arg, max);
Christopher Faulet5ab51772017-11-22 11:21:58 +0100484 return 1;
485 }
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100486
487 for (;low <= high; low++)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100488 *proc |= 1UL << (low-1);
Christopher Faulet62519022017-10-16 15:49:32 +0200489 }
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100490 *proc &= ~0UL >> (LONGBITS - max);
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100491
Christopher Faulet5ab51772017-11-22 11:21:58 +0100492 return 0;
Christopher Faulet62519022017-10-16 15:49:32 +0200493}
494
David Carlier7e351ee2017-12-01 09:14:02 +0000495#ifdef USE_CPU_AFFINITY
Christopher Faulet62519022017-10-16 15:49:32 +0200496/* Parse cpu sets. Each CPU set is either a unique number between 0 and
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200497 * ha_cpuset_size() - 1 or a range with two such numbers delimited by a dash
Amaury Denoyellea8082352021-04-06 16:46:15 +0200498 * ('-'). If <comma_allowed> is set, each CPU set can be a list of unique
499 * numbers or ranges separated by a comma. It is also possible to specify
500 * multiple cpu numbers or ranges in distinct argument in <args>. On success,
501 * it returns 0, otherwise it returns 1 with an error message in <err>.
Christopher Faulet62519022017-10-16 15:49:32 +0200502 */
Amaury Denoyellea8082352021-04-06 16:46:15 +0200503unsigned long parse_cpu_set(const char **args, struct hap_cpuset *cpu_set,
504 int comma_allowed, char **err)
Christopher Faulet62519022017-10-16 15:49:32 +0200505{
506 int cur_arg = 0;
Amaury Denoyellea8082352021-04-06 16:46:15 +0200507 const char *arg;
Christopher Faulet62519022017-10-16 15:49:32 +0200508
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200509 ha_cpuset_zero(cpu_set);
510
Amaury Denoyellea8082352021-04-06 16:46:15 +0200511 arg = args[cur_arg];
512 while (*arg) {
513 const char *dash, *comma;
Christopher Faulet62519022017-10-16 15:49:32 +0200514 unsigned int low, high;
515
Willy Tarreau90807112020-02-25 08:16:33 +0100516 if (!isdigit((unsigned char)*args[cur_arg])) {
Amaury Denoyellea8082352021-04-06 16:46:15 +0200517 memprintf(err, "'%s' is not a CPU range.", arg);
Christopher Faulet62519022017-10-16 15:49:32 +0200518 return -1;
519 }
520
Amaury Denoyellea8082352021-04-06 16:46:15 +0200521 low = high = str2uic(arg);
522
523 comma = comma_allowed ? strchr(arg, ',') : NULL;
524 dash = strchr(arg, '-');
525
526 if (dash && (!comma || dash < comma))
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200527 high = *(dash+1) ? str2uic(dash + 1) : ha_cpuset_size() - 1;
Christopher Faulet62519022017-10-16 15:49:32 +0200528
529 if (high < low) {
530 unsigned int swap = low;
531 low = high;
532 high = swap;
533 }
534
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200535 if (high >= ha_cpuset_size()) {
536 memprintf(err, "supports CPU numbers from 0 to %d.",
537 ha_cpuset_size() - 1);
Christopher Faulet62519022017-10-16 15:49:32 +0200538 return 1;
539 }
540
541 while (low <= high)
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200542 ha_cpuset_set(cpu_set, low++);
Christopher Faulet62519022017-10-16 15:49:32 +0200543
Amaury Denoyellea8082352021-04-06 16:46:15 +0200544 /* if a comma is present, parse the rest of the arg, else
545 * skip to the next arg */
546 arg = comma ? comma + 1 : args[++cur_arg];
Christopher Faulet62519022017-10-16 15:49:32 +0200547 }
548 return 0;
549}
David Carlier7e351ee2017-12-01 09:14:02 +0000550#endif
551
Frédéric Lécaille18251032019-01-11 11:07:15 +0100552/* Allocate and initialize the frontend of a "peers" section found in
553 * file <file> at line <linenum> with <id> as ID.
554 * Return 0 if succeeded, -1 if not.
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200555 * Note that this function may be called from "default-server"
556 * or "peer" lines.
Frédéric Lécaille18251032019-01-11 11:07:15 +0100557 */
558static int init_peers_frontend(const char *file, int linenum,
559 const char *id, struct peers *peers)
560{
561 struct proxy *p;
562
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200563 if (peers->peers_fe) {
564 p = peers->peers_fe;
565 goto out;
566 }
Frédéric Lécaille9492c4e2019-01-11 11:47:12 +0100567
Frédéric Lécaille18251032019-01-11 11:07:15 +0100568 p = calloc(1, sizeof *p);
569 if (!p) {
570 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
571 return -1;
572 }
573
574 init_new_proxy(p);
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200575 peers_setup_frontend(p);
Frédéric Lécaille18251032019-01-11 11:07:15 +0100576 p->parent = peers;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200577 /* Finally store this frontend. */
578 peers->peers_fe = p;
579
580 out:
581 if (id && !p->id)
582 p->id = strdup(id);
Frédéric Lécaille1055e682018-04-26 14:35:21 +0200583 free(p->conf.file);
Frédéric Lécaille18251032019-01-11 11:07:15 +0100584 p->conf.args.file = p->conf.file = strdup(file);
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100585 if (linenum != -1)
586 p->conf.args.line = p->conf.line = linenum;
Frédéric Lécaille18251032019-01-11 11:07:15 +0100587
588 return 0;
589}
Willy Tarreauade5ec42010-01-28 19:33:49 +0100590
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100591/* Only change ->file, ->line and ->arg struct bind_conf member values
592 * if already present.
593 */
594static struct bind_conf *bind_conf_uniq_alloc(struct proxy *p,
595 const char *file, int line,
596 const char *arg, struct xprt_ops *xprt)
597{
598 struct bind_conf *bind_conf;
599
600 if (!LIST_ISEMPTY(&p->conf.bind)) {
601 bind_conf = LIST_ELEM((&p->conf.bind)->n, typeof(bind_conf), by_fe);
602 free(bind_conf->file);
603 bind_conf->file = strdup(file);
604 bind_conf->line = line;
605 if (arg) {
606 free(bind_conf->arg);
607 bind_conf->arg = strdup(arg);
608 }
609 }
610 else {
611 bind_conf = bind_conf_alloc(p, file, line, arg, xprt);
612 }
613
614 return bind_conf;
615}
616
617/*
618 * Allocate a new struct peer parsed at line <linenum> in file <file>
619 * to be added to <peers>.
620 * Returns the new allocated structure if succeeded, NULL if not.
621 */
622static struct peer *cfg_peers_add_peer(struct peers *peers,
623 const char *file, int linenum,
624 const char *id, int local)
625{
626 struct peer *p;
627
628 p = calloc(1, sizeof *p);
629 if (!p) {
630 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
631 return NULL;
632 }
633
634 /* the peers are linked backwards first */
635 peers->count++;
636 p->next = peers->remote;
637 peers->remote = p;
638 p->conf.file = strdup(file);
639 p->conf.line = linenum;
640 p->last_change = now.tv_sec;
641 p->xprt = xprt_get(XPRT_RAW);
642 p->sock_init_arg = NULL;
643 HA_SPIN_INIT(&p->lock);
644 if (id)
645 p->id = strdup(id);
646 if (local) {
647 p->local = 1;
648 peers->local = p;
649 }
650
651 return p;
652}
653
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654/*
William Lallemand51097192015-04-14 16:35:22 +0200655 * Parse a line in a <listen>, <frontend> or <backend> section.
Willy Tarreau93893792009-07-23 13:19:11 +0200656 * Returns the error code, 0 if OK, or any combination of :
657 * - ERR_ABORT: must abort ASAP
658 * - ERR_FATAL: we can continue parsing but not start the service
659 * - ERR_WARN: a warning has been emitted
660 * - ERR_ALERT: an alert has been emitted
661 * Only the two first ones can stop processing, the two others are just
662 * indicators.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200663 */
Emeric Brun32da3c42010-09-23 18:39:19 +0200664int cfg_parse_peers(const char *file, int linenum, char **args, int kwm)
665{
666 static struct peers *curpeers = NULL;
667 struct peer *newpeer = NULL;
668 const char *err;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200669 struct bind_conf *bind_conf;
670 struct listener *l;
Emeric Brun32da3c42010-09-23 18:39:19 +0200671 int err_code = 0;
Willy Tarreau902636f2013-03-10 19:44:48 +0100672 char *errmsg = NULL;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100673 static int bind_line, peer_line;
674
675 if (strcmp(args[0], "bind") == 0 || strcmp(args[0], "default-bind") == 0) {
676 int cur_arg;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100677 struct bind_conf *bind_conf;
678 struct bind_kw *kw;
Emeric Brun32da3c42010-09-23 18:39:19 +0200679
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100680 cur_arg = 1;
681
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200682 if (init_peers_frontend(file, linenum, NULL, curpeers) != 0) {
683 err_code |= ERR_ALERT | ERR_ABORT;
684 goto out;
685 }
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100686
687 bind_conf = bind_conf_uniq_alloc(curpeers->peers_fe, file, linenum,
688 NULL, xprt_get(XPRT_RAW));
689 if (*args[0] == 'b') {
690 struct listener *l;
691
692 if (peer_line) {
693 ha_alert("parsing [%s:%d] : mixing \"peer\" and \"bind\" line is forbidden\n", file, linenum);
694 err_code |= ERR_ALERT | ERR_FATAL;
695 goto out;
696 }
697
698 if (!str2listener(args[1], curpeers->peers_fe, bind_conf, file, linenum, &errmsg)) {
699 if (errmsg && *errmsg) {
700 indent_msg(&errmsg, 2);
701 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
702 }
703 else
704 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
705 file, linenum, args[0], args[1], args[2]);
706 err_code |= ERR_FATAL;
707 goto out;
708 }
709 l = LIST_ELEM(bind_conf->listeners.n, typeof(l), by_bind);
710 l->maxaccept = 1;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100711 l->accept = session_accept_fd;
712 l->analysers |= curpeers->peers_fe->fe_req_ana;
713 l->default_target = curpeers->peers_fe->default_target;
714 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
Willy Tarreau18215cb2019-02-27 16:25:28 +0100715 global.maxsock++; /* for the listening socket */
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100716
717 bind_line = 1;
718 if (cfg_peers->local) {
719 newpeer = cfg_peers->local;
720 }
721 else {
722 /* This peer is local.
723 * Note that we do not set the peer ID. This latter is initialized
724 * when parsing "peer" or "server" line.
725 */
726 newpeer = cfg_peers_add_peer(curpeers, file, linenum, NULL, 1);
727 if (!newpeer) {
728 err_code |= ERR_ALERT | ERR_ABORT;
729 goto out;
730 }
731 }
Willy Tarreau37159062020-08-27 07:48:42 +0200732 newpeer->addr = l->rx.addr;
Willy Tarreau5fc93282020-09-16 18:25:03 +0200733 newpeer->proto = l->rx.proto;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100734 cur_arg++;
735 }
736
737 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
738 int ret;
739
740 ret = kw->parse(args, cur_arg, curpeers->peers_fe, bind_conf, &errmsg);
741 err_code |= ret;
742 if (ret) {
743 if (errmsg && *errmsg) {
744 indent_msg(&errmsg, 2);
745 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
746 }
747 else
748 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
749 file, linenum, args[cur_arg]);
750 if (ret & ERR_FATAL)
751 goto out;
752 }
753 cur_arg += 1 + kw->skip;
754 }
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100755 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +0100756 const char *best = bind_find_best_kw(args[cur_arg]);
757 if (best)
758 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
759 file, linenum, args[cur_arg], cursection, best);
760 else
761 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
762 file, linenum, args[cur_arg], cursection);
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100763 err_code |= ERR_ALERT | ERR_FATAL;
764 goto out;
765 }
766 }
767 else if (strcmp(args[0], "default-server") == 0) {
768 if (init_peers_frontend(file, -1, NULL, curpeers) != 0) {
769 err_code |= ERR_ALERT | ERR_ABORT;
770 goto out;
771 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100772 err_code |= parse_server(file, linenum, args, curpeers->peers_fe, NULL,
773 SRV_PARSE_DEFAULT_SERVER|SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_INITIAL_RESOLVE);
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200774 }
Frédéric Lécailleb6f759b2019-11-05 09:57:45 +0100775 else if (strcmp(args[0], "log") == 0) {
776 if (init_peers_frontend(file, linenum, NULL, curpeers) != 0) {
777 err_code |= ERR_ALERT | ERR_ABORT;
778 goto out;
779 }
Emeric Brun9533a702021-04-02 10:13:43 +0200780 if (!parse_logsrv(args, &curpeers->peers_fe->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Frédéric Lécailleb6f759b2019-11-05 09:57:45 +0100781 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
782 err_code |= ERR_ALERT | ERR_FATAL;
783 goto out;
784 }
785 }
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200786 else if (strcmp(args[0], "peers") == 0) { /* new peers section */
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100787 /* Initialize these static variables when entering a new "peers" section*/
788 bind_line = peer_line = 0;
Willy Tarreau0dbbf312013-03-05 11:31:55 +0100789 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100790 ha_alert("parsing [%s:%d] : missing name for peers section.\n", file, linenum);
Willy Tarreau54984722014-02-16 08:20:13 +0100791 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau0dbbf312013-03-05 11:31:55 +0100792 goto out;
793 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200794
William Lallemand6e62fb62015-04-28 16:55:23 +0200795 if (alertif_too_many_args(1, file, linenum, args, &err_code))
796 goto out;
797
Emeric Brun32da3c42010-09-23 18:39:19 +0200798 err = invalid_char(args[1]);
799 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100800 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
801 file, linenum, *err, args[0], args[1]);
Willy Tarreau54984722014-02-16 08:20:13 +0100802 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau0dbbf312013-03-05 11:31:55 +0100803 goto out;
Emeric Brun32da3c42010-09-23 18:39:19 +0200804 }
805
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200806 for (curpeers = cfg_peers; curpeers != NULL; curpeers = curpeers->next) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200807 /*
808 * If there are two proxies with the same name only following
809 * combinations are allowed:
810 */
811 if (strcmp(curpeers->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100812 ha_alert("Parsing [%s:%d]: peers section '%s' has the same name as another peers section declared at %s:%d.\n",
813 file, linenum, args[1], curpeers->conf.file, curpeers->conf.line);
Willy Tarreau911fa2e2015-05-26 10:35:50 +0200814 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun32da3c42010-09-23 18:39:19 +0200815 }
816 }
817
Vincent Bernat02779b62016-04-03 13:48:43 +0200818 if ((curpeers = calloc(1, sizeof(*curpeers))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100819 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Emeric Brun32da3c42010-09-23 18:39:19 +0200820 err_code |= ERR_ALERT | ERR_ABORT;
821 goto out;
822 }
823
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200824 curpeers->next = cfg_peers;
825 cfg_peers = curpeers;
Willy Tarreau8113a5d2012-10-04 08:01:43 +0200826 curpeers->conf.file = strdup(file);
Emeric Brun32da3c42010-09-23 18:39:19 +0200827 curpeers->conf.line = linenum;
828 curpeers->last_change = now.tv_sec;
829 curpeers->id = strdup(args[1]);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +0200830 curpeers->disabled = 0;
Emeric Brun32da3c42010-09-23 18:39:19 +0200831 }
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200832 else if (strcmp(args[0], "peer") == 0 ||
833 strcmp(args[0], "server") == 0) { /* peer or server definition */
Frédéric Lécaille04636b72019-01-31 06:48:16 +0100834 int local_peer, peer;
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100835 int parse_addr = 0;
Emeric Brun32da3c42010-09-23 18:39:19 +0200836
Frédéric Lécaille04636b72019-01-31 06:48:16 +0100837 peer = *args[0] == 'p';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100838 local_peer = strcmp(args[1], localpeer) == 0;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100839 /* The local peer may have already partially been parsed on a "bind" line. */
840 if (*args[0] == 'p') {
841 if (bind_line) {
842 ha_alert("parsing [%s:%d] : mixing \"peer\" and \"bind\" line is forbidden\n", file, linenum);
843 err_code |= ERR_ALERT | ERR_FATAL;
844 goto out;
845 }
846 peer_line = 1;
847 }
848 if (cfg_peers->local && !cfg_peers->local->id && local_peer) {
849 /* The local peer has already been initialized on a "bind" line.
850 * Let's use it and store its ID.
851 */
852 newpeer = cfg_peers->local;
853 newpeer->id = strdup(localpeer);
854 }
855 else {
856 if (local_peer && cfg_peers->local) {
857 ha_alert("parsing [%s:%d] : '%s %s' : local peer name already referenced at %s:%d. %s\n",
858 file, linenum, args[0], args[1],
859 curpeers->peers_fe->conf.file, curpeers->peers_fe->conf.line, cfg_peers->local->id);
860 err_code |= ERR_FATAL;
861 goto out;
862 }
863 newpeer = cfg_peers_add_peer(curpeers, file, linenum, args[1], local_peer);
864 if (!newpeer) {
865 err_code |= ERR_ALERT | ERR_ABORT;
866 goto out;
867 }
868 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200869
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100870 /* Line number and peer ID are updated only if this peer is the local one. */
871 if (init_peers_frontend(file,
872 newpeer->local ? linenum: -1,
873 newpeer->local ? newpeer->id : NULL,
874 curpeers) != 0) {
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200875 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreaub36487e2013-03-10 18:37:42 +0100876 goto out;
877 }
Willy Tarreau2aa38802013-02-20 19:20:59 +0100878
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100879 /* This initializes curpeer->peers->peers_fe->srv.
880 * The server address is parsed only if we are parsing a "peer" line,
881 * or if we are parsing a "server" line and the current peer is not the local one.
882 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100883 parse_addr = (peer || !local_peer) ? SRV_PARSE_PARSE_ADDR : 0;
884 err_code |= parse_server(file, linenum, args, curpeers->peers_fe, NULL,
885 SRV_PARSE_IN_PEER_SECTION|parse_addr|SRV_PARSE_INITIAL_RESOLVE);
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +0200886 if (!curpeers->peers_fe->srv) {
887 /* Remove the newly allocated peer. */
888 if (newpeer != curpeers->local) {
889 struct peer *p;
890
891 p = curpeers->remote;
892 curpeers->remote = curpeers->remote->next;
893 free(p->id);
894 free(p);
895 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200896 goto out;
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +0200897 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200898
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100899 /* If the peer address has just been parsed, let's copy it to <newpeer>
900 * and initializes ->proto.
901 */
902 if (peer || !local_peer) {
903 newpeer->addr = curpeers->peers_fe->srv->addr;
904 newpeer->proto = protocol_by_family(newpeer->addr.ss_family);
905 }
906
Willy Tarreaua261e9b2016-12-22 20:44:00 +0100907 newpeer->xprt = xprt_get(XPRT_RAW);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200908 newpeer->sock_init_arg = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100909 HA_SPIN_INIT(&newpeer->lock);
Willy Tarreau26d8c592012-05-07 18:12:14 +0200910
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100911 newpeer->srv = curpeers->peers_fe->srv;
912 if (!newpeer->local)
Frédéric Lécaille6617e762018-04-25 15:13:38 +0200913 goto out;
914
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100915 /* The lines above are reserved to "peer" lines. */
916 if (*args[0] == 's')
Frédéric Lécaille4ba51982018-04-25 15:32:18 +0200917 goto out;
Emeric Brun32da3c42010-09-23 18:39:19 +0200918
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100919 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 +0100920
Frédéric Lécaille91694d52019-01-11 11:43:53 +0100921 if (!str2listener(args[2], curpeers->peers_fe, bind_conf, file, linenum, &errmsg)) {
922 if (errmsg && *errmsg) {
923 indent_msg(&errmsg, 2);
924 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 +0100925 }
Frédéric Lécaille91694d52019-01-11 11:43:53 +0100926 else
927 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
928 file, linenum, args[0], args[1], args[2]);
929 err_code |= ERR_FATAL;
930 goto out;
931 }
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100932
933 l = LIST_ELEM(bind_conf->listeners.n, typeof(l), by_bind);
934 l->maxaccept = 1;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100935 l->accept = session_accept_fd;
936 l->analysers |= curpeers->peers_fe->fe_req_ana;
937 l->default_target = curpeers->peers_fe->default_target;
938 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
Willy Tarreau18215cb2019-02-27 16:25:28 +0100939 global.maxsock++; /* for the listening socket */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100940 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100941 else if (strcmp(args[0], "table") == 0) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100942 struct stktable *t, *other;
943 char *id;
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100944 size_t prefix_len;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100945
946 /* Line number and peer ID are updated only if this peer is the local one. */
947 if (init_peers_frontend(file, -1, NULL, curpeers) != 0) {
948 err_code |= ERR_ALERT | ERR_ABORT;
949 goto out;
950 }
951
952 other = stktable_find_by_name(args[1]);
953 if (other) {
954 ha_alert("parsing [%s:%d] : stick-table name '%s' conflicts with table declared in %s '%s' at %s:%d.\n",
955 file, linenum, args[1],
956 other->proxy ? proxy_cap_str(other->proxy->cap) : "peers",
957 other->proxy ? other->id : other->peers.p->id,
958 other->conf.file, other->conf.line);
959 err_code |= ERR_ALERT | ERR_FATAL;
960 goto out;
961 }
962
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100963 /* Build the stick-table name, concatenating the "peers" section name
964 * followed by a '/' character and the table name argument.
965 */
966 chunk_reset(&trash);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100967 if (!chunk_strcpy(&trash, curpeers->id)) {
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100968 ha_alert("parsing [%s:%d]: '%s %s' : stick-table name too long.\n",
969 file, linenum, args[0], args[1]);
970 err_code |= ERR_ALERT | ERR_FATAL;
971 goto out;
972 }
973
974 prefix_len = trash.data;
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100975 if (!chunk_memcat(&trash, "/", 1) || !chunk_strcat(&trash, args[1])) {
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100976 ha_alert("parsing [%s:%d]: '%s %s' : stick-table name too long.\n",
977 file, linenum, args[0], args[1]);
978 err_code |= ERR_ALERT | ERR_FATAL;
979 goto out;
980 }
981
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100982 t = calloc(1, sizeof *t);
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100983 id = strdup(trash.area);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100984 if (!t || !id) {
985 ha_alert("parsing [%s:%d]: '%s %s' : memory allocation failed\n",
986 file, linenum, args[0], args[1]);
Eric Salama1aab9112020-09-18 11:55:17 +0200987 free(t);
988 free(id);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100989 err_code |= ERR_ALERT | ERR_FATAL;
990 goto out;
991 }
992
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100993 err_code |= parse_stick_table(file, linenum, args, t, id, id + prefix_len, curpeers);
Eric Salama1aab9112020-09-18 11:55:17 +0200994 if (err_code & ERR_FATAL) {
995 free(t);
996 free(id);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100997 goto out;
Eric Salama1aab9112020-09-18 11:55:17 +0200998 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100999
1000 stktable_store_name(t);
1001 t->next = stktables_list;
1002 stktables_list = t;
1003 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001004 else if (strcmp(args[0], "disabled") == 0) { /* disables this peers section */
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02001005 curpeers->disabled = 1;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02001006 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001007 else if (strcmp(args[0], "enabled") == 0) { /* enables this peers section (used to revert a disabled default) */
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02001008 curpeers->disabled = 0;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02001009 }
Emeric Brun32da3c42010-09-23 18:39:19 +02001010 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001011 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
Emeric Brun32da3c42010-09-23 18:39:19 +02001012 err_code |= ERR_ALERT | ERR_FATAL;
1013 goto out;
1014 }
1015
1016out:
Willy Tarreau902636f2013-03-10 19:44:48 +01001017 free(errmsg);
Emeric Brun32da3c42010-09-23 18:39:19 +02001018 return err_code;
1019}
1020
Baptiste Assmann325137d2015-04-13 23:40:55 +02001021/*
William Lallemand51097192015-04-14 16:35:22 +02001022 * Parse a line in a <listen>, <frontend> or <backend> section.
Simon Horman0d16a402015-01-30 11:22:58 +09001023 * Returns the error code, 0 if OK, or any combination of :
1024 * - ERR_ABORT: must abort ASAP
1025 * - ERR_FATAL: we can continue parsing but not start the service
1026 * - ERR_WARN: a warning has been emitted
1027 * - ERR_ALERT: an alert has been emitted
1028 * Only the two first ones can stop processing, the two others are just
1029 * indicators.
1030 */
1031int cfg_parse_mailers(const char *file, int linenum, char **args, int kwm)
1032{
1033 static struct mailers *curmailers = NULL;
1034 struct mailer *newmailer = NULL;
1035 const char *err;
1036 int err_code = 0;
1037 char *errmsg = NULL;
1038
1039 if (strcmp(args[0], "mailers") == 0) { /* new mailers section */
1040 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001041 ha_alert("parsing [%s:%d] : missing name for mailers section.\n", file, linenum);
Simon Horman0d16a402015-01-30 11:22:58 +09001042 err_code |= ERR_ALERT | ERR_ABORT;
1043 goto out;
1044 }
1045
1046 err = invalid_char(args[1]);
1047 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001048 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1049 file, linenum, *err, args[0], args[1]);
Simon Horman0d16a402015-01-30 11:22:58 +09001050 err_code |= ERR_ALERT | ERR_ABORT;
1051 goto out;
1052 }
1053
1054 for (curmailers = mailers; curmailers != NULL; curmailers = curmailers->next) {
1055 /*
1056 * If there are two proxies with the same name only following
1057 * combinations are allowed:
1058 */
1059 if (strcmp(curmailers->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001060 ha_alert("Parsing [%s:%d]: mailers section '%s' has the same name as another mailers section declared at %s:%d.\n",
1061 file, linenum, args[1], curmailers->conf.file, curmailers->conf.line);
Willy Tarreau911fa2e2015-05-26 10:35:50 +02001062 err_code |= ERR_ALERT | ERR_FATAL;
Simon Horman0d16a402015-01-30 11:22:58 +09001063 }
1064 }
1065
Vincent Bernat02779b62016-04-03 13:48:43 +02001066 if ((curmailers = calloc(1, sizeof(*curmailers))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001067 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Simon Horman0d16a402015-01-30 11:22:58 +09001068 err_code |= ERR_ALERT | ERR_ABORT;
1069 goto out;
1070 }
1071
1072 curmailers->next = mailers;
1073 mailers = curmailers;
1074 curmailers->conf.file = strdup(file);
1075 curmailers->conf.line = linenum;
1076 curmailers->id = strdup(args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001077 curmailers->timeout.mail = DEF_MAILALERTTIME;/* XXX: Would like to Skip to the next alert, if any, ASAP.
1078 * But need enough time so that timeouts don't occur
1079 * during tcp procssing. For now just us an arbitrary default. */
Simon Horman0d16a402015-01-30 11:22:58 +09001080 }
1081 else if (strcmp(args[0], "mailer") == 0) { /* mailer definition */
1082 struct sockaddr_storage *sk;
1083 int port1, port2;
1084 struct protocol *proto;
1085
1086 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001087 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1088 file, linenum, args[0]);
Simon Horman0d16a402015-01-30 11:22:58 +09001089 err_code |= ERR_ALERT | ERR_FATAL;
1090 goto out;
1091 }
1092
1093 err = invalid_char(args[1]);
1094 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001095 ha_alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1096 file, linenum, *err, args[1]);
Simon Horman0d16a402015-01-30 11:22:58 +09001097 err_code |= ERR_ALERT | ERR_FATAL;
1098 goto out;
1099 }
1100
Vincent Bernat02779b62016-04-03 13:48:43 +02001101 if ((newmailer = calloc(1, sizeof(*newmailer))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001102 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Simon Horman0d16a402015-01-30 11:22:58 +09001103 err_code |= ERR_ALERT | ERR_ABORT;
1104 goto out;
1105 }
1106
1107 /* the mailers are linked backwards first */
1108 curmailers->count++;
1109 newmailer->next = curmailers->mailer_list;
1110 curmailers->mailer_list = newmailer;
1111 newmailer->mailers = curmailers;
1112 newmailer->conf.file = strdup(file);
1113 newmailer->conf.line = linenum;
1114
1115 newmailer->id = strdup(args[1]);
1116
Willy Tarreau5fc93282020-09-16 18:25:03 +02001117 sk = str2sa_range(args[2], NULL, &port1, &port2, NULL, &proto,
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001118 &errmsg, NULL, NULL,
1119 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 +09001120 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001121 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Simon Horman0d16a402015-01-30 11:22:58 +09001122 err_code |= ERR_ALERT | ERR_FATAL;
1123 goto out;
1124 }
1125
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001126 if (proto->sock_prot != IPPROTO_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001127 ha_alert("parsing [%s:%d] : '%s %s' : TCP not supported for this address family.\n",
1128 file, linenum, args[0], args[1]);
Simon Horman0d16a402015-01-30 11:22:58 +09001129 err_code |= ERR_ALERT | ERR_FATAL;
1130 goto out;
1131 }
1132
Simon Horman0d16a402015-01-30 11:22:58 +09001133 newmailer->addr = *sk;
1134 newmailer->proto = proto;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001135 newmailer->xprt = xprt_get(XPRT_RAW);
Simon Horman0d16a402015-01-30 11:22:58 +09001136 newmailer->sock_init_arg = NULL;
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001137 }
1138 else if (strcmp(args[0], "timeout") == 0) {
1139 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001140 ha_alert("parsing [%s:%d] : '%s' expects 'mail' and <time> as arguments.\n",
1141 file, linenum, args[0]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001142 err_code |= ERR_ALERT | ERR_FATAL;
1143 goto out;
1144 }
1145 else if (strcmp(args[1], "mail") == 0) {
1146 const char *res;
1147 unsigned int timeout_mail;
1148 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001149 ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
1150 file, linenum, args[0], args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001151 err_code |= ERR_ALERT | ERR_FATAL;
1152 goto out;
1153 }
1154 res = parse_time_err(args[2], &timeout_mail, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001155 if (res == PARSE_TIME_OVER) {
1156 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
1157 file, linenum, args[2], args[0], args[1]);
1158 err_code |= ERR_ALERT | ERR_FATAL;
1159 goto out;
1160 }
1161 else if (res == PARSE_TIME_UNDER) {
1162 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
1163 file, linenum, args[2], args[0], args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001164 err_code |= ERR_ALERT | ERR_FATAL;
1165 goto out;
1166 }
Willy Tarreau9faebe32019-06-07 19:00:37 +02001167 else if (res) {
1168 ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
1169 file, linenum, *res, args[0], args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001170 err_code |= ERR_ALERT | ERR_FATAL;
1171 goto out;
1172 }
1173 curmailers->timeout.mail = timeout_mail;
1174 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001175 ha_alert("parsing [%s:%d] : '%s' expects 'mail' and <time> as arguments got '%s'.\n",
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001176 file, linenum, args[0], args[1]);
1177 err_code |= ERR_ALERT | ERR_FATAL;
1178 goto out;
1179 }
1180 }
Simon Horman0d16a402015-01-30 11:22:58 +09001181 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001182 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
Simon Horman0d16a402015-01-30 11:22:58 +09001183 err_code |= ERR_ALERT | ERR_FATAL;
1184 goto out;
1185 }
1186
1187out:
1188 free(errmsg);
1189 return err_code;
1190}
1191
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +01001192void free_email_alert(struct proxy *p)
Simon Horman9dc49962015-01-30 11:22:59 +09001193{
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001194 ha_free(&p->email_alert.mailers.name);
1195 ha_free(&p->email_alert.from);
1196 ha_free(&p->email_alert.to);
1197 ha_free(&p->email_alert.myhostname);
Simon Horman9dc49962015-01-30 11:22:59 +09001198}
1199
Willy Tarreaubaaee002006-06-26 02:48:02 +02001200
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001201int
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001202cfg_parse_netns(const char *file, int linenum, char **args, int kwm)
1203{
Willy Tarreaue5733232019-05-22 19:24:06 +02001204#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001205 const char *err;
1206 const char *item = args[0];
1207
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001208 if (strcmp(item, "namespace_list") == 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001209 return 0;
1210 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001211 else if (strcmp(item, "namespace") == 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001212 size_t idx = 1;
1213 const char *current;
1214 while (*(current = args[idx++])) {
1215 err = invalid_char(current);
1216 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001217 ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1218 file, linenum, *err, item, current);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001219 return ERR_ALERT | ERR_FATAL;
1220 }
1221
1222 if (netns_store_lookup(current, strlen(current))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001223 ha_alert("parsing [%s:%d]: Namespace '%s' is already added.\n",
1224 file, linenum, current);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001225 return ERR_ALERT | ERR_FATAL;
1226 }
1227 if (!netns_store_insert(current)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001228 ha_alert("parsing [%s:%d]: Cannot open namespace '%s'.\n",
1229 file, linenum, current);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001230 return ERR_ALERT | ERR_FATAL;
1231 }
1232 }
1233 }
1234
1235 return 0;
1236#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001237 ha_alert("parsing [%s:%d]: namespace support is not compiled in.",
1238 file, linenum);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001239 return ERR_ALERT | ERR_FATAL;
1240#endif
1241}
1242
1243int
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001244cfg_parse_users(const char *file, int linenum, char **args, int kwm)
1245{
1246
1247 int err_code = 0;
1248 const char *err;
1249
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001250 if (strcmp(args[0], "userlist") == 0) { /* new userlist */
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001251 struct userlist *newul;
1252
1253 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001254 ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1255 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001256 err_code |= ERR_ALERT | ERR_FATAL;
1257 goto out;
1258 }
William Lallemand6e62fb62015-04-28 16:55:23 +02001259 if (alertif_too_many_args(1, file, linenum, args, &err_code))
1260 goto out;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001261
1262 err = invalid_char(args[1]);
1263 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001264 ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1265 file, linenum, *err, args[0], args[1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001266 err_code |= ERR_ALERT | ERR_FATAL;
1267 goto out;
1268 }
1269
1270 for (newul = userlist; newul; newul = newul->next)
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001271 if (strcmp(newul->name, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001272 ha_warning("parsing [%s:%d]: ignoring duplicated userlist '%s'.\n",
1273 file, linenum, args[1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001274 err_code |= ERR_WARN;
1275 goto out;
1276 }
1277
Vincent Bernat02779b62016-04-03 13:48:43 +02001278 newul = calloc(1, sizeof(*newul));
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001279 if (!newul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001280 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001281 err_code |= ERR_ALERT | ERR_ABORT;
1282 goto out;
1283 }
1284
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001285 newul->name = strdup(args[1]);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001286 if (!newul->name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001287 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001288 err_code |= ERR_ALERT | ERR_ABORT;
David Carlier97880bb2016-04-08 10:35:26 +01001289 free(newul);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001290 goto out;
1291 }
1292
1293 newul->next = userlist;
1294 userlist = newul;
1295
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001296 } else if (strcmp(args[0], "group") == 0) { /* new group */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001297 int cur_arg;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001298 const char *err;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001299 struct auth_groups *ag;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001300
1301 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001302 ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1303 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001304 err_code |= ERR_ALERT | ERR_FATAL;
1305 goto out;
1306 }
1307
1308 err = invalid_char(args[1]);
1309 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001310 ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1311 file, linenum, *err, args[0], args[1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001312 err_code |= ERR_ALERT | ERR_FATAL;
1313 goto out;
1314 }
1315
William Lallemand4ac9f542015-05-28 18:03:51 +02001316 if (!userlist)
1317 goto out;
1318
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001319 for (ag = userlist->groups; ag; ag = ag->next)
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001320 if (strcmp(ag->name, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001321 ha_warning("parsing [%s:%d]: ignoring duplicated group '%s' in userlist '%s'.\n",
1322 file, linenum, args[1], userlist->name);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001323 err_code |= ERR_ALERT;
1324 goto out;
1325 }
1326
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001327 ag = calloc(1, sizeof(*ag));
1328 if (!ag) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001329 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001330 err_code |= ERR_ALERT | ERR_ABORT;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001331 goto out;
1332 }
1333
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001334 ag->name = strdup(args[1]);
David Carlier70d60452016-08-22 23:27:42 +01001335 if (!ag->name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001336 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001337 err_code |= ERR_ALERT | ERR_ABORT;
David Carlier70d60452016-08-22 23:27:42 +01001338 free(ag);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001339 goto out;
1340 }
1341
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001342 cur_arg = 2;
1343
1344 while (*args[cur_arg]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001345 if (strcmp(args[cur_arg], "users") == 0) {
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001346 ag->groupusers = strdup(args[cur_arg + 1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001347 cur_arg += 2;
1348 continue;
1349 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001350 ha_alert("parsing [%s:%d]: '%s' only supports 'users' option.\n",
1351 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001352 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier70d60452016-08-22 23:27:42 +01001353 free(ag->groupusers);
1354 free(ag->name);
1355 free(ag);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001356 goto out;
1357 }
1358 }
1359
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001360 ag->next = userlist->groups;
1361 userlist->groups = ag;
1362
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001363 } else if (strcmp(args[0], "user") == 0) { /* new user */
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001364 struct auth_users *newuser;
1365 int cur_arg;
1366
1367 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001368 ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1369 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001370 err_code |= ERR_ALERT | ERR_FATAL;
1371 goto out;
1372 }
William Lallemand4ac9f542015-05-28 18:03:51 +02001373 if (!userlist)
1374 goto out;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001375
1376 for (newuser = userlist->users; newuser; newuser = newuser->next)
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001377 if (strcmp(newuser->user, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001378 ha_warning("parsing [%s:%d]: ignoring duplicated user '%s' in userlist '%s'.\n",
1379 file, linenum, args[1], userlist->name);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001380 err_code |= ERR_ALERT;
1381 goto out;
1382 }
1383
Vincent Bernat02779b62016-04-03 13:48:43 +02001384 newuser = calloc(1, sizeof(*newuser));
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001385 if (!newuser) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001386 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001387 err_code |= ERR_ALERT | ERR_ABORT;
1388 goto out;
1389 }
1390
1391 newuser->user = strdup(args[1]);
1392
1393 newuser->next = userlist->users;
1394 userlist->users = newuser;
1395
1396 cur_arg = 2;
1397
1398 while (*args[cur_arg]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001399 if (strcmp(args[cur_arg], "password") == 0) {
Willy Tarreaue5733232019-05-22 19:24:06 +02001400#ifdef USE_LIBCRYPT
Cyril Bonté1a0191d2014-08-29 20:20:02 +02001401 if (!crypt("", args[cur_arg + 1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001402 ha_alert("parsing [%s:%d]: the encrypted password used for user '%s' is not supported by crypt(3).\n",
1403 file, linenum, newuser->user);
Cyril Bonté1a0191d2014-08-29 20:20:02 +02001404 err_code |= ERR_ALERT | ERR_FATAL;
1405 goto out;
1406 }
1407#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001408 ha_warning("parsing [%s:%d]: no crypt(3) support compiled, encrypted passwords will not work.\n",
1409 file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001410 err_code |= ERR_ALERT;
1411#endif
1412 newuser->pass = strdup(args[cur_arg + 1]);
1413 cur_arg += 2;
1414 continue;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001415 } else if (strcmp(args[cur_arg], "insecure-password") == 0) {
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001416 newuser->pass = strdup(args[cur_arg + 1]);
1417 newuser->flags |= AU_O_INSECURE;
1418 cur_arg += 2;
1419 continue;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001420 } else if (strcmp(args[cur_arg], "groups") == 0) {
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001421 newuser->u.groups_names = strdup(args[cur_arg + 1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001422 cur_arg += 2;
1423 continue;
1424 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001425 ha_alert("parsing [%s:%d]: '%s' only supports 'password', 'insecure-password' and 'groups' options.\n",
1426 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001427 err_code |= ERR_ALERT | ERR_FATAL;
1428 goto out;
1429 }
1430 }
1431 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001432 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 +01001433 err_code |= ERR_ALERT | ERR_FATAL;
1434 }
1435
1436out:
1437 return err_code;
1438}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001439
Christopher Faulet79bdef32016-11-04 22:36:15 +01001440int
1441cfg_parse_scope(const char *file, int linenum, char *line)
1442{
1443 char *beg, *end, *scope = NULL;
1444 int err_code = 0;
1445 const char *err;
1446
1447 beg = line + 1;
1448 end = strchr(beg, ']');
1449
1450 /* Detect end of scope declaration */
1451 if (!end || end == beg) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001452 ha_alert("parsing [%s:%d] : empty scope name is forbidden.\n",
1453 file, linenum);
Christopher Faulet79bdef32016-11-04 22:36:15 +01001454 err_code |= ERR_ALERT | ERR_FATAL;
1455 goto out;
1456 }
1457
1458 /* Get scope name and check its validity */
1459 scope = my_strndup(beg, end-beg);
1460 err = invalid_char(scope);
1461 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001462 ha_alert("parsing [%s:%d] : character '%c' is not permitted in a scope name.\n",
1463 file, linenum, *err);
Christopher Faulet79bdef32016-11-04 22:36:15 +01001464 err_code |= ERR_ALERT | ERR_ABORT;
1465 goto out;
1466 }
1467
1468 /* Be sure to have a scope declaration alone on its line */
1469 line = end+1;
1470 while (isspace((unsigned char)*line))
1471 line++;
1472 if (*line && *line != '#' && *line != '\n' && *line != '\r') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001473 ha_alert("parsing [%s:%d] : character '%c' is not permitted after scope declaration.\n",
1474 file, linenum, *line);
Christopher Faulet79bdef32016-11-04 22:36:15 +01001475 err_code |= ERR_ALERT | ERR_ABORT;
1476 goto out;
1477 }
1478
1479 /* We have a valid scope declaration, save it */
1480 free(cfg_scope);
1481 cfg_scope = scope;
1482 scope = NULL;
1483
1484 out:
1485 free(scope);
1486 return err_code;
1487}
1488
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01001489int
1490cfg_parse_track_sc_num(unsigned int *track_sc_num,
1491 const char *arg, const char *end, char **errmsg)
1492{
1493 const char *p;
1494 unsigned int num;
1495
1496 p = arg;
1497 num = read_uint64(&arg, end);
1498
1499 if (arg != end) {
1500 memprintf(errmsg, "Wrong track-sc number '%s'", p);
1501 return -1;
1502 }
1503
1504 if (num >= MAX_SESS_STKCTR) {
1505 memprintf(errmsg, "%u track-sc number exceeding "
1506 "%d (MAX_SESS_STKCTR-1) value", num, MAX_SESS_STKCTR - 1);
1507 return -1;
1508 }
1509
1510 *track_sc_num = num;
1511 return 0;
1512}
1513
Willy Tarreaubaaee002006-06-26 02:48:02 +02001514/*
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001515 * Detect a global section after a non-global one and output a diagnostic
1516 * warning.
1517 */
1518static void check_section_position(char *section_name,
1519 const char *file, int linenum,
1520 int *non_global_parsed)
1521{
1522 if (!strcmp(section_name, "global")) {
1523 if (*non_global_parsed == 1)
1524 _ha_diag_warning("parsing [%s:%d] : global section detected after a non-global one, the prevalence of their statements is unspecified\n", file, linenum);
1525 }
1526 else if (*non_global_parsed == 0) {
1527 *non_global_parsed = 1;
1528 }
1529}
1530
Willy Tarreau8a022d52021-04-27 20:29:11 +02001531/* apply the current default_path setting for config file <file>, and
1532 * optionally replace the current path to <origin> if not NULL while the
1533 * default-path mode is set to "origin". Errors are returned into an
1534 * allocated string passed to <err> if it's not NULL. Returns 0 on failure
1535 * or non-zero on success.
1536 */
1537static int cfg_apply_default_path(const char *file, const char *origin, char **err)
1538{
1539 const char *beg, *end;
1540
1541 /* make path start at <beg> and end before <end>, and switch it to ""
1542 * if no slash was passed.
1543 */
1544 beg = file;
1545 end = strrchr(beg, '/');
1546 if (!end)
1547 end = beg;
1548
1549 if (!*initial_cwd) {
1550 if (getcwd(initial_cwd, sizeof(initial_cwd)) == NULL) {
1551 if (err)
1552 memprintf(err, "Impossible to retrieve startup directory name: %s", strerror(errno));
1553 return 0;
1554 }
1555 }
1556 else if (chdir(initial_cwd) == -1) {
1557 if (err)
1558 memprintf(err, "Impossible to get back to initial directory '%s': %s", initial_cwd, strerror(errno));
1559 return 0;
1560 }
1561
1562 /* OK now we're (back) to initial_cwd */
1563
1564 switch (default_path_mode) {
1565 case DEFAULT_PATH_CURRENT:
1566 /* current_cwd never set, nothing to do */
1567 return 1;
1568
1569 case DEFAULT_PATH_ORIGIN:
1570 /* current_cwd set in the config */
1571 if (origin &&
1572 snprintf(current_cwd, sizeof(current_cwd), "%s", origin) > sizeof(current_cwd)) {
1573 if (err)
1574 memprintf(err, "Absolute path too long: '%s'", origin);
1575 return 0;
1576 }
1577 break;
1578
1579 case DEFAULT_PATH_CONFIG:
1580 if (end - beg >= sizeof(current_cwd)) {
1581 if (err)
1582 memprintf(err, "Config file path too long, cannot use for relative paths: '%s'", file);
1583 return 0;
1584 }
1585 memcpy(current_cwd, beg, end - beg);
1586 current_cwd[end - beg] = 0;
1587 break;
1588
1589 case DEFAULT_PATH_PARENT:
1590 if (end - beg + 3 >= sizeof(current_cwd)) {
1591 if (err)
1592 memprintf(err, "Config file path too long, cannot use for relative paths: '%s'", file);
1593 return 0;
1594 }
1595 memcpy(current_cwd, beg, end - beg);
1596 if (end > beg)
1597 memcpy(current_cwd + (end - beg), "/..\0", 4);
1598 else
1599 memcpy(current_cwd + (end - beg), "..\0", 3);
1600 break;
1601 }
1602
1603 if (*current_cwd && chdir(current_cwd) == -1) {
1604 if (err)
1605 memprintf(err, "Impossible to get back to directory '%s': %s", initial_cwd, strerror(errno));
1606 return 0;
1607 }
1608
1609 return 1;
1610}
1611
1612/* parses a global "default-path" directive. */
1613static int cfg_parse_global_def_path(char **args, int section_type, struct proxy *curpx,
1614 const struct proxy *defpx, const char *file, int line,
1615 char **err)
1616{
1617 int ret = -1;
1618
1619 /* "current", "config", "parent", "origin <path>" */
1620
1621 if (strcmp(args[1], "current") == 0)
1622 default_path_mode = DEFAULT_PATH_CURRENT;
1623 else if (strcmp(args[1], "config") == 0)
1624 default_path_mode = DEFAULT_PATH_CONFIG;
1625 else if (strcmp(args[1], "parent") == 0)
1626 default_path_mode = DEFAULT_PATH_PARENT;
1627 else if (strcmp(args[1], "origin") == 0)
1628 default_path_mode = DEFAULT_PATH_ORIGIN;
1629 else {
1630 memprintf(err, "%s default-path mode '%s' for '%s', supported modes include 'current', 'config', 'parent', and 'origin'.", *args[1] ? "unsupported" : "missing", args[1], args[0]);
1631 goto end;
1632 }
1633
1634 if (default_path_mode == DEFAULT_PATH_ORIGIN) {
1635 if (!*args[2]) {
1636 memprintf(err, "'%s %s' expects a directory as an argument.", args[0], args[1]);
1637 goto end;
1638 }
1639 if (!cfg_apply_default_path(file, args[2], err)) {
1640 memprintf(err, "couldn't set '%s' to origin '%s': %s.", args[0], args[2], *err);
1641 goto end;
1642 }
1643 }
1644 else if (!cfg_apply_default_path(file, NULL, err)) {
1645 memprintf(err, "couldn't set '%s' to '%s': %s.", args[0], args[1], *err);
1646 goto end;
1647 }
1648
1649 /* note that once applied, the path is immediately updated */
1650
1651 ret = 0;
1652 end:
1653 return ret;
1654}
1655
Willy Tarreau732525f2021-05-06 15:49:04 +02001656/* looks up a cond predicate matching the keyword in <str>, possibly followed
1657 * by a parenthesis. Returns a pointer to it or NULL if not found.
1658 */
1659static const struct cond_pred_kw *cfg_lookup_cond_pred(const char *str)
1660{
1661 const struct cond_pred_kw *ret;
1662 int len = strcspn(str, " (");
1663
1664 for (ret = &cond_predicates[0]; ret->word; ret++) {
1665 if (len != strlen(ret->word))
1666 continue;
1667 if (strncmp(str, ret->word, len) != 0)
1668 continue;
1669 return ret;
1670 }
1671 return NULL;
1672}
1673
Willy Tarreau6a2110c2021-05-06 08:19:48 +02001674/* evaluate a condition on a .if/.elif line. The condition is already tokenized
1675 * in <err>. Returns -1 on error (in which case err is filled with a message,
Willy Tarreau299bd1c2021-05-06 15:07:10 +02001676 * and only in this case), 0 if the condition is false, 1 if it's true. If
1677 * <errptr> is not NULL, it's set to the first invalid character on error.
Willy Tarreau6a2110c2021-05-06 08:19:48 +02001678 */
Willy Tarreau299bd1c2021-05-06 15:07:10 +02001679static int cfg_eval_condition(char **args, char **err, const char **errptr)
Willy Tarreau6a2110c2021-05-06 08:19:48 +02001680{
Willy Tarreau732525f2021-05-06 15:49:04 +02001681 const struct cond_pred_kw *cond_pred = NULL;
1682 const char *end_ptr;
1683 struct arg *argp = NULL;
1684 int err_arg;
1685 int nbargs;
1686 int ret = -1;
Willy Tarreau6a2110c2021-05-06 08:19:48 +02001687 char *end;
1688 long val;
1689
1690 if (!*args[0]) /* note: empty = false */
1691 return 0;
1692
1693 val = strtol(args[0], &end, 0);
1694 if (end && *end == '\0')
1695 return val != 0;
1696
Willy Tarreau732525f2021-05-06 15:49:04 +02001697 /* below we'll likely all make_arg_list() so we must return only via
1698 * the <done> label which frees the arg list.
1699 */
1700 cond_pred = cfg_lookup_cond_pred(args[0]);
1701 if (cond_pred) {
1702 nbargs = make_arg_list(args[0] + strlen(cond_pred->word), -1,
1703 cond_pred->arg_mask, &argp, err,
1704 &end_ptr, &err_arg, NULL);
1705
1706 if (nbargs < 0) {
1707 memprintf(err, "%s in argument %d of predicate '%s' used in conditional expression", *err, err_arg, cond_pred->word);
1708 if (errptr)
1709 *errptr = end_ptr;
1710 goto done;
1711 }
1712
1713 /* here we know we have a valid predicate with <nbargs> valid
1714 * arguments, placed in <argp> (which we'll need to free).
1715 */
1716 switch (cond_pred->prd) {
Willy Tarreau42ed14b2021-05-06 15:55:14 +02001717 case CFG_PRED_DEFINED: // checks if arg exists as an environment variable
1718 ret = getenv(argp[0].data.str.area) != NULL;
1719 goto done;
1720
Willy Tarreau732525f2021-05-06 15:49:04 +02001721 default:
1722 memprintf(err, "internal error: unhandled conditional expression predicate '%s'", cond_pred->word);
1723 if (errptr)
1724 *errptr = args[0];
1725 goto done;
1726 }
1727 }
1728
Willy Tarreau299bd1c2021-05-06 15:07:10 +02001729 memprintf(err, "unparsable conditional expression '%s'", args[0]);
1730 if (errptr)
1731 *errptr = args[0];
Willy Tarreau732525f2021-05-06 15:49:04 +02001732 done:
1733 for (nbargs = 0; argp && argp[nbargs].type != ARGT_STOP; nbargs++) {
1734 if (argp[nbargs].type == ARGT_STR)
1735 free(argp[nbargs].data.str.area);
1736 }
1737 free(argp);
1738 return ret;
Willy Tarreau6a2110c2021-05-06 08:19:48 +02001739}
Willy Tarreau8a022d52021-04-27 20:29:11 +02001740
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001741/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001742 * This function reads and parses the configuration file given in the argument.
Willy Tarreauda543e12021-04-27 18:30:28 +02001743 * Returns the error code, 0 if OK, -1 if the config file couldn't be opened,
1744 * or any combination of :
Willy Tarreau058e9072009-07-20 09:30:05 +02001745 * - ERR_ABORT: must abort ASAP
1746 * - ERR_FATAL: we can continue parsing but not start the service
1747 * - ERR_WARN: a warning has been emitted
1748 * - ERR_ALERT: an alert has been emitted
1749 * Only the two first ones can stop processing, the two others are just
1750 * indicators.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001751 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001752int readcfgfile(const char *file)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001753{
Willy Tarreauda543e12021-04-27 18:30:28 +02001754 char *thisline = NULL;
William Lallemand64e84512015-05-12 14:25:37 +02001755 int linesize = LINESIZE;
Willy Tarreauda543e12021-04-27 18:30:28 +02001756 FILE *f = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001757 int linenum = 0;
Willy Tarreau058e9072009-07-20 09:30:05 +02001758 int err_code = 0;
William Lallemandd2ff56d2017-10-16 11:06:50 +02001759 struct cfg_section *cs = NULL, *pcs = NULL;
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001760 struct cfg_section *ics;
William Lallemand64e84512015-05-12 14:25:37 +02001761 int readbytes = 0;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001762 char *outline = NULL;
1763 size_t outlen = 0;
1764 size_t outlinesize = 0;
Willy Tarreau32234e72020-06-16 17:14:33 +02001765 int fatal = 0;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001766 int missing_lf = -1;
Willy Tarreau4b103022021-02-12 17:59:10 +01001767 int nested_cond_lvl = 0;
1768 enum nested_cond_state nested_conds[MAXNESTEDCONDS];
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001769 int non_global_section_parsed = 0;
Willy Tarreau8a022d52021-04-27 20:29:11 +02001770 char *errmsg = NULL;
William Lallemand64e84512015-05-12 14:25:37 +02001771
Willy Tarreau51508052021-05-06 10:04:45 +02001772 global.cfg_curr_line = 0;
1773 global.cfg_curr_file = file;
1774
William Lallemand64e84512015-05-12 14:25:37 +02001775 if ((thisline = malloc(sizeof(*thisline) * linesize)) == NULL) {
Willy Tarreauda543e12021-04-27 18:30:28 +02001776 ha_alert("Out of memory trying to allocate a buffer for a configuration line.\n");
1777 err_code = -1;
1778 goto err;
William Lallemand64e84512015-05-12 14:25:37 +02001779 }
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001780
Willy Tarreauda543e12021-04-27 18:30:28 +02001781 if ((f = fopen(file,"r")) == NULL) {
1782 err_code = -1;
1783 goto err;
David Carlier97880bb2016-04-08 10:35:26 +01001784 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001785
Willy Tarreau8a022d52021-04-27 20:29:11 +02001786 /* change to the new dir if required */
1787 if (!cfg_apply_default_path(file, NULL, &errmsg)) {
1788 ha_alert("parsing [%s:%d]: failed to apply default-path: %s.\n", file, linenum, errmsg);
1789 free(errmsg);
1790 err_code = -1;
1791 goto err;
1792 }
1793
William Lallemandb2f07452015-05-12 14:27:13 +02001794next_line:
William Lallemand64e84512015-05-12 14:25:37 +02001795 while (fgets(thisline + readbytes, linesize - readbytes, f) != NULL) {
Willy Tarreau3842f002009-06-14 11:39:52 +02001796 int arg, kwm = KWM_STD;
Krzysztof Piotr Oledzkie6bbd742007-11-01 00:33:12 +01001797 char *end;
1798 char *args[MAX_LINE_ARGS + 1];
1799 char *line = thisline;
1800
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001801 if (missing_lf != -1) {
Tim Duesterhusf92afb72020-08-18 22:00:04 +02001802 ha_alert("parsing [%s:%d]: Stray NUL character at position %d.\n",
1803 file, linenum, (missing_lf + 1));
1804 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001805 missing_lf = -1;
Tim Duesterhusf92afb72020-08-18 22:00:04 +02001806 break;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001807 }
1808
Willy Tarreaubaaee002006-06-26 02:48:02 +02001809 linenum++;
Willy Tarreau51508052021-05-06 10:04:45 +02001810 global.cfg_curr_line = linenum;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001811
Willy Tarreau32234e72020-06-16 17:14:33 +02001812 if (fatal >= 50) {
1813 ha_alert("parsing [%s:%d]: too many fatal errors (%d), stopping now.\n", file, linenum, fatal);
1814 break;
1815 }
1816
Willy Tarreaubaaee002006-06-26 02:48:02 +02001817 end = line + strlen(line);
1818
William Lallemand64e84512015-05-12 14:25:37 +02001819 if (end-line == linesize-1 && *(end-1) != '\n') {
Krzysztof Piotr Oledzkie6bbd742007-11-01 00:33:12 +01001820 /* Check if we reached the limit and the last char is not \n.
1821 * Watch out for the last line without the terminating '\n'!
1822 */
William Lallemand64e84512015-05-12 14:25:37 +02001823 char *newline;
1824 int newlinesize = linesize * 2;
1825
1826 newline = realloc(thisline, sizeof(*thisline) * newlinesize);
1827 if (newline == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001828 ha_alert("parsing [%s:%d]: line too long, cannot allocate memory.\n",
1829 file, linenum);
William Lallemand64e84512015-05-12 14:25:37 +02001830 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001831 fatal++;
Willy Tarreau40cb26f2020-06-25 09:37:54 +02001832 linenum--;
William Lallemand64e84512015-05-12 14:25:37 +02001833 continue;
1834 }
1835
1836 readbytes = linesize - 1;
1837 linesize = newlinesize;
1838 thisline = newline;
Willy Tarreau40cb26f2020-06-25 09:37:54 +02001839 linenum--;
William Lallemand64e84512015-05-12 14:25:37 +02001840 continue;
Krzysztof Piotr Oledzkie6bbd742007-11-01 00:33:12 +01001841 }
1842
William Lallemand64e84512015-05-12 14:25:37 +02001843 readbytes = 0;
1844
Willy Tarreau08488f62020-06-26 17:24:54 +02001845 if (end > line && *(end-1) == '\n') {
Tim Duesterhus70f58992020-06-22 22:57:44 +02001846 /* kill trailing LF */
1847 *(end - 1) = 0;
1848 }
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001849 else {
1850 /* mark this line as truncated */
1851 missing_lf = end - line;
1852 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001853
Willy Tarreaubaaee002006-06-26 02:48:02 +02001854 /* skip leading spaces */
Willy Tarreau8f8e6452007-06-17 21:51:38 +02001855 while (isspace((unsigned char)*line))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001856 line++;
William Lallemandf9873ba2015-05-05 17:37:14 +02001857
Joseph Herlanta14c03e2018-11-15 14:04:19 -08001858 if (*line == '[') {/* This is the beginning if a scope */
Christopher Faulet79bdef32016-11-04 22:36:15 +01001859 err_code |= cfg_parse_scope(file, linenum, line);
1860 goto next_line;
1861 }
1862
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001863 while (1) {
1864 uint32_t err;
1865 char *errptr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001866
Willy Tarreau61dd44b2020-06-25 07:35:42 +02001867 arg = MAX_LINE_ARGS + 1;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001868 outlen = outlinesize;
1869 err = parse_line(line, outline, &outlen, args, &arg,
1870 PARSE_OPT_ENV | PARSE_OPT_DQUOTE | PARSE_OPT_SQUOTE |
Amaury Denoyellefa41cb62020-10-01 14:32:35 +02001871 PARSE_OPT_BKSLASH | PARSE_OPT_SHARP | PARSE_OPT_WORD_EXPAND,
1872 &errptr);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001873
1874 if (err & PARSE_ERR_QUOTE) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001875 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1876
1877 ha_alert("parsing [%s:%d]: unmatched quote at position %d:\n"
1878 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001879 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001880 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001881 goto next_line;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001882 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001883
1884 if (err & PARSE_ERR_BRACE) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001885 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1886
1887 ha_alert("parsing [%s:%d]: unmatched brace in environment variable name at position %d:\n"
1888 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001889 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001890 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001891 goto next_line;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001892 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001893
1894 if (err & PARSE_ERR_VARNAME) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001895 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1896
1897 ha_alert("parsing [%s:%d]: forbidden first char in environment variable name at position %d:\n"
1898 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001899 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001900 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001901 goto next_line;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001902 }
William Lallemandb2f07452015-05-12 14:27:13 +02001903
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001904 if (err & PARSE_ERR_HEX) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001905 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1906
1907 ha_alert("parsing [%s:%d]: truncated or invalid hexadecimal sequence at position %d:\n"
1908 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001909 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus9f658a52020-06-16 18:14:21 +02001910 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001911 goto next_line;
1912 }
William Lallemandb2f07452015-05-12 14:27:13 +02001913
Amaury Denoyellefa41cb62020-10-01 14:32:35 +02001914 if (err & PARSE_ERR_WRONG_EXPAND) {
1915 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1916
1917 ha_alert("parsing [%s:%d]: truncated or invalid word expansion sequence at position %d:\n"
1918 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
1919 err_code |= ERR_ALERT | ERR_FATAL;
1920 fatal++;
1921 goto next_line;
1922 }
1923
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001924 if (err & (PARSE_ERR_TOOLARGE|PARSE_ERR_OVERLAP)) {
1925 outlinesize = (outlen + 1023) & -1024;
Ilya Shipitsin76837bc2021-01-07 22:45:13 +05001926 outline = my_realloc2(outline, outlinesize);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001927 if (outline == NULL) {
1928 ha_alert("parsing [%s:%d]: line too long, cannot allocate memory.\n",
1929 file, linenum);
William Lallemandb2f07452015-05-12 14:27:13 +02001930 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001931 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001932 goto next_line;
William Lallemandb2f07452015-05-12 14:27:13 +02001933 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001934 /* try again */
1935 continue;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001936 }
Willy Tarreau07d47062020-06-25 07:41:22 +02001937
1938 if (err & PARSE_ERR_TOOMANY) {
1939 /* only check this *after* being sure the output is allocated */
1940 ha_alert("parsing [%s:%d]: too many words, truncating after word %d, position %ld: <%s>.\n",
1941 file, linenum, MAX_LINE_ARGS, (long)(args[MAX_LINE_ARGS-1] - outline + 1), args[MAX_LINE_ARGS-1]);
1942 err_code |= ERR_ALERT | ERR_FATAL;
1943 fatal++;
1944 goto next_line;
1945 }
1946
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001947 /* everything's OK */
1948 break;
William Lallemandf9873ba2015-05-05 17:37:14 +02001949 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001950
1951 /* empty line */
1952 if (!**args)
1953 continue;
1954
Willy Tarreau4b103022021-02-12 17:59:10 +01001955 /* check for config macros */
1956 if (*args[0] == '.') {
1957 if (strcmp(args[0], ".if") == 0) {
Willy Tarreau299bd1c2021-05-06 15:07:10 +02001958 const char *errptr = NULL;
Willy Tarreau6a2110c2021-05-06 08:19:48 +02001959 char *errmsg = NULL;
1960 int cond;
1961
Willy Tarreau4b103022021-02-12 17:59:10 +01001962 nested_cond_lvl++;
1963 if (nested_cond_lvl >= MAXNESTEDCONDS) {
1964 ha_alert("parsing [%s:%d]: too many nested '.if', max is %d.\n", file, linenum, MAXNESTEDCONDS);
1965 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1966 goto err;
1967 }
1968
Willy Tarreau6e647c92021-05-06 08:46:11 +02001969 if (nested_cond_lvl > 1 &&
1970 (nested_conds[nested_cond_lvl - 1] == NESTED_COND_IF_DROP ||
1971 nested_conds[nested_cond_lvl - 1] == NESTED_COND_IF_SKIP ||
1972 nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELIF_DROP ||
1973 nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELIF_SKIP ||
1974 nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELSE_DROP)) {
Willy Tarreau4b103022021-02-12 17:59:10 +01001975 nested_conds[nested_cond_lvl] = NESTED_COND_IF_SKIP;
Willy Tarreau6a2110c2021-05-06 08:19:48 +02001976 goto next_line;
1977 }
1978
Willy Tarreau299bd1c2021-05-06 15:07:10 +02001979 cond = cfg_eval_condition(args + 1, &errmsg, &errptr);
Willy Tarreau6a2110c2021-05-06 08:19:48 +02001980 if (cond < 0) {
Willy Tarreau299bd1c2021-05-06 15:07:10 +02001981 size_t newpos = sanitize_for_printing(args[1], errptr - args[1], 76);
1982
1983 ha_alert("parsing [%s:%d]: %s in '.if' at position %d:\n .if %s\n %*s\n",
1984 file, linenum, errmsg,
1985 (int)(errptr-args[1]+1), args[1], (int)(newpos+5), "^");
1986
Willy Tarreau6a2110c2021-05-06 08:19:48 +02001987 free(errmsg);
Willy Tarreau4b103022021-02-12 17:59:10 +01001988 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1989 goto err;
1990 }
Willy Tarreau6a2110c2021-05-06 08:19:48 +02001991
1992 if (cond)
1993 nested_conds[nested_cond_lvl] = NESTED_COND_IF_TAKE;
1994 else
1995 nested_conds[nested_cond_lvl] = NESTED_COND_IF_DROP;
1996
Willy Tarreau4b103022021-02-12 17:59:10 +01001997 goto next_line;
1998 }
1999 else if (strcmp(args[0], ".elif") == 0) {
Willy Tarreau299bd1c2021-05-06 15:07:10 +02002000 const char *errptr = NULL;
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002001 char *errmsg = NULL;
2002 int cond;
2003
Willy Tarreau4b103022021-02-12 17:59:10 +01002004 if (!nested_cond_lvl) {
2005 ha_alert("parsing [%s:%d]: lone '.elif' with no matching '.if'.\n", file, linenum);
2006 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2007 goto err;
2008 }
2009
2010 if (nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_TAKE ||
2011 nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP) {
2012 ha_alert("parsing [%s:%d]: '.elif' after '.else' is not permitted.\n", file, linenum);
2013 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2014 goto err;
2015 }
2016
2017 if (nested_conds[nested_cond_lvl] == NESTED_COND_IF_TAKE ||
2018 nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
Willy Tarreauf67ff022021-05-06 08:48:09 +02002019 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_TAKE ||
Willy Tarreau4b103022021-02-12 17:59:10 +01002020 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP) {
2021 nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_SKIP;
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002022 goto next_line;
2023 }
2024
Willy Tarreau299bd1c2021-05-06 15:07:10 +02002025 cond = cfg_eval_condition(args + 1, &errmsg, &errptr);
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002026 if (cond < 0) {
Willy Tarreau299bd1c2021-05-06 15:07:10 +02002027 size_t newpos = sanitize_for_printing(args[1], errptr - args[1], 74);
2028
2029 ha_alert("parsing [%s:%d]: %s in '.elif' at position %d:\n .elif %s\n %*s\n",
2030 file, linenum, errmsg,
2031 (int)(errptr-args[1]+1), args[1], (int)(newpos+7), "^");
2032
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002033 free(errmsg);
Willy Tarreau4b103022021-02-12 17:59:10 +01002034 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2035 goto err;
2036 }
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002037
2038 if (cond)
2039 nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_TAKE;
2040 else
2041 nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_DROP;
2042
Willy Tarreau4b103022021-02-12 17:59:10 +01002043 goto next_line;
2044 }
2045 else if (strcmp(args[0], ".else") == 0) {
2046 if (!nested_cond_lvl) {
2047 ha_alert("parsing [%s:%d]: lone '.else' with no matching '.if'.\n", file, linenum);
2048 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2049 goto err;
2050 }
2051
2052 if (nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_TAKE ||
2053 nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP) {
2054 ha_alert("parsing [%s:%d]: '.else' after '.else' is not permitted.\n", file, linenum);
2055 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2056 goto err;
2057 }
2058
2059 if (nested_conds[nested_cond_lvl] == NESTED_COND_IF_TAKE ||
2060 nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
2061 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_TAKE ||
2062 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP) {
2063 nested_conds[nested_cond_lvl] = NESTED_COND_ELSE_DROP;
2064 } else {
2065 /* otherwise we take the "else" */
2066 nested_conds[nested_cond_lvl] = NESTED_COND_ELSE_TAKE;
2067 }
2068 goto next_line;
2069 }
2070 else if (strcmp(args[0], ".endif") == 0) {
2071 if (!nested_cond_lvl) {
2072 ha_alert("parsing [%s:%d]: lone '.endif' with no matching '.if'.\n", file, linenum);
2073 err_code |= ERR_ALERT | ERR_FATAL;
2074 fatal++;
2075 break;
2076 }
2077 nested_cond_lvl--;
2078 goto next_line;
2079 }
2080 }
2081
2082 if (nested_cond_lvl &&
2083 (nested_conds[nested_cond_lvl] == NESTED_COND_IF_DROP ||
2084 nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
2085 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_DROP ||
2086 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP ||
2087 nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP)) {
2088 /* The current block is masked out by the conditions */
2089 goto next_line;
2090 }
2091
2092 /* .warning/.error/.notice */
2093 if (*args[0] == '.') {
2094 if (strcmp(args[0], ".alert") == 0) {
2095 ha_alert("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
2096 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2097 goto err;
2098 }
2099 else if (strcmp(args[0], ".warning") == 0) {
2100 ha_warning("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
2101 err_code |= ERR_WARN;
2102 goto next_line;
2103 }
2104 else if (strcmp(args[0], ".notice") == 0) {
2105 ha_notice("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
2106 goto next_line;
2107 }
2108 else {
2109 ha_alert("parsing [%s:%d]: unknown directive '%s'.\n", file, linenum, args[0]);
2110 err_code |= ERR_ALERT | ERR_FATAL;
2111 fatal++;
2112 break;
2113 }
2114 }
2115
Willy Tarreau3842f002009-06-14 11:39:52 +02002116 /* check for keyword modifiers "no" and "default" */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002117 if (strcmp(args[0], "no") == 0) {
William Lallemand0f99e342011-10-12 17:50:54 +02002118 char *tmp;
2119
Willy Tarreau3842f002009-06-14 11:39:52 +02002120 kwm = KWM_NO;
William Lallemand0f99e342011-10-12 17:50:54 +02002121 tmp = args[0];
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002122 for (arg=0; *args[arg+1]; arg++)
2123 args[arg] = args[arg+1]; // shift args after inversion
William Lallemand0f99e342011-10-12 17:50:54 +02002124 *tmp = '\0'; // fix the next arg to \0
2125 args[arg] = tmp;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002126 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002127 else if (strcmp(args[0], "default") == 0) {
Willy Tarreau3842f002009-06-14 11:39:52 +02002128 kwm = KWM_DEF;
2129 for (arg=0; *args[arg+1]; arg++)
2130 args[arg] = args[arg+1]; // shift args after inversion
2131 }
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002132
William Dauchyec730982019-10-27 20:08:10 +01002133 if (kwm != KWM_STD && strcmp(args[0], "option") != 0 &&
2134 strcmp(args[0], "log") != 0 && strcmp(args[0], "busy-polling") != 0 &&
Willy Tarreaud96f1122019-12-03 07:07:36 +01002135 strcmp(args[0], "set-dumpable") != 0 && strcmp(args[0], "strict-limits") != 0 &&
Amaury Denoyelle0f50cb92021-03-26 18:50:33 +01002136 strcmp(args[0], "insecure-fork-wanted") != 0 &&
2137 strcmp(args[0], "numa-cpu-mapping") != 0) {
William Dauchyec730982019-10-27 20:08:10 +01002138 ha_alert("parsing [%s:%d]: negation/default currently "
William Dauchy0fec3ab2019-10-27 20:08:11 +01002139 "supported only for options, log, busy-polling, "
Amaury Denoyelle0f50cb92021-03-26 18:50:33 +01002140 "set-dumpable, strict-limits, insecure-fork-wanted "
2141 "and numa-cpu-mapping.\n", file, linenum);
Willy Tarreau058e9072009-07-20 09:30:05 +02002142 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02002143 fatal++;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002144 }
2145
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01002146 /* detect section start */
2147 list_for_each_entry(ics, &sections, list) {
2148 if (strcmp(args[0], ics->section_name) == 0) {
2149 cursection = ics->section_name;
Christopher Faulet7805e2b2018-11-30 13:50:47 +01002150 pcs = cs;
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01002151 cs = ics;
Willy Tarreau51508052021-05-06 10:04:45 +02002152 free(global.cfg_curr_section);
2153 global.cfg_curr_section = strdup(*args[1] ? args[1] : args[0]);
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02002154
2155 if (global.mode & MODE_DIAG) {
2156 check_section_position(args[0], file, linenum,
2157 &non_global_section_parsed);
2158 }
2159
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01002160 break;
2161 }
Emeric Brun32da3c42010-09-23 18:39:19 +02002162 }
2163
Christopher Faulet7805e2b2018-11-30 13:50:47 +01002164 if (pcs && pcs->post_section_parser) {
Willy Tarreau32234e72020-06-16 17:14:33 +02002165 int status;
2166
2167 status = pcs->post_section_parser();
2168 err_code |= status;
2169 if (status & ERR_FATAL)
2170 fatal++;
2171
Christopher Faulet7805e2b2018-11-30 13:50:47 +01002172 if (err_code & ERR_ABORT)
2173 goto err;
Christopher Faulet7805e2b2018-11-30 13:50:47 +01002174 }
Christopher Fauletbf7a9592018-12-02 09:37:38 +01002175 pcs = NULL;
Christopher Faulet7805e2b2018-11-30 13:50:47 +01002176
William Lallemandd2ff56d2017-10-16 11:06:50 +02002177 if (!cs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002178 ha_alert("parsing [%s:%d]: unknown keyword '%s' out of section.\n", file, linenum, args[0]);
Willy Tarreau058e9072009-07-20 09:30:05 +02002179 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02002180 fatal++;
William Lallemandd2ff56d2017-10-16 11:06:50 +02002181 } else {
Willy Tarreau32234e72020-06-16 17:14:33 +02002182 int status;
2183
2184 status = cs->section_parser(file, linenum, args, kwm);
2185 err_code |= status;
2186 if (status & ERR_FATAL)
2187 fatal++;
2188
William Lallemandd2ff56d2017-10-16 11:06:50 +02002189 if (err_code & ERR_ABORT)
2190 goto err;
2191 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002192 }
William Lallemandd2ff56d2017-10-16 11:06:50 +02002193
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02002194 if (missing_lf != -1) {
Tim Duesterhusf92afb72020-08-18 22:00:04 +02002195 ha_alert("parsing [%s:%d]: Missing LF on last line, file might have been truncated at position %d.\n",
2196 file, linenum, (missing_lf + 1));
2197 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02002198 }
2199
Willy Tarreau51508052021-05-06 10:04:45 +02002200 ha_free(&global.cfg_curr_section);
Christopher Fauletbf7a9592018-12-02 09:37:38 +01002201 if (cs && cs->post_section_parser)
Christopher Faulet7805e2b2018-11-30 13:50:47 +01002202 err_code |= cs->post_section_parser();
William Lallemandd2ff56d2017-10-16 11:06:50 +02002203
Willy Tarreau4b103022021-02-12 17:59:10 +01002204 if (nested_cond_lvl) {
2205 ha_alert("parsing [%s:%d]: non-terminated '.if' block.\n", file, linenum);
2206 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2207 }
Willy Tarreau8a022d52021-04-27 20:29:11 +02002208
2209 if (*initial_cwd && chdir(initial_cwd) == -1) {
2210 ha_alert("Impossible to get back to initial directory '%s' : %s\n", initial_cwd, strerror(errno));
2211 err_code |= ERR_ALERT | ERR_FATAL;
2212 }
2213
William Lallemandd2ff56d2017-10-16 11:06:50 +02002214err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002215 ha_free(&cfg_scope);
Willy Tarreau6daf3432008-01-22 16:44:08 +01002216 cursection = NULL;
William Lallemand64e84512015-05-12 14:25:37 +02002217 free(thisline);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02002218 free(outline);
Willy Tarreau51508052021-05-06 10:04:45 +02002219 global.cfg_curr_line = 0;
2220 global.cfg_curr_file = NULL;
2221
Willy Tarreauda543e12021-04-27 18:30:28 +02002222 if (f)
2223 fclose(f);
2224
Willy Tarreau058e9072009-07-20 09:30:05 +02002225 return err_code;
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002226}
2227
Willy Tarreau64ab6072014-09-16 12:17:36 +02002228/* This function propagates processes from frontend <from> to backend <to> so
2229 * that it is always guaranteed that a backend pointed to by a frontend is
2230 * bound to all of its processes. After that, if the target is a "listen"
2231 * instance, the function recursively descends the target's own targets along
Willy Tarreau98d04852015-05-26 12:18:29 +02002232 * default_backend and use_backend rules. Since the bits are
Willy Tarreau64ab6072014-09-16 12:17:36 +02002233 * checked first to ensure that <to> is already bound to all processes of
2234 * <from>, there is no risk of looping and we ensure to follow the shortest
2235 * path to the destination.
2236 *
2237 * It is possible to set <to> to NULL for the first call so that the function
2238 * takes care of visiting the initial frontend in <from>.
2239 *
2240 * It is important to note that the function relies on the fact that all names
2241 * have already been resolved.
2242 */
2243void propagate_processes(struct proxy *from, struct proxy *to)
2244{
2245 struct switching_rule *rule;
Willy Tarreau64ab6072014-09-16 12:17:36 +02002246
2247 if (to) {
2248 /* check whether we need to go down */
2249 if (from->bind_proc &&
2250 (from->bind_proc & to->bind_proc) == from->bind_proc)
2251 return;
2252
2253 if (!from->bind_proc && !to->bind_proc)
2254 return;
2255
2256 to->bind_proc = from->bind_proc ?
2257 (to->bind_proc | from->bind_proc) : 0;
2258
2259 /* now propagate down */
2260 from = to;
2261 }
2262
Willy Tarreau8a95d8c2014-12-18 13:56:26 +01002263 if (!(from->cap & PR_CAP_FE))
Willy Tarreau64ab6072014-09-16 12:17:36 +02002264 return;
2265
Willy Tarreauc3914d42020-09-24 08:39:22 +02002266 if (from->disabled)
Willy Tarreauf6b70012014-12-18 14:00:43 +01002267 return;
2268
Willy Tarreau64ab6072014-09-16 12:17:36 +02002269 /* default_backend */
2270 if (from->defbe.be)
2271 propagate_processes(from, from->defbe.be);
2272
2273 /* use_backend */
2274 list_for_each_entry(rule, &from->switching_rules, list) {
Cyril Bonté51639692014-10-02 19:56:25 +02002275 if (rule->dynamic)
2276 continue;
Willy Tarreau64ab6072014-09-16 12:17:36 +02002277 to = rule->be.backend;
2278 propagate_processes(from, to);
2279 }
Willy Tarreau64ab6072014-09-16 12:17:36 +02002280}
2281
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002282#if defined(__linux__) && defined USE_CPU_AFFINITY
2283/* filter directory name of the pattern node<X> */
2284static int numa_filter(const struct dirent *dir)
2285{
2286 char *endptr;
2287
2288 /* dir name must start with "node" prefix */
2289 if (strncmp(dir->d_name, "node", 4))
2290 return 0;
2291
2292 /* dir name must be at least 5 characters long */
2293 if (!dir->d_name[4])
2294 return 0;
2295
2296 /* dir name must end with a numeric id */
2297 if (strtol(&dir->d_name[4], &endptr, 10) < 0 || *endptr)
2298 return 0;
2299
2300 /* all tests succeeded */
2301 return 1;
2302}
2303
2304/* Parse a linux cpu map string representing to a numeric cpu mask map
2305 * The cpu map string is a list of 4-byte hex strings separated by commas, with
2306 * most-significant byte first, one bit per cpu number.
2307 */
2308static void parse_cpumap(char *cpumap_str, struct hap_cpuset *cpu_set)
2309{
2310 unsigned long cpumap;
2311 char *start, *endptr, *comma;
2312 int i, j;
2313
2314 ha_cpuset_zero(cpu_set);
2315
2316 i = 0;
2317 do {
2318 /* reverse-search for a comma, parse the string after the comma
2319 * or at the beginning if no comma found
2320 */
2321 comma = strrchr(cpumap_str, ',');
2322 start = comma ? comma + 1 : cpumap_str;
2323
2324 cpumap = strtoul(start, &endptr, 16);
2325 for (j = 0; cpumap; cpumap >>= 1, ++j) {
2326 if (cpumap & 0x1)
2327 ha_cpuset_set(cpu_set, j + i * 32);
2328 }
2329
2330 if (comma)
2331 *comma = '\0';
2332 ++i;
2333 } while (comma);
2334}
2335
2336/* Read the first line of a file from <path> into the trash buffer.
2337 * Returns 0 on success, otherwise non-zero.
2338 */
2339static int read_file_to_trash(const char *path)
2340{
2341 FILE *file;
2342 int ret = 1;
2343
2344 file = fopen(path, "r");
2345 if (file) {
2346 if (fgets(trash.area, trash.size, file))
2347 ret = 0;
2348
2349 fclose(file);
2350 }
2351
2352 return ret;
2353}
2354
2355/* Inspect the cpu topology of the machine on startup. If a multi-socket
2356 * machine is detected, try to bind on the first node with active cpu. This is
2357 * done to prevent an impact on the overall performance when the topology of
2358 * the machine is unknown. This function is not called if one of the conditions
2359 * is met :
2360 * - a non-null nbthread directive is active
2361 * - a restrictive cpu-map directive is active
2362 * - a restrictive affinity is already applied, for example via taskset
2363 *
2364 * Returns the count of cpus selected. If no automatic binding was required or
2365 * an error occurred and the topology is unknown, 0 is returned.
2366 */
2367static int numa_detect_topology()
2368{
2369 struct dirent **node_dirlist;
2370 int node_dirlist_size;
2371
2372 struct hap_cpuset active_cpus, node_cpu_set;
2373 const char *parse_cpu_set_args[2];
2374 char cpumap_path[PATH_MAX];
2375 char *err = NULL;
2376
2377 /* node_cpu_set count is used as return value */
2378 ha_cpuset_zero(&node_cpu_set);
2379
2380 /* 1. count the sysfs node<X> directories */
Willy Tarreau07bf21c2021-04-23 19:09:16 +02002381 node_dirlist = NULL;
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002382 node_dirlist_size = scandir(NUMA_DETECT_SYSTEM_SYSFS_PATH"/node", &node_dirlist, numa_filter, alphasort);
2383 if (node_dirlist_size <= 1)
2384 goto free_scandir_entries;
2385
2386 /* 2. read and parse the list of currently online cpu */
2387 if (read_file_to_trash(NUMA_DETECT_SYSTEM_SYSFS_PATH"/cpu/online")) {
2388 ha_notice("Cannot read online CPUs list, will not try to refine binding\n");
2389 goto free_scandir_entries;
2390 }
2391
2392 parse_cpu_set_args[0] = trash.area;
2393 parse_cpu_set_args[1] = "\0";
2394 if (parse_cpu_set(parse_cpu_set_args, &active_cpus, 1, &err)) {
2395 ha_notice("Cannot read online CPUs list: '%s'. Will not try to refine binding\n", err);
2396 free(err);
2397 goto free_scandir_entries;
2398 }
2399
2400 /* 3. loop through nodes dirs and find the first one with active cpus */
2401 while (node_dirlist_size--) {
2402 const char *node = node_dirlist[node_dirlist_size]->d_name;
2403 ha_cpuset_zero(&node_cpu_set);
2404
2405 snprintf(cpumap_path, PATH_MAX, "%s/node/%s/cpumap",
2406 NUMA_DETECT_SYSTEM_SYSFS_PATH, node);
2407
2408 if (read_file_to_trash(cpumap_path)) {
2409 ha_notice("Cannot read CPUs list of '%s', will not select them to refine binding\n", node);
2410 free(node_dirlist[node_dirlist_size]);
2411 continue;
2412 }
2413
2414 parse_cpumap(trash.area, &node_cpu_set);
2415 ha_cpuset_and(&node_cpu_set, &active_cpus);
2416
2417 /* 5. set affinity on the first found node with active cpus */
2418 if (!ha_cpuset_count(&node_cpu_set)) {
2419 free(node_dirlist[node_dirlist_size]);
2420 continue;
2421 }
2422
2423 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));
2424 if (sched_setaffinity(getpid(), sizeof(node_cpu_set.cpuset), &node_cpu_set.cpuset) == -1) {
2425 ha_warning("Cannot set the cpu affinity for this multi-cpu machine\n");
2426
2427 /* clear the cpuset used as return value */
2428 ha_cpuset_zero(&node_cpu_set);
2429 }
2430
2431 free(node_dirlist[node_dirlist_size]);
2432 break;
2433 }
2434
2435 free_scandir_entries:
2436 while (node_dirlist_size-- > 0)
2437 free(node_dirlist[node_dirlist_size]);
2438 free(node_dirlist);
2439
2440 return ha_cpuset_count(&node_cpu_set);
2441}
2442#endif /* __linux__ && USE_CPU_AFFINITY */
2443
Willy Tarreaubb925012009-07-23 13:36:36 +02002444/*
2445 * Returns the error code, 0 if OK, or any combination of :
2446 * - ERR_ABORT: must abort ASAP
2447 * - ERR_FATAL: we can continue parsing but not start the service
2448 * - ERR_WARN: a warning has been emitted
2449 * - ERR_ALERT: an alert has been emitted
2450 * Only the two first ones can stop processing, the two others are just
2451 * indicators.
2452 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002453int check_config_validity()
2454{
2455 int cfgerr = 0;
2456 struct proxy *curproxy = NULL;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002457 struct stktable *t;
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002458 struct server *newsrv = NULL;
Willy Tarreaubb925012009-07-23 13:36:36 +02002459 int err_code = 0;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002460 unsigned int next_pxid = 1;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002461 struct bind_conf *bind_conf;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002462 char *err;
William Lallemand48b4bb42017-10-23 14:36:34 +02002463 struct cfg_postparser *postparser;
Emeric Brun750fe792020-12-23 16:51:12 +01002464 struct resolvers *curr_resolvers = NULL;
Amaury Denoyelle3efee652021-03-08 17:31:39 +01002465 int i;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002466
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002467 bind_conf = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002468 /*
2469 * Now, check for the integrity of all that we have collected.
2470 */
2471
2472 /* will be needed further to delay some tasks */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002473 tv_update_date(0,1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002474
Willy Tarreau193b8c62012-11-22 00:17:38 +01002475 if (!global.tune.max_http_hdr)
2476 global.tune.max_http_hdr = MAX_HTTP_HDR;
2477
2478 if (!global.tune.cookie_len)
2479 global.tune.cookie_len = CAPTURE_LEN;
2480
Stéphane Cottin23e9e932017-05-18 08:58:41 +02002481 if (!global.tune.requri_len)
2482 global.tune.requri_len = REQURI_LEN;
2483
Willy Tarreau149ab772019-01-26 14:27:06 +01002484 if (!global.nbthread) {
2485 /* nbthread not set, thus automatic. In this case, and only if
2486 * running on a single process, we enable the same number of
2487 * threads as the number of CPUs the process is bound to. This
2488 * allows to easily control the number of threads using taskset.
2489 */
2490 global.nbthread = 1;
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002491
Willy Tarreau149ab772019-01-26 14:27:06 +01002492#if defined(USE_THREAD)
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002493 if (global.nbproc == 1) {
2494 int numa_cores = 0;
2495#if defined(__linux__) && defined USE_CPU_AFFINITY
Amaury Denoyelle0f50cb92021-03-26 18:50:33 +01002496 if (global.numa_cpu_mapping && !thread_cpu_mask_forced())
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002497 numa_cores = numa_detect_topology();
2498#endif
2499 global.nbthread = numa_cores ? numa_cores :
2500 thread_cpus_enabled_at_boot;
2501 }
Willy Tarreau149ab772019-01-26 14:27:06 +01002502 all_threads_mask = nbits(global.nbthread);
2503#endif
2504 }
2505
Willy Tarreau26f6ae12019-02-02 12:56:15 +01002506 if (global.nbproc > 1 && global.nbthread > 1) {
2507 ha_alert("config : cannot enable multiple processes if multiple threads are configured. Please use either nbproc or nbthread but not both.\n");
2508 err_code |= ERR_ALERT | ERR_FATAL;
2509 goto out;
2510 }
2511
Willy Tarreaubafbe012017-11-24 17:34:44 +01002512 pool_head_requri = create_pool("requri", global.tune.requri_len , MEM_F_SHARED);
Emeric Brun96fd9262017-07-05 13:33:16 +02002513
Willy Tarreaubafbe012017-11-24 17:34:44 +01002514 pool_head_capture = create_pool("capture", global.tune.cookie_len, MEM_F_SHARED);
Willy Tarreau193b8c62012-11-22 00:17:38 +01002515
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01002516 /* Post initialisation of the users and groups lists. */
2517 err_code = userlist_postinit();
2518 if (err_code != ERR_NONE)
2519 goto out;
2520
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002521 /* first, we will invert the proxy list order */
2522 curproxy = NULL;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002523 while (proxies_list) {
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002524 struct proxy *next;
2525
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002526 next = proxies_list->next;
2527 proxies_list->next = curproxy;
2528 curproxy = proxies_list;
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002529 if (!next)
2530 break;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002531 proxies_list = next;
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002532 }
2533
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002534 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreau55ea7572007-06-17 19:56:27 +02002535 struct switching_rule *rule;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002536 struct server_rule *srule;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002537 struct sticking_rule *mrule;
Dragan Dosen1322d092015-09-22 16:05:32 +02002538 struct logsrv *tmplogsrv;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002539 unsigned int next_id;
Willy Tarreau16a21472012-11-19 12:39:59 +01002540 int nbproc;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002541
Willy Tarreau050536d2012-10-04 08:47:34 +02002542 if (curproxy->uuid < 0) {
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002543 /* proxy ID not set, use automatic numbering with first
2544 * spare entry starting with next_pxid.
2545 */
2546 next_pxid = get_next_id(&used_proxy_id, next_pxid);
2547 curproxy->conf.id.key = curproxy->uuid = next_pxid;
2548 eb32_insert(&used_proxy_id, &curproxy->conf.id);
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002549 }
Krzysztof Piotr Oledzkidf5cb9f2010-02-05 20:58:27 +01002550 next_pxid++;
2551
Willy Tarreau55ea7572007-06-17 19:56:27 +02002552
Willy Tarreauc3914d42020-09-24 08:39:22 +02002553 if (curproxy->disabled) {
Willy Tarreau02b092f2020-10-07 18:36:54 +02002554 /* ensure we don't keep listeners uselessly bound. We
2555 * can't disable their listeners yet (fdtab not
2556 * allocated yet) but let's skip them.
2557 */
Dragan Dosen7d61a332019-05-07 14:16:18 +02002558 if (curproxy->table) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002559 ha_free(&curproxy->table->peers.name);
Dragan Dosen7d61a332019-05-07 14:16:18 +02002560 curproxy->table->peers.p = NULL;
2561 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002562 continue;
2563 }
2564
Willy Tarreau102df612014-05-07 23:56:38 +02002565 /* Check multi-process mode compatibility for the current proxy */
2566
2567 if (curproxy->bind_proc) {
2568 /* an explicit bind-process was specified, let's check how many
2569 * processes remain.
2570 */
David Carliere6c39412015-07-02 07:00:17 +00002571 nbproc = my_popcountl(curproxy->bind_proc);
Willy Tarreau102df612014-05-07 23:56:38 +02002572
Willy Tarreaua38a7172019-02-02 17:11:28 +01002573 curproxy->bind_proc &= all_proc_mask;
Willy Tarreau102df612014-05-07 23:56:38 +02002574 if (!curproxy->bind_proc && nbproc == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002575 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 +02002576 curproxy->bind_proc = 1;
2577 }
2578 else if (!curproxy->bind_proc && nbproc > 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002579 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 +02002580 curproxy->bind_proc = 0;
2581 }
2582 }
2583
Willy Tarreau3d209582014-05-09 17:06:11 +02002584 /* check and reduce the bind-proc of each listener */
2585 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
2586 unsigned long mask;
2587
Willy Tarreau45a66cc2017-11-24 11:28:00 +01002588 /* HTTP frontends with "h2" as ALPN/NPN will work in
2589 * HTTP/2 and absolutely require buffers 16kB or larger.
2590 */
2591#ifdef USE_OPENSSL
2592 if (curproxy->mode == PR_MODE_HTTP && global.tune.bufsize < 16384) {
2593#ifdef OPENSSL_NPN_NEGOTIATED
2594 /* check NPN */
Willy Tarreau4db49c02018-11-11 10:36:25 +01002595 if (bind_conf->ssl_conf.npn_str && strstr(bind_conf->ssl_conf.npn_str, "\002h2")) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002596 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",
2597 curproxy->id, bind_conf->file, bind_conf->line, global.tune.bufsize);
Willy Tarreau45a66cc2017-11-24 11:28:00 +01002598 cfgerr++;
2599 }
2600#endif
2601#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2602 /* check ALPN */
Willy Tarreau4db49c02018-11-11 10:36:25 +01002603 if (bind_conf->ssl_conf.alpn_str && strstr(bind_conf->ssl_conf.alpn_str, "\002h2")) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002604 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",
2605 curproxy->id, bind_conf->file, bind_conf->line, global.tune.bufsize);
Willy Tarreau45a66cc2017-11-24 11:28:00 +01002606 cfgerr++;
2607 }
2608#endif
2609 } /* HTTP && bufsize < 16384 */
2610#endif
2611
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002612 /* detect and address thread affinity inconsistencies */
Willy Tarreaue26993c2020-09-03 07:18:55 +02002613 mask = thread_mask(bind_conf->settings.bind_thread);
Willy Tarreau3d957172019-02-02 18:00:17 +01002614 if (!(mask & all_threads_mask)) {
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002615 unsigned long new_mask = 0;
2616
2617 while (mask) {
Willy Tarreau0c026f42018-08-01 19:12:20 +02002618 new_mask |= mask & all_threads_mask;
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002619 mask >>= global.nbthread;
2620 }
2621
Willy Tarreaue26993c2020-09-03 07:18:55 +02002622 bind_conf->settings.bind_thread = new_mask;
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002623 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",
2624 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line, new_mask);
2625 }
2626
2627 /* detect process and nbproc affinity inconsistencies */
Willy Tarreaue26993c2020-09-03 07:18:55 +02002628 mask = proc_mask(bind_conf->settings.bind_proc) & proc_mask(curproxy->bind_proc);
Willy Tarreaua36b3242019-02-02 13:14:34 +01002629 if (!(mask & all_proc_mask)) {
2630 mask = proc_mask(curproxy->bind_proc) & all_proc_mask;
Willy Tarreaue26993c2020-09-03 07:18:55 +02002631 nbproc = my_popcountl(bind_conf->settings.bind_proc);
2632 bind_conf->settings.bind_proc = proc_mask(bind_conf->settings.bind_proc) & mask;
Willy Tarreau3d209582014-05-09 17:06:11 +02002633
Willy Tarreaue26993c2020-09-03 07:18:55 +02002634 if (!bind_conf->settings.bind_proc && nbproc == 1) {
Willy Tarreaua36b3242019-02-02 13:14:34 +01002635 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",
2636 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreaue26993c2020-09-03 07:18:55 +02002637 bind_conf->settings.bind_proc = mask & ~(mask - 1);
Willy Tarreaua36b3242019-02-02 13:14:34 +01002638 }
Willy Tarreaue26993c2020-09-03 07:18:55 +02002639 else if (!bind_conf->settings.bind_proc && nbproc > 1) {
Willy Tarreaua36b3242019-02-02 13:14:34 +01002640 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",
2641 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreaue26993c2020-09-03 07:18:55 +02002642 bind_conf->settings.bind_proc = 0;
Willy Tarreaua36b3242019-02-02 13:14:34 +01002643 }
Willy Tarreau3d209582014-05-09 17:06:11 +02002644 }
2645 }
2646
Willy Tarreauff01a212009-03-15 13:46:16 +01002647 switch (curproxy->mode) {
Willy Tarreauff01a212009-03-15 13:46:16 +01002648 case PR_MODE_TCP:
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002649 cfgerr += proxy_cfg_ensure_no_http(curproxy);
Willy Tarreauff01a212009-03-15 13:46:16 +01002650 break;
2651
2652 case PR_MODE_HTTP:
Willy Tarreau25320b22013-03-24 07:22:08 +01002653 curproxy->http_needed = 1;
Willy Tarreauff01a212009-03-15 13:46:16 +01002654 break;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002655
2656 case PR_MODE_CLI:
2657 cfgerr += proxy_cfg_ensure_no_http(curproxy);
2658 break;
Emeric Brun54932b42020-07-07 09:43:24 +02002659 case PR_MODE_SYSLOG:
Willy Tarreaua389c9e2020-10-07 17:49:42 +02002660 case PR_MODE_PEERS:
Emeric Brun54932b42020-07-07 09:43:24 +02002661 case PR_MODES:
2662 /* should not happen, bug gcc warn missing switch statement */
Willy Tarreaua389c9e2020-10-07 17:49:42 +02002663 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 +02002664 proxy_type_str(curproxy), curproxy->id);
2665 cfgerr++;
2666 break;
Willy Tarreauff01a212009-03-15 13:46:16 +01002667 }
2668
Willy Tarreau4975d142021-03-13 11:00:33 +01002669 if (curproxy != global.cli_fe && (curproxy->cap & PR_CAP_FE) && LIST_ISEMPTY(&curproxy->conf.listeners)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002670 ha_warning("config : %s '%s' has no 'bind' directive. Please declare it as a backend if this was intended.\n",
2671 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauf3934b82015-08-11 11:36:45 +02002672 err_code |= ERR_WARN;
2673 }
2674
Willy Tarreau77e0dae2020-10-14 15:44:27 +02002675 if (curproxy->cap & PR_CAP_BE) {
Willy Tarreauf3e49f92009-10-03 12:21:20 +02002676 if (curproxy->lbprm.algo & BE_LB_KIND) {
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002677 if (curproxy->options & PR_O_TRANSP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002678 ha_alert("config : %s '%s' cannot use both transparent and balance mode.\n",
2679 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002680 cfgerr++;
2681 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002682#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002683 else if (curproxy->srv == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002684 ha_alert("config : %s '%s' needs at least 1 server in balance mode.\n",
2685 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002686 cfgerr++;
2687 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002688#endif
Willy Tarreau1620ec32011-08-06 17:05:02 +02002689 else if (curproxy->options & PR_O_DISPATCH) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002690 ha_warning("config : dispatch address of %s '%s' will be ignored in balance mode.\n",
2691 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubb925012009-07-23 13:36:36 +02002692 err_code |= ERR_WARN;
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002693 }
2694 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02002695 else if (!(curproxy->options & (PR_O_TRANSP | PR_O_DISPATCH | PR_O_HTTP_PROXY))) {
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002696 /* If no LB algo is set in a backend, and we're not in
2697 * transparent mode, dispatch mode nor proxy mode, we
2698 * want to use balance roundrobin by default.
2699 */
2700 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2701 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002702 }
2703 }
Willy Tarreau193cf932007-09-17 10:17:23 +02002704
Willy Tarreau1620ec32011-08-06 17:05:02 +02002705 if (curproxy->options & PR_O_DISPATCH)
2706 curproxy->options &= ~(PR_O_TRANSP | PR_O_HTTP_PROXY);
2707 else if (curproxy->options & PR_O_HTTP_PROXY)
2708 curproxy->options &= ~(PR_O_DISPATCH | PR_O_TRANSP);
2709 else if (curproxy->options & PR_O_TRANSP)
2710 curproxy->options &= ~(PR_O_DISPATCH | PR_O_HTTP_PROXY);
Willy Tarreau82936582007-11-30 15:20:09 +01002711
Christopher Faulete5870d82020-04-15 11:32:03 +02002712 if ((curproxy->tcpcheck_rules.flags & TCPCHK_RULES_UNUSED_HTTP_RS)) {
2713 ha_warning("config : %s '%s' uses http-check rules without 'option httpchk', so the rules are ignored.\n",
2714 proxy_type_str(curproxy), curproxy->id);
2715 err_code |= ERR_WARN;
2716 }
2717
2718 if ((curproxy->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK &&
Christopher Fauletc0fcbe42020-06-03 19:00:42 +02002719 (curproxy->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) != TCPCHK_RULES_HTTP_CHK) {
Willy Tarreau1620ec32011-08-06 17:05:02 +02002720 if (curproxy->options & PR_O_DISABLE404) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002721 ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option httpchk').\n",
2722 "disable-on-404", proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1620ec32011-08-06 17:05:02 +02002723 err_code |= ERR_WARN;
2724 curproxy->options &= ~PR_O_DISABLE404;
2725 }
2726 if (curproxy->options2 & PR_O2_CHK_SNDST) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002727 ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option httpchk').\n",
2728 "send-state", proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1620ec32011-08-06 17:05:02 +02002729 err_code |= ERR_WARN;
2730 curproxy->options &= ~PR_O2_CHK_SNDST;
2731 }
Willy Tarreauef781042010-01-27 11:53:01 +01002732 }
2733
Simon Horman98637e52014-06-20 12:30:16 +09002734 if ((curproxy->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2735 if (!global.external_check) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002736 ha_alert("Proxy '%s' : '%s' unable to find required 'global.external-check'.\n",
2737 curproxy->id, "option external-check");
Simon Horman98637e52014-06-20 12:30:16 +09002738 cfgerr++;
2739 }
2740 if (!curproxy->check_command) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002741 ha_alert("Proxy '%s' : '%s' unable to find required 'external-check command'.\n",
2742 curproxy->id, "option external-check");
Simon Horman98637e52014-06-20 12:30:16 +09002743 cfgerr++;
2744 }
Willy Tarreaud96f1122019-12-03 07:07:36 +01002745 if (!(global.tune.options & GTUNE_INSECURE_FORK)) {
2746 ha_warning("Proxy '%s' : 'insecure-fork-wanted' not enabled in the global section, '%s' will likely fail.\n",
2747 curproxy->id, "option external-check");
2748 err_code |= ERR_WARN;
2749 }
Simon Horman98637e52014-06-20 12:30:16 +09002750 }
2751
Simon Horman64e34162015-02-06 11:11:57 +09002752 if (curproxy->email_alert.set) {
Simon Horman0ba0e4a2015-01-30 11:23:00 +09002753 if (!(curproxy->email_alert.mailers.name && curproxy->email_alert.from && curproxy->email_alert.to)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002754 ha_warning("config : 'email-alert' will be ignored for %s '%s' (the presence any of "
2755 "'email-alert from', 'email-alert level' 'email-alert mailers', "
2756 "'email-alert myhostname', or 'email-alert to' "
2757 "requires each of 'email-alert from', 'email-alert mailers' and 'email-alert to' "
2758 "to be present).\n",
2759 proxy_type_str(curproxy), curproxy->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09002760 err_code |= ERR_WARN;
2761 free_email_alert(curproxy);
2762 }
2763 if (!curproxy->email_alert.myhostname)
Cyril Bontée22bfd62015-12-04 03:07:07 +01002764 curproxy->email_alert.myhostname = strdup(hostname);
Simon Horman9dc49962015-01-30 11:22:59 +09002765 }
2766
Simon Horman98637e52014-06-20 12:30:16 +09002767 if (curproxy->check_command) {
2768 int clear = 0;
2769 if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_EXT_CHK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002770 ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option external-check').\n",
2771 "external-check command", proxy_type_str(curproxy), curproxy->id);
Simon Horman98637e52014-06-20 12:30:16 +09002772 err_code |= ERR_WARN;
2773 clear = 1;
2774 }
2775 if (curproxy->check_command[0] != '/' && !curproxy->check_path) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002776 ha_alert("Proxy '%s': '%s' does not have a leading '/' and 'external-check path' is not set.\n",
2777 curproxy->id, "external-check command");
Simon Horman98637e52014-06-20 12:30:16 +09002778 cfgerr++;
2779 }
2780 if (clear) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002781 ha_free(&curproxy->check_command);
Simon Horman98637e52014-06-20 12:30:16 +09002782 }
2783 }
2784
2785 if (curproxy->check_path) {
2786 if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_EXT_CHK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002787 ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option external-check').\n",
2788 "external-check path", proxy_type_str(curproxy), curproxy->id);
Simon Horman98637e52014-06-20 12:30:16 +09002789 err_code |= ERR_WARN;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002790 ha_free(&curproxy->check_path);
Simon Horman98637e52014-06-20 12:30:16 +09002791 }
2792 }
2793
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002794 /* if a default backend was specified, let's find it */
2795 if (curproxy->defbe.name) {
2796 struct proxy *target;
2797
Willy Tarreauafb39922015-05-26 12:04:09 +02002798 target = proxy_be_by_name(curproxy->defbe.name);
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +01002799 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002800 ha_alert("Proxy '%s': unable to find required default_backend: '%s'.\n",
2801 curproxy->id, curproxy->defbe.name);
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002802 cfgerr++;
2803 } else if (target == curproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002804 ha_alert("Proxy '%s': loop detected for default_backend: '%s'.\n",
2805 curproxy->id, curproxy->defbe.name);
Willy Tarreaubb925012009-07-23 13:36:36 +02002806 cfgerr++;
Willy Tarreauafb39922015-05-26 12:04:09 +02002807 } else if (target->mode != curproxy->mode &&
2808 !(curproxy->mode == PR_MODE_TCP && target->mode == PR_MODE_HTTP)) {
2809
Christopher Faulet767a84b2017-11-24 16:50:31 +01002810 ha_alert("%s %s '%s' (%s:%d) tries to use incompatible %s %s '%s' (%s:%d) as its default backend (see 'mode').\n",
2811 proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
2812 curproxy->conf.file, curproxy->conf.line,
2813 proxy_mode_str(target->mode), proxy_type_str(target), target->id,
2814 target->conf.file, target->conf.line);
Willy Tarreauafb39922015-05-26 12:04:09 +02002815 cfgerr++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002816 } else {
2817 free(curproxy->defbe.name);
2818 curproxy->defbe.be = target;
Willy Tarreauff678132012-02-13 14:32:34 +01002819 /* Emit a warning if this proxy also has some servers */
2820 if (curproxy->srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002821 ha_warning("In proxy '%s', the 'default_backend' rule always has precedence over the servers, which will never be used.\n",
2822 curproxy->id);
Willy Tarreauff678132012-02-13 14:32:34 +01002823 err_code |= ERR_WARN;
2824 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002825 }
2826 }
2827
Willy Tarreau55ea7572007-06-17 19:56:27 +02002828 /* find the target proxy for 'use_backend' rules */
2829 list_for_each_entry(rule, &curproxy->switching_rules, list) {
Willy Tarreau55ea7572007-06-17 19:56:27 +02002830 struct proxy *target;
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01002831 struct logformat_node *node;
2832 char *pxname;
2833
2834 /* Try to parse the string as a log format expression. If the result
2835 * of the parsing is only one entry containing a simple string, then
2836 * it's a standard string corresponding to a static rule, thus the
2837 * parsing is cancelled and be.name is restored to be resolved.
2838 */
2839 pxname = rule->be.name;
2840 LIST_INIT(&rule->be.expr);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01002841 curproxy->conf.args.ctx = ARGC_UBK;
2842 curproxy->conf.args.file = rule->file;
2843 curproxy->conf.args.line = rule->line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002844 err = NULL;
2845 if (!parse_logformat_string(pxname, curproxy, &rule->be.expr, 0, SMP_VAL_FE_HRQ_HDR, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002846 ha_alert("Parsing [%s:%d]: failed to parse use_backend rule '%s' : %s.\n",
2847 rule->file, rule->line, pxname, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002848 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01002849 cfgerr++;
2850 continue;
2851 }
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01002852 node = LIST_NEXT(&rule->be.expr, struct logformat_node *, list);
2853
2854 if (!LIST_ISEMPTY(&rule->be.expr)) {
2855 if (node->type != LOG_FMT_TEXT || node->list.n != &rule->be.expr) {
2856 rule->dynamic = 1;
2857 free(pxname);
2858 continue;
2859 }
Christopher Fauletf82ea4a2020-05-07 15:59:33 +02002860 /* Only one element in the list, a simple string: free the expression and
2861 * fall back to static rule
2862 */
Willy Tarreau2b718102021-04-21 07:32:39 +02002863 LIST_DELETE(&node->list);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01002864 free(node->arg);
2865 free(node);
2866 }
2867
2868 rule->dynamic = 0;
2869 rule->be.name = pxname;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002870
Willy Tarreauafb39922015-05-26 12:04:09 +02002871 target = proxy_be_by_name(rule->be.name);
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +01002872 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002873 ha_alert("Proxy '%s': unable to find required use_backend: '%s'.\n",
2874 curproxy->id, rule->be.name);
Willy Tarreau55ea7572007-06-17 19:56:27 +02002875 cfgerr++;
2876 } else if (target == curproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002877 ha_alert("Proxy '%s': loop detected for use_backend: '%s'.\n",
2878 curproxy->id, rule->be.name);
Willy Tarreau55ea7572007-06-17 19:56:27 +02002879 cfgerr++;
Willy Tarreauafb39922015-05-26 12:04:09 +02002880 } else if (target->mode != curproxy->mode &&
2881 !(curproxy->mode == PR_MODE_TCP && target->mode == PR_MODE_HTTP)) {
2882
Christopher Faulet767a84b2017-11-24 16:50:31 +01002883 ha_alert("%s %s '%s' (%s:%d) tries to use incompatible %s %s '%s' (%s:%d) in a 'use_backend' rule (see 'mode').\n",
2884 proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
2885 curproxy->conf.file, curproxy->conf.line,
2886 proxy_mode_str(target->mode), proxy_type_str(target), target->id,
2887 target->conf.file, target->conf.line);
Willy Tarreauafb39922015-05-26 12:04:09 +02002888 cfgerr++;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002889 } else {
Willy Tarreau35cd7342021-02-26 20:51:47 +01002890 ha_free(&rule->be.name);
Willy Tarreau55ea7572007-06-17 19:56:27 +02002891 rule->be.backend = target;
2892 }
Christopher Faulet581db2b2021-03-26 10:02:46 +01002893 err_code |= warnif_tcp_http_cond(curproxy, rule->cond);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002894 }
2895
Willy Tarreau64ab6072014-09-16 12:17:36 +02002896 /* find the target server for 'use_server' rules */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002897 list_for_each_entry(srule, &curproxy->server_rules, list) {
Jerome Magnin824186b2020-03-29 09:37:12 +02002898 struct server *target;
2899 struct logformat_node *node;
2900 char *server_name;
2901
2902 /* We try to parse the string as a log format expression. If the result of the parsing
2903 * is only one entry containing a single string, then it's a standard string corresponding
2904 * to a static rule, thus the parsing is cancelled and we fall back to setting srv.ptr.
2905 */
2906 server_name = srule->srv.name;
2907 LIST_INIT(&srule->expr);
2908 curproxy->conf.args.ctx = ARGC_USRV;
2909 err = NULL;
2910 if (!parse_logformat_string(server_name, curproxy, &srule->expr, 0, SMP_VAL_FE_HRQ_HDR, &err)) {
2911 ha_alert("Parsing [%s:%d]; use-server rule failed to parse log-format '%s' : %s.\n",
2912 srule->file, srule->line, server_name, err);
2913 free(err);
2914 cfgerr++;
2915 continue;
2916 }
2917 node = LIST_NEXT(&srule->expr, struct logformat_node *, list);
2918
2919 if (!LIST_ISEMPTY(&srule->expr)) {
2920 if (node->type != LOG_FMT_TEXT || node->list.n != &srule->expr) {
2921 srule->dynamic = 1;
2922 free(server_name);
2923 continue;
2924 }
Christopher Fauletf82ea4a2020-05-07 15:59:33 +02002925 /* Only one element in the list, a simple string: free the expression and
2926 * fall back to static rule
2927 */
Willy Tarreau2b718102021-04-21 07:32:39 +02002928 LIST_DELETE(&node->list);
Jerome Magnin824186b2020-03-29 09:37:12 +02002929 free(node->arg);
2930 free(node);
2931 }
2932
2933 srule->dynamic = 0;
2934 srule->srv.name = server_name;
2935 target = findserver(curproxy, srule->srv.name);
Christopher Faulet581db2b2021-03-26 10:02:46 +01002936 err_code |= warnif_tcp_http_cond(curproxy, srule->cond);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002937
2938 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002939 ha_alert("config : %s '%s' : unable to find server '%s' referenced in a 'use-server' rule.\n",
2940 proxy_type_str(curproxy), curproxy->id, srule->srv.name);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002941 cfgerr++;
2942 continue;
2943 }
Willy Tarreau35cd7342021-02-26 20:51:47 +01002944 ha_free(&srule->srv.name);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002945 srule->srv.ptr = target;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002946 }
2947
Emeric Brunb982a3d2010-01-04 15:45:53 +01002948 /* find the target table for 'stick' rules */
2949 list_for_each_entry(mrule, &curproxy->sticking_rules, list) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002950 struct stktable *target;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002951
Emeric Brun1d33b292010-01-04 15:47:17 +01002952 curproxy->be_req_ana |= AN_REQ_STICKING_RULES;
2953 if (mrule->flags & STK_IS_STORE)
2954 curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
2955
Emeric Brunb982a3d2010-01-04 15:45:53 +01002956 if (mrule->table.name)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002957 target = stktable_find_by_name(mrule->table.name);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002958 else
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002959 target = curproxy->table;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002960
2961 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002962 ha_alert("Proxy '%s': unable to find stick-table '%s'.\n",
Willy Tarreau508d2322020-01-24 07:19:34 +01002963 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002964 cfgerr++;
2965 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002966 else if (!stktable_compatible_sample(mrule->expr, target->type)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002967 ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
2968 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002969 cfgerr++;
2970 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002971 else if (target->proxy && curproxy->bind_proc & ~target->proxy->bind_proc) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01002972 ha_alert("Proxy '%s': stick-table '%s' referenced 'stick-store' rule not present on all processes covered by proxy '%s'.\n",
2973 curproxy->id, target->id, curproxy->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01002974 cfgerr++;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01002975 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01002976 else {
Willy Tarreau35cd7342021-02-26 20:51:47 +01002977 ha_free(&mrule->table.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002978 mrule->table.t = target;
2979 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL);
Thayne McCombs92149f92020-11-20 01:28:26 -07002980 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_KEY, NULL);
Frédéric Lécaille9c3a0ce2019-09-02 14:02:28 +02002981 if (!in_proxies_list(target->proxies_list, curproxy)) {
2982 curproxy->next_stkt_ref = target->proxies_list;
2983 target->proxies_list = curproxy;
2984 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01002985 }
Christopher Faulet581db2b2021-03-26 10:02:46 +01002986 err_code |= warnif_tcp_http_cond(curproxy, mrule->cond);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002987 }
2988
2989 /* find the target table for 'store response' rules */
2990 list_for_each_entry(mrule, &curproxy->storersp_rules, list) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002991 struct stktable *target;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002992
Emeric Brun1d33b292010-01-04 15:47:17 +01002993 curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
2994
Emeric Brunb982a3d2010-01-04 15:45:53 +01002995 if (mrule->table.name)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002996 target = stktable_find_by_name(mrule->table.name);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002997 else
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002998 target = curproxy->table;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002999
3000 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003001 ha_alert("Proxy '%s': unable to find store table '%s'.\n",
Willy Tarreau508d2322020-01-24 07:19:34 +01003002 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01003003 cfgerr++;
3004 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003005 else if (!stktable_compatible_sample(mrule->expr, target->type)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003006 ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
3007 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01003008 cfgerr++;
3009 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003010 else if (target->proxy && (curproxy->bind_proc & ~target->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01003011 ha_alert("Proxy '%s': stick-table '%s' referenced 'stick-store' rule not present on all processes covered by proxy '%s'.\n",
3012 curproxy->id, target->id, curproxy->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01003013 cfgerr++;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01003014 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01003015 else {
Willy Tarreau35cd7342021-02-26 20:51:47 +01003016 ha_free(&mrule->table.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003017 mrule->table.t = target;
3018 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL);
Thayne McCombs92149f92020-11-20 01:28:26 -07003019 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_KEY, NULL);
Frédéric Lécaille9c3a0ce2019-09-02 14:02:28 +02003020 if (!in_proxies_list(target->proxies_list, curproxy)) {
3021 curproxy->next_stkt_ref = target->proxies_list;
3022 target->proxies_list = curproxy;
3023 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01003024 }
3025 }
3026
Christopher Faulet42c6cf92021-03-25 17:19:04 +01003027 /* check validity for 'tcp-request' layer 4/5/6/7 rules */
3028 cfgerr += check_action_rules(&curproxy->tcp_req.l4_rules, curproxy, &err_code);
3029 cfgerr += check_action_rules(&curproxy->tcp_req.l5_rules, curproxy, &err_code);
3030 cfgerr += check_action_rules(&curproxy->tcp_req.inspect_rules, curproxy, &err_code);
3031 cfgerr += check_action_rules(&curproxy->tcp_rep.inspect_rules, curproxy, &err_code);
3032 cfgerr += check_action_rules(&curproxy->http_req_rules, curproxy, &err_code);
3033 cfgerr += check_action_rules(&curproxy->http_res_rules, curproxy, &err_code);
3034 cfgerr += check_action_rules(&curproxy->http_after_res_rules, curproxy, &err_code);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003035
Christopher Faulet5eef0182021-03-31 17:13:49 +02003036 /* Warn is a switch-mode http is used on a TCP listener with servers but no backend */
3037 if (!curproxy->defbe.name && LIST_ISEMPTY(&curproxy->switching_rules) && curproxy->srv) {
3038 if ((curproxy->options & PR_O_HTTP_UPG) && curproxy->mode == PR_MODE_TCP)
3039 ha_warning("Proxy '%s' : 'switch-mode http' configured for a %s %s with no backend. "
3040 "Incoming connections upgraded to HTTP cannot be routed to TCP servers\n",
3041 curproxy->id, proxy_mode_str(curproxy->mode), proxy_type_str(curproxy));
3042 }
3043
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003044 if (curproxy->table && curproxy->table->peers.name) {
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003045 struct peers *curpeers;
Emeric Brun32da3c42010-09-23 18:39:19 +02003046
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003047 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003048 if (strcmp(curpeers->id, curproxy->table->peers.name) == 0) {
Willy Tarreau35cd7342021-02-26 20:51:47 +01003049 ha_free(&curproxy->table->peers.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003050 curproxy->table->peers.p = curpeers;
Emeric Brun32da3c42010-09-23 18:39:19 +02003051 break;
3052 }
3053 }
3054
3055 if (!curpeers) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003056 ha_alert("Proxy '%s': unable to find sync peers '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003057 curproxy->id, curproxy->table->peers.name);
Willy Tarreau35cd7342021-02-26 20:51:47 +01003058 ha_free(&curproxy->table->peers.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003059 curproxy->table->peers.p = NULL;
Emeric Brun32da3c42010-09-23 18:39:19 +02003060 cfgerr++;
3061 }
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003062 else if (curpeers->disabled) {
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003063 /* silently disable this peers section */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003064 curproxy->table->peers.p = NULL;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003065 }
Emeric Brun32da3c42010-09-23 18:39:19 +02003066 else if (!curpeers->peers_fe) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003067 ha_alert("Proxy '%s': unable to find local peer '%s' in peers section '%s'.\n",
3068 curproxy->id, localpeer, curpeers->id);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003069 curproxy->table->peers.p = NULL;
Emeric Brun32da3c42010-09-23 18:39:19 +02003070 cfgerr++;
3071 }
3072 }
3073
Simon Horman9dc49962015-01-30 11:22:59 +09003074
3075 if (curproxy->email_alert.mailers.name) {
3076 struct mailers *curmailers = mailers;
3077
3078 for (curmailers = mailers; curmailers; curmailers = curmailers->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003079 if (strcmp(curmailers->id, curproxy->email_alert.mailers.name) == 0)
Simon Horman9dc49962015-01-30 11:22:59 +09003080 break;
Simon Horman9dc49962015-01-30 11:22:59 +09003081 }
Simon Horman9dc49962015-01-30 11:22:59 +09003082 if (!curmailers) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003083 ha_alert("Proxy '%s': unable to find mailers '%s'.\n",
3084 curproxy->id, curproxy->email_alert.mailers.name);
Simon Horman9dc49962015-01-30 11:22:59 +09003085 free_email_alert(curproxy);
3086 cfgerr++;
3087 }
Christopher Faulet0108bb32017-10-20 21:34:32 +02003088 else {
3089 err = NULL;
3090 if (init_email_alert(curmailers, curproxy, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003091 ha_alert("Proxy '%s': %s.\n", curproxy->id, err);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003092 free(err);
3093 cfgerr++;
3094 }
3095 }
Simon Horman9dc49962015-01-30 11:22:59 +09003096 }
3097
Willy Tarreau5bbc6762021-02-12 11:49:25 +01003098 if (curproxy->uri_auth && !(curproxy->uri_auth->flags & STAT_CONVDONE) &&
Willy Tarreauff011f22011-01-06 17:51:27 +01003099 !LIST_ISEMPTY(&curproxy->uri_auth->http_req_rules) &&
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003100 (curproxy->uri_auth->userlist || curproxy->uri_auth->auth_realm )) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003101 ha_alert("%s '%s': stats 'auth'/'realm' and 'http-request' can't be used at the same time.\n",
3102 "proxy", curproxy->id);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003103 cfgerr++;
3104 goto out_uri_auth_compat;
3105 }
3106
Willy Tarreauee4f5f82019-10-09 09:59:22 +02003107 if (curproxy->uri_auth && curproxy->uri_auth->userlist &&
Willy Tarreau5bbc6762021-02-12 11:49:25 +01003108 (!(curproxy->uri_auth->flags & STAT_CONVDONE) ||
Willy Tarreauee4f5f82019-10-09 09:59:22 +02003109 LIST_ISEMPTY(&curproxy->uri_auth->http_req_rules))) {
Willy Tarreau95fa4692010-02-01 13:05:50 +01003110 const char *uri_auth_compat_req[10];
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003111 struct act_rule *rule;
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003112 i = 0;
3113
Willy Tarreau95fa4692010-02-01 13:05:50 +01003114 /* build the ACL condition from scratch. We're relying on anonymous ACLs for that */
3115 uri_auth_compat_req[i++] = "auth";
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003116
3117 if (curproxy->uri_auth->auth_realm) {
Willy Tarreau95fa4692010-02-01 13:05:50 +01003118 uri_auth_compat_req[i++] = "realm";
3119 uri_auth_compat_req[i++] = curproxy->uri_auth->auth_realm;
3120 }
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003121
Willy Tarreau95fa4692010-02-01 13:05:50 +01003122 uri_auth_compat_req[i++] = "unless";
3123 uri_auth_compat_req[i++] = "{";
3124 uri_auth_compat_req[i++] = "http_auth(.internal-stats-userlist)";
3125 uri_auth_compat_req[i++] = "}";
3126 uri_auth_compat_req[i++] = "";
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003127
Willy Tarreauff011f22011-01-06 17:51:27 +01003128 rule = parse_http_req_cond(uri_auth_compat_req, "internal-stats-auth-compat", 0, curproxy);
3129 if (!rule) {
Willy Tarreau95fa4692010-02-01 13:05:50 +01003130 cfgerr++;
3131 break;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003132 }
3133
Willy Tarreau2b718102021-04-21 07:32:39 +02003134 LIST_APPEND(&curproxy->uri_auth->http_req_rules, &rule->list);
Willy Tarreau95fa4692010-02-01 13:05:50 +01003135
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003136 if (curproxy->uri_auth->auth_realm) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003137 ha_free(&curproxy->uri_auth->auth_realm);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003138 }
Willy Tarreau5bbc6762021-02-12 11:49:25 +01003139 curproxy->uri_auth->flags |= STAT_CONVDONE;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003140 }
3141out_uri_auth_compat:
3142
Dragan Dosen43885c72015-10-01 13:18:13 +02003143 /* check whether we have a log server that uses RFC5424 log format */
Dragan Dosen1322d092015-09-22 16:05:32 +02003144 list_for_each_entry(tmplogsrv, &curproxy->logsrvs, list) {
Dragan Dosen43885c72015-10-01 13:18:13 +02003145 if (tmplogsrv->format == LOG_FORMAT_RFC5424) {
3146 if (!curproxy->conf.logformat_sd_string) {
3147 /* set the default logformat_sd_string */
3148 curproxy->conf.logformat_sd_string = default_rfc5424_sd_log_format;
3149 }
Dragan Dosen1322d092015-09-22 16:05:32 +02003150 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02003151 }
Dragan Dosen1322d092015-09-22 16:05:32 +02003152 }
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02003153
Willy Tarreaua4312fa2013-04-02 16:34:32 +02003154 /* compile the log format */
3155 if (!(curproxy->cap & PR_CAP_FE)) {
Willy Tarreau62a61232013-04-12 18:13:46 +02003156 if (curproxy->conf.logformat_string != default_http_log_format &&
3157 curproxy->conf.logformat_string != default_tcp_log_format &&
3158 curproxy->conf.logformat_string != clf_http_log_format)
3159 free(curproxy->conf.logformat_string);
3160 curproxy->conf.logformat_string = NULL;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003161 ha_free(&curproxy->conf.lfs_file);
Willy Tarreau62a61232013-04-12 18:13:46 +02003162 curproxy->conf.lfs_line = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003163
3164 if (curproxy->conf.logformat_sd_string != default_rfc5424_sd_log_format)
3165 free(curproxy->conf.logformat_sd_string);
3166 curproxy->conf.logformat_sd_string = NULL;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003167 ha_free(&curproxy->conf.lfsd_file);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003168 curproxy->conf.lfsd_line = 0;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02003169 }
3170
Willy Tarreau62a61232013-04-12 18:13:46 +02003171 if (curproxy->conf.logformat_string) {
3172 curproxy->conf.args.ctx = ARGC_LOG;
3173 curproxy->conf.args.file = curproxy->conf.lfs_file;
3174 curproxy->conf.args.line = curproxy->conf.lfs_line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003175 err = NULL;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +02003176 if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat,
3177 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003178 SMP_VAL_FE_LOG_END, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003179 ha_alert("Parsing [%s:%d]: failed to parse log-format : %s.\n",
3180 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003181 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01003182 cfgerr++;
3183 }
Willy Tarreau62a61232013-04-12 18:13:46 +02003184 curproxy->conf.args.file = NULL;
3185 curproxy->conf.args.line = 0;
3186 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02003187
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003188 if (curproxy->conf.logformat_sd_string) {
3189 curproxy->conf.args.ctx = ARGC_LOGSD;
3190 curproxy->conf.args.file = curproxy->conf.lfsd_file;
3191 curproxy->conf.args.line = curproxy->conf.lfsd_line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003192 err = NULL;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +02003193 if (!parse_logformat_string(curproxy->conf.logformat_sd_string, curproxy, &curproxy->logformat_sd,
3194 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003195 SMP_VAL_FE_LOG_END, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003196 ha_alert("Parsing [%s:%d]: failed to parse log-format-sd : %s.\n",
3197 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003198 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01003199 cfgerr++;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003200 } else if (!add_to_logformat_list(NULL, NULL, LF_SEPARATOR, &curproxy->logformat_sd, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003201 ha_alert("Parsing [%s:%d]: failed to parse log-format-sd : %s.\n",
3202 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003203 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01003204 cfgerr++;
3205 }
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003206 curproxy->conf.args.file = NULL;
3207 curproxy->conf.args.line = 0;
3208 }
3209
Willy Tarreau62a61232013-04-12 18:13:46 +02003210 if (curproxy->conf.uniqueid_format_string) {
3211 curproxy->conf.args.ctx = ARGC_UIF;
3212 curproxy->conf.args.file = curproxy->conf.uif_file;
3213 curproxy->conf.args.line = curproxy->conf.uif_line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003214 err = NULL;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +02003215 if (!parse_logformat_string(curproxy->conf.uniqueid_format_string, curproxy, &curproxy->format_unique_id,
3216 LOG_OPT_HTTP|LOG_OPT_MERGE_SPACES,
3217 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR
3218 : SMP_VAL_BE_HRQ_HDR,
3219 &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003220 ha_alert("Parsing [%s:%d]: failed to parse unique-id : %s.\n",
3221 curproxy->conf.uif_file, curproxy->conf.uif_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003222 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01003223 cfgerr++;
3224 }
Willy Tarreau62a61232013-04-12 18:13:46 +02003225 curproxy->conf.args.file = NULL;
3226 curproxy->conf.args.line = 0;
3227 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02003228
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01003229 /* only now we can check if some args remain unresolved.
3230 * This must be done after the users and groups resolution.
3231 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01003232 err = NULL;
3233 i = smp_resolve_args(curproxy, &err);
3234 cfgerr += i;
3235 if (i) {
3236 indent_msg(&err, 8);
3237 ha_alert("%s%s\n", i > 1 ? "multiple argument resolution errors:" : "", err);
3238 ha_free(&err);
3239 } else
Willy Tarreaua4312fa2013-04-02 16:34:32 +02003240 cfgerr += acl_find_targets(curproxy);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01003241
Willy Tarreau2738a142006-07-08 17:28:09 +02003242 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003243 (((curproxy->cap & PR_CAP_FE) && !curproxy->timeout.client) ||
Willy Tarreaud825eef2007-05-12 22:35:00 +02003244 ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) &&
Willy Tarreauce887fd2012-05-12 12:50:00 +02003245 (!curproxy->timeout.connect ||
3246 (!curproxy->timeout.server && (curproxy->mode == PR_MODE_HTTP || !curproxy->timeout.tunnel)))))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003247 ha_warning("config : missing timeouts for %s '%s'.\n"
3248 " | While not properly invalid, you will certainly encounter various problems\n"
3249 " | with such a configuration. To fix this, please ensure that all following\n"
3250 " | timeouts are set to a non-zero value: 'client', 'connect', 'server'.\n",
3251 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubb925012009-07-23 13:36:36 +02003252 err_code |= ERR_WARN;
Willy Tarreau2738a142006-07-08 17:28:09 +02003253 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02003254
Willy Tarreau1fa31262007-12-03 00:36:16 +01003255 /* Historically, the tarpit and queue timeouts were inherited from contimeout.
3256 * We must still support older configurations, so let's find out whether those
3257 * parameters have been set or must be copied from contimeouts.
3258 */
Willy Tarreau937c3ea2021-02-12 11:14:35 +01003259 if (!curproxy->timeout.tarpit)
3260 curproxy->timeout.tarpit = curproxy->timeout.connect;
3261 if ((curproxy->cap & PR_CAP_BE) && !curproxy->timeout.queue)
3262 curproxy->timeout.queue = curproxy->timeout.connect;
Willy Tarreau1fa31262007-12-03 00:36:16 +01003263
Christopher Faulete5870d82020-04-15 11:32:03 +02003264 if ((curproxy->tcpcheck_rules.flags & TCPCHK_RULES_UNUSED_TCP_RS)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003265 ha_warning("config : %s '%s' uses tcp-check rules without 'option tcp-check', so the rules are ignored.\n",
3266 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau215663d2014-06-13 18:30:23 +02003267 err_code |= ERR_WARN;
3268 }
3269
Willy Tarreau193b8c62012-11-22 00:17:38 +01003270 /* ensure that cookie capture length is not too large */
3271 if (curproxy->capture_len >= global.tune.cookie_len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003272 ha_warning("config : truncating capture length to %d bytes for %s '%s'.\n",
3273 global.tune.cookie_len - 1, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau193b8c62012-11-22 00:17:38 +01003274 err_code |= ERR_WARN;
3275 curproxy->capture_len = global.tune.cookie_len - 1;
3276 }
3277
Willy Tarreaucf7f3202007-05-13 22:46:04 +02003278 /* The small pools required for the capture lists */
Willy Tarreau9a54e132012-03-24 08:33:05 +01003279 if (curproxy->nb_req_cap) {
Willy Tarreaud9ed3d22014-06-13 12:23:06 +02003280 curproxy->req_cap_pool = create_pool("ptrcap",
3281 curproxy->nb_req_cap * sizeof(char *),
3282 MEM_F_SHARED);
Willy Tarreau9a54e132012-03-24 08:33:05 +01003283 }
3284
3285 if (curproxy->nb_rsp_cap) {
Willy Tarreaud9ed3d22014-06-13 12:23:06 +02003286 curproxy->rsp_cap_pool = create_pool("ptrcap",
3287 curproxy->nb_rsp_cap * sizeof(char *),
3288 MEM_F_SHARED);
Willy Tarreau9a54e132012-03-24 08:33:05 +01003289 }
Willy Tarreaucf7f3202007-05-13 22:46:04 +02003290
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003291 switch (curproxy->load_server_state_from_file) {
3292 case PR_SRV_STATE_FILE_UNSPEC:
3293 curproxy->load_server_state_from_file = PR_SRV_STATE_FILE_NONE;
3294 break;
3295 case PR_SRV_STATE_FILE_GLOBAL:
3296 if (!global.server_state_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003297 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",
3298 curproxy->id);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003299 err_code |= ERR_WARN;
3300 }
3301 break;
3302 }
3303
Willy Tarreaubaaee002006-06-26 02:48:02 +02003304 /* first, we will invert the servers list order */
3305 newsrv = NULL;
3306 while (curproxy->srv) {
3307 struct server *next;
3308
3309 next = curproxy->srv->next;
3310 curproxy->srv->next = newsrv;
3311 newsrv = curproxy->srv;
3312 if (!next)
3313 break;
3314 curproxy->srv = next;
3315 }
3316
Willy Tarreau17edc812014-01-03 12:14:34 +01003317 /* Check that no server name conflicts. This causes trouble in the stats.
3318 * We only emit a warning for the first conflict affecting each server,
3319 * in order to avoid combinatory explosion if all servers have the same
3320 * name. We do that only for servers which do not have an explicit ID,
3321 * because these IDs were made also for distinguishing them and we don't
3322 * want to annoy people who correctly manage them.
3323 */
3324 for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
3325 struct server *other_srv;
3326
3327 if (newsrv->puid)
3328 continue;
3329
3330 for (other_srv = curproxy->srv; other_srv && other_srv != newsrv; other_srv = other_srv->next) {
3331 if (!other_srv->puid && strcmp(other_srv->id, newsrv->id) == 0) {
Willy Tarreaub01302f2019-05-27 19:31:06 +02003332 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 +01003333 newsrv->conf.file, newsrv->conf.line,
3334 proxy_type_str(curproxy), curproxy->id,
3335 newsrv->id, other_srv->conf.line);
Willy Tarreaub01302f2019-05-27 19:31:06 +02003336 cfgerr++;
Willy Tarreau17edc812014-01-03 12:14:34 +01003337 break;
3338 }
3339 }
3340 }
3341
Willy Tarreaudd701652010-05-25 23:03:02 +02003342 /* assign automatic UIDs to servers which don't have one yet */
3343 next_id = 1;
3344 newsrv = curproxy->srv;
3345 while (newsrv != NULL) {
3346 if (!newsrv->puid) {
3347 /* server ID not set, use automatic numbering with first
3348 * spare entry starting with next_svid.
3349 */
3350 next_id = get_next_id(&curproxy->conf.used_server_id, next_id);
3351 newsrv->conf.id.key = newsrv->puid = next_id;
3352 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Frédéric Lécaille84d60462019-05-17 14:29:15 +02003353 newsrv->conf.name.key = newsrv->id;
3354 ebis_insert(&curproxy->conf.used_server_name, &newsrv->conf.name);
Willy Tarreaudd701652010-05-25 23:03:02 +02003355 }
3356 next_id++;
3357 newsrv = newsrv->next;
3358 }
3359
Willy Tarreau20697042007-11-15 23:26:18 +01003360 curproxy->lbprm.wmult = 1; /* default weight multiplier */
Willy Tarreau5dc2fa62007-11-19 19:10:18 +01003361 curproxy->lbprm.wdiv = 1; /* default weight divider */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003362
Willy Tarreau62c3be22012-01-20 13:12:32 +01003363 /*
3364 * If this server supports a maxconn parameter, it needs a dedicated
3365 * tasks to fill the emptied slots when a connection leaves.
3366 * Also, resolve deferred tracking dependency if needed.
3367 */
3368 newsrv = curproxy->srv;
3369 while (newsrv != NULL) {
3370 if (newsrv->minconn > newsrv->maxconn) {
3371 /* Only 'minconn' was specified, or it was higher than or equal
3372 * to 'maxconn'. Let's turn this into maxconn and clean it, as
3373 * this will avoid further useless expensive computations.
3374 */
3375 newsrv->maxconn = newsrv->minconn;
3376 } else if (newsrv->maxconn && !newsrv->minconn) {
3377 /* minconn was not specified, so we set it to maxconn */
3378 newsrv->minconn = newsrv->maxconn;
3379 }
3380
William Dauchyf6370442020-11-14 19:25:33 +01003381 /* this will also properly set the transport layer for
3382 * prod and checks
3383 * if default-server have use_ssl, prerare ssl init
3384 * without activating it */
3385 if (newsrv->use_ssl == 1 || newsrv->check.use_ssl == 1 ||
3386 (newsrv->proxy->options & PR_O_TCPCHK_SSL) ||
3387 (newsrv->use_ssl != 1 && curproxy->defsrv.use_ssl == 1)) {
Willy Tarreau17d45382016-12-22 21:16:08 +01003388 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv)
3389 cfgerr += xprt_get(XPRT_SSL)->prepare_srv(newsrv);
3390 }
Emeric Brun94324a42012-10-11 14:00:19 +02003391
Willy Tarreau034c88c2017-01-23 23:36:45 +01003392 if ((newsrv->flags & SRV_F_FASTOPEN) &&
3393 ((curproxy->retry_type & (PR_RE_DISCONNECTED | PR_RE_TIMEOUT)) !=
3394 (PR_RE_DISCONNECTED | PR_RE_TIMEOUT)))
3395 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",
3396 newsrv->conf.file, newsrv->conf.line,
3397 proxy_type_str(curproxy), curproxy->id,
3398 newsrv->id);
3399
Willy Tarreau62c3be22012-01-20 13:12:32 +01003400 if (newsrv->trackit) {
3401 struct proxy *px;
Willy Tarreau32091232014-05-16 13:52:00 +02003402 struct server *srv, *loop;
Willy Tarreau62c3be22012-01-20 13:12:32 +01003403 char *pname, *sname;
3404
3405 pname = newsrv->trackit;
3406 sname = strrchr(pname, '/');
3407
3408 if (sname)
3409 *sname++ = '\0';
3410 else {
3411 sname = pname;
3412 pname = NULL;
3413 }
3414
3415 if (pname) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02003416 px = proxy_be_by_name(pname);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003417 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003418 ha_alert("config : %s '%s', server '%s': unable to find required proxy '%s' for tracking.\n",
3419 proxy_type_str(curproxy), curproxy->id,
3420 newsrv->id, pname);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003421 cfgerr++;
3422 goto next_srv;
3423 }
3424 } else
3425 px = curproxy;
3426
3427 srv = findserver(px, sname);
3428 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003429 ha_alert("config : %s '%s', server '%s': unable to find required server '%s' for tracking.\n",
3430 proxy_type_str(curproxy), curproxy->id,
3431 newsrv->id, sname);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003432 cfgerr++;
3433 goto next_srv;
3434 }
3435
Christopher Faulet8892e5d2020-03-26 19:48:20 +01003436 if (!srv->do_check && !srv->do_agent && !srv->track && !srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003437 ha_alert("config : %s '%s', server '%s': unable to use %s/%s for "
3438 "tracking as it does not have any check nor agent enabled.\n",
3439 proxy_type_str(curproxy), curproxy->id,
3440 newsrv->id, px->id, srv->id);
Willy Tarreau32091232014-05-16 13:52:00 +02003441 cfgerr++;
3442 goto next_srv;
3443 }
3444
3445 for (loop = srv->track; loop && loop != newsrv; loop = loop->track);
3446
Frédéric Lécaille2efc6492017-03-14 14:32:17 +01003447 if (newsrv == srv || loop) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003448 ha_alert("config : %s '%s', server '%s': unable to track %s/%s as it "
3449 "belongs to a tracking chain looping back to %s/%s.\n",
3450 proxy_type_str(curproxy), curproxy->id,
3451 newsrv->id, px->id, srv->id, px->id,
3452 newsrv == srv ? srv->id : loop->id);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003453 cfgerr++;
3454 goto next_srv;
3455 }
3456
3457 if (curproxy != px &&
3458 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003459 ha_alert("config : %s '%s', server '%s': unable to use %s/%s for"
3460 "tracking: disable-on-404 option inconsistency.\n",
3461 proxy_type_str(curproxy), curproxy->id,
3462 newsrv->id, px->id, srv->id);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003463 cfgerr++;
3464 goto next_srv;
3465 }
3466
Willy Tarreau62c3be22012-01-20 13:12:32 +01003467 newsrv->track = srv;
Willy Tarreau1a53a3a2013-12-11 15:27:05 +01003468 newsrv->tracknext = srv->trackers;
3469 srv->trackers = newsrv;
Willy Tarreau62c3be22012-01-20 13:12:32 +01003470
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003471 ha_free(&newsrv->trackit);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003472 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003473
Willy Tarreau62c3be22012-01-20 13:12:32 +01003474 next_srv:
3475 newsrv = newsrv->next;
3476 }
3477
Olivier Houchard4e694042017-03-14 20:01:29 +01003478 /*
3479 * Try to generate dynamic cookies for servers now.
3480 * It couldn't be done earlier, since at the time we parsed
3481 * the server line, we may not have known yet that we
3482 * should use dynamic cookies, or the secret key may not
3483 * have been provided yet.
3484 */
3485 if (curproxy->ck_opts & PR_CK_DYNAMIC) {
3486 newsrv = curproxy->srv;
3487 while (newsrv != NULL) {
3488 srv_set_dyncookie(newsrv);
3489 newsrv = newsrv->next;
3490 }
3491
3492 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003493 /* We have to initialize the server lookup mechanism depending
Joseph Herlanta14c03e2018-11-15 14:04:19 -08003494 * on what LB algorithm was chosen.
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003495 */
3496
3497 curproxy->lbprm.algo &= ~(BE_LB_LKUP | BE_LB_PROP_DYN);
3498 switch (curproxy->lbprm.algo & BE_LB_KIND) {
3499 case BE_LB_KIND_RR:
Willy Tarreau9757a382009-10-03 12:56:50 +02003500 if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_RR_STATIC) {
3501 curproxy->lbprm.algo |= BE_LB_LKUP_MAP;
3502 init_server_map(curproxy);
Willy Tarreau760e81d2018-05-03 07:20:40 +02003503 } else if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_RR_RANDOM) {
3504 curproxy->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
3505 chash_init_server_tree(curproxy);
Willy Tarreau9757a382009-10-03 12:56:50 +02003506 } else {
3507 curproxy->lbprm.algo |= BE_LB_LKUP_RRTREE | BE_LB_PROP_DYN;
3508 fwrr_init_server_groups(curproxy);
3509 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003510 break;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003511
Willy Tarreau3ebb1162012-02-13 16:57:44 +01003512 case BE_LB_KIND_CB:
Willy Tarreauf09c6602012-02-13 17:12:08 +01003513 if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_CB_LC) {
3514 curproxy->lbprm.algo |= BE_LB_LKUP_LCTREE | BE_LB_PROP_DYN;
3515 fwlc_init_server_tree(curproxy);
3516 } else {
3517 curproxy->lbprm.algo |= BE_LB_LKUP_FSTREE | BE_LB_PROP_DYN;
3518 fas_init_server_tree(curproxy);
3519 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003520 break;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003521
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003522 case BE_LB_KIND_HI:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003523 if ((curproxy->lbprm.algo & BE_LB_HASH_TYPE) == BE_LB_HASH_CONS) {
3524 curproxy->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
3525 chash_init_server_tree(curproxy);
3526 } else {
3527 curproxy->lbprm.algo |= BE_LB_LKUP_MAP;
3528 init_server_map(curproxy);
3529 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003530 break;
3531 }
Willy Tarreaucd10def2020-10-17 18:48:47 +02003532 HA_RWLOCK_INIT(&curproxy->lbprm.lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003533
3534 if (curproxy->options & PR_O_LOGASAP)
3535 curproxy->to_log &= ~LW_BYTES;
3536
Willy Tarreaue7ded1f2009-08-09 10:11:45 +02003537 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003538 (curproxy->cap & PR_CAP_FE) && LIST_ISEMPTY(&curproxy->logsrvs) &&
3539 (!LIST_ISEMPTY(&curproxy->logformat) || !LIST_ISEMPTY(&curproxy->logformat_sd))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003540 ha_warning("config : log format ignored for %s '%s' since it has no log address.\n",
3541 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue7ded1f2009-08-09 10:11:45 +02003542 err_code |= ERR_WARN;
3543 }
3544
Christopher Faulet3b6446f2021-03-15 15:10:38 +01003545 if (curproxy->mode != PR_MODE_HTTP && !(curproxy->options & PR_O_HTTP_UPG)) {
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003546 int optnum;
3547
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003548 if (curproxy->uri_auth) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003549 ha_warning("config : 'stats' statement ignored for %s '%s' as it requires HTTP mode.\n",
3550 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003551 err_code |= ERR_WARN;
3552 curproxy->uri_auth = NULL;
3553 }
3554
Willy Tarreaude7dc882017-03-10 11:49:21 +01003555 if (curproxy->capture_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003556 ha_warning("config : 'capture' statement ignored for %s '%s' as it requires HTTP mode.\n",
3557 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003558 err_code |= ERR_WARN;
3559 }
3560
3561 if (!LIST_ISEMPTY(&curproxy->http_req_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003562 ha_warning("config : 'http-request' rules ignored for %s '%s' as they require HTTP mode.\n",
3563 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003564 err_code |= ERR_WARN;
3565 }
3566
3567 if (!LIST_ISEMPTY(&curproxy->http_res_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003568 ha_warning("config : 'http-response' rules ignored for %s '%s' as they require HTTP mode.\n",
3569 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003570 err_code |= ERR_WARN;
3571 }
3572
Christopher Fauletbb7abed2021-03-15 15:09:21 +01003573 if (!LIST_ISEMPTY(&curproxy->http_after_res_rules)) {
3574 ha_warning("config : 'http-after-response' rules ignored for %s '%s' as they require HTTP mode.\n",
3575 proxy_type_str(curproxy), curproxy->id);
3576 err_code |= ERR_WARN;
3577 }
3578
Willy Tarreaude7dc882017-03-10 11:49:21 +01003579 if (!LIST_ISEMPTY(&curproxy->redirect_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003580 ha_warning("config : 'redirect' rules ignored for %s '%s' as they require HTTP mode.\n",
3581 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003582 err_code |= ERR_WARN;
3583 }
3584
Willy Tarreau87cf5142011-08-19 22:57:24 +02003585 if (curproxy->options & (PR_O_FWDFOR | PR_O_FF_ALWAYS)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003586 ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3587 "forwardfor", proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003588 err_code |= ERR_WARN;
Willy Tarreau87cf5142011-08-19 22:57:24 +02003589 curproxy->options &= ~(PR_O_FWDFOR | PR_O_FF_ALWAYS);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003590 }
3591
3592 if (curproxy->options & PR_O_ORGTO) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003593 ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3594 "originalto", proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003595 err_code |= ERR_WARN;
3596 curproxy->options &= ~PR_O_ORGTO;
3597 }
3598
3599 for (optnum = 0; cfg_opts[optnum].name; optnum++) {
3600 if (cfg_opts[optnum].mode == PR_MODE_HTTP &&
3601 (curproxy->cap & cfg_opts[optnum].cap) &&
3602 (curproxy->options & cfg_opts[optnum].val)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003603 ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3604 cfg_opts[optnum].name, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003605 err_code |= ERR_WARN;
3606 curproxy->options &= ~cfg_opts[optnum].val;
3607 }
3608 }
3609
3610 for (optnum = 0; cfg_opts2[optnum].name; optnum++) {
3611 if (cfg_opts2[optnum].mode == PR_MODE_HTTP &&
3612 (curproxy->cap & cfg_opts2[optnum].cap) &&
3613 (curproxy->options2 & cfg_opts2[optnum].val)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003614 ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3615 cfg_opts2[optnum].name, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003616 err_code |= ERR_WARN;
3617 curproxy->options2 &= ~cfg_opts2[optnum].val;
3618 }
3619 }
Willy Tarreaubce70882009-09-07 11:51:47 +02003620
Willy Tarreau29fbe512015-08-20 19:35:14 +02003621#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreauef9a3602012-12-08 22:29:20 +01003622 if (curproxy->conn_src.bind_hdr_occ) {
3623 curproxy->conn_src.bind_hdr_occ = 0;
Christopher Faulet767a84b2017-11-24 16:50:31 +01003624 ha_warning("config : %s '%s' : ignoring use of header %s as source IP in non-HTTP mode.\n",
3625 proxy_type_str(curproxy), curproxy->id, curproxy->conn_src.bind_hdr_name);
Willy Tarreaubce70882009-09-07 11:51:47 +02003626 err_code |= ERR_WARN;
3627 }
Willy Tarreauefa5f512010-03-30 20:13:29 +02003628#endif
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003629 }
3630
Willy Tarreaubaaee002006-06-26 02:48:02 +02003631 /*
Willy Tarreau21d2af32008-02-14 20:25:24 +01003632 * ensure that we're not cross-dressing a TCP server into HTTP.
3633 */
3634 newsrv = curproxy->srv;
3635 while (newsrv != NULL) {
Willy Tarreau0cec3312011-10-31 13:49:26 +01003636 if ((curproxy->mode != PR_MODE_HTTP) && newsrv->rdr_len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003637 ha_alert("config : %s '%s' : server cannot have cookie or redirect prefix in non-HTTP mode.\n",
3638 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubb925012009-07-23 13:36:36 +02003639 cfgerr++;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003640 }
Willy Tarreaubce70882009-09-07 11:51:47 +02003641
Willy Tarreau0cec3312011-10-31 13:49:26 +01003642 if ((curproxy->mode != PR_MODE_HTTP) && newsrv->cklen) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003643 ha_warning("config : %s '%s' : ignoring cookie for server '%s' as HTTP mode is disabled.\n",
3644 proxy_type_str(curproxy), curproxy->id, newsrv->id);
Willy Tarreau0cec3312011-10-31 13:49:26 +01003645 err_code |= ERR_WARN;
3646 }
3647
Willy Tarreauc93cd162014-05-13 15:54:22 +02003648 if ((newsrv->flags & SRV_F_MAPPORTS) && (curproxy->options2 & PR_O2_RDPC_PRST)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003649 ha_warning("config : %s '%s' : RDP cookie persistence will not work for server '%s' because it lacks an explicit port number.\n",
3650 proxy_type_str(curproxy), curproxy->id, newsrv->id);
Willy Tarreau82ffa392013-08-13 17:19:08 +02003651 err_code |= ERR_WARN;
3652 }
3653
Willy Tarreau29fbe512015-08-20 19:35:14 +02003654#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreauef9a3602012-12-08 22:29:20 +01003655 if (curproxy->mode != PR_MODE_HTTP && newsrv->conn_src.bind_hdr_occ) {
3656 newsrv->conn_src.bind_hdr_occ = 0;
Christopher Faulet767a84b2017-11-24 16:50:31 +01003657 ha_warning("config : %s '%s' : server %s cannot use header %s as source IP in non-HTTP mode.\n",
3658 proxy_type_str(curproxy), curproxy->id, newsrv->id, newsrv->conn_src.bind_hdr_name);
Willy Tarreaubce70882009-09-07 11:51:47 +02003659 err_code |= ERR_WARN;
3660 }
Willy Tarreauefa5f512010-03-30 20:13:29 +02003661#endif
Willy Tarreau4c183462017-01-06 12:21:38 +01003662
Willy Tarreau46deab62018-04-28 07:18:15 +02003663 if ((curproxy->mode != PR_MODE_HTTP) && (curproxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR)
3664 curproxy->options &= ~PR_O_REUSE_MASK;
3665
Willy Tarreau21d2af32008-02-14 20:25:24 +01003666 newsrv = newsrv->next;
3667 }
3668
Christopher Fauletd7c91962015-04-30 11:48:27 +02003669 /* Check filter configuration, if any */
3670 cfgerr += flt_check(curproxy);
3671
Willy Tarreauc1a21672009-08-16 22:37:44 +02003672 if (curproxy->cap & PR_CAP_FE) {
Willy Tarreau050536d2012-10-04 08:47:34 +02003673 if (!curproxy->accept)
3674 curproxy->accept = frontend_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +02003675
Willy Tarreauc1a21672009-08-16 22:37:44 +02003676 if (curproxy->tcp_req.inspect_delay ||
3677 !LIST_ISEMPTY(&curproxy->tcp_req.inspect_rules))
Willy Tarreaufb356202010-08-03 14:02:05 +02003678 curproxy->fe_req_ana |= AN_REQ_INSPECT_FE;
Willy Tarreauc1a21672009-08-16 22:37:44 +02003679
Willy Tarreau4e5b8282009-08-16 22:57:50 +02003680 if (curproxy->mode == PR_MODE_HTTP) {
Willy Tarreauc1a21672009-08-16 22:37:44 +02003681 curproxy->fe_req_ana |= AN_REQ_WAIT_HTTP | AN_REQ_HTTP_PROCESS_FE;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003682 curproxy->fe_rsp_ana |= AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_FE;
Willy Tarreau4e5b8282009-08-16 22:57:50 +02003683 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02003684
William Lallemandcf62f7e2018-10-26 14:47:40 +02003685 if (curproxy->mode == PR_MODE_CLI) {
3686 curproxy->fe_req_ana |= AN_REQ_WAIT_CLI;
3687 curproxy->fe_rsp_ana |= AN_RES_WAIT_CLI;
3688 }
3689
Willy Tarreauc1a21672009-08-16 22:37:44 +02003690 /* both TCP and HTTP must check switching rules */
3691 curproxy->fe_req_ana |= AN_REQ_SWITCHING_RULES;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003692
3693 /* Add filters analyzers if needed */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01003694 if (!LIST_ISEMPTY(&curproxy->filter_configs)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003695 curproxy->fe_req_ana |= AN_REQ_FLT_START_FE | AN_REQ_FLT_XFER_DATA | AN_REQ_FLT_END;
3696 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 +02003697 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02003698 }
3699
3700 if (curproxy->cap & PR_CAP_BE) {
Willy Tarreaufb356202010-08-03 14:02:05 +02003701 if (curproxy->tcp_req.inspect_delay ||
3702 !LIST_ISEMPTY(&curproxy->tcp_req.inspect_rules))
3703 curproxy->be_req_ana |= AN_REQ_INSPECT_BE;
3704
Emeric Brun97679e72010-09-23 17:56:44 +02003705 if (!LIST_ISEMPTY(&curproxy->tcp_rep.inspect_rules))
3706 curproxy->be_rsp_ana |= AN_RES_INSPECT;
3707
Willy Tarreau4e5b8282009-08-16 22:57:50 +02003708 if (curproxy->mode == PR_MODE_HTTP) {
Willy Tarreauc1a21672009-08-16 22:37:44 +02003709 curproxy->be_req_ana |= AN_REQ_WAIT_HTTP | AN_REQ_HTTP_INNER | AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003710 curproxy->be_rsp_ana |= AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE;
Willy Tarreau4e5b8282009-08-16 22:57:50 +02003711 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02003712
3713 /* If the backend does requires RDP cookie persistence, we have to
3714 * enable the corresponding analyser.
3715 */
3716 if (curproxy->options2 & PR_O2_RDPC_PRST)
3717 curproxy->be_req_ana |= AN_REQ_PRST_RDP_COOKIE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003718
3719 /* Add filters analyzers if needed */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01003720 if (!LIST_ISEMPTY(&curproxy->filter_configs)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003721 curproxy->be_req_ana |= AN_REQ_FLT_START_BE | AN_REQ_FLT_XFER_DATA | AN_REQ_FLT_END;
3722 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 +02003723 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02003724 }
Christopher Fauleta717b992018-04-10 14:43:00 +02003725
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003726 /* Check the mux protocols, if any, for each listener and server
Christopher Fauleta717b992018-04-10 14:43:00 +02003727 * attached to the current proxy */
3728 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
3729 int mode = (1 << (curproxy->mode == PR_MODE_HTTP));
Willy Tarreau76a551d2018-12-02 13:09:09 +01003730 const struct mux_proto_list *mux_ent;
Christopher Fauleta717b992018-04-10 14:43:00 +02003731
3732 if (!bind_conf->mux_proto)
3733 continue;
Willy Tarreau76a551d2018-12-02 13:09:09 +01003734
3735 /* it is possible that an incorrect mux was referenced
3736 * due to the proxy's mode not being taken into account
3737 * on first pass. Let's adjust it now.
3738 */
3739 mux_ent = conn_get_best_mux_entry(bind_conf->mux_proto->token, PROTO_SIDE_FE, mode);
3740
3741 if (!mux_ent || !isteq(mux_ent->token, bind_conf->mux_proto->token)) {
Christopher Fauleta717b992018-04-10 14:43:00 +02003742 ha_alert("config : %s '%s' : MUX protocol '%.*s' is not usable for 'bind %s' at [%s:%d].\n",
3743 proxy_type_str(curproxy), curproxy->id,
3744 (int)bind_conf->mux_proto->token.len,
3745 bind_conf->mux_proto->token.ptr,
3746 bind_conf->arg, bind_conf->file, bind_conf->line);
3747 cfgerr++;
3748 }
Willy Tarreau76a551d2018-12-02 13:09:09 +01003749
3750 /* update the mux */
3751 bind_conf->mux_proto = mux_ent;
Christopher Fauleta717b992018-04-10 14:43:00 +02003752 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003753 for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
3754 int mode = (1 << (curproxy->mode == PR_MODE_HTTP));
Willy Tarreau76a551d2018-12-02 13:09:09 +01003755 const struct mux_proto_list *mux_ent;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003756
3757 if (!newsrv->mux_proto)
3758 continue;
Willy Tarreau76a551d2018-12-02 13:09:09 +01003759
3760 /* it is possible that an incorrect mux was referenced
3761 * due to the proxy's mode not being taken into account
3762 * on first pass. Let's adjust it now.
3763 */
3764 mux_ent = conn_get_best_mux_entry(newsrv->mux_proto->token, PROTO_SIDE_BE, mode);
3765
3766 if (!mux_ent || !isteq(mux_ent->token, newsrv->mux_proto->token)) {
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003767 ha_alert("config : %s '%s' : MUX protocol '%.*s' is not usable for server '%s' at [%s:%d].\n",
3768 proxy_type_str(curproxy), curproxy->id,
3769 (int)newsrv->mux_proto->token.len,
3770 newsrv->mux_proto->token.ptr,
3771 newsrv->id, newsrv->conf.file, newsrv->conf.line);
3772 cfgerr++;
3773 }
Willy Tarreau76a551d2018-12-02 13:09:09 +01003774
3775 /* update the mux */
3776 newsrv->mux_proto = mux_ent;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003777 }
Willy Tarreau4cdac162021-03-05 10:48:42 +01003778 }
Willy Tarreau835daa12019-02-07 14:46:29 +01003779
Willy Tarreau4cdac162021-03-05 10:48:42 +01003780 /***********************************************************/
3781 /* At this point, target names have already been resolved. */
3782 /***********************************************************/
Willy Tarreau835daa12019-02-07 14:46:29 +01003783
Willy Tarreau4cdac162021-03-05 10:48:42 +01003784 /* we must finish to initialize certain things on the servers */
Willy Tarreau835daa12019-02-07 14:46:29 +01003785
Willy Tarreau4cdac162021-03-05 10:48:42 +01003786 list_for_each_entry(newsrv, &servers_list, global_list) {
3787 /* initialize idle conns lists */
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003788 newsrv->per_thr = calloc(global.nbthread, sizeof(*newsrv->per_thr));
3789 if (!newsrv->per_thr) {
3790 ha_alert("parsing [%s:%d] : failed to allocate per-thread lists for server '%s'.\n",
3791 newsrv->conf.file, newsrv->conf.line, newsrv->id);
Willy Tarreau4cdac162021-03-05 10:48:42 +01003792 cfgerr++;
3793 continue;
3794 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003795
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003796 for (i = 0; i < global.nbthread; i++) {
3797 newsrv->per_thr[i].idle_conns = EB_ROOT;
3798 newsrv->per_thr[i].safe_conns = EB_ROOT;
3799 newsrv->per_thr[i].avail_conns = EB_ROOT;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01003800 MT_LIST_INIT(&newsrv->per_thr[i].streams);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003801 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003802
Willy Tarreau4cdac162021-03-05 10:48:42 +01003803 if (newsrv->max_idle_conns != 0) {
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003804 newsrv->curr_idle_thr = calloc(global.nbthread, sizeof(*newsrv->curr_idle_thr));
3805 if (!newsrv->curr_idle_thr) {
3806 ha_alert("parsing [%s:%d] : failed to allocate idle connection tasks for server '%s'.\n",
3807 newsrv->conf.file, newsrv->conf.line, newsrv->id);
3808 cfgerr++;
3809 continue;
3810 }
3811 }
3812 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003813
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003814 idle_conn_task = task_new(MAX_THREADS_MASK);
3815 if (!idle_conn_task) {
3816 ha_alert("parsing : failed to allocate global idle connection task.\n");
3817 cfgerr++;
3818 }
3819 else {
3820 idle_conn_task->process = srv_cleanup_idle_conns;
3821 idle_conn_task->context = NULL;
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003822
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003823 for (i = 0; i < global.nbthread; i++) {
3824 idle_conns[i].cleanup_task = task_new(1UL << i);
3825 if (!idle_conns[i].cleanup_task) {
3826 ha_alert("parsing : failed to allocate idle connection tasks for thread '%d'.\n", i);
3827 cfgerr++;
3828 break;
Willy Tarreau4cdac162021-03-05 10:48:42 +01003829 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003830
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003831 idle_conns[i].cleanup_task->process = srv_cleanup_toremove_conns;
3832 idle_conns[i].cleanup_task->context = NULL;
3833 HA_SPIN_INIT(&idle_conns[i].idle_conns_lock);
3834 MT_LIST_INIT(&idle_conns[i].toremove_conns);
Willy Tarreau835daa12019-02-07 14:46:29 +01003835 }
Willy Tarreau419ead82014-09-16 13:41:21 +02003836 }
3837
Willy Tarreau419ead82014-09-16 13:41:21 +02003838 /* Check multi-process mode compatibility */
3839
Willy Tarreau4975d142021-03-13 11:00:33 +01003840 if (global.nbproc > 1 && global.cli_fe) {
3841 list_for_each_entry(bind_conf, &global.cli_fe->conf.bind, by_fe) {
Willy Tarreau419ead82014-09-16 13:41:21 +02003842 unsigned long mask;
3843
Willy Tarreau4975d142021-03-13 11:00:33 +01003844 mask = proc_mask(global.cli_fe->bind_proc) && all_proc_mask;
Willy Tarreaue26993c2020-09-03 07:18:55 +02003845 mask &= proc_mask(bind_conf->settings.bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003846
3847 /* stop here if more than one process is used */
Willy Tarreau9504dd62018-10-15 09:37:03 +02003848 if (atleast2(mask))
Willy Tarreau419ead82014-09-16 13:41:21 +02003849 break;
3850 }
Willy Tarreau4975d142021-03-13 11:00:33 +01003851 if (&bind_conf->by_fe != &global.cli_fe->conf.bind) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003852 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 +02003853 }
3854 }
3855
3856 /* Make each frontend inherit bind-process from its listeners when not specified. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003857 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreau419ead82014-09-16 13:41:21 +02003858 if (curproxy->bind_proc)
3859 continue;
3860
3861 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
3862 unsigned long mask;
3863
Willy Tarreaue26993c2020-09-03 07:18:55 +02003864 mask = proc_mask(bind_conf->settings.bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003865 curproxy->bind_proc |= mask;
3866 }
Willy Tarreau6daac192019-02-02 17:39:53 +01003867 curproxy->bind_proc = proc_mask(curproxy->bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003868 }
3869
Willy Tarreau4975d142021-03-13 11:00:33 +01003870 if (global.cli_fe) {
3871 list_for_each_entry(bind_conf, &global.cli_fe->conf.bind, by_fe) {
Willy Tarreau419ead82014-09-16 13:41:21 +02003872 unsigned long mask;
3873
Willy Tarreaue26993c2020-09-03 07:18:55 +02003874 mask = bind_conf->settings.bind_proc ? bind_conf->settings.bind_proc : 0;
Willy Tarreau4975d142021-03-13 11:00:33 +01003875 global.cli_fe->bind_proc |= mask;
Willy Tarreau419ead82014-09-16 13:41:21 +02003876 }
Willy Tarreau4975d142021-03-13 11:00:33 +01003877 global.cli_fe->bind_proc = proc_mask(global.cli_fe->bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003878 }
3879
Willy Tarreauacbe8ab2014-10-01 20:50:17 +02003880 /* propagate bindings from frontends to backends. Don't do it if there
3881 * are any fatal errors as we must not call it with unresolved proxies.
3882 */
3883 if (!cfgerr) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003884 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreauacbe8ab2014-10-01 20:50:17 +02003885 if (curproxy->cap & PR_CAP_FE)
3886 propagate_processes(curproxy, NULL);
3887 }
Willy Tarreau419ead82014-09-16 13:41:21 +02003888 }
3889
3890 /* Bind each unbound backend to all processes when not specified. */
Willy Tarreau6daac192019-02-02 17:39:53 +01003891 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next)
3892 curproxy->bind_proc = proc_mask(curproxy->bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003893
3894 /*******************************************************/
3895 /* At this step, all proxies have a non-null bind_proc */
3896 /*******************************************************/
3897
3898 /* perform the final checks before creating tasks */
3899
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003900 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreau419ead82014-09-16 13:41:21 +02003901 struct listener *listener;
3902 unsigned int next_id;
Willy Tarreauc1a21672009-08-16 22:37:44 +02003903
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02003904 /* Configure SSL for each bind line.
3905 * Note: if configuration fails at some point, the ->ctx member
3906 * remains NULL so that listeners can later detach.
3907 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003908 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
Willy Tarreau55d37912016-12-21 23:38:39 +01003909 if (bind_conf->xprt->prepare_bind_conf &&
3910 bind_conf->xprt->prepare_bind_conf(bind_conf) < 0)
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02003911 cfgerr++;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003912 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02003913
Willy Tarreaue6b98942007-10-29 01:09:36 +01003914 /* adjust this proxy's listeners */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02003915 next_id = 1;
Willy Tarreau4348fad2012-09-20 16:48:07 +02003916 list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) {
Willy Tarreau7c0ffd22016-04-14 11:47:38 +02003917 int nbproc;
3918
3919 nbproc = my_popcountl(curproxy->bind_proc &
Willy Tarreaue26993c2020-09-03 07:18:55 +02003920 (listener->bind_conf->settings.bind_proc ? listener->bind_conf->settings.bind_proc : curproxy->bind_proc) &
Willy Tarreaua38a7172019-02-02 17:11:28 +01003921 all_proc_mask);
Willy Tarreau7c0ffd22016-04-14 11:47:38 +02003922
3923 if (!nbproc) /* no intersection between listener and frontend */
3924 nbproc = 1;
3925
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02003926 if (!listener->luid) {
3927 /* listener ID not set, use automatic numbering with first
3928 * spare entry starting with next_luid.
3929 */
3930 next_id = get_next_id(&curproxy->conf.used_listener_id, next_id);
3931 listener->conf.id.key = listener->luid = next_id;
3932 eb32_insert(&curproxy->conf.used_listener_id, &listener->conf.id);
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02003933 }
Krzysztof Piotr Oledzkidf5cb9f2010-02-05 20:58:27 +01003934 next_id++;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02003935
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003936 /* enable separate counters */
3937 if (curproxy->options2 & PR_O2_SOCKSTAT) {
Willy Tarreauae9bea02016-11-25 14:44:52 +01003938 listener->counters = calloc(1, sizeof(*listener->counters));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003939 if (!listener->name)
3940 memprintf(&listener->name, "sock-%d", listener->luid);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003941 }
Willy Tarreau81796be2012-09-22 19:11:47 +02003942
Willy Tarreaue6b98942007-10-29 01:09:36 +01003943 if (curproxy->options & PR_O_TCP_NOLING)
3944 listener->options |= LI_O_NOLINGER;
Willy Tarreau16a21472012-11-19 12:39:59 +01003945 if (!listener->maxaccept)
Willy Tarreau66161322021-02-19 15:50:27 +01003946 listener->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreau16a21472012-11-19 12:39:59 +01003947
3948 /* we want to have an optimal behaviour on single process mode to
3949 * maximize the work at once, but in multi-process we want to keep
3950 * some fairness between processes, so we target half of the max
3951 * number of events to be balanced over all the processes the proxy
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003952 * is bound to. Remember that maxaccept = -1 must be kept as it is
Willy Tarreau16a21472012-11-19 12:39:59 +01003953 * used to disable the limit.
3954 */
Christopher Faulet02f3cf12019-04-30 14:08:41 +02003955 if (listener->maxaccept > 0 && nbproc > 1) {
3956 listener->maxaccept = (listener->maxaccept + 1) / 2;
Willy Tarreau16a21472012-11-19 12:39:59 +01003957 listener->maxaccept = (listener->maxaccept + nbproc - 1) / nbproc;
3958 }
3959
Willy Tarreau9903f0e2015-04-04 18:50:31 +02003960 listener->accept = session_accept_fd;
Willy Tarreauc1a21672009-08-16 22:37:44 +02003961 listener->analysers |= curproxy->fe_req_ana;
Willy Tarreau10b688f2015-03-13 16:43:12 +01003962 listener->default_target = curproxy->default_target;
Willy Tarreau3bc13772008-12-07 11:50:35 +01003963
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02003964 if (!LIST_ISEMPTY(&curproxy->tcp_req.l4_rules))
Willy Tarreau7d9736f2016-10-21 16:34:21 +02003965 listener->options |= LI_O_TCP_L4_RULES;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02003966
Willy Tarreau620408f2016-10-21 16:37:51 +02003967 if (!LIST_ISEMPTY(&curproxy->tcp_req.l5_rules))
3968 listener->options |= LI_O_TCP_L5_RULES;
3969
Willy Tarreau9ea05a72009-06-14 12:07:01 +02003970 /* smart accept mode is automatic in HTTP mode */
3971 if ((curproxy->options2 & PR_O2_SMARTACC) ||
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003972 ((curproxy->mode == PR_MODE_HTTP || listener->bind_conf->is_ssl) &&
Willy Tarreau9ea05a72009-06-14 12:07:01 +02003973 !(curproxy->no_options2 & PR_O2_SMARTACC)))
3974 listener->options |= LI_O_NOQUICKACK;
Willy Tarreaue6b98942007-10-29 01:09:36 +01003975 }
3976
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003977 /* Release unused SSL configs */
3978 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01003979 if (!bind_conf->is_ssl && bind_conf->xprt->destroy_bind_conf)
3980 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003981 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02003982
Willy Tarreaua38a7172019-02-02 17:11:28 +01003983 if (atleast2(curproxy->bind_proc & all_proc_mask)) {
Willy Tarreau102df612014-05-07 23:56:38 +02003984 if (curproxy->uri_auth) {
Willy Tarreaueb791e02014-09-16 15:11:04 +02003985 int count, maxproc = 0;
3986
3987 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
Willy Tarreaue26993c2020-09-03 07:18:55 +02003988 count = my_popcountl(bind_conf->settings.bind_proc);
Willy Tarreaueb791e02014-09-16 15:11:04 +02003989 if (count > maxproc)
3990 maxproc = count;
3991 }
3992 /* backends have 0, frontends have 1 or more */
3993 if (maxproc != 1)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003994 ha_warning("Proxy '%s': in multi-process mode, stats will be"
3995 " limited to process assigned to the current request.\n",
3996 curproxy->id);
Willy Tarreaueb791e02014-09-16 15:11:04 +02003997
Willy Tarreau102df612014-05-07 23:56:38 +02003998 if (!LIST_ISEMPTY(&curproxy->uri_auth->admin_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003999 ha_warning("Proxy '%s': stats admin will not work correctly in multi-process mode.\n",
4000 curproxy->id);
Cyril Bonté02ff8ef2010-12-14 22:48:49 +01004001 }
Cyril Bonté02ff8ef2010-12-14 22:48:49 +01004002 }
Willy Tarreau102df612014-05-07 23:56:38 +02004003 if (!LIST_ISEMPTY(&curproxy->sticking_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004004 ha_warning("Proxy '%s': sticking rules will not work correctly in multi-process mode.\n",
4005 curproxy->id);
Cyril Bonté02ff8ef2010-12-14 22:48:49 +01004006 }
4007 }
Willy Tarreau918ff602011-07-25 16:33:49 +02004008
4009 /* create the task associated with the proxy */
Emeric Brunc60def82017-09-27 14:59:38 +02004010 curproxy->task = task_new(MAX_THREADS_MASK);
Willy Tarreau918ff602011-07-25 16:33:49 +02004011 if (curproxy->task) {
4012 curproxy->task->context = curproxy;
4013 curproxy->task->process = manage_proxy;
Willy Tarreau918ff602011-07-25 16:33:49 +02004014 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004015 ha_alert("Proxy '%s': no more memory when trying to allocate the management task\n",
4016 curproxy->id);
Willy Tarreau918ff602011-07-25 16:33:49 +02004017 cfgerr++;
4018 }
Willy Tarreaub369a042014-09-16 13:21:03 +02004019 }
4020
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01004021 /*
4022 * Recount currently required checks.
4023 */
4024
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004025 for (curproxy=proxies_list; curproxy; curproxy=curproxy->next) {
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01004026 int optnum;
4027
Willy Tarreau66aa61f2009-01-18 21:44:07 +01004028 for (optnum = 0; cfg_opts[optnum].name; optnum++)
4029 if (curproxy->options & cfg_opts[optnum].val)
4030 global.last_checks |= cfg_opts[optnum].checks;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01004031
Willy Tarreau66aa61f2009-01-18 21:44:07 +01004032 for (optnum = 0; cfg_opts2[optnum].name; optnum++)
4033 if (curproxy->options2 & cfg_opts2[optnum].val)
4034 global.last_checks |= cfg_opts2[optnum].checks;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01004035 }
4036
Willy Tarreau0fca4832015-05-01 19:12:05 +02004037 /* compute the required process bindings for the peers */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004038 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004039 if (curproxy->table && curproxy->table->peers.p)
4040 curproxy->table->peers.p->peers_fe->bind_proc |= curproxy->bind_proc;
Willy Tarreau0fca4832015-05-01 19:12:05 +02004041
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01004042 /* compute the required process bindings for the peers from <stktables_list>
4043 * for all the stick-tables, the ones coming with "peers" sections included.
4044 */
4045 for (t = stktables_list; t; t = t->next) {
4046 struct proxy *p;
4047
4048 for (p = t->proxies_list; p; p = p->next_stkt_ref) {
4049 if (t->peers.p && t->peers.p->peers_fe) {
4050 t->peers.p->peers_fe->bind_proc |= p->bind_proc;
4051 }
4052 }
4053 }
4054
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02004055 if (cfg_peers) {
4056 struct peers *curpeers = cfg_peers, **last;
Willy Tarreau122541c2011-09-07 21:24:49 +02004057 struct peer *p, *pb;
4058
William Lallemanda2cfd7e2020-03-24 16:02:48 +01004059 /* In the case the peers frontend was not initialized by a
4060 stick-table used in the configuration, set its bind_proc
4061 by default to the first process. */
4062 while (curpeers) {
William Lallemand3ef2d562020-03-24 16:42:15 +01004063 if (curpeers->peers_fe) {
4064 if (curpeers->peers_fe->bind_proc == 0)
4065 curpeers->peers_fe->bind_proc = 1;
4066 }
William Lallemanda2cfd7e2020-03-24 16:02:48 +01004067 curpeers = curpeers->next;
4068 }
4069
4070 curpeers = cfg_peers;
Willy Tarreau1e273012015-05-01 19:15:17 +02004071 /* Remove all peers sections which don't have a valid listener,
4072 * which are not used by any table, or which are bound to more
4073 * than one process.
Willy Tarreau122541c2011-09-07 21:24:49 +02004074 */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02004075 last = &cfg_peers;
Willy Tarreau122541c2011-09-07 21:24:49 +02004076 while (*last) {
Frédéric Lécaille87eacbb2020-03-24 20:08:30 +01004077 struct stktable *t;
Willy Tarreau122541c2011-09-07 21:24:49 +02004078 curpeers = *last;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02004079
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02004080 if (curpeers->disabled) {
Willy Tarreau77e4bd12015-05-01 20:02:17 +02004081 /* the "disabled" keyword was present */
4082 if (curpeers->peers_fe)
4083 stop_proxy(curpeers->peers_fe);
4084 curpeers->peers_fe = NULL;
4085 }
Frédéric Lécaille5a4fe5a2019-10-04 08:30:04 +02004086 else if (!curpeers->peers_fe || !curpeers->peers_fe->id) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004087 ha_warning("Removing incomplete section 'peers %s' (no peer named '%s').\n",
4088 curpeers->id, localpeer);
Frédéric Lécaille87eacbb2020-03-24 20:08:30 +01004089 if (curpeers->peers_fe)
4090 stop_proxy(curpeers->peers_fe);
4091 curpeers->peers_fe = NULL;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02004092 }
Willy Tarreau9504dd62018-10-15 09:37:03 +02004093 else if (atleast2(curpeers->peers_fe->bind_proc)) {
Willy Tarreau1e273012015-05-01 19:15:17 +02004094 /* either it's totally stopped or too much used */
4095 if (curpeers->peers_fe->bind_proc) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004096 ha_alert("Peers section '%s': peers referenced by sections "
4097 "running in different processes (%d different ones). "
4098 "Check global.nbproc and all tables' bind-process "
4099 "settings.\n", curpeers->id, my_popcountl(curpeers->peers_fe->bind_proc));
Willy Tarreau1e273012015-05-01 19:15:17 +02004100 cfgerr++;
4101 }
4102 stop_proxy(curpeers->peers_fe);
4103 curpeers->peers_fe = NULL;
4104 }
Willy Tarreau77e4bd12015-05-01 20:02:17 +02004105 else {
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +01004106 /* Initializes the transport layer of the server part of all the peers belonging to
4107 * <curpeers> section if required.
4108 * Note that ->srv is used by the local peer of a new process to connect to the local peer
4109 * of an old process.
4110 */
Frédéric Lécaille1055e682018-04-26 14:35:21 +02004111 p = curpeers->remote;
4112 while (p) {
Frédéric Lécaille355b2032019-01-11 14:06:12 +01004113 if (p->srv) {
Christopher Fauletf61f33a2020-03-27 18:55:49 +01004114 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 +01004115 cfgerr += xprt_get(XPRT_SSL)->prepare_srv(p->srv);
4116 }
Frédéric Lécaille1055e682018-04-26 14:35:21 +02004117 p = p->next;
4118 }
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +01004119 /* Configure the SSL bindings of the local peer if required. */
4120 if (!LIST_ISEMPTY(&curpeers->peers_fe->conf.bind)) {
4121 struct list *l;
4122 struct bind_conf *bind_conf;
4123
4124 l = &curpeers->peers_fe->conf.bind;
4125 bind_conf = LIST_ELEM(l->n, typeof(bind_conf), by_fe);
4126 if (bind_conf->xprt->prepare_bind_conf &&
4127 bind_conf->xprt->prepare_bind_conf(bind_conf) < 0)
4128 cfgerr++;
4129 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02004130 if (!peers_init_sync(curpeers) || !peers_alloc_dcache(curpeers)) {
Willy Tarreaud9443442018-10-15 11:18:03 +02004131 ha_alert("Peers section '%s': out of memory, giving up on peers.\n",
4132 curpeers->id);
4133 cfgerr++;
4134 break;
4135 }
Willy Tarreau122541c2011-09-07 21:24:49 +02004136 last = &curpeers->next;
4137 continue;
4138 }
4139
Willy Tarreau77e4bd12015-05-01 20:02:17 +02004140 /* clean what has been detected above */
Willy Tarreau122541c2011-09-07 21:24:49 +02004141 p = curpeers->remote;
4142 while (p) {
4143 pb = p->next;
4144 free(p->id);
4145 free(p);
4146 p = pb;
4147 }
4148
4149 /* Destroy and unlink this curpeers section.
4150 * Note: curpeers is backed up into *last.
4151 */
4152 free(curpeers->id);
4153 curpeers = curpeers->next;
Frédéric Lécaille87eacbb2020-03-24 20:08:30 +01004154 /* Reset any refereance to this peers section in the list of stick-tables */
4155 for (t = stktables_list; t; t = t->next) {
4156 if (t->peers.p && t->peers.p == *last)
4157 t->peers.p = NULL;
4158 }
Willy Tarreau122541c2011-09-07 21:24:49 +02004159 free(*last);
4160 *last = curpeers;
4161 }
4162 }
4163
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004164 for (t = stktables_list; t; t = t->next) {
4165 if (t->proxy)
4166 continue;
4167 if (!stktable_init(t)) {
4168 ha_alert("Proxy '%s': failed to initialize stick-table.\n", t->id);
4169 cfgerr++;
4170 }
4171 }
4172
Willy Tarreau6866f3f2015-05-01 19:09:08 +02004173 /* initialize stick-tables on backend capable proxies. This must not
4174 * be done earlier because the data size may be discovered while parsing
4175 * other proxies.
4176 */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004177 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02004178 if (curproxy->disabled || !curproxy->table)
Willy Tarreau6866f3f2015-05-01 19:09:08 +02004179 continue;
4180
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004181 if (!stktable_init(curproxy->table)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004182 ha_alert("Proxy '%s': failed to initialize stick-table.\n", curproxy->id);
Willy Tarreau6866f3f2015-05-01 19:09:08 +02004183 cfgerr++;
4184 }
4185 }
4186
Simon Horman0d16a402015-01-30 11:22:58 +09004187 if (mailers) {
4188 struct mailers *curmailers = mailers, **last;
4189 struct mailer *m, *mb;
4190
4191 /* Remove all mailers sections which don't have a valid listener.
4192 * This can happen when a mailers section is never referenced.
4193 */
4194 last = &mailers;
4195 while (*last) {
4196 curmailers = *last;
4197 if (curmailers->users) {
4198 last = &curmailers->next;
4199 continue;
4200 }
4201
Christopher Faulet767a84b2017-11-24 16:50:31 +01004202 ha_warning("Removing incomplete section 'mailers %s'.\n",
4203 curmailers->id);
Simon Horman0d16a402015-01-30 11:22:58 +09004204
4205 m = curmailers->mailer_list;
4206 while (m) {
4207 mb = m->next;
4208 free(m->id);
4209 free(m);
4210 m = mb;
4211 }
4212
4213 /* Destroy and unlink this curmailers section.
4214 * Note: curmailers is backed up into *last.
4215 */
4216 free(curmailers->id);
4217 curmailers = curmailers->next;
4218 free(*last);
4219 *last = curmailers;
4220 }
4221 }
4222
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02004223 /* Update server_state_file_name to backend name if backend is supposed to use
4224 * a server-state file locally defined and none has been provided */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004225 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02004226 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_LOCAL &&
4227 curproxy->server_state_file_name == NULL)
4228 curproxy->server_state_file_name = strdup(curproxy->id);
4229 }
4230
Emeric Brun750fe792020-12-23 16:51:12 +01004231 list_for_each_entry(curr_resolvers, &sec_resolvers, list) {
Ben Draut054fbee2018-04-13 15:43:04 -06004232 if (LIST_ISEMPTY(&curr_resolvers->nameservers)) {
4233 ha_warning("config : resolvers '%s' [%s:%d] has no nameservers configured!\n",
4234 curr_resolvers->id, curr_resolvers->conf.file,
4235 curr_resolvers->conf.line);
4236 err_code |= ERR_WARN;
4237 }
4238 }
4239
William Lallemand48b4bb42017-10-23 14:36:34 +02004240 list_for_each_entry(postparser, &postparsers, list) {
4241 if (postparser->func)
4242 cfgerr += postparser->func();
4243 }
4244
Willy Tarreaubb925012009-07-23 13:36:36 +02004245 if (cfgerr > 0)
4246 err_code |= ERR_ALERT | ERR_FATAL;
4247 out:
4248 return err_code;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004249}
4250
Willy Tarreau5b2c3362008-07-09 19:39:06 +02004251/*
4252 * Registers the CFG keyword list <kwl> as a list of valid keywords for next
4253 * parsing sessions.
4254 */
4255void cfg_register_keywords(struct cfg_kw_list *kwl)
4256{
Willy Tarreau2b718102021-04-21 07:32:39 +02004257 LIST_APPEND(&cfg_keywords.list, &kwl->list);
Willy Tarreau5b2c3362008-07-09 19:39:06 +02004258}
Willy Tarreaubaaee002006-06-26 02:48:02 +02004259
Willy Tarreau5b2c3362008-07-09 19:39:06 +02004260/*
4261 * Unregisters the CFG keyword list <kwl> from the list of valid keywords.
4262 */
4263void cfg_unregister_keywords(struct cfg_kw_list *kwl)
4264{
Willy Tarreau2b718102021-04-21 07:32:39 +02004265 LIST_DELETE(&kwl->list);
Willy Tarreau5b2c3362008-07-09 19:39:06 +02004266 LIST_INIT(&kwl->list);
4267}
Willy Tarreaubaaee002006-06-26 02:48:02 +02004268
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01004269/* this function register new section in the haproxy configuration file.
4270 * <section_name> is the name of this new section and <section_parser>
4271 * is the called parser. If two section declaration have the same name,
4272 * only the first declared is used.
4273 */
4274int cfg_register_section(char *section_name,
William Lallemandd2ff56d2017-10-16 11:06:50 +02004275 int (*section_parser)(const char *, int, char **, int),
4276 int (*post_section_parser)())
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01004277{
4278 struct cfg_section *cs;
4279
Willy Tarreau5e4261b2016-05-17 16:16:09 +02004280 list_for_each_entry(cs, &sections, list) {
4281 if (strcmp(cs->section_name, section_name) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004282 ha_alert("register section '%s': already registered.\n", section_name);
Willy Tarreau5e4261b2016-05-17 16:16:09 +02004283 return 0;
4284 }
4285 }
4286
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01004287 cs = calloc(1, sizeof(*cs));
4288 if (!cs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004289 ha_alert("register section '%s': out of memory.\n", section_name);
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01004290 return 0;
4291 }
4292
4293 cs->section_name = section_name;
4294 cs->section_parser = section_parser;
William Lallemandd2ff56d2017-10-16 11:06:50 +02004295 cs->post_section_parser = post_section_parser;
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01004296
Willy Tarreau2b718102021-04-21 07:32:39 +02004297 LIST_APPEND(&sections, &cs->list);
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01004298
4299 return 1;
4300}
4301
William Lallemand48b4bb42017-10-23 14:36:34 +02004302/* this function register a new function which will be called once the haproxy
4303 * configuration file has been parsed. It's useful to check dependencies
4304 * between sections or to resolve items once everything is parsed.
4305 */
4306int cfg_register_postparser(char *name, int (*func)())
4307{
4308 struct cfg_postparser *cp;
4309
4310 cp = calloc(1, sizeof(*cp));
4311 if (!cp) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004312 ha_alert("register postparser '%s': out of memory.\n", name);
William Lallemand48b4bb42017-10-23 14:36:34 +02004313 return 0;
4314 }
4315 cp->name = name;
4316 cp->func = func;
4317
Willy Tarreau2b718102021-04-21 07:32:39 +02004318 LIST_APPEND(&postparsers, &cp->list);
William Lallemand48b4bb42017-10-23 14:36:34 +02004319
4320 return 1;
4321}
4322
Willy Tarreaubaaee002006-06-26 02:48:02 +02004323/*
David Carlier845efb52015-09-25 11:49:18 +01004324 * free all config section entries
4325 */
4326void cfg_unregister_sections(void)
4327{
4328 struct cfg_section *cs, *ics;
4329
4330 list_for_each_entry_safe(cs, ics, &sections, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004331 LIST_DELETE(&cs->list);
David Carlier845efb52015-09-25 11:49:18 +01004332 free(cs);
4333 }
4334}
4335
Christopher Faulet7110b402016-10-26 11:09:44 +02004336void cfg_backup_sections(struct list *backup_sections)
4337{
4338 struct cfg_section *cs, *ics;
4339
4340 list_for_each_entry_safe(cs, ics, &sections, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004341 LIST_DELETE(&cs->list);
4342 LIST_APPEND(backup_sections, &cs->list);
Christopher Faulet7110b402016-10-26 11:09:44 +02004343 }
4344}
4345
4346void cfg_restore_sections(struct list *backup_sections)
4347{
4348 struct cfg_section *cs, *ics;
4349
4350 list_for_each_entry_safe(cs, ics, backup_sections, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004351 LIST_DELETE(&cs->list);
4352 LIST_APPEND(&sections, &cs->list);
Christopher Faulet7110b402016-10-26 11:09:44 +02004353 }
4354}
4355
Willy Tarreaue6552512018-11-26 11:33:13 +01004356/* these are the config sections handled by default */
4357REGISTER_CONFIG_SECTION("listen", cfg_parse_listen, NULL);
4358REGISTER_CONFIG_SECTION("frontend", cfg_parse_listen, NULL);
4359REGISTER_CONFIG_SECTION("backend", cfg_parse_listen, NULL);
4360REGISTER_CONFIG_SECTION("defaults", cfg_parse_listen, NULL);
4361REGISTER_CONFIG_SECTION("global", cfg_parse_global, NULL);
4362REGISTER_CONFIG_SECTION("userlist", cfg_parse_users, NULL);
4363REGISTER_CONFIG_SECTION("peers", cfg_parse_peers, NULL);
4364REGISTER_CONFIG_SECTION("mailers", cfg_parse_mailers, NULL);
4365REGISTER_CONFIG_SECTION("namespace_list", cfg_parse_netns, NULL);
Willy Tarreau659fbf02016-05-26 17:55:28 +02004366
Willy Tarreau8a022d52021-04-27 20:29:11 +02004367static struct cfg_kw_list cfg_kws = {{ },{
4368 { CFG_GLOBAL, "default-path", cfg_parse_global_def_path },
4369 { /* END */ }
4370}};
4371
4372INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4373
David Carlier845efb52015-09-25 11:49:18 +01004374/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004375 * Local variables:
4376 * c-indent-level: 8
4377 * c-basic-offset: 8
4378 * End:
4379 */