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>
27 #include <freeradius-devel/ident.h>
30 #include <freeradius-devel/radiusd.h>
31 #include <freeradius-devel/rad_assert.h>
43 static int sql_conn_destructor(void *conn)
45 rlm_sql_handle_t *handle = conn;
46 rlm_sql_t *inst = handle->inst;
50 exec_trigger(NULL, inst->cs, "modules.sql.close", FALSE);
53 (inst->module->sql_close)(handle, inst->config);
56 if (inst->module->sql_destroy_socket) {
57 (inst->module->sql_destroy_socket)(handle, inst->config);
63 static void *sql_conn_create(void *ctx)
66 rlm_sql_t *inst = ctx;
67 rlm_sql_handle_t *handle;
69 handle = talloc_zero(ctx, rlm_sql_handle_t);
72 * Handle requires a pointer to the SQL inst so the
73 * destructor has access to the module configuration.
76 talloc_set_destructor((void *) handle, sql_conn_destructor);
78 rcode = (inst->module->sql_init_socket)(handle, inst->config);
80 exec_trigger(NULL, inst->cs, "modules.sql.open", FALSE);
85 radlog(L_ERR, "rlm_sql (%s): Error opening connection: %s",
86 inst->config->xlat_name,
87 (inst->module->sql_error)(handle, inst->config));
89 exec_trigger(NULL, inst->cs, "modules.sql.fail", TRUE);
96 * @todo Calls to this should eventually go away.
98 static int sql_conn_delete(UNUSED void *ctx, void *conn)
100 return talloc_free(conn);
103 /*************************************************************************
105 * Function: sql_init_socketpool
107 * Purpose: Connect to the sql server, if possible
109 *************************************************************************/
110 int sql_init_socketpool(rlm_sql_t * inst)
112 inst->pool = fr_connection_pool_init(inst->cs, inst,
116 if (!inst->pool) return -1;
121 /*************************************************************************
123 * Function: sql_poolfree
125 * Purpose: Clean up and free sql pool
127 *************************************************************************/
128 void sql_poolfree(rlm_sql_t * inst)
130 fr_connection_pool_delete(inst->pool);
134 /*************************************************************************
136 * Function: sql_get_socket
138 * Purpose: Return a SQL handle from the connection pool
140 *************************************************************************/
141 rlm_sql_handle_t * sql_get_socket(rlm_sql_t * inst)
143 return fr_connection_get(inst->pool);
146 /*************************************************************************
148 * Function: sql_release_socket
150 * Purpose: Frees a SQL handle back to the connection pool
152 *************************************************************************/
153 int sql_release_socket(rlm_sql_t * inst, rlm_sql_handle_t * handle)
155 fr_connection_release(inst->pool, handle);
160 /*************************************************************************
162 * Function: sql_userparse
164 * Purpose: Read entries from the database and fill VALUE_PAIR structures
166 *************************************************************************/
167 int sql_userparse(VALUE_PAIR **head, rlm_sql_row_t row)
170 const char *ptr, *value;
171 char buf[MAX_STRING_LEN];
173 FR_TOKEN token, operator = T_EOL;
176 * Verify the 'Attribute' field
178 if (row[2] == NULL || row[2][0] == '\0') {
179 radlog(L_ERR, "rlm_sql: The 'Attribute' field is empty or NULL, skipping the entire row.");
184 * Verify the 'op' field
186 if (row[4] != NULL && row[4][0] != '\0') {
188 operator = gettoken(&ptr, buf, sizeof(buf));
189 if ((operator < T_OP_ADD) ||
190 (operator > T_OP_CMP_EQ)) {
191 radlog(L_ERR, "rlm_sql: Invalid operator \"%s\" for attribute %s", row[4], row[2]);
197 * Complain about empty or invalid 'op' field
199 operator = T_OP_CMP_EQ;
200 radlog(L_ERR, "rlm_sql: The 'op' field for attribute '%s = %s' is NULL, or non-existent.", row[2], row[3]);
201 radlog(L_ERR, "rlm_sql: You MUST FIX THIS if you want the configuration to behave as you expect.");
205 * The 'Value' field may be empty or NULL
209 * If we have a new-style quoted string, where the
210 * *entire* string is quoted, do xlat's.
212 if (row[3] != NULL &&
213 ((row[3][0] == '\'') || (row[3][0] == '`') || (row[3][0] == '"')) &&
214 (row[3][0] == row[3][strlen(row[3])-1])) {
216 token = gettoken(&value, buf, sizeof(buf));
219 * Take the unquoted string.
221 case T_SINGLE_QUOTED_STRING:
222 case T_DOUBLE_QUOTED_STRING:
227 * Mark the pair to be allocated later.
229 case T_BACK_QUOTED_STRING:
235 * Keep the original string.
246 vp = pairmake(row[2], NULL, operator);
248 radlog(L_ERR, "rlm_sql: Failed to create the pair: %s",
254 if (pairmark_xlat(vp, value) < 0) {
255 radlog(L_ERR, "rlm_sql: Error marking pair for xlat");
261 if (pairparsevalue(vp, value) < 0) {
262 radlog(L_ERR, "rlm_sql: Error parsing value");
270 * Add the pair into the packet
277 /*************************************************************************
279 * Function: rlm_sql_fetch_row
281 * Purpose: call the module's sql_fetch_row and implement re-connect
283 *************************************************************************/
284 int rlm_sql_fetch_row(rlm_sql_handle_t **handle, rlm_sql_t *inst)
288 if (!*handle || !(*handle)->conn) {
293 * We can't implement reconnect logic here, because the caller may require
294 * the original connection to free up queries or result sets associated with
297 ret = (inst->module->sql_fetch_row)(*handle, inst->config);
300 radlog(L_ERR, "rlm_sql (%s): Error fetching row: %s", inst->config->xlat_name,
301 (inst->module->sql_error)(*handle, inst->config));
307 /*************************************************************************
309 * Function: rlm_sql_query
311 * Purpose: call the module's sql_query and implement re-connect
313 *************************************************************************/
314 int rlm_sql_query(rlm_sql_handle_t **handle, rlm_sql_t *inst, char *query)
319 * If there's no query, return an error.
321 if (!query || !*query) {
325 if (!*handle || !(*handle)->conn) {
331 DEBUG("rlm_sql (%s): Executing query: '%s'",
332 inst->config->xlat_name, query);
334 ret = (inst->module->sql_query)(*handle, inst->config, query);
336 * Run through all available sockets until we exhaust all existing
337 * sockets in the pool and fail to establish a *new* connection.
339 if (ret == SQL_DOWN) {
341 *handle = fr_connection_reconnect(inst->pool, *handle);
342 if (!*handle) return SQL_DOWN;
349 "rlm_sql (%s): Database query error: '%s'",
350 inst->config->xlat_name,
351 (inst->module->sql_error)(*handle, inst->config));
358 /*************************************************************************
360 * Function: rlm_sql_select_query
362 * Purpose: call the module's sql_select_query and implement re-connect
364 *************************************************************************/
365 int rlm_sql_select_query(rlm_sql_handle_t **handle, rlm_sql_t *inst, char *query)
370 * If there's no query, return an error.
372 if (!query || !*query) {
376 if (!*handle || !(*handle)->conn) {
382 DEBUG("rlm_sql (%s): Executing query: '%s'",
383 inst->config->xlat_name, query);
385 ret = (inst->module->sql_select_query)(*handle, inst->config, query);
387 * Run through all available sockets until we exhaust all existing
388 * sockets in the pool and fail to establish a *new* connection.
390 if (ret == SQL_DOWN) {
392 *handle = fr_connection_reconnect(inst->pool, *handle);
393 if (!*handle) return SQL_DOWN;
400 "rlm_sql (%s): Database query error '%s'",
401 inst->config->xlat_name,
402 (inst->module->sql_error)(*handle, inst->config));
410 /*************************************************************************
412 * Function: sql_getvpdata
414 * Purpose: Get any group check or reply pairs
416 *************************************************************************/
417 int sql_getvpdata(rlm_sql_t * inst, rlm_sql_handle_t **handle, VALUE_PAIR **pair, char *query)
422 if (rlm_sql_select_query(handle, inst, query)) {
426 while (rlm_sql_fetch_row(handle, inst) == 0) {
427 row = (*handle)->row;
430 if (sql_userparse(pair, row) != 0) {
431 radlog(L_ERR, "rlm_sql (%s): Error getting data from database", inst->config->xlat_name);
433 (inst->module->sql_finish_select_query)(*handle, inst->config);
439 (inst->module->sql_finish_select_query)(*handle, inst->config);
445 * Log the query to a file.
447 void rlm_sql_query_log(rlm_sql_t *inst, REQUEST *request,
448 sql_acct_section_t *section, char *query)
451 const char *filename = NULL;
454 if (section) filename = section->logfile;
456 if (!filename) filename = inst->config->logfile;
458 if (!filename) return;
460 if (!radius_xlat(buffer, sizeof(buffer), filename, request, NULL, NULL)) {
461 radlog(L_ERR, "rlm_sql (%s): xlat failed.",
462 inst->config->xlat_name);
466 fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0666);
468 radlog(L_ERR, "rlm_sql (%s): Couldn't open logfile '%s': %s",
469 inst->config->xlat_name, buffer, strerror(errno));
473 if ((rad_lockfd(fd, MAX_QUERY_LEN) < 0) ||
474 (write(fd, query, strlen(query)) < 0) ||
475 (write(fd, ";\n", 2) < 0)) {
476 radlog(L_ERR, "rlm_sql (%s): Failed writing to logfile '%s': %s",
477 inst->config->xlat_name, buffer, strerror(errno));
479 close(fd); /* and release the lock */