30 RCSID(
"$Id: 6abbb5a397f4ab344256a97695cfbc22f1ae5595 $")
32 #define LOG_PREFIX "sql - db2"
34 #include <freeradius-devel/server/base.h>
35 #include <freeradius-devel/util/debug.h>
54 DEBUG2(
"Socket destructor called, closing socket");
58 SQLFreeHandle(SQL_HANDLE_DBC, conn->
dbc_handle);
78 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &(c->
env_handle));
82 SQLSetConnectAttr(c->
dbc_handle, SQL_ATTR_LOGIN_TIMEOUT, &timeout_ms, SQL_IS_UINTEGER);
100 if (ret != SQL_SUCCESS) {
101 ERROR(
"could not connect to DB2 server %s",
config->sql_server);
130 query_ctx->
tconn = tconn;
135 SQLAllocHandle(SQL_HANDLE_STMT, sql_conn->
dbc_handle, &(sql_conn->
stmt));
140 ret = SQLExecDirect(sql_conn->
stmt, db2_query, SQL_NTS);
141 if (ret != SQL_SUCCESS) {
145 SQLGetDiagField(SQL_HANDLE_STMT, sql_conn->
dbc_handle, 1, SQL_DIAG_SQLSTATE, state,
sizeof(state), &len);
147 if (strncmp((
char *)state, SQL_CONSTR_INDEX_UNIQUE, 5)) {
153 ERROR(
"Could not execute statement \"%s\"", query_ctx->
query_str);
170 SQLSMALLINT fields, len, i;
175 SQLNumResultCols(conn->
stmt, &fields);
178 MEM(
names = talloc_array(query_ctx,
char const *, fields));
180 for (i = 0; i < fields; i++) {
183 switch (SQLColAttribute(conn->
stmt, i, SQL_DESC_BASE_COLUMN_NAME,
184 field,
sizeof(field), &len, NULL)) {
185 case SQL_INVALID_HANDLE:
187 ERROR(
"Failed retrieving field name at index %i", i);
195 MEM(p = talloc_array(
names,
char, (
size_t)len + 1));
196 strlcpy(p, field, (
size_t)len + 1);
208 SQLINTEGER len, slen;
213 TALLOC_FREE(query_ctx->
row);
215 SQLNumResultCols(conn->
stmt, &c);
218 if (SQLFetch(conn->
stmt) == SQL_NO_DATA_FOUND) {
224 for (i = 0; i < c; i++) {
226 SQLColAttribute(conn->
stmt, i + 1, SQL_DESC_DISPLAY_SIZE, NULL, 0, NULL, &len);
228 MEM(row[i] = talloc_array(row,
char, len + 1));
231 SQLGetData(conn->
stmt, i + 1, SQL_C_CHAR, row[i], len + 1, &slen);
232 if (slen == SQL_NULL_DATA) row[i][0] =
'\0';
235 query_ctx->
row = row;
246 TALLOC_FREE(query_ctx->
row);
247 SQLFreeHandle(SQL_HANDLE_STMT, conn->
stmt);
276 SQLGetDiagRec(SQL_HANDLE_STMT, conn->
stmt, 1, (SQLCHAR *) state, &
err,
277 (SQLCHAR *) errbuff,
sizeof(errbuff), &rl);
278 if (errbuff[0] ==
'\0')
return 0;
296 SQLRowCount(conn->
stmt, &c);
306 query_ctx->
treq = NULL;
319 .sql_query_resume = sql_query_resume,
320 .sql_select_query_resume = sql_query_resume,
330 .connection_alloc = sql_trunk_connection_alloc,
331 .request_mux = sql_trunk_request_mux,
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
@ CONNECTION_STATE_FAILED
Connection has failed.
@ CONNECTION_STATE_CONNECTED
File descriptor is open (ready for writing).
fr_dcursor_eval_t void const * uctx
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
#define ROPTIONAL(_l_request, _l_global, _fmt,...)
Use different logging functions depending on whether request is NULL or not.
Stores all information relating to an event list.
static const conf_parser_t config[]
rlm_rcode_t
Return codes indicating the result of the module call.
Prototypes and functions for the SQL module.
fr_sql_query_status_t status
Status of the query.
trunk_connection_t * tconn
Trunk connection this query is being run on.
void * conn
Database specific connection handle.
char const * query_str
Query string to run.
request_t * request
Request this query relates to.
sql_rcode_t
Action to take at end of an SQL query.
@ RLM_SQL_ALT_QUERY
Key constraint violation, use an alternative query.
@ RLM_SQL_ERROR
General connection/server error.
@ RLM_SQL_NO_MORE_ROWS
No more rows available.
rlm_sql_handle_t * handle
Connection handle this query is being run on.
#define RLM_SQL_RCODE_FLAGS_ALT_QUERY
Can distinguish between other errors and those.
rlm_sql_row_t row
Row data from the last query.
sql_rcode_t rcode
Result code.
trunk_request_t * treq
Trunk request for this query.
@ SQL_QUERY_RETURNED
Query has executed.
rlm_sql_driver_t rlm_sql_db2
static sql_rcode_t sql_fields(char const **out[], fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
static size_t sql_error(TALLOC_CTX *ctx, sql_log_entry_t out[], NDEBUG_UNUSED size_t outlen, fr_sql_query_t *query_ctx)
Retrieves any errors associated with the query context.
static unlang_action_t sql_fetch_row(rlm_rcode_t *p_result, UNUSED int *priority, UNUSED request_t *request, void *uctx)
static void _sql_connection_close(UNUSED fr_event_list_t *el, void *h, UNUSED void *uctx)
static sql_rcode_t sql_finish_query(UNUSED fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
static sql_rcode_t sql_free_result(fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
static int sql_affected_rows(fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
static void sql_request_fail(request_t *request, void *preq, UNUSED void *rctx, UNUSED trunk_request_state_t state, UNUSED void *uctx)
Macros to reduce boilerplate in trunk SQL drivers.
#define SQL_TRUNK_CONNECTION_ALLOC
Allocate an SQL trunk connection.
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
size_t strlcpy(char *dst, char const *src, size_t siz)
module_t common
Common fields for all loadable modules.
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
#define talloc_get_type_abort_const
static const char * names[8]
static int64_t fr_time_delta_to_msec(fr_time_delta_t delta)
void trunk_request_signal_fail(trunk_request_t *treq)
Signal that a trunk request failed.
int trunk_connection_pop_request(trunk_request_t **treq_out, trunk_connection_t *tconn)
Pop a request off a connection's pending queue.
void trunk_request_signal_reapable(trunk_request_t *treq)
Signal that the request was written to a connection successfully, but no response is expected.
Associates request queues with a connection.
trunk_request_state_t
Used for sanity checks and to simplify freeing.
static fr_event_list_t * el
static size_t char ** out