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_malloc(sizeof(*sqlsocket));
50 memset(sqlsocket, 0, sizeof(*sqlsocket));
52 rcode = (inst->module->sql_init_socket)(sqlsocket, inst->config);
54 exec_trigger(NULL, inst->cs, "modules.sql.open", FALSE);
58 exec_trigger(NULL, inst->cs, "modules.sql.fail", TRUE);
65 static int sql_conn_delete(void *ctx, void *connection)
68 SQLSOCK *sqlsocket = connection;
70 exec_trigger(NULL, inst->cs, "modules.sql.close", FALSE);
72 if (sqlsocket->conn) {
73 (inst->module->sql_close)(sqlsocket, inst->config);
75 if (inst->module->sql_destroy_socket) {
76 (inst->module->sql_destroy_socket)(sqlsocket, inst->config);
84 /*************************************************************************
86 * Function: sql_init_socketpool
88 * Purpose: Connect to the sql server, if possible
90 *************************************************************************/
91 int sql_init_socketpool(SQL_INST * inst)
93 inst->pool = fr_connection_pool_init(inst->cs, inst,
97 if (!inst->pool) return -1;
102 /*************************************************************************
104 * Function: sql_poolfree
106 * Purpose: Clean up and free sql pool
108 *************************************************************************/
109 void sql_poolfree(SQL_INST * inst)
111 fr_connection_pool_delete(inst->pool);
115 /*************************************************************************
117 * Function: sql_get_socket
119 * Purpose: Return a SQL sqlsocket from the connection pool
121 *************************************************************************/
122 SQLSOCK * sql_get_socket(SQL_INST * inst)
124 return fr_connection_get(inst->pool);
127 /*************************************************************************
129 * Function: sql_release_socket
131 * Purpose: Frees a SQL sqlsocket back to the connection pool
133 *************************************************************************/
134 int sql_release_socket(SQL_INST * inst, SQLSOCK * sqlsocket)
136 fr_connection_release(inst->pool, sqlsocket);
141 /*************************************************************************
143 * Function: sql_userparse
145 * Purpose: Read entries from the database and fill VALUE_PAIR structures
147 *************************************************************************/
148 int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row)
151 const char *ptr, *value;
152 char buf[MAX_STRING_LEN];
154 FR_TOKEN token, operator = T_EOL;
157 * Verify the 'Attribute' field
159 if (row[2] == NULL || row[2][0] == '\0') {
160 radlog(L_ERR, "rlm_sql: The 'Attribute' field is empty or NULL, skipping the entire row.");
165 * Verify the 'op' field
167 if (row[4] != NULL && row[4][0] != '\0') {
169 operator = gettoken(&ptr, buf, sizeof(buf));
170 if ((operator < T_OP_ADD) ||
171 (operator > T_OP_CMP_EQ)) {
172 radlog(L_ERR, "rlm_sql: Invalid operator \"%s\" for attribute %s", row[4], row[2]);
178 * Complain about empty or invalid 'op' field
180 operator = T_OP_CMP_EQ;
181 radlog(L_ERR, "rlm_sql: The 'op' field for attribute '%s = %s' is NULL, or non-existent.", row[2], row[3]);
182 radlog(L_ERR, "rlm_sql: You MUST FIX THIS if you want the configuration to behave as you expect.");
186 * The 'Value' field may be empty or NULL
190 * If we have a new-style quoted string, where the
191 * *entire* string is quoted, do xlat's.
193 if (row[3] != NULL &&
194 ((row[3][0] == '\'') || (row[3][0] == '`') || (row[3][0] == '"')) &&
195 (row[3][0] == row[3][strlen(row[3])-1])) {
197 token = gettoken(&value, buf, sizeof(buf));
200 * Take the unquoted string.
202 case T_SINGLE_QUOTED_STRING:
203 case T_DOUBLE_QUOTED_STRING:
208 * Mark the pair to be allocated later.
210 case T_BACK_QUOTED_STRING:
216 * Keep the original string.
228 pair = pairmake_xlat(row[2], value, operator);
230 pair = pairmake(row[2], value, operator);
233 radlog(L_ERR, "rlm_sql: Failed to create the pair: %s", fr_strerror());
238 * Add the pair into the packet
240 pairadd(first_pair, pair);
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 rlm_sql_config_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) || (write(fd, ";\n", 2) < 0)))
442 radlog(L_ERR, "rlm_sql (%s): Failed writing to logfile '%s': %s",
443 inst->config->xlat_name, buffer, strerror(errno));
445 close(fd); /* and release the lock */