The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
map_builtin.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: 1cdbbeb50be0a2e088a2961ae98069b0f6df3044 $
19 *
20 * @file map_builtin.c
21 * @brief Built in map expansions.
22 *
23 * @copyright 2025 Network RADIUS SAS (legal@networkradius.com)
24 */
25
26
27RCSID("$Id: 1cdbbeb50be0a2e088a2961ae98069b0f6df3044 $")
28
29#include <freeradius-devel/server/base.h>
30#include "map.h"
31
32static TALLOC_CTX *map_ctx;
33
34static int list_map_verify(CONF_SECTION *cs, UNUSED void const *mod_inst, UNUSED void *proc_inst,
35 tmpl_t const *src, UNUSED map_list_t const *maps)
36{
37 if (!src) {
38 cf_log_err(cs, "Missing source of value list");
39 return -1;
40 }
41
42 return 0;
43}
44
45static int _list_map_proc_get_value(TALLOC_CTX *ctx, fr_pair_list_t *out,
46 request_t *request, map_t const *map, void *uctx)
47{
49 fr_value_box_t *value = talloc_get_type_abort(uctx, fr_value_box_t);
50
52 if (!vp) return -1;
53
54 if (fr_value_box_cast(vp, &vp->data, vp->data.type, vp->da, value) < 0) {
55 RPEDEBUG("Failed casting \"%pV\" for attribute %s", value, vp->da->name);
57 return -1;
58 }
60
61 return 0;
62}
63
64/** Map a list of value boxes to attributes using the index number in the list.
65 */
67 fr_value_box_list_t *in, map_list_t const *maps)
68{
70 fr_value_box_t *vb = NULL;
71 fr_value_box_t **values;
72 uint32_t index, i = 0, value_count = fr_value_box_list_num_elements(in);
73 TALLOC_CTX *local = talloc_new(NULL);
74 map_t *map = NULL;
75
76 if (value_count == 0) goto finish;
77 /*
78 * Use an array to point to the list entries so we don't
79 * repeatedly walk the list to find each index in the map.
80 */
81 MEM(values = talloc_array(local, fr_value_box_t *, value_count));
82 while ((vb = fr_value_box_list_next(in, vb))) values[i++] = vb;
83
84 /*
85 * Indexes are zero offset - so reduce value_count to the max index.
86 */
87 value_count --;
88
89 while ((map = map_list_next(maps, map))) {
90 if (tmpl_aexpand(local, &index, request, map->rhs, NULL, NULL) < 0) {
91 RPERROR("Failed expanding map RHS");
92 rcode = RLM_MODULE_FAIL;
93 goto finish;
94 }
95 if (index > value_count) {
96 RWARN("Asked for index %d when max is %d.", index, value_count);
97 continue;
98 }
99 if (values[index]->type == FR_TYPE_NULL) {
100 RDEBUG2("Skipping null value for index %d.", index);
101 continue;
102 }
103
104 if (map_to_request(request, map, _list_map_proc_get_value, values[index]) < 0) {
105 rcode = RLM_MODULE_FAIL;
106 goto finish;
107 }
108 rcode = RLM_MODULE_UPDATED;
109 }
110
111finish:
112 talloc_free(local);
113
114 RETURN_UNLANG_RCODE(rcode);
115}
116
117static int _map_global_init(UNUSED void *uctx)
118{
119 map_ctx = talloc_init("map");
121 return 0;
122}
123
124static int _map_global_free(UNUSED void *uctx)
125{
127 return 0;
128}
129
131{
132 int ret;
133 fr_atexit_global_once_ret(&ret, _map_global_init, _map_global_free, NULL);
134 return ret;
135}
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
#define RCSID(id)
Definition build.h:485
#define UNUSED
Definition build.h:317
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:286
#define MEM(x)
Definition debug.h:36
static fr_slen_t in
Definition dict.h:849
Test enumeration values.
Definition dict_test.h:92
#define RWARN(fmt,...)
Definition log.h:297
#define RPERROR(fmt,...)
Definition log.h:302
#define RPEDEBUG(fmt,...)
Definition log.h:376
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:1873
talloc_free(reap)
static int _map_global_init(UNUSED void *uctx)
static TALLOC_CTX * map_ctx
Definition map_builtin.c:32
static unlang_action_t mod_list_map_proc(unlang_result_t *p_result, UNUSED map_ctx_t const *mpctx, request_t *request, fr_value_box_list_t *in, map_list_t const *maps)
Map a list of value boxes to attributes using the index number in the list.
Definition map_builtin.c:66
static int _map_global_free(UNUSED void *uctx)
int map_global_init(void)
static int _list_map_proc_get_value(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map, void *uctx)
Definition map_builtin.c:45
static int list_map_verify(CONF_SECTION *cs, UNUSED void const *mod_inst, UNUSED void *proc_inst, tmpl_t const *src, UNUSED map_list_t const *maps)
Definition map_builtin.c:34
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:125
Temporary structure to hold arguments for map calls.
Definition map_proc.h:52
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
unsigned int uint32_t
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1343
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:287
#define RDEBUG2(fmt,...)
Definition radclient.h:54
#define RETURN_UNLANG_RCODE(_rcode)
Definition rcode.h:57
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:40
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:44
@ RLM_MODULE_UPDATED
OK (pairs modified).
Definition rcode.h:51
@ RLM_MODULE_NOOP
Module succeeded without doing anything.
Definition rcode.h:50
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:801
#define tmpl_aexpand(_ctx, _out, _request, _vpt, _escape, _escape_ctx)
Expand a tmpl to a C type, allocing a new buffer to hold the string.
Definition tmpl.h:1062
fr_aka_sim_id_type_t type
fr_pair_t * vp
Value pair map.
Definition map.h:77
tmpl_t * lhs
Typically describes the attribute to add, modify or compare.
Definition map.h:78
tmpl_t * rhs
Typically describes a literal value or a src attribute to copy or compare.
Definition map.h:79
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
int fr_value_box_cast(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert one type of fr_value_box_t to another.
Definition value.c:3744
static size_t char ** out
Definition value.h:1023
#define FR_VALUE_BOX_SAFE_FOR_ANY
Definition value.h:173