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 const char *sql_error(rlm_sql_handle_t *handle, rlm_sql_config_t *config);
50 static int sql_num_fields(rlm_sql_handle_t *handle, rlm_sql_config_t *config);
52 /*************************************************************************
54 * Function: sql_init_socket
56 * Purpose: Establish connection to the db
58 *************************************************************************/
59 static int sql_init_socket(rlm_sql_handle_t *handle, rlm_sql_config_t *config) {
61 rlm_sql_iodbc_sock *iodbc_sock;
65 handle->conn = (rlm_sql_iodbc_sock *)rad_malloc(sizeof(rlm_sql_iodbc_sock));
70 iodbc_sock = handle->conn;
71 memset(iodbc_sock, 0, sizeof(*iodbc_sock));
73 rcode = SQLAllocEnv(&iodbc_sock->env_handle);
74 if (!SQL_SUCCEEDED(rcode)) {
75 radlog(L_ERR, "sql_create_socket: SQLAllocEnv failed: %s",
76 sql_error(handle, config));
80 rcode = SQLAllocConnect(iodbc_sock->env_handle,
81 &iodbc_sock->dbc_handle);
82 if (!SQL_SUCCEEDED(rcode)) {
83 radlog(L_ERR, "sql_create_socket: SQLAllocConnect failed: %s",
84 sql_error(handle, config));
88 rcode = SQLConnect(iodbc_sock->dbc_handle, config->sql_server,
89 SQL_NTS, config->sql_login, SQL_NTS,
90 config->sql_password, SQL_NTS);
91 if (!SQL_SUCCEEDED(rcode)) {
92 radlog(L_ERR, "sql_create_socket: SQLConnectfailed: %s",
93 sql_error(handle, config));
100 /*************************************************************************
102 * Function: sql_destroy_socket
104 * Purpose: Free socket and private connection data
106 *************************************************************************/
107 static int sql_destroy_socket(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
114 /*************************************************************************
116 * Function: sql_query
118 * Purpose: Issue a non-SELECT query (ie: update/delete/insert) to
121 *************************************************************************/
122 static int sql_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char *querystr) {
124 rlm_sql_iodbc_sock *iodbc_sock = handle->conn;
127 rcode = SQLAllocStmt(iodbc_sock->dbc_handle,
128 &iodbc_sock->stmt_handle);
129 if (!SQL_SUCCEEDED(rcode)) {
130 radlog(L_ERR, "sql_create_socket: SQLAllocStmt failed: %s",
131 sql_error(handle, config));
135 if (iodbc_sock->dbc_handle == NULL) {
136 radlog(L_ERR, "sql_query: Socket not connected");
140 rcode = SQLExecDirect(iodbc_sock->stmt_handle, querystr, SQL_NTS);
141 if (!SQL_SUCCEEDED(rcode)) {
142 radlog(L_ERR, "sql_query: failed: %s",
143 sql_error(handle, config));
151 /*************************************************************************
153 * Function: sql_select_query
155 * Purpose: Issue a select query to the database
157 *************************************************************************/
158 static int sql_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char *querystr) {
164 rlm_sql_iodbc_sock *iodbc_sock = handle->conn;
166 if(sql_query(handle, config, querystr) < 0) {
170 numfields = sql_num_fields(handle, config);
172 row = (char **) rad_malloc(sizeof(char *) * (numfields+1));
173 memset(row, 0, (sizeof(char *) * (numfields)));
174 row[numfields] = NULL;
176 for(i=1; i<=numfields; i++) {
177 SQLColAttributes(iodbc_sock->stmt_handle, ((SQLUSMALLINT) i), SQL_COLUMN_LENGTH,
178 NULL, 0, NULL, &len);
182 * Allocate space for each column
184 row[i-1] = (SQLCHAR*)rad_malloc((int)len);
187 * This makes me feel dirty, but, according to Microsoft, it works.
188 * Any ODBC datatype can be converted to a 'char *' according to
191 * http://msdn.microsoft.com/library/psdk/dasdk/odap4o4z.htm
193 SQLBindCol(iodbc_sock->stmt_handle, i, SQL_C_CHAR, (SQLCHAR *)row[i-1], len, 0);
196 iodbc_sock->row = row;
202 /*************************************************************************
204 * Function: sql_store_result
206 * Purpose: database specific store_result function. Returns a result
209 *************************************************************************/
210 static int sql_store_result(UNUSED rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
216 /*************************************************************************
218 * Function: sql_num_fields
220 * Purpose: database specific num_fields function. Returns number
221 * of columns from query
223 *************************************************************************/
224 static int sql_num_fields(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
227 rlm_sql_iodbc_sock *iodbc_sock = handle->conn;
229 SQLNumResultCols(iodbc_sock->stmt_handle, &count);
234 /*************************************************************************
236 * Function: sql_num_rows
238 * Purpose: database specific num_rows. Returns number of rows in
241 *************************************************************************/
242 static int sql_num_rows(UNUSED rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
244 * I presume this function is used to determine the number of
245 * rows in a result set *before* fetching them. I don't think
246 * this is possible in ODBC 2.x, but I'd be happy to be proven
247 * wrong. If you know how to do this, email me at jeff@apex.net
253 /*************************************************************************
255 * Function: sql_fetch_row
257 * Purpose: database specific fetch_row. Returns a rlm_sql_row_t struct
258 * with all the data for the query in 'handle->row'. Returns
259 * 0 on success, -1 on failure, SQL_DOWN if 'database is down'
261 *************************************************************************/
262 static int sql_fetch_row(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
265 rlm_sql_iodbc_sock *iodbc_sock = handle->conn;
269 if((rc = SQLFetch(iodbc_sock->stmt_handle)) == SQL_NO_DATA_FOUND) {
272 /* XXX Check rc for database down, if so, return SQL_DOWN */
274 handle->row = iodbc_sock->row;
280 /*************************************************************************
282 * Function: sql_free_result
284 * Purpose: database specific free_result. Frees memory allocated
287 *************************************************************************/
288 static int sql_free_result(rlm_sql_handle_t *handle, rlm_sql_config_t *config) {
291 rlm_sql_iodbc_sock *iodbc_sock = handle->conn;
293 for(i=0; i<sql_num_fields(handle, config); i++) {
294 free(iodbc_sock->row[i]);
296 free(iodbc_sock->row);
297 iodbc_sock->row=NULL;
299 SQLFreeStmt( iodbc_sock->stmt_handle, SQL_DROP );
305 /*************************************************************************
307 * Function: sql_error
309 * Purpose: database specific error. Returns error associated with
312 *************************************************************************/
313 static const char *sql_error(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
315 SQLINTEGER errornum = 0;
316 SQLSMALLINT length = 0;
317 SQLCHAR state[256] = "";
318 static SQLCHAR error[256] = "";
319 rlm_sql_iodbc_sock *iodbc_sock = handle->conn;
321 SQLError(iodbc_sock->env_handle, iodbc_sock->dbc_handle, iodbc_sock->stmt_handle,
322 state, &errornum, error, 256, &length);
327 /*************************************************************************
329 * Function: sql_close
331 * Purpose: database specific close. Closes an open database
332 * connection and cleans up any open handles.
334 *************************************************************************/
335 static int sql_close(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
337 rlm_sql_iodbc_sock *iodbc_sock = handle->conn;
339 SQLFreeStmt(iodbc_sock->stmt_handle, SQL_DROP);
340 SQLDisconnect(iodbc_sock->dbc_handle);
341 SQLFreeConnect(iodbc_sock->dbc_handle);
342 SQLFreeEnv(iodbc_sock->env_handle);
344 iodbc_sock->stmt_handle = NULL;
345 iodbc_sock->dbc_handle = NULL;
346 iodbc_sock->env_handle = NULL;
352 /*************************************************************************
354 * Function: sql_finish_query
356 * Purpose: End the query, such as freeing memory
358 *************************************************************************/
359 static int sql_finish_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config) {
361 return sql_free_result(handle, config);
366 /*************************************************************************
368 * Function: sql_finish_select_query
370 * Purpose: End the select query, such as freeing memory or result
372 *************************************************************************/
373 static int sql_finish_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config) {
374 return sql_free_result(handle, config);
378 /*************************************************************************
380 * Function: sql_affected_rows
382 * Purpose: Return the number of rows affected by the query (update,
385 *************************************************************************/
386 static int sql_affected_rows(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
389 rlm_sql_iodbc_sock *iodbc_sock = handle->conn;
391 SQLRowCount(iodbc_sock->stmt_handle, &count);
395 /* Exported to rlm_sql */
396 rlm_sql_module_t rlm_sql_iodbc = {
411 sql_finish_select_query,