blob: 95407dd44aa0c5e5f1ff7b161fbf34ddb7d516c8 [file] [log] [blame]
Willy Tarreaua84d3742007-05-07 00:36:48 +02001/*
Willy Tarreau2bbba412010-01-28 16:48:33 +01002 * include/proto/acl.h
3 * This file provides interface definitions for ACL manipulation.
4 *
Willy Tarreau0e698542011-09-16 08:32:32 +02005 * Copyright (C) 2000-2011 Willy Tarreau - w@1wt.eu
Willy Tarreau2bbba412010-01-28 16:48:33 +01006 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy Tarreaua84d3742007-05-07 00:36:48 +020021
22#ifndef _PROTO_ACL_H
23#define _PROTO_ACL_H
24
25#include <common/config.h>
26#include <types/acl.h>
Willy Tarreau91845842011-12-16 15:47:06 +010027#include <proto/pattern.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020028
29/*
30 * FIXME: we need destructor functions too !
31 */
32
Willy Tarreau11382812008-07-09 16:18:21 +020033/* Negate an acl result. This turns (ACL_PAT_FAIL, ACL_PAT_MISS, ACL_PAT_PASS)
34 * into (ACL_PAT_PASS, ACL_PAT_MISS, ACL_PAT_FAIL).
35 */
36static inline int acl_neg(int res)
37{
38 return (3 >> res);
39}
40
41/* Convert an acl result to a boolean. Only ACL_PAT_PASS returns 1. */
42static inline int acl_pass(int res)
43{
44 return (res >> 1);
45}
Willy Tarreaua84d3742007-05-07 00:36:48 +020046
47/* Return a pointer to the ACL <name> within the list starting at <head>, or
48 * NULL if not found.
49 */
50struct acl *find_acl_by_name(const char *name, struct list *head);
51
52/* Return a pointer to the ACL keyword <kw> within the list starting at <head>,
53 * or NULL if not found. Note that if <kw> contains an opening parenthesis,
54 * only the left part of it is checked.
55 */
56struct acl_keyword *find_acl_kw(const char *kw);
57
58/* Parse an ACL expression starting at <args>[0], and return it.
59 * Right now, the only accepted syntax is :
60 * <subject> [<value>...]
61 */
62struct acl_expr *parse_acl_expr(const char **args);
63
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020064/* Purge everything in the acl <acl>, then return <acl>. */
65struct acl *prune_acl(struct acl *acl);
66
Willy Tarreaua84d3742007-05-07 00:36:48 +020067/* Parse an ACL with the name starting at <args>[0], and with a list of already
68 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
69 * A pointer to that ACL is returned.
70 *
71 * args syntax: <aclname> <acl_expr>
72 */
73struct acl *parse_acl(const char **args, struct list *known_acl);
74
75/* Purge everything in the acl_cond <cond>, then return <cond>. */
76struct acl_cond *prune_acl_cond(struct acl_cond *cond);
77
78/* Parse an ACL condition starting at <args>[0], relying on a list of already
79 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
80 * case of low memory). Supports multiple conditions separated by "or".
81 */
82struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol);
83
Willy Tarreau2bbba412010-01-28 16:48:33 +010084/* Builds an ACL condition starting at the if/unless keyword. The complete
85 * condition is returned. NULL is returned in case of error or if the first
86 * word is neither "if" nor "unless". It automatically sets the file name and
87 * the line number in the condition for better error reporting, and adds the
88 * ACL requirements to the proxy's acl_requires.
89 */
90struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args);
91
Willy Tarreau11382812008-07-09 16:18:21 +020092/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
93 * ACL_PAT_PASS depending on the test results. This function only computes the
94 * condition, it does not apply the polarity required by IF/UNLESS, it's up to
95 * the caller to do this.
Willy Tarreaua84d3742007-05-07 00:36:48 +020096 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020097int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir);
Willy Tarreaua84d3742007-05-07 00:36:48 +020098
Willy Tarreaudd64f8d2008-07-27 22:02:32 +020099/* Reports a pointer to the first ACL used in condition <cond> which requires
100 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
101 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +0100102struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200103
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100104/*
105 * Find targets for userlist and groups in acl. Function returns the number
106 * of errors or OK if everything is fine.
107 */
108int acl_find_targets(struct proxy *p);
109
Willy Tarreaua84d3742007-05-07 00:36:48 +0200110/* Return a pointer to the ACL <name> within the list starting at <head>, or
111 * NULL if not found.
112 */
113struct acl *find_acl_by_name(const char *name, struct list *head);
114
115/*
116 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
117 * parsing sessions.
118 */
119void acl_register_keywords(struct acl_kw_list *kwl);
120
121/*
122 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
123 */
124void acl_unregister_keywords(struct acl_kw_list *kwl);
125
126
127/*
128 *
129 * The following functions are general purpose ACL matching functions.
130 *
131 */
132
133
Willy Tarreau58393e12008-07-20 10:39:22 +0200134/* ignore the current line */
135int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque);
136
Willy Tarreaua84d3742007-05-07 00:36:48 +0200137/* NB: For two strings to be identical, it is required that their lengths match */
138int acl_match_str(struct acl_test *test, struct acl_pattern *pattern);
139
Willy Tarreau0e698542011-09-16 08:32:32 +0200140/* Checks that the length of the pattern in <test> is included between min and max */
141int acl_match_len(struct acl_test *test, struct acl_pattern *pattern);
142
Willy Tarreaua84d3742007-05-07 00:36:48 +0200143/* Checks that the integer in <test> is included between min and max */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200144int acl_match_int(struct acl_test *test, struct acl_pattern *pattern);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200145
146/* Parse an integer. It is put both in min and max. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200147int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200148
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200149/* Parse an version. It is put both in min and max. */
150int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque);
151
Willy Tarreaua84d3742007-05-07 00:36:48 +0200152/* Parse a range of integers delimited by either ':' or '-'. If only one
153 * integer is read, it is set as both min and max.
154 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200155int acl_parse_range(const char **text, struct acl_pattern *pattern, int *opaque);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200156
157/* Parse a string. It is allocated and duplicated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200158int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200159
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100160/* Parse and concatenate strings into one. It is allocated and duplicated. */
161int acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque);
162
Willy Tarreauf3d25982007-05-08 22:45:09 +0200163/* Parse a regex. It is allocated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200164int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200165
Willy Tarreaua67fad92007-05-08 19:50:09 +0200166/* Parse an IP address and an optional mask in the form addr[/mask].
167 * The addr may either be an IPv4 address or a hostname. The mask
168 * may either be a dotted mask or a number of bits. Returns 1 if OK,
169 * otherwise 0.
170 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200171int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque);
Willy Tarreaua67fad92007-05-08 19:50:09 +0200172
Willy Tarreau58393e12008-07-20 10:39:22 +0200173/* always fake a data retrieval */
174int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir,
175 struct acl_expr *expr, struct acl_test *test);
176
177/* always return false */
178int acl_match_nothing(struct acl_test *test, struct acl_pattern *pattern);
179
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200180/* Fetch the RDP cookie identified in the expression. */
181int acl_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
182 struct acl_expr *expr, struct acl_test *test);
183
Willy Tarreaua84d3742007-05-07 00:36:48 +0200184/* Checks that the pattern matches the end of the tested string. */
185int acl_match_end(struct acl_test *test, struct acl_pattern *pattern);
186
187/* Checks that the pattern matches the beginning of the tested string. */
188int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern);
189
190/* Checks that the pattern is included inside the tested string. */
191int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern);
192
193/* Checks that the pattern is included inside the tested string, but enclosed
194 * between slashes or at the beginning or end of the string. Slashes at the
195 * beginning or end of the pattern are ignored.
196 */
197int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern);
198
199/* Checks that the pattern is included inside the tested string, but enclosed
200 * between dots or at the beginning or end of the string. Dots at the beginning
201 * or end of the pattern are ignored.
202 */
203int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern);
204
Willy Tarreaua67fad92007-05-08 19:50:09 +0200205/* Check that the IPv4 address in <test> matches the IP/mask in pattern */
206int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern);
207
Willy Tarreauf3d25982007-05-08 22:45:09 +0200208/* Executes a regex. It needs to change the data. If it is marked READ_ONLY
209 * then it will be allocated and duplicated in place so that others may use
210 * it later on. Note that this is embarrassing because we always try to avoid
211 * allocating memory at run time.
212 */
213int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern);
214
Willy Tarreaua84d3742007-05-07 00:36:48 +0200215#endif /* _PROTO_ACL_H */
216
217/*
218 * Local variables:
219 * c-indent-level: 8
220 * c-basic-offset: 8
221 * End:
222 */