/
rlm_sql_db2.c
329 lines (270 loc) · 8.73 KB
/
rlm_sql_db2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* sql_db2.c IBM DB2 rlm_sql driver
*
* Version: $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2000,2006 The FreeRADIUS server project
* Copyright 2000 Mike Machado <mike@innercite.com>
* Copyright 2000 Alan DeKok <aland@ox.org>
* Copyright 2001 Joerg Wendland <wendland@scan-plus.de>
*/
/*
* Modification of rlm_sql_db2 to handle IBM DB2 UDB V7
* by Joerg Wendland <wendland@scan-plus.de>
*/
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <sys/stat.h>
#include <sql.h>
#include <sqlcli.h>
#include "rlm_sql.h"
typedef struct rlm_sql_conn {
SQLHANDLE hdbc;
SQLHANDLE henv;
SQLHANDLE stmt;
char *error;
} rlm_sql_db2_conn_t;
static int _sql_socket_destructor(rlm_sql_db2_conn_t *conn)
{
DEBUG2("rlm_sql_db2: Socket destructor called, closing socket");
if (conn->hdbc) {
SQLDisconnect(conn->hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, conn->hdbc);
}
if (conn->henv) {
SQLFreeHandle(SQL_HANDLE_ENV, conn->henv);
}
return RLM_SQL_OK;
}
/*************************************************************************
*
* Function: sql_create_socket
*
* Purpose: Establish connection to the db
*
*************************************************************************/
static sql_rcode_t sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
{
SQLRETURN retval;
rlm_sql_db2_conn_t *conn;
MEM(conn = handle->conn = talloc_zero(handle, rlm_sql_db2_conn_t));
talloc_set_destructor(conn, _sql_socket_destructor);
/* allocate handles */
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &(conn->henv));
SQLAllocHandle(SQL_HANDLE_DBC, conn->henv, &(conn->hdbc));
/*
* The db2 API doesn't qualify arguments as const even when they should be.
*/
{
SQLCHAR *server, *login, *password;
memcpy(&server, &config->sql_server, sizeof(server));
memcpy(&login, &config->sql_login, sizeof(login));
memcpy(&password, &config->sql_password, sizeof(password));
retval = SQLConnect(conn->hdbc,
server, SQL_NTS,
login, SQL_NTS,
password, SQL_NTS);
}
if(retval != SQL_SUCCESS) {
ERROR("could not connect to DB2 server %s", config->sql_server);
return RLM_SQL_ERROR;
}
return RLM_SQL_OK;
}
/*************************************************************************
*
* Function: sql_query
*
* Purpose: Issue a query to the database
*
*************************************************************************/
static sql_rcode_t sql_query(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config, char const *query)
{
SQLRETURN retval;
rlm_sql_db2_conn_t *conn;
conn = handle->conn;
/* allocate handle for statement */
SQLAllocHandle(SQL_HANDLE_STMT, conn->hdbc, &(conn->stmt));
/* execute query */
{
SQLCHAR *db2_query;
memcpy(&db2_query, &query, sizeof(query));
retval = SQLExecDirect(conn->stmt, db2_query, SQL_NTS);
if(retval != SQL_SUCCESS) {
/* XXX Check if retval means we should return RLM_SQL_RECONNECT */
ERROR("Could not execute statement \"%s\"\n", query);
return RLM_SQL_ERROR;
}
}
return RLM_SQL_OK;
}
/*************************************************************************
*
* Function: sql_select_query
*
* Purpose: Issue a select query to the database
*
*************************************************************************/
static sql_rcode_t sql_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char const *query)
{
return sql_query(handle, config, query);
}
/*************************************************************************
*
* Function: sql_num_fields
*
* Purpose: database specific num_fields function. Returns number
* of columns from query
*
*************************************************************************/
static int sql_num_fields(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
{
SQLSMALLINT c;
rlm_sql_db2_conn_t *conn;
conn = handle->conn;
SQLNumResultCols(conn->stmt, &c);
return c;
}
/*************************************************************************
*
* Function: sql_fetch_row
*
* Purpose: database specific fetch_row. Returns a rlm_sql_row_t struct
* with all the data for the query in 'handle->row'. Returns
* 0 on success, -1 on failure, RLM_SQL_RECONNECT if 'database is down'
*
*************************************************************************/
static sql_rcode_t sql_fetch_row(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
{
int c, i;
SQLINTEGER len, slen;
rlm_sql_row_t retval;
rlm_sql_db2_conn_t *conn;
conn = handle->conn;
c = sql_num_fields(handle, config);
retval = (rlm_sql_row_t)rad_malloc(c*sizeof(char*)+1);
memset(retval, 0, c*sizeof(char*)+1);
/* advance cursor */
if(SQLFetch(conn->stmt) == SQL_NO_DATA_FOUND) {
handle->row = NULL;
goto error;
}
for(i = 0; i < c; i++) {
/* get column length */
SQLColAttribute(conn->stmt, i+1, SQL_DESC_DISPLAY_SIZE, NULL, 0, NULL, &len);
retval[i] = rad_malloc(len+1);
/* get the actual column */
SQLGetData(conn->stmt, i + 1, SQL_C_CHAR, retval[i], len+1, &slen);
if(slen == SQL_NULL_DATA) {
retval[i][0] = '\0';
}
}
handle->row = retval;
return RLM_SQL_OK;
error:
for(i = 0; i < c; i++) {
free(retval[i]);
}
free(retval);
return RLM_SQL_ERROR;
}
/*************************************************************************
*
* Function: sql_free_result
*
* Purpose: database specific free_result. Frees memory allocated
* for a result set
*
*************************************************************************/
static sql_rcode_t sql_free_result(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
{
rlm_sql_db2_conn_t *conn;
conn = handle->conn;
SQLFreeHandle(SQL_HANDLE_STMT, conn->stmt);
return RLM_SQL_OK;
}
/*************************************************************************
*
* Function: sql_error
*
* Purpose: database specific error. Returns error associated with
* connection
*
*************************************************************************/
static char const *sql_error(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
{
/* this should really be enough, if not, you still got the sqlstate */
char sqlstate[6];
char msg[1024];
SQLINTEGER err;
SQLSMALLINT rl;
rlm_sql_db2_conn_t *conn = handle->conn;
TALLOC_FREE(conn->error);
SQLGetDiagRec(SQL_HANDLE_STMT, conn->stmt, 1, (SQLCHAR *) sqlstate, &err, (SQLCHAR *) msg, sizeof(msg), &rl);
conn->error = talloc_typed_asprintf(conn, "sqlstate %s: %s", sqlstate, msg);
return conn->error;
}
/*************************************************************************
*
* Function: sql_finish_query
*
* Purpose: End the query, such as freeing memory
*
*************************************************************************/
static sql_rcode_t sql_finish_query(UNUSED rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
{
return RLM_SQL_OK;
}
/*************************************************************************
*
* Function: sql_finish_select_query
*
* Purpose: End the select query, such as freeing memory or result
*
*************************************************************************/
static sql_rcode_t sql_finish_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
{
return sql_finish_query(handle, config);
}
/*************************************************************************
*
* Function: sql_affected_rows
*
* Purpose: Return the number of rows affected by the last query.
*
*************************************************************************/
static int sql_affected_rows(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
{
SQLINTEGER c;
rlm_sql_db2_conn_t *conn = handle->conn;
SQLRowCount(conn->stmt, &c);
return c;
}
/* Exported to rlm_sql */
rlm_sql_module_t rlm_sql_db2 = {
.name = "rlm_sql_db2",
.sql_socket_init = sql_socket_init,
.sql_query = sql_query,
.sql_select_query = sql_select_query,
.sql_num_fields = sql_num_fields,
.sql_fetch_row = sql_fetch_row,
.sql_free_result = sql_free_result,
.sql_error = sql_error,
.sql_finish_query = sql_finish_query,
.sql_finish_select_query = sql_finish_select_query,
.sql_affected_rows = sql_affected_rows
};