2 * sql_iodbc.c iODBC support for FreeRadius
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000 The FreeRADIUS server project
21 * Copyright 2000 Jeff Carneal <jeff@apex.net>
24 #include <freeradius-devel/autoconf.h>
35 #include <freeradius-devel/radiusd.h>
38 typedef struct rlm_sql_iodbc_sock {
45 struct sql_socket *next;
48 } rlm_sql_iodbc_sock;;
50 static char *sql_error(SQLSOCK *sqlsocket, SQL_CONFIG *config);
51 static int sql_num_fields(SQLSOCK *sqlsocket, SQL_CONFIG *config);
53 /*************************************************************************
55 * Function: sql_init_socket
57 * Purpose: Establish connection to the db
59 *************************************************************************/
60 static int sql_init_socket(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
62 rlm_sql_iodbc_sock *iodbc_sock;
65 if (!sqlsocket->conn) {
66 sqlsocket->conn = (rlm_sql_iodbc_sock *)rad_malloc(sizeof(rlm_sql_iodbc_sock));
67 if (!sqlsocket->conn) {
71 iodbc_sock = sqlsocket->conn;
72 memset(iodbc_sock, 0, sizeof(*iodbc_sock));
74 rcode = SQLAllocEnv(&iodbc_sock->env_handle);
75 if (!SQL_SUCCEEDED(rcode)) {
76 radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocEnv failed: %s",
77 sql_error(sqlsocket, config));
81 rcode = SQLAllocConnect(iodbc_sock->env_handle,
82 &iodbc_sock->dbc_handle);
83 if (!SQL_SUCCEEDED(rcode)) {
84 radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocConnect failed: %s",
85 sql_error(sqlsocket, config));
89 rcode = SQLConnect(iodbc_sock->dbc_handle, config->sql_db,
90 SQL_NTS, config->sql_login, SQL_NTS,
91 config->sql_password, SQL_NTS);
92 if (!SQL_SUCCEEDED(rcode)) {
93 radlog(L_CONS|L_ERR, "sql_create_socket: SQLConnectfailed: %s",
94 sql_error(sqlsocket, config));
101 /*************************************************************************
103 * Function: sql_destroy_socket
105 * Purpose: Free socket and private connection data
107 *************************************************************************/
108 static int sql_destroy_socket(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config)
110 free(sqlsocket->conn);
111 sqlsocket->conn = NULL;
115 /*************************************************************************
117 * Function: sql_query
119 * Purpose: Issue a non-SELECT query (ie: update/delete/insert) to
122 *************************************************************************/
123 static int sql_query(SQLSOCK *sqlsocket, SQL_CONFIG *config, char *querystr) {
125 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
128 rcode = SQLAllocStmt(iodbc_sock->dbc_handle,
129 &iodbc_sock->stmt_handle);
130 if (!SQL_SUCCEEDED(rcode)) {
131 radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocStmt failed: %s",
132 sql_error(sqlsocket, config));
136 if (config->sqltrace)
137 radlog(L_DBG, "rlm_sql: %s", querystr);
138 if (iodbc_sock->dbc_handle == NULL) {
139 radlog(L_ERR, "sql_query: Socket not connected");
143 rcode = SQLExecDirect(iodbc_sock->stmt_handle, querystr, SQL_NTS);
144 if (!SQL_SUCCEEDED(rcode)) {
145 radlog(L_CONS|L_ERR, "sql_query: failed: %s",
146 sql_error(sqlsocket, config));
154 /*************************************************************************
156 * Function: sql_select_query
158 * Purpose: Issue a select query to the database
160 *************************************************************************/
161 static int sql_select_query(SQLSOCK *sqlsocket, SQL_CONFIG *config, char *querystr) {
167 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
169 if(sql_query(sqlsocket, config, querystr) < 0) {
173 numfields = sql_num_fields(sqlsocket, config);
175 row = (char **) rad_malloc(sizeof(char *) * (numfields+1));
176 memset(row, 0, (sizeof(char *) * (numfields)));
177 row[numfields] = NULL;
179 for(i=1; i<=numfields; i++) {
180 SQLColAttributes(iodbc_sock->stmt_handle, ((SQLUSMALLINT) i), SQL_COLUMN_LENGTH,
181 NULL, 0, NULL, &len);
185 * Allocate space for each column
187 row[i-1] = (SQLCHAR*)rad_malloc((int)len);
190 * This makes me feel dirty, but, according to Microsoft, it works.
191 * Any ODBC datatype can be converted to a 'char *' according to
194 * http://msdn.microsoft.com/library/psdk/dasdk/odap4o4z.htm
196 SQLBindCol(iodbc_sock->stmt_handle, i, SQL_C_CHAR, (SQLCHAR *)row[i-1], len, 0);
199 iodbc_sock->row = row;
205 /*************************************************************************
207 * Function: sql_store_result
209 * Purpose: database specific store_result function. Returns a result
212 *************************************************************************/
213 static int sql_store_result(UNUSED SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
219 /*************************************************************************
221 * Function: sql_num_fields
223 * Purpose: database specific num_fields function. Returns number
224 * of columns from query
226 *************************************************************************/
227 static int sql_num_fields(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
230 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
232 SQLNumResultCols(iodbc_sock->stmt_handle, &count);
237 /*************************************************************************
239 * Function: sql_num_rows
241 * Purpose: database specific num_rows. Returns number of rows in
244 *************************************************************************/
245 static int sql_num_rows(UNUSED SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
247 * I presume this function is used to determine the number of
248 * rows in a result set *before* fetching them. I don't think
249 * this is possible in ODBC 2.x, but I'd be happy to be proven
250 * wrong. If you know how to do this, email me at jeff@apex.net
256 /*************************************************************************
258 * Function: sql_fetch_row
260 * Purpose: database specific fetch_row. Returns a SQL_ROW struct
261 * with all the data for the query in 'sqlsocket->row'. Returns
262 * 0 on success, -1 on failure, SQL_DOWN if 'database is down'
264 *************************************************************************/
265 static int sql_fetch_row(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
268 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
270 sqlsocket->row = NULL;
272 if((rc = SQLFetch(iodbc_sock->stmt_handle)) == SQL_NO_DATA_FOUND) {
275 /* XXX Check rc for database down, if so, return SQL_DOWN */
277 sqlsocket->row = iodbc_sock->row;
283 /*************************************************************************
285 * Function: sql_free_result
287 * Purpose: database specific free_result. Frees memory allocated
290 *************************************************************************/
291 static int sql_free_result(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
294 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
296 for(i=0; i<sql_num_fields(sqlsocket, config); i++) {
297 free(iodbc_sock->row[i]);
299 free(iodbc_sock->row);
300 iodbc_sock->row=NULL;
302 SQLFreeStmt( iodbc_sock->stmt_handle, SQL_DROP );
308 /*************************************************************************
310 * Function: sql_error
312 * Purpose: database specific error. Returns error associated with
315 *************************************************************************/
316 static char *sql_error(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
318 SQLINTEGER errornum = 0;
319 SQLSMALLINT length = 0;
320 SQLCHAR state[256] = "";
321 static SQLCHAR error[256] = "";
322 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
324 SQLError(iodbc_sock->env_handle, iodbc_sock->dbc_handle, iodbc_sock->stmt_handle,
325 state, &errornum, error, 256, &length);
330 /*************************************************************************
332 * Function: sql_close
334 * Purpose: database specific close. Closes an open database
335 * connection and cleans up any open handles.
337 *************************************************************************/
338 static int sql_close(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
340 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
342 SQLFreeStmt(iodbc_sock->stmt_handle, SQL_DROP);
343 SQLDisconnect(iodbc_sock->dbc_handle);
344 SQLFreeConnect(iodbc_sock->dbc_handle);
345 SQLFreeEnv(iodbc_sock->env_handle);
347 iodbc_sock->stmt_handle = NULL;
348 iodbc_sock->dbc_handle = NULL;
349 iodbc_sock->env_handle = NULL;
355 /*************************************************************************
357 * Function: sql_finish_query
359 * Purpose: End the query, such as freeing memory
361 *************************************************************************/
362 static int sql_finish_query(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
364 return sql_free_result(sqlsocket, config);
369 /*************************************************************************
371 * Function: sql_finish_select_query
373 * Purpose: End the select query, such as freeing memory or result
375 *************************************************************************/
376 static int sql_finish_select_query(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
377 return sql_free_result(sqlsocket, config);
381 /*************************************************************************
383 * Function: sql_affected_rows
385 * Purpose: Return the number of rows affected by the query (update,
388 *************************************************************************/
389 static int sql_affected_rows(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
392 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
394 SQLRowCount(iodbc_sock->stmt_handle, &count);
398 /* Exported to rlm_sql */
399 rlm_sql_module_t rlm_sql_iodbc = {
413 sql_finish_select_query,