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 | * |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 5 | * Copyright (C) 2000-2011 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 2bbba41 | 2010-01-28 16:48:33 +0100 | [diff] [blame] | 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> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 27 | #include <proto/sample.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * FIXME: we need destructor functions too ! |
| 31 | */ |
| 32 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 33 | /* 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 | */ |
| 36 | static 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. */ |
| 42 | static inline int acl_pass(int res) |
| 43 | { |
| 44 | return (res >> 1); |
| 45 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 46 | |
| 47 | /* Return a pointer to the ACL <name> within the list starting at <head>, or |
| 48 | * NULL if not found. |
| 49 | */ |
| 50 | struct 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 | */ |
| 56 | struct 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 Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 62 | struct acl_expr *parse_acl_expr(const char **args, char **err); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 63 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 64 | /* Purge everything in the acl <acl>, then return <acl>. */ |
| 65 | struct acl *prune_acl(struct acl *acl); |
| 66 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 67 | /* 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 Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 73 | struct acl *parse_acl(const char **args, struct list *known_acl, char **err); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 74 | |
| 75 | /* Purge everything in the acl_cond <cond>, then return <cond>. */ |
| 76 | struct 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 Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 82 | struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol, char **err); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 83 | |
Willy Tarreau | 2bbba41 | 2010-01-28 16:48:33 +0100 | [diff] [blame] | 84 | /* 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 |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 88 | * ACL requirements to the proxy's acl_requires. If <err> is not NULL, it will |
| 89 | * be set to an error message upon errors, that the caller will have to free. |
Willy Tarreau | 2bbba41 | 2010-01-28 16:48:33 +0100 | [diff] [blame] | 90 | */ |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 91 | struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args, char **err); |
Willy Tarreau | 2bbba41 | 2010-01-28 16:48:33 +0100 | [diff] [blame] | 92 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 93 | /* 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 Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 97 | */ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 98 | int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, unsigned int opt); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 99 | |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 100 | /* Reports a pointer to the first ACL used in condition <cond> which requires |
| 101 | * at least one of the USE_FLAGS in <require>. Returns NULL if none matches. |
| 102 | */ |
Willy Tarreau | f1e98b8 | 2010-01-28 17:59:39 +0100 | [diff] [blame] | 103 | struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 104 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 105 | /* |
| 106 | * Find targets for userlist and groups in acl. Function returns the number |
| 107 | * of errors or OK if everything is fine. |
| 108 | */ |
| 109 | int acl_find_targets(struct proxy *p); |
| 110 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 111 | /* Return a pointer to the ACL <name> within the list starting at <head>, or |
| 112 | * NULL if not found. |
| 113 | */ |
| 114 | struct acl *find_acl_by_name(const char *name, struct list *head); |
| 115 | |
| 116 | /* |
| 117 | * Registers the ACL keyword list <kwl> as a list of valid keywords for next |
| 118 | * parsing sessions. |
| 119 | */ |
| 120 | void acl_register_keywords(struct acl_kw_list *kwl); |
| 121 | |
| 122 | /* |
| 123 | * Unregisters the ACL keyword list <kwl> from the list of valid keywords. |
| 124 | */ |
| 125 | void acl_unregister_keywords(struct acl_kw_list *kwl); |
| 126 | |
| 127 | |
| 128 | /* |
| 129 | * |
| 130 | * The following functions are general purpose ACL matching functions. |
| 131 | * |
| 132 | */ |
| 133 | |
| 134 | |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 135 | /* ignore the current line */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 136 | int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque, char **err); |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 137 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 138 | /* NB: For two strings to be identical, it is required that their lengths match */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 139 | int acl_match_str(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 140 | |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 141 | /* Checks that the length of the pattern in <test> is included between min and max */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 142 | int acl_match_len(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 143 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 144 | /* Checks that the integer in <test> is included between min and max */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 145 | int acl_match_int(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 146 | |
| 147 | /* Parse an integer. It is put both in min and max. */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 148 | int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque, char **err); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 149 | |
Willy Tarreau | 4a26d2f | 2008-07-15 16:05:33 +0200 | [diff] [blame] | 150 | /* Parse an version. It is put both in min and max. */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 151 | int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err); |
Willy Tarreau | 4a26d2f | 2008-07-15 16:05:33 +0200 | [diff] [blame] | 152 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 153 | /* Parse a range of integers delimited by either ':' or '-'. If only one |
| 154 | * integer is read, it is set as both min and max. |
| 155 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 156 | int acl_parse_range(const char **text, struct acl_pattern *pattern, int *opaque, char **err); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 157 | |
| 158 | /* Parse a string. It is allocated and duplicated. */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 159 | int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque, char **err); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 160 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 161 | /* Parse and concatenate strings into one. It is allocated and duplicated. */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 162 | int acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque, char **err); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 163 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 164 | /* Parse a regex. It is allocated. */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 165 | int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque, char **err); |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 166 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 167 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 168 | * The addr may either be an IPv4 address or a hostname. The mask |
| 169 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 170 | * otherwise 0. |
| 171 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 172 | int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque, char **err); |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 173 | |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 174 | /* always fake a data retrieval */ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 175 | int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 176 | const struct arg *args, struct sample *smp); |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 177 | |
| 178 | /* always return false */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 179 | int acl_match_nothing(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 180 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 181 | /* Checks that the pattern matches the end of the tested string. */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 182 | int acl_match_end(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 183 | |
| 184 | /* Checks that the pattern matches the beginning of the tested string. */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 185 | int acl_match_beg(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 186 | |
| 187 | /* Checks that the pattern is included inside the tested string. */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 188 | int acl_match_sub(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 189 | |
| 190 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 191 | * between slashes or at the beginning or end of the string. Slashes at the |
| 192 | * beginning or end of the pattern are ignored. |
| 193 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 194 | int acl_match_dir(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 195 | |
| 196 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 197 | * between dots or at the beginning or end of the string. Dots at the beginning |
| 198 | * or end of the pattern are ignored. |
| 199 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 200 | int acl_match_dom(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 201 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 202 | /* Check that the IPv4 address in <test> matches the IP/mask in pattern */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 203 | int acl_match_ip(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 204 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 205 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
| 206 | * and restores the previous character when leaving. |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 207 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 208 | int acl_match_reg(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 209 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 210 | #endif /* _PROTO_ACL_H */ |
| 211 | |
| 212 | /* |
| 213 | * Local variables: |
| 214 | * c-indent-level: 8 |
| 215 | * c-basic-offset: 8 |
| 216 | * End: |
| 217 | */ |