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

Translates requests between the server an a python interpreter. More...

#include <freeradius-devel/server/base.h>
#include <freeradius-devel/server/module_rlm.h>
#include <freeradius-devel/server/pairmove.h>
#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/util/lsan.h>
#include <Python.h>
#include <frameobject.h>
#include <libgen.h>
#include <dlfcn.h>
+ Include dependency graph for rlm_python.c:

Go to the source code of this file.

Data Structures

struct  libpython_global_config_t
 Global config for python library. More...
 
struct  python_func_def_t
 Specifies the module.function to load for processing a section. More...
 
struct  rlm_python_t
 An instance of the rlm_python module. More...
 
struct  rlm_python_thread_t
 Tracks a python module inst/thread state pair. More...
 

Macros

#define A(x)
 
#define A(x)   { #x, x },
 
#define LOAD_INFO(_fmt, ...)   fr_log(LOG_DST, L_INFO, __FILE__, __LINE__, "rlm_python - " _fmt, ## __VA_ARGS__)
 
#define LOAD_WARN(_fmt, ...)
 
#define LOG_PREFIX   mctx->inst->name
 
#define MOD_FUNC(x)
 
#define PYTHON_FUNC_DESTROY(_x)   python_function_destroy(&inst->_x)
 
#define PYTHON_FUNC_LOAD(_x)   if (python_function_load(mctx, &inst->_x) < 0) goto error
 

Functions

static unlang_action_t do_python (rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request, PyObject *p_func, char const *funcname)
 Thread safe call to a python function. More...
 
static unlang_action_t do_python_single (rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request, PyObject *p_func, char const *funcname)
 
static void libpython_free (void)
 
static int libpython_init (void)
 
static int mod_detach (module_detach_ctx_t const *mctx)
 
static int mod_instantiate (module_inst_ctx_t const *mctx)
 
static PyObject * mod_log (UNUSED PyObject *module, PyObject *args)
 Allow fr_log to be called from python. More...
 
static int mod_populate_vptuple (module_ctx_t const *mctx, request_t *request, PyObject *pp, fr_pair_t *vp)
 
static int mod_thread_detach (module_thread_inst_ctx_t const *mctx)
 
static int mod_thread_instantiate (module_thread_inst_ctx_t const *mctx)
 
static void mod_vptuple (TALLOC_CTX *ctx, module_ctx_t const *mctx, request_t *request, fr_pair_list_t *vps, PyObject *p_value, char const *funcname, char const *list_name)
 
static void python_error_log (module_ctx_t const *mctx, request_t *request)
 Print out the current error. More...
 
static void python_function_destroy (python_func_def_t *def)
 
static int python_function_load (module_inst_ctx_t const *mctx, python_func_def_t *def)
 Import a user module and load a function from it. More...
 
static void python_interpreter_free (rlm_python_t *inst, PyThreadState *interp)
 
static int python_interpreter_init (module_inst_ctx_t const *mctx)
 
static int python_module_import_config (module_inst_ctx_t const *mctx, CONF_SECTION *conf, PyObject *module)
 Make the current instance's config available within the module we're initialising. More...
 
static int python_module_import_constants (module_inst_ctx_t const *mctx, PyObject *module)
 Import integer constants into the module we're initialising. More...
 
static PyObject * python_module_init (void)
 
static void python_obj_destroy (PyObject **ob)
 
static int python_parse_config (module_inst_ctx_t const *mctx, CONF_SECTION *cs, int lvl, PyObject *dict)
 

Variables

static CONF_SECTIONcurrent_conf
 Used for communication with inittab functions. More...
 
static module_ctx_t const * current_mctx
 Used for communication with inittab functions. More...
 
struct {
char const * name
 
int value
 
freeradius_constants []
 
static PyThreadState * global_interpreter
 Our first interpreter. More...
 
static libpython_global_config_t libpython_global_config
 
static conf_parser_t module_config []
 
static PyMethodDef module_methods []
 
static void * python_dlhandle
 
static conf_parser_t const python_global_config []
 
module_rlm_t rlm_python
 
global_lib_autoinst_t rlm_python_autoinst
 
global_lib_autoinst_t const *const rlm_python_lib []
 

Detailed Description

Translates requests between the server an a python interpreter.

Id
85cc03065976de6177711ca80d0bdbf95e8f6429
Note
Rewritten by Paul P. Komkoff Jr i@sti.nosp@m.ngr..nosp@m.net.

Definition in file rlm_python.c.


Data Structure Documentation

◆ libpython_global_config_t

struct libpython_global_config_t

Global config for python library.

Definition at line 78 of file rlm_python.c.

Data Fields
char const * path Path to search for python files in.
bool path_include_default Include the default python path in path

◆ python_func_def_t

struct python_func_def_t

Specifies the module.function to load for processing a section.

Definition at line 46 of file rlm_python.c.

Data Fields
PyObject * function Python reference to function in module.
char const * function_name String name of function in module.
PyObject * module Python reference to module.
char const * module_name String name of module.

◆ rlm_python_t

struct rlm_python_t

An instance of the rlm_python module.

Definition at line 57 of file rlm_python.c.

+ Collaboration diagram for rlm_python_t:
Data Fields
python_func_def_t accounting
python_func_def_t authenticate
python_func_def_t authorize
python_func_def_t detach
python_func_def_t instantiate
PyThreadState * interpreter The interpreter used for this instance of rlm_python.
PyObject * module Local, interpreter specific module.
char const * name Name of the module instance.
python_func_def_t post_auth
python_func_def_t preacct
PyObject * pythonconf_dict Configuration parameters defined in the module made available to the python script.

◆ rlm_python_thread_t

struct rlm_python_thread_t

Tracks a python module inst/thread state pair.

Multiple instances of python create multiple interpreters and each thread must have a PyThreadState per interpreter, to track execution.

Definition at line 88 of file rlm_python.c.

Data Fields
PyThreadState * state Module instance/thread specific state.

Macro Definition Documentation

◆ A [1/2]

#define A (   x)
Value:
{ FR_CONF_OFFSET("mod_" #x, rlm_python_t, x.module_name), .dflt = "${.module}" }, \
{ FR_CONF_OFFSET("func_" #x, rlm_python_t, x.function_name) },
#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
An instance of the rlm_python module.
Definition: rlm_python.c:57

◆ A [2/2]

#define A (   x)    { #x, x },

◆ LOAD_INFO

#define LOAD_INFO (   _fmt,
  ... 
)    fr_log(LOG_DST, L_INFO, __FILE__, __LINE__, "rlm_python - " _fmt, ## __VA_ARGS__)

◆ LOAD_WARN

#define LOAD_WARN (   _fmt,
  ... 
)
Value:
fr_log_perror(LOG_DST, L_WARN, __FILE__, __LINE__, \
.first_prefix = "rlm_python - ", \
.subsq_prefix = "rlm_python - ", \
}, \
_fmt, ## __VA_ARGS__)
#define LOG_DST
Definition: network.c:29
void fr_log_perror(fr_log_t const *log, fr_log_type_t type, char const *file, int line, fr_log_perror_format_t const *rules, char const *fmt,...)
Drain any outstanding messages from the fr_strerror buffers.
Definition: log.c:725
@ L_WARN
Warning.
Definition: log.h:57

◆ LOG_PREFIX

#define LOG_PREFIX   mctx->inst->name

Definition at line 30 of file rlm_python.c.

◆ MOD_FUNC

#define MOD_FUNC (   x)
Value:
static unlang_action_t mod_##x(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request) \
{ \
rlm_python_t const *inst = talloc_get_type_abort_const(mctx->inst->data, rlm_python_t); \
return do_python(p_result, mctx, request, inst->x.function, #x);\
}
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition: action.h:35
Temporary structure to hold arguments for module calls.
Definition: module_ctx.h:41
rlm_rcode_t
Return codes indicating the result of the module call.
Definition: rcode.h:40
static unlang_action_t do_python(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request, PyObject *p_func, char const *funcname)
Thread safe call to a python function.
Definition: rlm_python.c:607
eap_aka_sim_process_conf_t * inst
#define talloc_get_type_abort_const
Definition: talloc.h:270

Definition at line 627 of file rlm_python.c.

◆ PYTHON_FUNC_DESTROY

#define PYTHON_FUNC_DESTROY (   _x)    python_function_destroy(&inst->_x)

◆ PYTHON_FUNC_LOAD

#define PYTHON_FUNC_LOAD (   _x)    if (python_function_load(mctx, &inst->_x) < 0) goto error

Function Documentation

◆ do_python()

static unlang_action_t do_python ( rlm_rcode_t p_result,
module_ctx_t const *  mctx,
request_t request,
PyObject *  p_func,
char const *  funcname 
)
static

Thread safe call to a python function.

Will swap in thread state specific to module/thread.

Definition at line 607 of file rlm_python.c.

+ Here is the call graph for this function:

◆ do_python_single()

static unlang_action_t do_python_single ( rlm_rcode_t p_result,
module_ctx_t const *  mctx,
request_t request,
PyObject *  p_func,
char const *  funcname 
)
static

Definition at line 476 of file rlm_python.c.

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

◆ libpython_free()

static void libpython_free ( void  )
static

Definition at line 1146 of file rlm_python.c.

◆ libpython_init()

static int libpython_init ( void  )
static

Definition at line 1054 of file rlm_python.c.

+ Here is the call graph for this function:

◆ mod_detach()

static int mod_detach ( module_detach_ctx_t const *  mctx)
static

Definition at line 977 of file rlm_python.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 927 of file rlm_python.c.

+ Here is the call graph for this function:

◆ mod_log()

static PyObject* mod_log ( UNUSED PyObject *  module,
PyObject *  args 
)
static

Allow fr_log to be called from python.

Definition at line 204 of file rlm_python.c.

+ Here is the call graph for this function:

◆ mod_populate_vptuple()

static int mod_populate_vptuple ( module_ctx_t const *  mctx,
request_t request,
PyObject *  pp,
fr_pair_t vp 
)
static

Definition at line 373 of file rlm_python.c.

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

◆ mod_thread_detach()

static int mod_thread_detach ( module_thread_inst_ctx_t const *  mctx)
static

Definition at line 1041 of file rlm_python.c.

◆ mod_thread_instantiate()

static int mod_thread_instantiate ( module_thread_inst_ctx_t const *  mctx)
static

Definition at line 1023 of file rlm_python.c.

◆ mod_vptuple()

static void mod_vptuple ( TALLOC_CTX *  ctx,
module_ctx_t const *  mctx,
request_t request,
fr_pair_list_t vps,
PyObject *  p_value,
char const *  funcname,
char const *  list_name 
)
static

Definition at line 281 of file rlm_python.c.

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

◆ python_error_log()

static void python_error_log ( module_ctx_t const *  mctx,
request_t request 
)
static

Print out the current error.

Must be called with a valid thread state set

Definition at line 231 of file rlm_python.c.

+ Here is the caller graph for this function:

◆ python_function_destroy()

static void python_function_destroy ( python_func_def_t def)
static

Definition at line 648 of file rlm_python.c.

+ Here is the call graph for this function:

◆ python_function_load()

static int python_function_load ( module_inst_ctx_t const *  mctx,
python_func_def_t def 
)
static

Import a user module and load a function from it.

Definition at line 657 of file rlm_python.c.

+ Here is the call graph for this function:

◆ python_interpreter_free()

static void python_interpreter_free ( rlm_python_t inst,
PyThreadState *  interp 
)
static

Definition at line 902 of file rlm_python.c.

+ Here is the caller graph for this function:

◆ python_interpreter_init()

static int python_interpreter_init ( module_inst_ctx_t const *  mctx)
static

Definition at line 854 of file rlm_python.c.

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

◆ python_module_import_config()

static int python_module_import_config ( module_inst_ctx_t const *  mctx,
CONF_SECTION conf,
PyObject *  module 
)
static

Make the current instance's config available within the module we're initialising.

Definition at line 777 of file rlm_python.c.

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

◆ python_module_import_constants()

static int python_module_import_constants ( module_inst_ctx_t const *  mctx,
PyObject *  module 
)
static

Import integer constants into the module we're initialising.

Definition at line 813 of file rlm_python.c.

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

◆ python_module_init()

static PyObject* python_module_init ( void  )
static

Definition at line 831 of file rlm_python.c.

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

◆ python_obj_destroy()

static void python_obj_destroy ( PyObject **  ob)
static

Definition at line 640 of file rlm_python.c.

+ Here is the caller graph for this function:

◆ python_parse_config()

static int python_parse_config ( module_inst_ctx_t const *  mctx,
CONF_SECTION cs,
int  lvl,
PyObject *  dict 
)
static

Definition at line 694 of file rlm_python.c.

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

Variable Documentation

◆ current_conf

CONF_SECTION* current_conf
static

Used for communication with inittab functions.

Definition at line 96 of file rlm_python.c.

◆ current_mctx

module_ctx_t const* current_mctx
static

Used for communication with inittab functions.

Definition at line 95 of file rlm_python.c.

◆ 

struct { ... } freeradius_constants[]
Initial value:
= {
#define A(x)
{ NULL, 0 },
}
@ L_DBG_WARN_REQ
Less severe warning only displayed when debugging is enabled.
Definition: log.h:63
@ L_ERR
Error message.
Definition: log.h:56
@ L_DBG_ERR
Error only displayed when debugging is enabled.
Definition: log.h:62
@ L_DBG_ERR_REQ
Less severe error only displayed when debugging is enabled.
Definition: log.h:64
@ L_DBG_WARN
Warning only displayed when debugging is enabled.
Definition: log.h:61
@ L_INFO
Informational message.
Definition: log.h:55
@ L_DBG
Only displayed when debugging is enabled.
Definition: log.h:59
@ RLM_MODULE_INVALID
The module considers the request invalid.
Definition: rcode.h:45
@ RLM_MODULE_OK
The module is OK, continue.
Definition: rcode.h:43
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition: rcode.h:42
@ RLM_MODULE_DISALLOW
Reject the request (user is locked out).
Definition: rcode.h:46
@ RLM_MODULE_REJECT
Immediately reject the request.
Definition: rcode.h:41
@ RLM_MODULE_NOTFOUND
User not found.
Definition: rcode.h:47
@ RLM_MODULE_UPDATED
OK (pairs modified).
Definition: rcode.h:49
@ RLM_MODULE_NOOP
Module succeeded without doing anything.
Definition: rcode.h:48
@ RLM_MODULE_NUMCODES
How many valid return codes there are.
Definition: rcode.h:50
@ RLM_MODULE_HANDLED
The module handled the request, so stop.
Definition: rcode.h:44
#define A(x)

◆ global_interpreter

PyThreadState* global_interpreter
static

Our first interpreter.

Definition at line 93 of file rlm_python.c.

◆ libpython_global_config

libpython_global_config_t libpython_global_config
static
Initial value:
= {
.path = NULL,
.path_include_default = true
}

Definition at line 98 of file rlm_python.c.

◆ module_config

conf_parser_t module_config[]
static
Initial value:
= {
#define A(x)
A(authorize)
A(authenticate)
A(preacct)
A(accounting)
A(post_auth)
A(detach)
}
#define CONF_PARSER_TERMINATOR
Definition: cf_parse.h:626
static int instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_rest.c:1312

Definition at line 147 of file rlm_python.c.

◆ module_methods

PyMethodDef module_methods[]
static
Initial value:
= {
{ "log", &mod_log, METH_VARARGS,
"freeradius.log(level, msg)\n\n"
"Print a message using the freeradius daemon's logging system. level should be one of the\n"
"following constants L_DBG, L_WARN, L_INFO, L_ERR, L_DBG_WARN, L_DBG_ERR, L_DBG_WARN_REQ, L_DBG_ERR_REQ\n"
},
{ NULL, NULL, 0, NULL },
}
static PyObject * mod_log(UNUSED PyObject *module, PyObject *args)
Allow fr_log to be called from python.
Definition: rlm_python.c:204

Definition at line 218 of file rlm_python.c.

◆ python_dlhandle

void* python_dlhandle
static

Definition at line 92 of file rlm_python.c.

◆ python_global_config

conf_parser_t const python_global_config[]
static
Initial value:
= {
{ FR_CONF_OFFSET("path_include_default", libpython_global_config_t, path_include_default) },
}
Global config for python library.
Definition: rlm_python.c:78

Definition at line 103 of file rlm_python.c.

◆ rlm_python

module_rlm_t rlm_python
Initial value:
= {
.common = {
.name = "python",
.inst_size = sizeof(rlm_python_t),
.thread_inst_size = sizeof(rlm_python_thread_t),
.config = module_config,
.instantiate = mod_instantiate,
.detach = mod_detach,
.thread_instantiate = mod_thread_instantiate,
.thread_detach = mod_thread_detach
},
.method_names = (module_method_name_t[]){
{ .name1 = "authorize", .name2 = CF_IDENT_ANY, .method = mod_authorize },
{ .name1 = "recv", .name2 = "accounting-request", .method = mod_preacct },
{ .name1 = "recv", .name2 = CF_IDENT_ANY, .method = mod_authorize },
{ .name1 = "accounting", .name2 = CF_IDENT_ANY, .method = mod_accounting },
{ .name1 = "authenticate", .name2 = CF_IDENT_ANY, .method = mod_authenticate },
{ .name1 = "send", .name2 = CF_IDENT_ANY, .method = mod_post_auth },
}
}
#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 unlang_action_t mod_authenticate(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Definition: rlm_chap.c:228
static unlang_action_t mod_authorize(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Definition: rlm_chap.c:176
static unlang_action_t mod_accounting(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Write accounting data to Couchbase documents.
static unlang_action_t mod_post_auth(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Definition: rlm_detail.c:400
static int mod_detach(module_detach_ctx_t const *mctx)
Definition: rlm_python.c:977
static int mod_thread_instantiate(module_thread_inst_ctx_t const *mctx)
Definition: rlm_python.c:1023
static int mod_thread_detach(module_thread_inst_ctx_t const *mctx)
Definition: rlm_python.c:1041
static int mod_instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_python.c:927
static conf_parser_t module_config[]
Definition: rlm_python.c:147
Tracks a python module inst/thread state pair.
Definition: rlm_python.c:88
static unlang_action_t mod_preacct(rlm_rcode_t *p_result, module_ctx_t const *mctx, UNUSED request_t *request)
Definition: rlm_test.c:248
@ MODULE_TYPE_THREAD_SAFE
Module is threadsafe.
Definition: module.h:49
#define MODULE_NAME_TERMINATOR
Definition: module.h:135

Definition at line 1169 of file rlm_python.c.

◆ rlm_python_autoinst

global_lib_autoinst_t rlm_python_autoinst
Initial value:
= {
.name = "python",
.init = libpython_init,
.free = libpython_free,
}
static int libpython_init(void)
Definition: rlm_python.c:1054
static void libpython_free(void)
Definition: rlm_python.c:1146
static conf_parser_t const python_global_config[]
Definition: rlm_python.c:103
static libpython_global_config_t libpython_global_config
Definition: rlm_python.c:98

Definition at line 112 of file rlm_python.c.

◆ rlm_python_lib

global_lib_autoinst_t const *const rlm_python_lib
Initial value:
= {
}
#define GLOBAL_LIB_TERMINATOR
Definition: global_lib.h:51
global_lib_autoinst_t rlm_python_autoinst
Definition: rlm_python.c:112

Definition at line 121 of file rlm_python.c.