static void *sql_conn_create(void *ctx)
{
int rcode;
- SQL_INST *inst = ctx;
- SQLSOCK *sqlsocket;
+ rlm_sql_t *inst = ctx;
+ rlm_sql_handle_t *handle;
- sqlsocket = rad_malloc(sizeof(*sqlsocket));
- memset(sqlsocket, 0, sizeof(*sqlsocket));
+ handle = rad_calloc(sizeof(*handle));
- rcode = (inst->module->sql_init_socket)(sqlsocket, inst->config);
+ rcode = (inst->module->sql_init_socket)(handle, inst->config);
if (rcode == 0) {
- exec_trigger(NULL, inst->cs, "modules.sql.open");
- return sqlsocket;
+ exec_trigger(NULL, inst->cs, "modules.sql.open", FALSE);
+ return handle;
}
- free(sqlsocket);
+ exec_trigger(NULL, inst->cs, "modules.sql.fail", TRUE);
+
+ free(handle);
return NULL;
}
static int sql_conn_delete(void *ctx, void *connection)
{
- SQL_INST *inst = ctx;
- SQLSOCK *sqlsocket = connection;
+ rlm_sql_t *inst = ctx;
+ rlm_sql_handle_t *handle = connection;
- exec_trigger(NULL, inst->cs, "modules.sql.close");
+ exec_trigger(NULL, inst->cs, "modules.sql.close", FALSE);
- if (sqlsocket->conn) {
- (inst->module->sql_close)(sqlsocket, inst->config);
+ if (handle->conn) {
+ (inst->module->sql_close)(handle, inst->config);
}
if (inst->module->sql_destroy_socket) {
- (inst->module->sql_destroy_socket)(sqlsocket, inst->config);
+ (inst->module->sql_destroy_socket)(handle, inst->config);
}
- free(sqlsocket);
+ free(handle);
return 0;
}
* Purpose: Connect to the sql server, if possible
*
*************************************************************************/
-int sql_init_socketpool(SQL_INST * inst)
+int sql_init_socketpool(rlm_sql_t * inst)
{
inst->pool = fr_connection_pool_init(inst->cs, inst,
sql_conn_create,
* Purpose: Clean up and free sql pool
*
*************************************************************************/
-void sql_poolfree(SQL_INST * inst)
+void sql_poolfree(rlm_sql_t * inst)
{
fr_connection_pool_delete(inst->pool);
}
*
* Function: sql_get_socket
*
- * Purpose: Return a SQL sqlsocket from the connection pool
+ * Purpose: Return a SQL handle from the connection pool
*
*************************************************************************/
-SQLSOCK * sql_get_socket(SQL_INST * inst)
+rlm_sql_handle_t * sql_get_socket(rlm_sql_t * inst)
{
return fr_connection_get(inst->pool);
}
*
* Function: sql_release_socket
*
- * Purpose: Frees a SQL sqlsocket back to the connection pool
+ * Purpose: Frees a SQL handle back to the connection pool
*
*************************************************************************/
-int sql_release_socket(SQL_INST * inst, SQLSOCK * sqlsocket)
+int sql_release_socket(rlm_sql_t * inst, rlm_sql_handle_t * handle)
{
- fr_connection_release(inst->pool, sqlsocket);
+ fr_connection_release(inst->pool, handle);
return 0;
}
* Purpose: Read entries from the database and fill VALUE_PAIR structures
*
*************************************************************************/
-int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row)
+int sql_userparse(VALUE_PAIR **head, rlm_sql_row_t row)
{
- VALUE_PAIR *pair;
+ VALUE_PAIR *vp;
const char *ptr, *value;
char buf[MAX_STRING_LEN];
char do_xlat = 0;
/*
* Create the pair
*/
- pair = pairmake(row[2], value, operator);
- if (pair == NULL) {
- radlog(L_ERR, "rlm_sql: Failed to create the pair: %s", fr_strerror());
+ vp = pairmake(row[2], NULL, operator);
+ if (!vp) {
+ radlog(L_ERR, "rlm_sql: Failed to create the pair: %s",
+ fr_strerror());
return -1;
}
+
if (do_xlat) {
- pair->flags.do_xlat = 1;
- strlcpy(pair->vp_strvalue, buf, sizeof(pair->vp_strvalue));
- pair->length = 0;
+ if (pairmark_xlat(vp, value) < 0) {
+ radlog(L_ERR, "rlm_sql: Error marking pair for xlat");
+
+ pairbasicfree(vp);
+ return -1;
+ }
}
/*
* Add the pair into the packet
*/
- pairadd(first_pair, pair);
+ pairadd(head, vp);
return 0;
}
* Purpose: call the module's sql_fetch_row and implement re-connect
*
*************************************************************************/
-int rlm_sql_fetch_row(SQLSOCK *sqlsocket, SQL_INST *inst)
+int rlm_sql_fetch_row(rlm_sql_handle_t **handle, rlm_sql_t *inst)
{
int ret;
- if (sqlsocket->conn) {
- ret = (inst->module->sql_fetch_row)(sqlsocket, inst->config);
- } else {
- ret = SQL_DOWN;
+ if (!*handle || !(*handle)->conn) {
+ return -1;
}
-
- if (ret == SQL_DOWN) {
- sqlsocket = fr_connection_reconnect(inst->pool, sqlsocket);
- if (!sqlsocket) return -1;
-
- /* retry the query on the newly connected socket */
- ret = (inst->module->sql_fetch_row)(sqlsocket, inst->config);
-
- if (ret) {
- radlog(L_ERR, "rlm_sql (%s): failed after re-connect",
- inst->config->xlat_name);
- return -1;
- }
+
+ /*
+ * We can't implement reconnect logic here, because the caller may require
+ * the original connection to free up queries or result sets associated with
+ * that connection.
+ */
+ ret = (inst->module->sql_fetch_row)(*handle, inst->config);
+
+ if (ret < 0) {
+ radlog(L_ERR, "rlm_sql (%s): Error fetching row: %s", inst->config->xlat_name,
+ (inst->module->sql_error)(*handle, inst->config));
}
return ret;
* Purpose: call the module's sql_query and implement re-connect
*
*************************************************************************/
-int rlm_sql_query(SQLSOCK *sqlsocket, SQL_INST *inst, char *query)
+int rlm_sql_query(rlm_sql_handle_t **handle, rlm_sql_t *inst, char *query)
{
int ret;
return -1;
}
- if (sqlsocket->conn) {
- ret = (inst->module->sql_query)(sqlsocket, inst->config, query);
- } else {
- ret = SQL_DOWN;
+ if (!*handle || !(*handle)->conn) {
+ ret = -1;
+ goto sql_down;
}
+
+ while (1) {
+ DEBUG("rlm_sql (%s): Executing query: '%s'",
+ inst->config->xlat_name, query);
- if (ret == SQL_DOWN) {
- sqlsocket = fr_connection_reconnect(inst->pool, sqlsocket);
- if (!sqlsocket) return -1;
-
- /* retry the query on the newly connected socket */
- ret = (inst->module->sql_query)(sqlsocket, inst->config, query);
-
- if (ret) {
- radlog(L_ERR, "rlm_sql (%s): failed after re-connect",
- inst->config->xlat_name);
- return -1;
+ ret = (inst->module->sql_query)(*handle, inst->config, query);
+ /*
+ * Run through all available sockets until we exhaust all existing
+ * sockets in the pool and fail to establish a *new* connection.
+ */
+ if (ret == SQL_DOWN) {
+ sql_down:
+ *handle = fr_connection_reconnect(inst->pool, *handle);
+ if (!*handle) return SQL_DOWN;
+
+ continue;
+ }
+
+ if (ret < 0) {
+ radlog(L_ERR,
+ "rlm_sql (%s): Database query error: '%s'",
+ inst->config->xlat_name,
+ (inst->module->sql_error)(*handle, inst->config));
}
+
+ return ret;
}
-
- return ret;
}
/*************************************************************************
* Purpose: call the module's sql_select_query and implement re-connect
*
*************************************************************************/
-int rlm_sql_select_query(SQLSOCK *sqlsocket, SQL_INST *inst, char *query)
+int rlm_sql_select_query(rlm_sql_handle_t **handle, rlm_sql_t *inst, char *query)
{
int ret;
return -1;
}
- if (sqlsocket->conn) {
- ret = (inst->module->sql_select_query)(sqlsocket, inst->config,
- query);
- } else {
- ret = SQL_DOWN;
+ if (!*handle || !(*handle)->conn) {
+ ret = -1;
+ goto sql_down;
}
+
+ while (1) {
+ DEBUG("rlm_sql (%s): Executing query: '%s'",
+ inst->config->xlat_name, query);
- if (ret == SQL_DOWN) {
- sqlsocket = fr_connection_reconnect(inst->pool, sqlsocket);
- if (!sqlsocket) return -1;
-
- /* retry the query on the newly connected socket */
- ret = (inst->module->sql_select_query)(sqlsocket, inst->config, query);
-
- if (ret) {
- radlog(L_ERR, "rlm_sql (%s): failed after re-connect",
- inst->config->xlat_name);
- return -1;
+ ret = (inst->module->sql_select_query)(*handle, inst->config, query);
+ /*
+ * Run through all available sockets until we exhaust all existing
+ * sockets in the pool and fail to establish a *new* connection.
+ */
+ if (ret == SQL_DOWN) {
+ sql_down:
+ *handle = fr_connection_reconnect(inst->pool, *handle);
+ if (!*handle) return SQL_DOWN;
+
+ continue;
+ }
+
+ if (ret < 0) {
+ radlog(L_ERR,
+ "rlm_sql (%s): Database query error '%s'",
+ inst->config->xlat_name,
+ (inst->module->sql_error)(*handle, inst->config));
}
+
+ return ret;
}
-
- return ret;
}
* Purpose: Get any group check or reply pairs
*
*************************************************************************/
-int sql_getvpdata(SQL_INST * inst, SQLSOCK * sqlsocket, VALUE_PAIR **pair, char *query)
+int sql_getvpdata(rlm_sql_t * inst, rlm_sql_handle_t **handle, VALUE_PAIR **pair, char *query)
{
- SQL_ROW row;
+ rlm_sql_row_t row;
int rows = 0;
- if (rlm_sql_select_query(sqlsocket, inst, query)) {
- radlog(L_ERR, "rlm_sql_getvpdata: database query error");
+ if (rlm_sql_select_query(handle, inst, query))
return -1;
- }
- while (rlm_sql_fetch_row(sqlsocket, inst)==0) {
- row = sqlsocket->row;
+
+ while (rlm_sql_fetch_row(handle, inst) == 0) {
+ row = (*handle)->row;
if (!row)
break;
if (sql_userparse(pair, row) != 0) {
- radlog(L_ERR | L_CONS, "rlm_sql (%s): Error getting data from database", inst->config->xlat_name);
- (inst->module->sql_finish_select_query)(sqlsocket, inst->config);
+ radlog(L_ERR, "rlm_sql (%s): Error getting data from database", inst->config->xlat_name);
+
+ (inst->module->sql_finish_select_query)(*handle, inst->config);
+
return -1;
}
rows++;
}
- (inst->module->sql_finish_select_query)(sqlsocket, inst->config);
+ (inst->module->sql_finish_select_query)(*handle, inst->config);
return rows;
}
-void query_log(REQUEST *request, SQL_INST *inst, char *querystr)
+/*
+ * Log the query to a file.
+ */
+void rlm_sql_query_log(rlm_sql_t *inst, REQUEST *request,
+ sql_acct_section_t *section, char *query)
{
- FILE *sqlfile = NULL;
+ int fd;
+ const char *filename = NULL;
+ char buffer[8192];
- if (inst->config->sqltrace) {
- char buffer[8192];
+ if (section) filename = section->logfile;
- if (!radius_xlat(buffer, sizeof(buffer),
- inst->config->tracefile, request, NULL)) {
- radlog(L_ERR, "rlm_sql (%s): xlat failed.",
- inst->config->xlat_name);
- return;
- }
+ if (!filename) filename = inst->config->logfile;
- if ((sqlfile = fopen(buffer, "a")) == (FILE *) NULL) {
- radlog(L_ERR, "rlm_sql (%s): Couldn't open file %s",
- inst->config->xlat_name,
- buffer);
- } else {
- int fd = fileno(sqlfile);
-
- rad_lockfd(fd, MAX_QUERY_LEN);
- fputs(querystr, sqlfile);
- fputs(";\n", sqlfile);
- fclose(sqlfile); /* and release the lock */
- }
+ if (!filename) return;
+
+ if (!radius_xlat(buffer, sizeof(buffer), filename, request, NULL, NULL)) {
+ radlog(L_ERR, "rlm_sql (%s): xlat failed.",
+ inst->config->xlat_name);
+ return;
+ }
+
+ fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0666);
+ if (fd < 0) {
+ radlog(L_ERR, "rlm_sql (%s): Couldn't open logfile '%s': %s",
+ inst->config->xlat_name, buffer, strerror(errno));
+ return;
+ }
+
+ if ((rad_lockfd(fd, MAX_QUERY_LEN) < 0) ||
+ (write(fd, query, strlen(query)) < 0) ||
+ (write(fd, ";\n", 2) < 0)) {
+ radlog(L_ERR, "rlm_sql (%s): Failed writing to logfile '%s': %s",
+ inst->config->xlat_name, buffer, strerror(errno));
}
+ close(fd); /* and release the lock */
}