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 | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 5 | * Copyright (C) 2000-2013 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 | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 62 | struct acl_expr *parse_acl_expr(const char **args, char **err, struct arg_list *al); |
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 | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 73 | struct acl *parse_acl(const char **args, struct list *known_acl, char **err, struct arg_list *al); |
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 | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 82 | struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol, char **err, struct arg_list *al); |
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 |
Willy Tarreau | 25320b2 | 2013-03-24 07:22:08 +0100 | [diff] [blame] | 87 | * 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 Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 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 | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 100 | /* 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 | */ |
| 105 | const 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 Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 112 | */ |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 113 | int acl_cond_kw_conflicts(const struct acl_cond *cond, unsigned int where, struct acl const **acl, char const **kw); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 114 | |
Thierry FOURNIER | 3a103c5 | 2013-11-22 17:33:27 +0100 | [diff] [blame] | 115 | /* parse the <text> with <expr> compliant parser. <pattern> is a context for |
| 116 | * the current parsed acl. It must initialized at NULL: |
| 117 | * |
| 118 | * struct acl_pattern *pattern = NULL |
| 119 | * acl_register_pattern(..., &pattern, ...); |
| 120 | * |
| 121 | * patflag are a combination of 'ACL_PAT_F_*' flags pattern compatible. see |
| 122 | * <types/acl.h>. |
| 123 | * |
| 124 | * The function returns 1 if the processing is ok, return -1 if the parser |
| 125 | * fails, with <err> message filled. It returns -2 in "out of memory" |
| 126 | * error case. |
| 127 | */ |
| 128 | int acl_register_pattern(struct acl_expr *expr, char *text, struct acl_pattern **pattern, int patflags, char **err); |
| 129 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 130 | /* |
| 131 | * Find targets for userlist and groups in acl. Function returns the number |
| 132 | * of errors or OK if everything is fine. |
| 133 | */ |
| 134 | int acl_find_targets(struct proxy *p); |
| 135 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 136 | /* Return a pointer to the ACL <name> within the list starting at <head>, or |
| 137 | * NULL if not found. |
| 138 | */ |
| 139 | struct acl *find_acl_by_name(const char *name, struct list *head); |
| 140 | |
Thierry FOURNIER | 29d47b8 | 2013-11-22 18:03:42 +0100 | [diff] [blame] | 141 | /* This function execute the match part of the acl. |
| 142 | * it return ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS |
| 143 | */ |
| 144 | inline int acl_exec_match(struct acl_expr *expr, struct sample *smp); |
| 145 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 146 | /* |
| 147 | * Registers the ACL keyword list <kwl> as a list of valid keywords for next |
| 148 | * parsing sessions. |
| 149 | */ |
| 150 | void acl_register_keywords(struct acl_kw_list *kwl); |
| 151 | |
| 152 | /* |
| 153 | * Unregisters the ACL keyword list <kwl> from the list of valid keywords. |
| 154 | */ |
| 155 | void acl_unregister_keywords(struct acl_kw_list *kwl); |
| 156 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 157 | /* initializes ACLs by resolving the sample fetch names they rely upon. |
| 158 | * Returns 0 on success, otherwise an error. |
| 159 | */ |
| 160 | int init_acl(); |
| 161 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * |
| 165 | * The following functions are general purpose ACL matching functions. |
| 166 | * |
| 167 | */ |
| 168 | |
| 169 | |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 170 | /* ignore the current line */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 171 | 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] | 172 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 173 | /* 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] | 174 | int acl_match_str(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 175 | |
Emeric Brun | 07ca496 | 2012-10-17 13:38:19 +0200 | [diff] [blame] | 176 | /* NB: For two binary buffers to be identical, it is required that their lengths match */ |
| 177 | int acl_match_bin(struct sample *smp, struct acl_pattern *pattern); |
| 178 | |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 179 | /* 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] | 180 | int acl_match_len(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 181 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 182 | /* Checks that the integer in <test> is included between min and max */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 183 | int acl_match_int(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 184 | |
| 185 | /* Parse an integer. It is put both in min and max. */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 186 | 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] | 187 | |
Willy Tarreau | 4a26d2f | 2008-07-15 16:05:33 +0200 | [diff] [blame] | 188 | /* Parse an version. It is put both in min and max. */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 189 | 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] | 190 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 191 | /* Parse a range of integers delimited by either ':' or '-'. If only one |
| 192 | * integer is read, it is set as both min and max. |
| 193 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 194 | 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] | 195 | |
| 196 | /* Parse a string. It is allocated and duplicated. */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 197 | 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] | 198 | |
Emeric Brun | 07ca496 | 2012-10-17 13:38:19 +0200 | [diff] [blame] | 199 | /* Parse a hexa binary definition. It is allocated and duplicated. */ |
| 200 | int acl_parse_bin(const char **text, struct acl_pattern *pattern, int *opaque, char **err); |
| 201 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 202 | /* Parse and concatenate strings into one. It is allocated and duplicated. */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 203 | 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] | 204 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 205 | /* Parse a regex. It is allocated. */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 206 | 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] | 207 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 208 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 209 | * The addr may either be an IPv4 address or a hostname. The mask |
| 210 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 211 | * otherwise 0. |
| 212 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 213 | 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] | 214 | |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 215 | /* always return false */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 216 | int acl_match_nothing(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 217 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 218 | /* Checks that the pattern matches the end of the tested string. */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 219 | int acl_match_end(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 220 | |
| 221 | /* Checks that the pattern matches the beginning of the tested string. */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 222 | int acl_match_beg(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 223 | |
| 224 | /* Checks that the pattern is included inside the tested string. */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 225 | int acl_match_sub(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 226 | |
| 227 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 228 | * between slashes or at the beginning or end of the string. Slashes at the |
| 229 | * beginning or end of the pattern are ignored. |
| 230 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 231 | int acl_match_dir(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 232 | |
| 233 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 234 | * between dots or at the beginning or end of the string. Dots at the beginning |
| 235 | * or end of the pattern are ignored. |
| 236 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 237 | int acl_match_dom(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 238 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 239 | /* 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] | 240 | int acl_match_ip(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 241 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 242 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
| 243 | * and restores the previous character when leaving. |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 244 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 245 | int acl_match_reg(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 246 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 247 | #endif /* _PROTO_ACL_H */ |
| 248 | |
| 249 | /* |
| 250 | * Local variables: |
| 251 | * c-indent-level: 8 |
| 252 | * c-basic-offset: 8 |
| 253 | * End: |
| 254 | */ |