2 * sql.c rlm_sql - FreeRADIUS SQL Module
3 * Main code directly taken from ICRADIUS
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * Copyright 2001,2006 The FreeRADIUS server project
22 * Copyright 2000 Mike Machado <mike@innercite.com>
23 * Copyright 2000 Alan DeKok <aland@ox.org>
24 * Copyright 2001 Chad Miller <cmiller@surfsouth.com>
29 #include <freeradius-devel/radiusd.h>
30 #include <freeradius-devel/rad_assert.h>
42 static int sql_conn_destructor(void *conn)
44 rlm_sql_handle_t *handle = conn;
45 rlm_sql_t *inst = handle->inst;
49 exec_trigger(NULL, inst->cs, "modules.sql.close", false);
54 static void *mod_conn_create(void *instance)
57 rlm_sql_t *inst = instance;
58 rlm_sql_handle_t *handle;
60 handle = talloc_zero(instance, rlm_sql_handle_t);
63 * Handle requires a pointer to the SQL inst so the
64 * destructor has access to the module configuration.
69 * When something frees this handle the destructor set by
70 * the driver will be called first, closing any open sockets.
71 * Then we call our destructor to trigger an modules.sql.close
72 * event, then all the memory is freed.
74 talloc_set_destructor((void *) handle, sql_conn_destructor);
76 rcode = (inst->module->sql_socket_init)(handle, inst->config);
78 exec_trigger(NULL, inst->cs, "modules.sql.open", false);
83 exec_trigger(NULL, inst->cs, "modules.sql.fail", true);
86 * Destroy any half opened connections.
93 * @todo Calls to this should eventually go away.
95 static int mod_conn_delete(UNUSED void *instance, void *handle)
97 return talloc_free(handle);
100 /*************************************************************************
102 * Function: sql_socket_pool_init
104 * Purpose: Connect to the sql server, if possible
106 *************************************************************************/
107 int sql_socket_pool_init(rlm_sql_t * inst)
109 inst->pool = fr_connection_pool_init(inst->cs, inst,
110 mod_conn_create, NULL, mod_conn_delete,
112 if (!inst->pool) return -1;
117 /*************************************************************************
119 * Function: sql_poolfree
121 * Purpose: Clean up and free sql pool
123 *************************************************************************/
124 void sql_poolfree(rlm_sql_t * inst)
126 fr_connection_pool_delete(inst->pool);
130 /*************************************************************************
132 * Function: sql_get_socket
134 * Purpose: Return a SQL handle from the connection pool
136 *************************************************************************/
137 rlm_sql_handle_t * sql_get_socket(rlm_sql_t * inst)
139 return fr_connection_get(inst->pool);
142 /*************************************************************************
144 * Function: sql_release_socket
146 * Purpose: Frees a SQL handle back to the connection pool
148 *************************************************************************/
149 int sql_release_socket(rlm_sql_t * inst, rlm_sql_handle_t * handle)
151 fr_connection_release(inst->pool, handle);
156 /*************************************************************************
158 * Function: sql_userparse
160 * Purpose: Read entries from the database and fill VALUE_PAIR structures
162 *************************************************************************/
163 int sql_userparse(TALLOC_CTX *ctx, VALUE_PAIR **head, rlm_sql_row_t row)
166 char const *ptr, *value;
167 char buf[MAX_STRING_LEN];
169 FR_TOKEN token, operator = T_EOL;
172 * Verify the 'Attribute' field
174 if (!row[2] || row[2][0] == '\0') {
175 ERROR("rlm_sql: The 'Attribute' field is empty or NULL, skipping the entire row.");
180 * Verify the 'op' field
182 if (row[4] != NULL && row[4][0] != '\0') {
184 operator = gettoken(&ptr, buf, sizeof(buf));
185 if ((operator < T_OP_ADD) ||
186 (operator > T_OP_CMP_EQ)) {
187 ERROR("rlm_sql: Invalid operator \"%s\" for attribute %s", row[4], row[2]);
193 * Complain about empty or invalid 'op' field
195 operator = T_OP_CMP_EQ;
196 ERROR("rlm_sql: The 'op' field for attribute '%s = %s' is NULL, or non-existent.", row[2], row[3]);
197 ERROR("rlm_sql: You MUST FIX THIS if you want the configuration to behave as you expect.");
201 * The 'Value' field may be empty or NULL
205 * If we have a new-style quoted string, where the
206 * *entire* string is quoted, do xlat's.
208 if (row[3] != NULL &&
209 ((row[3][0] == '\'') || (row[3][0] == '`') || (row[3][0] == '"')) &&
210 (row[3][0] == row[3][strlen(row[3])-1])) {
212 token = gettoken(&value, buf, sizeof(buf));
215 * Take the unquoted string.
217 case T_SINGLE_QUOTED_STRING:
218 case T_DOUBLE_QUOTED_STRING:
223 * Mark the pair to be allocated later.
225 case T_BACK_QUOTED_STRING:
231 * Keep the original string.
242 vp = pairmake(ctx, NULL, row[2], NULL, operator);
244 ERROR("rlm_sql: Failed to create the pair: %s",
250 if (pairmark_xlat(vp, value) < 0) {
251 ERROR("rlm_sql: Error marking pair for xlat");
257 if (pairparsevalue(vp, value) < 0) {
258 ERROR("rlm_sql: Error parsing value");
266 * Add the pair into the packet
273 /*************************************************************************
275 * Function: rlm_sql_fetch_row
277 * Purpose: call the module's sql_fetch_row and implement re-connect
279 *************************************************************************/
280 int rlm_sql_fetch_row(rlm_sql_handle_t **handle, rlm_sql_t *inst)
284 if (!*handle || !(*handle)->conn) {
289 * We can't implement reconnect logic here, because the caller may require
290 * the original connection to free up queries or result sets associated with
293 ret = (inst->module->sql_fetch_row)(*handle, inst->config);
296 char const *error = (inst->module->sql_error)(*handle, inst->config);
297 DEBUGE("rlm_sql (%s): Error fetching row: %s",
298 inst->config->xlat_name, error ? error : "<UNKNOWN>");
304 /*************************************************************************
306 * Function: rlm_sql_query
308 * Purpose: call the module's sql_query and implement re-connect
310 *************************************************************************/
311 int rlm_sql_query(rlm_sql_handle_t **handle, rlm_sql_t *inst, char const *query)
316 * If there's no query, return an error.
318 if (!query || !*query) {
322 if (!*handle || !(*handle)->conn) {
328 DEBUG("rlm_sql (%s): Executing query: '%s'",
329 inst->config->xlat_name, query);
331 ret = (inst->module->sql_query)(*handle, inst->config, query);
333 * Run through all available sockets until we exhaust all existing
334 * sockets in the pool and fail to establish a *new* connection.
336 if (ret == RLM_SQL_RECONNECT) {
338 *handle = fr_connection_reconnect(inst->pool, *handle);
339 if (!*handle) return RLM_SQL_RECONNECT;
345 char const *error = (inst->module->sql_error)(*handle, inst->config);
346 ERROR("rlm_sql (%s): Database query error: %s",
347 inst->config->xlat_name, error ? error : "<UNKNOWN>");
354 /*************************************************************************
356 * Function: rlm_sql_select_query
358 * Purpose: call the module's sql_select_query and implement re-connect
360 *************************************************************************/
361 int rlm_sql_select_query(rlm_sql_handle_t **handle, rlm_sql_t *inst, char const *query)
366 * If there's no query, return an error.
368 if (!query || !*query) {
372 if (!*handle || !(*handle)->conn) {
378 DEBUG("rlm_sql (%s): Executing query: '%s'",
379 inst->config->xlat_name, query);
381 ret = (inst->module->sql_select_query)(*handle, inst->config, query);
383 * Run through all available sockets until we exhaust all existing
384 * sockets in the pool and fail to establish a *new* connection.
386 if (ret == RLM_SQL_RECONNECT) {
388 *handle = fr_connection_reconnect(inst->pool, *handle);
389 if (!*handle) return RLM_SQL_RECONNECT;
395 char const *error = (inst->module->sql_error)(*handle, inst->config);
396 ERROR("rlm_sql (%s): Database query error '%s'",
397 inst->config->xlat_name, error ? error : "<UNKNOWN>");
405 /*************************************************************************
407 * Function: sql_getvpdata
409 * Purpose: Get any group check or reply pairs
411 *************************************************************************/
412 int sql_getvpdata(rlm_sql_t * inst, rlm_sql_handle_t **handle,
413 TALLOC_CTX *ctx, VALUE_PAIR **pair, char const *query)
418 if (rlm_sql_select_query(handle, inst, query)) {
422 while (rlm_sql_fetch_row(handle, inst) == 0) {
423 row = (*handle)->row;
426 if (sql_userparse(ctx, pair, row) != 0) {
427 ERROR("rlm_sql (%s): Error getting data from database", inst->config->xlat_name);
429 (inst->module->sql_finish_select_query)(*handle, inst->config);
435 (inst->module->sql_finish_select_query)(*handle, inst->config);
441 * Log the query to a file.
443 void rlm_sql_query_log(rlm_sql_t *inst, REQUEST *request,
444 sql_acct_section_t *section, char const *query)
447 char const *filename = NULL;
448 char *expanded = NULL;
451 filename = section->logfile;
455 filename = inst->config->logfile;
462 if (radius_axlat(&expanded, request, filename, NULL, NULL) < 0) {
466 fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0666);
468 ERROR("rlm_sql (%s): Couldn't open logfile '%s': %s", inst->config->xlat_name,
469 expanded, strerror(errno));
471 talloc_free(expanded);
475 if ((rad_lockfd(fd, MAX_QUERY_LEN) < 0) || (write(fd, query, strlen(query)) < 0) || (write(fd, ";\n", 2) < 0)) {
476 ERROR("rlm_sql (%s): Failed writing to logfile '%s': %s", inst->config->xlat_name, expanded,
480 talloc_free(expanded);
481 close(fd); /* and release the lock */