summaryrefslogtreecommitdiffstats
path: root/src/cfgdiag.c
blob: f8e4a9e082c1a75d3f68140c2c30194c4147a154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdarg.h>
#include <stdlib.h>

#include <import/ebistree.h>

#include <haproxy/cfgdiag.h>
#include <haproxy/log.h>
#include <haproxy/proxy.h>
#include <haproxy/server.h>

/* Use this function to emit diagnostic.
 * This can be used as a shortcut to set value pointed by <ret> to 1 at the
 * same time.
 */
static inline void diag_warning(int *ret, char *fmt, ...)
{
	va_list argp;

	va_start(argp, fmt);
	*ret = 1;
	_ha_vdiag_warning(fmt, argp);
	va_end(argp);
}

/* Use this for dynamic allocation in diagnostics.
 * In case of allocation failure, this will immediately terminates haproxy.
 */
static inline void *diag_alloc(size_t size)
{
	void *out = NULL;

	if (!(out = malloc(size))) {
		fprintf(stderr, "out of memory\n");
		exit(1);
	}

	return out;
}

/* Checks that two servers from the same backend does not share the same cookie
 * value. Backup servers are not taken into account as it can be quite common to
 * share cookie values in this case.
 */
static void check_server_cookies(int *ret)
{
	struct cookie_entry {
		struct ebpt_node node;
	};

	struct proxy  *px;
	struct server *srv;

	struct eb_root cookies_tree = EB_ROOT_UNIQUE;
	struct ebpt_node *cookie_node;
	struct cookie_entry *cookie_entry;
	struct ebpt_node *node;

	for (px = proxies_list; px; px = px->next) {
		for (srv = px->srv; srv; srv = srv->next) {
			/* do not take into account backup servers */
			if (!srv->cookie || (srv->flags & SRV_F_BACKUP))
				continue;

			cookie_node = ebis_lookup(&cookies_tree, srv->cookie);
			if (cookie_node) {
				diag_warning(ret, "parsing [%s:%d] : 'server %s' : same cookie value is set for a previous non-backup server in the same backend, it may break connection persistence\n",
				             srv->conf.file, srv->conf.line, srv->id);
				continue;
			}

			cookie_entry = diag_alloc(sizeof(*cookie_entry));
			cookie_entry->node.key = srv->cookie;
			ebis_insert(&cookies_tree, &cookie_entry->node);
		}

		/* clear the tree and free its entries */
		while ((node = ebpt_first(&cookies_tree))) {
			cookie_entry = ebpt_entry(node, struct cookie_entry, node);
			eb_delete(&node->node);
			free(cookie_entry);
		}
	}
}

/* Placeholder to execute various diagnostic checks after the configuration file
 * has been fully parsed. It will output a warning for each diagnostic found.
 *
 * Returns 0 if no diagnostic message has been found else 1.
 */
int cfg_run_diagnostics()
{
	int ret = 0;

	check_server_cookies(&ret);

	return ret;
}