The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: e8d0a575fae4fabdeacb133ba7892fec823cf7ae $
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  */
28 RCSIDH(rlm_sql_h, "$Id: e8d0a575fae4fabdeacb133ba7892fec823cf7ae $")
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 
35 #define FR_ITEM_CHECK 0
36 #define FR_ITEM_REPLY 1
37 
38 
39 /** Action to take at end of an SQL query
40  *
41  */
42 typedef enum {
43  RLM_SQL_QUERY_INVALID = -3, //!< Query syntax error.
44  RLM_SQL_ERROR = -2, //!< General connection/server error.
45  RLM_SQL_OK = 0, //!< Success.
46  RLM_SQL_RECONNECT = 1, //!< Stale connection, should reconnect.
47  RLM_SQL_ALT_QUERY, //!< Key constraint violation, use an alternative query.
48  RLM_SQL_NO_MORE_ROWS, //!< No more rows available
49 } sql_rcode_t;
50 
51 typedef enum {
56 
57 typedef char **rlm_sql_row_t;
58 
59 typedef struct {
60  fr_log_type_t type; //!< Type of log entry L_ERR, L_WARN, L_INFO,
61  ///< L_DBG etc.
62  char const *msg; //!< Log message.
64 
65 typedef struct {
66  char const *sql_state; //!< 2-5 char error code.
67  char const *meaning; //!< Verbose description.
68  sql_rcode_t rcode; //!< What should happen if we receive this error.
70 
71 typedef struct {
72  char const *sql_server; //!< Server to connect to.
73  uint32_t sql_port; //!< Port to connect to.
74  char const *sql_login; //!< Login credentials to use.
75  char const *sql_password; //!< Login password to use.
76  char const *sql_db; //!< Database to run queries against.
77 
78  char const *group_attribute; //!< Name of the group attribute.
79 
80  bool cache_groups; //!< cache group names in &control.SQL-Group
81 
82  bool read_groups; //!< Read user groups by default.
83  //!< If false, Fall-Through = yes is required
84  //!< in the previous reply list to process
85  //!< groups.
86  bool read_profiles; //!< Read user profiles by default.
87  //!< If false, Fall-Through = yes is required
88  //!< in the previous reply list to process
89  //!< profiles.
90 
91  char const *allowed_chars; //!< Chars which done need escaping..
92  fr_time_delta_t query_timeout; //!< How long to allow queries to run for.
93 
94  char const *connect_query; //!< Query executed after establishing
95  //!< new connection.
97 
98 typedef struct sql_inst rlm_sql_t;
99 
100 typedef struct {
101  void *conn; //!< Database specific connection handle.
102  rlm_sql_row_t row; //!< Row data from the last query.
103  rlm_sql_t const *inst; //!< The rlm_sql instance this connection belongs to.
104  TALLOC_CTX *log_ctx; //!< Talloc pool used to avoid allocing memory
105  //!< when log strings need to be copied.
107 
109 extern size_t sql_rcode_description_table_len;
111 extern size_t sql_rcode_table_len;
112 
113 /*
114  * Capabilities flags for drivers
115  */
116 #define RLM_SQL_RCODE_FLAGS_ALT_QUERY 1 //!< Can distinguish between other errors and those
117  //!< resulting from a unique key violation.
118 
119 /** Retrieve errors from the last query operation
120  *
121  * @note Buffers allocated in the context provided will be automatically freed. The driver
122  * should not free these buffers explicitly.
123  * @note If the driver uses its own buffers to aggregate messages, they should be cleared
124  * on sql_query_finish, and after each call to sql_error, to prevent the same messages
125  * being printed multiple times.
126  *
127  * @param[in,out] ctx to allocate any buffers required. If static buffers are provided by the
128  * driver they need not be talloc_strdupd, just write the pointer to those buffers to the
129  * .msg field of a sql_log_entry_t element.
130  * @param[out] out a pre-allocated array of log entries to fill. Need not be NULL terminated.
131  * @param[in] outlen Number of log entries available for populating. Do not write to index
132  * out[outlen] or higher.
133  * @param[in] handle to retrieve errors from.
134  * @param[in] config of the SQL instance.
135  * @return
136  * 0 - If no error messages are available.
137  * >0 - Number of log entries
138  */
139 typedef size_t (*sql_error_t)(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, rlm_sql_handle_t *handle,
140  rlm_sql_config_t const *config);
141 
142 typedef struct {
143  rlm_sql_t const *sql;
146 
147 typedef struct {
148  module_t common; //!< Common fields for all loadable modules.
149 
150  int flags;
151 
152  int number; //! for safe operations
153 
156 
157  sql_rcode_t (*sql_query)(rlm_sql_handle_t *handle, rlm_sql_config_t const *config, char const *query);
158  sql_rcode_t (*sql_select_query)(rlm_sql_handle_t *handle, rlm_sql_config_t const *config, char const *query);
160 
164 
166  sql_rcode_t (*sql_fields)(char const **out[], rlm_sql_handle_t *handle, rlm_sql_config_t const *config);
168 
169  sql_error_t sql_error; //!< Get any errors from the previous query.
170 
173 
176 
177 struct sql_inst {
180 
181  fr_dict_attr_t const *sql_user; //!< Cached pointer to SQL-User-Name
182  //!< dictionary attribute.
184 
185  module_instance_t *driver_submodule; //!< Driver's submodule.
186  rlm_sql_driver_t const *driver; //!< Driver's exported interface.
187 
190  sql_rcode_t (*query)(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle, char const *query);
191  sql_rcode_t (*select)(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle, char const *query);
193 
194  char const *name; //!< Module instance name.
195  fr_dict_attr_t const *group_da; //!< Group dictionary attribute.
196 };
197 
198 void *sql_mod_conn_create(TALLOC_CTX *ctx, void *instance, fr_time_delta_t timeout);
199 int sql_get_map_list(TALLOC_CTX *ctx, rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle, map_list_t *out, char const *query, fr_dict_attr_t const *list);
200 void rlm_sql_query_log(rlm_sql_t const *inst, char const *filename, char const *query) CC_HINT(nonnull);
201 sql_rcode_t rlm_sql_select_query(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle, char const *query) CC_HINT(nonnull (1, 3, 4));
202 sql_rcode_t rlm_sql_query(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle, char const *query) CC_HINT(nonnull (1, 3, 4));
204 void rlm_sql_print_error(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t *handle, bool force_debug);
205 
206 /*
207  * sql_state.c
208  */
209 fr_trie_t *sql_state_trie_alloc(TALLOC_CTX *ctx);
210 int sql_state_entries_from_table(fr_trie_t *states, sql_state_entry_t const table[]);
211 int sql_state_entries_from_cs(fr_trie_t *states, CONF_SECTION *overrides);
212 sql_state_entry_t const *sql_state_entry_find(fr_trie_t const *states, char const *sql_state);
#define RCSIDH(h, id)
Definition: build.h:445
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:89
static fr_time_delta_t timeout
Definition: dhcpclient.c:54
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)
Definition: merged_model.c:213
unsigned int uint32_t
Definition: merged_model.c:33
unsigned long int size_t
Definition: merged_model.c:25
A connection pool.
Definition: pool.c:85
static const conf_parser_t config[]
Definition: base.c:188
char const * msg
Log message.
Definition: rlm_sql.h:62
rlm_sql_t const * inst
The rlm_sql instance this connection belongs to.
Definition: rlm_sql.h:103
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:68
char const * sql_db
Database to run queries against.
Definition: rlm_sql.h:76
fr_log_type_t type
Type of log entry L_ERR, L_WARN, L_INFO, L_DBG etc.
Definition: rlm_sql.h:60
TALLOC_CTX * log_ctx
Talloc pool used to avoid allocing memory when log strings need to be copied.
Definition: rlm_sql.h:104
size_t sql_rcode_table_len
Definition: sql.c:63
void * conn
Database specific connection handle.
Definition: rlm_sql.h:101
void rlm_sql_query_log(rlm_sql_t const *inst, char const *filename, char const *query)
Definition: sql.c:613
char const * meaning
Verbose description.
Definition: rlm_sql.h:67
sql_rcode_t rlm_sql_fetch_row(rlm_sql_row_t *out, rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle)
Call the driver's sql_fetch_row function.
Definition: sql.c:299
int sql_get_map_list(TALLOC_CTX *ctx, rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle, map_list_t *out, char const *query, fr_dict_attr_t const *list)
Definition: sql.c:559
rlm_sql_handle_t * handle
Definition: rlm_sql.h:144
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:78
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
char const * allowed_chars
Chars which done need escaping..
Definition: rlm_sql.h:91
sql_rcode_t
Action to take at end of an SQL query.
Definition: rlm_sql.h:42
@ RLM_SQL_QUERY_INVALID
Query syntax error.
Definition: rlm_sql.h:43
@ RLM_SQL_ALT_QUERY
Key constraint violation, use an alternative query.
Definition: rlm_sql.h:47
@ RLM_SQL_RECONNECT
Stale connection, should reconnect.
Definition: rlm_sql.h:46
@ RLM_SQL_ERROR
General connection/server error.
Definition: rlm_sql.h:44
@ RLM_SQL_OK
Success.
Definition: rlm_sql.h:45
@ RLM_SQL_NO_MORE_ROWS
No more rows available.
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 const *config)
Retrieve errors from the last query operation.
Definition: rlm_sql.h:139
fr_time_delta_t query_timeout
How long to allow queries to run for.
Definition: rlm_sql.h:92
bool cache_groups
cache group names in &control.SQL-Group
Definition: rlm_sql.h:80
char const * sql_server
Server to connect to.
Definition: rlm_sql.h:72
sql_rcode_t rlm_sql_select_query(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle, char const *query))
Call the driver's sql_select_query method, reconnecting if necessary.
Definition: sql.c:493
char const * sql_state
2-5 char error code.
Definition: rlm_sql.h:66
sql_fall_through_t
Definition: rlm_sql.h:51
@ FALL_THROUGH_NO
Definition: rlm_sql.h:52
@ FALL_THROUGH_DEFAULT
Definition: rlm_sql.h:54
@ FALL_THROUGH_YES
Definition: rlm_sql.h:53
char ** rlm_sql_row_t
Definition: rlm_sql.h:57
char const * sql_login
Login credentials to use.
Definition: rlm_sql.h:74
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
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
char const * sql_password
Login password to use.
Definition: rlm_sql.h:75
void * sql_mod_conn_create(TALLOC_CTX *ctx, void *instance, fr_time_delta_t timeout)
Definition: sql.c:65
bool read_profiles
Read user profiles by default.
Definition: rlm_sql.h:86
rlm_sql_t const * sql
Definition: rlm_sql.h:143
uint32_t sql_port
Port to connect to.
Definition: rlm_sql.h:73
size_t sql_rcode_description_table_len
Definition: sql.c:53
sql_rcode_t rlm_sql_query(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle, char const *query))
Call the driver's sql_query method, reconnecting if necessary.
Definition: sql.c:392
char const * connect_query
Query executed after establishing new connection.
Definition: rlm_sql.h:94
fr_table_num_sorted_t const sql_rcode_description_table[]
Definition: sql.c:45
void rlm_sql_print_error(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t *handle, bool force_debug)
Retrieve any errors from the SQL driver.
Definition: sql.c:337
rlm_sql_row_t row
Row data from the last query.
Definition: rlm_sql.h:102
bool read_groups
Read user groups by default.
Definition: rlm_sql.h:82
Definition: rlm_sql.h:59
Definition: rlm_sql.h:65
static sql_rcode_t sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t const *config, fr_time_delta_t timeout)
static sql_rcode_t sql_fetch_row(rlm_sql_row_t *out, rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
static sql_rcode_t sql_finish_query(rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
static int sql_affected_rows(UNUSED rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
static sql_rcode_t sql_free_result(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
static int sql_num_rows(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
static sql_rcode_t sql_fields(char const **out[], rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
static sql_rcode_t sql_query(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config, char const *query)
static int sql_num_fields(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
static sql_rcode_t sql_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t const *config, char const *query)
Definition: rlm_sql_db2.c:135
static sql_rcode_t sql_finish_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
Definition: rlm_sql_db2.c:272
static sql_rcode_t sql_store_result(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
Per instance data.
Definition: module.h:169
Struct exported by a rlm_* module.
Definition: module.h:142
eap_aka_sim_process_conf_t * inst
module_t common
Common fields for all loadable modules.
Definition: rlm_sql.h:148
sql_error_t sql_error
Get any errors from the previous query.
Definition: rlm_sql.h:169
xlat_escape_legacy_t sql_escape_func
Definition: rlm_sql.h:174
sql_rcode_t(* select)(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle, char const *query)
Definition: rlm_sql.h:191
module_instance_t * driver_submodule
Driver's submodule.
Definition: rlm_sql.h:185
char const * name
Module instance name.
Definition: rlm_sql.h:194
fr_dict_attr_t const * group_da
Group dictionary attribute.
Definition: rlm_sql.h:195
exfile_t * ef
Definition: rlm_sql.h:183
sql_rcode_t(* fetch_row)(rlm_sql_row_t *out, rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle)
Definition: rlm_sql.h:192
fr_pool_t * pool
Definition: rlm_sql.h:179
fr_dict_attr_t const * sql_user
Cached pointer to SQL-User-Name dictionary attribute.
Definition: rlm_sql.h:181
fr_value_box_escape_t box_escape_func
Definition: rlm_sql.h:189
rlm_sql_config_t config
Definition: rlm_sql.h:178
rlm_sql_driver_t const * driver
Driver's exported interface.
Definition: rlm_sql.h:186
sql_rcode_t(* query)(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t **handle, char const *query)
Definition: rlm_sql.h:190
xlat_escape_legacy_t sql_escape_func
Definition: rlm_sql.h:188
An element in a lexicographically sorted array of name to num mappings.
Definition: table.h:45
A time delta, a difference in time measured in nanoseconds.
Definition: time.h:80
int(* fr_value_box_escape_t)(fr_value_box_t *vb, void *uctx)
Escape a value box.
Definition: value.h:638
int nonnull(2, 5))
static size_t char ** out
Definition: value.h:984