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_conn {
45 struct sql_socket *next;
48 } rlm_sql_iodbc_conn_t;
50 static const char *sql_error(rlm_sql_handle_t *handle, rlm_sql_config_t *config);
51 static int sql_num_fields(rlm_sql_handle_t *handle, rlm_sql_config_t *config);
53 static int sql_socket_destructor(void *c)
55 rlm_sql_iodbc_conn_t *conn = c;
57 DEBUG2("rlm_sql_iodbc: Socket destructor called, closing socket");
59 if (conn->stmt_handle) {
60 SQLFreeStmt(conn->stmt_handle, SQL_DROP);
63 if (conn->dbc_handle) {
64 SQLDisconnect(conn->dbc_handle);
65 SQLFreeConnect(conn->dbc_handle);
68 if (conn->env_handle) {
69 SQLFreeEnv(conn->env_handle);
75 /*************************************************************************
77 * Function: sql_socket_init
79 * Purpose: Establish connection to the db
81 *************************************************************************/
82 static int sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t *config) {
84 rlm_sql_iodbc_conn_t *conn;
87 MEM(conn = handle->conn = talloc_zero(handle, rlm_sql_iodbc_conn_t));
88 talloc_set_destructor((void *) conn, sql_socket_destructor);
90 rcode = SQLAllocEnv(&conn->env_handle);
91 if (!SQL_SUCCEEDED(rcode)) {
92 radlog(L_ERR, "sql_create_socket: SQLAllocEnv failed: %s",
93 sql_error(handle, config));
97 rcode = SQLAllocConnect(conn->env_handle,
99 if (!SQL_SUCCEEDED(rcode)) {
100 radlog(L_ERR, "sql_create_socket: SQLAllocConnect failed: %s",
101 sql_error(handle, config));
105 rcode = SQLConnect(conn->dbc_handle, config->sql_server,
106 SQL_NTS, config->sql_login, SQL_NTS,
107 config->sql_password, SQL_NTS);
108 if (!SQL_SUCCEEDED(rcode)) {
109 radlog(L_ERR, "sql_create_socket: SQLConnectfailed: %s",
110 sql_error(handle, config));
117 /*************************************************************************
119 * Function: sql_query
121 * Purpose: Issue a non-SELECT query (ie: update/delete/insert) to
124 *************************************************************************/
125 static int sql_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char *querystr) {
127 rlm_sql_iodbc_conn_t *conn = handle->conn;
130 rcode = SQLAllocStmt(conn->dbc_handle,
132 if (!SQL_SUCCEEDED(rcode)) {
133 radlog(L_ERR, "sql_create_socket: SQLAllocStmt failed: %s",
134 sql_error(handle, config));
138 if (conn->dbc_handle == NULL) {
139 radlog(L_ERR, "sql_query: Socket not connected");
143 rcode = SQLExecDirect(conn->stmt_handle, querystr, SQL_NTS);
144 if (!SQL_SUCCEEDED(rcode)) {
145 radlog(L_ERR, "sql_query: failed: %s",
146 sql_error(handle, config));
154 /*************************************************************************
156 * Function: sql_select_query
158 * Purpose: Issue a select query to the database
160 *************************************************************************/
161 static int sql_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char *querystr) {
167 rlm_sql_iodbc_conn_t *conn = handle->conn;
169 if(sql_query(handle, config, querystr) < 0) {
173 numfields = sql_num_fields(handle, 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(conn->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(conn->stmt_handle, i, SQL_C_CHAR, (SQLCHAR *)row[i-1], len, 0);
205 /*************************************************************************
207 * Function: sql_store_result
209 * Purpose: database specific store_result function. Returns a result
212 *************************************************************************/
213 static int sql_store_result(UNUSED rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *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(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
230 rlm_sql_iodbc_conn_t *conn = handle->conn;
232 SQLNumResultCols(conn->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 rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *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 rlm_sql_row_t struct
261 * with all the data for the query in 'handle->row'. Returns
262 * 0 on success, -1 on failure, SQL_DOWN if 'database is down'
264 *************************************************************************/
265 static int sql_fetch_row(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
268 rlm_sql_iodbc_conn_t *conn = handle->conn;
272 if((rc = SQLFetch(conn->stmt_handle)) == SQL_NO_DATA_FOUND) {
275 /* XXX Check rc for database down, if so, return SQL_DOWN */
277 handle->row = conn->row;
283 /*************************************************************************
285 * Function: sql_free_result
287 * Purpose: database specific free_result. Frees memory allocated
290 *************************************************************************/
291 static int sql_free_result(rlm_sql_handle_t *handle, rlm_sql_config_t *config) {
294 rlm_sql_iodbc_conn_t *conn = handle->conn;
296 for(i=0; i<sql_num_fields(handle, config); i++) {
302 SQLFreeStmt( conn->stmt_handle, SQL_DROP );
308 /*************************************************************************
310 * Function: sql_error
312 * Purpose: database specific error. Returns error associated with
315 *************************************************************************/
316 static const char *sql_error(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
318 SQLINTEGER errornum = 0;
319 SQLSMALLINT length = 0;
320 SQLCHAR state[256] = "";
321 static SQLCHAR error[256] = "";
322 rlm_sql_iodbc_conn_t *conn = handle->conn;
324 SQLError(conn->env_handle, conn->dbc_handle, conn->stmt_handle,
325 state, &errornum, error, 256, &length);
329 /*************************************************************************
331 * Function: sql_finish_query
333 * Purpose: End the query, such as freeing memory
335 *************************************************************************/
336 static int sql_finish_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config) {
338 return sql_free_result(handle, config);
341 /*************************************************************************
343 * Function: sql_finish_select_query
345 * Purpose: End the select query, such as freeing memory or result
347 *************************************************************************/
348 static int sql_finish_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config) {
349 return sql_free_result(handle, config);
352 /*************************************************************************
354 * Function: sql_affected_rows
356 * Purpose: Return the number of rows affected by the query (update,
359 *************************************************************************/
360 static int sql_affected_rows(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
363 rlm_sql_iodbc_conn_t *conn = handle->conn;
365 SQLRowCount(conn->stmt_handle, &count);
369 /* Exported to rlm_sql */
370 rlm_sql_module_t rlm_sql_iodbc = {
383 sql_finish_select_query,