The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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
25 extern "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  */
58 typedef 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  */
69 typedef struct {
70  fr_dlist_t entry; //!< Entry in the list of filenames.
71 
72  char *filename; //!< Name of the file the dictionary was loaded on.
74 
75 /** Vendors and attribute names
76  *
77  * It's very likely that the same vendors will operate in multiple
78  * protocol spaces, but number their attributes differently, so we need
79  * per protocol dictionaries.
80  *
81  * There would also be conflicts for DHCP(v6)/RADIUS attributes etc...
82  */
83 struct fr_dict_s {
84  fr_dict_gctx_t *gctx; //!< Global dictionary context this dictionary
85  ///< was allocated in.
86 
87  fr_dlist_head_t filenames; //!< Files that this dictionary was loaded from.
88 
89  bool read_only; //!< If true, disallow modifications.
90 
91  bool in_protocol_by_name; //!< Whether the dictionary has been inserted into the
92  ///< protocol_by_name hash.
93  bool in_protocol_by_num; //!< Whether the dictionary has been inserted into the
94  //!< protocol_by_num table.
95 
96  bool string_based; //!< TACACS, etc.
97 
98  bool loading; //!< from fr_dict_protocol_afrom_file();
99 
100  bool loaded; //!< from fr_dict_protocol_afrom_file();
101 
102  fr_hash_table_t *vendors_by_name; //!< Lookup vendor by name.
103  fr_hash_table_t *vendors_by_num; //!< Lookup vendor by PEN.
104 
105  fr_dict_attr_t *root; //!< Root attribute of this dictionary.
106 
107  TALLOC_CTX *pool; //!< Talloc memory pool to reduce allocs.
108  ///< in the dictionary.
109 
110  fr_hash_table_t *autoref; //!< other dictionaries that we loaded via references
111 
112  fr_dict_t const *next; //!< for attribute overloading
113 
114  unsigned int vsa_parent; //!< varies with different protocols.
115 
116  fr_dict_attr_t **fixups; //!< Attributes that need fixing up.
117 
118  fr_rb_tree_t *dependents; //!< Which files are using this dictionary.
119 
120  dl_t *dl; //!< for validation
121 
122  fr_dict_protocol_t const *proto; //!< protocol-specific validation functions
123 };
124 
126  bool free_at_exit; //!< This gctx will be freed on exit.
127 
128  bool perm_check; //!< Whether we should check dictionary
129  ///< file permissions as they're loaded.
130 
131  bool read_only;
132 
133  char *dict_dir_default; //!< The default location for loading dictionaries if one
134  ///< wasn't provided.
135 
136  dl_loader_t *dict_loader; //!< for protocol validation
137 
138  fr_hash_table_t *protocol_by_name; //!< Hash containing names of all the
139  ///< registered protocols.
140  fr_hash_table_t *protocol_by_num; //!< Hash containing numbers of all the
141  ///< registered protocols.
142 
143  /** Magic internal dictionary
144  *
145  * Internal dictionary is checked in addition to the protocol dictionary
146  * when resolving attribute names.
147  *
148  * This is because internal attributes are valid for every
149  * protocol.
150  */
151  fr_dict_t *internal;
152 
154 };
155 
156 extern fr_dict_gctx_t *dict_gctx;
157 
159 
160 int dict_dependent_add(fr_dict_t *dict, char const *dependent);
161 
162 int dict_dependent_remove(fr_dict_t *dict, char const *dependent);
163 
164 fr_dict_t *dict_alloc(TALLOC_CTX *ctx);
165 
166 int dict_dlopen(fr_dict_t *dict, char const *name);
167 
168 /** Optional arguments for initialising/allocating attributes
169  *
170  */
171 typedef struct {
172  fr_dict_attr_flags_t const *flags; //!< Any flags to assign to the attribute.
173 
174  fr_dict_attr_t const *ref; //!< This attribute is a reference to another attribute.
176 
177 /** Partial initialisation functions
178  *
179  * These functions are used to initialise attributes in stages, i.e. when parsing a dictionary.
180  *
181  * The finalise function must be called to complete the initialisation.
182  *
183  * All functions must be called to fully initialise a dictionary attribute, except
184  * #dict_attr_parent_init this is not necessary for root attributes.
185  *
186  * @{
187  */
189 
191 
193 
194 int dict_attr_num_init(fr_dict_attr_t *da, unsigned int num);
195 
197 
198 void dict_attr_location_init(fr_dict_attr_t *da, char const *filename, int line);
199 
200 int dict_attr_finalise(fr_dict_attr_t **da_p, char const *name);
201 /** @} */
202 
203 /** Full initialisation functions
204  *
205  * These functions either initialise, or allocate and then initialise a
206  * complete dictionary attribute.
207  *
208  * The output of these functions can be added into a dictionary immediately
209  * @{
210  */
211 #define dict_attr_init(_da_p, _parent, _name, _attr, _type, _args) \
212  _dict_attr_init(__FILE__, __LINE__, _da_p, _parent, _name, _attr, _type, _args)
213 
214 int _dict_attr_init(char const *filename, int line,
215  fr_dict_attr_t **da_p, fr_dict_attr_t const *parent,
216  char const *name, unsigned int attr,
217  fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(1));
218 
219 #define dict_attr_init_name_only(_da_p, _parent, _name, _type, _args) \
220  _dict_attr_init_name_only(__FILE__, __LINE__, _da_p, _parent, _name, _type, _args)
221 
222 int _dict_attr_init_name_only(char const *filename, int line,
223  fr_dict_attr_t **da_p, fr_dict_attr_t const *parent,
224  char const *name,
225  fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(1));
226 
227 #define dict_attr_alloc_root(_ctx, _dict, _name, _attr, _args) \
228  _dict_attr_alloc_root(__FILE__, __LINE__, _ctx, _dict, _name, _attr, _args)
229 fr_dict_attr_t *_dict_attr_alloc_root(char const *filename, int line,
230  TALLOC_CTX *ctx,
231  fr_dict_t const *dict,
232  char const *name, int attr,
233  dict_attr_args_t const *args) CC_HINT(nonnull(4,5));
234 
235 #define dict_attr_alloc(_ctx, _parent, _name, _attr, _type, _args) \
236  _dict_attr_alloc(__FILE__, __LINE__, _ctx, _parent, _name, _attr, _type, (_args))
237 fr_dict_attr_t *_dict_attr_alloc(char const *filename, int line,
238  TALLOC_CTX *ctx,
239  fr_dict_attr_t const *parent,
240  char const *name, int attr,
241  fr_type_t type, dict_attr_args_t const *args) CC_HINT(nonnull(4));
242 /** @} */
243 
244 fr_dict_attr_t *dict_attr_acopy(TALLOC_CTX *ctx, fr_dict_attr_t const *in, char const *new_name);
245 
247 
249 
250 int dict_attr_alias_add(fr_dict_attr_t const *parent, char const *alias, fr_dict_attr_t const *ref);
251 
253 
255 
256 int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num);
257 
259 
260 bool dict_attr_flags_valid(fr_dict_attr_t *da) CC_HINT(nonnull(1));
261 
263 
265 
266 fr_dict_attr_t *dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr);
267 
269  fr_dict_t **out, fr_sbuff_t *name, fr_dict_t const *dict_def);
270 
271 fr_dict_t *dict_by_protocol_name(char const *name);
272 
273 fr_dict_t *dict_by_protocol_num(unsigned int num);
274 
276 
278 
279 int dict_attr_enum_add_name(fr_dict_attr_t *da, char const *name, fr_value_box_t const *value,
280  bool coerce, bool replace, fr_dict_attr_t const *child_struct);
281 
282 #ifdef __cplusplus
283 }
284 #endif
va_list args
Definition: acutest.h:770
int const char int line
Definition: acutest.h:702
static fr_dict_t * dict
Definition: fuzzer.c:46
static fr_slen_t err
Definition: dict.h:821
fr_dict_attr_err_t
Errors returned by attribute lookup functions.
Definition: dict.h:289
static fr_slen_t in
Definition: dict.h:821
Values of the encryption flags.
Definition: merged_model.c:139
Protocol-specific callbacks in libfreeradius-PROTOCOL.
Definition: dict.h:427
char * dict_dir_default
The default location for loading dictionaries if one wasn't provided.
Definition: dict_priv.h:133
fr_hash_table_t * protocol_by_name
Hash containing names of all the registered protocols.
Definition: dict_priv.h:138
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:102
fr_hash_table_t * vendors_by_num
Lookup vendor by PEN.
Definition: dict_priv.h:103
fr_dict_t * dict_by_da(fr_dict_attr_t const *da)
Internal version of fr_dict_by_da.
Definition: dict_util.c:2536
bool in_protocol_by_num
Whether the dictionary has been inserted into the protocol_by_num table.
Definition: dict_priv.h:93
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:2415
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:1752
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:87
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:988
fr_dict_gctx_t * gctx
Global dictionary context this dictionary was allocated in.
Definition: dict_priv.h:84
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:1017
fr_hash_table_t * protocol_by_num
Hash containing numbers of all the registered protocols.
Definition: dict_priv.h:140
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:1391
fr_dict_attr_t * root
Root attribute of this dictionary.
Definition: dict_priv.h:105
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_protocol_num(unsigned int num)
Internal version of fr_dict_by_protocol_num.
Definition: dict_util.c:2522
bool string_based
TACACS, etc.
Definition: dict_priv.h:96
bool in_protocol_by_name
Whether the dictionary has been inserted into the protocol_by_name hash.
Definition: dict_priv.h:91
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:1524
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:956
bool read_only
If true, disallow modifications.
Definition: dict_priv.h:89
fr_dict_attr_flags_t const * flags
Any flags to assign to the attribute.
Definition: dict_priv.h:172
fr_dict_t * dict_by_protocol_name(char const *name)
Internal version of fr_dict_by_protocol_name.
Definition: dict_util.c:2508
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:1425
int dict_dependent_remove(fr_dict_t *dict, char const *dependent)
Decrement ref count for a dependent in a dictionary.
Definition: dict_util.c:3706
fr_dict_t * dict_alloc(TALLOC_CTX *ctx)
Allocate a new dictionary.
Definition: dict_util.c:3872
int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num)
Add a vendor to the dictionary.
Definition: dict_util.c:1309
fr_dict_protocol_t const * proto
protocol-specific validation functions
Definition: dict_priv.h:122
fr_rb_tree_t * dependents
Which files are using this dictionary.
Definition: dict_priv.h:118
dl_loader_t * dict_loader
for protocol validation
Definition: dict_priv.h:136
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:1174
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_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:3281
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.
Definition: dict_validate.c:35
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:114
int dict_dlopen(fr_dict_t *dict, char const *name)
Definition: dict_util.c:3555
bool loading
from fr_dict_protocol_afrom_file();
Definition: dict_priv.h:98
fr_dict_t const * next
for attribute overloading
Definition: dict_priv.h:112
fr_dict_attr_t const * attr_protocol_encapsulation
Definition: dict_priv.h:153
fr_hash_table_t * autoref
other dictionaries that we loaded via references
Definition: dict_priv.h:110
bool loaded
from fr_dict_protocol_afrom_file();
Definition: dict_priv.h:100
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:1087
fr_dict_attr_t ** fixups
Attributes that need fixing up.
Definition: dict_priv.h:116
TALLOC_CTX * pool
Talloc memory pool to reduce allocs.
Definition: dict_priv.h:107
dl_t * dl
for validation
Definition: dict_priv.h:120
bool free_at_exit
This gctx will be freed on exit.
Definition: dict_priv.h:126
int dict_dependent_add(fr_dict_t *dict, char const *dependent)
Record a new dependency on a dictionary.
Definition: dict_util.c:3647
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
char * filename
Name of the file the dictionary was loaded on.
Definition: dict_priv.h:72
fr_dict_attr_t const * ref
This attribute is a reference to another attribute.
Definition: dict_priv.h:174
int dict_protocol_add(fr_dict_t *dict)
Add a protocol to the global protocol table.
Definition: dict_util.c:1236
bool dict_has_dependents(fr_dict_t *dict)
Check if a dictionary still has dependents.
Definition: dict_util.c:3738
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:1145
bool perm_check
Whether we should check dictionary file permissions as they're loaded.
Definition: dict_priv.h:128
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
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:3217
int count
How many references are held by this file.
Definition: dict_priv.h:60
Optional arguments for initialising/allocating attributes.
Definition: dict_priv.h:171
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:83
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
Definition: merged_model.c:80
ssize_t fr_slen_t
Definition: merged_model.c:35
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:851
int nonnull(2, 5))
static size_t char ** out
Definition: value.h:997