25RCSID(
"$Id: 7270645e97d8b30d3689aa52354c1f9b75edf1a9 $")
27#include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
28#include <freeradius-devel/server/cf_file.h>
29#include <freeradius-devel/server/cf_parse.h>
30#include <freeradius-devel/server/exec.h>
31#include <freeradius-devel/server/main_loop.h>
32#include <freeradius-devel/server/request_data.h>
33#include <freeradius-devel/server/trigger.h>
34#include <freeradius-devel/unlang/function.h>
35#include <freeradius-devel/unlang/subrequest.h>
36#include <freeradius-devel/unlang/xlat.h>
37#include <freeradius-devel/unlang/tmpl.h>
49#define REQUEST_INDEX_TRIGGER_NAME 1
50#define REQUEST_INDEX_TRIGGER_ARGS 2
80 ERROR(
"Triggers are not enabled");
85 ERROR(
"trigger xlat may only be used in a trigger command");
93 ERROR(
"Unknown attribute \"%pV\"", in_head);
99 ERROR(
"Attribute \"%pV\" is not valid for this trigger", in_head);
124 return CMP(a->
ci, b->ci);
216 if (!subcs)
goto use_global;
223 attr = strrchr(
name,
'.');
244 DEBUG3(
"Failed finding trigger '%s'", attr);
249 ERROR(
"Trigger is not a configuration variable: %s", attr);
312 PERROR(
"Failed copying trigger arguments");
319 false,
false,
false) < 0)
goto args_error;
329 fr_value_box_list_init(&trigger->
args);
330 fr_value_box_list_init(&trigger->
out);
361 cf_log_err(cp,
"Failed parsing trigger command");
396 .type = UNLANG_TMPL_ARGS_TYPE_EXEC,
398 .status_out = &trigger->exec_status,
399 .timeout = fr_time_delta_from_sec(1),
420 DEBUG(
"Failed setting timeout on trigger %s",
value);
426 PERROR(
"Running trigger failed");
474 ERROR(
"Incomplete dictionary: Missing definition for \"Connection-Pool-Server\"");
480 ERROR(
"Incomplete dictionary: Missing definition for \"Connection-Pool-Port\"");
489 vp->vp_uint16 = port;
495 pthread_mutex_destroy(mutex);
528 ERROR(
"%s - Pointer to main_config was NULL", __FUNCTION__);
534 WARN(
"trigger { ... } subsection not found, triggers will be disabled");
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
CONF_ITEM * cf_reference_item(CONF_SECTION const *parent_cs, CONF_SECTION const *outer_cs, char const *ptr)
Common header for all CONF_* types.
Configuration AVP similar to a fr_pair_t.
A section grouping multiple CONF_PAIR.
bool cf_item_is_pair(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_PAIR.
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
fr_token_t cf_pair_value_quote(CONF_PAIR const *pair)
Return the value (rhs) quoting of a pair.
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
#define cf_log_err(_cf, _fmt,...)
#define cf_log_perr(_cf, _fmt,...)
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
static char const * spaces
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
fr_dict_t const * fr_dict_internal(void)
fr_dict_attr_t const * fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Check if a child attribute exists in a parent using an attribute number.
int unlang_interpret_set_timeout(request_t *request, fr_time_delta_t timeout)
Set a timeout for a request.
void unlang_interpret_set(request_t *request, unlang_interpret_t *intp)
Set a specific interpreter for a request.
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
rlm_rcode_t unlang_interpret_synchronous(fr_event_list_t *el, request_t *request)
Execute an unlang section synchronously.
Stores all information relating to an event list.
void fr_canonicalize_error(TALLOC_CTX *ctx, char **sp, char **text, ssize_t slen, char const *fmt)
Canonicalize error strings, removing tabs, and generate spaces for error marker.
fr_event_list_t * main_loop_event_list(void)
Return the main loop event list.
@ FR_TYPE_STRING
String of printable characters.
int fr_pair_list_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from)
Duplicate a list of pairs.
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
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.
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a 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.
The main red black tree structure.
fr_dict_attr_t const * request_attr_request
#define request_local_alloc_internal(_ctx, _args)
Allocate a new internal request outside of the request pool.
Optional arguments for initialising requests.
void * request_data_reference(request_t *request, void const *unique_ptr, int unique_int)
Get opaque data from a request without removing it.
#define request_data_add(_request, _unique_ptr, _unique_int, _opaque, _free_on_replace, _free_on_parent, _persist)
Add opaque data to a request_t.
#define FR_SBUFF_IN(_start, _len_or_end)
void tmpl_set_xlat(tmpl_t *vpt, xlat_exp_head_t *xlat)
Change the default dictionary in the tmpl's resolution rules.
tmpl_t * tmpl_alloc(TALLOC_CTX *ctx, tmpl_type_t type, fr_token_t quote, char const *name, ssize_t len)
Create a new heap allocated tmpl_t.
@ TMPL_TYPE_EXEC
Callout to an external script or program.
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
struct tmpl_rules_s tmpl_rules_t
Optional arguments passed to vp_tmpl functions.
#define fr_time()
Allow us to arbitrarily manipulate time.
fr_dict_t const * dict_def
Default dictionary to use with unqualified attribute references.
Stores an attribute, a value and various bits of other data.
int unlang_subrequest_child_push_and_detach(request_t *request)
Add a child request to the runnable queue.
void * talloc_null_ctx(void)
Retrieve the current talloc NULL ctx.
static int64_t fr_time_to_sec(fr_time_t when)
Convert an fr_time_t (internal time) to number of sec since the unix epoch (wallclock time)
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
#define fr_time_wrap(_time)
int unlang_tmpl_push(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *tmpl, unlang_tmpl_args_t *args)
Push a tmpl onto the stack for evaluation.
static int _trigger_exec_init(void *cs_arg)
Set the global trigger section trigger_exec will search in, and register xlats.
fr_value_box_list_t args
Arguments to pass to the trigger exec.
void trigger_args_afrom_server(TALLOC_CTX *ctx, fr_pair_list_t *list, char const *server, uint16_t port)
Create trigger arguments to describe the server the pool connects to.
xlat_arg_parser_t const trigger_xlat_args[]
static int _mutex_free(pthread_mutex_t *mutex)
int trigger_exec_init(CONF_SECTION const *cs)
fr_rb_node_t node
Entry in the trigger last fired tree.
fr_value_box_list_t out
result of the xlap (which we ignore)
fr_time_t last_fired
When this trigger last fired.
static int _trigger_exec_free(UNUSED void *uctx)
Free trigger resources.
#define REQUEST_INDEX_TRIGGER_NAME
static fr_rb_tree_t * trigger_last_fired_tree
#define REQUEST_INDEX_TRIGGER_ARGS
xlat_action_t trigger_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Retrieve attributes from a special trigger list.
int trigger_exec(unlang_interpret_t *intp, CONF_SECTION const *cs, char const *name, bool rate_limit, fr_pair_list_t *args)
Execute a trigger - call an executable to process an event.
unlang_result_t result
the result of expansion
CONF_ITEM * ci
Config item this rate limit counter is associated with.
static pthread_mutex_t * trigger_mutex
static int8_t _trigger_last_fired_cmp(void const *one, void const *two)
Compares two last fired structures.
static CONF_SECTION const * trigger_cs
Whether triggers are enabled globally.
static void _trigger_last_fired_free(void *data)
bool trigger_enabled(void)
Return whether triggers are enabled.
Describes a rate limiting entry for a trigger.
static fr_event_list_t * el
Arguments for evaluating different types of tmpls.
int unlang_xlat_push(TALLOC_CTX *ctx, unlang_result_t *p_result, 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.
fr_slen_t xlat_tokenize(TALLOC_CTX *ctx, xlat_exp_head_t **head, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
Tokenize an xlat expansion.
uint8_t required
Argument must be present, and non-empty.
#define XLAT_ARG_PARSER_TERMINATOR
@ XLAT_ACTION_FAIL
An xlat function failed.
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
fr_slen_t xlat_tokenize_argv(TALLOC_CTX *ctx, xlat_exp_head_t **head, fr_sbuff_t *in, xlat_arg_parser_t const *xlat_args, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, bool spaces))
Tokenize an xlat expansion into a series of XLAT_TYPE_CHILD arguments.
Definition for a single argument consumend by an xlat function.
char const * fr_strerror(void)
Get the last library error.
int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
Copy value data verbatim duplicating any buffers.
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
static size_t char ** out