The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
map.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 /**
19  * $Id: cb19772b01ddb2110203d35c80118ac2c86d2d9e $
20  *
21  * @file lib/server/map.h
22  * @brief Structures and prototypes for maps
23  *
24  * @copyright 2015 The FreeRADIUS server project
25  * @copyright 2015 Arran Cudbard-bell (a.cudbardb@freeradius.org)
26  */
27 RCSIDH(map_h, "$Id: cb19772b01ddb2110203d35c80118ac2c86d2d9e $")
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 typedef struct map_s map_t;
34 typedef struct vp_list_mod_s vp_list_mod_t;
35 
36 #ifdef __cplusplus
37 }
38 #endif
39 
40 #include <freeradius-devel/server/cf_util.h>
41 #include <freeradius-devel/server/tmpl.h>
42 #include <freeradius-devel/util/dlist.h>
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 /** Single character tokens used as terminals for the LHS operand
49  *
50  */
51 #define MAP_LHS_TERMINALS \
52  ['-'] = true, \
53  [':'] = true, \
54  ['+'] = true, \
55  ['<'] = true, \
56  ['='] = true, \
57  ['>'] = true, \
58  ['~'] = true
59 
60 FR_DLIST_TYPES(map_list)
61 
62 /** Given these are used in so many places, it's more friendly to have a proper type
63  *
64  */
65 typedef FR_DLIST_HEAD(map_list) map_list_t;
66 
67 /** Value pair map
68  *
69  * Value pair maps contain a pair of templates, that describe a src attribute
70  * or value, and a destination attribute.
71  *
72  * Neither src or dst need to be an FR attribute, and their type can be inferred
73  * from whether map->da is NULL (not FR).
74  *
75  * @see tmpl_t
76  */
77 struct map_s {
78  tmpl_t *lhs; //!< Typically describes the attribute to add, modify or compare.
79  tmpl_t *rhs; //!< Typically describes a literal value or a src attribute
80  ///< to copy or compare.
81 
82  fr_token_t op; //!< The operator that controls insertion of the dst attribute.
83  fr_type_t cast; //!< Cast value to this type.
84 
85  CONF_ITEM *ci; //!< Config item that the map was created from. Mainly used for
86  //!< logging validation errors.
87 
88  map_t *parent; //! parent map, for nested ones
89  map_list_t child; //!< a child map. If it exists, `rhs` MUST be NULL
90 
91  FR_DLIST_ENTRY(map_list) entry; //!< List entry.
92 };
93 
94 FR_DLIST_FUNCS(map_list, map_t, entry)
95 
96 /** A list modification
97  *
98  */
99 struct vp_list_mod_s {
100  map_t const *map; //!< Original map describing the change to be made.
101 
102  map_list_t mod; //!< New map containing the destination (LHS) and
103  ///< values (RHS).
104  fr_dlist_t entry; //!< Entry into dlist
105 };
106 
107 #ifndef WITH_VERIFY_PTR
108 # define MAP_VERIFY(_x) fr_assert((_x)->lhs)
109 #else
110 # define MAP_VERIFY(_x) do { \
111  TMPL_VERIFY((_x)->lhs); \
112  if ((_x)->rhs) TMPL_VERIFY((_x)->rhs); \
113 } while (0)
114 #endif
115 
116 typedef int (*map_validate_t)(map_t *map, void *ctx);
117 typedef int (*radius_map_getvalue_t)(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request,
118  map_t const *map, void *uctx);
119 
120 int map_afrom_cp(TALLOC_CTX *ctx, map_t **out, map_t *parent, CONF_PAIR *cp,
121  tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, bool edit);
122 
123 int map_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs,
124  tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules,
125  map_validate_t validate, void *uctx, unsigned int max) CC_HINT(nonnull(2, 3));
126 
127 int map_afrom_cs_edit(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs,
128  tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules,
129  map_validate_t validate, void *uctx, unsigned int max) CC_HINT(nonnull(2, 3));
130 
131 int map_list_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs,
132  tmpl_rules_t const *t_rules,
133  map_validate_t validate, void *uctx,
134  unsigned int max);
135 
136 int map_afrom_value_box(TALLOC_CTX *ctx, map_t **out,
137  char const *lhs, fr_token_t lhs_type, tmpl_rules_t const *lhs_rules,
138  fr_token_t op,
139  fr_value_box_t *rhs, bool steal_rhs_buffs);
140 
141 int map_afrom_attr_str(TALLOC_CTX *ctx, map_t **out, char const *raw,
142  tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules);
143 
144 int map_afrom_vp(TALLOC_CTX *ctx, map_t **out, fr_pair_t *vp,
145  tmpl_rules_t const *rules);
146 
147 ssize_t map_afrom_substr(TALLOC_CTX *ctx, map_t **out, map_t **parent_p, fr_sbuff_t *in,
148  fr_table_num_sorted_t const *op_table, size_t op_table_len,
149  tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules,
150  fr_sbuff_parse_rules_t const *p_rules);
151 
152 int map_afrom_fields(TALLOC_CTX *ctx, map_t **out, map_t **parent_p, request_t *request,
153  char const *lhs, char const *op, char const *rhs,
154  tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules) CC_HINT(nonnull);
155 
156 int map_to_vp(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request,
157  map_t const *map, void *uctx) CC_HINT(nonnull (2,3,4));
158 
159 int map_list_mod_apply(request_t *request, vp_list_mod_t const *vlm);
160 
161 int map_to_list_mod(TALLOC_CTX *ctx, vp_list_mod_t **out,
162  request_t *request, map_t const *map,
163  fr_value_box_list_t *lhs_result, fr_value_box_list_t *rhs_result);
164 
165 int map_to_request(request_t *request, map_t const *map,
166  radius_map_getvalue_t func, void *ctx);
167 
169 
170 void map_debug_log(request_t *request, map_t const *map,
171  fr_pair_t const *vp) CC_HINT(nonnull(1, 2));
172 
174 extern size_t map_assignment_op_table_len;
175 
176 extern fr_sbuff_parse_rules_t const map_parse_rules_bareword_quoted;
177 extern fr_sbuff_parse_rules_t const *map_parse_rules_quoted[T_TOKEN_LAST];
178 
179 #ifdef __cplusplus
180 }
181 #endif
#define RCSIDH(h, id)
Definition: build.h:482
Common header for all CONF_* types.
Definition: cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition: cf_priv.h:70
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:101
fr_dcursor_eval_t void const * uctx
Definition: dcursor.h:546
static fr_slen_t in
Definition: dict.h:821
#define FR_DLIST_TYPES(_name)
Define type specific wrapper structs for dlists.
Definition: dlist.h:1129
#define FR_DLIST_FUNCS(_name, _element_type, _element_entry)
Define type specific wrapper functions for dlists.
Definition: dlist.h:1152
Entry in a doubly linked list.
Definition: dlist.h:41
int map_afrom_cp(TALLOC_CTX *ctx, map_t **out, map_t *parent, CONF_PAIR *cp, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, bool edit)
Convert CONFIG_PAIR (which may contain refs) to map_t.
Definition: map.c:109
fr_dlist_t entry
Entry into dlist.
Definition: map.h:104
int map_afrom_value_box(TALLOC_CTX *ctx, map_t **out, char const *lhs, fr_token_t lhs_type, tmpl_rules_t const *lhs_rules, fr_token_t op, fr_value_box_t *rhs, bool steal_rhs_buffs)
Convert a value box to a map.
Definition: map.c:1269
map_t const * map
Original map describing the change to be made.
Definition: map.h:100
fr_table_num_sorted_t const map_assignment_op_table[]
Definition: map.c:329
int(* radius_map_getvalue_t)(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map, void *uctx)
Definition: map.h:117
fr_sbuff_parse_rules_t const map_parse_rules_bareword_quoted
Definition: map.c:347
ssize_t map_afrom_substr(TALLOC_CTX *ctx, map_t **out, map_t **parent_p, fr_sbuff_t *in, fr_table_num_sorted_t const *op_table, size_t op_table_len, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, fr_sbuff_parse_rules_t const *p_rules)
Parse sbuff into (which may contain refs) to map_t.
Definition: map.c:427
int map_list_mod_apply(request_t *request, vp_list_mod_t const *vlm)
Apply the output of map_to_list_mod to a request.
Definition: map_async.c:922
typedef FR_DLIST_HEAD(map_list) map_list_t
Given these are used in so many places, it's more friendly to have a proper type.
int map_to_list_mod(TALLOC_CTX *ctx, vp_list_mod_t **out, request_t *request, map_t const *map, fr_value_box_list_t *lhs_result, fr_value_box_list_t *rhs_result)
Evaluate a map creating a new map with TMPL_TYPE_ATTR LHS and TMPL_TYPE_DATA RHS.
Definition: map_async.c:251
int map_to_vp(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map, void *uctx))
int map_afrom_vp(TALLOC_CTX *ctx, map_t **out, fr_pair_t *vp, tmpl_rules_t const *rules)
Convert a fr_pair_t into a map.
Definition: map.c:1350
void map_debug_log(request_t *request, map_t const *map, fr_pair_t const *vp))
Definition: map.c:2350
int map_to_request(request_t *request, map_t const *map, radius_map_getvalue_t func, void *ctx)
Convert map_t to fr_pair_t (s) and add them to a request_t.
Definition: map.c:1781
int map_afrom_cs_edit(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, map_validate_t validate, void *uctx, unsigned int max))
Convert a config section into an attribute map for editing.
Definition: map.c:1045
int map_afrom_fields(TALLOC_CTX *ctx, map_t **out, map_t **parent_p, request_t *request, char const *lhs, char const *op, char const *rhs, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules)
Convert a fr_pair_t into a map.
Definition: map.c:2439
int map_afrom_attr_str(TALLOC_CTX *ctx, map_t **out, char const *raw, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules)
Convert a value pair string to valuepair map.
Definition: map.c:1319
int map_list_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs, tmpl_rules_t const *t_rules, map_validate_t validate, void *uctx, unsigned int max)
Convert a config section into a list of { a, b, c, d, ...
Definition: map.c:1238
int(* map_validate_t)(map_t *map, void *ctx)
Definition: map.h:116
int map_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, map_validate_t validate, void *uctx, unsigned int max))
Convert a config section into an attribute map.
Definition: map.c:1014
map_list_t mod
New map containing the destination (LHS) and values (RHS).
Definition: map.h:102
ssize_t map_print(fr_sbuff_t *out, map_t const *map)
Print a map to a string.
Definition: map.c:2295
size_t map_assignment_op_table_len
Definition: map.c:345
fr_sbuff_parse_rules_t const * map_parse_rules_quoted[T_TOKEN_LAST]
Definition: map.c:391
A list modification.
Definition: map.h:99
fr_type_t
Definition: merged_model.c:80
long int ssize_t
Definition: merged_model.c:24
Optional arguments passed to vp_tmpl functions.
Definition: tmpl.h:341
fr_pair_t * vp
Value pair map.
Definition: map.h:77
fr_token_t op
The operator that controls insertion of the dst attribute.
Definition: map.h:82
tmpl_t * lhs
Typically describes the attribute to add, modify or compare.
Definition: map.h:78
map_list_t child
parent map, for nested ones
Definition: map.h:89
FR_DLIST_ENTRY(map_list) entry
List entry.
map_t * parent
Definition: map.h:88
tmpl_t * rhs
Typically describes a literal value or a src attribute to copy or compare.
Definition: map.h:79
CONF_ITEM * ci
Config item that the map was created from.
Definition: map.h:85
fr_type_t cast
Cast value to this type.
Definition: map.h:83
Stores an attribute, a value and various bits of other data.
Definition: pair.h:68
An element in a lexicographically sorted array of name to num mappings.
Definition: table.h:49
enum fr_token fr_token_t
#define T_TOKEN_LAST
Definition: token.h:129
static fr_slen_t parent
Definition: pair.h:851
int nonnull(2, 5))
static size_t char ** out
Definition: value.h:997