All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dict.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 #ifndef _FR_DICT_H
17 #define _FR_DICT_H
18 /**
19  * $Id: 8e36574b4dae0534c60b810ba2cc11a187fb705e $
20  *
21  * @file include/dict.h
22  * @brief Multi-protocol attribute dictionary API.
23  *
24  * @copyright 2015 The FreeRADIUS server project
25  */
26 #include <freeradius-devel/libradius.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #ifdef WITH_VERIFY_PTR
33 # define VERIFY_DA(_x) fr_dict_verify(__FILE__, __LINE__, _x)
34 #else
35 # define VERIFY_DA(_x) fr_assert(_x)
36 #endif
37 
38 /** Values of the encryption flags
39  */
40 typedef struct attr_flags {
41  unsigned int is_root : 1; //!< Is root of a dictionary.
42  unsigned int is_unknown : 1; //!< Attribute number or vendor is unknown.
43 
44  unsigned int internal : 1; //!< Internal attribute, should not be received
45  //!< in protocol packets, should not be encoded.
46  unsigned int has_tag : 1; //!< Tagged attribute.
47  unsigned int array : 1; //!< Pack multiples into 1 attr.
48  unsigned int has_value : 1; //!< Has a value.
49 
50  unsigned int concat : 1; //!< concatenate multiple instances
51  unsigned int is_pointer : 1; //!< data is a pointer
52 
53  unsigned int virtual : 1; //!< for dynamic expansion
54 
55  unsigned int compare : 1; //!< has a paircompare registered
56 
57  enum {
58  FLAG_ENCRYPT_NONE = 0, //!< Don't encrypt the attribute.
59  FLAG_ENCRYPT_USER_PASSWORD, //!< Encrypt attribute RFC 2865 style.
60  FLAG_ENCRYPT_TUNNEL_PASSWORD, //!< Encrypt attribute RFC 2868 style.
61  FLAG_ENCRYPT_ASCEND_SECRET //!< Encrypt attribute ascend style.
62  } encrypt;
63 
64  uint8_t length; //!< length of the attribute
65  uint8_t type_size; //!< for TLV2, size of the type
67 
68 extern const FR_NAME_NUMBER dict_attr_types[];
69 extern const size_t dict_attr_sizes[PW_TYPE_MAX][2];
70 
71 typedef struct dict_attr fr_dict_attr_t;
72 typedef struct fr_dict fr_dict_t;
74 
75 /** Dictionary attribute
76  */
77 struct dict_attr {
78  unsigned int vendor; //!< Vendor that defines this attribute.
79  unsigned int attr; //!< Attribute number.
80  PW_TYPE type; //!< Value type.
81 
82  fr_dict_attr_t const *parent; //!< Immediate parent of this attribute.
83  fr_dict_attr_t const **children; //!< Children of this attribute.
84  fr_dict_attr_t const *next; //!< Next child in bin.
85 
86  unsigned int depth; //!< Depth of nesting for this attribute.
87 
89  char name[1]; //!< Attribute name.
90 };
91 
92 /** Value of an enumerated attribute
93  */
94 typedef struct dict_enum {
95  fr_dict_attr_t const *da; //!< Dictionary attribute enum is associated with.
96  int value; //!< Enum value
97  char name[1]; //!< Enum name.
99 
100 /** Private enterprise
101  */
102 typedef struct dict_vendor {
103  unsigned int vendorpec; //!< Private enterprise number.
104  size_t type; //!< Length of type data
105  size_t length; //!< Length of length data
106  size_t flags; //!< Vendor flags.
107  char name[1]; //!< Vendor name.
109 
110 /*
111  * Dictionary constants
112  */
113 #define FR_DICT_ENUM_MAX_NAME_LEN (128)
114 #define FR_DICT_VENDOR_MAX_NAME_LEN (128)
115 #define FR_DICT_ATTR_MAX_NAME_LEN (128)
116 
117 /** Maximum level of TLV nesting allowed
118  */
119 #define FR_DICT_TLV_NEST_MAX (24)
120 
121 /** Maximum TLV stack size
122  */
123 #define FR_DICT_MAX_TLV_STACK (FR_DICT_TLV_NEST_MAX + 5)
124 
125 /** Maximum dictionary attribute size
126  */
127 #define FR_DICT_ATTR_SIZE (sizeof(fr_dict_attr_t) + FR_DICT_ATTR_MAX_NAME_LEN)
128 
129 extern const int fr_dict_attr_allowed_chars[256];
130 /*
131  * Dictionary population
132  */
133 int fr_dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int value);
134 
135 int fr_dict_attr_add(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, int attr,
136  PW_TYPE type, fr_dict_attr_flags_t flags);
137 
138 int fr_dict_enum_add(fr_dict_t *dict, char const *attr, char const *alias, int value);
139 
140 int fr_dict_str_to_argv(char *str, char **argv, int max_argc);
141 
142 int fr_dict_init(TALLOC_CTX *ctx, fr_dict_t **out,
143  char const *dir, char const *fn, char const *name);
144 
145 int fr_dict_read(fr_dict_t *dict, char const *dir, char const *filename);
146 
147 int fr_dict_parse_str(fr_dict_t *dict, char *buf,
148  fr_dict_attr_t const *parent, unsigned int vendor);
149 
150 fr_dict_attr_t const *fr_dict_root(fr_dict_t const *dict);
151 
152 /*
153  * Unknown ephemeral attributes
154  */
156 
157 void fr_dict_unknown_free(fr_dict_attr_t const **da);
158 
159 int fr_dict_unknown_vendor_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const **out,
160  fr_dict_attr_t const *parent, unsigned int vendor);
161 
162 size_t dict_print_attr_oid(char *buffer, size_t outlen,
163  fr_dict_attr_t const *ancestor, fr_dict_attr_t const *da);
164 
166  unsigned int vendor, unsigned int attr) CC_HINT(nonnull);
167 
168 fr_dict_attr_t *fr_dict_unknown_afrom_fields(TALLOC_CTX *ctx, fr_dict_attr_t const *parent,
169  unsigned int vendor, unsigned int attr) CC_HINT(nonnull);
170 
172  fr_dict_attr_t const *parent, char const *name);
173 
174 fr_dict_attr_t const *fr_dict_unknown_afrom_oid(TALLOC_CTX *ctx, fr_dict_t *dict,
175  fr_dict_attr_t const *parent, char const *name);
176 
178  fr_dict_attr_t const *parent, char const **name);
179 
180 
181 /*
182  * Lineage
183  */
184 void fr_dict_print(fr_dict_attr_t const *da, int depth);
185 
186 fr_dict_attr_t const *fr_dict_parent_common(fr_dict_attr_t const *a, fr_dict_attr_t const *b, bool is_ancestor);
187 
188 int fr_dict_oid_component(unsigned int *out, char const **oid);
189 
190 ssize_t fr_dict_attr_by_oid(fr_dict_t *dict, fr_dict_attr_t const **parent,
191  unsigned int *vendor, unsigned int *attr, char const *oid);
192 
193 /*
194  * Lookup
195  */
196 int fr_dict_vendor_by_name(fr_dict_t *dict, char const *name);
197 
198 fr_dict_vendor_t const *fr_dict_vendor_by_num(fr_dict_t *dict, int vendor);
199 
200 fr_dict_attr_t const *fr_dict_attr_by_name_substr(fr_dict_t *dict, char const **name);
201 
202 fr_dict_attr_t const *fr_dict_attr_by_name(fr_dict_t *dict, char const *attr);
203 
204 fr_dict_attr_t const *fr_dict_attr_by_num(fr_dict_t *dict, unsigned int vendor, unsigned int attr);
205 
206 fr_dict_attr_t const *fr_dict_attr_by_type(fr_dict_t *dict, unsigned int vendor, unsigned int attr, PW_TYPE type);
207 
208 fr_dict_attr_t const *fr_dict_attr_child_by_da(fr_dict_attr_t const *parent, fr_dict_attr_t const *child);
209 
210 fr_dict_attr_t const *fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr);
211 
212 fr_dict_enum_t *fr_dict_enum_by_da(fr_dict_t *dict, fr_dict_attr_t const *da, int value);
213 
214 char const *fr_dict_enum_name_by_da(fr_dict_t *dict, fr_dict_attr_t const *da, int value);
215 
216 fr_dict_enum_t *fr_dict_enum_by_name(fr_dict_t *dict, fr_dict_attr_t const *da, char const *val);
217 
218 /*
219  * Validation
220  */
221 int fr_dict_valid_name(char const *name);
222 
223 void fr_dict_verify(char const *file, int line, fr_dict_attr_t const *da);
224 #ifdef __cplusplus
225 }
226 #endif
227 #endif /* _FR_DICT_H */
fr_dict_attr_t const * fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Check if a child attribute exists in a parent using an attribute number.
Definition: dict.c:3611
void fr_dict_unknown_free(fr_dict_attr_t const **da)
Free dynamically allocated (unknown attributes)
Definition: dict.c:2426
fr_dict_attr_t * fr_dict_unknown_afrom_fields(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int vendor, unsigned int attr) CC_HINT(nonnull)
Allocates an unknown attribute.
Definition: dict.c:2613
unsigned int array
Pack multiples into 1 attr.
Definition: dict.h:47
void fr_dict_print(fr_dict_attr_t const *da, int depth)
Definition: dict.c:3113
int fr_dict_read(fr_dict_t *dict, char const *dir, char const *filename)
Definition: dict.c:2291
fr_dict_attr_t const ** children
Children of this attribute.
Definition: dict.h:83
Dictionary attribute.
Definition: dict.h:77
struct attr_flags fr_dict_attr_flags_t
Values of the encryption flags.
static char const * name
const size_t dict_attr_sizes[PW_TYPE_MAX][2]
Map data types to min / max data sizes.
Definition: dict.c:119
uint8_t length
length of the attribute
Definition: dict.h:64
fr_dict_attr_t const * da
Dictionary attribute enum is associated with.
Definition: dict.h:95
size_t length
Length of length data.
Definition: dict.h:105
int fr_dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int value)
Add a vendor to the dictionary.
Definition: dict.c:399
#define CC_HINT(_x)
Definition: build.h:71
char name[1]
Vendor name.
Definition: dict.h:107
unsigned int is_pointer
data is a pointer
Definition: dict.h:51
size_t flags
Vendor flags.
Definition: dict.h:106
int fr_dict_attr_add(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, int attr, PW_TYPE type, fr_dict_attr_flags_t flags)
Add an attribute to the dictionary.
Definition: dict.c:582
Number of defined data types.
Definition: radius.h:59
Values of the encryption flags.
Definition: dict.h:40
char const * fr_dict_enum_name_by_da(fr_dict_t *dict, fr_dict_attr_t const *da, int value)
Lookup the name of an enum value in a fr_dict_attr_t.
Definition: dict.c:3688
unsigned int depth
Depth of nesting for this attribute.
Definition: dict.h:86
struct dict_enum fr_dict_enum_t
Value of an enumerated attribute.
fr_dict_enum_t * fr_dict_enum_by_name(fr_dict_t *dict, fr_dict_attr_t const *da, char const *val)
Definition: dict.c:3703
fr_dict_attr_flags_t flags
Flags.
Definition: dict.h:88
fr_dict_vendor_t const * fr_dict_vendor_by_num(fr_dict_t *dict, int vendor)
Look up a vendor by its PEN.
Definition: dict.c:3412
const int fr_dict_attr_allowed_chars[256]
Definition: dict.c:145
Don't encrypt the attribute.
Definition: dict.h:58
Vendors and attribute names.
Definition: dict.c:61
int fr_dict_vendor_by_name(fr_dict_t *dict, char const *name)
Look up a vendor by its name.
Definition: dict.c:3386
void fr_dict_verify(char const *file, int line, fr_dict_attr_t const *da)
Definition: dict.c:3748
unsigned int is_unknown
Attribute number or vendor is unknown.
Definition: dict.h:42
int fr_dict_valid_name(char const *name)
Definition: dict.c:3730
const FR_NAME_NUMBER dict_attr_types[]
Map data types to names representing those types.
Definition: dict.c:85
unsigned int attr
Attribute number.
Definition: dict.h:79
int fr_dict_oid_component(unsigned int *out, char const **oid)
Process a single OID component.
Definition: dict.c:3217
unsigned int vendor
Vendor that defines this attribute.
Definition: dict.h:78
int fr_dict_enum_add(fr_dict_t *dict, char const *attr, char const *alias, int value)
Definition: dict.c:1153
int fr_dict_unknown_from_suboid(fr_dict_t *dict, fr_dict_attr_t *vendor_da, fr_dict_attr_t *da, fr_dict_attr_t const *parent, char const **name)
Create a dictionary attribute by name embedded in another string.
Definition: dict.c:3034
fr_dict_attr_t const * parent
Immediate parent of this attribute.
Definition: dict.h:82
enum attr_flags::@0 encrypt
unsigned int is_root
Is root of a dictionary.
Definition: dict.h:41
unsigned int concat
concatenate multiple instances
Definition: dict.h:50
int fr_dict_unknown_from_fields(fr_dict_attr_t *da, fr_dict_attr_t const *parent, unsigned int vendor, unsigned int attr) CC_HINT(nonnull)
Initialises an unknown attribute.
Definition: dict.c:2571
Private enterprise.
Definition: dict.h:102
char name[1]
Enum name.
Definition: dict.h:97
Encrypt attribute RFC 2868 style.
Definition: dict.h:60
fr_dict_attr_t const * next
Next child in bin.
Definition: dict.h:84
fr_dict_attr_t const * fr_dict_attr_by_type(fr_dict_t *dict, unsigned int vendor, unsigned int attr, PW_TYPE type)
Lookup a attribute by its its vendor and attribute numbers and data type.
Definition: dict.c:3549
#define val(x)
Definition: timestr.c:37
char name[1]
Attribute name.
Definition: dict.h:89
struct dict_vendor fr_dict_vendor_t
Private enterprise.
Encrypt attribute RFC 2865 style.
Definition: dict.h:59
Encrypt attribute ascend style.
Definition: dict.h:61
size_t dict_print_attr_oid(char *buffer, size_t outlen, fr_dict_attr_t const *ancestor, fr_dict_attr_t const *da)
Build the tlv_stack for the specified DA and encode the path in OID form.
Definition: dict.c:2525
unsigned int vendorpec
Private enterprise number.
Definition: dict.h:103
fr_dict_enum_t * fr_dict_enum_by_da(fr_dict_t *dict, fr_dict_attr_t const *da, int value)
Lookup the structure representing an enum value in a fr_dict_attr_t.
Definition: dict.c:3654
int value
Enum value.
Definition: dict.h:96
fr_dict_attr_t const * fr_dict_attr_by_name_substr(fr_dict_t *dict, char const **name)
Look up a dictionary attribute by a name embedded in another string.
Definition: dict.c:3445
fr_dict_attr_t const * fr_dict_unknown_add(fr_dict_t *dict, fr_dict_attr_t const *old)
Converts an unknown to a known by adding it to the internal dictionaries.
Definition: dict.c:2384
unsigned int has_tag
Tagged attribute.
Definition: dict.h:46
int fr_dict_unknown_vendor_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const **out, fr_dict_attr_t const *parent, unsigned int vendor)
Build an unknown vendor, parented by a VSA or EVS attribute.
Definition: dict.c:2459
size_t type
Length of type data.
Definition: dict.h:104
fr_dict_attr_t const * fr_dict_parent_common(fr_dict_attr_t const *a, fr_dict_attr_t const *b, bool is_ancestor)
Find a common ancestor that two TLV type attributes share.
Definition: dict.c:3174
int fr_dict_parse_str(fr_dict_t *dict, char *buf, fr_dict_attr_t const *parent, unsigned int vendor)
Definition: dict.c:2306
fr_dict_attr_t const * fr_dict_unknown_afrom_oid(TALLOC_CTX *ctx, fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name)
Create a fr_dict_attr_t from an ASCII attribute and value.
Definition: dict.c:2957
uint8_t type_size
for TLV2, size of the type
Definition: dict.h:65
fr_dict_attr_t const * fr_dict_attr_by_num(fr_dict_t *dict, unsigned int vendor, unsigned int attr)
Lookup a fr_dict_attr_t by its vendor and attribute numbers.
Definition: dict.c:3519
ssize_t fr_dict_attr_by_oid(fr_dict_t *dict, fr_dict_attr_t const **parent, unsigned int *vendor, unsigned int *attr, char const *oid)
Get the leaf attribute of an OID string.
Definition: dict.c:3263
PW_TYPE type
Value type.
Definition: dict.h:80
int fr_dict_unknown_from_oid(fr_dict_t *dict, fr_dict_attr_t *vendor_da, fr_dict_attr_t *da, fr_dict_attr_t const *parent, char const *name)
Initialise a fr_dict_attr_t from an ASCII attribute and value.
Definition: dict.c:2705
fr_dict_t * fr_dict_internal
Internal server dictionary.
Definition: dict.c:81
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition: dict.c:2339
int fr_dict_str_to_argv(char *str, char **argv, int max_argc)
Definition: dict.c:1304
unsigned int compare
has a paircompare registered
Definition: dict.h:55
Value of an enumerated attribute.
Definition: dict.h:94
int fr_dict_init(TALLOC_CTX *ctx, fr_dict_t **out, char const *dir, char const *fn, char const *name)
(re)initialize a protocol dictionary
Definition: dict.c:2148
fr_dict_attr_t const * fr_dict_attr_child_by_da(fr_dict_attr_t const *parent, fr_dict_attr_t const *child)
Check if a child attribute exists in a parent using a pointer (da)
Definition: dict.c:3570
PW_TYPE
Internal data types used within libfreeradius.
Definition: radius.h:31
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_t *dict, char const *attr)
Locate a fr_dict_attr_t by its name.
Definition: dict.c:3493
unsigned int has_value
Has a value.
Definition: dict.h:48