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 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 25 | #include <haproxy/api.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 26 | #include <types/acl.h> |
| 27 | |
| 28 | /* |
| 29 | * FIXME: we need destructor functions too ! |
| 30 | */ |
| 31 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 32 | /* Negate an acl result. This turns (ACL_MATCH_FAIL, ACL_MATCH_MISS, |
| 33 | * ACL_MATCH_PASS) into (ACL_MATCH_PASS, ACL_MATCH_MISS, ACL_MATCH_FAIL). |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 34 | */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 35 | static inline enum acl_test_res acl_neg(enum acl_test_res res) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 36 | { |
| 37 | return (3 >> res); |
| 38 | } |
| 39 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 40 | /* Convert an acl result to a boolean. Only ACL_MATCH_PASS returns 1. */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 41 | static inline int acl_pass(enum acl_test_res res) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 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 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 61 | struct acl_expr *parse_acl_expr(const char **args, char **err, struct arg_list *al, const char *file, int line); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 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 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 72 | struct acl *parse_acl(const char **args, struct list *known_acl, char **err, struct arg_list *al, const char *file, int line); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 81 | struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 82 | enum acl_cond_pol pol, char **err, struct arg_list *al, |
| 83 | const char *file, int line); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 84 | |
Willy Tarreau | 2bbba41 | 2010-01-28 16:48:33 +0100 | [diff] [blame] | 85 | /* Builds an ACL condition starting at the if/unless keyword. The complete |
| 86 | * condition is returned. NULL is returned in case of error or if the first |
| 87 | * 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] | 88 | * the line number in the condition for better error reporting, and sets the |
| 89 | * 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] | 90 | * 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] | 91 | */ |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 92 | struct acl_cond *build_acl_cond(const char *file, int line, struct list *known_acl, |
| 93 | struct proxy *px, const char **args, char **err); |
Willy Tarreau | 2bbba41 | 2010-01-28 16:48:33 +0100 | [diff] [blame] | 94 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 95 | /* Execute condition <cond> and return either ACL_TEST_FAIL, ACL_TEST_MISS or |
| 96 | * ACL_TEST_PASS depending on the test results. ACL_TEST_MISS may only be |
| 97 | * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete |
| 98 | * data is being examined. The function automatically sets SMP_OPT_ITERATE. This |
| 99 | * function only computes the condition, it does not apply the polarity required |
| 100 | * by IF/UNLESS, it's up to the caller to do this. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 101 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 102 | enum acl_test_res acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 103 | |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 104 | /* Returns a pointer to the first ACL conflicting with usage at place <where> |
| 105 | * which is one of the SMP_VAL_* bits indicating a check place, or NULL if |
| 106 | * no conflict is found. Only full conflicts are detected (ACL is not usable). |
| 107 | * Use the next function to check for useless keywords. |
| 108 | */ |
| 109 | const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where); |
| 110 | |
| 111 | /* Returns a pointer to the first ACL and its first keyword to conflict with |
| 112 | * usage at place <where> which is one of the SMP_VAL_* bits indicating a check |
| 113 | * place. Returns true if a conflict is found, with <acl> and <kw> set (if non |
| 114 | * null), or false if not conflict is found. The first useless keyword is |
| 115 | * returned. |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 116 | */ |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 117 | 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] | 118 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 119 | /* |
| 120 | * Find targets for userlist and groups in acl. Function returns the number |
| 121 | * of errors or OK if everything is fine. |
| 122 | */ |
| 123 | int acl_find_targets(struct proxy *p); |
| 124 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 125 | /* Return a pointer to the ACL <name> within the list starting at <head>, or |
| 126 | * NULL if not found. |
| 127 | */ |
| 128 | struct acl *find_acl_by_name(const char *name, struct list *head); |
| 129 | |
| 130 | /* |
| 131 | * Registers the ACL keyword list <kwl> as a list of valid keywords for next |
| 132 | * parsing sessions. |
| 133 | */ |
| 134 | void acl_register_keywords(struct acl_kw_list *kwl); |
| 135 | |
| 136 | /* |
| 137 | * Unregisters the ACL keyword list <kwl> from the list of valid keywords. |
| 138 | */ |
| 139 | void acl_unregister_keywords(struct acl_kw_list *kwl); |
| 140 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 141 | /* initializes ACLs by resolving the sample fetch names they rely upon. |
| 142 | * Returns 0 on success, otherwise an error. |
| 143 | */ |
| 144 | int init_acl(); |
| 145 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 146 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 147 | #endif /* _PROTO_ACL_H */ |
| 148 | |
| 149 | /* |
| 150 | * Local variables: |
| 151 | * c-indent-level: 8 |
| 152 | * c-basic-offset: 8 |
| 153 | * End: |
| 154 | */ |