The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
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: 7b885f40b9d6c73144fb67d24104f4e64d226782 $
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: 7b885f40b9d6c73144fb67d24104f4e64d226782 $")
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 fr_cmp_ret_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 fr_rb_find((void **)&found, 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 map_proc_t *found;
84
85 if (!map_proc_root) return NULL;
86
87 strlcpy(find.name, name, sizeof(find.name));
88 find.length = strlen(find.name);
89
90 fr_rb_find((void **)&found, map_proc_root, &find);
91
92 return found;
93}
94
95static int _map_proc_tree_init(UNUSED void *uctx)
96{
98 return 0;
99}
100
101static int _map_proc_tree_free(UNUSED void *uctx)
102{
104
105 fr_assert_msg(fr_rb_num_elements(mpr) == 0, "map_proc_t still registered");
106
107 map_proc_root = NULL;
108 talloc_free(mpr);
109 return 0;
110}
111
112/** Register a map processor
113 *
114 * This should be called by every module that provides a map processing function.
115 *
116 * @param[in] ctx if non-null, the ctx to bind this map processor to.
117 * @param[in] mod_inst of module registering the map_proc.
118 * @param[in] name of map processor. If processor already exists, it is replaced.
119 * @param[in] evaluate Module's map processor function.
120 * @param[in] instantiate function (optional).
121 * @param[in] inst_size of talloc chunk to allocate for instance data (optional).
122 * @param[in] literals_safe_for What safe_for value to assign to literals.
123 * @return
124 * - 0 on success.
125 * - -1 on failure.
126 */
127int map_proc_register(TALLOC_CTX *ctx, void const *mod_inst, char const *name,
128 map_proc_func_t evaluate,
129 map_proc_instantiate_t instantiate, size_t inst_size, fr_value_box_safe_for_t literals_safe_for)
130{
131 map_proc_t *proc;
132
133 fr_assert(name && name[0]);
134
136
137 /*
138 * If it already exists, replace it.
139 */
140 proc = map_proc_find(name);
141 if (!proc) {
142 /*
143 * Don't allocate directly in the parent ctx, it might be mprotected
144 * later, and that'll cause segfaults if any of the map_proc_t are still
145 * protected when we start shuffling the contents of the rbtree.
146 */
147 proc = talloc_zero(NULL, map_proc_t);
148 if (ctx) talloc_link_ctx(ctx, proc);
149
150 strlcpy(proc->name, name, sizeof(proc->name));
151 proc->length = strlen(proc->name);
152
153 if (fr_rb_replace(NULL, map_proc_root, proc) < 0) {
154 talloc_free(proc);
155 return -1;
156 }
157
158 talloc_set_destructor(proc, _map_proc_talloc_free);
159 }
160
161 DEBUG3("map_proc_register: %s", proc->name);
162
163 proc->mod_inst = mod_inst;
164 proc->evaluate = evaluate;
165 proc->instantiate = instantiate;
166 proc->inst_size = inst_size;
167 proc->literals_safe_for = literals_safe_for;
168
169 return 0;
170}
171
172/** Unregister a map processor by name
173 *
174 * @param[in] name of map processor to unregister.
175 * @return
176 * - 0 if map processor was found and unregistered.
177 * - -1 if map processor was not found.
178 */
179int map_proc_unregister(char const *name)
180{
181 map_proc_t *proc;
182
183 proc = map_proc_find(name);
184 if (proc) {
185 talloc_free(proc);
186 return 0;
187 }
188
189 return -1;
190}
191
192
193/** Create a new map proc instance
194 *
195 * This should be called for every map {} section in the configuration.
196 *
197 * @param[in] ctx to allocate proc instance in.
198 * @param[in] proc resolved with #map_proc_find.
199 * @param[in] cs #CONF_SECTION representing this instance of a map processor.
200 * @param[in] src template.
201 * @param[in] maps Head of the list of maps.
202 * @return
203 * - New #map_proc_inst_t on success.
204 * - NULL on error.
205 */
206map_proc_inst_t *map_proc_instantiate(TALLOC_CTX *ctx, map_proc_t const *proc,
207 CONF_SECTION *cs, tmpl_t const *src, map_list_t const *maps)
208{
210
211 inst = talloc_zero(ctx, map_proc_inst_t);
212 inst->proc = proc;
213 inst->src = src;
214 inst->maps = maps;
215
216 if (proc->instantiate) {
217 if (proc->inst_size > 0) {
218 inst->data = talloc_zero_array(inst, uint8_t, proc->inst_size);
219 if (!inst->data) return NULL;
220 }
221
222 if (proc->instantiate(cs, proc->mod_inst, inst->data, src, maps) < 0) {
224 return NULL;
225 }
226 }
227
228 return inst;
229}
#define fr_atexit_global_once(_init, _free, _uctx)
Definition atexit.h:214
#define RCSID(id)
Definition build.h:560
#define UNUSED
Definition build.h:384
#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:106
#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:243
#define MEM(x)
Definition debug.h:38
talloc_free(hp)
#define DEBUG3(_fmt,...)
Definition log.h:271
static int _map_proc_tree_init(UNUSED void *uctx)
Definition map_proc.c:95
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:101
int map_proc_unregister(char const *name)
Unregister a map processor by name.
Definition map_proc.c:179
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:127
static fr_cmp_ret_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:206
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
fr_cmp_ret_t
Result of an ordering comparison.
Definition misc.h:50
#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:807
int fr_rb_find(void **found, fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:586
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:656
int 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:767
#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:168
uintptr_t fr_value_box_safe_for_t
Escaping that's been applied to a value box.
Definition value.h:162