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,2006 The FreeRADIUS server project
21 * Copyright 2000 Jeff Carneal <jeff@apex.net>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
37 typedef struct rlm_sql_iodbc_sock {
44 struct sql_socket *next;
47 } rlm_sql_iodbc_sock;;
49 static char *sql_error(SQLSOCK *sqlsocket, SQL_CONFIG *config);
50 static int sql_num_fields(SQLSOCK *sqlsocket, SQL_CONFIG *config);
52 /*************************************************************************
54 * Function: sql_init_socket
56 * Purpose: Establish connection to the db
58 *************************************************************************/
59 static int sql_init_socket(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
61 rlm_sql_iodbc_sock *iodbc_sock;
64 if (!sqlsocket->conn) {
65 sqlsocket->conn = (rlm_sql_iodbc_sock *)rad_malloc(sizeof(rlm_sql_iodbc_sock));
66 if (!sqlsocket->conn) {
70 iodbc_sock = sqlsocket->conn;
71 memset(iodbc_sock, 0, sizeof(*iodbc_sock));
73 rcode = SQLAllocEnv(&iodbc_sock->env_handle);
74 if (!SQL_SUCCEEDED(rcode)) {
75 radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocEnv failed: %s",
76 sql_error(sqlsocket, config));
80 rcode = SQLAllocConnect(iodbc_sock->env_handle,
81 &iodbc_sock->dbc_handle);
82 if (!SQL_SUCCEEDED(rcode)) {
83 radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocConnect failed: %s",
84 sql_error(sqlsocket, config));
88 rcode = SQLConnect(iodbc_sock->dbc_handle, config->sql_db,
89 SQL_NTS, config->sql_login, SQL_NTS,
90 config->sql_password, SQL_NTS);
91 if (!SQL_SUCCEEDED(rcode)) {
92 radlog(L_CONS|L_ERR, "sql_create_socket: SQLConnectfailed: %s",
93 sql_error(sqlsocket, config));
100 /*************************************************************************
102 * Function: sql_destroy_socket
104 * Purpose: Free socket and private connection data
106 *************************************************************************/
107 static int sql_destroy_socket(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config)
109 free(sqlsocket->conn);
110 sqlsocket->conn = NULL;
114 /*************************************************************************
116 * Function: sql_query
118 * Purpose: Issue a non-SELECT query (ie: update/delete/insert) to
121 *************************************************************************/
122 static int sql_query(SQLSOCK *sqlsocket, SQL_CONFIG *config, char *querystr) {
124 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
127 rcode = SQLAllocStmt(iodbc_sock->dbc_handle,
128 &iodbc_sock->stmt_handle);
129 if (!SQL_SUCCEEDED(rcode)) {
130 radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocStmt failed: %s",
131 sql_error(sqlsocket, config));
135 if (config->sqltrace)
136 radlog(L_DBG, "rlm_sql: %s", querystr);
137 if (iodbc_sock->dbc_handle == NULL) {
138 radlog(L_ERR, "sql_query: Socket not connected");
142 rcode = SQLExecDirect(iodbc_sock->stmt_handle, querystr, SQL_NTS);
143 if (!SQL_SUCCEEDED(rcode)) {
144 radlog(L_CONS|L_ERR, "sql_query: failed: %s",
145 sql_error(sqlsocket, config));
153 /*************************************************************************
155 * Function: sql_select_query
157 * Purpose: Issue a select query to the database
159 *************************************************************************/
160 static int sql_select_query(SQLSOCK *sqlsocket, SQL_CONFIG *config, char *querystr) {
166 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
168 if(sql_query(sqlsocket, config, querystr) < 0) {
172 numfields = sql_num_fields(sqlsocket, config);
174 row = (char **) rad_malloc(sizeof(char *) * (numfields+1));
175 memset(row, 0, (sizeof(char *) * (numfields)));
176 row[numfields] = NULL;
178 for(i=1; i<=numfields; i++) {
179 SQLColAttributes(iodbc_sock->stmt_handle, ((SQLUSMALLINT) i), SQL_COLUMN_LENGTH,
180 NULL, 0, NULL, &len);
184 * Allocate space for each column
186 row[i-1] = (SQLCHAR*)rad_malloc((int)len);
189 * This makes me feel dirty, but, according to Microsoft, it works.
190 * Any ODBC datatype can be converted to a 'char *' according to
193 * http://msdn.microsoft.com/library/psdk/dasdk/odap4o4z.htm
195 SQLBindCol(iodbc_sock->stmt_handle, i, SQL_C_CHAR, (SQLCHAR *)row[i-1], len, 0);
198 iodbc_sock->row = row;
204 /*************************************************************************
206 * Function: sql_store_result
208 * Purpose: database specific store_result function. Returns a result
211 *************************************************************************/
212 static int sql_store_result(UNUSED SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
218 /*************************************************************************
220 * Function: sql_num_fields
222 * Purpose: database specific num_fields function. Returns number
223 * of columns from query
225 *************************************************************************/
226 static int sql_num_fields(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
229 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
231 SQLNumResultCols(iodbc_sock->stmt_handle, &count);
236 /*************************************************************************
238 * Function: sql_num_rows
240 * Purpose: database specific num_rows. Returns number of rows in
243 *************************************************************************/
244 static int sql_num_rows(UNUSED SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
246 * I presume this function is used to determine the number of
247 * rows in a result set *before* fetching them. I don't think
248 * this is possible in ODBC 2.x, but I'd be happy to be proven
249 * wrong. If you know how to do this, email me at jeff@apex.net
255 /*************************************************************************
257 * Function: sql_fetch_row
259 * Purpose: database specific fetch_row. Returns a SQL_ROW struct
260 * with all the data for the query in 'sqlsocket->row'. Returns
261 * 0 on success, -1 on failure, SQL_DOWN if 'database is down'
263 *************************************************************************/
264 static int sql_fetch_row(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
267 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
269 sqlsocket->row = NULL;
271 if((rc = SQLFetch(iodbc_sock->stmt_handle)) == SQL_NO_DATA_FOUND) {
274 /* XXX Check rc for database down, if so, return SQL_DOWN */
276 sqlsocket->row = iodbc_sock->row;
282 /*************************************************************************
284 * Function: sql_free_result
286 * Purpose: database specific free_result. Frees memory allocated
289 *************************************************************************/
290 static int sql_free_result(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
293 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
295 for(i=0; i<sql_num_fields(sqlsocket, config); i++) {
296 free(iodbc_sock->row[i]);
298 free(iodbc_sock->row);
299 iodbc_sock->row=NULL;
301 SQLFreeStmt( iodbc_sock->stmt_handle, SQL_DROP );
307 /*************************************************************************
309 * Function: sql_error
311 * Purpose: database specific error. Returns error associated with
314 *************************************************************************/
315 static char *sql_error(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
317 SQLINTEGER errornum = 0;
318 SQLSMALLINT length = 0;
319 SQLCHAR state[256] = "";
320 static SQLCHAR error[256] = "";
321 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
323 SQLError(iodbc_sock->env_handle, iodbc_sock->dbc_handle, iodbc_sock->stmt_handle,
324 state, &errornum, error, 256, &length);
329 /*************************************************************************
331 * Function: sql_close
333 * Purpose: database specific close. Closes an open database
334 * connection and cleans up any open handles.
336 *************************************************************************/
337 static int sql_close(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
339 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
341 SQLFreeStmt(iodbc_sock->stmt_handle, SQL_DROP);
342 SQLDisconnect(iodbc_sock->dbc_handle);
343 SQLFreeConnect(iodbc_sock->dbc_handle);
344 SQLFreeEnv(iodbc_sock->env_handle);
346 iodbc_sock->stmt_handle = NULL;
347 iodbc_sock->dbc_handle = NULL;
348 iodbc_sock->env_handle = NULL;
354 /*************************************************************************
356 * Function: sql_finish_query
358 * Purpose: End the query, such as freeing memory
360 *************************************************************************/
361 static int sql_finish_query(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
363 return sql_free_result(sqlsocket, config);
368 /*************************************************************************
370 * Function: sql_finish_select_query
372 * Purpose: End the select query, such as freeing memory or result
374 *************************************************************************/
375 static int sql_finish_select_query(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
376 return sql_free_result(sqlsocket, config);
380 /*************************************************************************
382 * Function: sql_affected_rows
384 * Purpose: Return the number of rows affected by the query (update,
387 *************************************************************************/
388 static int sql_affected_rows(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config) {
391 rlm_sql_iodbc_sock *iodbc_sock = sqlsocket->conn;
393 SQLRowCount(iodbc_sock->stmt_handle, &count);
397 /* Exported to rlm_sql */
398 rlm_sql_module_t rlm_sql_iodbc = {
412 sql_finish_select_query,