blob: 6859b3d1c4ad265e38cbddc66829c69b993ab3ae [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 Tarreaud4c33c82013-01-07 21:59:07 +01005 * Copyright (C) 2000-2013 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 Tarreaucd3b0942012-04-27 21:52:18 +020027#include <proto/sample.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 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +020062struct acl_expr *parse_acl_expr(const char **args, char **err, struct arg_list *al);
Willy Tarreaua84d3742007-05-07 00:36:48 +020063
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 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +020073struct acl *parse_acl(const char **args, struct list *known_acl, char **err, struct arg_list *al);
Willy Tarreaua84d3742007-05-07 00:36:48 +020074
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 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +020082struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol, char **err, struct arg_list *al);
Willy Tarreaua84d3742007-05-07 00:36:48 +020083
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
Willy Tarreau25320b22013-03-24 07:22:08 +010087 * the line number in the condition for better error reporting, and sets the
88 * HTTP initialization requirements in the proxy. If <err> is not NULL, it will
Willy Tarreaub7451bb2012-04-27 12:38:15 +020089 * be set to an error message upon errors, that the caller will have to free.
Willy Tarreau2bbba412010-01-28 16:48:33 +010090 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +020091struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args, char **err);
Willy Tarreau2bbba412010-01-28 16:48:33 +010092
Willy Tarreau11382812008-07-09 16:18:21 +020093/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
94 * ACL_PAT_PASS depending on the test results. This function only computes the
95 * condition, it does not apply the polarity required by IF/UNLESS, it's up to
96 * the caller to do this.
Willy Tarreaua84d3742007-05-07 00:36:48 +020097 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020098int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, unsigned int opt);
Willy Tarreaua84d3742007-05-07 00:36:48 +020099
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100100/* Returns a pointer to the first ACL conflicting with usage at place <where>
101 * which is one of the SMP_VAL_* bits indicating a check place, or NULL if
102 * no conflict is found. Only full conflicts are detected (ACL is not usable).
103 * Use the next function to check for useless keywords.
104 */
105const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where);
106
107/* Returns a pointer to the first ACL and its first keyword to conflict with
108 * usage at place <where> which is one of the SMP_VAL_* bits indicating a check
109 * place. Returns true if a conflict is found, with <acl> and <kw> set (if non
110 * null), or false if not conflict is found. The first useless keyword is
111 * returned.
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200112 */
Willy Tarreau93fddf12013-03-31 22:59:32 +0200113int acl_cond_kw_conflicts(const struct acl_cond *cond, unsigned int where, struct acl const **acl, char const **kw);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200114
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100115/*
116 * Find targets for userlist and groups in acl. Function returns the number
117 * of errors or OK if everything is fine.
118 */
119int acl_find_targets(struct proxy *p);
120
Willy Tarreaua84d3742007-05-07 00:36:48 +0200121/* Return a pointer to the ACL <name> within the list starting at <head>, or
122 * NULL if not found.
123 */
124struct acl *find_acl_by_name(const char *name, struct list *head);
125
126/*
127 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
128 * parsing sessions.
129 */
130void acl_register_keywords(struct acl_kw_list *kwl);
131
132/*
133 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
134 */
135void acl_unregister_keywords(struct acl_kw_list *kwl);
136
Willy Tarreau8ed669b2013-01-11 15:49:37 +0100137/* initializes ACLs by resolving the sample fetch names they rely upon.
138 * Returns 0 on success, otherwise an error.
139 */
140int init_acl();
141
Willy Tarreaua84d3742007-05-07 00:36:48 +0200142
143/*
144 *
145 * The following functions are general purpose ACL matching functions.
146 *
147 */
148
149
Willy Tarreau58393e12008-07-20 10:39:22 +0200150/* ignore the current line */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200151int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
Willy Tarreau58393e12008-07-20 10:39:22 +0200152
Willy Tarreaua84d3742007-05-07 00:36:48 +0200153/* NB: For two strings to be identical, it is required that their lengths match */
Willy Tarreau37406352012-04-23 16:16:37 +0200154int acl_match_str(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200155
Emeric Brun07ca4962012-10-17 13:38:19 +0200156/* NB: For two binary buffers to be identical, it is required that their lengths match */
157int acl_match_bin(struct sample *smp, struct acl_pattern *pattern);
158
Willy Tarreau0e698542011-09-16 08:32:32 +0200159/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200160int acl_match_len(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreau0e698542011-09-16 08:32:32 +0200161
Willy Tarreaua84d3742007-05-07 00:36:48 +0200162/* Checks that the integer in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200163int acl_match_int(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200164
165/* Parse an integer. It is put both in min and max. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200166int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200167
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200168/* Parse an version. It is put both in min and max. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200169int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200170
Willy Tarreaua84d3742007-05-07 00:36:48 +0200171/* Parse a range of integers delimited by either ':' or '-'. If only one
172 * integer is read, it is set as both min and max.
173 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200174int acl_parse_range(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200175
176/* Parse a string. It is allocated and duplicated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200177int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200178
Emeric Brun07ca4962012-10-17 13:38:19 +0200179/* Parse a hexa binary definition. It is allocated and duplicated. */
180int acl_parse_bin(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
181
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100182/* Parse and concatenate strings into one. It is allocated and duplicated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200183int acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100184
Willy Tarreauf3d25982007-05-08 22:45:09 +0200185/* Parse a regex. It is allocated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200186int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200187
Willy Tarreaua67fad92007-05-08 19:50:09 +0200188/* Parse an IP address and an optional mask in the form addr[/mask].
189 * The addr may either be an IPv4 address or a hostname. The mask
190 * may either be a dotted mask or a number of bits. Returns 1 if OK,
191 * otherwise 0.
192 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200193int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
Willy Tarreaua67fad92007-05-08 19:50:09 +0200194
Willy Tarreau58393e12008-07-20 10:39:22 +0200195/* always return false */
Willy Tarreau37406352012-04-23 16:16:37 +0200196int acl_match_nothing(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreau58393e12008-07-20 10:39:22 +0200197
Willy Tarreaua84d3742007-05-07 00:36:48 +0200198/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200199int acl_match_end(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200200
201/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200202int acl_match_beg(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200203
204/* Checks that the pattern is included inside the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200205int acl_match_sub(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200206
207/* Checks that the pattern is included inside the tested string, but enclosed
208 * between slashes or at the beginning or end of the string. Slashes at the
209 * beginning or end of the pattern are ignored.
210 */
Willy Tarreau37406352012-04-23 16:16:37 +0200211int acl_match_dir(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200212
213/* Checks that the pattern is included inside the tested string, but enclosed
214 * between dots or at the beginning or end of the string. Dots at the beginning
215 * or end of the pattern are ignored.
216 */
Willy Tarreau37406352012-04-23 16:16:37 +0200217int acl_match_dom(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200218
Willy Tarreaua67fad92007-05-08 19:50:09 +0200219/* Check that the IPv4 address in <test> matches the IP/mask in pattern */
Willy Tarreau37406352012-04-23 16:16:37 +0200220int acl_match_ip(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreaua67fad92007-05-08 19:50:09 +0200221
Willy Tarreau21e5b0e2012-04-23 19:25:44 +0200222/* Executes a regex. It temporarily changes the data to add a trailing zero,
223 * and restores the previous character when leaving.
Willy Tarreauf3d25982007-05-08 22:45:09 +0200224 */
Willy Tarreau37406352012-04-23 16:16:37 +0200225int acl_match_reg(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200226
Willy Tarreaua84d3742007-05-07 00:36:48 +0200227#endif /* _PROTO_ACL_H */
228
229/*
230 * Local variables:
231 * c-indent-level: 8
232 * c-basic-offset: 8
233 * End:
234 */