The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
module.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /**
19  * $Id: f725600b1b874e3e68e17303ff810ba5582adfe2 $
20  *
21  * @file lib/server/module.h
22  * @brief Interface to the FreeRADIUS module system.
23  *
24  * @copyright 2022 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
25  * @copyright 2013 The FreeRADIUS server project
26  */
27 RCSIDH(modules_h, "$Id: f725600b1b874e3e68e17303ff810ba5582adfe2 $")
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <freeradius-devel/server/cf_util.h>
34 #include <freeradius-devel/server/request.h>
35 #include <freeradius-devel/server/module_ctx.h>
36 #include <freeradius-devel/unlang/action.h>
37 #include <freeradius-devel/unlang/compile.h>
38 #include <freeradius-devel/unlang/call_env.h>
39 #include <freeradius-devel/util/event.h>
40 
41 typedef struct module_s module_t;
45 typedef struct module_list_t module_list_t;
46 
47 DIAG_OFF(attributes)
48 typedef enum CC_HINT(flag_enum) {
49  MODULE_TYPE_THREAD_SAFE = (0 << 0), //!< Module is threadsafe.
50  MODULE_TYPE_THREAD_UNSAFE = (1 << 0), //!< Module is not threadsafe.
51  //!< Server will protect calls with mutex.
52  MODULE_TYPE_RESUMABLE = (1 << 2), //!< does yield / resume
53 
54  MODULE_TYPE_RETRY = (1 << 3) //!< can handle retries
56 DIAG_ON(attributes)
57 
58 /** Module section callback
59  *
60  * Is called when the module is listed in a particular section of a virtual
61  * server, and the request has reached the module call.
62  *
63  * @param[out] p_result Result code of the module method.
64  * @param[in] mctx Holds global instance data, thread instance
65  * data and call specific instance data.
66  * @param[in] request to process.
67  * @return the appropriate rcode.
68  */
69 typedef unlang_action_t (*module_method_t)(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request);
70 
71 /** Module instantiation callback
72  *
73  * Is called once per module instance. Is not called when new threads are
74  * spawned. See module_thread_instantiate_t for that.
75  *
76  * @param[in] mctx Holds global instance data.
77  * @return
78  * - 0 on success.
79  * - -1 if instantiation failed.
80  */
81 typedef int (*module_instantiate_t)(module_inst_ctx_t const *mctx);
82 
83 /** Module thread creation callback
84  *
85  * Called whenever a new thread is created.
86  *
87  * @param[in] mctx Holds global instance data, thread instance
88  * data, and the thread-specific event list.
89  * @return
90  * - 0 on success.
91  * - -1 if instantiation failed.
92  */
94 
95 /** Module thread destruction callback
96  *
97  * Destroy a module/thread instance.
98  *
99  * @param[in] mctx Holds global instance data, thread instance
100  * data, and the thread-specific event list.
101  * @return
102  * - 0 on success.
103  * - -1 if instantiation failed.
104  */
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #include <freeradius-devel/server/components.h>
112 #include <freeradius-devel/server/dl_module.h>
113 #include <freeradius-devel/server/exfile.h>
114 #include <freeradius-devel/server/pool.h>
115 #include <freeradius-devel/server/rcode.h>
116 
117 #include <freeradius-devel/io/schedule.h>
118 #include <freeradius-devel/features.h>
119 
120 #ifdef __cplusplus
121 extern "C" {
122 #endif
123 
124 /** Named methods exported by a module
125  *
126  */
128  char const *name1; //!< i.e. "recv", "send", "process"
129  char const *name2; //!< The packet type i.e Access-Request, Access-Reject.
130 
131  module_method_t method; //!< Module method to call
132  call_env_method_t const *method_env; //!< Call specific conf parsing.
133 };
134 
135 #define MODULE_NAME_TERMINATOR { NULL }
136 
137 /** Struct exported by a rlm_* module
138  *
139  * Determines the capabilities of the module, and maps internal functions
140  * within the module to different sections.
141  */
142 struct module_s {
143  DL_MODULE_COMMON; //!< Common fields for all loadable modules.
144 
147  int flags; /* flags */
150  char const *thread_inst_type;
152 };
153 
154 /** What state the module instance is currently in
155  *
156  */
157 typedef enum {
162 
163 /** Per instance data
164  *
165  * Per-instance data structure, to correlate the modules with the
166  * instance names (may NOT be the module names!), and the per-instance
167  * data structures.
168  */
170  fr_heap_index_t inst_idx; //!< Entry in the bootstrap/instantiation heap.
171  //!< should be an identical value to the thread-specific
172  ///< data for this module.
173 
174  fr_rb_node_t name_node; //!< Entry in the name tree.
175  fr_rb_node_t data_node; //!< Entry in the data tree.
176 
177  module_list_t *ml; //!< Module list this instance belongs to.
178 
179  uint32_t number; //!< Unique module number.
180 
181  char const *name; //!< Instance name e.g. user_database.
182 
183  dl_module_inst_t *dl_inst; //!< Structure containing the module's instance data,
184  //!< configuration, and dl handle. This can be used
185  ///< to access the parsed configuration data for the
186  ///< module.
187 
188  module_t const *module; //!< Public module structure. Cached for convenience.
189  ///< This exports module methods, i.e. the functions
190  ///< which allow the module to perform actions.
191 
192  pthread_mutex_t mutex; //!< Used prevent multiple threads entering a thread
193  ///< unsafe module simultaneously.
194 
195  module_instance_state_t state; //!< What's been done with this module so far.
196 
197  /** @name Return code overrides
198  * @{
199  */
200  bool force; //!< Force the module to return a specific code.
201  //!< Usually set via an administrative interface.
202 
203  rlm_rcode_t code; //!< Code module will return when 'force' has
204  //!< has been set to true.
205 
206  unlang_actions_t actions; //!< default actions and retries.
207 
208  /** @} */
209 };
210 
211 /** Per thread per instance data
212  *
213  * Stores module and thread specific data.
214  */
216  fr_heap_index_t inst_idx; //!< Entry in the thread-specific bootstrap heap.
217  ///< Should be an identical value to the global
218  ///< instance data for the same module.
219 
220  void *data; //!< Thread specific instance data.
221 
222  fr_event_list_t *el; //!< Event list associated with this thread.
223 
224  module_instance_t const *mi; //!< As opposed to the thread local inst.
225 
226  uint64_t total_calls; //! total number of times we've been called
227  uint64_t active_callers; //! number of active callers. i.e. number of current yields
228 };
229 
230 /** A list of modules
231  *
232  * This allows modules to be instantiated and freed in phases,
233  * i.e. proto modules before rlm modules.
234  */
236  uint32_t last_number; //!< Last identifier assigned to a module instance.
237  char const *name; //!< Friendly list identifier.
238  fr_rb_tree_t *name_tree; //!< Modules indexed by name.
239  fr_rb_tree_t *data_tree; //!< Modules indexed by data.
240 };
241 
242 /** Map string values to module state method
243  *
244  */
245 typedef struct {
246  char const *name; //!< String identifier for state.
247  module_method_t func; //!< State function.
249 
250 /** @name Callbacks for the conf_parser_t
251  *
252  * @{
253  */
254 int module_submodule_parse(UNUSED TALLOC_CTX *ctx, void *out, void *parent,
255  CONF_ITEM *ci, UNUSED conf_parser_t const *rule) CC_HINT(warn_unused_result);
256 /** @} */
257 
258 /** @name Module and module thread lookup
259  *
260  * @{
261  */
262 module_instance_t *module_parent(module_instance_t const *child) CC_HINT(warn_unused_result);
263 
264 module_instance_t *module_root(module_instance_t const *child); CC_HINT(warn_unused_result)
265 
266 module_instance_t *module_by_name(module_list_t const *ml, module_instance_t const *parent, char const *asked_name)
267  CC_HINT(nonnull(1,3)) CC_HINT(warn_unused_result);
268 
269 module_instance_t *module_by_data(module_list_t const *ml, void const *data) CC_HINT(warn_unused_result);
270 
271 module_thread_instance_t *module_thread(module_instance_t *mi) CC_HINT(warn_unused_result);
272 
273 module_thread_instance_t *module_thread_by_data(module_list_t const *ml, void const *data) CC_HINT(warn_unused_result);
274 /** @} */
275 
276 /** @name Module and module thread initialisation and instantiation
277  *
278  * @{
279  */
281 
282 void modules_thread_detach(module_list_t const *ml);
283 
284 int modules_thread_instantiate(TALLOC_CTX *ctx, module_list_t const *ml, fr_event_list_t *el) CC_HINT(nonnull) CC_HINT(warn_unused_result);
285 
286 int module_instantiate(module_instance_t *mi) CC_HINT(nonnull) CC_HINT(warn_unused_result);
287 
288 int modules_instantiate(module_list_t const *ml) CC_HINT(nonnull) CC_HINT(warn_unused_result);
289 
290 int module_bootstrap(module_instance_t *mi) CC_HINT(nonnull) CC_HINT(warn_unused_result);
291 
292 int modules_bootstrap(module_list_t const *ml) CC_HINT(nonnull) CC_HINT(warn_unused_result);
293 
294 int module_conf_parse(module_instance_t *mi, CONF_SECTION *mod_cs) CC_HINT(nonnull) CC_HINT(warn_unused_result);
295 
297  module_instance_t const *parent,
298  dl_module_type_t type, char const *mod_name, char const *inst_name)
299  CC_HINT(nonnull(1)) CC_HINT(warn_unused_result);
300 
301 module_list_t *module_list_alloc(TALLOC_CTX *ctx, char const *name) CC_HINT(warn_unused_result);
302 
303 void modules_init(char const *lib_dir);
304 /** @} */
305 
306 #ifdef __cplusplus
307 }
308 #endif
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition: action.h:35
#define DIAG_ON(_x)
Definition: build.h:419
#define RCSIDH(h, id)
Definition: build.h:445
#define UNUSED
Definition: build.h:313
#define DIAG_OFF(_x)
Definition: build.h:418
Defines a CONF_PAIR to C data type mapping.
Definition: cf_parse.h:563
Common header for all CONF_* types.
Definition: cf_priv.h:49
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:89
dl_module_type_t
Definition: dl_module.h:67
A module/inst tuple.
Definition: dl_module.h:162
unsigned int fr_heap_index_t
Definition: heap.h:80
Stores all information relating to an event list.
Definition: event.c:411
static char const * mod_name(fr_listen_t *li)
Definition: master.c:2704
unsigned int uint32_t
Definition: merged_model.c:33
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
Temporary structure to hold arguments for thread_instantiation calls.
Definition: module_ctx.h:58
Specifies a module method identifier.
Definition: module_method.c:36
The main red black tree structure.
Definition: rb.h:73
rlm_rcode_t
Return codes indicating the result of the module call.
Definition: rcode.h:40
static char const * name
module_instance_t const * mi
As opposed to the thread local inst.
Definition: module.h:224
char const * name
String identifier for state.
Definition: module.h:246
unlang_actions_t actions
default actions and retries.
Definition: module.h:206
module_instance_t * module_by_name(module_list_t const *ml, module_instance_t const *parent, char const *asked_name))
Find an existing module instance by its name and parent.
Definition: module.c:367
void modules_init(char const *lib_dir)
Perform global initialisation for modules.
Definition: module.c:1163
char const * name
Instance name e.g. user_database.
Definition: module.h:181
module_flags_t
Definition: module.h:48
@ MODULE_TYPE_THREAD_UNSAFE
Module is not threadsafe.
Definition: module.h:50
@ MODULE_TYPE_RESUMABLE
does yield / resume
Definition: module.h:52
@ MODULE_TYPE_RETRY
can handle retries
Definition: module.h:54
@ MODULE_TYPE_THREAD_SAFE
Module is threadsafe.
Definition: module.h:49
fr_rb_node_t name_node
Entry in the name tree.
Definition: module.h:174
uint64_t active_callers
total number of times we've been called
Definition: module.h:227
bool force
Force the module to return a specific code.
Definition: module.h:200
module_thread_instance_t * module_thread(module_instance_t *mi)
Retrieve module/thread specific instance for a module.
Definition: module.c:459
module_instance_state_t state
What's been done with this module so far.
Definition: module.h:195
int module_submodule_parse(UNUSED TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
module_thread_instantiate_t thread_instantiate
Definition: module.h:148
module_instantiate_t instantiate
Definition: module.h:146
int modules_thread_instantiate(TALLOC_CTX *ctx, module_list_t const *ml, fr_event_list_t *el)
Creates per-thread instance data for modules which need it.
Definition: module.c:663
module_instance_t * module_alloc(module_list_t *ml, module_instance_t const *parent, dl_module_type_t type, char const *mod_name, char const *inst_name))
Allocate a new module and add it to a module list for later bootstrap/instantiation.
Definition: module.c:976
char const * name2
The packet type i.e Access-Request, Access-Reject.
Definition: module.h:129
int modules_instantiate(module_list_t const *ml)
Completes instantiation of modules.
Definition: module.c:759
fr_rb_tree_t * name_tree
Modules indexed by name.
Definition: module.h:238
call_env_method_t const * method_env
Call specific conf parsing.
Definition: module.h:132
int(* module_instantiate_t)(module_inst_ctx_t const *mctx)
Module instantiation callback.
Definition: module.h:81
module_instance_t * module_by_data(module_list_t const *ml, void const *data)
Find an existing module instance by its private instance data.
Definition: module.c:438
dl_module_inst_t * dl_inst
Structure containing the module's instance data, configuration, and dl handle.
Definition: module.h:183
void * data
Thread specific instance data.
Definition: module.h:220
rlm_rcode_t code
Code module will return when 'force' has has been set to true.
Definition: module.h:203
module_thread_instance_t * module_thread_by_data(module_list_t const *ml, void const *data)
Retrieve module/thread specific instance data for a module.
Definition: module.c:485
void modules_thread_detach(module_list_t const *ml)
Remove thread-specific data for a given module list.
Definition: module.c:516
void module_free(module_instance_t *mi)
Explicitly free a module if a fatal error occurs during bootstrap.
Definition: module.c:507
fr_rb_tree_t * data_tree
Modules indexed by data.
Definition: module.h:239
fr_heap_index_t inst_idx
Entry in the bootstrap/instantiation heap.
Definition: module.h:170
char const * name1
i.e. "recv", "send", "process"
Definition: module.h:128
module_instance_state_t
What state the module instance is currently in.
Definition: module.h:157
@ MODULE_INSTANCE_INSTANTIATED
Definition: module.h:160
@ MODULE_INSTANCE_INIT
Definition: module.h:158
@ MODULE_INSTANCE_BOOTSTRAPPED
Definition: module.h:159
int(* module_thread_detach_t)(module_thread_inst_ctx_t const *mctx)
Module thread destruction callback.
Definition: module.h:105
uint32_t number
Unique module number.
Definition: module.h:179
module_method_t method
Module method to call.
Definition: module.h:131
module_instance_t * module_root(module_instance_t const *child)
Find the module's shallowest parent.
Definition: module.c:416
int module_instantiate(module_instance_t *mi)
Manually complete module setup by calling its instantiate function.
Definition: module.c:702
char const * thread_inst_type
Definition: module.h:150
module_instantiate_t bootstrap
Definition: module.h:145
int modules_bootstrap(module_list_t const *ml)
Bootstrap any modules which have not been bootstrapped already.
Definition: module.c:830
fr_rb_node_t data_node
Entry in the data tree.
Definition: module.h:175
DL_MODULE_COMMON
Common fields for all loadable modules.
Definition: module.h:143
module_thread_detach_t thread_detach
Definition: module.h:149
int flags
Definition: module.h:147
int(* module_thread_instantiate_t)(module_thread_inst_ctx_t const *mctx)
Module thread creation callback.
Definition: module.h:93
int module_conf_parse(module_instance_t *mi, CONF_SECTION *mod_cs)
Parse the configuration associated with a module.
Definition: module.c:946
module_t const * module
Public module structure.
Definition: module.h:188
uint32_t last_number
Last identifier assigned to a module instance.
Definition: module.h:236
module_list_t * ml
Module list this instance belongs to.
Definition: module.h:177
size_t thread_inst_size
Definition: module.h:151
char const * name
Friendly list identifier.
Definition: module.h:237
fr_event_list_t * el
Event list associated with this thread.
Definition: module.h:222
fr_heap_index_t inst_idx
Entry in the thread-specific bootstrap heap.
Definition: module.h:216
module_method_t func
State function.
Definition: module.h:247
module_instance_t * module_parent(module_instance_t const *child)
Find the module's parent (if any)
Definition: module.c:399
unlang_action_t(* module_method_t)(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Module section callback.
Definition: module.h:69
pthread_mutex_t mutex
Used prevent multiple threads entering a thread unsafe module simultaneously.
Definition: module.h:192
module_list_t * module_list_alloc(TALLOC_CTX *ctx, char const *name)
Allocate a new module list.
Definition: module.c:1118
int module_bootstrap(module_instance_t *mi)
Manually complete module bootstrap by calling its instantiate function.
Definition: module.c:788
Per instance data.
Definition: module.h:169
A list of modules.
Definition: module.h:235
Named methods exported by a module.
Definition: module.h:127
Struct exported by a rlm_* module.
Definition: module.h:142
Map string values to module state method.
Definition: module.h:245
Per thread per instance data.
Definition: module.h:215
fr_aka_sim_id_type_t type
static fr_event_list_t * el
static fr_slen_t parent
Definition: pair.h:844
static fr_slen_t data
Definition: value.h:1259
int nonnull(2, 5))
static size_t char ** out
Definition: value.h:984