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(rlm_sql_db2_conn_t *conn)
50 DEBUG2("rlm_sql_db2: Socket destructor called, closing socket");
53 SQLDisconnect(conn->hdbc);
54 SQLFreeHandle(SQL_HANDLE_DBC, conn->hdbc);
58 SQLFreeHandle(SQL_HANDLE_ENV, conn->henv);
64 /*************************************************************************
66 * Function: sql_create_socket
68 * Purpose: Establish connection to the db
70 *************************************************************************/
71 static sql_rcode_t sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
74 rlm_sql_db2_conn_t *conn;
76 MEM(conn = handle->conn = talloc_zero(handle, rlm_sql_db2_conn_t));
77 talloc_set_destructor(conn, _sql_socket_destructor);
79 /* allocate handles */
80 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &(conn->henv));
81 SQLAllocHandle(SQL_HANDLE_DBC, conn->henv, &(conn->hdbc));
84 * The db2 API doesn't qualify arguments as const even when they should be.
87 SQLCHAR *server, *login, *password;
89 memcpy(&server, &config->sql_server, sizeof(server));
90 memcpy(&login, &config->sql_login, sizeof(login));
91 memcpy(&password, &config->sql_password, sizeof(password));
93 retval = SQLConnect(conn->hdbc,
100 if(retval != SQL_SUCCESS) {
101 ERROR("could not connect to DB2 server %s", config->sql_server);
103 return RLM_SQL_ERROR;
109 /*************************************************************************
111 * Function: sql_query
113 * Purpose: Issue a query to the database
115 *************************************************************************/
116 static sql_rcode_t sql_query(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config, char const *query)
119 rlm_sql_db2_conn_t *conn;
123 /* allocate handle for statement */
124 SQLAllocHandle(SQL_HANDLE_STMT, conn->hdbc, &(conn->stmt));
129 memcpy(&db2_query, &query, sizeof(query));
131 retval = SQLExecDirect(conn->stmt, db2_query, SQL_NTS);
132 if(retval != SQL_SUCCESS) {
133 /* XXX Check if retval means we should return RLM_SQL_RECONNECT */
134 ERROR("Could not execute statement \"%s\"\n", query);
135 return RLM_SQL_ERROR;
143 /*************************************************************************
145 * Function: sql_select_query
147 * Purpose: Issue a select query to the database
149 *************************************************************************/
150 static sql_rcode_t sql_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char const *query)
152 return sql_query(handle, config, query);
156 /*************************************************************************
158 * Function: sql_num_fields
160 * Purpose: database specific num_fields function. Returns number
161 * of columns from query
163 *************************************************************************/
164 static int sql_num_fields(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
167 rlm_sql_db2_conn_t *conn;
170 SQLNumResultCols(conn->stmt, &c);
175 /*************************************************************************
177 * Function: sql_fetch_row
179 * Purpose: database specific fetch_row. Returns a rlm_sql_row_t struct
180 * with all the data for the query in 'handle->row'. Returns
181 * 0 on success, -1 on failure, RLM_SQL_RECONNECT if 'database is down'
183 *************************************************************************/
184 static sql_rcode_t sql_fetch_row(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
187 SQLINTEGER len, slen;
188 rlm_sql_row_t retval;
189 rlm_sql_db2_conn_t *conn;
193 c = sql_num_fields(handle, config);
194 retval = (rlm_sql_row_t)rad_malloc(c*sizeof(char*)+1);
195 memset(retval, 0, c*sizeof(char*)+1);
198 if(SQLFetch(conn->stmt) == SQL_NO_DATA_FOUND) {
203 for(i = 0; i < c; i++) {
204 /* get column length */
205 SQLColAttribute(conn->stmt, i+1, SQL_DESC_DISPLAY_SIZE, NULL, 0, NULL, &len);
207 retval[i] = rad_malloc(len+1);
209 /* get the actual column */
210 SQLGetData(conn->stmt, i + 1, SQL_C_CHAR, retval[i], len+1, &slen);
211 if(slen == SQL_NULL_DATA) {
216 handle->row = retval;
220 for(i = 0; i < c; i++) {
225 return RLM_SQL_ERROR;
228 /*************************************************************************
230 * Function: sql_free_result
232 * Purpose: database specific free_result. Frees memory allocated
235 *************************************************************************/
236 static sql_rcode_t sql_free_result(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
238 rlm_sql_db2_conn_t *conn;
240 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 */
262 rlm_sql_db2_conn_t *conn = handle->conn;
264 TALLOC_FREE(conn->error);
265 SQLGetDiagRec(SQL_HANDLE_STMT, conn->stmt, 1, (SQLCHAR *) sqlstate, &err, (SQLCHAR *) msg, sizeof(msg), &rl);
266 conn->error = talloc_typed_asprintf(conn, "sqlstate %s: %s", sqlstate, msg);
272 /*************************************************************************
274 * Function: sql_finish_query
276 * Purpose: End the query, such as freeing memory
278 *************************************************************************/
279 static sql_rcode_t sql_finish_query(UNUSED rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
286 /*************************************************************************
288 * Function: sql_finish_select_query
290 * Purpose: End the select query, such as freeing memory or result
292 *************************************************************************/
293 static sql_rcode_t sql_finish_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
295 return sql_finish_query(handle, config);
299 /*************************************************************************
301 * Function: sql_affected_rows
303 * Purpose: Return the number of rows affected by the last query.
305 *************************************************************************/
306 static int sql_affected_rows(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
309 rlm_sql_db2_conn_t *conn = handle->conn;
311 SQLRowCount(conn->stmt, &c);
316 /* Exported to rlm_sql */
317 rlm_sql_module_t rlm_sql_db2 = {
323 NULL, /* sql_store_result */
325 NULL, /* sql_num_rows */
330 sql_finish_select_query,