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>
33 #include <freeradius-devel/radiusd.h>
41 typedef struct rlm_sql_conn {
47 static int sql_socket_destructor(void *c)
49 rlm_sql_db2_conn_t *conn = c;
51 DEBUG2("rlm_sql_db2: Socket destructor called, closing socket");
54 SQLDisconnect(conn->hdbc);
55 SQLFreeHandle(SQL_HANDLE_DBC, conn->hdbc);
59 SQLFreeHandle(SQL_HANDLE_ENV, conn->henv);
65 /*************************************************************************
67 * Function: sql_create_socket
69 * Purpose: Establish connection to the db
71 *************************************************************************/
72 static sql_rcode_t sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
75 rlm_sql_db2_conn_t *conn;
77 MEM(conn = handle->conn = talloc_zero(handle, rlm_sql_db2_conn_t));
78 talloc_set_destructor((void *) conn, sql_socket_destructor);
80 /* allocate handles */
81 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &(conn->henv));
82 SQLAllocHandle(SQL_HANDLE_DBC, conn->henv, &(conn->hdbc));
85 * The db2 API doesn't qualify arguments as const even when they should be.
88 SQLCHAR *server, *login, *password;
90 memcpy(&server, &config->sql_server, sizeof(server));
91 memcpy(&login, &config->sql_login, sizeof(login));
92 memcpy(&password, &config->sql_password, sizeof(password));
94 retval = SQLConnect(conn->hdbc,
101 if(retval != SQL_SUCCESS) {
102 ERROR("could not connect to DB2 server %s\n",
111 /*************************************************************************
113 * Function: sql_query
115 * Purpose: Issue a query to the database
117 *************************************************************************/
118 static sql_rcode_t sql_query(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config, char const *query)
121 rlm_sql_db2_conn_t *conn;
125 /* allocate handle for statement */
126 SQLAllocHandle(SQL_HANDLE_STMT, conn->hdbc, &(conn->stmt));
131 memcpy(&db2_query, &query, sizeof(query));
133 retval = SQLExecDirect(conn->stmt, db2_query, SQL_NTS);
134 if(retval != SQL_SUCCESS) {
135 /* XXX Check if retval means we should return RLM_SQL_RECONNECT */
136 ERROR("Could not execute statement \"%s\"\n", query);
145 /*************************************************************************
147 * Function: sql_select_query
149 * Purpose: Issue a select query to the database
151 *************************************************************************/
152 static sql_rcode_t sql_select_query(rlm_sql_handle_t * handle, rlm_sql_config_t *config, char const *query)
154 return sql_query(handle, config, query);
158 /*************************************************************************
160 * Function: sql_num_fields
162 * Purpose: database specific num_fields function. Returns number
163 * of columns from query
165 *************************************************************************/
166 static int sql_num_fields(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
169 rlm_sql_db2_conn_t *conn;
172 SQLNumResultCols(conn->stmt, &c);
177 /*************************************************************************
179 * Function: sql_fetch_row
181 * Purpose: database specific fetch_row. Returns a rlm_sql_row_t struct
182 * with all the data for the query in 'handle->row'. Returns
183 * 0 on success, -1 on failure, RLM_SQL_RECONNECT if 'database is down'
185 *************************************************************************/
186 static sql_rcode_t sql_fetch_row(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
189 SQLINTEGER len, slen;
190 rlm_sql_row_t retval;
191 rlm_sql_db2_conn_t *conn;
195 c = sql_num_fields(handle, config);
196 retval = (rlm_sql_row_t)rad_malloc(c*sizeof(char*)+1);
197 memset(retval, 0, c*sizeof(char*)+1);
200 if(SQLFetch(conn->stmt) == SQL_NO_DATA_FOUND) {
205 for(i = 0; i < c; i++) {
206 /* get column length */
207 SQLColAttribute(conn->stmt,
208 i+1, SQL_DESC_DISPLAY_SIZE,
209 NULL, 0, NULL, &len);
210 retval[i] = (char*)rad_malloc(len+1);
211 /* get the actual column */
212 SQLGetData(conn->stmt,
213 i+1, SQL_C_CHAR, retval[i], len+1, &slen);
214 if(slen == SQL_NULL_DATA)
218 handle->row = retval;
222 for(i = 0; i < c; i++) {
229 /*************************************************************************
231 * Function: sql_free_result
233 * Purpose: database specific free_result. Frees memory allocated
236 *************************************************************************/
237 static sql_rcode_t sql_free_result(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
239 rlm_sql_db2_conn_t *conn;
241 SQLFreeHandle(SQL_HANDLE_STMT, conn->stmt);
247 /*************************************************************************
249 * Function: sql_error
251 * Purpose: database specific error. Returns error associated with
254 *************************************************************************/
255 static char const *sql_error(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
257 /* this should really be enough, if not, you still got the sqlstate */
264 rlm_sql_db2_conn_t *conn;
268 SQLGetDiagRec(SQL_HANDLE_STMT, conn->stmt, 1, (SQLCHAR *) sqlstate, &err, (SQLCHAR *) msg, MSGLEN, &rl);
270 retval = rad_malloc(strlen(msg)+20);
271 sprintf(retval, "SQLSTATE %s: %s", sqlstate, msg);
277 /*************************************************************************
279 * Function: sql_finish_query
281 * Purpose: End the query, such as freeing memory
283 *************************************************************************/
284 static sql_rcode_t sql_finish_query(UNUSED rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
291 /*************************************************************************
293 * Function: sql_finish_select_query
295 * Purpose: End the select query, such as freeing memory or result
297 *************************************************************************/
298 static sql_rcode_t sql_finish_select_query(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
300 return sql_finish_query(handle, config);
304 /*************************************************************************
306 * Function: sql_affected_rows
308 * Purpose: Return the number of rows affected by the last query.
310 *************************************************************************/
311 static int sql_affected_rows(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
314 rlm_sql_db2_conn_t *conn;
318 SQLRowCount(conn->stmt, &c);
324 not_implemented(UNUSED rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
326 ERROR("sql_db2: calling unimplemented function");
331 /* Exported to rlm_sql */
332 rlm_sql_module_t rlm_sql_db2 = {
338 not_implemented, /* sql_store_result */
340 not_implemented, /* sql_num_rows */
345 sql_finish_select_query,