Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 2bbba41 | 2010-01-28 16:48:33 +0100 | [diff] [blame] | 2 | * include/proto/acl.h |
| 3 | * This file provides interface definitions for ACL manipulation. |
| 4 | * |
| 5 | * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 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 Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _PROTO_ACL_H |
| 23 | #define _PROTO_ACL_H |
| 24 | |
| 25 | #include <common/config.h> |
| 26 | #include <types/acl.h> |
| 27 | |
| 28 | /* |
| 29 | * FIXME: we need destructor functions too ! |
| 30 | */ |
| 31 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 32 | /* Negate an acl result. This turns (ACL_PAT_FAIL, ACL_PAT_MISS, ACL_PAT_PASS) |
| 33 | * into (ACL_PAT_PASS, ACL_PAT_MISS, ACL_PAT_FAIL). |
| 34 | */ |
| 35 | static inline int acl_neg(int res) |
| 36 | { |
| 37 | return (3 >> res); |
| 38 | } |
| 39 | |
| 40 | /* Convert an acl result to a boolean. Only ACL_PAT_PASS returns 1. */ |
| 41 | static inline int acl_pass(int res) |
| 42 | { |
| 43 | return (res >> 1); |
| 44 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 45 | |
| 46 | /* Return a pointer to the ACL <name> within the list starting at <head>, or |
| 47 | * NULL if not found. |
| 48 | */ |
| 49 | struct acl *find_acl_by_name(const char *name, struct list *head); |
| 50 | |
| 51 | /* Return a pointer to the ACL keyword <kw> within the list starting at <head>, |
| 52 | * or NULL if not found. Note that if <kw> contains an opening parenthesis, |
| 53 | * only the left part of it is checked. |
| 54 | */ |
| 55 | struct acl_keyword *find_acl_kw(const char *kw); |
| 56 | |
| 57 | /* Parse an ACL expression starting at <args>[0], and return it. |
| 58 | * Right now, the only accepted syntax is : |
| 59 | * <subject> [<value>...] |
| 60 | */ |
| 61 | struct acl_expr *parse_acl_expr(const char **args); |
| 62 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 63 | /* Purge everything in the acl <acl>, then return <acl>. */ |
| 64 | struct acl *prune_acl(struct acl *acl); |
| 65 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 66 | /* Parse an ACL with the name starting at <args>[0], and with a list of already |
| 67 | * known ACLs in <acl>. If the ACL was not in the list, it will be added. |
| 68 | * A pointer to that ACL is returned. |
| 69 | * |
| 70 | * args syntax: <aclname> <acl_expr> |
| 71 | */ |
| 72 | struct acl *parse_acl(const char **args, struct list *known_acl); |
| 73 | |
| 74 | /* Purge everything in the acl_cond <cond>, then return <cond>. */ |
| 75 | struct acl_cond *prune_acl_cond(struct acl_cond *cond); |
| 76 | |
| 77 | /* Parse an ACL condition starting at <args>[0], relying on a list of already |
| 78 | * known ACLs passed in <known_acl>. The new condition is returned (or NULL in |
| 79 | * case of low memory). Supports multiple conditions separated by "or". |
| 80 | */ |
| 81 | struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol); |
| 82 | |
Willy Tarreau | 2bbba41 | 2010-01-28 16:48:33 +0100 | [diff] [blame] | 83 | /* Builds an ACL condition starting at the if/unless keyword. The complete |
| 84 | * condition is returned. NULL is returned in case of error or if the first |
| 85 | * word is neither "if" nor "unless". It automatically sets the file name and |
| 86 | * the line number in the condition for better error reporting, and adds the |
| 87 | * ACL requirements to the proxy's acl_requires. |
| 88 | */ |
| 89 | struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args); |
| 90 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 91 | /* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or |
| 92 | * ACL_PAT_PASS depending on the test results. This function only computes the |
| 93 | * condition, it does not apply the polarity required by IF/UNLESS, it's up to |
| 94 | * the caller to do this. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 95 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 96 | int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 97 | |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 98 | /* Reports a pointer to the first ACL used in condition <cond> which requires |
| 99 | * at least one of the USE_FLAGS in <require>. Returns NULL if none matches. |
| 100 | */ |
| 101 | struct acl *cond_find_require(struct acl_cond *cond, unsigned int require); |
| 102 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 103 | /* Return a pointer to the ACL <name> within the list starting at <head>, or |
| 104 | * NULL if not found. |
| 105 | */ |
| 106 | struct acl *find_acl_by_name(const char *name, struct list *head); |
| 107 | |
| 108 | /* |
| 109 | * Registers the ACL keyword list <kwl> as a list of valid keywords for next |
| 110 | * parsing sessions. |
| 111 | */ |
| 112 | void acl_register_keywords(struct acl_kw_list *kwl); |
| 113 | |
| 114 | /* |
| 115 | * Unregisters the ACL keyword list <kwl> from the list of valid keywords. |
| 116 | */ |
| 117 | void acl_unregister_keywords(struct acl_kw_list *kwl); |
| 118 | |
| 119 | |
| 120 | /* |
| 121 | * |
| 122 | * The following functions are general purpose ACL matching functions. |
| 123 | * |
| 124 | */ |
| 125 | |
| 126 | |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 127 | /* ignore the current line */ |
| 128 | int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque); |
| 129 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 130 | /* NB: For two strings to be identical, it is required that their lengths match */ |
| 131 | int acl_match_str(struct acl_test *test, struct acl_pattern *pattern); |
| 132 | |
| 133 | /* Checks that the integer in <test> is included between min and max */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 134 | int acl_match_int(struct acl_test *test, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 135 | |
| 136 | /* Parse an integer. It is put both in min and max. */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 137 | int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 138 | |
Willy Tarreau | 4a26d2f | 2008-07-15 16:05:33 +0200 | [diff] [blame] | 139 | /* Parse an version. It is put both in min and max. */ |
| 140 | int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque); |
| 141 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 142 | /* Parse a range of integers delimited by either ':' or '-'. If only one |
| 143 | * integer is read, it is set as both min and max. |
| 144 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 145 | int acl_parse_range(const char **text, struct acl_pattern *pattern, int *opaque); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 146 | |
| 147 | /* Parse a string. It is allocated and duplicated. */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 148 | int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 149 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 150 | /* Parse a regex. It is allocated. */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 151 | int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque); |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 152 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 153 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 154 | * The addr may either be an IPv4 address or a hostname. The mask |
| 155 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 156 | * otherwise 0. |
| 157 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 158 | int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque); |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 159 | |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 160 | /* always fake a data retrieval */ |
| 161 | int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir, |
| 162 | struct acl_expr *expr, struct acl_test *test); |
| 163 | |
| 164 | /* always return false */ |
| 165 | int acl_match_nothing(struct acl_test *test, struct acl_pattern *pattern); |
| 166 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 167 | /* Checks that the pattern matches the end of the tested string. */ |
| 168 | int acl_match_end(struct acl_test *test, struct acl_pattern *pattern); |
| 169 | |
| 170 | /* Checks that the pattern matches the beginning of the tested string. */ |
| 171 | int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern); |
| 172 | |
| 173 | /* Checks that the pattern is included inside the tested string. */ |
| 174 | int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern); |
| 175 | |
| 176 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 177 | * between slashes or at the beginning or end of the string. Slashes at the |
| 178 | * beginning or end of the pattern are ignored. |
| 179 | */ |
| 180 | int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern); |
| 181 | |
| 182 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 183 | * between dots or at the beginning or end of the string. Dots at the beginning |
| 184 | * or end of the pattern are ignored. |
| 185 | */ |
| 186 | int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern); |
| 187 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 188 | /* Check that the IPv4 address in <test> matches the IP/mask in pattern */ |
| 189 | int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern); |
| 190 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 191 | /* Executes a regex. It needs to change the data. If it is marked READ_ONLY |
| 192 | * then it will be allocated and duplicated in place so that others may use |
| 193 | * it later on. Note that this is embarrassing because we always try to avoid |
| 194 | * allocating memory at run time. |
| 195 | */ |
| 196 | int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern); |
| 197 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 198 | #endif /* _PROTO_ACL_H */ |
| 199 | |
| 200 | /* |
| 201 | * Local variables: |
| 202 | * c-indent-level: 8 |
| 203 | * c-basic-offset: 8 |
| 204 | * End: |
| 205 | */ |