import from branch_1_1:
[freeradius.git] / src / modules / rlm_policy / rlm_policy.h
1 /*
2  * rlm_policy.h    Header file for policy module
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2004  Alan DeKok <aland@freeradius.org>
21  * Copyright 2006  The FreeRADIUS server project
22  */
23 #ifndef _RLM_POLICY_H
24 #define _RLM_POLICY_H
25
26 #include <freeradius-devel/ident.h>
27 RCSIDH(rlm_policy_h, "$Id$")
28
29 #include <freeradius-devel/radiusd.h>
30 #include <freeradius-devel/modules.h>
31 #include <freeradius-devel/modcall.h>
32 #include <freeradius-devel/rad_assert.h>
33
34 /*
35  *      Internal lexer.
36  */
37 typedef enum policy_lex_t {
38         POLICY_LEX_BAD = 0,
39         POLICY_LEX_EOF,         /* end of the file/input */
40         POLICY_LEX_EOL,         /* end of the line */
41         POLICY_LEX_WHITESPACE,
42         POLICY_LEX_HASH,
43         POLICY_LEX_L_BRACKET,
44         POLICY_LEX_R_BRACKET,
45         POLICY_LEX_LC_BRACKET,  /* left curly bracket */
46         POLICY_LEX_RC_BRACKET,  /* right curly bracket */
47         POLICY_LEX_COMMA,
48         POLICY_LEX_L_AND,       /* logical AND */
49         POLICY_LEX_L_OR,        /* logical OR */
50         POLICY_LEX_AND,         /* bit-wise AND */
51         POLICY_LEX_OR,          /* bit-wise OR */
52         POLICY_LEX_L_NOT,
53         POLICY_LEX_PLUS,        /* + */
54         POLICY_LEX_MINUS,       /* - */
55         POLICY_LEX_ASSIGN,      /* = */
56         POLICY_LEX_CMP_EQUALS,
57         POLICY_LEX_CMP_NOT_EQUALS,
58         POLICY_LEX_CMP_TRUE,
59         POLICY_LEX_CMP_FALSE,
60         POLICY_LEX_LT,
61         POLICY_LEX_GT,
62         POLICY_LEX_LE,
63         POLICY_LEX_GE,
64         POLICY_LEX_RX_EQUALS,
65         POLICY_LEX_RX_NOT_EQUALS,
66         POLICY_LEX_SET_EQUALS,  /* := */
67         POLICY_LEX_AND_EQUALS,  /* &= */
68         POLICY_LEX_OR_EQUALS,   /* |= */
69         POLICY_LEX_PLUS_EQUALS, /* += */
70         POLICY_LEX_MINUS_EQUALS, /* -= */
71         POLICY_LEX_CONCAT_EQUALS, /* .= */
72         POLICY_LEX_VARIABLE,    /* %{foo} */
73         POLICY_LEX_FUNCTION,    /* Hmmm... */
74         POLICY_LEX_DOUBLE_QUOTED_STRING,
75         POLICY_LEX_SINGLE_QUOTED_STRING,
76         POLICY_LEX_BACK_QUOTED_STRING,
77         POLICY_LEX_BARE_WORD
78 } policy_lex_t;
79
80 typedef enum policy_type_t {
81         POLICY_TYPE_BAD = 0,
82         POLICY_TYPE_IF,
83         POLICY_TYPE_CONDITIONAL,
84         POLICY_TYPE_ASSIGNMENT,
85         POLICY_TYPE_ATTRIBUTE_LIST,
86         POLICY_TYPE_PRINT,
87         POLICY_TYPE_NAMED_POLICY,
88         POLICY_TYPE_CALL,
89         POLICY_TYPE_RETURN,
90         POLICY_TYPE_MODULE,
91         POLICY_TYPE_NUM_TYPES
92 } policy_type_t;
93
94
95 /*
96  *      For our policy language, we want to have some reserved words.
97  */
98 typedef enum policy_reserved_word_t {
99         POLICY_RESERVED_UNKNOWN = 0,
100         POLICY_RESERVED_CONTROL,
101         POLICY_RESERVED_REQUEST,
102         POLICY_RESERVED_REPLY,
103         POLICY_RESERVED_PROXY_REQUEST,
104         POLICY_RESERVED_PROXY_REPLY,
105         POLICY_RESERVED_IF,
106         POLICY_RESERVED_ELSE,
107         POLICY_RESERVED_DEBUG,
108         POLICY_RESERVED_PRINT,
109         POLICY_RESERVED_POLICY,
110         POLICY_RESERVED_INCLUDE,
111         POLICY_RESERVED_RETURN,
112         POLICY_RESERVED_MODULE,
113         POLICY_RESERVED_NUM_WORDS
114 } policy_reserved_word_t;
115
116
117 #define POLICY_DEBUG_NONE           0
118 #define POLICY_DEBUG_PEEK           (1 << 0)
119 #define POLICY_DEBUG_PRINT_TOKENS   (1 << 1)
120 #define POLICY_DEBUG_PRINT_POLICY   (1 << 2)
121 #define POLICY_DEBUG_EVALUATE       (1 << 3)
122
123 /*
124  *      A policy item
125  */
126 typedef struct policy_item_t {
127         struct policy_item_t    *next;
128         policy_type_t           type;
129         int                     lineno;
130 } policy_item_t;
131
132
133 /*
134  *      A list of attributes to add/replace/whatever in a packet.
135  */
136 typedef struct policy_print_t {
137         policy_item_t           item;
138         policy_lex_t            rhs_type;
139         const char              *rhs;
140 } policy_print_t;
141
142
143 /*
144  *      A list of attributes to add/replace/whatever in a packet.
145  */
146 typedef struct policy_attributes_t {
147         policy_item_t           item;
148         policy_reserved_word_t  where; /* where to do it */
149         policy_lex_t            how; /* how to do */
150         policy_item_t           *attributes; /* things to do */
151         /* FIXME: VALUE_PAIR *vps; */
152 } policy_attributes_t;
153
154
155 /*
156  *      Holds a named policy
157  */
158 typedef struct policy_named_t {
159         policy_item_t   item;
160         const char      *name;
161         policy_item_t   *policy;
162 } policy_named_t;
163
164
165 /*
166  *      Reference to a named policy
167  */
168 typedef struct policy_call_t {
169         policy_item_t   item;
170         const char      *name;
171 } policy_call_t;
172
173
174 /*
175  *      Hold a return code
176  */
177 typedef struct policy_return_t {
178         policy_item_t   item;
179         int             rcode;
180 } policy_return_t;
181
182
183 /*
184  *      Holds an assignment.
185  */
186 typedef struct policy_assignment_t {
187         policy_item_t   item;
188         char            *lhs;
189         policy_lex_t    assign; /* operator for the assignment */
190         policy_lex_t    rhs_type;
191         char            *rhs;
192 } policy_assignment_t;
193
194
195 /*
196  *      Condition
197  */
198 typedef struct policy_condition_t {
199         policy_item_t   item;
200
201         policy_lex_t    lhs_type;
202         char            *lhs;
203         policy_lex_t    compare;
204         policy_lex_t    rhs_type; /* bare word, quoted string, etc. */
205         char            *rhs;
206
207         policy_lex_t    child_condition;
208         policy_item_t   *child;
209 } policy_condition_t;
210
211
212 /*
213  *      Holds an "if" statement.  The "else" may be a block, or another "if"
214  */
215 typedef struct policy_if_t {
216         policy_item_t           item;
217         policy_item_t           *condition;
218         policy_item_t           *if_true;
219         policy_item_t           *if_false;      /* assignment, or other 'if' */
220 } policy_if_t;
221
222
223 /*
224  *      Holds a reference to calling other modules... wild.
225  */
226 typedef struct policy_module_t {
227         policy_item_t   item;
228         int             component; /* authorize, authenticate, etc. */
229         CONF_SECTION    *cs;
230         modcallable     *mc;
231 } policy_module_t;
232
233
234 /*
235  *      Define a structure for our module configuration.
236  *
237  *      These variables do not need to be in a structure, but it's
238  *      a lot cleaner to do so, and a pointer to the structure can
239  *      be used as the instance handle.
240  */
241 typedef struct rlm_policy_t {
242         char            *filename;
243         rbtree_t        *policies;
244 } rlm_policy_t;
245
246
247 /*
248  *      Functions.
249  */
250 extern const LRAD_NAME_NUMBER rlm_policy_tokens[];
251 extern const LRAD_NAME_NUMBER policy_reserved_words[];
252 extern const LRAD_NAME_NUMBER policy_return_codes[];
253 extern const LRAD_NAME_NUMBER policy_component_names[];
254
255 extern int rlm_policy_insert(rbtree_t *head, policy_named_t *policy);
256 extern policy_named_t *rlm_policy_find(rbtree_t *head, const char *name);
257
258 extern int rlm_policy_parse(rbtree_t *policies, const char *filename);
259 extern void rlm_policy_free_item(policy_item_t *item);
260 extern void rlm_policy_print(const policy_item_t *item);
261 extern int rlm_policy_evaluate(rlm_policy_t *inst, REQUEST *request,
262                                const char *name);
263
264 #endif /* _RLM_POLICY_H */