All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rlm_sql.h
Go to the documentation of this file.
1 /*
2  * This program is 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 (at
5  * 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: 13b4a3a66c289a94edb054d77b56aa855577cb3b $
19  * @file rlm_sql.h
20  * @brief Prototypes and functions for the SQL module
21  *
22  * @copyright 2012-2014 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
23  * @copyright 2000,2006 The FreeRADIUS server project
24  * @copyright 2000 Mike Machado <mike@innercite.com>
25  * @copyright 2000 Alan DeKok <aland@ox.org>
26  */
27 #ifndef _RLM_SQL_H
28 #define _RLM_SQL_H
29 
30 RCSIDH(rlm_sql_h, "$Id: 13b4a3a66c289a94edb054d77b56aa855577cb3b $")
31 
32 #include <freeradius-devel/radiusd.h>
33 #include <freeradius-devel/connection.h>
34 #include <freeradius-devel/modpriv.h>
35 #include <freeradius-devel/exfile.h>
36 
37 #define MOD_PREFIX "rlm_sql"
38 
39 #define PW_ITEM_CHECK 0
40 #define PW_ITEM_REPLY 1
41 
42 
43 /* SQL Errors */
44 typedef enum {
45  RLM_SQL_QUERY_INVALID = -3, //!< Query syntax error
46  RLM_SQL_ERROR = -2, //!< General connection/server error
47  RLM_SQL_OK = 0, //!< Success
48  RLM_SQL_RECONNECT = 1, //!< Stale connection, should reconnect
49  RLM_SQL_ALT_QUERY = 2 //!< Key constraint violation
50 } sql_rcode_t;
51 
52 typedef enum {
57 
58 
59 typedef char **rlm_sql_row_t;
60 
61 typedef struct sql_log_entry {
62  log_type_t type; //!< Type of log entry L_ERR, L_WARN, L_INFO, L_DBG etc..
63  char const *msg; //!< Log message.
65 
66 /*
67  * Sections where we dynamically resolve the config entry to use,
68  * by xlating reference.
69  */
70 typedef struct sql_acct_section {
71  CONF_SECTION *cs; //!< The CONF_SECTION representing the group
72  //!< of queries to process.
73 
74  char const *reference; //!< Reference string, expanded to point to
75  //!< a group of queries.
77 
78  char const *logfile;
79 
80  char const **query; /* for xlat parsing */
82 
83 typedef struct sql_config {
84  char const *sql_driver_name; //!< SQL driver module name e.g. rlm_sql_sqlite.
85  char const *sql_server; //!< Server to connect to.
86  uint32_t sql_port; //!< Port to connect to.
87  char const *sql_login; //!< Login credentials to use.
88  char const *sql_password; //!< Login password to use.
89  char const *sql_db; //!< Database to run queries against.
90 
91  char const *query_user; //!< xlat expansion used to specify the user
92  //!< to use as the subject of queries.
93 
94  char const *group_attribute; //!< Name of the group attribute.
95 
96  char const *default_profile; //!< Default profile to use if no other
97  //!< profiles were configured.
98 
99  char const *client_query; //!< Query used to get FreeRADIUS client
100  //!< definitions.
101 
102  char const *authorize_check_query; //!< Query used get check VPs for a user.
103  char const *authorize_reply_query; //!< Query used get reply VPs for a user.
104  char const *authorize_group_check_query; //!< Query used get check VPs for a group.
105  char const *authorize_group_reply_query; //!< Query used get reply VPs for a group.
106  char const *simul_count_query; //!< Query used get number of active sessions
107  //!< for a user (basic simultaneous use check).
108  char const *simul_verify_query; //!< Query to get active sessions for a user
109  //!< the result is fed to session_zap.
110  char const *groupmemb_query; //!< Query to determine group membership.
111 
112  bool do_clients; //!< Read clients from SQL database.
113  bool read_groups; //!< Read user groups by default.
114  //!< If false, Fall-Through = yes is required
115  //!< in the previous reply list to process
116  //!< groups.
117  bool read_profiles; //!< Read user profiles by default.
118  //!< If false, Fall-Through = yes is required
119  //!< in the previous reply list to process
120  //!< profiles.
121  char const *logfile; //!< Keep a log of all SQL queries executed
122  //!< Useful for batch insertion with the
123  //!< NULL drivers.
124 
125  bool delete_stale_sessions; //!< Whether we should use session_zap to create
126  //!< a fake stop packet, to terminate any
127  //!< stale sessions.
128 
129  char const *allowed_chars; //!< Chars which done need escaping..
130  uint32_t query_timeout; //!< How long to allow queries to run for.
131 
132  char const *connect_query; //!< Query executed after establishing
133  //!< new connection.
134 
135  void *driver; //!< Where drivers should write a
136  //!< pointer to their configurations.
137 
138  /*
139  * @todo The rest of the queries should also be moved into
140  * their own sections.
141  */
142 
143  /*
144  * Section configurations
145  */
149 
150 typedef struct sql_inst rlm_sql_t;
151 
152 typedef struct rlm_sql_handle {
153  void *conn; //!< Database specific connection handle.
154  rlm_sql_row_t row; //!< Row data from the last query.
155  rlm_sql_t *inst; //!< The rlm_sql instance this connection belongs to.
156  TALLOC_CTX *log_ctx; //!< Talloc pool used to avoid mallocing memory on
157  //!< when log strings need to be copied.
159 
160 extern const FR_NAME_NUMBER sql_rcode_table[];
161 /*
162  * Capabilities flags for drivers
163  */
164 #define RLM_SQL_RCODE_FLAGS_ALT_QUERY 1 //!< Can distinguish between other errors and those
165  //!< resulting from a unique key violation.
166 
167 /** Retrieve errors from the last query operation
168  *
169  * @note Buffers allocated in the context provided will be automatically freed. The driver
170  * should not free these buffers explicitly.
171  * @note If the driver uses its own buffers to aggregate messages, they should be cleared
172  * on sql_query_finish, and after each call to sql_error, to prevent the same messages
173  * being printed multiple times.
174  *
175  * @param[in,out] ctx to allocate any buffers required. If static buffers are provided by the
176  * driver they need not be strduped, just write the pointer to those buffers to the .msg
177  * field of a sql_log_entry_t element.
178  * @param[out] out a pre-allocated array of log entries to fill. Need not be NULL terminated.
179  * @param[in] outlen Number of log entries available for populating. Do not write to index
180  * out[outlen] or higher.
181  * @param[in] handle to retrieve errors from.
182  * @param[in] config of the SQL instance.
183  * @return
184  * 0 - If no error messages are available.
185  * >0 - Number of log entries
186  */
187 typedef size_t (*sql_error_t)(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, rlm_sql_handle_t *handle,
188  rlm_sql_config_t *config);
189 
190 typedef struct rlm_sql_module_t {
191  char const *name;
192  int flags;
193 
196  struct timeval const *timeout);
197 
198  sql_rcode_t (*sql_query)(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char const *query);
199  sql_rcode_t (*sql_select_query)(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char const *query);
201 
205 
206  sql_rcode_t (*sql_fetch_row)(rlm_sql_row_t *out, rlm_sql_handle_t *handle, rlm_sql_config_t *config);
207  sql_rcode_t (*sql_fields)(char const **out[], rlm_sql_handle_t *handle, rlm_sql_config_t *config);
209 
210  sql_error_t sql_error; //!< Get any errors from the previous query.
211 
214 
217 
218 struct sql_inst {
223 
224  fr_dict_attr_t const *sql_user; //!< Cached pointer to SQL-User-Name
225  //!< dictionary attribute.
227 
228  void *handle;
230 
231  int (*sql_set_user)(rlm_sql_t const *inst, REQUEST *request, char const *username);
233  sql_rcode_t (*sql_query)(rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle, char const *query);
234  sql_rcode_t (*sql_select_query)(rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle, char const *query);
235  sql_rcode_t (*sql_fetch_row)(rlm_sql_row_t *out, rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle);
236 
237  char const *name; //!< Module instance name.
238  fr_dict_attr_t const *group_da; //!< Group dictionary attribute.
239 };
240 
241 typedef struct sql_grouplist {
242  char *name;
245 
246 void *mod_conn_create(TALLOC_CTX *ctx, void *instance, struct timeval const *timeout);
247 int sql_fr_pair_list_afrom_str(TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR **first_pair, rlm_sql_row_t row);
248 int sql_read_realms(rlm_sql_handle_t *handle);
249 int sql_getvpdata(TALLOC_CTX *ctx, rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle, VALUE_PAIR **pair, char const *query);
251 int sql_dict_init(rlm_sql_handle_t *handle);
252 void rlm_sql_query_log(rlm_sql_t const *inst, REQUEST *request, sql_acct_section_t *section, char const *query) CC_HINT(nonnull (1, 2, 4));
253 sql_rcode_t rlm_sql_select_query(rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle, char const *query) CC_HINT(nonnull (1, 3, 4));
254 sql_rcode_t rlm_sql_query(rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle, char const *query) CC_HINT(nonnull (1, 3, 4));
255 int rlm_sql_fetch_row(rlm_sql_row_t *out, rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle);
256 void rlm_sql_print_error(rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t *handle, bool force_debug);
257 int sql_set_user(rlm_sql_t const *inst, REQUEST *request, char const *username);
258 #endif
exfile_t * ef
Definition: rlm_sql.h:226
rlm_sql_t * inst
The rlm_sql instance this connection belongs to.
Definition: rlm_sql.h:155
#define RCSIDH(h, id)
Definition: build.h:136
const FR_NAME_NUMBER sql_rcode_table[]
Definition: sql.c:46
General connection/server error.
Definition: rlm_sql.h:46
char const * client_query
Query used to get FreeRADIUS client definitions.
Definition: rlm_sql.h:99
sql_rcode_t(* sql_query)(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char const *query)
Definition: rlm_sql.h:198
sql_rcode_t(* sql_fields)(char const **out[], rlm_sql_handle_t *handle, rlm_sql_config_t *config)
Definition: rlm_sql.h:207
fr_dict_attr_t const * group_da
Group dictionary attribute.
Definition: rlm_sql.h:238
void sql_rcode_t sql_rcode_t rlm_sql_query(rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle, char const *query) CC_HINT(nonnull(1
char const * authorize_group_check_query
Query used get check VPs for a group.
Definition: rlm_sql.h:104
sql_acct_section_t postauth
Definition: rlm_sql.h:146
char const ** query
Definition: rlm_sql.h:80
enum log_type log_type_t
sql_rcode_t(* sql_finish_select_query)(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
Definition: rlm_sql.h:213
char ** rlm_sql_row_t
Definition: rlm_sql.h:59
log_type_t type
Type of log entry L_ERR, L_WARN, L_INFO, L_DBG etc..
Definition: rlm_sql.h:62
void sql_rcode_t sql_rcode_t int rlm_sql_fetch_row(rlm_sql_row_t *out, rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle)
Call the driver's sql_fetch_row function.
Definition: sql.c:249
Dictionary attribute.
Definition: dict.h:77
char const * simul_count_query
Query used get number of active sessions for a user (basic simultaneous use check).
Definition: rlm_sql.h:106
char const * authorize_reply_query
Query used get reply VPs for a user.
Definition: rlm_sql.h:103
struct sql_grouplist rlm_sql_grouplist_t
int(* sql_num_rows)(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
Definition: rlm_sql.h:203
int sql_read_clients(rlm_sql_handle_t *handle)
void sql_rcode_t rlm_sql_select_query(rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle, char const *query) CC_HINT(nonnull(1
char const * logfile
Keep a log of all SQL queries executed Useful for batch insertion with the NULL drivers.
Definition: rlm_sql.h:121
char const * msg
Log message.
Definition: rlm_sql.h:63
#define CC_HINT(_x)
Definition: build.h:71
CONF_SECTION * cs
The CONF_SECTION representing the group of queries to process.
Definition: rlm_sql.h:71
sql_acct_section_t accounting
Definition: rlm_sql.h:147
char const * reference
Reference string, expanded to point to a group of queries.
Definition: rlm_sql.h:74
char const * sql_server
Server to connect to.
Definition: rlm_sql.h:85
sql_rcode_t(* sql_free_result)(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
Definition: rlm_sql.h:208
fr_dict_attr_t const * sql_user
Cached pointer to SQL-User-Name.
Definition: rlm_sql.h:224
static float timeout
Definition: radclient.c:43
#define inst
size_t(* xlat_escape_t)(REQUEST *request, char *out, size_t outlen, char const *in, void *arg)
Definition: xlat.h:36
sql_rcode_t(* sql_fetch_row)(rlm_sql_row_t *out, rlm_sql_handle_t *handle, rlm_sql_config_t *config)
Definition: rlm_sql.h:206
bool do_clients
Read clients from SQL database.
Definition: rlm_sql.h:112
char const * simul_verify_query
Query to get active sessions for a user the result is fed to session_zap.
Definition: rlm_sql.h:108
Key constraint violation.
Definition: rlm_sql.h:49
sql_rcode_t(* sql_socket_init)(rlm_sql_handle_t *handle, rlm_sql_config_t *config, struct timeval const *timeout)
Definition: rlm_sql.h:195
sql_rcode_t(* sql_fetch_row)(rlm_sql_row_t *out, rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle)
Definition: rlm_sql.h:235
bool reference_cp
Definition: rlm_sql.h:76
struct sql_grouplist * next
Definition: rlm_sql.h:243
Stale connection, should reconnect.
Definition: rlm_sql.h:48
size_t(* sql_error_t)(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, rlm_sql_handle_t *handle, rlm_sql_config_t *config)
Retrieve errors from the last query operation.
Definition: rlm_sql.h:187
char const * name
Definition: rlm_sql.h:191
CONF_SECTION * cs
Definition: rlm_sql.h:222
int sql_read_realms(rlm_sql_handle_t *handle)
char * name
Definition: rlm_sql.h:242
int(* sql_set_user)(rlm_sql_t const *inst, REQUEST *request, char const *username)
Definition: rlm_sql.h:231
char const * query_user
xlat expansion used to specify the user to use as the subject of queries.
Definition: rlm_sql.h:91
int sql_dict_init(rlm_sql_handle_t *handle)
sql_rcode_t
Definition: rlm_sql.h:44
int sql_fr_pair_list_afrom_str(TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR **first_pair, rlm_sql_row_t row)
Definition: sql.c:128
bool read_groups
Read user groups by default.
Definition: rlm_sql.h:113
sql_rcode_t(* sql_select_query)(rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle, char const *query)
Definition: rlm_sql.h:234
uint32_t sql_port
Port to connect to.
Definition: rlm_sql.h:86
void rlm_sql_query_log(rlm_sql_t const *inst, REQUEST *request, sql_acct_section_t *section, char const *query) CC_HINT(nonnull(1
Definition: rlm_sql.h:61
xlat_escape_t sql_escape_func
Definition: rlm_sql.h:232
sql_error_t sql_error
Get any errors from the previous query.
Definition: rlm_sql.h:210
void rlm_sql_print_error(rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t *handle, bool force_debug)
Retrieve any errors from the SQL driver.
Definition: sql.c:280
Stores an attribute, a value and various bits of other data.
Definition: pair.h:112
Success.
Definition: rlm_sql.h:47
xlat_escape_t sql_escape_func
Definition: rlm_sql.h:215
struct sql_log_entry sql_log_entry_t
rlm_sql_config_t * config
Definition: rlm_sql.h:221
char const * sql_password
Login password to use.
Definition: rlm_sql.h:88
void * driver
Where drivers should write a pointer to their configurations.
Definition: rlm_sql.h:135
static rs_t * conf
Definition: radsniff.c:46
void * conn
Database specific connection handle.
Definition: rlm_sql.h:153
int sql_set_user(rlm_sql_t const *inst, REQUEST *request, char const *username)
Definition: rlm_sql.c:641
char const * group_attribute
Name of the group attribute.
Definition: rlm_sql.h:94
rlm_sql_row_t row
Row data from the last query.
Definition: rlm_sql.h:154
int sql_getvpdata(TALLOC_CTX *ctx, rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle, VALUE_PAIR **pair, char const *query)
Definition: sql.c:504
sql_rcode_t(* sql_select_query)(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char const *query)
Definition: rlm_sql.h:199
rlm_sql_config_t myconfig
Definition: rlm_sql.h:219
char const * allowed_chars
Chars which done need escaping..
Definition: rlm_sql.h:129
char const * sql_login
Login credentials to use.
Definition: rlm_sql.h:87
char const * default_profile
Default profile to use if no other profiles were configured.
Definition: rlm_sql.h:96
A connection pool.
Definition: connection.c:85
Query syntax error.
Definition: rlm_sql.h:45
bool delete_stale_sessions
Whether we should use session_zap to create a fake stop packet, to terminate any stale sessions...
Definition: rlm_sql.h:125
struct sql_config rlm_sql_config_t
uint32_t query_timeout
How long to allow queries to run for.
Definition: rlm_sql.h:130
bool read_profiles
Read user profiles by default.
Definition: rlm_sql.h:117
char const * sql_db
Database to run queries against.
Definition: rlm_sql.h:89
struct rlm_sql_handle rlm_sql_handle_t
sql_fall_through_t
Definition: rlm_sql.h:52
char const * name
Module instance name.
Definition: rlm_sql.h:237
TALLOC_CTX * log_ctx
Talloc pool used to avoid mallocing memory on when log strings need to be copied. ...
Definition: rlm_sql.h:156
struct rlm_sql_module_t rlm_sql_module_t
char const * connect_query
Query executed after establishing new connection.
Definition: rlm_sql.h:132
char const * logfile
Definition: rlm_sql.h:78
void * handle
Definition: rlm_sql.h:228
sql_rcode_t(* sql_query)(rlm_sql_t const *inst, REQUEST *request, rlm_sql_handle_t **handle, char const *query)
Definition: rlm_sql.h:233
char const * sql_driver_name
SQL driver module name e.g. rlm_sql_sqlite.
Definition: rlm_sql.h:84
sql_rcode_t(* sql_finish_query)(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
Definition: rlm_sql.h:212
void * mod_conn_create(TALLOC_CTX *ctx, void *instance, struct timeval const *timeout)
Create a new connection pool handle.
Definition: mod.c:68
rlm_sql_module_t * module
Definition: rlm_sql.h:229
fr_connection_pool_t * pool
Definition: rlm_sql.h:220
sql_rcode_t(* mod_instantiate)(CONF_SECTION *conf, rlm_sql_config_t *config)
Definition: rlm_sql.h:194
int(* sql_num_fields)(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
Definition: rlm_sql.h:202
sql_rcode_t(* sql_store_result)(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
Definition: rlm_sql.h:200
int(* sql_affected_rows)(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
Definition: rlm_sql.h:204
char const * authorize_check_query
Query used get check VPs for a user.
Definition: rlm_sql.h:102
struct sql_acct_section sql_acct_section_t
char const * groupmemb_query
Query to determine group membership.
Definition: rlm_sql.h:110
char const * authorize_group_reply_query
Query used get reply VPs for a group.
Definition: rlm_sql.h:105