The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
condition.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: 26325f366177232b53a36fecfc6db5fc4d6a8f93 $
19  *
20  * @file unlang/condition.c
21  * @brief Unlang "condition" keyword evaluation.
22  *
23  * @copyright 2006-2019 The FreeRADIUS server project
24  */
25 RCSID("$Id: 26325f366177232b53a36fecfc6db5fc4d6a8f93 $")
26 
27 #include "condition_priv.h"
28 #include "group_priv.h"
29 
30 typedef struct {
31  fr_value_box_list_t out; //!< Head of the result of a nested
32  ///< expansion.
33  bool success; //!< If set, where to record the result
34  ///< of the execution.
36 
38 {
39  unlang_frame_state_cond_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_cond_t);
40  fr_value_box_t *box = fr_value_box_list_head(&state->out);
41  bool value;
42 
43  if (!box) {
44  value = false;
45 
46  } else if (fr_value_box_list_next(&state->out, box) != NULL) {
47  value = true;
48 
49  } else {
51  }
52 
53  if (!value) {
54  RDEBUG2("...");
56  }
57 
58  /*
59  * Tell the main interpreter to skip over the else /
60  * elsif blocks, as this "if" condition was taken.
61  */
62  while (frame->next &&
63  ((frame->next->type == UNLANG_TYPE_ELSE) ||
64  (frame->next->type == UNLANG_TYPE_ELSIF))) {
65  frame->next = frame->next->next;
66  }
67 
68  /*
69  * We took the "if". Go recurse into its' children.
70  */
71  return unlang_group(p_result, request, frame);
72 }
73 
75 {
78  unlang_frame_state_cond_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_cond_t);
79 
80  fr_assert(gext->head != NULL);
81 
82  /*
83  * If we always run this condition, then don't bother pushing anything onto the stack.
84  *
85  * We still run this condition, even for "false" values, due to things like
86  *
87  * if (0) { ... } elsif ....
88  */
89  if (gext->is_truthy) {
90  return unlang_group(p_result, request, frame);
91  }
92 
94 
95  fr_value_box_list_init(&state->out);
96 
97  if (unlang_xlat_push(state, &state->success, &state->out,
98  request, gext->head, UNLANG_SUB_FRAME) < 0) return UNLANG_ACTION_FAIL;
99 
101 }
102 
104 {
106  &(unlang_op_t){
107  .name = "if",
108  .interpret = unlang_if,
109  .debug_braces = true,
110  .frame_state_size = sizeof(unlang_frame_state_cond_t),
111  .frame_state_type = "unlang_frame_state_cond_t",
112  });
113 
115  &(unlang_op_t){
116  .name = "else",
117  .interpret = unlang_group,
118  .debug_braces = true
119  });
120 
122  &(unlang_op_t){
123  .name = "elseif",
124  .interpret = unlang_if,
125  .debug_braces = true,
126  .frame_state_size = sizeof(unlang_frame_state_cond_t),
127  .frame_state_type = "unlang_frame_state_cond_t",
128  });
129 }
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_FAIL
Encountered an unexpected error.
Definition: action.h:36
#define RCSID(id)
Definition: build.h:481
bool success
If set, where to record the result of the execution.
Definition: condition.c:33
static unlang_action_t unlang_if(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: condition.c:74
static unlang_action_t unlang_if_resume(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: condition.c:37
void unlang_condition_init(void)
Definition: condition.c:103
fr_value_box_list_t out
Head of the result of a nested expansion.
Definition: condition.c:31
xlat_exp_head_t * head
static unlang_cond_t * unlang_group_to_cond(unlang_group_t *g)
Cast a group structure to the cond keyword extension.
Test enumeration values.
Definition: dict_test.h:92
unlang_action_t unlang_group(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: group.c:30
Declarations for the "group" keyword.
#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
#define RDEBUG2(fmt,...)
Definition: radclient.h:54
rlm_rcode_t
Return codes indicating the result of the module call.
Definition: rcode.h:40
fr_assert(0)
int unlang_xlat_push(TALLOC_CTX *ctx, bool *p_success, fr_value_box_list_t *out, request_t *request, xlat_exp_head_t const *xlat, bool top_frame)
Push a pre-compiled xlat onto the stack for evaluation.
Definition: xlat.c:274
unlang_t * next
Next node (executed on UNLANG_ACTION_EXECUTE_NEXT et al).
Definition: unlang_priv.h:114
void * state
Stack frame specialisations.
Definition: unlang_priv.h:296
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
Definition: unlang_priv.h:531
@ UNLANG_TYPE_ELSIF
!Condition && Condition.
Definition: unlang_priv.h:55
@ UNLANG_TYPE_ELSE
!Condition.
Definition: unlang_priv.h:54
@ UNLANG_TYPE_IF
Condition.
Definition: unlang_priv.h:53
static void frame_repeat(unlang_stack_frame_t *frame, unlang_process_t process)
Mark the current stack frame up for repeat, and set a new process function.
Definition: unlang_priv.h:518
unlang_t const * instruction
The unlang node we're evaluating.
Definition: unlang_priv.h:281
unlang_t const * next
The next unlang node we will evaluate.
Definition: unlang_priv.h:282
unlang_type_t type
The specialisation of this node.
Definition: unlang_priv.h:117
Generic representation of a grouping.
Definition: unlang_priv.h:145
An unlang operation.
Definition: unlang_priv.h:204
Our interpreter stack, as distinct from the C stack.
Definition: unlang_priv.h:280
bool fr_value_box_is_truthy(fr_value_box_t const *in)
Check truthiness of values.
Definition: value.c:6326