2007-05-06 18:36:48 -04:00
|
|
|
/*
|
2009-10-04 16:02:50 -04:00
|
|
|
* include/types/acl.h
|
|
|
|
|
* This file provides structures and types for ACLs.
|
|
|
|
|
*
|
2012-04-19 11:16:54 -04:00
|
|
|
* Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
|
2009-10-04 16:02:50 -04:00
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
|
* exclusively.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
2007-05-06 18:36:48 -04:00
|
|
|
|
|
|
|
|
#ifndef _TYPES_ACL_H
|
|
|
|
|
#define _TYPES_ACL_H
|
|
|
|
|
|
|
|
|
|
#include <common/compat.h>
|
|
|
|
|
#include <common/config.h>
|
|
|
|
|
#include <common/mini-clist.h>
|
|
|
|
|
|
2012-04-19 11:16:54 -04:00
|
|
|
#include <types/arg.h>
|
2010-01-29 13:26:18 -05:00
|
|
|
#include <types/auth.h>
|
2007-05-06 18:36:48 -04:00
|
|
|
#include <types/proxy.h>
|
2012-04-27 15:52:18 -04:00
|
|
|
#include <types/sample.h>
|
2010-05-16 16:18:27 -04:00
|
|
|
#include <types/server.h>
|
2007-05-06 18:36:48 -04:00
|
|
|
#include <types/session.h>
|
|
|
|
|
|
2010-05-10 16:29:06 -04:00
|
|
|
#include <ebmbtree.h>
|
2007-05-06 18:36:48 -04:00
|
|
|
|
2008-07-09 10:18:21 -04:00
|
|
|
/* Pattern matching function result.
|
|
|
|
|
*
|
|
|
|
|
* We're using a 3-state matching system :
|
|
|
|
|
* - PASS : at least one pattern already matches
|
|
|
|
|
* - MISS : some data is missing to decide if some rules may finally match.
|
|
|
|
|
* - FAIL : no mattern may ever match
|
|
|
|
|
*
|
|
|
|
|
* We assign values 0, 1 and 3 to FAIL, MISS and PASS respectively, so that we
|
|
|
|
|
* can make use of standard arithmetics for the truth tables below :
|
|
|
|
|
*
|
|
|
|
|
* x | !x x&y | F(0) | M(1) | P(3) x|y | F(0) | M(1) | P(3)
|
|
|
|
|
* ------+----- -----+------+------+----- -----+------+------+-----
|
|
|
|
|
* F(0) | P(3) F(0)| F(0) | F(0) | F(0) F(0)| F(0) | M(1) | P(3)
|
|
|
|
|
* M(1) | M(1) M(1)| F(0) | M(1) | M(1) M(1)| M(1) | M(1) | P(3)
|
|
|
|
|
* P(3) | F(0) P(3)| F(0) | M(1) | P(3) P(3)| P(3) | P(3) | P(3)
|
|
|
|
|
*
|
|
|
|
|
* neg(x) = (3 >> x) and(x,y) = (x & y) or(x,y) = (x | y)
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2007-05-06 18:36:48 -04:00
|
|
|
enum {
|
|
|
|
|
ACL_PAT_FAIL = 0, /* test failed */
|
2008-07-09 10:18:21 -04:00
|
|
|
ACL_PAT_MISS = 1, /* test may pass with more info */
|
|
|
|
|
ACL_PAT_PASS = 3, /* test passed */
|
2007-05-06 18:36:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Condition polarity. It makes it easier for any option to choose between
|
|
|
|
|
* IF/UNLESS if it can store that information within the condition itself.
|
2008-07-09 10:18:21 -04:00
|
|
|
* Those should be interpreted as "IF/UNLESS result == PASS".
|
2007-05-06 18:36:48 -04:00
|
|
|
*/
|
|
|
|
|
enum {
|
|
|
|
|
ACL_COND_NONE, /* no polarity set yet */
|
|
|
|
|
ACL_COND_IF, /* positive condition (after 'if') */
|
|
|
|
|
ACL_COND_UNLESS, /* negative condition (after 'unless') */
|
|
|
|
|
};
|
|
|
|
|
|
2007-06-17 02:20:33 -04:00
|
|
|
/* possible flags for expressions or patterns */
|
|
|
|
|
enum {
|
|
|
|
|
ACL_PAT_F_IGNORE_CASE = 1 << 0, /* ignore case */
|
|
|
|
|
ACL_PAT_F_FROM_FILE = 1 << 1, /* pattern comes from a file */
|
2010-05-10 16:29:06 -04:00
|
|
|
ACL_PAT_F_TREE_OK = 1 << 2, /* the pattern parser is allowed to build a tree */
|
|
|
|
|
ACL_PAT_F_TREE = 1 << 3, /* some patterns are arranged in a tree */
|
2007-06-17 02:20:33 -04:00
|
|
|
};
|
|
|
|
|
|
2013-03-31 16:13:34 -04:00
|
|
|
/* ACL match methods */
|
|
|
|
|
enum {
|
|
|
|
|
ACL_MATCH_FOUND, /* just ensure that fetch found the sample */
|
|
|
|
|
ACL_MATCH_BOOL, /* match fetch's integer value as boolean */
|
|
|
|
|
ACL_MATCH_INT, /* unsigned integer (int) */
|
|
|
|
|
ACL_MATCH_IP, /* IPv4/IPv6 address (IP) */
|
|
|
|
|
ACL_MATCH_BIN, /* hex string (bin) */
|
|
|
|
|
ACL_MATCH_LEN, /* string length (str -> int) */
|
|
|
|
|
ACL_MATCH_STR, /* exact string match (str) */
|
|
|
|
|
ACL_MATCH_BEG, /* beginning of string (str) */
|
|
|
|
|
ACL_MATCH_SUB, /* substring (str) */
|
|
|
|
|
ACL_MATCH_DIR, /* directory-like sub-string (str) */
|
|
|
|
|
ACL_MATCH_DOM, /* domain-like sub-string (str) */
|
|
|
|
|
ACL_MATCH_END, /* end of string (str) */
|
|
|
|
|
ACL_MATCH_REG, /* regex (str -> reg) */
|
|
|
|
|
/* keep this one last */
|
|
|
|
|
ACL_MATCH_NUM
|
|
|
|
|
};
|
|
|
|
|
|
2007-05-06 18:36:48 -04:00
|
|
|
/* How to store a time range and the valid days in 29 bits */
|
|
|
|
|
struct acl_time {
|
|
|
|
|
int dow:7; /* 1 bit per day of week: 0-6 */
|
|
|
|
|
int h1:5, m1:6; /* 0..24:0..60. Use 0:0 for all day. */
|
|
|
|
|
int h2:5, m2:6; /* 0..24:0..60. Use 24:0 for all day. */
|
|
|
|
|
};
|
|
|
|
|
|
2012-04-27 16:10:57 -04:00
|
|
|
/* This describes one ACL pattern, which might be a single value or a tree of
|
|
|
|
|
* values. All patterns for a single ACL expression are linked together. Some
|
|
|
|
|
* of them might have a type (eg: IP). Right now, the types are shared with
|
|
|
|
|
* the samples, though it is possible that in the future this will change to
|
|
|
|
|
* accommodate for other types (eg: meth, regex). Unsigned and constant types
|
|
|
|
|
* are preferred when there is a doubt.
|
|
|
|
|
*/
|
2007-05-06 18:36:48 -04:00
|
|
|
struct acl_pattern {
|
|
|
|
|
struct list list; /* chaining */
|
2012-04-27 16:10:57 -04:00
|
|
|
int type; /* type of the ACL pattern (SMP_T_*) */
|
2007-05-06 18:36:48 -04:00
|
|
|
union {
|
|
|
|
|
int i; /* integer value */
|
2007-06-09 17:10:04 -04:00
|
|
|
struct {
|
|
|
|
|
signed long long min, max;
|
|
|
|
|
int min_set :1;
|
|
|
|
|
int max_set :1;
|
|
|
|
|
} range; /* integer range */
|
2007-05-08 13:50:09 -04:00
|
|
|
struct {
|
|
|
|
|
struct in_addr addr;
|
|
|
|
|
struct in_addr mask;
|
|
|
|
|
} ipv4; /* IPv4 address */
|
MEDIUM: acl: support IPv6 address matching
Make use of the new IPv6 pattern type so that acl_match_ip() knows how to
compare pattern and sample.
IPv6 may be entered in their usual form, with or without a netmask appended.
Only bit counts are accepted for IPv6 netmasks. In order to avoid any risk of
trouble with randomly resolved IP addresses, host names are never allowed in
IPv6 patterns.
HAProxy is also able to match IPv4 addresses with IPv6 addresses in the
following situations :
- tested address is IPv4, pattern address is IPv4, the match applies
in IPv4 using the supplied mask if any.
- tested address is IPv6, pattern address is IPv6, the match applies
in IPv6 using the supplied mask if any.
- tested address is IPv6, pattern address is IPv4, the match applies in IPv4
using the pattern's mask if the IPv6 address matches with 2002:IPV4::,
::IPV4 or ::ffff:IPV4, otherwise it fails.
- tested address is IPv4, pattern address is IPv6, the IPv4 address is first
converted to IPv6 by prefixing ::ffff: in front of it, then the match is
applied in IPv6 using the supplied IPv6 mask.
2012-04-27 18:41:46 -04:00
|
|
|
struct {
|
|
|
|
|
struct in6_addr addr;
|
|
|
|
|
unsigned char mask; /* number of bits */
|
|
|
|
|
} ipv6; /* IPv6 address/mask */
|
2007-05-06 18:36:48 -04:00
|
|
|
struct acl_time time; /* valid hours and days */
|
2010-01-29 13:26:18 -05:00
|
|
|
unsigned int group_mask;
|
2010-05-11 17:25:05 -04:00
|
|
|
struct eb_root *tree; /* tree storing all values if any */
|
2007-05-06 18:36:48 -04:00
|
|
|
} val; /* direct value */
|
|
|
|
|
union {
|
|
|
|
|
void *ptr; /* any data */
|
|
|
|
|
char *str; /* any string */
|
2013-01-13 01:00:42 -05:00
|
|
|
regex *reg; /* a compiled regex */
|
2007-05-06 18:36:48 -04:00
|
|
|
} ptr; /* indirect values, allocated */
|
2008-05-31 07:53:23 -04:00
|
|
|
void(*freeptrbuf)(void *ptr); /* a destructor able to free objects from the ptr */
|
2007-05-06 18:36:48 -04:00
|
|
|
int len; /* data length when required */
|
2007-06-17 02:20:33 -04:00
|
|
|
int flags; /* expr or pattern flags. */
|
2007-05-06 18:36:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* some dummy declarations to silent the compiler */
|
|
|
|
|
struct proxy;
|
|
|
|
|
struct session;
|
|
|
|
|
|
2012-04-27 16:10:57 -04:00
|
|
|
/*
|
|
|
|
|
* ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers.
|
|
|
|
|
*/
|
2007-06-09 17:10:04 -04:00
|
|
|
/*
|
|
|
|
|
* NOTE:
|
|
|
|
|
* The 'parse' function is called to parse words in the configuration. It must
|
|
|
|
|
* return the number of valid words read. 0 = error. The 'opaque' argument may
|
|
|
|
|
* be used by functions which need to maintain a context between consecutive
|
|
|
|
|
* values. It is initialized to zero before the first call, and passed along
|
|
|
|
|
* successive calls.
|
|
|
|
|
*/
|
|
|
|
|
|
2007-06-10 05:47:14 -04:00
|
|
|
struct acl_expr;
|
2007-05-06 18:36:48 -04:00
|
|
|
struct acl_keyword {
|
|
|
|
|
const char *kw;
|
2013-01-11 09:49:37 -05:00
|
|
|
char *fetch_kw;
|
2012-04-27 11:52:25 -04:00
|
|
|
int (*parse)(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
|
2012-04-23 10:16:37 -04:00
|
|
|
int (*match)(struct sample *smp, struct acl_pattern *pattern);
|
2012-04-19 12:42:05 -04:00
|
|
|
/* must be after the config params */
|
2013-01-11 09:49:37 -05:00
|
|
|
struct sample_fetch *smp; /* the sample fetch we depend on */
|
2007-05-06 18:36:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* A keyword list. It is a NULL-terminated array of keywords. It embeds a
|
|
|
|
|
* struct list in order to be linked to other lists, allowing it to easily
|
|
|
|
|
* be declared where it is needed, and linked without duplicating data nor
|
|
|
|
|
* allocating memory.
|
|
|
|
|
*/
|
|
|
|
|
struct acl_kw_list {
|
|
|
|
|
struct list list;
|
|
|
|
|
struct acl_keyword kw[VAR_ARRAY];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Description of an ACL expression.
|
2013-03-31 12:34:33 -04:00
|
|
|
* The expression is part of a list. It contains pointers to the keyword, the
|
|
|
|
|
* parse and match functions which default to the keyword's, the sample fetch
|
|
|
|
|
* descriptor which also defaults to the keyword's, and a list or tree of
|
|
|
|
|
* patterns to test against. The structure is organized so that the hot parts
|
|
|
|
|
* are grouped together in order to optimize caching.
|
2007-05-06 18:36:48 -04:00
|
|
|
*/
|
|
|
|
|
struct acl_expr {
|
2013-03-31 12:34:33 -04:00
|
|
|
int (*parse)(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
|
|
|
|
|
int (*match)(struct sample *smp, struct acl_pattern *pattern);
|
|
|
|
|
struct arg *args; /* optional fetch argument list (eg: header or cookie name) */
|
|
|
|
|
struct sample_fetch *smp; /* the sample fetch we depend on */
|
|
|
|
|
struct list patterns; /* list of acl_patterns */
|
2010-05-10 16:29:06 -04:00
|
|
|
struct eb_root pattern_tree; /* may be used for lookup in large datasets */
|
2013-03-31 12:34:33 -04:00
|
|
|
struct list list; /* chaining */
|
2013-03-31 16:59:32 -04:00
|
|
|
const char *kw; /* points to the ACL kw's name or fetch's name (must not free) */
|
2007-05-06 18:36:48 -04:00
|
|
|
};
|
|
|
|
|
|
2012-04-27 16:10:57 -04:00
|
|
|
/* The acl will be linked to from the proxy where it is declared */
|
2007-05-06 18:36:48 -04:00
|
|
|
struct acl {
|
|
|
|
|
struct list list; /* chaining */
|
|
|
|
|
char *name; /* acl name */
|
|
|
|
|
struct list expr; /* list of acl_exprs */
|
|
|
|
|
int cache_idx; /* ACL index in cache */
|
2013-03-25 03:12:18 -04:00
|
|
|
unsigned int use; /* or'ed bit mask of all acl_expr's SMP_USE_* */
|
|
|
|
|
unsigned int val; /* or'ed bit mask of all acl_expr's SMP_VAL_* */
|
2007-05-06 18:36:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* the condition will be linked to from an action in a proxy */
|
|
|
|
|
struct acl_term {
|
|
|
|
|
struct list list; /* chaining */
|
|
|
|
|
struct acl *acl; /* acl pointed to by this term */
|
|
|
|
|
int neg; /* 1 if the ACL result must be negated */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct acl_term_suite {
|
|
|
|
|
struct list list; /* chaining of term suites */
|
|
|
|
|
struct list terms; /* list of acl_terms */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct acl_cond {
|
|
|
|
|
struct list list; /* Some specific tests may use multiple conditions */
|
|
|
|
|
struct list suites; /* list of acl_term_suites */
|
|
|
|
|
int pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */
|
2013-03-25 03:12:18 -04:00
|
|
|
unsigned int use; /* or'ed bit mask of all suites's SMP_USE_* */
|
|
|
|
|
unsigned int val; /* or'ed bit mask of all suites's SMP_VAL_* */
|
2009-10-04 16:02:50 -04:00
|
|
|
const char *file; /* config file where the condition is declared */
|
2008-07-25 13:13:19 -04:00
|
|
|
int line; /* line in the config file where the condition is declared */
|
2007-05-06 18:36:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* _TYPES_ACL_H */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Local variables:
|
|
|
|
|
* c-indent-level: 8
|
|
|
|
|
* c-basic-offset: 8
|
|
|
|
|
* End:
|
|
|
|
|
*/
|