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