The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
rlm_sql.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 (at
6 * 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: c961e8f0a414f27ffc53eb19ae195fda29252fa2 $
20 * @file rlm_sql.h
21 * @brief Prototypes and functions for the SQL module
22 *
23 * @copyright 2012-2014 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 * @copyright 2000,2006 The FreeRADIUS server project
25 * @copyright 2000 Mike Machado (mike@innercite.com)
26 * @copyright 2000 Alan DeKok (aland@freeradius.org)
27 */
28RCSIDH(rlm_sql_h, "$Id: c961e8f0a414f27ffc53eb19ae195fda29252fa2 $")
29
30#include <freeradius-devel/server/base.h>
31#include <freeradius-devel/server/pool.h>
32#include <freeradius-devel/server/modpriv.h>
33#include <freeradius-devel/server/exfile.h>
34#include <freeradius-devel/server/trunk.h>
35#include <freeradius-devel/unlang/function.h>
36
37#define FR_ITEM_CHECK 0
38#define FR_ITEM_REPLY 1
39
40
41/** Action to take at end of an SQL query
42 *
43 */
44typedef 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, //!< Key constraint violation, use an alternative query.
50 RLM_SQL_NO_MORE_ROWS, //!< No more rows available
52
58
59typedef char **rlm_sql_row_t;
60
61typedef struct {
62 fr_log_type_t type; //!< Type of log entry L_ERR, L_WARN, L_INFO,
63 ///< L_DBG etc.
64 char const *msg; //!< Log message.
66
67typedef struct {
68 char const *sql_state; //!< 2-5 char error code.
69 char const *meaning; //!< Verbose description.
70 sql_rcode_t rcode; //!< What should happen if we receive this error.
72
73typedef struct {
74 char const *sql_server; //!< Server to connect to.
75 uint32_t sql_port; //!< Port to connect to.
76 char const *sql_login; //!< Login credentials to use.
77 char const *sql_password; //!< Login password to use.
78 char const *sql_db; //!< Database to run queries against.
79
80 char const *group_attribute; //!< Name of the group attribute.
81
82 bool cache_groups; //!< cache group names in &control.SQL-Group
83
84 bool read_groups; //!< Read user groups by default.
85 //!< If false, Fall-Through = yes is required
86 //!< in the previous reply list to process
87 //!< groups.
88 bool read_profiles; //!< Read user profiles by default.
89 //!< If false, Fall-Through = yes is required
90 //!< in the previous reply list to process
91 //!< profiles.
92
93 bool expand_rhs; //!< expand the RHS for check / reply tables
94
95 char const *allowed_chars; //!< Chars which done need escaping..
96 fr_time_delta_t query_timeout; //!< How long to allow queries to run for.
97
98 char const *connect_query; //!< Query executed after establishing
99 //!< new connection.
100
101 trunk_conf_t trunk_conf; //!< Configuration for trunk connections.
103
104typedef struct sql_inst rlm_sql_t;
105
106/*
107 * Per-thread instance data structure
108 */
109typedef struct {
110 trunk_t *trunk; //!< Trunk connection for this thread.
111 rlm_sql_t const *inst; //!< Module instance data.
112 void *sql_escape_arg; //!< Thread specific argument to be passed to escape function.
114
119
120/** Status of an SQL query
121 */
122typedef enum {
123 SQL_QUERY_FAILED = -1, //!< Failed to submit.
124 SQL_QUERY_PREPARED = 0, //!< Ready to submit.
125 SQL_QUERY_SUBMITTED, //!< Submitted for execution.
126 SQL_QUERY_RETURNED, //!< Query has executed.
127 SQL_QUERY_FETCHING_RESULTS, //!< Fetching results from server.
128 SQL_QUERY_RESULTS_FETCHED, //!< Results fetched from the server.
129 SQL_QUERY_CANCELLED //!< A cancellation has been sent to the server.
131
132typedef struct {
133 rlm_sql_t const *inst; //!< Module instance for this query.
134 request_t *request; //!< Request this query relates to.
135 trunk_t *trunk; //!< Trunk this query is being run on.
136 trunk_connection_t *tconn; //!< Trunk connection this query is being run on.
137 trunk_request_t *treq; //!< Trunk request for this query.
138 char const *query_str; //!< Query string to run.
139 fr_sql_query_type_t type; //!< Type of query.
140 fr_sql_query_status_t status; //!< Status of the query.
141 sql_rcode_t rcode; //!< Result code.
142 rlm_sql_row_t row; //!< Row data from the last query.
143 void *uctx; //!< Driver specific data.
145
146/** Context used when fetching attribute value pairs as a map list
147 */
148typedef struct {
149 TALLOC_CTX *ctx; //!< To allocate map entries in.
150 rlm_sql_t const *inst; //!< Module instance data.
151 fr_value_box_t *query; //!< Query string used for fetching pairs.
152 fr_sql_query_t *query_ctx; //!< Query context.
153 fr_dict_attr_t const *list; //!< Default list for pair evaluation.
154 map_list_t *out; //!< List to append entries to.
155 int rows; //!< How many rows the query returned.
156 bool expand_rhs; //!< for reply items
158
162extern size_t sql_rcode_table_len;
163
164/*
165 * Capabilities flags for drivers
166 */
167#define RLM_SQL_RCODE_FLAGS_ALT_QUERY 1 //!< Can distinguish between other errors and those
168 //!< resulting from a unique key violation.
169#define RLM_SQL_MULTI_QUERY_CONN 2 //!< Can support multiple queries on a single connection.
170
171/** Retrieve errors from the last query operation
172 *
173 * @note Buffers allocated in the context provided will be automatically freed. The driver
174 * should not free these buffers explicitly.
175 * @note If the driver uses its own buffers to aggregate messages, they should be cleared
176 * on sql_query_finish, and after each call to sql_error, to prevent the same messages
177 * being printed multiple times.
178 *
179 * @param[in,out] ctx to allocate any buffers required. If static buffers are provided by the
180 * driver they need not be talloc_strdupd, just write the pointer to those buffers to the
181 * .msg field of a sql_log_entry_t element.
182 * @param[out] out a pre-allocated array of log entries to fill. Need not be NULL terminated.
183 * @param[in] outlen Number of log entries available for populating. Do not write to index
184 * out[outlen] or higher.
185 * @param[in] query_ctx to retrieve errors from.
186 * @return
187 * 0 - If no error messages are available.
188 * >0 - Number of log entries
189 */
190typedef size_t (*sql_error_t)(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, fr_sql_query_t *query_ctx);
191
192typedef struct {
195
196typedef struct {
197 module_t common; //!< Common fields for all loadable modules.
198
199 int flags;
200
201 unlang_function_t sql_query_resume; //!< Callback run after an SQL trunk query is run.
202 unlang_function_t sql_select_query_resume; //!< Callback run after an SQL select trunk query is run.
203
206
208 sql_rcode_t (*sql_fields)(char const **out[], fr_sql_query_t *query_ctx, rlm_sql_config_t const *config);
210
211 sql_error_t sql_error; //!< Get any errors from the previous query.
212
215
217 void *(*sql_escape_arg_alloc)(TALLOC_CTX *ctx, fr_event_list_t *el, void *uctx);
218 void (*sql_escape_arg_free)(void *uctx);
219
220 trunk_io_funcs_t trunk_io_funcs; //!< Trunk callback functions for this driver.
222
223struct sql_inst {
225
226 fr_dict_attr_t const *sql_user; //!< Cached pointer to SQL-User-Name
227 //!< dictionary attribute.
229
230 module_instance_t *driver_submodule; //!< Driver's submodule.
231 rlm_sql_driver_t const *driver; //!< Driver's exported interface.
232
235 void *sql_escape_arg; //!< Instance specific argument to be passed to escape function.
239 fr_sql_query_t *(*query_alloc)(TALLOC_CTX *ctx, rlm_sql_t const *inst, request_t *request, trunk_t *trunk, char const *query_str, fr_sql_query_type_t type);
240
241 char const *name; //!< Module instance name.
242 fr_dict_attr_t const *group_da; //!< Group dictionary attribute.
243 module_instance_t const *mi; //!< Module instance data for thread lookups.
244};
245
247void rlm_sql_query_log(rlm_sql_t const *inst, char const *filename, char const *query) CC_HINT(nonnull);
248unlang_action_t rlm_sql_trunk_query(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx);
249unlang_action_t rlm_sql_fetch_row(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx);
250void rlm_sql_print_error(rlm_sql_t const *inst, request_t *request, fr_sql_query_t *query_ctx, bool force_debug);
251fr_sql_query_t *fr_sql_query_alloc(TALLOC_CTX *ctx, rlm_sql_t const *inst, request_t *request, trunk_t *trunk, char const *query_str, fr_sql_query_type_t type);
252
253/*
254 * sql_state.c
255 */
256fr_trie_t *sql_state_trie_alloc(TALLOC_CTX *ctx);
258int sql_state_entries_from_cs(fr_trie_t *states, CONF_SECTION *overrides);
259sql_state_entry_t const *sql_state_entry_find(fr_trie_t const *states, char const *sql_state);
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
#define RCSIDH(h, id)
Definition build.h:484
#define UNUSED
Definition build.h:315
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
unlang_action_t(* unlang_function_t)(rlm_rcode_t *p_result, int *priority, request_t *request, void *uctx)
A generic function pushed by a module or xlat to functions deeper in the C call stack to create resum...
Definition function.h:49
Stores all information relating to an event list.
Definition event.c:411
fr_log_type_t
Definition log.h:54
size_t(* xlat_escape_legacy_t)(request_t *request, char *out, size_t outlen, char const *in, void *arg)
unsigned int uint32_t
unsigned long int size_t
static const conf_parser_t config[]
Definition base.c:183
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:40
char const * msg
Log message.
Definition rlm_sql.h:64
int sql_state_entries_from_table(fr_trie_t *states, sql_state_entry_t const table[])
Insert the contents of a state table into the state trie.
Definition sql_state.c:124
sql_rcode_t rcode
What should happen if we receive this error.
Definition rlm_sql.h:70
char const * sql_db
Database to run queries against.
Definition rlm_sql.h:78
fr_log_type_t type
Type of log entry L_ERR, L_WARN, L_INFO, L_DBG etc.
Definition rlm_sql.h:62
fr_sql_query_t * fr_sql_query_alloc(TALLOC_CTX *ctx, rlm_sql_t const *inst, request_t *request, trunk_t *trunk, char const *query_str, fr_sql_query_type_t type)
Allocate an sql query structure.
Definition sql.c:184
trunk_t * trunk
Trunk connection for this thread.
Definition rlm_sql.h:110
fr_sql_query_status_t status
Status of the query.
Definition rlm_sql.h:140
trunk_connection_t * tconn
Trunk connection this query is being run on.
Definition rlm_sql.h:136
size_t sql_rcode_table_len
Definition sql.c:63
rlm_sql_t const * inst
Module instance data.
Definition rlm_sql.h:111
fr_sql_query_type_t type
Type of query.
Definition rlm_sql.h:139
unlang_action_t rlm_sql_fetch_row(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
Call the driver's sql_fetch_row function.
Definition sql.c:80
void rlm_sql_query_log(rlm_sql_t const *inst, char const *filename, char const *query)
Definition sql.c:364
char const * meaning
Verbose description.
Definition rlm_sql.h:69
void * uctx
Driver specific data.
Definition rlm_sql.h:143
bool expand_rhs
expand the RHS for check / reply tables
Definition rlm_sql.h:93
trunk_t * trunk
Trunk this query is being run on.
Definition rlm_sql.h:135
fr_table_num_sorted_t const sql_rcode_table[]
Definition sql.c:55
char const * group_attribute
Name of the group attribute.
Definition rlm_sql.h:80
char const * allowed_chars
Chars which done need escaping..
Definition rlm_sql.h:95
rlm_sql_t const * inst
Module instance for this query.
Definition rlm_sql.h:133
unlang_action_t sql_get_map_list(request_t *request, fr_sql_map_ctx_t *map_ctx, trunk_t *trunk)
Submit the query to get any user / group check or reply pairs.
Definition sql.c:347
char const * query_str
Query string to run.
Definition rlm_sql.h:138
request_t * request
Request this query relates to.
Definition rlm_sql.h:134
fr_sql_query_t * query_ctx
Query context.
Definition rlm_sql.h:152
map_list_t * out
List to append entries to.
Definition rlm_sql.h:154
fr_trie_t * sql_state_trie_alloc(TALLOC_CTX *ctx)
Allocate a sql_state trie, and insert the initial set of entries.
Definition sql_state.c:102
sql_rcode_t
Action to take at end of an SQL query.
Definition rlm_sql.h:44
@ RLM_SQL_QUERY_INVALID
Query syntax error.
Definition rlm_sql.h:45
@ RLM_SQL_ALT_QUERY
Key constraint violation, use an alternative query.
Definition rlm_sql.h:49
@ RLM_SQL_RECONNECT
Stale connection, should reconnect.
Definition rlm_sql.h:48
@ RLM_SQL_ERROR
General connection/server error.
Definition rlm_sql.h:46
@ RLM_SQL_OK
Success.
Definition rlm_sql.h:47
@ RLM_SQL_NO_MORE_ROWS
No more rows available.
Definition rlm_sql.h:50
fr_sql_query_type_t
Definition rlm_sql.h:115
@ SQL_QUERY_SELECT
Definition rlm_sql.h:116
@ SQL_QUERY_OTHER
Definition rlm_sql.h:117
fr_time_delta_t query_timeout
How long to allow queries to run for.
Definition rlm_sql.h:96
unlang_action_t rlm_sql_trunk_query(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
Submit an SQL query using a trunk connection.
Definition sql.c:235
bool expand_rhs
for reply items
Definition rlm_sql.h:156
bool cache_groups
cache group names in &control.SQL-Group
Definition rlm_sql.h:82
char const * sql_server
Server to connect to.
Definition rlm_sql.h:74
TALLOC_CTX * ctx
To allocate map entries in.
Definition rlm_sql.h:149
void * sql_escape_arg
Thread specific argument to be passed to escape function.
Definition rlm_sql.h:112
char const * sql_state
2-5 char error code.
Definition rlm_sql.h:68
sql_fall_through_t
Definition rlm_sql.h:53
@ FALL_THROUGH_NO
Definition rlm_sql.h:54
@ FALL_THROUGH_DEFAULT
Definition rlm_sql.h:56
@ FALL_THROUGH_YES
Definition rlm_sql.h:55
char ** rlm_sql_row_t
Definition rlm_sql.h:59
rlm_sql_t const * inst
Module instance data.
Definition rlm_sql.h:150
char const * sql_login
Login credentials to use.
Definition rlm_sql.h:76
int rows
How many rows the query returned.
Definition rlm_sql.h:155
void rlm_sql_print_error(rlm_sql_t const *inst, request_t *request, fr_sql_query_t *query_ctx, bool force_debug)
Retrieve any errors from the SQL driver.
Definition sql.c:123
rlm_sql_row_t row
Row data from the last query.
Definition rlm_sql.h:142
int sql_state_entries_from_cs(fr_trie_t *states, CONF_SECTION *overrides)
Insert the contents of a CONF_SECTION into the state trie.
Definition sql_state.c:153
sql_rcode_t rcode
Result code.
Definition rlm_sql.h:141
char const * sql_password
Login password to use.
Definition rlm_sql.h:77
fr_value_box_t * query
Query string used for fetching pairs.
Definition rlm_sql.h:151
trunk_request_t * treq
Trunk request for this query.
Definition rlm_sql.h:137
bool read_profiles
Read user profiles by default.
Definition rlm_sql.h:88
rlm_sql_t const * sql
Definition rlm_sql.h:193
uint32_t sql_port
Port to connect to.
Definition rlm_sql.h:75
size_t sql_rcode_description_table_len
Definition sql.c:53
char const * connect_query
Query executed after establishing new connection.
Definition rlm_sql.h:98
trunk_conf_t trunk_conf
Configuration for trunk connections.
Definition rlm_sql.h:101
fr_table_num_sorted_t const sql_rcode_description_table[]
Definition sql.c:45
fr_sql_query_status_t
Status of an SQL query.
Definition rlm_sql.h:122
@ SQL_QUERY_CANCELLED
A cancellation has been sent to the server.
Definition rlm_sql.h:129
@ SQL_QUERY_RETURNED
Query has executed.
Definition rlm_sql.h:126
@ SQL_QUERY_FETCHING_RESULTS
Fetching results from server.
Definition rlm_sql.h:127
@ SQL_QUERY_FAILED
Failed to submit.
Definition rlm_sql.h:123
@ SQL_QUERY_SUBMITTED
Submitted for execution.
Definition rlm_sql.h:125
@ SQL_QUERY_PREPARED
Ready to submit.
Definition rlm_sql.h:124
@ SQL_QUERY_RESULTS_FETCHED
Results fetched from the server.
Definition rlm_sql.h:128
size_t(* sql_error_t)(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, fr_sql_query_t *query_ctx)
Retrieve errors from the last query operation.
Definition rlm_sql.h:190
fr_dict_attr_t const * list
Default list for pair evaluation.
Definition rlm_sql.h:153
bool read_groups
Read user groups by default.
Definition rlm_sql.h:84
sql_state_entry_t const * sql_state_entry_find(fr_trie_t const *states, char const *sql_state)
Lookup an SQL state based on an error code returned from the SQL server or client library.
Definition sql_state.c:203
Context used when fetching attribute value pairs as a map list.
Definition rlm_sql.h:148
Definition rlm_sql.h:61
Definition rlm_sql.h:67
static sql_rcode_t sql_fields(char const **out[], fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
static int sql_num_rows(fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
static int sql_affected_rows(UNUSED fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
static sql_rcode_t sql_finish_query(fr_sql_query_t *query_ctx, rlm_sql_config_t const *config)
static sql_rcode_t sql_free_result(fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
static sql_rcode_t sql_finish_select_query(fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
static void sql_escape_arg_free(void *uctx)
Module instance data.
Definition module.h:265
Struct exported by a rlm_* module.
Definition module.h:195
eap_aka_sim_process_conf_t * inst
fr_aka_sim_id_type_t type
module_t common
Common fields for all loadable modules.
Definition rlm_sql.h:197
sql_error_t sql_error
Get any errors from the previous query.
Definition rlm_sql.h:211
trunk_io_funcs_t trunk_io_funcs
Trunk callback functions for this driver.
Definition rlm_sql.h:220
xlat_escape_legacy_t sql_escape_func
Definition rlm_sql.h:216
unlang_function_t sql_query_resume
Callback run after an SQL trunk query is run.
Definition rlm_sql.h:201
unlang_function_t sql_fetch_row
Definition rlm_sql.h:207
unlang_function_t sql_select_query_resume
Callback run after an SQL select trunk query is run.
Definition rlm_sql.h:202
unlang_function_t fetch_row
Definition rlm_sql.h:238
unlang_function_t query
Definition rlm_sql.h:236
module_instance_t * driver_submodule
Driver's submodule.
Definition rlm_sql.h:230
char const * name
Module instance name.
Definition rlm_sql.h:241
fr_dict_attr_t const * group_da
Group dictionary attribute.
Definition rlm_sql.h:242
exfile_t * ef
Definition rlm_sql.h:228
fr_dict_attr_t const * sql_user
Cached pointer to SQL-User-Name dictionary attribute.
Definition rlm_sql.h:226
unlang_function_t select
Definition rlm_sql.h:237
fr_value_box_escape_t box_escape_func
Definition rlm_sql.h:234
rlm_sql_config_t config
Definition rlm_sql.h:224
rlm_sql_driver_t const * driver
Driver's exported interface.
Definition rlm_sql.h:231
xlat_escape_legacy_t sql_escape_func
Definition rlm_sql.h:233
module_instance_t const * mi
Module instance data for thread lookups.
Definition rlm_sql.h:243
void * sql_escape_arg
Instance specific argument to be passed to escape function.
Definition rlm_sql.h:235
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
Associates request queues with a connection.
Definition trunk.c:134
Wraps a normal request.
Definition trunk.c:100
Main trunk management handle.
Definition trunk.c:198
Common configuration parameters for a trunk.
Definition trunk.h:224
I/O functions to pass to trunk_alloc.
Definition trunk.h:732
static fr_event_list_t * el
int(* fr_value_box_escape_t)(fr_value_box_t *vb, void *uctx)
Escape a value box.
Definition value.h:651
int nonnull(2, 5))
static size_t char ** out
Definition value.h:997