26 RCSID(
"$Id: 1561166f23f98d496a4637b082b0038da1e662f7 $")
28 #define LOG_PREFIX inst->name
31 #include <freeradius-devel/util/debug.h>
32 #include <freeradius-devel/radius/radius.h>
33 #include <freeradius-devel/unlang/function.h>
139 if (!query || !*query)
return 0;
144 if (!handle || !*handle)
return -1;
146 ret = sql->
query(sql, request, handle, query);
147 if (ret < 0)
return -1;
152 if (!*handle)
return -1;
164 #define DO_PART(_x) if(env->_x.type == FR_TYPE_STRING) { \
165 if(sqlippool_command(env->_x.vb_strvalue, &handle, sql, request) <0) goto error; \
167 #define DO_AFFECTED(_x, _affected) if (env->_x.type == FR_TYPE_STRING) { \
168 _affected = sqlippool_command(env->_x.vb_strvalue, &handle, sql, request); if (_affected < 0) goto error; \
170 #define RESERVE_CONNECTION(_handle, _pool, _request) _handle = fr_pool_connection_get(_pool, _request); \
172 REDEBUG("Failed reserving SQL connection"); \
173 RETURN_MODULE_FAIL; \
189 retval = sql->select(sql, request, handle, query);
191 if ((retval != 0) || !*handle) {
192 REDEBUG(
"database query error on '%s'", query);
196 if (sql->fetch_row(&row, sql, request, handle) < 0) {
197 REDEBUG(
"Failed fetching query result");
202 RDEBUG2(
"SQL query did not return any results");
207 REDEBUG(
"The first column of the result was NULL");
211 rlen = strlen(row[0]);
212 if (rlen >= outlen) {
213 REDEBUG(
"The first column of the result was too long (%d)", rlen);
221 (sql->driver->sql_finish_select_query)(*handle, &sql->config);
259 if (strcmp(talloc_get_name(
inst->sql),
"rlm_sql_t") != 0) {
260 cf_log_err(
conf,
"Module \"%s\" is not an instance of the rlm_sql module",
277 #define REPEAT_MOD_ALLOC_RESUME if (unlang_function_repeat_set(request, mod_alloc_resume) < 0) RETURN_MODULE_FAIL
295 int allocation_len = 0;
301 switch (alloc_ctx->
status) {
304 allocation_len =
sqlippool_query1(allocation,
sizeof(allocation), query->vb_strvalue, &handle,
305 alloc_ctx->
sql, request);
312 if (allocation_len > 0)
goto make_pair;
328 allocation_len =
sqlippool_query1(allocation,
sizeof(allocation), query->vb_strvalue, &handle,
329 alloc_ctx->
sql, request);
331 if (!handle)
goto error;
332 if (allocation_len > 0)
goto make_pair;
349 allocation_len =
sqlippool_query1(allocation,
sizeof(allocation), query->vb_strvalue, &handle,
350 alloc_ctx->
sql, request);
352 if (!handle)
goto error;
354 if (allocation_len == 0) {
370 RWDEBUG(
"IP address could not be allocated");
390 REDEBUG(
"Invalid IP address [%s] returned from database query.", allocation);
394 RDEBUG2(
"Allocated IP %s", allocation);
416 query->vb_strvalue, &handle, sql, request);
420 if (allocation_len) {
439 RWDEBUG(
"IP address could not be allocated as no pool exists with the name \"%pV\"",
447 if (
sqlippool_command(query->vb_strvalue, &handle, sql, request) < 0)
goto error;
509 fr_value_box_list_init(&alloc_ctx->
values);
594 UNUSED char const *section_name1,
UNUSED char const *section_name2,
622 *(
void **)
out = parsed_tmpl;
626 #define QUERY_ESCAPE .pair.escape = { \
627 .func = sqlippool_box_escape, \
628 .mode = TMPL_ESCAPE_PRE_CONCAT, \
629 .uctx = { .func = { .alloc = sql_escape_uctx_alloc }, .type = TMPL_ESCAPE_UCTX_ALLOC_FUNC }, \
630 }, .pair.func = call_env_parse
637 .pair.dflt =
"&control.IP-Pool.Name", .pair.dflt_quote =
T_BARE_WORD },
723 { .name1 =
"recv", .name2 =
"access-request", .method =
mod_alloc,
725 { .name1 =
"accounting", .name2 =
"start", .method =
mod_common,
727 { .name1 =
"accounting", .name2 =
"alive", .method =
mod_common,
729 { .name1 =
"accounting", .name2 =
"stop", .method =
mod_common,
731 { .name1 =
"accounting", .name2 =
"accounting-on", .method =
mod_common,
733 { .name1 =
"accounting", .name2 =
"accounting-off", .method =
mod_common,
739 { .name1 =
"recv", .name2 =
"Discover", .method =
mod_alloc,
741 { .name1 =
"recv", .name2 =
"Request", .method =
mod_common,
743 { .name1 =
"recv", .name2 =
"Confirm", .method =
mod_common,
745 { .name1 =
"recv", .name2 =
"Rebind", .method =
mod_common,
747 { .name1 =
"recv", .name2 =
"Renew", .method =
mod_common,
749 { .name1 =
"recv", .name2 =
"Release", .method =
mod_common,
751 { .name1 =
"recv", .name2 =
"Decline", .method =
mod_common,
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
@ UNLANG_ACTION_PUSHED_CHILD
unlang_t pushed a new child onto the stack, execute it instead of continuing.
strcpy(log_entry->msg, buffer)
#define fr_atexit_thread_local(_name, _free, _uctx)
#define CALL_ENV_TERMINATOR
#define FR_CALL_ENV_PARSE_OFFSET(_name, _cast_type, _flags, _struct, _field, _parse_field)
Specify a call_env_parser_t which writes out runtime results and the result of the parsing phase to t...
#define FR_CALL_ENV_METHOD_OUT(_inst)
Helper macro for populating the size/type fields of a call_env_method_t from the output structure typ...
@ CALL_ENV_FLAG_CONCAT
If the tmpl produced multiple boxes they should be concatenated.
@ CALL_ENV_FLAG_ATTRIBUTE
Tmpl must contain an attribute reference.
@ CALL_ENV_FLAG_PARSE_ONLY
The result of parsing will not be evaluated at runtime.
@ CALL_ENV_FLAG_REQUIRED
Associated conf pair or section is required.
@ CALL_ENV_FLAG_NULLABLE
Tmpl expansions are allowed to produce no output.
#define FR_CALL_ENV_OFFSET(_name, _cast_type, _flags, _struct, _field)
Specify a call_env_parser_t which writes out runtime results to the specified field.
#define FR_CALL_ENV_PARSE_ONLY_OFFSET(_name, _cast_type, _flags, _struct, _parse_field)
Specify a call_env_parser_t which writes out the result of the parsing phase to the field specified.
#define CONF_PARSER_TERMINATOR
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Defines a CONF_PAIR to C data type mapping.
Common header for all CONF_* types.
Configuration AVP similar to a fr_pair_t.
A section grouping multiple CONF_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.
fr_token_t cf_pair_value_quote(CONF_PAIR const *pair)
Return the value (rhs) quoting of a pair.
#define cf_log_err(_cf, _fmt,...)
char const *_CONST name
Instance name.
void *_CONST data
Module instance's parsed configuration.
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
CONF_SECTION *_CONST conf
Module's instance configuration.
#define unlang_function_push(_request, _func, _repeat, _signal, _sigmask, _top_frame, _uctx)
Push a generic function onto the unlang stack.
TALLOC_CTX * unlang_interpret_frame_talloc_ctx(request_t *request)
Get a talloc_ctx which is valid only for this frame.
int map_to_vp(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map, UNUSED void *uctx)
Convert a map to a fr_pair_t.
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.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
void * env_data
Per call environment data.
dl_module_inst_t const * inst
Dynamic loader API handle for the module.
dl_module_inst_t const * inst
Dynamic loader API handle for the module.
Temporary structure to hold arguments for module calls.
Temporary structure to hold arguments for instantiation calls.
Specifies a module method identifier.
module_instance_t * module_rlm_by_name(module_instance_t const *parent, char const *asked_name)
module_t common
Common fields presented by all modules.
void fr_pool_connection_release(fr_pool_t *pool, request_t *request, void *conn)
Release a connection.
static const conf_parser_t config[]
#define RETURN_MODULE_NOOP
#define RETURN_MODULE_UPDATED
rlm_rcode_t
Return codes indicating the result of the module call.
#define RETURN_MODULE_NOTFOUND
void * request_data_get(request_t *request, void const *unique_ptr, int unique_int)
Get opaque data from a request.
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.
static int instantiate(module_inst_ctx_t const *mctx)
Prototypes and functions for the SQL module.
static void * sql_escape_uctx_alloc(request_t *request, void const *uctx)
fr_value_box_t requested_address
IP address being requested by client.
#define RESERVE_CONNECTION(_handle, _pool, _request)
static int sqlippool_query1(char *out, int outlen, char const *query, rlm_sql_handle_t **handle, rlm_sql_t const *sql, request_t *request)
#define REPEAT_MOD_ALLOC_RESUME
tmpl_t * pool_check
tmpl to expand as query for checking for existence of the pool.
rlm_sql_handle_t * handle
SQL handle being used for queries.
static const call_env_method_t sqlippool_bulk_release_method_env
static int _sql_escape_uxtx_free(void *uctx)
fr_value_box_t allocated_address
Existing value for allocated IP.
fr_value_box_t free
SQL query to clear other offered IPs. Only used in "update" method.
#define DO_AFFECTED(_x, _affected)
fr_value_box_list_t values
Where to put the expanded queries ready for execution.
static const call_env_method_t sqlippool_release_method_env
static const call_env_method_t sqlippool_update_method_env
static int sqlippool_command(char const *query, rlm_sql_handle_t **handle, rlm_sql_t const *sql, request_t *request)
Perform a single sqlippool query.
static unlang_action_t mod_alloc(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Initiate the allocation of an IP address from the pool.
ippool_alloc_call_env_t * env
Call environment for the allocation.
static int mod_bootstrap(module_inst_ctx_t const *mctx)
static const call_env_method_t sqlippool_alloc_method_env
request_t * request
Current request.
static unlang_action_t mod_common(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Common function used by module methods which perform an optional "free" then "update".
static const call_env_method_t sqlippool_mark_method_env
static unlang_action_t mod_alloc_resume(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
Resume function called after each IP allocation query is expanded.
static int sqlippool_box_escape(fr_value_box_t *vb, void *uctx)
Call SQL module box_escape_func to escape tainted values.
tmpl_t * pool_name_tmpl
Tmpl used to expand pool_name.
fr_value_box_t commit
SQL query to commit transaction.
tmpl_t * existing
tmpl to expand as query for finding the existing IP.
tmpl_t * requested
tmpl to expand as query for finding the requested IP.
fr_value_box_t pool_name
Name of pool address will be allocated from.
static int call_env_parse(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci, UNUSED char const *section_name1, UNUSED char const *section_name2, void const *data, UNUSED call_env_parser_t const *rule)
Custom parser for sqlippool call env.
static int sqlippool_alloc_ctx_free(ippool_alloc_ctx_t *to_free)
Release SQL pool connections when alloc context is freed.
fr_value_box_t update
SQL query to update an IP record.
fr_value_box_t begin
SQL query to begin transaction.
tmpl_t * find
tmpl to expand as query for finding an unused IP.
tmpl_t * allocated_address_attr
Attribute to populate with allocated IP.
ippool_alloc_status_t status
Status of the allocation.
module_rlm_t rlm_sqlippool
ippool_alloc_status_t
Current step in IP allocation state machine.
@ IPPOOL_ALLOC_POOL_CHECK
Expanding the "pool_check" query.
@ IPPOOL_ALLOC_REQUESTED
Expanding the "requested" query.
@ IPPOOL_ALLOC_FIND
Expanding the "find" query.
@ IPPOOL_ALLOC_EXISTING
Expanding the "existing" query.
@ IPPOOL_ALLOC_UPDATE
Expanding the "update" query.
static int mod_instantiate(module_inst_ctx_t const *mctx)
static conf_parser_t module_config[]
tmpl_t * update
tmpl to expand as query for updating the found IP.
rlm_sql_t const * sql
SQL module instance.
Call environment used by module alloc method.
Resume context for IP allocation.
Call environment used by all other module methods.
#define FR_SBUFF_IN(_start, _len_or_end)
@ MODULE_TYPE_THREAD_SAFE
Module is threadsafe.
#define MODULE_NAME_TERMINATOR
dl_module_inst_t * dl_inst
Structure containing the module's instance data, configuration, and dl handle.
tmpl_escape_t escape
How escaping should be handled during evaluation.
fr_value_box_safe_for_t literals_safe_for
safe_for value assigned to literal values in xlats, execs, and data.
@ TMPL_TYPE_DATA
Value in native boxed format.
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.
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.
Optional arguments passed to vp_tmpl functions.
if(!subtype_vp) goto fail
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
eap_aka_sim_process_conf_t * inst
tmpl_t * lhs
Typically describes the attribute to add, modify or compare.
tmpl_t * rhs
Typically describes a literal value or a src attribute to copy or compare.
int(* sql_affected_rows)(rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
sql_rcode_t(* sql_finish_query)(rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
fr_value_box_escape_t box_escape_func
rlm_sql_driver_t const * driver
Driver's exported interface.
sql_rcode_t(* query)(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle, char const *query)
#define talloc_get_type_abort_const
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.
fr_value_box_escape_t func
How to escape when returned from evaluation.
struct tmpl_escape_t::@72 uctx
fr_value_box_safe_for_t safe_for
Value to set on boxes which have been escaped by the fr_value_box_escape_t function.
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.
#define FR_MAX_STRING_LEN
uintptr_t fr_value_box_safe_for_t
Escaping that's been applied to a value box.
static size_t char ** out