The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
rlm_always.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/**
19 * $Id: 6532858d20c80569514ce466ae6dc9008583c6a4 $
20 * @file rlm_always.c
21 * @brief Return preconfigured fixed rcodes.
22 *
23 * @copyright 2000,2006 The FreeRADIUS server project
24 */
25RCSID("$Id: 6532858d20c80569514ce466ae6dc9008583c6a4 $")
26
27#define LOG_PREFIX mctx->mi->name
28
29#include <freeradius-devel/server/base.h>
30#include <freeradius-devel/server/module_rlm.h>
31#include <freeradius-devel/unlang/xlat_func.h>
32
33/*
34 * Instance data is mprotected for runtime
35 * this is fine for the majority of module
36 * instances, but not for rlm_always.
37 *
38 * This struct is allocated outside of the
39 */
40typedef struct {
41 rlm_rcode_t rcode; //!< The integer constant representing rcode_str.
42 bool force; //!< If true, we force the rcode.
44
45/*
46 * The instance data for rlm_always is the list of fake values we are
47 * going to return.
48 */
49typedef struct {
50 char const *rcode_str; //!< The base value.
54
55/*
56 * A mapping of configuration file names to internal variables.
57 */
58static const conf_parser_t module_config[] = {
59 { FR_CONF_OFFSET("rcode", rlm_always_t, rcode_str), .dflt = "fail" },
61};
62
64 { .single = true, .type = FR_TYPE_STRING },
66};
67
68/** Set module status or rcode
69 *
70 * Look ma, no locks...
71 *
72 * Example: %db_status('fail')
73 */
74static xlat_action_t always_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out,
75 xlat_ctx_t const *xctx,
76 request_t *request, fr_value_box_list_t *in)
77{
78 rlm_always_t *inst = talloc_get_type_abort(xctx->mctx->mi->data, rlm_always_t);
79 module_instance_t *mi = inst->mi;
80 char const *status;
81 char const *p;
83 fr_value_box_t *in_head = fr_value_box_list_head(in);
84
85 /*
86 * Expand to the existing status
87 */
88 p = "alive";
89 if (mi->force) {
90 p = fr_table_str_by_value(rcode_table, mi->code, "<invalid>");
91 }
92
93 if (!in_head || in_head->vb_length == 0) goto done;
94 status = in_head->vb_strvalue;
95
96 /*
97 * Set the module status
98 */
99 if (strcmp(status, "alive") == 0) {
100 mi->force = false;
101 } else {
102 int rcode;
103
105 if (rcode == RLM_MODULE_NOT_SET) {
106 RWARN("Unknown status \"%s\"", status);
107 return XLAT_ACTION_FAIL;
108 }
109
110 mi->code = rcode;
111 mi->force = true;
112
113 }
114
115done:
116
117 MEM(vb = fr_value_box_alloc_null(ctx));
118 fr_value_box_strdup(vb, vb, NULL, p, false);
120
121 return XLAT_ACTION_DONE;
122
123}
124
125/*
126 * Just return the rcode ... this function is autz, auth, acct, and
127 * preacct!
128 */
130{
132
133 RETURN_UNLANG_RCODE(inst->mutable->rcode);
134}
135
136static int mod_detach(module_detach_ctx_t const *mctx)
137{
138 rlm_always_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_always_t);
139
140 talloc_free(inst->mutable);
141 return 0;
142}
143
144static int mod_instantiate(module_inst_ctx_t const *mctx)
145{
146 rlm_always_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_always_t);
147
148 inst->mi = UNCONST(module_instance_t *, mctx->mi);
149 if (!inst->mi) {
150 cf_log_err(mctx->mi->conf, "Can't find the module instance data for this module: %s", mctx->mi->name);
151 return -1;
152 }
153
154 /*
155 * Allocate this outside of the module instance data,
156 * as that gets mprotected
157 */
158 MEM(inst->mutable = talloc_zero(NULL, rlm_always_mutable_t));
159
160 /*
161 * Convert the rcode string to an int
162 */
163 inst->mutable->rcode = fr_table_value_by_str(rcode_table, inst->rcode_str, RLM_MODULE_NOT_SET);
164 if (inst->mutable->rcode == RLM_MODULE_NOT_SET) {
165 cf_log_err(mctx->mi->conf, "rcode value \"%s\" is invalid", inst->rcode_str);
166 return -1;
167 }
168
169 return 0;
170}
171
172static int mod_bootstrap(module_inst_ctx_t const *mctx)
173{
174 xlat_t *xlat;
175
176 xlat = module_rlm_xlat_register(mctx->mi->boot, mctx, NULL, always_xlat, FR_TYPE_STRING);
178
179 return 0;
180}
181
184 .common = {
185 .magic = MODULE_MAGIC_INIT,
186 .name = "always",
187 .inst_size = sizeof(rlm_always_t),
189 .bootstrap = mod_bootstrap,
190 .instantiate = mod_instantiate,
191 .detach = mod_detach
192 },
193 .method_group = {
194 .bindings = (module_method_binding_t[]){
195 { .section = SECTION_NAME(CF_IDENT_ANY, CF_IDENT_ANY), .method = mod_always_return },
197 }
198 }
199};
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:168
#define RCSID(id)
Definition build.h:488
#define UNUSED
Definition build.h:318
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:657
#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:280
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:594
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:285
#define CF_IDENT_ANY
Definition cf_util.h:75
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:406
#define MEM(x)
Definition debug.h:46
static fr_slen_t in
Definition dict.h:882
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
talloc_free(hp)
#define RWARN(fmt,...)
Definition log.h:309
@ FR_TYPE_STRING
String of printable characters.
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:42
module_instance_t * mi
Module instance to detach.
Definition module_ctx.h:57
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for module calls.
Definition module_ctx.h:41
Temporary structure to hold arguments for detach calls.
Definition module_ctx.h:56
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
xlat_t * module_rlm_xlat_register(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx, char const *name, xlat_func_t func, fr_type_t return_type)
Definition module_rlm.c:247
module_t common
Common fields presented by all modules.
Definition module_rlm.h:39
static const conf_parser_t config[]
Definition base.c:163
static bool done
Definition radclient.c:80
fr_table_num_sorted_t const rcode_table[]
Definition rcode.c:35
#define RETURN_UNLANG_RCODE(_rcode)
Definition rcode.h:61
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:44
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:45
static int mod_detach(module_detach_ctx_t const *mctx)
Definition rlm_always.c:136
static xlat_action_t always_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Set module status or rcode.
Definition rlm_always.c:74
module_instance_t * mi
Definition rlm_always.c:51
static unlang_action_t mod_always_return(unlang_result_t *p_result, module_ctx_t const *mctx, UNUSED request_t *request)
Definition rlm_always.c:129
char const * rcode_str
The base value.
Definition rlm_always.c:50
module_rlm_t rlm_always
Definition rlm_always.c:183
static int mod_bootstrap(module_inst_ctx_t const *mctx)
Definition rlm_always.c:172
static xlat_arg_parser_t const always_xlat_args[]
Definition rlm_always.c:63
static const conf_parser_t module_config[]
Definition rlm_always.c:58
bool force
If true, we force the rcode.
Definition rlm_always.c:42
static int mod_instantiate(module_inst_ctx_t const *mctx)
Definition rlm_always.c:144
rlm_rcode_t rcode
The integer constant representing rcode_str.
Definition rlm_always.c:41
#define SECTION_NAME(_name1, _name2)
Define a section name consisting of a verb and a noun.
Definition section.h:39
char const * name
Instance name e.g. user_database.
Definition module.h:355
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:349
size_t inst_size
Size of the module's instance data.
Definition module.h:212
bool force
Force the module to return a specific code.
Definition module.h:317
void * data
Module's instance data.
Definition module.h:291
void * boot
Data allocated during the boostrap phase.
Definition module.h:294
rlm_rcode_t code
Code module will return when 'force' has has been set to true.
Definition module.h:320
#define MODULE_BINDING_TERMINATOR
Terminate a module binding list.
Definition module.h:152
Module instance data.
Definition module.h:285
Named methods exported by a module.
Definition module.h:174
eap_aka_sim_process_conf_t * inst
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:653
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
#define talloc_get_type_abort_const
Definition talloc.h:110
unsigned int single
Argument must only contain a single box.
Definition xlat.h:148
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:170
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
Definition for a single argument consumend by an xlat function.
Definition xlat.h:145
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
Definition value.c:4633
int nonnull(2, 5))
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:655
static size_t char ** out
Definition value.h:1030
module_ctx_t const * mctx
Synthesised module calling ctx.
Definition xlat_ctx.h:52
An xlat calling ctx.
Definition xlat_ctx.h:49
int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition xlat_func.c:363