24 RCSID(
"$Id: adeb4a1209bec1d5da9c862806a9e74dbc7bd0ca $")
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/rad_assert.h>
38 #define IODBC_MAX_ERROR_LEN 256
59 DEBUG2(
"rlm_sql_iodbc: Socket destructor called, closing socket");
61 if (conn->
stmt) SQLFreeStmt(conn->
stmt, SQL_DROP);
85 if (!SQL_SUCCEEDED(rcode)) {
86 ERROR(
"rlm_sql_iodbc: SQLAllocEnv failed");
87 if (
sql_error(NULL, &entry, 1, handle, config) > 0)
ERROR(
"rlm_sql_iodbc: %s", entry.
msg);
93 if (!SQL_SUCCEEDED(rcode)) {
94 ERROR(
"rlm_sql_iodbc: SQLAllocConnect failed");
95 if (
sql_error(NULL, &entry, 1, handle, config) > 0)
ERROR(
"rlm_sql_iodbc: %s", entry.
msg);
101 SQLSetConnectAttr(conn->
dbc_handle, SQL_ATTR_LOGIN_TIMEOUT, &timeout_ms, SQL_IS_UINTEGER);
107 SQLCHAR *server, *login, *password;
109 memcpy(&server, &config->
sql_server,
sizeof(server));
110 memcpy(&login, &config->
sql_login,
sizeof(login));
111 memcpy(&password, &config->
sql_password,
sizeof(password));
113 rcode = SQLConnect(conn->
dbc_handle, server, SQL_NTS, login, SQL_NTS, password, SQL_NTS);
115 if (!SQL_SUCCEEDED(rcode)) {
116 ERROR(
"rlm_sql_iodbc: SQLConnectfailed");
117 if (
sql_error(NULL, &entry, 1, handle, config) > 0)
ERROR(
"rlm_sql_iodbc: %s", entry.
msg);
134 ERROR(
"rlm_sql_iodbc: Socket not connected");
141 memcpy(&statement, &query,
sizeof(statement));
142 rcode = SQLExecDirect(conn->
stmt, statement, SQL_NTS);
162 row = (
char **)
rad_malloc(
sizeof(
char *) * (numfields+1));
163 memset(row, 0, (
sizeof(
char *) * (numfields)));
164 row[numfields] = NULL;
166 for(i=1; i<=numfields; i++) {
167 SQLColAttributes(conn->
stmt, ((SQLUSMALLINT) i), SQL_COLUMN_LENGTH, NULL, 0, NULL, &len);
182 SQLBindCol(conn->
stmt, i, SQL_C_CHAR, (SQLCHAR *)row[i-1], len, 0);
196 SQLNumResultCols(conn->
stmt, &count);
205 SQLSMALLINT fields, len, i;
210 SQLNumResultCols(conn->
stmt, &fields);
213 MEM(names = talloc_array(handle,
char const *, fields));
215 for (i = 0; i < fields; i++) {
218 switch (SQLColAttribute(conn->
stmt, i, SQL_DESC_BASE_COLUMN_NAME,
219 field,
sizeof(field), &len, NULL)) {
220 case SQL_INVALID_HANDLE:
222 ERROR(
"Failed retrieving field name at index %i", i);
230 MEM(p = talloc_array(names,
char, (
size_t)len + 1));
231 strlcpy(p, field, (
size_t)len + 1);
244 *out = handle->
row = NULL;
246 if ((rc = SQLFetch(conn->
stmt)) == SQL_NO_DATA_FOUND)
return 0;
249 *out = handle->
row = conn->
row;
262 SQLFreeStmt(conn->
stmt, SQL_DROP);
282 SQLINTEGER errornum = 0;
284 SQLCHAR
state[256] =
"";
292 if (errbuff[0] ==
'\0')
return 0;
295 out[0].
msg = talloc_asprintf(ctx,
"%s: %s", state, errbuff);
315 SQLRowCount(conn->
stmt, &count);
322 .
name =
"rlm_sql_iodbc",
static int sql_num_fields(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
General connection/server error.
Prototypes and functions for the SQL module.
log_type_t type
Type of log entry L_ERR, L_WARN, L_INFO, L_DBG etc..
void * rad_malloc(size_t size)
char const * msg
Log message.
char const * sql_server
Server to connect to.
static sql_rcode_t sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t *config, struct timeval const *timeout)
static int sql_affected_rows(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
rlm_sql_module_t rlm_sql_iodbc
static sql_rcode_t sql_free_result(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
static sql_rcode_t sql_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config, char const *query)
static sql_rcode_t sql_fields(char const **out[], rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
static sql_rcode_t sql_finish_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
static sql_rcode_t sql_query(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config, char const *query)
char const * sql_password
Login password to use.
void * conn
Database specific connection handle.
rlm_sql_row_t row
Row data from the last query.
struct rlm_sql_iodbc_conn rlm_sql_iodbc_conn_t
static int _sql_socket_destructor(rlm_sql_iodbc_conn_t *conn)
static size_t sql_error(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
Retrieves any errors associated with the connection handle.
char const * sql_login
Login credentials to use.
#define IODBC_MAX_ERROR_LEN
size_t strlcpy(char *dst, char const *src, size_t siz)
#define FR_TIMEVAL_TO_MS(_x)
static sql_rcode_t sql_fetch_row(rlm_sql_row_t *out, rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
static sql_rcode_t sql_finish_select_query(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
#define USES_APPLE_DEPRECATED_API