The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
dict_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/** Private Multi-protocol AVP dictionary API
19 *
20 * @file src/lib/util/dict_priv.h
21 *
22 * @copyright 2019 The FreeRADIUS server project
23 */
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#define _DICT_PRIVATE 1
29
30#include <freeradius-devel/protocol/base.h>
31#include <freeradius-devel/util/dict.h>
32#include <freeradius-devel/util/dict_ext_priv.h>
33#include <freeradius-devel/util/dl.h>
34#include <freeradius-devel/util/hash.h>
35#include <freeradius-devel/util/value.h>
36
37#define DICT_POOL_SIZE (1024 * 1024 * 2)
38#define DICT_FIXUP_POOL_SIZE (1024)
39
40/** Set the internal dictionary if none was provided
41 *
42 * @param _dict Dict pointer to check/set.
43 * @param _ret Value to return if no dictionaries are available.
44 */
45#define INTERNAL_IF_NULL(_dict, _ret) \
46 do { \
47 if (!(_dict)) { \
48 _dict = dict_gctx ? dict_gctx->internal : NULL; \
49 if (unlikely(!(_dict))) { \
50 fr_strerror_const("No dictionaries available for attribute resolution"); \
51 return (_ret); \
52 } \
53 } \
54 } while (0)
55
56/** Entry recording dictionary reference holders by file
57 */
58typedef struct {
60 int count; //!< How many references are held by this file.
61 ///< Signed to help figure out when things go wrong...
62 char const *dependent; //!< File holding the reference.
64
65/** Entry in the filename list of files associated with this dictionary
66 *
67 * Mainly used for debugging.
68 */
69typedef struct {
70 fr_dlist_t entry; //!< Entry in the list of filenames.
71
72 char const *src_file; //!< the source file which did the $INCLUDE
73 int src_line; //!< the line number in the source file
74 char *filename; //!< Name of the file the dictionary was loaded on.
76
77/** Vendors and attribute names
78 *
79 * It's very likely that the same vendors will operate in multiple
80 * protocol spaces, but number their attributes differently, so we need
81 * per protocol dictionaries.
82 *
83 * There would also be conflicts for DHCP(v6)/RADIUS attributes etc...
84 */
85struct fr_dict_s {
86 fr_dict_gctx_t *gctx; //!< Global dictionary context this dictionary
87 ///< was allocated in.
88
89 char const *dir; //!< where this protocol is located
90
91 fr_dlist_head_t filenames; //!< Files that this dictionary was loaded from.
92
93 bool read_only; //!< If true, disallow modifications.
94
95 bool in_protocol_by_name; //!< Whether the dictionary has been inserted into the
96 ///< protocol_by_name hash.
97 bool in_protocol_by_num; //!< Whether the dictionary has been inserted into the
98 //!< protocol_by_num table.
99
100 bool string_based; //!< TACACS, etc.
101
102 bool loading; //!< from fr_dict_protocol_afrom_file();
103
104 bool loaded; //!< from fr_dict_protocol_afrom_file();
105
106 fr_hash_table_t *vendors_by_name; //!< Lookup vendor by name.
107 fr_hash_table_t *vendors_by_num; //!< Lookup vendor by PEN.
108
109 fr_dict_attr_t *root; //!< Root attribute of this dictionary.
110
111 TALLOC_CTX *pool; //!< Talloc memory pool to reduce allocs.
112 ///< in the dictionary.
113
114 fr_hash_table_t *autoref; //!< other dictionaries that we loaded via references
115
116 fr_dict_t const *next; //!< for attribute overloading
117
118 unsigned int vsa_parent; //!< varies with different protocols.
119
120 fr_dict_attr_t **fixups; //!< Attributes that need fixing up.
121
122 fr_rb_tree_t *dependents; //!< Which files are using this dictionary.
123
124 dl_t *dl; //!< for validation
125
126 fr_dict_protocol_t const *proto; //!< protocol-specific validation functions
127};
128
130 bool free_at_exit; //!< This gctx will be freed on exit.
131
132 bool perm_check; //!< Whether we should check dictionary
133 ///< file permissions as they're loaded.
134
136
137 char *dict_dir_default; //!< The default location for loading dictionaries if one
138 ///< wasn't provided.
139
140 dl_loader_t *dict_loader; //!< for protocol validation
141
142 fr_hash_table_t *protocol_by_name; //!< Hash containing names of all the
143 ///< registered protocols.
144 fr_hash_table_t *protocol_by_num; //!< Hash containing numbers of all the
145 ///< registered protocols.
146
147 /** Magic internal dictionary
148 *
149 * Internal dictionary is checked in addition to the protocol dictionary
150 * when resolving attribute names.
151 *
152 * This is because internal attributes are valid for every
153 * protocol.
154 */
156
158};
159
161
163
164int dict_dependent_add(fr_dict_t *dict, char const *dependent);
165
166int dict_dependent_remove(fr_dict_t *dict, char const *dependent);
167
168fr_dict_t *dict_alloc(TALLOC_CTX *ctx);
169
170int dict_dlopen(fr_dict_t *dict, char const *name);
171
172/** Optional arguments for initialising/allocating attributes
173 *
174 */
175typedef struct {
176 fr_dict_attr_flags_t const *flags; //!< Any flags to assign to the attribute.
177
178 fr_dict_attr_t const *ref; //!< This attribute is a reference to another attribute.
180
181/** Partial initialisation functions
182 *
183 * These functions are used to initialise attributes in stages, i.e. when parsing a dictionary.
184 *
185 * The finalise function must be called to complete the initialisation.
186 *
187 * All functions must be called to fully initialise a dictionary attribute, except
188 * #dict_attr_parent_init this is not necessary for root attributes.
189 *
190 * @{
191 */
192fr_dict_attr_t *dict_attr_alloc_null(TALLOC_CTX *ctx, fr_dict_protocol_t const *dict);
193
195
197
198int dict_attr_num_init(fr_dict_attr_t *da, unsigned int num);
199
201
202void dict_attr_location_init(fr_dict_attr_t *da, char const *filename, int line);
203
204int dict_attr_finalise(fr_dict_attr_t **da_p, char const *name);
205/** @} */
206
207/** Full initialisation functions
208 *
209 * These functions either initialise, or allocate and then initialise a
210 * complete dictionary attribute.
211 *
212 * The output of these functions can be added into a dictionary immediately
213 * @{
214 */
215#define dict_attr_init(_da_p, _parent, _name, _attr, _type, _args) \
216 _dict_attr_init(__FILE__, __LINE__, _da_p, _parent, _name, _attr, _type, _args)
217
218int _dict_attr_init(char const *filename, int line,
219 fr_dict_attr_t **da_p, fr_dict_attr_t const *parent,
220 char const *name, unsigned int attr,
221 fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(1));
222
223#define dict_attr_init_name_only(_da_p, _parent, _name, _type, _args) \
224 _dict_attr_init_name_only(__FILE__, __LINE__, _da_p, _parent, _name, _type, _args)
225
226int _dict_attr_init_name_only(char const *filename, int line,
227 fr_dict_attr_t **da_p, fr_dict_attr_t const *parent,
228 char const *name,
229 fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(1));
230
231#define dict_attr_alloc_root(_ctx, _dict, _name, _attr, _args) \
232 _dict_attr_alloc_root(__FILE__, __LINE__, _ctx, _dict, _name, _attr, _args)
233fr_dict_attr_t *_dict_attr_alloc_root(char const *filename, int line,
234 TALLOC_CTX *ctx,
235 fr_dict_t const *dict,
236 char const *name, int attr,
237 dict_attr_args_t const *args) CC_HINT(nonnull(4,5));
238
239#define dict_attr_alloc(_ctx, _parent, _name, _attr, _type, _args) \
240 _dict_attr_alloc(__FILE__, __LINE__, _ctx, _parent, _name, _attr, _type, (_args))
241fr_dict_attr_t *_dict_attr_alloc(char const *filename, int line,
242 TALLOC_CTX *ctx,
243 fr_dict_attr_t const *parent,
244 char const *name, int attr,
245 fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(4));
246/** @} */
247
248fr_dict_attr_t *dict_attr_acopy(TALLOC_CTX *ctx, fr_dict_attr_t const *in, char const *new_name);
249
251
253
254int dict_attr_alias_add(fr_dict_attr_t const *parent, char const *alias, fr_dict_attr_t const *ref);
255
257
259
260int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num);
261
263
264bool dict_attr_flags_valid(fr_dict_attr_t *da) CC_HINT(nonnull(1));
265
267
269
271
273 fr_dict_t **out, fr_sbuff_t *name, fr_dict_t const *dict_def);
274
276
277fr_dict_t *dict_by_protocol_num(unsigned int num);
278
280
282
284 bool coerce, bool replace, fr_dict_attr_t const *child_struct);
285
286#ifdef __cplusplus
287}
288#endif
va_list args
Definition acutest.h:770
int const char int line
Definition acutest.h:702
static fr_slen_t err
Definition dict.h:831
fr_dict_attr_err_t
Errors returned by attribute lookup functions.
Definition dict.h:294
static fr_slen_t in
Definition dict.h:831
Values of the encryption flags.
Protocol-specific callbacks in libfreeradius-PROTOCOL.
Definition dict.h:432
char * dict_dir_default
The default location for loading dictionaries if one wasn't provided.
Definition dict_priv.h:137
fr_hash_table_t * protocol_by_name
Hash containing names of all the registered protocols.
Definition dict_priv.h:142
fr_dlist_t entry
Entry in the list of filenames.
Definition dict_priv.h:70
fr_hash_table_t * vendors_by_name
Lookup vendor by name.
Definition dict_priv.h:106
fr_hash_table_t * vendors_by_num
Lookup vendor by PEN.
Definition dict_priv.h:107
bool in_protocol_by_num
Whether the dictionary has been inserted into the protocol_by_num table.
Definition dict_priv.h:97
fr_slen_t dict_by_protocol_substr(fr_dict_attr_err_t *err, fr_dict_t **out, fr_sbuff_t *name, fr_dict_t const *dict_def)
Definition dict_util.c:2422
int dict_attr_enum_add_name(fr_dict_attr_t *da, char const *name, fr_value_box_t const *value, bool coerce, bool replace, fr_dict_attr_t const *child_struct)
Definition dict_util.c:1759
int dict_attr_type_init(fr_dict_attr_t **da_p, fr_type_t type)
Initialise type specific fields within the dictionary attribute.
Definition dict_util.c:519
int dict_attr_parent_init(fr_dict_attr_t **da_p, fr_dict_attr_t const *parent)
Initialise fields which depend on a parent attribute.
Definition dict_util.c:613
fr_dlist_head_t filenames
Files that this dictionary was loaded from.
Definition dict_priv.h:91
fr_dict_t * dict_alloc(TALLOC_CTX *ctx)
Allocate a new dictionary.
Definition dict_util.c:3879
fr_dict_gctx_t * gctx
Global dictionary context this dictionary was allocated in.
Definition dict_priv.h:86
fr_hash_table_t * protocol_by_num
Hash containing numbers of all the registered protocols.
Definition dict_priv.h:144
bool dict_attr_can_have_children(fr_dict_attr_t const *da)
See if a fr_dict_attr_t can have children.
Definition dict_util.c:1398
fr_dict_attr_t * root
Root attribute of this dictionary.
Definition dict_priv.h:109
void dict_attr_location_init(fr_dict_attr_t *da, char const *filename, int line)
Set where the dictionary attribute was defined.
Definition dict_util.c:695
fr_rb_node_t node
Definition dict_priv.h:59
fr_dict_t * dict_by_da(fr_dict_attr_t const *da)
Internal version of fr_dict_by_da.
Definition dict_util.c:2543
bool string_based
TACACS, etc.
Definition dict_priv.h:100
bool in_protocol_by_name
Whether the dictionary has been inserted into the protocol_by_name hash.
Definition dict_priv.h:95
int dict_attr_add_to_namespace(fr_dict_attr_t const *parent, fr_dict_attr_t *da)
Add an attribute to the name table for an attribute.
Definition dict_util.c:1531
fr_dict_attr_t * dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Internal version of fr_dict_attr_child_by_num.
Definition dict_util.c:3288
bool read_only
If true, disallow modifications.
Definition dict_priv.h:93
fr_dict_attr_t * dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *name)
Definition dict_util.c:3224
fr_dict_attr_flags_t const * flags
Any flags to assign to the attribute.
Definition dict_priv.h:176
int src_line
the line number in the source file
Definition dict_priv.h:73
fr_dict_t * dict_by_protocol_num(unsigned int num)
Internal version of fr_dict_by_protocol_num.
Definition dict_util.c:2529
int dict_attr_child_add(fr_dict_attr_t *parent, fr_dict_attr_t *child)
Add a child to a parent.
Definition dict_util.c:1432
int dict_dependent_remove(fr_dict_t *dict, char const *dependent)
Decrement ref count for a dependent in a dictionary.
Definition dict_util.c:3713
int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num)
Add a vendor to the dictionary.
Definition dict_util.c:1316
char const * src_file
the source file which did the $INCLUDE
Definition dict_priv.h:72
fr_dict_protocol_t const * proto
protocol-specific validation functions
Definition dict_priv.h:126
fr_rb_tree_t * dependents
Which files are using this dictionary.
Definition dict_priv.h:122
fr_dict_attr_t * dict_attr_acopy(TALLOC_CTX *ctx, fr_dict_attr_t const *in, char const *new_name)
Copy a an existing attribute.
Definition dict_util.c:1021
dl_loader_t * dict_loader
for protocol validation
Definition dict_priv.h:140
int dict_attr_alias_add(fr_dict_attr_t const *parent, char const *alias, fr_dict_attr_t const *ref)
Add an alias to an existing attribute.
Definition dict_util.c:1181
int dict_attr_finalise(fr_dict_attr_t **da_p, char const *name)
Set remaining fields in a dictionary attribute before insertion.
Definition dict_util.c:709
int _dict_attr_init_name_only(char const *filename, int line, fr_dict_attr_t **da_p, fr_dict_attr_t const *parent, char const *name, fr_type_t type, dict_attr_args_t const *args))
Initialise fields in a dictionary attribute structure.
Definition dict_util.c:865
fr_dict_attr_t * _dict_attr_alloc_root(char const *filename, int line, TALLOC_CTX *ctx, fr_dict_t const *dict, char const *name, int attr, dict_attr_args_t const *args))
Allocate a dictionary root attribute on the heap.
Definition dict_util.c:958
char const * dependent
File holding the reference.
Definition dict_priv.h:62
bool dict_attr_flags_valid(fr_dict_attr_t *da))
Validate a set of flags.
int dict_attr_num_init(fr_dict_attr_t *da, unsigned int num)
Set the attribute number (if any)
Definition dict_util.c:665
bool dict_attr_valid(fr_dict_attr_t *da)
Validate a new attribute definition.
int dict_attr_num_init_name_only(fr_dict_attr_t *da)
Set the attribute number (if any)
Definition dict_util.c:683
unsigned int vsa_parent
varies with different protocols.
Definition dict_priv.h:118
int dict_dlopen(fr_dict_t *dict, char const *name)
Definition dict_util.c:3562
bool loading
from fr_dict_protocol_afrom_file();
Definition dict_priv.h:102
fr_dict_t const * next
for attribute overloading
Definition dict_priv.h:116
fr_dict_attr_t const * attr_protocol_encapsulation
Definition dict_priv.h:157
fr_hash_table_t * autoref
other dictionaries that we loaded via references
Definition dict_priv.h:114
bool loaded
from fr_dict_protocol_afrom_file();
Definition dict_priv.h:104
fr_dict_t * dict_by_protocol_name(char const *name)
Internal version of fr_dict_by_protocol_name.
Definition dict_util.c:2515
int dict_attr_acopy_children(fr_dict_t *dict, fr_dict_attr_t *dst, fr_dict_attr_t const *src)
Copy the children of an existing attribute.
Definition dict_util.c:1109
fr_dict_attr_t ** fixups
Attributes that need fixing up.
Definition dict_priv.h:120
fr_dict_t * internal
Magic internal dictionary.
Definition dict_priv.h:155
fr_dict_attr_t * dict_attr_alloc_null(TALLOC_CTX *ctx, fr_dict_protocol_t const *dict)
Partial initialisation functions.
Definition dict_util.c:917
fr_dict_attr_t * _dict_attr_alloc(char const *filename, int line, TALLOC_CTX *ctx, fr_dict_attr_t const *parent, char const *name, int attr, fr_type_t type, dict_attr_args_t const *args))
Allocate a dictionary attribute on the heap.
Definition dict_util.c:992
TALLOC_CTX * pool
Talloc memory pool to reduce allocs.
Definition dict_priv.h:111
dl_t * dl
for validation
Definition dict_priv.h:124
bool free_at_exit
This gctx will be freed on exit.
Definition dict_priv.h:130
int dict_dependent_add(fr_dict_t *dict, char const *dependent)
Record a new dependency on a dictionary.
Definition dict_util.c:3654
char * filename
Name of the file the dictionary was loaded on.
Definition dict_priv.h:74
fr_dict_attr_t const * ref
This attribute is a reference to another attribute.
Definition dict_priv.h:178
int dict_protocol_add(fr_dict_t *dict)
Add a protocol to the global protocol table.
Definition dict_util.c:1243
bool dict_has_dependents(fr_dict_t *dict)
Check if a dictionary still has dependents.
Definition dict_util.c:3745
fr_dict_gctx_t * dict_gctx
Top level structure containing global dictionary state.
Definition dict_util.c:46
int dict_attr_acopy_enumv(fr_dict_attr_t *dst, fr_dict_attr_t const *src)
Copy the VALUEs of an existing attribute, by casting them.
Definition dict_util.c:1152
bool perm_check
Whether we should check dictionary file permissions as they're loaded.
Definition dict_priv.h:132
int _dict_attr_init(char const *filename, int line, fr_dict_attr_t **da_p, fr_dict_attr_t const *parent, char const *name, unsigned int attr, fr_type_t type, dict_attr_args_t const *args))
Initialise fields in a dictionary attribute structure.
Definition dict_util.c:824
int count
How many references are held by this file.
Definition dict_priv.h:60
char const * dir
where this protocol is located
Definition dict_priv.h:89
Optional arguments for initialising/allocating attributes.
Definition dict_priv.h:175
Entry recording dictionary reference holders by file.
Definition dict_priv.h:58
Entry in the filename list of files associated with this dictionary.
Definition dict_priv.h:69
Vendors and attribute names.
Definition dict_priv.h:85
Test enumeration values.
Definition dict_test.h:92
A dynamic loader.
Definition dl.c:81
Module handle.
Definition dl.h:58
Head of a doubly linked list.
Definition dlist.h:51
Entry in a doubly linked list.
Definition dlist.h:41
fr_type_t
ssize_t fr_slen_t
The main red black tree structure.
Definition rb.h:73
static char const * name
fr_aka_sim_id_type_t type
static fr_slen_t parent
Definition pair.h:845
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1012