Fix compile warnings
[freeradius.git] / src / modules / rlm_sql / drivers / rlm_sql_db2 / rlm_sql_db2.c
1 /*
2  * sql_db2.c            IBM DB2 rlm_sql driver
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
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>
24  */
25
26 /*
27  * Modification of rlm_sql_db2 to handle IBM DB2 UDB V7
28  * by Joerg Wendland <wendland@scan-plus.de>
29  */
30
31 #include <freeradius-devel/ident.h>
32 RCSID("$Id$")
33
34 #include <freeradius-devel/radiusd.h>
35
36 #include <sys/stat.h>
37
38 #include <sql.h>
39 #include <sqlcli.h>
40 #include "rlm_sql.h"
41
42 typedef struct rlm_sql_db2_sock {
43         SQLHANDLE hdbc;
44         SQLHANDLE henv;
45         SQLHANDLE stmt;
46 } rlm_sql_db2_sock;
47
48 /*************************************************************************
49  *
50  *      Function: sql_create_socket
51  *
52  *      Purpose: Establish connection to the db
53  *
54  *************************************************************************/
55 static int sql_init_socket(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
56 {
57         SQLRETURN retval;
58         rlm_sql_db2_sock *sock;
59
60         /* allocate socket */
61         if (!handle->conn) {
62                 handle->conn = (rlm_sql_db2_sock*)rad_malloc(sizeof(rlm_sql_db2_sock));
63                 if (!handle->conn) {
64                         return -1;
65                 }
66         }
67         sock = handle->conn;
68         memset(sock, 0, sizeof(*sock));
69
70         /* allocate handles */
71         SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &(sock->henv));
72         SQLAllocHandle(SQL_HANDLE_DBC, sock->henv, &(sock->hdbc));
73
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);
84                 return -1;
85         }
86
87         return 0;
88 }
89
90
91 /*************************************************************************
92  *
93  *      Function: sql_destroy_socket
94  *
95  *      Purpose: Free socket and private connection data
96  *
97  *************************************************************************/
98 static int sql_destroy_socket(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
99 {
100         free(handle->conn);
101         handle->conn = NULL;
102         return 0;
103 }
104
105 /*************************************************************************
106  *
107  *      Function: sql_query
108  *
109  *      Purpose: Issue a query to the database
110  *
111  *************************************************************************/
112 static int sql_query(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config, char *querystr)
113 {
114         SQLRETURN retval;
115         rlm_sql_db2_sock *sock;
116
117         sock = handle->conn;
118
119         /* allocate handle for statement */
120         SQLAllocHandle(SQL_HANDLE_STMT, sock->hdbc,
121                         &(sock->stmt));
122
123         /* execute query */
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);
128                 return -1;
129         }
130
131         return 0;
132 }
133
134
135 /*************************************************************************
136  *
137  *      Function: sql_select_query
138  *
139  *      Purpose: Issue a select query to the database
140  *
141  *************************************************************************/
142 static int sql_select_query(rlm_sql_handle_t * handle, rlm_sql_config_t *config, char *querystr)
143 {
144         return sql_query(handle, config, querystr);
145 }
146
147
148 /*************************************************************************
149  *
150  *      Function: sql_num_fields
151  *
152  *      Purpose: database specific num_fields function. Returns number
153  *               of columns from query
154  *
155  *************************************************************************/
156 static int sql_num_fields(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
157 {
158         SQLSMALLINT c;
159         rlm_sql_db2_sock *sock;
160
161         sock = handle->conn;
162         SQLNumResultCols(sock->stmt, &c);
163         return c;
164 }
165
166
167 /*************************************************************************
168  *
169  *      Function: sql_fetch_row
170  *
171  *      Purpose: database specific fetch_row. Returns a rlm_sql_row_t struct
172  *               with all the data for the query in 'handle->row'. Returns
173  *               0 on success, -1 on failure, SQL_DOWN if 'database is down'
174  *
175  *************************************************************************/
176 static int sql_fetch_row(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
177 {
178         int c, i;
179         SQLINTEGER len, slen;
180         rlm_sql_row_t retval;
181         rlm_sql_db2_sock *sock;
182
183         sock = handle->conn;
184
185         c = sql_num_fields(handle, config);
186         retval = (rlm_sql_row_t)rad_malloc(c*sizeof(char*)+1);
187         memset(retval, 0, c*sizeof(char*)+1);
188
189         /* advance cursor */
190         if(SQLFetch(sock->stmt) == SQL_NO_DATA_FOUND) {
191                 handle->row = NULL;
192                 goto error;
193         }
194
195         for(i = 0; i < c; i++) {
196                 /* get column length */
197                 SQLColAttribute(sock->stmt,
198                                 i+1, SQL_DESC_DISPLAY_SIZE,
199                                 NULL, 0, NULL, &len);
200                 retval[i] = (char*)rad_malloc(len+1);
201                 /* get the actual column */
202                 SQLGetData(sock->stmt,
203                                 i+1, SQL_C_CHAR, retval[i], len+1, &slen);
204                 if(slen == SQL_NULL_DATA)
205                         retval[i][0] = '\0';
206         }
207
208         handle->row = retval;
209         return 0;
210
211 error:
212         for(i = 0; i < c; i++) {
213                 free(retval[i]);
214         }
215         free(retval);
216         return -1;
217 }
218
219 /*************************************************************************
220  *
221  *      Function: sql_free_result
222  *
223  *      Purpose: database specific free_result. Frees memory allocated
224  *               for a result set
225  *
226  *************************************************************************/
227 static int sql_free_result(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
228 {
229         rlm_sql_db2_sock *sock;
230         sock = handle->conn;
231         SQLFreeHandle(SQL_HANDLE_STMT, sock->stmt);
232         return 0;
233 }
234
235
236
237 /*************************************************************************
238  *
239  *      Function: sql_error
240  *
241  *      Purpose: database specific error. Returns error associated with
242  *               connection
243  *
244  *************************************************************************/
245 static const char *sql_error(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
246 {
247         /* this should really be enough, if not, you still got the sqlstate */
248 #define MSGLEN 512
249         char sqlstate[6];
250         char msg[MSGLEN];
251         char *retval;
252         SQLINTEGER err;
253         SQLSMALLINT rl;
254         rlm_sql_db2_sock *sock;
255
256         sock = handle->conn;
257
258         SQLGetDiagRec(SQL_HANDLE_STMT, sock->stmt,
259                         1, sqlstate, &err, msg, MSGLEN, &rl);
260         retval = (char*)rad_malloc(strlen(msg)+20);
261         sprintf(retval, "SQLSTATE %s: %s", sqlstate, msg);
262         return retval;
263 }
264
265
266 /*************************************************************************
267  *
268  *      Function: sql_close
269  *
270  *      Purpose: database specific close. Closes an open database
271  *               connection
272  *
273  *************************************************************************/
274 static int sql_close(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
275 {
276         rlm_sql_db2_sock *sock;
277
278         sock = handle->conn;
279
280         SQLFreeHandle(SQL_HANDLE_DBC, sock->hdbc);
281         SQLFreeHandle(SQL_HANDLE_ENV, sock->henv);
282         return 0;
283 }
284
285
286 /*************************************************************************
287  *
288  *      Function: sql_finish_query
289  *
290  *      Purpose: End the query, such as freeing memory
291  *
292  *************************************************************************/
293 static int sql_finish_query(UNUSED rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
294 {
295         return 0;
296 }
297
298
299
300 /*************************************************************************
301  *
302  *      Function: sql_finish_select_query
303  *
304  *      Purpose: End the select query, such as freeing memory or result
305  *
306  *************************************************************************/
307 static int sql_finish_select_query(rlm_sql_handle_t * handle, rlm_sql_config_t *config)
308 {
309         return sql_finish_query(handle, config);
310 }
311
312
313 /*************************************************************************
314  *
315  *      Function: sql_affected_rows
316  *
317  *      Purpose: Return the number of rows affected by the last query.
318  *
319  *************************************************************************/
320 static int sql_affected_rows(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
321 {
322         SQLINTEGER c;
323         rlm_sql_db2_sock *sock;
324
325         sock = handle->conn;
326
327         SQLRowCount(sock->stmt, &c);
328         return c;
329 }
330
331
332 static int
333 not_implemented(UNUSED rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config)
334 {
335         radlog(L_ERR, "sql_db2: calling unimplemented function");
336         exit(1);
337 }
338
339
340 /* Exported to rlm_sql */
341 rlm_sql_module_t rlm_sql_db2 = {
342         "rlm_sql_db2",
343         sql_init_socket,
344         sql_destroy_socket, /* sql_destroy_socket*/
345         sql_query,
346         sql_select_query,
347         not_implemented, /* sql_store_result */
348         sql_num_fields,
349         not_implemented, /* sql_num_rows */
350         sql_fetch_row,
351         sql_free_result,
352         sql_error,
353         sql_close,
354         sql_finish_query,
355         sql_finish_select_query,
356         sql_affected_rows,
357 };