2 * sql_iodbc.c iODBC support for FreeRadius
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Copyright 2000 The FreeRADIUS server project
19 * Copyright 2000 Jeff Carneal <jeff@apex.net>
35 typedef struct rlm_sql_iodbc_sock {
42 struct sql_socket *next;
45 } rlm_sql_iodbc_sock;;
47 static char *sql_error(SQLSOCK *sqlsocket, SQL_CONFIG *config);
48 static int sql_num_fields(SQLSOCK *sqlsocket, SQL_CONFIG *config);
50 /*************************************************************************
52 * Function: sql_init_socket
54 * Purpose: Establish connection to the db
56 *************************************************************************/
57 static int sql_init_socket(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
59 rlm_sql_iodbc_sock *iodbc_sock;
61 if (!sqlsocket->conn) {
62 sqlsocket->conn = (rlm_sql_iodbc_sock *)rad_malloc(sizeof(rlm_sql_iodbc_sock));
63 if (!sqlsocket->conn) {
67 iodbc_sock = sqlsocket->conn;
68 memset(iodbc_sock, 0, sizeof(*iodbc_sock));
70 if(SQLAllocEnv(&iodbc_sock->env_handle) != SQL_SUCCESS) {
71 radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocEnv failed: %s",
72 sql_error(sqlsocket, config));
76 if(SQLAllocConnect(iodbc_sock->env_handle, &iodbc_sock->dbc_handle) != SQL_SUCCESS) {
77 radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocConnect failed: %s",
78 sql_error(sqlsocket, config));
82 if (SQLConnect(iodbc_sock->dbc_handle, config->sql_db, SQL_NTS,
83 config->sql_login, SQL_NTS, config->sql_password,
84 SQL_NTS) != SQL_SUCCESS) {
85 radlog(L_CONS|L_ERR, "sql_create_socket: SQLConnectfailed: %s",
86 sql_error(sqlsocket, config));
93 /*************************************************************************
95 * Function: sql_destroy_socket
97 * Purpose: Free socket and private connection data
99 *************************************************************************/
100 static int sql_destroy_socket(SQLSOCK *sqlsocket, SQL_CONFIG *config)
102 free(sqlsocket->conn);
103 sqlsocket->conn = NULL;
107 /*************************************************************************
109 * Function: sql_query
111 * Purpose: Issue a non-SELECT query (ie: update/delete/insert) to
114 *************************************************************************/
115 static int sql_query(SQLSOCK *sqlsocket, SQL_CONFIG *config, char *querystr) {
117 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
119 if(SQLAllocStmt(iodbc_sock->dbc_handle, &iodbc_sock->stmt_handle) != SQL_SUCCESS) {
120 radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocStmt failed: %s",
121 sql_error(sqlsocket, config));
125 if (config->sqltrace)
126 radlog(L_DBG, "rlm_sql: %s", querystr);
127 if (iodbc_sock->dbc_handle == NULL) {
128 radlog(L_ERR, "sql_query: Socket not connected");
132 if (SQLExecDirect(iodbc_sock->stmt_handle, querystr, SQL_NTS) != SQL_SUCCESS) {
133 radlog(L_CONS|L_ERR, "sql_query: failed: %s",
134 sql_error(sqlsocket, config));
142 /*************************************************************************
144 * Function: sql_select_query
146 * Purpose: Issue a select query to the database
148 *************************************************************************/
149 static int sql_select_query(SQLSOCK *sqlsocket, SQL_CONFIG *config, char *querystr) {
155 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
157 if(sql_query(sqlsocket, config, querystr) < 0) {
161 numfields = sql_num_fields(sqlsocket, config);
163 row = (char **) rad_malloc(sizeof(char *) * (numfields+1));
164 memset(row, 0, (sizeof(char *) * (numfields)));
165 row[numfields] = NULL;
167 for(i=1; i<=numfields; i++) {
168 SQLColAttributes(iodbc_sock->stmt_handle, ((SQLUSMALLINT) i), SQL_COLUMN_LENGTH,
169 NULL, 0, NULL, &len);
173 * Allocate space for each column
175 row[i-1] = (SQLCHAR*)rad_malloc((int)len);
178 * This makes me feel dirty, but, according to Microsoft, it works.
179 * Any ODBC datatype can be converted to a 'char *' according to
182 * http://msdn.microsoft.com/library/psdk/dasdk/odap4o4z.htm
184 SQLBindCol(iodbc_sock->stmt_handle, i, SQL_C_CHAR, (SQLCHAR *)row[i-1], len, 0);
187 iodbc_sock->row = row;
193 /*************************************************************************
195 * Function: sql_store_result
197 * Purpose: database specific store_result function. Returns a result
200 *************************************************************************/
201 static int sql_store_result(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
207 /*************************************************************************
209 * Function: sql_num_fields
211 * Purpose: database specific num_fields function. Returns number
212 * of columns from query
214 *************************************************************************/
215 static int sql_num_fields(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
218 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
220 SQLNumResultCols(iodbc_sock->stmt_handle, &count);
225 /*************************************************************************
227 * Function: sql_num_rows
229 * Purpose: database specific num_rows. Returns number of rows in
232 *************************************************************************/
233 static int sql_num_rows(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
235 * I presume this function is used to determine the number of
236 * rows in a result set *before* fetching them. I don't think
237 * this is possible in ODBC 2.x, but I'd be happy to be proven
238 * wrong. If you know how to do this, email me at jeff@apex.net
244 /*************************************************************************
246 * Function: sql_fetch_row
248 * Purpose: database specific fetch_row. Returns a SQL_ROW struct
249 * with all the data for the query in 'sqlsocket->row'. Returns
250 * 0 on success, -1 on failure, SQL_DOWN if 'database is down'
252 *************************************************************************/
253 static int sql_fetch_row(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
256 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
258 sqlsocket->row = NULL;
260 if((rc = SQLFetch(iodbc_sock->stmt_handle)) == SQL_NO_DATA_FOUND) {
263 /* XXX Check rc for database down, if so, return SQL_DOWN */
265 sqlsocket->row = iodbc_sock->row;
271 /*************************************************************************
273 * Function: sql_free_result
275 * Purpose: database specific free_result. Frees memory allocated
278 *************************************************************************/
279 static int sql_free_result(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
282 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
284 for(i=0; i<sql_num_fields(sqlsocket, config); i++) {
285 free(iodbc_sock->row[i]);
287 free(iodbc_sock->row);
288 iodbc_sock->row=NULL;
290 SQLFreeStmt( iodbc_sock->stmt_handle, SQL_DROP );
296 /*************************************************************************
298 * Function: sql_error
300 * Purpose: database specific error. Returns error associated with
303 *************************************************************************/
304 static char *sql_error(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
306 SQLINTEGER errornum = 0;
307 SQLSMALLINT length = 0;
308 SQLCHAR state[256] = "";
309 static SQLCHAR error[256] = "";
310 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
312 SQLError(iodbc_sock->env_handle, iodbc_sock->dbc_handle, iodbc_sock->stmt_handle,
313 state, &errornum, error, 256, &length);
318 /*************************************************************************
320 * Function: sql_close
322 * Purpose: database specific close. Closes an open database
323 * connection and cleans up any open handles.
325 *************************************************************************/
326 static int sql_close(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
328 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
330 SQLFreeStmt(iodbc_sock->stmt_handle, SQL_DROP);
331 SQLDisconnect(iodbc_sock->dbc_handle);
332 SQLFreeConnect(iodbc_sock->dbc_handle);
333 SQLFreeEnv(iodbc_sock->env_handle);
335 iodbc_sock->stmt_handle = NULL;
336 iodbc_sock->dbc_handle = NULL;
337 iodbc_sock->env_handle = NULL;
343 /*************************************************************************
345 * Function: sql_finish_query
347 * Purpose: End the query, such as freeing memory
349 *************************************************************************/
350 static int sql_finish_query(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
352 return sql_free_result(sqlsocket, config);
357 /*************************************************************************
359 * Function: sql_finish_select_query
361 * Purpose: End the select query, such as freeing memory or result
363 *************************************************************************/
364 static int sql_finish_select_query(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
365 return sql_free_result(sqlsocket, config);
369 /*************************************************************************
371 * Function: sql_affected_rows
373 * Purpose: Return the number of rows affected by the query (update,
376 *************************************************************************/
377 static int sql_affected_rows(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
380 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
382 SQLRowCount(iodbc_sock->stmt_handle, &count);
386 /* Exported to rlm_sql */
387 rlm_sql_module_t rlm_sql_iodbc = {
401 sql_finish_select_query,