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>
43 static void *sql_conn_create(void *ctx)
49 sqlsocket = rad_calloc(sizeof(*sqlsocket));
51 rcode = (inst->module->sql_init_socket)(sqlsocket, inst->config);
53 exec_trigger(NULL, inst->cs, "modules.sql.open", FALSE);
57 exec_trigger(NULL, inst->cs, "modules.sql.fail", TRUE);
64 static int sql_conn_delete(void *ctx, void *connection)
67 SQLSOCK *sqlsocket = connection;
69 exec_trigger(NULL, inst->cs, "modules.sql.close", FALSE);
71 if (sqlsocket->conn) {
72 (inst->module->sql_close)(sqlsocket, inst->config);
74 if (inst->module->sql_destroy_socket) {
75 (inst->module->sql_destroy_socket)(sqlsocket, inst->config);
83 /*************************************************************************
85 * Function: sql_init_socketpool
87 * Purpose: Connect to the sql server, if possible
89 *************************************************************************/
90 int sql_init_socketpool(SQL_INST * inst)
92 inst->pool = fr_connection_pool_init(inst->cs, inst,
96 if (!inst->pool) return -1;
101 /*************************************************************************
103 * Function: sql_poolfree
105 * Purpose: Clean up and free sql pool
107 *************************************************************************/
108 void sql_poolfree(SQL_INST * inst)
110 fr_connection_pool_delete(inst->pool);
114 /*************************************************************************
116 * Function: sql_get_socket
118 * Purpose: Return a SQL sqlsocket from the connection pool
120 *************************************************************************/
121 SQLSOCK * sql_get_socket(SQL_INST * inst)
123 return fr_connection_get(inst->pool);
126 /*************************************************************************
128 * Function: sql_release_socket
130 * Purpose: Frees a SQL sqlsocket back to the connection pool
132 *************************************************************************/
133 int sql_release_socket(SQL_INST * inst, SQLSOCK * sqlsocket)
135 fr_connection_release(inst->pool, sqlsocket);
140 /*************************************************************************
142 * Function: sql_userparse
144 * Purpose: Read entries from the database and fill VALUE_PAIR structures
146 *************************************************************************/
147 int sql_userparse(VALUE_PAIR **head, SQL_ROW row)
150 const char *ptr, *value;
151 char buf[MAX_STRING_LEN];
153 FR_TOKEN token, operator = T_EOL;
156 * Verify the 'Attribute' field
158 if (row[2] == NULL || row[2][0] == '\0') {
159 radlog(L_ERR, "rlm_sql: The 'Attribute' field is empty or NULL, skipping the entire row.");
164 * Verify the 'op' field
166 if (row[4] != NULL && row[4][0] != '\0') {
168 operator = gettoken(&ptr, buf, sizeof(buf));
169 if ((operator < T_OP_ADD) ||
170 (operator > T_OP_CMP_EQ)) {
171 radlog(L_ERR, "rlm_sql: Invalid operator \"%s\" for attribute %s", row[4], row[2]);
177 * Complain about empty or invalid 'op' field
179 operator = T_OP_CMP_EQ;
180 radlog(L_ERR, "rlm_sql: The 'op' field for attribute '%s = %s' is NULL, or non-existent.", row[2], row[3]);
181 radlog(L_ERR, "rlm_sql: You MUST FIX THIS if you want the configuration to behave as you expect.");
185 * The 'Value' field may be empty or NULL
189 * If we have a new-style quoted string, where the
190 * *entire* string is quoted, do xlat's.
192 if (row[3] != NULL &&
193 ((row[3][0] == '\'') || (row[3][0] == '`') || (row[3][0] == '"')) &&
194 (row[3][0] == row[3][strlen(row[3])-1])) {
196 token = gettoken(&value, buf, sizeof(buf));
199 * Take the unquoted string.
201 case T_SINGLE_QUOTED_STRING:
202 case T_DOUBLE_QUOTED_STRING:
207 * Mark the pair to be allocated later.
209 case T_BACK_QUOTED_STRING:
215 * Keep the original string.
226 vp = pairmake(row[2], NULL, operator);
228 radlog(L_ERR, "rlm_sql: Failed to create the pair: %s",
234 pairmark_xlat(vp, value);
238 * Add the pair into the packet
245 /*************************************************************************
247 * Function: rlm_sql_fetch_row
249 * Purpose: call the module's sql_fetch_row and implement re-connect
251 *************************************************************************/
252 int rlm_sql_fetch_row(SQLSOCK **sqlsocket, SQL_INST *inst)
256 if (!*sqlsocket || !(*sqlsocket)->conn) {
261 * We can't implement reconnect logic here, because the caller may require
262 * the original connection to free up queries or result sets associated with
265 ret = (inst->module->sql_fetch_row)(*sqlsocket, inst->config);
268 radlog(L_ERR, "rlm_sql (%s): Error fetching row: %s", inst->config->xlat_name,
269 (inst->module->sql_error)(*sqlsocket, inst->config));
275 /*************************************************************************
277 * Function: rlm_sql_query
279 * Purpose: call the module's sql_query and implement re-connect
281 *************************************************************************/
282 int rlm_sql_query(SQLSOCK **sqlsocket, SQL_INST *inst, char *query)
287 * If there's no query, return an error.
289 if (!query || !*query) {
293 if (!*sqlsocket || !(*sqlsocket)->conn) {
299 DEBUG("rlm_sql (%s): Executing query: '%s'",
300 inst->config->xlat_name, query);
302 ret = (inst->module->sql_query)(*sqlsocket, inst->config, query);
304 * Run through all available sockets until we exhaust all existing
305 * sockets in the pool and fail to establish a *new* connection.
307 if (ret == SQL_DOWN) {
309 *sqlsocket = fr_connection_reconnect(inst->pool, *sqlsocket);
310 if (!*sqlsocket) return SQL_DOWN;
317 "rlm_sql (%s): Database query error: '%s'",
318 inst->config->xlat_name,
319 (inst->module->sql_error)(*sqlsocket, inst->config));
326 /*************************************************************************
328 * Function: rlm_sql_select_query
330 * Purpose: call the module's sql_select_query and implement re-connect
332 *************************************************************************/
333 int rlm_sql_select_query(SQLSOCK **sqlsocket, SQL_INST *inst, char *query)
338 * If there's no query, return an error.
340 if (!query || !*query) {
344 if (!*sqlsocket || !(*sqlsocket)->conn) {
350 DEBUG("rlm_sql (%s): Executing query: '%s'",
351 inst->config->xlat_name, query);
353 ret = (inst->module->sql_select_query)(*sqlsocket, inst->config, query);
355 * Run through all available sockets until we exhaust all existing
356 * sockets in the pool and fail to establish a *new* connection.
358 if (ret == SQL_DOWN) {
360 *sqlsocket = fr_connection_reconnect(inst->pool, *sqlsocket);
361 if (!*sqlsocket) return SQL_DOWN;
368 "rlm_sql (%s): Database query error '%s'",
369 inst->config->xlat_name,
370 (inst->module->sql_error)(*sqlsocket, inst->config));
378 /*************************************************************************
380 * Function: sql_getvpdata
382 * Purpose: Get any group check or reply pairs
384 *************************************************************************/
385 int sql_getvpdata(SQL_INST * inst, SQLSOCK **sqlsocket, VALUE_PAIR **pair, char *query)
390 if (rlm_sql_select_query(sqlsocket, inst, query))
393 while (rlm_sql_fetch_row(sqlsocket, inst) == 0) {
394 row = (*sqlsocket)->row;
397 if (sql_userparse(pair, row) != 0) {
398 radlog(L_ERR | L_CONS, "rlm_sql (%s): Error getting data from database", inst->config->xlat_name);
400 (inst->module->sql_finish_select_query)(*sqlsocket, inst->config);
406 (inst->module->sql_finish_select_query)(*sqlsocket, inst->config);
412 * Log the query to a file.
414 void rlm_sql_query_log(SQL_INST *inst, REQUEST *request,
415 sql_acct_section_t *section, char *query)
418 const char *filename = NULL;
421 if (section) filename = section->logfile;
423 if (!filename) filename = inst->config->logfile;
425 if (!filename) return;
427 if (!radius_xlat(buffer, sizeof(buffer), filename, request, NULL, NULL)) {
428 radlog(L_ERR, "rlm_sql (%s): xlat failed.",
429 inst->config->xlat_name);
433 fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0666);
435 radlog(L_ERR, "rlm_sql (%s): Couldn't open logfile '%s': %s",
436 inst->config->xlat_name, buffer, strerror(errno));
440 rad_lockfd(fd, MAX_QUERY_LEN);
441 if ((write(fd, query, strlen(query)) < 0) ||
442 (write(fd, ";\n", 2) < 0)) {
443 radlog(L_ERR, "rlm_sql (%s): Failed writing to logfile '%s': %s",
444 inst->config->xlat_name, buffer, strerror(errno));
446 close(fd); /* and release the lock */