The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
switch.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: eb3adecdd471407c6a8955bddab5bfe09877e686 $
19  *
20  * @file unlang/switch.c
21  * @brief Unlang "switch" keyword evaluation.
22  *
23  * @copyright 2006-2019 The FreeRADIUS server project
24  */
25 RCSID("$Id: eb3adecdd471407c6a8955bddab5bfe09877e686 $")
26 
27 #include "group_priv.h"
28 #include "switch_priv.h"
29 
31 {
32  unlang_t *found;
33 
34  unlang_group_t *switch_g;
35  unlang_switch_t *switch_gext;
36 
37  tmpl_t vpt;
38  fr_value_box_t const *box = NULL;
39 
40  fr_pair_t *vp;
41 
42  /*
43  * Mock up an unlang_cast_t. Note that these on-stack
44  * buffers are the reason why case_cmp(), case_hash(),
45  * and case_to_key() use direct casts, and not the
46  * "generic to x" functions.
47  */
48  tmpl_t case_vpt = (tmpl_t) {
49  .type = TMPL_TYPE_DATA,
50  };
51  unlang_case_t my_case = (unlang_case_t) {
52  .group = (unlang_group_t) {
53  .self = (unlang_t) {
55  },
56  },
57  .vpt = &case_vpt,
58  };
59 
60  switch_g = unlang_generic_to_group(frame->instruction);
61  switch_gext = unlang_group_to_switch(switch_g);
62 
63  found = NULL;
64 
65  /*
66  * The attribute doesn't exist. We can skip
67  * directly to the default 'case' statement.
68  */
69  if (tmpl_is_attr(switch_gext->vpt)) {
70  if (tmpl_find_vp(&vp, request, switch_gext->vpt) < 0) {
71  found = switch_gext->default_case;
72  goto do_null_case;
73  } else {
74  box = &vp->data;
75  }
76 
77  /*
78  * Expand the template if necessary, so that it
79  * is evaluated once instead of for each 'case'
80  * statement.
81  */
82  } else if (tmpl_is_xlat(switch_gext->vpt) ||
83  tmpl_is_xlat_unresolved(switch_gext->vpt) ||
84  tmpl_is_exec(switch_gext->vpt)) {
85  char *p;
86  ssize_t len;
87 
88  len = tmpl_aexpand(request, &p, request, switch_gext->vpt, NULL, NULL);
89  if (len < 0) goto find_null_case;
90 
92  fr_value_box_bstrndup_shallow(&vpt.data.literal, NULL, p, len, false);
93  box = tmpl_value(&vpt);
94  } else if (!fr_cond_assert_msg(0, "Invalid tmpl type %s", tmpl_type_to_str(switch_gext->vpt->type))) {
95  return UNLANG_ACTION_FAIL;
96  }
97 
98  /*
99  * case_gext->vpt.data.literal is an in-line box, so we
100  * have to make a shallow copy of its contents.
101  *
102  * Note: We do not pass a ctx here as we don't want to
103  * create a reference.
104  */
105  fr_value_box_copy_shallow(NULL, &case_vpt.data.literal, box);
106  found = fr_htrie_find(switch_gext->ht, &my_case);
107  if (!found) {
108  find_null_case:
109  found = switch_gext->default_case;
110  }
111 
112 do_null_case:
113  if (box == tmpl_value(&vpt)) fr_value_box_clear_value(&vpt.data.literal);
114 
115  /*
116  * Nothing found. Just continue, and ignore the "switch"
117  * statement.
118  */
119  if (!found) return UNLANG_ACTION_EXECUTE_NEXT;
120 
121  if (unlang_interpret_push(request, found, frame->result, UNLANG_NEXT_STOP, UNLANG_SUB_FRAME) < 0) {
122  *p_result = RLM_MODULE_FAIL;
124  }
125 
127 }
128 
129 
131 {
133 
134  if (!g->children) {
135  *p_result = RLM_MODULE_NOOP;
137  }
138 
139  return unlang_group(p_result, request, frame);
140 }
141 
143 {
145  &(unlang_op_t){
146  .name = "switch",
147  .interpret = unlang_switch,
148  .debug_braces = true
149  });
150 
152  &(unlang_op_t){
153  .name = "case",
154  .interpret = unlang_case,
155  .debug_braces = true
156  });
157 }
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition: action.h:35
@ UNLANG_ACTION_PUSHED_CHILD
unlang_t pushed a new child onto the stack, execute it instead of continuing.
Definition: action.h:39
@ UNLANG_ACTION_EXECUTE_NEXT
Execute the next unlang_t.
Definition: action.h:38
@ UNLANG_ACTION_STOP_PROCESSING
Break out of processing the current request (unwind).
Definition: action.h:43
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
Definition: action.h:36
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition: action.h:37
#define RCSID(id)
Definition: build.h:481
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition: debug.h:156
unlang_action_t unlang_group(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: group.c:30
static void * fr_htrie_find(fr_htrie_t *ht, void const *data)
Find data in a htrie.
Definition: htrie.h:104
int unlang_interpret_push(request_t *request, unlang_t const *instruction, rlm_rcode_t default_rcode, bool do_next_sibling, bool top_frame)
Push a new frame onto the stack.
Definition: interpret.c:161
#define UNLANG_SUB_FRAME
Definition: interpret.h:36
void unlang_register(int type, unlang_op_t *op)
Register an operation with the interpreter.
Definition: base.c:63
long int ssize_t
Definition: merged_model.c:24
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:42
@ RLM_MODULE_NOOP
Module succeeded without doing anything.
Definition: rcode.h:48
int tmpl_find_vp(fr_pair_t **out, request_t *request, tmpl_t const *vpt))
Returns the first VP matching a tmpl_t.
Definition: tmpl_eval.c:889
#define tmpl_is_xlat(vpt)
Definition: tmpl.h:215
static fr_slen_t vpt
Definition: tmpl.h:1272
#define tmpl_value(_tmpl)
Definition: tmpl.h:948
#define tmpl_is_xlat_unresolved(vpt)
Definition: tmpl.h:225
#define tmpl_is_attr(vpt)
Definition: tmpl.h:213
#define tmpl_is_exec(vpt)
Definition: tmpl.h:216
@ TMPL_TYPE_DATA
Value in native boxed format.
Definition: tmpl.h:142
tmpl_t * tmpl_init_shallow(tmpl_t *vpt, tmpl_type_t type, fr_token_t quote, char const *name, ssize_t len, tmpl_rules_t const *t_rules))
Initialise a tmpl without copying the input name string.
struct tmpl_s tmpl_t
Definition: tmpl.h:241
#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:1070
static char const * tmpl_type_to_str(tmpl_type_t type)
Return a static string containing the type name.
Definition: tmpl.h:645
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition: pair.h:68
void unlang_switch_init(void)
Definition: switch.c:142
static unlang_action_t unlang_switch(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: switch.c:30
static unlang_action_t unlang_case(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: switch.c:130
fr_htrie_t * ht
Definition: switch_priv.h:36
unlang_group_t group
Definition: switch_priv.h:56
unlang_t * default_case
Definition: switch_priv.h:34
static unlang_switch_t * unlang_group_to_switch(unlang_group_t *g)
Cast a group structure to the switch keyword extension.
Definition: switch_priv.h:42
tmpl_t * vpt
Definition: switch_priv.h:35
@ T_SINGLE_QUOTED_STRING
Definition: token.h:122
#define UNLANG_NEXT_STOP
Definition: unlang_priv.h:92
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
Definition: unlang_priv.h:531
@ UNLANG_TYPE_SWITCH
Switch section.
Definition: unlang_priv.h:57
@ UNLANG_TYPE_CASE
Case section (within a UNLANG_TYPE_SWITCH).
Definition: unlang_priv.h:58
unlang_t const * instruction
The unlang node we're evaluating.
Definition: unlang_priv.h:281
unlang_t self
Definition: unlang_priv.h:146
rlm_rcode_t result
The result from executing the instruction.
Definition: unlang_priv.h:300
struct unlang_s unlang_t
Definition: unlang_priv.h:98
unlang_type_t type
The specialisation of this node.
Definition: unlang_priv.h:117
unlang_t * children
Children beneath this group.
Definition: unlang_priv.h:147
Generic representation of a grouping.
Definition: unlang_priv.h:145
An unlang operation.
Definition: unlang_priv.h:204
A node in a graph of unlang_op_t (s) that we execute.
Definition: unlang_priv.h:112
Our interpreter stack, as distinct from the C stack.
Definition: unlang_priv.h:280
void fr_value_box_copy_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t const *src)
Perform a shallow copy of a value_box.
Definition: value.c:3834
void fr_value_box_clear_value(fr_value_box_t *data)
Clear/free any existing value.
Definition: value.c:3681
void fr_value_box_bstrndup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Assign a string to to a fr_value_box_t.
Definition: value.c:4232