The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
cf_priv.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/**
19 * $Id: f91b4d4ba8844de6bdb403d90d414f76ebe0bef2 $
20 *
21 * @file cf_priv.h
22 * @brief Private data structures and types for cf_*.c
23 *
24 * @copyright 2017 The FreeRADIUS server project
25 */
26RCSIDH(cf_priv_h, "$Id: f91b4d4ba8844de6bdb403d90d414f76ebe0bef2 $")
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#include <stdint.h>
33#include <sys/stat.h>
34
35#include <freeradius-devel/server/cf_parse.h>
36#include <freeradius-devel/util/rb.h>
37#include <freeradius-devel/util/dlist.h>
38
39typedef enum conf_type : uint8_t {
44 CONF_ITEM_COMMENT, //!< A `# ...` line preserved verbatim
45 ///< from the input - only created when
46 ///< the parser is asked to keep comments
47 ///< (off by default; the runtime parser
48 ///< doesn't keep them, tooling can opt in).
50
51/** Common header for all CONF_* types
52 *
53 */
54struct cf_item {
55 fr_rb_node_t ident1_node; //!< Entry in the ident1 tree.
56 fr_rb_node_t ident2_node; //!< Entry in the ident2 tree.
57
58 fr_dlist_t entry; //!< Entry in dlist
59 fr_dlist_head_t children; //!< The head of the ordered list of children.
60
61 CONF_ITEM *parent; //!< Parent
62
63 fr_rb_tree_t *ident1; //!< Tree to store the first identifier (name1 || type || attr).
64 fr_rb_tree_t *ident2; //!< Tree to store the second identifier (name2 || name).
65
66 CONF_ITEM_TYPE type; //!< Whether the config item is a config_pair, conf_section or cf_data.
67
68 bool parsed; //!< Was this item used during parsing?
69 bool referenced; //!< Was this item referenced in the config?
70 int lineno; //!< The line number the config item began on.
71 char const *filename; //!< The file the config item was parsed from.
72};
73
74/** Configuration AVP similar to a fr_pair_t
75 *
76 */
77struct cf_pair {
78 CONF_ITEM item; //!< Common set of fields.
79
80 char const *attr; //!< Attribute name
81 char const *value; //!< Attribute value
82
83 fr_token_t op; //!< Operator e.g. =, :=
84 fr_token_t lhs_quote; //!< Name quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
85 fr_token_t rhs_quote; //!< Value Quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
86
87 bool pass2; //!< do expansion in pass2.
88 bool printed; //!< Was this item printed already in debug mode?
89};
90
91typedef enum : uint8_t {
92 CF_UNLANG_NONE = 0, //!< no unlang
93 CF_UNLANG_ALLOW, //!< allow unlang in this section
94 CF_UNLANG_SERVER, //!< this section is a virtual server, allow unlang 2 down
95 CF_UNLANG_POLICY, //!< this section is a policy, allow unlang 2 down
96 CF_UNLANG_MODULES, //!< this section is in "modules", allow unlang 2 down
97 CF_UNLANG_EDIT, //!< only edit commands
98 CF_UNLANG_ASSIGNMENT, //!< only assignments inside of map / update
99 CF_UNLANG_DICTIONARY, //!< only local variable definitions
100 CF_UNLANG_CAN_HAVE_UPDATE, //!< can have "update"
102
103/** A section grouping multiple #CONF_PAIR
104 *
105 */
107 CONF_ITEM item; //!< Common set of fields.
108
109 char const *name1; //!< First name token. Given ``foo bar {}`` would be ``foo``.
110 char const *name2; //!< Second name token. Given ``foo bar {}`` would be ``bar``.
111
112 fr_token_t name2_quote; //!< The type of quoting around name2.
113
114 int argc; //!< number of additional arguments
115 char const **argv; //!< additional arguments
117
118 void *base;
119 int depth;
121 bool allow_locals; //!< allow local variables
122 bool at_reference; //!< this thing was created from an @...
123
124 CONF_SECTION *template;
125};
126
127/** Limit the nesting depth.
128 *
129 * 256 is way past what any human being would create, and way past
130 * anything that's reasonable.
131 */
132#define CONF_SECTION_MAX_DEPTH 256
133
134/** Limit the number of direct children that a section can have.
135 *
136 * This limit is largely for the fuzzers, which find pathological
137 * cases of copying references. When copying a ${name} that resolves
138 * to a section, cf_expand_variables() copies that section into the
139 * enclosing one. Two references per section double the item count at
140 * each level, so each extra pair of lines quadruples the memory.
141 *
142 * Instead of having a limit just for references, we instead limit
143 * the total number of children that a section can have.
144 *
145 * The number 1024 here is picked arbitrarily. It's large enough to
146 * not affect most cases, including the "cheat" of using the
147 * configuration as a read-only key-value store. However, if you're
148 * storing more than 1024 things, you should really use a database.
149 */
150#define CF_SECTION_MAX_CHILDREN (1024U)
151
152/** Limit the number of indirect children that a section can have.
153 *
154 * In a pathological case, a configuration can copy sections via:
155 *
156 * a1 = { a, b } 2 children
157 * a2 = { a = ${a1}, b = ${a1} } 4 children
158 * a3 = { a = ${a2}, b = ${a2} } 8 children
159 * ...
160 *
161 * Therefore before we duplicate a section, we check if the copy will
162 * result in too many children. The limit of 1024 is unrelated to
163 * CF_SECTION_MAX_CHILDREN, but is chosen for similar reasons.
164 */
165#define CF_SECTION_MAX_DUP_ITEMS (1024U)
166
167/*
168 * Private getter for cf_file.c's parser hook - the public API is the
169 * `cf_preserve_comments_set()` setter in cf_util.h; the flag itself
170 * lives static in cf_util.c.
171 */
172bool _cf_preserve_comments(void);
173
174/*
175 * Private getter for cf_file.c's cf_expand_variables() short-circuit.
176 * Public API is `cf_expand_variables_set()` in cf_util.h.
177 */
178bool _cf_expand_variables(void);
179
180/** A `# ...` comment line preserved verbatim from the input.
181 *
182 * Only constructed when `cf_preserve_comments` is set before parsing;
183 * the runtime config parser drops comments as it always has.
184 */
186 CONF_ITEM item; //!< Common set of fields.
187
188 char const *text; //!< Comment text, with the leading `#`
189 //!< and any surrounding whitespace stripped.
190};
191
192/** Internal data that is associated with a configuration section
193 *
194 */
195struct cf_data {
196 CONF_ITEM item; //!< Common set of fields.
197
198 char const *type; //!< C type of data being stored.
199 char const *name; //!< Additional qualification of type.
200
201 void const *data; //!< User data.
202 bool is_talloced; //!< If true we can do extra checks.
203};
204
205typedef struct {
207 char const *filename; //!< name of the file
208 CONF_SECTION *cs; //!< CONF_SECTION associated with the file
209 struct stat buf; //!< stat about the file
210 bool from_dir; //!< was read from a directory
211} cf_file_t;
212
213/** Iterate over the contents of a list
214 *
215 * @param[in] _ci to iterate over.
216 * @param[in] _iter Name of iteration variable.
217 * Will be declared in the scope of the loop.
218 * @param[in] _prev previous pointer
219 */
220#define cf_item_foreach_next(_ci, _iter, _prev) \
221 for (CONF_ITEM *_iter = fr_dlist_next(&(_ci)->children, _prev); _iter; _iter = fr_dlist_next(&(_ci)->children, _iter))
222
223/** Iterate over the contents of a list in reverse order
224 *
225 * @param[in] _ci to iterate over.
226 * @param[in] _iter Name of iteration variable.
227 * Will be declared in the scope of the loop.
228 * @param[in] _prev previous pointer
229 */
230#define cf_item_foreach_prev(_ci, _iter, _prev) \
231 for (CONF_ITEM *_iter = fr_dlist_prev(&(_ci)->children, _prev); _iter; _iter = fr_dlist_prev(&(_ci)->children, _iter))
232
233/** Check if the CONF_ITEM has no children.
234 *
235 * Which is the common use-case
236 *
237 * @param[in] ci to check
238 * @return true/false
239 */
240static inline CC_HINT(nonnull) bool cf_item_has_no_children(CONF_ITEM const *ci)
241{
242 return fr_dlist_empty(&ci->children);
243}
244
245#ifdef __cplusplus
246}
247#endif
#define RCSIDH(h, id)
Definition build.h:604
fr_rb_node_t ident2_node
Entry in the ident2 tree.
Definition cf_priv.h:56
CONF_SECTION * cs
CONF_SECTION associated with the file.
Definition cf_priv.h:208
bool printed
Was this item printed already in debug mode?
Definition cf_priv.h:88
CONF_ITEM item
Common set of fields.
Definition cf_priv.h:107
CONF_ITEM * parent
Parent.
Definition cf_priv.h:61
fr_token_t name2_quote
The type of quoting around name2.
Definition cf_priv.h:112
bool from_dir
was read from a directory
Definition cf_priv.h:210
void * base
Definition cf_priv.h:118
char const * name2
Second name token. Given foo bar {} would be bar.
Definition cf_priv.h:110
bool allow_locals
allow local variables
Definition cf_priv.h:121
int argc
number of additional arguments
Definition cf_priv.h:114
char const * attr
Attribute name.
Definition cf_priv.h:80
char const * name
Additional qualification of type.
Definition cf_priv.h:199
conf_type
Definition cf_priv.h:39
@ CONF_ITEM_INVALID
Definition cf_priv.h:40
@ CONF_ITEM_PAIR
Definition cf_priv.h:41
@ CONF_ITEM_DATA
Definition cf_priv.h:43
@ CONF_ITEM_SECTION
Definition cf_priv.h:42
@ CONF_ITEM_COMMENT
A # ... line preserved verbatim from the input - only created when the parser is asked to keep commen...
Definition cf_priv.h:44
bool referenced
Was this item referenced in the config?
Definition cf_priv.h:69
fr_rb_tree_t * ident2
Tree to store the second identifier (name2 || name).
Definition cf_priv.h:64
fr_token_t rhs_quote
Value Quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
Definition cf_priv.h:85
bool parsed
Was this item used during parsing?
Definition cf_priv.h:68
cf_unlang_t
Definition cf_priv.h:91
@ CF_UNLANG_MODULES
this section is in "modules", allow unlang 2 down
Definition cf_priv.h:96
@ CF_UNLANG_NONE
no unlang
Definition cf_priv.h:92
@ CF_UNLANG_CAN_HAVE_UPDATE
can have "update"
Definition cf_priv.h:100
@ CF_UNLANG_ALLOW
allow unlang in this section
Definition cf_priv.h:93
@ CF_UNLANG_POLICY
this section is a policy, allow unlang 2 down
Definition cf_priv.h:95
@ CF_UNLANG_ASSIGNMENT
only assignments inside of map / update
Definition cf_priv.h:98
@ CF_UNLANG_EDIT
only edit commands
Definition cf_priv.h:97
@ CF_UNLANG_DICTIONARY
only local variable definitions
Definition cf_priv.h:99
@ CF_UNLANG_SERVER
this section is a virtual server, allow unlang 2 down
Definition cf_priv.h:94
int depth
Definition cf_priv.h:119
char const * value
Attribute value.
Definition cf_priv.h:81
cf_unlang_t unlang
Definition cf_priv.h:120
char const * name1
First name token. Given foo bar {} would be foo.
Definition cf_priv.h:109
void const * data
User data.
Definition cf_priv.h:201
static bool cf_item_has_no_children(CONF_ITEM const *ci)
Check if the CONF_ITEM has no children.
Definition cf_priv.h:240
fr_dlist_head_t children
The head of the ordered list of children.
Definition cf_priv.h:59
fr_token_t * argv_quote
Definition cf_priv.h:116
fr_token_t op
Operator e.g. =, :=.
Definition cf_priv.h:83
CONF_ITEM item
Common set of fields.
Definition cf_priv.h:78
fr_rb_node_t node
Definition cf_priv.h:206
bool pass2
do expansion in pass2.
Definition cf_priv.h:87
char const * filename
The file the config item was parsed from.
Definition cf_priv.h:71
fr_rb_tree_t * ident1
Tree to store the first identifier (name1 || type || attr).
Definition cf_priv.h:63
struct stat buf
stat about the file
Definition cf_priv.h:209
bool at_reference
this thing was created from an ...
Definition cf_priv.h:122
bool _cf_expand_variables(void)
Definition cf_util.c:816
bool is_talloced
If true we can do extra checks.
Definition cf_priv.h:202
bool _cf_preserve_comments(void)
Definition cf_util.c:796
char const * filename
name of the file
Definition cf_priv.h:207
fr_rb_node_t ident1_node
Entry in the ident1 tree.
Definition cf_priv.h:55
char const * text
Comment text, with the leading # and any surrounding whitespace stripped.
Definition cf_priv.h:188
fr_dlist_t entry
Entry in dlist.
Definition cf_priv.h:58
enum conf_type CONF_ITEM_TYPE
char const ** argv
additional arguments
Definition cf_priv.h:115
CONF_ITEM item
Common set of fields.
Definition cf_priv.h:186
char const * type
C type of data being stored.
Definition cf_priv.h:198
CONF_ITEM item
Common set of fields.
Definition cf_priv.h:196
fr_token_t lhs_quote
Name quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
Definition cf_priv.h:84
int lineno
The line number the config item began on.
Definition cf_priv.h:70
CONF_ITEM_TYPE type
Whether the config item is a config_pair, conf_section or cf_data.
Definition cf_priv.h:66
A # ... comment line preserved verbatim from the input.
Definition cf_priv.h:185
Internal data that is associated with a configuration section.
Definition cf_priv.h:195
Common header for all CONF_* types.
Definition cf_priv.h:54
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:77
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
static bool fr_dlist_empty(fr_dlist_head_t const *list_head)
Check whether a list has any items.
Definition dlist.h:483
Head of a doubly linked list.
Definition dlist.h:51
Entry in a doubly linked list.
Definition dlist.h:41
unsigned char uint8_t
The main red black tree structure.
Definition rb.h:71
enum fr_token fr_token_t
int nonnull(2, 5))