blob: ac98a9567beb097e3c79a5d9d913701e2870829c [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 Tarreau2e644722022-08-07 16:55:07 +020013/* This is to have crypt() and sched_setaffinity() defined on Linux */
Cyril Bonté1a0191d2014-08-29 20:20:02 +020014#define _GNU_SOURCE
15
Willy Tarreau2e644722022-08-07 16:55:07 +020016#ifdef USE_LIBCRYPT
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 Tarreau2e644722022-08-07 16:55:07 +020032#ifdef USE_CPU_AFFINITY
33#include <sched.h>
34#endif
Willy Tarreau3f49b302007-06-11 00:29:26 +020035#include <sys/types.h>
36#include <sys/stat.h>
Willy Tarreau3f49b302007-06-11 00:29:26 +020037#include <unistd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
Willy Tarreaudcc048a2020-06-04 19:11:43 +020039#include <haproxy/acl.h>
Christopher Faulet42c6cf92021-03-25 17:19:04 +010040#include <haproxy/action.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/api.h>
Willy Tarreau732525f2021-05-06 15:49:04 +020042#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020043#include <haproxy/auth.h>
Willy Tarreau49801602020-06-04 22:50:02 +020044#include <haproxy/backend.h>
Willy Tarreau278161c2020-06-04 11:18:28 +020045#include <haproxy/capture.h>
Willy Tarreau66243b42021-07-16 15:39:28 +020046#include <haproxy/cfgcond.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020047#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020048#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020049#include <haproxy/check.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020050#include <haproxy/chunk.h>
Willy Tarreau55542642021-10-08 09:33:24 +020051#include <haproxy/clock.h>
Amaury Denoyellea6f9c5d2021-04-23 16:58:08 +020052#ifdef USE_CPU_AFFINITY
Amaury Denoyellec90932b2021-04-14 16:16:03 +020053#include <haproxy/cpuset.h>
Amaury Denoyellea6f9c5d2021-04-23 16:58:08 +020054#endif
Willy Tarreau7ea393d2020-06-04 18:02:10 +020055#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020056#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020057#include <haproxy/filters.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020058#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020059#include <haproxy/global.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020060#include <haproxy/http_ana.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020061#include <haproxy/http_rules.h>
Willy Tarreaufbe8da32020-06-04 14:34:27 +020062#include <haproxy/lb_chash.h>
Willy Tarreaub5fc3bf2020-06-04 14:37:38 +020063#include <haproxy/lb_fas.h>
Willy Tarreau02549412020-06-04 14:41:04 +020064#include <haproxy/lb_fwlc.h>
Willy Tarreau546ba422020-06-04 14:45:03 +020065#include <haproxy/lb_fwrr.h>
Willy Tarreau28671592020-06-04 20:22:59 +020066#include <haproxy/lb_map.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020067#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020068#include <haproxy/log.h>
Emeric Brund6e581d2022-09-13 16:16:30 +020069#include <haproxy/sink.h>
Willy Tarreaucee013e2020-06-05 11:40:38 +020070#include <haproxy/mailers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020071#include <haproxy/namespace.h>
Amaury Denoyelleb59b8892022-01-25 17:48:47 +010072#include <haproxy/quic_sock.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020073#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020074#include <haproxy/peers-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020075#include <haproxy/peers.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020076#include <haproxy/pool.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020077#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020078#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010079#include <haproxy/resolvers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020080#include <haproxy/sample.h>
81#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020082#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020083#include <haproxy/stats-t.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020084#include <haproxy/stick_table.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020085#include <haproxy/stream.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020086#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020087#include <haproxy/tcp_rules.h>
Amaury Denoyellef2c27a52021-07-23 15:46:46 +020088#include <haproxy/tcpcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020089#include <haproxy/thread.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020090#include <haproxy/tools.h>
91#include <haproxy/uri_auth-t.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020092
93
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +010094/* Used to chain configuration sections definitions. This list
95 * stores struct cfg_section
96 */
97struct list sections = LIST_HEAD_INIT(sections);
98
William Lallemand48b4bb42017-10-23 14:36:34 +020099struct list postparsers = LIST_HEAD_INIT(postparsers);
100
Eric Salama1b8dacc2021-03-16 15:12:17 +0100101extern struct proxy *mworker_proxy;
102
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100103char *cursection = NULL;
Willy Tarreauc8d5b952019-02-27 17:25:52 +0100104int cfg_maxpconn = 0; /* # of simultaneous connections per proxy (-N) */
Willy Tarreau5af24ef2009-03-15 15:23:16 +0100105int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */
Christopher Faulet79bdef32016-11-04 22:36:15 +0100106char *cfg_scope = NULL; /* the current scope during the configuration parsing */
Christopher Faulet037e3f82022-11-18 15:46:06 +0100107int non_global_section_parsed = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108
Willy Tarreau8a022d52021-04-27 20:29:11 +0200109/* how to handle default paths */
110static enum default_path_mode {
111 DEFAULT_PATH_CURRENT = 0, /* "current": paths are relative to CWD (this is the default) */
112 DEFAULT_PATH_CONFIG, /* "config": paths are relative to config file */
113 DEFAULT_PATH_PARENT, /* "parent": paths are relative to config file's ".." */
114 DEFAULT_PATH_ORIGIN, /* "origin": paths are relative to default_path_origin */
115} default_path_mode;
116
117static char initial_cwd[PATH_MAX];
118static char current_cwd[PATH_MAX];
119
Willy Tarreau5b2c3362008-07-09 19:39:06 +0200120/* List head of all known configuration keywords */
Willy Tarreau36b9e222018-11-11 15:19:52 +0100121struct cfg_kw_list cfg_keywords = {
Willy Tarreau5b2c3362008-07-09 19:39:06 +0200122 .list = LIST_HEAD_INIT(cfg_keywords.list)
123};
124
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125/*
126 * converts <str> to a list of listeners which are dynamically allocated.
127 * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
128 * - <addr> can be empty or "*" to indicate INADDR_ANY ;
129 * - <port> is a numerical port from 1 to 65535 ;
130 * - <end> indicates to use the range from <port> to <end> instead (inclusive).
131 * This can be repeated as many times as necessary, separated by a coma.
Willy Tarreau4fbb2282012-09-20 20:01:39 +0200132 * Function returns 1 for success or 0 if error. In case of errors, if <err> is
133 * not NULL, it must be a valid pointer to either NULL or a freeable area that
134 * will be replaced with an error message.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200135 */
Willy Tarreau4fbb2282012-09-20 20:01:39 +0200136int 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 +0200137{
Willy Tarreau9b3178d2020-09-16 17:58:55 +0200138 struct protocol *proto;
Willy Tarreau2dff0c22011-03-04 15:43:13 +0100139 char *next, *dupstr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200140 int port, end;
141
142 next = dupstr = strdup(str);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200143
Willy Tarreaubaaee002006-06-26 02:48:02 +0200144 while (next && *next) {
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200145 struct sockaddr_storage *ss2;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100146 int fd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200147
148 str = next;
149 /* 1) look for the end of the first address */
Krzysztof Piotr Oledzki52d522b2009-01-27 16:57:08 +0100150 if ((next = strchr(str, ',')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151 *next++ = 0;
152 }
153
Willy Tarreau5fc93282020-09-16 18:25:03 +0200154 ss2 = str2sa_range(str, NULL, &port, &end, &fd, &proto, err,
Eric Salama1b8dacc2021-03-16 15:12:17 +0100155 (curproxy == global.cli_fe || curproxy == mworker_proxy) ? NULL : global.unix_bind.prefix,
Willy Tarreau32819932020-09-04 15:53:16 +0200156 NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
Willy Tarreau5e1779a2020-09-16 16:28:08 +0200157 PA_O_SOCKET_FD | PA_O_STREAM | PA_O_XPRT);
Willy Tarreau12eb2a62013-03-06 15:45:03 +0100158 if (!ss2)
159 goto fail;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200160
Willy Tarreau12eb2a62013-03-06 15:45:03 +0100161 /* OK the address looks correct */
Willy Tarreau91b780a2022-05-20 16:15:01 +0200162 if (proto->proto_type == PROTO_TYPE_DGRAM)
163 bind_conf->options |= BC_O_USE_SOCK_DGRAM;
164 else
165 bind_conf->options |= BC_O_USE_SOCK_STREAM;
166
167 if (proto->xprt_type == PROTO_TYPE_DGRAM)
168 bind_conf->options |= BC_O_USE_XPRT_DGRAM;
169 else
170 bind_conf->options |= BC_O_USE_XPRT_STREAM;
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100171
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 Tarreau08138612021-05-08 19:58:37 +0200235 * Sends a warning if proxy <proxy> does not have at least one of the
236 * capabilities in <cap>. An optional <hint> may be added at the end
237 * of the warning to help the user. Returns 1 if a warning was emitted
238 * or 0 if the condition is valid.
239 */
240int warnifnotcap(struct proxy *proxy, int cap, const char *file, int line, const char *arg, const char *hint)
241{
242 char *msg;
243
244 switch (cap) {
245 case PR_CAP_BE: msg = "no backend"; break;
246 case PR_CAP_FE: msg = "no frontend"; break;
247 case PR_CAP_BE|PR_CAP_FE: msg = "neither frontend nor backend"; break;
248 default: msg = "not enough"; break;
249 }
250
251 if (!(proxy->cap & cap)) {
252 ha_warning("parsing [%s:%d] : '%s' ignored because %s '%s' has %s capability.%s\n",
253 file, line, arg, proxy_type_str(proxy), proxy->id, msg, hint ? hint : "");
254 return 1;
255 }
256 return 0;
257}
258
259/*
260 * Sends an alert if proxy <proxy> does not have at least one of the
261 * capabilities in <cap>. An optional <hint> may be added at the end
262 * of the alert to help the user. Returns 1 if an alert was emitted
263 * or 0 if the condition is valid.
264 */
265int failifnotcap(struct proxy *proxy, int cap, const char *file, int line, const char *arg, const char *hint)
266{
267 char *msg;
268
269 switch (cap) {
270 case PR_CAP_BE: msg = "no backend"; break;
271 case PR_CAP_FE: msg = "no frontend"; break;
272 case PR_CAP_BE|PR_CAP_FE: msg = "neither frontend nor backend"; break;
273 default: msg = "not enough"; break;
274 }
275
276 if (!(proxy->cap & cap)) {
277 ha_alert("parsing [%s:%d] : '%s' not allowed because %s '%s' has %s capability.%s\n",
278 file, line, arg, proxy_type_str(proxy), proxy->id, msg, hint ? hint : "");
279 return 1;
280 }
281 return 0;
282}
283
284/*
Willy Tarreauece9b072016-12-21 22:41:44 +0100285 * Report an error in <msg> when there are too many arguments. This version is
286 * intended to be used by keyword parsers so that the message will be included
287 * into the general error message. The index is the current keyword in args.
288 * Return 0 if the number of argument is correct, otherwise build a message and
289 * return 1. Fill err_code with an ERR_ALERT and an ERR_FATAL if not null. The
290 * message may also be null, it will simply not be produced (useful to check only).
291 * <msg> and <err_code> are only affected on error.
292 */
293int too_many_args_idx(int maxarg, int index, char **args, char **msg, int *err_code)
294{
295 int i;
296
297 if (!*args[index + maxarg + 1])
298 return 0;
299
300 if (msg) {
301 *msg = NULL;
302 memprintf(msg, "%s", args[0]);
303 for (i = 1; i <= index; i++)
304 memprintf(msg, "%s %s", *msg, args[i]);
305
306 memprintf(msg, "'%s' cannot handle unexpected argument '%s'.", *msg, args[index + maxarg + 1]);
307 }
308 if (err_code)
309 *err_code |= ERR_ALERT | ERR_FATAL;
310
311 return 1;
312}
313
314/*
315 * same as too_many_args_idx with a 0 index
316 */
317int too_many_args(int maxarg, char **args, char **msg, int *err_code)
318{
319 return too_many_args_idx(maxarg, 0, args, msg, err_code);
320}
321
322/*
William Lallemand6e62fb62015-04-28 16:55:23 +0200323 * Report a fatal Alert when there is too much arguments
324 * The index is the current keyword in args
325 * Return 0 if the number of argument is correct, otherwise emit an alert and return 1
326 * Fill err_code with an ERR_ALERT and an ERR_FATAL
327 */
328int alertif_too_many_args_idx(int maxarg, int index, const char *file, int linenum, char **args, int *err_code)
329{
330 char *kw = NULL;
331 int i;
332
333 if (!*args[index + maxarg + 1])
334 return 0;
335
336 memprintf(&kw, "%s", args[0]);
337 for (i = 1; i <= index; i++) {
338 memprintf(&kw, "%s %s", kw, args[i]);
339 }
340
Christopher Faulet767a84b2017-11-24 16:50:31 +0100341 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 +0200342 free(kw);
343 *err_code |= ERR_ALERT | ERR_FATAL;
344 return 1;
345}
346
347/*
348 * same as alertif_too_many_args_idx with a 0 index
349 */
350int alertif_too_many_args(int maxarg, const char *file, int linenum, char **args, int *err_code)
351{
352 return alertif_too_many_args_idx(maxarg, 0, file, linenum, args, err_code);
353}
354
Willy Tarreau61d18892009-03-31 10:49:21 +0200355
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100356/* Report it if a request ACL condition uses some keywords that are incompatible
357 * with the place where the ACL is used. It returns either 0 or ERR_WARN so that
358 * its result can be or'ed with err_code. Note that <cond> may be NULL and then
359 * will be ignored.
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100360 */
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100361int warnif_cond_conflicts(const struct acl_cond *cond, unsigned int where, const char *file, int line)
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100362{
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100363 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +0200364 const char *kw;
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100365
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100366 if (!cond)
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100367 return 0;
368
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100369 acl = acl_cond_conflicts(cond, where);
370 if (acl) {
371 if (acl->name && *acl->name)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100372 ha_warning("parsing [%s:%d] : acl '%s' will never match because it only involves keywords that are incompatible with '%s'\n",
373 file, line, acl->name, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100374 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100375 ha_warning("parsing [%s:%d] : anonymous acl will never match because it uses keyword '%s' which is incompatible with '%s'\n",
376 file, line, LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100377 return ERR_WARN;
378 }
379 if (!acl_cond_kw_conflicts(cond, where, &acl, &kw))
Willy Tarreaufdb563c2010-01-31 15:43:27 +0100380 return 0;
381
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100382 if (acl->name && *acl->name)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100383 ha_warning("parsing [%s:%d] : acl '%s' involves keywords '%s' which is incompatible with '%s'\n",
384 file, line, acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100385 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100386 ha_warning("parsing [%s:%d] : anonymous acl involves keyword '%s' which is incompatible with '%s'\n",
387 file, line, kw, sample_ckp_names(where));
Willy Tarreaufdb563c2010-01-31 15:43:27 +0100388 return ERR_WARN;
389}
390
Christopher Faulet581db2b2021-03-26 10:02:46 +0100391/* Report it if an ACL uses a L6 sample fetch from an HTTP proxy. It returns
392 * either 0 or ERR_WARN so that its result can be or'ed with err_code. Note that
393 * <cond> may be NULL and then will be ignored.
394*/
395int warnif_tcp_http_cond(const struct proxy *px, const struct acl_cond *cond)
396{
397 if (!cond || px->mode != PR_MODE_HTTP)
398 return 0;
399
400 if (cond->use & (SMP_USE_L6REQ|SMP_USE_L6RES)) {
401 ha_warning("Proxy '%s': L6 sample fetches ignored on HTTP proxies (declared at %s:%d).\n",
402 px->id, cond->file, cond->line);
403 return ERR_WARN;
404 }
405 return 0;
406}
407
Willy Tarreaue2afcc42021-03-12 09:08:04 +0100408/* try to find in <list> the word that looks closest to <word> by counting
409 * transitions between letters, digits and other characters. Will return the
410 * best matching word if found, otherwise NULL. An optional array of extra
411 * words to compare may be passed in <extra>, but it must then be terminated
412 * by a NULL entry. If unused it may be NULL.
413 */
414const char *cfg_find_best_match(const char *word, const struct list *list, int section, const char **extra)
415{
416 uint8_t word_sig[1024]; // 0..25=letter, 26=digit, 27=other, 28=begin, 29=end
417 uint8_t list_sig[1024];
418 const struct cfg_kw_list *kwl;
419 int index;
420 const char *best_ptr = NULL;
421 int dist, best_dist = INT_MAX;
422
423 make_word_fingerprint(word_sig, word);
424 list_for_each_entry(kwl, list, list) {
425 for (index = 0; kwl->kw[index].kw != NULL; index++) {
426 if (kwl->kw[index].section != section)
427 continue;
428
429 make_word_fingerprint(list_sig, kwl->kw[index].kw);
430 dist = word_fingerprint_distance(word_sig, list_sig);
431 if (dist < best_dist) {
432 best_dist = dist;
433 best_ptr = kwl->kw[index].kw;
434 }
435 }
436 }
437
438 while (extra && *extra) {
439 make_word_fingerprint(list_sig, *extra);
440 dist = word_fingerprint_distance(word_sig, list_sig);
441 if (dist < best_dist) {
442 best_dist = dist;
443 best_ptr = *extra;
444 }
445 extra++;
446 }
447
448 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
449 best_ptr = NULL;
450 return best_ptr;
451}
452
Christopher Faulet62519022017-10-16 15:49:32 +0200453/* Parse a string representing a process number or a set of processes. It must
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100454 * be "all", "odd", "even", a number between 1 and <max> or a range with
Christopher Faulet5ab51772017-11-22 11:21:58 +0100455 * two such numbers delimited by a dash ('-'). On success, it returns
456 * 0. otherwise it returns 1 with an error message in <err>.
Christopher Faulet62519022017-10-16 15:49:32 +0200457 *
458 * Note: this function can also be used to parse a thread number or a set of
459 * threads.
460 */
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100461int parse_process_number(const char *arg, unsigned long *proc, int max, int *autoinc, char **err)
Christopher Faulet62519022017-10-16 15:49:32 +0200462{
Christopher Faulet26028f62017-11-22 15:01:51 +0100463 if (autoinc) {
464 *autoinc = 0;
465 if (strncmp(arg, "auto:", 5) == 0) {
466 arg += 5;
467 *autoinc = 1;
468 }
469 }
470
Christopher Faulet62519022017-10-16 15:49:32 +0200471 if (strcmp(arg, "all") == 0)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100472 *proc |= ~0UL;
Christopher Faulet62519022017-10-16 15:49:32 +0200473 else if (strcmp(arg, "odd") == 0)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100474 *proc |= ~0UL/3UL; /* 0x555....555 */
Christopher Faulet62519022017-10-16 15:49:32 +0200475 else if (strcmp(arg, "even") == 0)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100476 *proc |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Christopher Faulet62519022017-10-16 15:49:32 +0200477 else {
Christopher Faulet18cca782019-02-07 16:29:41 +0100478 const char *p, *dash = NULL;
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100479 unsigned int low, high;
480
Christopher Faulet18cca782019-02-07 16:29:41 +0100481 for (p = arg; *p; p++) {
482 if (*p == '-' && !dash)
483 dash = p;
Willy Tarreau90807112020-02-25 08:16:33 +0100484 else if (!isdigit((unsigned char)*p)) {
Christopher Faulet18cca782019-02-07 16:29:41 +0100485 memprintf(err, "'%s' is not a valid number/range.", arg);
486 return -1;
487 }
Christopher Faulet5ab51772017-11-22 11:21:58 +0100488 }
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100489
490 low = high = str2uic(arg);
Christopher Faulet18cca782019-02-07 16:29:41 +0100491 if (dash)
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100492 high = ((!*(dash+1)) ? max : str2uic(dash + 1));
Christopher Fauletff4121f2017-11-22 16:38:49 +0100493
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100494 if (high < low) {
495 unsigned int swap = low;
496 low = high;
497 high = swap;
498 }
499
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100500 if (low < 1 || low > max || high > max) {
Christopher Fauletcb6a9452017-11-22 16:50:41 +0100501 memprintf(err, "'%s' is not a valid number/range."
502 " It supports numbers from 1 to %d.\n",
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100503 arg, max);
Christopher Faulet5ab51772017-11-22 11:21:58 +0100504 return 1;
505 }
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100506
507 for (;low <= high; low++)
Christopher Faulet5ab51772017-11-22 11:21:58 +0100508 *proc |= 1UL << (low-1);
Christopher Faulet62519022017-10-16 15:49:32 +0200509 }
Willy Tarreauc9a82e42019-01-26 13:25:14 +0100510 *proc &= ~0UL >> (LONGBITS - max);
Christopher Faulet1dcb9cb2017-11-22 10:24:40 +0100511
Christopher Faulet5ab51772017-11-22 11:21:58 +0100512 return 0;
Christopher Faulet62519022017-10-16 15:49:32 +0200513}
514
David Carlier7e351ee2017-12-01 09:14:02 +0000515#ifdef USE_CPU_AFFINITY
Christopher Faulet62519022017-10-16 15:49:32 +0200516/* Parse cpu sets. Each CPU set is either a unique number between 0 and
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200517 * ha_cpuset_size() - 1 or a range with two such numbers delimited by a dash
Amaury Denoyellea8082352021-04-06 16:46:15 +0200518 * ('-'). If <comma_allowed> is set, each CPU set can be a list of unique
519 * numbers or ranges separated by a comma. It is also possible to specify
520 * multiple cpu numbers or ranges in distinct argument in <args>. On success,
521 * it returns 0, otherwise it returns 1 with an error message in <err>.
Christopher Faulet62519022017-10-16 15:49:32 +0200522 */
Amaury Denoyellea8082352021-04-06 16:46:15 +0200523unsigned long parse_cpu_set(const char **args, struct hap_cpuset *cpu_set,
524 int comma_allowed, char **err)
Christopher Faulet62519022017-10-16 15:49:32 +0200525{
526 int cur_arg = 0;
Amaury Denoyellea8082352021-04-06 16:46:15 +0200527 const char *arg;
Christopher Faulet62519022017-10-16 15:49:32 +0200528
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200529 ha_cpuset_zero(cpu_set);
530
Amaury Denoyellea8082352021-04-06 16:46:15 +0200531 arg = args[cur_arg];
532 while (*arg) {
533 const char *dash, *comma;
Christopher Faulet62519022017-10-16 15:49:32 +0200534 unsigned int low, high;
535
Willy Tarreau90807112020-02-25 08:16:33 +0100536 if (!isdigit((unsigned char)*args[cur_arg])) {
Amaury Denoyellea8082352021-04-06 16:46:15 +0200537 memprintf(err, "'%s' is not a CPU range.", arg);
Christopher Faulet62519022017-10-16 15:49:32 +0200538 return -1;
539 }
540
Amaury Denoyellea8082352021-04-06 16:46:15 +0200541 low = high = str2uic(arg);
542
543 comma = comma_allowed ? strchr(arg, ',') : NULL;
544 dash = strchr(arg, '-');
545
546 if (dash && (!comma || dash < comma))
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200547 high = *(dash+1) ? str2uic(dash + 1) : ha_cpuset_size() - 1;
Christopher Faulet62519022017-10-16 15:49:32 +0200548
549 if (high < low) {
550 unsigned int swap = low;
551 low = high;
552 high = swap;
553 }
554
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200555 if (high >= ha_cpuset_size()) {
556 memprintf(err, "supports CPU numbers from 0 to %d.",
557 ha_cpuset_size() - 1);
Christopher Faulet62519022017-10-16 15:49:32 +0200558 return 1;
559 }
560
561 while (low <= high)
Amaury Denoyellec90932b2021-04-14 16:16:03 +0200562 ha_cpuset_set(cpu_set, low++);
Christopher Faulet62519022017-10-16 15:49:32 +0200563
Amaury Denoyellea8082352021-04-06 16:46:15 +0200564 /* if a comma is present, parse the rest of the arg, else
565 * skip to the next arg */
566 arg = comma ? comma + 1 : args[++cur_arg];
Christopher Faulet62519022017-10-16 15:49:32 +0200567 }
568 return 0;
569}
David Carlier7e351ee2017-12-01 09:14:02 +0000570#endif
571
Frédéric Lécaille18251032019-01-11 11:07:15 +0100572/* Allocate and initialize the frontend of a "peers" section found in
Christopher Faulet244331f2022-07-25 15:10:44 +0200573 * file <file> at line <linenum> with <id> as ID.
Frédéric Lécaille18251032019-01-11 11:07:15 +0100574 * Return 0 if succeeded, -1 if not.
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200575 * Note that this function may be called from "default-server"
576 * or "peer" lines.
Frédéric Lécaille18251032019-01-11 11:07:15 +0100577 */
Christopher Faulet244331f2022-07-25 15:10:44 +0200578static int init_peers_frontend(const char *file, int linenum,
579 const char *id, struct peers *peers)
Frédéric Lécaille18251032019-01-11 11:07:15 +0100580{
581 struct proxy *p;
582
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200583 if (peers->peers_fe) {
584 p = peers->peers_fe;
585 goto out;
586 }
Frédéric Lécaille9492c4e2019-01-11 11:47:12 +0100587
Frédéric Lécaille18251032019-01-11 11:07:15 +0100588 p = calloc(1, sizeof *p);
589 if (!p) {
590 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
591 return -1;
592 }
593
594 init_new_proxy(p);
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200595 peers_setup_frontend(p);
Frédéric Lécaille18251032019-01-11 11:07:15 +0100596 p->parent = peers;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200597 /* Finally store this frontend. */
598 peers->peers_fe = p;
599
600 out:
Christopher Faulet244331f2022-07-25 15:10:44 +0200601 if (id && !p->id)
602 p->id = strdup(id);
Frédéric Lécaille1055e682018-04-26 14:35:21 +0200603 free(p->conf.file);
Frédéric Lécaille18251032019-01-11 11:07:15 +0100604 p->conf.args.file = p->conf.file = strdup(file);
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100605 if (linenum != -1)
606 p->conf.args.line = p->conf.line = linenum;
Frédéric Lécaille18251032019-01-11 11:07:15 +0100607
608 return 0;
609}
Willy Tarreauade5ec42010-01-28 19:33:49 +0100610
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100611/* Only change ->file, ->line and ->arg struct bind_conf member values
612 * if already present.
613 */
614static struct bind_conf *bind_conf_uniq_alloc(struct proxy *p,
615 const char *file, int line,
616 const char *arg, struct xprt_ops *xprt)
617{
618 struct bind_conf *bind_conf;
619
620 if (!LIST_ISEMPTY(&p->conf.bind)) {
621 bind_conf = LIST_ELEM((&p->conf.bind)->n, typeof(bind_conf), by_fe);
Emeric Brun49f6f4b2022-05-25 10:12:07 +0200622 /*
623 * We keep bind_conf->file and bind_conf->line unchanged
624 * to make them available for error messages
625 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100626 if (arg) {
627 free(bind_conf->arg);
628 bind_conf->arg = strdup(arg);
629 }
630 }
631 else {
632 bind_conf = bind_conf_alloc(p, file, line, arg, xprt);
633 }
634
635 return bind_conf;
636}
637
638/*
639 * Allocate a new struct peer parsed at line <linenum> in file <file>
640 * to be added to <peers>.
641 * Returns the new allocated structure if succeeded, NULL if not.
642 */
643static struct peer *cfg_peers_add_peer(struct peers *peers,
644 const char *file, int linenum,
645 const char *id, int local)
646{
647 struct peer *p;
648
649 p = calloc(1, sizeof *p);
650 if (!p) {
651 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
652 return NULL;
653 }
654
655 /* the peers are linked backwards first */
656 peers->count++;
Christopher Faulet387e7972022-05-12 14:47:52 +0200657 p->peers = peers;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100658 p->next = peers->remote;
659 peers->remote = p;
660 p->conf.file = strdup(file);
661 p->conf.line = linenum;
662 p->last_change = now.tv_sec;
663 p->xprt = xprt_get(XPRT_RAW);
664 p->sock_init_arg = NULL;
665 HA_SPIN_INIT(&p->lock);
666 if (id)
667 p->id = strdup(id);
668 if (local) {
669 p->local = 1;
670 peers->local = p;
671 }
672
673 return p;
674}
675
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676/*
William Lallemand51097192015-04-14 16:35:22 +0200677 * Parse a line in a <listen>, <frontend> or <backend> section.
Willy Tarreau93893792009-07-23 13:19:11 +0200678 * Returns the error code, 0 if OK, or any combination of :
679 * - ERR_ABORT: must abort ASAP
680 * - ERR_FATAL: we can continue parsing but not start the service
681 * - ERR_WARN: a warning has been emitted
682 * - ERR_ALERT: an alert has been emitted
683 * Only the two first ones can stop processing, the two others are just
684 * indicators.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200685 */
Emeric Brun32da3c42010-09-23 18:39:19 +0200686int cfg_parse_peers(const char *file, int linenum, char **args, int kwm)
687{
688 static struct peers *curpeers = NULL;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200689 static int nb_shards = 0;
Emeric Brun32da3c42010-09-23 18:39:19 +0200690 struct peer *newpeer = NULL;
691 const char *err;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200692 struct bind_conf *bind_conf;
693 struct listener *l;
Emeric Brun32da3c42010-09-23 18:39:19 +0200694 int err_code = 0;
Willy Tarreau902636f2013-03-10 19:44:48 +0100695 char *errmsg = NULL;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100696 static int bind_line, peer_line;
697
698 if (strcmp(args[0], "bind") == 0 || strcmp(args[0], "default-bind") == 0) {
699 int cur_arg;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100700 struct bind_conf *bind_conf;
Willy Tarreau55f0f7b2022-05-20 15:44:17 +0200701 int ret;
Emeric Brun32da3c42010-09-23 18:39:19 +0200702
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100703 cur_arg = 1;
704
Christopher Faulet244331f2022-07-25 15:10:44 +0200705 if (init_peers_frontend(file, linenum, NULL, curpeers) != 0) {
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200706 err_code |= ERR_ALERT | ERR_ABORT;
707 goto out;
708 }
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100709
710 bind_conf = bind_conf_uniq_alloc(curpeers->peers_fe, file, linenum,
Willy Tarreau8d158132022-07-05 15:54:09 +0200711 args[1], xprt_get(XPRT_RAW));
William Lallemand1d932172022-07-06 14:30:23 +0200712 if (!bind_conf) {
713 ha_alert("parsing [%s:%d] : '%s %s' : cannot allocate memory.\n", file, linenum, args[0], args[1]);
714 err_code |= ERR_FATAL;
715 goto out;
716 }
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100717 if (*args[0] == 'b') {
718 struct listener *l;
719
720 if (peer_line) {
721 ha_alert("parsing [%s:%d] : mixing \"peer\" and \"bind\" line is forbidden\n", file, linenum);
722 err_code |= ERR_ALERT | ERR_FATAL;
723 goto out;
724 }
725
Emeric Brun49f6f4b2022-05-25 10:12:07 +0200726 if (!LIST_ISEMPTY(&bind_conf->listeners)) {
727 ha_alert("parsing [%s:%d] : One listener per \"peers\" section is authorized but another is already configured at [%s:%d].\n", file, linenum, bind_conf->file, bind_conf->line);
728 err_code |= ERR_FATAL;
729 }
730
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100731 if (!str2listener(args[1], curpeers->peers_fe, bind_conf, file, linenum, &errmsg)) {
732 if (errmsg && *errmsg) {
733 indent_msg(&errmsg, 2);
734 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
735 }
736 else
737 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
Willy Tarreau0d044102022-05-20 15:19:48 +0200738 file, linenum, args[0], args[1], args[1]);
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100739 err_code |= ERR_FATAL;
740 goto out;
741 }
Emeric Brunca825782022-05-25 10:25:45 +0200742 /*
743 * Newly allocated listener is at the end of the list
744 */
745 l = LIST_ELEM(bind_conf->listeners.p, typeof(l), by_bind);
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100746 l->maxaccept = 1;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100747 l->accept = session_accept_fd;
748 l->analysers |= curpeers->peers_fe->fe_req_ana;
749 l->default_target = curpeers->peers_fe->default_target;
750 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
Willy Tarreau18215cb2019-02-27 16:25:28 +0100751 global.maxsock++; /* for the listening socket */
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100752
753 bind_line = 1;
754 if (cfg_peers->local) {
755 newpeer = cfg_peers->local;
756 }
757 else {
758 /* This peer is local.
759 * Note that we do not set the peer ID. This latter is initialized
760 * when parsing "peer" or "server" line.
761 */
762 newpeer = cfg_peers_add_peer(curpeers, file, linenum, NULL, 1);
763 if (!newpeer) {
764 err_code |= ERR_ALERT | ERR_ABORT;
765 goto out;
766 }
767 }
Willy Tarreau37159062020-08-27 07:48:42 +0200768 newpeer->addr = l->rx.addr;
Willy Tarreau5fc93282020-09-16 18:25:03 +0200769 newpeer->proto = l->rx.proto;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100770 cur_arg++;
771 }
772
Willy Tarreau55f0f7b2022-05-20 15:44:17 +0200773 ret = bind_parse_args_list(bind_conf, args, cur_arg, cursection, file, linenum);
774 err_code |= ret;
775 if (ret != 0)
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100776 goto out;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100777 }
778 else if (strcmp(args[0], "default-server") == 0) {
Christopher Faulet244331f2022-07-25 15:10:44 +0200779 if (init_peers_frontend(file, -1, NULL, curpeers) != 0) {
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100780 err_code |= ERR_ALERT | ERR_ABORT;
781 goto out;
782 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100783 err_code |= parse_server(file, linenum, args, curpeers->peers_fe, NULL,
784 SRV_PARSE_DEFAULT_SERVER|SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_INITIAL_RESOLVE);
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200785 }
Frédéric Lécailleb6f759b2019-11-05 09:57:45 +0100786 else if (strcmp(args[0], "log") == 0) {
Christopher Faulet244331f2022-07-25 15:10:44 +0200787 if (init_peers_frontend(file, linenum, NULL, curpeers) != 0) {
Frédéric Lécailleb6f759b2019-11-05 09:57:45 +0100788 err_code |= ERR_ALERT | ERR_ABORT;
789 goto out;
790 }
Emeric Brun9533a702021-04-02 10:13:43 +0200791 if (!parse_logsrv(args, &curpeers->peers_fe->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Frédéric Lécailleb6f759b2019-11-05 09:57:45 +0100792 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
793 err_code |= ERR_ALERT | ERR_FATAL;
794 goto out;
795 }
796 }
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200797 else if (strcmp(args[0], "peers") == 0) { /* new peers section */
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100798 /* Initialize these static variables when entering a new "peers" section*/
799 bind_line = peer_line = 0;
Willy Tarreau0dbbf312013-03-05 11:31:55 +0100800 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100801 ha_alert("parsing [%s:%d] : missing name for peers section.\n", file, linenum);
Willy Tarreau54984722014-02-16 08:20:13 +0100802 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau0dbbf312013-03-05 11:31:55 +0100803 goto out;
804 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200805
William Lallemand6e62fb62015-04-28 16:55:23 +0200806 if (alertif_too_many_args(1, file, linenum, args, &err_code))
807 goto out;
808
Emeric Brun32da3c42010-09-23 18:39:19 +0200809 err = invalid_char(args[1]);
810 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100811 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
812 file, linenum, *err, args[0], args[1]);
Willy Tarreau54984722014-02-16 08:20:13 +0100813 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau0dbbf312013-03-05 11:31:55 +0100814 goto out;
Emeric Brun32da3c42010-09-23 18:39:19 +0200815 }
816
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200817 for (curpeers = cfg_peers; curpeers != NULL; curpeers = curpeers->next) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200818 /*
819 * If there are two proxies with the same name only following
820 * combinations are allowed:
821 */
822 if (strcmp(curpeers->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100823 ha_alert("Parsing [%s:%d]: peers section '%s' has the same name as another peers section declared at %s:%d.\n",
824 file, linenum, args[1], curpeers->conf.file, curpeers->conf.line);
Willy Tarreau911fa2e2015-05-26 10:35:50 +0200825 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun32da3c42010-09-23 18:39:19 +0200826 }
827 }
828
Vincent Bernat02779b62016-04-03 13:48:43 +0200829 if ((curpeers = calloc(1, sizeof(*curpeers))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100830 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Emeric Brun32da3c42010-09-23 18:39:19 +0200831 err_code |= ERR_ALERT | ERR_ABORT;
832 goto out;
833 }
834
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200835 curpeers->next = cfg_peers;
836 cfg_peers = curpeers;
Willy Tarreau8113a5d2012-10-04 08:01:43 +0200837 curpeers->conf.file = strdup(file);
Emeric Brun32da3c42010-09-23 18:39:19 +0200838 curpeers->conf.line = linenum;
839 curpeers->last_change = now.tv_sec;
840 curpeers->id = strdup(args[1]);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +0200841 curpeers->disabled = 0;
Emeric Brun32da3c42010-09-23 18:39:19 +0200842 }
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200843 else if (strcmp(args[0], "peer") == 0 ||
844 strcmp(args[0], "server") == 0) { /* peer or server definition */
Frédéric Lécaille04636b72019-01-31 06:48:16 +0100845 int local_peer, peer;
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100846 int parse_addr = 0;
Emeric Brun32da3c42010-09-23 18:39:19 +0200847
Frédéric Lécaille04636b72019-01-31 06:48:16 +0100848 peer = *args[0] == 'p';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100849 local_peer = strcmp(args[1], localpeer) == 0;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100850 /* The local peer may have already partially been parsed on a "bind" line. */
851 if (*args[0] == 'p') {
852 if (bind_line) {
853 ha_alert("parsing [%s:%d] : mixing \"peer\" and \"bind\" line is forbidden\n", file, linenum);
854 err_code |= ERR_ALERT | ERR_FATAL;
855 goto out;
856 }
857 peer_line = 1;
858 }
859 if (cfg_peers->local && !cfg_peers->local->id && local_peer) {
860 /* The local peer has already been initialized on a "bind" line.
861 * Let's use it and store its ID.
862 */
863 newpeer = cfg_peers->local;
864 newpeer->id = strdup(localpeer);
865 }
866 else {
867 if (local_peer && cfg_peers->local) {
868 ha_alert("parsing [%s:%d] : '%s %s' : local peer name already referenced at %s:%d. %s\n",
869 file, linenum, args[0], args[1],
870 curpeers->peers_fe->conf.file, curpeers->peers_fe->conf.line, cfg_peers->local->id);
871 err_code |= ERR_FATAL;
872 goto out;
873 }
874 newpeer = cfg_peers_add_peer(curpeers, file, linenum, args[1], local_peer);
875 if (!newpeer) {
876 err_code |= ERR_ALERT | ERR_ABORT;
877 goto out;
878 }
879 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200880
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100881 /* Line number and peer ID are updated only if this peer is the local one. */
882 if (init_peers_frontend(file,
883 newpeer->local ? linenum: -1,
Christopher Faulet244331f2022-07-25 15:10:44 +0200884 newpeer->local ? newpeer->id : NULL,
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100885 curpeers) != 0) {
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +0200886 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreaub36487e2013-03-10 18:37:42 +0100887 goto out;
888 }
Willy Tarreau2aa38802013-02-20 19:20:59 +0100889
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100890 /* This initializes curpeer->peers->peers_fe->srv.
891 * The server address is parsed only if we are parsing a "peer" line,
892 * or if we are parsing a "server" line and the current peer is not the local one.
893 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100894 parse_addr = (peer || !local_peer) ? SRV_PARSE_PARSE_ADDR : 0;
895 err_code |= parse_server(file, linenum, args, curpeers->peers_fe, NULL,
896 SRV_PARSE_IN_PEER_SECTION|parse_addr|SRV_PARSE_INITIAL_RESOLVE);
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +0200897 if (!curpeers->peers_fe->srv) {
898 /* Remove the newly allocated peer. */
899 if (newpeer != curpeers->local) {
900 struct peer *p;
901
902 p = curpeers->remote;
903 curpeers->remote = curpeers->remote->next;
904 free(p->id);
905 free(p);
906 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200907 goto out;
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +0200908 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200909
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200910 if (nb_shards && curpeers->peers_fe->srv->shard > nb_shards) {
911 ha_warning("parsing [%s:%d] : '%s %s' : %d peer shard greater value than %d shards value is ignored.\n",
912 file, linenum, args[0], args[1], curpeers->peers_fe->srv->shard, nb_shards);
913 curpeers->peers_fe->srv->shard = 0;
914 err_code |= ERR_WARN;
915 }
916
Willy Tarreau824c8c52022-05-31 09:42:44 +0200917 if (curpeers->peers_fe->srv->init_addr_methods || curpeers->peers_fe->srv->resolvers_id ||
918 curpeers->peers_fe->srv->do_check || curpeers->peers_fe->srv->do_agent) {
919 ha_warning("parsing [%s:%d] : '%s %s' : init_addr, resolvers, check and agent are ignored for peers.\n", file, linenum, args[0], args[1]);
920 err_code |= ERR_WARN;
921 }
922
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100923 /* If the peer address has just been parsed, let's copy it to <newpeer>
924 * and initializes ->proto.
925 */
926 if (peer || !local_peer) {
927 newpeer->addr = curpeers->peers_fe->srv->addr;
Willy Tarreau14e7f292021-10-27 17:41:07 +0200928 newpeer->proto = protocol_lookup(newpeer->addr.ss_family, PROTO_TYPE_STREAM, 0);
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100929 }
930
Willy Tarreaua261e9b2016-12-22 20:44:00 +0100931 newpeer->xprt = xprt_get(XPRT_RAW);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200932 newpeer->sock_init_arg = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100933 HA_SPIN_INIT(&newpeer->lock);
Willy Tarreau26d8c592012-05-07 18:12:14 +0200934
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +0100935 newpeer->srv = curpeers->peers_fe->srv;
936 if (!newpeer->local)
Frédéric Lécaille6617e762018-04-25 15:13:38 +0200937 goto out;
938
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100939 /* The lines above are reserved to "peer" lines. */
940 if (*args[0] == 's')
Frédéric Lécaille4ba51982018-04-25 15:32:18 +0200941 goto out;
Emeric Brun32da3c42010-09-23 18:39:19 +0200942
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100943 bind_conf = bind_conf_uniq_alloc(curpeers->peers_fe, file, linenum, args[2], xprt_get(XPRT_RAW));
William Lallemand1d932172022-07-06 14:30:23 +0200944 if (!bind_conf) {
945 ha_alert("parsing [%s:%d] : '%s %s' : Cannot allocate memory.\n", file, linenum, args[0], args[1]);
946 err_code |= ERR_FATAL;
947 goto out;
948 }
Frédéric Lécaille16e49102019-01-11 11:27:16 +0100949
Emeric Brun49f6f4b2022-05-25 10:12:07 +0200950 if (!LIST_ISEMPTY(&bind_conf->listeners)) {
951 ha_alert("parsing [%s:%d] : One listener per \"peers\" section is authorized but another is already configured at [%s:%d].\n", file, linenum, bind_conf->file, bind_conf->line);
952 err_code |= ERR_FATAL;
953 }
954
Frédéric Lécaille91694d52019-01-11 11:43:53 +0100955 if (!str2listener(args[2], curpeers->peers_fe, bind_conf, file, linenum, &errmsg)) {
956 if (errmsg && *errmsg) {
957 indent_msg(&errmsg, 2);
958 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 +0100959 }
Frédéric Lécaille91694d52019-01-11 11:43:53 +0100960 else
961 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
962 file, linenum, args[0], args[1], args[2]);
963 err_code |= ERR_FATAL;
964 goto out;
965 }
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100966
Emeric Brunca825782022-05-25 10:25:45 +0200967 /*
968 * Newly allocated listener is at the end of the list
969 */
970 l = LIST_ELEM(bind_conf->listeners.p, typeof(l), by_bind);
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100971 l->maxaccept = 1;
Frédéric Lécaille355b2032019-01-11 14:06:12 +0100972 l->accept = session_accept_fd;
973 l->analysers |= curpeers->peers_fe->fe_req_ana;
974 l->default_target = curpeers->peers_fe->default_target;
975 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
Willy Tarreau18215cb2019-02-27 16:25:28 +0100976 global.maxsock++; /* for the listening socket */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100977 }
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200978 else if (strcmp(args[0], "shards") == 0) {
979 char *endptr;
980
981 if (!*args[1]) {
982 ha_alert("parsing [%s:%d] : '%s' : missing value\n", file, linenum, args[0]);
983 err_code |= ERR_FATAL;
984 goto out;
985 }
986
987 curpeers->nb_shards = strtol(args[1], &endptr, 10);
988 if (*endptr != '\0') {
989 ha_alert("parsing [%s:%d] : '%s' : expects an integer argument, found '%s'\n",
990 file, linenum, args[0], args[1]);
991 err_code |= ERR_FATAL;
992 goto out;
993 }
994
995 if (!curpeers->nb_shards) {
996 ha_alert("parsing [%s:%d] : '%s' : expects a strictly positive integer argument\n",
997 file, linenum, args[0]);
998 err_code |= ERR_FATAL;
999 goto out;
1000 }
1001
1002 nb_shards = curpeers->nb_shards;
1003 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001004 else if (strcmp(args[0], "table") == 0) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001005 struct stktable *t, *other;
1006 char *id;
Frédéric Lécaillec02766a2019-03-20 15:06:55 +01001007 size_t prefix_len;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001008
1009 /* Line number and peer ID are updated only if this peer is the local one. */
Christopher Faulet244331f2022-07-25 15:10:44 +02001010 if (init_peers_frontend(file, -1, NULL, curpeers) != 0) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001011 err_code |= ERR_ALERT | ERR_ABORT;
1012 goto out;
1013 }
1014
1015 other = stktable_find_by_name(args[1]);
1016 if (other) {
1017 ha_alert("parsing [%s:%d] : stick-table name '%s' conflicts with table declared in %s '%s' at %s:%d.\n",
1018 file, linenum, args[1],
1019 other->proxy ? proxy_cap_str(other->proxy->cap) : "peers",
1020 other->proxy ? other->id : other->peers.p->id,
1021 other->conf.file, other->conf.line);
1022 err_code |= ERR_ALERT | ERR_FATAL;
1023 goto out;
1024 }
1025
Frédéric Lécaillec02766a2019-03-20 15:06:55 +01001026 /* Build the stick-table name, concatenating the "peers" section name
1027 * followed by a '/' character and the table name argument.
1028 */
1029 chunk_reset(&trash);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01001030 if (!chunk_strcpy(&trash, curpeers->id)) {
Frédéric Lécaillec02766a2019-03-20 15:06:55 +01001031 ha_alert("parsing [%s:%d]: '%s %s' : stick-table name too long.\n",
1032 file, linenum, args[0], args[1]);
1033 err_code |= ERR_ALERT | ERR_FATAL;
1034 goto out;
1035 }
1036
1037 prefix_len = trash.data;
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01001038 if (!chunk_memcat(&trash, "/", 1) || !chunk_strcat(&trash, args[1])) {
Frédéric Lécaillec02766a2019-03-20 15:06:55 +01001039 ha_alert("parsing [%s:%d]: '%s %s' : stick-table name too long.\n",
1040 file, linenum, args[0], args[1]);
1041 err_code |= ERR_ALERT | ERR_FATAL;
1042 goto out;
1043 }
1044
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001045 t = calloc(1, sizeof *t);
Frédéric Lécaillec02766a2019-03-20 15:06:55 +01001046 id = strdup(trash.area);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001047 if (!t || !id) {
1048 ha_alert("parsing [%s:%d]: '%s %s' : memory allocation failed\n",
1049 file, linenum, args[0], args[1]);
Eric Salama1aab9112020-09-18 11:55:17 +02001050 free(t);
1051 free(id);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001052 err_code |= ERR_ALERT | ERR_FATAL;
1053 goto out;
1054 }
1055
Frédéric Lécaillec02766a2019-03-20 15:06:55 +01001056 err_code |= parse_stick_table(file, linenum, args, t, id, id + prefix_len, curpeers);
Eric Salama1aab9112020-09-18 11:55:17 +02001057 if (err_code & ERR_FATAL) {
1058 free(t);
1059 free(id);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001060 goto out;
Eric Salama1aab9112020-09-18 11:55:17 +02001061 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001062
1063 stktable_store_name(t);
1064 t->next = stktables_list;
1065 stktables_list = t;
1066 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001067 else if (strcmp(args[0], "disabled") == 0) { /* disables this peers section */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001068 curpeers->disabled |= PR_FL_DISABLED;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02001069 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001070 else if (strcmp(args[0], "enabled") == 0) { /* enables this peers section (used to revert a disabled default) */
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02001071 curpeers->disabled = 0;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02001072 }
Emeric Brun32da3c42010-09-23 18:39:19 +02001073 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001074 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
Emeric Brun32da3c42010-09-23 18:39:19 +02001075 err_code |= ERR_ALERT | ERR_FATAL;
1076 goto out;
1077 }
1078
1079out:
Willy Tarreau902636f2013-03-10 19:44:48 +01001080 free(errmsg);
Emeric Brun32da3c42010-09-23 18:39:19 +02001081 return err_code;
1082}
1083
Baptiste Assmann325137d2015-04-13 23:40:55 +02001084/*
William Lallemand51097192015-04-14 16:35:22 +02001085 * Parse a line in a <listen>, <frontend> or <backend> section.
Simon Horman0d16a402015-01-30 11:22:58 +09001086 * Returns the error code, 0 if OK, or any combination of :
1087 * - ERR_ABORT: must abort ASAP
1088 * - ERR_FATAL: we can continue parsing but not start the service
1089 * - ERR_WARN: a warning has been emitted
1090 * - ERR_ALERT: an alert has been emitted
1091 * Only the two first ones can stop processing, the two others are just
1092 * indicators.
1093 */
1094int cfg_parse_mailers(const char *file, int linenum, char **args, int kwm)
1095{
1096 static struct mailers *curmailers = NULL;
1097 struct mailer *newmailer = NULL;
1098 const char *err;
1099 int err_code = 0;
1100 char *errmsg = NULL;
1101
1102 if (strcmp(args[0], "mailers") == 0) { /* new mailers section */
1103 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001104 ha_alert("parsing [%s:%d] : missing name for mailers section.\n", file, linenum);
Simon Horman0d16a402015-01-30 11:22:58 +09001105 err_code |= ERR_ALERT | ERR_ABORT;
1106 goto out;
1107 }
1108
1109 err = invalid_char(args[1]);
1110 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001111 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1112 file, linenum, *err, args[0], args[1]);
Simon Horman0d16a402015-01-30 11:22:58 +09001113 err_code |= ERR_ALERT | ERR_ABORT;
1114 goto out;
1115 }
1116
1117 for (curmailers = mailers; curmailers != NULL; curmailers = curmailers->next) {
1118 /*
1119 * If there are two proxies with the same name only following
1120 * combinations are allowed:
1121 */
1122 if (strcmp(curmailers->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001123 ha_alert("Parsing [%s:%d]: mailers section '%s' has the same name as another mailers section declared at %s:%d.\n",
1124 file, linenum, args[1], curmailers->conf.file, curmailers->conf.line);
Willy Tarreau911fa2e2015-05-26 10:35:50 +02001125 err_code |= ERR_ALERT | ERR_FATAL;
Simon Horman0d16a402015-01-30 11:22:58 +09001126 }
1127 }
1128
Vincent Bernat02779b62016-04-03 13:48:43 +02001129 if ((curmailers = calloc(1, sizeof(*curmailers))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001130 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Simon Horman0d16a402015-01-30 11:22:58 +09001131 err_code |= ERR_ALERT | ERR_ABORT;
1132 goto out;
1133 }
1134
1135 curmailers->next = mailers;
1136 mailers = curmailers;
1137 curmailers->conf.file = strdup(file);
1138 curmailers->conf.line = linenum;
1139 curmailers->id = strdup(args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001140 curmailers->timeout.mail = DEF_MAILALERTTIME;/* XXX: Would like to Skip to the next alert, if any, ASAP.
1141 * But need enough time so that timeouts don't occur
1142 * during tcp procssing. For now just us an arbitrary default. */
Simon Horman0d16a402015-01-30 11:22:58 +09001143 }
1144 else if (strcmp(args[0], "mailer") == 0) { /* mailer definition */
1145 struct sockaddr_storage *sk;
1146 int port1, port2;
1147 struct protocol *proto;
1148
1149 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001150 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1151 file, linenum, args[0]);
Simon Horman0d16a402015-01-30 11:22:58 +09001152 err_code |= ERR_ALERT | ERR_FATAL;
1153 goto out;
1154 }
1155
1156 err = invalid_char(args[1]);
1157 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001158 ha_alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1159 file, linenum, *err, args[1]);
Simon Horman0d16a402015-01-30 11:22:58 +09001160 err_code |= ERR_ALERT | ERR_FATAL;
1161 goto out;
1162 }
1163
Vincent Bernat02779b62016-04-03 13:48:43 +02001164 if ((newmailer = calloc(1, sizeof(*newmailer))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001165 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Simon Horman0d16a402015-01-30 11:22:58 +09001166 err_code |= ERR_ALERT | ERR_ABORT;
1167 goto out;
1168 }
1169
1170 /* the mailers are linked backwards first */
1171 curmailers->count++;
1172 newmailer->next = curmailers->mailer_list;
1173 curmailers->mailer_list = newmailer;
1174 newmailer->mailers = curmailers;
1175 newmailer->conf.file = strdup(file);
1176 newmailer->conf.line = linenum;
1177
1178 newmailer->id = strdup(args[1]);
1179
Willy Tarreau5fc93282020-09-16 18:25:03 +02001180 sk = str2sa_range(args[2], NULL, &port1, &port2, NULL, &proto,
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001181 &errmsg, NULL, NULL,
1182 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 +09001183 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001184 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Simon Horman0d16a402015-01-30 11:22:58 +09001185 err_code |= ERR_ALERT | ERR_FATAL;
1186 goto out;
1187 }
1188
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001189 if (proto->sock_prot != IPPROTO_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001190 ha_alert("parsing [%s:%d] : '%s %s' : TCP not supported for this address family.\n",
1191 file, linenum, args[0], args[1]);
Simon Horman0d16a402015-01-30 11:22:58 +09001192 err_code |= ERR_ALERT | ERR_FATAL;
1193 goto out;
1194 }
1195
Simon Horman0d16a402015-01-30 11:22:58 +09001196 newmailer->addr = *sk;
1197 newmailer->proto = proto;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001198 newmailer->xprt = xprt_get(XPRT_RAW);
Simon Horman0d16a402015-01-30 11:22:58 +09001199 newmailer->sock_init_arg = NULL;
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001200 }
1201 else if (strcmp(args[0], "timeout") == 0) {
1202 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001203 ha_alert("parsing [%s:%d] : '%s' expects 'mail' and <time> as arguments.\n",
1204 file, linenum, args[0]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001205 err_code |= ERR_ALERT | ERR_FATAL;
1206 goto out;
1207 }
1208 else if (strcmp(args[1], "mail") == 0) {
1209 const char *res;
1210 unsigned int timeout_mail;
1211 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001212 ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
1213 file, linenum, args[0], args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001214 err_code |= ERR_ALERT | ERR_FATAL;
1215 goto out;
1216 }
1217 res = parse_time_err(args[2], &timeout_mail, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001218 if (res == PARSE_TIME_OVER) {
1219 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
1220 file, linenum, args[2], args[0], args[1]);
1221 err_code |= ERR_ALERT | ERR_FATAL;
1222 goto out;
1223 }
1224 else if (res == PARSE_TIME_UNDER) {
1225 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
1226 file, linenum, args[2], args[0], args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001227 err_code |= ERR_ALERT | ERR_FATAL;
1228 goto out;
1229 }
Willy Tarreau9faebe32019-06-07 19:00:37 +02001230 else if (res) {
1231 ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
1232 file, linenum, *res, args[0], args[1]);
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001233 err_code |= ERR_ALERT | ERR_FATAL;
1234 goto out;
1235 }
1236 curmailers->timeout.mail = timeout_mail;
1237 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001238 ha_alert("parsing [%s:%d] : '%s' expects 'mail' and <time> as arguments got '%s'.\n",
Pieter Baauw235fcfc2016-02-13 15:33:40 +01001239 file, linenum, args[0], args[1]);
1240 err_code |= ERR_ALERT | ERR_FATAL;
1241 goto out;
1242 }
1243 }
Simon Horman0d16a402015-01-30 11:22:58 +09001244 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001245 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
Simon Horman0d16a402015-01-30 11:22:58 +09001246 err_code |= ERR_ALERT | ERR_FATAL;
1247 goto out;
1248 }
1249
1250out:
1251 free(errmsg);
1252 return err_code;
1253}
1254
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +01001255void free_email_alert(struct proxy *p)
Simon Horman9dc49962015-01-30 11:22:59 +09001256{
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001257 ha_free(&p->email_alert.mailers.name);
1258 ha_free(&p->email_alert.from);
1259 ha_free(&p->email_alert.to);
1260 ha_free(&p->email_alert.myhostname);
Simon Horman9dc49962015-01-30 11:22:59 +09001261}
1262
Willy Tarreaubaaee002006-06-26 02:48:02 +02001263
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001264int
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001265cfg_parse_netns(const char *file, int linenum, char **args, int kwm)
1266{
Willy Tarreaue5733232019-05-22 19:24:06 +02001267#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001268 const char *err;
1269 const char *item = args[0];
1270
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001271 if (strcmp(item, "namespace_list") == 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001272 return 0;
1273 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001274 else if (strcmp(item, "namespace") == 0) {
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001275 size_t idx = 1;
1276 const char *current;
1277 while (*(current = args[idx++])) {
1278 err = invalid_char(current);
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, item, current);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001282 return ERR_ALERT | ERR_FATAL;
1283 }
1284
1285 if (netns_store_lookup(current, strlen(current))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001286 ha_alert("parsing [%s:%d]: Namespace '%s' is already added.\n",
1287 file, linenum, current);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001288 return ERR_ALERT | ERR_FATAL;
1289 }
1290 if (!netns_store_insert(current)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001291 ha_alert("parsing [%s:%d]: Cannot open namespace '%s'.\n",
1292 file, linenum, current);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001293 return ERR_ALERT | ERR_FATAL;
1294 }
1295 }
1296 }
1297
1298 return 0;
1299#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001300 ha_alert("parsing [%s:%d]: namespace support is not compiled in.",
1301 file, linenum);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001302 return ERR_ALERT | ERR_FATAL;
1303#endif
1304}
1305
1306int
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001307cfg_parse_users(const char *file, int linenum, char **args, int kwm)
1308{
1309
1310 int err_code = 0;
1311 const char *err;
1312
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001313 if (strcmp(args[0], "userlist") == 0) { /* new userlist */
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001314 struct userlist *newul;
1315
1316 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001317 ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1318 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001319 err_code |= ERR_ALERT | ERR_FATAL;
1320 goto out;
1321 }
William Lallemand6e62fb62015-04-28 16:55:23 +02001322 if (alertif_too_many_args(1, file, linenum, args, &err_code))
1323 goto out;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001324
1325 err = invalid_char(args[1]);
1326 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001327 ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1328 file, linenum, *err, args[0], args[1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001329 err_code |= ERR_ALERT | ERR_FATAL;
1330 goto out;
1331 }
1332
1333 for (newul = userlist; newul; newul = newul->next)
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001334 if (strcmp(newul->name, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001335 ha_warning("parsing [%s:%d]: ignoring duplicated userlist '%s'.\n",
1336 file, linenum, args[1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001337 err_code |= ERR_WARN;
1338 goto out;
1339 }
1340
Vincent Bernat02779b62016-04-03 13:48:43 +02001341 newul = calloc(1, sizeof(*newul));
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001342 if (!newul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001343 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001344 err_code |= ERR_ALERT | ERR_ABORT;
1345 goto out;
1346 }
1347
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001348 newul->name = strdup(args[1]);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001349 if (!newul->name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001350 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001351 err_code |= ERR_ALERT | ERR_ABORT;
David Carlier97880bb2016-04-08 10:35:26 +01001352 free(newul);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001353 goto out;
1354 }
1355
1356 newul->next = userlist;
1357 userlist = newul;
1358
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001359 } else if (strcmp(args[0], "group") == 0) { /* new group */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001360 int cur_arg;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001361 const char *err;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001362 struct auth_groups *ag;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001363
1364 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001365 ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1366 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001367 err_code |= ERR_ALERT | ERR_FATAL;
1368 goto out;
1369 }
1370
1371 err = invalid_char(args[1]);
1372 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001373 ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1374 file, linenum, *err, args[0], args[1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001375 err_code |= ERR_ALERT | ERR_FATAL;
1376 goto out;
1377 }
1378
William Lallemand4ac9f542015-05-28 18:03:51 +02001379 if (!userlist)
1380 goto out;
1381
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001382 for (ag = userlist->groups; ag; ag = ag->next)
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001383 if (strcmp(ag->name, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001384 ha_warning("parsing [%s:%d]: ignoring duplicated group '%s' in userlist '%s'.\n",
1385 file, linenum, args[1], userlist->name);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001386 err_code |= ERR_ALERT;
1387 goto out;
1388 }
1389
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001390 ag = calloc(1, sizeof(*ag));
1391 if (!ag) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001392 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001393 err_code |= ERR_ALERT | ERR_ABORT;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001394 goto out;
1395 }
1396
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001397 ag->name = strdup(args[1]);
David Carlier70d60452016-08-22 23:27:42 +01001398 if (!ag->name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001399 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001400 err_code |= ERR_ALERT | ERR_ABORT;
David Carlier70d60452016-08-22 23:27:42 +01001401 free(ag);
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001402 goto out;
1403 }
1404
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001405 cur_arg = 2;
1406
1407 while (*args[cur_arg]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001408 if (strcmp(args[cur_arg], "users") == 0) {
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001409 ag->groupusers = strdup(args[cur_arg + 1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001410 cur_arg += 2;
1411 continue;
1412 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001413 ha_alert("parsing [%s:%d]: '%s' only supports 'users' option.\n",
1414 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001415 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier70d60452016-08-22 23:27:42 +01001416 free(ag->groupusers);
1417 free(ag->name);
1418 free(ag);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001419 goto out;
1420 }
1421 }
1422
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001423 ag->next = userlist->groups;
1424 userlist->groups = ag;
1425
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001426 } else if (strcmp(args[0], "user") == 0) { /* new user */
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001427 struct auth_users *newuser;
1428 int cur_arg;
1429
1430 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001431 ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1432 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001433 err_code |= ERR_ALERT | ERR_FATAL;
1434 goto out;
1435 }
William Lallemand4ac9f542015-05-28 18:03:51 +02001436 if (!userlist)
1437 goto out;
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001438
1439 for (newuser = userlist->users; newuser; newuser = newuser->next)
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001440 if (strcmp(newuser->user, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001441 ha_warning("parsing [%s:%d]: ignoring duplicated user '%s' in userlist '%s'.\n",
1442 file, linenum, args[1], userlist->name);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001443 err_code |= ERR_ALERT;
1444 goto out;
1445 }
1446
Vincent Bernat02779b62016-04-03 13:48:43 +02001447 newuser = calloc(1, sizeof(*newuser));
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001448 if (!newuser) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001449 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001450 err_code |= ERR_ALERT | ERR_ABORT;
1451 goto out;
1452 }
1453
1454 newuser->user = strdup(args[1]);
1455
1456 newuser->next = userlist->users;
1457 userlist->users = newuser;
1458
1459 cur_arg = 2;
1460
1461 while (*args[cur_arg]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001462 if (strcmp(args[cur_arg], "password") == 0) {
Willy Tarreaue5733232019-05-22 19:24:06 +02001463#ifdef USE_LIBCRYPT
Cyril Bonté1a0191d2014-08-29 20:20:02 +02001464 if (!crypt("", args[cur_arg + 1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001465 ha_alert("parsing [%s:%d]: the encrypted password used for user '%s' is not supported by crypt(3).\n",
1466 file, linenum, newuser->user);
Cyril Bonté1a0191d2014-08-29 20:20:02 +02001467 err_code |= ERR_ALERT | ERR_FATAL;
1468 goto out;
1469 }
1470#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001471 ha_warning("parsing [%s:%d]: no crypt(3) support compiled, encrypted passwords will not work.\n",
1472 file, linenum);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001473 err_code |= ERR_ALERT;
1474#endif
1475 newuser->pass = strdup(args[cur_arg + 1]);
1476 cur_arg += 2;
1477 continue;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001478 } else if (strcmp(args[cur_arg], "insecure-password") == 0) {
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001479 newuser->pass = strdup(args[cur_arg + 1]);
1480 newuser->flags |= AU_O_INSECURE;
1481 cur_arg += 2;
1482 continue;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001483 } else if (strcmp(args[cur_arg], "groups") == 0) {
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001484 newuser->u.groups_names = strdup(args[cur_arg + 1]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001485 cur_arg += 2;
1486 continue;
1487 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001488 ha_alert("parsing [%s:%d]: '%s' only supports 'password', 'insecure-password' and 'groups' options.\n",
1489 file, linenum, args[0]);
Krzysztof Piotr Oledzki96105042010-01-29 17:50:44 +01001490 err_code |= ERR_ALERT | ERR_FATAL;
1491 goto out;
1492 }
1493 }
1494 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001495 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 +01001496 err_code |= ERR_ALERT | ERR_FATAL;
1497 }
1498
1499out:
1500 return err_code;
1501}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001502
Christopher Faulet79bdef32016-11-04 22:36:15 +01001503int
1504cfg_parse_scope(const char *file, int linenum, char *line)
1505{
1506 char *beg, *end, *scope = NULL;
1507 int err_code = 0;
1508 const char *err;
1509
1510 beg = line + 1;
1511 end = strchr(beg, ']');
1512
1513 /* Detect end of scope declaration */
1514 if (!end || end == beg) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001515 ha_alert("parsing [%s:%d] : empty scope name is forbidden.\n",
1516 file, linenum);
Christopher Faulet79bdef32016-11-04 22:36:15 +01001517 err_code |= ERR_ALERT | ERR_FATAL;
1518 goto out;
1519 }
1520
1521 /* Get scope name and check its validity */
1522 scope = my_strndup(beg, end-beg);
1523 err = invalid_char(scope);
1524 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001525 ha_alert("parsing [%s:%d] : character '%c' is not permitted in a scope name.\n",
1526 file, linenum, *err);
Christopher Faulet79bdef32016-11-04 22:36:15 +01001527 err_code |= ERR_ALERT | ERR_ABORT;
1528 goto out;
1529 }
1530
1531 /* Be sure to have a scope declaration alone on its line */
1532 line = end+1;
1533 while (isspace((unsigned char)*line))
1534 line++;
1535 if (*line && *line != '#' && *line != '\n' && *line != '\r') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001536 ha_alert("parsing [%s:%d] : character '%c' is not permitted after scope declaration.\n",
1537 file, linenum, *line);
Christopher Faulet79bdef32016-11-04 22:36:15 +01001538 err_code |= ERR_ALERT | ERR_ABORT;
1539 goto out;
1540 }
1541
1542 /* We have a valid scope declaration, save it */
1543 free(cfg_scope);
1544 cfg_scope = scope;
1545 scope = NULL;
1546
1547 out:
1548 free(scope);
1549 return err_code;
1550}
1551
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01001552int
1553cfg_parse_track_sc_num(unsigned int *track_sc_num,
1554 const char *arg, const char *end, char **errmsg)
1555{
1556 const char *p;
1557 unsigned int num;
1558
1559 p = arg;
1560 num = read_uint64(&arg, end);
1561
1562 if (arg != end) {
1563 memprintf(errmsg, "Wrong track-sc number '%s'", p);
1564 return -1;
1565 }
1566
1567 if (num >= MAX_SESS_STKCTR) {
1568 memprintf(errmsg, "%u track-sc number exceeding "
1569 "%d (MAX_SESS_STKCTR-1) value", num, MAX_SESS_STKCTR - 1);
1570 return -1;
1571 }
1572
1573 *track_sc_num = num;
1574 return 0;
1575}
1576
Willy Tarreaubaaee002006-06-26 02:48:02 +02001577/*
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001578 * Detect a global section after a non-global one and output a diagnostic
1579 * warning.
1580 */
Christopher Faulet037e3f82022-11-18 15:46:06 +01001581static void check_section_position(char *section_name, const char *file, int linenum)
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001582{
Tim Duesterhusc5aa1132021-10-16 17:48:15 +02001583 if (strcmp(section_name, "global") == 0) {
Christopher Faulet037e3f82022-11-18 15:46:06 +01001584 if ((global.mode & MODE_DIAG) && non_global_section_parsed == 1)
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001585 _ha_diag_warning("parsing [%s:%d] : global section detected after a non-global one, the prevalence of their statements is unspecified\n", file, linenum);
1586 }
Christopher Faulet037e3f82022-11-18 15:46:06 +01001587 else if (non_global_section_parsed == 0) {
1588 non_global_section_parsed = 1;
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001589 }
1590}
1591
Willy Tarreau8a022d52021-04-27 20:29:11 +02001592/* apply the current default_path setting for config file <file>, and
1593 * optionally replace the current path to <origin> if not NULL while the
1594 * default-path mode is set to "origin". Errors are returned into an
1595 * allocated string passed to <err> if it's not NULL. Returns 0 on failure
1596 * or non-zero on success.
1597 */
1598static int cfg_apply_default_path(const char *file, const char *origin, char **err)
1599{
1600 const char *beg, *end;
1601
1602 /* make path start at <beg> and end before <end>, and switch it to ""
1603 * if no slash was passed.
1604 */
1605 beg = file;
1606 end = strrchr(beg, '/');
1607 if (!end)
1608 end = beg;
1609
1610 if (!*initial_cwd) {
1611 if (getcwd(initial_cwd, sizeof(initial_cwd)) == NULL) {
1612 if (err)
1613 memprintf(err, "Impossible to retrieve startup directory name: %s", strerror(errno));
1614 return 0;
1615 }
1616 }
1617 else if (chdir(initial_cwd) == -1) {
1618 if (err)
1619 memprintf(err, "Impossible to get back to initial directory '%s': %s", initial_cwd, strerror(errno));
1620 return 0;
1621 }
1622
1623 /* OK now we're (back) to initial_cwd */
1624
1625 switch (default_path_mode) {
1626 case DEFAULT_PATH_CURRENT:
1627 /* current_cwd never set, nothing to do */
1628 return 1;
1629
1630 case DEFAULT_PATH_ORIGIN:
1631 /* current_cwd set in the config */
1632 if (origin &&
1633 snprintf(current_cwd, sizeof(current_cwd), "%s", origin) > sizeof(current_cwd)) {
1634 if (err)
1635 memprintf(err, "Absolute path too long: '%s'", origin);
1636 return 0;
1637 }
1638 break;
1639
1640 case DEFAULT_PATH_CONFIG:
1641 if (end - beg >= sizeof(current_cwd)) {
1642 if (err)
1643 memprintf(err, "Config file path too long, cannot use for relative paths: '%s'", file);
1644 return 0;
1645 }
1646 memcpy(current_cwd, beg, end - beg);
1647 current_cwd[end - beg] = 0;
1648 break;
1649
1650 case DEFAULT_PATH_PARENT:
1651 if (end - beg + 3 >= sizeof(current_cwd)) {
1652 if (err)
1653 memprintf(err, "Config file path too long, cannot use for relative paths: '%s'", file);
1654 return 0;
1655 }
1656 memcpy(current_cwd, beg, end - beg);
1657 if (end > beg)
1658 memcpy(current_cwd + (end - beg), "/..\0", 4);
1659 else
1660 memcpy(current_cwd + (end - beg), "..\0", 3);
1661 break;
1662 }
1663
1664 if (*current_cwd && chdir(current_cwd) == -1) {
1665 if (err)
1666 memprintf(err, "Impossible to get back to directory '%s': %s", initial_cwd, strerror(errno));
1667 return 0;
1668 }
1669
1670 return 1;
1671}
1672
1673/* parses a global "default-path" directive. */
1674static int cfg_parse_global_def_path(char **args, int section_type, struct proxy *curpx,
1675 const struct proxy *defpx, const char *file, int line,
1676 char **err)
1677{
1678 int ret = -1;
1679
1680 /* "current", "config", "parent", "origin <path>" */
1681
1682 if (strcmp(args[1], "current") == 0)
1683 default_path_mode = DEFAULT_PATH_CURRENT;
1684 else if (strcmp(args[1], "config") == 0)
1685 default_path_mode = DEFAULT_PATH_CONFIG;
1686 else if (strcmp(args[1], "parent") == 0)
1687 default_path_mode = DEFAULT_PATH_PARENT;
1688 else if (strcmp(args[1], "origin") == 0)
1689 default_path_mode = DEFAULT_PATH_ORIGIN;
1690 else {
1691 memprintf(err, "%s default-path mode '%s' for '%s', supported modes include 'current', 'config', 'parent', and 'origin'.", *args[1] ? "unsupported" : "missing", args[1], args[0]);
1692 goto end;
1693 }
1694
1695 if (default_path_mode == DEFAULT_PATH_ORIGIN) {
1696 if (!*args[2]) {
1697 memprintf(err, "'%s %s' expects a directory as an argument.", args[0], args[1]);
1698 goto end;
1699 }
1700 if (!cfg_apply_default_path(file, args[2], err)) {
1701 memprintf(err, "couldn't set '%s' to origin '%s': %s.", args[0], args[2], *err);
1702 goto end;
1703 }
1704 }
1705 else if (!cfg_apply_default_path(file, NULL, err)) {
1706 memprintf(err, "couldn't set '%s' to '%s': %s.", args[0], args[1], *err);
1707 goto end;
1708 }
1709
1710 /* note that once applied, the path is immediately updated */
1711
1712 ret = 0;
1713 end:
1714 return ret;
1715}
1716
Amaury Denoyelle728be0f2021-03-31 11:43:47 +02001717/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001718 * This function reads and parses the configuration file given in the argument.
Willy Tarreauda543e12021-04-27 18:30:28 +02001719 * Returns the error code, 0 if OK, -1 if the config file couldn't be opened,
1720 * or any combination of :
Willy Tarreau058e9072009-07-20 09:30:05 +02001721 * - ERR_ABORT: must abort ASAP
1722 * - ERR_FATAL: we can continue parsing but not start the service
1723 * - ERR_WARN: a warning has been emitted
1724 * - ERR_ALERT: an alert has been emitted
1725 * Only the two first ones can stop processing, the two others are just
1726 * indicators.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001727 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001728int readcfgfile(const char *file)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001729{
Willy Tarreauda543e12021-04-27 18:30:28 +02001730 char *thisline = NULL;
William Lallemand64e84512015-05-12 14:25:37 +02001731 int linesize = LINESIZE;
Willy Tarreauda543e12021-04-27 18:30:28 +02001732 FILE *f = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001733 int linenum = 0;
Willy Tarreau058e9072009-07-20 09:30:05 +02001734 int err_code = 0;
William Lallemandd2ff56d2017-10-16 11:06:50 +02001735 struct cfg_section *cs = NULL, *pcs = NULL;
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001736 struct cfg_section *ics;
William Lallemand64e84512015-05-12 14:25:37 +02001737 int readbytes = 0;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001738 char *outline = NULL;
1739 size_t outlen = 0;
1740 size_t outlinesize = 0;
Willy Tarreau32234e72020-06-16 17:14:33 +02001741 int fatal = 0;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001742 int missing_lf = -1;
Willy Tarreau4b103022021-02-12 17:59:10 +01001743 int nested_cond_lvl = 0;
1744 enum nested_cond_state nested_conds[MAXNESTEDCONDS];
Willy Tarreau8a022d52021-04-27 20:29:11 +02001745 char *errmsg = NULL;
William Lallemand64e84512015-05-12 14:25:37 +02001746
Willy Tarreau51508052021-05-06 10:04:45 +02001747 global.cfg_curr_line = 0;
1748 global.cfg_curr_file = file;
1749
William Lallemand64e84512015-05-12 14:25:37 +02001750 if ((thisline = malloc(sizeof(*thisline) * linesize)) == NULL) {
Willy Tarreauda543e12021-04-27 18:30:28 +02001751 ha_alert("Out of memory trying to allocate a buffer for a configuration line.\n");
1752 err_code = -1;
1753 goto err;
William Lallemand64e84512015-05-12 14:25:37 +02001754 }
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01001755
Willy Tarreauda543e12021-04-27 18:30:28 +02001756 if ((f = fopen(file,"r")) == NULL) {
1757 err_code = -1;
1758 goto err;
David Carlier97880bb2016-04-08 10:35:26 +01001759 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001760
Willy Tarreau8a022d52021-04-27 20:29:11 +02001761 /* change to the new dir if required */
1762 if (!cfg_apply_default_path(file, NULL, &errmsg)) {
1763 ha_alert("parsing [%s:%d]: failed to apply default-path: %s.\n", file, linenum, errmsg);
1764 free(errmsg);
1765 err_code = -1;
1766 goto err;
1767 }
1768
William Lallemandb2f07452015-05-12 14:27:13 +02001769next_line:
William Lallemand64e84512015-05-12 14:25:37 +02001770 while (fgets(thisline + readbytes, linesize - readbytes, f) != NULL) {
Willy Tarreau3842f002009-06-14 11:39:52 +02001771 int arg, kwm = KWM_STD;
Krzysztof Piotr Oledzkie6bbd742007-11-01 00:33:12 +01001772 char *end;
1773 char *args[MAX_LINE_ARGS + 1];
1774 char *line = thisline;
1775
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001776 if (missing_lf != -1) {
Tim Duesterhusf92afb72020-08-18 22:00:04 +02001777 ha_alert("parsing [%s:%d]: Stray NUL character at position %d.\n",
1778 file, linenum, (missing_lf + 1));
1779 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001780 missing_lf = -1;
Tim Duesterhusf92afb72020-08-18 22:00:04 +02001781 break;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001782 }
1783
Willy Tarreaubaaee002006-06-26 02:48:02 +02001784 linenum++;
Willy Tarreau51508052021-05-06 10:04:45 +02001785 global.cfg_curr_line = linenum;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001786
Willy Tarreau32234e72020-06-16 17:14:33 +02001787 if (fatal >= 50) {
1788 ha_alert("parsing [%s:%d]: too many fatal errors (%d), stopping now.\n", file, linenum, fatal);
1789 break;
1790 }
1791
Willy Tarreaubaaee002006-06-26 02:48:02 +02001792 end = line + strlen(line);
1793
William Lallemand64e84512015-05-12 14:25:37 +02001794 if (end-line == linesize-1 && *(end-1) != '\n') {
Krzysztof Piotr Oledzkie6bbd742007-11-01 00:33:12 +01001795 /* Check if we reached the limit and the last char is not \n.
1796 * Watch out for the last line without the terminating '\n'!
1797 */
William Lallemand64e84512015-05-12 14:25:37 +02001798 char *newline;
1799 int newlinesize = linesize * 2;
1800
1801 newline = realloc(thisline, sizeof(*thisline) * newlinesize);
1802 if (newline == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001803 ha_alert("parsing [%s:%d]: line too long, cannot allocate memory.\n",
1804 file, linenum);
William Lallemand64e84512015-05-12 14:25:37 +02001805 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001806 fatal++;
Willy Tarreau40cb26f2020-06-25 09:37:54 +02001807 linenum--;
William Lallemand64e84512015-05-12 14:25:37 +02001808 continue;
1809 }
1810
1811 readbytes = linesize - 1;
1812 linesize = newlinesize;
1813 thisline = newline;
Willy Tarreau40cb26f2020-06-25 09:37:54 +02001814 linenum--;
William Lallemand64e84512015-05-12 14:25:37 +02001815 continue;
Krzysztof Piotr Oledzkie6bbd742007-11-01 00:33:12 +01001816 }
1817
William Lallemand64e84512015-05-12 14:25:37 +02001818 readbytes = 0;
1819
Willy Tarreau08488f62020-06-26 17:24:54 +02001820 if (end > line && *(end-1) == '\n') {
Tim Duesterhus70f58992020-06-22 22:57:44 +02001821 /* kill trailing LF */
1822 *(end - 1) = 0;
1823 }
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02001824 else {
1825 /* mark this line as truncated */
1826 missing_lf = end - line;
1827 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001828
Willy Tarreaubaaee002006-06-26 02:48:02 +02001829 /* skip leading spaces */
Willy Tarreau8f8e6452007-06-17 21:51:38 +02001830 while (isspace((unsigned char)*line))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001831 line++;
William Lallemandf9873ba2015-05-05 17:37:14 +02001832
Joseph Herlanta14c03e2018-11-15 14:04:19 -08001833 if (*line == '[') {/* This is the beginning if a scope */
Christopher Faulet79bdef32016-11-04 22:36:15 +01001834 err_code |= cfg_parse_scope(file, linenum, line);
1835 goto next_line;
1836 }
1837
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001838 while (1) {
1839 uint32_t err;
Maximilian Mader29c6cd72021-06-06 00:50:21 +02001840 const char *errptr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001841
Tim Duesterhusb3168b32021-06-06 00:50:20 +02001842 arg = sizeof(args) / sizeof(*args);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001843 outlen = outlinesize;
1844 err = parse_line(line, outline, &outlen, args, &arg,
1845 PARSE_OPT_ENV | PARSE_OPT_DQUOTE | PARSE_OPT_SQUOTE |
Amaury Denoyellefa41cb62020-10-01 14:32:35 +02001846 PARSE_OPT_BKSLASH | PARSE_OPT_SHARP | PARSE_OPT_WORD_EXPAND,
1847 &errptr);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001848
1849 if (err & PARSE_ERR_QUOTE) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001850 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1851
1852 ha_alert("parsing [%s:%d]: unmatched quote at position %d:\n"
1853 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001854 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001855 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001856 goto next_line;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001857 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001858
1859 if (err & PARSE_ERR_BRACE) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001860 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1861
1862 ha_alert("parsing [%s:%d]: unmatched brace in environment variable name at position %d:\n"
1863 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001864 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001865 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001866 goto next_line;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001867 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001868
1869 if (err & PARSE_ERR_VARNAME) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001870 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1871
1872 ha_alert("parsing [%s:%d]: forbidden first char in environment variable name at position %d:\n"
1873 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001874 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02001875 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001876 goto next_line;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001877 }
William Lallemandb2f07452015-05-12 14:27:13 +02001878
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001879 if (err & PARSE_ERR_HEX) {
Willy Tarreauc54e5ad2020-06-25 09:15:40 +02001880 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1881
1882 ha_alert("parsing [%s:%d]: truncated or invalid hexadecimal sequence at position %d:\n"
1883 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001884 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus9f658a52020-06-16 18:14:21 +02001885 fatal++;
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001886 goto next_line;
1887 }
William Lallemandb2f07452015-05-12 14:27:13 +02001888
Amaury Denoyellefa41cb62020-10-01 14:32:35 +02001889 if (err & PARSE_ERR_WRONG_EXPAND) {
1890 size_t newpos = sanitize_for_printing(line, errptr - line, 80);
1891
1892 ha_alert("parsing [%s:%d]: truncated or invalid word expansion sequence at position %d:\n"
1893 " %s\n %*s\n", file, linenum, (int)(errptr-thisline+1), line, (int)(newpos+1), "^");
1894 err_code |= ERR_ALERT | ERR_FATAL;
1895 fatal++;
1896 goto next_line;
1897 }
1898
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001899 if (err & (PARSE_ERR_TOOLARGE|PARSE_ERR_OVERLAP)) {
1900 outlinesize = (outlen + 1023) & -1024;
Ilya Shipitsin76837bc2021-01-07 22:45:13 +05001901 outline = my_realloc2(outline, outlinesize);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001902 if (outline == NULL) {
1903 ha_alert("parsing [%s:%d]: line too long, cannot allocate memory.\n",
1904 file, linenum);
Willy Tarreau8ec9c812022-05-20 09:13:38 +02001905 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
Willy Tarreau32234e72020-06-16 17:14:33 +02001906 fatal++;
Christopher Fauletdfe32c72022-05-18 16:22:43 +02001907 outlinesize = 0;
Willy Tarreau8ec9c812022-05-20 09:13:38 +02001908 goto err;
William Lallemandb2f07452015-05-12 14:27:13 +02001909 }
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001910 /* try again */
1911 continue;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001912 }
Willy Tarreau07d47062020-06-25 07:41:22 +02001913
1914 if (err & PARSE_ERR_TOOMANY) {
1915 /* only check this *after* being sure the output is allocated */
1916 ha_alert("parsing [%s:%d]: too many words, truncating after word %d, position %ld: <%s>.\n",
1917 file, linenum, MAX_LINE_ARGS, (long)(args[MAX_LINE_ARGS-1] - outline + 1), args[MAX_LINE_ARGS-1]);
1918 err_code |= ERR_ALERT | ERR_FATAL;
1919 fatal++;
1920 goto next_line;
1921 }
1922
Willy Tarreau9e1758e2020-06-16 16:32:59 +02001923 /* everything's OK */
1924 break;
William Lallemandf9873ba2015-05-05 17:37:14 +02001925 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001926
Erwan Le Goasb0c05012022-09-14 17:51:55 +02001927 /* dump cfg */
1928 if (global.mode & MODE_DUMP_CFG) {
1929 if (args[0] != NULL) {
1930 struct cfg_section *sect;
1931 int is_sect = 0;
1932 int i = 0;
1933 uint32_t g_key = HA_ATOMIC_LOAD(&global.anon_key);
1934
Erwan Le Goasf30c5d72022-09-29 10:34:04 +02001935 if (global.mode & MODE_DUMP_NB_L)
1936 qfprintf(stdout, "%d\t", linenum);
Erwan Le Goasb0c05012022-09-14 17:51:55 +02001937
1938 /* if a word is in sections list, is_sect = 1 */
1939 list_for_each_entry(sect, &sections, list) {
1940 if (strcmp(args[0], sect->section_name) == 0) {
1941 is_sect = 1;
1942 break;
1943 }
1944 }
1945
1946 if (g_key == 0) {
1947 /* no anonymizing needed, dump the config as-is (but without comments).
1948 * Note: tabs were lost during tokenizing, so we reinsert for non-section
1949 * keywords.
1950 */
1951 if (!is_sect)
1952 qfprintf(stdout, "\t");
1953
1954 for (i = 0; i < arg; i++) {
1955 qfprintf(stdout, "%s ", args[i]);
1956 }
1957 qfprintf(stdout, "\n");
1958 continue;
1959 }
1960
1961 /* We're anonymizing */
1962
1963 if (is_sect) {
1964 /* new sections are optionally followed by an identifier */
1965 if (arg >= 2) {
1966 qfprintf(stdout, "%s %s\n", args[0], HA_ANON_ID(g_key, args[1]));
1967 }
1968 else {
1969 qfprintf(stdout, "%s\n", args[0]);
1970 }
1971 continue;
1972 }
1973
1974 /* non-section keywords start indented */
1975 qfprintf(stdout, "\t");
1976
1977 /* some keywords deserve special treatment */
1978 if (!*args[0]) {
1979 qfprintf(stdout, "\n");
1980 }
1981
1982 else if (strcmp(args[0], "anonkey") == 0) {
1983 qfprintf(stdout, "%s [...]\n", args[0]);
1984 }
1985
1986 else if (strcmp(args[0], "maxconn") == 0) {
1987 qfprintf(stdout, "%s %s\n", args[0], args[1]);
1988 }
1989
1990 else if (strcmp(args[0], "stats") == 0 &&
1991 (strcmp(args[1], "timeout") == 0 || strcmp(args[1], "maxconn") == 0)) {
1992 qfprintf(stdout, "%s %s %s\n", args[0], args[1], args[2]);
1993 }
1994
1995 else if (strcmp(args[0], "stats") == 0 && strcmp(args[1], "socket") == 0) {
1996 qfprintf(stdout, "%s %s ", args[0], args[1]);
1997
Erwan Le Goasbe5ed922022-09-29 10:30:00 +02001998 if (arg > 2) {
Erwan Le Goas5eef1582022-09-29 10:25:31 +02001999 qfprintf(stdout, "%s ", hash_ipanon(g_key, args[2], 1));
Erwan Le Goasb0c05012022-09-14 17:51:55 +02002000
Erwan Le Goasbe5ed922022-09-29 10:30:00 +02002001 if (arg > 3) {
Erwan Le Goasb0c05012022-09-14 17:51:55 +02002002 qfprintf(stdout, "[...]\n");
2003 }
2004 else {
2005 qfprintf(stdout, "\n");
2006 }
2007 }
2008 else {
2009 qfprintf(stdout, "\n");
2010 }
2011 }
2012
2013 else if (strcmp(args[0], "timeout") == 0) {
2014 qfprintf(stdout, "%s %s %s\n", args[0], args[1], args[2]);
2015 }
2016
2017 else if (strcmp(args[0], "mode") == 0) {
2018 qfprintf(stdout, "%s %s\n", args[0], args[1]);
2019 }
2020
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05002021 /* It concerns user in global section and in userlist */
Erwan Le Goasb0c05012022-09-14 17:51:55 +02002022 else if (strcmp(args[0], "user") == 0) {
2023 qfprintf(stdout, "%s %s ", args[0], HA_ANON_ID(g_key, args[1]));
2024
2025 if (arg > 2) {
2026 qfprintf(stdout, "[...]\n");
2027 }
2028 else {
2029 qfprintf(stdout, "\n");
2030 }
2031 }
2032
2033 else if (strcmp(args[0], "bind") == 0) {
2034 qfprintf(stdout, "%s ", args[0]);
Erwan Le Goas5eef1582022-09-29 10:25:31 +02002035 qfprintf(stdout, "%s ", hash_ipanon(g_key, args[1], 1));
Erwan Le Goasb0c05012022-09-14 17:51:55 +02002036 if (arg > 2) {
2037 qfprintf(stdout, "[...]\n");
2038 }
2039 else {
2040 qfprintf(stdout, "\n");
2041 }
2042 }
2043
2044 else if (strcmp(args[0], "server") == 0) {
Erwan Le Goas059d05f2022-09-29 10:31:18 +02002045 qfprintf(stdout, "%s %s ", args[0], HA_ANON_ID(g_key, args[1]));
Erwan Le Goasb0c05012022-09-14 17:51:55 +02002046
Erwan Le Goasb0c05012022-09-14 17:51:55 +02002047 if (arg > 2) {
Erwan Le Goas5eef1582022-09-29 10:25:31 +02002048 qfprintf(stdout, "%s ", hash_ipanon(g_key, args[2], 1));
Erwan Le Goasb0c05012022-09-14 17:51:55 +02002049 }
2050 if (arg > 3) {
2051 qfprintf(stdout, "[...]\n");
2052 }
2053 else {
2054 qfprintf(stdout, "\n");
2055 }
2056 }
2057
2058 else if (strcmp(args[0], "redirect") == 0) {
2059 qfprintf(stdout, "%s %s ", args[0], args[1]);
2060
2061 if (strcmp(args[1], "prefix") == 0 || strcmp(args[1], "location") == 0) {
2062 qfprintf(stdout, "%s ", HA_ANON_PATH(g_key, args[2]));
2063 }
2064 else {
2065 qfprintf(stdout, "%s ", args[2]);
2066 }
2067 if (arg > 3) {
2068 qfprintf(stdout, "[...]");
2069 }
2070 qfprintf(stdout, "\n");
2071 }
2072
2073 else if (strcmp(args[0], "acl") == 0) {
2074 qfprintf(stdout, "%s %s %s ", args[0], HA_ANON_ID(g_key, args[1]), args[2]);
2075
2076 if (arg > 3) {
2077 qfprintf(stdout, "[...]");
2078 }
2079 qfprintf(stdout, "\n");
2080 }
2081
2082 else if (strcmp(args[0], "log") == 0) {
2083 qfprintf(stdout, "log ");
2084
2085 if (strcmp(args[1], "global") == 0) {
2086 qfprintf(stdout, "%s ", args[1]);
2087 }
2088 else {
Erwan Le Goas5eef1582022-09-29 10:25:31 +02002089 qfprintf(stdout, "%s ", hash_ipanon(g_key, args[1], 1));
Erwan Le Goasb0c05012022-09-14 17:51:55 +02002090 }
2091 if (arg > 2) {
2092 qfprintf(stdout, "[...]");
2093 }
2094 qfprintf(stdout, "\n");
2095 }
2096
2097 else if (strcmp(args[0], "peer") == 0) {
2098 qfprintf(stdout, "%s %s ", args[0], HA_ANON_ID(g_key, args[1]));
Erwan Le Goas5eef1582022-09-29 10:25:31 +02002099 qfprintf(stdout, "%s ", hash_ipanon(g_key, args[2], 1));
Erwan Le Goasb0c05012022-09-14 17:51:55 +02002100
2101 if (arg > 3) {
2102 qfprintf(stdout, "[...]");
2103 }
2104 qfprintf(stdout, "\n");
2105 }
2106
2107 else if (strcmp(args[0], "use_backend") == 0) {
2108 qfprintf(stdout, "%s %s ", args[0], HA_ANON_ID(g_key, args[1]));
2109
2110 if (arg > 2) {
2111 qfprintf(stdout, "[...]");
2112 }
2113 qfprintf(stdout, "\n");
2114 }
2115
2116 else if (strcmp(args[0], "default_backend") == 0) {
2117 qfprintf(stdout, "%s %s\n", args[0], HA_ANON_ID(g_key, args[1]));
2118 }
2119
Erwan Le Goas059d05f2022-09-29 10:31:18 +02002120 else if (strcmp(args[0], "source") == 0) {
2121 qfprintf(stdout, "%s %s ", args[0], hash_ipanon(g_key, args[1], 1));
2122
2123 if (arg > 2) {
2124 qfprintf(stdout, "[...]");
2125 }
2126 qfprintf(stdout, "\n");
2127 }
2128
2129 else if (strcmp(args[0], "nameserver") == 0) {
2130 qfprintf(stdout, "%s %s %s ", args[0],
2131 HA_ANON_ID(g_key, args[1]), hash_ipanon(g_key, args[2], 1));
2132 if (arg > 3) {
2133 qfprintf(stdout, "[...]");
2134 }
2135 qfprintf(stdout, "\n");
2136 }
2137
2138 else if (strcmp(args[0], "http-request") == 0) {
2139 qfprintf(stdout, "%s %s ", args[0], args[1]);
2140 if (arg > 2)
2141 qfprintf(stdout, "[...]");
2142 qfprintf(stdout, "\n");
2143 }
2144
2145 else if (strcmp(args[0], "http-response") == 0) {
2146 qfprintf(stdout, "%s %s ", args[0], args[1]);
2147 if (arg > 2)
2148 qfprintf(stdout, "[...]");
2149 qfprintf(stdout, "\n");
2150 }
2151
2152 else if (strcmp(args[0], "http-after-response") == 0) {
2153 qfprintf(stdout, "%s %s ", args[0], args[1]);
2154 if (arg > 2)
2155 qfprintf(stdout, "[...]");
2156 qfprintf(stdout, "\n");
2157 }
2158
2159 else if (strcmp(args[0], "filter") == 0) {
2160 qfprintf(stdout, "%s %s ", args[0], args[1]);
2161 if (arg > 2)
2162 qfprintf(stdout, "[...]");
2163 qfprintf(stdout, "\n");
2164 }
2165
2166 else if (strcmp(args[0], "errorfile") == 0) {
2167 qfprintf(stdout, "%s %s %s\n", args[0], args[1], HA_ANON_PATH(g_key, args[2]));
2168 }
2169
2170 else if (strcmp(args[0], "cookie") == 0) {
2171 qfprintf(stdout, "%s %s ", args[0], HA_ANON_ID(g_key, args[1]));
2172 if (arg > 2)
2173 qfprintf(stdout, "%s ", args[2]);
2174 if (arg > 3)
2175 qfprintf(stdout, "[...]");
2176 qfprintf(stdout, "\n");
2177 }
2178
2179 else if (strcmp(args[0], "stats") == 0 && strcmp(args[1], "auth") == 0) {
2180 qfprintf(stdout, "%s %s %s\n", args[0], args[1], HA_ANON_STR(g_key, args[2]));
2181 }
2182
Erwan Le Goasb0c05012022-09-14 17:51:55 +02002183 else {
2184 /* display up to 3 words and mask the rest which might be confidential */
2185 for (i = 0; i < MIN(arg, 3); i++) {
2186 qfprintf(stdout, "%s ", args[i]);
2187 }
2188 if (arg > 3) {
2189 qfprintf(stdout, "[...]");
2190 }
2191 qfprintf(stdout, "\n");
2192 }
2193 }
2194 continue;
2195 }
2196 /* end of config dump */
2197
Willy Tarreaubaaee002006-06-26 02:48:02 +02002198 /* empty line */
2199 if (!**args)
2200 continue;
2201
Willy Tarreau4b103022021-02-12 17:59:10 +01002202 /* check for config macros */
2203 if (*args[0] == '.') {
2204 if (strcmp(args[0], ".if") == 0) {
Willy Tarreau299bd1c2021-05-06 15:07:10 +02002205 const char *errptr = NULL;
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002206 char *errmsg = NULL;
2207 int cond;
Willy Tarreauc8194c32021-07-16 16:38:58 +02002208 char *w;
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002209
Willy Tarreauc8194c32021-07-16 16:38:58 +02002210 /* remerge all words into a single expression */
2211 for (w = *args; (w += strlen(w)) < outline + outlen - 1; *w = ' ')
2212 ;
Tim Duesterhus5546c8b2021-05-26 17:45:33 +02002213
Willy Tarreau4b103022021-02-12 17:59:10 +01002214 nested_cond_lvl++;
2215 if (nested_cond_lvl >= MAXNESTEDCONDS) {
2216 ha_alert("parsing [%s:%d]: too many nested '.if', max is %d.\n", file, linenum, MAXNESTEDCONDS);
2217 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2218 goto err;
2219 }
2220
Willy Tarreau6e647c92021-05-06 08:46:11 +02002221 if (nested_cond_lvl > 1 &&
2222 (nested_conds[nested_cond_lvl - 1] == NESTED_COND_IF_DROP ||
2223 nested_conds[nested_cond_lvl - 1] == NESTED_COND_IF_SKIP ||
2224 nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELIF_DROP ||
2225 nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELIF_SKIP ||
2226 nested_conds[nested_cond_lvl - 1] == NESTED_COND_ELSE_DROP)) {
Willy Tarreau4b103022021-02-12 17:59:10 +01002227 nested_conds[nested_cond_lvl] = NESTED_COND_IF_SKIP;
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002228 goto next_line;
2229 }
2230
Willy Tarreau299bd1c2021-05-06 15:07:10 +02002231 cond = cfg_eval_condition(args + 1, &errmsg, &errptr);
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002232 if (cond < 0) {
Willy Tarreau299bd1c2021-05-06 15:07:10 +02002233 size_t newpos = sanitize_for_printing(args[1], errptr - args[1], 76);
2234
2235 ha_alert("parsing [%s:%d]: %s in '.if' at position %d:\n .if %s\n %*s\n",
2236 file, linenum, errmsg,
2237 (int)(errptr-args[1]+1), args[1], (int)(newpos+5), "^");
2238
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002239 free(errmsg);
Willy Tarreau4b103022021-02-12 17:59:10 +01002240 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2241 goto err;
2242 }
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002243
2244 if (cond)
2245 nested_conds[nested_cond_lvl] = NESTED_COND_IF_TAKE;
2246 else
2247 nested_conds[nested_cond_lvl] = NESTED_COND_IF_DROP;
2248
Willy Tarreau4b103022021-02-12 17:59:10 +01002249 goto next_line;
2250 }
2251 else if (strcmp(args[0], ".elif") == 0) {
Willy Tarreau299bd1c2021-05-06 15:07:10 +02002252 const char *errptr = NULL;
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002253 char *errmsg = NULL;
2254 int cond;
Willy Tarreauc8194c32021-07-16 16:38:58 +02002255 char *w;
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002256
Willy Tarreauc8194c32021-07-16 16:38:58 +02002257 /* remerge all words into a single expression */
2258 for (w = *args; (w += strlen(w)) < outline + outlen - 1; *w = ' ')
2259 ;
Tim Duesterhus5546c8b2021-05-26 17:45:33 +02002260
Willy Tarreau4b103022021-02-12 17:59:10 +01002261 if (!nested_cond_lvl) {
2262 ha_alert("parsing [%s:%d]: lone '.elif' with no matching '.if'.\n", file, linenum);
2263 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2264 goto err;
2265 }
2266
2267 if (nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_TAKE ||
2268 nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP) {
2269 ha_alert("parsing [%s:%d]: '.elif' after '.else' is not permitted.\n", file, linenum);
2270 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2271 goto err;
2272 }
2273
2274 if (nested_conds[nested_cond_lvl] == NESTED_COND_IF_TAKE ||
2275 nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
Willy Tarreauf67ff022021-05-06 08:48:09 +02002276 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_TAKE ||
Willy Tarreau4b103022021-02-12 17:59:10 +01002277 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP) {
2278 nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_SKIP;
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002279 goto next_line;
2280 }
2281
Willy Tarreau299bd1c2021-05-06 15:07:10 +02002282 cond = cfg_eval_condition(args + 1, &errmsg, &errptr);
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002283 if (cond < 0) {
Willy Tarreau299bd1c2021-05-06 15:07:10 +02002284 size_t newpos = sanitize_for_printing(args[1], errptr - args[1], 74);
2285
2286 ha_alert("parsing [%s:%d]: %s in '.elif' at position %d:\n .elif %s\n %*s\n",
2287 file, linenum, errmsg,
2288 (int)(errptr-args[1]+1), args[1], (int)(newpos+7), "^");
2289
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002290 free(errmsg);
Willy Tarreau4b103022021-02-12 17:59:10 +01002291 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2292 goto err;
2293 }
Willy Tarreau6a2110c2021-05-06 08:19:48 +02002294
2295 if (cond)
2296 nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_TAKE;
2297 else
2298 nested_conds[nested_cond_lvl] = NESTED_COND_ELIF_DROP;
2299
Willy Tarreau4b103022021-02-12 17:59:10 +01002300 goto next_line;
2301 }
2302 else if (strcmp(args[0], ".else") == 0) {
Tim Duesterhus5546c8b2021-05-26 17:45:33 +02002303 if (*args[1]) {
Ilya Shipitsin213bb992021-06-12 15:55:27 +05002304 ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'.\n",
Tim Duesterhus5546c8b2021-05-26 17:45:33 +02002305 file, linenum, args[1], args[0]);
2306 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2307 break;
2308 }
2309
Willy Tarreau4b103022021-02-12 17:59:10 +01002310 if (!nested_cond_lvl) {
2311 ha_alert("parsing [%s:%d]: lone '.else' with no matching '.if'.\n", file, linenum);
2312 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2313 goto err;
2314 }
2315
2316 if (nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_TAKE ||
2317 nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP) {
2318 ha_alert("parsing [%s:%d]: '.else' after '.else' is not permitted.\n", file, linenum);
2319 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2320 goto err;
2321 }
2322
2323 if (nested_conds[nested_cond_lvl] == NESTED_COND_IF_TAKE ||
2324 nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
2325 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_TAKE ||
2326 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP) {
2327 nested_conds[nested_cond_lvl] = NESTED_COND_ELSE_DROP;
2328 } else {
2329 /* otherwise we take the "else" */
2330 nested_conds[nested_cond_lvl] = NESTED_COND_ELSE_TAKE;
2331 }
2332 goto next_line;
2333 }
2334 else if (strcmp(args[0], ".endif") == 0) {
Tim Duesterhus5546c8b2021-05-26 17:45:33 +02002335 if (*args[1]) {
Ilya Shipitsin213bb992021-06-12 15:55:27 +05002336 ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'.\n",
Tim Duesterhus5546c8b2021-05-26 17:45:33 +02002337 file, linenum, args[1], args[0]);
2338 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2339 break;
2340 }
2341
Willy Tarreau4b103022021-02-12 17:59:10 +01002342 if (!nested_cond_lvl) {
2343 ha_alert("parsing [%s:%d]: lone '.endif' with no matching '.if'.\n", file, linenum);
Tim Duesterhus5546c8b2021-05-26 17:45:33 +02002344 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
Willy Tarreau4b103022021-02-12 17:59:10 +01002345 break;
2346 }
2347 nested_cond_lvl--;
2348 goto next_line;
2349 }
2350 }
2351
2352 if (nested_cond_lvl &&
2353 (nested_conds[nested_cond_lvl] == NESTED_COND_IF_DROP ||
2354 nested_conds[nested_cond_lvl] == NESTED_COND_IF_SKIP ||
2355 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_DROP ||
2356 nested_conds[nested_cond_lvl] == NESTED_COND_ELIF_SKIP ||
2357 nested_conds[nested_cond_lvl] == NESTED_COND_ELSE_DROP)) {
2358 /* The current block is masked out by the conditions */
2359 goto next_line;
2360 }
2361
Willy Tarreau7190b982021-05-07 08:59:50 +02002362 /* .warning/.error/.notice/.diag */
Willy Tarreau4b103022021-02-12 17:59:10 +01002363 if (*args[0] == '.') {
2364 if (strcmp(args[0], ".alert") == 0) {
Tim Duesterhus5546c8b2021-05-26 17:45:33 +02002365 if (*args[2]) {
2366 ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'. Use quotes if the message should contain spaces.\n",
2367 file, linenum, args[2], args[0]);
2368 err_code |= ERR_ALERT | ERR_FATAL;
2369 goto next_line;
2370 }
2371
Willy Tarreau4b103022021-02-12 17:59:10 +01002372 ha_alert("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
2373 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2374 goto err;
2375 }
2376 else if (strcmp(args[0], ".warning") == 0) {
Tim Duesterhus5546c8b2021-05-26 17:45:33 +02002377 if (*args[2]) {
2378 ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'. Use quotes if the message should contain spaces.\n",
2379 file, linenum, args[2], args[0]);
2380 err_code |= ERR_ALERT | ERR_FATAL;
2381 goto next_line;
2382 }
2383
Willy Tarreau4b103022021-02-12 17:59:10 +01002384 ha_warning("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
2385 err_code |= ERR_WARN;
2386 goto next_line;
2387 }
2388 else if (strcmp(args[0], ".notice") == 0) {
Tim Duesterhus5546c8b2021-05-26 17:45:33 +02002389 if (*args[2]) {
2390 ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'. Use quotes if the message should contain spaces.\n",
2391 file, linenum, args[2], args[0]);
2392 err_code |= ERR_ALERT | ERR_FATAL;
2393 goto next_line;
2394 }
2395
Willy Tarreau4b103022021-02-12 17:59:10 +01002396 ha_notice("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
2397 goto next_line;
2398 }
Willy Tarreau7190b982021-05-07 08:59:50 +02002399 else if (strcmp(args[0], ".diag") == 0) {
Tim Duesterhus5546c8b2021-05-26 17:45:33 +02002400 if (*args[2]) {
2401 ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'. Use quotes if the message should contain spaces.\n",
2402 file, linenum, args[2], args[0]);
2403 err_code |= ERR_ALERT | ERR_FATAL;
2404 goto next_line;
2405 }
2406
Willy Tarreau7190b982021-05-07 08:59:50 +02002407 ha_diag_warning("parsing [%s:%d]: '%s'.\n", file, linenum, args[1]);
2408 goto next_line;
2409 }
Willy Tarreau4b103022021-02-12 17:59:10 +01002410 else {
2411 ha_alert("parsing [%s:%d]: unknown directive '%s'.\n", file, linenum, args[0]);
2412 err_code |= ERR_ALERT | ERR_FATAL;
2413 fatal++;
2414 break;
2415 }
2416 }
2417
Willy Tarreau3842f002009-06-14 11:39:52 +02002418 /* check for keyword modifiers "no" and "default" */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002419 if (strcmp(args[0], "no") == 0) {
William Lallemand0f99e342011-10-12 17:50:54 +02002420 char *tmp;
2421
Willy Tarreau3842f002009-06-14 11:39:52 +02002422 kwm = KWM_NO;
William Lallemand0f99e342011-10-12 17:50:54 +02002423 tmp = args[0];
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002424 for (arg=0; *args[arg+1]; arg++)
2425 args[arg] = args[arg+1]; // shift args after inversion
William Lallemand0f99e342011-10-12 17:50:54 +02002426 *tmp = '\0'; // fix the next arg to \0
2427 args[arg] = tmp;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002428 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002429 else if (strcmp(args[0], "default") == 0) {
Willy Tarreau3842f002009-06-14 11:39:52 +02002430 kwm = KWM_DEF;
2431 for (arg=0; *args[arg+1]; arg++)
2432 args[arg] = args[arg+1]; // shift args after inversion
2433 }
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002434
William Dauchyec730982019-10-27 20:08:10 +01002435 if (kwm != KWM_STD && strcmp(args[0], "option") != 0 &&
2436 strcmp(args[0], "log") != 0 && strcmp(args[0], "busy-polling") != 0 &&
Willy Tarreaud96f1122019-12-03 07:07:36 +01002437 strcmp(args[0], "set-dumpable") != 0 && strcmp(args[0], "strict-limits") != 0 &&
Amaury Denoyelle0f50cb92021-03-26 18:50:33 +01002438 strcmp(args[0], "insecure-fork-wanted") != 0 &&
2439 strcmp(args[0], "numa-cpu-mapping") != 0) {
William Dauchyec730982019-10-27 20:08:10 +01002440 ha_alert("parsing [%s:%d]: negation/default currently "
William Dauchy0fec3ab2019-10-27 20:08:11 +01002441 "supported only for options, log, busy-polling, "
Amaury Denoyelle0f50cb92021-03-26 18:50:33 +01002442 "set-dumpable, strict-limits, insecure-fork-wanted "
2443 "and numa-cpu-mapping.\n", file, linenum);
Willy Tarreau058e9072009-07-20 09:30:05 +02002444 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02002445 fatal++;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002446 }
2447
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01002448 /* detect section start */
2449 list_for_each_entry(ics, &sections, list) {
2450 if (strcmp(args[0], ics->section_name) == 0) {
2451 cursection = ics->section_name;
Christopher Faulet7805e2b2018-11-30 13:50:47 +01002452 pcs = cs;
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01002453 cs = ics;
Willy Tarreau51508052021-05-06 10:04:45 +02002454 free(global.cfg_curr_section);
2455 global.cfg_curr_section = strdup(*args[1] ? args[1] : args[0]);
Christopher Faulet037e3f82022-11-18 15:46:06 +01002456 check_section_position(args[0], file, linenum);
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01002457 break;
2458 }
Emeric Brun32da3c42010-09-23 18:39:19 +02002459 }
2460
Christopher Faulet7805e2b2018-11-30 13:50:47 +01002461 if (pcs && pcs->post_section_parser) {
Willy Tarreau32234e72020-06-16 17:14:33 +02002462 int status;
2463
2464 status = pcs->post_section_parser();
2465 err_code |= status;
2466 if (status & ERR_FATAL)
2467 fatal++;
2468
Christopher Faulet7805e2b2018-11-30 13:50:47 +01002469 if (err_code & ERR_ABORT)
2470 goto err;
Christopher Faulet7805e2b2018-11-30 13:50:47 +01002471 }
Christopher Fauletbf7a9592018-12-02 09:37:38 +01002472 pcs = NULL;
Christopher Faulet7805e2b2018-11-30 13:50:47 +01002473
William Lallemandd2ff56d2017-10-16 11:06:50 +02002474 if (!cs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002475 ha_alert("parsing [%s:%d]: unknown keyword '%s' out of section.\n", file, linenum, args[0]);
Willy Tarreau058e9072009-07-20 09:30:05 +02002476 err_code |= ERR_ALERT | ERR_FATAL;
Willy Tarreau32234e72020-06-16 17:14:33 +02002477 fatal++;
William Lallemandd2ff56d2017-10-16 11:06:50 +02002478 } else {
Willy Tarreau32234e72020-06-16 17:14:33 +02002479 int status;
2480
2481 status = cs->section_parser(file, linenum, args, kwm);
2482 err_code |= status;
2483 if (status & ERR_FATAL)
2484 fatal++;
2485
William Lallemandd2ff56d2017-10-16 11:06:50 +02002486 if (err_code & ERR_ABORT)
2487 goto err;
2488 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002489 }
William Lallemandd2ff56d2017-10-16 11:06:50 +02002490
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02002491 if (missing_lf != -1) {
Tim Duesterhusf92afb72020-08-18 22:00:04 +02002492 ha_alert("parsing [%s:%d]: Missing LF on last line, file might have been truncated at position %d.\n",
2493 file, linenum, (missing_lf + 1));
2494 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus2fd5bdb2020-06-22 22:57:45 +02002495 }
2496
Willy Tarreau51508052021-05-06 10:04:45 +02002497 ha_free(&global.cfg_curr_section);
Christopher Fauletbf7a9592018-12-02 09:37:38 +01002498 if (cs && cs->post_section_parser)
Christopher Faulet7805e2b2018-11-30 13:50:47 +01002499 err_code |= cs->post_section_parser();
William Lallemandd2ff56d2017-10-16 11:06:50 +02002500
Willy Tarreau4b103022021-02-12 17:59:10 +01002501 if (nested_cond_lvl) {
2502 ha_alert("parsing [%s:%d]: non-terminated '.if' block.\n", file, linenum);
2503 err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT;
2504 }
Willy Tarreau8a022d52021-04-27 20:29:11 +02002505
2506 if (*initial_cwd && chdir(initial_cwd) == -1) {
2507 ha_alert("Impossible to get back to initial directory '%s' : %s\n", initial_cwd, strerror(errno));
2508 err_code |= ERR_ALERT | ERR_FATAL;
2509 }
2510
William Lallemandd2ff56d2017-10-16 11:06:50 +02002511err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002512 ha_free(&cfg_scope);
Willy Tarreau6daf3432008-01-22 16:44:08 +01002513 cursection = NULL;
William Lallemand64e84512015-05-12 14:25:37 +02002514 free(thisline);
Willy Tarreau9e1758e2020-06-16 16:32:59 +02002515 free(outline);
Willy Tarreau51508052021-05-06 10:04:45 +02002516 global.cfg_curr_line = 0;
2517 global.cfg_curr_file = NULL;
2518
Willy Tarreauda543e12021-04-27 18:30:28 +02002519 if (f)
2520 fclose(f);
2521
Willy Tarreau058e9072009-07-20 09:30:05 +02002522 return err_code;
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002523}
2524
Amaury Denoyelleb09f4472021-12-15 09:48:39 +01002525#if defined(USE_THREAD) && defined USE_CPU_AFFINITY
2526#if defined(__linux__)
2527
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002528/* filter directory name of the pattern node<X> */
2529static int numa_filter(const struct dirent *dir)
2530{
2531 char *endptr;
2532
2533 /* dir name must start with "node" prefix */
2534 if (strncmp(dir->d_name, "node", 4))
2535 return 0;
2536
2537 /* dir name must be at least 5 characters long */
2538 if (!dir->d_name[4])
2539 return 0;
2540
2541 /* dir name must end with a numeric id */
2542 if (strtol(&dir->d_name[4], &endptr, 10) < 0 || *endptr)
2543 return 0;
2544
2545 /* all tests succeeded */
2546 return 1;
2547}
2548
2549/* Parse a linux cpu map string representing to a numeric cpu mask map
2550 * The cpu map string is a list of 4-byte hex strings separated by commas, with
2551 * most-significant byte first, one bit per cpu number.
2552 */
2553static void parse_cpumap(char *cpumap_str, struct hap_cpuset *cpu_set)
2554{
2555 unsigned long cpumap;
2556 char *start, *endptr, *comma;
2557 int i, j;
2558
2559 ha_cpuset_zero(cpu_set);
2560
2561 i = 0;
2562 do {
2563 /* reverse-search for a comma, parse the string after the comma
2564 * or at the beginning if no comma found
2565 */
2566 comma = strrchr(cpumap_str, ',');
2567 start = comma ? comma + 1 : cpumap_str;
2568
2569 cpumap = strtoul(start, &endptr, 16);
2570 for (j = 0; cpumap; cpumap >>= 1, ++j) {
2571 if (cpumap & 0x1)
2572 ha_cpuset_set(cpu_set, j + i * 32);
2573 }
2574
2575 if (comma)
2576 *comma = '\0';
2577 ++i;
2578 } while (comma);
2579}
2580
2581/* Read the first line of a file from <path> into the trash buffer.
2582 * Returns 0 on success, otherwise non-zero.
2583 */
2584static int read_file_to_trash(const char *path)
2585{
2586 FILE *file;
2587 int ret = 1;
2588
2589 file = fopen(path, "r");
2590 if (file) {
2591 if (fgets(trash.area, trash.size, file))
2592 ret = 0;
2593
2594 fclose(file);
2595 }
2596
2597 return ret;
2598}
2599
2600/* Inspect the cpu topology of the machine on startup. If a multi-socket
2601 * machine is detected, try to bind on the first node with active cpu. This is
2602 * done to prevent an impact on the overall performance when the topology of
2603 * the machine is unknown. This function is not called if one of the conditions
2604 * is met :
2605 * - a non-null nbthread directive is active
2606 * - a restrictive cpu-map directive is active
2607 * - a restrictive affinity is already applied, for example via taskset
2608 *
2609 * Returns the count of cpus selected. If no automatic binding was required or
2610 * an error occurred and the topology is unknown, 0 is returned.
2611 */
2612static int numa_detect_topology()
2613{
2614 struct dirent **node_dirlist;
2615 int node_dirlist_size;
2616
2617 struct hap_cpuset active_cpus, node_cpu_set;
2618 const char *parse_cpu_set_args[2];
2619 char cpumap_path[PATH_MAX];
2620 char *err = NULL;
2621
2622 /* node_cpu_set count is used as return value */
2623 ha_cpuset_zero(&node_cpu_set);
2624
2625 /* 1. count the sysfs node<X> directories */
Willy Tarreau07bf21c2021-04-23 19:09:16 +02002626 node_dirlist = NULL;
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002627 node_dirlist_size = scandir(NUMA_DETECT_SYSTEM_SYSFS_PATH"/node", &node_dirlist, numa_filter, alphasort);
2628 if (node_dirlist_size <= 1)
2629 goto free_scandir_entries;
2630
2631 /* 2. read and parse the list of currently online cpu */
2632 if (read_file_to_trash(NUMA_DETECT_SYSTEM_SYSFS_PATH"/cpu/online")) {
2633 ha_notice("Cannot read online CPUs list, will not try to refine binding\n");
2634 goto free_scandir_entries;
2635 }
2636
2637 parse_cpu_set_args[0] = trash.area;
2638 parse_cpu_set_args[1] = "\0";
2639 if (parse_cpu_set(parse_cpu_set_args, &active_cpus, 1, &err)) {
2640 ha_notice("Cannot read online CPUs list: '%s'. Will not try to refine binding\n", err);
2641 free(err);
2642 goto free_scandir_entries;
2643 }
2644
2645 /* 3. loop through nodes dirs and find the first one with active cpus */
2646 while (node_dirlist_size--) {
2647 const char *node = node_dirlist[node_dirlist_size]->d_name;
2648 ha_cpuset_zero(&node_cpu_set);
2649
2650 snprintf(cpumap_path, PATH_MAX, "%s/node/%s/cpumap",
2651 NUMA_DETECT_SYSTEM_SYSFS_PATH, node);
2652
2653 if (read_file_to_trash(cpumap_path)) {
2654 ha_notice("Cannot read CPUs list of '%s', will not select them to refine binding\n", node);
2655 free(node_dirlist[node_dirlist_size]);
2656 continue;
2657 }
2658
2659 parse_cpumap(trash.area, &node_cpu_set);
2660 ha_cpuset_and(&node_cpu_set, &active_cpus);
2661
2662 /* 5. set affinity on the first found node with active cpus */
2663 if (!ha_cpuset_count(&node_cpu_set)) {
2664 free(node_dirlist[node_dirlist_size]);
2665 continue;
2666 }
2667
2668 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));
2669 if (sched_setaffinity(getpid(), sizeof(node_cpu_set.cpuset), &node_cpu_set.cpuset) == -1) {
2670 ha_warning("Cannot set the cpu affinity for this multi-cpu machine\n");
2671
2672 /* clear the cpuset used as return value */
2673 ha_cpuset_zero(&node_cpu_set);
2674 }
2675
2676 free(node_dirlist[node_dirlist_size]);
2677 break;
2678 }
2679
2680 free_scandir_entries:
2681 while (node_dirlist_size-- > 0)
2682 free(node_dirlist[node_dirlist_size]);
2683 free(node_dirlist);
2684
2685 return ha_cpuset_count(&node_cpu_set);
2686}
Amaury Denoyelleb09f4472021-12-15 09:48:39 +01002687
David CARLIERf5d48f82021-12-06 11:00:10 +00002688#elif defined(__FreeBSD__)
2689static int numa_detect_topology()
2690{
2691 struct hap_cpuset node_cpu_set;
2692 int ndomains = 0, i;
2693 size_t len = sizeof(ndomains);
2694
2695 if (sysctlbyname("vm.ndomains", &ndomains, &len, NULL, 0) == -1) {
2696 ha_notice("Cannot assess the number of CPUs domains\n");
2697 return 0;
2698 }
2699
2700 BUG_ON(ndomains > MAXMEMDOM);
2701 ha_cpuset_zero(&node_cpu_set);
2702
2703 /*
2704 * We retrieve the first active valid CPU domain
2705 * with active cpu and binding it, we returns
2706 * the number of cpu from the said domain
2707 */
2708 for (i = 0; i < ndomains; i ++) {
2709 struct hap_cpuset dom;
2710 ha_cpuset_zero(&dom);
2711 if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_DOMAIN, i, sizeof(dom.cpuset), &dom.cpuset) == -1)
2712 continue;
2713
2714 if (!ha_cpuset_count(&dom))
2715 continue;
2716
2717 ha_cpuset_assign(&node_cpu_set, &dom);
2718
2719 ha_diag_warning("Multi-socket cpu detected, automatically binding on active CPUs of '%d' (%u active cpu(s))\n", i, ha_cpuset_count(&node_cpu_set));
2720 if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(node_cpu_set.cpuset), &node_cpu_set.cpuset) == -1) {
2721 ha_warning("Cannot set the cpu affinity for this multi-cpu machine\n");
2722
2723 /* clear the cpuset used as return value */
2724 ha_cpuset_zero(&node_cpu_set);
2725 }
2726 break;
2727 }
2728
2729 return ha_cpuset_count(&node_cpu_set);
2730}
2731
Amaury Denoyelleb09f4472021-12-15 09:48:39 +01002732#else
2733static int numa_detect_topology()
2734{
2735 return 0;
2736}
2737
2738#endif
2739#endif /* USE_THREAD && USE_CPU_AFFINITY */
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002740
Willy Tarreaubb925012009-07-23 13:36:36 +02002741/*
2742 * Returns the error code, 0 if OK, or any combination of :
2743 * - ERR_ABORT: must abort ASAP
2744 * - ERR_FATAL: we can continue parsing but not start the service
2745 * - ERR_WARN: a warning has been emitted
2746 * - ERR_ALERT: an alert has been emitted
2747 * Only the two first ones can stop processing, the two others are just
2748 * indicators.
2749 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002750int check_config_validity()
2751{
2752 int cfgerr = 0;
2753 struct proxy *curproxy = NULL;
Emeric Brun3b68b602022-08-18 15:53:21 +02002754 struct proxy *init_proxies_list = NULL;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002755 struct stktable *t;
Willy Tarreau915e1eb2009-06-22 15:48:36 +02002756 struct server *newsrv = NULL;
Willy Tarreaubb925012009-07-23 13:36:36 +02002757 int err_code = 0;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002758 unsigned int next_pxid = 1;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002759 struct bind_conf *bind_conf;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01002760 char *err;
William Lallemand48b4bb42017-10-23 14:36:34 +02002761 struct cfg_postparser *postparser;
Emeric Brun750fe792020-12-23 16:51:12 +01002762 struct resolvers *curr_resolvers = NULL;
Amaury Denoyelle3efee652021-03-08 17:31:39 +01002763 int i;
Frédéric Lécaille372508c2022-05-06 08:53:16 +02002764 int diag_no_cluster_secret = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002765
Willy Tarreau2a65ff02012-09-13 17:54:29 +02002766 bind_conf = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002767 /*
2768 * Now, check for the integrity of all that we have collected.
2769 */
2770
2771 /* will be needed further to delay some tasks */
Willy Tarreau55542642021-10-08 09:33:24 +02002772 clock_update_date(0,1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002773
Willy Tarreau193b8c62012-11-22 00:17:38 +01002774 if (!global.tune.max_http_hdr)
2775 global.tune.max_http_hdr = MAX_HTTP_HDR;
2776
2777 if (!global.tune.cookie_len)
2778 global.tune.cookie_len = CAPTURE_LEN;
2779
Stéphane Cottin23e9e932017-05-18 08:58:41 +02002780 if (!global.tune.requri_len)
2781 global.tune.requri_len = REQURI_LEN;
2782
Willy Tarreau149ab772019-01-26 14:27:06 +01002783 if (!global.nbthread) {
2784 /* nbthread not set, thus automatic. In this case, and only if
2785 * running on a single process, we enable the same number of
2786 * threads as the number of CPUs the process is bound to. This
2787 * allows to easily control the number of threads using taskset.
2788 */
2789 global.nbthread = 1;
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002790
Willy Tarreau149ab772019-01-26 14:27:06 +01002791#if defined(USE_THREAD)
Willy Tarreaub63dbb72021-06-11 16:50:29 +02002792 {
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002793 int numa_cores = 0;
Amaury Denoyelleb09f4472021-12-15 09:48:39 +01002794#if defined(USE_CPU_AFFINITY)
Amaury Denoyelle0f50cb92021-03-26 18:50:33 +01002795 if (global.numa_cpu_mapping && !thread_cpu_mask_forced())
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002796 numa_cores = numa_detect_topology();
2797#endif
2798 global.nbthread = numa_cores ? numa_cores :
2799 thread_cpus_enabled_at_boot;
Willy Tarreau400b3ae2022-12-08 08:04:46 +01002800
2801 /* Note that we cannot have more than 32 or 64 threads per group */
2802 if (!global.nbtgroups)
2803 global.nbtgroups = 1;
2804
2805 if (global.nbthread > MAX_THREADS_PER_GROUP * global.nbtgroups) {
2806 ha_diag_warning("nbthread not set, found %d CPUs, limiting to %d threads (maximum is %d per thread group). Please set nbthreads and/or increase thread-groups in the global section to silence this warning.\n",
2807 global.nbthread, MAX_THREADS_PER_GROUP * global.nbtgroups, MAX_THREADS_PER_GROUP);
2808 global.nbthread = MAX_THREADS_PER_GROUP * global.nbtgroups;
2809 }
Amaury Denoyelleb56a7c82021-03-26 18:20:47 +01002810 }
Willy Tarreau149ab772019-01-26 14:27:06 +01002811#endif
2812 }
2813
Willy Tarreauc33b9692021-09-22 12:07:23 +02002814 if (!global.nbtgroups)
2815 global.nbtgroups = 1;
2816
Willy Tarreaue6806eb2021-09-27 10:10:26 +02002817 if (thread_map_to_groups() < 0) {
2818 err_code |= ERR_ALERT | ERR_FATAL;
2819 goto out;
2820 }
2821
Willy Tarreaubafbe012017-11-24 17:34:44 +01002822 pool_head_requri = create_pool("requri", global.tune.requri_len , MEM_F_SHARED);
Emeric Brun96fd9262017-07-05 13:33:16 +02002823
Willy Tarreaubafbe012017-11-24 17:34:44 +01002824 pool_head_capture = create_pool("capture", global.tune.cookie_len, MEM_F_SHARED);
Willy Tarreau193b8c62012-11-22 00:17:38 +01002825
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01002826 /* Post initialisation of the users and groups lists. */
2827 err_code = userlist_postinit();
2828 if (err_code != ERR_NONE)
2829 goto out;
2830
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002831 /* first, we will invert the proxy list order */
2832 curproxy = NULL;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002833 while (proxies_list) {
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002834 struct proxy *next;
2835
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002836 next = proxies_list->next;
2837 proxies_list->next = curproxy;
2838 curproxy = proxies_list;
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002839 if (!next)
2840 break;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002841 proxies_list = next;
Willy Tarreau55bc0f82009-03-15 14:51:53 +01002842 }
2843
Emeric Brun3b68b602022-08-18 15:53:21 +02002844 /* starting to initialize the main proxies list */
2845 init_proxies_list = proxies_list;
2846
2847init_proxies_list_stage1:
2848 for (curproxy = init_proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreau55ea7572007-06-17 19:56:27 +02002849 struct switching_rule *rule;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002850 struct server_rule *srule;
Emeric Brunb982a3d2010-01-04 15:45:53 +01002851 struct sticking_rule *mrule;
Dragan Dosen1322d092015-09-22 16:05:32 +02002852 struct logsrv *tmplogsrv;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002853 unsigned int next_id;
2854
Willy Tarreaud3dbfd92021-08-20 10:15:40 +02002855 if (!(curproxy->cap & PR_CAP_INT) && curproxy->uuid < 0) {
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002856 /* proxy ID not set, use automatic numbering with first
Willy Tarreaud3dbfd92021-08-20 10:15:40 +02002857 * spare entry starting with next_pxid. We don't assign
2858 * numbers for internal proxies as they may depend on
2859 * build or config options and we don't want them to
2860 * possibly reuse existing IDs.
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002861 */
2862 next_pxid = get_next_id(&used_proxy_id, next_pxid);
2863 curproxy->conf.id.key = curproxy->uuid = next_pxid;
2864 eb32_insert(&used_proxy_id, &curproxy->conf.id);
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02002865 }
Krzysztof Piotr Oledzkidf5cb9f2010-02-05 20:58:27 +01002866
Willy Tarreau32b51cd2021-08-26 15:59:44 +02002867 if (curproxy->mode == PR_MODE_HTTP && global.tune.bufsize >= (256 << 20) && ONLY_ONCE()) {
2868 ha_alert("global.tune.bufsize must be below 256 MB when HTTP is in use (current value = %d).\n",
2869 global.tune.bufsize);
2870 cfgerr++;
2871 }
2872
Willy Tarreaud3dbfd92021-08-20 10:15:40 +02002873 /* next IDs are shifted even if the proxy is disabled, this
2874 * guarantees that a proxy that is temporarily disabled in the
2875 * configuration doesn't cause a renumbering. Internal proxies
2876 * that are not assigned a static ID must never shift the IDs
2877 * either since they may appear in any order (Lua, logs, etc).
2878 * The GLOBAL proxy that carries the stats socket has its ID
2879 * forced to zero.
2880 */
2881 if (curproxy->uuid >= 0)
2882 next_pxid++;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002883
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02002884 if (curproxy->flags & PR_FL_DISABLED) {
Willy Tarreau02b092f2020-10-07 18:36:54 +02002885 /* ensure we don't keep listeners uselessly bound. We
2886 * can't disable their listeners yet (fdtab not
2887 * allocated yet) but let's skip them.
2888 */
Dragan Dosen7d61a332019-05-07 14:16:18 +02002889 if (curproxy->table) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002890 ha_free(&curproxy->table->peers.name);
Dragan Dosen7d61a332019-05-07 14:16:18 +02002891 curproxy->table->peers.p = NULL;
2892 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002893 continue;
2894 }
2895
Christopher Fauletff556272021-10-13 11:04:10 +02002896 /* The current proxy is referencing a default proxy. We must
2897 * finalize its config, but only once. If the default proxy is
2898 * ready (PR_FL_READY) it means it was already fully configured.
2899 */
2900 if (curproxy->defpx) {
2901 if (!(curproxy->defpx->flags & PR_FL_READY)) {
Christopher Fauletee08d6c2021-10-13 15:40:15 +02002902 /* check validity for 'tcp-request' layer 4/5/6/7 rules */
2903 cfgerr += check_action_rules(&curproxy->defpx->tcp_req.l4_rules, curproxy->defpx, &err_code);
2904 cfgerr += check_action_rules(&curproxy->defpx->tcp_req.l5_rules, curproxy->defpx, &err_code);
2905 cfgerr += check_action_rules(&curproxy->defpx->tcp_req.inspect_rules, curproxy->defpx, &err_code);
2906 cfgerr += check_action_rules(&curproxy->defpx->tcp_rep.inspect_rules, curproxy->defpx, &err_code);
2907 cfgerr += check_action_rules(&curproxy->defpx->http_req_rules, curproxy->defpx, &err_code);
2908 cfgerr += check_action_rules(&curproxy->defpx->http_res_rules, curproxy->defpx, &err_code);
2909 cfgerr += check_action_rules(&curproxy->defpx->http_after_res_rules, curproxy->defpx, &err_code);
2910
Christopher Fauletff556272021-10-13 11:04:10 +02002911 err = NULL;
2912 i = smp_resolve_args(curproxy->defpx, &err);
2913 cfgerr += i;
2914 if (i) {
2915 indent_msg(&err, 8);
2916 ha_alert("%s%s\n", i > 1 ? "multiple argument resolution errors:" : "", err);
2917 ha_free(&err);
2918 }
2919 else
2920 cfgerr += acl_find_targets(curproxy->defpx);
2921
2922 /* default proxy is now ready. Set the right FE/BE capabilities */
2923 curproxy->defpx->flags |= PR_FL_READY;
2924 }
2925 }
2926
Willy Tarreau3d209582014-05-09 17:06:11 +02002927 /* check and reduce the bind-proc of each listener */
2928 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
2929 unsigned long mask;
Willy Tarreau01cac3f2021-10-12 08:47:54 +02002930 struct listener *li;
Willy Tarreau3d209582014-05-09 17:06:11 +02002931
Willy Tarreau45a66cc2017-11-24 11:28:00 +01002932 /* HTTP frontends with "h2" as ALPN/NPN will work in
2933 * HTTP/2 and absolutely require buffers 16kB or larger.
2934 */
2935#ifdef USE_OPENSSL
2936 if (curproxy->mode == PR_MODE_HTTP && global.tune.bufsize < 16384) {
2937#ifdef OPENSSL_NPN_NEGOTIATED
2938 /* check NPN */
Willy Tarreau4db49c02018-11-11 10:36:25 +01002939 if (bind_conf->ssl_conf.npn_str && strstr(bind_conf->ssl_conf.npn_str, "\002h2")) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02002940 ha_alert("HTTP frontend '%s' enables HTTP/2 via NPN at [%s:%d], so global.tune.bufsize must be at least 16384 bytes (%d now).\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01002941 curproxy->id, bind_conf->file, bind_conf->line, global.tune.bufsize);
Willy Tarreau45a66cc2017-11-24 11:28:00 +01002942 cfgerr++;
2943 }
2944#endif
2945#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2946 /* check ALPN */
Willy Tarreau4db49c02018-11-11 10:36:25 +01002947 if (bind_conf->ssl_conf.alpn_str && strstr(bind_conf->ssl_conf.alpn_str, "\002h2")) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02002948 ha_alert("HTTP frontend '%s' enables HTTP/2 via ALPN at [%s:%d], so global.tune.bufsize must be at least 16384 bytes (%d now).\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01002949 curproxy->id, bind_conf->file, bind_conf->line, global.tune.bufsize);
Willy Tarreau45a66cc2017-11-24 11:28:00 +01002950 cfgerr++;
2951 }
2952#endif
2953 } /* HTTP && bufsize < 16384 */
2954#endif
2955
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002956 /* detect and address thread affinity inconsistencies */
Willy Tarreaue3f4d742021-09-29 19:02:25 +02002957 err = NULL;
Willy Tarreau01cac3f2021-10-12 08:47:54 +02002958 if (thread_resolve_group_mask(bind_conf->bind_tgroup, bind_conf->bind_thread,
2959 &bind_conf->bind_tgroup, &bind_conf->bind_thread, &err) < 0) {
Willy Tarreaue3f4d742021-09-29 19:02:25 +02002960 ha_alert("Proxy '%s': %s in 'bind %s' at [%s:%d].\n",
2961 curproxy->id, err, bind_conf->arg, bind_conf->file, bind_conf->line);
2962 free(err);
2963 cfgerr++;
Willy Tarreaud0b73bc2022-06-28 08:30:43 +02002964 } else if (!((mask = bind_conf->bind_thread) & ha_tgroup_info[bind_conf->bind_tgroup-1].threads_enabled)) {
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002965 unsigned long new_mask = 0;
Willy Tarreaud0b73bc2022-06-28 08:30:43 +02002966 ulong thr_mask = ha_tgroup_info[bind_conf->bind_tgroup-1].threads_enabled;
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002967
2968 while (mask) {
Willy Tarreaud0b73bc2022-06-28 08:30:43 +02002969 new_mask |= mask & thr_mask;
2970 mask >>= ha_tgroup_info[bind_conf->bind_tgroup-1].count;
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002971 }
2972
Willy Tarreau01cac3f2021-10-12 08:47:54 +02002973 bind_conf->bind_thread = new_mask;
Willy Tarreaud0b73bc2022-06-28 08:30:43 +02002974 ha_warning("Proxy '%s': the thread range specified on the 'thread' directive of 'bind %s' at [%s:%d] only refers to thread numbers out of the range supported by thread group %d (%d). The thread numbers were remapped to existing threads instead (mask 0x%lx).\n",
2975 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line,
2976 bind_conf->bind_tgroup, ha_tgroup_info[bind_conf->bind_tgroup-1].count, new_mask);
Willy Tarreauc477b6f2018-07-27 18:07:41 +02002977 }
Willy Tarreau01cac3f2021-10-12 08:47:54 +02002978
2979 /* apply thread masks and groups to all receivers */
2980 list_for_each_entry(li, &bind_conf->listeners, by_bind) {
Willy Tarreau6dfbef42021-10-12 15:23:03 +02002981 if (bind_conf->settings.shards <= 1) {
2982 li->rx.bind_thread = bind_conf->bind_thread;
2983 li->rx.bind_tgroup = bind_conf->bind_tgroup;
2984 } else {
2985 struct listener *new_li;
2986 int shard, shards, todo, done, bit;
2987 ulong mask;
2988
2989 shards = bind_conf->settings.shards;
2990 todo = my_popcountl(bind_conf->bind_thread);
2991
2992 /* no more shards than total threads */
2993 if (shards > todo)
2994 shards = todo;
2995
2996 shard = done = bit = 0;
2997 new_li = li;
2998
2999 while (1) {
3000 mask = 0;
3001 while (done < todo) {
3002 /* enlarge mask to cover next bit of bind_thread */
3003 while (!(bind_conf->bind_thread & (1UL << bit)))
3004 bit++;
3005 mask |= (1UL << bit);
3006 bit++;
3007 done += shards;
3008 }
3009
3010 new_li->rx.bind_thread = bind_conf->bind_thread & mask;
3011 new_li->rx.bind_tgroup = bind_conf->bind_tgroup;
3012 done -= todo;
3013
3014 shard++;
3015 if (shard >= shards)
3016 break;
3017
3018 /* create another listener for new shards */
3019 new_li = clone_listener(li);
3020 if (!new_li) {
3021 ha_alert("Out of memory while trying to allocate extra listener for shard %d in %s %s\n",
3022 shard, proxy_type_str(curproxy), curproxy->id);
3023 cfgerr++;
3024 err_code |= ERR_FATAL | ERR_ALERT;
3025 goto out;
3026 }
3027 }
3028 }
Willy Tarreau01cac3f2021-10-12 08:47:54 +02003029 }
Willy Tarreau3d209582014-05-09 17:06:11 +02003030 }
3031
Willy Tarreauff01a212009-03-15 13:46:16 +01003032 switch (curproxy->mode) {
Willy Tarreauff01a212009-03-15 13:46:16 +01003033 case PR_MODE_TCP:
Willy Tarreau915e1eb2009-06-22 15:48:36 +02003034 cfgerr += proxy_cfg_ensure_no_http(curproxy);
Willy Tarreauff01a212009-03-15 13:46:16 +01003035 break;
3036
3037 case PR_MODE_HTTP:
Willy Tarreau25320b22013-03-24 07:22:08 +01003038 curproxy->http_needed = 1;
Willy Tarreauff01a212009-03-15 13:46:16 +01003039 break;
William Lallemandcf62f7e2018-10-26 14:47:40 +02003040
3041 case PR_MODE_CLI:
3042 cfgerr += proxy_cfg_ensure_no_http(curproxy);
3043 break;
Emeric Brun3b68b602022-08-18 15:53:21 +02003044
Emeric Brun54932b42020-07-07 09:43:24 +02003045 case PR_MODE_SYSLOG:
Emeric Brun3b68b602022-08-18 15:53:21 +02003046 /* this mode is initialized as the classic tcp proxy */
3047 cfgerr += proxy_cfg_ensure_no_http(curproxy);
3048 break;
3049
Willy Tarreaua389c9e2020-10-07 17:49:42 +02003050 case PR_MODE_PEERS:
Emeric Brun54932b42020-07-07 09:43:24 +02003051 case PR_MODES:
3052 /* should not happen, bug gcc warn missing switch statement */
Emeric Brun3b68b602022-08-18 15:53:21 +02003053 ha_alert("%s '%s' cannot initialize this proxy mode (peers) in this way. 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 +02003054 proxy_type_str(curproxy), curproxy->id);
3055 cfgerr++;
3056 break;
Willy Tarreauff01a212009-03-15 13:46:16 +01003057 }
3058
William Lallemand2c04a5a2021-08-13 15:21:12 +02003059 if (!(curproxy->cap & PR_CAP_INT) && (curproxy->cap & PR_CAP_FE) && LIST_ISEMPTY(&curproxy->conf.listeners)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003060 ha_warning("%s '%s' has no 'bind' directive. Please declare it as a backend if this was intended.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003061 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauf3934b82015-08-11 11:36:45 +02003062 err_code |= ERR_WARN;
3063 }
3064
Willy Tarreau77e0dae2020-10-14 15:44:27 +02003065 if (curproxy->cap & PR_CAP_BE) {
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003066 if (curproxy->lbprm.algo & BE_LB_KIND) {
Willy Tarreau3cd9af22009-03-15 14:06:41 +01003067 if (curproxy->options & PR_O_TRANSP) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003068 ha_alert("%s '%s' cannot use both transparent and balance mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003069 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau3cd9af22009-03-15 14:06:41 +01003070 cfgerr++;
3071 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003072#ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
Willy Tarreau3cd9af22009-03-15 14:06:41 +01003073 else if (curproxy->srv == NULL) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003074 ha_alert("%s '%s' needs at least 1 server in balance mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003075 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau3cd9af22009-03-15 14:06:41 +01003076 cfgerr++;
3077 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003078#endif
Willy Tarreau1620ec32011-08-06 17:05:02 +02003079 else if (curproxy->options & PR_O_DISPATCH) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003080 ha_warning("dispatch address of %s '%s' will be ignored in balance mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003081 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubb925012009-07-23 13:36:36 +02003082 err_code |= ERR_WARN;
Willy Tarreau3cd9af22009-03-15 14:06:41 +01003083 }
3084 }
Willy Tarreau25241232021-07-18 19:18:56 +02003085 else if (!(curproxy->options & (PR_O_TRANSP | PR_O_DISPATCH))) {
Willy Tarreau3cd9af22009-03-15 14:06:41 +01003086 /* If no LB algo is set in a backend, and we're not in
3087 * transparent mode, dispatch mode nor proxy mode, we
3088 * want to use balance roundrobin by default.
3089 */
3090 curproxy->lbprm.algo &= ~BE_LB_ALGO;
3091 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003092 }
3093 }
Willy Tarreau193cf932007-09-17 10:17:23 +02003094
Willy Tarreau1620ec32011-08-06 17:05:02 +02003095 if (curproxy->options & PR_O_DISPATCH)
Willy Tarreau25241232021-07-18 19:18:56 +02003096 curproxy->options &= ~PR_O_TRANSP;
Willy Tarreau1620ec32011-08-06 17:05:02 +02003097 else if (curproxy->options & PR_O_TRANSP)
Willy Tarreau25241232021-07-18 19:18:56 +02003098 curproxy->options &= ~PR_O_DISPATCH;
Willy Tarreau82936582007-11-30 15:20:09 +01003099
Christopher Faulete5870d82020-04-15 11:32:03 +02003100 if ((curproxy->tcpcheck_rules.flags & TCPCHK_RULES_UNUSED_HTTP_RS)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003101 ha_warning("%s '%s' uses http-check rules without 'option httpchk', so the rules are ignored.\n",
Christopher Faulete5870d82020-04-15 11:32:03 +02003102 proxy_type_str(curproxy), curproxy->id);
3103 err_code |= ERR_WARN;
3104 }
3105
3106 if ((curproxy->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK &&
Christopher Fauletc0fcbe42020-06-03 19:00:42 +02003107 (curproxy->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) != TCPCHK_RULES_HTTP_CHK) {
Willy Tarreau1620ec32011-08-06 17:05:02 +02003108 if (curproxy->options & PR_O_DISABLE404) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003109 ha_warning("'%s' will be ignored for %s '%s' (requires 'option httpchk').\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003110 "disable-on-404", proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1620ec32011-08-06 17:05:02 +02003111 err_code |= ERR_WARN;
3112 curproxy->options &= ~PR_O_DISABLE404;
3113 }
3114 if (curproxy->options2 & PR_O2_CHK_SNDST) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003115 ha_warning("'%s' will be ignored for %s '%s' (requires 'option httpchk').\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003116 "send-state", proxy_type_str(curproxy), curproxy->id);
Willy Tarreau1620ec32011-08-06 17:05:02 +02003117 err_code |= ERR_WARN;
3118 curproxy->options &= ~PR_O2_CHK_SNDST;
3119 }
Willy Tarreauef781042010-01-27 11:53:01 +01003120 }
3121
Simon Horman98637e52014-06-20 12:30:16 +09003122 if ((curproxy->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
3123 if (!global.external_check) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003124 ha_alert("Proxy '%s' : '%s' unable to find required 'global.external-check'.\n",
3125 curproxy->id, "option external-check");
Simon Horman98637e52014-06-20 12:30:16 +09003126 cfgerr++;
3127 }
3128 if (!curproxy->check_command) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003129 ha_alert("Proxy '%s' : '%s' unable to find required 'external-check command'.\n",
3130 curproxy->id, "option external-check");
Simon Horman98637e52014-06-20 12:30:16 +09003131 cfgerr++;
3132 }
Willy Tarreaud96f1122019-12-03 07:07:36 +01003133 if (!(global.tune.options & GTUNE_INSECURE_FORK)) {
3134 ha_warning("Proxy '%s' : 'insecure-fork-wanted' not enabled in the global section, '%s' will likely fail.\n",
3135 curproxy->id, "option external-check");
3136 err_code |= ERR_WARN;
3137 }
Simon Horman98637e52014-06-20 12:30:16 +09003138 }
3139
Simon Horman64e34162015-02-06 11:11:57 +09003140 if (curproxy->email_alert.set) {
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003141 if (!(curproxy->email_alert.mailers.name && curproxy->email_alert.from && curproxy->email_alert.to)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003142 ha_warning("'email-alert' will be ignored for %s '%s' (the presence any of "
Christopher Faulet767a84b2017-11-24 16:50:31 +01003143 "'email-alert from', 'email-alert level' 'email-alert mailers', "
3144 "'email-alert myhostname', or 'email-alert to' "
3145 "requires each of 'email-alert from', 'email-alert mailers' and 'email-alert to' "
3146 "to be present).\n",
3147 proxy_type_str(curproxy), curproxy->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003148 err_code |= ERR_WARN;
3149 free_email_alert(curproxy);
3150 }
3151 if (!curproxy->email_alert.myhostname)
Cyril Bontée22bfd62015-12-04 03:07:07 +01003152 curproxy->email_alert.myhostname = strdup(hostname);
Simon Horman9dc49962015-01-30 11:22:59 +09003153 }
3154
Simon Horman98637e52014-06-20 12:30:16 +09003155 if (curproxy->check_command) {
3156 int clear = 0;
3157 if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_EXT_CHK) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003158 ha_warning("'%s' will be ignored for %s '%s' (requires 'option external-check').\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003159 "external-check command", proxy_type_str(curproxy), curproxy->id);
Simon Horman98637e52014-06-20 12:30:16 +09003160 err_code |= ERR_WARN;
3161 clear = 1;
3162 }
3163 if (curproxy->check_command[0] != '/' && !curproxy->check_path) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003164 ha_alert("Proxy '%s': '%s' does not have a leading '/' and 'external-check path' is not set.\n",
3165 curproxy->id, "external-check command");
Simon Horman98637e52014-06-20 12:30:16 +09003166 cfgerr++;
3167 }
3168 if (clear) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003169 ha_free(&curproxy->check_command);
Simon Horman98637e52014-06-20 12:30:16 +09003170 }
3171 }
3172
3173 if (curproxy->check_path) {
3174 if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_EXT_CHK) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003175 ha_warning("'%s' will be ignored for %s '%s' (requires 'option external-check').\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003176 "external-check path", proxy_type_str(curproxy), curproxy->id);
Simon Horman98637e52014-06-20 12:30:16 +09003177 err_code |= ERR_WARN;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003178 ha_free(&curproxy->check_path);
Simon Horman98637e52014-06-20 12:30:16 +09003179 }
3180 }
3181
Willy Tarreau5fdfb912007-01-01 23:11:07 +01003182 /* if a default backend was specified, let's find it */
3183 if (curproxy->defbe.name) {
3184 struct proxy *target;
3185
Willy Tarreauafb39922015-05-26 12:04:09 +02003186 target = proxy_be_by_name(curproxy->defbe.name);
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +01003187 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003188 ha_alert("Proxy '%s': unable to find required default_backend: '%s'.\n",
3189 curproxy->id, curproxy->defbe.name);
Willy Tarreau5fdfb912007-01-01 23:11:07 +01003190 cfgerr++;
3191 } else if (target == curproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003192 ha_alert("Proxy '%s': loop detected for default_backend: '%s'.\n",
3193 curproxy->id, curproxy->defbe.name);
Willy Tarreaubb925012009-07-23 13:36:36 +02003194 cfgerr++;
Willy Tarreauafb39922015-05-26 12:04:09 +02003195 } else if (target->mode != curproxy->mode &&
3196 !(curproxy->mode == PR_MODE_TCP && target->mode == PR_MODE_HTTP)) {
3197
Christopher Faulet767a84b2017-11-24 16:50:31 +01003198 ha_alert("%s %s '%s' (%s:%d) tries to use incompatible %s %s '%s' (%s:%d) as its default backend (see 'mode').\n",
3199 proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
3200 curproxy->conf.file, curproxy->conf.line,
3201 proxy_mode_str(target->mode), proxy_type_str(target), target->id,
3202 target->conf.file, target->conf.line);
Willy Tarreauafb39922015-05-26 12:04:09 +02003203 cfgerr++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01003204 } else {
3205 free(curproxy->defbe.name);
3206 curproxy->defbe.be = target;
Willy Tarreauff678132012-02-13 14:32:34 +01003207 /* Emit a warning if this proxy also has some servers */
3208 if (curproxy->srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003209 ha_warning("In proxy '%s', the 'default_backend' rule always has precedence over the servers, which will never be used.\n",
3210 curproxy->id);
Willy Tarreauff678132012-02-13 14:32:34 +01003211 err_code |= ERR_WARN;
3212 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003213 }
3214 }
3215
Willy Tarreau55ea7572007-06-17 19:56:27 +02003216 /* find the target proxy for 'use_backend' rules */
3217 list_for_each_entry(rule, &curproxy->switching_rules, list) {
Willy Tarreau55ea7572007-06-17 19:56:27 +02003218 struct proxy *target;
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01003219 struct logformat_node *node;
3220 char *pxname;
3221
3222 /* Try to parse the string as a log format expression. If the result
3223 * of the parsing is only one entry containing a simple string, then
3224 * it's a standard string corresponding to a static rule, thus the
3225 * parsing is cancelled and be.name is restored to be resolved.
3226 */
3227 pxname = rule->be.name;
3228 LIST_INIT(&rule->be.expr);
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +01003229 curproxy->conf.args.ctx = ARGC_UBK;
3230 curproxy->conf.args.file = rule->file;
3231 curproxy->conf.args.line = rule->line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003232 err = NULL;
3233 if (!parse_logformat_string(pxname, curproxy, &rule->be.expr, 0, SMP_VAL_FE_HRQ_HDR, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003234 ha_alert("Parsing [%s:%d]: failed to parse use_backend rule '%s' : %s.\n",
3235 rule->file, rule->line, pxname, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003236 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01003237 cfgerr++;
3238 continue;
3239 }
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01003240 node = LIST_NEXT(&rule->be.expr, struct logformat_node *, list);
3241
3242 if (!LIST_ISEMPTY(&rule->be.expr)) {
3243 if (node->type != LOG_FMT_TEXT || node->list.n != &rule->be.expr) {
3244 rule->dynamic = 1;
3245 free(pxname);
3246 continue;
3247 }
Christopher Fauletf82ea4a2020-05-07 15:59:33 +02003248 /* Only one element in the list, a simple string: free the expression and
3249 * fall back to static rule
3250 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003251 LIST_DELETE(&node->list);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01003252 free(node->arg);
3253 free(node);
3254 }
3255
3256 rule->dynamic = 0;
3257 rule->be.name = pxname;
Willy Tarreau55ea7572007-06-17 19:56:27 +02003258
Willy Tarreauafb39922015-05-26 12:04:09 +02003259 target = proxy_be_by_name(rule->be.name);
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +01003260 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003261 ha_alert("Proxy '%s': unable to find required use_backend: '%s'.\n",
3262 curproxy->id, rule->be.name);
Willy Tarreau55ea7572007-06-17 19:56:27 +02003263 cfgerr++;
3264 } else if (target == curproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003265 ha_alert("Proxy '%s': loop detected for use_backend: '%s'.\n",
3266 curproxy->id, rule->be.name);
Willy Tarreau55ea7572007-06-17 19:56:27 +02003267 cfgerr++;
Willy Tarreauafb39922015-05-26 12:04:09 +02003268 } else if (target->mode != curproxy->mode &&
3269 !(curproxy->mode == PR_MODE_TCP && target->mode == PR_MODE_HTTP)) {
3270
Christopher Faulet767a84b2017-11-24 16:50:31 +01003271 ha_alert("%s %s '%s' (%s:%d) tries to use incompatible %s %s '%s' (%s:%d) in a 'use_backend' rule (see 'mode').\n",
3272 proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
3273 curproxy->conf.file, curproxy->conf.line,
3274 proxy_mode_str(target->mode), proxy_type_str(target), target->id,
3275 target->conf.file, target->conf.line);
Willy Tarreauafb39922015-05-26 12:04:09 +02003276 cfgerr++;
Willy Tarreau55ea7572007-06-17 19:56:27 +02003277 } else {
Willy Tarreau35cd7342021-02-26 20:51:47 +01003278 ha_free(&rule->be.name);
Willy Tarreau55ea7572007-06-17 19:56:27 +02003279 rule->be.backend = target;
3280 }
Christopher Faulet581db2b2021-03-26 10:02:46 +01003281 err_code |= warnif_tcp_http_cond(curproxy, rule->cond);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02003282 }
3283
Willy Tarreau64ab6072014-09-16 12:17:36 +02003284 /* find the target server for 'use_server' rules */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02003285 list_for_each_entry(srule, &curproxy->server_rules, list) {
Jerome Magnin824186b2020-03-29 09:37:12 +02003286 struct server *target;
3287 struct logformat_node *node;
3288 char *server_name;
3289
3290 /* We try to parse the string as a log format expression. If the result of the parsing
3291 * is only one entry containing a single string, then it's a standard string corresponding
3292 * to a static rule, thus the parsing is cancelled and we fall back to setting srv.ptr.
3293 */
3294 server_name = srule->srv.name;
3295 LIST_INIT(&srule->expr);
3296 curproxy->conf.args.ctx = ARGC_USRV;
3297 err = NULL;
3298 if (!parse_logformat_string(server_name, curproxy, &srule->expr, 0, SMP_VAL_FE_HRQ_HDR, &err)) {
3299 ha_alert("Parsing [%s:%d]; use-server rule failed to parse log-format '%s' : %s.\n",
3300 srule->file, srule->line, server_name, err);
3301 free(err);
3302 cfgerr++;
3303 continue;
3304 }
3305 node = LIST_NEXT(&srule->expr, struct logformat_node *, list);
3306
3307 if (!LIST_ISEMPTY(&srule->expr)) {
3308 if (node->type != LOG_FMT_TEXT || node->list.n != &srule->expr) {
3309 srule->dynamic = 1;
3310 free(server_name);
3311 continue;
3312 }
Christopher Fauletf82ea4a2020-05-07 15:59:33 +02003313 /* Only one element in the list, a simple string: free the expression and
3314 * fall back to static rule
3315 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003316 LIST_DELETE(&node->list);
Jerome Magnin824186b2020-03-29 09:37:12 +02003317 free(node->arg);
3318 free(node);
3319 }
3320
3321 srule->dynamic = 0;
3322 srule->srv.name = server_name;
3323 target = findserver(curproxy, srule->srv.name);
Christopher Faulet581db2b2021-03-26 10:02:46 +01003324 err_code |= warnif_tcp_http_cond(curproxy, srule->cond);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02003325
3326 if (!target) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003327 ha_alert("%s '%s' : unable to find server '%s' referenced in a 'use-server' rule.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003328 proxy_type_str(curproxy), curproxy->id, srule->srv.name);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02003329 cfgerr++;
3330 continue;
3331 }
Willy Tarreau35cd7342021-02-26 20:51:47 +01003332 ha_free(&srule->srv.name);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02003333 srule->srv.ptr = target;
Amaury Denoyelle06269612021-08-23 14:05:07 +02003334 target->flags |= SRV_F_NON_PURGEABLE;
Willy Tarreau55ea7572007-06-17 19:56:27 +02003335 }
3336
Emeric Brunb982a3d2010-01-04 15:45:53 +01003337 /* find the target table for 'stick' rules */
3338 list_for_each_entry(mrule, &curproxy->sticking_rules, list) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003339 struct stktable *target;
Emeric Brunb982a3d2010-01-04 15:45:53 +01003340
Emeric Brun1d33b292010-01-04 15:47:17 +01003341 curproxy->be_req_ana |= AN_REQ_STICKING_RULES;
3342 if (mrule->flags & STK_IS_STORE)
3343 curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
3344
Emeric Brunb982a3d2010-01-04 15:45:53 +01003345 if (mrule->table.name)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003346 target = stktable_find_by_name(mrule->table.name);
Emeric Brunb982a3d2010-01-04 15:45:53 +01003347 else
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003348 target = curproxy->table;
Emeric Brunb982a3d2010-01-04 15:45:53 +01003349
3350 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003351 ha_alert("Proxy '%s': unable to find stick-table '%s'.\n",
Willy Tarreau508d2322020-01-24 07:19:34 +01003352 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01003353 cfgerr++;
3354 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003355 else if (!stktable_compatible_sample(mrule->expr, target->type)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003356 ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
3357 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01003358 cfgerr++;
3359 }
3360 else {
Willy Tarreau35cd7342021-02-26 20:51:47 +01003361 ha_free(&mrule->table.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003362 mrule->table.t = target;
Emeric Brunc64a2a32021-06-30 18:01:02 +02003363 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL, NULL);
3364 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_KEY, NULL, NULL);
Frédéric Lécaille9c3a0ce2019-09-02 14:02:28 +02003365 if (!in_proxies_list(target->proxies_list, curproxy)) {
3366 curproxy->next_stkt_ref = target->proxies_list;
3367 target->proxies_list = curproxy;
3368 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01003369 }
Christopher Faulet581db2b2021-03-26 10:02:46 +01003370 err_code |= warnif_tcp_http_cond(curproxy, mrule->cond);
Emeric Brunb982a3d2010-01-04 15:45:53 +01003371 }
3372
3373 /* find the target table for 'store response' rules */
3374 list_for_each_entry(mrule, &curproxy->storersp_rules, list) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003375 struct stktable *target;
Emeric Brunb982a3d2010-01-04 15:45:53 +01003376
Emeric Brun1d33b292010-01-04 15:47:17 +01003377 curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
3378
Emeric Brunb982a3d2010-01-04 15:45:53 +01003379 if (mrule->table.name)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003380 target = stktable_find_by_name(mrule->table.name);
Emeric Brunb982a3d2010-01-04 15:45:53 +01003381 else
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003382 target = curproxy->table;
Emeric Brunb982a3d2010-01-04 15:45:53 +01003383
3384 if (!target) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003385 ha_alert("Proxy '%s': unable to find store table '%s'.\n",
Willy Tarreau508d2322020-01-24 07:19:34 +01003386 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01003387 cfgerr++;
3388 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003389 else if (!stktable_compatible_sample(mrule->expr, target->type)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003390 ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
3391 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
Emeric Brunb982a3d2010-01-04 15:45:53 +01003392 cfgerr++;
3393 }
3394 else {
Willy Tarreau35cd7342021-02-26 20:51:47 +01003395 ha_free(&mrule->table.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003396 mrule->table.t = target;
Emeric Brunc64a2a32021-06-30 18:01:02 +02003397 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL, NULL);
3398 stktable_alloc_data_type(target, STKTABLE_DT_SERVER_KEY, NULL, NULL);
Frédéric Lécaille9c3a0ce2019-09-02 14:02:28 +02003399 if (!in_proxies_list(target->proxies_list, curproxy)) {
3400 curproxy->next_stkt_ref = target->proxies_list;
3401 target->proxies_list = curproxy;
3402 }
Emeric Brunb982a3d2010-01-04 15:45:53 +01003403 }
3404 }
3405
Christopher Faulet42c6cf92021-03-25 17:19:04 +01003406 /* check validity for 'tcp-request' layer 4/5/6/7 rules */
3407 cfgerr += check_action_rules(&curproxy->tcp_req.l4_rules, curproxy, &err_code);
3408 cfgerr += check_action_rules(&curproxy->tcp_req.l5_rules, curproxy, &err_code);
3409 cfgerr += check_action_rules(&curproxy->tcp_req.inspect_rules, curproxy, &err_code);
3410 cfgerr += check_action_rules(&curproxy->tcp_rep.inspect_rules, curproxy, &err_code);
3411 cfgerr += check_action_rules(&curproxy->http_req_rules, curproxy, &err_code);
3412 cfgerr += check_action_rules(&curproxy->http_res_rules, curproxy, &err_code);
3413 cfgerr += check_action_rules(&curproxy->http_after_res_rules, curproxy, &err_code);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003414
Christopher Faulet5eef0182021-03-31 17:13:49 +02003415 /* Warn is a switch-mode http is used on a TCP listener with servers but no backend */
3416 if (!curproxy->defbe.name && LIST_ISEMPTY(&curproxy->switching_rules) && curproxy->srv) {
3417 if ((curproxy->options & PR_O_HTTP_UPG) && curproxy->mode == PR_MODE_TCP)
3418 ha_warning("Proxy '%s' : 'switch-mode http' configured for a %s %s with no backend. "
3419 "Incoming connections upgraded to HTTP cannot be routed to TCP servers\n",
3420 curproxy->id, proxy_mode_str(curproxy->mode), proxy_type_str(curproxy));
3421 }
3422
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003423 if (curproxy->table && curproxy->table->peers.name) {
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003424 struct peers *curpeers;
Emeric Brun32da3c42010-09-23 18:39:19 +02003425
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003426 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003427 if (strcmp(curpeers->id, curproxy->table->peers.name) == 0) {
Willy Tarreau35cd7342021-02-26 20:51:47 +01003428 ha_free(&curproxy->table->peers.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003429 curproxy->table->peers.p = curpeers;
Emeric Brun32da3c42010-09-23 18:39:19 +02003430 break;
3431 }
3432 }
3433
3434 if (!curpeers) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003435 ha_alert("Proxy '%s': unable to find sync peers '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003436 curproxy->id, curproxy->table->peers.name);
Willy Tarreau35cd7342021-02-26 20:51:47 +01003437 ha_free(&curproxy->table->peers.name);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003438 curproxy->table->peers.p = NULL;
Emeric Brun32da3c42010-09-23 18:39:19 +02003439 cfgerr++;
3440 }
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003441 else if (curpeers->disabled) {
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003442 /* silently disable this peers section */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003443 curproxy->table->peers.p = NULL;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02003444 }
Emeric Brun32da3c42010-09-23 18:39:19 +02003445 else if (!curpeers->peers_fe) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003446 ha_alert("Proxy '%s': unable to find local peer '%s' in peers section '%s'.\n",
3447 curproxy->id, localpeer, curpeers->id);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003448 curproxy->table->peers.p = NULL;
Emeric Brun32da3c42010-09-23 18:39:19 +02003449 cfgerr++;
3450 }
3451 }
3452
Simon Horman9dc49962015-01-30 11:22:59 +09003453
3454 if (curproxy->email_alert.mailers.name) {
3455 struct mailers *curmailers = mailers;
3456
3457 for (curmailers = mailers; curmailers; curmailers = curmailers->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003458 if (strcmp(curmailers->id, curproxy->email_alert.mailers.name) == 0)
Simon Horman9dc49962015-01-30 11:22:59 +09003459 break;
Simon Horman9dc49962015-01-30 11:22:59 +09003460 }
Simon Horman9dc49962015-01-30 11:22:59 +09003461 if (!curmailers) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003462 ha_alert("Proxy '%s': unable to find mailers '%s'.\n",
3463 curproxy->id, curproxy->email_alert.mailers.name);
Simon Horman9dc49962015-01-30 11:22:59 +09003464 free_email_alert(curproxy);
3465 cfgerr++;
3466 }
Christopher Faulet0108bb32017-10-20 21:34:32 +02003467 else {
3468 err = NULL;
3469 if (init_email_alert(curmailers, curproxy, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003470 ha_alert("Proxy '%s': %s.\n", curproxy->id, err);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003471 free(err);
3472 cfgerr++;
3473 }
3474 }
Simon Horman9dc49962015-01-30 11:22:59 +09003475 }
3476
Willy Tarreau5bbc6762021-02-12 11:49:25 +01003477 if (curproxy->uri_auth && !(curproxy->uri_auth->flags & STAT_CONVDONE) &&
Willy Tarreauff011f22011-01-06 17:51:27 +01003478 !LIST_ISEMPTY(&curproxy->uri_auth->http_req_rules) &&
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003479 (curproxy->uri_auth->userlist || curproxy->uri_auth->auth_realm )) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003480 ha_alert("%s '%s': stats 'auth'/'realm' and 'http-request' can't be used at the same time.\n",
3481 "proxy", curproxy->id);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003482 cfgerr++;
3483 goto out_uri_auth_compat;
3484 }
3485
Willy Tarreauee4f5f82019-10-09 09:59:22 +02003486 if (curproxy->uri_auth && curproxy->uri_auth->userlist &&
Willy Tarreau5bbc6762021-02-12 11:49:25 +01003487 (!(curproxy->uri_auth->flags & STAT_CONVDONE) ||
Willy Tarreauee4f5f82019-10-09 09:59:22 +02003488 LIST_ISEMPTY(&curproxy->uri_auth->http_req_rules))) {
Willy Tarreau95fa4692010-02-01 13:05:50 +01003489 const char *uri_auth_compat_req[10];
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003490 struct act_rule *rule;
Amaury Denoyelle3efee652021-03-08 17:31:39 +01003491 i = 0;
3492
Willy Tarreau95fa4692010-02-01 13:05:50 +01003493 /* build the ACL condition from scratch. We're relying on anonymous ACLs for that */
3494 uri_auth_compat_req[i++] = "auth";
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003495
3496 if (curproxy->uri_auth->auth_realm) {
Willy Tarreau95fa4692010-02-01 13:05:50 +01003497 uri_auth_compat_req[i++] = "realm";
3498 uri_auth_compat_req[i++] = curproxy->uri_auth->auth_realm;
3499 }
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003500
Willy Tarreau95fa4692010-02-01 13:05:50 +01003501 uri_auth_compat_req[i++] = "unless";
3502 uri_auth_compat_req[i++] = "{";
3503 uri_auth_compat_req[i++] = "http_auth(.internal-stats-userlist)";
3504 uri_auth_compat_req[i++] = "}";
3505 uri_auth_compat_req[i++] = "";
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003506
Willy Tarreauff011f22011-01-06 17:51:27 +01003507 rule = parse_http_req_cond(uri_auth_compat_req, "internal-stats-auth-compat", 0, curproxy);
3508 if (!rule) {
Willy Tarreau95fa4692010-02-01 13:05:50 +01003509 cfgerr++;
3510 break;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003511 }
3512
Willy Tarreau2b718102021-04-21 07:32:39 +02003513 LIST_APPEND(&curproxy->uri_auth->http_req_rules, &rule->list);
Willy Tarreau95fa4692010-02-01 13:05:50 +01003514
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003515 if (curproxy->uri_auth->auth_realm) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003516 ha_free(&curproxy->uri_auth->auth_realm);
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003517 }
Willy Tarreau5bbc6762021-02-12 11:49:25 +01003518 curproxy->uri_auth->flags |= STAT_CONVDONE;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003519 }
3520out_uri_auth_compat:
3521
Dragan Dosen43885c72015-10-01 13:18:13 +02003522 /* check whether we have a log server that uses RFC5424 log format */
Dragan Dosen1322d092015-09-22 16:05:32 +02003523 list_for_each_entry(tmplogsrv, &curproxy->logsrvs, list) {
Dragan Dosen43885c72015-10-01 13:18:13 +02003524 if (tmplogsrv->format == LOG_FORMAT_RFC5424) {
3525 if (!curproxy->conf.logformat_sd_string) {
3526 /* set the default logformat_sd_string */
3527 curproxy->conf.logformat_sd_string = default_rfc5424_sd_log_format;
3528 }
Dragan Dosen1322d092015-09-22 16:05:32 +02003529 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02003530 }
Dragan Dosen1322d092015-09-22 16:05:32 +02003531 }
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02003532
Willy Tarreaua4312fa2013-04-02 16:34:32 +02003533 /* compile the log format */
3534 if (!(curproxy->cap & PR_CAP_FE)) {
Willy Tarreau62a61232013-04-12 18:13:46 +02003535 if (curproxy->conf.logformat_string != default_http_log_format &&
3536 curproxy->conf.logformat_string != default_tcp_log_format &&
3537 curproxy->conf.logformat_string != clf_http_log_format)
3538 free(curproxy->conf.logformat_string);
3539 curproxy->conf.logformat_string = NULL;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003540 ha_free(&curproxy->conf.lfs_file);
Willy Tarreau62a61232013-04-12 18:13:46 +02003541 curproxy->conf.lfs_line = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003542
3543 if (curproxy->conf.logformat_sd_string != default_rfc5424_sd_log_format)
3544 free(curproxy->conf.logformat_sd_string);
3545 curproxy->conf.logformat_sd_string = NULL;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003546 ha_free(&curproxy->conf.lfsd_file);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003547 curproxy->conf.lfsd_line = 0;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02003548 }
3549
Willy Tarreau62a61232013-04-12 18:13:46 +02003550 if (curproxy->conf.logformat_string) {
3551 curproxy->conf.args.ctx = ARGC_LOG;
3552 curproxy->conf.args.file = curproxy->conf.lfs_file;
3553 curproxy->conf.args.line = curproxy->conf.lfs_line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003554 err = NULL;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +02003555 if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat,
3556 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003557 SMP_VAL_FE_LOG_END, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003558 ha_alert("Parsing [%s:%d]: failed to parse log-format : %s.\n",
3559 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003560 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01003561 cfgerr++;
3562 }
Willy Tarreau62a61232013-04-12 18:13:46 +02003563 curproxy->conf.args.file = NULL;
3564 curproxy->conf.args.line = 0;
3565 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02003566
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003567 if (curproxy->conf.logformat_sd_string) {
3568 curproxy->conf.args.ctx = ARGC_LOGSD;
3569 curproxy->conf.args.file = curproxy->conf.lfsd_file;
3570 curproxy->conf.args.line = curproxy->conf.lfsd_line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003571 err = NULL;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +02003572 if (!parse_logformat_string(curproxy->conf.logformat_sd_string, curproxy, &curproxy->logformat_sd,
3573 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003574 SMP_VAL_FE_LOG_END, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003575 ha_alert("Parsing [%s:%d]: failed to parse log-format-sd : %s.\n",
3576 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003577 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01003578 cfgerr++;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003579 } else if (!add_to_logformat_list(NULL, NULL, LF_SEPARATOR, &curproxy->logformat_sd, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003580 ha_alert("Parsing [%s:%d]: failed to parse log-format-sd : %s.\n",
3581 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003582 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01003583 cfgerr++;
3584 }
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003585 curproxy->conf.args.file = NULL;
3586 curproxy->conf.args.line = 0;
3587 }
3588
Willy Tarreau62a61232013-04-12 18:13:46 +02003589 if (curproxy->conf.uniqueid_format_string) {
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02003590 int where = 0;
3591
Willy Tarreau62a61232013-04-12 18:13:46 +02003592 curproxy->conf.args.ctx = ARGC_UIF;
3593 curproxy->conf.args.file = curproxy->conf.uif_file;
3594 curproxy->conf.args.line = curproxy->conf.uif_line;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003595 err = NULL;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02003596 if (curproxy->cap & PR_CAP_FE)
3597 where |= SMP_VAL_FE_HRQ_HDR;
3598 if (curproxy->cap & PR_CAP_BE)
3599 where |= SMP_VAL_BE_HRQ_HDR;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +02003600 if (!parse_logformat_string(curproxy->conf.uniqueid_format_string, curproxy, &curproxy->format_unique_id,
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02003601 LOG_OPT_HTTP|LOG_OPT_MERGE_SPACES, where, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003602 ha_alert("Parsing [%s:%d]: failed to parse unique-id : %s.\n",
3603 curproxy->conf.uif_file, curproxy->conf.uif_line, err);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01003604 free(err);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01003605 cfgerr++;
3606 }
Willy Tarreau62a61232013-04-12 18:13:46 +02003607 curproxy->conf.args.file = NULL;
3608 curproxy->conf.args.line = 0;
3609 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02003610
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +02003611 if (curproxy->conf.error_logformat_string) {
3612 curproxy->conf.args.ctx = ARGC_LOG;
3613 curproxy->conf.args.file = curproxy->conf.elfs_file;
3614 curproxy->conf.args.line = curproxy->conf.elfs_line;
3615 err = NULL;
3616 if (!parse_logformat_string(curproxy->conf.error_logformat_string, curproxy, &curproxy->logformat_error,
3617 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
3618 SMP_VAL_FE_LOG_END, &err)) {
3619 ha_alert("Parsing [%s:%d]: failed to parse error-log-format : %s.\n",
3620 curproxy->conf.elfs_file, curproxy->conf.elfs_line, err);
3621 free(err);
3622 cfgerr++;
3623 }
3624 curproxy->conf.args.file = NULL;
3625 curproxy->conf.args.line = 0;
3626 }
3627
Willy Tarreau7c9a0fe2022-04-25 10:25:34 +02003628 /* "balance hash" needs to compile its expression */
3629 if ((curproxy->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_SMP) {
3630 int idx = 0;
3631 const char *args[] = {
3632 curproxy->lbprm.arg_str,
3633 NULL,
3634 };
3635
3636 err = NULL;
3637 curproxy->conf.args.ctx = ARGC_USRV; // same context as use_server.
3638 curproxy->lbprm.expr =
3639 sample_parse_expr((char **)args, &idx,
3640 curproxy->conf.file, curproxy->conf.line,
3641 &err, &curproxy->conf.args, NULL);
3642
3643 if (!curproxy->lbprm.expr) {
3644 ha_alert("%s '%s' [%s:%d]: failed to parse 'balance hash' expression '%s' in : %s.\n",
3645 proxy_type_str(curproxy), curproxy->id,
3646 curproxy->conf.file, curproxy->conf.line,
3647 curproxy->lbprm.arg_str, err);
3648 ha_free(&err);
3649 cfgerr++;
3650 }
3651 else if (!(curproxy->lbprm.expr->fetch->val & SMP_VAL_BE_SET_SRV)) {
3652 ha_alert("%s '%s' [%s:%d]: error detected while parsing 'balance hash' expression '%s' "
3653 "which requires information from %s, which is not available here.\n",
3654 proxy_type_str(curproxy), curproxy->id,
3655 curproxy->conf.file, curproxy->conf.line,
3656 curproxy->lbprm.arg_str, sample_src_names(curproxy->lbprm.expr->fetch->use));
3657 cfgerr++;
3658 }
3659 else if (curproxy->mode == PR_MODE_HTTP && (curproxy->lbprm.expr->fetch->use & SMP_USE_L6REQ)) {
3660 ha_warning("%s '%s' [%s:%d]: L6 sample fetch <%s> will be ignored in 'balance hash' expression in HTTP mode.\n",
3661 proxy_type_str(curproxy), curproxy->id,
3662 curproxy->conf.file, curproxy->conf.line,
3663 curproxy->lbprm.arg_str);
3664 }
3665 else
3666 curproxy->http_needed |= !!(curproxy->lbprm.expr->fetch->use & SMP_USE_HTTP_ANY);
3667 }
3668
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01003669 /* only now we can check if some args remain unresolved.
3670 * This must be done after the users and groups resolution.
3671 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01003672 err = NULL;
3673 i = smp_resolve_args(curproxy, &err);
3674 cfgerr += i;
3675 if (i) {
3676 indent_msg(&err, 8);
3677 ha_alert("%s%s\n", i > 1 ? "multiple argument resolution errors:" : "", err);
3678 ha_free(&err);
3679 } else
Willy Tarreaua4312fa2013-04-02 16:34:32 +02003680 cfgerr += acl_find_targets(curproxy);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01003681
William Lallemand2c04a5a2021-08-13 15:21:12 +02003682 if (!(curproxy->cap & PR_CAP_INT) && (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003683 (((curproxy->cap & PR_CAP_FE) && !curproxy->timeout.client) ||
Willy Tarreaud825eef2007-05-12 22:35:00 +02003684 ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) &&
Willy Tarreauce887fd2012-05-12 12:50:00 +02003685 (!curproxy->timeout.connect ||
3686 (!curproxy->timeout.server && (curproxy->mode == PR_MODE_HTTP || !curproxy->timeout.tunnel)))))) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003687 ha_warning("missing timeouts for %s '%s'.\n"
Christopher Faulet767a84b2017-11-24 16:50:31 +01003688 " | While not properly invalid, you will certainly encounter various problems\n"
3689 " | with such a configuration. To fix this, please ensure that all following\n"
3690 " | timeouts are set to a non-zero value: 'client', 'connect', 'server'.\n",
3691 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubb925012009-07-23 13:36:36 +02003692 err_code |= ERR_WARN;
Willy Tarreau2738a142006-07-08 17:28:09 +02003693 }
Willy Tarreauf3c69202006-07-09 16:42:34 +02003694
Willy Tarreau1fa31262007-12-03 00:36:16 +01003695 /* Historically, the tarpit and queue timeouts were inherited from contimeout.
3696 * We must still support older configurations, so let's find out whether those
3697 * parameters have been set or must be copied from contimeouts.
3698 */
Willy Tarreau937c3ea2021-02-12 11:14:35 +01003699 if (!curproxy->timeout.tarpit)
3700 curproxy->timeout.tarpit = curproxy->timeout.connect;
3701 if ((curproxy->cap & PR_CAP_BE) && !curproxy->timeout.queue)
3702 curproxy->timeout.queue = curproxy->timeout.connect;
Willy Tarreau1fa31262007-12-03 00:36:16 +01003703
Christopher Faulete5870d82020-04-15 11:32:03 +02003704 if ((curproxy->tcpcheck_rules.flags & TCPCHK_RULES_UNUSED_TCP_RS)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003705 ha_warning("%s '%s' uses tcp-check rules without 'option tcp-check', so the rules are ignored.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003706 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau215663d2014-06-13 18:30:23 +02003707 err_code |= ERR_WARN;
3708 }
3709
Willy Tarreau193b8c62012-11-22 00:17:38 +01003710 /* ensure that cookie capture length is not too large */
3711 if (curproxy->capture_len >= global.tune.cookie_len) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003712 ha_warning("truncating capture length to %d bytes for %s '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003713 global.tune.cookie_len - 1, proxy_type_str(curproxy), curproxy->id);
Willy Tarreau193b8c62012-11-22 00:17:38 +01003714 err_code |= ERR_WARN;
3715 curproxy->capture_len = global.tune.cookie_len - 1;
3716 }
3717
Willy Tarreaucf7f3202007-05-13 22:46:04 +02003718 /* The small pools required for the capture lists */
Willy Tarreau9a54e132012-03-24 08:33:05 +01003719 if (curproxy->nb_req_cap) {
Willy Tarreaud9ed3d22014-06-13 12:23:06 +02003720 curproxy->req_cap_pool = create_pool("ptrcap",
3721 curproxy->nb_req_cap * sizeof(char *),
3722 MEM_F_SHARED);
Willy Tarreau9a54e132012-03-24 08:33:05 +01003723 }
3724
3725 if (curproxy->nb_rsp_cap) {
Willy Tarreaud9ed3d22014-06-13 12:23:06 +02003726 curproxy->rsp_cap_pool = create_pool("ptrcap",
3727 curproxy->nb_rsp_cap * sizeof(char *),
3728 MEM_F_SHARED);
Willy Tarreau9a54e132012-03-24 08:33:05 +01003729 }
Willy Tarreaucf7f3202007-05-13 22:46:04 +02003730
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003731 switch (curproxy->load_server_state_from_file) {
3732 case PR_SRV_STATE_FILE_UNSPEC:
3733 curproxy->load_server_state_from_file = PR_SRV_STATE_FILE_NONE;
3734 break;
3735 case PR_SRV_STATE_FILE_GLOBAL:
3736 if (!global.server_state_file) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003737 ha_warning("backend '%s' configured to load server state file from global section 'server-state-file' directive. Unfortunately, 'server-state-file' is not set!\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003738 curproxy->id);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003739 err_code |= ERR_WARN;
3740 }
3741 break;
3742 }
3743
Willy Tarreaubaaee002006-06-26 02:48:02 +02003744 /* first, we will invert the servers list order */
3745 newsrv = NULL;
3746 while (curproxy->srv) {
3747 struct server *next;
3748
3749 next = curproxy->srv->next;
3750 curproxy->srv->next = newsrv;
3751 newsrv = curproxy->srv;
3752 if (!next)
3753 break;
3754 curproxy->srv = next;
3755 }
3756
Willy Tarreau17edc812014-01-03 12:14:34 +01003757 /* Check that no server name conflicts. This causes trouble in the stats.
3758 * We only emit a warning for the first conflict affecting each server,
3759 * in order to avoid combinatory explosion if all servers have the same
3760 * name. We do that only for servers which do not have an explicit ID,
3761 * because these IDs were made also for distinguishing them and we don't
3762 * want to annoy people who correctly manage them.
3763 */
3764 for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
3765 struct server *other_srv;
3766
3767 if (newsrv->puid)
3768 continue;
3769
3770 for (other_srv = curproxy->srv; other_srv && other_srv != newsrv; other_srv = other_srv->next) {
3771 if (!other_srv->puid && strcmp(other_srv->id, newsrv->id) == 0) {
Willy Tarreaub01302f2019-05-27 19:31:06 +02003772 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 +01003773 newsrv->conf.file, newsrv->conf.line,
3774 proxy_type_str(curproxy), curproxy->id,
3775 newsrv->id, other_srv->conf.line);
Willy Tarreaub01302f2019-05-27 19:31:06 +02003776 cfgerr++;
Willy Tarreau17edc812014-01-03 12:14:34 +01003777 break;
3778 }
3779 }
3780 }
3781
Willy Tarreaudd701652010-05-25 23:03:02 +02003782 /* assign automatic UIDs to servers which don't have one yet */
3783 next_id = 1;
3784 newsrv = curproxy->srv;
3785 while (newsrv != NULL) {
3786 if (!newsrv->puid) {
3787 /* server ID not set, use automatic numbering with first
3788 * spare entry starting with next_svid.
3789 */
3790 next_id = get_next_id(&curproxy->conf.used_server_id, next_id);
3791 newsrv->conf.id.key = newsrv->puid = next_id;
3792 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
3793 }
Amaury Denoyelle077c6b82021-06-14 17:04:25 +02003794 newsrv->conf.name.key = newsrv->id;
3795 ebis_insert(&curproxy->conf.used_server_name, &newsrv->conf.name);
3796
Willy Tarreaudd701652010-05-25 23:03:02 +02003797 next_id++;
3798 newsrv = newsrv->next;
3799 }
3800
Willy Tarreau20697042007-11-15 23:26:18 +01003801 curproxy->lbprm.wmult = 1; /* default weight multiplier */
Willy Tarreau5dc2fa62007-11-19 19:10:18 +01003802 curproxy->lbprm.wdiv = 1; /* default weight divider */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003803
Willy Tarreau62c3be22012-01-20 13:12:32 +01003804 /*
3805 * If this server supports a maxconn parameter, it needs a dedicated
3806 * tasks to fill the emptied slots when a connection leaves.
3807 * Also, resolve deferred tracking dependency if needed.
3808 */
3809 newsrv = curproxy->srv;
3810 while (newsrv != NULL) {
Amaury Denoyellee74cbc32021-05-28 10:34:01 +02003811 set_usermsgs_ctx(newsrv->conf.file, newsrv->conf.line, &newsrv->obj_type);
3812
Willy Tarreau62c3be22012-01-20 13:12:32 +01003813 if (newsrv->minconn > newsrv->maxconn) {
3814 /* Only 'minconn' was specified, or it was higher than or equal
3815 * to 'maxconn'. Let's turn this into maxconn and clean it, as
3816 * this will avoid further useless expensive computations.
3817 */
3818 newsrv->maxconn = newsrv->minconn;
3819 } else if (newsrv->maxconn && !newsrv->minconn) {
3820 /* minconn was not specified, so we set it to maxconn */
3821 newsrv->minconn = newsrv->maxconn;
3822 }
3823
William Dauchyf6370442020-11-14 19:25:33 +01003824 /* this will also properly set the transport layer for
3825 * prod and checks
3826 * if default-server have use_ssl, prerare ssl init
3827 * without activating it */
3828 if (newsrv->use_ssl == 1 || newsrv->check.use_ssl == 1 ||
Christopher Faulet4ab26792021-12-01 09:50:41 +01003829 (newsrv->proxy->options & PR_O_TCPCHK_SSL) ||
3830 ((newsrv->flags & SRV_F_DEFSRV_USE_SSL) && newsrv->use_ssl != 1)) {
Willy Tarreau17d45382016-12-22 21:16:08 +01003831 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv)
3832 cfgerr += xprt_get(XPRT_SSL)->prepare_srv(newsrv);
3833 }
Emeric Brun94324a42012-10-11 14:00:19 +02003834
Willy Tarreau034c88c2017-01-23 23:36:45 +01003835 if ((newsrv->flags & SRV_F_FASTOPEN) &&
3836 ((curproxy->retry_type & (PR_RE_DISCONNECTED | PR_RE_TIMEOUT)) !=
3837 (PR_RE_DISCONNECTED | PR_RE_TIMEOUT)))
Amaury Denoyellee74cbc32021-05-28 10:34:01 +02003838 ha_warning("server 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");
Willy Tarreau034c88c2017-01-23 23:36:45 +01003839
Willy Tarreau62c3be22012-01-20 13:12:32 +01003840 if (newsrv->trackit) {
Amaury Denoyelle669b6202021-07-13 10:35:23 +02003841 if (srv_apply_track(newsrv, curproxy)) {
3842 ++cfgerr;
Willy Tarreau62c3be22012-01-20 13:12:32 +01003843 goto next_srv;
3844 }
Willy Tarreau62c3be22012-01-20 13:12:32 +01003845 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003846
Willy Tarreau62c3be22012-01-20 13:12:32 +01003847 next_srv:
Amaury Denoyellee74cbc32021-05-28 10:34:01 +02003848 reset_usermsgs_ctx();
Willy Tarreau62c3be22012-01-20 13:12:32 +01003849 newsrv = newsrv->next;
3850 }
3851
Olivier Houchard4e694042017-03-14 20:01:29 +01003852 /*
3853 * Try to generate dynamic cookies for servers now.
3854 * It couldn't be done earlier, since at the time we parsed
3855 * the server line, we may not have known yet that we
3856 * should use dynamic cookies, or the secret key may not
3857 * have been provided yet.
3858 */
3859 if (curproxy->ck_opts & PR_CK_DYNAMIC) {
3860 newsrv = curproxy->srv;
3861 while (newsrv != NULL) {
3862 srv_set_dyncookie(newsrv);
3863 newsrv = newsrv->next;
3864 }
3865
3866 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003867 /* We have to initialize the server lookup mechanism depending
Joseph Herlanta14c03e2018-11-15 14:04:19 -08003868 * on what LB algorithm was chosen.
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003869 */
3870
3871 curproxy->lbprm.algo &= ~(BE_LB_LKUP | BE_LB_PROP_DYN);
3872 switch (curproxy->lbprm.algo & BE_LB_KIND) {
3873 case BE_LB_KIND_RR:
Willy Tarreau9757a382009-10-03 12:56:50 +02003874 if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_RR_STATIC) {
3875 curproxy->lbprm.algo |= BE_LB_LKUP_MAP;
3876 init_server_map(curproxy);
Willy Tarreau760e81d2018-05-03 07:20:40 +02003877 } else if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_RR_RANDOM) {
3878 curproxy->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
Remi Tricot-Le Breton47646202021-05-19 16:40:28 +02003879 if (chash_init_server_tree(curproxy) < 0) {
3880 cfgerr++;
3881 }
Willy Tarreau9757a382009-10-03 12:56:50 +02003882 } else {
3883 curproxy->lbprm.algo |= BE_LB_LKUP_RRTREE | BE_LB_PROP_DYN;
3884 fwrr_init_server_groups(curproxy);
3885 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003886 break;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003887
Willy Tarreau3ebb1162012-02-13 16:57:44 +01003888 case BE_LB_KIND_CB:
Willy Tarreauf09c6602012-02-13 17:12:08 +01003889 if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_CB_LC) {
3890 curproxy->lbprm.algo |= BE_LB_LKUP_LCTREE | BE_LB_PROP_DYN;
3891 fwlc_init_server_tree(curproxy);
3892 } else {
3893 curproxy->lbprm.algo |= BE_LB_LKUP_FSTREE | BE_LB_PROP_DYN;
3894 fas_init_server_tree(curproxy);
3895 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003896 break;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003897
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003898 case BE_LB_KIND_HI:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003899 if ((curproxy->lbprm.algo & BE_LB_HASH_TYPE) == BE_LB_HASH_CONS) {
3900 curproxy->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
Remi Tricot-Le Breton47646202021-05-19 16:40:28 +02003901 if (chash_init_server_tree(curproxy) < 0) {
3902 cfgerr++;
3903 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02003904 } else {
3905 curproxy->lbprm.algo |= BE_LB_LKUP_MAP;
3906 init_server_map(curproxy);
3907 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02003908 break;
3909 }
Willy Tarreaucd10def2020-10-17 18:48:47 +02003910 HA_RWLOCK_INIT(&curproxy->lbprm.lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003911
3912 if (curproxy->options & PR_O_LOGASAP)
3913 curproxy->to_log &= ~LW_BYTES;
3914
William Lallemand2c04a5a2021-08-13 15:21:12 +02003915 if (!(curproxy->cap & PR_CAP_INT) && (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003916 (curproxy->cap & PR_CAP_FE) && LIST_ISEMPTY(&curproxy->logsrvs) &&
3917 (!LIST_ISEMPTY(&curproxy->logformat) || !LIST_ISEMPTY(&curproxy->logformat_sd))) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003918 ha_warning("log format ignored for %s '%s' since it has no log address.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003919 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue7ded1f2009-08-09 10:11:45 +02003920 err_code |= ERR_WARN;
3921 }
3922
Christopher Faulet3b6446f2021-03-15 15:10:38 +01003923 if (curproxy->mode != PR_MODE_HTTP && !(curproxy->options & PR_O_HTTP_UPG)) {
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003924 int optnum;
3925
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003926 if (curproxy->uri_auth) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003927 ha_warning("'stats' statement ignored for %s '%s' as it requires HTTP mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003928 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003929 err_code |= ERR_WARN;
3930 curproxy->uri_auth = NULL;
3931 }
3932
Willy Tarreaude7dc882017-03-10 11:49:21 +01003933 if (curproxy->capture_name) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003934 ha_warning("'capture' statement ignored for %s '%s' as it requires HTTP mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003935 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003936 err_code |= ERR_WARN;
3937 }
3938
3939 if (!LIST_ISEMPTY(&curproxy->http_req_rules)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003940 ha_warning("'http-request' rules ignored for %s '%s' as they require HTTP mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003941 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003942 err_code |= ERR_WARN;
3943 }
3944
3945 if (!LIST_ISEMPTY(&curproxy->http_res_rules)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003946 ha_warning("'http-response' rules ignored for %s '%s' as they require HTTP mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003947 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003948 err_code |= ERR_WARN;
3949 }
3950
Christopher Fauletbb7abed2021-03-15 15:09:21 +01003951 if (!LIST_ISEMPTY(&curproxy->http_after_res_rules)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003952 ha_warning("'http-after-response' rules ignored for %s '%s' as they require HTTP mode.\n",
Christopher Fauletbb7abed2021-03-15 15:09:21 +01003953 proxy_type_str(curproxy), curproxy->id);
3954 err_code |= ERR_WARN;
3955 }
3956
Willy Tarreaude7dc882017-03-10 11:49:21 +01003957 if (!LIST_ISEMPTY(&curproxy->redirect_rules)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003958 ha_warning("'redirect' rules ignored for %s '%s' as they require HTTP mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003959 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaude7dc882017-03-10 11:49:21 +01003960 err_code |= ERR_WARN;
3961 }
3962
Willy Tarreau87cf5142011-08-19 22:57:24 +02003963 if (curproxy->options & (PR_O_FWDFOR | PR_O_FF_ALWAYS)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003964 ha_warning("'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003965 "forwardfor", proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003966 err_code |= ERR_WARN;
Willy Tarreau87cf5142011-08-19 22:57:24 +02003967 curproxy->options &= ~(PR_O_FWDFOR | PR_O_FF_ALWAYS);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003968 }
3969
3970 if (curproxy->options & PR_O_ORGTO) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003971 ha_warning("'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003972 "originalto", proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003973 err_code |= ERR_WARN;
3974 curproxy->options &= ~PR_O_ORGTO;
3975 }
3976
3977 for (optnum = 0; cfg_opts[optnum].name; optnum++) {
3978 if (cfg_opts[optnum].mode == PR_MODE_HTTP &&
3979 (curproxy->cap & cfg_opts[optnum].cap) &&
3980 (curproxy->options & cfg_opts[optnum].val)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003981 ha_warning("'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003982 cfg_opts[optnum].name, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003983 err_code |= ERR_WARN;
3984 curproxy->options &= ~cfg_opts[optnum].val;
3985 }
3986 }
3987
3988 for (optnum = 0; cfg_opts2[optnum].name; optnum++) {
3989 if (cfg_opts2[optnum].mode == PR_MODE_HTTP &&
3990 (curproxy->cap & cfg_opts2[optnum].cap) &&
3991 (curproxy->options2 & cfg_opts2[optnum].val)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003992 ha_warning("'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003993 cfg_opts2[optnum].name, proxy_type_str(curproxy), curproxy->id);
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01003994 err_code |= ERR_WARN;
3995 curproxy->options2 &= ~cfg_opts2[optnum].val;
3996 }
3997 }
Willy Tarreaubce70882009-09-07 11:51:47 +02003998
Willy Tarreau29fbe512015-08-20 19:35:14 +02003999#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreauef9a3602012-12-08 22:29:20 +01004000 if (curproxy->conn_src.bind_hdr_occ) {
4001 curproxy->conn_src.bind_hdr_occ = 0;
Amaury Denoyelle11124302021-06-04 18:22:08 +02004002 ha_warning("%s '%s' : ignoring use of header %s as source IP in non-HTTP mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004003 proxy_type_str(curproxy), curproxy->id, curproxy->conn_src.bind_hdr_name);
Willy Tarreaubce70882009-09-07 11:51:47 +02004004 err_code |= ERR_WARN;
4005 }
Willy Tarreauefa5f512010-03-30 20:13:29 +02004006#endif
Willy Tarreaue24fdfb2010-03-25 07:22:56 +01004007 }
4008
Willy Tarreaubaaee002006-06-26 02:48:02 +02004009 /*
Willy Tarreau21d2af32008-02-14 20:25:24 +01004010 * ensure that we're not cross-dressing a TCP server into HTTP.
4011 */
4012 newsrv = curproxy->srv;
4013 while (newsrv != NULL) {
Willy Tarreau0cec3312011-10-31 13:49:26 +01004014 if ((curproxy->mode != PR_MODE_HTTP) && newsrv->rdr_len) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02004015 ha_alert("%s '%s' : server cannot have cookie or redirect prefix in non-HTTP mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004016 proxy_type_str(curproxy), curproxy->id);
Willy Tarreaubb925012009-07-23 13:36:36 +02004017 cfgerr++;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004018 }
Willy Tarreaubce70882009-09-07 11:51:47 +02004019
Willy Tarreau0cec3312011-10-31 13:49:26 +01004020 if ((curproxy->mode != PR_MODE_HTTP) && newsrv->cklen) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02004021 ha_warning("%s '%s' : ignoring cookie for server '%s' as HTTP mode is disabled.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004022 proxy_type_str(curproxy), curproxy->id, newsrv->id);
Willy Tarreau0cec3312011-10-31 13:49:26 +01004023 err_code |= ERR_WARN;
4024 }
4025
Willy Tarreauc93cd162014-05-13 15:54:22 +02004026 if ((newsrv->flags & SRV_F_MAPPORTS) && (curproxy->options2 & PR_O2_RDPC_PRST)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02004027 ha_warning("%s '%s' : RDP cookie persistence will not work for server '%s' because it lacks an explicit port number.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004028 proxy_type_str(curproxy), curproxy->id, newsrv->id);
Willy Tarreau82ffa392013-08-13 17:19:08 +02004029 err_code |= ERR_WARN;
4030 }
4031
Willy Tarreau29fbe512015-08-20 19:35:14 +02004032#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreauef9a3602012-12-08 22:29:20 +01004033 if (curproxy->mode != PR_MODE_HTTP && newsrv->conn_src.bind_hdr_occ) {
4034 newsrv->conn_src.bind_hdr_occ = 0;
Amaury Denoyelle11124302021-06-04 18:22:08 +02004035 ha_warning("%s '%s' : server %s cannot use header %s as source IP in non-HTTP mode.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004036 proxy_type_str(curproxy), curproxy->id, newsrv->id, newsrv->conn_src.bind_hdr_name);
Willy Tarreaubce70882009-09-07 11:51:47 +02004037 err_code |= ERR_WARN;
4038 }
Willy Tarreauefa5f512010-03-30 20:13:29 +02004039#endif
Willy Tarreau4c183462017-01-06 12:21:38 +01004040
Willy Tarreau46deab62018-04-28 07:18:15 +02004041 if ((curproxy->mode != PR_MODE_HTTP) && (curproxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR)
4042 curproxy->options &= ~PR_O_REUSE_MASK;
4043
Willy Tarreau21d2af32008-02-14 20:25:24 +01004044 newsrv = newsrv->next;
4045 }
4046
Christopher Fauletd7c91962015-04-30 11:48:27 +02004047 /* Check filter configuration, if any */
4048 cfgerr += flt_check(curproxy);
4049
Willy Tarreauc1a21672009-08-16 22:37:44 +02004050 if (curproxy->cap & PR_CAP_FE) {
Willy Tarreau050536d2012-10-04 08:47:34 +02004051 if (!curproxy->accept)
4052 curproxy->accept = frontend_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +02004053
Christopher Fauletee08d6c2021-10-13 15:40:15 +02004054 if (!LIST_ISEMPTY(&curproxy->tcp_req.inspect_rules) ||
4055 (curproxy->defpx && !LIST_ISEMPTY(&curproxy->defpx->tcp_req.inspect_rules)))
Willy Tarreaufb356202010-08-03 14:02:05 +02004056 curproxy->fe_req_ana |= AN_REQ_INSPECT_FE;
Willy Tarreauc1a21672009-08-16 22:37:44 +02004057
Willy Tarreau4e5b8282009-08-16 22:57:50 +02004058 if (curproxy->mode == PR_MODE_HTTP) {
Willy Tarreauc1a21672009-08-16 22:37:44 +02004059 curproxy->fe_req_ana |= AN_REQ_WAIT_HTTP | AN_REQ_HTTP_PROCESS_FE;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004060 curproxy->fe_rsp_ana |= AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_FE;
Willy Tarreau4e5b8282009-08-16 22:57:50 +02004061 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02004062
William Lallemandcf62f7e2018-10-26 14:47:40 +02004063 if (curproxy->mode == PR_MODE_CLI) {
4064 curproxy->fe_req_ana |= AN_REQ_WAIT_CLI;
4065 curproxy->fe_rsp_ana |= AN_RES_WAIT_CLI;
4066 }
4067
Willy Tarreauc1a21672009-08-16 22:37:44 +02004068 /* both TCP and HTTP must check switching rules */
4069 curproxy->fe_req_ana |= AN_REQ_SWITCHING_RULES;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004070
4071 /* Add filters analyzers if needed */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01004072 if (!LIST_ISEMPTY(&curproxy->filter_configs)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004073 curproxy->fe_req_ana |= AN_REQ_FLT_START_FE | AN_REQ_FLT_XFER_DATA | AN_REQ_FLT_END;
4074 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 +02004075 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02004076 }
4077
4078 if (curproxy->cap & PR_CAP_BE) {
Christopher Fauletee08d6c2021-10-13 15:40:15 +02004079 if (!LIST_ISEMPTY(&curproxy->tcp_req.inspect_rules) ||
4080 (curproxy->defpx && !LIST_ISEMPTY(&curproxy->defpx->tcp_req.inspect_rules)))
Willy Tarreaufb356202010-08-03 14:02:05 +02004081 curproxy->be_req_ana |= AN_REQ_INSPECT_BE;
4082
Christopher Fauletee08d6c2021-10-13 15:40:15 +02004083 if (!LIST_ISEMPTY(&curproxy->tcp_rep.inspect_rules) ||
4084 (curproxy->defpx && !LIST_ISEMPTY(&curproxy->defpx->tcp_rep.inspect_rules)))
Emeric Brun97679e72010-09-23 17:56:44 +02004085 curproxy->be_rsp_ana |= AN_RES_INSPECT;
4086
Willy Tarreau4e5b8282009-08-16 22:57:50 +02004087 if (curproxy->mode == PR_MODE_HTTP) {
Willy Tarreauc1a21672009-08-16 22:37:44 +02004088 curproxy->be_req_ana |= AN_REQ_WAIT_HTTP | AN_REQ_HTTP_INNER | AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004089 curproxy->be_rsp_ana |= AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE;
Willy Tarreau4e5b8282009-08-16 22:57:50 +02004090 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02004091
4092 /* If the backend does requires RDP cookie persistence, we have to
4093 * enable the corresponding analyser.
4094 */
4095 if (curproxy->options2 & PR_O2_RDPC_PRST)
4096 curproxy->be_req_ana |= AN_REQ_PRST_RDP_COOKIE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004097
4098 /* Add filters analyzers if needed */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01004099 if (!LIST_ISEMPTY(&curproxy->filter_configs)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004100 curproxy->be_req_ana |= AN_REQ_FLT_START_BE | AN_REQ_FLT_XFER_DATA | AN_REQ_FLT_END;
4101 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 +02004102 }
Willy Tarreauc1a21672009-08-16 22:37:44 +02004103 }
Christopher Fauleta717b992018-04-10 14:43:00 +02004104
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02004105 /* Check the mux protocols, if any, for each listener and server
Christopher Fauleta717b992018-04-10 14:43:00 +02004106 * attached to the current proxy */
4107 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
4108 int mode = (1 << (curproxy->mode == PR_MODE_HTTP));
Willy Tarreau76a551d2018-12-02 13:09:09 +01004109 const struct mux_proto_list *mux_ent;
Christopher Fauleta717b992018-04-10 14:43:00 +02004110
Willy Tarreau730cc022022-05-20 18:07:06 +02004111 if (!bind_conf->mux_proto) {
4112 /* No protocol was specified. If we're using QUIC at the transport
4113 * layer, we'll instantiate it as a mux as well. If QUIC is not
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05004114 * compiled in, this will remain NULL.
Willy Tarreau730cc022022-05-20 18:07:06 +02004115 */
4116 if (bind_conf->xprt && bind_conf->xprt == xprt_get(XPRT_QUIC))
4117 bind_conf->mux_proto = get_mux_proto(ist("quic"));
4118 }
4119
Christopher Fauleta717b992018-04-10 14:43:00 +02004120 if (!bind_conf->mux_proto)
4121 continue;
Willy Tarreau76a551d2018-12-02 13:09:09 +01004122
4123 /* it is possible that an incorrect mux was referenced
4124 * due to the proxy's mode not being taken into account
4125 * on first pass. Let's adjust it now.
4126 */
4127 mux_ent = conn_get_best_mux_entry(bind_conf->mux_proto->token, PROTO_SIDE_FE, mode);
4128
4129 if (!mux_ent || !isteq(mux_ent->token, bind_conf->mux_proto->token)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02004130 ha_alert("%s '%s' : MUX protocol '%.*s' is not usable for 'bind %s' at [%s:%d].\n",
Christopher Fauleta717b992018-04-10 14:43:00 +02004131 proxy_type_str(curproxy), curproxy->id,
4132 (int)bind_conf->mux_proto->token.len,
4133 bind_conf->mux_proto->token.ptr,
4134 bind_conf->arg, bind_conf->file, bind_conf->line);
4135 cfgerr++;
Willy Tarreaucac619b2022-05-20 17:53:32 +02004136 } else {
4137 if ((mux_ent->mux->flags & MX_FL_FRAMED) && !(bind_conf->options & BC_O_USE_SOCK_DGRAM)) {
4138 ha_alert("%s '%s' : frame-based MUX protocol '%.*s' is incompatible with stream transport of 'bind %s' at [%s:%d].\n",
4139 proxy_type_str(curproxy), curproxy->id,
4140 (int)bind_conf->mux_proto->token.len,
4141 bind_conf->mux_proto->token.ptr,
4142 bind_conf->arg, bind_conf->file, bind_conf->line);
4143 cfgerr++;
4144 }
4145 else if (!(mux_ent->mux->flags & MX_FL_FRAMED) && !(bind_conf->options & BC_O_USE_SOCK_STREAM)) {
4146 ha_alert("%s '%s' : stream-based MUX protocol '%.*s' is incompatible with framed transport of 'bind %s' at [%s:%d].\n",
4147 proxy_type_str(curproxy), curproxy->id,
4148 (int)bind_conf->mux_proto->token.len,
4149 bind_conf->mux_proto->token.ptr,
4150 bind_conf->arg, bind_conf->file, bind_conf->line);
4151 cfgerr++;
4152 }
Christopher Fauleta717b992018-04-10 14:43:00 +02004153 }
Willy Tarreau76a551d2018-12-02 13:09:09 +01004154
4155 /* update the mux */
4156 bind_conf->mux_proto = mux_ent;
Christopher Fauleta717b992018-04-10 14:43:00 +02004157 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02004158 for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
4159 int mode = (1 << (curproxy->mode == PR_MODE_HTTP));
Willy Tarreau76a551d2018-12-02 13:09:09 +01004160 const struct mux_proto_list *mux_ent;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02004161
4162 if (!newsrv->mux_proto)
4163 continue;
Willy Tarreau76a551d2018-12-02 13:09:09 +01004164
4165 /* it is possible that an incorrect mux was referenced
4166 * due to the proxy's mode not being taken into account
4167 * on first pass. Let's adjust it now.
4168 */
4169 mux_ent = conn_get_best_mux_entry(newsrv->mux_proto->token, PROTO_SIDE_BE, mode);
4170
4171 if (!mux_ent || !isteq(mux_ent->token, newsrv->mux_proto->token)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02004172 ha_alert("%s '%s' : MUX protocol '%.*s' is not usable for server '%s' at [%s:%d].\n",
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02004173 proxy_type_str(curproxy), curproxy->id,
4174 (int)newsrv->mux_proto->token.len,
4175 newsrv->mux_proto->token.ptr,
4176 newsrv->id, newsrv->conf.file, newsrv->conf.line);
4177 cfgerr++;
4178 }
Willy Tarreau76a551d2018-12-02 13:09:09 +01004179
4180 /* update the mux */
4181 newsrv->mux_proto = mux_ent;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02004182 }
Amaury Denoyellef2c27a52021-07-23 15:46:46 +02004183
4184 /* Allocate default tcp-check rules for proxies without
4185 * explicit rules.
4186 */
4187 if (curproxy->cap & PR_CAP_BE) {
4188 if (!(curproxy->options2 & PR_O2_CHK_ANY)) {
4189 struct tcpcheck_ruleset *rs = NULL;
4190 struct tcpcheck_rules *rules = &curproxy->tcpcheck_rules;
4191
4192 curproxy->options2 |= PR_O2_TCPCHK_CHK;
4193
4194 rs = find_tcpcheck_ruleset("*tcp-check");
4195 if (!rs) {
4196 rs = create_tcpcheck_ruleset("*tcp-check");
4197 if (rs == NULL) {
4198 ha_alert("config: %s '%s': out of memory.\n",
4199 proxy_type_str(curproxy), curproxy->id);
4200 cfgerr++;
4201 }
4202 }
4203
4204 free_tcpcheck_vars(&rules->preset_vars);
4205 rules->list = &rs->rules;
4206 rules->flags = 0;
4207 }
4208 }
Willy Tarreau4cdac162021-03-05 10:48:42 +01004209 }
Willy Tarreau835daa12019-02-07 14:46:29 +01004210
Emeric Brun3b68b602022-08-18 15:53:21 +02004211 /*
4212 * We have just initialized the main proxies list
4213 * we must also configure the log-forward proxies list
4214 */
4215 if (init_proxies_list == proxies_list) {
4216 init_proxies_list = cfg_log_forward;
Emeric Brun8032a272022-08-22 10:25:11 +02004217 /* check if list is not null to avoid infinite loop */
4218 if (init_proxies_list)
4219 goto init_proxies_list_stage1;
Emeric Brun3b68b602022-08-18 15:53:21 +02004220 }
4221
Emeric Brund6e581d2022-09-13 16:16:30 +02004222 if (init_proxies_list == cfg_log_forward) {
4223 init_proxies_list = sink_proxies_list;
4224 /* check if list is not null to avoid infinite loop */
4225 if (init_proxies_list)
4226 goto init_proxies_list_stage1;
4227 }
4228
Willy Tarreau4cdac162021-03-05 10:48:42 +01004229 /***********************************************************/
4230 /* At this point, target names have already been resolved. */
4231 /***********************************************************/
Willy Tarreau835daa12019-02-07 14:46:29 +01004232
Willy Tarreau4cdac162021-03-05 10:48:42 +01004233 /* we must finish to initialize certain things on the servers */
Willy Tarreau835daa12019-02-07 14:46:29 +01004234
Willy Tarreau4cdac162021-03-05 10:48:42 +01004235 list_for_each_entry(newsrv, &servers_list, global_list) {
4236 /* initialize idle conns lists */
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004237 if (srv_init_per_thr(newsrv) == -1) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01004238 ha_alert("parsing [%s:%d] : failed to allocate per-thread lists for server '%s'.\n",
4239 newsrv->conf.file, newsrv->conf.line, newsrv->id);
Willy Tarreau4cdac162021-03-05 10:48:42 +01004240 cfgerr++;
4241 continue;
4242 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01004243
Willy Tarreau4cdac162021-03-05 10:48:42 +01004244 if (newsrv->max_idle_conns != 0) {
Amaury Denoyelle3efee652021-03-08 17:31:39 +01004245 newsrv->curr_idle_thr = calloc(global.nbthread, sizeof(*newsrv->curr_idle_thr));
4246 if (!newsrv->curr_idle_thr) {
4247 ha_alert("parsing [%s:%d] : failed to allocate idle connection tasks for server '%s'.\n",
4248 newsrv->conf.file, newsrv->conf.line, newsrv->id);
4249 cfgerr++;
4250 continue;
4251 }
Remi Tricot-Le Breton47646202021-05-19 16:40:28 +02004252
Amaury Denoyelle3efee652021-03-08 17:31:39 +01004253 }
4254 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01004255
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004256 idle_conn_task = task_new_anywhere();
Amaury Denoyelle3efee652021-03-08 17:31:39 +01004257 if (!idle_conn_task) {
4258 ha_alert("parsing : failed to allocate global idle connection task.\n");
4259 cfgerr++;
4260 }
4261 else {
4262 idle_conn_task->process = srv_cleanup_idle_conns;
4263 idle_conn_task->context = NULL;
Olivier Houcharddc2f2752020-02-13 19:12:07 +01004264
Amaury Denoyelle3efee652021-03-08 17:31:39 +01004265 for (i = 0; i < global.nbthread; i++) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004266 idle_conns[i].cleanup_task = task_new_on(i);
Amaury Denoyelle3efee652021-03-08 17:31:39 +01004267 if (!idle_conns[i].cleanup_task) {
4268 ha_alert("parsing : failed to allocate idle connection tasks for thread '%d'.\n", i);
4269 cfgerr++;
4270 break;
Willy Tarreau4cdac162021-03-05 10:48:42 +01004271 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01004272
Amaury Denoyelle3efee652021-03-08 17:31:39 +01004273 idle_conns[i].cleanup_task->process = srv_cleanup_toremove_conns;
4274 idle_conns[i].cleanup_task->context = NULL;
4275 HA_SPIN_INIT(&idle_conns[i].idle_conns_lock);
4276 MT_LIST_INIT(&idle_conns[i].toremove_conns);
Willy Tarreau835daa12019-02-07 14:46:29 +01004277 }
Willy Tarreau419ead82014-09-16 13:41:21 +02004278 }
4279
Willy Tarreau419ead82014-09-16 13:41:21 +02004280 /* perform the final checks before creating tasks */
4281
Emeric Brun3b68b602022-08-18 15:53:21 +02004282 /* starting to initialize the main proxies list */
4283 init_proxies_list = proxies_list;
4284
4285init_proxies_list_stage2:
4286 for (curproxy = init_proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreau419ead82014-09-16 13:41:21 +02004287 struct listener *listener;
4288 unsigned int next_id;
Willy Tarreauc1a21672009-08-16 22:37:44 +02004289
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02004290 /* Configure SSL for each bind line.
4291 * Note: if configuration fails at some point, the ->ctx member
4292 * remains NULL so that listeners can later detach.
4293 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004294 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
Willy Tarreau55d37912016-12-21 23:38:39 +01004295 if (bind_conf->xprt->prepare_bind_conf &&
4296 bind_conf->xprt->prepare_bind_conf(bind_conf) < 0)
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02004297 cfgerr++;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004298 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02004299
Willy Tarreaue6b98942007-10-29 01:09:36 +01004300 /* adjust this proxy's listeners */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02004301 next_id = 1;
Willy Tarreau4348fad2012-09-20 16:48:07 +02004302 list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) {
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02004303 if (!listener->luid) {
4304 /* listener ID not set, use automatic numbering with first
4305 * spare entry starting with next_luid.
4306 */
4307 next_id = get_next_id(&curproxy->conf.used_listener_id, next_id);
4308 listener->conf.id.key = listener->luid = next_id;
4309 eb32_insert(&curproxy->conf.used_listener_id, &listener->conf.id);
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02004310 }
Krzysztof Piotr Oledzkidf5cb9f2010-02-05 20:58:27 +01004311 next_id++;
Willy Tarreau53fb4ae2009-10-04 23:04:08 +02004312
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004313 /* enable separate counters */
4314 if (curproxy->options2 & PR_O2_SOCKSTAT) {
Willy Tarreauae9bea02016-11-25 14:44:52 +01004315 listener->counters = calloc(1, sizeof(*listener->counters));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004316 if (!listener->name)
4317 memprintf(&listener->name, "sock-%d", listener->luid);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004318 }
Willy Tarreau81796be2012-09-22 19:11:47 +02004319
Willy Tarreaue6b98942007-10-29 01:09:36 +01004320 if (curproxy->options & PR_O_TCP_NOLING)
4321 listener->options |= LI_O_NOLINGER;
Willy Tarreau16a21472012-11-19 12:39:59 +01004322 if (!listener->maxaccept)
Willy Tarreau66161322021-02-19 15:50:27 +01004323 listener->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreau16a21472012-11-19 12:39:59 +01004324
Amaury Denoyelleb59b8892022-01-25 17:48:47 +01004325 /* listener accept callback */
4326 listener->accept = session_accept_fd;
4327#ifdef USE_QUIC
4328 /* override the accept callback for QUIC listeners. */
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +01004329 if (listener->flags & LI_F_QUIC_LISTENER) {
Amaury Denoyelle996ca7d2022-11-14 16:17:13 +01004330 if (!global.cluster_secret) {
Frédéric Lécaille372508c2022-05-06 08:53:16 +02004331 diag_no_cluster_secret = 1;
Amaury Denoyelle996ca7d2022-11-14 16:17:13 +01004332 if (listener->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
4333 ha_alert("QUIC listener with quic-force-retry requires global cluster-secret to be set.\n");
4334 cfgerr++;
4335 }
4336 }
Amaury Denoyelle0aba11e2022-09-29 18:31:24 +02004337
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +01004338 li_init_per_thr(listener);
4339 }
Amaury Denoyelleb59b8892022-01-25 17:48:47 +01004340#endif
Amaury Denoyelle57af0692022-01-18 15:39:02 +01004341
Willy Tarreauc1a21672009-08-16 22:37:44 +02004342 listener->analysers |= curproxy->fe_req_ana;
Willy Tarreau10b688f2015-03-13 16:43:12 +01004343 listener->default_target = curproxy->default_target;
Willy Tarreau3bc13772008-12-07 11:50:35 +01004344
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02004345 if (!LIST_ISEMPTY(&curproxy->tcp_req.l4_rules))
Willy Tarreau7d9736f2016-10-21 16:34:21 +02004346 listener->options |= LI_O_TCP_L4_RULES;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02004347
Willy Tarreau620408f2016-10-21 16:37:51 +02004348 if (!LIST_ISEMPTY(&curproxy->tcp_req.l5_rules))
4349 listener->options |= LI_O_TCP_L5_RULES;
4350
Willy Tarreau9ea05a72009-06-14 12:07:01 +02004351 /* smart accept mode is automatic in HTTP mode */
4352 if ((curproxy->options2 & PR_O2_SMARTACC) ||
Willy Tarreau11ba4042022-05-20 15:56:32 +02004353 ((curproxy->mode == PR_MODE_HTTP || (listener->bind_conf->options & BC_O_USE_SSL)) &&
Willy Tarreau9ea05a72009-06-14 12:07:01 +02004354 !(curproxy->no_options2 & PR_O2_SMARTACC)))
4355 listener->options |= LI_O_NOQUICKACK;
Willy Tarreaue6b98942007-10-29 01:09:36 +01004356 }
4357
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004358 /* Release unused SSL configs */
4359 list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
Willy Tarreau11ba4042022-05-20 15:56:32 +02004360 if (!(bind_conf->options & BC_O_USE_SSL) && bind_conf->xprt->destroy_bind_conf)
Willy Tarreau795cdab2016-12-22 17:30:54 +01004361 bind_conf->xprt->destroy_bind_conf(bind_conf);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004362 }
Willy Tarreauf5ae8f72012-09-07 16:58:00 +02004363
Willy Tarreau918ff602011-07-25 16:33:49 +02004364 /* create the task associated with the proxy */
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004365 curproxy->task = task_new_anywhere();
Willy Tarreau918ff602011-07-25 16:33:49 +02004366 if (curproxy->task) {
4367 curproxy->task->context = curproxy;
4368 curproxy->task->process = manage_proxy;
Christopher Faulet56717802021-10-13 10:10:09 +02004369 curproxy->flags |= PR_FL_READY;
Willy Tarreau918ff602011-07-25 16:33:49 +02004370 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004371 ha_alert("Proxy '%s': no more memory when trying to allocate the management task\n",
4372 curproxy->id);
Willy Tarreau918ff602011-07-25 16:33:49 +02004373 cfgerr++;
4374 }
Willy Tarreaub369a042014-09-16 13:21:03 +02004375 }
4376
Emeric Brun3b68b602022-08-18 15:53:21 +02004377 /*
4378 * We have just initialized the main proxies list
4379 * we must also configure the log-forward proxies list
4380 */
4381 if (init_proxies_list == proxies_list) {
4382 init_proxies_list = cfg_log_forward;
Emeric Brun8032a272022-08-22 10:25:11 +02004383 /* check if list is not null to avoid infinite loop */
4384 if (init_proxies_list)
4385 goto init_proxies_list_stage2;
Emeric Brun3b68b602022-08-18 15:53:21 +02004386 }
4387
Amaury Denoyelle28ea31c2022-11-14 16:18:46 +01004388 if (diag_no_cluster_secret) {
4389 ha_diag_warning("Generating a random cluster secret. "
4390 "You should define your own one in the configuration to ensure consistency "
4391 "after reload/restart or across your whole cluster.\n");
4392 }
Frédéric Lécaille372508c2022-05-06 08:53:16 +02004393
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01004394 /*
4395 * Recount currently required checks.
4396 */
4397
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004398 for (curproxy=proxies_list; curproxy; curproxy=curproxy->next) {
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01004399 int optnum;
4400
Willy Tarreau66aa61f2009-01-18 21:44:07 +01004401 for (optnum = 0; cfg_opts[optnum].name; optnum++)
4402 if (curproxy->options & cfg_opts[optnum].val)
4403 global.last_checks |= cfg_opts[optnum].checks;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01004404
Willy Tarreau66aa61f2009-01-18 21:44:07 +01004405 for (optnum = 0; cfg_opts2[optnum].name; optnum++)
4406 if (curproxy->options2 & cfg_opts2[optnum].val)
4407 global.last_checks |= cfg_opts2[optnum].checks;
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01004408 }
4409
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02004410 if (cfg_peers) {
4411 struct peers *curpeers = cfg_peers, **last;
Willy Tarreau122541c2011-09-07 21:24:49 +02004412 struct peer *p, *pb;
4413
Willy Tarreau1e273012015-05-01 19:15:17 +02004414 /* Remove all peers sections which don't have a valid listener,
4415 * which are not used by any table, or which are bound to more
4416 * than one process.
Willy Tarreau122541c2011-09-07 21:24:49 +02004417 */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02004418 last = &cfg_peers;
Willy Tarreau122541c2011-09-07 21:24:49 +02004419 while (*last) {
Frédéric Lécaille36d15652022-10-17 14:58:19 +02004420 struct peer *peer;
Frédéric Lécaille87eacbb2020-03-24 20:08:30 +01004421 struct stktable *t;
Willy Tarreau122541c2011-09-07 21:24:49 +02004422 curpeers = *last;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02004423
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02004424 if (curpeers->disabled) {
Willy Tarreau77e4bd12015-05-01 20:02:17 +02004425 /* the "disabled" keyword was present */
4426 if (curpeers->peers_fe)
4427 stop_proxy(curpeers->peers_fe);
4428 curpeers->peers_fe = NULL;
4429 }
Frédéric Lécaille5a4fe5a2019-10-04 08:30:04 +02004430 else if (!curpeers->peers_fe || !curpeers->peers_fe->id) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004431 ha_warning("Removing incomplete section 'peers %s' (no peer named '%s').\n",
4432 curpeers->id, localpeer);
Frédéric Lécaille87eacbb2020-03-24 20:08:30 +01004433 if (curpeers->peers_fe)
4434 stop_proxy(curpeers->peers_fe);
4435 curpeers->peers_fe = NULL;
Willy Tarreau77e4bd12015-05-01 20:02:17 +02004436 }
4437 else {
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +01004438 /* Initializes the transport layer of the server part of all the peers belonging to
4439 * <curpeers> section if required.
4440 * Note that ->srv is used by the local peer of a new process to connect to the local peer
4441 * of an old process.
4442 */
Christopher Faulet56717802021-10-13 10:10:09 +02004443 curpeers->peers_fe->flags |= PR_FL_READY;
Frédéric Lécaille1055e682018-04-26 14:35:21 +02004444 p = curpeers->remote;
4445 while (p) {
Frédéric Lécaille355b2032019-01-11 14:06:12 +01004446 if (p->srv) {
Christopher Fauletf61f33a2020-03-27 18:55:49 +01004447 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 +01004448 cfgerr += xprt_get(XPRT_SSL)->prepare_srv(p->srv);
4449 }
Frédéric Lécaille1055e682018-04-26 14:35:21 +02004450 p = p->next;
4451 }
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +01004452 /* Configure the SSL bindings of the local peer if required. */
4453 if (!LIST_ISEMPTY(&curpeers->peers_fe->conf.bind)) {
4454 struct list *l;
4455 struct bind_conf *bind_conf;
Willy Tarreaud2494e02022-07-05 16:00:56 +02004456 struct listener *li;
4457 unsigned long mask;
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +01004458
4459 l = &curpeers->peers_fe->conf.bind;
4460 bind_conf = LIST_ELEM(l->n, typeof(bind_conf), by_fe);
Willy Tarreaud2494e02022-07-05 16:00:56 +02004461
Christopher Faulet1b6fa7f2022-07-26 19:03:51 +02004462 if (curpeers->local->srv) {
4463 if (curpeers->local->srv->use_ssl == 1 && !(bind_conf->options & BC_O_USE_SSL)) {
4464 ha_warning("Peers section '%s': local peer have a non-SSL listener and a SSL server configured at line %s:%d.\n",
4465 curpeers->peers_fe->id, curpeers->local->conf.file, curpeers->local->conf.line);
4466 }
4467 else if (curpeers->local->srv->use_ssl != 1 && (bind_conf->options & BC_O_USE_SSL)) {
4468 ha_warning("Peers section '%s': local peer have a SSL listener and a non-SSL server configured at line %s:%d.\n",
4469 curpeers->peers_fe->id, curpeers->local->conf.file, curpeers->local->conf.line);
4470 }
4471 }
4472
Willy Tarreaud2494e02022-07-05 16:00:56 +02004473 err = NULL;
4474 if (thread_resolve_group_mask(bind_conf->bind_tgroup, bind_conf->bind_thread,
4475 &bind_conf->bind_tgroup, &bind_conf->bind_thread, &err) < 0) {
4476 ha_alert("Peers section '%s': %s in 'bind %s' at [%s:%d].\n",
4477 curpeers->peers_fe->id, err, bind_conf->arg, bind_conf->file, bind_conf->line);
4478 free(err);
4479 cfgerr++;
Willy Tarreaud0b73bc2022-06-28 08:30:43 +02004480 } else if (!((mask = bind_conf->bind_thread) & ha_tgroup_info[bind_conf->bind_tgroup-1].threads_enabled)) {
Willy Tarreaud2494e02022-07-05 16:00:56 +02004481 unsigned long new_mask = 0;
Willy Tarreaud0b73bc2022-06-28 08:30:43 +02004482 ulong thr_mask = ha_tgroup_info[bind_conf->bind_tgroup-1].threads_enabled;
Willy Tarreaud2494e02022-07-05 16:00:56 +02004483
4484 while (mask) {
Willy Tarreaud0b73bc2022-06-28 08:30:43 +02004485 new_mask |= mask & thr_mask;
4486 mask >>= ha_tgroup_info[bind_conf->bind_tgroup-1].count;
Willy Tarreaud2494e02022-07-05 16:00:56 +02004487 }
4488
4489 bind_conf->bind_thread = new_mask;
Willy Tarreaud0b73bc2022-06-28 08:30:43 +02004490 ha_warning("Peers section '%s': the thread range specified on the 'thread' directive of 'bind %s' at [%s:%d] only refers to thread numbers out of the range supported by thread group %d (%d). The thread numbers were remapped to existing threads instead (mask 0x%lx).\n",
4491 curpeers->peers_fe->id, bind_conf->arg, bind_conf->file, bind_conf->line,
4492 bind_conf->bind_tgroup, ha_tgroup_info[bind_conf->bind_tgroup-1].count, new_mask);
Willy Tarreaud2494e02022-07-05 16:00:56 +02004493 }
4494
4495 /* apply thread masks and groups to all receivers */
4496 list_for_each_entry(li, &bind_conf->listeners, by_bind) {
4497 li->rx.bind_thread = bind_conf->bind_thread;
4498 li->rx.bind_tgroup = bind_conf->bind_tgroup;
4499 }
4500
Frédéric Lécaille76d2cef2019-02-12 19:12:32 +01004501 if (bind_conf->xprt->prepare_bind_conf &&
4502 bind_conf->xprt->prepare_bind_conf(bind_conf) < 0)
4503 cfgerr++;
4504 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02004505 if (!peers_init_sync(curpeers) || !peers_alloc_dcache(curpeers)) {
Willy Tarreaud9443442018-10-15 11:18:03 +02004506 ha_alert("Peers section '%s': out of memory, giving up on peers.\n",
4507 curpeers->id);
4508 cfgerr++;
4509 break;
4510 }
Willy Tarreau122541c2011-09-07 21:24:49 +02004511 last = &curpeers->next;
Frédéric Lécaille36d15652022-10-17 14:58:19 +02004512
4513 /* Ignore the peer shard greater than the number of peer shard for this section.
4514 * Also ignore the peer shard of the local peer.
4515 */
4516 for (peer = curpeers->remote; peer; peer = peer->next) {
4517 if (peer == curpeers->local) {
4518 if (peer->srv->shard) {
4519 ha_warning("Peers section '%s': shard ignored for '%s' local peer\n",
4520 curpeers->id, peer->id);
4521 peer->srv->shard = 0;
4522 }
4523 }
4524 else if (peer->srv->shard > curpeers->nb_shards) {
4525 ha_warning("Peers section '%s': shard ignored for '%s' local peer because "
4526 "%d shard value is greater than the section number of shards (%d)\n",
4527 curpeers->id, peer->id, peer->srv->shard, curpeers->nb_shards);
4528 peer->srv->shard = 0;
4529 }
4530 }
4531
Willy Tarreau122541c2011-09-07 21:24:49 +02004532 continue;
4533 }
4534
Willy Tarreau77e4bd12015-05-01 20:02:17 +02004535 /* clean what has been detected above */
Willy Tarreau122541c2011-09-07 21:24:49 +02004536 p = curpeers->remote;
4537 while (p) {
4538 pb = p->next;
4539 free(p->id);
4540 free(p);
4541 p = pb;
4542 }
4543
4544 /* Destroy and unlink this curpeers section.
4545 * Note: curpeers is backed up into *last.
4546 */
4547 free(curpeers->id);
4548 curpeers = curpeers->next;
Frédéric Lécaille87eacbb2020-03-24 20:08:30 +01004549 /* Reset any refereance to this peers section in the list of stick-tables */
4550 for (t = stktables_list; t; t = t->next) {
4551 if (t->peers.p && t->peers.p == *last)
4552 t->peers.p = NULL;
4553 }
Willy Tarreau122541c2011-09-07 21:24:49 +02004554 free(*last);
4555 *last = curpeers;
4556 }
4557 }
4558
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004559 for (t = stktables_list; t; t = t->next) {
4560 if (t->proxy)
4561 continue;
4562 if (!stktable_init(t)) {
4563 ha_alert("Proxy '%s': failed to initialize stick-table.\n", t->id);
4564 cfgerr++;
4565 }
4566 }
4567
Willy Tarreau6866f3f2015-05-01 19:09:08 +02004568 /* initialize stick-tables on backend capable proxies. This must not
4569 * be done earlier because the data size may be discovered while parsing
4570 * other proxies.
4571 */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004572 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004573 if ((curproxy->flags & PR_FL_DISABLED) || !curproxy->table)
Willy Tarreau6866f3f2015-05-01 19:09:08 +02004574 continue;
4575
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004576 if (!stktable_init(curproxy->table)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004577 ha_alert("Proxy '%s': failed to initialize stick-table.\n", curproxy->id);
Willy Tarreau6866f3f2015-05-01 19:09:08 +02004578 cfgerr++;
4579 }
4580 }
4581
Simon Horman0d16a402015-01-30 11:22:58 +09004582 if (mailers) {
4583 struct mailers *curmailers = mailers, **last;
4584 struct mailer *m, *mb;
4585
4586 /* Remove all mailers sections which don't have a valid listener.
4587 * This can happen when a mailers section is never referenced.
4588 */
4589 last = &mailers;
4590 while (*last) {
4591 curmailers = *last;
4592 if (curmailers->users) {
4593 last = &curmailers->next;
4594 continue;
4595 }
4596
Christopher Faulet767a84b2017-11-24 16:50:31 +01004597 ha_warning("Removing incomplete section 'mailers %s'.\n",
4598 curmailers->id);
Simon Horman0d16a402015-01-30 11:22:58 +09004599
4600 m = curmailers->mailer_list;
4601 while (m) {
4602 mb = m->next;
4603 free(m->id);
4604 free(m);
4605 m = mb;
4606 }
4607
4608 /* Destroy and unlink this curmailers section.
4609 * Note: curmailers is backed up into *last.
4610 */
4611 free(curmailers->id);
4612 curmailers = curmailers->next;
4613 free(*last);
4614 *last = curmailers;
4615 }
4616 }
4617
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02004618 /* Update server_state_file_name to backend name if backend is supposed to use
4619 * a server-state file locally defined and none has been provided */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004620 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02004621 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_LOCAL &&
4622 curproxy->server_state_file_name == NULL)
4623 curproxy->server_state_file_name = strdup(curproxy->id);
4624 }
4625
Emeric Brun750fe792020-12-23 16:51:12 +01004626 list_for_each_entry(curr_resolvers, &sec_resolvers, list) {
Ben Draut054fbee2018-04-13 15:43:04 -06004627 if (LIST_ISEMPTY(&curr_resolvers->nameservers)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02004628 ha_warning("resolvers '%s' [%s:%d] has no nameservers configured!\n",
Ben Draut054fbee2018-04-13 15:43:04 -06004629 curr_resolvers->id, curr_resolvers->conf.file,
4630 curr_resolvers->conf.line);
4631 err_code |= ERR_WARN;
4632 }
4633 }
4634
William Lallemand48b4bb42017-10-23 14:36:34 +02004635 list_for_each_entry(postparser, &postparsers, list) {
4636 if (postparser->func)
4637 cfgerr += postparser->func();
4638 }
4639
Willy Tarreaubb925012009-07-23 13:36:36 +02004640 if (cfgerr > 0)
4641 err_code |= ERR_ALERT | ERR_FATAL;
4642 out:
4643 return err_code;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004644}
4645
Willy Tarreau5b2c3362008-07-09 19:39:06 +02004646/*
4647 * Registers the CFG keyword list <kwl> as a list of valid keywords for next
4648 * parsing sessions.
4649 */
4650void cfg_register_keywords(struct cfg_kw_list *kwl)
4651{
Willy Tarreau2b718102021-04-21 07:32:39 +02004652 LIST_APPEND(&cfg_keywords.list, &kwl->list);
Willy Tarreau5b2c3362008-07-09 19:39:06 +02004653}
Willy Tarreaubaaee002006-06-26 02:48:02 +02004654
Willy Tarreau5b2c3362008-07-09 19:39:06 +02004655/*
4656 * Unregisters the CFG keyword list <kwl> from the list of valid keywords.
4657 */
4658void cfg_unregister_keywords(struct cfg_kw_list *kwl)
4659{
Willy Tarreau2b718102021-04-21 07:32:39 +02004660 LIST_DELETE(&kwl->list);
Willy Tarreau5b2c3362008-07-09 19:39:06 +02004661 LIST_INIT(&kwl->list);
4662}
Willy Tarreaubaaee002006-06-26 02:48:02 +02004663
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01004664/* this function register new section in the haproxy configuration file.
4665 * <section_name> is the name of this new section and <section_parser>
4666 * is the called parser. If two section declaration have the same name,
4667 * only the first declared is used.
4668 */
4669int cfg_register_section(char *section_name,
William Lallemandd2ff56d2017-10-16 11:06:50 +02004670 int (*section_parser)(const char *, int, char **, int),
4671 int (*post_section_parser)())
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01004672{
4673 struct cfg_section *cs;
4674
Willy Tarreau5e4261b2016-05-17 16:16:09 +02004675 list_for_each_entry(cs, &sections, list) {
4676 if (strcmp(cs->section_name, section_name) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004677 ha_alert("register section '%s': already registered.\n", section_name);
Willy Tarreau5e4261b2016-05-17 16:16:09 +02004678 return 0;
4679 }
4680 }
4681
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01004682 cs = calloc(1, sizeof(*cs));
4683 if (!cs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004684 ha_alert("register section '%s': out of memory.\n", section_name);
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01004685 return 0;
4686 }
4687
4688 cs->section_name = section_name;
4689 cs->section_parser = section_parser;
William Lallemandd2ff56d2017-10-16 11:06:50 +02004690 cs->post_section_parser = post_section_parser;
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01004691
Willy Tarreau2b718102021-04-21 07:32:39 +02004692 LIST_APPEND(&sections, &cs->list);
Thierry FOURNIERfa45f1d2014-03-18 13:54:18 +01004693
4694 return 1;
4695}
4696
William Lallemand48b4bb42017-10-23 14:36:34 +02004697/* this function register a new function which will be called once the haproxy
4698 * configuration file has been parsed. It's useful to check dependencies
4699 * between sections or to resolve items once everything is parsed.
4700 */
4701int cfg_register_postparser(char *name, int (*func)())
4702{
4703 struct cfg_postparser *cp;
4704
4705 cp = calloc(1, sizeof(*cp));
4706 if (!cp) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004707 ha_alert("register postparser '%s': out of memory.\n", name);
William Lallemand48b4bb42017-10-23 14:36:34 +02004708 return 0;
4709 }
4710 cp->name = name;
4711 cp->func = func;
4712
Willy Tarreau2b718102021-04-21 07:32:39 +02004713 LIST_APPEND(&postparsers, &cp->list);
William Lallemand48b4bb42017-10-23 14:36:34 +02004714
4715 return 1;
4716}
4717
Willy Tarreaubaaee002006-06-26 02:48:02 +02004718/*
David Carlier845efb52015-09-25 11:49:18 +01004719 * free all config section entries
4720 */
4721void cfg_unregister_sections(void)
4722{
4723 struct cfg_section *cs, *ics;
4724
4725 list_for_each_entry_safe(cs, ics, &sections, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004726 LIST_DELETE(&cs->list);
David Carlier845efb52015-09-25 11:49:18 +01004727 free(cs);
4728 }
4729}
4730
Christopher Faulet7110b402016-10-26 11:09:44 +02004731void cfg_backup_sections(struct list *backup_sections)
4732{
4733 struct cfg_section *cs, *ics;
4734
4735 list_for_each_entry_safe(cs, ics, &sections, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004736 LIST_DELETE(&cs->list);
4737 LIST_APPEND(backup_sections, &cs->list);
Christopher Faulet7110b402016-10-26 11:09:44 +02004738 }
4739}
4740
4741void cfg_restore_sections(struct list *backup_sections)
4742{
4743 struct cfg_section *cs, *ics;
4744
4745 list_for_each_entry_safe(cs, ics, backup_sections, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004746 LIST_DELETE(&cs->list);
4747 LIST_APPEND(&sections, &cs->list);
Christopher Faulet7110b402016-10-26 11:09:44 +02004748 }
4749}
4750
Willy Tarreauca1acd62022-03-29 15:02:44 +02004751/* dumps all registered keywords by section on stdout */
4752void cfg_dump_registered_keywords()
4753{
4754 const char* sect_names[] = { "", "global", "listen", "userlist", "peers", 0 };
4755 int section;
4756 int index;
4757
4758 for (section = 1; sect_names[section]; section++) {
4759 struct cfg_kw_list *kwl;
Willy Tarreaud9058262022-03-30 11:21:32 +02004760 const struct cfg_keyword *kwp, *kwn;
Willy Tarreauca1acd62022-03-29 15:02:44 +02004761
4762 printf("%s\n", sect_names[section]);
Willy Tarreaud9058262022-03-30 11:21:32 +02004763
4764 for (kwn = kwp = NULL;; kwp = kwn) {
4765 list_for_each_entry(kwl, &cfg_keywords.list, list) {
4766 for (index = 0; kwl->kw[index].kw != NULL; index++)
4767 if (kwl->kw[index].section == section &&
4768 strordered(kwp ? kwp->kw : NULL, kwl->kw[index].kw, kwn != kwp ? kwn->kw : NULL))
4769 kwn = &kwl->kw[index];
4770 }
4771 if (kwn == kwp)
4772 break;
4773 printf("\t%s\n", kwn->kw);
Willy Tarreauca1acd62022-03-29 15:02:44 +02004774 }
4775
4776 if (section == CFG_LISTEN) {
4777 /* there are plenty of other keywords there */
4778 extern struct list tcp_req_conn_keywords, tcp_req_sess_keywords,
4779 tcp_req_cont_keywords, tcp_res_cont_keywords;
4780 extern struct bind_kw_list bind_keywords;
4781 extern struct ssl_bind_kw ssl_bind_kws[] __maybe_unused;
4782 extern struct srv_kw_list srv_keywords;
Willy Tarreauca1acd62022-03-29 15:02:44 +02004783 struct bind_kw_list *bkwl;
4784 struct srv_kw_list *skwl;
Willy Tarreaud9058262022-03-30 11:21:32 +02004785 const struct bind_kw *bkwp, *bkwn;
4786 const struct srv_kw *skwp, *skwn;
4787 const struct ssl_bind_kw *sbkwp __maybe_unused, *sbkwn __maybe_unused;
4788 const struct cfg_opt *coptp, *coptn;
Willy Tarreauca1acd62022-03-29 15:02:44 +02004789
Willy Tarreaud9058262022-03-30 11:21:32 +02004790 for (bkwn = bkwp = NULL;; bkwp = bkwn) {
4791 list_for_each_entry(bkwl, &bind_keywords.list, list) {
4792 for (index = 0; bkwl->kw[index].kw != NULL; index++)
4793 if (strordered(bkwp ? bkwp->kw : NULL,
4794 bkwl->kw[index].kw,
4795 bkwn != bkwp ? bkwn->kw : NULL))
4796 bkwn = &bkwl->kw[index];
Willy Tarreauca1acd62022-03-29 15:02:44 +02004797 }
Willy Tarreaud9058262022-03-30 11:21:32 +02004798 if (bkwn == bkwp)
4799 break;
4800
4801 if (!bkwn->skip)
4802 printf("\tbind <addr> %s\n", bkwn->kw);
4803 else
4804 printf("\tbind <addr> %s +%d\n", bkwn->kw, bkwn->skip);
Willy Tarreauca1acd62022-03-29 15:02:44 +02004805 }
4806
4807#if defined(USE_OPENSSL)
Willy Tarreaud9058262022-03-30 11:21:32 +02004808 for (sbkwn = sbkwp = NULL;; sbkwp = sbkwn) {
4809 for (index = 0; ssl_bind_kws[index].kw != NULL; index++) {
4810 if (strordered(sbkwp ? sbkwp->kw : NULL,
4811 ssl_bind_kws[index].kw,
4812 sbkwn != sbkwp ? sbkwn->kw : NULL))
4813 sbkwn = &ssl_bind_kws[index];
4814 }
4815 if (sbkwn == sbkwp)
4816 break;
4817 if (!sbkwn->skip)
4818 printf("\tbind <addr> ssl %s\n", sbkwn->kw);
Willy Tarreauca1acd62022-03-29 15:02:44 +02004819 else
Willy Tarreaud9058262022-03-30 11:21:32 +02004820 printf("\tbind <addr> ssl %s +%d\n", sbkwn->kw, sbkwn->skip);
Willy Tarreauca1acd62022-03-29 15:02:44 +02004821 }
4822#endif
4823
Willy Tarreaud9058262022-03-30 11:21:32 +02004824 for (skwn = skwp = NULL;; skwp = skwn) {
4825 list_for_each_entry(skwl, &srv_keywords.list, list) {
4826 for (index = 0; skwl->kw[index].kw != NULL; index++)
4827 if (strordered(skwp ? skwp->kw : NULL,
4828 skwl->kw[index].kw,
4829 skwn != skwp ? skwn->kw : NULL))
4830 skwn = &skwl->kw[index];
Willy Tarreauca1acd62022-03-29 15:02:44 +02004831 }
Willy Tarreaud9058262022-03-30 11:21:32 +02004832 if (skwn == skwp)
4833 break;
Willy Tarreauca1acd62022-03-29 15:02:44 +02004834
Willy Tarreaud9058262022-03-30 11:21:32 +02004835 if (!skwn->skip)
4836 printf("\tserver <name> <addr> %s\n", skwn->kw);
4837 else
4838 printf("\tserver <name> <addr> %s +%d\n", skwn->kw, skwn->skip);
Willy Tarreauca1acd62022-03-29 15:02:44 +02004839 }
4840
Willy Tarreaud9058262022-03-30 11:21:32 +02004841 for (coptn = coptp = NULL;; coptp = coptn) {
4842 for (index = 0; cfg_opts[index].name; index++)
4843 if (strordered(coptp ? coptp->name : NULL,
4844 cfg_opts[index].name,
4845 coptn != coptp ? coptn->name : NULL))
4846 coptn = &cfg_opts[index];
4847
4848 for (index = 0; cfg_opts2[index].name; index++)
4849 if (strordered(coptp ? coptp->name : NULL,
4850 cfg_opts2[index].name,
4851 coptn != coptp ? coptn->name : NULL))
4852 coptn = &cfg_opts2[index];
4853 if (coptn == coptp)
4854 break;
4855
4856 printf("\toption %s [ ", coptn->name);
4857 if (coptn->cap & PR_CAP_FE)
Willy Tarreauca1acd62022-03-29 15:02:44 +02004858 printf("FE ");
Willy Tarreaud9058262022-03-30 11:21:32 +02004859 if (coptn->cap & PR_CAP_BE)
Willy Tarreauca1acd62022-03-29 15:02:44 +02004860 printf("BE ");
Willy Tarreaud9058262022-03-30 11:21:32 +02004861 if (coptn->mode == PR_MODE_HTTP)
Willy Tarreauca1acd62022-03-29 15:02:44 +02004862 printf("HTTP ");
4863 printf("]\n");
4864 }
4865
Willy Tarreaud9058262022-03-30 11:21:32 +02004866 dump_act_rules(&tcp_req_conn_keywords, "\ttcp-request connection ");
4867 dump_act_rules(&tcp_req_sess_keywords, "\ttcp-request session ");
4868 dump_act_rules(&tcp_req_cont_keywords, "\ttcp-request content ");
4869 dump_act_rules(&tcp_res_cont_keywords, "\ttcp-response content ");
4870 dump_act_rules(&http_req_keywords.list, "\thttp-request ");
4871 dump_act_rules(&http_res_keywords.list, "\thttp-response ");
4872 dump_act_rules(&http_after_res_keywords.list, "\thttp-after-response ");
Willy Tarreauca1acd62022-03-29 15:02:44 +02004873 }
4874 }
4875}
4876
Willy Tarreaue6552512018-11-26 11:33:13 +01004877/* these are the config sections handled by default */
4878REGISTER_CONFIG_SECTION("listen", cfg_parse_listen, NULL);
4879REGISTER_CONFIG_SECTION("frontend", cfg_parse_listen, NULL);
4880REGISTER_CONFIG_SECTION("backend", cfg_parse_listen, NULL);
4881REGISTER_CONFIG_SECTION("defaults", cfg_parse_listen, NULL);
4882REGISTER_CONFIG_SECTION("global", cfg_parse_global, NULL);
4883REGISTER_CONFIG_SECTION("userlist", cfg_parse_users, NULL);
4884REGISTER_CONFIG_SECTION("peers", cfg_parse_peers, NULL);
4885REGISTER_CONFIG_SECTION("mailers", cfg_parse_mailers, NULL);
4886REGISTER_CONFIG_SECTION("namespace_list", cfg_parse_netns, NULL);
Willy Tarreau659fbf02016-05-26 17:55:28 +02004887
Willy Tarreau8a022d52021-04-27 20:29:11 +02004888static struct cfg_kw_list cfg_kws = {{ },{
4889 { CFG_GLOBAL, "default-path", cfg_parse_global_def_path },
4890 { /* END */ }
4891}};
4892
4893INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4894
David Carlier845efb52015-09-25 11:49:18 +01004895/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004896 * Local variables:
4897 * c-indent-level: 8
4898 * c-basic-offset: 8
4899 * End:
4900 */