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
175
177
179
180int dict_dependent_add(fr_dict_t *dict, char const *dependent);
181
182int dict_dependent_remove(fr_dict_t *dict, char const *dependent);
183
184fr_dict_t *dict_alloc(TALLOC_CTX *ctx);
185
186int dict_dlopen(fr_dict_t *dict, char const *name);
187
188/** Optional arguments for initialising/allocating attributes
189 *
190 */
191typedef struct {
192 fr_dict_attr_flags_t const *flags; //!< Any flags to assign to the attribute.
193
194 fr_dict_attr_t const *ref; //!< This attribute is a reference to another attribute.
196
197/** Partial initialisation functions
198 *
199 * These functions are used to initialise attributes in stages, i.e. when parsing a dictionary.
200 *
201 * The finalise function must be called to complete the initialisation.
202 *
203 * All functions must be called to fully initialise a dictionary attribute, except
204 * #dict_attr_parent_init this is not necessary for root attributes.
205 *
206 * @{
207 */
208fr_dict_attr_t *dict_attr_alloc_null(TALLOC_CTX *ctx, fr_dict_protocol_t const *dict);
209
211
213
214int dict_attr_num_init(fr_dict_attr_t *da, unsigned int num);
215
217
218void dict_attr_location_init(fr_dict_attr_t *da, char const *filename, int line);
219
220int dict_attr_finalise(fr_dict_attr_t **da_p, char const *name);
221/** @} */
222
223/** Full initialisation functions
224 *
225 * These functions either initialise, or allocate and then initialise a
226 * complete dictionary attribute.
227 *
228 * The output of these functions can be added into a dictionary immediately
229 * @{
230 */
231#define dict_attr_init(_da_p, _parent, _name, _attr, _type, _args) \
232 _dict_attr_init(__FILE__, __LINE__, _da_p, _parent, _name, _attr, _type, _args)
233
234int _dict_attr_init(char const *filename, int line,
235 fr_dict_attr_t **da_p, fr_dict_attr_t const *parent,
236 char const *name, unsigned int attr,
237 fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(1));
238
239#define dict_attr_init_name_only(_da_p, _parent, _name, _type, _args) \
240 _dict_attr_init_name_only(__FILE__, __LINE__, _da_p, _parent, _name, _type, _args)
241
242int _dict_attr_init_name_only(char const *filename, int line,
243 fr_dict_attr_t **da_p, fr_dict_attr_t const *parent,
244 char const *name,
245 fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(1));
246
247#define dict_attr_alloc_root(_ctx, _dict, _name, _attr, _args) \
248 _dict_attr_alloc_root(__FILE__, __LINE__, _ctx, _dict, _name, _attr, _args)
249fr_dict_attr_t *_dict_attr_alloc_root(char const *filename, int line,
250 TALLOC_CTX *ctx,
251 fr_dict_t const *dict,
252 char const *name, int attr,
253 dict_attr_args_t const *args) CC_HINT(nonnull(4,5));
254
255#define dict_attr_alloc(_ctx, _parent, _name, _attr, _type, _args) \
256 _dict_attr_alloc(__FILE__, __LINE__, _ctx, _parent, _name, _attr, _type, (_args))
257fr_dict_attr_t *_dict_attr_alloc(char const *filename, int line,
258 TALLOC_CTX *ctx,
259 fr_dict_attr_t const *parent,
260 char const *name, int attr,
261 fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(4));
262/** @} */
263
264fr_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);
265
267
269
271
272int dict_attr_alias_add(fr_dict_attr_t const *parent, char const *alias, fr_dict_attr_t const *ref);
273
275
277
278int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num);
279
281
282bool dict_attr_flags_valid(fr_dict_attr_t *da) CC_HINT(nonnull(1));
283
285
287
289
291 fr_dict_t **out, fr_sbuff_t *name, fr_dict_t const *dict_def);
292
294
295fr_dict_t *dict_by_protocol_num(unsigned int num);
296
298
300
302 bool coerce, bool replace, fr_dict_attr_t const *child_struct);
303
304#ifdef __cplusplus
305}
306#endif
va_list args
Definition acutest.h:772
int const char int line
Definition acutest.h:704
static fr_slen_t err
Definition dict.h:887
fr_dict_attr_err_t
Errors returned by attribute lookup functions.
Definition dict.h:318
static fr_slen_t in
Definition dict.h:887
Values of the encryption flags.
Protocol-specific callbacks in libfreeradius-PROTOCOL.
Definition dict.h:456
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:2719
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:2057
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:588
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:673
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:4217
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:1692
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:785
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:2840
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:1816
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:3585
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:3521
fr_dict_attr_flags_t const * flags
Any flags to assign to the attribute.
Definition dict_priv.h:192
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:2826
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:1717
int dict_dependent_remove(fr_dict_t *dict, char const *dependent)
Decrement ref count for a dependent in a dictionary.
Definition dict_util.c:4051
int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num)
Add a vendor to the dictionary.
Definition dict_util.c:1622
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
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:1445
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:799
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:978
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:1071
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:755
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:773
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:3900
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
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:104
fr_dict_t * dict_by_protocol_name(char const *name)
Internal version of fr_dict_by_protocol_name.
Definition dict_util.c:2812
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:1232
fr_dict_protocol_id_t
Definition dict_priv.h:160
@ FR_DICT_PROTO_LDAP
Definition dict_priv.h:172
@ FR_DICT_PROTO_DNS
Definition dict_priv.h:171
@ FR_DICT_PROTO_RADIUS
Definition dict_priv.h:161
@ FR_DICT_PROTO_SNMP
Definition dict_priv.h:167
@ FR_DICT_PROTO_DHCPv4
Definition dict_priv.h:162
@ FR_DICT_PROTO_DHCPv6
Definition dict_priv.h:163
@ FR_DICT_PROTO_TACACS
Definition dict_priv.h:165
@ FR_DICT_PROTO_TLS
Definition dict_priv.h:170
@ FR_DICT_PROTO_VMPS
Definition dict_priv.h:166
@ FR_DICT_PROTO_ARP
Definition dict_priv.h:168
@ FR_DICT_PROTO_ETHERNET
Definition dict_priv.h:164
@ FR_DICT_PROTO_TFTP
Definition dict_priv.h:169
@ FR_DICT_PROTO_BFD
Definition dict_priv.h:173
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:1030
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:1105
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:3992
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:194
int dict_protocol_add(fr_dict_t *dict)
Add a protocol to the global protocol table.
Definition dict_util.c:1549
bool dict_has_dependents(fr_dict_t *dict)
Check if a dictionary still has dependents.
Definition dict_util.c:4083
fr_dict_gctx_t * dict_gctx
Top level structure containing global dictionary state.
Definition dict_util.c:42
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:1329
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:1134
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:931
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:191
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:857
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1023