-/***************************************************************************
-* iODBC support for FreeRadius
-* www.iodbc.org - iODBC info
-* Jeff Carneal - Author of this module driver <jeff@apex.net>
-***************************************************************************/
+/*
+ * sql_iodbc.c iODBC support for FreeRadius
+ *
+ * Version: $Id$
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ *
+ * Copyright 2000 The FreeRADIUS server project
+ * Copyright 2000 Jeff Carneal <jeff@apex.net>
+ */
+
+#include <freeradius-devel/autoconf.h>
+
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
-#include "radiusd.h"
-#include "rlm_sql.h"
-
#include <isql.h>
#include <isqlext.h>
#include <sqltypes.h>
+
+#include <freeradius-devel/radiusd.h>
#include "rlm_sql.h"
typedef struct rlm_sql_iodbc_sock {
HSTMT stmt_handle;
int id;
SQL_ROW row;
-
+
struct sql_socket *next;
void *conn;
static int sql_init_socket(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
rlm_sql_iodbc_sock *iodbc_sock;
+ SQLRETURN rcode;
- sqlsocket->conn = (rlm_sql_iodbc_sock *)rad_malloc(sizeof(rlm_sql_iodbc_sock));
-
+ if (!sqlsocket->conn) {
+ sqlsocket->conn = (rlm_sql_iodbc_sock *)rad_malloc(sizeof(rlm_sql_iodbc_sock));
+ if (!sqlsocket->conn) {
+ return -1;
+ }
+ }
iodbc_sock = sqlsocket->conn;
+ memset(iodbc_sock, 0, sizeof(*iodbc_sock));
- if(SQLAllocEnv(&iodbc_sock->env_handle) != SQL_SUCCESS) {
- radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocEnv failed: %s",
+ rcode = SQLAllocEnv(&iodbc_sock->env_handle);
+ if (!SQL_SUCCEEDED(rcode)) {
+ radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocEnv failed: %s",
sql_error(sqlsocket, config));
return -1;
}
- if(SQLAllocConnect(iodbc_sock->env_handle, &iodbc_sock->dbc_handle) != SQL_SUCCESS) {
- radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocConnect failed: %s",
+ rcode = SQLAllocConnect(iodbc_sock->env_handle,
+ &iodbc_sock->dbc_handle);
+ if (!SQL_SUCCEEDED(rcode)) {
+ radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocConnect failed: %s",
sql_error(sqlsocket, config));
return -1;
}
- if (SQLConnect(iodbc_sock->dbc_handle, config->sql_db, SQL_NTS,
- config->sql_login, SQL_NTS, config->sql_password,
- SQL_NTS) != SQL_SUCCESS) {
- radlog(L_CONS|L_ERR, "sql_create_socket: SQLConnectfailed: %s",
+ rcode = SQLConnect(iodbc_sock->dbc_handle, config->sql_db,
+ SQL_NTS, config->sql_login, SQL_NTS,
+ config->sql_password, SQL_NTS);
+ if (!SQL_SUCCEEDED(rcode)) {
+ radlog(L_CONS|L_ERR, "sql_create_socket: SQLConnectfailed: %s",
sql_error(sqlsocket, config));
return -1;
}
* Purpose: Free socket and private connection data
*
*************************************************************************/
-static int sql_destroy_socket(SQLSOCK *sqlsocket, SQL_CONFIG *config)
+static int sql_destroy_socket(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config)
{
free(sqlsocket->conn);
sqlsocket->conn = NULL;
static int sql_query(SQLSOCK *sqlsocket, SQL_CONFIG *config, char *querystr) {
rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
+ SQLRETURN rcode;
- if(SQLAllocStmt(iodbc_sock->dbc_handle, &iodbc_sock->stmt_handle) != SQL_SUCCESS) {
- radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocStmt failed: %s",
+ rcode = SQLAllocStmt(iodbc_sock->dbc_handle,
+ &iodbc_sock->stmt_handle);
+ if (!SQL_SUCCEEDED(rcode)) {
+ radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocStmt failed: %s",
sql_error(sqlsocket, config));
return -1;
}
return -1;
}
- if (SQLExecDirect(iodbc_sock->stmt_handle, querystr, SQL_NTS) != SQL_SUCCESS) {
+ rcode = SQLExecDirect(iodbc_sock->stmt_handle, querystr, SQL_NTS);
+ if (!SQL_SUCCEEDED(rcode)) {
radlog(L_CONS|L_ERR, "sql_query: failed: %s",
sql_error(sqlsocket, config));
return -1;
rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
if(sql_query(sqlsocket, config, querystr) < 0) {
- return -1;
+ return -1;
}
numfields = sql_num_fields(sqlsocket, config);
row = (char **) rad_malloc(sizeof(char *) * (numfields+1));
- memset(row, 0, (sizeof(char *) * (numfields)));
+ memset(row, 0, (sizeof(char *) * (numfields)));
row[numfields] = NULL;
for(i=1; i<=numfields; i++) {
NULL, 0, NULL, &len);
len++;
- /*
- * Allocate space for each column
+ /*
+ * Allocate space for each column
*/
row[i-1] = (SQLCHAR*)rad_malloc((int)len);
* set for the query.
*
*************************************************************************/
-static int sql_store_result(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
+static int sql_store_result(UNUSED SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
return 0;
}
* of columns from query
*
*************************************************************************/
-static int sql_num_fields(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
+static int sql_num_fields(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
SQLSMALLINT count=0;
rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
* query
*
*************************************************************************/
-static int sql_num_rows(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
+static int sql_num_rows(UNUSED SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
/*
* I presume this function is used to determine the number of
* rows in a result set *before* fetching them. I don't think
* 0 on success, -1 on failure, SQL_DOWN if 'database is down'
*
*************************************************************************/
-static int sql_fetch_row(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
+static int sql_fetch_row(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
SQLRETURN rc;
rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
return 0;
}
/* XXX Check rc for database down, if so, return SQL_DOWN */
-
+
sqlsocket->row = iodbc_sock->row;
return 0;
}
* connection
*
*************************************************************************/
-static char *sql_error(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
+static char *sql_error(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
SQLINTEGER errornum = 0;
SQLSMALLINT length = 0;
static SQLCHAR error[256] = "";
rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
- SQLError(iodbc_sock->env_handle, iodbc_sock->dbc_handle, iodbc_sock->stmt_handle,
+ SQLError(iodbc_sock->env_handle, iodbc_sock->dbc_handle, iodbc_sock->stmt_handle,
state, &errornum, error, 256, &length);
return error;
}
* connection and cleans up any open handles.
*
*************************************************************************/
-static int sql_close(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
+static int sql_close(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
* or insert)
*
*************************************************************************/
-static int sql_affected_rows(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
+static int sql_affected_rows(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
SQLINTEGER count;
rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;