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  * $Id: 9ba81b3bc1fb7f19a25bfbbd8007a6ea8239d561 $
19  *
20  * @file modules.h
21  * @brief Interface to the RADIUS module system.
22  *
23  * @copyright 2013 The FreeRADIUS server project
24  */
25 
26 #ifndef RADIUS_MODULES_H
27 #define RADIUS_MODULES_H
28 
29 RCSIDH(modules_h, "$Id: 9ba81b3bc1fb7f19a25bfbbd8007a6ea8239d561 $")
30 
31 #include <freeradius-devel/conffile.h>
32 #include <freeradius-devel/features.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /** The different section components of the server
39  *
40  * Used as indexes in the methods array in the module_t struct.
41  */
42 typedef enum rlm_components {
43 	MOD_AUTHENTICATE = 0,	//!< 0 methods index for authenticate section.
44 	MOD_AUTHORIZE,		//!< 1 methods index for authorize section.
45 	MOD_PREACCT,		//!< 2 methods index for preacct section.
46 	MOD_ACCOUNTING,		//!< 3 methods index for accounting section.
47 	MOD_SESSION,		//!< 4 methods index for checksimul section.
48 	MOD_PRE_PROXY,		//!< 5 methods index for preproxy section.
49 	MOD_POST_PROXY,		//!< 6 methods index for postproxy section.
50 	MOD_POST_AUTH,		//!< 7 methods index for postauth section.
51 #ifdef WITH_COA
52 	MOD_RECV_COA,		//!< 8 methods index for recvcoa section.
53 	MOD_SEND_COA,		//!< 9 methods index for sendcoa section.
54 #endif
55 	MOD_COUNT		//!< 10 how many components there are.
56 } rlm_components_t;
57 
58 extern const FR_NAME_NUMBER mod_rcode_table[];
59 
60 /** Map a section name, to a section typename, to an attribute number
61  *
62  * Used by modules.c to define the mappings between names, types and control
63  * attributes.
64  */
65 typedef struct section_type_value_t {
66 	char const      *section;	//!< Section name e.g. "Authorize".
67 	char const      *typename;	//!< Type name e.g. "Auth-Type".
68 	int	     attr;		//!< Attribute number.
69 } section_type_value_t;
70 
71 /** Mappings between section names, typenames and control attributes
72  *
73  * Defined in modules.c.
74  */
75 extern const section_type_value_t section_type_value[];
76 
77 #define RLM_TYPE_THREAD_SAFE	(0 << 0) 	//!< Module is threadsafe.
78 #define RLM_TYPE_THREAD_UNSAFE	(1 << 0) 	//!< Module is not threadsafe.
79 						//!< Server will protect calls
80 						//!< with mutex.
81 #define RLM_TYPE_HUP_SAFE	(1 << 2) 	//!< Will be restarted on HUP.
82 						//!< Server will instantiated
83 						//!< new instance, and then
84 						//!< destroy old instance.
85 
86 
87 /* Stop people using different module/library/server versions together */
88 #define RLM_MODULE_INIT RADIUSD_MAGIC_NUMBER
89 
90 /** Module section callback
91  *
92  * Is called when the module is listed in a particular section of a virtual
93  * server, and the request has reached the module call.
94  *
95  * @param[in] instance created in instantiated, holds module config.
96  * @param[in,out] request being processed.
97  * @return the appropriate rcode.
98  */
99 typedef rlm_rcode_t (*packetmethod)(void *instance, REQUEST *request);
100 
101 /** Module instantiation callback
102  *
103  * Is called once per module instance. Is not called when new threads are
104  * spawned. Modules that require separate thread contexts should use the
105  * connection pool API.
106  *
107  * @param[in] mod_cs Module instance's configuration section.
108  * @param[out] instance Module instance's configuration structure, should be
109  *		alloced by by callback and freed by detach.
110  * @return -1 if instantiation failed, else 0.
111  */
112 typedef int (*instantiate_t)(CONF_SECTION *mod_cs, void *instance);
113 
114 /** Module detach callback
115  *
116  * Is called just before the server exits, and after re-instantiation on HUP,
117  * to free the old module instance.
118  *
119  * Detach should close all handles associated with the module instance, and
120  * free any memory allocated during instantiate.
121  *
122  * @param[in] instance to free.
123  * @return -1 if detach failed, else 0.
124  */
125 typedef int (*detach_t)(void *instance);
126 
127 /** Metadata exported by the module
128  *
129  * This determines the capabilities of the module, and maps internal functions
130  * within the module to different sections.
131  */
132 typedef struct module_t {
133 	uint64_t 		magic;			//!< Used to validate module struct.
134 	char const		*name;			//!< The name of the module (without rlm_ prefix).
135 	int			type;			//!< One or more of the RLM_TYPE_* constants.
136 	size_t			inst_size;		//!< Size of the instance data
137 	CONF_PARSER const	*config;		//!< Configuration information
138 	instantiate_t		bootstrap;		//!< register dynamic attrs, etc.
139 	instantiate_t		instantiate;		//!< Function to use for instantiation.
140 	detach_t		detach;			//!< Function to use to free module instance.
141 	packetmethod		methods[MOD_COUNT];	//!< Pointers to the various section functions.
142 } module_t;
143 
144 int modules_init(CONF_SECTION *);
145 int modules_free(void);
146 int modules_hup(CONF_SECTION *modules);
147 rlm_rcode_t process_authorize(int type, REQUEST *request);
148 rlm_rcode_t process_authenticate(int type, REQUEST *request);
149 rlm_rcode_t module_preacct(REQUEST *request);
150 rlm_rcode_t process_accounting(int type, REQUEST *request);
151 int process_checksimul(int type, REQUEST *request, int maxsimul);
152 rlm_rcode_t process_pre_proxy(int type, REQUEST *request);
153 rlm_rcode_t process_post_proxy(int type, REQUEST *request);
154 rlm_rcode_t process_post_auth(int type, REQUEST *request);
155 #ifdef WITH_COA
156 rlm_rcode_t process_recv_coa(int type, REQUEST *request);
157 rlm_rcode_t process_send_coa(int type, REQUEST *request);
158 #define MODULE_NULL_COA_FUNCS ,NULL,NULL
159 #else
160 #define MODULE_NULL_COA_FUNCS
161 #endif
162 
163 rlm_rcode_t indexed_modcall(rlm_components_t comp, int idx, REQUEST *request);
164 
165 /*
166  *	For now, these are strongly tied together.
167  */
168 int virtual_servers_load(CONF_SECTION *config);
169 void virtual_servers_free(time_t when);
170 
171 #ifdef __cplusplus
172 }
173 #endif
174 
175 #endif /* RADIUS_MODULES_H */
176