include <string.h> if necessary, too
[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/autoconf.h>
30
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
34
35 #include <string.h>
36 #include <stdlib.h>
37
38 #include <freeradius-devel/radiusd.h>
39 #include <freeradius-devel/modules.h>
40 #include <freeradius-devel/modcall.h>
41
42 #include <freeradius-devel/rad_assert.h>
43
44 /*
45  *      Internal lexer.
46  */
47 typedef enum policy_lex_t {
48         POLICY_LEX_BAD = 0,
49         POLICY_LEX_EOF,         /* end of the file/input */
50         POLICY_LEX_EOL,         /* end of the line */
51         POLICY_LEX_WHITESPACE,
52         POLICY_LEX_HASH,
53         POLICY_LEX_L_BRACKET,
54         POLICY_LEX_R_BRACKET,
55         POLICY_LEX_LC_BRACKET,  /* left curly bracket */
56         POLICY_LEX_RC_BRACKET,  /* right curly bracket */
57         POLICY_LEX_COMMA,
58         POLICY_LEX_L_AND,       /* logical AND */
59         POLICY_LEX_L_OR,        /* logical OR */
60         POLICY_LEX_AND,         /* bit-wise AND */
61         POLICY_LEX_OR,          /* bit-wise OR */
62         POLICY_LEX_L_NOT,
63         POLICY_LEX_PLUS,        /* + */
64         POLICY_LEX_MINUS,       /* - */
65         POLICY_LEX_ASSIGN,      /* = */
66         POLICY_LEX_CMP_EQUALS,
67         POLICY_LEX_CMP_NOT_EQUALS,
68         POLICY_LEX_CMP_TRUE,
69         POLICY_LEX_CMP_FALSE,
70         POLICY_LEX_LT,
71         POLICY_LEX_GT,
72         POLICY_LEX_LE,
73         POLICY_LEX_GE,
74         POLICY_LEX_RX_EQUALS,
75         POLICY_LEX_RX_NOT_EQUALS,
76         POLICY_LEX_SET_EQUALS,  /* := */
77         POLICY_LEX_AND_EQUALS,  /* &= */
78         POLICY_LEX_OR_EQUALS,   /* |= */
79         POLICY_LEX_PLUS_EQUALS, /* += */
80         POLICY_LEX_MINUS_EQUALS, /* -= */
81         POLICY_LEX_CONCAT_EQUALS, /* .= */
82         POLICY_LEX_VARIABLE,    /* %{foo} */
83         POLICY_LEX_FUNCTION,    /* Hmmm... */
84         POLICY_LEX_DOUBLE_QUOTED_STRING,
85         POLICY_LEX_SINGLE_QUOTED_STRING,
86         POLICY_LEX_BACK_QUOTED_STRING,
87         POLICY_LEX_BARE_WORD
88 } policy_lex_t;
89
90 typedef enum policy_type_t {
91         POLICY_TYPE_BAD = 0,
92         POLICY_TYPE_IF,
93         POLICY_TYPE_CONDITIONAL,
94         POLICY_TYPE_ASSIGNMENT,
95         POLICY_TYPE_ATTRIBUTE_LIST,
96         POLICY_TYPE_PRINT,
97         POLICY_TYPE_NAMED_POLICY,
98         POLICY_TYPE_CALL,
99         POLICY_TYPE_RETURN,
100         POLICY_TYPE_MODULE,
101         POLICY_TYPE_NUM_TYPES
102 } policy_type_t;
103
104
105 /*
106  *      For our policy language, we want to have some reserved words.
107  */
108 typedef enum policy_reserved_word_t {
109         POLICY_RESERVED_UNKNOWN = 0,
110         POLICY_RESERVED_CONTROL,
111         POLICY_RESERVED_REQUEST,
112         POLICY_RESERVED_REPLY,
113         POLICY_RESERVED_PROXY_REQUEST,
114         POLICY_RESERVED_PROXY_REPLY,
115         POLICY_RESERVED_IF,
116         POLICY_RESERVED_ELSE,
117         POLICY_RESERVED_DEBUG,
118         POLICY_RESERVED_PRINT,
119         POLICY_RESERVED_POLICY,
120         POLICY_RESERVED_INCLUDE,
121         POLICY_RESERVED_RETURN,
122         POLICY_RESERVED_MODULE,
123         POLICY_RESERVED_NUM_WORDS
124 } policy_reserved_word_t;
125
126
127 #define POLICY_DEBUG_NONE           0
128 #define POLICY_DEBUG_PEEK           (1 << 0)
129 #define POLICY_DEBUG_PRINT_TOKENS   (1 << 1)
130 #define POLICY_DEBUG_PRINT_POLICY   (1 << 2)
131 #define POLICY_DEBUG_EVALUATE       (1 << 3)
132
133 /*
134  *      A policy item
135  */
136 typedef struct policy_item_t {
137         struct policy_item_t    *next;
138         policy_type_t           type;
139         int                     lineno;
140 } policy_item_t;
141
142
143 /*
144  *      A list of attributes to add/replace/whatever in a packet.
145  */
146 typedef struct policy_print_t {
147         policy_item_t           item;
148         policy_lex_t            rhs_type;
149         const char              *rhs;
150 } policy_print_t;
151
152
153 /*
154  *      A list of attributes to add/replace/whatever in a packet.
155  */
156 typedef struct policy_attributes_t {
157         policy_item_t           item;
158         policy_reserved_word_t  where; /* where to do it */
159         policy_lex_t            how; /* how to do */
160         policy_item_t           *attributes; /* things to do */
161         /* FIXME: VALUE_PAIR *vps; */
162 } policy_attributes_t;
163
164
165 /*
166  *      Holds a named policy
167  */
168 typedef struct policy_named_t {
169         policy_item_t   item;
170         const char      *name;
171         policy_item_t   *policy;
172 } policy_named_t;
173
174
175 /*
176  *      Reference to a named policy
177  */
178 typedef struct policy_call_t {
179         policy_item_t   item;
180         const char      *name;
181 } policy_call_t;
182
183
184 /*
185  *      Hold a return code
186  */
187 typedef struct policy_return_t {
188         policy_item_t   item;
189         int             rcode;
190 } policy_return_t;
191
192
193 /*
194  *      Holds an assignment.
195  */
196 typedef struct policy_assignment_t {
197         policy_item_t   item;
198         char            *lhs;
199         policy_lex_t    assign; /* operator for the assignment */
200         policy_lex_t    rhs_type;
201         char            *rhs;
202 } policy_assignment_t;
203
204
205 /*
206  *      Condition
207  */
208 typedef struct policy_condition_t {
209         policy_item_t   item;
210
211         policy_lex_t    lhs_type;
212         char            *lhs;
213         policy_lex_t    compare;
214         policy_lex_t    rhs_type; /* bare word, quoted string, etc. */
215         char            *rhs;
216
217         policy_lex_t    child_condition;
218         policy_item_t   *child;
219 } policy_condition_t;
220
221
222 /*
223  *      Holds an "if" statement.  The "else" may be a block, or another "if"
224  */
225 typedef struct policy_if_t {
226         policy_item_t           item;
227         policy_item_t           *condition;
228         policy_item_t           *if_true;
229         policy_item_t           *if_false;      /* assignment, or other 'if' */
230 } policy_if_t;
231
232
233 /*
234  *      Holds a reference to calling other modules... wild.
235  */
236 typedef struct policy_module_t {
237         policy_item_t   item;
238         int             component; /* authorize, authenticate, etc. */
239         CONF_SECTION    *cs;
240         modcallable     *mc;
241 } policy_module_t;
242
243
244 /*
245  *      Define a structure for our module configuration.
246  *
247  *      These variables do not need to be in a structure, but it's
248  *      a lot cleaner to do so, and a pointer to the structure can
249  *      be used as the instance handle.
250  */
251 typedef struct rlm_policy_t {
252         char            *filename;
253         rbtree_t        *policies;
254 } rlm_policy_t;
255
256
257 /*
258  *      Functions.
259  */
260 extern const LRAD_NAME_NUMBER rlm_policy_tokens[];
261 extern const LRAD_NAME_NUMBER policy_reserved_words[];
262 extern const LRAD_NAME_NUMBER policy_return_codes[];
263 extern const LRAD_NAME_NUMBER policy_component_names[];
264
265 extern int rlm_policy_insert(rbtree_t *head, policy_named_t *policy);
266 extern policy_named_t *rlm_policy_find(rbtree_t *head, const char *name);
267
268 extern int rlm_policy_parse(rbtree_t *policies, const char *filename);
269 extern void rlm_policy_free_item(policy_item_t *item);
270 extern void rlm_policy_print(const policy_item_t *item);
271 extern int rlm_policy_evaluate(rlm_policy_t *inst, REQUEST *request,
272                                const char *name);
273
274 #endif /* _RLM_POLICY_H */