The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
Data Structures | Macros | Enumerations | Functions | Variables
rlm_sql_mysql.c File Reference

MySQL driver. More...

#include <freeradius-devel/server/base.h>
#include <freeradius-devel/util/debug.h>
#include <sys/stat.h>
#include "config.h"
#include "rlm_sql.h"
#include "rlm_sql_trunk.h"
+ Include dependency graph for rlm_sql_mysql.c:

Go to the source code of this file.

Data Structures

struct  rlm_sql_mysql_conn_t
 
struct  rlm_sql_mysql_t
 

Macros

#define LOG_PREFIX   log_prefix
 
#define LOG_PREFIX   "rlm_sql_mysql"
 
#define LOG_PREFIX   log_prefix
 

Enumerations

enum  rlm_sql_mysql_warnings {
  SERVER_WARNINGS_AUTO = 0 ,
  SERVER_WARNINGS_YES ,
  SERVER_WARNINGS_NO
}
 

Functions

static void _sql_connect_io_notify (fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
 Callback for I/O events in response to mysql_real_connect_start()
 
static void _sql_connect_query_run (connection_t *conn, UNUSED connection_state_t prev, UNUSED connection_state_t state, void *uctx)
 
static void _sql_connection_close (fr_event_list_t *el, void *h, UNUSED void *uctx)
 
static connection_state_t _sql_connection_init (void **h, connection_t *conn, void *uctx)
 
static int mod_instantiate (module_inst_ctx_t const *mctx)
 
static int mod_load (void)
 
static void mod_unload (void)
 
static int sql_affected_rows (fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
 
static sql_rcode_t sql_check_error (MYSQL *server, int client_errno)
 Analyse the last error that occurred on the socket, and determine an action.
 
static size_t sql_error (TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, fr_sql_query_t *query_ctx)
 Retrieves any errors associated with the query context.
 
static void * sql_escape_arg_alloc (TALLOC_CTX *ctx, fr_event_list_t *el, void *uctx)
 Allocate the argument used for the SQL escape function.
 
static void sql_escape_arg_free (void *uctx)
 
static int sql_escape_func (request_t *request, fr_value_box_t *vb, void *arg)
 
static unlang_action_t sql_fetch_row (unlang_result_t *p_result, UNUSED request_t *request, void *uctx)
 
static sql_rcode_t sql_fields (char const **out[], fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
 
static sql_rcode_t sql_finish_query (fr_sql_query_t *query_ctx, rlm_sql_config_t const *config)
 Finish query.
 
static sql_rcode_t sql_free_result (fr_sql_query_t *, rlm_sql_config_t const *)
 
static sql_rcode_t sql_free_result (fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
 
static void sql_mysql_fd_release (rlm_sql_mysql_conn_t *c, fr_event_list_t *el)
 Stop tracking our fd for a connection.
 
static int sql_mysql_fd_track (rlm_sql_mysql_conn_t *c, fr_event_list_t *el)
 Track the socket actually being used by the client library.
 
static int sql_num_rows (fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
 
static void sql_request_cancel (connection_t *conn, void *preq, trunk_cancel_reason_t reason, UNUSED void *uctx)
 
static void sql_request_cancel_mux (UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
 
SQL_QUERY_FAIL static SQL_QUERY_RESUME unlang_action_t sql_select_query_resume (unlang_result_t *p_result, UNUSED request_t *request, void *uctx)
 
static sql_rcode_t sql_store_result (rlm_sql_mysql_conn_t *conn, UNUSED rlm_sql_config_t const *config)
 
static void sql_trunk_request_demux (UNUSED fr_event_list_t *el, UNUSED trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
 
static void sql_trunk_request_mux (UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
 
static ssize_t sql_warnings (TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, rlm_sql_mysql_conn_t *conn)
 Retrieves any warnings associated with the last query.
 

Variables

static const conf_parser_t driver_config []
 
rlm_sql_driver_t rlm_sql_mysql
 
static fr_table_num_sorted_t const server_warnings_table []
 
static size_t server_warnings_table_len = NUM_ELEMENTS(server_warnings_table)
 
static conf_parser_t tls_config []
 

Detailed Description

MySQL driver.

Id
27b343cf2f2534b47816ca80cc58c8896eb6c5a6

Definition in file rlm_sql_mysql.c.


Data Structure Documentation

◆ rlm_sql_mysql_conn_t

struct rlm_sql_mysql_conn_t

Definition at line 68 of file rlm_sql_mysql.c.

+ Collaboration diagram for rlm_sql_mysql_conn_t:
Data Fields
int client_fd Socket the client library last reported, for detecting fd changes.
connection_t * conn Generic connection structure for this connection.
MYSQL db Structure representing connection details.
int fd Our fd for this connection's I/O events.
fr_sql_query_t * query_ctx Current query running on this connection.
MYSQL_RES * result Result from most recent query.
MYSQL * sock Connection details as returned by connection init functions.
int status returned by the most recent non-blocking function call.

◆ rlm_sql_mysql_t

struct rlm_sql_mysql_t

Definition at line 79 of file rlm_sql_mysql.c.

Data Fields
char const * character_set Character set to use on connections.
char const * tls_ca_file Path to the CA used to validate the server's certificate.
char const * tls_ca_path Directory containing CAs that may be used to validate the servers certificate.
char const * tls_certificate_file Public certificate we present to the server.
bool tls_check_cert Verify there's a trust relationship between the server's cert and one of the CAs we have configured.
bool tls_check_cert_cn Verify that the CN in the server cert matches the host we passed to mysql_real_connect().
char const * tls_cipher Colon separated list of TLS ciphers for TLS <= 1.2.
char const * tls_crl_file Public certificate we present to the server.
char const * tls_crl_path Private key for the certificate we present to the server.
char const * tls_private_key_file Private key for the certificate we present to the server.
bool tls_required Require that the connection is encrypted.
rlm_sql_mysql_warnings warnings mysql_warning_count() doesn't appear to work with NDB cluster
char const * warnings_str Whether we always query the server for additional warnings.

Macro Definition Documentation

◆ LOG_PREFIX [1/3]

#define LOG_PREFIX   log_prefix

Definition at line 29 of file rlm_sql_mysql.c.

◆ LOG_PREFIX [2/3]

#define LOG_PREFIX   "rlm_sql_mysql"

Definition at line 29 of file rlm_sql_mysql.c.

◆ LOG_PREFIX [3/3]

#define LOG_PREFIX   log_prefix

Definition at line 29 of file rlm_sql_mysql.c.

Enumeration Type Documentation

◆ rlm_sql_mysql_warnings

Enumerator
SERVER_WARNINGS_AUTO 
SERVER_WARNINGS_YES 
SERVER_WARNINGS_NO 

Definition at line 55 of file rlm_sql_mysql.c.

Function Documentation

◆ _sql_connect_io_notify()

static void _sql_connect_io_notify ( fr_event_list_t el,
UNUSED int  fd,
UNUSED int  flags,
void *  uctx 
)
static

Callback for I/O events in response to mysql_real_connect_start()

Definition at line 273 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _sql_connect_query_run()

static void _sql_connect_query_run ( connection_t conn,
UNUSED connection_state_t  prev,
UNUSED connection_state_t  state,
void *  uctx 
)
static

Definition at line 329 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _sql_connection_close()

static void _sql_connection_close ( fr_event_list_t el,
void *  h,
UNUSED void *  uctx 
)
static

Definition at line 463 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _sql_connection_init()

static connection_state_t _sql_connection_init ( void **  h,
connection_t conn,
void *  uctx 
)
static

For MariaDB, It should be true as can be seen in https://github.com/MariaDB/server/blob/mariadb-5.5.68/sql-common/client.c#L4338

Definition at line 362 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mod_instantiate()

static int mod_instantiate ( module_inst_ctx_t const *  mctx)
static

Definition at line 141 of file rlm_sql_mysql.c.

◆ mod_load()

static int mod_load ( void  )
static

Definition at line 177 of file rlm_sql_mysql.c.

◆ mod_unload()

static void mod_unload ( void  )
static

Definition at line 172 of file rlm_sql_mysql.c.

◆ sql_affected_rows()

static int sql_affected_rows ( fr_sql_query_t query_ctx,
UNUSED rlm_sql_config_t const *  config 
)
static

Definition at line 916 of file rlm_sql_mysql.c.

◆ sql_check_error()

static sql_rcode_t sql_check_error ( MYSQL *  server,
int  client_errno 
)
static

Analyse the last error that occurred on the socket, and determine an action.

Parameters
serverSocket from which to extract the server error. May be NULL.
client_errnoError from the client.
Returns
an action for rlm_sql_t to take.

Definition at line 483 of file rlm_sql_mysql.c.

+ Here is the caller graph for this function:

◆ sql_error()

static size_t sql_error ( TALLOC_CTX *  ctx,
sql_log_entry_t  out[],
size_t  outlen,
fr_sql_query_t query_ctx 
)
static

Retrieves any errors associated with the query context.

Note
Caller should free any memory allocated in ctx (talloc_free_children()).
Parameters
ctxto allocate temporary error buffers in.
outArray of sql_log_entrys to fill.
outlenLength of out array.
query_ctxQuery context to retrieve error for.
Returns
number of errors written to the sql_log_entry_t array.

Definition at line 774 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:

◆ sql_escape_arg_alloc()

static void * sql_escape_arg_alloc ( TALLOC_CTX *  ctx,
fr_event_list_t el,
void *  uctx 
)
static

Allocate the argument used for the SQL escape function.

In this case, a dedicated connection to allow the escape function to have access to server side parameters, though no packets ever flow after the connection is made.

Definition at line 1191 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:

◆ sql_escape_arg_free()

static void sql_escape_arg_free ( void *  uctx)
static

Definition at line 1214 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:

◆ sql_escape_func()

static int sql_escape_func ( request_t request,
fr_value_box_t vb,
void *  arg 
)
static

Definition at line 923 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:

◆ sql_fetch_row()

static unlang_action_t sql_fetch_row ( unlang_result_t p_result,
UNUSED request_t request,
void *  uctx 
)
static

Definition at line 614 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:

◆ sql_fields()

static sql_rcode_t sql_fields ( char const **  out[],
fr_sql_query_t query_ctx,
UNUSED rlm_sql_config_t const *  config 
)
static

Definition at line 583 of file rlm_sql_mysql.c.

◆ sql_finish_query()

static sql_rcode_t sql_finish_query ( fr_sql_query_t query_ctx,
rlm_sql_config_t const *  config 
)
static

Finish query.

As a single SQL statement may return multiple results sets, (for example stored procedures) it is necessary to check whether more results exist and process them in turn if so.

Definition at line 850 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:

◆ sql_free_result() [1/2]

static sql_rcode_t sql_free_result ( fr_sql_query_t ,
rlm_sql_config_t const *   
)
static
+ Here is the caller graph for this function:

◆ sql_free_result() [2/2]

static sql_rcode_t sql_free_result ( fr_sql_query_t query_ctx,
UNUSED rlm_sql_config_t const *  config 
)
static

Definition at line 682 of file rlm_sql_mysql.c.

◆ sql_mysql_fd_release()

static void sql_mysql_fd_release ( rlm_sql_mysql_conn_t c,
fr_event_list_t el 
)
static

Stop tracking our fd for a connection.

Removes the event and closes our descriptor. Safe to call repeatedly.

Definition at line 195 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sql_mysql_fd_track()

static int sql_mysql_fd_track ( rlm_sql_mysql_conn_t c,
fr_event_list_t el 
)
static

Track the socket actually being used by the client library.

The client library owns its socket and potentially both closes and replaces it without telling us during connection setup, meaning events are still registered on the wrong fd.

Create a dup() of the fd which gets used in the event list to avoid this issue.

Call after each mysql_real_connect_* call to ensure the correct fd is in the event list.

Parameters
[in]cconnection to update.
[in]elevent list the old event is registered in, may be NULL if nothing is registered yet.
Returns
  • 0 if the descriptor is unchanged or was updated.
  • -1 on error or if the library closed closed the connection.

Definition at line 224 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sql_num_rows()

static int sql_num_rows ( fr_sql_query_t query_ctx,
UNUSED rlm_sql_config_t const *  config 
)
static

Definition at line 574 of file rlm_sql_mysql.c.

◆ sql_request_cancel()

static void sql_request_cancel ( connection_t conn,
void *  preq,
trunk_cancel_reason_t  reason,
UNUSED void *  uctx 
)
static

Definition at line 1135 of file rlm_sql_mysql.c.

◆ sql_request_cancel_mux()

static void sql_request_cancel_mux ( UNUSED fr_event_list_t el,
trunk_connection_t tconn,
connection_t conn,
UNUSED void *  uctx 
)
static

Definition at line 1147 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:

◆ sql_select_query_resume()

SQL_QUERY_FAIL static SQL_QUERY_RESUME unlang_action_t sql_select_query_resume ( unlang_result_t p_result,
UNUSED request_t request,
void *  uctx 
)
static

Definition at line 1165 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sql_store_result()

static sql_rcode_t sql_store_result ( rlm_sql_mysql_conn_t conn,
UNUSED rlm_sql_config_t const *  config 
)
static

Definition at line 554 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sql_trunk_request_demux()

static void sql_trunk_request_demux ( UNUSED fr_event_list_t el,
UNUSED trunk_connection_t tconn,
connection_t conn,
UNUSED void *  uctx 
)
static

Definition at line 1062 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:

◆ sql_trunk_request_mux()

static void sql_trunk_request_mux ( UNUSED fr_event_list_t el,
trunk_connection_t tconn,
connection_t conn,
UNUSED void *  uctx 
)
static

Definition at line 965 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:

◆ sql_warnings()

static ssize_t sql_warnings ( TALLOC_CTX *  ctx,
sql_log_entry_t  out[],
size_t  outlen,
rlm_sql_mysql_conn_t conn 
)
static

Retrieves any warnings associated with the last query.

MySQL stores a limited number of warnings associated with the last query executed. These can be very useful in diagnosing issues, or in some cases working around bugs in MySQL which causes it to return the wrong error.

Note
Caller should free any memory allocated in ctx (talloc_free_children()).
Parameters
ctxto allocate temporary error buffers in.
outArray of sql_log_entrys to fill.
outlenLength of out array.
connMySQL connection the query was run on.
Returns

Definition at line 711 of file rlm_sql_mysql.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ driver_config

const conf_parser_t driver_config[]
static
Initial value:
= {
{ FR_CONF_POINTER("tls", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) tls_config },
{ FR_CONF_OFFSET("warnings", rlm_sql_mysql_t, warnings_str), .dflt = "auto" },
{ FR_CONF_OFFSET("character_set", rlm_sql_mysql_t, character_set) },
}
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:669
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
Definition cf_parse.h:334
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition cf_parse.h:423
static conf_parser_t tls_config[]

Definition at line 130 of file rlm_sql_mysql.c.

◆ rlm_sql_mysql

rlm_sql_driver_t rlm_sql_mysql

Definition at line 1222 of file rlm_sql_mysql.c.

◆ server_warnings_table

fr_table_num_sorted_t const server_warnings_table[]
static
Initial value:
= {
{ L("auto"), SERVER_WARNINGS_AUTO },
{ L("no"), SERVER_WARNINGS_NO },
{ L("yes"), SERVER_WARNINGS_YES }
}
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:228
@ SERVER_WARNINGS_AUTO
@ SERVER_WARNINGS_NO
@ SERVER_WARNINGS_YES

Definition at line 61 of file rlm_sql_mysql.c.

◆ server_warnings_table_len

size_t server_warnings_table_len = NUM_ELEMENTS(server_warnings_table)
static

Definition at line 66 of file rlm_sql_mysql.c.

◆ tls_config

conf_parser_t tls_config[]
static
Initial value:
= {
{ FR_CONF_OFFSET_FLAGS("certificate_file", CONF_FLAG_FILE_READABLE, rlm_sql_mysql_t, tls_certificate_file) },
{ FR_CONF_OFFSET_FLAGS("private_key_file", CONF_FLAG_FILE_READABLE, rlm_sql_mysql_t, tls_private_key_file) },
{ FR_CONF_OFFSET("cipher", rlm_sql_mysql_t, tls_cipher) },
{ FR_CONF_OFFSET("tls_required", rlm_sql_mysql_t, tls_required) },
{ FR_CONF_OFFSET("check_cert", rlm_sql_mysql_t, tls_check_cert) },
{ FR_CONF_OFFSET("check_cert_cn", rlm_sql_mysql_t, tls_check_cert_cn) },
}
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:268
@ CONF_FLAG_FILE_READABLE
File matching value must exist, and must be readable.
Definition cf_parse.h:435

Definition at line 104 of file rlm_sql_mysql.c.