The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
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/** One load or $INCLUDE of a dictionary file
66 *
67 */
68typedef struct {
69 fr_dlist_t entry; //!< Entry in the file's list of sources.
70
71 char const *src_file; //!< File which did the $INCLUDE. NULL for a
72 ///< top-level load.
73 int src_line; //!< Line of the $INCLUDE in src_file.
75
76/** Entry in the table of files associated with this dictionary
77 *
78 * Mainly used for debugging.
79 */
80typedef struct {
81 uint16_t id; //!< Ordinal of this file within the dictionary.
82 ///< fr_dict_attr_t stores the id instead of a
83 ///< pointer, which keeps the attribute struct
84 ///< within one cache line. Id 0 is reserved for
85 ///< "no location recorded".
86 char *filename; //!< Name of the file the dictionary was loaded from.
87 fr_dlist_head_t sources; //!< Each load or $INCLUDE of the file.
89
90/** Vendors and attribute names
91 *
92 * It's very likely that the same vendors will operate in multiple
93 * protocol spaces, but number their attributes differently, so we need
94 * per protocol dictionaries.
95 *
96 * There would also be conflicts for DHCP(v6)/RADIUS attributes etc...
97 */
98struct fr_dict_s {
99 fr_dict_gctx_t *gctx; //!< Global dictionary context this dictionary
100 ///< was allocated in.
101
102 char const *dir; //!< where this protocol is located
103
104 fr_hash_table_t *filenames; //!< Files that this dictionary was loaded from,
105 ///< keyed by filename.
106
107 fr_dict_filename_t **filename_by_id; //!< Files indexed by id - 1, for resolving
108 ///< attribute definition locations.
109
110 bool read_only; //!< If true, disallow modifications.
111
112 bool in_protocol_by_name; //!< Whether the dictionary has been inserted into the
113 ///< protocol_by_name hash.
114 bool in_protocol_by_num; //!< Whether the dictionary has been inserted into the
115 //!< protocol_by_num table.
116
117 bool string_based; //!< TACACS, etc.
118
119 bool loading; //!< from fr_dict_protocol_afrom_file();
120
121 bool loaded; //!< from fr_dict_protocol_afrom_file();
122
123 fr_hash_table_t *vendors_by_name; //!< Lookup vendor by name.
124 fr_hash_table_t *vendors_by_num; //!< Lookup vendor by PEN.
125
126 fr_dict_attr_t *root; //!< Root attribute of this dictionary.
127
128 TALLOC_CTX *pool; //!< Talloc memory pool to reduce allocs.
129 ///< in the dictionary.
130 size_t pool_size; //!< Size of the talloc pool in bytes. talloc has no
131 ///< function that returns the size of a pool, so
132 ///< dict_alloc() records the size.
133
134 fr_hash_table_t *autoref; //!< other dictionaries that we loaded via references
135
136 fr_dict_t const *next; //!< for attribute overloading
137
138 unsigned int vsa_parent; //!< varies with different protocols.
139
140 fr_dict_attr_t **fixups; //!< Attributes that need fixing up.
141
142 fr_rb_tree_t *dependents; //!< Which files are using this dictionary.
143
144 dl_t *dl; //!< for validation
145
146 fr_dict_protocol_t const *proto; //!< protocol-specific validation functions
147};
148
150 bool free_at_exit; //!< This gctx will be freed on exit.
151
152 bool perm_check; //!< Whether we should check dictionary
153 ///< file permissions as they're loaded.
154
156
157 char *dict_dir_default; //!< The default location for loading dictionaries if one
158 ///< wasn't provided.
159
160 size_t pool_size_min; //!< Smallest pool, in bytes, that dict_alloc() creates.
161 ///< dict_alloc() uses pool_size_min in place of any
162 ///< smaller pool= on a PROTOCOL line.
163
164 dl_loader_t *dict_loader; //!< for protocol validation
165
166 fr_hash_table_t *protocol_by_name; //!< Hash containing names of all the
167 ///< registered protocols.
168 fr_hash_table_t *protocol_by_num; //!< Hash containing numbers of all the
169 ///< registered protocols.
170
171 /** Magic internal dictionary
172 *
173 * Internal dictionary is checked in addition to the protocol dictionary
174 * when resolving attribute names.
175 *
176 * This is because internal attributes are valid for every
177 * protocol.
178 */
180
182};
183
200
202
204
205int dict_dependent_add(fr_dict_t *dict, char const *dependent);
206
207int dict_dependent_remove(fr_dict_t *dict, char const *dependent);
208
209fr_dict_t *dict_alloc(TALLOC_CTX *ctx, size_t pool_size);
210
211int dict_dlopen(fr_dict_t *dict, char const *name);
212
213/** Optional arguments for initialising/allocating attributes
214 *
215 */
216typedef struct {
217 fr_dict_attr_flags_t const *flags; //!< Any flags to assign to the attribute.
218
219 fr_dict_attr_t const *ref; //!< This attribute is a reference to another attribute.
221
222/** Partial initialisation functions
223 *
224 * These functions are used to initialise attributes in stages, i.e. when parsing a dictionary.
225 *
226 * The finalise function must be called to complete the initialisation.
227 *
228 * All functions must be called to fully initialise a dictionary attribute, except
229 * #dict_attr_parent_init this is not necessary for root attributes.
230 *
231 * @{
232 */
233fr_dict_attr_t *dict_attr_alloc_null(TALLOC_CTX *ctx, fr_dict_protocol_t const *dict);
234
236
238
239int dict_attr_num_init(fr_dict_attr_t *da, unsigned int num);
240
242
243void dict_attr_location_init(fr_dict_t *dict, fr_dict_attr_t *da, char const *filename, int line);
244fr_dict_filename_t *dict_filename_intern(fr_dict_t *dict, char const *filename);
245
246int dict_attr_finalise(fr_dict_attr_t **da_p, char const *name);
247/** @} */
248
249/** Full initialisation functions
250 *
251 * These functions either initialise, or allocate and then initialise a
252 * complete dictionary attribute.
253 *
254 * The output of these functions can be added into a dictionary immediately
255 * @{
256 */
257#define dict_attr_init(_da_p, _parent, _name, _attr, _type, _args) \
258 _dict_attr_init(__FILE__, __LINE__, _da_p, _parent, _name, _attr, _type, _args)
259
260int _dict_attr_init(char const *filename, int line,
261 fr_dict_attr_t **da_p, fr_dict_attr_t const *parent,
262 char const *name, unsigned int attr,
263 fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(1));
264
265#define dict_attr_init_name_only(_da_p, _parent, _name, _type, _args) \
266 _dict_attr_init_name_only(__FILE__, __LINE__, _da_p, _parent, _name, _type, _args)
267
268int _dict_attr_init_name_only(char const *filename, int line,
269 fr_dict_attr_t **da_p, fr_dict_attr_t const *parent,
270 char const *name,
271 fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(1));
272
273#define dict_attr_alloc_root(_ctx, _dict, _name, _attr, _args) \
274 _dict_attr_alloc_root(__FILE__, __LINE__, _ctx, _dict, _name, _attr, _args)
275fr_dict_attr_t *_dict_attr_alloc_root(char const *filename, int line,
276 TALLOC_CTX *ctx,
277 fr_dict_t const *dict,
278 char const *name, int attr,
279 dict_attr_args_t const *args) CC_HINT(nonnull(4,5));
280
281#define dict_attr_alloc(_ctx, _parent, _name, _attr, _type, _args) \
282 _dict_attr_alloc(__FILE__, __LINE__, _ctx, _parent, _name, _attr, _type, (_args))
283fr_dict_attr_t *_dict_attr_alloc(char const *filename, int line,
284 TALLOC_CTX *ctx,
285 fr_dict_attr_t const *parent,
286 char const *name, int attr,
287 fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(4));
288/** @} */
289
290fr_dict_attr_t *dict_attr_acopy(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, fr_dict_attr_t const *in, char const *new_name);
291
293
295
297
298int dict_attr_alias_add(fr_dict_attr_t const *parent, char const *alias, fr_dict_attr_t const *ref, bool from_public);
299
301
303
304int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num);
305
307
308bool dict_attr_flags_valid(fr_dict_attr_t *da) CC_HINT(nonnull(1));
309
311
313
315
317 fr_dict_t **out, fr_sbuff_t *name, fr_dict_t const *dict_def);
318
320
321fr_dict_t *dict_by_protocol_num(unsigned int num);
322
324
326
328 bool coerce, bool replace, fr_dict_attr_t const *child_struct);
329
330int fr_dict_afrom_file(fr_dict_t **out, char const *dir, char const *filename) CC_HINT(nonnull(1,3));
331
332#ifdef __cplusplus
333}
334#endif
va_list args
Definition acutest.h:923
void int line
Definition acutest.h:810
char const char const * dependent
Definition dict.h:912
fr_dict_attr_err_t
Errors returned by attribute lookup functions.
Definition dict.h:341
Values of the encryption flags.
Protocol-specific callbacks in libfreeradius-PROTOCOL.
Definition dict.h:479
size_t pool_size_min
Smallest pool, in bytes, that dict_alloc() creates.
Definition dict_priv.h:160
void dict_attr_location_init(fr_dict_t *dict, fr_dict_attr_t *da, char const *filename, int line)
Set where the dictionary attribute was defined.
Definition dict_util.c:774
char * dict_dir_default
The default location for loading dictionaries if one wasn't provided.
Definition dict_priv.h:157
fr_hash_table_t * protocol_by_name
Hash containing names of all the registered protocols.
Definition dict_priv.h:166
char const * src_file
File which did the $INCLUDE.
Definition dict_priv.h:71
fr_hash_table_t * vendors_by_name
Lookup vendor by name.
Definition dict_priv.h:123
fr_hash_table_t * vendors_by_num
Lookup vendor by PEN.
Definition dict_priv.h:124
uint16_t id
Ordinal of this file within the dictionary.
Definition dict_priv.h:81
bool in_protocol_by_num
Whether the dictionary has been inserted into the protocol_by_num table.
Definition dict_priv.h:114
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:2732
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:2073
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:523
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:608
fr_dict_gctx_t * gctx
Global dictionary context this dictionary was allocated in.
Definition dict_priv.h:99
fr_hash_table_t * protocol_by_num
Hash containing numbers of all the registered protocols.
Definition dict_priv.h:168
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:1704
fr_dict_attr_t * root
Root attribute of this dictionary.
Definition dict_priv.h:126
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:2865
int fr_dict_afrom_file(fr_dict_t **out, char const *dir, char const *filename))
Load one dictionary file.
int src_line
Line of the $INCLUDE in src_file.
Definition dict_priv.h:73
bool string_based
TACACS, etc.
Definition dict_priv.h:117
bool in_protocol_by_name
Whether the dictionary has been inserted into the protocol_by_name hash.
Definition dict_priv.h:112
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:1828
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:3620
bool read_only
If true, disallow modifications.
Definition dict_priv.h:110
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:3554
fr_dict_protocol_id_t
Definition dict_priv.h:184
@ FR_DICT_PROTO_LDAP
Definition dict_priv.h:196
@ FR_DICT_PROTO_DNS
Definition dict_priv.h:195
@ FR_DICT_PROTO_RADIUS
Definition dict_priv.h:185
@ FR_DICT_PROTO_SNMP
Definition dict_priv.h:191
@ FR_DICT_PROTO_DHCPv4
Definition dict_priv.h:186
@ FR_DICT_PROTO_DHCPv6
Definition dict_priv.h:187
@ FR_DICT_PROTO_TACACS
Definition dict_priv.h:189
@ FR_DICT_PROTO_TLS
Definition dict_priv.h:194
@ FR_DICT_PROTO_CRL
Definition dict_priv.h:198
@ FR_DICT_PROTO_VMPS
Definition dict_priv.h:190
@ FR_DICT_PROTO_ARP
Definition dict_priv.h:192
@ FR_DICT_PROTO_ETHERNET
Definition dict_priv.h:188
@ FR_DICT_PROTO_TFTP
Definition dict_priv.h:193
@ FR_DICT_PROTO_BFD
Definition dict_priv.h:197
fr_dict_attr_flags_t const * flags
Any flags to assign to the attribute.
Definition dict_priv.h:217
fr_dict_t * dict_by_protocol_num(unsigned int num)
Internal version of fr_dict_by_protocol_num.
Definition dict_util.c:2847
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:1729
fr_dict_filename_t ** filename_by_id
Files indexed by id - 1, for resolving attribute definition locations.
Definition dict_priv.h:107
fr_dict_filename_t * dict_filename_intern(fr_dict_t *dict, char const *filename)
Return the table entry for a filename, creating the entry if the filename is not in the table.
Definition dict_util.c:726
int dict_dependent_remove(fr_dict_t *dict, char const *dependent)
Decrement ref count for a dependent in a dictionary.
Definition dict_util.c:4118
int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num)
Add a vendor to the dictionary.
Definition dict_util.c:1634
fr_dict_protocol_t const * proto
protocol-specific validation functions
Definition dict_priv.h:146
fr_rb_tree_t * dependents
Which files are using this dictionary.
Definition dict_priv.h:142
fr_hash_table_t * filenames
Files that this dictionary was loaded from, keyed by filename.
Definition dict_priv.h:104
dl_loader_t * dict_loader
for protocol validation
Definition dict_priv.h:164
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:807
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:986
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:1079
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:690
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:708
unsigned int vsa_parent
varies with different protocols.
Definition dict_priv.h:138
int dict_dlopen(fr_dict_t *dict, char const *name)
Definition dict_util.c:3968
bool loading
from fr_dict_protocol_afrom_file();
Definition dict_priv.h:119
fr_dict_t const * next
for attribute overloading
Definition dict_priv.h:136
fr_dict_attr_t const * attr_protocol_encapsulation
Definition dict_priv.h:181
fr_hash_table_t * autoref
other dictionaries that we loaded via references
Definition dict_priv.h:134
int dict_attr_acopy_aliases(fr_dict_attr_t *dst, fr_dict_attr_t const *src)
Copy aliases of an existing attribute to a new one.
Definition dict_util.c:1372
bool loaded
from fr_dict_protocol_afrom_file();
Definition dict_priv.h:121
fr_dict_t * dict_by_protocol_name(char const *name)
Internal version of fr_dict_by_protocol_name.
Definition dict_util.c:2829
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:1240
fr_dlist_head_t sources
Each load or $INCLUDE of the file.
Definition dict_priv.h:87
fr_dict_t * dict_alloc(TALLOC_CTX *ctx, size_t pool_size)
Allocate a new dictionary.
Definition dict_util.c:4300
fr_dict_attr_t ** fixups
Attributes that need fixing up.
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, bool from_public)
Add an alias to an existing attribute.
Definition dict_util.c:1453
fr_dict_t * internal
Magic internal dictionary.
Definition dict_priv.h:179
fr_dict_attr_t * dict_attr_alloc_null(TALLOC_CTX *ctx, fr_dict_protocol_t const *dict)
Partial initialisation functions.
Definition dict_util.c:1038
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:1113
TALLOC_CTX * pool
Talloc memory pool to reduce allocs.
Definition dict_priv.h:128
dl_t * dl
for validation
Definition dict_priv.h:144
bool free_at_exit
This gctx will be freed on exit.
Definition dict_priv.h:150
int dict_dependent_add(fr_dict_t *dict, char const *dependent)
Record a new dependency on a dictionary.
Definition dict_util.c:4059
char * filename
Name of the file the dictionary was loaded from.
Definition dict_priv.h:86
fr_dict_attr_t const * ref
This attribute is a reference to another attribute.
Definition dict_priv.h:219
size_t pool_size
Size of the talloc pool in bytes.
Definition dict_priv.h:130
int dict_protocol_add(fr_dict_t *dict)
Add a protocol to the global protocol table.
Definition dict_util.c:1561
fr_dlist_t entry
Entry in the file's list of sources.
Definition dict_priv.h:69
bool dict_has_dependents(fr_dict_t *dict)
Check if a dictionary still has dependents.
Definition dict_util.c:4150
fr_dict_gctx_t * dict_gctx
Top level structure containing global dictionary state.
Definition dict_util.c:41
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:1337
fr_dict_attr_t * dict_attr_acopy(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, fr_dict_attr_t const *in, char const *new_name)
Copy a an existing attribute, possibly to a new location.
Definition dict_util.c:1142
bool perm_check
Whether we should check dictionary file permissions as they're loaded.
Definition dict_priv.h:152
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:939
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:102
Optional arguments for initialising/allocating attributes.
Definition dict_priv.h:216
Entry recording dictionary reference holders by file.
Definition dict_priv.h:58
One load or $INCLUDE of a dictionary file.
Definition dict_priv.h:68
Entry in the table of files associated with this dictionary.
Definition dict_priv.h:80
Vendors and attribute names.
Definition dict_priv.h:98
Test enumeration values.
Definition dict_test.h:92
A dynamic loader.
Definition dl.c:81
Module handle.
Definition dl.h:57
Head of a doubly linked list.
Definition dlist.h:51
Entry in a doubly linked list.
Definition dlist.h:41
unsigned short uint16_t
fr_type_t
unsigned char uint8_t
ssize_t fr_slen_t
The main red black tree structure.
Definition rb.h:71
static char const * name
fr_aka_sim_id_type_t type
static fr_slen_t parent
Definition pair.h:864
static fr_sbuff_err_t char size_t fr_sbuff_t * in
Definition value.h:1061
static fr_sbuff_err_t char ** out
Definition value.h:1061
int nonnull(2, 5))