2 * sql_db2.c IBM DB2 rlm_sql driver
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 Mike Machado <mike@innercite.com>
22 * Copyright 2000 Alan DeKok <aland@ox.org>
23 * Copyright 2001 Joerg Wendland <wendland@scan-plus.de>
27 * Modification of rlm_sql_db2 to handle IBM DB2 UDB V7
28 * by Joerg Wendland <wendland@scan-plus.de>
31 #include <freeradius-devel/ident.h>
34 #include <freeradius-devel/radiusd.h>
42 typedef struct rlm_sql_conn {
48 static int sql_socket_destructor(void *c)
50 rlm_sql_db2_conn_t *conn = c;
52 DEBUG2("rlm_sql_db2: Socket destructor called, closing socket");
55 SQLDisconnect(conn->hdbc);
56 SQLFreeHandle(SQL_HANDLE_DBC, conn->hdbc);
60 SQLFreeHandle(SQL_HANDLE_ENV, conn->henv);
66 /*************************************************************************
68 * Function: sql_create_socket
70 * Purpose: Establish connection to the db
72 *************************************************************************/
73 static int sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
76 rlm_sql_db2_conn_t *conn;
78 MEM(conn = handle->conn = talloc_zero(handle, rlm_sql_db2_conn_t));
79 talloc_set_destructor((void *) conn, sql_socket_destructor);
81 /* allocate handles */
82 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &(conn->henv));
83 SQLAllocHandle(SQL_HANDLE_DBC, conn->henv, &(conn->hdbc));
85 /* connect to database */
86 retval = SQLConnect(conn->hdbc,
87 config->sql_server, SQL_NTS,
88 config->sql_login, SQL_NTS,
89 config->sql_password, SQL_NTS);
90 if(retval != SQL_SUCCESS) {
91 radlog(L_ERR, "could not connect to DB2 server %s\n",
100 /*************************************************************************
102 * Function: sql_query
104 * Purpose: Issue a query to the database
106 *************************************************************************/
107 static int sql_query(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config,
111 rlm_sql_db2_conn_t *conn;
115 /* allocate handle for statement */
116 SQLAllocHandle(SQL_HANDLE_STMT, conn->hdbc,
120 retval = SQLExecDirect(conn->stmt, querystr, SQL_NTS);
121 if(retval != SQL_SUCCESS) {
122 /* XXX Check if retval means we should return SQL_DOWN */
123 radlog(L_ERR, "could not execute statement \"%s\"\n", querystr);
131 /*************************************************************************
133 * Function: sql_select_query
135 * Purpose: Issue a select query to the database
137 *************************************************************************/
138 static int sql_select_query(rlm_sql_handle_t * handle, rlm_sql_config_t *config, char *querystr)
140 return sql_query(handle, config, querystr);
144 /*************************************************************************
146 * Function: sql_num_fields
148 * Purpose: database specific num_fields function. Returns number
149 * of columns from query
151 *************************************************************************/
152 static int sql_num_fields(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
155 rlm_sql_db2_conn_t *conn;
158 SQLNumResultCols(conn->stmt, &c);
163 /*************************************************************************
165 * Function: sql_fetch_row
167 * Purpose: database specific fetch_row. Returns a rlm_sql_row_t struct
168 * with all the data for the query in 'handle->row'. Returns
169 * 0 on success, -1 on failure, SQL_DOWN if 'database is down'
171 *************************************************************************/
172 static int sql_fetch_row(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
175 SQLINTEGER len, slen;
176 rlm_sql_row_t retval;
177 rlm_sql_db2_conn_t *conn;
181 c = sql_num_fields(handle, config);
182 retval = (rlm_sql_row_t)rad_malloc(c*sizeof(char*)+1);
183 memset(retval, 0, c*sizeof(char*)+1);
186 if(SQLFetch(conn->stmt) == SQL_NO_DATA_FOUND) {
191 for(i = 0; i < c; i++) {
192 /* get column length */
193 SQLColAttribute(conn->stmt,
194 i+1, SQL_DESC_DISPLAY_SIZE,
195 NULL, 0, NULL, &len);
196 retval[i] = (char*)rad_malloc(len+1);
197 /* get the actual column */
198 SQLGetData(conn->stmt,
199 i+1, SQL_C_CHAR, retval[i], len+1, &slen);
200 if(slen == SQL_NULL_DATA)
204 handle->row = retval;
208 for(i = 0; i < c; i++) {
215 /*************************************************************************
217 * Function: sql_free_result
219 * Purpose: database specific free_result. Frees memory allocated
222 *************************************************************************/
223 static int sql_free_result(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
225 rlm_sql_db2_conn_t *conn;
227 SQLFreeHandle(SQL_HANDLE_STMT, conn->stmt);
233 /*************************************************************************
235 * Function: sql_error
237 * Purpose: database specific error. Returns error associated with
240 *************************************************************************/
241 static const char *sql_error(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
243 /* this should really be enough, if not, you still got the sqlstate */
250 rlm_sql_db2_conn_t *conn;
254 SQLGetDiagRec(SQL_HANDLE_STMT, conn->stmt,
255 1, sqlstate, &err, msg, MSGLEN, &rl);
256 retval = (char*)rad_malloc(strlen(msg)+20);
257 sprintf(retval, "SQLSTATE %s: %s", sqlstate, msg);
262 /*************************************************************************
264 * Function: sql_finish_query
266 * Purpose: End the query, such as freeing memory
268 *************************************************************************/
269 static int sql_finish_query(UNUSED rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
276 /*************************************************************************
278 * Function: sql_finish_select_query
280 * Purpose: End the select query, such as freeing memory or result
282 *************************************************************************/
283 static int sql_finish_select_query(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
285 return sql_finish_query(handle, config);
289 /*************************************************************************
291 * Function: sql_affected_rows
293 * Purpose: Return the number of rows affected by the last query.
295 *************************************************************************/
296 static int sql_affected_rows(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
299 rlm_sql_db2_conn_t *conn;
303 SQLRowCount(conn->stmt, &c);
309 not_implemented(UNUSED rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
311 radlog(L_ERR, "sql_db2: calling unimplemented function");
316 /* Exported to rlm_sql */
317 rlm_sql_module_t rlm_sql_db2 = {
323 not_implemented, /* sql_store_result */
325 not_implemented, /* sql_num_rows */
330 sql_finish_select_query,