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: ef2cf6c2891e86194ad659cbc1f43b053b1ae7c8 $
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: ef2cf6c2891e86194ad659cbc1f43b053b1ae7c8 $")
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 char const *allowed_chars; //!< Chars which done need escaping..
94 fr_time_delta_t query_timeout; //!< How long to allow queries to run for.
95
96 char const *connect_query; //!< Query executed after establishing
97 //!< new connection.
98
99 trunk_conf_t trunk_conf; //!< Configuration for trunk connections.
101
102typedef struct sql_inst rlm_sql_t;
103
104/*
105 * Per-thread instance data structure
106 */
107typedef struct {
108 trunk_t *trunk; //!< Trunk connection for this thread.
109 rlm_sql_t const *inst; //!< Module instance data.
110 void *sql_escape_arg; //!< Thread specific argument to be passed to escape function.
112
117
118/** Status of an SQL query
119 */
120typedef enum {
121 SQL_QUERY_FAILED = -1, //!< Failed to submit.
122 SQL_QUERY_PREPARED = 0, //!< Ready to submit.
123 SQL_QUERY_SUBMITTED, //!< Submitted for execution.
124 SQL_QUERY_RETURNED, //!< Query has executed.
125 SQL_QUERY_FETCHING_RESULTS, //!< Fetching results from server.
126 SQL_QUERY_RESULTS_FETCHED, //!< Results fetched from the server.
127 SQL_QUERY_CANCELLED //!< A cancellation has been sent to the server.
129
130typedef struct {
131 rlm_sql_t const *inst; //!< Module instance for this query.
132 request_t *request; //!< Request this query relates to.
133 trunk_t *trunk; //!< Trunk this query is being run on.
134 trunk_connection_t *tconn; //!< Trunk connection this query is being run on.
135 trunk_request_t *treq; //!< Trunk request for this query.
136 char const *query_str; //!< Query string to run.
137 fr_sql_query_type_t type; //!< Type of query.
138 fr_sql_query_status_t status; //!< Status of the query.
139 sql_rcode_t rcode; //!< Result code.
140 rlm_sql_row_t row; //!< Row data from the last query.
141 void *uctx; //!< Driver specific data.
143
144/** Context used when fetching attribute value pairs as a map list
145 */
146typedef struct {
147 TALLOC_CTX *ctx; //!< To allocate map entries in.
148 rlm_sql_t const *inst; //!< Module instance data.
149 fr_value_box_t *query; //!< Query string used for fetching pairs.
150 fr_sql_query_t *query_ctx; //!< Query context.
151 fr_dict_attr_t const *list; //!< Default list for pair evaluation.
152 map_list_t *out; //!< List to append entries to.
153 int rows; //!< How many rows the query returned.
155
159extern size_t sql_rcode_table_len;
160
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#define RLM_SQL_MULTI_QUERY_CONN 2 //!< Can support multiple queries on a single connection.
167
168/** Retrieve errors from the last query operation
169 *
170 * @note Buffers allocated in the context provided will be automatically freed. The driver
171 * should not free these buffers explicitly.
172 * @note If the driver uses its own buffers to aggregate messages, they should be cleared
173 * on sql_query_finish, and after each call to sql_error, to prevent the same messages
174 * being printed multiple times.
175 *
176 * @param[in,out] ctx to allocate any buffers required. If static buffers are provided by the
177 * driver they need not be talloc_strdupd, just write the pointer to those buffers to the
178 * .msg field of a sql_log_entry_t element.
179 * @param[out] out a pre-allocated array of log entries to fill. Need not be NULL terminated.
180 * @param[in] outlen Number of log entries available for populating. Do not write to index
181 * out[outlen] or higher.
182 * @param[in] query_ctx to retrieve errors from.
183 * @return
184 * 0 - If no error messages are available.
185 * >0 - Number of log entries
186 */
187typedef size_t (*sql_error_t)(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, fr_sql_query_t *query_ctx);
188
189typedef struct {
192
193typedef struct {
194 module_t common; //!< Common fields for all loadable modules.
195
196 int flags;
197
198 unlang_function_t sql_query_resume; //!< Callback run after an SQL trunk query is run.
199 unlang_function_t sql_select_query_resume; //!< Callback run after an SQL select trunk query is run.
200
203
205 sql_rcode_t (*sql_fields)(char const **out[], fr_sql_query_t *query_ctx, rlm_sql_config_t const *config);
207
208 sql_error_t sql_error; //!< Get any errors from the previous query.
209
212
214 void *(*sql_escape_arg_alloc)(TALLOC_CTX *ctx, fr_event_list_t *el, void *uctx);
215 void (*sql_escape_arg_free)(void *uctx);
216
217 trunk_io_funcs_t trunk_io_funcs; //!< Trunk callback functions for this driver.
219
220struct sql_inst {
222
223 fr_dict_attr_t const *sql_user; //!< Cached pointer to SQL-User-Name
224 //!< dictionary attribute.
226
227 module_instance_t *driver_submodule; //!< Driver's submodule.
228 rlm_sql_driver_t const *driver; //!< Driver's exported interface.
229
232 void *sql_escape_arg; //!< Instance specific argument to be passed to escape function.
236 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);
237
238 char const *name; //!< Module instance name.
239 fr_dict_attr_t const *group_da; //!< Group dictionary attribute.
240 module_instance_t const *mi; //!< Module instance data for thread lookups.
241};
242
244void rlm_sql_query_log(rlm_sql_t const *inst, char const *filename, char const *query) CC_HINT(nonnull);
245unlang_action_t rlm_sql_trunk_query(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx);
246unlang_action_t rlm_sql_fetch_row(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx);
247void rlm_sql_print_error(rlm_sql_t const *inst, request_t *request, fr_sql_query_t *query_ctx, bool force_debug);
248fr_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);
249
250/*
251 * sql_state.c
252 */
253fr_trie_t *sql_state_trie_alloc(TALLOC_CTX *ctx);
255int sql_state_entries_from_cs(fr_trie_t *states, CONF_SECTION *overrides);
256sql_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:108
fr_sql_query_status_t status
Status of the query.
Definition rlm_sql.h:138
trunk_connection_t * tconn
Trunk connection this query is being run on.
Definition rlm_sql.h:134
size_t sql_rcode_table_len
Definition sql.c:63
rlm_sql_t const * inst
Module instance data.
Definition rlm_sql.h:109
fr_sql_query_type_t type
Type of query.
Definition rlm_sql.h:137
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:359
char const * meaning
Verbose description.
Definition rlm_sql.h:69
void * uctx
Driver specific data.
Definition rlm_sql.h:141
trunk_t * trunk
Trunk this query is being run on.
Definition rlm_sql.h:133
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:93
rlm_sql_t const * inst
Module instance for this query.
Definition rlm_sql.h:131
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:342
char const * query_str
Query string to run.
Definition rlm_sql.h:136
request_t * request
Request this query relates to.
Definition rlm_sql.h:132
fr_sql_query_t * query_ctx
Query context.
Definition rlm_sql.h:150
map_list_t * out
List to append entries to.
Definition rlm_sql.h:152
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:113
@ SQL_QUERY_SELECT
Definition rlm_sql.h:114
@ SQL_QUERY_OTHER
Definition rlm_sql.h:115
fr_time_delta_t query_timeout
How long to allow queries to run for.
Definition rlm_sql.h:94
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 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:147
void * sql_escape_arg
Thread specific argument to be passed to escape function.
Definition rlm_sql.h:110
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:148
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:153
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:140
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:139
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:149
trunk_request_t * treq
Trunk request for this query.
Definition rlm_sql.h:135
bool read_profiles
Read user profiles by default.
Definition rlm_sql.h:88
rlm_sql_t const * sql
Definition rlm_sql.h:190
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:96
trunk_conf_t trunk_conf
Configuration for trunk connections.
Definition rlm_sql.h:99
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:120
@ SQL_QUERY_CANCELLED
A cancellation has been sent to the server.
Definition rlm_sql.h:127
@ SQL_QUERY_RETURNED
Query has executed.
Definition rlm_sql.h:124
@ SQL_QUERY_FETCHING_RESULTS
Fetching results from server.
Definition rlm_sql.h:125
@ SQL_QUERY_FAILED
Failed to submit.
Definition rlm_sql.h:121
@ SQL_QUERY_SUBMITTED
Submitted for execution.
Definition rlm_sql.h:123
@ SQL_QUERY_PREPARED
Ready to submit.
Definition rlm_sql.h:122
@ SQL_QUERY_RESULTS_FETCHED
Results fetched from the server.
Definition rlm_sql.h:126
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:187
fr_dict_attr_t const * list
Default list for pair evaluation.
Definition rlm_sql.h:151
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:146
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:194
sql_error_t sql_error
Get any errors from the previous query.
Definition rlm_sql.h:208
trunk_io_funcs_t trunk_io_funcs
Trunk callback functions for this driver.
Definition rlm_sql.h:217
xlat_escape_legacy_t sql_escape_func
Definition rlm_sql.h:213
unlang_function_t sql_query_resume
Callback run after an SQL trunk query is run.
Definition rlm_sql.h:198
unlang_function_t sql_fetch_row
Definition rlm_sql.h:204
unlang_function_t sql_select_query_resume
Callback run after an SQL select trunk query is run.
Definition rlm_sql.h:199
unlang_function_t fetch_row
Definition rlm_sql.h:235
unlang_function_t query
Definition rlm_sql.h:233
module_instance_t * driver_submodule
Driver's submodule.
Definition rlm_sql.h:227
char const * name
Module instance name.
Definition rlm_sql.h:238
fr_dict_attr_t const * group_da
Group dictionary attribute.
Definition rlm_sql.h:239
exfile_t * ef
Definition rlm_sql.h:225
fr_dict_attr_t const * sql_user
Cached pointer to SQL-User-Name dictionary attribute.
Definition rlm_sql.h:223
unlang_function_t select
Definition rlm_sql.h:234
fr_value_box_escape_t box_escape_func
Definition rlm_sql.h:231
rlm_sql_config_t config
Definition rlm_sql.h:221
rlm_sql_driver_t const * driver
Driver's exported interface.
Definition rlm_sql.h:228
xlat_escape_legacy_t sql_escape_func
Definition rlm_sql.h:230
module_instance_t const * mi
Module instance data for thread lookups.
Definition rlm_sql.h:240
void * sql_escape_arg
Instance specific argument to be passed to escape function.
Definition rlm_sql.h:232
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