1 /*
2  * Configuration parser
3  *
4  * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
5  *
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 
13 #ifdef USE_LIBCRYPT
14 /* This is to have crypt() defined on Linux */
15 #define _GNU_SOURCE
16 
17 #ifdef USE_CRYPT_H
18 /* some platforms such as Solaris need this */
19 #include <crypt.h>
20 #endif
21 #endif /* USE_LIBCRYPT */
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <netdb.h>
27 #include <ctype.h>
28 #include <pwd.h>
29 #include <grp.h>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 
36 #include <common/cfgparse.h>
37 #include <common/chunk.h>
38 #include <common/config.h>
39 #include <common/errors.h>
40 #include <common/memory.h>
41 #include <common/standard.h>
42 #include <common/time.h>
43 #include <common/uri_auth.h>
44 #include <common/namespace.h>
45 #include <common/hathreads.h>
46 
47 #include <types/capture.h>
48 #include <types/filters.h>
49 #include <types/global.h>
50 #include <types/obj_type.h>
51 #include <types/peers.h>
52 #include <types/mailers.h>
53 #include <types/dns.h>
54 #include <types/stats.h>
55 
56 #include <proto/acl.h>
57 #include <proto/action.h>
58 #include <proto/auth.h>
59 #include <proto/backend.h>
60 #include <proto/channel.h>
61 #include <proto/checks.h>
62 #include <proto/dns.h>
63 #include <proto/stats.h>
64 #include <proto/filters.h>
65 #include <proto/frontend.h>
66 #include <proto/hdr_idx.h>
67 #include <proto/http_rules.h>
68 #include <proto/lb_chash.h>
69 #include <proto/lb_fas.h>
70 #include <proto/lb_fwlc.h>
71 #include <proto/lb_fwrr.h>
72 #include <proto/lb_map.h>
73 #include <proto/listener.h>
74 #include <proto/log.h>
75 #include <proto/protocol.h>
76 #include <proto/proto_http.h>
77 #include <proto/proxy.h>
78 #include <proto/peers.h>
79 #include <proto/sample.h>
80 #include <proto/session.h>
81 #include <proto/server.h>
82 #include <proto/stream.h>
83 #include <proto/stick_table.h>
84 #include <proto/task.h>
85 #include <proto/tcp_rules.h>
86 #include <proto/connection.h>
87 
88 
89 /* This is the SSLv3 CLIENT HELLO packet used in conjunction with the
90  * ssl-hello-chk option to ensure that the remote server speaks SSL.
91  *
92  * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details.
93  */
94 const char sslv3_client_hello_pkt[] = {
95 	"\x16"                /* ContentType         : 0x16 = Hanshake           */
96 	"\x03\x00"            /* ProtocolVersion     : 0x0300 = SSLv3            */
97 	"\x00\x79"            /* ContentLength       : 0x79 bytes after this one */
98 	"\x01"                /* HanshakeType        : 0x01 = CLIENT HELLO       */
99 	"\x00\x00\x75"        /* HandshakeLength     : 0x75 bytes after this one */
100 	"\x03\x00"            /* Hello Version       : 0x0300 = v3               */
101 	"\x00\x00\x00\x00"    /* Unix GMT Time (s)   : filled with <now> (@0x0B) */
102 	"HAPROXYSSLCHK\nHAPROXYSSLCHK\n" /* Random   : must be exactly 28 bytes  */
103 	"\x00"                /* Session ID length   : empty (no session ID)     */
104 	"\x00\x4E"            /* Cipher Suite Length : 78 bytes after this one   */
105 	"\x00\x01" "\x00\x02" "\x00\x03" "\x00\x04" /* 39 most common ciphers :  */
106 	"\x00\x05" "\x00\x06" "\x00\x07" "\x00\x08" /* 0x01...0x1B, 0x2F...0x3A  */
107 	"\x00\x09" "\x00\x0A" "\x00\x0B" "\x00\x0C" /* This covers RSA/DH,       */
108 	"\x00\x0D" "\x00\x0E" "\x00\x0F" "\x00\x10" /* various bit lengths,      */
109 	"\x00\x11" "\x00\x12" "\x00\x13" "\x00\x14" /* SHA1/MD5, DES/3DES/AES... */
110 	"\x00\x15" "\x00\x16" "\x00\x17" "\x00\x18"
111 	"\x00\x19" "\x00\x1A" "\x00\x1B" "\x00\x2F"
112 	"\x00\x30" "\x00\x31" "\x00\x32" "\x00\x33"
113 	"\x00\x34" "\x00\x35" "\x00\x36" "\x00\x37"
114 	"\x00\x38" "\x00\x39" "\x00\x3A"
115 	"\x01"                /* Compression Length  : 0x01 = 1 byte for types   */
116 	"\x00"                /* Compression Type    : 0x00 = NULL compression   */
117 };
118 
119 /* Used to chain configuration sections definitions. This list
120  * stores struct cfg_section
121  */
122 struct list sections = LIST_HEAD_INIT(sections);
123 
124 struct list postparsers = LIST_HEAD_INIT(postparsers);
125 
126 extern struct proxy *mworker_proxy;
127 
128 char *cursection = NULL;
129 struct proxy defproxy = { };		/* fake proxy used to assign default values on all instances */
130 int cfg_maxpconn = 0;                   /* # of simultaneous connections per proxy (-N) */
131 int cfg_maxconn = 0;			/* # of simultaneous connections, (-n) */
132 char *cfg_scope = NULL;                 /* the current scope during the configuration parsing */
133 
134 /* List head of all known configuration keywords */
135 struct cfg_kw_list cfg_keywords = {
136 	.list = LIST_HEAD_INIT(cfg_keywords.list)
137 };
138 
139 /*
140  * converts <str> to a list of listeners which are dynamically allocated.
141  * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
142  *  - <addr> can be empty or "*" to indicate INADDR_ANY ;
143  *  - <port> is a numerical port from 1 to 65535 ;
144  *  - <end> indicates to use the range from <port> to <end> instead (inclusive).
145  * This can be repeated as many times as necessary, separated by a coma.
146  * Function returns 1 for success or 0 if error. In case of errors, if <err> is
147  * not NULL, it must be a valid pointer to either NULL or a freeable area that
148  * will be replaced with an error message.
149  */
str2listener(char * str,struct proxy * curproxy,struct bind_conf * bind_conf,const char * file,int line,char ** err)150 int str2listener(char *str, struct proxy *curproxy, struct bind_conf *bind_conf, const char *file, int line, char **err)
151 {
152 	char *next, *dupstr;
153 	int port, end;
154 
155 	next = dupstr = strdup(str);
156 
157 	while (next && *next) {
158 		int inherited = 0;
159 		struct sockaddr_storage *ss2;
160 		int fd = -1;
161 
162 		str = next;
163 		/* 1) look for the end of the first address */
164 		if ((next = strchr(str, ',')) != NULL) {
165 			*next++ = 0;
166 		}
167 
168 		ss2 = str2sa_range(str, NULL, &port, &end, err,
169 		                   (curproxy == global.stats_fe || curproxy == mworker_proxy) ? NULL : global.unix_bind.prefix,
170 		                   NULL, 1);
171 		if (!ss2)
172 			goto fail;
173 
174 		if (ss2->ss_family == AF_INET || ss2->ss_family == AF_INET6) {
175 			if (!port && !end) {
176 				memprintf(err, "missing port number: '%s'\n", str);
177 				goto fail;
178 			}
179 
180 			if (!port || !end) {
181 				memprintf(err, "port offsets are not allowed in 'bind': '%s'\n", str);
182 				goto fail;
183 			}
184 
185 			if (port < 1 || port > 65535) {
186 				memprintf(err, "invalid port '%d' specified for address '%s'.\n", port, str);
187 				goto fail;
188 			}
189 
190 			if (end < 1 || end > 65535) {
191 				memprintf(err, "invalid port '%d' specified for address '%s'.\n", end, str);
192 				goto fail;
193 			}
194 		}
195 		else if (ss2->ss_family == AF_UNSPEC) {
196 			socklen_t addr_len;
197 			inherited = 1;
198 
199 			/* We want to attach to an already bound fd whose number
200 			 * is in the addr part of ss2 when cast to sockaddr_in.
201 			 * Note that by definition there is a single listener.
202 			 * We still have to determine the address family to
203 			 * register the correct protocol.
204 			 */
205 			fd = ((struct sockaddr_in *)ss2)->sin_addr.s_addr;
206 			addr_len = sizeof(*ss2);
207 			if (getsockname(fd, (struct sockaddr *)ss2, &addr_len) == -1) {
208 				memprintf(err, "cannot use file descriptor '%d' : %s.\n", fd, strerror(errno));
209 				goto fail;
210 			}
211 
212 			port = end = get_host_port(ss2);
213 
214 		} else if (ss2->ss_family == AF_CUST_SOCKPAIR) {
215 			socklen_t addr_len;
216 			inherited = 1;
217 
218 			fd = ((struct sockaddr_in *)ss2)->sin_addr.s_addr;
219 			addr_len = sizeof(*ss2);
220 			if (getsockname(fd, (struct sockaddr *)ss2, &addr_len) == -1) {
221 				memprintf(err, "cannot use file descriptor '%d' : %s.\n", fd, strerror(errno));
222 				goto fail;
223 			}
224 
225 			ss2->ss_family = AF_CUST_SOCKPAIR; /* reassign AF_CUST_SOCKPAIR because of getsockname */
226 			port = end = 0;
227 		}
228 
229 		/* OK the address looks correct */
230 		if (!create_listeners(bind_conf, ss2, port, end, fd, inherited, err)) {
231 			memprintf(err, "%s for address '%s'.\n", *err, str);
232 			goto fail;
233 		}
234 	} /* end while(next) */
235 	free(dupstr);
236 	return 1;
237  fail:
238 	free(dupstr);
239 	return 0;
240 }
241 
242 /*
243  * Report an error in <msg> when there are too many arguments. This version is
244  * intended to be used by keyword parsers so that the message will be included
245  * into the general error message. The index is the current keyword in args.
246  * Return 0 if the number of argument is correct, otherwise build a message and
247  * return 1. Fill err_code with an ERR_ALERT and an ERR_FATAL if not null. The
248  * message may also be null, it will simply not be produced (useful to check only).
249  * <msg> and <err_code> are only affected on error.
250  */
too_many_args_idx(int maxarg,int index,char ** args,char ** msg,int * err_code)251 int too_many_args_idx(int maxarg, int index, char **args, char **msg, int *err_code)
252 {
253 	int i;
254 
255 	if (!*args[index + maxarg + 1])
256 		return 0;
257 
258 	if (msg) {
259 		*msg = NULL;
260 		memprintf(msg, "%s", args[0]);
261 		for (i = 1; i <= index; i++)
262 			memprintf(msg, "%s %s", *msg, args[i]);
263 
264 		memprintf(msg, "'%s' cannot handle unexpected argument '%s'.", *msg, args[index + maxarg + 1]);
265 	}
266 	if (err_code)
267 		*err_code |= ERR_ALERT | ERR_FATAL;
268 
269 	return 1;
270 }
271 
272 /*
273  * same as too_many_args_idx with a 0 index
274  */
too_many_args(int maxarg,char ** args,char ** msg,int * err_code)275 int too_many_args(int maxarg, char **args, char **msg, int *err_code)
276 {
277 	return too_many_args_idx(maxarg, 0, args, msg, err_code);
278 }
279 
280 /*
281  * Report a fatal Alert when there is too much arguments
282  * The index is the current keyword in args
283  * Return 0 if the number of argument is correct, otherwise emit an alert and return 1
284  * Fill err_code with an ERR_ALERT and an ERR_FATAL
285  */
alertif_too_many_args_idx(int maxarg,int index,const char * file,int linenum,char ** args,int * err_code)286 int alertif_too_many_args_idx(int maxarg, int index, const char *file, int linenum, char **args, int *err_code)
287 {
288 	char *kw = NULL;
289 	int i;
290 
291 	if (!*args[index + maxarg + 1])
292 		return 0;
293 
294 	memprintf(&kw, "%s", args[0]);
295 	for (i = 1; i <= index; i++) {
296 		memprintf(&kw, "%s %s", kw, args[i]);
297 	}
298 
299 	ha_alert("parsing [%s:%d] : '%s' cannot handle unexpected argument '%s'.\n", file, linenum, kw, args[index + maxarg + 1]);
300 	free(kw);
301 	*err_code |= ERR_ALERT | ERR_FATAL;
302 	return 1;
303 }
304 
305 /*
306  * same as alertif_too_many_args_idx with a 0 index
307  */
alertif_too_many_args(int maxarg,const char * file,int linenum,char ** args,int * err_code)308 int alertif_too_many_args(int maxarg, const char *file, int linenum, char **args, int *err_code)
309 {
310 	return alertif_too_many_args_idx(maxarg, 0, file, linenum, args, err_code);
311 }
312 
313 
314 /* Report it if a request ACL condition uses some keywords that are incompatible
315  * with the place where the ACL is used. It returns either 0 or ERR_WARN so that
316  * its result can be or'ed with err_code. Note that <cond> may be NULL and then
317  * will be ignored.
318  */
warnif_cond_conflicts(const struct acl_cond * cond,unsigned int where,const char * file,int line)319 int warnif_cond_conflicts(const struct acl_cond *cond, unsigned int where, const char *file, int line)
320 {
321 	const struct acl *acl;
322 	const char *kw;
323 
324 	if (!cond)
325 		return 0;
326 
327 	acl = acl_cond_conflicts(cond, where);
328 	if (acl) {
329 		if (acl->name && *acl->name)
330 			ha_warning("parsing [%s:%d] : acl '%s' will never match because it only involves keywords that are incompatible with '%s'\n",
331 				   file, line, acl->name, sample_ckp_names(where));
332 		else
333 			ha_warning("parsing [%s:%d] : anonymous acl will never match because it uses keyword '%s' which is incompatible with '%s'\n",
334 				   file, line, LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, sample_ckp_names(where));
335 		return ERR_WARN;
336 	}
337 	if (!acl_cond_kw_conflicts(cond, where, &acl, &kw))
338 		return 0;
339 
340 	if (acl->name && *acl->name)
341 		ha_warning("parsing [%s:%d] : acl '%s' involves keywords '%s' which is incompatible with '%s'\n",
342 			   file, line, acl->name, kw, sample_ckp_names(where));
343 	else
344 		ha_warning("parsing [%s:%d] : anonymous acl involves keyword '%s' which is incompatible with '%s'\n",
345 			   file, line, kw, sample_ckp_names(where));
346 	return ERR_WARN;
347 }
348 
349 /* Parse a string representing a process number or a set of processes. It must
350  * be "all", "odd", "even", a number between 1 and <max> or a range with
351  * two such numbers delimited by a dash ('-'). On success, it returns
352  * 0. otherwise it returns 1 with an error message in <err>.
353  *
354  * Note: this function can also be used to parse a thread number or a set of
355  * threads.
356  */
parse_process_number(const char * arg,unsigned long * proc,int max,int * autoinc,char ** err)357 int parse_process_number(const char *arg, unsigned long *proc, int max, int *autoinc, char **err)
358 {
359 	if (autoinc) {
360 		*autoinc = 0;
361 		if (strncmp(arg, "auto:", 5) == 0) {
362 			arg += 5;
363 			*autoinc = 1;
364 		}
365 	}
366 
367 	if (strcmp(arg, "all") == 0)
368 		*proc |= ~0UL;
369 	else if (strcmp(arg, "odd") == 0)
370 		*proc |= ~0UL/3UL; /* 0x555....555 */
371 	else if (strcmp(arg, "even") == 0)
372 		*proc |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
373 	else {
374 		const char *p, *dash = NULL;
375 		unsigned int low, high;
376 
377 		for (p = arg; *p; p++) {
378 			if (*p == '-' && !dash)
379 				dash = p;
380 			else if (!isdigit((int)*p)) {
381 				memprintf(err, "'%s' is not a valid number/range.", arg);
382 				return -1;
383 			}
384 		}
385 
386 		low = high = str2uic(arg);
387 		if (dash)
388 			high = ((!*(dash+1)) ? max : str2uic(dash + 1));
389 
390 		if (high < low) {
391 			unsigned int swap = low;
392 			low  = high;
393 			high = swap;
394 		}
395 
396 		if (low < 1 || low > max || high > max) {
397 			memprintf(err, "'%s' is not a valid number/range."
398 				  " It supports numbers from 1 to %d.\n",
399 				  arg, max);
400 			return 1;
401 		}
402 
403 		for (;low <= high; low++)
404 			*proc |= 1UL << (low-1);
405 	}
406 	*proc &= ~0UL >> (LONGBITS - max);
407 
408 	return 0;
409 }
410 
411 #ifdef USE_CPU_AFFINITY
412 /* Parse cpu sets. Each CPU set is either a unique number between 0 and
413  * <LONGBITS> or a range with two such numbers delimited by a dash
414  * ('-'). Multiple CPU numbers or ranges may be specified. On success, it
415  * returns 0. otherwise it returns 1 with an error message in <err>.
416  */
parse_cpu_set(const char ** args,unsigned long * cpu_set,char ** err)417 unsigned long parse_cpu_set(const char **args, unsigned long *cpu_set, char **err)
418 {
419 	int cur_arg = 0;
420 
421 	*cpu_set = 0;
422 	while (*args[cur_arg]) {
423 		char        *dash;
424 		unsigned int low, high;
425 
426 		if (!isdigit((int)*args[cur_arg])) {
427 			memprintf(err, "'%s' is not a CPU range.\n", args[cur_arg]);
428 			return -1;
429 		}
430 
431 		low = high = str2uic(args[cur_arg]);
432 		if ((dash = strchr(args[cur_arg], '-')) != NULL)
433 			high = ((!*(dash+1)) ? LONGBITS-1 : str2uic(dash + 1));
434 
435 		if (high < low) {
436 			unsigned int swap = low;
437 			low = high;
438 			high = swap;
439 		}
440 
441 		if (high >= LONGBITS) {
442 			memprintf(err, "supports CPU numbers from 0 to %d.\n", LONGBITS - 1);
443 			return 1;
444 		}
445 
446 		while (low <= high)
447 			*cpu_set |= 1UL << low++;
448 
449 		cur_arg++;
450 	}
451 	return 0;
452 }
453 #endif
454 
init_default_instance()455 void init_default_instance()
456 {
457 	init_new_proxy(&defproxy);
458 	defproxy.mode = PR_MODE_TCP;
459 	defproxy.state = PR_STNEW;
460 	defproxy.maxconn = cfg_maxpconn;
461 	defproxy.conn_retries = CONN_RETRIES;
462 	defproxy.redispatch_after = 0;
463 	defproxy.options = PR_O_REUSE_SAFE;
464 	defproxy.max_out_conns = MAX_SRV_LIST;
465 
466 	defproxy.defsrv.check.inter = DEF_CHKINTR;
467 	defproxy.defsrv.check.fastinter = 0;
468 	defproxy.defsrv.check.downinter = 0;
469 	defproxy.defsrv.agent.inter = DEF_CHKINTR;
470 	defproxy.defsrv.agent.fastinter = 0;
471 	defproxy.defsrv.agent.downinter = 0;
472 	defproxy.defsrv.check.rise = DEF_RISETIME;
473 	defproxy.defsrv.check.fall = DEF_FALLTIME;
474 	defproxy.defsrv.agent.rise = DEF_AGENT_RISETIME;
475 	defproxy.defsrv.agent.fall = DEF_AGENT_FALLTIME;
476 	defproxy.defsrv.check.port = 0;
477 	defproxy.defsrv.agent.port = 0;
478 	defproxy.defsrv.maxqueue = 0;
479 	defproxy.defsrv.minconn = 0;
480 	defproxy.defsrv.maxconn = 0;
481 	defproxy.defsrv.max_reuse = -1;
482 	defproxy.defsrv.max_idle_conns = -1;
483 	defproxy.defsrv.pool_purge_delay = 5000;
484 	defproxy.defsrv.slowstart = 0;
485 	defproxy.defsrv.onerror = DEF_HANA_ONERR;
486 	defproxy.defsrv.consecutive_errors_limit = DEF_HANA_ERRLIMIT;
487 	defproxy.defsrv.uweight = defproxy.defsrv.iweight = 1;
488 
489 	defproxy.email_alert.level = LOG_ALERT;
490 	defproxy.load_server_state_from_file = PR_SRV_STATE_FILE_UNSPEC;
491 }
492 
493 /* Allocate and initialize the frontend of a "peers" section found in
494  * file <file> at line <linenum> with <id> as ID.
495  * Return 0 if succeeded, -1 if not.
496  * Note that this function may be called from "default-server"
497  * or "peer" lines.
498  */
init_peers_frontend(const char * file,int linenum,const char * id,struct peers * peers)499 static int init_peers_frontend(const char *file, int linenum,
500                                const char *id, struct peers *peers)
501 {
502 	struct proxy *p;
503 
504 	if (peers->peers_fe) {
505 		p = peers->peers_fe;
506 		goto out;
507 	}
508 
509 	p = calloc(1, sizeof *p);
510 	if (!p) {
511 		ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
512 		return -1;
513 	}
514 
515 	init_new_proxy(p);
516 	peers_setup_frontend(p);
517 	p->parent = peers;
518 	/* Finally store this frontend. */
519 	peers->peers_fe = p;
520 
521  out:
522 	if (id && !p->id)
523 		p->id = strdup(id);
524 	free(p->conf.file);
525 	p->conf.args.file = p->conf.file = strdup(file);
526 	if (linenum != -1)
527 		p->conf.args.line = p->conf.line = linenum;
528 
529 	return 0;
530 }
531 
532 /* Only change ->file, ->line and ->arg struct bind_conf member values
533  * if already present.
534  */
bind_conf_uniq_alloc(struct proxy * p,const char * file,int line,const char * arg,struct xprt_ops * xprt)535 static struct bind_conf *bind_conf_uniq_alloc(struct proxy *p,
536                                               const char *file, int line,
537                                               const char *arg, struct xprt_ops *xprt)
538 {
539 	struct bind_conf *bind_conf;
540 
541 	if (!LIST_ISEMPTY(&p->conf.bind)) {
542 		bind_conf = LIST_ELEM((&p->conf.bind)->n, typeof(bind_conf), by_fe);
543 		free(bind_conf->file);
544 		bind_conf->file = strdup(file);
545 		bind_conf->line = line;
546 		if (arg) {
547 			free(bind_conf->arg);
548 			bind_conf->arg = strdup(arg);
549 		}
550 	}
551 	else {
552 		bind_conf = bind_conf_alloc(p, file, line, arg, xprt);
553 	}
554 
555 	return bind_conf;
556 }
557 
558 /*
559  * Allocate a new struct peer parsed at line <linenum> in file <file>
560  * to be added to <peers>.
561  * Returns the new allocated structure if succeeded, NULL if not.
562  */
cfg_peers_add_peer(struct peers * peers,const char * file,int linenum,const char * id,int local)563 static struct peer *cfg_peers_add_peer(struct peers *peers,
564                                        const char *file, int linenum,
565                                        const char *id, int local)
566 {
567 	struct peer *p;
568 
569 	p = calloc(1, sizeof *p);
570 	if (!p) {
571 		ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
572 		return NULL;
573 	}
574 
575 	/* the peers are linked backwards first */
576 	peers->count++;
577 	p->next = peers->remote;
578 	peers->remote = p;
579 	p->conf.file = strdup(file);
580 	p->conf.line = linenum;
581 	p->last_change = now.tv_sec;
582 	p->xprt  = xprt_get(XPRT_RAW);
583 	p->sock_init_arg = NULL;
584 	HA_SPIN_INIT(&p->lock);
585 	if (id)
586 		p->id = strdup(id);
587 	if (local) {
588 		p->local = 1;
589 		peers->local = p;
590 	}
591 
592 	return p;
593 }
594 
595 /*
596  * Parse a line in a <listen>, <frontend> or <backend> section.
597  * Returns the error code, 0 if OK, or any combination of :
598  *  - ERR_ABORT: must abort ASAP
599  *  - ERR_FATAL: we can continue parsing but not start the service
600  *  - ERR_WARN: a warning has been emitted
601  *  - ERR_ALERT: an alert has been emitted
602  * Only the two first ones can stop processing, the two others are just
603  * indicators.
604  */
cfg_parse_peers(const char * file,int linenum,char ** args,int kwm)605 int cfg_parse_peers(const char *file, int linenum, char **args, int kwm)
606 {
607 	static struct peers *curpeers = NULL;
608 	struct peer *newpeer = NULL;
609 	const char *err;
610 	struct bind_conf *bind_conf;
611 	struct listener *l;
612 	int err_code = 0;
613 	char *errmsg = NULL;
614 	static int bind_line, peer_line;
615 
616 	if (strcmp(args[0], "bind") == 0 || strcmp(args[0], "default-bind") == 0) {
617 		int cur_arg;
618 		static int kws_dumped;
619 		struct bind_conf *bind_conf;
620 		struct bind_kw *kw;
621 
622 		cur_arg = 1;
623 
624 		if (init_peers_frontend(file, linenum, NULL, curpeers) != 0) {
625 			err_code |= ERR_ALERT | ERR_ABORT;
626 			goto out;
627 		}
628 
629 		bind_conf = bind_conf_uniq_alloc(curpeers->peers_fe, file, linenum,
630 		                                 NULL, xprt_get(XPRT_RAW));
631 		if (*args[0] == 'b') {
632 			struct listener *l;
633 
634 			if (peer_line) {
635 				ha_alert("parsing [%s:%d] : mixing \"peer\" and \"bind\" line is forbidden\n", file, linenum);
636 				err_code |= ERR_ALERT | ERR_FATAL;
637 				goto out;
638 			}
639 
640 			if (!str2listener(args[1], curpeers->peers_fe, bind_conf, file, linenum, &errmsg)) {
641 				if (errmsg && *errmsg) {
642 					indent_msg(&errmsg, 2);
643 					ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
644 				}
645 				else
646 					ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
647 							 file, linenum, args[0], args[1], args[2]);
648 				err_code |= ERR_FATAL;
649 				goto out;
650 			}
651 			l = LIST_ELEM(bind_conf->listeners.n, typeof(l), by_bind);
652 			l->maxaccept = 1;
653 			l->accept = session_accept_fd;
654 			l->analysers |=  curpeers->peers_fe->fe_req_ana;
655 			l->default_target = curpeers->peers_fe->default_target;
656 			l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
657 			global.maxsock++; /* for the listening socket */
658 
659 			bind_line = 1;
660 			if (cfg_peers->local) {
661 				newpeer = cfg_peers->local;
662 			}
663 			else {
664 				/* This peer is local.
665 				 * Note that we do not set the peer ID. This latter is initialized
666 				 * when parsing "peer" or "server" line.
667 				 */
668 				newpeer = cfg_peers_add_peer(curpeers, file, linenum, NULL, 1);
669 				if (!newpeer) {
670 					err_code |= ERR_ALERT | ERR_ABORT;
671 					goto out;
672 				}
673 			}
674 			newpeer->addr = l->addr;
675 			newpeer->proto = protocol_by_family(newpeer->addr.ss_family);
676 			cur_arg++;
677 		}
678 
679 		while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
680 			int ret;
681 
682 			ret = kw->parse(args, cur_arg, curpeers->peers_fe, bind_conf, &errmsg);
683 			err_code |= ret;
684 			if (ret) {
685 				if (errmsg && *errmsg) {
686 					indent_msg(&errmsg, 2);
687 					ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
688 				}
689 				else
690 					ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
691 					         file, linenum, args[cur_arg]);
692 				if (ret & ERR_FATAL)
693 					goto out;
694 			}
695 			cur_arg += 1 + kw->skip;
696 		}
697 		if (*args[cur_arg] != 0) {
698 			char *kws = NULL;
699 
700 			if (!kws_dumped) {
701 				kws_dumped = 1;
702 				bind_dump_kws(&kws);
703 				indent_msg(&kws, 4);
704 			}
705 			ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.%s%s\n",
706 			         file, linenum, args[cur_arg], cursection,
707 			         kws ? " Registered keywords :" : "", kws ? kws: "");
708 			free(kws);
709 			err_code |= ERR_ALERT | ERR_FATAL;
710 			goto out;
711 		}
712 	}
713 	else if (strcmp(args[0], "default-server") == 0) {
714 		if (init_peers_frontend(file, -1, NULL, curpeers) != 0) {
715 			err_code |= ERR_ALERT | ERR_ABORT;
716 			goto out;
717 		}
718 		err_code |= parse_server(file, linenum, args, curpeers->peers_fe, NULL, 0, 1, 1);
719 	}
720 	else if (strcmp(args[0], "peers") == 0) { /* new peers section */
721 		/* Initialize these static variables when entering a new "peers" section*/
722 		bind_line = peer_line = 0;
723 		if (!*args[1]) {
724 			ha_alert("parsing [%s:%d] : missing name for peers section.\n", file, linenum);
725 			err_code |= ERR_ALERT | ERR_ABORT;
726 			goto out;
727 		}
728 
729 		if (alertif_too_many_args(1, file, linenum, args, &err_code))
730 			goto out;
731 
732 		err = invalid_char(args[1]);
733 		if (err) {
734 			ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
735 				 file, linenum, *err, args[0], args[1]);
736 			err_code |= ERR_ALERT | ERR_ABORT;
737 			goto out;
738 		}
739 
740 		for (curpeers = cfg_peers; curpeers != NULL; curpeers = curpeers->next) {
741 			/*
742 			 * If there are two proxies with the same name only following
743 			 * combinations are allowed:
744 			 */
745 			if (strcmp(curpeers->id, args[1]) == 0) {
746 				ha_alert("Parsing [%s:%d]: peers section '%s' has the same name as another peers section declared at %s:%d.\n",
747 					 file, linenum, args[1], curpeers->conf.file, curpeers->conf.line);
748 				err_code |= ERR_ALERT | ERR_FATAL;
749 			}
750 		}
751 
752 		if ((curpeers = calloc(1, sizeof(*curpeers))) == NULL) {
753 			ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
754 			err_code |= ERR_ALERT | ERR_ABORT;
755 			goto out;
756 		}
757 
758 		curpeers->next = cfg_peers;
759 		cfg_peers = curpeers;
760 		curpeers->conf.file = strdup(file);
761 		curpeers->conf.line = linenum;
762 		curpeers->last_change = now.tv_sec;
763 		curpeers->id = strdup(args[1]);
764 		curpeers->state = PR_STNEW;
765 	}
766 	else if (strcmp(args[0], "peer") == 0 ||
767 	         strcmp(args[0], "server") == 0) { /* peer or server definition */
768 		int local_peer, peer;
769 
770 		peer = *args[0] == 'p';
771 		local_peer = !strcmp(args[1], localpeer);
772 		/* The local peer may have already partially been parsed on a "bind" line. */
773 		if (*args[0] == 'p') {
774 			if (bind_line) {
775 				ha_alert("parsing [%s:%d] : mixing \"peer\" and \"bind\" line is forbidden\n", file, linenum);
776 				err_code |= ERR_ALERT | ERR_FATAL;
777 				goto out;
778 			}
779 			peer_line = 1;
780 		}
781 		if (cfg_peers->local && !cfg_peers->local->id && local_peer) {
782 			/* The local peer has already been initialized on a "bind" line.
783 			 * Let's use it and store its ID.
784 			 */
785 			newpeer = cfg_peers->local;
786 			newpeer->id = strdup(localpeer);
787 		}
788 		else {
789 			if (local_peer && cfg_peers->local) {
790 				ha_alert("parsing [%s:%d] : '%s %s' : local peer name already referenced at %s:%d. %s\n",
791 				         file, linenum, args[0], args[1],
792 				 curpeers->peers_fe->conf.file, curpeers->peers_fe->conf.line, cfg_peers->local->id);
793 				err_code |= ERR_FATAL;
794 				goto out;
795 			}
796 			newpeer = cfg_peers_add_peer(curpeers, file, linenum, args[1], local_peer);
797 			if (!newpeer) {
798 				err_code |= ERR_ALERT | ERR_ABORT;
799 				goto out;
800 			}
801 		}
802 
803 		/* Line number and peer ID are updated only if this peer is the local one. */
804 		if (init_peers_frontend(file,
805 		                        newpeer->local ? linenum: -1,
806 		                        newpeer->local ? newpeer->id : NULL,
807 		                        curpeers) != 0) {
808 			err_code |= ERR_ALERT | ERR_ABORT;
809 			goto out;
810 		}
811 
812 		/* This initializes curpeer->peers->peers_fe->srv.
813 		 * The server address is parsed only if we are parsing a "peer" line,
814 		 * or if we are parsing a "server" line and the current peer is not the local one.
815 		 */
816 		err_code |= parse_server(file, linenum, args, curpeers->peers_fe, NULL, peer || !local_peer, 1, 1);
817 		if (!curpeers->peers_fe->srv) {
818 			/* Remove the newly allocated peer. */
819 			if (newpeer != curpeers->local) {
820 				struct peer *p;
821 
822 				p = curpeers->remote;
823 				curpeers->remote = curpeers->remote->next;
824 				free(p->id);
825 				free(p);
826 			}
827 			goto out;
828 		}
829 
830 		/* If the peer address has just been parsed, let's copy it to <newpeer>
831 		 * and initializes ->proto.
832 		 */
833 		if (peer || !local_peer) {
834 			newpeer->addr = curpeers->peers_fe->srv->addr;
835 			newpeer->proto = protocol_by_family(newpeer->addr.ss_family);
836 		}
837 
838 		newpeer->xprt  = xprt_get(XPRT_RAW);
839 		newpeer->sock_init_arg = NULL;
840 		HA_SPIN_INIT(&newpeer->lock);
841 
842 		newpeer->srv = curpeers->peers_fe->srv;
843 		if (!newpeer->local)
844 			goto out;
845 
846 		/* The lines above are reserved to "peer" lines. */
847 		if (*args[0] == 's')
848 			goto out;
849 
850 		bind_conf = bind_conf_uniq_alloc(curpeers->peers_fe, file, linenum, args[2], xprt_get(XPRT_RAW));
851 
852 		if (!str2listener(args[2], curpeers->peers_fe, bind_conf, file, linenum, &errmsg)) {
853 			if (errmsg && *errmsg) {
854 				indent_msg(&errmsg, 2);
855 				ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
856 			}
857 			else
858 				ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
859 				         file, linenum, args[0], args[1], args[2]);
860 			err_code |= ERR_FATAL;
861 			goto out;
862 		}
863 
864 		l = LIST_ELEM(bind_conf->listeners.n, typeof(l), by_bind);
865 		l->maxaccept = 1;
866 		l->accept = session_accept_fd;
867 		l->analysers |=  curpeers->peers_fe->fe_req_ana;
868 		l->default_target = curpeers->peers_fe->default_target;
869 		l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
870 		global.maxsock++; /* for the listening socket */
871 	}
872 	else if (!strcmp(args[0], "table")) {
873 		struct stktable *t, *other;
874 		char *id;
875 		size_t prefix_len;
876 
877 		/* Line number and peer ID are updated only if this peer is the local one. */
878 		if (init_peers_frontend(file, -1, NULL, curpeers) != 0) {
879 			err_code |= ERR_ALERT | ERR_ABORT;
880 			goto out;
881 		}
882 
883 		other = stktable_find_by_name(args[1]);
884 		if (other) {
885 			ha_alert("parsing [%s:%d] : stick-table name '%s' conflicts with table declared in %s '%s' at %s:%d.\n",
886 				 file, linenum, args[1],
887 				 other->proxy ? proxy_cap_str(other->proxy->cap) : "peers",
888 				 other->proxy ? other->id : other->peers.p->id,
889 				 other->conf.file, other->conf.line);
890 			err_code |= ERR_ALERT | ERR_FATAL;
891 			goto out;
892 		}
893 
894 		/* Build the stick-table name, concatenating the "peers" section name
895 		 * followed by a '/' character and the table name argument.
896 		 */
897 		chunk_reset(&trash);
898 		if (!chunk_strcpy(&trash, curpeers->id)) {
899 			ha_alert("parsing [%s:%d]: '%s %s' : stick-table name too long.\n",
900 			         file, linenum, args[0], args[1]);
901 			err_code |= ERR_ALERT | ERR_FATAL;
902 			goto out;
903 		}
904 
905 		prefix_len = trash.data;
906 		if (!chunk_memcat(&trash, "/", 1) || !chunk_strcat(&trash, args[1])) {
907 			ha_alert("parsing [%s:%d]: '%s %s' : stick-table name too long.\n",
908 			         file, linenum, args[0], args[1]);
909 			err_code |= ERR_ALERT | ERR_FATAL;
910 			goto out;
911 		}
912 
913 		t = calloc(1, sizeof *t);
914 		id = strdup(trash.area);
915 		if (!t || !id) {
916 			ha_alert("parsing [%s:%d]: '%s %s' : memory allocation failed\n",
917 			         file, linenum, args[0], args[1]);
918 			free(t);
919 			free(id);
920 			err_code |= ERR_ALERT | ERR_FATAL;
921 			goto out;
922 		}
923 
924 		err_code |= parse_stick_table(file, linenum, args, t, id, id + prefix_len, curpeers);
925 		if (err_code & ERR_FATAL) {
926 			free(t);
927 			free(id);
928 			goto out;
929 		}
930 
931 		stktable_store_name(t);
932 		t->next = stktables_list;
933 		stktables_list = t;
934 	}
935 	else if (!strcmp(args[0], "disabled")) {  /* disables this peers section */
936 		curpeers->state = PR_STSTOPPED;
937 	}
938 	else if (!strcmp(args[0], "enabled")) {  /* enables this peers section (used to revert a disabled default) */
939 		curpeers->state = PR_STNEW;
940 	}
941 	else if (*args[0] != 0) {
942 		ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
943 		err_code |= ERR_ALERT | ERR_FATAL;
944 		goto out;
945 	}
946 
947 out:
948 	free(errmsg);
949 	return err_code;
950 }
951 
952 /*
953  * Parse a <resolvers> section.
954  * Returns the error code, 0 if OK, or any combination of :
955  *  - ERR_ABORT: must abort ASAP
956  *  - ERR_FATAL: we can continue parsing but not start the service
957  *  - ERR_WARN: a warning has been emitted
958  *  - ERR_ALERT: an alert has been emitted
959  * Only the two first ones can stop processing, the two others are just
960  * indicators.
961  */
cfg_parse_resolvers(const char * file,int linenum,char ** args,int kwm)962 int cfg_parse_resolvers(const char *file, int linenum, char **args, int kwm)
963 {
964 	static struct dns_resolvers *curr_resolvers = NULL;
965 	const char *err;
966 	int err_code = 0;
967 	char *errmsg = NULL;
968 
969 	if (strcmp(args[0], "resolvers") == 0) { /* new resolvers section */
970 		if (!*args[1]) {
971 			ha_alert("parsing [%s:%d] : missing name for resolvers section.\n", file, linenum);
972 			err_code |= ERR_ALERT | ERR_ABORT;
973 			goto out;
974 		}
975 
976 		err = invalid_char(args[1]);
977 		if (err) {
978 			ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
979 				 file, linenum, *err, args[0], args[1]);
980 			err_code |= ERR_ALERT | ERR_ABORT;
981 			goto out;
982 		}
983 
984 		list_for_each_entry(curr_resolvers, &dns_resolvers, list) {
985 			/* Error if two resolvers owns the same name */
986 			if (strcmp(curr_resolvers->id, args[1]) == 0) {
987 				ha_alert("Parsing [%s:%d]: resolvers '%s' has same name as another resolvers (declared at %s:%d).\n",
988 					 file, linenum, args[1], curr_resolvers->conf.file, curr_resolvers->conf.line);
989 				err_code |= ERR_ALERT | ERR_ABORT;
990 			}
991 		}
992 
993 		if ((curr_resolvers = calloc(1, sizeof(*curr_resolvers))) == NULL) {
994 			ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
995 			err_code |= ERR_ALERT | ERR_ABORT;
996 			goto out;
997 		}
998 
999 		/* default values */
1000 		LIST_ADDQ(&dns_resolvers, &curr_resolvers->list);
1001 		curr_resolvers->conf.file = strdup(file);
1002 		curr_resolvers->conf.line = linenum;
1003 		curr_resolvers->id = strdup(args[1]);
1004 		curr_resolvers->query_ids = EB_ROOT;
1005 		/* default maximum response size */
1006 		curr_resolvers->accepted_payload_size = 512;
1007 		/* default hold period for nx, other, refuse and timeout is 30s */
1008 		curr_resolvers->hold.nx = 30000;
1009 		curr_resolvers->hold.other = 30000;
1010 		curr_resolvers->hold.refused = 30000;
1011 		curr_resolvers->hold.timeout = 30000;
1012 		curr_resolvers->hold.obsolete = 0;
1013 		/* default hold period for valid is 10s */
1014 		curr_resolvers->hold.valid = 10000;
1015 		curr_resolvers->timeout.resolve = 1000;
1016 		curr_resolvers->timeout.retry   = 1000;
1017 		curr_resolvers->resolve_retries = 3;
1018 		curr_resolvers->nb_nameservers  = 0;
1019 		LIST_INIT(&curr_resolvers->nameservers);
1020 		LIST_INIT(&curr_resolvers->resolutions.curr);
1021 		LIST_INIT(&curr_resolvers->resolutions.wait);
1022 		HA_SPIN_INIT(&curr_resolvers->lock);
1023 	}
1024 	else if (strcmp(args[0], "nameserver") == 0) { /* nameserver definition */
1025 		struct dns_nameserver *newnameserver = NULL;
1026 		struct sockaddr_storage *sk;
1027 		int port1, port2;
1028 		struct protocol *proto;
1029 
1030 		if (!*args[2]) {
1031 			ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1032 				 file, linenum, args[0]);
1033 			err_code |= ERR_ALERT | ERR_FATAL;
1034 			goto out;
1035 		}
1036 
1037 		err = invalid_char(args[1]);
1038 		if (err) {
1039 			ha_alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1040 				 file, linenum, *err, args[1]);
1041 			err_code |= ERR_ALERT | ERR_FATAL;
1042 			goto out;
1043 		}
1044 
1045 		list_for_each_entry(newnameserver, &curr_resolvers->nameservers, list) {
1046 			/* Error if two resolvers owns the same name */
1047 			if (strcmp(newnameserver->id, args[1]) == 0) {
1048 				ha_alert("Parsing [%s:%d]: nameserver '%s' has same name as another nameserver (declared at %s:%d).\n",
1049 					 file, linenum, args[1], newnameserver->conf.file, newnameserver->conf.line);
1050 				err_code |= ERR_ALERT | ERR_FATAL;
1051 			}
1052 		}
1053 
1054 		if ((newnameserver = calloc(1, sizeof(*newnameserver))) == NULL) {
1055 			ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1056 			err_code |= ERR_ALERT | ERR_ABORT;
1057 			goto out;
1058 		}
1059 
1060 		/* the nameservers are linked backward first */
1061 		LIST_ADDQ(&curr_resolvers->nameservers, &newnameserver->list);
1062 		newnameserver->resolvers = curr_resolvers;
1063 		newnameserver->conf.file = strdup(file);
1064 		newnameserver->conf.line = linenum;
1065 		newnameserver->id = strdup(args[1]);
1066 
1067 		sk = str2sa_range(args[2], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
1068 		if (!sk) {
1069 			ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1070 			err_code |= ERR_ALERT | ERR_FATAL;
1071 			goto out;
1072 		}
1073 
1074 		proto = protocol_by_family(sk->ss_family);
1075 		if (!proto || !proto->connect) {
1076 			ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1077 				file, linenum, args[0], args[1]);
1078 			err_code |= ERR_ALERT | ERR_FATAL;
1079 			goto out;
1080 		}
1081 
1082 		if (port1 != port2) {
1083 			ha_alert("parsing [%s:%d] : '%s %s' : port ranges and offsets are not allowed in '%s'\n",
1084 				 file, linenum, args[0], args[1], args[2]);
1085 			err_code |= ERR_ALERT | ERR_FATAL;
1086 			goto out;
1087 		}
1088 
1089 		if (!port1 && !port2) {
1090 			ha_alert("parsing [%s:%d] : '%s %s' : no UDP port specified\n",
1091 				 file, linenum, args[0], args[1]);
1092 			err_code |= ERR_ALERT | ERR_FATAL;
1093 			goto out;
1094 		}
1095 
1096 		newnameserver->addr = *sk;
1097 	}
1098 	else if (strcmp(args[0], "parse-resolv-conf") == 0) {
1099 		struct dns_nameserver *newnameserver = NULL;
1100 		const char *whitespace = "\r\n\t ";
1101 		char *resolv_line = NULL;
1102 		int resolv_linenum = 0;
1103 		FILE *f = NULL;
1104 		char *address = NULL;
1105 		struct sockaddr_storage *sk = NULL;
1106 		struct protocol *proto;
1107 		int duplicate_name = 0;
1108 
1109 		if ((resolv_line = malloc(sizeof(*resolv_line) * LINESIZE)) == NULL) {
1110 			ha_alert("parsing [%s:%d] : out of memory.\n",
1111 				 file, linenum);
1112 			err_code |= ERR_ALERT | ERR_FATAL;
1113 			goto resolv_out;
1114 		}
1115 
1116 		if ((f = fopen("/etc/resolv.conf", "r")) == NULL) {
1117 			ha_alert("parsing [%s:%d] : failed to open /etc/resolv.conf.\n",
1118 				 file, linenum);
1119 			err_code |= ERR_ALERT | ERR_FATAL;
1120 			goto resolv_out;
1121 		}
1122 
1123 		sk = calloc(1, sizeof(*sk));
1124 		if (sk == NULL) {
1125 			ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n",
1126 				 resolv_linenum);
1127 			err_code |= ERR_ALERT | ERR_FATAL;
1128 			goto resolv_out;
1129 		}
1130 
1131 		while (fgets(resolv_line, LINESIZE, f) != NULL) {
1132 			resolv_linenum++;
1133 			if (strncmp(resolv_line, "nameserver", 10) != 0)
1134 				continue;
1135 
1136 			address = strtok(resolv_line + 10, whitespace);
1137 			if (address == resolv_line + 10)
1138 				continue;
1139 
1140 			if (address == NULL) {
1141 				ha_warning("parsing [/etc/resolv.conf:%d] : nameserver line is missing address.\n",
1142 					   resolv_linenum);
1143 				err_code |= ERR_WARN;
1144 				continue;
1145 			}
1146 
1147 			duplicate_name = 0;
1148 			list_for_each_entry(newnameserver, &curr_resolvers->nameservers, list) {
1149 				if (strcmp(newnameserver->id, address) == 0) {
1150 					ha_warning("Parsing [/etc/resolv.conf:%d] : generated name for /etc/resolv.conf nameserver '%s' conflicts with another nameserver (declared at %s:%d), it appears to be a duplicate and will be excluded.\n",
1151 						 resolv_linenum, address, newnameserver->conf.file, newnameserver->conf.line);
1152 					err_code |= ERR_WARN;
1153 					duplicate_name = 1;
1154 				}
1155 			}
1156 
1157 			if (duplicate_name)
1158 				continue;
1159 
1160 			memset(sk, 0, sizeof(*sk));
1161 			if (!str2ip2(address, sk, 1)) {
1162 				ha_warning("parsing [/etc/resolv.conf:%d] : address '%s' could not be recognized, nameserver will be excluded.\n",
1163 					   resolv_linenum, address);
1164 				err_code |= ERR_WARN;
1165 				continue;
1166 			}
1167 
1168 			set_host_port(sk, 53);
1169 
1170 			proto = protocol_by_family(sk->ss_family);
1171 			if (!proto || !proto->connect) {
1172 				ha_warning("parsing [/etc/resolv.conf:%d] : '%s' : connect() not supported for this address family.\n",
1173 					   resolv_linenum, address);
1174 				err_code |= ERR_WARN;
1175 				continue;
1176 			}
1177 
1178 			if ((newnameserver = calloc(1, sizeof(*newnameserver))) == NULL) {
1179 				ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
1180 				err_code |= ERR_ALERT | ERR_FATAL;
1181 				goto resolv_out;
1182 			}
1183 
1184 			newnameserver->conf.file = strdup("/etc/resolv.conf");
1185 			if (newnameserver->conf.file == NULL) {
1186 				ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
1187 				err_code |= ERR_ALERT | ERR_FATAL;
1188 				free(newnameserver);
1189 				goto resolv_out;
1190 			}
1191 
1192 			newnameserver->id = strdup(address);
1193 			if (newnameserver->id == NULL) {
1194 				ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
1195 				err_code |= ERR_ALERT | ERR_FATAL;
1196 				free((char *)newnameserver->conf.file);
1197 				free(newnameserver);
1198 				goto resolv_out;
1199 			}
1200 
1201 			newnameserver->resolvers = curr_resolvers;
1202 			newnameserver->conf.line = resolv_linenum;
1203 			newnameserver->addr = *sk;
1204 
1205 			LIST_ADDQ(&curr_resolvers->nameservers, &newnameserver->list);
1206 		}
1207 
1208 resolv_out:
1209 		free(sk);
1210 		free(resolv_line);
1211 		if (f != NULL)
1212 			fclose(f);
1213 	}
1214 	else if (strcmp(args[0], "hold") == 0) { /* hold periods */
1215 		const char *res;
1216 		unsigned int time;
1217 
1218 		if (!*args[2]) {
1219 			ha_alert("parsing [%s:%d] : '%s' expects an <event> and a <time> as arguments.\n",
1220 				 file, linenum, args[0]);
1221 			ha_alert("<event> can be either 'valid', 'nx', 'refused', 'timeout', or 'other'\n");
1222 			err_code |= ERR_ALERT | ERR_FATAL;
1223 			goto out;
1224 		}
1225 		res = parse_time_err(args[2], &time, TIME_UNIT_MS);
1226 		if (res == PARSE_TIME_OVER) {
1227 			ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n",
1228 			         file, linenum, args[1], args[0]);
1229 			err_code |= ERR_ALERT | ERR_FATAL;
1230 			goto out;
1231 		}
1232 		else if (res == PARSE_TIME_UNDER) {
1233 			ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
1234 			         file, linenum, args[1], args[0]);
1235 			err_code |= ERR_ALERT | ERR_FATAL;
1236 			goto out;
1237 		}
1238 		else if (res) {
1239 			ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s>.\n",
1240 				 file, linenum, *res, args[0]);
1241 			err_code |= ERR_ALERT | ERR_FATAL;
1242 			goto out;
1243 		}
1244 		if (strcmp(args[1], "nx") == 0)
1245 			curr_resolvers->hold.nx = time;
1246 		else if (strcmp(args[1], "other") == 0)
1247 			curr_resolvers->hold.other = time;
1248 		else if (strcmp(args[1], "refused") == 0)
1249 			curr_resolvers->hold.refused = time;
1250 		else if (strcmp(args[1], "timeout") == 0)
1251 			curr_resolvers->hold.timeout = time;
1252 		else if (strcmp(args[1], "valid") == 0)
1253 			curr_resolvers->hold.valid = time;
1254 		else if (strcmp(args[1], "obsolete") == 0)
1255 			curr_resolvers->hold.obsolete = time;
1256 		else {
1257 			ha_alert("parsing [%s:%d] : '%s' unknown <event>: '%s', expects either 'nx', 'timeout', 'valid', 'obsolete' or 'other'.\n",
1258 				 file, linenum, args[0], args[1]);
1259 			err_code |= ERR_ALERT | ERR_FATAL;
1260 			goto out;
1261 		}
1262 
1263 	}
1264 	else if (strcmp(args[0], "accepted_payload_size") == 0) {
1265 		int i = 0;
1266 
1267 		if (!*args[1]) {
1268 			ha_alert("parsing [%s:%d] : '%s' expects <nb> as argument.\n",
1269 				 file, linenum, args[0]);
1270 			err_code |= ERR_ALERT | ERR_FATAL;
1271 			goto out;
1272 		}
1273 
1274 		i = atoi(args[1]);
1275 		if (i < DNS_HEADER_SIZE || i > DNS_MAX_UDP_MESSAGE) {
1276 			ha_alert("parsing [%s:%d] : '%s' must be between %d and %d inclusive (was %s).\n",
1277 				 file, linenum, args[0], DNS_HEADER_SIZE, DNS_MAX_UDP_MESSAGE, args[1]);
1278 			err_code |= ERR_ALERT | ERR_FATAL;
1279 			goto out;
1280 		}
1281 
1282 		curr_resolvers->accepted_payload_size = i;
1283 	}
1284 	else if (strcmp(args[0], "resolution_pool_size") == 0) {
1285 		ha_alert("parsing [%s:%d] : '%s' directive is not supported anymore (it never appeared in a stable release).\n",
1286 			   file, linenum, args[0]);
1287 		err_code |= ERR_ALERT | ERR_FATAL;
1288 		goto out;
1289 	}
1290 	else if (strcmp(args[0], "resolve_retries") == 0) {
1291 		if (!*args[1]) {
1292 			ha_alert("parsing [%s:%d] : '%s' expects <nb> as argument.\n",
1293 				 file, linenum, args[0]);
1294 			err_code |= ERR_ALERT | ERR_FATAL;
1295 			goto out;
1296 		}
1297 		curr_resolvers->resolve_retries = atoi(args[1]);
1298 	}
1299 	else if (strcmp(args[0], "timeout") == 0) {
1300 		if (!*args[1]) {
1301 			ha_alert("parsing [%s:%d] : '%s' expects 'retry' or 'resolve' and <time> as arguments.\n",
1302 				 file, linenum, args[0]);
1303 			err_code |= ERR_ALERT | ERR_FATAL;
1304 			goto out;
1305 		}
1306 		else if (strcmp(args[1], "retry") == 0 ||
1307 			 strcmp(args[1], "resolve") == 0) {
1308 			const char *res;
1309 			unsigned int tout;
1310 
1311 			if (!*args[2]) {
1312 				ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
1313 					 file, linenum, args[0], args[1]);
1314 				err_code |= ERR_ALERT | ERR_FATAL;
1315 				goto out;
1316 			}
1317 			res = parse_time_err(args[2], &tout, TIME_UNIT_MS);
1318 			if (res == PARSE_TIME_OVER) {
1319 				ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
1320 					 file, linenum, args[2], args[0], args[1]);
1321 				err_code |= ERR_ALERT | ERR_FATAL;
1322 				goto out;
1323 			}
1324 			else if (res == PARSE_TIME_UNDER) {
1325 				ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
1326 					 file, linenum, args[2], args[0], args[1]);
1327 				err_code |= ERR_ALERT | ERR_FATAL;
1328 				goto out;
1329 			}
1330 			else if (res) {
1331 				ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
1332 					 file, linenum, *res, args[0], args[1]);
1333 				err_code |= ERR_ALERT | ERR_FATAL;
1334 				goto out;
1335 			}
1336 			if (args[1][2] == 't')
1337 				curr_resolvers->timeout.retry = tout;
1338 			else
1339 				curr_resolvers->timeout.resolve = tout;
1340 		}
1341 		else {
1342 			ha_alert("parsing [%s:%d] : '%s' expects 'retry' or 'resolve' and <time> as arguments got '%s'.\n",
1343 				 file, linenum, args[0], args[1]);
1344 			err_code |= ERR_ALERT | ERR_FATAL;
1345 			goto out;
1346 		}
1347 	}
1348 	else if (*args[0] != 0) {
1349 		ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
1350 		err_code |= ERR_ALERT | ERR_FATAL;
1351 		goto out;
1352 	}
1353 
1354  out:
1355 	free(errmsg);
1356 	return err_code;
1357 }
1358 
1359 /*
1360  * Parse a line in a <listen>, <frontend> or <backend> section.
1361  * Returns the error code, 0 if OK, or any combination of :
1362  *  - ERR_ABORT: must abort ASAP
1363  *  - ERR_FATAL: we can continue parsing but not start the service
1364  *  - ERR_WARN: a warning has been emitted
1365  *  - ERR_ALERT: an alert has been emitted
1366  * Only the two first ones can stop processing, the two others are just
1367  * indicators.
1368  */
cfg_parse_mailers(const char * file,int linenum,char ** args,int kwm)1369 int cfg_parse_mailers(const char *file, int linenum, char **args, int kwm)
1370 {
1371 	static struct mailers *curmailers = NULL;
1372 	struct mailer *newmailer = NULL;
1373 	const char *err;
1374 	int err_code = 0;
1375 	char *errmsg = NULL;
1376 
1377 	if (strcmp(args[0], "mailers") == 0) { /* new mailers section */
1378 		if (!*args[1]) {
1379 			ha_alert("parsing [%s:%d] : missing name for mailers section.\n", file, linenum);
1380 			err_code |= ERR_ALERT | ERR_ABORT;
1381 			goto out;
1382 		}
1383 
1384 		err = invalid_char(args[1]);
1385 		if (err) {
1386 			ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1387 				 file, linenum, *err, args[0], args[1]);
1388 			err_code |= ERR_ALERT | ERR_ABORT;
1389 			goto out;
1390 		}
1391 
1392 		for (curmailers = mailers; curmailers != NULL; curmailers = curmailers->next) {
1393 			/*
1394 			 * If there are two proxies with the same name only following
1395 			 * combinations are allowed:
1396 			 */
1397 			if (strcmp(curmailers->id, args[1]) == 0) {
1398 				ha_alert("Parsing [%s:%d]: mailers section '%s' has the same name as another mailers section declared at %s:%d.\n",
1399 					 file, linenum, args[1], curmailers->conf.file, curmailers->conf.line);
1400 				err_code |= ERR_ALERT | ERR_FATAL;
1401 			}
1402 		}
1403 
1404 		if ((curmailers = calloc(1, sizeof(*curmailers))) == NULL) {
1405 			ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1406 			err_code |= ERR_ALERT | ERR_ABORT;
1407 			goto out;
1408 		}
1409 
1410 		curmailers->next = mailers;
1411 		mailers = curmailers;
1412 		curmailers->conf.file = strdup(file);
1413 		curmailers->conf.line = linenum;
1414 		curmailers->id = strdup(args[1]);
1415 		curmailers->timeout.mail = DEF_MAILALERTTIME;/* XXX: Would like to Skip to the next alert, if any, ASAP.
1416 			* But need enough time so that timeouts don't occur
1417 			* during tcp procssing. For now just us an arbitrary default. */
1418 	}
1419 	else if (strcmp(args[0], "mailer") == 0) { /* mailer definition */
1420 		struct sockaddr_storage *sk;
1421 		int port1, port2;
1422 		struct protocol *proto;
1423 
1424 		if (!*args[2]) {
1425 			ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1426 				 file, linenum, args[0]);
1427 			err_code |= ERR_ALERT | ERR_FATAL;
1428 			goto out;
1429 		}
1430 
1431 		err = invalid_char(args[1]);
1432 		if (err) {
1433 			ha_alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1434 				 file, linenum, *err, args[1]);
1435 			err_code |= ERR_ALERT | ERR_FATAL;
1436 			goto out;
1437 		}
1438 
1439 		if ((newmailer = calloc(1, sizeof(*newmailer))) == NULL) {
1440 			ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1441 			err_code |= ERR_ALERT | ERR_ABORT;
1442 			goto out;
1443 		}
1444 
1445 		/* the mailers are linked backwards first */
1446 		curmailers->count++;
1447 		newmailer->next = curmailers->mailer_list;
1448 		curmailers->mailer_list = newmailer;
1449 		newmailer->mailers = curmailers;
1450 		newmailer->conf.file = strdup(file);
1451 		newmailer->conf.line = linenum;
1452 
1453 		newmailer->id = strdup(args[1]);
1454 
1455 		sk = str2sa_range(args[2], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
1456 		if (!sk) {
1457 			ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1458 			err_code |= ERR_ALERT | ERR_FATAL;
1459 			goto out;
1460 		}
1461 
1462 		proto = protocol_by_family(sk->ss_family);
1463 		if (!proto || !proto->connect || proto->sock_prot != IPPROTO_TCP) {
1464 			ha_alert("parsing [%s:%d] : '%s %s' : TCP not supported for this address family.\n",
1465 				 file, linenum, args[0], args[1]);
1466 			err_code |= ERR_ALERT | ERR_FATAL;
1467 			goto out;
1468 		}
1469 
1470 		if (port1 != port2) {
1471 			ha_alert("parsing [%s:%d] : '%s %s' : port ranges and offsets are not allowed in '%s'\n",
1472 				 file, linenum, args[0], args[1], args[2]);
1473 			err_code |= ERR_ALERT | ERR_FATAL;
1474 			goto out;
1475 		}
1476 
1477 		if (!port1) {
1478 			ha_alert("parsing [%s:%d] : '%s %s' : missing or invalid port in '%s'\n",
1479 				 file, linenum, args[0], args[1], args[2]);
1480 			err_code |= ERR_ALERT | ERR_FATAL;
1481 			goto out;
1482 		}
1483 
1484 		newmailer->addr = *sk;
1485 		newmailer->proto = proto;
1486 		newmailer->xprt  = xprt_get(XPRT_RAW);
1487 		newmailer->sock_init_arg = NULL;
1488 	}
1489 	else if (strcmp(args[0], "timeout") == 0) {
1490 		if (!*args[1]) {
1491 			ha_alert("parsing [%s:%d] : '%s' expects 'mail' and <time> as arguments.\n",
1492 				 file, linenum, args[0]);
1493 			err_code |= ERR_ALERT | ERR_FATAL;
1494 			goto out;
1495 		}
1496 		else if (strcmp(args[1], "mail") == 0) {
1497 			const char *res;
1498 			unsigned int timeout_mail;
1499 			if (!*args[2]) {
1500 				ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
1501 					 file, linenum, args[0], args[1]);
1502 				err_code |= ERR_ALERT | ERR_FATAL;
1503 				goto out;
1504 			}
1505 			res = parse_time_err(args[2], &timeout_mail, TIME_UNIT_MS);
1506 			if (res == PARSE_TIME_OVER) {
1507 				ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
1508 					 file, linenum, args[2], args[0], args[1]);
1509 				err_code |= ERR_ALERT | ERR_FATAL;
1510 				goto out;
1511 			}
1512 			else if (res == PARSE_TIME_UNDER) {
1513 				ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
1514 					 file, linenum, args[2], args[0], args[1]);
1515 				err_code |= ERR_ALERT | ERR_FATAL;
1516 				goto out;
1517 			}
1518 			else if (res) {
1519 				ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
1520 					 file, linenum, *res, args[0], args[1]);
1521 				err_code |= ERR_ALERT | ERR_FATAL;
1522 				goto out;
1523 			}
1524 			curmailers->timeout.mail = timeout_mail;
1525 		} else {
1526 			ha_alert("parsing [%s:%d] : '%s' expects 'mail' and <time> as arguments got '%s'.\n",
1527 				file, linenum, args[0], args[1]);
1528 			err_code |= ERR_ALERT | ERR_FATAL;
1529 			goto out;
1530 		}
1531 	}
1532 	else if (*args[0] != 0) {
1533 		ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
1534 		err_code |= ERR_ALERT | ERR_FATAL;
1535 		goto out;
1536 	}
1537 
1538 out:
1539 	free(errmsg);
1540 	return err_code;
1541 }
1542 
free_email_alert(struct proxy * p)1543 void free_email_alert(struct proxy *p)
1544 {
1545 	free(p->email_alert.mailers.name);
1546 	p->email_alert.mailers.name = NULL;
1547 	free(p->email_alert.from);
1548 	p->email_alert.from = NULL;
1549 	free(p->email_alert.to);
1550 	p->email_alert.to = NULL;
1551 	free(p->email_alert.myhostname);
1552 	p->email_alert.myhostname = NULL;
1553 }
1554 
1555 
1556 int
cfg_parse_netns(const char * file,int linenum,char ** args,int kwm)1557 cfg_parse_netns(const char *file, int linenum, char **args, int kwm)
1558 {
1559 #ifdef USE_NS
1560 	const char *err;
1561 	const char *item = args[0];
1562 
1563 	if (!strcmp(item, "namespace_list")) {
1564 		return 0;
1565 	}
1566 	else if (!strcmp(item, "namespace")) {
1567 		size_t idx = 1;
1568 		const char *current;
1569 		while (*(current = args[idx++])) {
1570 			err = invalid_char(current);
1571 			if (err) {
1572 				ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1573 					 file, linenum, *err, item, current);
1574 				return ERR_ALERT | ERR_FATAL;
1575 			}
1576 
1577 			if (netns_store_lookup(current, strlen(current))) {
1578 				ha_alert("parsing [%s:%d]: Namespace '%s' is already added.\n",
1579 					 file, linenum, current);
1580 				return ERR_ALERT | ERR_FATAL;
1581 			}
1582 			if (!netns_store_insert(current)) {
1583 				ha_alert("parsing [%s:%d]: Cannot open namespace '%s'.\n",
1584 					 file, linenum, current);
1585 				return ERR_ALERT | ERR_FATAL;
1586 			}
1587 		}
1588 	}
1589 
1590 	return 0;
1591 #else
1592 	ha_alert("parsing [%s:%d]: namespace support is not compiled in.",
1593 		 file, linenum);
1594 	return ERR_ALERT | ERR_FATAL;
1595 #endif
1596 }
1597 
1598 int
cfg_parse_users(const char * file,int linenum,char ** args,int kwm)1599 cfg_parse_users(const char *file, int linenum, char **args, int kwm)
1600 {
1601 
1602 	int err_code = 0;
1603 	const char *err;
1604 
1605 	if (!strcmp(args[0], "userlist")) {		/* new userlist */
1606 		struct userlist *newul;
1607 
1608 		if (!*args[1]) {
1609 			ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1610 				 file, linenum, args[0]);
1611 			err_code |= ERR_ALERT | ERR_FATAL;
1612 			goto out;
1613 		}
1614 		if (alertif_too_many_args(1, file, linenum, args, &err_code))
1615 			goto out;
1616 
1617 		err = invalid_char(args[1]);
1618 		if (err) {
1619 			ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1620 				 file, linenum, *err, args[0], args[1]);
1621 			err_code |= ERR_ALERT | ERR_FATAL;
1622 			goto out;
1623 		}
1624 
1625 		for (newul = userlist; newul; newul = newul->next)
1626 			if (!strcmp(newul->name, args[1])) {
1627 				ha_warning("parsing [%s:%d]: ignoring duplicated userlist '%s'.\n",
1628 					   file, linenum, args[1]);
1629 				err_code |= ERR_WARN;
1630 				goto out;
1631 			}
1632 
1633 		newul = calloc(1, sizeof(*newul));
1634 		if (!newul) {
1635 			ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
1636 			err_code |= ERR_ALERT | ERR_ABORT;
1637 			goto out;
1638 		}
1639 
1640 		newul->name = strdup(args[1]);
1641 		if (!newul->name) {
1642 			ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
1643 			err_code |= ERR_ALERT | ERR_ABORT;
1644 			free(newul);
1645 			goto out;
1646 		}
1647 
1648 		newul->next = userlist;
1649 		userlist = newul;
1650 
1651 	} else if (!strcmp(args[0], "group")) {  	/* new group */
1652 		int cur_arg;
1653 		const char *err;
1654 		struct auth_groups *ag;
1655 
1656 		if (!*args[1]) {
1657 			ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1658 				 file, linenum, args[0]);
1659 			err_code |= ERR_ALERT | ERR_FATAL;
1660 			goto out;
1661 		}
1662 
1663 		err = invalid_char(args[1]);
1664 		if (err) {
1665 			ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
1666 				 file, linenum, *err, args[0], args[1]);
1667 			err_code |= ERR_ALERT | ERR_FATAL;
1668 			goto out;
1669 		}
1670 
1671 		if (!userlist)
1672 			goto out;
1673 
1674 		for (ag = userlist->groups; ag; ag = ag->next)
1675 			if (!strcmp(ag->name, args[1])) {
1676 				ha_warning("parsing [%s:%d]: ignoring duplicated group '%s' in userlist '%s'.\n",
1677 					   file, linenum, args[1], userlist->name);
1678 				err_code |= ERR_ALERT;
1679 				goto out;
1680 			}
1681 
1682 		ag = calloc(1, sizeof(*ag));
1683 		if (!ag) {
1684 			ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
1685 			err_code |= ERR_ALERT | ERR_ABORT;
1686 			goto out;
1687 		}
1688 
1689 		ag->name = strdup(args[1]);
1690 		if (!ag->name) {
1691 			ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
1692 			err_code |= ERR_ALERT | ERR_ABORT;
1693 			free(ag);
1694 			goto out;
1695 		}
1696 
1697 		cur_arg = 2;
1698 
1699 		while (*args[cur_arg]) {
1700 			if (!strcmp(args[cur_arg], "users")) {
1701 				ag->groupusers = strdup(args[cur_arg + 1]);
1702 				cur_arg += 2;
1703 				continue;
1704 			} else {
1705 				ha_alert("parsing [%s:%d]: '%s' only supports 'users' option.\n",
1706 					 file, linenum, args[0]);
1707 				err_code |= ERR_ALERT | ERR_FATAL;
1708 				free(ag->groupusers);
1709 				free(ag->name);
1710 				free(ag);
1711 				goto out;
1712 			}
1713 		}
1714 
1715 		ag->next = userlist->groups;
1716 		userlist->groups = ag;
1717 
1718 	} else if (!strcmp(args[0], "user")) {		/* new user */
1719 		struct auth_users *newuser;
1720 		int cur_arg;
1721 
1722 		if (!*args[1]) {
1723 			ha_alert("parsing [%s:%d]: '%s' expects <name> as arguments.\n",
1724 				 file, linenum, args[0]);
1725 			err_code |= ERR_ALERT | ERR_FATAL;
1726 			goto out;
1727 		}
1728 		if (!userlist)
1729 			goto out;
1730 
1731 		for (newuser = userlist->users; newuser; newuser = newuser->next)
1732 			if (!strcmp(newuser->user, args[1])) {
1733 				ha_warning("parsing [%s:%d]: ignoring duplicated user '%s' in userlist '%s'.\n",
1734 					   file, linenum, args[1], userlist->name);
1735 				err_code |= ERR_ALERT;
1736 				goto out;
1737 			}
1738 
1739 		newuser = calloc(1, sizeof(*newuser));
1740 		if (!newuser) {
1741 			ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
1742 			err_code |= ERR_ALERT | ERR_ABORT;
1743 			goto out;
1744 		}
1745 
1746 		newuser->user = strdup(args[1]);
1747 
1748 		newuser->next = userlist->users;
1749 		userlist->users = newuser;
1750 
1751 		cur_arg = 2;
1752 
1753 		while (*args[cur_arg]) {
1754 			if (!strcmp(args[cur_arg], "password")) {
1755 #ifdef USE_LIBCRYPT
1756 				if (!crypt("", args[cur_arg + 1])) {
1757 					ha_alert("parsing [%s:%d]: the encrypted password used for user '%s' is not supported by crypt(3).\n",
1758 						 file, linenum, newuser->user);
1759 					err_code |= ERR_ALERT | ERR_FATAL;
1760 					goto out;
1761 				}
1762 #else
1763 				ha_warning("parsing [%s:%d]: no crypt(3) support compiled, encrypted passwords will not work.\n",
1764 					   file, linenum);
1765 				err_code |= ERR_ALERT;
1766 #endif
1767 				newuser->pass = strdup(args[cur_arg + 1]);
1768 				cur_arg += 2;
1769 				continue;
1770 			} else if (!strcmp(args[cur_arg], "insecure-password")) {
1771 				newuser->pass = strdup(args[cur_arg + 1]);
1772 				newuser->flags |= AU_O_INSECURE;
1773 				cur_arg += 2;
1774 				continue;
1775 			} else if (!strcmp(args[cur_arg], "groups")) {
1776 				newuser->u.groups_names = strdup(args[cur_arg + 1]);
1777 				cur_arg += 2;
1778 				continue;
1779 			} else {
1780 				ha_alert("parsing [%s:%d]: '%s' only supports 'password', 'insecure-password' and 'groups' options.\n",
1781 					 file, linenum, args[0]);
1782 				err_code |= ERR_ALERT | ERR_FATAL;
1783 				goto out;
1784 			}
1785 		}
1786 	} else {
1787 		ha_alert("parsing [%s:%d]: unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "users");
1788 		err_code |= ERR_ALERT | ERR_FATAL;
1789 	}
1790 
1791 out:
1792 	return err_code;
1793 }
1794 
1795 int
cfg_parse_scope(const char * file,int linenum,char * line)1796 cfg_parse_scope(const char *file, int linenum, char *line)
1797 {
1798 	char *beg, *end, *scope = NULL;
1799 	int err_code = 0;
1800 	const char *err;
1801 
1802 	beg = line + 1;
1803 	end = strchr(beg, ']');
1804 
1805 	/* Detect end of scope declaration */
1806 	if (!end || end == beg) {
1807 		ha_alert("parsing [%s:%d] : empty scope name is forbidden.\n",
1808 			 file, linenum);
1809 		err_code |= ERR_ALERT | ERR_FATAL;
1810 		goto out;
1811 	}
1812 
1813 	/* Get scope name and check its validity */
1814 	scope = my_strndup(beg, end-beg);
1815 	err = invalid_char(scope);
1816 	if (err) {
1817 		ha_alert("parsing [%s:%d] : character '%c' is not permitted in a scope name.\n",
1818 			 file, linenum, *err);
1819 		err_code |= ERR_ALERT | ERR_ABORT;
1820 		goto out;
1821 	}
1822 
1823 	/* Be sure to have a scope declaration alone on its line */
1824 	line = end+1;
1825 	while (isspace((unsigned char)*line))
1826 		line++;
1827 	if (*line && *line != '#' && *line != '\n' && *line != '\r') {
1828 		ha_alert("parsing [%s:%d] : character '%c' is not permitted after scope declaration.\n",
1829 			 file, linenum, *line);
1830 		err_code |= ERR_ALERT | ERR_ABORT;
1831 		goto out;
1832 	}
1833 
1834 	/* We have a valid scope declaration, save it */
1835 	free(cfg_scope);
1836 	cfg_scope = scope;
1837 	scope = NULL;
1838 
1839   out:
1840 	free(scope);
1841 	return err_code;
1842 }
1843 
1844 int
cfg_parse_track_sc_num(unsigned int * track_sc_num,const char * arg,const char * end,char ** errmsg)1845 cfg_parse_track_sc_num(unsigned int *track_sc_num,
1846                        const char *arg, const char *end, char **errmsg)
1847 {
1848 	const char *p;
1849 	unsigned int num;
1850 
1851 	p = arg;
1852 	num = read_uint64(&arg, end);
1853 
1854 	if (arg != end) {
1855 		memprintf(errmsg, "Wrong track-sc number '%s'", p);
1856 		return -1;
1857 	}
1858 
1859 	if (num >= MAX_SESS_STKCTR) {
1860 		memprintf(errmsg, "%u track-sc number exceeding "
1861 		          "%d (MAX_SESS_STKCTR-1) value", num, MAX_SESS_STKCTR - 1);
1862 		return -1;
1863 	}
1864 
1865 	*track_sc_num = num;
1866 	return 0;
1867 }
1868 
1869 /*
1870  * This function reads and parses the configuration file given in the argument.
1871  * Returns the error code, 0 if OK, or any combination of :
1872  *  - ERR_ABORT: must abort ASAP
1873  *  - ERR_FATAL: we can continue parsing but not start the service
1874  *  - ERR_WARN: a warning has been emitted
1875  *  - ERR_ALERT: an alert has been emitted
1876  * Only the two first ones can stop processing, the two others are just
1877  * indicators.
1878  */
readcfgfile(const char * file)1879 int readcfgfile(const char *file)
1880 {
1881 	char *thisline;
1882 	int linesize = LINESIZE;
1883 	FILE *f;
1884 	int linenum = 0;
1885 	int err_code = 0;
1886 	struct cfg_section *cs = NULL, *pcs = NULL;
1887 	struct cfg_section *ics;
1888 	int readbytes = 0;
1889 
1890 	if ((thisline = malloc(sizeof(*thisline) * linesize)) == NULL) {
1891 		ha_alert("parsing [%s] : out of memory.\n", file);
1892 		return -1;
1893 	}
1894 
1895 	if ((f=fopen(file,"r")) == NULL) {
1896 		free(thisline);
1897 		return -1;
1898 	}
1899 
1900 next_line:
1901 	while (fgets(thisline + readbytes, linesize - readbytes, f) != NULL) {
1902 		int arg, kwm = KWM_STD;
1903 		char *end;
1904 		char *args[MAX_LINE_ARGS + 1];
1905 		char *line = thisline;
1906 		int dquote = 0;  /* double quote */
1907 		int squote = 0;  /* simple quote */
1908 
1909 		linenum++;
1910 
1911 		end = line + strlen(line);
1912 
1913 		if (end-line == linesize-1 && *(end-1) != '\n') {
1914 			/* Check if we reached the limit and the last char is not \n.
1915 			 * Watch out for the last line without the terminating '\n'!
1916 			 */
1917 			char *newline;
1918 			int newlinesize = linesize * 2;
1919 
1920 			newline = realloc(thisline, sizeof(*thisline) * newlinesize);
1921 			if (newline == NULL) {
1922 				ha_alert("parsing [%s:%d]: line too long, cannot allocate memory.\n",
1923 					 file, linenum);
1924 				err_code |= ERR_ALERT | ERR_FATAL;
1925 				linenum--;
1926 				continue;
1927 			}
1928 
1929 			readbytes = linesize - 1;
1930 			linesize = newlinesize;
1931 			thisline = newline;
1932 			linenum--;
1933 			continue;
1934 		}
1935 
1936 		readbytes = 0;
1937 
1938 		/* skip leading spaces */
1939 		while (isspace((unsigned char)*line))
1940 			line++;
1941 
1942 
1943 		if (*line == '[') {/* This is the beginning if a scope */
1944 			err_code |= cfg_parse_scope(file, linenum, line);
1945 			goto next_line;
1946 		}
1947 
1948 		arg = 0;
1949 		args[arg] = line;
1950 
1951 		while (*line && arg < MAX_LINE_ARGS) {
1952 			if (*line == '"' && !squote) {  /* double quote outside single quotes */
1953 				if (dquote)
1954 					dquote = 0;
1955 				else
1956 					dquote = 1;
1957 				memmove(line, line + 1, end - line);
1958 				end--;
1959 			}
1960 			else if (*line == '\'' && !dquote) { /* single quote outside double quotes */
1961 				if (squote)
1962 					squote = 0;
1963 				else
1964 					squote = 1;
1965 				memmove(line, line + 1, end - line);
1966 				end--;
1967 			}
1968 			else if (*line == '\\' && !squote) {
1969 			/* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their
1970 			 * C equivalent value. Other combinations left unchanged (eg: \1).
1971 			 */
1972 				int skip = 0;
1973 				if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') {
1974 					*line = line[1];
1975 					skip = 1;
1976 				}
1977 				else if (line[1] == 'r') {
1978 					*line = '\r';
1979 					skip = 1;
1980 				}
1981 				else if (line[1] == 'n') {
1982 					*line = '\n';
1983 					skip = 1;
1984 				}
1985 				else if (line[1] == 't') {
1986 					*line = '\t';
1987 					skip = 1;
1988 				}
1989 				else if (line[1] == 'x') {
1990 					if ((line + 3 < end) && ishex(line[2]) && ishex(line[3])) {
1991 						unsigned char hex1, hex2;
1992 						hex1 = toupper(line[2]) - '0';
1993 						hex2 = toupper(line[3]) - '0';
1994 						if (hex1 > 9) hex1 -= 'A' - '9' - 1;
1995 						if (hex2 > 9) hex2 -= 'A' - '9' - 1;
1996 						*line = (hex1<<4) + hex2;
1997 						skip = 3;
1998 					}
1999 					else {
2000 						ha_alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]);
2001 						err_code |= ERR_ALERT | ERR_FATAL;
2002 						goto next_line;
2003 					}
2004 				} else if (line[1] == '"') {
2005 					*line = '"';
2006 					skip = 1;
2007 				} else if (line[1] == '\'') {
2008 					*line = '\'';
2009 					skip = 1;
2010 				} else if (line[1] == '$' && dquote) { /* escaping of $ only inside double quotes */
2011 					*line = '$';
2012 					skip = 1;
2013 				}
2014 				if (skip) {
2015 					memmove(line + 1, line + 1 + skip, end - (line + skip));
2016 					end -= skip;
2017 				}
2018 				line++;
2019 			}
2020 			else if ((!squote && !dquote && *line == '#') || *line == '\n' || *line == '\r') {
2021 				/* end of string, end of loop */
2022 				*line = 0;
2023 				break;
2024 			}
2025 			else if (!squote && !dquote && isspace((unsigned char)*line)) {
2026 				/* a non-escaped space is an argument separator */
2027 				*line++ = '\0';
2028 				while (isspace((unsigned char)*line))
2029 					line++;
2030 				args[++arg] = line;
2031 			}
2032 			else if (dquote && *line == '$') {
2033 				/* environment variables are evaluated inside double quotes */
2034 				char *var_beg;
2035 				char *var_end;
2036 				char save_char;
2037 				char *value;
2038 				int val_len;
2039 				int newlinesize;
2040 				int braces = 0;
2041 
2042 				var_beg = line + 1;
2043 				var_end = var_beg;
2044 
2045 				if (*var_beg == '{') {
2046 					var_beg++;
2047 					var_end++;
2048 					braces = 1;
2049 				}
2050 
2051 				if (!isalpha((int)(unsigned char)*var_beg) && *var_beg != '_') {
2052 					ha_alert("parsing [%s:%d] : Variable expansion: Unrecognized character '%c' in variable name.\n", file, linenum, *var_beg);
2053 					err_code |= ERR_ALERT | ERR_FATAL;
2054 					goto next_line; /* skip current line */
2055 				}
2056 
2057 				while (isalnum((int)(unsigned char)*var_end) || *var_end == '_')
2058 					var_end++;
2059 
2060 				save_char = *var_end;
2061 				*var_end = '\0';
2062 				value = getenv(var_beg);
2063 				*var_end = save_char;
2064 				val_len = value ? strlen(value) : 0;
2065 
2066 				if (braces) {
2067 					if (*var_end == '}') {
2068 						var_end++;
2069 						braces = 0;
2070 					} else {
2071 						ha_alert("parsing [%s:%d] : Variable expansion: Mismatched braces.\n", file, linenum);
2072 						err_code |= ERR_ALERT | ERR_FATAL;
2073 						goto next_line; /* skip current line */
2074 					}
2075 				}
2076 
2077 				newlinesize = (end - thisline) - (var_end - line) + val_len + 1;
2078 
2079 				/* if not enough space in thisline */
2080 				if (newlinesize  > linesize) {
2081 					char *newline;
2082 
2083 					newline = realloc(thisline, newlinesize * sizeof(*thisline));
2084 					if (newline == NULL) {
2085 						ha_alert("parsing [%s:%d] : Variable expansion: Not enough memory.\n", file, linenum);
2086 						err_code |= ERR_ALERT | ERR_FATAL;
2087 						goto next_line; /* slip current line */
2088 					}
2089 					/* recompute pointers if realloc returns a new pointer */
2090 					if (newline != thisline) {
2091 						int i;
2092 						int diff;
2093 
2094 						for (i = 0; i <= arg; i++) {
2095 							diff = args[i] - thisline;
2096 							args[i] = newline + diff;
2097 						}
2098 
2099 						diff = var_end - thisline;
2100 						var_end = newline + diff;
2101 						diff = end - thisline;
2102 						end = newline + diff;
2103 						diff = line - thisline;
2104 						line = newline + diff;
2105 						thisline = newline;
2106 					}
2107 					linesize = newlinesize;
2108 				}
2109 
2110 				/* insert value inside the line */
2111 				memmove(line + val_len, var_end, end - var_end + 1);
2112 				memcpy(line, value, val_len);
2113 				end += val_len - (var_end - line);
2114 				line += val_len;
2115 			}
2116 			else {
2117 				line++;
2118 			}
2119 		}
2120 
2121 		if (dquote) {
2122 			ha_alert("parsing [%s:%d] : Mismatched double quotes.\n", file, linenum);
2123 			err_code |= ERR_ALERT | ERR_FATAL;
2124 		}
2125 
2126 		if (squote) {
2127 			ha_alert("parsing [%s:%d] : Mismatched simple quotes.\n", file, linenum);
2128 			err_code |= ERR_ALERT | ERR_FATAL;
2129 		}
2130 
2131 		/* empty line */
2132 		if (!**args)
2133 			continue;
2134 
2135 		if (*line) {
2136 			/* we had to stop due to too many args.
2137 			 * Let's terminate the string, print the offending part then cut the
2138 			 * last arg.
2139 			 */
2140 			while (*line && *line != '#' && *line != '\n' && *line != '\r')
2141 				line++;
2142 			*line = '\0';
2143 
2144 			ha_alert("parsing [%s:%d]: line too long, truncating at word %d, position %ld: <%s>.\n",
2145 				 file, linenum, arg + 1, (long)(args[arg] - thisline + 1), args[arg]);
2146 			err_code |= ERR_ALERT | ERR_FATAL;
2147 			args[arg] = line;
2148 		}
2149 
2150 		/* zero out remaining args and ensure that at least one entry
2151 		 * is zeroed out.
2152 		 */
2153 		while (++arg <= MAX_LINE_ARGS) {
2154 			args[arg] = line;
2155 		}
2156 
2157 		/* check for keyword modifiers "no" and "default" */
2158 		if (!strcmp(args[0], "no")) {
2159 			char *tmp;
2160 
2161 			kwm = KWM_NO;
2162 			tmp = args[0];
2163 			for (arg=0; *args[arg+1]; arg++)
2164 				args[arg] = args[arg+1];		// shift args after inversion
2165 			*tmp = '\0'; 					// fix the next arg to \0
2166 			args[arg] = tmp;
2167 		}
2168 		else if (!strcmp(args[0], "default")) {
2169 			kwm = KWM_DEF;
2170 			for (arg=0; *args[arg+1]; arg++)
2171 				args[arg] = args[arg+1];		// shift args after inversion
2172 		}
2173 
2174 		if (kwm != KWM_STD && strcmp(args[0], "option") != 0 && 	\
2175 		    strcmp(args[0], "log") != 0 && strcmp(args[0], "busy-polling")) {
2176 			ha_alert("parsing [%s:%d]: negation/default currently supported only for options, log, and busy-polling.\n", file, linenum);
2177 			err_code |= ERR_ALERT | ERR_FATAL;
2178 		}
2179 
2180 		/* detect section start */
2181 		list_for_each_entry(ics, &sections, list) {
2182 			if (strcmp(args[0], ics->section_name) == 0) {
2183 				cursection = ics->section_name;
2184 				pcs = cs;
2185 				cs = ics;
2186 				break;
2187 			}
2188 		}
2189 
2190 		if (pcs && pcs->post_section_parser) {
2191 			err_code |= pcs->post_section_parser();
2192 			if (err_code & ERR_ABORT)
2193 				goto err;
2194 		}
2195 		pcs = NULL;
2196 
2197 		if (!cs) {
2198 			ha_alert("parsing [%s:%d]: unknown keyword '%s' out of section.\n", file, linenum, args[0]);
2199 			err_code |= ERR_ALERT | ERR_FATAL;
2200 		} else {
2201 			err_code |= cs->section_parser(file, linenum, args, kwm);
2202 			if (err_code & ERR_ABORT)
2203 				goto err;
2204 		}
2205 	}
2206 
2207 	if (cs && cs->post_section_parser)
2208 		err_code |= cs->post_section_parser();
2209 
2210 err:
2211 	free(cfg_scope);
2212 	cfg_scope = NULL;
2213 	cursection = NULL;
2214 	free(thisline);
2215 	fclose(f);
2216 	return err_code;
2217 }
2218 
2219 /* This function propagates processes from frontend <from> to backend <to> so
2220  * that it is always guaranteed that a backend pointed to by a frontend is
2221  * bound to all of its processes. After that, if the target is a "listen"
2222  * instance, the function recursively descends the target's own targets along
2223  * default_backend and use_backend rules. Since the bits are
2224  * checked first to ensure that <to> is already bound to all processes of
2225  * <from>, there is no risk of looping and we ensure to follow the shortest
2226  * path to the destination.
2227  *
2228  * It is possible to set <to> to NULL for the first call so that the function
2229  * takes care of visiting the initial frontend in <from>.
2230  *
2231  * It is important to note that the function relies on the fact that all names
2232  * have already been resolved.
2233  */
propagate_processes(struct proxy * from,struct proxy * to)2234 void propagate_processes(struct proxy *from, struct proxy *to)
2235 {
2236 	struct switching_rule *rule;
2237 
2238 	if (to) {
2239 		/* check whether we need to go down */
2240 		if (from->bind_proc &&
2241 		    (from->bind_proc & to->bind_proc) == from->bind_proc)
2242 			return;
2243 
2244 		if (!from->bind_proc && !to->bind_proc)
2245 			return;
2246 
2247 		to->bind_proc = from->bind_proc ?
2248 			(to->bind_proc | from->bind_proc) : 0;
2249 
2250 		/* now propagate down */
2251 		from = to;
2252 	}
2253 
2254 	if (!(from->cap & PR_CAP_FE))
2255 		return;
2256 
2257 	if (from->state == PR_STSTOPPED)
2258 		return;
2259 
2260 	/* default_backend */
2261 	if (from->defbe.be)
2262 		propagate_processes(from, from->defbe.be);
2263 
2264 	/* use_backend */
2265 	list_for_each_entry(rule, &from->switching_rules, list) {
2266 		if (rule->dynamic)
2267 			continue;
2268 		to = rule->be.backend;
2269 		propagate_processes(from, to);
2270 	}
2271 }
2272 
2273 /*
2274  * Returns the error code, 0 if OK, or any combination of :
2275  *  - ERR_ABORT: must abort ASAP
2276  *  - ERR_FATAL: we can continue parsing but not start the service
2277  *  - ERR_WARN: a warning has been emitted
2278  *  - ERR_ALERT: an alert has been emitted
2279  * Only the two first ones can stop processing, the two others are just
2280  * indicators.
2281  */
check_config_validity()2282 int check_config_validity()
2283 {
2284 	int cfgerr = 0;
2285 	struct proxy *curproxy = NULL;
2286 	struct stktable *t;
2287 	struct server *newsrv = NULL;
2288 	int err_code = 0;
2289 	unsigned int next_pxid = 1;
2290 	struct bind_conf *bind_conf;
2291 	char *err;
2292 	struct cfg_postparser *postparser;
2293 	struct dns_resolvers *curr_resolvers = NULL;
2294 
2295 	bind_conf = NULL;
2296 	/*
2297 	 * Now, check for the integrity of all that we have collected.
2298 	 */
2299 
2300 	/* will be needed further to delay some tasks */
2301 	tv_update_date(0,1);
2302 
2303 	if (!global.tune.max_http_hdr)
2304 		global.tune.max_http_hdr = MAX_HTTP_HDR;
2305 
2306 	if (!global.tune.cookie_len)
2307 		global.tune.cookie_len = CAPTURE_LEN;
2308 
2309 	if (!global.tune.requri_len)
2310 		global.tune.requri_len = REQURI_LEN;
2311 
2312 	if (!global.nbthread) {
2313 		/* nbthread not set, thus automatic. In this case, and only if
2314 		 * running on a single process, we enable the same number of
2315 		 * threads as the number of CPUs the process is bound to. This
2316 		 * allows to easily control the number of threads using taskset.
2317 		 */
2318 		global.nbthread = 1;
2319 #if defined(USE_THREAD)
2320 		if (global.nbproc == 1)
2321 			global.nbthread = thread_cpus_enabled_at_boot;
2322 		all_threads_mask = nbits(global.nbthread);
2323 #endif
2324 	}
2325 
2326 	if (global.nbproc > 1 && global.nbthread > 1) {
2327 		ha_alert("config : cannot enable multiple processes if multiple threads are configured. Please use either nbproc or nbthread but not both.\n");
2328 		err_code |= ERR_ALERT | ERR_FATAL;
2329 		goto out;
2330 	}
2331 
2332 	pool_head_requri = create_pool("requri", global.tune.requri_len , MEM_F_SHARED);
2333 
2334 	pool_head_capture = create_pool("capture", global.tune.cookie_len, MEM_F_SHARED);
2335 
2336 	/* Post initialisation of the users and groups lists. */
2337 	err_code = userlist_postinit();
2338 	if (err_code != ERR_NONE)
2339 		goto out;
2340 
2341 	/* first, we will invert the proxy list order */
2342 	curproxy = NULL;
2343 	while (proxies_list) {
2344 		struct proxy *next;
2345 
2346 		next = proxies_list->next;
2347 		proxies_list->next = curproxy;
2348 		curproxy = proxies_list;
2349 		if (!next)
2350 			break;
2351 		proxies_list = next;
2352 	}
2353 
2354 	for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
2355 		struct switching_rule *rule;
2356 		struct server_rule *srule;
2357 		struct sticking_rule *mrule;
2358 		struct act_rule *arule;
2359 		struct logsrv *tmplogsrv;
2360 		unsigned int next_id;
2361 		int nbproc;
2362 
2363 		if (curproxy->uuid < 0) {
2364 			/* proxy ID not set, use automatic numbering with first
2365 			 * spare entry starting with next_pxid.
2366 			 */
2367 			next_pxid = get_next_id(&used_proxy_id, next_pxid);
2368 			curproxy->conf.id.key = curproxy->uuid = next_pxid;
2369 			eb32_insert(&used_proxy_id, &curproxy->conf.id);
2370 		}
2371 		next_pxid++;
2372 
2373 		if (curproxy->mode == PR_MODE_HTTP && global.tune.bufsize >= (256 << 20) && ONLY_ONCE()) {
2374 			ha_alert("global.tune.bufsize must be below 256 MB when HTTP is in use (current value = %d).\n",
2375 				 global.tune.bufsize);
2376 			cfgerr++;
2377 		}
2378 
2379 		if (curproxy->state == PR_STSTOPPED) {
2380 			/* ensure we don't keep listeners uselessly bound */
2381 			stop_proxy(curproxy);
2382 			if (curproxy->table) {
2383 				free((void *)curproxy->table->peers.name);
2384 				curproxy->table->peers.p = NULL;
2385 			}
2386 			continue;
2387 		}
2388 
2389 		/* Check multi-process mode compatibility for the current proxy */
2390 
2391 		if (curproxy->bind_proc) {
2392 			/* an explicit bind-process was specified, let's check how many
2393 			 * processes remain.
2394 			 */
2395 			nbproc = my_popcountl(curproxy->bind_proc);
2396 
2397 			curproxy->bind_proc &= all_proc_mask;
2398 			if (!curproxy->bind_proc && nbproc == 1) {
2399 				ha_warning("Proxy '%s': the process specified on the 'bind-process' directive refers to a process number that is higher than global.nbproc. The proxy has been forced to run on process 1 only.\n", curproxy->id);
2400 				curproxy->bind_proc = 1;
2401 			}
2402 			else if (!curproxy->bind_proc && nbproc > 1) {
2403 				ha_warning("Proxy '%s': all processes specified on the 'bind-process' directive refer to numbers that are all higher than global.nbproc. The directive was ignored and the proxy will run on all processes.\n", curproxy->id);
2404 				curproxy->bind_proc = 0;
2405 			}
2406 		}
2407 
2408 		/* check and reduce the bind-proc of each listener */
2409 		list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
2410 			unsigned long mask;
2411 
2412 			/* HTTP frontends with "h2" as ALPN/NPN will work in
2413 			 * HTTP/2 and absolutely require buffers 16kB or larger.
2414 			 */
2415 #ifdef USE_OPENSSL
2416 			if (curproxy->mode == PR_MODE_HTTP && global.tune.bufsize < 16384) {
2417 #ifdef OPENSSL_NPN_NEGOTIATED
2418 				/* check NPN */
2419 				if (bind_conf->ssl_conf.npn_str && strstr(bind_conf->ssl_conf.npn_str, "\002h2")) {
2420 					ha_alert("config : HTTP frontend '%s' enables HTTP/2 via NPN at [%s:%d], so global.tune.bufsize must be at least 16384 bytes (%d now).\n",
2421 						 curproxy->id, bind_conf->file, bind_conf->line, global.tune.bufsize);
2422 					cfgerr++;
2423 				}
2424 #endif
2425 #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2426 				/* check ALPN */
2427 				if (bind_conf->ssl_conf.alpn_str && strstr(bind_conf->ssl_conf.alpn_str, "\002h2")) {
2428 					ha_alert("config : HTTP frontend '%s' enables HTTP/2 via ALPN at [%s:%d], so global.tune.bufsize must be at least 16384 bytes (%d now).\n",
2429 						 curproxy->id, bind_conf->file, bind_conf->line, global.tune.bufsize);
2430 					cfgerr++;
2431 				}
2432 #endif
2433 			} /* HTTP && bufsize < 16384 */
2434 #endif
2435 
2436 			/* detect and address thread affinity inconsistencies */
2437 			mask = thread_mask(bind_conf->bind_thread);
2438 			if (!(mask & all_threads_mask)) {
2439 				unsigned long new_mask = 0;
2440 
2441 				while (mask) {
2442 					new_mask |= mask & all_threads_mask;
2443 					mask >>= global.nbthread;
2444 				}
2445 
2446 				bind_conf->bind_thread = new_mask;
2447 				ha_warning("Proxy '%s': the thread range specified on the 'process' directive of 'bind %s' at [%s:%d] only refers to thread numbers out of the range defined by the global 'nbthread' directive. The thread numbers were remapped to existing threads instead (mask 0x%lx).\n",
2448 					   curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line, new_mask);
2449 			}
2450 
2451 			/* detect process and nbproc affinity inconsistencies */
2452 			mask = proc_mask(bind_conf->bind_proc) & proc_mask(curproxy->bind_proc);
2453 			if (!(mask & all_proc_mask)) {
2454 				mask = proc_mask(curproxy->bind_proc) & all_proc_mask;
2455 				nbproc = my_popcountl(bind_conf->bind_proc);
2456 				bind_conf->bind_proc = proc_mask(bind_conf->bind_proc) & mask;
2457 
2458 				if (!bind_conf->bind_proc && nbproc == 1) {
2459 					ha_warning("Proxy '%s': the process number specified on the 'process' directive of 'bind %s' at [%s:%d] refers to a process not covered by the proxy. This has been fixed by forcing it to run on the proxy's first process only.\n",
2460 						   curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
2461 					bind_conf->bind_proc = mask & ~(mask - 1);
2462 				}
2463 				else if (!bind_conf->bind_proc && nbproc > 1) {
2464 					ha_warning("Proxy '%s': the process range specified on the 'process' directive of 'bind %s' at [%s:%d] only refers to processes not covered by the proxy. The directive was ignored so that all of the proxy's processes are used.\n",
2465 						   curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
2466 					bind_conf->bind_proc = 0;
2467 				}
2468 			}
2469 		}
2470 
2471 		switch (curproxy->mode) {
2472 		case PR_MODE_HEALTH:
2473 			cfgerr += proxy_cfg_ensure_no_http(curproxy);
2474 			if (!(curproxy->cap & PR_CAP_FE)) {
2475 				ha_alert("config : %s '%s' cannot be in health mode as it has no frontend capability.\n",
2476 					 proxy_type_str(curproxy), curproxy->id);
2477 				cfgerr++;
2478 			}
2479 
2480 			if (curproxy->srv != NULL)
2481 				ha_warning("config : servers will be ignored for %s '%s'.\n",
2482 					   proxy_type_str(curproxy), curproxy->id);
2483 			break;
2484 
2485 		case PR_MODE_TCP:
2486 			cfgerr += proxy_cfg_ensure_no_http(curproxy);
2487 			break;
2488 
2489 		case PR_MODE_HTTP:
2490 			curproxy->http_needed = 1;
2491 			break;
2492 
2493 		case PR_MODE_CLI:
2494 			cfgerr += proxy_cfg_ensure_no_http(curproxy);
2495 			break;
2496 		}
2497 
2498 		if (curproxy != global.stats_fe && (curproxy->cap & PR_CAP_FE) && LIST_ISEMPTY(&curproxy->conf.listeners)) {
2499 			ha_warning("config : %s '%s' has no 'bind' directive. Please declare it as a backend if this was intended.\n",
2500 				   proxy_type_str(curproxy), curproxy->id);
2501 			err_code |= ERR_WARN;
2502 		}
2503 
2504 		if ((curproxy->cap & PR_CAP_BE) && (curproxy->mode != PR_MODE_HEALTH)) {
2505 			if (curproxy->lbprm.algo & BE_LB_KIND) {
2506 				if (curproxy->options & PR_O_TRANSP) {
2507 					ha_alert("config : %s '%s' cannot use both transparent and balance mode.\n",
2508 						 proxy_type_str(curproxy), curproxy->id);
2509 					cfgerr++;
2510 				}
2511 #ifdef WE_DONT_SUPPORT_SERVERLESS_LISTENERS
2512 				else if (curproxy->srv == NULL) {
2513 					ha_alert("config : %s '%s' needs at least 1 server in balance mode.\n",
2514 						 proxy_type_str(curproxy), curproxy->id);
2515 					cfgerr++;
2516 				}
2517 #endif
2518 				else if (curproxy->options & PR_O_DISPATCH) {
2519 					ha_warning("config : dispatch address of %s '%s' will be ignored in balance mode.\n",
2520 						   proxy_type_str(curproxy), curproxy->id);
2521 					err_code |= ERR_WARN;
2522 				}
2523 			}
2524 			else if (!(curproxy->options & (PR_O_TRANSP | PR_O_DISPATCH | PR_O_HTTP_PROXY))) {
2525 				/* If no LB algo is set in a backend, and we're not in
2526 				 * transparent mode, dispatch mode nor proxy mode, we
2527 				 * want to use balance roundrobin by default.
2528 				 */
2529 				curproxy->lbprm.algo &= ~BE_LB_ALGO;
2530 				curproxy->lbprm.algo |= BE_LB_ALGO_RR;
2531 			}
2532 		}
2533 
2534 		if (curproxy->options & PR_O_DISPATCH)
2535 			curproxy->options &= ~(PR_O_TRANSP | PR_O_HTTP_PROXY);
2536 		else if (curproxy->options & PR_O_HTTP_PROXY)
2537 			curproxy->options &= ~(PR_O_DISPATCH | PR_O_TRANSP);
2538 		else if (curproxy->options & PR_O_TRANSP)
2539 			curproxy->options &= ~(PR_O_DISPATCH | PR_O_HTTP_PROXY);
2540 
2541 		if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_HTTP_CHK) {
2542 			if (curproxy->options & PR_O_DISABLE404) {
2543 				ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option httpchk').\n",
2544 					   "disable-on-404", proxy_type_str(curproxy), curproxy->id);
2545 				err_code |= ERR_WARN;
2546 				curproxy->options &= ~PR_O_DISABLE404;
2547 			}
2548 			if (curproxy->options2 & PR_O2_CHK_SNDST) {
2549 				ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option httpchk').\n",
2550 					   "send-state", proxy_type_str(curproxy), curproxy->id);
2551 				err_code |= ERR_WARN;
2552 				curproxy->options &= ~PR_O2_CHK_SNDST;
2553 			}
2554 		}
2555 
2556 		if ((curproxy->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2557 			if (!global.external_check) {
2558 				ha_alert("Proxy '%s' : '%s' unable to find required 'global.external-check'.\n",
2559 					 curproxy->id, "option external-check");
2560 				cfgerr++;
2561 			}
2562 			if (!curproxy->check_command) {
2563 				ha_alert("Proxy '%s' : '%s' unable to find required 'external-check command'.\n",
2564 					 curproxy->id, "option external-check");
2565 				cfgerr++;
2566 			}
2567 		}
2568 
2569 		if ((curproxy->retry_type &~ PR_RE_CONN_FAILED) &&
2570 		    !(curproxy->options2 & PR_O2_USE_HTX)) {
2571 			ha_warning("Proxy '%s' : retry-on with any other keywords than 'conn-failure' will be ignored, requires 'option http-use-htx'.\n", curproxy->id);
2572 			err_code |= ERR_WARN;
2573 			curproxy->retry_type &= PR_RE_CONN_FAILED;
2574 		}
2575 		if (curproxy->email_alert.set) {
2576 		    if (!(curproxy->email_alert.mailers.name && curproxy->email_alert.from && curproxy->email_alert.to)) {
2577 			    ha_warning("config : 'email-alert' will be ignored for %s '%s' (the presence any of "
2578 				       "'email-alert from', 'email-alert level' 'email-alert mailers', "
2579 				       "'email-alert myhostname', or 'email-alert to' "
2580 				       "requires each of 'email-alert from', 'email-alert mailers' and 'email-alert to' "
2581 				       "to be present).\n",
2582 				       proxy_type_str(curproxy), curproxy->id);
2583 			    err_code |= ERR_WARN;
2584 			    free_email_alert(curproxy);
2585 		    }
2586 		    if (!curproxy->email_alert.myhostname)
2587 			    curproxy->email_alert.myhostname = strdup(hostname);
2588 		}
2589 
2590 		if (curproxy->check_command) {
2591 			int clear = 0;
2592 			if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_EXT_CHK) {
2593 				ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option external-check').\n",
2594 					   "external-check command", proxy_type_str(curproxy), curproxy->id);
2595 				err_code |= ERR_WARN;
2596 				clear = 1;
2597 			}
2598 			if (curproxy->check_command[0] != '/' && !curproxy->check_path) {
2599 				ha_alert("Proxy '%s': '%s' does not have a leading '/' and 'external-check path' is not set.\n",
2600 					 curproxy->id, "external-check command");
2601 				cfgerr++;
2602 			}
2603 			if (clear) {
2604 				free(curproxy->check_command);
2605 				curproxy->check_command = NULL;
2606 			}
2607 		}
2608 
2609 		if (curproxy->check_path) {
2610 			if ((curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_EXT_CHK) {
2611 				ha_warning("config : '%s' will be ignored for %s '%s' (requires 'option external-check').\n",
2612 					   "external-check path", proxy_type_str(curproxy), curproxy->id);
2613 				err_code |= ERR_WARN;
2614 				free(curproxy->check_path);
2615 				curproxy->check_path = NULL;
2616 			}
2617 		}
2618 
2619 		/* if a default backend was specified, let's find it */
2620 		if (curproxy->defbe.name) {
2621 			struct proxy *target;
2622 
2623 			target = proxy_be_by_name(curproxy->defbe.name);
2624 			if (!target) {
2625 				ha_alert("Proxy '%s': unable to find required default_backend: '%s'.\n",
2626 					 curproxy->id, curproxy->defbe.name);
2627 				cfgerr++;
2628 			} else if (target == curproxy) {
2629 				ha_alert("Proxy '%s': loop detected for default_backend: '%s'.\n",
2630 					 curproxy->id, curproxy->defbe.name);
2631 				cfgerr++;
2632 			} else if (target->mode != curproxy->mode &&
2633 				   !(curproxy->mode == PR_MODE_TCP && target->mode == PR_MODE_HTTP)) {
2634 
2635 				ha_alert("%s %s '%s' (%s:%d) tries to use incompatible %s %s '%s' (%s:%d) as its default backend (see 'mode').\n",
2636 					 proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
2637 					 curproxy->conf.file, curproxy->conf.line,
2638 					 proxy_mode_str(target->mode), proxy_type_str(target), target->id,
2639 					 target->conf.file, target->conf.line);
2640 				cfgerr++;
2641 			} else if ((curproxy->options2 ^ target->options2) & PR_O2_USE_HTX) {
2642 				ha_alert("%s %s '%s' (%s:%d) tries to use %s %s '%s' (%s:%d) as its default backend, both of which disagree on 'option http-use-htx'.\n",
2643 					 proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
2644 					 curproxy->conf.file, curproxy->conf.line,
2645 					 proxy_mode_str(target->mode), proxy_type_str(target), target->id,
2646 					 target->conf.file, target->conf.line);
2647 				cfgerr++;
2648 			} else {
2649 				free(curproxy->defbe.name);
2650 				curproxy->defbe.be = target;
2651 				/* Emit a warning if this proxy also has some servers */
2652 				if (curproxy->srv) {
2653 					ha_warning("In proxy '%s', the 'default_backend' rule always has precedence over the servers, which will never be used.\n",
2654 						   curproxy->id);
2655 					err_code |= ERR_WARN;
2656 				}
2657 			}
2658 		}
2659 
2660 		/* find the target proxy for 'use_backend' rules */
2661 		list_for_each_entry(rule, &curproxy->switching_rules, list) {
2662 			struct proxy *target;
2663 			struct logformat_node *node;
2664 			char *pxname;
2665 
2666 			/* Try to parse the string as a log format expression. If the result
2667 			 * of the parsing is only one entry containing a simple string, then
2668 			 * it's a standard string corresponding to a static rule, thus the
2669 			 * parsing is cancelled and be.name is restored to be resolved.
2670 			 */
2671 			pxname = rule->be.name;
2672 			LIST_INIT(&rule->be.expr);
2673 			curproxy->conf.args.ctx = ARGC_UBK;
2674 			curproxy->conf.args.file = rule->file;
2675 			curproxy->conf.args.line = rule->line;
2676 			err = NULL;
2677 			if (!parse_logformat_string(pxname, curproxy, &rule->be.expr, 0, SMP_VAL_FE_HRQ_HDR, &err)) {
2678 				ha_alert("Parsing [%s:%d]: failed to parse use_backend rule '%s' : %s.\n",
2679 					 rule->file, rule->line, pxname, err);
2680 				free(err);
2681 				cfgerr++;
2682 				continue;
2683 			}
2684 			node = LIST_NEXT(&rule->be.expr, struct logformat_node *, list);
2685 
2686 			if (!LIST_ISEMPTY(&rule->be.expr)) {
2687 				if (node->type != LOG_FMT_TEXT || node->list.n != &rule->be.expr) {
2688 					rule->dynamic = 1;
2689 					free(pxname);
2690 					continue;
2691 				}
2692 				/* Only one element in the list, a simple string: free the expression and
2693 				 * fall back to static rule
2694 				 */
2695 				LIST_DEL(&node->list);
2696 				free(node->arg);
2697 				free(node);
2698 			}
2699 
2700 			rule->dynamic = 0;
2701 			rule->be.name = pxname;
2702 
2703 			target = proxy_be_by_name(rule->be.name);
2704 			if (!target) {
2705 				ha_alert("Proxy '%s': unable to find required use_backend: '%s'.\n",
2706 					 curproxy->id, rule->be.name);
2707 				cfgerr++;
2708 			} else if (target == curproxy) {
2709 				ha_alert("Proxy '%s': loop detected for use_backend: '%s'.\n",
2710 					 curproxy->id, rule->be.name);
2711 				cfgerr++;
2712 			} else if (target->mode != curproxy->mode &&
2713 				   !(curproxy->mode == PR_MODE_TCP && target->mode == PR_MODE_HTTP)) {
2714 
2715 				ha_alert("%s %s '%s' (%s:%d) tries to use incompatible %s %s '%s' (%s:%d) in a 'use_backend' rule (see 'mode').\n",
2716 					 proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
2717 					 curproxy->conf.file, curproxy->conf.line,
2718 					 proxy_mode_str(target->mode), proxy_type_str(target), target->id,
2719 					 target->conf.file, target->conf.line);
2720 				cfgerr++;
2721 			} else if ((curproxy->options2 ^ target->options2) & PR_O2_USE_HTX) {
2722 				ha_alert("%s %s '%s' (%s:%d) tries to use %s %s '%s' (%s:%d) in a 'use_backend' rule, both of which disagree on 'option http-use-htx'.\n",
2723 					 proxy_mode_str(curproxy->mode), proxy_type_str(curproxy), curproxy->id,
2724 					 curproxy->conf.file, curproxy->conf.line,
2725 					 proxy_mode_str(target->mode), proxy_type_str(target), target->id,
2726 					 target->conf.file, target->conf.line);
2727 				cfgerr++;
2728 			} else {
2729 				free((void *)rule->be.name);
2730 				rule->be.backend = target;
2731 			}
2732 		}
2733 
2734 		/* find the target server for 'use_server' rules */
2735 		list_for_each_entry(srule, &curproxy->server_rules, list) {
2736 			struct server *target = findserver(curproxy, srule->srv.name);
2737 
2738 			if (!target) {
2739 				ha_alert("config : %s '%s' : unable to find server '%s' referenced in a 'use-server' rule.\n",
2740 					 proxy_type_str(curproxy), curproxy->id, srule->srv.name);
2741 				cfgerr++;
2742 				continue;
2743 			}
2744 			free((void *)srule->srv.name);
2745 			srule->srv.ptr = target;
2746 		}
2747 
2748 		/* find the target table for 'stick' rules */
2749 		list_for_each_entry(mrule, &curproxy->sticking_rules, list) {
2750 			struct stktable *target;
2751 
2752 			curproxy->be_req_ana |= AN_REQ_STICKING_RULES;
2753 			if (mrule->flags & STK_IS_STORE)
2754 				curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
2755 
2756 			if (mrule->table.name)
2757 				target = stktable_find_by_name(mrule->table.name);
2758 			else
2759 				target = curproxy->table;
2760 
2761 			if (!target) {
2762 				ha_alert("Proxy '%s': unable to find stick-table '%s'.\n",
2763 					 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
2764 				cfgerr++;
2765 			}
2766 			else if (!stktable_compatible_sample(mrule->expr,  target->type)) {
2767 				ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
2768 					 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
2769 				cfgerr++;
2770 			}
2771 			else if (target->proxy && curproxy->bind_proc & ~target->proxy->bind_proc) {
2772 				ha_alert("Proxy '%s': stick-table '%s' referenced 'stick-store' rule not present on all processes covered by proxy '%s'.\n",
2773 				         curproxy->id, target->id, curproxy->id);
2774 				cfgerr++;
2775 			}
2776 			else {
2777 				free((void *)mrule->table.name);
2778 				mrule->table.t = target;
2779 				stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL);
2780 				stktable_alloc_data_type(target, STKTABLE_DT_SERVER_NAME, NULL);
2781 				if (!in_proxies_list(target->proxies_list, curproxy)) {
2782 					curproxy->next_stkt_ref = target->proxies_list;
2783 					target->proxies_list = curproxy;
2784 				}
2785 			}
2786 		}
2787 
2788 		/* find the target table for 'store response' rules */
2789 		list_for_each_entry(mrule, &curproxy->storersp_rules, list) {
2790 			struct stktable *target;
2791 
2792 			curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
2793 
2794 			if (mrule->table.name)
2795 				target = stktable_find_by_name(mrule->table.name);
2796 			else
2797 				target = curproxy->table;
2798 
2799 			if (!target) {
2800 				ha_alert("Proxy '%s': unable to find store table '%s'.\n",
2801 					 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
2802 				cfgerr++;
2803 			}
2804 			else if (!stktable_compatible_sample(mrule->expr, target->type)) {
2805 				ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
2806 					 curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
2807 				cfgerr++;
2808 			}
2809 			else if (target->proxy && (curproxy->bind_proc & ~target->proxy->bind_proc)) {
2810 				ha_alert("Proxy '%s': stick-table '%s' referenced 'stick-store' rule not present on all processes covered by proxy '%s'.\n",
2811 				         curproxy->id, target->id, curproxy->id);
2812 				cfgerr++;
2813 			}
2814 			else {
2815 				free((void *)mrule->table.name);
2816 				mrule->table.t = target;
2817 				stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL);
2818 				stktable_alloc_data_type(target, STKTABLE_DT_SERVER_NAME, NULL);
2819 				if (!in_proxies_list(target->proxies_list, curproxy)) {
2820 					curproxy->next_stkt_ref = target->proxies_list;
2821 					target->proxies_list = curproxy;
2822 				}
2823 			}
2824 		}
2825 
2826 		/* check validity for 'tcp-request' layer 4/5/6/7 rules */
2827 		cfgerr += check_action_rules(&curproxy->tcp_req.l4_rules, curproxy, &err_code);
2828 		cfgerr += check_action_rules(&curproxy->tcp_req.l5_rules, curproxy, &err_code);
2829 		cfgerr += check_action_rules(&curproxy->tcp_req.inspect_rules, curproxy, &err_code);
2830 		cfgerr += check_action_rules(&curproxy->tcp_rep.inspect_rules, curproxy, &err_code);
2831 		cfgerr += check_action_rules(&curproxy->http_req_rules, curproxy, &err_code);
2832 		cfgerr += check_action_rules(&curproxy->http_res_rules, curproxy, &err_code);
2833 
2834 		/* move any "block" rules at the beginning of the http-request rules */
2835 		if (!LIST_ISEMPTY(&curproxy->block_rules)) {
2836 			/* insert block_rules into http_req_rules at the beginning */
2837 			curproxy->block_rules.p->n    = curproxy->http_req_rules.n;
2838 			curproxy->http_req_rules.n->p = curproxy->block_rules.p;
2839 			curproxy->block_rules.n->p    = &curproxy->http_req_rules;
2840 			curproxy->http_req_rules.n    = curproxy->block_rules.n;
2841 			LIST_INIT(&curproxy->block_rules);
2842 		}
2843 
2844 		if (curproxy->table && curproxy->table->peers.name) {
2845 			struct peers *curpeers;
2846 
2847 			for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
2848 				if (strcmp(curpeers->id, curproxy->table->peers.name) == 0) {
2849 					free((void *)curproxy->table->peers.name);
2850 					curproxy->table->peers.p = curpeers;
2851 					break;
2852 				}
2853 			}
2854 
2855 			if (!curpeers) {
2856 				ha_alert("Proxy '%s': unable to find sync peers '%s'.\n",
2857 					 curproxy->id, curproxy->table->peers.name);
2858 				free((void *)curproxy->table->peers.name);
2859 				curproxy->table->peers.p = NULL;
2860 				cfgerr++;
2861 			}
2862 			else if (curpeers->state == PR_STSTOPPED) {
2863 				/* silently disable this peers section */
2864 				curproxy->table->peers.p = NULL;
2865 			}
2866 			else if (!curpeers->peers_fe) {
2867 				ha_alert("Proxy '%s': unable to find local peer '%s' in peers section '%s'.\n",
2868 					 curproxy->id, localpeer, curpeers->id);
2869 				curproxy->table->peers.p = NULL;
2870 				cfgerr++;
2871 			}
2872 		}
2873 
2874 
2875 		if (curproxy->email_alert.mailers.name) {
2876 			struct mailers *curmailers = mailers;
2877 
2878 			for (curmailers = mailers; curmailers; curmailers = curmailers->next) {
2879 				if (!strcmp(curmailers->id, curproxy->email_alert.mailers.name))
2880 					break;
2881 			}
2882 			if (!curmailers) {
2883 				ha_alert("Proxy '%s': unable to find mailers '%s'.\n",
2884 					 curproxy->id, curproxy->email_alert.mailers.name);
2885 				free_email_alert(curproxy);
2886 				cfgerr++;
2887 			}
2888 			else {
2889 				err = NULL;
2890 				if (init_email_alert(curmailers, curproxy, &err)) {
2891 					ha_alert("Proxy '%s': %s.\n", curproxy->id, err);
2892 					free(err);
2893 					cfgerr++;
2894 				}
2895 			}
2896 		}
2897 
2898 		if (curproxy->uri_auth && !(curproxy->uri_auth->flags & ST_CONVDONE) &&
2899 		    !LIST_ISEMPTY(&curproxy->uri_auth->http_req_rules) &&
2900 		    (curproxy->uri_auth->userlist || curproxy->uri_auth->auth_realm )) {
2901 			ha_alert("%s '%s': stats 'auth'/'realm' and 'http-request' can't be used at the same time.\n",
2902 				 "proxy", curproxy->id);
2903 			cfgerr++;
2904 			goto out_uri_auth_compat;
2905 		}
2906 
2907 		if (curproxy->uri_auth && curproxy->uri_auth->userlist && !(curproxy->uri_auth->flags & ST_CONVDONE)) {
2908 			const char *uri_auth_compat_req[10];
2909 			struct act_rule *rule;
2910 			int i = 0;
2911 
2912 			/* build the ACL condition from scratch. We're relying on anonymous ACLs for that */
2913 			uri_auth_compat_req[i++] = "auth";
2914 
2915 			if (curproxy->uri_auth->auth_realm) {
2916 				uri_auth_compat_req[i++] = "realm";
2917 				uri_auth_compat_req[i++] = curproxy->uri_auth->auth_realm;
2918 			}
2919 
2920 			uri_auth_compat_req[i++] = "unless";
2921 			uri_auth_compat_req[i++] = "{";
2922 			uri_auth_compat_req[i++] = "http_auth(.internal-stats-userlist)";
2923 			uri_auth_compat_req[i++] = "}";
2924 			uri_auth_compat_req[i++] = "";
2925 
2926 			rule = parse_http_req_cond(uri_auth_compat_req, "internal-stats-auth-compat", 0, curproxy);
2927 			if (!rule) {
2928 				cfgerr++;
2929 				break;
2930 			}
2931 
2932 			LIST_ADDQ(&curproxy->uri_auth->http_req_rules, &rule->list);
2933 
2934 			if (curproxy->uri_auth->auth_realm) {
2935 				free(curproxy->uri_auth->auth_realm);
2936 				curproxy->uri_auth->auth_realm = NULL;
2937 			}
2938 
2939 			curproxy->uri_auth->flags |= ST_CONVDONE;
2940 		}
2941 out_uri_auth_compat:
2942 
2943 		/* check whether we have a log server that uses RFC5424 log format */
2944 		list_for_each_entry(tmplogsrv, &curproxy->logsrvs, list) {
2945 			if (tmplogsrv->format == LOG_FORMAT_RFC5424) {
2946 				if (!curproxy->conf.logformat_sd_string) {
2947 					/* set the default logformat_sd_string */
2948 					curproxy->conf.logformat_sd_string = default_rfc5424_sd_log_format;
2949 				}
2950 				break;
2951 			}
2952 		}
2953 
2954 		/* compile the log format */
2955 		if (!(curproxy->cap & PR_CAP_FE)) {
2956 			if (curproxy->conf.logformat_string != default_http_log_format &&
2957 			    curproxy->conf.logformat_string != default_tcp_log_format &&
2958 			    curproxy->conf.logformat_string != clf_http_log_format)
2959 				free(curproxy->conf.logformat_string);
2960 			curproxy->conf.logformat_string = NULL;
2961 			free(curproxy->conf.lfs_file);
2962 			curproxy->conf.lfs_file = NULL;
2963 			curproxy->conf.lfs_line = 0;
2964 
2965 			if (curproxy->conf.logformat_sd_string != default_rfc5424_sd_log_format)
2966 				free(curproxy->conf.logformat_sd_string);
2967 			curproxy->conf.logformat_sd_string = NULL;
2968 			free(curproxy->conf.lfsd_file);
2969 			curproxy->conf.lfsd_file = NULL;
2970 			curproxy->conf.lfsd_line = 0;
2971 		}
2972 
2973 		if (curproxy->conf.logformat_string) {
2974 			curproxy->conf.args.ctx = ARGC_LOG;
2975 			curproxy->conf.args.file = curproxy->conf.lfs_file;
2976 			curproxy->conf.args.line = curproxy->conf.lfs_line;
2977 			err = NULL;
2978 			if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat, LOG_OPT_MANDATORY,
2979 			                            SMP_VAL_FE_LOG_END, &err)) {
2980 				ha_alert("Parsing [%s:%d]: failed to parse log-format : %s.\n",
2981 					 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
2982 				free(err);
2983 				cfgerr++;
2984 			}
2985 			curproxy->conf.args.file = NULL;
2986 			curproxy->conf.args.line = 0;
2987 		}
2988 
2989 		if (curproxy->conf.logformat_sd_string) {
2990 			curproxy->conf.args.ctx = ARGC_LOGSD;
2991 			curproxy->conf.args.file = curproxy->conf.lfsd_file;
2992 			curproxy->conf.args.line = curproxy->conf.lfsd_line;
2993 			err = NULL;
2994 			if (!parse_logformat_string(curproxy->conf.logformat_sd_string, curproxy, &curproxy->logformat_sd, LOG_OPT_MANDATORY,
2995 			                            SMP_VAL_FE_LOG_END, &err)) {
2996 				ha_alert("Parsing [%s:%d]: failed to parse log-format-sd : %s.\n",
2997 					 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
2998 				free(err);
2999 				cfgerr++;
3000 			} else if (!add_to_logformat_list(NULL, NULL, LF_SEPARATOR, &curproxy->logformat_sd, &err)) {
3001 				ha_alert("Parsing [%s:%d]: failed to parse log-format-sd : %s.\n",
3002 					 curproxy->conf.lfs_file, curproxy->conf.lfs_line, err);
3003 				free(err);
3004 				cfgerr++;
3005 			}
3006 			curproxy->conf.args.file = NULL;
3007 			curproxy->conf.args.line = 0;
3008 		}
3009 
3010 		if (curproxy->conf.uniqueid_format_string) {
3011 			curproxy->conf.args.ctx = ARGC_UIF;
3012 			curproxy->conf.args.file = curproxy->conf.uif_file;
3013 			curproxy->conf.args.line = curproxy->conf.uif_line;
3014 			err = NULL;
3015 			if (!parse_logformat_string(curproxy->conf.uniqueid_format_string, curproxy, &curproxy->format_unique_id, LOG_OPT_HTTP,
3016 			                            (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &err)) {
3017 				ha_alert("Parsing [%s:%d]: failed to parse unique-id : %s.\n",
3018 					 curproxy->conf.uif_file, curproxy->conf.uif_line, err);
3019 				free(err);
3020 				cfgerr++;
3021 			}
3022 			curproxy->conf.args.file = NULL;
3023 			curproxy->conf.args.line = 0;
3024 		}
3025 
3026 		/* only now we can check if some args remain unresolved.
3027 		 * This must be done after the users and groups resolution.
3028 		 */
3029 		cfgerr += smp_resolve_args(curproxy);
3030 		if (!cfgerr)
3031 			cfgerr += acl_find_targets(curproxy);
3032 
3033 		if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
3034 		    (((curproxy->cap & PR_CAP_FE) && !curproxy->timeout.client) ||
3035 		     ((curproxy->cap & PR_CAP_BE) && (curproxy->srv) &&
3036 		      (!curproxy->timeout.connect ||
3037 		       (!curproxy->timeout.server && (curproxy->mode == PR_MODE_HTTP || !curproxy->timeout.tunnel)))))) {
3038 			ha_warning("config : missing timeouts for %s '%s'.\n"
3039 				   "   | While not properly invalid, you will certainly encounter various problems\n"
3040 				   "   | with such a configuration. To fix this, please ensure that all following\n"
3041 				   "   | timeouts are set to a non-zero value: 'client', 'connect', 'server'.\n",
3042 				   proxy_type_str(curproxy), curproxy->id);
3043 			err_code |= ERR_WARN;
3044 		}
3045 
3046 		/* Historically, the tarpit and queue timeouts were inherited from contimeout.
3047 		 * We must still support older configurations, so let's find out whether those
3048 		 * parameters have been set or must be copied from contimeouts.
3049 		 */
3050 		if (!curproxy->timeout.tarpit)
3051 			curproxy->timeout.tarpit = curproxy->timeout.connect;
3052 		if ((curproxy->cap & PR_CAP_BE) && !curproxy->timeout.queue)
3053 			curproxy->timeout.queue = curproxy->timeout.connect;
3054 
3055 		if ((curproxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK) {
3056 			curproxy->check_len = sizeof(sslv3_client_hello_pkt) - 1;
3057 			curproxy->check_req = malloc(curproxy->check_len);
3058 			memcpy(curproxy->check_req, sslv3_client_hello_pkt, curproxy->check_len);
3059 		}
3060 
3061 		if (!LIST_ISEMPTY(&curproxy->tcpcheck_rules) &&
3062 		    (curproxy->options2 & PR_O2_CHK_ANY) != PR_O2_TCPCHK_CHK) {
3063 			ha_warning("config : %s '%s' uses tcp-check rules without 'option tcp-check', so the rules are ignored.\n",
3064 				   proxy_type_str(curproxy), curproxy->id);
3065 			err_code |= ERR_WARN;
3066 		}
3067 
3068 		/* ensure that cookie capture length is not too large */
3069 		if (curproxy->capture_len >= global.tune.cookie_len) {
3070 			ha_warning("config : truncating capture length to %d bytes for %s '%s'.\n",
3071 				   global.tune.cookie_len - 1, proxy_type_str(curproxy), curproxy->id);
3072 			err_code |= ERR_WARN;
3073 			curproxy->capture_len = global.tune.cookie_len - 1;
3074 		}
3075 
3076 		/* The small pools required for the capture lists */
3077 		if (curproxy->nb_req_cap) {
3078 			curproxy->req_cap_pool = create_pool("ptrcap",
3079 			                                     curproxy->nb_req_cap * sizeof(char *),
3080 			                                     MEM_F_SHARED);
3081 		}
3082 
3083 		if (curproxy->nb_rsp_cap) {
3084 			curproxy->rsp_cap_pool = create_pool("ptrcap",
3085 			                                     curproxy->nb_rsp_cap * sizeof(char *),
3086 			                                     MEM_F_SHARED);
3087 		}
3088 
3089 		switch (curproxy->load_server_state_from_file) {
3090 			case PR_SRV_STATE_FILE_UNSPEC:
3091 				curproxy->load_server_state_from_file = PR_SRV_STATE_FILE_NONE;
3092 				break;
3093 			case PR_SRV_STATE_FILE_GLOBAL:
3094 				if (!global.server_state_file) {
3095 					ha_warning("config : backend '%s' configured to load server state file from global section 'server-state-file' directive. Unfortunately, 'server-state-file' is not set!\n",
3096 						   curproxy->id);
3097 					err_code |= ERR_WARN;
3098 				}
3099 				break;
3100 		}
3101 
3102 		/* first, we will invert the servers list order */
3103 		newsrv = NULL;
3104 		while (curproxy->srv) {
3105 			struct server *next;
3106 
3107 			next = curproxy->srv->next;
3108 			curproxy->srv->next = newsrv;
3109 			newsrv = curproxy->srv;
3110 			if (!next)
3111 				break;
3112 			curproxy->srv = next;
3113 		}
3114 
3115 		/* Check that no server name conflicts. This causes trouble in the stats.
3116 		 * We only emit a warning for the first conflict affecting each server,
3117 		 * in order to avoid combinatory explosion if all servers have the same
3118 		 * name. We do that only for servers which do not have an explicit ID,
3119 		 * because these IDs were made also for distinguishing them and we don't
3120 		 * want to annoy people who correctly manage them.
3121 		 */
3122 		for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
3123 			struct server *other_srv;
3124 
3125 			if (newsrv->puid)
3126 				continue;
3127 
3128 			for (other_srv = curproxy->srv; other_srv && other_srv != newsrv; other_srv = other_srv->next) {
3129 				if (!other_srv->puid && strcmp(other_srv->id, newsrv->id) == 0) {
3130 					ha_alert("parsing [%s:%d] : %s '%s', another server named '%s' was already defined at line %d, please use distinct names.\n",
3131 						   newsrv->conf.file, newsrv->conf.line,
3132 						   proxy_type_str(curproxy), curproxy->id,
3133 						   newsrv->id, other_srv->conf.line);
3134 					cfgerr++;
3135 					break;
3136 				}
3137 			}
3138 		}
3139 
3140 		/* assign automatic UIDs to servers which don't have one yet */
3141 		next_id = 1;
3142 		newsrv = curproxy->srv;
3143 		while (newsrv != NULL) {
3144 			if (!newsrv->puid) {
3145 				/* server ID not set, use automatic numbering with first
3146 				 * spare entry starting with next_svid.
3147 				 */
3148 				next_id = get_next_id(&curproxy->conf.used_server_id, next_id);
3149 				newsrv->conf.id.key = newsrv->puid = next_id;
3150 				eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
3151 			}
3152 			newsrv->conf.name.key = newsrv->id;
3153 			ebis_insert(&curproxy->conf.used_server_name, &newsrv->conf.name);
3154 
3155 			next_id++;
3156 			newsrv = newsrv->next;
3157 		}
3158 
3159 		curproxy->lbprm.wmult = 1; /* default weight multiplier */
3160 		curproxy->lbprm.wdiv  = 1; /* default weight divider */
3161 
3162 		/*
3163 		 * If this server supports a maxconn parameter, it needs a dedicated
3164 		 * tasks to fill the emptied slots when a connection leaves.
3165 		 * Also, resolve deferred tracking dependency if needed.
3166 		 */
3167 		newsrv = curproxy->srv;
3168 		while (newsrv != NULL) {
3169 			if (newsrv->minconn > newsrv->maxconn) {
3170 				/* Only 'minconn' was specified, or it was higher than or equal
3171 				 * to 'maxconn'. Let's turn this into maxconn and clean it, as
3172 				 * this will avoid further useless expensive computations.
3173 				 */
3174 				newsrv->maxconn = newsrv->minconn;
3175 			} else if (newsrv->maxconn && !newsrv->minconn) {
3176 				/* minconn was not specified, so we set it to maxconn */
3177 				newsrv->minconn = newsrv->maxconn;
3178 			}
3179 
3180 			/* this will also properly set the transport layer for prod and checks */
3181 			if (newsrv->use_ssl == 1 || newsrv->check.use_ssl == 1 || (newsrv->proxy->options & PR_O_TCPCHK_SSL)) {
3182 				if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv)
3183 					cfgerr += xprt_get(XPRT_SSL)->prepare_srv(newsrv);
3184 			}
3185 
3186 			if ((newsrv->flags & SRV_F_FASTOPEN) &&
3187 			    ((curproxy->retry_type & (PR_RE_DISCONNECTED | PR_RE_TIMEOUT)) !=
3188 			     (PR_RE_DISCONNECTED | PR_RE_TIMEOUT)))
3189 				ha_warning("parsing [%s:%d] : %s '%s': server '%s' has tfo activated, the backend should be configured with at least 'conn-failure', 'empty-response' and 'response-timeout' or we wouldn't be able to retry the connection on failure.\n",
3190 				    newsrv->conf.file, newsrv->conf.line,
3191 				    proxy_type_str(curproxy), curproxy->id,
3192 				    newsrv->id);
3193 
3194 			if (newsrv->trackit) {
3195 				struct proxy *px;
3196 				struct server *srv, *loop;
3197 				char *pname, *sname;
3198 
3199 				pname = newsrv->trackit;
3200 				sname = strrchr(pname, '/');
3201 
3202 				if (sname)
3203 					*sname++ = '\0';
3204 				else {
3205 					sname = pname;
3206 					pname = NULL;
3207 				}
3208 
3209 				if (pname) {
3210 					px = proxy_be_by_name(pname);
3211 					if (!px) {
3212 						ha_alert("config : %s '%s', server '%s': unable to find required proxy '%s' for tracking.\n",
3213 							 proxy_type_str(curproxy), curproxy->id,
3214 							 newsrv->id, pname);
3215 						cfgerr++;
3216 						goto next_srv;
3217 					}
3218 				} else
3219 					px = curproxy;
3220 
3221 				srv = findserver(px, sname);
3222 				if (!srv) {
3223 					ha_alert("config : %s '%s', server '%s': unable to find required server '%s' for tracking.\n",
3224 						 proxy_type_str(curproxy), curproxy->id,
3225 						 newsrv->id, sname);
3226 					cfgerr++;
3227 					goto next_srv;
3228 				}
3229 
3230 				if (!srv->do_check && !srv->do_agent && !srv->track && !srv->trackit) {
3231 					ha_alert("config : %s '%s', server '%s': unable to use %s/%s for "
3232 						 "tracking as it does not have any check nor agent enabled.\n",
3233 						 proxy_type_str(curproxy), curproxy->id,
3234 						 newsrv->id, px->id, srv->id);
3235 					cfgerr++;
3236 					goto next_srv;
3237 				}
3238 
3239 				for (loop = srv->track; loop && loop != newsrv; loop = loop->track);
3240 
3241 				if (newsrv == srv || loop) {
3242 					ha_alert("config : %s '%s', server '%s': unable to track %s/%s as it "
3243 						 "belongs to a tracking chain looping back to %s/%s.\n",
3244 						 proxy_type_str(curproxy), curproxy->id,
3245 						 newsrv->id, px->id, srv->id, px->id,
3246 						 newsrv == srv ? srv->id : loop->id);
3247 					cfgerr++;
3248 					goto next_srv;
3249 				}
3250 
3251 				if (curproxy != px &&
3252 					(curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
3253 					ha_alert("config : %s '%s', server '%s': unable to use %s/%s for"
3254 						 "tracking: disable-on-404 option inconsistency.\n",
3255 						 proxy_type_str(curproxy), curproxy->id,
3256 						 newsrv->id, px->id, srv->id);
3257 					cfgerr++;
3258 					goto next_srv;
3259 				}
3260 
3261 				newsrv->track = srv;
3262 				newsrv->tracknext = srv->trackers;
3263 				srv->trackers = newsrv;
3264 
3265 				free(newsrv->trackit);
3266 				newsrv->trackit = NULL;
3267 			}
3268 
3269 		next_srv:
3270 			newsrv = newsrv->next;
3271 		}
3272 
3273 		/*
3274 		 * Try to generate dynamic cookies for servers now.
3275 		 * It couldn't be done earlier, since at the time we parsed
3276 		 * the server line, we may not have known yet that we
3277 		 * should use dynamic cookies, or the secret key may not
3278 		 * have been provided yet.
3279 		 */
3280 		if (curproxy->ck_opts & PR_CK_DYNAMIC) {
3281 			newsrv = curproxy->srv;
3282 			while (newsrv != NULL) {
3283 				srv_set_dyncookie(newsrv);
3284 				newsrv = newsrv->next;
3285 			}
3286 
3287 		}
3288 		/* We have to initialize the server lookup mechanism depending
3289 		 * on what LB algorithm was chosen.
3290 		 */
3291 
3292 		curproxy->lbprm.algo &= ~(BE_LB_LKUP | BE_LB_PROP_DYN);
3293 		switch (curproxy->lbprm.algo & BE_LB_KIND) {
3294 		case BE_LB_KIND_RR:
3295 			if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_RR_STATIC) {
3296 				curproxy->lbprm.algo |= BE_LB_LKUP_MAP;
3297 				init_server_map(curproxy);
3298 			} else if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_RR_RANDOM) {
3299 				curproxy->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
3300 				if (chash_init_server_tree(curproxy) < 0) {
3301 					cfgerr++;
3302 				}
3303 			} else {
3304 				curproxy->lbprm.algo |= BE_LB_LKUP_RRTREE | BE_LB_PROP_DYN;
3305 				fwrr_init_server_groups(curproxy);
3306 			}
3307 			break;
3308 
3309 		case BE_LB_KIND_CB:
3310 			if ((curproxy->lbprm.algo & BE_LB_PARM) == BE_LB_CB_LC) {
3311 				curproxy->lbprm.algo |= BE_LB_LKUP_LCTREE | BE_LB_PROP_DYN;
3312 				fwlc_init_server_tree(curproxy);
3313 			} else {
3314 				curproxy->lbprm.algo |= BE_LB_LKUP_FSTREE | BE_LB_PROP_DYN;
3315 				fas_init_server_tree(curproxy);
3316 			}
3317 			break;
3318 
3319 		case BE_LB_KIND_HI:
3320 			if ((curproxy->lbprm.algo & BE_LB_HASH_TYPE) == BE_LB_HASH_CONS) {
3321 				curproxy->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
3322 				if (chash_init_server_tree(curproxy) < 0) {
3323 					cfgerr++;
3324 				}
3325 			} else {
3326 				curproxy->lbprm.algo |= BE_LB_LKUP_MAP;
3327 				init_server_map(curproxy);
3328 			}
3329 			break;
3330 		}
3331 		HA_SPIN_INIT(&curproxy->lbprm.lock);
3332 
3333 		if (curproxy->options & PR_O_LOGASAP)
3334 			curproxy->to_log &= ~LW_BYTES;
3335 
3336 		if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
3337 		    (curproxy->cap & PR_CAP_FE) && LIST_ISEMPTY(&curproxy->logsrvs) &&
3338 		    (!LIST_ISEMPTY(&curproxy->logformat) || !LIST_ISEMPTY(&curproxy->logformat_sd))) {
3339 			ha_warning("config : log format ignored for %s '%s' since it has no log address.\n",
3340 				   proxy_type_str(curproxy), curproxy->id);
3341 			err_code |= ERR_WARN;
3342 		}
3343 
3344 		if (curproxy->mode != PR_MODE_HTTP) {
3345 			int optnum;
3346 
3347 			if (curproxy->uri_auth) {
3348 				ha_warning("config : 'stats' statement ignored for %s '%s' as it requires HTTP mode.\n",
3349 					   proxy_type_str(curproxy), curproxy->id);
3350 				err_code |= ERR_WARN;
3351 				curproxy->uri_auth = NULL;
3352 			}
3353 
3354 			if (curproxy->capture_name) {
3355 				ha_warning("config : 'capture' statement ignored for %s '%s' as it requires HTTP mode.\n",
3356 					   proxy_type_str(curproxy), curproxy->id);
3357 				err_code |= ERR_WARN;
3358 			}
3359 
3360 			if (!LIST_ISEMPTY(&curproxy->http_req_rules)) {
3361 				ha_warning("config : 'http-request' rules ignored for %s '%s' as they require HTTP mode.\n",
3362 					   proxy_type_str(curproxy), curproxy->id);
3363 				err_code |= ERR_WARN;
3364 			}
3365 
3366 			if (!LIST_ISEMPTY(&curproxy->http_res_rules)) {
3367 				ha_warning("config : 'http-response' rules ignored for %s '%s' as they require HTTP mode.\n",
3368 					   proxy_type_str(curproxy), curproxy->id);
3369 				err_code |= ERR_WARN;
3370 			}
3371 
3372 			if (!LIST_ISEMPTY(&curproxy->block_rules)) {
3373 				ha_warning("config : 'block' rules ignored for %s '%s' as they require HTTP mode.\n",
3374 					   proxy_type_str(curproxy), curproxy->id);
3375 				err_code |= ERR_WARN;
3376 			}
3377 
3378 			if (!LIST_ISEMPTY(&curproxy->redirect_rules)) {
3379 				ha_warning("config : 'redirect' rules ignored for %s '%s' as they require HTTP mode.\n",
3380 					   proxy_type_str(curproxy), curproxy->id);
3381 				err_code |= ERR_WARN;
3382 			}
3383 
3384 			if (curproxy->options & (PR_O_FWDFOR | PR_O_FF_ALWAYS)) {
3385 				ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3386 					   "forwardfor", proxy_type_str(curproxy), curproxy->id);
3387 				err_code |= ERR_WARN;
3388 				curproxy->options &= ~(PR_O_FWDFOR | PR_O_FF_ALWAYS);
3389 			}
3390 
3391 			if (curproxy->options & PR_O_ORGTO) {
3392 				ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3393 					   "originalto", proxy_type_str(curproxy), curproxy->id);
3394 				err_code |= ERR_WARN;
3395 				curproxy->options &= ~PR_O_ORGTO;
3396 			}
3397 
3398 			for (optnum = 0; cfg_opts[optnum].name; optnum++) {
3399 				if (cfg_opts[optnum].mode == PR_MODE_HTTP &&
3400 				    (curproxy->cap & cfg_opts[optnum].cap) &&
3401 				    (curproxy->options & cfg_opts[optnum].val)) {
3402 					ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3403 						   cfg_opts[optnum].name, proxy_type_str(curproxy), curproxy->id);
3404 					err_code |= ERR_WARN;
3405 					curproxy->options &= ~cfg_opts[optnum].val;
3406 				}
3407 			}
3408 
3409 			for (optnum = 0; cfg_opts2[optnum].name; optnum++) {
3410 				if (cfg_opts2[optnum].mode == PR_MODE_HTTP &&
3411 				    (curproxy->cap & cfg_opts2[optnum].cap) &&
3412 				    (curproxy->options2 & cfg_opts2[optnum].val)) {
3413 					ha_warning("config : 'option %s' ignored for %s '%s' as it requires HTTP mode.\n",
3414 						   cfg_opts2[optnum].name, proxy_type_str(curproxy), curproxy->id);
3415 					err_code |= ERR_WARN;
3416 					curproxy->options2 &= ~cfg_opts2[optnum].val;
3417 				}
3418 			}
3419 
3420 #if defined(CONFIG_HAP_TRANSPARENT)
3421 			if (curproxy->conn_src.bind_hdr_occ) {
3422 				curproxy->conn_src.bind_hdr_occ = 0;
3423 				ha_warning("config : %s '%s' : ignoring use of header %s as source IP in non-HTTP mode.\n",
3424 					   proxy_type_str(curproxy), curproxy->id, curproxy->conn_src.bind_hdr_name);
3425 				err_code |= ERR_WARN;
3426 			}
3427 #endif
3428 		}
3429 
3430 		/*
3431 		 * ensure that we're not cross-dressing a TCP server into HTTP.
3432 		 */
3433 		newsrv = curproxy->srv;
3434 		while (newsrv != NULL) {
3435 			if ((curproxy->mode != PR_MODE_HTTP) && newsrv->rdr_len) {
3436 				ha_alert("config : %s '%s' : server cannot have cookie or redirect prefix in non-HTTP mode.\n",
3437 					 proxy_type_str(curproxy), curproxy->id);
3438 				cfgerr++;
3439 			}
3440 
3441 			if ((curproxy->mode != PR_MODE_HTTP) && newsrv->cklen) {
3442 				ha_warning("config : %s '%s' : ignoring cookie for server '%s' as HTTP mode is disabled.\n",
3443 					   proxy_type_str(curproxy), curproxy->id, newsrv->id);
3444 				err_code |= ERR_WARN;
3445 			}
3446 
3447 			if ((newsrv->flags & SRV_F_MAPPORTS) && (curproxy->options2 & PR_O2_RDPC_PRST)) {
3448 				ha_warning("config : %s '%s' : RDP cookie persistence will not work for server '%s' because it lacks an explicit port number.\n",
3449 					   proxy_type_str(curproxy), curproxy->id, newsrv->id);
3450 				err_code |= ERR_WARN;
3451 			}
3452 
3453 #if defined(CONFIG_HAP_TRANSPARENT)
3454 			if (curproxy->mode != PR_MODE_HTTP && newsrv->conn_src.bind_hdr_occ) {
3455 				newsrv->conn_src.bind_hdr_occ = 0;
3456 				ha_warning("config : %s '%s' : server %s cannot use header %s as source IP in non-HTTP mode.\n",
3457 					   proxy_type_str(curproxy), curproxy->id, newsrv->id, newsrv->conn_src.bind_hdr_name);
3458 				err_code |= ERR_WARN;
3459 			}
3460 #endif
3461 
3462 			if ((curproxy->mode != PR_MODE_HTTP) && (curproxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR)
3463 				curproxy->options &= ~PR_O_REUSE_MASK;
3464 
3465 			newsrv = newsrv->next;
3466 		}
3467 
3468 		/* check if we have a frontend with "tcp-request content" looking at L7
3469 		 * with no inspect-delay
3470 		 */
3471 		if ((curproxy->cap & PR_CAP_FE) && !curproxy->tcp_req.inspect_delay) {
3472 			list_for_each_entry(arule, &curproxy->tcp_req.inspect_rules, list) {
3473 				if (arule->action == ACT_TCP_CAPTURE &&
3474 				    !(arule->arg.cap.expr->fetch->val & SMP_VAL_FE_SES_ACC))
3475 					break;
3476 				if  ((arule->action >= ACT_ACTION_TRK_SC0 && arule->action <= ACT_ACTION_TRK_SCMAX) &&
3477 				     !(arule->arg.trk_ctr.expr->fetch->val & SMP_VAL_FE_SES_ACC))
3478 					break;
3479 			}
3480 
3481 			if (&arule->list != &curproxy->tcp_req.inspect_rules) {
3482 				ha_warning("config : %s '%s' : some 'tcp-request content' rules explicitly depending on request"
3483 					   " contents were found in a frontend without any 'tcp-request inspect-delay' setting."
3484 					   " This means that these rules will randomly find their contents. This can be fixed by"
3485 					   " setting the tcp-request inspect-delay.\n",
3486 					   proxy_type_str(curproxy), curproxy->id);
3487 				err_code |= ERR_WARN;
3488 			}
3489 		}
3490 
3491 		/* Check filter configuration, if any */
3492 		cfgerr += flt_check(curproxy);
3493 
3494 		if (curproxy->cap & PR_CAP_FE) {
3495 			if (!curproxy->accept)
3496 				curproxy->accept = frontend_accept;
3497 
3498 			if (curproxy->tcp_req.inspect_delay ||
3499 			    !LIST_ISEMPTY(&curproxy->tcp_req.inspect_rules))
3500 				curproxy->fe_req_ana |= AN_REQ_INSPECT_FE;
3501 
3502 			if (curproxy->mode == PR_MODE_HTTP) {
3503 				curproxy->fe_req_ana |= AN_REQ_WAIT_HTTP | AN_REQ_HTTP_PROCESS_FE;
3504 				curproxy->fe_rsp_ana |= AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_FE;
3505 			}
3506 
3507 			if (curproxy->mode == PR_MODE_CLI) {
3508 				curproxy->fe_req_ana |= AN_REQ_WAIT_CLI;
3509 				curproxy->fe_rsp_ana |= AN_RES_WAIT_CLI;
3510 			}
3511 
3512 			/* both TCP and HTTP must check switching rules */
3513 			curproxy->fe_req_ana |= AN_REQ_SWITCHING_RULES;
3514 
3515 			/* Add filters analyzers if needed */
3516 			if (!LIST_ISEMPTY(&curproxy->filter_configs)) {
3517 				curproxy->fe_req_ana |= AN_REQ_FLT_START_FE | AN_REQ_FLT_XFER_DATA | AN_REQ_FLT_END;
3518 				curproxy->fe_rsp_ana |= AN_RES_FLT_START_FE | AN_RES_FLT_XFER_DATA | AN_RES_FLT_END;
3519 				if (curproxy->mode == PR_MODE_HTTP) {
3520 					curproxy->fe_req_ana |= AN_REQ_FLT_HTTP_HDRS;
3521 					curproxy->fe_rsp_ana |= AN_RES_FLT_HTTP_HDRS;
3522 				}
3523 			}
3524 		}
3525 
3526 		if (curproxy->cap & PR_CAP_BE) {
3527 			if (curproxy->tcp_req.inspect_delay ||
3528 			    !LIST_ISEMPTY(&curproxy->tcp_req.inspect_rules))
3529 				curproxy->be_req_ana |= AN_REQ_INSPECT_BE;
3530 
3531 			if (!LIST_ISEMPTY(&curproxy->tcp_rep.inspect_rules))
3532                                 curproxy->be_rsp_ana |= AN_RES_INSPECT;
3533 
3534 			if (curproxy->mode == PR_MODE_HTTP) {
3535 				curproxy->be_req_ana |= AN_REQ_WAIT_HTTP | AN_REQ_HTTP_INNER | AN_REQ_HTTP_PROCESS_BE;
3536 				curproxy->be_rsp_ana |= AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE;
3537 			}
3538 
3539 			/* If the backend does requires RDP cookie persistence, we have to
3540 			 * enable the corresponding analyser.
3541 			 */
3542 			if (curproxy->options2 & PR_O2_RDPC_PRST)
3543 				curproxy->be_req_ana |= AN_REQ_PRST_RDP_COOKIE;
3544 
3545 			/* Add filters analyzers if needed */
3546 			if (!LIST_ISEMPTY(&curproxy->filter_configs)) {
3547 				curproxy->be_req_ana |= AN_REQ_FLT_START_BE | AN_REQ_FLT_XFER_DATA | AN_REQ_FLT_END;
3548 				curproxy->be_rsp_ana |= AN_RES_FLT_START_BE | AN_RES_FLT_XFER_DATA | AN_RES_FLT_END;
3549 				if (curproxy->mode == PR_MODE_HTTP) {
3550 					curproxy->be_req_ana |= AN_REQ_FLT_HTTP_HDRS;
3551 					curproxy->be_rsp_ana |= AN_RES_FLT_HTTP_HDRS;
3552 				}
3553 			}
3554 		}
3555 
3556 		/* Check the mux protocols, if any, for each listener and server
3557 		 * attached to the current proxy */
3558 		list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
3559 			int mode = (1 << (curproxy->mode == PR_MODE_HTTP));
3560 			const struct mux_proto_list *mux_ent;
3561 
3562 			/* Special case for HTX because legacy HTTP still exists */
3563 			if (mode == PROTO_MODE_HTTP && (curproxy->options2 & PR_O2_USE_HTX))
3564 				mode = PROTO_MODE_HTX;
3565 
3566 			if (!bind_conf->mux_proto)
3567 				continue;
3568 
3569 			/* it is possible that an incorrect mux was referenced
3570 			 * due to the proxy's mode not being taken into account
3571 			 * on first pass. Let's adjust it now.
3572 			 */
3573 			mux_ent = conn_get_best_mux_entry(bind_conf->mux_proto->token, PROTO_SIDE_FE, mode);
3574 
3575 			if (!mux_ent || !isteq(mux_ent->token, bind_conf->mux_proto->token)) {
3576 				ha_alert("config : %s '%s' : MUX protocol '%.*s' is not usable for 'bind %s' at [%s:%d].\n",
3577 					 proxy_type_str(curproxy), curproxy->id,
3578 					 (int)bind_conf->mux_proto->token.len,
3579 					 bind_conf->mux_proto->token.ptr,
3580 					 bind_conf->arg, bind_conf->file, bind_conf->line);
3581 				cfgerr++;
3582 			}
3583 
3584 			/* update the mux */
3585 			bind_conf->mux_proto = mux_ent;
3586 		}
3587 		for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
3588 			int mode = (1 << (curproxy->mode == PR_MODE_HTTP));
3589 			const struct mux_proto_list *mux_ent;
3590 
3591 			/* Special case for HTX because legacy HTTP still exists */
3592 			if (mode == PROTO_MODE_HTTP && (curproxy->options2 & PR_O2_USE_HTX))
3593 				mode = PROTO_MODE_HTX;
3594 
3595 			if (!newsrv->mux_proto)
3596 				continue;
3597 
3598 			/* it is possible that an incorrect mux was referenced
3599 			 * due to the proxy's mode not being taken into account
3600 			 * on first pass. Let's adjust it now.
3601 			 */
3602 			mux_ent = conn_get_best_mux_entry(newsrv->mux_proto->token, PROTO_SIDE_BE, mode);
3603 
3604 			if (!mux_ent || !isteq(mux_ent->token, newsrv->mux_proto->token)) {
3605 				ha_alert("config : %s '%s' : MUX protocol '%.*s' is not usable for server '%s' at [%s:%d].\n",
3606 					 proxy_type_str(curproxy), curproxy->id,
3607 					 (int)newsrv->mux_proto->token.len,
3608 					 newsrv->mux_proto->token.ptr,
3609 					 newsrv->id, newsrv->conf.file, newsrv->conf.line);
3610 				cfgerr++;
3611 			}
3612 
3613 			/* update the mux */
3614 			newsrv->mux_proto = mux_ent;
3615 		}
3616 
3617 		/* the option "http-tunnel" is ignored when HTX is enabled and
3618 		 * only works with the legacy HTTP. So emit a warning if the
3619 		 * option is set on a HTX frontend. */
3620 		if ((curproxy->cap & PR_CAP_FE) && curproxy->options2 & PR_O2_USE_HTX &&
3621 		    (curproxy->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) {
3622 			ha_warning("config : %s '%s' : the option 'http-tunnel' is ignored for HTX proxies.\n",
3623 				   proxy_type_str(curproxy), curproxy->id);
3624 			curproxy->options &= ~PR_O_HTTP_MODE;
3625 		}
3626 
3627 		/* initialize idle conns lists */
3628 		for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
3629 			int i;
3630 
3631 			newsrv->priv_conns = calloc(global.nbthread, sizeof(*newsrv->priv_conns));
3632 			newsrv->idle_conns = calloc(global.nbthread, sizeof(*newsrv->idle_conns));
3633 			newsrv->safe_conns = calloc(global.nbthread, sizeof(*newsrv->safe_conns));
3634 
3635 			if (!newsrv->priv_conns || !newsrv->idle_conns || !newsrv->safe_conns) {
3636 				free(newsrv->safe_conns); newsrv->safe_conns = NULL;
3637 				free(newsrv->idle_conns); newsrv->idle_conns = NULL;
3638 				free(newsrv->priv_conns); newsrv->priv_conns = NULL;
3639 				ha_alert("parsing [%s:%d] : failed to allocate idle connections for server '%s'.\n",
3640 					 newsrv->conf.file, newsrv->conf.line, newsrv->id);
3641 				cfgerr++;
3642 				continue;
3643 			}
3644 
3645 			for (i = 0; i < global.nbthread; i++) {
3646 				LIST_INIT(&newsrv->priv_conns[i]);
3647 				LIST_INIT(&newsrv->idle_conns[i]);
3648 				LIST_INIT(&newsrv->safe_conns[i]);
3649 			}
3650 
3651 			if (newsrv->max_idle_conns != 0) {
3652 				if (idle_conn_task == NULL) {
3653 					idle_conn_task = task_new(MAX_THREADS_MASK);
3654 					if (!idle_conn_task)
3655 						goto err;
3656 					idle_conn_task->process = srv_cleanup_idle_connections;
3657 					idle_conn_task->context = NULL;
3658 					for (i = 0; i < global.nbthread; i++) {
3659 						idle_conn_cleanup[i] = task_new(1UL << i);
3660 						if (!idle_conn_cleanup[i])
3661 							goto err;
3662 						idle_conn_cleanup[i]->process = srv_cleanup_toremove_connections;
3663 						idle_conn_cleanup[i]->context = NULL;
3664 						HA_SPIN_INIT(&toremove_lock[i]);
3665 						LIST_INIT(&toremove_connections[i]);
3666 					}
3667 				}
3668 				newsrv->idle_orphan_conns = calloc((unsigned short)global.nbthread, sizeof(*newsrv->idle_orphan_conns));
3669 				if (!newsrv->idle_orphan_conns)
3670 					goto err;
3671 				for (i = 0; i < global.nbthread; i++)
3672 					LIST_INIT(&newsrv->idle_orphan_conns[i]);
3673 				newsrv->curr_idle_thr = calloc(global.nbthread, sizeof(*newsrv->curr_idle_thr));
3674 				if (!newsrv->curr_idle_thr)
3675 					goto err;
3676 				continue;
3677 			err:
3678 				ha_alert("parsing [%s:%d] : failed to allocate idle connection tasks for server '%s'.\n",
3679 					 newsrv->conf.file, newsrv->conf.line, newsrv->id);
3680 				cfgerr++;
3681 				continue;
3682 			}
3683 		}
3684 	}
3685 
3686 	/***********************************************************/
3687 	/* At this point, target names have already been resolved. */
3688 	/***********************************************************/
3689 
3690 	/* Check multi-process mode compatibility */
3691 
3692 	if (global.nbproc > 1 && global.stats_fe) {
3693 		list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
3694 			unsigned long mask;
3695 
3696 			mask  = proc_mask(global.stats_fe->bind_proc) && all_proc_mask;
3697 			mask &= proc_mask(bind_conf->bind_proc);
3698 
3699 			/* stop here if more than one process is used */
3700 			if (atleast2(mask))
3701 				break;
3702 		}
3703 		if (&bind_conf->by_fe != &global.stats_fe->conf.bind) {
3704 			ha_warning("stats socket will not work as expected in multi-process mode (nbproc > 1), you should force process binding globally using 'stats bind-process' or per socket using the 'process' attribute.\n");
3705 		}
3706 	}
3707 
3708 	/* Make each frontend inherit bind-process from its listeners when not specified. */
3709 	for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
3710 		if (curproxy->bind_proc)
3711 			continue;
3712 
3713 		list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
3714 			unsigned long mask;
3715 
3716 			mask = proc_mask(bind_conf->bind_proc);
3717 			curproxy->bind_proc |= mask;
3718 		}
3719 		curproxy->bind_proc = proc_mask(curproxy->bind_proc);
3720 	}
3721 
3722 	if (global.stats_fe) {
3723 		list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
3724 			unsigned long mask;
3725 
3726 			mask = bind_conf->bind_proc ? bind_conf->bind_proc : 0;
3727 			global.stats_fe->bind_proc |= mask;
3728 		}
3729 		global.stats_fe->bind_proc = proc_mask(global.stats_fe->bind_proc);
3730 	}
3731 
3732 	/* propagate bindings from frontends to backends. Don't do it if there
3733 	 * are any fatal errors as we must not call it with unresolved proxies.
3734 	 */
3735 	if (!cfgerr) {
3736 		for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
3737 			if (curproxy->cap & PR_CAP_FE)
3738 				propagate_processes(curproxy, NULL);
3739 		}
3740 	}
3741 
3742 	/* Bind each unbound backend to all processes when not specified. */
3743 	for (curproxy = proxies_list; curproxy; curproxy = curproxy->next)
3744 		curproxy->bind_proc = proc_mask(curproxy->bind_proc);
3745 
3746 	/*******************************************************/
3747 	/* At this step, all proxies have a non-null bind_proc */
3748 	/*******************************************************/
3749 
3750 	/* perform the final checks before creating tasks */
3751 
3752 	for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
3753 		struct listener *listener;
3754 		unsigned int next_id;
3755 
3756 		/* Configure SSL for each bind line.
3757 		 * Note: if configuration fails at some point, the ->ctx member
3758 		 * remains NULL so that listeners can later detach.
3759 		 */
3760 		list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
3761 			if (bind_conf->xprt->prepare_bind_conf &&
3762 			    bind_conf->xprt->prepare_bind_conf(bind_conf) < 0)
3763 				cfgerr++;
3764 		}
3765 
3766 		/* adjust this proxy's listeners */
3767 		next_id = 1;
3768 		list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) {
3769 			int nbproc;
3770 
3771 			nbproc = my_popcountl(curproxy->bind_proc &
3772 			                      (listener->bind_conf->bind_proc ? listener->bind_conf->bind_proc : curproxy->bind_proc) &
3773 			                      all_proc_mask);
3774 
3775 			if (!nbproc) /* no intersection between listener and frontend */
3776 				nbproc = 1;
3777 
3778 			if (!listener->luid) {
3779 				/* listener ID not set, use automatic numbering with first
3780 				 * spare entry starting with next_luid.
3781 				 */
3782 				next_id = get_next_id(&curproxy->conf.used_listener_id, next_id);
3783 				listener->conf.id.key = listener->luid = next_id;
3784 				eb32_insert(&curproxy->conf.used_listener_id, &listener->conf.id);
3785 			}
3786 			next_id++;
3787 
3788 			/* enable separate counters */
3789 			if (curproxy->options2 & PR_O2_SOCKSTAT) {
3790 				listener->counters = calloc(1, sizeof(*listener->counters));
3791 				if (!listener->name)
3792 					memprintf(&listener->name, "sock-%d", listener->luid);
3793 			}
3794 
3795 			if (curproxy->options & PR_O_TCP_NOLING)
3796 				listener->options |= LI_O_NOLINGER;
3797 			if (!listener->maxaccept)
3798 				listener->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : 64;
3799 
3800 			/* we want to have an optimal behaviour on single process mode to
3801 			 * maximize the work at once, but in multi-process we want to keep
3802 			 * some fairness between processes, so we target half of the max
3803 			 * number of events to be balanced over all the processes the proxy
3804 			 * is bound to. Rememeber that maxaccept = -1 must be kept as it is
3805 			 * used to disable the limit.
3806 			 */
3807 			if (listener->maxaccept > 0 && nbproc > 1) {
3808 				listener->maxaccept = (listener->maxaccept + 1) / 2;
3809 				listener->maxaccept = (listener->maxaccept + nbproc - 1) / nbproc;
3810 			}
3811 
3812 			listener->accept = session_accept_fd;
3813 			listener->analysers |= curproxy->fe_req_ana;
3814 			listener->default_target = curproxy->default_target;
3815 
3816 			if (!LIST_ISEMPTY(&curproxy->tcp_req.l4_rules))
3817 				listener->options |= LI_O_TCP_L4_RULES;
3818 
3819 			if (!LIST_ISEMPTY(&curproxy->tcp_req.l5_rules))
3820 				listener->options |= LI_O_TCP_L5_RULES;
3821 
3822 			if (curproxy->mon_mask.s_addr)
3823 				listener->options |= LI_O_CHK_MONNET;
3824 
3825 			/* smart accept mode is automatic in HTTP mode */
3826 			if ((curproxy->options2 & PR_O2_SMARTACC) ||
3827 			    ((curproxy->mode == PR_MODE_HTTP || listener->bind_conf->is_ssl) &&
3828 			     !(curproxy->no_options2 & PR_O2_SMARTACC)))
3829 				listener->options |= LI_O_NOQUICKACK;
3830 		}
3831 
3832 		/* Release unused SSL configs */
3833 		list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
3834 			if (!bind_conf->is_ssl && bind_conf->xprt->destroy_bind_conf)
3835 				bind_conf->xprt->destroy_bind_conf(bind_conf);
3836 		}
3837 
3838 		if (atleast2(curproxy->bind_proc & all_proc_mask)) {
3839 			if (curproxy->uri_auth) {
3840 				int count, maxproc = 0;
3841 
3842 				list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
3843 					count = my_popcountl(bind_conf->bind_proc);
3844 					if (count > maxproc)
3845 						maxproc = count;
3846 				}
3847 				/* backends have 0, frontends have 1 or more */
3848 				if (maxproc != 1)
3849 					ha_warning("Proxy '%s': in multi-process mode, stats will be"
3850 						   " limited to process assigned to the current request.\n",
3851 						   curproxy->id);
3852 
3853 				if (!LIST_ISEMPTY(&curproxy->uri_auth->admin_rules)) {
3854 					ha_warning("Proxy '%s': stats admin will not work correctly in multi-process mode.\n",
3855 						   curproxy->id);
3856 				}
3857 			}
3858 			if (!LIST_ISEMPTY(&curproxy->sticking_rules)) {
3859 				ha_warning("Proxy '%s': sticking rules will not work correctly in multi-process mode.\n",
3860 					   curproxy->id);
3861 			}
3862 		}
3863 
3864 		/* create the task associated with the proxy */
3865 		curproxy->task = task_new(MAX_THREADS_MASK);
3866 		if (curproxy->task) {
3867 			curproxy->task->context = curproxy;
3868 			curproxy->task->process = manage_proxy;
3869 		} else {
3870 			ha_alert("Proxy '%s': no more memory when trying to allocate the management task\n",
3871 				 curproxy->id);
3872 			cfgerr++;
3873 		}
3874 	}
3875 
3876 	/*
3877 	 * Recount currently required checks.
3878 	 */
3879 
3880 	for (curproxy=proxies_list; curproxy; curproxy=curproxy->next) {
3881 		int optnum;
3882 
3883 		for (optnum = 0; cfg_opts[optnum].name; optnum++)
3884 			if (curproxy->options & cfg_opts[optnum].val)
3885 				global.last_checks |= cfg_opts[optnum].checks;
3886 
3887 		for (optnum = 0; cfg_opts2[optnum].name; optnum++)
3888 			if (curproxy->options2 & cfg_opts2[optnum].val)
3889 				global.last_checks |= cfg_opts2[optnum].checks;
3890 	}
3891 
3892 	/* compute the required process bindings for the peers */
3893 	for (curproxy = proxies_list; curproxy; curproxy = curproxy->next)
3894 		if (curproxy->table && curproxy->table->peers.p)
3895 			curproxy->table->peers.p->peers_fe->bind_proc |= curproxy->bind_proc;
3896 
3897 	/* compute the required process bindings for the peers from <stktables_list>
3898 	 * for all the stick-tables, the ones coming with "peers" sections included.
3899 	 */
3900 	for (t = stktables_list; t; t = t->next) {
3901 		struct proxy *p;
3902 
3903 		for (p = t->proxies_list; p; p = p->next_stkt_ref) {
3904 			if (t->peers.p && t->peers.p->peers_fe) {
3905 				t->peers.p->peers_fe->bind_proc |= p->bind_proc;
3906 			}
3907 		}
3908 	}
3909 
3910 	if (cfg_peers) {
3911 		struct peers *curpeers = cfg_peers, **last;
3912 		struct peer *p, *pb;
3913 
3914 		/* In the case the peers frontend was not initialized by a
3915 		 stick-table used in the configuration, set its bind_proc
3916 		 by default to the first process. */
3917 		while (curpeers) {
3918 			if (curpeers->peers_fe) {
3919 				if (curpeers->peers_fe->bind_proc == 0)
3920 					curpeers->peers_fe->bind_proc = 1;
3921 			}
3922 			curpeers = curpeers->next;
3923 		}
3924 
3925 		curpeers = cfg_peers;
3926 		/* Remove all peers sections which don't have a valid listener,
3927 		 * which are not used by any table, or which are bound to more
3928 		 * than one process.
3929 		 */
3930 		last = &cfg_peers;
3931 		while (*last) {
3932 			struct stktable *t;
3933 			curpeers = *last;
3934 
3935 			if (curpeers->state == PR_STSTOPPED) {
3936 				/* the "disabled" keyword was present */
3937 				if (curpeers->peers_fe)
3938 					stop_proxy(curpeers->peers_fe);
3939 				curpeers->peers_fe = NULL;
3940 			}
3941 			else if (!curpeers->peers_fe || !curpeers->peers_fe->id) {
3942 				ha_warning("Removing incomplete section 'peers %s' (no peer named '%s').\n",
3943 					   curpeers->id, localpeer);
3944 				if (curpeers->peers_fe)
3945 					stop_proxy(curpeers->peers_fe);
3946 				curpeers->peers_fe = NULL;
3947 			}
3948 			else if (atleast2(curpeers->peers_fe->bind_proc)) {
3949 				/* either it's totally stopped or too much used */
3950 				if (curpeers->peers_fe->bind_proc) {
3951 					ha_alert("Peers section '%s': peers referenced by sections "
3952 						 "running in different processes (%d different ones). "
3953 						 "Check global.nbproc and all tables' bind-process "
3954 						 "settings.\n", curpeers->id, my_popcountl(curpeers->peers_fe->bind_proc));
3955 					cfgerr++;
3956 				}
3957 				stop_proxy(curpeers->peers_fe);
3958 				curpeers->peers_fe = NULL;
3959 			}
3960 			else {
3961 				/* Initializes the transport layer of the server part of all the peers belonging to
3962 				 * <curpeers> section if required.
3963 				 * Note that ->srv is used by the local peer of a new process to connect to the local peer
3964 				 * of an old process.
3965 				 */
3966 				p = curpeers->remote;
3967 				while (p) {
3968 					if (p->srv) {
3969 						if (p->srv->use_ssl == 1 && xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv)
3970 							cfgerr += xprt_get(XPRT_SSL)->prepare_srv(p->srv);
3971 					}
3972 					p = p->next;
3973 				}
3974 				/* Configure the SSL bindings of the local peer if required. */
3975 				if (!LIST_ISEMPTY(&curpeers->peers_fe->conf.bind)) {
3976 					struct list *l;
3977 					struct bind_conf *bind_conf;
3978 
3979 					l = &curpeers->peers_fe->conf.bind;
3980 					bind_conf = LIST_ELEM(l->n, typeof(bind_conf), by_fe);
3981 					if (bind_conf->xprt->prepare_bind_conf &&
3982 						bind_conf->xprt->prepare_bind_conf(bind_conf) < 0)
3983 						cfgerr++;
3984 				}
3985 				if (!peers_init_sync(curpeers) || !peers_alloc_dcache(curpeers)) {
3986 					ha_alert("Peers section '%s': out of memory, giving up on peers.\n",
3987 						 curpeers->id);
3988 					cfgerr++;
3989 					break;
3990 				}
3991 				last = &curpeers->next;
3992 				continue;
3993 			}
3994 
3995 			/* clean what has been detected above */
3996 			p = curpeers->remote;
3997 			while (p) {
3998 				pb = p->next;
3999 				free(p->id);
4000 				free(p);
4001 				p = pb;
4002 			}
4003 
4004 			/* Destroy and unlink this curpeers section.
4005 			 * Note: curpeers is backed up into *last.
4006 			 */
4007 			free(curpeers->id);
4008 			curpeers = curpeers->next;
4009 			/* Reset any refereance to this peers section in the list of stick-tables */
4010 			for (t = stktables_list; t; t = t->next) {
4011 				if (t->peers.p && t->peers.p == *last)
4012 					t->peers.p = NULL;
4013 			}
4014 			free(*last);
4015 			*last = curpeers;
4016 		}
4017 	}
4018 
4019 	for (t = stktables_list; t; t = t->next) {
4020 		if (t->proxy)
4021 			continue;
4022 		if (!stktable_init(t)) {
4023 			ha_alert("Proxy '%s': failed to initialize stick-table.\n", t->id);
4024 			cfgerr++;
4025 		}
4026 	}
4027 
4028 	/* initialize stick-tables on backend capable proxies. This must not
4029 	 * be done earlier because the data size may be discovered while parsing
4030 	 * other proxies.
4031 	 */
4032 	for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
4033 		if (curproxy->state == PR_STSTOPPED || !curproxy->table)
4034 			continue;
4035 
4036 		if (!stktable_init(curproxy->table)) {
4037 			ha_alert("Proxy '%s': failed to initialize stick-table.\n", curproxy->id);
4038 			cfgerr++;
4039 		}
4040 	}
4041 
4042 	if (mailers) {
4043 		struct mailers *curmailers = mailers, **last;
4044 		struct mailer *m, *mb;
4045 
4046 		/* Remove all mailers sections which don't have a valid listener.
4047 		 * This can happen when a mailers section is never referenced.
4048 		 */
4049 		last = &mailers;
4050 		while (*last) {
4051 			curmailers = *last;
4052 			if (curmailers->users) {
4053 				last = &curmailers->next;
4054 				continue;
4055 			}
4056 
4057 			ha_warning("Removing incomplete section 'mailers %s'.\n",
4058 				   curmailers->id);
4059 
4060 			m = curmailers->mailer_list;
4061 			while (m) {
4062 				mb = m->next;
4063 				free(m->id);
4064 				free(m);
4065 				m = mb;
4066 			}
4067 
4068 			/* Destroy and unlink this curmailers section.
4069 			 * Note: curmailers is backed up into *last.
4070 			 */
4071 			free(curmailers->id);
4072 			curmailers = curmailers->next;
4073 			free(*last);
4074 			*last = curmailers;
4075 		}
4076 	}
4077 
4078 	/* Update server_state_file_name to backend name if backend is supposed to use
4079 	 * a server-state file locally defined and none has been provided */
4080 	for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
4081 		if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_LOCAL &&
4082 		    curproxy->server_state_file_name == NULL)
4083 			curproxy->server_state_file_name = strdup(curproxy->id);
4084 	}
4085 
4086 	pool_head_hdr_idx = create_pool("hdr_idx",
4087 				    global.tune.max_http_hdr * sizeof(struct hdr_idx_elem),
4088 				    MEM_F_SHARED);
4089 
4090 	list_for_each_entry(curr_resolvers, &dns_resolvers, list) {
4091 		if (LIST_ISEMPTY(&curr_resolvers->nameservers)) {
4092 			ha_warning("config : resolvers '%s' [%s:%d] has no nameservers configured!\n",
4093 				   curr_resolvers->id, curr_resolvers->conf.file,
4094 				   curr_resolvers->conf.line);
4095 			err_code |= ERR_WARN;
4096 		}
4097 	}
4098 
4099 	list_for_each_entry(postparser, &postparsers, list) {
4100 		if (postparser->func)
4101 			cfgerr += postparser->func();
4102 	}
4103 
4104 	if (cfgerr > 0)
4105 		err_code |= ERR_ALERT | ERR_FATAL;
4106  out:
4107 	return err_code;
4108 }
4109 
4110 /*
4111  * Registers the CFG keyword list <kwl> as a list of valid keywords for next
4112  * parsing sessions.
4113  */
cfg_register_keywords(struct cfg_kw_list * kwl)4114 void cfg_register_keywords(struct cfg_kw_list *kwl)
4115 {
4116 	LIST_ADDQ(&cfg_keywords.list, &kwl->list);
4117 }
4118 
4119 /*
4120  * Unregisters the CFG keyword list <kwl> from the list of valid keywords.
4121  */
cfg_unregister_keywords(struct cfg_kw_list * kwl)4122 void cfg_unregister_keywords(struct cfg_kw_list *kwl)
4123 {
4124 	LIST_DEL(&kwl->list);
4125 	LIST_INIT(&kwl->list);
4126 }
4127 
4128 /* this function register new section in the haproxy configuration file.
4129  * <section_name> is the name of this new section and <section_parser>
4130  * is the called parser. If two section declaration have the same name,
4131  * only the first declared is used.
4132  */
cfg_register_section(char * section_name,int (* section_parser)(const char *,int,char **,int),int (* post_section_parser)())4133 int cfg_register_section(char *section_name,
4134                          int (*section_parser)(const char *, int, char **, int),
4135                          int (*post_section_parser)())
4136 {
4137 	struct cfg_section *cs;
4138 
4139 	list_for_each_entry(cs, &sections, list) {
4140 		if (strcmp(cs->section_name, section_name) == 0) {
4141 			ha_alert("register section '%s': already registered.\n", section_name);
4142 			return 0;
4143 		}
4144 	}
4145 
4146 	cs = calloc(1, sizeof(*cs));
4147 	if (!cs) {
4148 		ha_alert("register section '%s': out of memory.\n", section_name);
4149 		return 0;
4150 	}
4151 
4152 	cs->section_name = section_name;
4153 	cs->section_parser = section_parser;
4154 	cs->post_section_parser = post_section_parser;
4155 
4156 	LIST_ADDQ(&sections, &cs->list);
4157 
4158 	return 1;
4159 }
4160 
4161 /* this function register a new function which will be called once the haproxy
4162  * configuration file has been parsed. It's useful to check dependencies
4163  * between sections or to resolve items once everything is parsed.
4164  */
cfg_register_postparser(char * name,int (* func)())4165 int cfg_register_postparser(char *name, int (*func)())
4166 {
4167 	struct cfg_postparser *cp;
4168 
4169 	cp = calloc(1, sizeof(*cp));
4170 	if (!cp) {
4171 		ha_alert("register postparser '%s': out of memory.\n", name);
4172 		return 0;
4173 	}
4174 	cp->name = name;
4175 	cp->func = func;
4176 
4177 	LIST_ADDQ(&postparsers, &cp->list);
4178 
4179 	return 1;
4180 }
4181 
4182 /*
4183  * free all config section entries
4184  */
cfg_unregister_sections(void)4185 void cfg_unregister_sections(void)
4186 {
4187 	struct cfg_section *cs, *ics;
4188 
4189 	list_for_each_entry_safe(cs, ics, &sections, list) {
4190 		LIST_DEL(&cs->list);
4191 		free(cs);
4192 	}
4193 }
4194 
cfg_backup_sections(struct list * backup_sections)4195 void cfg_backup_sections(struct list *backup_sections)
4196 {
4197 	struct cfg_section *cs, *ics;
4198 
4199 	list_for_each_entry_safe(cs, ics, &sections, list) {
4200 		LIST_DEL(&cs->list);
4201 		LIST_ADDQ(backup_sections, &cs->list);
4202 	}
4203 }
4204 
cfg_restore_sections(struct list * backup_sections)4205 void cfg_restore_sections(struct list *backup_sections)
4206 {
4207 	struct cfg_section *cs, *ics;
4208 
4209 	list_for_each_entry_safe(cs, ics, backup_sections, list) {
4210 		LIST_DEL(&cs->list);
4211 		LIST_ADDQ(&sections, &cs->list);
4212 	}
4213 }
4214 
4215 /* these are the config sections handled by default */
4216 REGISTER_CONFIG_SECTION("listen",         cfg_parse_listen,    NULL);
4217 REGISTER_CONFIG_SECTION("frontend",       cfg_parse_listen,    NULL);
4218 REGISTER_CONFIG_SECTION("backend",        cfg_parse_listen,    NULL);
4219 REGISTER_CONFIG_SECTION("defaults",       cfg_parse_listen,    NULL);
4220 REGISTER_CONFIG_SECTION("global",         cfg_parse_global,    NULL);
4221 REGISTER_CONFIG_SECTION("userlist",       cfg_parse_users,     NULL);
4222 REGISTER_CONFIG_SECTION("peers",          cfg_parse_peers,     NULL);
4223 REGISTER_CONFIG_SECTION("mailers",        cfg_parse_mailers,   NULL);
4224 REGISTER_CONFIG_SECTION("namespace_list", cfg_parse_netns,     NULL);
4225 REGISTER_CONFIG_SECTION("resolvers",      cfg_parse_resolvers, NULL);
4226 
4227 /*
4228  * Local variables:
4229  *  c-indent-level: 8
4230  *  c-basic-offset: 8
4231  * End:
4232  */
4233