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 int 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));
84 /* connect to database */
85 retval = SQLConnect(conn->hdbc,
86 config->sql_server, SQL_NTS,
87 config->sql_login, SQL_NTS,
88 config->sql_password, SQL_NTS);
89 if(retval != SQL_SUCCESS) {
90 radlog(L_ERR, "could not connect to DB2 server %s\n",
99 /*************************************************************************
101 * Function: sql_query
103 * Purpose: Issue a query to the database
105 *************************************************************************/
106 static int sql_query(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config,
110 rlm_sql_db2_conn_t *conn;
114 /* allocate handle for statement */
115 SQLAllocHandle(SQL_HANDLE_STMT, conn->hdbc,
119 retval = SQLExecDirect(conn->stmt, querystr, SQL_NTS);
120 if(retval != SQL_SUCCESS) {
121 /* XXX Check if retval means we should return SQL_DOWN */
122 radlog(L_ERR, "could not execute statement \"%s\"\n", querystr);
130 /*************************************************************************
132 * Function: sql_select_query
134 * Purpose: Issue a select query to the database
136 *************************************************************************/
137 static int sql_select_query(rlm_sql_handle_t * handle, rlm_sql_config_t *config, char *querystr)
139 return sql_query(handle, config, querystr);
143 /*************************************************************************
145 * Function: sql_num_fields
147 * Purpose: database specific num_fields function. Returns number
148 * of columns from query
150 *************************************************************************/
151 static int sql_num_fields(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
154 rlm_sql_db2_conn_t *conn;
157 SQLNumResultCols(conn->stmt, &c);
162 /*************************************************************************
164 * Function: sql_fetch_row
166 * Purpose: database specific fetch_row. Returns a rlm_sql_row_t struct
167 * with all the data for the query in 'handle->row'. Returns
168 * 0 on success, -1 on failure, SQL_DOWN if 'database is down'
170 *************************************************************************/
171 static int sql_fetch_row(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
174 SQLINTEGER len, slen;
175 rlm_sql_row_t retval;
176 rlm_sql_db2_conn_t *conn;
180 c = sql_num_fields(handle, config);
181 retval = (rlm_sql_row_t)rad_malloc(c*sizeof(char*)+1);
182 memset(retval, 0, c*sizeof(char*)+1);
185 if(SQLFetch(conn->stmt) == SQL_NO_DATA_FOUND) {
190 for(i = 0; i < c; i++) {
191 /* get column length */
192 SQLColAttribute(conn->stmt,
193 i+1, SQL_DESC_DISPLAY_SIZE,
194 NULL, 0, NULL, &len);
195 retval[i] = (char*)rad_malloc(len+1);
196 /* get the actual column */
197 SQLGetData(conn->stmt,
198 i+1, SQL_C_CHAR, retval[i], len+1, &slen);
199 if(slen == SQL_NULL_DATA)
203 handle->row = retval;
207 for(i = 0; i < c; i++) {
214 /*************************************************************************
216 * Function: sql_free_result
218 * Purpose: database specific free_result. Frees memory allocated
221 *************************************************************************/
222 static int sql_free_result(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
224 rlm_sql_db2_conn_t *conn;
226 SQLFreeHandle(SQL_HANDLE_STMT, conn->stmt);
232 /*************************************************************************
234 * Function: sql_error
236 * Purpose: database specific error. Returns error associated with
239 *************************************************************************/
240 static const char *sql_error(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
242 /* this should really be enough, if not, you still got the sqlstate */
249 rlm_sql_db2_conn_t *conn;
253 SQLGetDiagRec(SQL_HANDLE_STMT, conn->stmt,
254 1, sqlstate, &err, msg, MSGLEN, &rl);
255 retval = (char*)rad_malloc(strlen(msg)+20);
256 sprintf(retval, "SQLSTATE %s: %s", sqlstate, msg);
261 /*************************************************************************
263 * Function: sql_finish_query
265 * Purpose: End the query, such as freeing memory
267 *************************************************************************/
268 static int sql_finish_query(UNUSED rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
275 /*************************************************************************
277 * Function: sql_finish_select_query
279 * Purpose: End the select query, such as freeing memory or result
281 *************************************************************************/
282 static int sql_finish_select_query(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
284 return sql_finish_query(handle, config);
288 /*************************************************************************
290 * Function: sql_affected_rows
292 * Purpose: Return the number of rows affected by the last query.
294 *************************************************************************/
295 static int sql_affected_rows(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
298 rlm_sql_db2_conn_t *conn;
302 SQLRowCount(conn->stmt, &c);
308 not_implemented(UNUSED rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
310 radlog(L_ERR, "sql_db2: calling unimplemented function");
315 /* Exported to rlm_sql */
316 rlm_sql_module_t rlm_sql_db2 = {
322 not_implemented, /* sql_store_result */
324 not_implemented, /* sql_num_rows */
329 sql_finish_select_query,