The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: a79079a6889f252d5a56090fcb510d9f5f78aca8 $
20  * @file rlm_always.c
21  * @brief Return preconfigured fixed rcodes.
22  *
23  * @copyright 2000,2006 The FreeRADIUS server project
24  */
25 RCSID("$Id: a79079a6889f252d5a56090fcb510d9f5f78aca8 $")
26 
27 #define LOG_PREFIX mctx->inst->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  * The instance data for rlm_always is the list of fake values we are
35  * going to return.
36  */
37 typedef struct {
38  char const *rcode_str; //!< The base value.
40 
41  rlm_rcode_t rcode; //!< The integer constant representing rcode_str.
43  bool mpp;
44 } rlm_always_t;
45 
46 /*
47  * A mapping of configuration file names to internal variables.
48  */
49 static const conf_parser_t module_config[] = {
50  { FR_CONF_OFFSET("rcode", rlm_always_t, rcode_str), .dflt = "fail" },
51  { FR_CONF_OFFSET("simulcount", rlm_always_t, simulcount), .dflt = "0" },
52  { FR_CONF_OFFSET("mpp", rlm_always_t, mpp), .dflt = "no" },
54 };
55 
57  { .single = true, .type = FR_TYPE_STRING },
59 };
60 
61 /** Set module status or rcode
62  *
63  * Look ma, no locks...
64  *
65  * Example: %db_status(fail)
66  */
67 static xlat_action_t always_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out,
68  xlat_ctx_t const *xctx,
69  request_t *request, fr_value_box_list_t *in)
70 {
71  rlm_always_t *inst = talloc_get_type_abort(xctx->mctx->inst->data, rlm_always_t);
72  module_instance_t *mi = inst->mi;
73  char const *status;
74  char const *p;
75  fr_value_box_t *vb;
76  fr_value_box_t *in_head = fr_value_box_list_head(in);
77 
78  /*
79  * Expand to the existing status
80  */
81  p = "alive";
82  if (mi->force) {
83  p = fr_table_str_by_value(rcode_table, mi->code, "<invalid>");
84  }
85 
86  if (!in_head || in_head->vb_length == 0) goto done;
87  status = in_head->vb_strvalue;
88 
89  /*
90  * Set the module status
91  */
92  if (strcmp(status, "alive") == 0) {
93  mi->force = false;
94  } else {
95  int rcode;
96 
98  if (rcode == RLM_MODULE_NOT_SET) {
99  RWARN("Unknown status \"%s\"", status);
100  return XLAT_ACTION_FAIL;
101  }
102 
103  mi->code = rcode;
104  mi->force = true;
105 
106  }
107 
108 done:
109 
110  MEM(vb = fr_value_box_alloc_null(ctx));
111  fr_value_box_strdup(vb, vb, NULL, p, false);
112  fr_dcursor_append(out, vb);
113 
114  return XLAT_ACTION_DONE;
115 
116 }
117 
118 static int mod_bootstrap(module_inst_ctx_t const *mctx)
119 {
120  rlm_always_t *inst = talloc_get_type_abort(mctx->inst->data, rlm_always_t);
121  xlat_t *xlat;
122 
123  inst->mi = module_rlm_by_name(NULL, mctx->inst->name);
124  if (!inst->mi) {
125  cf_log_err(mctx->inst->conf, "Can't find the module instance data for this module: %s", mctx->inst->name);
126  return -1;
127  }
128 
131 
132  return 0;
133 }
134 
135 static int mod_instantiate(module_inst_ctx_t const *mctx)
136 {
137  rlm_always_t *inst = talloc_get_type_abort(mctx->inst->data, rlm_always_t);
138 
139  /*
140  * Convert the rcode string to an int
141  */
143  if (inst->rcode == RLM_MODULE_NOT_SET) {
144  cf_log_err(mctx->inst->conf, "rcode value \"%s\" is invalid", inst->rcode_str);
145  return -1;
146  }
147 
148  return 0;
149 }
150 
151 /*
152  * Just return the rcode ... this function is autz, auth, acct, and
153  * preacct!
154  */
155 static unlang_action_t CC_HINT(nonnull) mod_always_return(rlm_rcode_t *p_result, module_ctx_t const *mctx, UNUSED request_t *request)
156 {
158 
159  RETURN_MODULE_RCODE(inst->rcode);
160 }
161 
162 extern module_rlm_t rlm_always;
164  .common = {
165  .magic = MODULE_MAGIC_INIT,
166  .name = "always",
167  .inst_size = sizeof(rlm_always_t),
169  .bootstrap = mod_bootstrap,
171  },
172  .method_names = (module_method_name_t[]){
173  { .name1 = CF_IDENT_ANY, .name2 = CF_IDENT_ANY, .method = mod_always_return },
175  }
176 };
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition: action.h:35
#define RCSID(id)
Definition: build.h:444
#define UNUSED
Definition: build.h:313
#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
Defines a CONF_PAIR to C data type mapping.
Definition: cf_parse.h:563
#define cf_log_err(_cf, _fmt,...)
Definition: cf_util.h:265
#define CF_IDENT_ANY
Definition: cf_util.h:78
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition: dcursor.h:405
static fr_slen_t in
Definition: dict.h:645
char const *_CONST name
Instance name.
Definition: dl_module.h:163
void *_CONST data
Module instance's parsed configuration.
Definition: dl_module.h:165
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition: dl_module.h:65
CONF_SECTION *_CONST conf
Module's instance configuration.
Definition: dl_module.h:166
#define RWARN(fmt,...)
Definition: log.h:297
@ FR_TYPE_STRING
String of printable characters.
Definition: merged_model.c:83
unsigned int uint32_t
Definition: merged_model.c:33
dl_module_inst_t const * inst
Dynamic loader API handle for the module.
Definition: module_ctx.h:52
dl_module_inst_t const * inst
Dynamic loader API handle for the module.
Definition: module_ctx.h:42
Temporary structure to hold arguments for module calls.
Definition: module_ctx.h:41
Temporary structure to hold arguments for instantiation calls.
Definition: module_ctx.h:51
Specifies a module method identifier.
Definition: module_method.c:36
module_instance_t * module_rlm_by_name(module_instance_t const *parent, char const *asked_name)
Definition: module_rlm.c:785
module_t common
Common fields presented by all modules.
Definition: module_rlm.h:37
static const conf_parser_t config[]
Definition: base.c:188
static bool done
Definition: radclient.c:80
fr_table_num_sorted_t const rcode_table[]
Definition: rcode.c:35
#define RETURN_MODULE_RCODE(_rcode)
Definition: rcode.h:64
rlm_rcode_t
Return codes indicating the result of the module call.
Definition: rcode.h:40
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition: rcode.h:51
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:67
rlm_rcode_t rcode
The integer constant representing rcode_str.
Definition: rlm_always.c:41
module_instance_t * mi
Definition: rlm_always.c:39
static unlang_action_t mod_always_return(rlm_rcode_t *p_result, module_ctx_t const *mctx, UNUSED request_t *request)
Definition: rlm_always.c:155
char const * rcode_str
The base value.
Definition: rlm_always.c:38
uint32_t simulcount
Definition: rlm_always.c:42
module_rlm_t rlm_always
Definition: rlm_always.c:163
static int mod_bootstrap(module_inst_ctx_t const *mctx)
Definition: rlm_always.c:118
static xlat_arg_parser_t const always_xlat_args[]
Definition: rlm_always.c:56
static const conf_parser_t module_config[]
Definition: rlm_always.c:49
static int mod_instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_always.c:135
static int instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_rest.c:1312
bool force
Force the module to return a specific code.
Definition: module.h:200
#define MODULE_NAME_TERMINATOR
Definition: module.h:135
rlm_rcode_t code
Code module will return when 'force' has has been set to true.
Definition: module.h:203
Per instance data.
Definition: module.h:169
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
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:134
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition: table.h:253
#define talloc_get_type_abort_const
Definition: talloc.h:270
bool single
Argument must only contain a single box.
Definition: xlat.h:148
#define XLAT_ARG_PARSER_TERMINATOR
Definition: xlat.h:166
xlat_action_t
Definition: xlat.h:35
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition: xlat.h:42
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition: xlat.h:41
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:3876
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:619
static size_t char ** out
Definition: value.h:984
module_ctx_t const * mctx
Synthesised module calling ctx.
Definition: xlat_ctx.h:45
An xlat calling ctx.
Definition: xlat_ctx.h:42
int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition: xlat_func.c:360
xlat_t * xlat_func_register_module(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx, char const *name, xlat_func_t func, fr_type_t return_type)
Register an xlat function for a module.
Definition: xlat_func.c:274