blob: f891697a1d2960b418a10aa21a579f01fc659b75 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Configuration parser
3 *
Willy Tarreauff011f22011-01-06 17:51:27 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaue5733232019-05-22 19:24:06 +020013#ifdef USE_LIBCRYPT
Cyril Bonté1a0191d2014-08-29 20:20:02 +020014/* This is to have crypt() defined on Linux */
15#define _GNU_SOURCE
16
Willy Tarreaue5733232019-05-22 19:24:06 +020017#ifdef USE_CRYPT_H
Cyril Bonté1a0191d2014-08-29 20:20:02 +020018/* some platforms such as Solaris need this */
19#include <crypt.h>
20#endif
Willy Tarreaue5733232019-05-22 19:24:06 +020021#endif /* USE_LIBCRYPT */
Cyril Bonté1a0191d2014-08-29 20:20:02 +020022
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +010023#include <dirent.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <netdb.h>
28#include <ctype.h>
Willy Tarreau95c20ac2007-03-25 15:39:23 +020029#include <pwd.h>
30#include <grp.h>
Willy Tarreau0b4ed902007-03-26 00:18:40 +020031#include <errno.h>
Willy Tarreau3f49b302007-06-11 00:29:26 +020032#include <sys/types.h>
33#include <sys/stat.h>
34#include <fcntl.h>
35#include <unistd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreaudcc048a2020-06-04 19:11:43 +020037#include <haproxy/acl.h>
Christopher Faulet42c6cf92021-03-25 17:19:04 +010038#include <haproxy/action.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/api.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020040#include <haproxy/auth.h>
Willy Tarreau49801602020-06-04 22:50:02 +020041#include <haproxy/backend.h>
Willy Tarreau278161c2020-06-04 11:18:28 +020042#include <haproxy/capture.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020043#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020044#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020045#include <haproxy/check.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020046#include <haproxy/chunk.h>
Amaury Denoyellea6f9c5d2021-04-23 16:58:08 +020047#ifdef USE_CPU_AFFINITY
Amaury Denoyellec90932b2021-04-14 16:16:03 +020048#include <haproxy/cpuset.h>
Amaury Denoyellea6f9c5d2021-04-23 16:58:08 +020049#endif
Willy Tarreau7ea393d2020-06-04 18:02:10 +020050#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020051#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020052#include <haproxy/filters.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020053#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020054#include <haproxy/global.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020055#include <haproxy/http_ana.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020056#include <haproxy/http_rules.h>
Willy Tarreaufbe8da32020-06-04 14:34:27 +020057#include <haproxy/lb_chash.h>
Willy Tarreaub5fc3bf2020-06-04 14:37:38 +020058#include <haproxy/lb_fas.h>
Willy Tarreau02549412020-06-04 14:41:04 +020059#include <haproxy/lb_fwlc.h>
Willy Tarreau546ba422020-06-04 14:45:03 +020060#include <haproxy/lb_fwrr.h>
Willy Tarreau28671592020-06-04 20:22:59 +020061#include <haproxy/lb_map.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020062#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020063#include <haproxy/log.h>
Willy Tarreaucee013e2020-06-05 11:40:38 +020064#include <haproxy/mailers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/namespace.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020066#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020067#include <haproxy/peers-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020068#include <haproxy/peers.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020069#include <haproxy/pool.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020070#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020071#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010072#include <haproxy/resolvers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020073#include <haproxy/sample.h>
74#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020075#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020076#include <haproxy/stats-t.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020077#include <haproxy/stick_table.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020078#include <haproxy/stream.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020079#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020080#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020081#include <haproxy/thread.h>
82#include <haproxy/time.h>
83#include <haproxy/tools.h>
84#include <haproxy/uri_auth-t.h>
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010085#include <haproxy/xprt_quic.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
87
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +010088/* Used to chain configuration sections definitions. This list
89 * stores struct cfg_section
90 */
91struct list sections = LIST_HEAD_INIT(sections);
92
William Lallemand48b4bb42017-10-23 14:36:34 +020093struct list postparsers = LIST_HEAD_INIT(postparsers);
94
Eric Salama1b8dacc2021-03-16 15:12:17 +010095extern struct proxy *mworker_proxy;
96
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010097char *cursection = NULL;
Willy Tarreauc8d5b952019-02-27 17:25:52 +010098int cfg_maxpconn = 0; /* # of simultaneous connections per proxy (-N) */
Willy Tarreau5af24ef2009-03-15 15:23:16 +010099int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */
Christopher Faulet79bdef32016-11-04 22:36:15 +0100100char *cfg_scope = NULL; /* the current scope during the configuration parsing */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101
Willy Tarreau5b2c3362008-07-09 19:39:06 +0200102/* List head of all known configuration keywords */
Willy Tarreau36b9e222018-11-11 15:19:52 +0100103struct cfg_kw_list cfg_keywords = {
Willy Tarreau5b2c3362008-07-09 19:39:06 +0200104 .list = LIST_HEAD_INIT(cfg_keywords.list)
105};
106
Willy Tarreau4b103022021-02-12 17:59:10 +0100107/* nested if/elif/else/endif block states */
108enum nested_cond_state {
109 NESTED_COND_IF_TAKE, // "if" with a true condition
110 NESTED_COND_IF_DROP, // "if" with a false condition
111 NESTED_COND_IF_SKIP, // "if" masked by an outer false condition
112
113 NESTED_COND_ELIF_TAKE, // "elif" with a true condition from a false one
114 NESTED_COND_ELIF_DROP, // "elif" with a false condition from a false one
115 NESTED_COND_ELIF_SKIP, // "elif" masked by an outer false condition or a previously taken if
116
117 NESTED_COND_ELSE_TAKE, // taken "else" after an if false condition
118 NESTED_COND_ELSE_DROP, // "else" masked by outer false condition or an if true condition
119};
120
121/* 100 levels of nested conditions should already be sufficient */
122#define MAXNESTEDCONDS 100
123
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124/*
125 * converts <str> to a list of listeners which are dynamically allocated.
126 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
127 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
128 * - <port> is a numerical port from 1 to 65535 ;
129 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
130 * This can be repeated as many times as necessary, separated by a coma.
Willy Tarreau4fbb2282012-09-20 20:01:39 +0200131 * Function returns 1 for success or 0 if error. In case of errors, if <err> is
132 * not NULL, it must be a valid pointer to either NULL or a freeable area that
133 * will be replaced with an error message.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134 */
Willy Tarreau4fbb2282012-09-20 20:01:39 +0200135int str2listener(char *str, struct proxy *curproxy, struct bind_conf *bind_conf, const char *file, int line, char **err)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136{
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200137 struct protocol *proto;
Willy Tarreau2dff0c22011-03-04 15:43:13 +0100138 char *next, *dupstr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139 int port, end;
140
141 next = dupstr = strdup(str);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200142
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143 while (next && *next) {
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200144 struct sockaddr_storage *ss2;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100145 int fd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200146
147 str = next;
148 /* 1) look for the end of the first address */
Krzysztof Piotr Oledzki52d522b2009-01-27 16:57:08 +0100149 if ((next = strchr(str, ',')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200150 *next++ = 0;
151 }
152
Willy Tarreau5fc93282020-09-16 18:25:03 +0200153 ss2 = str2sa_range(str, NULL, &port, &end, &fd, &proto, err,
Eric Salama1b8dacc2021-03-16 15:12:17 +0100154 (curproxy == global.cli_fe || curproxy == mworker_proxy) ? NULL : global.unix_bind.prefix,
Willy Tarreau32819932020-09-04 15:53:16 +0200155 NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
Willy Tarreau5e1779a2020-09-16 16:28:08 +0200156 PA_O_SOCKET_FD | PA_O_STREAM | PA_O_XPRT);
Willy Tarreau12eb2a62013-03-06 15:45:03 +0100157 if (!ss2)
158 goto fail;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159
Willy Tarreau12eb2a62013-03-06 15:45:03 +0100160 /* OK the address looks correct */
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100161
162#ifdef USE_QUIC
163 /* The transport layer automatically switches to QUIC when QUIC
164 * is selected, regardless of bind_conf settings. We then need
165 * to initialize QUIC params.
166 */
167 if (proto->sock_type == SOCK_DGRAM && proto->ctrl_type == SOCK_STREAM) {
168 bind_conf->xprt = xprt_get(XPRT_QUIC);
169 quic_transport_params_init(&bind_conf->quic_params, 1);
170 }
171#endif
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200172 if (!create_listeners(bind_conf, ss2, port, end, fd, proto, err)) {
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200173 memprintf(err, "%s for address '%s'.\n", *err, str);
174 goto fail;
175 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200176 } /* end while(next) */
177 free(dupstr);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200178 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200179 fail:
180 free(dupstr);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200181 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182}
183
William Lallemand6e62fb62015-04-28 16:55:23 +0200184/*
Willy Tarreauaa333122020-09-16 15:13:04 +0200185 * converts <str> to a list of datagram-oriented listeners which are dynamically
186 * allocated.
187 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
188 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
189 * - <port> is a numerical port from 1 to 65535 ;
190 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
191 * This can be repeated as many times as necessary, separated by a coma.
192 * Function returns 1 for success or 0 if error. In case of errors, if <err> is
193 * not NULL, it must be a valid pointer to either NULL or a freeable area that
194 * will be replaced with an error message.
195 */
196int str2receiver(char *str, struct proxy *curproxy, struct bind_conf *bind_conf, const char *file, int line, char **err)
197{
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200198 struct protocol *proto;
Willy Tarreauaa333122020-09-16 15:13:04 +0200199 char *next, *dupstr;
200 int port, end;
201
202 next = dupstr = strdup(str);
203
204 while (next && *next) {
205 struct sockaddr_storage *ss2;
206 int fd = -1;
207
208 str = next;
209 /* 1) look for the end of the first address */
210 if ((next = strchr(str, ',')) != NULL) {
211 *next++ = 0;
212 }
213
Willy Tarreau5fc93282020-09-16 18:25:03 +0200214 ss2 = str2sa_range(str, NULL, &port, &end, &fd, &proto, err,
Willy Tarreau4975d142021-03-13 11:00:33 +0100215 curproxy == global.cli_fe ? NULL : global.unix_bind.prefix,
Willy Tarreauaa333122020-09-16 15:13:04 +0200216 NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
217 PA_O_SOCKET_FD | PA_O_DGRAM | PA_O_XPRT);
218 if (!ss2)
219 goto fail;
220
221 /* OK the address looks correct */
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200222 if (!create_listeners(bind_conf, ss2, port, end, fd, proto, err)) {
Willy Tarreauaa333122020-09-16 15:13:04 +0200223 memprintf(err, "%s for address '%s'.\n", *err, str);
224 goto fail;
225 }
226 } /* end while(next) */
227 free(dupstr);
228 return 1;
229 fail:
230 free(dupstr);
231 return 0;
232}
233
234/*
Willy Tarreauece9b072016-12-21 22:41:44 +0100235 * Report an error in <msg> when there are too many arguments. This version is
236 * intended to be used by keyword parsers so that the message will be included
237 * into the general error message. The index is the current keyword in args.
238 * Return 0 if the number of argument is correct, otherwise build a message and
239 * return 1. Fill err_code with an ERR_ALERT and an ERR_FATAL if not null. The
240 * message may also be null, it will simply not be produced (useful to check only).
241 * <msg> and <err_code> are only affected on error.
242 */
243int too_many_args_idx(int maxarg, int index, char **args, char **msg, int *err_code)
244{
245 int i;
246
247 if (!*args[index + maxarg + 1])
248 return 0;
249
250 if (msg) {
251 *msg = NULL;
252 memprintf(msg, "%s", args[0]);
253 for (i = 1; i <= index; i++)
254 memprintf(msg, "%s %s", *msg, args[i]);
255
256 memprintf(msg, "'%s' cannot handle unexpected argument '%s'.", *msg, args[index + maxarg + 1]);
257 }
258 if (err_code)
259 *err_code |= ERR_ALERT | ERR_FATAL;
260
261 return 1;
262}
263
264/*
265 * same as too_many_args_idx with a 0 index
266 */
267int too_many_args(int maxarg, char **args, char **msg, int *err_code)
268{
269 return too_many_args_idx(maxarg, 0, args, msg, err_code);
270}
271
272/*
William Lallemand6e62fb62015-04-28 16:55:23 +0200273 * Report a fatal Alert when there is too much arguments
274 * The index is the current keyword in args
275 * Return 0 if the number of argument is correct, otherwise emit an alert and return 1
276 * Fill err_code with an ERR_ALERT and an ERR_FATAL
277 */
278int alertif_too_many_args_idx(int maxarg, int index, const char *file, int linenum, char **args, int *err_code)
279{
280 char *kw = NULL;
281 int i;
282
283 if (!*args[index + maxarg + 1])
284 return 0;
285
286 memprintf(&kw, "%s", args[0]);
287 for (i = 1; i <= index; i++) {
288 memprintf(&kw, "%s %s", kw, args[i]);
289 }
290
Christopher Faulet767a84b2017-11-24 16:50:31 +0100291 ha_alert("parsing [%s:%d] : '%s' cannot handle unexpected argument '%s'.\n", file, linenum, kw, args[index + maxarg + 1]);
William Lallemand6e62fb62015-04-28 16:55:23 +0200292 free(kw);
293 *err_code |= ERR_ALERT | ERR_FATAL;
294 return 1;
295}
296
297/*
298 * same as alertif_too_many_args_idx with a 0 index
299 */
300int alertif_too_many_args(int maxarg, const char *file, int linenum, char **args, int *err_code)
301{
302 return alertif_too_many_args_idx(maxarg, 0, file, linenum, args, err_code);
303}
304
Willy Tarreau61d18892009-03-31 10:49:21 +0200305
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100306/* Report it if a request ACL condition uses some keywords that are incompatible
307 * with the place where the ACL is used. It returns either 0 or ERR_WARN so that
308 * its result can be or'ed with err_code. Note that <cond> may be NULL and then
309 * will be ignored.
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100310 */
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100311int warnif_cond_conflicts(const struct acl_cond *cond, unsigned int where, const char *file, int line)
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100312{
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100313 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +0200314 const char *kw;
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100315
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100316 if (!cond)
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100317 return 0;
318
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100319 acl = acl_cond_conflicts(cond, where);
320 if (acl) {
321 if (acl->name && *acl->name)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100322 ha_warning("parsing [%s:%d] : acl '%s' will never match because it only involves keywords that are incompatible with '%s'\n",
323 file, line, acl->name, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100324 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100325 ha_warning("parsing [%s:%d] : anonymous acl will never match because it uses keyword '%s' which is incompatible with '%s'\n",
326 file, line, LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100327 return ERR_WARN;
328 }
329 if (!acl_cond_kw_conflicts(cond, where, &acl, &kw))
Willy Tarreaufdb563c2010-01-31 15:43:27 +0100330 return 0;
331
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100332 if (acl->name && *acl->name)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100333 ha_warning("parsing [%s:%d] : acl '%s' involves keywords '%s' which is incompatible with '%s'\n",
334 file, line, acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100335 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100336 ha_warning("parsing [%s:%d] : anonymous acl involves keyword '%s' which is incompatible with '%s'\n",
337 file, line, kw, sample_ckp_names(where));
Willy Tarreaufdb563c2010-01-31 15:43:27 +0100338 return ERR_WARN;
339}
340
Christopher Faulet581db2b2021-03-26 10:02:46 +0100341/* Report it if an ACL uses a L6 sample fetch from an HTTP proxy. It returns
342 * either 0 or ERR_WARN so that its result can be or'ed with err_code. Note that
343 * <cond> may be NULL and then will be ignored.
344*/
345int warnif_tcp_http_cond(const struct proxy *px, const struct acl_cond *cond)
346{
347 if (!cond || px->mode != PR_MODE_HTTP)
348 return 0;
349
350 if (cond->use & (SMP_USE_L6REQ|SMP_USE_L6RES)) {
351 ha_warning("Proxy '%s': L6 sample fetches ignored on HTTP proxies (declared at %s:%d).\n",
352 px->id, cond->file, cond->line);
353 return ERR_WARN;
354 }
355 return 0;
356}
357
Willy Tarreaue2afcc42021-03-12 09:08:04 +0100358/* try to find in <list> the word that looks closest to <word> by counting
359 * transitions between letters, digits and other characters. Will return the
360 * best matching word if found, otherwise NULL. An optional array of extra
361 * words to compare may be passed in <extra>, but it must then be terminated
362 * by a NULL entry. If unused it may be NULL.
363 */
364const char *cfg_find_best_match(const char *word, const struct list *list, int section, const char **extra)
365{
366 uint8_t word_sig[1024]; // 0..25=letter, 26=digit, 27=other, 28=begin, 29=end
367 uint8_t list_sig[1024];
368 const struct cfg_kw_list *kwl;
369 int index;
370 const char *best_ptr = NULL;
371 int dist, best_dist = INT_MAX;
372
373 make_word_fingerprint(word_sig, word);
374 list_for_each_entry(kwl, list, list) {
375 for (index = 0; kwl->kw[index].kw != NULL; index++) {
376 if (kwl->kw[index].section != section)
377 continue;
378
379 make_word_fingerprint(list_sig, kwl->kw[index].kw);
380 dist = word_fingerprint_distance(word_sig, list_sig);
381 if (dist < best_dist) {
382 best_dist = dist;
383 best_ptr = kwl->kw[index].kw;
384 }
385 }
386 }
387
388 while (extra && *extra) {
389 make_word_fingerprint(list_sig, *extra);
390 dist = word_fingerprint_distance(word_sig, list_sig);
391 if (dist < best_dist) {
392 best_dist = dist;
393 best_ptr = *extra;
394 }
395 extra++;
396 }
397
398 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
399 best_ptr = NULL;
400 return best_ptr;
401}
402
Christopher Faulet62519022017-10-16 15:49:32 +0200403/* Parse a string representing a process number or a set of processes. It must
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100404 * be "all", "odd", "even", a number between 1 and <max> or a range with
Christopher Faulet5ab51772017-11-22 11:21:58 +0100405 * two such numbers delimited by a dash ('-'). On success, it returns
406 * 0. otherwise it returns 1 with an error message in <err>.
Christopher Faulet62519022017-10-16 15:49:32 +0200407 *
408 * Note: this function can also be used to parse a thread number or a set of
409 * threads.
410 */
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100411int parse_process_number(const char *arg, unsigned long *proc, int max, int *autoinc, char **err)
Christopher Faulet62519022017-10-16 15:49:32 +0200412{
Christopher Faulet26028f62017-11-22 15:01:51 +0100413 if (autoinc) {
414 *autoinc = 0;
415 if (strncmp(arg, "auto:", 5) == 0) {
416 arg += 5;
417 *autoinc = 1;
418 }
419 }
420
Christopher Faulet62519022017-10-16 15:49:32 +0200421 if (strcmp(arg, "all") == 0)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100422 *proc |= ~0UL;
Christopher Faulet62519022017-10-16 15:49:32 +0200423 else if (strcmp(arg, "odd") == 0)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100424 *proc |= ~0UL/3UL; /* 0x555....555 */
Christopher Faulet62519022017-10-16 15:49:32 +0200425 else if (strcmp(arg, "even") == 0)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100426 *proc |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Christopher Faulet62519022017-10-16 15:49:32 +0200427 else {
Christopher Faulet18cca782019-02-07 16:29:41 +0100428 const char *p, *dash = NULL;
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100429 unsigned int low, high;
430
Christopher Faulet18cca782019-02-07 16:29:41 +0100431 for (p = arg; *p; p++) {
432 if (*p == '-' && !dash)
433 dash = p;
Willy Tarreau90807112020-02-25 08:16:33 +0100434 else if (!isdigit((unsigned char)*p)) {
Christopher Faulet18cca782019-02-07 16:29:41 +0100435 memprintf(err, "'%s' is not a valid number/range.", arg);
436 return -1;
437 }
Christopher Faulet5ab51772017-11-22 11:21:58 +0100438 }
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100439
440 low = high = str2uic(arg);
Christopher Faulet18cca782019-02-07 16:29:41 +0100441 if (dash)
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100442 high = ((!*(dash+1)) ? max : str2uic(dash + 1));
Christopher Fauletff4121f2017-11-22 16:38:49 +0100443
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100444 if (high < low) {
445 unsigned int swap = low;
446 low = high;
447 high = swap;
448 }
449
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100450 if (low < 1 || low > max || high > max) {
Christopher Fauletcb6a9452017-11-22 16:50:41 +0100451 memprintf(err, "'%s' is not a valid number/range."
452 " It supports numbers from 1 to %d.\n",
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100453 arg, max);
Christopher Faulet5ab51772017-11-22 11:21:58 +0100454 return 1;
455 }
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100456
457 for (;low <= high; low++)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100458 *proc |= 1UL << (low-1);
Christopher Faulet62519022017-10-16 15:49:32 +0200459 }
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100460 *proc &= ~0UL >> (LONGBITS - max);
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100461
Christopher Faulet5ab51772017-11-22 11:21:58 +0100462 return 0;
Christopher Faulet62519022017-10-16 15:49:32 +0200463}
464
David Carlier7e351ee2017-12-01 09:14:02 +0000465#ifdef USE_CPU_AFFINITY
Christopher Faulet62519022017-10-16 15:49:32 +0200466/* Parse cpu sets. Each CPU set is either a unique number between 0 and
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200467 * ha_cpuset_size() - 1 or a range with two such numbers delimited by a dash
Amaury Denoyellea8082352021-04-06 16:46:15 +0200468 * ('-'). If <comma_allowed> is set, each CPU set can be a list of unique
469 * numbers or ranges separated by a comma. It is also possible to specify
470 * multiple cpu numbers or ranges in distinct argument in <args>. On success,
471 * it returns 0, otherwise it returns 1 with an error message in <err>.
Christopher Faulet62519022017-10-16 15:49:32 +0200472 */
Amaury Denoyellea8082352021-04-06 16:46:15 +0200473unsigned long parse_cpu_set(const char **args, struct hap_cpuset *cpu_set,
474 int comma_allowed, char **err)
Christopher Faulet62519022017-10-16 15:49:32 +0200475{
476 int cur_arg = 0;
Amaury Denoyellea8082352021-04-06 16:46:15 +0200477 const char *arg;
Christopher Faulet62519022017-10-16 15:49:32 +0200478
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200479 ha_cpuset_zero(cpu_set);
480
Amaury Denoyellea8082352021-04-06 16:46:15 +0200481 arg = args[cur_arg];
482 while (*arg) {
483 const char *dash, *comma;
Christopher Faulet62519022017-10-16 15:49:32 +0200484 unsigned int low, high;
485
Willy Tarreau90807112020-02-25 08:16:33 +0100486 if (!isdigit((unsigned char)*args[cur_arg])) {
Amaury Denoyellea8082352021-04-06 16:46:15 +0200487 memprintf(err, "'%s' is not a CPU range.", arg);
Christopher Faulet62519022017-10-16 15:49:32 +0200488 return -1;
489 }
490
Amaury Denoyellea8082352021-04-06 16:46:15 +0200491 low = high = str2uic(arg);
492
493 comma = comma_allowed ? strchr(arg, ',') : NULL;
494 dash = strchr(arg, '-');
495
496 if (dash && (!comma || dash < comma))
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200497 high = *(dash+1) ? str2uic(dash + 1) : ha_cpuset_size() - 1;
Christopher Faulet62519022017-10-16 15:49:32 +0200498
499 if (high < low) {
500 unsigned int swap = low;
501 low = high;
502 high = swap;
503 }
504
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200505 if (high >= ha_cpuset_size()) {
506 memprintf(err, "supports CPU numbers from 0 to %d.",
507 ha_cpuset_size() - 1);
Christopher Faulet62519022017-10-16 15:49:32 +0200508 return 1;
509 }
510
511 while (low <= high)
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200512 ha_cpuset_set(cpu_set, low++);
Christopher Faulet62519022017-10-16 15:49:32 +0200513
Amaury Denoyellea8082352021-04-06 16:46:15 +0200514 /* if a comma is present, parse the rest of the arg, else
515 * skip to the next arg */
516 arg = comma ? comma + 1 : args[++cur_arg];
Christopher Faulet62519022017-10-16 15:49:32 +0200517 }
518 return 0;
519}
David Carlier7e351ee2017-12-01 09:14:02 +0000520#endif
521
Frédéric Lécaille18251032019-01-11 11:07:15 +0100522/* Allocate and initialize the frontend of a "peers" section found in
523 * file <file> at line <linenum> with <id> as ID.
524 * Return 0 if succeeded, -1 if not.
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200525 * Note that this function may be called from "default-server"
526 * or "peer" lines.
Frédéric Lécaille18251032019-01-11 11:07:15 +0100527 */
528static int init_peers_frontend(const char *file, int linenum,
529 const char *id, struct peers *peers)
530{
531 struct proxy *p;
532
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200533 if (peers->peers_fe) {
534 p = peers->peers_fe;
535 goto out;
536 }
Frédéric Lécaille9492c4e2019-01-11 11:47:12 +0100537
Frédéric Lécaille18251032019-01-11 11:07:15 +0100538 p = calloc(1, sizeof *p);
539 if (!p) {
540 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
541 return -1;
542 }
543
544 init_new_proxy(p);
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200545 peers_setup_frontend(p);
Frédéric Lécaille18251032019-01-11 11:07:15 +0100546 p->parent = peers;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200547 /* Finally store this frontend. */
548 peers->peers_fe = p;
549
550 out:
551 if (id && !p->id)
552 p->id = strdup(id);
Frédéric Lécaille1055e682018-04-26 14:35:21 +0200553 free(p->conf.file);
Frédéric Lécaille18251032019-01-11 11:07:15 +0100554 p->conf.args.file = p->conf.file = strdup(file);
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100555 if (linenum != -1)
556 p->conf.args.line = p->conf.line = linenum;
Frédéric Lécaille18251032019-01-11 11:07:15 +0100557
558 return 0;
559}
Willy Tarreauade5ec42010-01-28 19:33:49 +0100560
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100561/* Only change ->file, ->line and ->arg struct bind_conf member values
562 * if already present.
563 */
564static struct bind_conf *bind_conf_uniq_alloc(struct proxy *p,
565 const char *file, int line,
566 const char *arg, struct xprt_ops *xprt)
567{
568 struct bind_conf *bind_conf;
569
570 if (!LIST_ISEMPTY(&p->conf.bind)) {
571 bind_conf = LIST_ELEM((&p->conf.bind)->n, typeof(bind_conf), by_fe);
572 free(bind_conf->file);
573 bind_conf->file = strdup(file);
574 bind_conf->line = line;
575 if (arg) {
576 free(bind_conf->arg);
577 bind_conf->arg = strdup(arg);
578 }
579 }
580 else {
581 bind_conf = bind_conf_alloc(p, file, line, arg, xprt);
582 }
583
584 return bind_conf;
585}
586
587/*
588 * Allocate a new struct peer parsed at line <linenum> in file <file>
589 * to be added to <peers>.
590 * Returns the new allocated structure if succeeded, NULL if not.
591 */
592static struct peer *cfg_peers_add_peer(struct peers *peers,
593 const char *file, int linenum,
594 const char *id, int local)
595{
596 struct peer *p;
597
598 p = calloc(1, sizeof *p);
599 if (!p) {
600 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
601 return NULL;
602 }
603
604 /* the peers are linked backwards first */
605 peers->count++;
606 p->next = peers->remote;
607 peers->remote = p;
608 p->conf.file = strdup(file);
609 p->conf.line = linenum;
610 p->last_change = now.tv_sec;
611 p->xprt = xprt_get(XPRT_RAW);
612 p->sock_init_arg = NULL;
613 HA_SPIN_INIT(&p->lock);
614 if (id)
615 p->id = strdup(id);
616 if (local) {
617 p->local = 1;
618 peers->local = p;
619 }
620
621 return p;
622}
623
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624/*
William Lallemand51097192015-04-14 16:35:22 +0200625 * Parse a line in a <listen>, <frontend> or <backend> section.
Willy Tarreau93893792009-07-23 13:19:11 +0200626 * Returns the error code, 0 if OK, or any combination of :
627 * - ERR_ABORT: must abort ASAP
628 * - ERR_FATAL: we can continue parsing but not start the service
629 * - ERR_WARN: a warning has been emitted
630 * - ERR_ALERT: an alert has been emitted
631 * Only the two first ones can stop processing, the two others are just
632 * indicators.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200633 */
Emeric Brun32da3c42010-09-23 18:39:19 +0200634int cfg_parse_peers(const char *file, int linenum, char **args, int kwm)
635{
636 static struct peers *curpeers = NULL;
637 struct peer *newpeer = NULL;
638 const char *err;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200639 struct bind_conf *bind_conf;
640 struct listener *l;
Emeric Brun32da3c42010-09-23 18:39:19 +0200641 int err_code = 0;
Willy Tarreau902636f2013-03-10 19:44:48 +0100642 char *errmsg = NULL;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100643 static int bind_line, peer_line;
644
645 if (strcmp(args[0], "bind") == 0 || strcmp(args[0], "default-bind") == 0) {
646 int cur_arg;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100647 struct bind_conf *bind_conf;
648 struct bind_kw *kw;
Emeric Brun32da3c42010-09-23 18:39:19 +0200649
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100650 cur_arg = 1;
651
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200652 if (init_peers_frontend(file, linenum, NULL, curpeers) != 0) {
653 err_code |= ERR_ALERT | ERR_ABORT;
654 goto out;
655 }
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100656
657 bind_conf = bind_conf_uniq_alloc(curpeers->peers_fe, file, linenum,
658 NULL, xprt_get(XPRT_RAW));
659 if (*args[0] == 'b') {
660 struct listener *l;
661
662 if (peer_line) {
663 ha_alert("parsing [%s:%d] : mixing \"peer\" and \"bind\" line is forbidden\n", file, linenum);
664 err_code |= ERR_ALERT | ERR_FATAL;
665 goto out;
666 }
667
668 if (!str2listener(args[1], curpeers->peers_fe, bind_conf, file, linenum, &errmsg)) {
669 if (errmsg && *errmsg) {
670 indent_msg(&errmsg, 2);
671 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
672 }
673 else
674 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
675 file, linenum, args[0], args[1], args[2]);
676 err_code |= ERR_FATAL;
677 goto out;
678 }
679 l = LIST_ELEM(bind_conf->listeners.n, typeof(l), by_bind);
680 l->maxaccept = 1;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100681 l->accept = session_accept_fd;
682 l->analysers |= curpeers->peers_fe->fe_req_ana;
683 l->default_target = curpeers->peers_fe->default_target;
684 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
Willy Tarreau18215cb2019-02-27 16:25:28 +0100685 global.maxsock++; /* for the listening socket */
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100686
687 bind_line = 1;
688 if (cfg_peers->local) {
689 newpeer = cfg_peers->local;
690 }
691 else {
692 /* This peer is local.
693 * Note that we do not set the peer ID. This latter is initialized
694 * when parsing "peer" or "server" line.
695 */
696 newpeer = cfg_peers_add_peer(curpeers, file, linenum, NULL, 1);
697 if (!newpeer) {
698 err_code |= ERR_ALERT | ERR_ABORT;
699 goto out;
700 }
701 }
Willy Tarreau37159062020-08-27 07:48:42 +0200702 newpeer->addr = l->rx.addr;
Willy Tarreau5fc93282020-09-16 18:25:03 +0200703 newpeer->proto = l->rx.proto;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100704 cur_arg++;
705 }
706
707 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
708 int ret;
709
710 ret = kw->parse(args, cur_arg, curpeers->peers_fe, bind_conf, &errmsg);
711 err_code |= ret;
712 if (ret) {
713 if (errmsg && *errmsg) {
714 indent_msg(&errmsg, 2);
715 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
716 }
717 else
718 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
719 file, linenum, args[cur_arg]);
720 if (ret & ERR_FATAL)
721 goto out;
722 }
723 cur_arg += 1 + kw->skip;
724 }
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100725 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +0100726 const char *best = bind_find_best_kw(args[cur_arg]);
727 if (best)
728 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
729 file, linenum, args[cur_arg], cursection, best);
730 else
731 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
732 file, linenum, args[cur_arg], cursection);
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100733 err_code |= ERR_ALERT | ERR_FATAL;
734 goto out;
735 }
736 }
737 else if (strcmp(args[0], "default-server") == 0) {
738 if (init_peers_frontend(file, -1, NULL, curpeers) != 0) {
739 err_code |= ERR_ALERT | ERR_ABORT;
740 goto out;
741 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100742 err_code |= parse_server(file, linenum, args, curpeers->peers_fe, NULL,
743 SRV_PARSE_DEFAULT_SERVER|SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_INITIAL_RESOLVE);
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200744 }
Frédéric Lécailleb6f759b2019-11-05 09:57:45 +0100745 else if (strcmp(args[0], "log") == 0) {
746 if (init_peers_frontend(file, linenum, NULL, curpeers) != 0) {
747 err_code |= ERR_ALERT | ERR_ABORT;
748 goto out;
749 }
Emeric Brun9533a702021-04-02 10:13:43 +0200750 if (!parse_logsrv(args, &curpeers->peers_fe->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Frédéric Lécailleb6f759b2019-11-05 09:57:45 +0100751 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
752 err_code |= ERR_ALERT | ERR_FATAL;
753 goto out;
754 }
755 }
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200756 else if (strcmp(args[0], "peers") == 0) { /* new peers section */
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100757 /* Initialize these static variables when entering a new "peers" section*/
758 bind_line = peer_line = 0;
Willy Tarreau0dbbf312013-03-05 11:31:55 +0100759 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100760 ha_alert("parsing [%s:%d] : missing name for peers section.\n", file, linenum);
Willy Tarreau54984722014-02-16 08:20:13 +0100761 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau0dbbf312013-03-05 11:31:55 +0100762 goto out;
763 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200764
William Lallemand6e62fb62015-04-28 16:55:23 +0200765 if (alertif_too_many_args(1, file, linenum, args, &err_code))
766 goto out;
767
Emeric Brun32da3c42010-09-23 18:39:19 +0200768 err = invalid_char(args[1]);
769 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100770 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
771 file, linenum, *err, args[0], args[1]);
Willy Tarreau54984722014-02-16 08:20:13 +0100772 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau0dbbf312013-03-05 11:31:55 +0100773 goto out;
Emeric Brun32da3c42010-09-23 18:39:19 +0200774 }
775
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200776 for (curpeers = cfg_peers; curpeers != NULL; curpeers = curpeers->next) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200777 /*
778 * If there are two proxies with the same name only following
779 * combinations are allowed:
780 */
781 if (strcmp(curpeers->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100782 ha_alert("Parsing [%s:%d]: peers section '%s' has the same name as another peers section declared at %s:%d.\n",
783 file, linenum, args[1], curpeers->conf.file, curpeers->conf.line);
Willy Tarreau911fa2e2015-05-26 10:35:50 +0200784 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun32da3c42010-09-23 18:39:19 +0200785 }
786 }
787
Vincent Bernat02779b62016-04-03 13:48:43 +0200788 if ((curpeers = calloc(1, sizeof(*curpeers))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100789 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Emeric Brun32da3c42010-09-23 18:39:19 +0200790 err_code |= ERR_ALERT | ERR_ABORT;
791 goto out;
792 }
793
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200794 curpeers->next = cfg_peers;
795 cfg_peers = curpeers;
Willy Tarreau8113a5d2012-10-04 08:01:43 +0200796 curpeers->conf.file = strdup(file);
Emeric Brun32da3c42010-09-23 18:39:19 +0200797 curpeers->conf.line = linenum;
798 curpeers->last_change = now.tv_sec;
799 curpeers->id = strdup(args[1]);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +0200800 curpeers->disabled = 0;
Emeric Brun32da3c42010-09-23 18:39:19 +0200801 }
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200802 else if (strcmp(args[0], "peer") == 0 ||
803 strcmp(args[0], "server") == 0) { /* peer or server definition */
Frédéric Lécaille04636b72019-01-31 06:48:16 +0100804 int local_peer, peer;
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100805 int parse_addr = 0;
Emeric Brun32da3c42010-09-23 18:39:19 +0200806
Frédéric Lécaille04636b72019-01-31 06:48:16 +0100807 peer = *args[0] == 'p';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100808 local_peer = strcmp(args[1], localpeer) == 0;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100809 /* The local peer may have already partially been parsed on a "bind" line. */
810 if (*args[0] == 'p') {
811 if (bind_line) {
812 ha_alert("parsing [%s:%d] : mixing \"peer\" and \"bind\" line is forbidden\n", file, linenum);
813 err_code |= ERR_ALERT | ERR_FATAL;
814 goto out;
815 }
816 peer_line = 1;
817 }
818 if (cfg_peers->local && !cfg_peers->local->id && local_peer) {
819 /* The local peer has already been initialized on a "bind" line.
820 * Let's use it and store its ID.
821 */
822 newpeer = cfg_peers->local;
823 newpeer->id = strdup(localpeer);
824 }
825 else {
826 if (local_peer && cfg_peers->local) {
827 ha_alert("parsing [%s:%d] : '%s %s' : local peer name already referenced at %s:%d. %s\n",
828 file, linenum, args[0], args[1],
829 curpeers->peers_fe->conf.file, curpeers->peers_fe->conf.line, cfg_peers->local->id);
830 err_code |= ERR_FATAL;
831 goto out;
832 }
833 newpeer = cfg_peers_add_peer(curpeers, file, linenum, args[1], local_peer);
834 if (!newpeer) {
835 err_code |= ERR_ALERT | ERR_ABORT;
836 goto out;
837 }
838 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200839
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100840 /* Line number and peer ID are updated only if this peer is the local one. */
841 if (init_peers_frontend(file,
842 newpeer->local ? linenum: -1,
843 newpeer->local ? newpeer->id : NULL,
844 curpeers) != 0) {
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200845 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreaub36487e2013-03-10 18:37:42 +0100846 goto out;
847 }
Willy Tarreau2aa38802013-02-20 19:20:59 +0100848
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100849 /* This initializes curpeer->peers->peers_fe->srv.
850 * The server address is parsed only if we are parsing a "peer" line,
851 * or if we are parsing a "server" line and the current peer is not the local one.
852 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100853 parse_addr = (peer || !local_peer) ? SRV_PARSE_PARSE_ADDR : 0;
854 err_code |= parse_server(file, linenum, args, curpeers->peers_fe, NULL,
855 SRV_PARSE_IN_PEER_SECTION|parse_addr|SRV_PARSE_INITIAL_RESOLVE);
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +0200856 if (!curpeers->peers_fe->srv) {
857 /* Remove the newly allocated peer. */
858 if (newpeer != curpeers->local) {
859 struct peer *p;
860
861 p = curpeers->remote;
862 curpeers->remote = curpeers->remote->next;
863 free(p->id);
864 free(p);
865 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200866 goto out;
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +0200867 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200868
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100869 /* If the peer address has just been parsed, let's copy it to <newpeer>
870 * and initializes ->proto.
871 */
872 if (peer || !local_peer) {
873 newpeer->addr = curpeers->peers_fe->srv->addr;
874 newpeer->proto = protocol_by_family(newpeer->addr.ss_family);
875 }
876
Willy Tarreaua261e9b2016-12-22 20:44:00 +0100877 newpeer->xprt = xprt_get(XPRT_RAW);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200878 newpeer->sock_init_arg = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100879 HA_SPIN_INIT(&newpeer->lock);
Willy Tarreau26d8c592012-05-07 18:12:14 +0200880
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100881 newpeer->srv = curpeers->peers_fe->srv;
882 if (!newpeer->local)
Frédéric Lécaille6617e762018-04-25 15:13:38 +0200883 goto out;
884
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100885 /* The lines above are reserved to "peer" lines. */
886 if (*args[0] == 's')
Frédéric Lécaille4ba51982018-04-25 15:32:18 +0200887 goto out;
Emeric Brun32da3c42010-09-23 18:39:19 +0200888
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100889 bind_conf = bind_conf_uniq_alloc(curpeers->peers_fe, file, linenum, args[2], xprt_get(XPRT_RAW));
Frédéric Lécaille16e49102019-01-11 11:27:16 +0100890
Frédéric Lécaille91694d52019-01-11 11:43:53 +0100891 if (!str2listener(args[2], curpeers->peers_fe, bind_conf, file, linenum, &errmsg)) {
892 if (errmsg && *errmsg) {
893 indent_msg(&errmsg, 2);
894 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Frédéric Lécaille16e49102019-01-11 11:27:16 +0100895 }
Frédéric Lécaille91694d52019-01-11 11:43:53 +0100896 else
897 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
898 file, linenum, args[0], args[1], args[2]);
899 err_code |= ERR_FATAL;
900 goto out;
901 }
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100902
903 l = LIST_ELEM(bind_conf->listeners.n, typeof(l), by_bind);
904 l->maxaccept = 1;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100905 l->accept = session_accept_fd;
906 l->analysers |= curpeers->peers_fe->fe_req_ana;
907 l->default_target = curpeers->peers_fe->default_target;
908 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
Willy Tarreau18215cb2019-02-27 16:25:28 +0100909 global.maxsock++; /* for the listening socket */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100910 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100911 else if (strcmp(args[0], "table") == 0) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100912 struct stktable *t, *other;
913 char *id;
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100914 size_t prefix_len;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100915
916 /* Line number and peer ID are updated only if this peer is the local one. */
917 if (init_peers_frontend(file, -1, NULL, curpeers) != 0) {
918 err_code |= ERR_ALERT | ERR_ABORT;
919 goto out;
920 }
921
922 other = stktable_find_by_name(args[1]);
923 if (other) {
924 ha_alert("parsing [%s:%d] : stick-table name '%s' conflicts with table declared in %s '%s' at %s:%d.\n",
925 file, linenum, args[1],
926 other->proxy ? proxy_cap_str(other->proxy->cap) : "peers",
927 other->proxy ? other->id : other->peers.p->id,
928 other->conf.file, other->conf.line);
929 err_code |= ERR_ALERT | ERR_FATAL;
930 goto out;
931 }
932
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100933 /* Build the stick-table name, concatenating the "peers" section name
934 * followed by a '/' character and the table name argument.
935 */
936 chunk_reset(&trash);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100937 if (!chunk_strcpy(&trash, curpeers->id)) {
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100938 ha_alert("parsing [%s:%d]: '%s %s' : stick-table name too long.\n",
939 file, linenum, args[0], args[1]);
940 err_code |= ERR_ALERT | ERR_FATAL;
941 goto out;
942 }
943
944 prefix_len = trash.data;
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100945 if (!chunk_memcat(&trash, "/", 1) || !chunk_strcat(&trash, args[1])) {
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100946 ha_alert("parsing [%s:%d]: '%s %s' : stick-table name too long.\n",
947 file, linenum, args[0], args[1]);
948 err_code |= ERR_ALERT | ERR_FATAL;
949 goto out;
950 }
951
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100952 t = calloc(1, sizeof *t);
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100953 id = strdup(trash.area);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100954 if (!t || !id) {
955 ha_alert("parsing [%s:%d]: '%s %s' : memory allocation failed\n",
956 file, linenum, args[0], args[1]);
Eric Salama1aab9112020-09-18 11:55:17 +0200957 free(t);
958 free(id);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100959 err_code |= ERR_ALERT | ERR_FATAL;
960 goto out;
961 }
962
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100963 err_code |= parse_stick_table(file, linenum, args, t, id, id + prefix_len, curpeers);
Eric Salama1aab9112020-09-18 11:55:17 +0200964 if (err_code & ERR_FATAL) {
965 free(t);
966 free(id);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100967 goto out;
Eric Salama1aab9112020-09-18 11:55:17 +0200968 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100969
970 stktable_store_name(t);
971 t->next = stktables_list;
972 stktables_list = t;
973 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100974 else if (strcmp(args[0], "disabled") == 0) { /* disables this peers section */
Willy Tarreau1ad64ac2020-09-24 08:48:08 +0200975 curpeers->disabled = 1;
Willy Tarreau77e4bd12015-05-01 20:02:17 +0200976 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100977 else if (strcmp(args[0], "enabled") == 0) { /* enables this peers section (used to revert a disabled default) */
Willy Tarreau1ad64ac2020-09-24 08:48:08 +0200978 curpeers->disabled = 0;
Willy Tarreau77e4bd12015-05-01 20:02:17 +0200979 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200980 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100981 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
Emeric Brun32da3c42010-09-23 18:39:19 +0200982 err_code |= ERR_ALERT | ERR_FATAL;
983 goto out;
984 }
985
986out:
Willy Tarreau902636f2013-03-10 19:44:48 +0100987 free(errmsg);
Emeric Brun32da3c42010-09-23 18:39:19 +0200988 return err_code;
989}
990
Baptiste Assmann325137d2015-04-13 23:40:55 +0200991/*
William Lallemand51097192015-04-14 16:35:22 +0200992 * Parse a line in a <listen>, <frontend> or <backend> section.
Simon Horman0d16a402015-01-30 11:22:58 +0900993 * Returns the error code, 0 if OK, or any combination of :
994 * - ERR_ABORT: must abort ASAP
995 * - ERR_FATAL: we can continue parsing but not start the service
996 * - ERR_WARN: a warning has been emitted
997 * - ERR_ALERT: an alert has been emitted
998 * Only the two first ones can stop processing, the two others are just
999 * indicators.
1000 */
1001int cfg_parse_mailers(const char *file, int linenum, char **args, int kwm)
1002{
1003 static struct mailers *curmailers = NULL;
1004 struct mailer *newmailer = NULL;
1005 const char *err;
1006 int err_code = 0;
1007 char *errmsg = NULL;
1008
1009 if (strcmp(args[0], "mailers") == 0) { /* new mailers section */
1010 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001011 ha_alert("parsing [%s:%d] : missing name for mailers section.\n", file, linenum);
Simon Horman0d16a402015-01-30 11:22:58 +09001012 err_code |= ERR_ALERT | ERR_ABORT;
1013 goto out;
1014 }
1015
1016 err = invalid_char(args[1]);
1017 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001018 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1019 file, linenum, *err, args[0], args[1]);
Simon Horman0d16a402015-01-30 11:22:58 +09001020 err_code |= ERR_ALERT | ERR_ABORT;
1021 goto out;
1022 }
1023
1024 for (curmailers = mailers; curmailers != NULL; curmailers = curmailers->next) {
1025 /*
1026 * If there are two proxies with the same name only following
1027 * combinations are allowed:
1028 */
1029 if (strcmp(curmailers->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001030 ha_alert("Parsing [%s:%d]: mailers section '%s' has the same name as another mailers section declared at %s:%d.\n",
1031 file, linenum, args[1], curmailers->conf.file, curmailers->conf.line);
Willy Tarreau911fa2e2015-05-26 10:35:50 +02001032 err_code |= ERR_ALERT | ERR_FATAL;
Simon Horman0d16a402015-01-30 11:22:58 +09001033 }
1034 }
1035
Vincent Bernat02779b62016-04-03 13:48:43 +02001036 if ((curmailers = calloc(1, sizeof(*curmailers))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001037 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Simon Horman0d16a402015-01-30 11:22:58 +09001038 err_code |= ERR_ALERT | ERR_ABORT;
1039 goto out;
1040 }
1041
1042 curmailers->next = mailers;
1043 mailers = curmailers;
1044 curmailers->conf.file = strdup(file);
1045 curmailers->conf.line = linenum;
1046 curmailers->id = strdup(args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001047 curmailers->timeout.mail = DEF_MAILALERTTIME;/* XXX: Would like to Skip to the next alert, if any, ASAP.
1048 * But need enough time so that timeouts don't occur
1049 * during tcp procssing. For now just us an arbitrary default. */
Simon Horman0d16a402015-01-30 11:22:58 +09001050 }
1051 else if (strcmp(args[0], "mailer") == 0) { /* mailer definition */
1052 struct sockaddr_storage *sk;
1053 int port1, port2;
1054 struct protocol *proto;
1055
1056 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001057 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1058 file, linenum, args[0]);
Simon Horman0d16a402015-01-30 11:22:58 +09001059 err_code |= ERR_ALERT | ERR_FATAL;
1060 goto out;
1061 }
1062
1063 err = invalid_char(args[1]);
1064 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001065 ha_alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1066 file, linenum, *err, args[1]);
Simon Horman0d16a402015-01-30 11:22:58 +09001067 err_code |= ERR_ALERT | ERR_FATAL;
1068 goto out;
1069 }
1070
Vincent Bernat02779b62016-04-03 13:48:43 +02001071 if ((newmailer = calloc(1, sizeof(*newmailer))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001072 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Simon Horman0d16a402015-01-30 11:22:58 +09001073 err_code |= ERR_ALERT | ERR_ABORT;
1074 goto out;
1075 }
1076
1077 /* the mailers are linked backwards first */
1078 curmailers->count++;
1079 newmailer->next = curmailers->mailer_list;
1080 curmailers->mailer_list = newmailer;
1081 newmailer->mailers = curmailers;
1082 newmailer->conf.file = strdup(file);
1083 newmailer->conf.line = linenum;
1084
1085 newmailer->id = strdup(args[1]);
1086
Willy Tarreau5fc93282020-09-16 18:25:03 +02001087 sk = str2sa_range(args[2], NULL, &port1, &port2, NULL, &proto,
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001088 &errmsg, NULL, NULL,
1089 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
Simon Horman0d16a402015-01-30 11:22:58 +09001090 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001091 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Simon Horman0d16a402015-01-30 11:22:58 +09001092 err_code |= ERR_ALERT | ERR_FATAL;
1093 goto out;
1094 }
1095
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001096 if (proto->sock_prot != IPPROTO_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001097 ha_alert("parsing [%s:%d] : '%s %s' : TCP not supported for this address family.\n",
1098 file, linenum, args[0], args[1]);
Simon Horman0d16a402015-01-30 11:22:58 +09001099 err_code |= ERR_ALERT | ERR_FATAL;
1100 goto out;
1101 }
1102
Simon Horman0d16a402015-01-30 11:22:58 +09001103 newmailer->addr = *sk;
1104 newmailer->proto = proto;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001105 newmailer->xprt = xprt_get(XPRT_RAW);
Simon Horman0d16a402015-01-30 11:22:58 +09001106 newmailer->sock_init_arg = NULL;
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001107 }
1108 else if (strcmp(args[0], "timeout") == 0) {
1109 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001110 ha_alert("parsing [%s:%d] : '%s' expects 'mail' and <time> as arguments.\n",
1111 file, linenum, args[0]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001112 err_code |= ERR_ALERT | ERR_FATAL;
1113 goto out;
1114 }
1115 else if (strcmp(args[1], "mail") == 0) {
1116 const char *res;
1117 unsigned int timeout_mail;
1118 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001119 ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
1120 file, linenum, args[0], args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001121 err_code |= ERR_ALERT | ERR_FATAL;
1122 goto out;
1123 }
1124 res = parse_time_err(args[2], &timeout_mail, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001125 if (res == PARSE_TIME_OVER) {
1126 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
1127 file, linenum, args[2], args[0], args[1]);
1128 err_code |= ERR_ALERT | ERR_FATAL;
1129 goto out;
1130 }
1131 else if (res == PARSE_TIME_UNDER) {
1132 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
1133 file, linenum, args[2], args[0], args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001134 err_code |= ERR_ALERT | ERR_FATAL;
1135 goto out;
1136 }
Willy Tarreau9faebe32019-06-07 19:00:37 +02001137 else if (res) {
1138 ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
1139 file, linenum, *res, args[0], args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001140 err_code |= ERR_ALERT | ERR_FATAL;
1141 goto out;
1142 }
1143 curmailers->timeout.mail = timeout_mail;
1144 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001145 ha_alert("parsing [%s:%d] : '%s' expects 'mail' and <time> as arguments got '%s'.\n",
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001146 file, linenum, args[0], args[1]);
1147 err_code |= ERR_ALERT | ERR_FATAL;
1148 goto out;
1149 }
1150 }
Simon Horman0d16a402015-01-30 11:22:58 +09001151 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001152 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
Simon Horman0d16a402015-01-30 11:22:58 +09001153 err_code |= ERR_ALERT | ERR_FATAL;
1154 goto out;
1155 }
1156
1157out:
1158 free(errmsg);
1159 return err_code;
1160}
1161
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +01001162void free_email_alert(struct proxy *p)
Simon Horman9dc49962015-01-30 11:22:59 +09001163{
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001164 ha_free(&p->email_alert.mailers.name);
1165 ha_free(&p->email_alert.from);
1166 ha_free(&p->email_alert.to);
1167 ha_free(&p->email_alert.myhostname);
Simon Horman9dc49962015-01-30 11:22:59 +09001168}
1169
Willy Tarreaubaaee002006-06-26 02:48:02 +02001170
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001171int
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001172cfg_parse_netns(const char *file, int linenum, char **args, int kwm)
1173{
Willy Tarreaue5733232019-05-22 19:24:06 +02001174#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001175 const char *err;
1176 const char *item = args[0];
1177
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001178 if (strcmp(item, "namespace_list") == 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001179 return 0;
1180 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001181 else if (strcmp(item, "namespace") == 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001182 size_t idx = 1;
1183 const char *current;
1184 while (*(current = args[idx++])) {
1185 err = invalid_char(current);
1186 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001187 ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1188 file, linenum, *err, item, current);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001189 return ERR_ALERT | ERR_FATAL;
1190 }
1191
1192 if (netns_store_lookup(current, strlen(current))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001193 ha_alert("parsing [%s:%d]: Namespace '%s' is already added.\n",
1194 file, linenum, current);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001195 return ERR_ALERT | ERR_FATAL;
1196 }
1197 if (!netns_store_insert(current)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001198 ha_alert("parsing [%s:%d]: Cannot open namespace '%s'.\n",
1199 file, linenum, current);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001200 return ERR_ALERT | ERR_FATAL;
1201 }
1202 }
1203 }
1204
1205 return 0;
1206#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001207 ha_alert("parsing [%s:%d]: namespace support is not compiled in.",
1208 file, linenum);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001209 return ERR_ALERT | ERR_FATAL;
1210#endif
1211}
1212
1213int
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001214cfg_parse_users(const char *file, int linenum, char **args, int kwm)
1215{
1216
1217 int err_code = 0;
1218 const char *err;
1219
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001220 if (strcmp(args[0], "userlist") == 0) { /* new userlist */
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001221 struct userlist *newul;
1222
1223 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001224 ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1225 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001226 err_code |= ERR_ALERT | ERR_FATAL;
1227 goto out;
1228 }
William Lallemand6e62fb62015-04-28 16:55:23 +02001229 if (alertif_too_many_args(1, file, linenum, args, &err_code))
1230 goto out;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001231
1232 err = invalid_char(args[1]);
1233 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001234 ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1235 file, linenum, *err, args[0], args[1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001236 err_code |= ERR_ALERT | ERR_FATAL;
1237 goto out;
1238 }
1239
1240 for (newul = userlist; newul; newul = newul->next)
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001241 if (strcmp(newul->name, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001242 ha_warning("parsing [%s:%d]: ignoring duplicated userlist '%s'.\n",
1243 file, linenum, args[1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001244 err_code |= ERR_WARN;
1245 goto out;
1246 }
1247
Vincent Bernat02779b62016-04-03 13:48:43 +02001248 newul = calloc(1, sizeof(*newul));
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001249 if (!newul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001250 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001251 err_code |= ERR_ALERT | ERR_ABORT;
1252 goto out;
1253 }
1254
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001255 newul->name = strdup(args[1]);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001256 if (!newul->name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001257 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001258 err_code |= ERR_ALERT | ERR_ABORT;
David Carlier97880bb2016-04-08 10:35:26 +01001259 free(newul);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001260 goto out;
1261 }
1262
1263 newul->next = userlist;
1264 userlist = newul;
1265
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001266 } else if (strcmp(args[0], "group") == 0) { /* new group */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001267 int cur_arg;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001268 const char *err;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001269 struct auth_groups *ag;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001270
1271 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001272 ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1273 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001274 err_code |= ERR_ALERT | ERR_FATAL;
1275 goto out;
1276 }
1277
1278 err = invalid_char(args[1]);
1279 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001280 ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1281 file, linenum, *err, args[0], args[1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001282 err_code |= ERR_ALERT | ERR_FATAL;
1283 goto out;
1284 }
1285
William Lallemand4ac9f542015-05-28 18:03:51 +02001286 if (!userlist)
1287 goto out;
1288
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001289 for (ag = userlist->groups; ag; ag = ag->next)
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001290 if (strcmp(ag->name, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001291 ha_warning("parsing [%s:%d]: ignoring duplicated group '%s' in userlist '%s'.\n",
1292 file, linenum, args[1], userlist->name);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001293 err_code |= ERR_ALERT;
1294 goto out;
1295 }
1296
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001297 ag = calloc(1, sizeof(*ag));
1298 if (!ag) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001299 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001300 err_code |= ERR_ALERT | ERR_ABORT;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001301 goto out;
1302 }
1303
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001304 ag->name = strdup(args[1]);
David Carlier70d60452016-08-22 23:27:42 +01001305 if (!ag->name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001306 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001307 err_code |= ERR_ALERT | ERR_ABORT;
David Carlier70d60452016-08-22 23:27:42 +01001308 free(ag);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001309 goto out;
1310 }
1311
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001312 cur_arg = 2;
1313
1314 while (*args[cur_arg]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001315 if (strcmp(args[cur_arg], "users") == 0) {
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001316 ag->groupusers = strdup(args[cur_arg + 1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001317 cur_arg += 2;
1318 continue;
1319 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001320 ha_alert("parsing [%s:%d]: '%s' only supports 'users' option.\n",
1321 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001322 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier70d60452016-08-22 23:27:42 +01001323 free(ag->groupusers);
1324 free(ag->name);
1325 free(ag);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001326 goto out;
1327 }
1328 }
1329
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001330 ag->next = userlist->groups;
1331 userlist->groups = ag;
1332
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001333 } else if (strcmp(args[0], "user") == 0) { /* new user */
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001334 struct auth_users *newuser;
1335 int cur_arg;
1336
1337 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001338 ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1339 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001340 err_code |= ERR_ALERT | ERR_FATAL;
1341 goto out;
1342 }
William Lallemand4ac9f542015-05-28 18:03:51 +02001343 if (!userlist)
1344 goto out;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001345
1346 for (newuser = userlist->users; newuser; newuser = newuser->next)
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001347 if (strcmp(newuser->user, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001348 ha_warning("parsing [%s:%d]: ignoring duplicated user '%s' in userlist '%s'.\n",
1349 file, linenum, args[1], userlist->name);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001350 err_code |= ERR_ALERT;
1351 goto out;
1352 }
1353
Vincent Bernat02779b62016-04-03 13:48:43 +02001354 newuser = calloc(1, sizeof(*newuser));
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001355 if (!newuser) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001356 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001357 err_code |= ERR_ALERT | ERR_ABORT;
1358 goto out;
1359 }
1360
1361 newuser->user = strdup(args[1]);
1362
1363 newuser->next = userlist->users;
1364 userlist->users = newuser;
1365
1366 cur_arg = 2;
1367
1368 while (*args[cur_arg]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001369 if (strcmp(args[cur_arg], "password") == 0) {
Willy Tarreaue5733232019-05-22 19:24:06 +02001370#ifdef USE_LIBCRYPT
Cyril Bonté1a0191d2014-08-29 20:20:02 +02001371 if (!crypt("", args[cur_arg + 1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001372 ha_alert("parsing [%s:%d]: the encrypted password used for user '%s' is not supported by crypt(3).\n",
1373 file, linenum, newuser->user);
Cyril Bonté1a0191d2014-08-29 20:20:02 +02001374 err_code |= ERR_ALERT | ERR_FATAL;
1375 goto out;
1376 }
1377#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001378 ha_warning("parsing [%s:%d]: no crypt(3) support compiled, encrypted passwords will not work.\n",
1379 file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001380 err_code |= ERR_ALERT;
1381#endif
1382 newuser->pass = strdup(args[cur_arg + 1]);
1383 cur_arg += 2;
1384 continue;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001385 } else if (strcmp(args[cur_arg], "insecure-password") == 0) {
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001386 newuser->pass = strdup(args[cur_arg + 1]);
1387 newuser->flags |= AU_O_INSECURE;
1388 cur_arg += 2;
1389 continue;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001390 } else if (strcmp(args[cur_arg], "groups") == 0) {
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001391 newuser->u.groups_names = strdup(args[cur_arg + 1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001392 cur_arg += 2;
1393 continue;
1394 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001395 ha_alert("parsing [%s:%d]: '%s' only supports 'password', 'insecure-password' and 'groups' options.\n",
1396 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001397 err_code |= ERR_ALERT | ERR_FATAL;
1398 goto out;
1399 }
1400 }
1401 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001402 ha_alert("parsing [%s:%d]: unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "users");
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001403 err_code |= ERR_ALERT | ERR_FATAL;
1404 }
1405
1406out:
1407 return err_code;
1408}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001409
Christopher Faulet79bdef32016-11-04 22:36:15 +01001410int
1411cfg_parse_scope(const char *file, int linenum, char *line)
1412{
1413 char *beg, *end, *scope = NULL;
1414 int err_code = 0;
1415 const char *err;
1416
1417 beg = line + 1;
1418 end = strchr(beg, ']');
1419
1420 /* Detect end of scope declaration */
1421 if (!end || end == beg) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001422 ha_alert("parsing [%s:%d] : empty scope name is forbidden.\n",
1423 file, linenum);
Christopher Faulet79bdef32016-11-04 22:36:15 +01001424 err_code |= ERR_ALERT | ERR_FATAL;
1425 goto out;
1426 }
1427
1428 /* Get scope name and check its validity */
1429 scope = my_strndup(beg, end-beg);
1430 err = invalid_char(scope);
1431 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001432 ha_alert("parsing [%s:%d] : character '%c' is not permitted in a scope name.\n",
1433 file, linenum, *err);
Christopher Faulet79bdef32016-11-04 22:36:15 +01001434 err_code |= ERR_ALERT | ERR_ABORT;
1435 goto out;
1436 }
1437
1438 /* Be sure to have a scope declaration alone on its line */
1439 line = end+1;
1440 while (isspace((unsigned char)*line))
1441 line++;
1442 if (*line && *line != '#' && *line != '\n' && *line != '\r') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001443 ha_alert("parsing [%s:%d] : character '%c' is not permitted after scope declaration.\n",
1444 file, linenum, *line);
Christopher Faulet79bdef32016-11-04 22:36:15 +01001445 err_code |= ERR_ALERT | ERR_ABORT;
1446 goto out;
1447 }
1448
1449 /* We have a valid scope declaration, save it */
1450 free(cfg_scope);
1451 cfg_scope = scope;
1452 scope = NULL;
1453
1454 out:
1455 free(scope);
1456 return err_code;
1457}
1458
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01001459int
1460cfg_parse_track_sc_num(unsigned int *track_sc_num,
1461 const char *arg, const char *end, char **errmsg)
1462{
1463 const char *p;
1464 unsigned int num;
1465
1466 p = arg;
1467 num = read_uint64(&arg, end);
1468
1469 if (arg != end) {
1470 memprintf(errmsg, "Wrong track-sc number '%s'", p);
1471 return -1;
1472 }
1473
1474 if (num >= MAX_SESS_STKCTR) {
1475 memprintf(errmsg, "%u track-sc number exceeding "
1476 "%d (MAX_SESS_STKCTR-1) value", num, MAX_SESS_STKCTR - 1);
1477 return -1;
1478 }
1479
1480 *track_sc_num = num;
1481 return 0;
1482}
1483
Willy Tarreaubaaee002006-06-26 02:48:02 +02001484/*
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001485 * Detect a global section after a non-global one and output a diagnostic
1486 * warning.
1487 */
1488static void check_section_position(char *section_name,
1489 const char *file, int linenum,
1490 int *non_global_parsed)
1491{
1492 if (!strcmp(section_name, "global")) {
1493 if (*non_global_parsed == 1)
1494 _ha_diag_warning("parsing [%s:%d] : global section detected after a non-global one, the prevalence of their statements is unspecified\n", file, linenum);
1495 }
1496 else if (*non_global_parsed == 0) {
1497 *non_global_parsed = 1;
1498 }
1499}
1500
1501/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001502 * This function reads and parses the configuration file given in the argument.
Willy Tarreau058e9072009-07-20 09:30:05 +02001503 * Returns the error code, 0 if OK, or any combination of :
1504 * - ERR_ABORT: must abort ASAP
1505 * - ERR_FATAL: we can continue parsing but not start the service
1506 * - ERR_WARN: a warning has been emitted
1507 * - ERR_ALERT: an alert has been emitted
1508 * Only the two first ones can stop processing, the two others are just
1509 * indicators.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001510 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001511int readcfgfile(const char *file)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001512{
William Lallemand64e84512015-05-12 14:25:37 +02001513 char *thisline;
1514 int linesize = LINESIZE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001515 FILE *f;
1516 int linenum = 0;
Willy Tarreau058e9072009-07-20 09:30:05 +02001517 int err_code = 0;
William Lallemandd2ff56d2017-10-16 11:06:50 +02001518 struct cfg_section *cs = NULL, *pcs = NULL;
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001519 struct cfg_section *ics;
William Lallemand64e84512015-05-12 14:25:37 +02001520 int readbytes = 0;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001521 char *outline = NULL;
1522 size_t outlen = 0;
1523 size_t outlinesize = 0;
Willy Tarreau32234e72020-06-16 17:14:33 +02001524 int fatal = 0;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001525 int missing_lf = -1;
Willy Tarreau4b103022021-02-12 17:59:10 +01001526 int nested_cond_lvl = 0;
1527 enum nested_cond_state nested_conds[MAXNESTEDCONDS];
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001528 int non_global_section_parsed = 0;
William Lallemand64e84512015-05-12 14:25:37 +02001529
1530 if ((thisline = malloc(sizeof(*thisline) * linesize)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001531 ha_alert("parsing [%s] : out of memory.\n", file);
William Lallemand64e84512015-05-12 14:25:37 +02001532 return -1;
1533 }
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001534
David Carlier97880bb2016-04-08 10:35:26 +01001535 if ((f=fopen(file,"r")) == NULL) {
1536 free(thisline);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001537 return -1;
David Carlier97880bb2016-04-08 10:35:26 +01001538 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001539
William Lallemandb2f07452015-05-12 14:27:13 +02001540next_line:
William Lallemand64e84512015-05-12 14:25:37 +02001541 while (fgets(thisline + readbytes, linesize - readbytes, f) != NULL) {
Willy Tarreau3842f002009-06-14 11:39:52 +02001542 int arg, kwm = KWM_STD;
Krzysztof Piotr Oledzkie6bbd742007-11-01 00:33:12 +01001543 char *end;
1544 char *args[MAX_LINE_ARGS + 1];
1545 char *line = thisline;
1546
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001547 if (missing_lf != -1) {
Tim Duesterhusf92afb72020-08-18 22:00:04 +02001548 ha_alert("parsing [%s:%d]: Stray NUL character at position %d.\n",
1549 file, linenum, (missing_lf + 1));
1550 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001551 missing_lf = -1;
Tim Duesterhusf92afb72020-08-18 22:00:04 +02001552 break;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001553 }
1554
Willy Tarreaubaaee002006-06-26 02:48:02 +02001555 linenum++;
1556
Willy Tarreau32234e72020-06-16 17:14:33 +02001557 if (fatal >= 50) {
1558 ha_alert("parsing [%s:%d]: too many fatal errors (%d), stopping now.\n", file, linenum, fatal);
1559 break;
1560 }
1561
Willy Tarreaubaaee002006-06-26 02:48:02 +02001562 end = line + strlen(line);
1563
William Lallemand64e84512015-05-12 14:25:37 +02001564 if (end-line == linesize-1 && *(end-1) != '\n') {
Krzysztof Piotr Oledzkie6bbd742007-11-01 00:33:12 +01001565 /* Check if we reached the limit and the last char is not \n.
1566 * Watch out for the last line without the terminating '\n'!
1567 */
William Lallemand64e84512015-05-12 14:25:37 +02001568 char *newline;
1569 int newlinesize = linesize * 2;
1570
1571 newline = realloc(thisline, sizeof(*thisline) * newlinesize);
1572 if (newline == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001573 ha_alert("parsing [%s:%d]: line too long, cannot allocate memory.\n",
1574 file, linenum);
William Lallemand64e84512015-05-12 14:25:37 +02001575 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001576 fatal++;
Willy Tarreau40cb26f2020-06-25 09:37:54 +02001577 linenum--;
William Lallemand64e84512015-05-12 14:25:37 +02001578 continue;
1579 }
1580
1581 readbytes = linesize - 1;
1582 linesize = newlinesize;
1583 thisline = newline;
Willy Tarreau40cb26f2020-06-25 09:37:54 +02001584 linenum--;
William Lallemand64e84512015-05-12 14:25:37 +02001585 continue;
Krzysztof Piotr Oledzkie6bbd742007-11-01 00:33:12 +01001586 }
1587
William Lallemand64e84512015-05-12 14:25:37 +02001588 readbytes = 0;
1589
Willy Tarreau08488f62020-06-26 17:24:54 +02001590 if (end > line && *(end-1) == '\n') {
Tim Duesterhus70f58992020-06-22 22:57:44 +02001591 /* kill trailing LF */
1592 *(end - 1) = 0;
1593 }
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001594 else {
1595 /* mark this line as truncated */
1596 missing_lf = end - line;
1597 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001598
Willy Tarreaubaaee002006-06-26 02:48:02 +02001599 /* skip leading spaces */
Willy Tarreau8f8e6452007-06-17 21:51:38 +02001600 while (isspace((unsigned char)*line))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001601 line++;
William Lallemandf9873ba2015-05-05 17:37:14 +02001602
Joseph Herlanta14c03e2018-11-15 14:04:19 -08001603 if (*line == '[') {/* This is the beginning if a scope */
Christopher Faulet79bdef32016-11-04 22:36:15 +01001604 err_code |= cfg_parse_scope(file, linenum, line);
1605 goto next_line;
1606 }
1607
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001608 while (1) {
1609 uint32_t err;
1610 char *errptr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001611
Willy Tarreau61dd44b2020-06-25 07:35:42 +02001612 arg = MAX_LINE_ARGS + 1;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001613 outlen = outlinesize;
1614 err = parse_line(line, outline, &outlen, args, &arg,
1615 PARSE_OPT_ENV | PARSE_OPT_DQUOTE | PARSE_OPT_SQUOTE |
Amaury Denoyellefa41cb62020-10-01 14:32:35 +02001616 PARSE_OPT_BKSLASH | PARSE_OPT_SHARP | PARSE_OPT_WORD_EXPAND,
1617 &errptr);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001618
1619 if (err & PARSE_ERR_QUOTE) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001620 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1621
1622 ha_alert("parsing [%s:%d]: unmatched quote at position %d:\n"
1623 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001624 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001625 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001626 goto next_line;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001627 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001628
1629 if (err & PARSE_ERR_BRACE) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001630 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1631
1632 ha_alert("parsing [%s:%d]: unmatched brace in environment variable name at position %d:\n"
1633 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001634 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001635 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001636 goto next_line;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001637 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001638
1639 if (err & PARSE_ERR_VARNAME) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001640 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1641
1642 ha_alert("parsing [%s:%d]: forbidden first char in environment variable name at position %d:\n"
1643 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001644 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001645 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001646 goto next_line;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001647 }
William Lallemandb2f07452015-05-12 14:27:13 +02001648
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001649 if (err & PARSE_ERR_HEX) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001650 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1651
1652 ha_alert("parsing [%s:%d]: truncated or invalid hexadecimal sequence at position %d:\n"
1653 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001654 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus9f658a52020-06-16 18:14:21 +02001655 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001656 goto next_line;
1657 }
William Lallemandb2f07452015-05-12 14:27:13 +02001658
Amaury Denoyellefa41cb62020-10-01 14:32:35 +02001659 if (err & PARSE_ERR_WRONG_EXPAND) {
1660 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1661
1662 ha_alert("parsing [%s:%d]: truncated or invalid word expansion sequence at position %d:\n"
1663 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
1664 err_code |= ERR_ALERT | ERR_FATAL;
1665 fatal++;
1666 goto next_line;
1667 }
1668
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001669 if (err & (PARSE_ERR_TOOLARGE|PARSE_ERR_OVERLAP)) {
1670 outlinesize = (outlen + 1023) & -1024;
Ilya Shipitsin76837bc2021-01-07 22:45:13 +05001671 outline = my_realloc2(outline, outlinesize);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001672 if (outline == NULL) {
1673 ha_alert("parsing [%s:%d]: line too long, cannot allocate memory.\n",
1674 file, linenum);
William Lallemandb2f07452015-05-12 14:27:13 +02001675 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001676 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001677 goto next_line;
William Lallemandb2f07452015-05-12 14:27:13 +02001678 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001679 /* try again */
1680 continue;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001681 }
Willy Tarreau07d47062020-06-25 07:41:22 +02001682
1683 if (err & PARSE_ERR_TOOMANY) {
1684 /* only check this *after* being sure the output is allocated */
1685 ha_alert("parsing [%s:%d]: too many words, truncating after word %d, position %ld: <%s>.\n",
1686 file, linenum, MAX_LINE_ARGS, (long)(args[MAX_LINE_ARGS-1] - outline + 1), args[MAX_LINE_ARGS-1]);
1687 err_code |= ERR_ALERT | ERR_FATAL;
1688 fatal++;
1689 goto next_line;
1690 }
1691
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001692 /* everything's OK */
1693 break;
William Lallemandf9873ba2015-05-05 17:37:14 +02001694 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001695
1696 /* empty line */
1697 if (!**args)
1698 continue;
1699
Willy Tarreau4b103022021-02-12 17:59:10 +01001700 /* check for config macros */
1701 if (*args[0] == '.') {
1702 if (strcmp(args[0], ".if") == 0) {
1703 nested_cond_lvl++;
1704 if (nested_cond_lvl >= MAXNESTEDCONDS) {
1705 ha_alert("parsing [%s:%d]: too many nested '.if', max is %d.\n", file, linenum, MAXNESTEDCONDS);
1706 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1707 goto err;
1708 }
1709
1710 if (nested_conds[nested_cond_lvl - 1] == NESTED_COND_IF_DROP ||
1711 nested_conds[nested_cond_lvl - 1] == NESTED_COND_IF_SKIP ||
1712 nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELIF_DROP ||
1713 nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELIF_SKIP ||
1714 nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELSE_DROP) {
1715 nested_conds[nested_cond_lvl] = NESTED_COND_IF_SKIP;
1716 } else if (!*args[1] || *args[1] == '0') {
1717 /* empty = false */
1718 nested_conds[nested_cond_lvl] = NESTED_COND_IF_DROP;
1719 } else if (atoi(args[1]) > 0) {
1720 /* true */
1721 nested_conds[nested_cond_lvl] = NESTED_COND_IF_TAKE;
1722 } else {
1723 ha_alert("parsing [%s:%d]: unparsable conditional expression '%s'.\n", file, linenum, args[1]);
1724 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1725 goto err;
1726 }
1727 goto next_line;
1728 }
1729 else if (strcmp(args[0], ".elif") == 0) {
1730 if (!nested_cond_lvl) {
1731 ha_alert("parsing [%s:%d]: lone '.elif' with no matching '.if'.\n", file, linenum);
1732 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1733 goto err;
1734 }
1735
1736 if (nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_TAKE ||
1737 nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP) {
1738 ha_alert("parsing [%s:%d]: '.elif' after '.else' is not permitted.\n", file, linenum);
1739 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1740 goto err;
1741 }
1742
1743 if (nested_conds[nested_cond_lvl] == NESTED_COND_IF_TAKE ||
1744 nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
1745 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP) {
1746 nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_SKIP;
1747 } else if (!*args[1] || *args[1] == '0') {
1748 /* empty = false */
1749 nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_DROP;
1750 } else if (atoi(args[1]) > 0) {
1751 /* true */
1752 nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_TAKE;
1753 } else {
1754 ha_alert("parsing [%s:%d]: unparsable conditional expression '%s'.\n", file, linenum, args[1]);
1755 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1756 goto err;
1757 }
1758 goto next_line;
1759 }
1760 else if (strcmp(args[0], ".else") == 0) {
1761 if (!nested_cond_lvl) {
1762 ha_alert("parsing [%s:%d]: lone '.else' with no matching '.if'.\n", file, linenum);
1763 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1764 goto err;
1765 }
1766
1767 if (nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_TAKE ||
1768 nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP) {
1769 ha_alert("parsing [%s:%d]: '.else' after '.else' is not permitted.\n", file, linenum);
1770 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1771 goto err;
1772 }
1773
1774 if (nested_conds[nested_cond_lvl] == NESTED_COND_IF_TAKE ||
1775 nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
1776 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_TAKE ||
1777 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP) {
1778 nested_conds[nested_cond_lvl] = NESTED_COND_ELSE_DROP;
1779 } else {
1780 /* otherwise we take the "else" */
1781 nested_conds[nested_cond_lvl] = NESTED_COND_ELSE_TAKE;
1782 }
1783 goto next_line;
1784 }
1785 else if (strcmp(args[0], ".endif") == 0) {
1786 if (!nested_cond_lvl) {
1787 ha_alert("parsing [%s:%d]: lone '.endif' with no matching '.if'.\n", file, linenum);
1788 err_code |= ERR_ALERT | ERR_FATAL;
1789 fatal++;
1790 break;
1791 }
1792 nested_cond_lvl--;
1793 goto next_line;
1794 }
1795 }
1796
1797 if (nested_cond_lvl &&
1798 (nested_conds[nested_cond_lvl] == NESTED_COND_IF_DROP ||
1799 nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
1800 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_DROP ||
1801 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP ||
1802 nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP)) {
1803 /* The current block is masked out by the conditions */
1804 goto next_line;
1805 }
1806
1807 /* .warning/.error/.notice */
1808 if (*args[0] == '.') {
1809 if (strcmp(args[0], ".alert") == 0) {
1810 ha_alert("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
1811 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1812 goto err;
1813 }
1814 else if (strcmp(args[0], ".warning") == 0) {
1815 ha_warning("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
1816 err_code |= ERR_WARN;
1817 goto next_line;
1818 }
1819 else if (strcmp(args[0], ".notice") == 0) {
1820 ha_notice("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
1821 goto next_line;
1822 }
1823 else {
1824 ha_alert("parsing [%s:%d]: unknown directive '%s'.\n", file, linenum, args[0]);
1825 err_code |= ERR_ALERT | ERR_FATAL;
1826 fatal++;
1827 break;
1828 }
1829 }
1830
Willy Tarreau3842f002009-06-14 11:39:52 +02001831 /* check for keyword modifiers "no" and "default" */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001832 if (strcmp(args[0], "no") == 0) {
William Lallemand0f99e342011-10-12 17:50:54 +02001833 char *tmp;
1834
Willy Tarreau3842f002009-06-14 11:39:52 +02001835 kwm = KWM_NO;
William Lallemand0f99e342011-10-12 17:50:54 +02001836 tmp = args[0];
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01001837 for (arg=0; *args[arg+1]; arg++)
1838 args[arg] = args[arg+1]; // shift args after inversion
William Lallemand0f99e342011-10-12 17:50:54 +02001839 *tmp = '\0'; // fix the next arg to \0
1840 args[arg] = tmp;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01001841 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001842 else if (strcmp(args[0], "default") == 0) {
Willy Tarreau3842f002009-06-14 11:39:52 +02001843 kwm = KWM_DEF;
1844 for (arg=0; *args[arg+1]; arg++)
1845 args[arg] = args[arg+1]; // shift args after inversion
1846 }
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01001847
William Dauchyec730982019-10-27 20:08:10 +01001848 if (kwm != KWM_STD && strcmp(args[0], "option") != 0 &&
1849 strcmp(args[0], "log") != 0 && strcmp(args[0], "busy-polling") != 0 &&
Willy Tarreaud96f1122019-12-03 07:07:36 +01001850 strcmp(args[0], "set-dumpable") != 0 && strcmp(args[0], "strict-limits") != 0 &&
Amaury Denoyelle0f50cb92021-03-26 18:50:33 +01001851 strcmp(args[0], "insecure-fork-wanted") != 0 &&
1852 strcmp(args[0], "numa-cpu-mapping") != 0) {
William Dauchyec730982019-10-27 20:08:10 +01001853 ha_alert("parsing [%s:%d]: negation/default currently "
William Dauchy0fec3ab2019-10-27 20:08:11 +01001854 "supported only for options, log, busy-polling, "
Amaury Denoyelle0f50cb92021-03-26 18:50:33 +01001855 "set-dumpable, strict-limits, insecure-fork-wanted "
1856 "and numa-cpu-mapping.\n", file, linenum);
Willy Tarreau058e9072009-07-20 09:30:05 +02001857 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001858 fatal++;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01001859 }
1860
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001861 /* detect section start */
1862 list_for_each_entry(ics, &sections, list) {
1863 if (strcmp(args[0], ics->section_name) == 0) {
1864 cursection = ics->section_name;
Christopher Faulet7805e2b2018-11-30 13:50:47 +01001865 pcs = cs;
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001866 cs = ics;
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001867
1868 if (global.mode & MODE_DIAG) {
1869 check_section_position(args[0], file, linenum,
1870 &non_global_section_parsed);
1871 }
1872
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001873 break;
1874 }
Emeric Brun32da3c42010-09-23 18:39:19 +02001875 }
1876
Christopher Faulet7805e2b2018-11-30 13:50:47 +01001877 if (pcs && pcs->post_section_parser) {
Willy Tarreau32234e72020-06-16 17:14:33 +02001878 int status;
1879
1880 status = pcs->post_section_parser();
1881 err_code |= status;
1882 if (status & ERR_FATAL)
1883 fatal++;
1884
Christopher Faulet7805e2b2018-11-30 13:50:47 +01001885 if (err_code & ERR_ABORT)
1886 goto err;
Christopher Faulet7805e2b2018-11-30 13:50:47 +01001887 }
Christopher Fauletbf7a9592018-12-02 09:37:38 +01001888 pcs = NULL;
Christopher Faulet7805e2b2018-11-30 13:50:47 +01001889
William Lallemandd2ff56d2017-10-16 11:06:50 +02001890 if (!cs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001891 ha_alert("parsing [%s:%d]: unknown keyword '%s' out of section.\n", file, linenum, args[0]);
Willy Tarreau058e9072009-07-20 09:30:05 +02001892 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001893 fatal++;
William Lallemandd2ff56d2017-10-16 11:06:50 +02001894 } else {
Willy Tarreau32234e72020-06-16 17:14:33 +02001895 int status;
1896
1897 status = cs->section_parser(file, linenum, args, kwm);
1898 err_code |= status;
1899 if (status & ERR_FATAL)
1900 fatal++;
1901
William Lallemandd2ff56d2017-10-16 11:06:50 +02001902 if (err_code & ERR_ABORT)
1903 goto err;
1904 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001905 }
William Lallemandd2ff56d2017-10-16 11:06:50 +02001906
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001907 if (missing_lf != -1) {
Tim Duesterhusf92afb72020-08-18 22:00:04 +02001908 ha_alert("parsing [%s:%d]: Missing LF on last line, file might have been truncated at position %d.\n",
1909 file, linenum, (missing_lf + 1));
1910 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001911 }
1912
Christopher Fauletbf7a9592018-12-02 09:37:38 +01001913 if (cs && cs->post_section_parser)
Christopher Faulet7805e2b2018-11-30 13:50:47 +01001914 err_code |= cs->post_section_parser();
William Lallemandd2ff56d2017-10-16 11:06:50 +02001915
Willy Tarreau4b103022021-02-12 17:59:10 +01001916 if (nested_cond_lvl) {
1917 ha_alert("parsing [%s:%d]: non-terminated '.if' block.\n", file, linenum);
1918 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
1919 }
William Lallemandd2ff56d2017-10-16 11:06:50 +02001920err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001921 ha_free(&cfg_scope);
Willy Tarreau6daf3432008-01-22 16:44:08 +01001922 cursection = NULL;
William Lallemand64e84512015-05-12 14:25:37 +02001923 free(thisline);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001924 free(outline);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001925 fclose(f);
Willy Tarreau058e9072009-07-20 09:30:05 +02001926 return err_code;
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001927}
1928
Willy Tarreau64ab6072014-09-16 12:17:36 +02001929/* This function propagates processes from frontend <from> to backend <to> so
1930 * that it is always guaranteed that a backend pointed to by a frontend is
1931 * bound to all of its processes. After that, if the target is a "listen"
1932 * instance, the function recursively descends the target's own targets along
Willy Tarreau98d04852015-05-26 12:18:29 +02001933 * default_backend and use_backend rules. Since the bits are
Willy Tarreau64ab6072014-09-16 12:17:36 +02001934 * checked first to ensure that <to> is already bound to all processes of
1935 * <from>, there is no risk of looping and we ensure to follow the shortest
1936 * path to the destination.
1937 *
1938 * It is possible to set <to> to NULL for the first call so that the function
1939 * takes care of visiting the initial frontend in <from>.
1940 *
1941 * It is important to note that the function relies on the fact that all names
1942 * have already been resolved.
1943 */
1944void propagate_processes(struct proxy *from, struct proxy *to)
1945{
1946 struct switching_rule *rule;
Willy Tarreau64ab6072014-09-16 12:17:36 +02001947
1948 if (to) {
1949 /* check whether we need to go down */
1950 if (from->bind_proc &&
1951 (from->bind_proc & to->bind_proc) == from->bind_proc)
1952 return;
1953
1954 if (!from->bind_proc && !to->bind_proc)
1955 return;
1956
1957 to->bind_proc = from->bind_proc ?
1958 (to->bind_proc | from->bind_proc) : 0;
1959
1960 /* now propagate down */
1961 from = to;
1962 }
1963
Willy Tarreau8a95d8c2014-12-18 13:56:26 +01001964 if (!(from->cap & PR_CAP_FE))
Willy Tarreau64ab6072014-09-16 12:17:36 +02001965 return;
1966
Willy Tarreauc3914d42020-09-24 08:39:22 +02001967 if (from->disabled)
Willy Tarreauf6b70012014-12-18 14:00:43 +01001968 return;
1969
Willy Tarreau64ab6072014-09-16 12:17:36 +02001970 /* default_backend */
1971 if (from->defbe.be)
1972 propagate_processes(from, from->defbe.be);
1973
1974 /* use_backend */
1975 list_for_each_entry(rule, &from->switching_rules, list) {
Cyril Bonté51639692014-10-02 19:56:25 +02001976 if (rule->dynamic)
1977 continue;
Willy Tarreau64ab6072014-09-16 12:17:36 +02001978 to = rule->be.backend;
1979 propagate_processes(from, to);
1980 }
Willy Tarreau64ab6072014-09-16 12:17:36 +02001981}
1982
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01001983#if defined(__linux__) && defined USE_CPU_AFFINITY
1984/* filter directory name of the pattern node<X> */
1985static int numa_filter(const struct dirent *dir)
1986{
1987 char *endptr;
1988
1989 /* dir name must start with "node" prefix */
1990 if (strncmp(dir->d_name, "node", 4))
1991 return 0;
1992
1993 /* dir name must be at least 5 characters long */
1994 if (!dir->d_name[4])
1995 return 0;
1996
1997 /* dir name must end with a numeric id */
1998 if (strtol(&dir->d_name[4], &endptr, 10) < 0 || *endptr)
1999 return 0;
2000
2001 /* all tests succeeded */
2002 return 1;
2003}
2004
2005/* Parse a linux cpu map string representing to a numeric cpu mask map
2006 * The cpu map string is a list of 4-byte hex strings separated by commas, with
2007 * most-significant byte first, one bit per cpu number.
2008 */
2009static void parse_cpumap(char *cpumap_str, struct hap_cpuset *cpu_set)
2010{
2011 unsigned long cpumap;
2012 char *start, *endptr, *comma;
2013 int i, j;
2014
2015 ha_cpuset_zero(cpu_set);
2016
2017 i = 0;
2018 do {
2019 /* reverse-search for a comma, parse the string after the comma
2020 * or at the beginning if no comma found
2021 */
2022 comma = strrchr(cpumap_str, ',');
2023 start = comma ? comma + 1 : cpumap_str;
2024
2025 cpumap = strtoul(start, &endptr, 16);
2026 for (j = 0; cpumap; cpumap >>= 1, ++j) {
2027 if (cpumap & 0x1)
2028 ha_cpuset_set(cpu_set, j + i * 32);
2029 }
2030
2031 if (comma)
2032 *comma = '\0';
2033 ++i;
2034 } while (comma);
2035}
2036
2037/* Read the first line of a file from <path> into the trash buffer.
2038 * Returns 0 on success, otherwise non-zero.
2039 */
2040static int read_file_to_trash(const char *path)
2041{
2042 FILE *file;
2043 int ret = 1;
2044
2045 file = fopen(path, "r");
2046 if (file) {
2047 if (fgets(trash.area, trash.size, file))
2048 ret = 0;
2049
2050 fclose(file);
2051 }
2052
2053 return ret;
2054}
2055
2056/* Inspect the cpu topology of the machine on startup. If a multi-socket
2057 * machine is detected, try to bind on the first node with active cpu. This is
2058 * done to prevent an impact on the overall performance when the topology of
2059 * the machine is unknown. This function is not called if one of the conditions
2060 * is met :
2061 * - a non-null nbthread directive is active
2062 * - a restrictive cpu-map directive is active
2063 * - a restrictive affinity is already applied, for example via taskset
2064 *
2065 * Returns the count of cpus selected. If no automatic binding was required or
2066 * an error occurred and the topology is unknown, 0 is returned.
2067 */
2068static int numa_detect_topology()
2069{
2070 struct dirent **node_dirlist;
2071 int node_dirlist_size;
2072
2073 struct hap_cpuset active_cpus, node_cpu_set;
2074 const char *parse_cpu_set_args[2];
2075 char cpumap_path[PATH_MAX];
2076 char *err = NULL;
2077
2078 /* node_cpu_set count is used as return value */
2079 ha_cpuset_zero(&node_cpu_set);
2080
2081 /* 1. count the sysfs node<X> directories */
2082 node_dirlist_size = scandir(NUMA_DETECT_SYSTEM_SYSFS_PATH"/node", &node_dirlist, numa_filter, alphasort);
2083 if (node_dirlist_size <= 1)
2084 goto free_scandir_entries;
2085
2086 /* 2. read and parse the list of currently online cpu */
2087 if (read_file_to_trash(NUMA_DETECT_SYSTEM_SYSFS_PATH"/cpu/online")) {
2088 ha_notice("Cannot read online CPUs list, will not try to refine binding\n");
2089 goto free_scandir_entries;
2090 }
2091
2092 parse_cpu_set_args[0] = trash.area;
2093 parse_cpu_set_args[1] = "\0";
2094 if (parse_cpu_set(parse_cpu_set_args, &active_cpus, 1, &err)) {
2095 ha_notice("Cannot read online CPUs list: '%s'. Will not try to refine binding\n", err);
2096 free(err);
2097 goto free_scandir_entries;
2098 }
2099
2100 /* 3. loop through nodes dirs and find the first one with active cpus */
2101 while (node_dirlist_size--) {
2102 const char *node = node_dirlist[node_dirlist_size]->d_name;
2103 ha_cpuset_zero(&node_cpu_set);
2104
2105 snprintf(cpumap_path, PATH_MAX, "%s/node/%s/cpumap",
2106 NUMA_DETECT_SYSTEM_SYSFS_PATH, node);
2107
2108 if (read_file_to_trash(cpumap_path)) {
2109 ha_notice("Cannot read CPUs list of '%s', will not select them to refine binding\n", node);
2110 free(node_dirlist[node_dirlist_size]);
2111 continue;
2112 }
2113
2114 parse_cpumap(trash.area, &node_cpu_set);
2115 ha_cpuset_and(&node_cpu_set, &active_cpus);
2116
2117 /* 5. set affinity on the first found node with active cpus */
2118 if (!ha_cpuset_count(&node_cpu_set)) {
2119 free(node_dirlist[node_dirlist_size]);
2120 continue;
2121 }
2122
2123 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));
2124 if (sched_setaffinity(getpid(), sizeof(node_cpu_set.cpuset), &node_cpu_set.cpuset) == -1) {
2125 ha_warning("Cannot set the cpu affinity for this multi-cpu machine\n");
2126
2127 /* clear the cpuset used as return value */
2128 ha_cpuset_zero(&node_cpu_set);
2129 }
2130
2131 free(node_dirlist[node_dirlist_size]);
2132 break;
2133 }
2134
2135 free_scandir_entries:
2136 while (node_dirlist_size-- > 0)
2137 free(node_dirlist[node_dirlist_size]);
2138 free(node_dirlist);
2139
2140 return ha_cpuset_count(&node_cpu_set);
2141}
2142#endif /* __linux__ && USE_CPU_AFFINITY */
2143
Willy Tarreaubb925012009-07-23 13:36:36 +02002144/*
2145 * Returns the error code, 0 if OK, or any combination of :
2146 * - ERR_ABORT: must abort ASAP
2147 * - ERR_FATAL: we can continue parsing but not start the service
2148 * - ERR_WARN: a warning has been emitted
2149 * - ERR_ALERT: an alert has been emitted
2150 * Only the two first ones can stop processing, the two others are just
2151 * indicators.
2152 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002153int check_config_validity()
2154{
2155 int cfgerr = 0;
2156 struct proxy *curproxy = NULL;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002157 struct stktable *t;
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002158 struct server *newsrv = NULL;
Willy Tarreaubb925012009-07-23 13:36:36 +02002159 int err_code = 0;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002160 unsigned int next_pxid = 1;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002161 struct bind_conf *bind_conf;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002162 char *err;
William Lallemand48b4bb42017-10-23 14:36:34 +02002163 struct cfg_postparser *postparser;
Emeric Brun750fe792020-12-23 16:51:12 +01002164 struct resolvers *curr_resolvers = NULL;
Amaury Denoyelle3efee652021-03-08 17:31:39 +01002165 int i;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002166
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002167 bind_conf = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002168 /*
2169 * Now, check for the integrity of all that we have collected.
2170 */
2171
2172 /* will be needed further to delay some tasks */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02002173 tv_update_date(0,1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002174
Willy Tarreau193b8c62012-11-22 00:17:38 +01002175 if (!global.tune.max_http_hdr)
2176 global.tune.max_http_hdr = MAX_HTTP_HDR;
2177
2178 if (!global.tune.cookie_len)
2179 global.tune.cookie_len = CAPTURE_LEN;
2180
Stéphane Cottin23e9e932017-05-18 08:58:41 +02002181 if (!global.tune.requri_len)
2182 global.tune.requri_len = REQURI_LEN;
2183
Willy Tarreau149ab772019-01-26 14:27:06 +01002184 if (!global.nbthread) {
2185 /* nbthread not set, thus automatic. In this case, and only if
2186 * running on a single process, we enable the same number of
2187 * threads as the number of CPUs the process is bound to. This
2188 * allows to easily control the number of threads using taskset.
2189 */
2190 global.nbthread = 1;
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002191
Willy Tarreau149ab772019-01-26 14:27:06 +01002192#if defined(USE_THREAD)
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002193 if (global.nbproc == 1) {
2194 int numa_cores = 0;
2195#if defined(__linux__) && defined USE_CPU_AFFINITY
Amaury Denoyelle0f50cb92021-03-26 18:50:33 +01002196 if (global.numa_cpu_mapping && !thread_cpu_mask_forced())
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002197 numa_cores = numa_detect_topology();
2198#endif
2199 global.nbthread = numa_cores ? numa_cores :
2200 thread_cpus_enabled_at_boot;
2201 }
Willy Tarreau149ab772019-01-26 14:27:06 +01002202 all_threads_mask = nbits(global.nbthread);
2203#endif
2204 }
2205
Willy Tarreau26f6ae12019-02-02 12:56:15 +01002206 if (global.nbproc > 1 && global.nbthread > 1) {
2207 ha_alert("config : cannot enable multiple processes if multiple threads are configured. Please use either nbproc or nbthread but not both.\n");
2208 err_code |= ERR_ALERT | ERR_FATAL;
2209 goto out;
2210 }
2211
Willy Tarreaubafbe012017-11-24 17:34:44 +01002212 pool_head_requri = create_pool("requri", global.tune.requri_len , MEM_F_SHARED);
Emeric Brun96fd9262017-07-05 13:33:16 +02002213
Willy Tarreaubafbe012017-11-24 17:34:44 +01002214 pool_head_capture = create_pool("capture", global.tune.cookie_len, MEM_F_SHARED);
Willy Tarreau193b8c62012-11-22 00:17:38 +01002215
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01002216 /* Post initialisation of the users and groups lists. */
2217 err_code = userlist_postinit();
2218 if (err_code != ERR_NONE)
2219 goto out;
2220
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002221 /* first, we will invert the proxy list order */
2222 curproxy = NULL;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002223 while (proxies_list) {
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002224 struct proxy *next;
2225
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002226 next = proxies_list->next;
2227 proxies_list->next = curproxy;
2228 curproxy = proxies_list;
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002229 if (!next)
2230 break;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002231 proxies_list = next;
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002232 }
2233
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002234 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreau55ea7572007-06-17 19:56:27 +02002235 struct switching_rule *rule;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002236 struct server_rule *srule;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002237 struct sticking_rule *mrule;
Dragan Dosen1322d092015-09-22 16:05:32 +02002238 struct logsrv *tmplogsrv;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002239 unsigned int next_id;
Willy Tarreau16a21472012-11-19 12:39:59 +01002240 int nbproc;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002241
Willy Tarreau050536d2012-10-04 08:47:34 +02002242 if (curproxy->uuid < 0) {
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002243 /* proxy ID not set, use automatic numbering with first
2244 * spare entry starting with next_pxid.
2245 */
2246 next_pxid = get_next_id(&used_proxy_id, next_pxid);
2247 curproxy->conf.id.key = curproxy->uuid = next_pxid;
2248 eb32_insert(&used_proxy_id, &curproxy->conf.id);
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002249 }
Krzysztof Piotr Oledzkidf5cb9f2010-02-05 20:58:27 +01002250 next_pxid++;
2251
Willy Tarreau55ea7572007-06-17 19:56:27 +02002252
Willy Tarreauc3914d42020-09-24 08:39:22 +02002253 if (curproxy->disabled) {
Willy Tarreau02b092f2020-10-07 18:36:54 +02002254 /* ensure we don't keep listeners uselessly bound. We
2255 * can't disable their listeners yet (fdtab not
2256 * allocated yet) but let's skip them.
2257 */
Dragan Dosen7d61a332019-05-07 14:16:18 +02002258 if (curproxy->table) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002259 ha_free(&curproxy->table->peers.name);
Dragan Dosen7d61a332019-05-07 14:16:18 +02002260 curproxy->table->peers.p = NULL;
2261 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002262 continue;
2263 }
2264
Willy Tarreau102df612014-05-07 23:56:38 +02002265 /* Check multi-process mode compatibility for the current proxy */
2266
2267 if (curproxy->bind_proc) {
2268 /* an explicit bind-process was specified, let's check how many
2269 * processes remain.
2270 */
David Carliere6c39412015-07-02 07:00:17 +00002271 nbproc = my_popcountl(curproxy->bind_proc);
Willy Tarreau102df612014-05-07 23:56:38 +02002272
Willy Tarreaua38a7172019-02-02 17:11:28 +01002273 curproxy->bind_proc &= all_proc_mask;
Willy Tarreau102df612014-05-07 23:56:38 +02002274 if (!curproxy->bind_proc && nbproc == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002275 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 +02002276 curproxy->bind_proc = 1;
2277 }
2278 else if (!curproxy->bind_proc && nbproc > 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002279 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 +02002280 curproxy->bind_proc = 0;
2281 }
2282 }
2283
Willy Tarreau3d209582014-05-09 17:06:11 +02002284 /* check and reduce the bind-proc of each listener */
2285 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
2286 unsigned long mask;
2287
Willy Tarreau45a66cc2017-11-24 11:28:00 +01002288 /* HTTP frontends with "h2" as ALPN/NPN will work in
2289 * HTTP/2 and absolutely require buffers 16kB or larger.
2290 */
2291#ifdef USE_OPENSSL
2292 if (curproxy->mode == PR_MODE_HTTP && global.tune.bufsize < 16384) {
2293#ifdef OPENSSL_NPN_NEGOTIATED
2294 /* check NPN */
Willy Tarreau4db49c02018-11-11 10:36:25 +01002295 if (bind_conf->ssl_conf.npn_str && strstr(bind_conf->ssl_conf.npn_str, "\002h2")) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002296 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",
2297 curproxy->id, bind_conf->file, bind_conf->line, global.tune.bufsize);
Willy Tarreau45a66cc2017-11-24 11:28:00 +01002298 cfgerr++;
2299 }
2300#endif
2301#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2302 /* check ALPN */
Willy Tarreau4db49c02018-11-11 10:36:25 +01002303 if (bind_conf->ssl_conf.alpn_str && strstr(bind_conf->ssl_conf.alpn_str, "\002h2")) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002304 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",
2305 curproxy->id, bind_conf->file, bind_conf->line, global.tune.bufsize);
Willy Tarreau45a66cc2017-11-24 11:28:00 +01002306 cfgerr++;
2307 }
2308#endif
2309 } /* HTTP && bufsize < 16384 */
2310#endif
2311
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002312 /* detect and address thread affinity inconsistencies */
Willy Tarreaue26993c2020-09-03 07:18:55 +02002313 mask = thread_mask(bind_conf->settings.bind_thread);
Willy Tarreau3d957172019-02-02 18:00:17 +01002314 if (!(mask & all_threads_mask)) {
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002315 unsigned long new_mask = 0;
2316
2317 while (mask) {
Willy Tarreau0c026f42018-08-01 19:12:20 +02002318 new_mask |= mask & all_threads_mask;
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002319 mask >>= global.nbthread;
2320 }
2321
Willy Tarreaue26993c2020-09-03 07:18:55 +02002322 bind_conf->settings.bind_thread = new_mask;
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002323 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",
2324 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line, new_mask);
2325 }
2326
2327 /* detect process and nbproc affinity inconsistencies */
Willy Tarreaue26993c2020-09-03 07:18:55 +02002328 mask = proc_mask(bind_conf->settings.bind_proc) & proc_mask(curproxy->bind_proc);
Willy Tarreaua36b3242019-02-02 13:14:34 +01002329 if (!(mask & all_proc_mask)) {
2330 mask = proc_mask(curproxy->bind_proc) & all_proc_mask;
Willy Tarreaue26993c2020-09-03 07:18:55 +02002331 nbproc = my_popcountl(bind_conf->settings.bind_proc);
2332 bind_conf->settings.bind_proc = proc_mask(bind_conf->settings.bind_proc) & mask;
Willy Tarreau3d209582014-05-09 17:06:11 +02002333
Willy Tarreaue26993c2020-09-03 07:18:55 +02002334 if (!bind_conf->settings.bind_proc && nbproc == 1) {
Willy Tarreaua36b3242019-02-02 13:14:34 +01002335 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",
2336 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreaue26993c2020-09-03 07:18:55 +02002337 bind_conf->settings.bind_proc = mask & ~(mask - 1);
Willy Tarreaua36b3242019-02-02 13:14:34 +01002338 }
Willy Tarreaue26993c2020-09-03 07:18:55 +02002339 else if (!bind_conf->settings.bind_proc && nbproc > 1) {
Willy Tarreaua36b3242019-02-02 13:14:34 +01002340 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",
2341 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreaue26993c2020-09-03 07:18:55 +02002342 bind_conf->settings.bind_proc = 0;
Willy Tarreaua36b3242019-02-02 13:14:34 +01002343 }
Willy Tarreau3d209582014-05-09 17:06:11 +02002344 }
2345 }
2346
Willy Tarreauff01a212009-03-15 13:46:16 +01002347 switch (curproxy->mode) {
Willy Tarreauff01a212009-03-15 13:46:16 +01002348 case PR_MODE_TCP:
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002349 cfgerr += proxy_cfg_ensure_no_http(curproxy);
Willy Tarreauff01a212009-03-15 13:46:16 +01002350 break;
2351
2352 case PR_MODE_HTTP:
Willy Tarreau25320b22013-03-24 07:22:08 +01002353 curproxy->http_needed = 1;
Willy Tarreauff01a212009-03-15 13:46:16 +01002354 break;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002355
2356 case PR_MODE_CLI:
2357 cfgerr += proxy_cfg_ensure_no_http(curproxy);
2358 break;
Emeric Brun54932b42020-07-07 09:43:24 +02002359 case PR_MODE_SYSLOG:
Willy Tarreaua389c9e2020-10-07 17:49:42 +02002360 case PR_MODE_PEERS:
Emeric Brun54932b42020-07-07 09:43:24 +02002361 case PR_MODES:
2362 /* should not happen, bug gcc warn missing switch statement */
Willy Tarreaua389c9e2020-10-07 17:49:42 +02002363 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 +02002364 proxy_type_str(curproxy), curproxy->id);
2365 cfgerr++;
2366 break;
Willy Tarreauff01a212009-03-15 13:46:16 +01002367 }
2368
Willy Tarreau4975d142021-03-13 11:00:33 +01002369 if (curproxy != global.cli_fe && (curproxy->cap & PR_CAP_FE) && LIST_ISEMPTY(&curproxy->conf.listeners)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002370 ha_warning("config : %s '%s' has no 'bind' directive. Please declare it as a backend if this was intended.\n",
2371 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauf3934b82015-08-11 11:36:45 +02002372 err_code |= ERR_WARN;
2373 }
2374
Willy Tarreau77e0dae2020-10-14 15:44:27 +02002375 if (curproxy->cap & PR_CAP_BE) {
Willy Tarreauf3e49f92009-10-03 12:21:20 +02002376 if (curproxy->lbprm.algo & BE_LB_KIND) {
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002377 if (curproxy->options & PR_O_TRANSP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002378 ha_alert("config : %s '%s' cannot use both transparent and balance mode.\n",
2379 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002380 cfgerr++;
2381 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002382#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002383 else if (curproxy->srv == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002384 ha_alert("config : %s '%s' needs at least 1 server in balance mode.\n",
2385 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002386 cfgerr++;
2387 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002388#endif
Willy Tarreau1620ec32011-08-06 17:05:02 +02002389 else if (curproxy->options & PR_O_DISPATCH) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002390 ha_warning("config : dispatch address of %s '%s' will be ignored in balance mode.\n",
2391 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubb925012009-07-23 13:36:36 +02002392 err_code |= ERR_WARN;
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002393 }
2394 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02002395 else if (!(curproxy->options & (PR_O_TRANSP | PR_O_DISPATCH | PR_O_HTTP_PROXY))) {
Willy Tarreau3cd9af22009-03-15 14:06:41 +01002396 /* If no LB algo is set in a backend, and we're not in
2397 * transparent mode, dispatch mode nor proxy mode, we
2398 * want to use balance roundrobin by default.
2399 */
2400 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2401 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002402 }
2403 }
Willy Tarreau193cf932007-09-17 10:17:23 +02002404
Willy Tarreau1620ec32011-08-06 17:05:02 +02002405 if (curproxy->options & PR_O_DISPATCH)
2406 curproxy->options &= ~(PR_O_TRANSP | PR_O_HTTP_PROXY);
2407 else if (curproxy->options & PR_O_HTTP_PROXY)
2408 curproxy->options &= ~(PR_O_DISPATCH | PR_O_TRANSP);
2409 else if (curproxy->options & PR_O_TRANSP)
2410 curproxy->options &= ~(PR_O_DISPATCH | PR_O_HTTP_PROXY);
Willy Tarreau82936582007-11-30 15:20:09 +01002411
Christopher Faulete5870d82020-04-15 11:32:03 +02002412 if ((curproxy->tcpcheck_rules.flags & TCPCHK_RULES_UNUSED_HTTP_RS)) {
2413 ha_warning("config : %s '%s' uses http-check rules without 'option httpchk', so the rules are ignored.\n",
2414 proxy_type_str(curproxy), curproxy->id);
2415 err_code |= ERR_WARN;
2416 }
2417
2418 if ((curproxy->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK &&
Christopher Fauletc0fcbe42020-06-03 19:00:42 +02002419 (curproxy->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) != TCPCHK_RULES_HTTP_CHK) {
Willy Tarreau1620ec32011-08-06 17:05:02 +02002420 if (curproxy->options & PR_O_DISABLE404) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002421 ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option httpchk').\n",
2422 "disable-on-404", proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1620ec32011-08-06 17:05:02 +02002423 err_code |= ERR_WARN;
2424 curproxy->options &= ~PR_O_DISABLE404;
2425 }
2426 if (curproxy->options2 & PR_O2_CHK_SNDST) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002427 ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option httpchk').\n",
2428 "send-state", proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1620ec32011-08-06 17:05:02 +02002429 err_code |= ERR_WARN;
2430 curproxy->options &= ~PR_O2_CHK_SNDST;
2431 }
Willy Tarreauef781042010-01-27 11:53:01 +01002432 }
2433
Simon Horman98637e52014-06-20 12:30:16 +09002434 if ((curproxy->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2435 if (!global.external_check) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002436 ha_alert("Proxy '%s' : '%s' unable to find required 'global.external-check'.\n",
2437 curproxy->id, "option external-check");
Simon Horman98637e52014-06-20 12:30:16 +09002438 cfgerr++;
2439 }
2440 if (!curproxy->check_command) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002441 ha_alert("Proxy '%s' : '%s' unable to find required 'external-check command'.\n",
2442 curproxy->id, "option external-check");
Simon Horman98637e52014-06-20 12:30:16 +09002443 cfgerr++;
2444 }
Willy Tarreaud96f1122019-12-03 07:07:36 +01002445 if (!(global.tune.options & GTUNE_INSECURE_FORK)) {
2446 ha_warning("Proxy '%s' : 'insecure-fork-wanted' not enabled in the global section, '%s' will likely fail.\n",
2447 curproxy->id, "option external-check");
2448 err_code |= ERR_WARN;
2449 }
Simon Horman98637e52014-06-20 12:30:16 +09002450 }
2451
Simon Horman64e34162015-02-06 11:11:57 +09002452 if (curproxy->email_alert.set) {
Simon Horman0ba0e4a2015-01-30 11:23:00 +09002453 if (!(curproxy->email_alert.mailers.name && curproxy->email_alert.from && curproxy->email_alert.to)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002454 ha_warning("config : 'email-alert' will be ignored for %s '%s' (the presence any of "
2455 "'email-alert from', 'email-alert level' 'email-alert mailers', "
2456 "'email-alert myhostname', or 'email-alert to' "
2457 "requires each of 'email-alert from', 'email-alert mailers' and 'email-alert to' "
2458 "to be present).\n",
2459 proxy_type_str(curproxy), curproxy->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09002460 err_code |= ERR_WARN;
2461 free_email_alert(curproxy);
2462 }
2463 if (!curproxy->email_alert.myhostname)
Cyril Bontée22bfd62015-12-04 03:07:07 +01002464 curproxy->email_alert.myhostname = strdup(hostname);
Simon Horman9dc49962015-01-30 11:22:59 +09002465 }
2466
Simon Horman98637e52014-06-20 12:30:16 +09002467 if (curproxy->check_command) {
2468 int clear = 0;
2469 if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_EXT_CHK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002470 ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option external-check').\n",
2471 "external-check command", proxy_type_str(curproxy), curproxy->id);
Simon Horman98637e52014-06-20 12:30:16 +09002472 err_code |= ERR_WARN;
2473 clear = 1;
2474 }
2475 if (curproxy->check_command[0] != '/' && !curproxy->check_path) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002476 ha_alert("Proxy '%s': '%s' does not have a leading '/' and 'external-check path' is not set.\n",
2477 curproxy->id, "external-check command");
Simon Horman98637e52014-06-20 12:30:16 +09002478 cfgerr++;
2479 }
2480 if (clear) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002481 ha_free(&curproxy->check_command);
Simon Horman98637e52014-06-20 12:30:16 +09002482 }
2483 }
2484
2485 if (curproxy->check_path) {
2486 if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_EXT_CHK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002487 ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option external-check').\n",
2488 "external-check path", proxy_type_str(curproxy), curproxy->id);
Simon Horman98637e52014-06-20 12:30:16 +09002489 err_code |= ERR_WARN;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002490 ha_free(&curproxy->check_path);
Simon Horman98637e52014-06-20 12:30:16 +09002491 }
2492 }
2493
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002494 /* if a default backend was specified, let's find it */
2495 if (curproxy->defbe.name) {
2496 struct proxy *target;
2497
Willy Tarreauafb39922015-05-26 12:04:09 +02002498 target = proxy_be_by_name(curproxy->defbe.name);
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +01002499 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002500 ha_alert("Proxy '%s': unable to find required default_backend: '%s'.\n",
2501 curproxy->id, curproxy->defbe.name);
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002502 cfgerr++;
2503 } else if (target == curproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002504 ha_alert("Proxy '%s': loop detected for default_backend: '%s'.\n",
2505 curproxy->id, curproxy->defbe.name);
Willy Tarreaubb925012009-07-23 13:36:36 +02002506 cfgerr++;
Willy Tarreauafb39922015-05-26 12:04:09 +02002507 } else if (target->mode != curproxy->mode &&
2508 !(curproxy->mode == PR_MODE_TCP && target->mode == PR_MODE_HTTP)) {
2509
Christopher Faulet767a84b2017-11-24 16:50:31 +01002510 ha_alert("%s %s '%s' (%s:%d) tries to use incompatible %s %s '%s' (%s:%d) as its default backend (see 'mode').\n",
2511 proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
2512 curproxy->conf.file, curproxy->conf.line,
2513 proxy_mode_str(target->mode), proxy_type_str(target), target->id,
2514 target->conf.file, target->conf.line);
Willy Tarreauafb39922015-05-26 12:04:09 +02002515 cfgerr++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002516 } else {
2517 free(curproxy->defbe.name);
2518 curproxy->defbe.be = target;
Willy Tarreauff678132012-02-13 14:32:34 +01002519 /* Emit a warning if this proxy also has some servers */
2520 if (curproxy->srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002521 ha_warning("In proxy '%s', the 'default_backend' rule always has precedence over the servers, which will never be used.\n",
2522 curproxy->id);
Willy Tarreauff678132012-02-13 14:32:34 +01002523 err_code |= ERR_WARN;
2524 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002525 }
2526 }
2527
Willy Tarreau55ea7572007-06-17 19:56:27 +02002528 /* find the target proxy for 'use_backend' rules */
2529 list_for_each_entry(rule, &curproxy->switching_rules, list) {
Willy Tarreau55ea7572007-06-17 19:56:27 +02002530 struct proxy *target;
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01002531 struct logformat_node *node;
2532 char *pxname;
2533
2534 /* Try to parse the string as a log format expression. If the result
2535 * of the parsing is only one entry containing a simple string, then
2536 * it's a standard string corresponding to a static rule, thus the
2537 * parsing is cancelled and be.name is restored to be resolved.
2538 */
2539 pxname = rule->be.name;
2540 LIST_INIT(&rule->be.expr);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01002541 curproxy->conf.args.ctx = ARGC_UBK;
2542 curproxy->conf.args.file = rule->file;
2543 curproxy->conf.args.line = rule->line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002544 err = NULL;
2545 if (!parse_logformat_string(pxname, curproxy, &rule->be.expr, 0, SMP_VAL_FE_HRQ_HDR, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002546 ha_alert("Parsing [%s:%d]: failed to parse use_backend rule '%s' : %s.\n",
2547 rule->file, rule->line, pxname, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002548 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01002549 cfgerr++;
2550 continue;
2551 }
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01002552 node = LIST_NEXT(&rule->be.expr, struct logformat_node *, list);
2553
2554 if (!LIST_ISEMPTY(&rule->be.expr)) {
2555 if (node->type != LOG_FMT_TEXT || node->list.n != &rule->be.expr) {
2556 rule->dynamic = 1;
2557 free(pxname);
2558 continue;
2559 }
Christopher Fauletf82ea4a2020-05-07 15:59:33 +02002560 /* Only one element in the list, a simple string: free the expression and
2561 * fall back to static rule
2562 */
Willy Tarreau2b718102021-04-21 07:32:39 +02002563 LIST_DELETE(&node->list);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01002564 free(node->arg);
2565 free(node);
2566 }
2567
2568 rule->dynamic = 0;
2569 rule->be.name = pxname;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002570
Willy Tarreauafb39922015-05-26 12:04:09 +02002571 target = proxy_be_by_name(rule->be.name);
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +01002572 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002573 ha_alert("Proxy '%s': unable to find required use_backend: '%s'.\n",
2574 curproxy->id, rule->be.name);
Willy Tarreau55ea7572007-06-17 19:56:27 +02002575 cfgerr++;
2576 } else if (target == curproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002577 ha_alert("Proxy '%s': loop detected for use_backend: '%s'.\n",
2578 curproxy->id, rule->be.name);
Willy Tarreau55ea7572007-06-17 19:56:27 +02002579 cfgerr++;
Willy Tarreauafb39922015-05-26 12:04:09 +02002580 } else if (target->mode != curproxy->mode &&
2581 !(curproxy->mode == PR_MODE_TCP && target->mode == PR_MODE_HTTP)) {
2582
Christopher Faulet767a84b2017-11-24 16:50:31 +01002583 ha_alert("%s %s '%s' (%s:%d) tries to use incompatible %s %s '%s' (%s:%d) in a 'use_backend' rule (see 'mode').\n",
2584 proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
2585 curproxy->conf.file, curproxy->conf.line,
2586 proxy_mode_str(target->mode), proxy_type_str(target), target->id,
2587 target->conf.file, target->conf.line);
Willy Tarreauafb39922015-05-26 12:04:09 +02002588 cfgerr++;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002589 } else {
Willy Tarreau35cd7342021-02-26 20:51:47 +01002590 ha_free(&rule->be.name);
Willy Tarreau55ea7572007-06-17 19:56:27 +02002591 rule->be.backend = target;
2592 }
Christopher Faulet581db2b2021-03-26 10:02:46 +01002593 err_code |= warnif_tcp_http_cond(curproxy, rule->cond);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002594 }
2595
Willy Tarreau64ab6072014-09-16 12:17:36 +02002596 /* find the target server for 'use_server' rules */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002597 list_for_each_entry(srule, &curproxy->server_rules, list) {
Jerome Magnin824186b2020-03-29 09:37:12 +02002598 struct server *target;
2599 struct logformat_node *node;
2600 char *server_name;
2601
2602 /* We try to parse the string as a log format expression. If the result of the parsing
2603 * is only one entry containing a single string, then it's a standard string corresponding
2604 * to a static rule, thus the parsing is cancelled and we fall back to setting srv.ptr.
2605 */
2606 server_name = srule->srv.name;
2607 LIST_INIT(&srule->expr);
2608 curproxy->conf.args.ctx = ARGC_USRV;
2609 err = NULL;
2610 if (!parse_logformat_string(server_name, curproxy, &srule->expr, 0, SMP_VAL_FE_HRQ_HDR, &err)) {
2611 ha_alert("Parsing [%s:%d]; use-server rule failed to parse log-format '%s' : %s.\n",
2612 srule->file, srule->line, server_name, err);
2613 free(err);
2614 cfgerr++;
2615 continue;
2616 }
2617 node = LIST_NEXT(&srule->expr, struct logformat_node *, list);
2618
2619 if (!LIST_ISEMPTY(&srule->expr)) {
2620 if (node->type != LOG_FMT_TEXT || node->list.n != &srule->expr) {
2621 srule->dynamic = 1;
2622 free(server_name);
2623 continue;
2624 }
Christopher Fauletf82ea4a2020-05-07 15:59:33 +02002625 /* Only one element in the list, a simple string: free the expression and
2626 * fall back to static rule
2627 */
Willy Tarreau2b718102021-04-21 07:32:39 +02002628 LIST_DELETE(&node->list);
Jerome Magnin824186b2020-03-29 09:37:12 +02002629 free(node->arg);
2630 free(node);
2631 }
2632
2633 srule->dynamic = 0;
2634 srule->srv.name = server_name;
2635 target = findserver(curproxy, srule->srv.name);
Christopher Faulet581db2b2021-03-26 10:02:46 +01002636 err_code |= warnif_tcp_http_cond(curproxy, srule->cond);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002637
2638 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002639 ha_alert("config : %s '%s' : unable to find server '%s' referenced in a 'use-server' rule.\n",
2640 proxy_type_str(curproxy), curproxy->id, srule->srv.name);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002641 cfgerr++;
2642 continue;
2643 }
Willy Tarreau35cd7342021-02-26 20:51:47 +01002644 ha_free(&srule->srv.name);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002645 srule->srv.ptr = target;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002646 }
2647
Emeric Brunb982a3d2010-01-04 15:45:53 +01002648 /* find the target table for 'stick' rules */
2649 list_for_each_entry(mrule, &curproxy->sticking_rules, list) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002650 struct stktable *target;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002651
Emeric Brun1d33b292010-01-04 15:47:17 +01002652 curproxy->be_req_ana |= AN_REQ_STICKING_RULES;
2653 if (mrule->flags & STK_IS_STORE)
2654 curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
2655
Emeric Brunb982a3d2010-01-04 15:45:53 +01002656 if (mrule->table.name)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002657 target = stktable_find_by_name(mrule->table.name);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002658 else
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002659 target = curproxy->table;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002660
2661 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002662 ha_alert("Proxy '%s': unable to find stick-table '%s'.\n",
Willy Tarreau508d2322020-01-24 07:19:34 +01002663 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002664 cfgerr++;
2665 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002666 else if (!stktable_compatible_sample(mrule->expr, target->type)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002667 ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
2668 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002669 cfgerr++;
2670 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002671 else if (target->proxy && curproxy->bind_proc & ~target->proxy->bind_proc) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01002672 ha_alert("Proxy '%s': stick-table '%s' referenced 'stick-store' rule not present on all processes covered by proxy '%s'.\n",
2673 curproxy->id, target->id, curproxy->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01002674 cfgerr++;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01002675 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01002676 else {
Willy Tarreau35cd7342021-02-26 20:51:47 +01002677 ha_free(&mrule->table.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002678 mrule->table.t = target;
2679 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL);
Thayne McCombs92149f92020-11-20 01:28:26 -07002680 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_KEY, NULL);
Frédéric Lécaille9c3a0ce2019-09-02 14:02:28 +02002681 if (!in_proxies_list(target->proxies_list, curproxy)) {
2682 curproxy->next_stkt_ref = target->proxies_list;
2683 target->proxies_list = curproxy;
2684 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01002685 }
Christopher Faulet581db2b2021-03-26 10:02:46 +01002686 err_code |= warnif_tcp_http_cond(curproxy, mrule->cond);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002687 }
2688
2689 /* find the target table for 'store response' rules */
2690 list_for_each_entry(mrule, &curproxy->storersp_rules, list) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002691 struct stktable *target;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002692
Emeric Brun1d33b292010-01-04 15:47:17 +01002693 curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
2694
Emeric Brunb982a3d2010-01-04 15:45:53 +01002695 if (mrule->table.name)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002696 target = stktable_find_by_name(mrule->table.name);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002697 else
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002698 target = curproxy->table;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002699
2700 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002701 ha_alert("Proxy '%s': unable to find store table '%s'.\n",
Willy Tarreau508d2322020-01-24 07:19:34 +01002702 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002703 cfgerr++;
2704 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002705 else if (!stktable_compatible_sample(mrule->expr, target->type)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002706 ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
2707 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01002708 cfgerr++;
2709 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002710 else if (target->proxy && (curproxy->bind_proc & ~target->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01002711 ha_alert("Proxy '%s': stick-table '%s' referenced 'stick-store' rule not present on all processes covered by proxy '%s'.\n",
2712 curproxy->id, target->id, curproxy->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01002713 cfgerr++;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01002714 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01002715 else {
Willy Tarreau35cd7342021-02-26 20:51:47 +01002716 ha_free(&mrule->table.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002717 mrule->table.t = target;
2718 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL);
Thayne McCombs92149f92020-11-20 01:28:26 -07002719 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_KEY, NULL);
Frédéric Lécaille9c3a0ce2019-09-02 14:02:28 +02002720 if (!in_proxies_list(target->proxies_list, curproxy)) {
2721 curproxy->next_stkt_ref = target->proxies_list;
2722 target->proxies_list = curproxy;
2723 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01002724 }
2725 }
2726
Christopher Faulet42c6cf92021-03-25 17:19:04 +01002727 /* check validity for 'tcp-request' layer 4/5/6/7 rules */
2728 cfgerr += check_action_rules(&curproxy->tcp_req.l4_rules, curproxy, &err_code);
2729 cfgerr += check_action_rules(&curproxy->tcp_req.l5_rules, curproxy, &err_code);
2730 cfgerr += check_action_rules(&curproxy->tcp_req.inspect_rules, curproxy, &err_code);
2731 cfgerr += check_action_rules(&curproxy->tcp_rep.inspect_rules, curproxy, &err_code);
2732 cfgerr += check_action_rules(&curproxy->http_req_rules, curproxy, &err_code);
2733 cfgerr += check_action_rules(&curproxy->http_res_rules, curproxy, &err_code);
2734 cfgerr += check_action_rules(&curproxy->http_after_res_rules, curproxy, &err_code);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002735
Christopher Faulet5eef0182021-03-31 17:13:49 +02002736 /* Warn is a switch-mode http is used on a TCP listener with servers but no backend */
2737 if (!curproxy->defbe.name && LIST_ISEMPTY(&curproxy->switching_rules) && curproxy->srv) {
2738 if ((curproxy->options & PR_O_HTTP_UPG) && curproxy->mode == PR_MODE_TCP)
2739 ha_warning("Proxy '%s' : 'switch-mode http' configured for a %s %s with no backend. "
2740 "Incoming connections upgraded to HTTP cannot be routed to TCP servers\n",
2741 curproxy->id, proxy_mode_str(curproxy->mode), proxy_type_str(curproxy));
2742 }
2743
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002744 if (curproxy->table && curproxy->table->peers.name) {
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002745 struct peers *curpeers;
Emeric Brun32da3c42010-09-23 18:39:19 +02002746
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002747 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002748 if (strcmp(curpeers->id, curproxy->table->peers.name) == 0) {
Willy Tarreau35cd7342021-02-26 20:51:47 +01002749 ha_free(&curproxy->table->peers.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002750 curproxy->table->peers.p = curpeers;
Emeric Brun32da3c42010-09-23 18:39:19 +02002751 break;
2752 }
2753 }
2754
2755 if (!curpeers) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002756 ha_alert("Proxy '%s': unable to find sync peers '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002757 curproxy->id, curproxy->table->peers.name);
Willy Tarreau35cd7342021-02-26 20:51:47 +01002758 ha_free(&curproxy->table->peers.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002759 curproxy->table->peers.p = NULL;
Emeric Brun32da3c42010-09-23 18:39:19 +02002760 cfgerr++;
2761 }
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02002762 else if (curpeers->disabled) {
Willy Tarreau77e4bd12015-05-01 20:02:17 +02002763 /* silently disable this peers section */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002764 curproxy->table->peers.p = NULL;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02002765 }
Emeric Brun32da3c42010-09-23 18:39:19 +02002766 else if (!curpeers->peers_fe) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002767 ha_alert("Proxy '%s': unable to find local peer '%s' in peers section '%s'.\n",
2768 curproxy->id, localpeer, curpeers->id);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002769 curproxy->table->peers.p = NULL;
Emeric Brun32da3c42010-09-23 18:39:19 +02002770 cfgerr++;
2771 }
2772 }
2773
Simon Horman9dc49962015-01-30 11:22:59 +09002774
2775 if (curproxy->email_alert.mailers.name) {
2776 struct mailers *curmailers = mailers;
2777
2778 for (curmailers = mailers; curmailers; curmailers = curmailers->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002779 if (strcmp(curmailers->id, curproxy->email_alert.mailers.name) == 0)
Simon Horman9dc49962015-01-30 11:22:59 +09002780 break;
Simon Horman9dc49962015-01-30 11:22:59 +09002781 }
Simon Horman9dc49962015-01-30 11:22:59 +09002782 if (!curmailers) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002783 ha_alert("Proxy '%s': unable to find mailers '%s'.\n",
2784 curproxy->id, curproxy->email_alert.mailers.name);
Simon Horman9dc49962015-01-30 11:22:59 +09002785 free_email_alert(curproxy);
2786 cfgerr++;
2787 }
Christopher Faulet0108bb32017-10-20 21:34:32 +02002788 else {
2789 err = NULL;
2790 if (init_email_alert(curmailers, curproxy, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002791 ha_alert("Proxy '%s': %s.\n", curproxy->id, err);
Christopher Faulet0108bb32017-10-20 21:34:32 +02002792 free(err);
2793 cfgerr++;
2794 }
2795 }
Simon Horman9dc49962015-01-30 11:22:59 +09002796 }
2797
Willy Tarreau5bbc6762021-02-12 11:49:25 +01002798 if (curproxy->uri_auth && !(curproxy->uri_auth->flags & STAT_CONVDONE) &&
Willy Tarreauff011f22011-01-06 17:51:27 +01002799 !LIST_ISEMPTY(&curproxy->uri_auth->http_req_rules) &&
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002800 (curproxy->uri_auth->userlist || curproxy->uri_auth->auth_realm )) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002801 ha_alert("%s '%s': stats 'auth'/'realm' and 'http-request' can't be used at the same time.\n",
2802 "proxy", curproxy->id);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002803 cfgerr++;
2804 goto out_uri_auth_compat;
2805 }
2806
Willy Tarreauee4f5f82019-10-09 09:59:22 +02002807 if (curproxy->uri_auth && curproxy->uri_auth->userlist &&
Willy Tarreau5bbc6762021-02-12 11:49:25 +01002808 (!(curproxy->uri_auth->flags & STAT_CONVDONE) ||
Willy Tarreauee4f5f82019-10-09 09:59:22 +02002809 LIST_ISEMPTY(&curproxy->uri_auth->http_req_rules))) {
Willy Tarreau95fa4692010-02-01 13:05:50 +01002810 const char *uri_auth_compat_req[10];
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002811 struct act_rule *rule;
Amaury Denoyelle3efee652021-03-08 17:31:39 +01002812 i = 0;
2813
Willy Tarreau95fa4692010-02-01 13:05:50 +01002814 /* build the ACL condition from scratch. We're relying on anonymous ACLs for that */
2815 uri_auth_compat_req[i++] = "auth";
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002816
2817 if (curproxy->uri_auth->auth_realm) {
Willy Tarreau95fa4692010-02-01 13:05:50 +01002818 uri_auth_compat_req[i++] = "realm";
2819 uri_auth_compat_req[i++] = curproxy->uri_auth->auth_realm;
2820 }
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002821
Willy Tarreau95fa4692010-02-01 13:05:50 +01002822 uri_auth_compat_req[i++] = "unless";
2823 uri_auth_compat_req[i++] = "{";
2824 uri_auth_compat_req[i++] = "http_auth(.internal-stats-userlist)";
2825 uri_auth_compat_req[i++] = "}";
2826 uri_auth_compat_req[i++] = "";
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002827
Willy Tarreauff011f22011-01-06 17:51:27 +01002828 rule = parse_http_req_cond(uri_auth_compat_req, "internal-stats-auth-compat", 0, curproxy);
2829 if (!rule) {
Willy Tarreau95fa4692010-02-01 13:05:50 +01002830 cfgerr++;
2831 break;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002832 }
2833
Willy Tarreau2b718102021-04-21 07:32:39 +02002834 LIST_APPEND(&curproxy->uri_auth->http_req_rules, &rule->list);
Willy Tarreau95fa4692010-02-01 13:05:50 +01002835
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002836 if (curproxy->uri_auth->auth_realm) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002837 ha_free(&curproxy->uri_auth->auth_realm);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002838 }
Willy Tarreau5bbc6762021-02-12 11:49:25 +01002839 curproxy->uri_auth->flags |= STAT_CONVDONE;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002840 }
2841out_uri_auth_compat:
2842
Dragan Dosen43885c72015-10-01 13:18:13 +02002843 /* check whether we have a log server that uses RFC5424 log format */
Dragan Dosen1322d092015-09-22 16:05:32 +02002844 list_for_each_entry(tmplogsrv, &curproxy->logsrvs, list) {
Dragan Dosen43885c72015-10-01 13:18:13 +02002845 if (tmplogsrv->format == LOG_FORMAT_RFC5424) {
2846 if (!curproxy->conf.logformat_sd_string) {
2847 /* set the default logformat_sd_string */
2848 curproxy->conf.logformat_sd_string = default_rfc5424_sd_log_format;
2849 }
Dragan Dosen1322d092015-09-22 16:05:32 +02002850 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02002851 }
Dragan Dosen1322d092015-09-22 16:05:32 +02002852 }
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02002853
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002854 /* compile the log format */
2855 if (!(curproxy->cap & PR_CAP_FE)) {
Willy Tarreau62a61232013-04-12 18:13:46 +02002856 if (curproxy->conf.logformat_string != default_http_log_format &&
2857 curproxy->conf.logformat_string != default_tcp_log_format &&
2858 curproxy->conf.logformat_string != clf_http_log_format)
2859 free(curproxy->conf.logformat_string);
2860 curproxy->conf.logformat_string = NULL;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002861 ha_free(&curproxy->conf.lfs_file);
Willy Tarreau62a61232013-04-12 18:13:46 +02002862 curproxy->conf.lfs_line = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002863
2864 if (curproxy->conf.logformat_sd_string != default_rfc5424_sd_log_format)
2865 free(curproxy->conf.logformat_sd_string);
2866 curproxy->conf.logformat_sd_string = NULL;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002867 ha_free(&curproxy->conf.lfsd_file);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002868 curproxy->conf.lfsd_line = 0;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002869 }
2870
Willy Tarreau62a61232013-04-12 18:13:46 +02002871 if (curproxy->conf.logformat_string) {
2872 curproxy->conf.args.ctx = ARGC_LOG;
2873 curproxy->conf.args.file = curproxy->conf.lfs_file;
2874 curproxy->conf.args.line = curproxy->conf.lfs_line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002875 err = NULL;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +02002876 if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat,
2877 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002878 SMP_VAL_FE_LOG_END, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002879 ha_alert("Parsing [%s:%d]: failed to parse log-format : %s.\n",
2880 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002881 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01002882 cfgerr++;
2883 }
Willy Tarreau62a61232013-04-12 18:13:46 +02002884 curproxy->conf.args.file = NULL;
2885 curproxy->conf.args.line = 0;
2886 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002887
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002888 if (curproxy->conf.logformat_sd_string) {
2889 curproxy->conf.args.ctx = ARGC_LOGSD;
2890 curproxy->conf.args.file = curproxy->conf.lfsd_file;
2891 curproxy->conf.args.line = curproxy->conf.lfsd_line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002892 err = NULL;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +02002893 if (!parse_logformat_string(curproxy->conf.logformat_sd_string, curproxy, &curproxy->logformat_sd,
2894 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002895 SMP_VAL_FE_LOG_END, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002896 ha_alert("Parsing [%s:%d]: failed to parse log-format-sd : %s.\n",
2897 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002898 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01002899 cfgerr++;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002900 } else if (!add_to_logformat_list(NULL, NULL, LF_SEPARATOR, &curproxy->logformat_sd, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002901 ha_alert("Parsing [%s:%d]: failed to parse log-format-sd : %s.\n",
2902 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002903 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01002904 cfgerr++;
2905 }
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002906 curproxy->conf.args.file = NULL;
2907 curproxy->conf.args.line = 0;
2908 }
2909
Willy Tarreau62a61232013-04-12 18:13:46 +02002910 if (curproxy->conf.uniqueid_format_string) {
2911 curproxy->conf.args.ctx = ARGC_UIF;
2912 curproxy->conf.args.file = curproxy->conf.uif_file;
2913 curproxy->conf.args.line = curproxy->conf.uif_line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002914 err = NULL;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +02002915 if (!parse_logformat_string(curproxy->conf.uniqueid_format_string, curproxy, &curproxy->format_unique_id,
2916 LOG_OPT_HTTP|LOG_OPT_MERGE_SPACES,
2917 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR
2918 : SMP_VAL_BE_HRQ_HDR,
2919 &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002920 ha_alert("Parsing [%s:%d]: failed to parse unique-id : %s.\n",
2921 curproxy->conf.uif_file, curproxy->conf.uif_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002922 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01002923 cfgerr++;
2924 }
Willy Tarreau62a61232013-04-12 18:13:46 +02002925 curproxy->conf.args.file = NULL;
2926 curproxy->conf.args.line = 0;
2927 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002928
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01002929 /* only now we can check if some args remain unresolved.
2930 * This must be done after the users and groups resolution.
2931 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01002932 err = NULL;
2933 i = smp_resolve_args(curproxy, &err);
2934 cfgerr += i;
2935 if (i) {
2936 indent_msg(&err, 8);
2937 ha_alert("%s%s\n", i > 1 ? "multiple argument resolution errors:" : "", err);
2938 ha_free(&err);
2939 } else
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002940 cfgerr += acl_find_targets(curproxy);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002941
Willy Tarreau2738a142006-07-08 17:28:09 +02002942 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002943 (((curproxy->cap & PR_CAP_FE) && !curproxy->timeout.client) ||
Willy Tarreaud825eef2007-05-12 22:35:00 +02002944 ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) &&
Willy Tarreauce887fd2012-05-12 12:50:00 +02002945 (!curproxy->timeout.connect ||
2946 (!curproxy->timeout.server && (curproxy->mode == PR_MODE_HTTP || !curproxy->timeout.tunnel)))))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002947 ha_warning("config : missing timeouts for %s '%s'.\n"
2948 " | While not properly invalid, you will certainly encounter various problems\n"
2949 " | with such a configuration. To fix this, please ensure that all following\n"
2950 " | timeouts are set to a non-zero value: 'client', 'connect', 'server'.\n",
2951 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubb925012009-07-23 13:36:36 +02002952 err_code |= ERR_WARN;
Willy Tarreau2738a142006-07-08 17:28:09 +02002953 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02002954
Willy Tarreau1fa31262007-12-03 00:36:16 +01002955 /* Historically, the tarpit and queue timeouts were inherited from contimeout.
2956 * We must still support older configurations, so let's find out whether those
2957 * parameters have been set or must be copied from contimeouts.
2958 */
Willy Tarreau937c3ea2021-02-12 11:14:35 +01002959 if (!curproxy->timeout.tarpit)
2960 curproxy->timeout.tarpit = curproxy->timeout.connect;
2961 if ((curproxy->cap & PR_CAP_BE) && !curproxy->timeout.queue)
2962 curproxy->timeout.queue = curproxy->timeout.connect;
Willy Tarreau1fa31262007-12-03 00:36:16 +01002963
Christopher Faulete5870d82020-04-15 11:32:03 +02002964 if ((curproxy->tcpcheck_rules.flags & TCPCHK_RULES_UNUSED_TCP_RS)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002965 ha_warning("config : %s '%s' uses tcp-check rules without 'option tcp-check', so the rules are ignored.\n",
2966 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau215663d2014-06-13 18:30:23 +02002967 err_code |= ERR_WARN;
2968 }
2969
Willy Tarreau193b8c62012-11-22 00:17:38 +01002970 /* ensure that cookie capture length is not too large */
2971 if (curproxy->capture_len >= global.tune.cookie_len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002972 ha_warning("config : truncating capture length to %d bytes for %s '%s'.\n",
2973 global.tune.cookie_len - 1, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau193b8c62012-11-22 00:17:38 +01002974 err_code |= ERR_WARN;
2975 curproxy->capture_len = global.tune.cookie_len - 1;
2976 }
2977
Willy Tarreaucf7f3202007-05-13 22:46:04 +02002978 /* The small pools required for the capture lists */
Willy Tarreau9a54e132012-03-24 08:33:05 +01002979 if (curproxy->nb_req_cap) {
Willy Tarreaud9ed3d22014-06-13 12:23:06 +02002980 curproxy->req_cap_pool = create_pool("ptrcap",
2981 curproxy->nb_req_cap * sizeof(char *),
2982 MEM_F_SHARED);
Willy Tarreau9a54e132012-03-24 08:33:05 +01002983 }
2984
2985 if (curproxy->nb_rsp_cap) {
Willy Tarreaud9ed3d22014-06-13 12:23:06 +02002986 curproxy->rsp_cap_pool = create_pool("ptrcap",
2987 curproxy->nb_rsp_cap * sizeof(char *),
2988 MEM_F_SHARED);
Willy Tarreau9a54e132012-03-24 08:33:05 +01002989 }
Willy Tarreaucf7f3202007-05-13 22:46:04 +02002990
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002991 switch (curproxy->load_server_state_from_file) {
2992 case PR_SRV_STATE_FILE_UNSPEC:
2993 curproxy->load_server_state_from_file = PR_SRV_STATE_FILE_NONE;
2994 break;
2995 case PR_SRV_STATE_FILE_GLOBAL:
2996 if (!global.server_state_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002997 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",
2998 curproxy->id);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002999 err_code |= ERR_WARN;
3000 }
3001 break;
3002 }
3003
Willy Tarreaubaaee002006-06-26 02:48:02 +02003004 /* first, we will invert the servers list order */
3005 newsrv = NULL;
3006 while (curproxy->srv) {
3007 struct server *next;
3008
3009 next = curproxy->srv->next;
3010 curproxy->srv->next = newsrv;
3011 newsrv = curproxy->srv;
3012 if (!next)
3013 break;
3014 curproxy->srv = next;
3015 }
3016
Willy Tarreau17edc812014-01-03 12:14:34 +01003017 /* Check that no server name conflicts. This causes trouble in the stats.
3018 * We only emit a warning for the first conflict affecting each server,
3019 * in order to avoid combinatory explosion if all servers have the same
3020 * name. We do that only for servers which do not have an explicit ID,
3021 * because these IDs were made also for distinguishing them and we don't
3022 * want to annoy people who correctly manage them.
3023 */
3024 for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
3025 struct server *other_srv;
3026
3027 if (newsrv->puid)
3028 continue;
3029
3030 for (other_srv = curproxy->srv; other_srv && other_srv != newsrv; other_srv = other_srv->next) {
3031 if (!other_srv->puid && strcmp(other_srv->id, newsrv->id) == 0) {
Willy Tarreaub01302f2019-05-27 19:31:06 +02003032 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 +01003033 newsrv->conf.file, newsrv->conf.line,
3034 proxy_type_str(curproxy), curproxy->id,
3035 newsrv->id, other_srv->conf.line);
Willy Tarreaub01302f2019-05-27 19:31:06 +02003036 cfgerr++;
Willy Tarreau17edc812014-01-03 12:14:34 +01003037 break;
3038 }
3039 }
3040 }
3041
Willy Tarreaudd701652010-05-25 23:03:02 +02003042 /* assign automatic UIDs to servers which don't have one yet */
3043 next_id = 1;
3044 newsrv = curproxy->srv;
3045 while (newsrv != NULL) {
3046 if (!newsrv->puid) {
3047 /* server ID not set, use automatic numbering with first
3048 * spare entry starting with next_svid.
3049 */
3050 next_id = get_next_id(&curproxy->conf.used_server_id, next_id);
3051 newsrv->conf.id.key = newsrv->puid = next_id;
3052 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Frédéric Lécaille84d60462019-05-17 14:29:15 +02003053 newsrv->conf.name.key = newsrv->id;
3054 ebis_insert(&curproxy->conf.used_server_name, &newsrv->conf.name);
Willy Tarreaudd701652010-05-25 23:03:02 +02003055 }
3056 next_id++;
3057 newsrv = newsrv->next;
3058 }
3059
Willy Tarreau20697042007-11-15 23:26:18 +01003060 curproxy->lbprm.wmult = 1; /* default weight multiplier */
Willy Tarreau5dc2fa62007-11-19 19:10:18 +01003061 curproxy->lbprm.wdiv = 1; /* default weight divider */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003062
Willy Tarreau62c3be22012-01-20 13:12:32 +01003063 /*
3064 * If this server supports a maxconn parameter, it needs a dedicated
3065 * tasks to fill the emptied slots when a connection leaves.
3066 * Also, resolve deferred tracking dependency if needed.
3067 */
3068 newsrv = curproxy->srv;
3069 while (newsrv != NULL) {
3070 if (newsrv->minconn > newsrv->maxconn) {
3071 /* Only 'minconn' was specified, or it was higher than or equal
3072 * to 'maxconn'. Let's turn this into maxconn and clean it, as
3073 * this will avoid further useless expensive computations.
3074 */
3075 newsrv->maxconn = newsrv->minconn;
3076 } else if (newsrv->maxconn && !newsrv->minconn) {
3077 /* minconn was not specified, so we set it to maxconn */
3078 newsrv->minconn = newsrv->maxconn;
3079 }
3080
William Dauchyf6370442020-11-14 19:25:33 +01003081 /* this will also properly set the transport layer for
3082 * prod and checks
3083 * if default-server have use_ssl, prerare ssl init
3084 * without activating it */
3085 if (newsrv->use_ssl == 1 || newsrv->check.use_ssl == 1 ||
3086 (newsrv->proxy->options & PR_O_TCPCHK_SSL) ||
3087 (newsrv->use_ssl != 1 && curproxy->defsrv.use_ssl == 1)) {
Willy Tarreau17d45382016-12-22 21:16:08 +01003088 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv)
3089 cfgerr += xprt_get(XPRT_SSL)->prepare_srv(newsrv);
3090 }
Emeric Brun94324a42012-10-11 14:00:19 +02003091
Willy Tarreau034c88c2017-01-23 23:36:45 +01003092 if ((newsrv->flags & SRV_F_FASTOPEN) &&
3093 ((curproxy->retry_type & (PR_RE_DISCONNECTED | PR_RE_TIMEOUT)) !=
3094 (PR_RE_DISCONNECTED | PR_RE_TIMEOUT)))
3095 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",
3096 newsrv->conf.file, newsrv->conf.line,
3097 proxy_type_str(curproxy), curproxy->id,
3098 newsrv->id);
3099
Willy Tarreau62c3be22012-01-20 13:12:32 +01003100 if (newsrv->trackit) {
3101 struct proxy *px;
Willy Tarreau32091232014-05-16 13:52:00 +02003102 struct server *srv, *loop;
Willy Tarreau62c3be22012-01-20 13:12:32 +01003103 char *pname, *sname;
3104
3105 pname = newsrv->trackit;
3106 sname = strrchr(pname, '/');
3107
3108 if (sname)
3109 *sname++ = '\0';
3110 else {
3111 sname = pname;
3112 pname = NULL;
3113 }
3114
3115 if (pname) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02003116 px = proxy_be_by_name(pname);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003117 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003118 ha_alert("config : %s '%s', server '%s': unable to find required proxy '%s' for tracking.\n",
3119 proxy_type_str(curproxy), curproxy->id,
3120 newsrv->id, pname);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003121 cfgerr++;
3122 goto next_srv;
3123 }
3124 } else
3125 px = curproxy;
3126
3127 srv = findserver(px, sname);
3128 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003129 ha_alert("config : %s '%s', server '%s': unable to find required server '%s' for tracking.\n",
3130 proxy_type_str(curproxy), curproxy->id,
3131 newsrv->id, sname);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003132 cfgerr++;
3133 goto next_srv;
3134 }
3135
Christopher Faulet8892e5d2020-03-26 19:48:20 +01003136 if (!srv->do_check && !srv->do_agent && !srv->track && !srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003137 ha_alert("config : %s '%s', server '%s': unable to use %s/%s for "
3138 "tracking as it does not have any check nor agent enabled.\n",
3139 proxy_type_str(curproxy), curproxy->id,
3140 newsrv->id, px->id, srv->id);
Willy Tarreau32091232014-05-16 13:52:00 +02003141 cfgerr++;
3142 goto next_srv;
3143 }
3144
3145 for (loop = srv->track; loop && loop != newsrv; loop = loop->track);
3146
Frédéric Lécaille2efc6492017-03-14 14:32:17 +01003147 if (newsrv == srv || loop) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003148 ha_alert("config : %s '%s', server '%s': unable to track %s/%s as it "
3149 "belongs to a tracking chain looping back to %s/%s.\n",
3150 proxy_type_str(curproxy), curproxy->id,
3151 newsrv->id, px->id, srv->id, px->id,
3152 newsrv == srv ? srv->id : loop->id);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003153 cfgerr++;
3154 goto next_srv;
3155 }
3156
3157 if (curproxy != px &&
3158 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003159 ha_alert("config : %s '%s', server '%s': unable to use %s/%s for"
3160 "tracking: disable-on-404 option inconsistency.\n",
3161 proxy_type_str(curproxy), curproxy->id,
3162 newsrv->id, px->id, srv->id);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003163 cfgerr++;
3164 goto next_srv;
3165 }
3166
Willy Tarreau62c3be22012-01-20 13:12:32 +01003167 newsrv->track = srv;
Willy Tarreau1a53a3a2013-12-11 15:27:05 +01003168 newsrv->tracknext = srv->trackers;
3169 srv->trackers = newsrv;
Willy Tarreau62c3be22012-01-20 13:12:32 +01003170
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003171 ha_free(&newsrv->trackit);
Willy Tarreau62c3be22012-01-20 13:12:32 +01003172 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003173
Willy Tarreau62c3be22012-01-20 13:12:32 +01003174 next_srv:
3175 newsrv = newsrv->next;
3176 }
3177
Olivier Houchard4e694042017-03-14 20:01:29 +01003178 /*
3179 * Try to generate dynamic cookies for servers now.
3180 * It couldn't be done earlier, since at the time we parsed
3181 * the server line, we may not have known yet that we
3182 * should use dynamic cookies, or the secret key may not
3183 * have been provided yet.
3184 */
3185 if (curproxy->ck_opts & PR_CK_DYNAMIC) {
3186 newsrv = curproxy->srv;
3187 while (newsrv != NULL) {
3188 srv_set_dyncookie(newsrv);
3189 newsrv = newsrv->next;
3190 }
3191
3192 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003193 /* We have to initialize the server lookup mechanism depending
Joseph Herlanta14c03e2018-11-15 14:04:19 -08003194 * on what LB algorithm was chosen.
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003195 */
3196
3197 curproxy->lbprm.algo &= ~(BE_LB_LKUP | BE_LB_PROP_DYN);
3198 switch (curproxy->lbprm.algo & BE_LB_KIND) {
3199 case BE_LB_KIND_RR:
Willy Tarreau9757a382009-10-03 12:56:50 +02003200 if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_RR_STATIC) {
3201 curproxy->lbprm.algo |= BE_LB_LKUP_MAP;
3202 init_server_map(curproxy);
Willy Tarreau760e81d2018-05-03 07:20:40 +02003203 } else if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_RR_RANDOM) {
3204 curproxy->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
3205 chash_init_server_tree(curproxy);
Willy Tarreau9757a382009-10-03 12:56:50 +02003206 } else {
3207 curproxy->lbprm.algo |= BE_LB_LKUP_RRTREE | BE_LB_PROP_DYN;
3208 fwrr_init_server_groups(curproxy);
3209 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003210 break;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003211
Willy Tarreau3ebb1162012-02-13 16:57:44 +01003212 case BE_LB_KIND_CB:
Willy Tarreauf09c6602012-02-13 17:12:08 +01003213 if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_CB_LC) {
3214 curproxy->lbprm.algo |= BE_LB_LKUP_LCTREE | BE_LB_PROP_DYN;
3215 fwlc_init_server_tree(curproxy);
3216 } else {
3217 curproxy->lbprm.algo |= BE_LB_LKUP_FSTREE | BE_LB_PROP_DYN;
3218 fas_init_server_tree(curproxy);
3219 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003220 break;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003221
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003222 case BE_LB_KIND_HI:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003223 if ((curproxy->lbprm.algo & BE_LB_HASH_TYPE) == BE_LB_HASH_CONS) {
3224 curproxy->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
3225 chash_init_server_tree(curproxy);
3226 } else {
3227 curproxy->lbprm.algo |= BE_LB_LKUP_MAP;
3228 init_server_map(curproxy);
3229 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003230 break;
3231 }
Willy Tarreaucd10def2020-10-17 18:48:47 +02003232 HA_RWLOCK_INIT(&curproxy->lbprm.lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003233
3234 if (curproxy->options & PR_O_LOGASAP)
3235 curproxy->to_log &= ~LW_BYTES;
3236
Willy Tarreaue7ded1f2009-08-09 10:11:45 +02003237 if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003238 (curproxy->cap & PR_CAP_FE) && LIST_ISEMPTY(&curproxy->logsrvs) &&
3239 (!LIST_ISEMPTY(&curproxy->logformat) || !LIST_ISEMPTY(&curproxy->logformat_sd))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003240 ha_warning("config : log format ignored for %s '%s' since it has no log address.\n",
3241 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue7ded1f2009-08-09 10:11:45 +02003242 err_code |= ERR_WARN;
3243 }
3244
Christopher Faulet3b6446f2021-03-15 15:10:38 +01003245 if (curproxy->mode != PR_MODE_HTTP && !(curproxy->options & PR_O_HTTP_UPG)) {
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003246 int optnum;
3247
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003248 if (curproxy->uri_auth) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003249 ha_warning("config : 'stats' statement ignored for %s '%s' as it requires HTTP mode.\n",
3250 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003251 err_code |= ERR_WARN;
3252 curproxy->uri_auth = NULL;
3253 }
3254
Willy Tarreaude7dc882017-03-10 11:49:21 +01003255 if (curproxy->capture_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003256 ha_warning("config : 'capture' statement ignored for %s '%s' as it requires HTTP mode.\n",
3257 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003258 err_code |= ERR_WARN;
3259 }
3260
3261 if (!LIST_ISEMPTY(&curproxy->http_req_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003262 ha_warning("config : 'http-request' rules ignored for %s '%s' as they require HTTP mode.\n",
3263 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003264 err_code |= ERR_WARN;
3265 }
3266
3267 if (!LIST_ISEMPTY(&curproxy->http_res_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003268 ha_warning("config : 'http-response' rules ignored for %s '%s' as they require HTTP mode.\n",
3269 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003270 err_code |= ERR_WARN;
3271 }
3272
Christopher Fauletbb7abed2021-03-15 15:09:21 +01003273 if (!LIST_ISEMPTY(&curproxy->http_after_res_rules)) {
3274 ha_warning("config : 'http-after-response' rules ignored for %s '%s' as they require HTTP mode.\n",
3275 proxy_type_str(curproxy), curproxy->id);
3276 err_code |= ERR_WARN;
3277 }
3278
Willy Tarreaude7dc882017-03-10 11:49:21 +01003279 if (!LIST_ISEMPTY(&curproxy->redirect_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003280 ha_warning("config : 'redirect' rules ignored for %s '%s' as they require HTTP mode.\n",
3281 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003282 err_code |= ERR_WARN;
3283 }
3284
Willy Tarreau87cf5142011-08-19 22:57:24 +02003285 if (curproxy->options & (PR_O_FWDFOR | PR_O_FF_ALWAYS)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003286 ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3287 "forwardfor", proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003288 err_code |= ERR_WARN;
Willy Tarreau87cf5142011-08-19 22:57:24 +02003289 curproxy->options &= ~(PR_O_FWDFOR | PR_O_FF_ALWAYS);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003290 }
3291
3292 if (curproxy->options & PR_O_ORGTO) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003293 ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3294 "originalto", proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003295 err_code |= ERR_WARN;
3296 curproxy->options &= ~PR_O_ORGTO;
3297 }
3298
3299 for (optnum = 0; cfg_opts[optnum].name; optnum++) {
3300 if (cfg_opts[optnum].mode == PR_MODE_HTTP &&
3301 (curproxy->cap & cfg_opts[optnum].cap) &&
3302 (curproxy->options & cfg_opts[optnum].val)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003303 ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3304 cfg_opts[optnum].name, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003305 err_code |= ERR_WARN;
3306 curproxy->options &= ~cfg_opts[optnum].val;
3307 }
3308 }
3309
3310 for (optnum = 0; cfg_opts2[optnum].name; optnum++) {
3311 if (cfg_opts2[optnum].mode == PR_MODE_HTTP &&
3312 (curproxy->cap & cfg_opts2[optnum].cap) &&
3313 (curproxy->options2 & cfg_opts2[optnum].val)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003314 ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3315 cfg_opts2[optnum].name, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003316 err_code |= ERR_WARN;
3317 curproxy->options2 &= ~cfg_opts2[optnum].val;
3318 }
3319 }
Willy Tarreaubce70882009-09-07 11:51:47 +02003320
Willy Tarreau29fbe512015-08-20 19:35:14 +02003321#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreauef9a3602012-12-08 22:29:20 +01003322 if (curproxy->conn_src.bind_hdr_occ) {
3323 curproxy->conn_src.bind_hdr_occ = 0;
Christopher Faulet767a84b2017-11-24 16:50:31 +01003324 ha_warning("config : %s '%s' : ignoring use of header %s as source IP in non-HTTP mode.\n",
3325 proxy_type_str(curproxy), curproxy->id, curproxy->conn_src.bind_hdr_name);
Willy Tarreaubce70882009-09-07 11:51:47 +02003326 err_code |= ERR_WARN;
3327 }
Willy Tarreauefa5f512010-03-30 20:13:29 +02003328#endif
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003329 }
3330
Willy Tarreaubaaee002006-06-26 02:48:02 +02003331 /*
Willy Tarreau21d2af32008-02-14 20:25:24 +01003332 * ensure that we're not cross-dressing a TCP server into HTTP.
3333 */
3334 newsrv = curproxy->srv;
3335 while (newsrv != NULL) {
Willy Tarreau0cec3312011-10-31 13:49:26 +01003336 if ((curproxy->mode != PR_MODE_HTTP) && newsrv->rdr_len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003337 ha_alert("config : %s '%s' : server cannot have cookie or redirect prefix in non-HTTP mode.\n",
3338 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubb925012009-07-23 13:36:36 +02003339 cfgerr++;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003340 }
Willy Tarreaubce70882009-09-07 11:51:47 +02003341
Willy Tarreau0cec3312011-10-31 13:49:26 +01003342 if ((curproxy->mode != PR_MODE_HTTP) && newsrv->cklen) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003343 ha_warning("config : %s '%s' : ignoring cookie for server '%s' as HTTP mode is disabled.\n",
3344 proxy_type_str(curproxy), curproxy->id, newsrv->id);
Willy Tarreau0cec3312011-10-31 13:49:26 +01003345 err_code |= ERR_WARN;
3346 }
3347
Willy Tarreauc93cd162014-05-13 15:54:22 +02003348 if ((newsrv->flags & SRV_F_MAPPORTS) && (curproxy->options2 & PR_O2_RDPC_PRST)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003349 ha_warning("config : %s '%s' : RDP cookie persistence will not work for server '%s' because it lacks an explicit port number.\n",
3350 proxy_type_str(curproxy), curproxy->id, newsrv->id);
Willy Tarreau82ffa392013-08-13 17:19:08 +02003351 err_code |= ERR_WARN;
3352 }
3353
Willy Tarreau29fbe512015-08-20 19:35:14 +02003354#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreauef9a3602012-12-08 22:29:20 +01003355 if (curproxy->mode != PR_MODE_HTTP && newsrv->conn_src.bind_hdr_occ) {
3356 newsrv->conn_src.bind_hdr_occ = 0;
Christopher Faulet767a84b2017-11-24 16:50:31 +01003357 ha_warning("config : %s '%s' : server %s cannot use header %s as source IP in non-HTTP mode.\n",
3358 proxy_type_str(curproxy), curproxy->id, newsrv->id, newsrv->conn_src.bind_hdr_name);
Willy Tarreaubce70882009-09-07 11:51:47 +02003359 err_code |= ERR_WARN;
3360 }
Willy Tarreauefa5f512010-03-30 20:13:29 +02003361#endif
Willy Tarreau4c183462017-01-06 12:21:38 +01003362
Willy Tarreau46deab62018-04-28 07:18:15 +02003363 if ((curproxy->mode != PR_MODE_HTTP) && (curproxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR)
3364 curproxy->options &= ~PR_O_REUSE_MASK;
3365
Willy Tarreau21d2af32008-02-14 20:25:24 +01003366 newsrv = newsrv->next;
3367 }
3368
Christopher Fauletd7c91962015-04-30 11:48:27 +02003369 /* Check filter configuration, if any */
3370 cfgerr += flt_check(curproxy);
3371
Willy Tarreauc1a21672009-08-16 22:37:44 +02003372 if (curproxy->cap & PR_CAP_FE) {
Willy Tarreau050536d2012-10-04 08:47:34 +02003373 if (!curproxy->accept)
3374 curproxy->accept = frontend_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +02003375
Willy Tarreauc1a21672009-08-16 22:37:44 +02003376 if (curproxy->tcp_req.inspect_delay ||
3377 !LIST_ISEMPTY(&curproxy->tcp_req.inspect_rules))
Willy Tarreaufb356202010-08-03 14:02:05 +02003378 curproxy->fe_req_ana |= AN_REQ_INSPECT_FE;
Willy Tarreauc1a21672009-08-16 22:37:44 +02003379
Willy Tarreau4e5b8282009-08-16 22:57:50 +02003380 if (curproxy->mode == PR_MODE_HTTP) {
Willy Tarreauc1a21672009-08-16 22:37:44 +02003381 curproxy->fe_req_ana |= AN_REQ_WAIT_HTTP | AN_REQ_HTTP_PROCESS_FE;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003382 curproxy->fe_rsp_ana |= AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_FE;
Willy Tarreau4e5b8282009-08-16 22:57:50 +02003383 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02003384
William Lallemandcf62f7e2018-10-26 14:47:40 +02003385 if (curproxy->mode == PR_MODE_CLI) {
3386 curproxy->fe_req_ana |= AN_REQ_WAIT_CLI;
3387 curproxy->fe_rsp_ana |= AN_RES_WAIT_CLI;
3388 }
3389
Willy Tarreauc1a21672009-08-16 22:37:44 +02003390 /* both TCP and HTTP must check switching rules */
3391 curproxy->fe_req_ana |= AN_REQ_SWITCHING_RULES;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003392
3393 /* Add filters analyzers if needed */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01003394 if (!LIST_ISEMPTY(&curproxy->filter_configs)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003395 curproxy->fe_req_ana |= AN_REQ_FLT_START_FE | AN_REQ_FLT_XFER_DATA | AN_REQ_FLT_END;
3396 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 +02003397 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02003398 }
3399
3400 if (curproxy->cap & PR_CAP_BE) {
Willy Tarreaufb356202010-08-03 14:02:05 +02003401 if (curproxy->tcp_req.inspect_delay ||
3402 !LIST_ISEMPTY(&curproxy->tcp_req.inspect_rules))
3403 curproxy->be_req_ana |= AN_REQ_INSPECT_BE;
3404
Emeric Brun97679e72010-09-23 17:56:44 +02003405 if (!LIST_ISEMPTY(&curproxy->tcp_rep.inspect_rules))
3406 curproxy->be_rsp_ana |= AN_RES_INSPECT;
3407
Willy Tarreau4e5b8282009-08-16 22:57:50 +02003408 if (curproxy->mode == PR_MODE_HTTP) {
Willy Tarreauc1a21672009-08-16 22:37:44 +02003409 curproxy->be_req_ana |= AN_REQ_WAIT_HTTP | AN_REQ_HTTP_INNER | AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003410 curproxy->be_rsp_ana |= AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE;
Willy Tarreau4e5b8282009-08-16 22:57:50 +02003411 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02003412
3413 /* If the backend does requires RDP cookie persistence, we have to
3414 * enable the corresponding analyser.
3415 */
3416 if (curproxy->options2 & PR_O2_RDPC_PRST)
3417 curproxy->be_req_ana |= AN_REQ_PRST_RDP_COOKIE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003418
3419 /* Add filters analyzers if needed */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01003420 if (!LIST_ISEMPTY(&curproxy->filter_configs)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003421 curproxy->be_req_ana |= AN_REQ_FLT_START_BE | AN_REQ_FLT_XFER_DATA | AN_REQ_FLT_END;
3422 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 +02003423 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02003424 }
Christopher Fauleta717b992018-04-10 14:43:00 +02003425
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003426 /* Check the mux protocols, if any, for each listener and server
Christopher Fauleta717b992018-04-10 14:43:00 +02003427 * attached to the current proxy */
3428 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
3429 int mode = (1 << (curproxy->mode == PR_MODE_HTTP));
Willy Tarreau76a551d2018-12-02 13:09:09 +01003430 const struct mux_proto_list *mux_ent;
Christopher Fauleta717b992018-04-10 14:43:00 +02003431
3432 if (!bind_conf->mux_proto)
3433 continue;
Willy Tarreau76a551d2018-12-02 13:09:09 +01003434
3435 /* it is possible that an incorrect mux was referenced
3436 * due to the proxy's mode not being taken into account
3437 * on first pass. Let's adjust it now.
3438 */
3439 mux_ent = conn_get_best_mux_entry(bind_conf->mux_proto->token, PROTO_SIDE_FE, mode);
3440
3441 if (!mux_ent || !isteq(mux_ent->token, bind_conf->mux_proto->token)) {
Christopher Fauleta717b992018-04-10 14:43:00 +02003442 ha_alert("config : %s '%s' : MUX protocol '%.*s' is not usable for 'bind %s' at [%s:%d].\n",
3443 proxy_type_str(curproxy), curproxy->id,
3444 (int)bind_conf->mux_proto->token.len,
3445 bind_conf->mux_proto->token.ptr,
3446 bind_conf->arg, bind_conf->file, bind_conf->line);
3447 cfgerr++;
3448 }
Willy Tarreau76a551d2018-12-02 13:09:09 +01003449
3450 /* update the mux */
3451 bind_conf->mux_proto = mux_ent;
Christopher Fauleta717b992018-04-10 14:43:00 +02003452 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003453 for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
3454 int mode = (1 << (curproxy->mode == PR_MODE_HTTP));
Willy Tarreau76a551d2018-12-02 13:09:09 +01003455 const struct mux_proto_list *mux_ent;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003456
3457 if (!newsrv->mux_proto)
3458 continue;
Willy Tarreau76a551d2018-12-02 13:09:09 +01003459
3460 /* it is possible that an incorrect mux was referenced
3461 * due to the proxy's mode not being taken into account
3462 * on first pass. Let's adjust it now.
3463 */
3464 mux_ent = conn_get_best_mux_entry(newsrv->mux_proto->token, PROTO_SIDE_BE, mode);
3465
3466 if (!mux_ent || !isteq(mux_ent->token, newsrv->mux_proto->token)) {
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003467 ha_alert("config : %s '%s' : MUX protocol '%.*s' is not usable for server '%s' at [%s:%d].\n",
3468 proxy_type_str(curproxy), curproxy->id,
3469 (int)newsrv->mux_proto->token.len,
3470 newsrv->mux_proto->token.ptr,
3471 newsrv->id, newsrv->conf.file, newsrv->conf.line);
3472 cfgerr++;
3473 }
Willy Tarreau76a551d2018-12-02 13:09:09 +01003474
3475 /* update the mux */
3476 newsrv->mux_proto = mux_ent;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02003477 }
Willy Tarreau4cdac162021-03-05 10:48:42 +01003478 }
Willy Tarreau835daa12019-02-07 14:46:29 +01003479
Willy Tarreau4cdac162021-03-05 10:48:42 +01003480 /***********************************************************/
3481 /* At this point, target names have already been resolved. */
3482 /***********************************************************/
Willy Tarreau835daa12019-02-07 14:46:29 +01003483
Willy Tarreau4cdac162021-03-05 10:48:42 +01003484 /* we must finish to initialize certain things on the servers */
Willy Tarreau835daa12019-02-07 14:46:29 +01003485
Willy Tarreau4cdac162021-03-05 10:48:42 +01003486 list_for_each_entry(newsrv, &servers_list, global_list) {
3487 /* initialize idle conns lists */
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003488 newsrv->per_thr = calloc(global.nbthread, sizeof(*newsrv->per_thr));
3489 if (!newsrv->per_thr) {
3490 ha_alert("parsing [%s:%d] : failed to allocate per-thread lists for server '%s'.\n",
3491 newsrv->conf.file, newsrv->conf.line, newsrv->id);
Willy Tarreau4cdac162021-03-05 10:48:42 +01003492 cfgerr++;
3493 continue;
3494 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003495
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003496 for (i = 0; i < global.nbthread; i++) {
3497 newsrv->per_thr[i].idle_conns = EB_ROOT;
3498 newsrv->per_thr[i].safe_conns = EB_ROOT;
3499 newsrv->per_thr[i].avail_conns = EB_ROOT;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01003500 MT_LIST_INIT(&newsrv->per_thr[i].streams);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003501 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003502
Willy Tarreau4cdac162021-03-05 10:48:42 +01003503 if (newsrv->max_idle_conns != 0) {
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003504 newsrv->curr_idle_thr = calloc(global.nbthread, sizeof(*newsrv->curr_idle_thr));
3505 if (!newsrv->curr_idle_thr) {
3506 ha_alert("parsing [%s:%d] : failed to allocate idle connection tasks for server '%s'.\n",
3507 newsrv->conf.file, newsrv->conf.line, newsrv->id);
3508 cfgerr++;
3509 continue;
3510 }
3511 }
3512 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003513
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003514 idle_conn_task = task_new(MAX_THREADS_MASK);
3515 if (!idle_conn_task) {
3516 ha_alert("parsing : failed to allocate global idle connection task.\n");
3517 cfgerr++;
3518 }
3519 else {
3520 idle_conn_task->process = srv_cleanup_idle_conns;
3521 idle_conn_task->context = NULL;
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003522
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003523 for (i = 0; i < global.nbthread; i++) {
3524 idle_conns[i].cleanup_task = task_new(1UL << i);
3525 if (!idle_conns[i].cleanup_task) {
3526 ha_alert("parsing : failed to allocate idle connection tasks for thread '%d'.\n", i);
3527 cfgerr++;
3528 break;
Willy Tarreau4cdac162021-03-05 10:48:42 +01003529 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003530
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003531 idle_conns[i].cleanup_task->process = srv_cleanup_toremove_conns;
3532 idle_conns[i].cleanup_task->context = NULL;
3533 HA_SPIN_INIT(&idle_conns[i].idle_conns_lock);
3534 MT_LIST_INIT(&idle_conns[i].toremove_conns);
Willy Tarreau835daa12019-02-07 14:46:29 +01003535 }
Willy Tarreau419ead82014-09-16 13:41:21 +02003536 }
3537
Willy Tarreau419ead82014-09-16 13:41:21 +02003538 /* Check multi-process mode compatibility */
3539
Willy Tarreau4975d142021-03-13 11:00:33 +01003540 if (global.nbproc > 1 && global.cli_fe) {
3541 list_for_each_entry(bind_conf, &global.cli_fe->conf.bind, by_fe) {
Willy Tarreau419ead82014-09-16 13:41:21 +02003542 unsigned long mask;
3543
Willy Tarreau4975d142021-03-13 11:00:33 +01003544 mask = proc_mask(global.cli_fe->bind_proc) && all_proc_mask;
Willy Tarreaue26993c2020-09-03 07:18:55 +02003545 mask &= proc_mask(bind_conf->settings.bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003546
3547 /* stop here if more than one process is used */
Willy Tarreau9504dd62018-10-15 09:37:03 +02003548 if (atleast2(mask))
Willy Tarreau419ead82014-09-16 13:41:21 +02003549 break;
3550 }
Willy Tarreau4975d142021-03-13 11:00:33 +01003551 if (&bind_conf->by_fe != &global.cli_fe->conf.bind) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003552 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 +02003553 }
3554 }
3555
3556 /* Make each frontend inherit bind-process from its listeners when not specified. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003557 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreau419ead82014-09-16 13:41:21 +02003558 if (curproxy->bind_proc)
3559 continue;
3560
3561 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
3562 unsigned long mask;
3563
Willy Tarreaue26993c2020-09-03 07:18:55 +02003564 mask = proc_mask(bind_conf->settings.bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003565 curproxy->bind_proc |= mask;
3566 }
Willy Tarreau6daac192019-02-02 17:39:53 +01003567 curproxy->bind_proc = proc_mask(curproxy->bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003568 }
3569
Willy Tarreau4975d142021-03-13 11:00:33 +01003570 if (global.cli_fe) {
3571 list_for_each_entry(bind_conf, &global.cli_fe->conf.bind, by_fe) {
Willy Tarreau419ead82014-09-16 13:41:21 +02003572 unsigned long mask;
3573
Willy Tarreaue26993c2020-09-03 07:18:55 +02003574 mask = bind_conf->settings.bind_proc ? bind_conf->settings.bind_proc : 0;
Willy Tarreau4975d142021-03-13 11:00:33 +01003575 global.cli_fe->bind_proc |= mask;
Willy Tarreau419ead82014-09-16 13:41:21 +02003576 }
Willy Tarreau4975d142021-03-13 11:00:33 +01003577 global.cli_fe->bind_proc = proc_mask(global.cli_fe->bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003578 }
3579
Willy Tarreauacbe8ab2014-10-01 20:50:17 +02003580 /* propagate bindings from frontends to backends. Don't do it if there
3581 * are any fatal errors as we must not call it with unresolved proxies.
3582 */
3583 if (!cfgerr) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003584 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreauacbe8ab2014-10-01 20:50:17 +02003585 if (curproxy->cap & PR_CAP_FE)
3586 propagate_processes(curproxy, NULL);
3587 }
Willy Tarreau419ead82014-09-16 13:41:21 +02003588 }
3589
3590 /* Bind each unbound backend to all processes when not specified. */
Willy Tarreau6daac192019-02-02 17:39:53 +01003591 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next)
3592 curproxy->bind_proc = proc_mask(curproxy->bind_proc);
Willy Tarreau419ead82014-09-16 13:41:21 +02003593
3594 /*******************************************************/
3595 /* At this step, all proxies have a non-null bind_proc */
3596 /*******************************************************/
3597
3598 /* perform the final checks before creating tasks */
3599
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003600 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreau419ead82014-09-16 13:41:21 +02003601 struct listener *listener;
3602 unsigned int next_id;
Willy Tarreauc1a21672009-08-16 22:37:44 +02003603
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02003604 /* Configure SSL for each bind line.
3605 * Note: if configuration fails at some point, the ->ctx member
3606 * remains NULL so that listeners can later detach.
3607 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003608 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
Willy Tarreau55d37912016-12-21 23:38:39 +01003609 if (bind_conf->xprt->prepare_bind_conf &&
3610 bind_conf->xprt->prepare_bind_conf(bind_conf) < 0)
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02003611 cfgerr++;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003612 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02003613
Willy Tarreaue6b98942007-10-29 01:09:36 +01003614 /* adjust this proxy's listeners */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02003615 next_id = 1;
Willy Tarreau4348fad2012-09-20 16:48:07 +02003616 list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) {
Willy Tarreau7c0ffd22016-04-14 11:47:38 +02003617 int nbproc;
3618
3619 nbproc = my_popcountl(curproxy->bind_proc &
Willy Tarreaue26993c2020-09-03 07:18:55 +02003620 (listener->bind_conf->settings.bind_proc ? listener->bind_conf->settings.bind_proc : curproxy->bind_proc) &
Willy Tarreaua38a7172019-02-02 17:11:28 +01003621 all_proc_mask);
Willy Tarreau7c0ffd22016-04-14 11:47:38 +02003622
3623 if (!nbproc) /* no intersection between listener and frontend */
3624 nbproc = 1;
3625
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02003626 if (!listener->luid) {
3627 /* listener ID not set, use automatic numbering with first
3628 * spare entry starting with next_luid.
3629 */
3630 next_id = get_next_id(&curproxy->conf.used_listener_id, next_id);
3631 listener->conf.id.key = listener->luid = next_id;
3632 eb32_insert(&curproxy->conf.used_listener_id, &listener->conf.id);
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02003633 }
Krzysztof Piotr Oledzkidf5cb9f2010-02-05 20:58:27 +01003634 next_id++;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02003635
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003636 /* enable separate counters */
3637 if (curproxy->options2 & PR_O2_SOCKSTAT) {
Willy Tarreauae9bea02016-11-25 14:44:52 +01003638 listener->counters = calloc(1, sizeof(*listener->counters));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003639 if (!listener->name)
3640 memprintf(&listener->name, "sock-%d", listener->luid);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003641 }
Willy Tarreau81796be2012-09-22 19:11:47 +02003642
Willy Tarreaue6b98942007-10-29 01:09:36 +01003643 if (curproxy->options & PR_O_TCP_NOLING)
3644 listener->options |= LI_O_NOLINGER;
Willy Tarreau16a21472012-11-19 12:39:59 +01003645 if (!listener->maxaccept)
Willy Tarreau66161322021-02-19 15:50:27 +01003646 listener->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreau16a21472012-11-19 12:39:59 +01003647
3648 /* we want to have an optimal behaviour on single process mode to
3649 * maximize the work at once, but in multi-process we want to keep
3650 * some fairness between processes, so we target half of the max
3651 * number of events to be balanced over all the processes the proxy
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003652 * is bound to. Remember that maxaccept = -1 must be kept as it is
Willy Tarreau16a21472012-11-19 12:39:59 +01003653 * used to disable the limit.
3654 */
Christopher Faulet02f3cf12019-04-30 14:08:41 +02003655 if (listener->maxaccept > 0 && nbproc > 1) {
3656 listener->maxaccept = (listener->maxaccept + 1) / 2;
Willy Tarreau16a21472012-11-19 12:39:59 +01003657 listener->maxaccept = (listener->maxaccept + nbproc - 1) / nbproc;
3658 }
3659
Willy Tarreau9903f0e2015-04-04 18:50:31 +02003660 listener->accept = session_accept_fd;
Willy Tarreauc1a21672009-08-16 22:37:44 +02003661 listener->analysers |= curproxy->fe_req_ana;
Willy Tarreau10b688f2015-03-13 16:43:12 +01003662 listener->default_target = curproxy->default_target;
Willy Tarreau3bc13772008-12-07 11:50:35 +01003663
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02003664 if (!LIST_ISEMPTY(&curproxy->tcp_req.l4_rules))
Willy Tarreau7d9736f2016-10-21 16:34:21 +02003665 listener->options |= LI_O_TCP_L4_RULES;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02003666
Willy Tarreau620408f2016-10-21 16:37:51 +02003667 if (!LIST_ISEMPTY(&curproxy->tcp_req.l5_rules))
3668 listener->options |= LI_O_TCP_L5_RULES;
3669
Willy Tarreau9ea05a72009-06-14 12:07:01 +02003670 /* smart accept mode is automatic in HTTP mode */
3671 if ((curproxy->options2 & PR_O2_SMARTACC) ||
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003672 ((curproxy->mode == PR_MODE_HTTP || listener->bind_conf->is_ssl) &&
Willy Tarreau9ea05a72009-06-14 12:07:01 +02003673 !(curproxy->no_options2 & PR_O2_SMARTACC)))
3674 listener->options |= LI_O_NOQUICKACK;
Willy Tarreaue6b98942007-10-29 01:09:36 +01003675 }
3676
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003677 /* Release unused SSL configs */
3678 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01003679 if (!bind_conf->is_ssl && bind_conf->xprt->destroy_bind_conf)
3680 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003681 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02003682
Willy Tarreaua38a7172019-02-02 17:11:28 +01003683 if (atleast2(curproxy->bind_proc & all_proc_mask)) {
Willy Tarreau102df612014-05-07 23:56:38 +02003684 if (curproxy->uri_auth) {
Willy Tarreaueb791e02014-09-16 15:11:04 +02003685 int count, maxproc = 0;
3686
3687 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
Willy Tarreaue26993c2020-09-03 07:18:55 +02003688 count = my_popcountl(bind_conf->settings.bind_proc);
Willy Tarreaueb791e02014-09-16 15:11:04 +02003689 if (count > maxproc)
3690 maxproc = count;
3691 }
3692 /* backends have 0, frontends have 1 or more */
3693 if (maxproc != 1)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003694 ha_warning("Proxy '%s': in multi-process mode, stats will be"
3695 " limited to process assigned to the current request.\n",
3696 curproxy->id);
Willy Tarreaueb791e02014-09-16 15:11:04 +02003697
Willy Tarreau102df612014-05-07 23:56:38 +02003698 if (!LIST_ISEMPTY(&curproxy->uri_auth->admin_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003699 ha_warning("Proxy '%s': stats admin will not work correctly in multi-process mode.\n",
3700 curproxy->id);
Cyril Bonté02ff8ef2010-12-14 22:48:49 +01003701 }
Cyril Bonté02ff8ef2010-12-14 22:48:49 +01003702 }
Willy Tarreau102df612014-05-07 23:56:38 +02003703 if (!LIST_ISEMPTY(&curproxy->sticking_rules)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003704 ha_warning("Proxy '%s': sticking rules will not work correctly in multi-process mode.\n",
3705 curproxy->id);
Cyril Bonté02ff8ef2010-12-14 22:48:49 +01003706 }
3707 }
Willy Tarreau918ff602011-07-25 16:33:49 +02003708
3709 /* create the task associated with the proxy */
Emeric Brunc60def82017-09-27 14:59:38 +02003710 curproxy->task = task_new(MAX_THREADS_MASK);
Willy Tarreau918ff602011-07-25 16:33:49 +02003711 if (curproxy->task) {
3712 curproxy->task->context = curproxy;
3713 curproxy->task->process = manage_proxy;
Willy Tarreau918ff602011-07-25 16:33:49 +02003714 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003715 ha_alert("Proxy '%s': no more memory when trying to allocate the management task\n",
3716 curproxy->id);
Willy Tarreau918ff602011-07-25 16:33:49 +02003717 cfgerr++;
3718 }
Willy Tarreaub369a042014-09-16 13:21:03 +02003719 }
3720
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003721 /*
3722 * Recount currently required checks.
3723 */
3724
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003725 for (curproxy=proxies_list; curproxy; curproxy=curproxy->next) {
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003726 int optnum;
3727
Willy Tarreau66aa61f2009-01-18 21:44:07 +01003728 for (optnum = 0; cfg_opts[optnum].name; optnum++)
3729 if (curproxy->options & cfg_opts[optnum].val)
3730 global.last_checks |= cfg_opts[optnum].checks;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003731
Willy Tarreau66aa61f2009-01-18 21:44:07 +01003732 for (optnum = 0; cfg_opts2[optnum].name; optnum++)
3733 if (curproxy->options2 & cfg_opts2[optnum].val)
3734 global.last_checks |= cfg_opts2[optnum].checks;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003735 }
3736
Willy Tarreau0fca4832015-05-01 19:12:05 +02003737 /* compute the required process bindings for the peers */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003738 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003739 if (curproxy->table && curproxy->table->peers.p)
3740 curproxy->table->peers.p->peers_fe->bind_proc |= curproxy->bind_proc;
Willy Tarreau0fca4832015-05-01 19:12:05 +02003741
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01003742 /* compute the required process bindings for the peers from <stktables_list>
3743 * for all the stick-tables, the ones coming with "peers" sections included.
3744 */
3745 for (t = stktables_list; t; t = t->next) {
3746 struct proxy *p;
3747
3748 for (p = t->proxies_list; p; p = p->next_stkt_ref) {
3749 if (t->peers.p && t->peers.p->peers_fe) {
3750 t->peers.p->peers_fe->bind_proc |= p->bind_proc;
3751 }
3752 }
3753 }
3754
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003755 if (cfg_peers) {
3756 struct peers *curpeers = cfg_peers, **last;
Willy Tarreau122541c2011-09-07 21:24:49 +02003757 struct peer *p, *pb;
3758
William Lallemanda2cfd7e2020-03-24 16:02:48 +01003759 /* In the case the peers frontend was not initialized by a
3760 stick-table used in the configuration, set its bind_proc
3761 by default to the first process. */
3762 while (curpeers) {
William Lallemand3ef2d562020-03-24 16:42:15 +01003763 if (curpeers->peers_fe) {
3764 if (curpeers->peers_fe->bind_proc == 0)
3765 curpeers->peers_fe->bind_proc = 1;
3766 }
William Lallemanda2cfd7e2020-03-24 16:02:48 +01003767 curpeers = curpeers->next;
3768 }
3769
3770 curpeers = cfg_peers;
Willy Tarreau1e273012015-05-01 19:15:17 +02003771 /* Remove all peers sections which don't have a valid listener,
3772 * which are not used by any table, or which are bound to more
3773 * than one process.
Willy Tarreau122541c2011-09-07 21:24:49 +02003774 */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003775 last = &cfg_peers;
Willy Tarreau122541c2011-09-07 21:24:49 +02003776 while (*last) {
Frédéric Lécaille87eacbb2020-03-24 20:08:30 +01003777 struct stktable *t;
Willy Tarreau122541c2011-09-07 21:24:49 +02003778 curpeers = *last;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003779
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003780 if (curpeers->disabled) {
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003781 /* the "disabled" keyword was present */
3782 if (curpeers->peers_fe)
3783 stop_proxy(curpeers->peers_fe);
3784 curpeers->peers_fe = NULL;
3785 }
Frédéric Lécaille5a4fe5a2019-10-04 08:30:04 +02003786 else if (!curpeers->peers_fe || !curpeers->peers_fe->id) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003787 ha_warning("Removing incomplete section 'peers %s' (no peer named '%s').\n",
3788 curpeers->id, localpeer);
Frédéric Lécaille87eacbb2020-03-24 20:08:30 +01003789 if (curpeers->peers_fe)
3790 stop_proxy(curpeers->peers_fe);
3791 curpeers->peers_fe = NULL;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003792 }
Willy Tarreau9504dd62018-10-15 09:37:03 +02003793 else if (atleast2(curpeers->peers_fe->bind_proc)) {
Willy Tarreau1e273012015-05-01 19:15:17 +02003794 /* either it's totally stopped or too much used */
3795 if (curpeers->peers_fe->bind_proc) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003796 ha_alert("Peers section '%s': peers referenced by sections "
3797 "running in different processes (%d different ones). "
3798 "Check global.nbproc and all tables' bind-process "
3799 "settings.\n", curpeers->id, my_popcountl(curpeers->peers_fe->bind_proc));
Willy Tarreau1e273012015-05-01 19:15:17 +02003800 cfgerr++;
3801 }
3802 stop_proxy(curpeers->peers_fe);
3803 curpeers->peers_fe = NULL;
3804 }
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003805 else {
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +01003806 /* Initializes the transport layer of the server part of all the peers belonging to
3807 * <curpeers> section if required.
3808 * Note that ->srv is used by the local peer of a new process to connect to the local peer
3809 * of an old process.
3810 */
Frédéric Lécaille1055e682018-04-26 14:35:21 +02003811 p = curpeers->remote;
3812 while (p) {
Frédéric Lécaille355b2032019-01-11 14:06:12 +01003813 if (p->srv) {
Christopher Fauletf61f33a2020-03-27 18:55:49 +01003814 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 +01003815 cfgerr += xprt_get(XPRT_SSL)->prepare_srv(p->srv);
3816 }
Frédéric Lécaille1055e682018-04-26 14:35:21 +02003817 p = p->next;
3818 }
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +01003819 /* Configure the SSL bindings of the local peer if required. */
3820 if (!LIST_ISEMPTY(&curpeers->peers_fe->conf.bind)) {
3821 struct list *l;
3822 struct bind_conf *bind_conf;
3823
3824 l = &curpeers->peers_fe->conf.bind;
3825 bind_conf = LIST_ELEM(l->n, typeof(bind_conf), by_fe);
3826 if (bind_conf->xprt->prepare_bind_conf &&
3827 bind_conf->xprt->prepare_bind_conf(bind_conf) < 0)
3828 cfgerr++;
3829 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003830 if (!peers_init_sync(curpeers) || !peers_alloc_dcache(curpeers)) {
Willy Tarreaud9443442018-10-15 11:18:03 +02003831 ha_alert("Peers section '%s': out of memory, giving up on peers.\n",
3832 curpeers->id);
3833 cfgerr++;
3834 break;
3835 }
Willy Tarreau122541c2011-09-07 21:24:49 +02003836 last = &curpeers->next;
3837 continue;
3838 }
3839
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003840 /* clean what has been detected above */
Willy Tarreau122541c2011-09-07 21:24:49 +02003841 p = curpeers->remote;
3842 while (p) {
3843 pb = p->next;
3844 free(p->id);
3845 free(p);
3846 p = pb;
3847 }
3848
3849 /* Destroy and unlink this curpeers section.
3850 * Note: curpeers is backed up into *last.
3851 */
3852 free(curpeers->id);
3853 curpeers = curpeers->next;
Frédéric Lécaille87eacbb2020-03-24 20:08:30 +01003854 /* Reset any refereance to this peers section in the list of stick-tables */
3855 for (t = stktables_list; t; t = t->next) {
3856 if (t->peers.p && t->peers.p == *last)
3857 t->peers.p = NULL;
3858 }
Willy Tarreau122541c2011-09-07 21:24:49 +02003859 free(*last);
3860 *last = curpeers;
3861 }
3862 }
3863
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003864 for (t = stktables_list; t; t = t->next) {
3865 if (t->proxy)
3866 continue;
3867 if (!stktable_init(t)) {
3868 ha_alert("Proxy '%s': failed to initialize stick-table.\n", t->id);
3869 cfgerr++;
3870 }
3871 }
3872
Willy Tarreau6866f3f2015-05-01 19:09:08 +02003873 /* initialize stick-tables on backend capable proxies. This must not
3874 * be done earlier because the data size may be discovered while parsing
3875 * other proxies.
3876 */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003877 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02003878 if (curproxy->disabled || !curproxy->table)
Willy Tarreau6866f3f2015-05-01 19:09:08 +02003879 continue;
3880
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003881 if (!stktable_init(curproxy->table)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003882 ha_alert("Proxy '%s': failed to initialize stick-table.\n", curproxy->id);
Willy Tarreau6866f3f2015-05-01 19:09:08 +02003883 cfgerr++;
3884 }
3885 }
3886
Simon Horman0d16a402015-01-30 11:22:58 +09003887 if (mailers) {
3888 struct mailers *curmailers = mailers, **last;
3889 struct mailer *m, *mb;
3890
3891 /* Remove all mailers sections which don't have a valid listener.
3892 * This can happen when a mailers section is never referenced.
3893 */
3894 last = &mailers;
3895 while (*last) {
3896 curmailers = *last;
3897 if (curmailers->users) {
3898 last = &curmailers->next;
3899 continue;
3900 }
3901
Christopher Faulet767a84b2017-11-24 16:50:31 +01003902 ha_warning("Removing incomplete section 'mailers %s'.\n",
3903 curmailers->id);
Simon Horman0d16a402015-01-30 11:22:58 +09003904
3905 m = curmailers->mailer_list;
3906 while (m) {
3907 mb = m->next;
3908 free(m->id);
3909 free(m);
3910 m = mb;
3911 }
3912
3913 /* Destroy and unlink this curmailers section.
3914 * Note: curmailers is backed up into *last.
3915 */
3916 free(curmailers->id);
3917 curmailers = curmailers->next;
3918 free(*last);
3919 *last = curmailers;
3920 }
3921 }
3922
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003923 /* Update server_state_file_name to backend name if backend is supposed to use
3924 * a server-state file locally defined and none has been provided */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003925 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003926 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_LOCAL &&
3927 curproxy->server_state_file_name == NULL)
3928 curproxy->server_state_file_name = strdup(curproxy->id);
3929 }
3930
Emeric Brun750fe792020-12-23 16:51:12 +01003931 list_for_each_entry(curr_resolvers, &sec_resolvers, list) {
Ben Draut054fbee2018-04-13 15:43:04 -06003932 if (LIST_ISEMPTY(&curr_resolvers->nameservers)) {
3933 ha_warning("config : resolvers '%s' [%s:%d] has no nameservers configured!\n",
3934 curr_resolvers->id, curr_resolvers->conf.file,
3935 curr_resolvers->conf.line);
3936 err_code |= ERR_WARN;
3937 }
3938 }
3939
William Lallemand48b4bb42017-10-23 14:36:34 +02003940 list_for_each_entry(postparser, &postparsers, list) {
3941 if (postparser->func)
3942 cfgerr += postparser->func();
3943 }
3944
Willy Tarreaubb925012009-07-23 13:36:36 +02003945 if (cfgerr > 0)
3946 err_code |= ERR_ALERT | ERR_FATAL;
3947 out:
3948 return err_code;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003949}
3950
Willy Tarreau5b2c3362008-07-09 19:39:06 +02003951/*
3952 * Registers the CFG keyword list <kwl> as a list of valid keywords for next
3953 * parsing sessions.
3954 */
3955void cfg_register_keywords(struct cfg_kw_list *kwl)
3956{
Willy Tarreau2b718102021-04-21 07:32:39 +02003957 LIST_APPEND(&cfg_keywords.list, &kwl->list);
Willy Tarreau5b2c3362008-07-09 19:39:06 +02003958}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003959
Willy Tarreau5b2c3362008-07-09 19:39:06 +02003960/*
3961 * Unregisters the CFG keyword list <kwl> from the list of valid keywords.
3962 */
3963void cfg_unregister_keywords(struct cfg_kw_list *kwl)
3964{
Willy Tarreau2b718102021-04-21 07:32:39 +02003965 LIST_DELETE(&kwl->list);
Willy Tarreau5b2c3362008-07-09 19:39:06 +02003966 LIST_INIT(&kwl->list);
3967}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003968
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01003969/* this function register new section in the haproxy configuration file.
3970 * <section_name> is the name of this new section and <section_parser>
3971 * is the called parser. If two section declaration have the same name,
3972 * only the first declared is used.
3973 */
3974int cfg_register_section(char *section_name,
William Lallemandd2ff56d2017-10-16 11:06:50 +02003975 int (*section_parser)(const char *, int, char **, int),
3976 int (*post_section_parser)())
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01003977{
3978 struct cfg_section *cs;
3979
Willy Tarreau5e4261b2016-05-17 16:16:09 +02003980 list_for_each_entry(cs, &sections, list) {
3981 if (strcmp(cs->section_name, section_name) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003982 ha_alert("register section '%s': already registered.\n", section_name);
Willy Tarreau5e4261b2016-05-17 16:16:09 +02003983 return 0;
3984 }
3985 }
3986
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01003987 cs = calloc(1, sizeof(*cs));
3988 if (!cs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003989 ha_alert("register section '%s': out of memory.\n", section_name);
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01003990 return 0;
3991 }
3992
3993 cs->section_name = section_name;
3994 cs->section_parser = section_parser;
William Lallemandd2ff56d2017-10-16 11:06:50 +02003995 cs->post_section_parser = post_section_parser;
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01003996
Willy Tarreau2b718102021-04-21 07:32:39 +02003997 LIST_APPEND(&sections, &cs->list);
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01003998
3999 return 1;
4000}
4001
William Lallemand48b4bb42017-10-23 14:36:34 +02004002/* this function register a new function which will be called once the haproxy
4003 * configuration file has been parsed. It's useful to check dependencies
4004 * between sections or to resolve items once everything is parsed.
4005 */
4006int cfg_register_postparser(char *name, int (*func)())
4007{
4008 struct cfg_postparser *cp;
4009
4010 cp = calloc(1, sizeof(*cp));
4011 if (!cp) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004012 ha_alert("register postparser '%s': out of memory.\n", name);
William Lallemand48b4bb42017-10-23 14:36:34 +02004013 return 0;
4014 }
4015 cp->name = name;
4016 cp->func = func;
4017
Willy Tarreau2b718102021-04-21 07:32:39 +02004018 LIST_APPEND(&postparsers, &cp->list);
William Lallemand48b4bb42017-10-23 14:36:34 +02004019
4020 return 1;
4021}
4022
Willy Tarreaubaaee002006-06-26 02:48:02 +02004023/*
David Carlier845efb52015-09-25 11:49:18 +01004024 * free all config section entries
4025 */
4026void cfg_unregister_sections(void)
4027{
4028 struct cfg_section *cs, *ics;
4029
4030 list_for_each_entry_safe(cs, ics, &sections, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004031 LIST_DELETE(&cs->list);
David Carlier845efb52015-09-25 11:49:18 +01004032 free(cs);
4033 }
4034}
4035
Christopher Faulet7110b402016-10-26 11:09:44 +02004036void cfg_backup_sections(struct list *backup_sections)
4037{
4038 struct cfg_section *cs, *ics;
4039
4040 list_for_each_entry_safe(cs, ics, &sections, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004041 LIST_DELETE(&cs->list);
4042 LIST_APPEND(backup_sections, &cs->list);
Christopher Faulet7110b402016-10-26 11:09:44 +02004043 }
4044}
4045
4046void cfg_restore_sections(struct list *backup_sections)
4047{
4048 struct cfg_section *cs, *ics;
4049
4050 list_for_each_entry_safe(cs, ics, backup_sections, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004051 LIST_DELETE(&cs->list);
4052 LIST_APPEND(&sections, &cs->list);
Christopher Faulet7110b402016-10-26 11:09:44 +02004053 }
4054}
4055
Willy Tarreaue6552512018-11-26 11:33:13 +01004056/* these are the config sections handled by default */
4057REGISTER_CONFIG_SECTION("listen", cfg_parse_listen, NULL);
4058REGISTER_CONFIG_SECTION("frontend", cfg_parse_listen, NULL);
4059REGISTER_CONFIG_SECTION("backend", cfg_parse_listen, NULL);
4060REGISTER_CONFIG_SECTION("defaults", cfg_parse_listen, NULL);
4061REGISTER_CONFIG_SECTION("global", cfg_parse_global, NULL);
4062REGISTER_CONFIG_SECTION("userlist", cfg_parse_users, NULL);
4063REGISTER_CONFIG_SECTION("peers", cfg_parse_peers, NULL);
4064REGISTER_CONFIG_SECTION("mailers", cfg_parse_mailers, NULL);
4065REGISTER_CONFIG_SECTION("namespace_list", cfg_parse_netns, NULL);
Willy Tarreau659fbf02016-05-26 17:55:28 +02004066
David Carlier845efb52015-09-25 11:49:18 +01004067/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004068 * Local variables:
4069 * c-indent-level: 8
4070 * c-basic-offset: 8
4071 * End:
4072 */