The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: d4a94dbe3aeecd3e04bee4184f2f50c611706d65 $
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 
27 RCSID("$Id: d4a94dbe3aeecd3e04bee4184f2f50c611706d65 $")
28 
29 #include <freeradius-devel/server/base.h>
30 #include <freeradius-devel/server/map_proc.h>
31 #include <freeradius-devel/server/map_proc_priv.h>
32 #include <freeradius-devel/util/atexit.h>
33 #include <freeradius-devel/util/debug.h>
34 #include <freeradius-devel/util/value.h>
35 #include <freeradius-devel/util/talloc.h>
36 
37 static fr_rb_tree_t *map_proc_root = NULL;
38 
39 /** Compare two map_proc_t structs, based ONLY on the name
40  *
41  * @param[in] one First map struct.
42  * @param[in] two Second map struct.
43  * @return Integer specifying order of map func instances.
44  */
45 static int8_t map_proc_cmp(void const *one, void const *two)
46 {
47  map_proc_t const *a = one, *b = two;
48 
49  MEMCMP_RETURN(a, b, name, length);
50  return 0;
51 }
52 
53 /** Unregister a map processor
54  *
55  * @param[in] proc to unregister.
56  */
58 {
59  map_proc_t find;
60  map_proc_t *found;
61 
62  if (!map_proc_root) return 0;
63 
64  strlcpy(find.name, proc->name, sizeof(find.name));
65  find.length = strlen(find.name);
66 
67  found = fr_rb_find(map_proc_root, &find);
68  if (!found) return 0;
69 
71 
72  return 0;
73 }
74 
76 {
77  return proc->literals_safe_for;
78 }
79 
80 /** Find a map processor by name
81  *
82  * @param[in] name of map processor.
83  * @return
84  * - #map_proc matching name.
85  * - NULL if none was found.
86  */
88 {
89  map_proc_t find;
90 
91  if (!map_proc_root) return NULL;
92 
93  strlcpy(find.name, name, sizeof(find.name));
94  find.length = strlen(find.name);
95 
96  return fr_rb_find(map_proc_root, &find);
97 }
98 
99 static int _map_proc_tree_init(UNUSED void *uctx)
100 {
102  return 0;
103 }
104 
105 static int _map_proc_tree_free(UNUSED void *uctx)
106 {
108 
109  fr_assert_msg(fr_rb_num_elements(mpr) == 0, "map_proc_t still registered");
110 
111  map_proc_root = NULL;
112  talloc_free(mpr);
113  return 0;
114 }
115 
116 /** Register a map processor
117  *
118  * This should be called by every module that provides a map processing function.
119  *
120  * @param[in] ctx if non-null, the ctx to bind this map processor to.
121  * @param[in] mod_inst of module registering the map_proc.
122  * @param[in] name of map processor. If processor already exists, it is replaced.
123  * @param[in] evaluate Module's map processor function.
124  * @param[in] instantiate function (optional).
125  * @param[in] inst_size of talloc chunk to allocate for instance data (optional).
126  * @param[in] literals_safe_for What safe_for value to assign to literals.
127  * @return
128  * - 0 on success.
129  * - -1 on failure.
130  */
131 int map_proc_register(TALLOC_CTX *ctx, void const *mod_inst, char const *name,
132  map_proc_func_t evaluate,
133  map_proc_instantiate_t instantiate, size_t inst_size, fr_value_box_safe_for_t literals_safe_for)
134 {
135  map_proc_t *proc;
136 
137  fr_assert(name && name[0]);
138 
140 
141  /*
142  * If it already exists, replace it.
143  */
144  proc = map_proc_find(name);
145  if (!proc) {
146  /*
147  * Don't allocate directly in the parent ctx, it might be mprotected
148  * later, and that'll cause segfaults if any of the map_proc_t are still
149  * protected when we start shuffling the contents of the rbtree.
150  */
151  proc = talloc_zero(NULL, map_proc_t);
152  if (ctx) talloc_link_ctx(ctx, proc);
153 
154  strlcpy(proc->name, name, sizeof(proc->name));
155  proc->length = strlen(proc->name);
156 
157  if (fr_rb_replace(NULL, map_proc_root, proc) < 0) {
158  talloc_free(proc);
159  return -1;
160  }
161 
162  talloc_set_destructor(proc, _map_proc_talloc_free);
163  }
164 
165  DEBUG3("map_proc_register: %s", proc->name);
166 
167  proc->mod_inst = mod_inst;
168  proc->evaluate = evaluate;
169  proc->instantiate = instantiate;
170  proc->inst_size = inst_size;
171  proc->literals_safe_for = literals_safe_for;
172 
173  return 0;
174 }
175 
176 /** Unregister a map processor by name
177  *
178  * @param[in] name of map processor to unregister.
179  * @return
180  * - 0 if map processor was found and unregistered.
181  * - -1 if map processor was not found.
182  */
183 int map_proc_unregister(char const *name)
184 {
185  map_proc_t *proc;
186 
187  proc = map_proc_find(name);
188  if (proc) {
189  talloc_free(proc);
190  return 0;
191  }
192 
193  return -1;
194 }
195 
196 
197 /** Create a new map proc instance
198  *
199  * This should be called for every map {} section in the configuration.
200  *
201  * @param[in] ctx to allocate proc instance in.
202  * @param[in] proc resolved with #map_proc_find.
203  * @param[in] cs #CONF_SECTION representing this instance of a map processor.
204  * @param[in] src template.
205  * @param[in] maps Head of the list of maps.
206  * @return
207  * - New #map_proc_inst_t on success.
208  * - NULL on error.
209  */
210 map_proc_inst_t *map_proc_instantiate(TALLOC_CTX *ctx, map_proc_t const *proc,
211  CONF_SECTION *cs, tmpl_t const *src, map_list_t const *maps)
212 {
214 
215  inst = talloc_zero(ctx, map_proc_inst_t);
216  inst->proc = proc;
217  inst->src = src;
218  inst->maps = maps;
219 
220  if (proc->instantiate) {
221  if (proc->inst_size > 0) {
222  inst->data = talloc_zero_array(inst, uint8_t, proc->inst_size);
223  if (!inst->data) return NULL;
224  }
225 
226  if (proc->instantiate(cs, proc->mod_inst, inst->data, src, maps) < 0) {
227  talloc_free(inst);
228  return NULL;
229  }
230  }
231 
232  return inst;
233 }
234 
235 /** Evaluate a set of maps using the specified map processor
236  *
237  * Evaluate the map processor src template, then call a map processor function to do
238  * something with the expanded src template and map the result to attributes in the request.
239  *
240  * @param[out] p_result Result code of evaluating the map.
241  * @param[in] request The current request.
242  * @param[in] inst of a map processor.
243  * @param[in,out] result Result of expanding the map input. May be consumed
244  * by the map processor.
245  * @return one of UNLANG_ACTION_*
246  */
247 unlang_action_t map_proc(rlm_rcode_t *p_result, request_t *request, map_proc_inst_t const *inst, fr_value_box_list_t *result)
248 {
249  return inst->proc->evaluate(p_result, inst->proc->mod_inst, inst->data, request, result, inst->maps);
250 }
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition: action.h:35
#define fr_atexit_global_once(_init, _free, _uctx)
Definition: atexit.h:211
#define RCSID(id)
Definition: build.h:481
#define MEMCMP_RETURN(_a, _b, _field, _len_field)
Return if the contents of the specified field is not identical between the specified structures.
Definition: build.h:154
#define UNUSED
Definition: build.h:313
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:101
fr_dcursor_eval_t void const * uctx
Definition: dcursor.h:546
#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:210
#define DEBUG3(_fmt,...)
Definition: log.h:266
talloc_free(reap)
static int _map_proc_tree_init(UNUSED void *uctx)
Definition: map_proc.c:99
unlang_action_t map_proc(rlm_rcode_t *p_result, request_t *request, map_proc_inst_t const *inst, fr_value_box_list_t *result)
Evaluate a set of maps using the specified map processor.
Definition: map_proc.c:247
map_proc_t * map_proc_find(char const *name)
Find a map processor by name.
Definition: map_proc.c:87
static int _map_proc_talloc_free(map_proc_t *proc)
Unregister a map processor.
Definition: map_proc.c:57
static int _map_proc_tree_free(UNUSED void *uctx)
Definition: map_proc.c:105
int map_proc_unregister(char const *name)
Unregister a map processor by name.
Definition: map_proc.c:183
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:210
static fr_rb_tree_t * map_proc_root
Definition: map_proc.c:37
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:131
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:45
fr_value_box_safe_for_t map_proc_literals_safe_for(map_proc_t const *proc)
Definition: map_proc.c:75
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:77
unlang_action_t(* map_proc_func_t)(rlm_rcode_t *p_result, void const *mod_inst, void *proc_inst, 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:63
char name[FR_MAX_STRING_LEN]
Name of the map function.
Definition: map_proc_priv.h:42
int length
Length of name.
Definition: map_proc_priv.h:43
void const * mod_inst
Module instance.
Definition: map_proc_priv.h:41
fr_value_box_safe_for_t literals_safe_for
Safe for values to be set for literals in the map source.
Definition: map_proc_priv.h:48
size_t inst_size
Size of map_proc instance data to allocate.
Definition: map_proc_priv.h:47
map_proc_func_t evaluate
Module's map processor function.
Definition: map_proc_priv.h:45
map_proc_instantiate_t instantiate
Callback to create new instance struct.
Definition: map_proc_priv.h:46
Map processor registration.
Definition: map_proc_priv.h:39
Map processor instance.
Definition: map_proc_priv.h:53
unsigned char uint8_t
Definition: merged_model.c:30
uint32_t fr_rb_num_elements(fr_rb_tree_t *tree)
#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:246
int fr_rb_replace(void **old, fr_rb_tree_t *tree, void const *data))
bool fr_rb_delete(fr_rb_tree_t *tree, void const *data)
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
The main red black tree structure.
Definition: rb.h:73
rlm_rcode_t
Return codes indicating the result of the module call.
Definition: rcode.h:40
static char const * name
static int instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_rest.c:1302
fr_assert(0)
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
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:171
uintptr_t fr_value_box_safe_for_t
Escaping that's been applied to a value box.
Definition: value.h:155