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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Copyright 2001 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>
28 #include <sys/types.h>
29 #include <sys/socket.h>
34 #include <netinet/in.h>
57 * Connect to a server. If error, set this socket's state to be
58 * "sockunconnected" and set a grace period, during which we won't try
59 * connecting again (to prevent unduly lagging the server and being
60 * impolite to a DB server that may be having other issues). If
61 * successful in connecting, set state to sockconnected.
64 static int connect_single_socket(SQLSOCK *sqlsocket, SQL_INST *inst)
67 radlog(L_DBG, "rlm_sql (%s): Attempting to connect %s #%d",
68 inst->config->xlat_name, inst->module->name, sqlsocket->id);
70 rcode = (inst->module->sql_init_socket)(sqlsocket, inst->config);
72 radlog(L_DBG, "rlm_sql (%s): Connected new DB handle, #%d",
73 inst->config->xlat_name, sqlsocket->id);
74 sqlsocket->state = sockconnected;
81 radlog(L_CONS | L_ERR, "rlm_sql (%s): Failed to connect DB handle #%d", inst->config->xlat_name, sqlsocket->id);
82 inst->connect_after = time(NULL) + inst->config->connect_failure_retry_delay;
83 sqlsocket->state = sockunconnected;
88 /*************************************************************************
90 * Function: sql_init_socketpool
92 * Purpose: Connect to the sql server, if possible
94 *************************************************************************/
95 int sql_init_socketpool(SQL_INST * inst)
101 inst->connect_after = 0;
102 inst->sqlpool = NULL;
104 for (i = 0; i < inst->config->num_sql_socks; i++) {
105 radlog(L_DBG, "rlm_sql (%s): starting %d",
106 inst->config->xlat_name, i);
108 sqlsocket = rad_malloc(sizeof(SQLSOCK));
109 if (sqlsocket == NULL) {
112 sqlsocket->conn = NULL;
114 sqlsocket->state = sockunconnected;
117 rcode = pthread_mutex_init(&sqlsocket->mutex,NULL);
119 radlog(L_ERR, "rlm_sql: Failed to init lock: %s",
125 if (time(NULL) > inst->connect_after) {
127 * This sets the sqlsocket->state, and
128 * possibly also inst->connect_after
130 if (connect_single_socket(sqlsocket, inst) == 0) {
135 /* Add this socket to the list of sockets */
136 sqlsocket->next = inst->sqlpool;
137 inst->sqlpool = sqlsocket;
139 inst->last_used = NULL;
142 radlog(L_DBG, "rlm_sql (%s): Failed to connect to any SQL server.",
143 inst->config->xlat_name);
149 /*************************************************************************
151 * Function: sql_poolfree
153 * Purpose: Clean up and free sql pool
155 *************************************************************************/
156 void sql_poolfree(SQL_INST * inst)
161 for (cur = inst->sqlpool; cur; cur = next) {
163 sql_close_socket(inst, cur);
168 /*************************************************************************
170 * Function: sql_close_socket
172 * Purpose: Close and free a sql sqlsocket
174 *************************************************************************/
175 int sql_close_socket(SQL_INST *inst, SQLSOCK * sqlsocket)
177 radlog(L_DBG, "rlm_sql (%s): Closing sqlsocket %d",
178 inst->config->xlat_name, sqlsocket->id);
179 if (sqlsocket->state == sockconnected)
180 (inst->module->sql_close)(sqlsocket, inst->config);
182 pthread_mutex_destroy(&sqlsocket->mutex);
189 /*************************************************************************
191 * Function: sql_get_socket
193 * Purpose: Return a SQL sqlsocket from the connection pool
195 *************************************************************************/
196 SQLSOCK * sql_get_socket(SQL_INST * inst)
198 SQLSOCK *cur, *start;
199 int tried_to_connect = 0;
203 * Start at the last place we left off.
205 start = inst->last_used;
206 if (!start) start = inst->sqlpool;
213 * If this socket is in use by another thread,
214 * skip it, and try another socket.
216 * If it isn't used, then grab it ourselves.
218 if (pthread_mutex_trylock(&cur->mutex) != 0) {
220 } /* else we now have the lock */
224 * If we happen upon an unconnected socket, and
225 * this instance's grace period on
226 * (re)connecting has expired, then try to
227 * connect it. This should be really rare.
229 if ((cur->state == sockunconnected) && (time(NULL) > inst->connect_after)) {
230 radlog(L_INFO, "rlm_sql (%s): Trying to (re)connect unconnected handle %d..", inst->config->xlat_name, cur->id);
232 connect_single_socket(cur, inst);
235 /* if we still aren't connected, ignore this handle */
236 if (cur->state == sockunconnected) {
237 radlog(L_DBG, "rlm_sql (%s): Ignoring unconnected handle %d..", inst->config->xlat_name, cur->id);
240 pthread_mutex_unlock(&cur->mutex);
245 /* should be connected, grab it */
246 radlog(L_DBG, "rlm_sql (%s): Reserving sql socket id: %d", inst->config->xlat_name, cur->id);
248 if (unconnected != 0 || tried_to_connect != 0) {
249 radlog(L_INFO, "rlm_sql (%s): got socket %d after skipping %d unconnected handles, tried to reconnect %d though", inst->config->xlat_name, cur->id, unconnected, tried_to_connect);
253 * The socket is returned in the locked
256 * We also remember where we left off,
257 * so that the next search can start from
260 * Note that multiple threads MAY over-write
261 * the 'inst->last_used' variable. This is OK,
262 * as it's a pointer only used for reading.
264 inst->last_used = cur->next;
267 /* move along the list */
272 * Because we didnt start at the start, once we
273 * hit the end of the linklist, we should go
274 * back to the beginning and work toward the
282 * If we're at the socket we started
289 /* We get here if every DB handle is unconnected and unconnectABLE */
290 radlog(L_INFO, "rlm_sql (%s): There are no DB handles to use! skipped %d, tried to connect %d", inst->config->xlat_name, unconnected, tried_to_connect);
294 /*************************************************************************
296 * Function: sql_release_socket
298 * Purpose: Frees a SQL sqlsocket back to the connection pool
300 *************************************************************************/
301 int sql_release_socket(SQL_INST * inst, SQLSOCK * sqlsocket)
304 pthread_mutex_unlock(&sqlsocket->mutex);
307 radlog(L_DBG, "rlm_sql (%s): Released sql socket id: %d",
308 inst->config->xlat_name, sqlsocket->id);
314 /*************************************************************************
316 * Function: sql_userparse
318 * Purpose: Read entries from the database and fill VALUE_PAIR structures
320 *************************************************************************/
321 int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row, int querymode)
324 VALUE_PAIR *pair, *check;
327 int pairmode = T_EOL;
329 if ((attr = dict_attrbyname(row[2])) == (DICT_ATTR *) NULL) {
330 radlog(L_ERR | L_CONS, "rlm_sql: unknown attribute %s",
335 if (row[4] != NULL && strlen(row[4]) > 0) {
337 pairmode = gettoken(&ptr, buf, sizeof(buf));
340 * 'op' fields of NULL are a plague, and a bane on the
341 * existence of mankind.
343 radlog(L_ERR, "rlm_sql: The 'op' field for attribute '%s = %s' is NULL, or non-existent.", row[2], row[3]);
344 radlog(L_ERR, "rlm_sql: You MUST FIX THIS if you want the configuration to behave as you expect.");
346 if (pairmode <= T_EOL) pairmode = T_OP_CMP_EQ;
349 * If attribute is already there, skip it because we checked usercheck first
350 * and we want user settings to over ride group settings
352 if (pairmode != T_OP_ADD && (check = pairfind(*first_pair, attr->attr)) != NULL &&
354 attr->type != PW_TYPE_ABINARY &&
356 querymode == PW_VP_GROUPDATA)
359 pair = pairmake(row[2], row[3], pairmode);
360 pairadd(first_pair, pair);
366 /*************************************************************************
368 * Function: rlm_sql_fetch_row
370 * Purpose: call the module's sql_fetch_row and implement re-connect
372 *************************************************************************/
373 int rlm_sql_fetch_row(SQLSOCK *sqlsocket, SQL_INST *inst)
377 ret = (inst->module->sql_fetch_row)(sqlsocket, inst->config);
379 if (ret == SQL_DOWN) {
380 /* close the socket that failed */
381 (inst->module->sql_close)(sqlsocket, inst->config);
383 /* reconnect the socket */
384 if (connect_single_socket(sqlsocket, inst) < 0) {
385 radlog(L_ERR, "rlm_sql (%s): reconnect failed, database down?", inst->config->xlat_name);
389 /* retry the query on the newly connected socket */
390 ret = (inst->module->sql_fetch_row)(sqlsocket, inst->config);
393 radlog(L_ERR, "rlm_sql (%s): failed after re-connect",
394 inst->config->xlat_name);
402 /*************************************************************************
404 * Function: rlm_sql_query
406 * Purpose: call the module's sql_query and implement re-connect
408 *************************************************************************/
409 int rlm_sql_query(SQLSOCK *sqlsocket, SQL_INST *inst, char *query)
414 * If there's no query, return an error.
416 if (!query || !*query) {
420 ret = (inst->module->sql_query)(sqlsocket, inst->config, query);
422 if (ret == SQL_DOWN) {
423 /* close the socket that failed */
424 (inst->module->sql_close)(sqlsocket, inst->config);
426 /* reconnect the socket */
427 if (connect_single_socket(sqlsocket, inst) < 0) {
428 radlog(L_ERR, "rlm_sql (%s): reconnect failed, database down?", inst->config->xlat_name);
432 /* retry the query on the newly connected socket */
433 ret = (inst->module->sql_query)(sqlsocket, inst->config, query);
436 radlog(L_ERR, "rlm_sql (%s): failed after re-connect",
437 inst->config->xlat_name);
445 /*************************************************************************
447 * Function: rlm_sql_select_query
449 * Purpose: call the module's sql_select_query and implement re-connect
451 *************************************************************************/
452 int rlm_sql_select_query(SQLSOCK *sqlsocket, SQL_INST *inst, char *query)
457 * If there's no query, return an error.
459 if (!query || !*query) {
463 ret = (inst->module->sql_select_query)(sqlsocket, inst->config, query);
465 if (ret == SQL_DOWN) {
466 /* close the socket that failed */
467 (inst->module->sql_close)(sqlsocket, inst->config);
469 /* reconnect the socket */
470 if (connect_single_socket(sqlsocket, inst) < 0) {
471 radlog(L_ERR, "rlm_sql (%s): reconnect failed, database down?", inst->config->xlat_name);
475 /* retry the query on the newly connected socket */
476 ret = (inst->module->sql_select_query)(sqlsocket, inst->config, query);
479 radlog(L_ERR, "rlm_sql (%s): failed after re-connect",
480 inst->config->xlat_name);
489 /*************************************************************************
491 * Function: sql_getvpdata
493 * Purpose: Get any group check or reply pairs
495 *************************************************************************/
496 int sql_getvpdata(SQL_INST * inst, SQLSOCK * sqlsocket, VALUE_PAIR **pair, char *query, int mode)
502 * If there's no query, return an error.
504 if (!query || !*query) {
508 if (rlm_sql_select_query(sqlsocket, inst, query)) {
509 radlog(L_ERR, "rlm_sql_getvpdata: database query error");
512 while (rlm_sql_fetch_row(sqlsocket, inst)==0) {
513 row = sqlsocket->row;
516 if (sql_userparse(pair, row, mode) != 0) {
517 radlog(L_ERR | L_CONS, "rlm_sql (%s): Error getting data from database", inst->config->xlat_name);
518 (inst->module->sql_finish_select_query)(sqlsocket, inst->config);
523 (inst->module->sql_finish_select_query)(sqlsocket, inst->config);
528 void query_log(REQUEST *request, SQL_INST *inst, char *querystr)
530 FILE *sqlfile = NULL;
532 if (inst->config->sqltrace) {
535 if (!radius_xlat(buffer, sizeof(buffer),
536 inst->config->tracefile, request, NULL)) {
537 radlog(L_ERR, "rlm_sql (%s): xlat failed.",
538 inst->config->xlat_name);
542 if ((sqlfile = fopen(buffer, "a")) == (FILE *) NULL) {
543 radlog(L_ERR, "rlm_sql (%s): Couldn't open file %s",
544 inst->config->xlat_name,
547 int fd = fileno(sqlfile);
549 rad_lockfd(fd, MAX_QUERY_LEN);
550 fputs(querystr, sqlfile);
551 fputs(";\n", sqlfile);
552 fclose(sqlfile); /* and release the lock */