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

Tracks data usage and other counters using SQL. More...

#include <rlm_sql.h>
#include <freeradius-devel/server/base.h>
#include <freeradius-devel/server/module_rlm.h>
#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/unlang/function.h>
#include <ctype.h>
+ Include dependency graph for rlm_sqlcounter.c:

Go to the source code of this file.

Data Structures

struct  rlm_sqlcounter_t
 
struct  sqlcounter_call_env_t
 
struct  sqlcounter_rctx_t
 

Macros

#define ATTR_CHECK(_tmpl, _name)
 
#define LOG_PREFIX   "sqlcounter"
 

Functions

static int call_env_query_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 call_env parser to tokenize the SQL query xlat used for counter retrieval. More...
 
static int find_next_reset (rlm_sqlcounter_t *inst, fr_time_t now)
 
static int find_prev_reset (rlm_sqlcounter_t *inst, fr_time_t now)
 
static unlang_action_t mod_authorize (rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
 Check the value of a counter retrieved from an SQL query with a limit More...
 
static unlang_action_t mod_authorize_resume (rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
 Handle the result of calling the SQL query to retrieve the counter value. More...
 
static int mod_bootstrap (module_inst_ctx_t const *mctx)
 
static int mod_instantiate (module_inst_ctx_t const *mctx)
 

Variables

static fr_dict_t const * dict_freeradius
 
static const conf_parser_t module_config []
 
module_rlm_t rlm_sqlcounter
 
fr_dict_autoload_t rlm_sqlcounter_dict []
 
static const call_env_method_t sqlcounter_call_env
 

Detailed Description

Tracks data usage and other counters using SQL.

Id
c73cb6ef67fff33c92915852423b299554a1bbb3

Definition in file rlm_sqlcounter.c.


Data Structure Documentation

◆ rlm_sqlcounter_t

struct rlm_sqlcounter_t

Definition at line 62 of file rlm_sqlcounter.c.

+ Collaboration diagram for rlm_sqlcounter_t:
Data Fields
bool auto_extend If the remaining allowance is sufficient to reach the next period allow for that in setting the reply attribute.
tmpl_t * counter_attr Daily-Session-Time.
tmpl_t * end_attr &control.${.:instance}-End
tmpl_t * key User-Name.
fr_time_t last_reset
tmpl_t * limit_attr Max-Daily-Session.
char const * query SQL query to retrieve current session time.
char const * reset Daily, weekly, monthly, never or user defined.
fr_time_t reset_time
char const * sql_name Instance of SQL module to use, usually just 'sql'.
tmpl_t * start_attr &control.${.:instance}-Start
bool utc Use UTC time.

◆ sqlcounter_call_env_t

struct sqlcounter_call_env_t

Definition at line 104 of file rlm_sqlcounter.c.

+ Collaboration diagram for sqlcounter_call_env_t:
Data Fields
xlat_exp_head_t * query_xlat Tokenized xlat to run query.
tmpl_t * reply_attr Attribute to write timeout to.
tmpl_t * reply_msg_attr Attribute to write reply message to.

◆ sqlcounter_rctx_t

struct sqlcounter_rctx_t

Definition at line 250 of file rlm_sqlcounter.c.

+ Collaboration diagram for sqlcounter_rctx_t:
Data Fields
sqlcounter_call_env_t * env
rlm_sqlcounter_t * inst
bool last_success
fr_pair_t * limit
fr_value_box_list_t result

Macro Definition Documentation

◆ ATTR_CHECK

#define ATTR_CHECK (   _tmpl,
  _name 
)
Value:
if (tmpl_is_attr_unresolved(inst->_tmpl)) { \
cf_log_perr(conf, "Failed defining %s attribute", _name); \
return -1; \
} \
} else if (tmpl_is_attr(inst->_tmpl)) { \
if (tmpl_attr_tail_da(inst->_tmpl)->type != FR_TYPE_UINT64) { \
cf_log_err(conf, "%s attribute %s must be uint64", _name, tmpl_attr_tail_da(inst->_tmpl)->name); \
return -1; \
} \
}
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition: dict_util.c:1997
fr_dict_t * fr_dict_unconst(fr_dict_t const *dict)
Coerce to non-const.
Definition: dict_util.c:4179
int fr_dict_attr_add(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, int attr, fr_type_t type, fr_dict_attr_flags_t const *flags))
Add an attribute to the dictionary.
Definition: dict_util.c:1245
@ FR_TYPE_UINT64
64 Bit unsigned integer.
Definition: merged_model.c:100
static rs_t * conf
Definition: radsniff.c:53
static fr_dict_t const * dict_freeradius
#define tmpl_is_attr_unresolved(vpt)
Definition: tmpl.h:224
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition: tmpl.h:796
#define tmpl_is_attr(vpt)
Definition: tmpl.h:213
static char const * tmpl_attr_tail_unresolved(tmpl_t const *vpt)
Return the last attribute reference unresolved da.
Definition: tmpl.h:864
eap_aka_sim_process_conf_t * inst

Definition at line 480 of file rlm_sqlcounter.c.

◆ LOG_PREFIX

#define LOG_PREFIX   "sqlcounter"

Definition at line 27 of file rlm_sqlcounter.c.

Function Documentation

◆ call_env_query_parse()

static int call_env_query_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 call_env parser to tokenize the SQL query xlat used for counter retrieval.

Definition at line 520 of file rlm_sqlcounter.c.

+ Here is the call graph for this function:

◆ find_next_reset()

static int find_next_reset ( rlm_sqlcounter_t inst,
fr_time_t  now 
)
static

Definition at line 118 of file rlm_sqlcounter.c.

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

◆ find_prev_reset()

static int find_prev_reset ( rlm_sqlcounter_t inst,
fr_time_t  now 
)
static

Definition at line 186 of file rlm_sqlcounter.c.

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

◆ mod_authorize()

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

Check the value of a counter retrieved from an SQL query with a limit

Module specific attributes containing the start / end times are created / updated, the query is tokenized as an xlat call to the relevant SQL module and then pushed on the stack for evaluation.

Definition at line 384 of file rlm_sqlcounter.c.

+ Here is the call graph for this function:

◆ mod_authorize_resume()

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

Handle the result of calling the SQL query to retrieve the counter value.

Create / update the counter attribute in the control list If counter > limit, optionally populate a reply message and return RLM_MODULE_REJECT. Otherwise, optionally populate a reply attribute with the value of limit - counter and return RLM_MODULE_UPDATED. If no reply attribute is set, return RLM_MODULE_OK.

Definition at line 265 of file rlm_sqlcounter.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 492 of file rlm_sqlcounter.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 453 of file rlm_sqlcounter.c.

+ Here is the call graph for this function:

Variable Documentation

◆ dict_freeradius

fr_dict_t const* dict_freeradius
static

Definition at line 110 of file rlm_sqlcounter.c.

◆ module_config

const conf_parser_t module_config[]
static
Initial value:
= {
{ FR_CONF_OFFSET_FLAGS("sql_module_instance", CONF_FLAG_REQUIRED, rlm_sqlcounter_t, sql_name) },
{ FR_CONF_OFFSET_FLAGS("auto_extend", CONF_FLAG_OK_MISSING, rlm_sqlcounter_t, auto_extend) },
{ FR_CONF_OFFSET_FLAGS("key", CONF_FLAG_NOT_EMPTY, rlm_sqlcounter_t, key), .dflt = "%{%{Stripped-User-Name} || %{User-Name}}", .quote = T_DOUBLE_QUOTED_STRING },
{ FR_CONF_OFFSET_FLAGS("reset_period_start_name", CONF_FLAG_ATTRIBUTE, rlm_sqlcounter_t, start_attr),
.dflt = "&control.${.:instance}-Reset-Start", .quote = T_BARE_WORD },
{ FR_CONF_OFFSET_FLAGS("reset_period_end_name", CONF_FLAG_ATTRIBUTE, rlm_sqlcounter_t, end_attr),
.dflt = "&control.${.:instance}-Reset-End", .quote = T_BARE_WORD },
}
#define CONF_PARSER_TERMINATOR
Definition: cf_parse.h:626
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition: cf_parse.h:256
@ CONF_FLAG_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition: cf_parse.h:406
@ CONF_FLAG_ATTRIBUTE
Value must resolve to attribute in dict (deprecated, use CONF_FLAG_TMPL).
Definition: cf_parse.h:408
@ CONF_FLAG_NOT_EMPTY
CONF_PAIR is required to have a non zero length value.
Definition: cf_parse.h:421
@ CONF_FLAG_XLAT
string will be dynamically expanded.
Definition: cf_parse.h:417
@ CONF_FLAG_OK_MISSING
OK if it's missing.
Definition: cf_parse.h:428
@ T_BARE_WORD
Definition: token.h:120
@ T_DOUBLE_QUOTED_STRING
Definition: token.h:121

Definition at line 81 of file rlm_sqlcounter.c.

◆ rlm_sqlcounter

module_rlm_t rlm_sqlcounter
Initial value:
= {
.common = {
.name = "sqlcounter",
.inst_size = sizeof(rlm_sqlcounter_t),
.bootstrap = mod_bootstrap,
},
.method_names = (module_method_name_t[]){
{ .name1 = CF_IDENT_ANY, .name2 = CF_IDENT_ANY, .method = mod_authorize,
.method_env = &sqlcounter_call_env },
}
}
#define CF_IDENT_ANY
Definition: cf_util.h:78
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition: dl_module.h:65
Specifies a module method identifier.
Definition: module_method.c:36
static const conf_parser_t config[]
Definition: base.c:188
static int instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_rest.c:1312
static int mod_bootstrap(module_inst_ctx_t const *mctx)
static unlang_action_t mod_authorize(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Check the value of a counter retrieved from an SQL query with a limit
static const call_env_method_t sqlcounter_call_env
static const conf_parser_t module_config[]
static int mod_instantiate(module_inst_ctx_t const *mctx)
@ MODULE_TYPE_THREAD_SAFE
Module is threadsafe.
Definition: module.h:49
#define MODULE_NAME_TERMINATOR
Definition: module.h:135

Definition at line 578 of file rlm_sqlcounter.c.

◆ rlm_sqlcounter_dict

fr_dict_autoload_t rlm_sqlcounter_dict
Initial value:
= {
{ .out = &dict_freeradius, .proto = "freeradius" },
{ NULL }
}

Definition at line 113 of file rlm_sqlcounter.c.

◆ sqlcounter_call_env

const call_env_method_t sqlcounter_call_env
static
Initial value:
= {
.env = (call_env_parser_t[]){
.pair.func = call_env_query_parse },
}
}
#define CALL_ENV_TERMINATOR
Definition: call_env.h:212
#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_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
#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
static int call_env_query_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 call_env parser to tokenize the SQL query xlat used for counter retrieval.

Definition at line 557 of file rlm_sqlcounter.c.