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_db2_sock {
48 /*************************************************************************
50 * Function: sql_create_socket
52 * Purpose: Establish connection to the db
54 *************************************************************************/
55 static int sql_init_socket(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
58 rlm_sql_db2_sock *sock;
62 handle->conn = (rlm_sql_db2_sock*)rad_malloc(sizeof(rlm_sql_db2_sock));
68 memset(sock, 0, sizeof(*sock));
70 /* allocate handles */
71 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &(sock->henv));
72 SQLAllocHandle(SQL_HANDLE_DBC, sock->henv, &(sock->hdbc));
74 /* connect to database */
75 retval = SQLConnect(sock->hdbc,
76 config->sql_server, SQL_NTS,
77 config->sql_login, SQL_NTS,
78 config->sql_password, SQL_NTS);
79 if(retval != SQL_SUCCESS) {
80 radlog(L_ERR, "could not connect to DB2 server %s\n", config->sql_server);
81 SQLDisconnect(sock->hdbc);
82 SQLFreeHandle(SQL_HANDLE_DBC, sock->hdbc);
83 SQLFreeHandle(SQL_HANDLE_ENV, sock->henv);
91 /*************************************************************************
93 * Function: sql_destroy_socket
95 * Purpose: Free socket and private connection data
97 *************************************************************************/
98 static int sql_destroy_socket(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
105 /*************************************************************************
107 * Function: sql_query
109 * Purpose: Issue a query to the database
111 *************************************************************************/
112 static int sql_query(rlm_sql_handle_t * handle, rlm_sql_config_t *config, char *querystr)
115 rlm_sql_db2_sock *sock;
119 /* allocate handle for statement */
120 SQLAllocHandle(SQL_HANDLE_STMT, sock->hdbc,
124 retval = SQLExecDirect(sock->stmt, querystr, SQL_NTS);
125 if(retval != SQL_SUCCESS) {
126 /* XXX Check if retval means we should return SQL_DOWN */
127 radlog(L_ERR, "could not execute statement \"%s\"\n", querystr);
135 /*************************************************************************
137 * Function: sql_select_query
139 * Purpose: Issue a select query to the database
141 *************************************************************************/
142 static int sql_select_query(rlm_sql_handle_t * handle, rlm_sql_config_t *config, char *querystr)
144 return sql_query(handle, config, querystr);
148 /*************************************************************************
150 * Function: sql_store_result
152 * Purpose: database specific store_result function. Returns a result
155 *************************************************************************/
156 static int sql_store_result(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
162 /*************************************************************************
164 * Function: sql_num_fields
166 * Purpose: database specific num_fields function. Returns number
167 * of columns from query
169 *************************************************************************/
170 static int sql_num_fields(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
173 rlm_sql_db2_sock *sock;
176 SQLNumResultCols(sock->stmt, &c);
181 /*************************************************************************
183 * Function: sql_fetch_row
185 * Purpose: database specific fetch_row. Returns a rlm_sql_row_t struct
186 * with all the data for the query in 'handle->row'. Returns
187 * 0 on success, -1 on failure, SQL_DOWN if 'database is down'
189 *************************************************************************/
190 static int sql_fetch_row(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
193 SQLINTEGER len, slen;
194 rlm_sql_row_t retval;
195 rlm_sql_db2_sock *sock;
199 c = sql_num_fields(handle, config);
200 retval = (rlm_sql_row_t)rad_malloc(c*sizeof(char*)+1);
201 memset(retval, 0, c*sizeof(char*)+1);
204 if(SQLFetch(sock->stmt) == SQL_NO_DATA_FOUND) {
209 for(i = 0; i < c; i++) {
210 /* get column length */
211 SQLColAttribute(sock->stmt,
212 i+1, SQL_DESC_DISPLAY_SIZE,
213 NULL, 0, NULL, &len);
214 retval[i] = (char*)rad_malloc(len+1);
215 /* get the actual column */
216 SQLGetData(sock->stmt,
217 i+1, SQL_C_CHAR, retval[i], len+1, &slen);
218 if(slen == SQL_NULL_DATA)
222 handle->row = retval;
226 for(i = 0; i < c; i++) {
233 /*************************************************************************
235 * Function: sql_free_result
237 * Purpose: database specific free_result. Frees memory allocated
240 *************************************************************************/
241 static int sql_free_result(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
243 rlm_sql_db2_sock *sock;
245 SQLFreeHandle(SQL_HANDLE_STMT, sock->stmt);
251 /*************************************************************************
253 * Function: sql_error
255 * Purpose: database specific error. Returns error associated with
258 *************************************************************************/
259 static char *sql_error(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
261 /* this should really be enough, if not, you still got the sqlstate */
268 rlm_sql_db2_sock *sock;
272 SQLGetDiagRec(SQL_HANDLE_STMT, sock->stmt,
273 1, sqlstate, &err, msg, MSGLEN, &rl);
274 retval = (char*)rad_malloc(strlen(msg)+20);
275 sprintf(retval, "SQLSTATE %s: %s", sqlstate, msg);
280 /*************************************************************************
282 * Function: sql_close
284 * Purpose: database specific close. Closes an open database
287 *************************************************************************/
288 static int sql_close(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
290 rlm_sql_db2_sock *sock;
294 SQLFreeHandle(SQL_HANDLE_DBC, sock->hdbc);
295 SQLFreeHandle(SQL_HANDLE_ENV, sock->henv);
300 /*************************************************************************
302 * Function: sql_finish_query
304 * Purpose: End the query, such as freeing memory
306 *************************************************************************/
307 static int sql_finish_query(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
314 /*************************************************************************
316 * Function: sql_finish_select_query
318 * Purpose: End the select query, such as freeing memory or result
320 *************************************************************************/
321 static int sql_finish_select_query(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
323 return sql_finish_query(handle, config);
327 /*************************************************************************
329 * Function: sql_affected_rows
331 * Purpose: Return the number of rows affected by the last query.
333 *************************************************************************/
334 static int sql_affected_rows(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
337 rlm_sql_db2_sock *sock;
341 SQLRowCount(sock->stmt, &c);
347 not_implemented(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
349 radlog(L_ERR, "sql_db2: calling unimplemented function");
354 /* Exported to rlm_sql */
355 rlm_sql_module_t rlm_sql_db2 = {
358 sql_destroy_socket, /* sql_destroy_socket*/
361 not_implemented, /* sql_store_result */
363 not_implemented, /* sql_num_rows */
369 sql_finish_select_query,