The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
limit.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: a5f40495b4d14a76c82d40173a090cb1fb211eb0 $
19 *
20 * @file unlang/limit.c
21 * @brief Unlang "limit" keyword evaluation.
22 *
23 * @copyright 2022 Network RADIUS SAS (legal@networkradius.com)
24 */
25RCSID("$Id: a5f40495b4d14a76c82d40173a090cb1fb211eb0 $")
26
27#include <freeradius-devel/server/rcode.h>
28#include "group_priv.h"
29#include "limit_priv.h"
30
34
42
43/** Send a signal (usually stop) to a request
44 *
45 * @param[in] request The current request.
46 * @param[in] frame current stack frame.
47 * @param[in] action to signal.
48 */
50{
51 unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
52
53 if (action == FR_SIGNAL_CANCEL) {
54 state->thread->active_callers--;
55 }
56}
57
66
68{
69 unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
70 unlang_action_t action;
71
73 fr_assert(state->thread != NULL);
74
75 if (state->thread->active_callers >= state->limit) return UNLANG_ACTION_FAIL;
76
78
80
82
83 return action;
84}
85
87{
88 unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
89 fr_value_box_t *box = fr_value_box_list_head(&state->result);
90
92 /*
93 * compile_limit() ensures that the tmpl is cast to uint32, so we don't have to do any more work here.
94 */
95 state->limit = box->vb_uint32;
96
97 return unlang_limit_enforce(p_result, request, frame);
98}
99
101{
103 unlang_limit_t *gext;
104 unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
105
107 gext = unlang_group_to_limit(g);
108
109 state->request = request;
110
111 if (!gext->vpt) {
112 state->limit = gext->limit;
113 return unlang_limit_enforce(p_result, request, frame);
114 }
115
116 fr_value_box_list_init(&state->result);
117
118 if (unlang_tmpl_push(state, NULL, &state->result, request, gext->vpt, NULL, UNLANG_SUB_FRAME) < 0) return UNLANG_ACTION_FAIL;
119
121
123}
124
125
127{
129 char const *name2;
130 unlang_t *c;
132 unlang_limit_t *gext;
133 tmpl_t *vpt = NULL;
134 uint32_t limit = 0;
135 fr_token_t token;
136 ssize_t slen;
137 tmpl_rules_t t_rules;
138
139 /*
140 * limit <number>
141 */
142 name2 = cf_section_name2(cs);
143 if (!name2) {
144 cf_log_err(cs, "You must specify a value for 'limit'");
145 print_url:
146 cf_log_err(ci, DOC_KEYWORD_REF(limit));
147 return NULL;
148 }
149
150 if (!cf_item_next(cs, NULL)) return UNLANG_IGNORE;
151
153 if (!g) return NULL;
154
155 gext = unlang_group_to_limit(g);
156
157 token = cf_section_name2_quote(cs);
158
159 /*
160 * We don't allow unknown attributes here.
161 */
162 t_rules = *(unlang_ctx->rules);
163 t_rules.attr.allow_unknown = false;
164 RULES_VERIFY(&t_rules);
165
166 slen = tmpl_afrom_substr(gext, &vpt,
167 &FR_SBUFF_IN_STR(name2),
168 token,
169 NULL,
170 &t_rules);
171 if (!vpt) {
172 syntax_error:
173 cf_canonicalize_error(cs, slen, "Failed parsing argument to 'foreach'", name2);
174 talloc_free(g);
175 return NULL;
176 }
177
178 /*
179 * Fixup the tmpl so that we know it's somewhat sane.
180 */
181 if (!pass2_fixup_tmpl(gext, &vpt, cf_section_to_item(cs), unlang_ctx->rules->attr.dict_def)) {
182 talloc_free(g);
183 return NULL;
184 }
185
186 if (tmpl_is_list(vpt)) {
187 cf_log_err(cs, "Cannot use list as argument for 'limit' statement");
188 error:
189 talloc_free(g);
190 goto print_url;
191 }
192
194 cf_log_err(cs, "Cannot use regular expression as argument for 'limit' statement");
195 goto error;
196 }
197
198 if (tmpl_is_data(vpt) && (token == T_BARE_WORD)) {
199 fr_value_box_t box;
200
201 if (fr_value_box_cast(NULL, &box, FR_TYPE_UINT32, NULL, tmpl_value(vpt)) < 0) goto syntax_error;
202
203 limit = box.vb_uint32;
204
205 } else {
206 /*
207 * Attribute or data MUST be cast to a 32-bit unsigned number.
208 */
209 if (tmpl_cast_set(vpt, FR_TYPE_UINT32) < 0) {
210 cf_log_perr(cs, "Failed setting cast type");
211 goto syntax_error;
212 }
213 }
214
215 /*
216 * Compile the contents of a "limit".
217 */
219 if (!c) return NULL;
220
222 gext = unlang_group_to_limit(g);
223 gext->limit = limit;
224 gext->vpt = vpt;
225
226 return c;
227}
228
230{
232 .name = "limit",
233 .type = UNLANG_TYPE_LIMIT,
235
236 .compile = unlang_compile_limit,
237 .interpret = unlang_limit,
238 .signal = unlang_limit_signal,
239
240 .unlang_size = sizeof(unlang_limit_t),
241 .unlang_name = "unlang_limit_t",
242
243 .frame_state_size = sizeof(unlang_frame_state_limit_t),
244 .frame_state_type = "unlang_frame_state_limit_t",
245
246 .thread_inst_size = sizeof(unlang_thread_limit_t),
247 .thread_inst_type = "unlang_thread_limit_t",
248 });
249}
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_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:485
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
#define RULES_VERIFY(_cs, _rules)
Definition cf_file.c:180
Common header for all CONF_* types.
Definition cf_priv.h:49
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1184
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:737
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:683
fr_token_t cf_section_name2_quote(CONF_SECTION const *cs)
Return the quoting of the name2 identifier.
Definition cf_util.c:1229
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:289
#define cf_canonicalize_error(_ci, _slen, _msg, _str)
Definition cf_util.h:367
#define cf_item_next(_parent, _curr)
Definition cf_util.h:92
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:296
unlang_t * unlang_compile_section(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:1508
bool pass2_fixup_tmpl(UNUSED TALLOC_CTX *ctx, tmpl_t **vpt_p, CONF_ITEM const *ci, fr_dict_t const *dict)
Definition compile.c:95
unlang_group_t * unlang_group_allocate(unlang_t *parent, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:447
void * unlang_thread_instance(unlang_t const *instruction)
Get the thread-instance data for an instruction.
Definition compile.c:2340
Declarations for the "group" keyword.
unlang_action_t unlang_interpret_push_children(unlang_result_t *p_result, request_t *request, rlm_rcode_t default_rcode, bool do_next_sibling)
Push the children of the current frame onto a new frame onto the stack.
Definition interpret.c:384
#define UNLANG_SUB_FRAME
Definition interpret.h:37
static TALLOC_CTX * unlang_ctx
Definition base.c:71
void unlang_register(unlang_op_t *op)
Register an operation with the interpreter.
Definition base.c:56
talloc_free(reap)
static unlang_action_t unlang_limit_enforce(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition limit.c:67
static unlang_action_t unlang_limit_xlat_done(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition limit.c:86
static unlang_t * unlang_compile_limit(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_ITEM const *ci)
Definition limit.c:126
void unlang_limit_init(void)
Definition limit.c:229
static unlang_action_t unlang_limit_resume_done(UNUSED unlang_result_t *p_result, UNUSED request_t *request, unlang_stack_frame_t *frame)
Definition limit.c:58
request_t * request
Definition limit.c:38
unlang_thread_limit_t * thread
Definition limit.c:36
static void unlang_limit_signal(UNUSED request_t *request, unlang_stack_frame_t *frame, fr_signal_t action)
Send a signal (usually stop) to a request.
Definition limit.c:49
fr_value_box_list_t result
Definition limit.c:40
static unlang_action_t unlang_limit(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition limit.c:100
uint32_t active_callers
Definition limit.c:32
uint32_t limit
Definition limit_priv.h:34
static unlang_limit_t * unlang_group_to_limit(unlang_group_t *g)
Cast a group structure to the limit keyword extension.
Definition limit_priv.h:40
tmpl_t * vpt
Definition limit_priv.h:33
@ FR_TYPE_UINT32
32 Bit unsigned integer.
unsigned int uint32_t
long int ssize_t
#define fr_assert(_expr)
Definition rad_assert.h:38
#define RETURN_UNLANG_FAIL
Definition rcode.h:59
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:41
#define FR_SBUFF_IN_STR(_start)
#define tmpl_value(_tmpl)
Definition tmpl.h:937
#define tmpl_contains_regex(vpt)
Definition tmpl.h:226
ssize_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_token_t quote, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Convert an arbitrary string into a tmpl_t.
static bool tmpl_is_list(tmpl_t const *vpt)
Definition tmpl.h:920
#define tmpl_is_data(vpt)
Definition tmpl.h:206
static fr_slen_t vpt
Definition tmpl.h:1269
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:335
int tmpl_cast_set(tmpl_t *vpt, fr_type_t type)
Set a cast for a tmpl.
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:332
fr_signal_t
Signals that can be generated/processed by request signal handlers.
Definition signal.h:38
@ FR_SIGNAL_CANCEL
Request has been cancelled.
Definition signal.h:40
uint8_t allow_unknown
Allow unknown attributes i.e.
Definition tmpl.h:301
int unlang_tmpl_push(TALLOC_CTX *ctx, unlang_result_t *p_result, fr_value_box_list_t *out, request_t *request, tmpl_t const *tmpl, unlang_tmpl_args_t *args, bool top_frame)
Push a tmpl onto the stack for evaluation.
Definition tmpl.c:276
enum fr_token fr_token_t
@ T_BARE_WORD
Definition token.h:120
void * state
Stack frame specialisations.
#define UNLANG_NEXT_STOP
Definition unlang_priv.h:99
#define UNLANG_IGNORE
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
@ UNLANG_TYPE_LIMIT
limit number of requests in a section
Definition unlang_priv.h:75
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.
unlang_t const * instruction
The unlang node we're evaluating.
@ UNLANG_OP_FLAG_DEBUG_BRACES
Print debug braces.
Generic representation of a grouping.
An unlang operation.
A node in a graph of unlang_op_t (s) that we execute.
Our interpreter stack, as distinct from the C stack.
static fr_slen_t parent
Definition pair.h:841
#define DOC_KEYWORD_REF(_x)
Definition version.h:89
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:3741