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>
25 USES_APPLE_DEPRECATED_API
27 #include <freeradius-devel/radiusd.h>
37 #define IODBC_MAX_ERROR_LEN 256
39 typedef struct rlm_sql_iodbc_conn {
47 struct sql_socket *next;
49 SQLCHAR error[IODBC_MAX_ERROR_LEN];
51 } rlm_sql_iodbc_conn_t;
53 static char const *sql_error(rlm_sql_handle_t *handle, rlm_sql_config_t *config);
54 static int sql_num_fields(rlm_sql_handle_t *handle, rlm_sql_config_t *config);
56 static int _sql_socket_destructor(rlm_sql_iodbc_conn_t *conn)
58 DEBUG2("rlm_sql_iodbc: Socket destructor called, closing socket");
60 if (conn->stmt_handle) {
61 SQLFreeStmt(conn->stmt_handle, SQL_DROP);
64 if (conn->dbc_handle) {
65 SQLDisconnect(conn->dbc_handle);
66 SQLFreeConnect(conn->dbc_handle);
69 if (conn->env_handle) {
70 SQLFreeEnv(conn->env_handle);
76 /*************************************************************************
78 * Function: sql_socket_init
80 * Purpose: Establish connection to the db
82 *************************************************************************/
83 static sql_rcode_t sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t *config) {
85 rlm_sql_iodbc_conn_t *conn;
88 MEM(conn = handle->conn = talloc_zero(handle, rlm_sql_iodbc_conn_t));
89 talloc_set_destructor(conn, _sql_socket_destructor);
91 rcode = SQLAllocEnv(&conn->env_handle);
92 if (!SQL_SUCCEEDED(rcode)) {
93 ERROR("sql_create_socket: SQLAllocEnv failed: %s",
94 sql_error(handle, config));
98 rcode = SQLAllocConnect(conn->env_handle,
100 if (!SQL_SUCCEEDED(rcode)) {
101 ERROR("sql_create_socket: SQLAllocConnect failed: %s",
102 sql_error(handle, config));
107 * The iodbc API doesn't qualify arguments as const even when they should be.
110 SQLCHAR *server, *login, *password;
112 memcpy(&server, &config->sql_server, sizeof(server));
113 memcpy(&login, &config->sql_login, sizeof(login));
114 memcpy(&password, &config->sql_password, sizeof(password));
116 rcode = SQLConnect(conn->dbc_handle, server, SQL_NTS, login, SQL_NTS, password, SQL_NTS);
118 if (!SQL_SUCCEEDED(rcode)) {
119 ERROR("sql_create_socket: SQLConnectfailed: %s",
120 sql_error(handle, config));
127 /*************************************************************************
129 * Function: sql_query
131 * Purpose: Issue a non-SELECT query (ie: update/delete/insert) to
134 *************************************************************************/
135 static sql_rcode_t sql_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char const *query) {
137 rlm_sql_iodbc_conn_t *conn = handle->conn;
140 rcode = SQLAllocStmt(conn->dbc_handle,
142 if (!SQL_SUCCEEDED(rcode)) {
143 ERROR("sql_create_socket: SQLAllocStmt failed: %s",
144 sql_error(handle, config));
148 if (!conn->dbc_handle) {
149 ERROR("sql_query: Socket not connected");
156 memcpy(&statement, &query, sizeof(statement));
157 rcode = SQLExecDirect(conn->stmt_handle, statement, SQL_NTS);
160 if (!SQL_SUCCEEDED(rcode)) {
161 ERROR("sql_query: failed: %s",
162 sql_error(handle, config));
170 /*************************************************************************
172 * Function: sql_select_query
174 * Purpose: Issue a select query to the database
176 *************************************************************************/
177 static sql_rcode_t sql_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char const *query) {
183 rlm_sql_iodbc_conn_t *conn = handle->conn;
185 if(sql_query(handle, config, query) < 0) {
189 numfields = sql_num_fields(handle, config);
191 row = (char **) rad_malloc(sizeof(char *) * (numfields+1));
192 memset(row, 0, (sizeof(char *) * (numfields)));
193 row[numfields] = NULL;
195 for(i=1; i<=numfields; i++) {
196 SQLColAttributes(conn->stmt_handle, ((SQLUSMALLINT) i), SQL_COLUMN_LENGTH, NULL, 0, NULL, &len);
200 * Allocate space for each column
202 row[i - 1] = rad_malloc((size_t) len);
205 * This makes me feel dirty, but, according to Microsoft, it works.
206 * Any ODBC datatype can be converted to a 'char *' according to
209 * http://msdn.microsoft.com/library/psdk/dasdk/odap4o4z.htm
211 SQLBindCol(conn->stmt_handle, i, SQL_C_CHAR, (SQLCHAR *)row[i-1], len, 0);
220 /*************************************************************************
222 * Function: sql_store_result
224 * Purpose: database specific store_result function. Returns a result
227 *************************************************************************/
228 static sql_rcode_t sql_store_result(UNUSED rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
234 /*************************************************************************
236 * Function: sql_num_fields
238 * Purpose: database specific num_fields function. Returns number
239 * of columns from query
241 *************************************************************************/
242 static int sql_num_fields(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
245 rlm_sql_iodbc_conn_t *conn = handle->conn;
247 SQLNumResultCols(conn->stmt_handle, &count);
252 /*************************************************************************
254 * Function: sql_num_rows
256 * Purpose: database specific num_rows. Returns number of rows in
259 *************************************************************************/
260 static int sql_num_rows(UNUSED rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
262 * I presume this function is used to determine the number of
263 * rows in a result set *before* fetching them. I don't think
264 * this is possible in ODBC 2.x, but I'd be happy to be proven
265 * wrong. If you know how to do this, email me at jeff@apex.net
271 /*************************************************************************
273 * Function: sql_fetch_row
275 * Purpose: database specific fetch_row. Returns a rlm_sql_row_t struct
276 * with all the data for the query in 'handle->row'. Returns
277 * 0 on success, -1 on failure, RLM_SQL_RECONNECT if 'database is down'
279 *************************************************************************/
280 static sql_rcode_t sql_fetch_row(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
283 rlm_sql_iodbc_conn_t *conn = handle->conn;
287 if((rc = SQLFetch(conn->stmt_handle)) == SQL_NO_DATA_FOUND) {
290 /* XXX Check rc for database down, if so, return RLM_SQL_RECONNECT */
292 handle->row = conn->row;
298 /*************************************************************************
300 * Function: sql_free_result
302 * Purpose: database specific free_result. Frees memory allocated
305 *************************************************************************/
306 static sql_rcode_t sql_free_result(rlm_sql_handle_t *handle, rlm_sql_config_t *config) {
309 rlm_sql_iodbc_conn_t *conn = handle->conn;
311 for(i=0; i<sql_num_fields(handle, config); i++) {
317 SQLFreeStmt( conn->stmt_handle, SQL_DROP );
323 /*************************************************************************
325 * Function: sql_error
327 * Purpose: database specific error. Returns error associated with
330 *************************************************************************/
331 static char const *sql_error(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
333 SQLINTEGER errornum = 0;
334 SQLSMALLINT length = 0;
335 SQLCHAR state[256] = "";
336 rlm_sql_iodbc_conn_t *conn = handle->conn;
338 conn->error[0] = '\0';
340 SQLError(conn->env_handle, conn->dbc_handle, conn->stmt_handle,
341 state, &errornum, conn->error, IODBC_MAX_ERROR_LEN, &length);
342 return (char const *) &conn->error;
345 /*************************************************************************
347 * Function: sql_finish_query
349 * Purpose: End the query, such as freeing memory
351 *************************************************************************/
352 static sql_rcode_t sql_finish_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config) {
354 return sql_free_result(handle, config);
357 /*************************************************************************
359 * Function: sql_finish_select_query
361 * Purpose: End the select query, such as freeing memory or result
363 *************************************************************************/
364 static sql_rcode_t sql_finish_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config) {
365 return sql_free_result(handle, config);
368 /*************************************************************************
370 * Function: sql_affected_rows
372 * Purpose: Return the number of rows affected by the query (update,
375 *************************************************************************/
376 static int sql_affected_rows(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config) {
379 rlm_sql_iodbc_conn_t *conn = handle->conn;
381 SQLRowCount(conn->stmt_handle, &count);
385 /* Exported to rlm_sql */
386 rlm_sql_module_t rlm_sql_iodbc = {
399 sql_finish_select_query,