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 /** Vendors and attribute names
66  *
67  * It's very likely that the same vendors will operate in multiple
68  * protocol spaces, but number their attributes differently, so we need
69  * per protocol dictionaries.
70  *
71  * There would also be conflicts for DHCP(v6)/RADIUS attributes etc...
72  */
73 struct fr_dict {
74  fr_dict_gctx_t *gctx; //!< Global dictionary context this dictionary
75  ///< was allocated in.
76  bool read_only; //!< If true, disallow modifications.
77 
78  bool in_protocol_by_name; //!< Whether the dictionary has been inserted into the
79  ///< protocol_by_name hash.
80  bool in_protocol_by_num; //!< Whether the dictionary has been inserted into the
81  //!< protocol_by_num table.
82 
83  bool string_based; //!< TACACS, etc.
84 
85  bool loading; //!< from fr_dict_protocol_afrom_file();
86 
87  bool loaded; //!< from fr_dict_protocol_afrom_file();
88 
89  fr_hash_table_t *vendors_by_name; //!< Lookup vendor by name.
90  fr_hash_table_t *vendors_by_num; //!< Lookup vendor by PEN.
91 
92  fr_dict_attr_t *root; //!< Root attribute of this dictionary.
93 
94  TALLOC_CTX *pool; //!< Talloc memory pool to reduce allocs.
95  ///< in the dictionary.
96 
97  fr_hash_table_t *autoref; //!< other dictionaries that we loaded via references
98 
99  fr_dict_t const *next; //!< for attribute overloading
100 
101  fr_table_num_ordered_t const *subtype_table; //!< table of subtypes for this protocol
102  size_t subtype_table_len; //!< length of table of subtypes for this protocol
103 
104  unsigned int vsa_parent; //!< varies with different protocols
105  int default_type_size; //!< for TLVs and VSAs
106  int default_type_length; //!< for TLVs and VSAs
107 
108  dl_t *dl; //!< for validation
109 
110  fr_dict_protocol_t const *proto; //!< protocol-specific validation functions
111 
112  fr_dict_attr_valid_func_t attr_valid; //!< validation function for new attributes
113 
114  fr_dict_attr_t **fixups; //!< Attributes that need fixing up.
115 
116  fr_rb_tree_t *dependents; //!< Which files are using this dictionary.
117 };
118 
120  bool free_at_exit; //!< This gctx will be freed on exit.
121 
122  bool perm_check; //!< Whether we should check dictionary
123  ///< file permissions as they're loaded.
124 
125  bool read_only;
126 
127  char *dict_dir_default; //!< The default location for loading dictionaries if one
128  ///< wasn't provided.
129 
130  dl_loader_t *dict_loader; //!< for protocol validation
131 
132  fr_hash_table_t *protocol_by_name; //!< Hash containing names of all the
133  ///< registered protocols.
134  fr_hash_table_t *protocol_by_num; //!< Hash containing numbers of all the
135  ///< registered protocols.
136 
137  /** Magic internal dictionary
138  *
139  * Internal dictionary is checked in addition to the protocol dictionary
140  * when resolving attribute names.
141  *
142  * This is because internal attributes are valid for every
143  * protocol.
144  */
145  fr_dict_t *internal;
146 
148 };
149 
150 extern fr_dict_gctx_t *dict_gctx;
151 
153 
154 int dict_dependent_add(fr_dict_t *dict, char const *dependent);
155 
156 int dict_dependent_remove(fr_dict_t *dict, char const *dependent);
157 
158 fr_dict_t *dict_alloc(TALLOC_CTX *ctx);
159 
160 int dict_dlopen(fr_dict_t *dict, char const *name);
161 
162 fr_dict_attr_t *dict_attr_alloc_null(TALLOC_CTX *ctx);
163 
164 /** Optional arguments for initialising/allocating attributes
165  *
166  */
167 typedef struct {
168  fr_dict_attr_flags_t const *flags; //!< Any flags to assign to the attribute.
169 
170  fr_dict_attr_t const *ref; //!< This attribute is a reference to another attribute.
172 
173 int dict_attr_init(fr_dict_attr_t **da_p,
174  fr_dict_attr_t const *parent,
175  char const *name, int attr,
177 
178 fr_dict_attr_t *dict_attr_alloc(TALLOC_CTX *ctx,
179  fr_dict_attr_t const *parent,
180  char const *name, int attr,
182 
183 fr_dict_attr_t *dict_attr_acopy(TALLOC_CTX *ctx, fr_dict_attr_t const *in, char const *new_name);
184 
186 
188 
190 
192 
193 int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num);
194 
196 
198  UNUSED char const *name, int *attr, fr_type_t type,
199  fr_dict_attr_flags_t *flags) CC_HINT(nonnull(1,2,6));
200 
202  char const *name, int *attr, fr_type_t type,
203  fr_dict_attr_flags_t *flags);
204 
206 
207 fr_dict_attr_t *dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr);
208 
210  fr_dict_t **out, fr_sbuff_t *name, fr_dict_t const *dict_def);
211 
212 fr_dict_t *dict_by_protocol_name(char const *name);
213 
214 fr_dict_t *dict_by_protocol_num(unsigned int num);
215 
217 
219 
220 int dict_attr_enum_add_name(fr_dict_attr_t *da, char const *name, fr_value_box_t const *value,
221  bool coerce, bool replace, fr_dict_attr_t const *child_struct);
222 
223 #ifdef __cplusplus
224 }
225 #endif
va_list args
Definition: acutest.h:770
static fr_dict_t * dict
Definition: fuzzer.c:46
#define UNUSED
Definition: build.h:313
static fr_slen_t err
Definition: dict.h:645
bool(* fr_dict_attr_valid_func_t)(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, int attr, fr_type_t type, fr_dict_attr_flags_t *flags)
Definition: dict.h:288
fr_dict_attr_err_t
Errors returned by attribute lookup functions.
Definition: dict.h:272
static fr_slen_t in
Definition: dict.h:645
Values of the encryption flags.
Definition: merged_model.c:139
Protocol-specific callbacks in libfreeradius-PROTOCOL.
Definition: dict.h:341
fr_dict_attr_t * root
Root attribute of this dictionary.
Definition: dict_priv.h:92
bool loaded
from fr_dict_protocol_afrom_file();
Definition: dict_priv.h:87
fr_dict_attr_t ** fixups
Attributes that need fixing up.
Definition: dict_priv.h:114
char * dict_dir_default
The default location for loading dictionaries if one wasn't provided.
Definition: dict_priv.h:127
fr_dict_t const * next
for attribute overloading
Definition: dict_priv.h:99
fr_hash_table_t * protocol_by_name
Hash containing names of all the registered protocols.
Definition: dict_priv.h:132
bool read_only
If true, disallow modifications.
Definition: dict_priv.h:76
fr_dict_attr_valid_func_t attr_valid
validation function for new attributes
Definition: dict_priv.h:112
fr_dict_t * dict_by_da(fr_dict_attr_t const *da)
Internal version of fr_dict_by_da.
Definition: dict_util.c:2133
int default_type_size
for TLVs and VSAs
Definition: dict_priv.h:105
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:2012
fr_hash_table_t * vendors_by_name
Lookup vendor by name.
Definition: dict_priv.h:89
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:1346
fr_dict_attr_t * dict_attr_alloc_null(TALLOC_CTX *ctx)
Allocate a partially completed attribute.
Definition: dict_util.c:657
TALLOC_CTX * pool
Talloc memory pool to reduce allocs.
Definition: dict_priv.h:94
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:725
fr_rb_tree_t * dependents
Which files are using this dictionary.
Definition: dict_priv.h:116
fr_hash_table_t * protocol_by_num
Hash containing numbers of all the registered protocols.
Definition: dict_priv.h:134
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:1012
int dict_attr_init(fr_dict_attr_t **da_p, fr_dict_attr_t const *parent, char const *name, int attr, fr_type_t type, dict_attr_args_t const *args)
Initialise fields in a dictionary attribute structure.
Definition: dict_util.c:521
bool dict_attr_flags_valid(fr_dict_t *dict, fr_dict_attr_t const *parent, UNUSED char const *name, int *attr, fr_type_t type, fr_dict_attr_flags_t *flags))
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:2119
fr_dict_protocol_t const * proto
protocol-specific validation functions
Definition: dict_priv.h:110
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:1145
fr_dict_attr_flags_t const * flags
Any flags to assign to the attribute.
Definition: dict_priv.h:168
bool loading
from fr_dict_protocol_afrom_file();
Definition: dict_priv.h:85
fr_dict_t * dict_by_protocol_name(char const *name)
Internal version of fr_dict_by_protocol_name.
Definition: dict_util.c:2105
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:1046
int dict_dependent_remove(fr_dict_t *dict, char const *dependent)
Decrement ref count for a dependent in a dictionary.
Definition: dict_util.c:3262
fr_dict_t * dict_alloc(TALLOC_CTX *ctx)
Allocate a new dictionary.
Definition: dict_util.c:3428
int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num)
Add a vendor to the dictionary.
Definition: dict_util.c:930
dl_loader_t * dict_loader
for protocol validation
Definition: dict_priv.h:130
fr_dict_gctx_t * gctx
Global dictionary context this dictionary was allocated in.
Definition: dict_priv.h:74
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:2878
unsigned int vsa_parent
varies with different protocols
Definition: dict_priv.h:104
char const * dependent
File holding the reference.
Definition: dict_priv.h:62
fr_hash_table_t * vendors_by_num
Lookup vendor by PEN.
Definition: dict_priv.h:90
int dict_dlopen(fr_dict_t *dict, char const *name)
Definition: dict_util.c:3152
fr_dict_attr_t const * attr_protocol_encapsulation
Definition: dict_priv.h:147
int default_type_length
for TLVs and VSAs
Definition: dict_priv.h:106
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:751
bool in_protocol_by_num
Whether the dictionary has been inserted into the protocol_by_num table.
Definition: dict_priv.h:80
bool string_based
TACACS, etc.
Definition: dict_priv.h:83
dl_t * dl
for validation
Definition: dict_priv.h:108
fr_table_num_ordered_t const * subtype_table
table of subtypes for this protocol
Definition: dict_priv.h:101
bool free_at_exit
This gctx will be freed on exit.
Definition: dict_priv.h:120
int dict_dependent_add(fr_dict_t *dict, char const *dependent)
Record a new dependency on a dictionary.
Definition: dict_util.c:3203
fr_dict_attr_t * dict_attr_alloc(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:697
fr_dict_attr_t const * ref
This attribute is a reference to another attribute.
Definition: dict_priv.h:170
int dict_protocol_add(fr_dict_t *dict)
Add a protocol to the global protocol table.
Definition: dict_util.c:858
bool dict_has_dependents(fr_dict_t *dict)
Check if a dictionary still has dependents.
Definition: dict_util.c:3294
fr_dict_gctx_t * dict_gctx
Top level structure containing global dictionary state.
Definition: dict_util.c:40
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:804
bool perm_check
Whether we should check dictionary file permissions as they're loaded.
Definition: dict_priv.h:122
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:2814
bool in_protocol_by_name
Whether the dictionary has been inserted into the protocol_by_name hash.
Definition: dict_priv.h:78
int count
How many references are held by this file.
Definition: dict_priv.h:60
bool dict_attr_fields_valid(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, int *attr, fr_type_t type, fr_dict_attr_flags_t *flags)
Validate a new attribute definition.
size_t subtype_table_len
length of table of subtypes for this protocol
Definition: dict_priv.h:102
fr_hash_table_t * autoref
other dictionaries that we loaded via references
Definition: dict_priv.h:97
Optional arguments for initialising/allocating attributes.
Definition: dict_priv.h:167
Vendors and attribute names.
Definition: dict_priv.h:73
Entry recording dictionary reference holders by file.
Definition: dict_priv.h:58
Test enumeration values.
Definition: dict_test.h:92
A dynamic loader.
Definition: dl.c:81
Module handle.
Definition: dl.h:58
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
An element in an arbitrarily ordered array of name to num mappings.
Definition: table.h:53
static fr_slen_t parent
Definition: pair.h:844
int nonnull(2, 5))
static size_t char ** out
Definition: value.h:984