The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
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
131 fr_hash_table_t *autoref; //!< other dictionaries that we loaded via references
132
133 fr_dict_t const *next; //!< for attribute overloading
134
135 unsigned int vsa_parent; //!< varies with different protocols.
136
137 fr_dict_attr_t **fixups; //!< Attributes that need fixing up.
138
139 fr_rb_tree_t *dependents; //!< Which files are using this dictionary.
140
141 dl_t *dl; //!< for validation
142
143 fr_dict_protocol_t const *proto; //!< protocol-specific validation functions
144};
145
147 bool free_at_exit; //!< This gctx will be freed on exit.
148
149 bool perm_check; //!< Whether we should check dictionary
150 ///< file permissions as they're loaded.
151
153
154 char *dict_dir_default; //!< The default location for loading dictionaries if one
155 ///< wasn't provided.
156
157 dl_loader_t *dict_loader; //!< for protocol validation
158
159 fr_hash_table_t *protocol_by_name; //!< Hash containing names of all the
160 ///< registered protocols.
161 fr_hash_table_t *protocol_by_num; //!< Hash containing numbers of all the
162 ///< registered protocols.
163
164 /** Magic internal dictionary
165 *
166 * Internal dictionary is checked in addition to the protocol dictionary
167 * when resolving attribute names.
168 *
169 * This is because internal attributes are valid for every
170 * protocol.
171 */
173
175};
176
193
195
197
198int dict_dependent_add(fr_dict_t *dict, char const *dependent);
199
200int dict_dependent_remove(fr_dict_t *dict, char const *dependent);
201
202fr_dict_t *dict_alloc(TALLOC_CTX *ctx);
203
204int dict_dlopen(fr_dict_t *dict, char const *name);
205
206/** Optional arguments for initialising/allocating attributes
207 *
208 */
209typedef struct {
210 fr_dict_attr_flags_t const *flags; //!< Any flags to assign to the attribute.
211
212 fr_dict_attr_t const *ref; //!< This attribute is a reference to another attribute.
214
215/** Partial initialisation functions
216 *
217 * These functions are used to initialise attributes in stages, i.e. when parsing a dictionary.
218 *
219 * The finalise function must be called to complete the initialisation.
220 *
221 * All functions must be called to fully initialise a dictionary attribute, except
222 * #dict_attr_parent_init this is not necessary for root attributes.
223 *
224 * @{
225 */
226fr_dict_attr_t *dict_attr_alloc_null(TALLOC_CTX *ctx, fr_dict_protocol_t const *dict);
227
229
231
232int dict_attr_num_init(fr_dict_attr_t *da, unsigned int num);
233
235
236void dict_attr_location_init(fr_dict_t *dict, fr_dict_attr_t *da, char const *filename, int line);
237fr_dict_filename_t *dict_filename_intern(fr_dict_t *dict, char const *filename);
238
239int dict_attr_finalise(fr_dict_attr_t **da_p, char const *name);
240/** @} */
241
242/** Full initialisation functions
243 *
244 * These functions either initialise, or allocate and then initialise a
245 * complete dictionary attribute.
246 *
247 * The output of these functions can be added into a dictionary immediately
248 * @{
249 */
250#define dict_attr_init(_da_p, _parent, _name, _attr, _type, _args) \
251 _dict_attr_init(__FILE__, __LINE__, _da_p, _parent, _name, _attr, _type, _args)
252
253int _dict_attr_init(char const *filename, int line,
254 fr_dict_attr_t **da_p, fr_dict_attr_t const *parent,
255 char const *name, unsigned int attr,
256 fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(1));
257
258#define dict_attr_init_name_only(_da_p, _parent, _name, _type, _args) \
259 _dict_attr_init_name_only(__FILE__, __LINE__, _da_p, _parent, _name, _type, _args)
260
261int _dict_attr_init_name_only(char const *filename, int line,
262 fr_dict_attr_t **da_p, fr_dict_attr_t const *parent,
263 char const *name,
264 fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(1));
265
266#define dict_attr_alloc_root(_ctx, _dict, _name, _attr, _args) \
267 _dict_attr_alloc_root(__FILE__, __LINE__, _ctx, _dict, _name, _attr, _args)
268fr_dict_attr_t *_dict_attr_alloc_root(char const *filename, int line,
269 TALLOC_CTX *ctx,
270 fr_dict_t const *dict,
271 char const *name, int attr,
272 dict_attr_args_t const *args) CC_HINT(nonnull(4,5));
273
274#define dict_attr_alloc(_ctx, _parent, _name, _attr, _type, _args) \
275 _dict_attr_alloc(__FILE__, __LINE__, _ctx, _parent, _name, _attr, _type, (_args))
276fr_dict_attr_t *_dict_attr_alloc(char const *filename, int line,
277 TALLOC_CTX *ctx,
278 fr_dict_attr_t const *parent,
279 char const *name, int attr,
280 fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(4));
281/** @} */
282
283fr_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);
284
286
288
290
291int dict_attr_alias_add(fr_dict_attr_t const *parent, char const *alias, fr_dict_attr_t const *ref, bool from_public);
292
294
296
297int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num);
298
300
301bool dict_attr_flags_valid(fr_dict_attr_t *da) CC_HINT(nonnull(1));
302
304
306
308
310 fr_dict_t **out, fr_sbuff_t *name, fr_dict_t const *dict_def);
311
313
314fr_dict_t *dict_by_protocol_num(unsigned int num);
315
317
319
321 bool coerce, bool replace, fr_dict_attr_t const *child_struct);
322
323int fr_dict_afrom_file(fr_dict_t **out, char const *dir, char const *filename) CC_HINT(nonnull(1,3));
324
325#ifdef __cplusplus
326}
327#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:904
fr_dict_attr_err_t
Errors returned by attribute lookup functions.
Definition dict.h:341
static fr_slen_t in
Definition dict.h:904
Values of the encryption flags.
Protocol-specific callbacks in libfreeradius-PROTOCOL.
Definition dict.h:479
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:777
char * dict_dir_default
The default location for loading dictionaries if one wasn't provided.
Definition dict_priv.h:154
fr_hash_table_t * protocol_by_name
Hash containing names of all the registered protocols.
Definition dict_priv.h:159
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:2735
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:2076
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:526
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:611
fr_dict_t * dict_alloc(TALLOC_CTX *ctx)
Allocate a new dictionary.
Definition dict_util.c:4302
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:161
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:1707
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:2868
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:1831
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:3623
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:3557
fr_dict_attr_flags_t const * flags
Any flags to assign to the attribute.
Definition dict_priv.h:210
fr_dict_t * dict_by_protocol_num(unsigned int num)
Internal version of fr_dict_by_protocol_num.
Definition dict_util.c:2850
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:1732
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:729
int dict_dependent_remove(fr_dict_t *dict, char const *dependent)
Decrement ref count for a dependent in a dictionary.
Definition dict_util.c:4121
int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num)
Add a vendor to the dictionary.
Definition dict_util.c:1637
fr_dict_protocol_t const * proto
protocol-specific validation functions
Definition dict_priv.h:143
fr_rb_tree_t * dependents
Which files are using this dictionary.
Definition dict_priv.h:139
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:157
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:810
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:989
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:1082
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:693
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:711
unsigned int vsa_parent
varies with different protocols.
Definition dict_priv.h:135
int dict_dlopen(fr_dict_t *dict, char const *name)
Definition dict_util.c:3971
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:133
fr_dict_attr_t const * attr_protocol_encapsulation
Definition dict_priv.h:174
fr_hash_table_t * autoref
other dictionaries that we loaded via references
Definition dict_priv.h:131
int dict_attr_acopy_aliases(fr_dict_attr_t *dst, fr_dict_attr_t const *src)
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:2832
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:1243
fr_dict_protocol_id_t
Definition dict_priv.h:177
@ FR_DICT_PROTO_LDAP
Definition dict_priv.h:189
@ FR_DICT_PROTO_DNS
Definition dict_priv.h:188
@ FR_DICT_PROTO_RADIUS
Definition dict_priv.h:178
@ FR_DICT_PROTO_SNMP
Definition dict_priv.h:184
@ FR_DICT_PROTO_DHCPv4
Definition dict_priv.h:179
@ FR_DICT_PROTO_DHCPv6
Definition dict_priv.h:180
@ FR_DICT_PROTO_TACACS
Definition dict_priv.h:182
@ FR_DICT_PROTO_TLS
Definition dict_priv.h:187
@ FR_DICT_PROTO_CRL
Definition dict_priv.h:191
@ FR_DICT_PROTO_VMPS
Definition dict_priv.h:183
@ FR_DICT_PROTO_ARP
Definition dict_priv.h:185
@ FR_DICT_PROTO_ETHERNET
Definition dict_priv.h:181
@ FR_DICT_PROTO_TFTP
Definition dict_priv.h:186
@ FR_DICT_PROTO_BFD
Definition dict_priv.h:190
fr_dlist_head_t sources
Each load or $INCLUDE of the file.
Definition dict_priv.h:87
fr_dict_attr_t ** fixups
Attributes that need fixing up.
Definition dict_priv.h:137
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:1456
fr_dict_t * internal
Magic internal dictionary.
Definition dict_priv.h:172
fr_dict_attr_t * dict_attr_alloc_null(TALLOC_CTX *ctx, fr_dict_protocol_t const *dict)
Partial initialisation functions.
Definition dict_util.c:1041
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:1116
TALLOC_CTX * pool
Talloc memory pool to reduce allocs.
Definition dict_priv.h:128
dl_t * dl
for validation
Definition dict_priv.h:141
bool free_at_exit
This gctx will be freed on exit.
Definition dict_priv.h:147
int dict_dependent_add(fr_dict_t *dict, char const *dependent)
Record a new dependency on a dictionary.
Definition dict_util.c:4062
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:212
int dict_protocol_add(fr_dict_t *dict)
Add a protocol to the global protocol table.
Definition dict_util.c:1564
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:4153
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:1340
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:1145
bool perm_check
Whether we should check dictionary file permissions as they're loaded.
Definition dict_priv.h:149
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:942
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:209
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
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:860
static fr_sbuff_err_t char ** out
Definition value.h:1062
int nonnull(2, 5))