The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
map_proc.c
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
17/*
18 * $Id: 6ea897d3e30376a3d4d8ac9d5cb9df233434e9a6 $
19 *
20 * @brief Map processor functions
21 * @file src/lib/server/map_proc.c
22 *
23 * @copyright 2015 The FreeRADIUS server project
24 * @copyright 2015 Arran Cudbard-bell (a.cudbardb@freeradius.org)
25 */
26
27RCSID("$Id: 6ea897d3e30376a3d4d8ac9d5cb9df233434e9a6 $")
28
29#include <freeradius-devel/server/base.h>
30
32
33/** Compare two map_proc_t structs, based ONLY on the name
34 *
35 * @param[in] one First map struct.
36 * @param[in] two Second map struct.
37 * @return Integer specifying order of map func instances.
38 */
39static int8_t map_proc_cmp(void const *one, void const *two)
40{
41 map_proc_t const *a = one, *b = two;
42
43 return MEMCMP_FIELDS(a, b, name, length);
44}
45
46/** Unregister a map processor
47 *
48 * @param[in] proc to unregister.
49 */
51{
52 map_proc_t find;
53 map_proc_t *found;
54
55 if (!map_proc_root) return 0;
56
57 strlcpy(find.name, proc->name, sizeof(find.name));
58 find.length = strlen(find.name);
59
60 found = fr_rb_find(map_proc_root, &find);
61 if (!found) return 0;
62
64
65 return 0;
66}
67
72
73/** Find a map processor by name
74 *
75 * @param[in] name of map processor.
76 * @return
77 * - #map_proc matching name.
78 * - NULL if none was found.
79 */
81{
82 map_proc_t find;
83
84 if (!map_proc_root) return NULL;
85
86 strlcpy(find.name, name, sizeof(find.name));
87 find.length = strlen(find.name);
88
89 return fr_rb_find(map_proc_root, &find);
90}
91
92static int _map_proc_tree_init(UNUSED void *uctx)
93{
95 return 0;
96}
97
98static int _map_proc_tree_free(UNUSED void *uctx)
99{
101
102 fr_assert_msg(fr_rb_num_elements(mpr) == 0, "map_proc_t still registered");
103
104 map_proc_root = NULL;
105 talloc_free(mpr);
106 return 0;
107}
108
109/** Register a map processor
110 *
111 * This should be called by every module that provides a map processing function.
112 *
113 * @param[in] ctx if non-null, the ctx to bind this map processor to.
114 * @param[in] mod_inst of module registering the map_proc.
115 * @param[in] name of map processor. If processor already exists, it is replaced.
116 * @param[in] evaluate Module's map processor function.
117 * @param[in] instantiate function (optional).
118 * @param[in] inst_size of talloc chunk to allocate for instance data (optional).
119 * @param[in] literals_safe_for What safe_for value to assign to literals.
120 * @return
121 * - 0 on success.
122 * - -1 on failure.
123 */
124int map_proc_register(TALLOC_CTX *ctx, void const *mod_inst, char const *name,
125 map_proc_func_t evaluate,
126 map_proc_instantiate_t instantiate, size_t inst_size, fr_value_box_safe_for_t literals_safe_for)
127{
128 map_proc_t *proc;
129
130 fr_assert(name && name[0]);
131
133
134 /*
135 * If it already exists, replace it.
136 */
137 proc = map_proc_find(name);
138 if (!proc) {
139 /*
140 * Don't allocate directly in the parent ctx, it might be mprotected
141 * later, and that'll cause segfaults if any of the map_proc_t are still
142 * protected when we start shuffling the contents of the rbtree.
143 */
144 proc = talloc_zero(NULL, map_proc_t);
145 if (ctx) talloc_link_ctx(ctx, proc);
146
147 strlcpy(proc->name, name, sizeof(proc->name));
148 proc->length = strlen(proc->name);
149
150 if (fr_rb_replace(NULL, map_proc_root, proc) < 0) {
151 talloc_free(proc);
152 return -1;
153 }
154
155 talloc_set_destructor(proc, _map_proc_talloc_free);
156 }
157
158 DEBUG3("map_proc_register: %s", proc->name);
159
160 proc->mod_inst = mod_inst;
161 proc->evaluate = evaluate;
162 proc->instantiate = instantiate;
163 proc->inst_size = inst_size;
164 proc->literals_safe_for = literals_safe_for;
165
166 return 0;
167}
168
169/** Unregister a map processor by name
170 *
171 * @param[in] name of map processor to unregister.
172 * @return
173 * - 0 if map processor was found and unregistered.
174 * - -1 if map processor was not found.
175 */
176int map_proc_unregister(char const *name)
177{
178 map_proc_t *proc;
179
180 proc = map_proc_find(name);
181 if (proc) {
182 talloc_free(proc);
183 return 0;
184 }
185
186 return -1;
187}
188
189
190/** Create a new map proc instance
191 *
192 * This should be called for every map {} section in the configuration.
193 *
194 * @param[in] ctx to allocate proc instance in.
195 * @param[in] proc resolved with #map_proc_find.
196 * @param[in] cs #CONF_SECTION representing this instance of a map processor.
197 * @param[in] src template.
198 * @param[in] maps Head of the list of maps.
199 * @return
200 * - New #map_proc_inst_t on success.
201 * - NULL on error.
202 */
203map_proc_inst_t *map_proc_instantiate(TALLOC_CTX *ctx, map_proc_t const *proc,
204 CONF_SECTION *cs, tmpl_t const *src, map_list_t const *maps)
205{
207
208 inst = talloc_zero(ctx, map_proc_inst_t);
209 inst->proc = proc;
210 inst->src = src;
211 inst->maps = maps;
212
213 if (proc->instantiate) {
214 if (proc->inst_size > 0) {
215 inst->data = talloc_zero_array(inst, uint8_t, proc->inst_size);
216 if (!inst->data) return NULL;
217 }
218
219 if (proc->instantiate(cs, proc->mod_inst, inst->data, src, maps) < 0) {
221 return NULL;
222 }
223 }
224
225 return inst;
226}
#define fr_atexit_global_once(_init, _free, _uctx)
Definition atexit.h:210
#define RCSID(id)
Definition build.h:506
#define UNUSED
Definition build.h:336
#define MEMCMP_FIELDS(_a, _b, _field, _len_field)
Return the comparison of two opaque fields of a structure.
Definition build.h:178
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:212
#define MEM(x)
Definition debug.h:46
talloc_free(hp)
#define DEBUG3(_fmt,...)
Definition log.h:266
static int _map_proc_tree_init(UNUSED void *uctx)
Definition map_proc.c:92
map_proc_t * map_proc_find(char const *name)
Find a map processor by name.
Definition map_proc.c:80
static int _map_proc_talloc_free(map_proc_t *proc)
Unregister a map processor.
Definition map_proc.c:50
static int _map_proc_tree_free(UNUSED void *uctx)
Definition map_proc.c:98
int map_proc_unregister(char const *name)
Unregister a map processor by name.
Definition map_proc.c:176
static fr_rb_tree_t * map_proc_root
Definition map_proc.c:31
int map_proc_register(TALLOC_CTX *ctx, void const *mod_inst, char const *name, map_proc_func_t evaluate, map_proc_instantiate_t instantiate, size_t inst_size, fr_value_box_safe_for_t literals_safe_for)
Register a map processor.
Definition map_proc.c:124
static int8_t map_proc_cmp(void const *one, void const *two)
Compare two map_proc_t structs, based ONLY on the name.
Definition map_proc.c:39
map_proc_inst_t * map_proc_instantiate(TALLOC_CTX *ctx, map_proc_t const *proc, CONF_SECTION *cs, tmpl_t const *src, map_list_t const *maps)
Create a new map proc instance.
Definition map_proc.c:203
fr_value_box_safe_for_t map_proc_literals_safe_for(map_proc_t const *proc)
Definition map_proc.c:68
int(* map_proc_instantiate_t)(CONF_SECTION *cs, void const *mod_inst, void *proc_inst, tmpl_t const *src, map_list_t const *maps)
Allocate new instance data for a map processor.
Definition map_proc.h:85
unlang_action_t(* map_proc_func_t)(unlang_result_t *p_result, map_ctx_t const *mpctx, request_t *request, fr_value_box_list_t *result, map_list_t const *maps)
Function to evaluate the src string and map the result to server attributes.
Definition map_proc.h:71
char name[FR_MAX_STRING_LEN]
Name of the map function.
int length
Length of name.
void const * mod_inst
Module instance.
fr_value_box_safe_for_t literals_safe_for
Safe for values to be set for literals in the map source.
size_t inst_size
Size of map_proc instance data to allocate.
map_proc_func_t evaluate
Module's map processor function.
map_proc_instantiate_t instantiate
Callback to create new instance struct.
Map processor registration.
Map processor instance.
unsigned char uint8_t
#define fr_assert(_expr)
Definition rad_assert.h:37
uint32_t fr_rb_num_elements(fr_rb_tree_t *tree)
Return how many nodes there are in a tree.
Definition rb.c:781
int fr_rb_replace(void **old, fr_rb_tree_t *tree, void const *data)
Replace old data with new data, OR insert if there is no old.
Definition rb.c:647
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:577
bool fr_rb_delete(fr_rb_tree_t *tree, void const *data)
Remove node and free data (if a free function was specified)
Definition rb.c:741
#define fr_rb_inline_talloc_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black that verifies elements are of a specific talloc type.
Definition rb.h:244
The main red black tree structure.
Definition rb.h:71
static char const * name
eap_aka_sim_process_conf_t * inst
size_t strlcpy(char *dst, char const *src, size_t siz)
Definition strlcpy.c:34
int talloc_link_ctx(TALLOC_CTX *parent, TALLOC_CTX *child)
Link two different parent and child contexts, so the child is freed before the parent.
Definition talloc.c:167
uintptr_t fr_value_box_safe_for_t
Escaping that's been applied to a value box.
Definition value.h:162