The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Data Structures | Macros | Enumerations | Functions | Variables
rlm_sqlippool.c File Reference

Allocates an IPv4 address from pools stored in SQL. More...

#include <rlm_sql.h>
#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/radius/radius.h>
#include <freeradius-devel/unlang/function.h>
#include <ctype.h>
+ Include dependency graph for rlm_sqlippool.c:

Go to the source code of this file.

Data Structures

struct  ippool_alloc_call_env_t
 Call environment used by module alloc method. More...
 
struct  ippool_alloc_ctx_t
 Resume context for IP allocation. More...
 
struct  ippool_common_call_env_t
 Call environment used by all other module methods. More...
 
struct  rlm_sqlippool_t
 

Macros

#define DO_AFFECTED(_x, _affected)
 
#define DO_PART(_x)
 
#define LOG_PREFIX   inst->name
 
#define QUERY_ESCAPE
 
#define REPEAT_MOD_ALLOC_RESUME   if (unlang_function_repeat_set(request, mod_alloc_resume) < 0) RETURN_MODULE_FAIL
 
#define RESERVE_CONNECTION(_handle, _pool, _request)
 

Enumerations

enum  ippool_alloc_status_t {
  IPPOOL_ALLOC_EXISTING ,
  IPPOOL_ALLOC_REQUESTED ,
  IPPOOL_ALLOC_FIND ,
  IPPOOL_ALLOC_POOL_CHECK ,
  IPPOOL_ALLOC_UPDATE
}
 Current step in IP allocation state machine. More...
 

Functions

static int _sql_escape_uxtx_free (void *uctx)
 
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. More...
 
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. More...
 
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. More...
 
static int mod_bootstrap (module_inst_ctx_t const *mctx)
 
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". More...
 
static int mod_instantiate (module_inst_ctx_t const *mctx)
 
static void * sql_escape_uctx_alloc (request_t *request, void const *uctx)
 
static int sqlippool_alloc_ctx_free (ippool_alloc_ctx_t *to_free)
 Release SQL pool connections when alloc context is freed. More...
 
static int sqlippool_box_escape (fr_value_box_t *vb, void *uctx)
 Call SQL module box_escape_func to escape tainted values. More...
 
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. More...
 
static int sqlippool_query1 (char *out, int outlen, char const *query, rlm_sql_handle_t **handle, rlm_sql_t const *sql, request_t *request)
 

Variables

static conf_parser_t module_config []
 
module_rlm_t rlm_sqlippool
 
static const call_env_method_t sqlippool_alloc_method_env
 
static const call_env_method_t sqlippool_bulk_release_method_env
 
static const call_env_method_t sqlippool_mark_method_env
 
static const call_env_method_t sqlippool_release_method_env
 
static const call_env_method_t sqlippool_update_method_env
 

Detailed Description

Allocates an IPv4 address from pools stored in SQL.

Id
1561166f23f98d496a4637b082b0038da1e662f7

Definition in file rlm_sqlippool.c.


Data Structure Documentation

◆ ippool_alloc_call_env_t

struct ippool_alloc_call_env_t

Call environment used by module alloc method.

Definition at line 49 of file rlm_sqlippool.c.

+ Collaboration diagram for ippool_alloc_call_env_t:
Data Fields
fr_value_box_t allocated_address Existing value for allocated IP.
tmpl_t * allocated_address_attr Attribute to populate with allocated IP.
fr_value_box_t begin SQL query to begin transaction.
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 * find tmpl to expand as query for finding an unused IP.
tmpl_t * pool_check tmpl to expand as query for checking for existence of the pool.
fr_value_box_t pool_name Name of pool address will be allocated from.
tmpl_t * pool_name_tmpl Tmpl used to expand pool_name.
tmpl_t * requested tmpl to expand as query for finding the requested IP.
fr_value_box_t requested_address IP address being requested by client.
tmpl_t * update tmpl to expand as query for updating the found IP.

◆ ippool_alloc_ctx_t

struct ippool_alloc_ctx_t

Resume context for IP allocation.

Definition at line 83 of file rlm_sqlippool.c.

+ Collaboration diagram for ippool_alloc_ctx_t:
Data Fields
ippool_alloc_call_env_t * env Call environment for the allocation.
rlm_sql_handle_t * handle SQL handle being used for queries.
request_t * request Current request.
rlm_sql_t const * sql SQL module instance.
ippool_alloc_status_t status Status of the allocation.
fr_value_box_list_t values Where to put the expanded queries ready for execution.

◆ ippool_common_call_env_t

struct ippool_common_call_env_t

Call environment used by all other module methods.

Definition at line 66 of file rlm_sqlippool.c.

+ Collaboration diagram for ippool_common_call_env_t:
Data Fields
fr_value_box_t free SQL query to clear other offered IPs. Only used in "update" method.
fr_value_box_t update SQL query to update an IP record.

◆ rlm_sqlippool_t

struct rlm_sqlippool_t

Definition at line 40 of file rlm_sqlippool.c.

+ Collaboration diagram for rlm_sqlippool_t:
Data Fields
char const * name
rlm_sql_t const * sql
char const * sql_name

Macro Definition Documentation

◆ DO_AFFECTED

#define DO_AFFECTED (   _x,
  _affected 
)
Value:
if (env->_x.type == FR_TYPE_STRING) { \
_affected = sqlippool_command(env->_x.vb_strvalue, &handle, sql, request); if (_affected < 0) goto error; \
}
@ FR_TYPE_STRING
String of printable characters.
Definition: merged_model.c:83
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.

Definition at line 167 of file rlm_sqlippool.c.

◆ DO_PART

#define DO_PART (   _x)
Value:
if(env->_x.type == FR_TYPE_STRING) { \
if(sqlippool_command(env->_x.vb_strvalue, &handle, sql, request) <0) goto error; \
}

Definition at line 164 of file rlm_sqlippool.c.

◆ LOG_PREFIX

#define LOG_PREFIX   inst->name

Definition at line 28 of file rlm_sqlippool.c.

◆ QUERY_ESCAPE

#define QUERY_ESCAPE
Value:
.pair.escape = { \
.uctx = { .func = { .alloc = sql_escape_uctx_alloc }, .type = TMPL_ESCAPE_UCTX_ALLOC_FUNC }, \
}, .pair.func = call_env_parse
static void * sql_escape_uctx_alloc(request_t *request, void const *uctx)
static int sqlippool_box_escape(fr_value_box_t *vb, void *uctx)
Call SQL module box_escape_func to escape tainted values.
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.
@ TMPL_ESCAPE_UCTX_ALLOC_FUNC
A new uctx of the specified size and type is allocated and pre-populated by memcpying uctx....
Definition: tmpl_escape.h:35
@ TMPL_ESCAPE_PRE_CONCAT
Pre-concatenation escaping is useful for DSLs where elements of the expansion are static,...
Definition: tmpl_escape.h:61

Definition at line 626 of file rlm_sqlippool.c.

◆ REPEAT_MOD_ALLOC_RESUME

#define REPEAT_MOD_ALLOC_RESUME   if (unlang_function_repeat_set(request, mod_alloc_resume) < 0) RETURN_MODULE_FAIL

Definition at line 277 of file rlm_sqlippool.c.

◆ RESERVE_CONNECTION

#define RESERVE_CONNECTION (   _handle,
  _pool,
  _request 
)
Value:
_handle = fr_pool_connection_get(_pool, _request); \
if (!_handle) { \
REDEBUG("Failed reserving SQL connection"); \
RETURN_MODULE_FAIL; \
}
void * fr_pool_connection_get(fr_pool_t *pool, request_t *request)
Reserve a connection in the connection pool.
Definition: pool.c:1390

Definition at line 170 of file rlm_sqlippool.c.

Enumeration Type Documentation

◆ ippool_alloc_status_t

Current step in IP allocation state machine.

Enumerator
IPPOOL_ALLOC_EXISTING 

Expanding the "existing" query.

IPPOOL_ALLOC_REQUESTED 

Expanding the "requested" query.

IPPOOL_ALLOC_FIND 

Expanding the "find" query.

IPPOOL_ALLOC_POOL_CHECK 

Expanding the "pool_check" query.

IPPOOL_ALLOC_UPDATE 

Expanding the "update" query.

Definition at line 73 of file rlm_sqlippool.c.

Function Documentation

◆ _sql_escape_uxtx_free()

static int _sql_escape_uxtx_free ( void *  uctx)
static

Definition at line 98 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ call_env_parse()

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 
)
static

Custom parser for sqlippool call env.

Needed as the escape function needs to reference the correct instance of the SQL module since escaping functions are dependent on the driver used by a given module instance.

Definition at line 593 of file rlm_sqlippool.c.

+ Here is the call graph for this function:

◆ mod_alloc()

static unlang_action_t mod_alloc ( rlm_rcode_t p_result,
module_ctx_t const *  mctx,
request_t request 
)
static

Initiate the allocation of an IP address from the pool.

Based on configured queries and attributes which exist, determines the first query tmpl to expand.

Parameters
p_resultResult of the allocation (if it fails).
mctxModule context.
requestCurrent request.
Returns
One of the UNLANG_ACTION_* values.

Definition at line 475 of file rlm_sqlippool.c.

+ Here is the call graph for this function:

◆ mod_alloc_resume()

static unlang_action_t mod_alloc_resume ( rlm_rcode_t p_result,
UNUSED int *  priority,
request_t request,
void *  uctx 
)
static

Resume function called after each IP allocation query is expanded.

Executes the query and, if appropriate, pushes the next tmpl for expansion

Following the final (successful) query, the destination attribute is populated.

Parameters
p_resultResult of IP allocation.
priorityUnused.
requestCurrent request.
uctxCurrent allocation context.
Returns
One of the UNLANG_ACTION_* values.

Definition at line 291 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mod_bootstrap()

static int mod_bootstrap ( module_inst_ctx_t const *  mctx)
static

Definition at line 226 of file rlm_sqlippool.c.

◆ mod_common()

static unlang_action_t mod_common ( rlm_rcode_t p_result,
module_ctx_t const *  mctx,
request_t request 
)
static

Common function used by module methods which perform an optional "free" then "update".

  • update
  • release
  • bulk_release
  • mark

Definition at line 550 of file rlm_sqlippool.c.

+ Here is the call graph for this function:

◆ mod_instantiate()

static int mod_instantiate ( module_inst_ctx_t const *  mctx)
static

Definition at line 244 of file rlm_sqlippool.c.

+ Here is the call graph for this function:

◆ sql_escape_uctx_alloc()

static void* sql_escape_uctx_alloc ( request_t request,
void const *  uctx 
)
static

Definition at line 103 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sqlippool_alloc_ctx_free()

static int sqlippool_alloc_ctx_free ( ippool_alloc_ctx_t to_free)
static

Release SQL pool connections when alloc context is freed.

Definition at line 270 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sqlippool_box_escape()

static int sqlippool_box_escape ( fr_value_box_t vb,
void *  uctx 
)
static

Call SQL module box_escape_func to escape tainted values.

Definition at line 581 of file rlm_sqlippool.c.

◆ sqlippool_command()

static int sqlippool_command ( char const *  query,
rlm_sql_handle_t **  handle,
rlm_sql_t const *  sql,
request_t request 
)
static

Perform a single sqlippool query.

Mostly wrapper around sql_query which returns the number of affected rows.

Parameters
[in]querysql query to execute.
[in]handlesql connection handle.
[in]sqlInstance of rlm_sql.
[in]requestCurrent request.
Returns
  • number of affected rows on success.
  • < 0 on error.

Definition at line 131 of file rlm_sqlippool.c.

+ Here is the caller graph for this function:

◆ sqlippool_query1()

static int sqlippool_query1 ( char *  out,
int  outlen,
char const *  query,
rlm_sql_handle_t **  handle,
rlm_sql_t const *  sql,
request_t request 
)
static

Definition at line 180 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ module_config

conf_parser_t module_config[]
static
Initial value:
= {
{ FR_CONF_OFFSET("sql_module_instance", rlm_sqlippool_t, sql_name), .dflt = "sql" },
}
#define CONF_PARSER_TERMINATOR
Definition: cf_parse.h:626
#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
Definition: cf_parse.h:268

Definition at line 92 of file rlm_sqlippool.c.

◆ rlm_sqlippool

module_rlm_t rlm_sqlippool

Definition at line 709 of file rlm_sqlippool.c.

◆ sqlippool_alloc_method_env

const call_env_method_t sqlippool_alloc_method_env
static
Initial value:
= {
.env = (call_env_parser_t[]) {
ippool_alloc_call_env_t, pool_name, pool_name_tmpl),
.pair.dflt = "&control.IP-Pool.Name", .pair.dflt_quote = T_BARE_WORD },
ippool_alloc_call_env_t, requested_address) },
{ FR_CALL_ENV_PARSE_OFFSET("allocated_address_attr", FR_TYPE_VOID,
ippool_alloc_call_env_t, allocated_address, allocated_address_attr) },
}
}
#define CALL_ENV_TERMINATOR
Definition: call_env.h:212
#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...
Definition: call_env.h:341
#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...
Definition: call_env.h:216
@ CALL_ENV_FLAG_CONCAT
If the tmpl produced multiple boxes they should be concatenated.
Definition: call_env.h:74
@ CALL_ENV_FLAG_ATTRIBUTE
Tmpl must contain an attribute reference.
Definition: call_env.h:84
@ CALL_ENV_FLAG_PARSE_ONLY
The result of parsing will not be evaluated at runtime.
Definition: call_env.h:83
@ CALL_ENV_FLAG_REQUIRED
Associated conf pair or section is required.
Definition: call_env.h:73
@ CALL_ENV_FLAG_NULLABLE
Tmpl expansions are allowed to produce no output.
Definition: call_env.h:78
#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.
Definition: call_env.h:316
#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.
Definition: call_env.h:365
Per method call config.
Definition: call_env.h:171
@ FR_TYPE_VOID
User data.
Definition: merged_model.c:127
#define QUERY_ESCAPE
Call environment used by module alloc method.
Definition: rlm_sqlippool.c:49
@ T_BARE_WORD
Definition: token.h:120

Definition at line 632 of file rlm_sqlippool.c.

◆ sqlippool_bulk_release_method_env

const call_env_method_t sqlippool_bulk_release_method_env
static
Initial value:

Definition at line 681 of file rlm_sqlippool.c.

◆ sqlippool_mark_method_env

const call_env_method_t sqlippool_mark_method_env
static

◆ sqlippool_release_method_env

const call_env_method_t sqlippool_release_method_env
static

◆ sqlippool_update_method_env

const call_env_method_t sqlippool_update_method_env
static