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 {
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 sql_rcode_t 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));
86 * The db2 API doesn't qualify arguments as const even when they should be.
89 SQLCHAR *server, *login, *password;
91 memcpy(&server, &config->sql_server, sizeof(server));
92 memcpy(&login, &config->sql_login, sizeof(login));
93 memcpy(&password, &config->sql_password, sizeof(password));
95 retval = SQLConnect(conn->hdbc,
102 if(retval != SQL_SUCCESS) {
103 ERROR("could not connect to DB2 server %s", config->sql_server);
105 return RLM_SQL_ERROR;
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);
137 return RLM_SQL_ERROR;
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, i+1, SQL_DESC_DISPLAY_SIZE, NULL, 0, NULL, &len);
209 retval[i] = rad_malloc(len+1);
211 /* get the actual column */
212 SQLGetData(conn->stmt, i + 1, SQL_C_CHAR, retval[i], len+1, &slen);
213 if(slen == SQL_NULL_DATA) {
218 handle->row = retval;
222 for(i = 0; i < c; i++) {
227 return RLM_SQL_ERROR;
230 /*************************************************************************
232 * Function: sql_free_result
234 * Purpose: database specific free_result. Frees memory allocated
237 *************************************************************************/
238 static sql_rcode_t sql_free_result(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
240 rlm_sql_db2_conn_t *conn;
242 SQLFreeHandle(SQL_HANDLE_STMT, conn->stmt);
249 /*************************************************************************
251 * Function: sql_error
253 * Purpose: database specific error. Returns error associated with
256 *************************************************************************/
257 static char const *sql_error(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
259 /* this should really be enough, if not, you still got the sqlstate */
264 rlm_sql_db2_conn_t *conn = handle->conn;
266 TALLOC_FREE(conn->error);
267 SQLGetDiagRec(SQL_HANDLE_STMT, conn->stmt, 1, (SQLCHAR *) sqlstate, &err, (SQLCHAR *) msg, sizeof(msg), &rl);
268 conn->error = talloc_asprintf(conn, "sqlstate %s: %s", sqlstate, msg);
274 /*************************************************************************
276 * Function: sql_finish_query
278 * Purpose: End the query, such as freeing memory
280 *************************************************************************/
281 static sql_rcode_t sql_finish_query(UNUSED rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
288 /*************************************************************************
290 * Function: sql_finish_select_query
292 * Purpose: End the select query, such as freeing memory or result
294 *************************************************************************/
295 static sql_rcode_t sql_finish_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
297 return sql_finish_query(handle, config);
301 /*************************************************************************
303 * Function: sql_affected_rows
305 * Purpose: Return the number of rows affected by the last query.
307 *************************************************************************/
308 static int sql_affected_rows(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
311 rlm_sql_db2_conn_t *conn = handle->conn;
313 SQLRowCount(conn->stmt, &c);
318 /* Exported to rlm_sql */
319 rlm_sql_module_t rlm_sql_db2 = {
325 NULL, /* sql_store_result */
327 NULL, /* sql_num_rows */
332 sql_finish_select_query,