Apache HTTP Server Version 2.4

| Description: | Manages SQL database connections |
|---|---|
| Status: | Extension |
| Module Identifier: | dbd_module |
| Source File: | mod_dbd.c |
| Compatibility: | Version 2.1 and later |
mod_dbd manages SQL database connections using
APR. It provides database connections on request
to modules requiring SQL database functions, and takes care of
managing databases with optimal efficiency and scalability
for both threaded and non-threaded MPMs. For details, see the
APR website.
This module manages database connections, in a manner
optimized for the platform. On non-threaded platforms,
it provides a persistent connection in the manner of
classic LAMP (Linux, Apache, Mysql, Perl/PHP/Python).
On threaded platform, it provides an altogether more
scalable and efficient connection pool, as
described in this
article at ApacheTutor. Note that mod_dbd
supersedes the modules presented in that article.
To connect to your database, you'll need to specify a driver, and connection parameters. These vary from one database engine to another. For example, to connect to mysql, do the following:
DBDriver mysql DBDParams host=localhost,dbname=pony,user=shetland,pass=appaloosa
You can then use this connection in a variety of other
modules, including mod_rewrite,
mod_authn_dbd, and mod_lua.
Further usage examples appear in each of those modules'
documentation.
See DBDParams for connection string
information for each of the supported database drivers.
mod_dbd exports five functions for other modules
to use. The API is as follows:
typedef struct {
apr_dbd_t *handle;
apr_dbd_driver_t *driver;
apr_hash_t *prepared;
} ap_dbd_t;
/* Export functions to access the database */
/* acquire a connection that MUST be explicitly closed.
* Returns NULL on error
*/
AP_DECLARE(ap_dbd_t*) ap_dbd_open(apr_pool_t*, server_rec*);
/* release a connection acquired with ap_dbd_open */
AP_DECLARE(void) ap_dbd_close(server_rec*, ap_dbd_t*);
/* acquire a connection that will have the lifetime of a request
* and MUST NOT be explicitly closed. Return NULL on error.
* This is the preferred function for most applications.
*/
AP_DECLARE(ap_dbd_t*) ap_dbd_acquire(request_rec*);
/* acquire a connection that will have the lifetime of a connection
* and MUST NOT be explicitly closed. Return NULL on error.
*/
AP_DECLARE(ap_dbd_t*) ap_dbd_cacquire(conn_rec*);
/* Prepare a statement for use by a client module */
AP_DECLARE(void) ap_dbd_prepare(server_rec*, const char*, const char*);
/* Also export them as optional functions for modules that prefer it */
APR_DECLARE_OPTIONAL_FN(ap_dbd_t*, ap_dbd_open, (apr_pool_t*, server_rec*));
APR_DECLARE_OPTIONAL_FN(void, ap_dbd_close, (server_rec*, ap_dbd_t*));
APR_DECLARE_OPTIONAL_FN(ap_dbd_t*, ap_dbd_acquire, (request_rec*));
APR_DECLARE_OPTIONAL_FN(ap_dbd_t*, ap_dbd_cacquire, (conn_rec*));
APR_DECLARE_OPTIONAL_FN(void, ap_dbd_prepare, (server_rec*, const char*, const char*));
mod_dbd supports SQL prepared statements on behalf
of modules that may wish to use them. Each prepared statement
must be assigned a name (label), and they are stored in a hash:
the prepared field of an ap_dbd_t.
Hash entries are of type apr_dbd_prepared_t
and can be used in any of the apr_dbd prepared statement
SQL query or select commands.
It is up to dbd user modules to use the prepared statements
and document what statements can be specified in httpd.conf,
or to provide their own directives and use ap_dbd_prepare.
reconnect to 0 in the connection string as to avoid errors that
arise from the MySQL client reconnecting without properly resetting the
prepared statements. If set to 1, any broken connections will be attempted
fixed, but as mod_dbd is not informed, the prepared statements will be invalidated.
Any web/database application needs to secure itself against SQL injection attacks. In most cases, Apache DBD is safe, because applications use prepared statements, and untrusted inputs are only ever used as data. Of course, if you use it via third-party modules, you should ascertain what precautions they may require.
However, the FreeTDS driver is inherently unsafe. The underlying library doesn't support prepared statements, so the driver emulates them, and the untrusted input is merged into the SQL statement.
It can be made safe by untainting all inputs: a process inspired by Perl's taint checking. Each input is matched against a regexp, and only the match is used, according to the Perl idiom:
$untrusted =~ /([a-z]+)/;
$trusted = $1;To use this, the untainting regexps must be included in the prepared statements configured. The regexp follows immediately after the % in the prepared statement, and is enclosed in curly brackets {}. For example, if your application expects alphanumeric input, you can use:
"SELECT foo FROM bar WHERE input = %s"
with other drivers, and suffer nothing worse than a failed query. But with FreeTDS you'd need:
"SELECT foo FROM bar WHERE input = %{([A-Za-z0-9]+)}s"
Now anything that doesn't match the regexp's $1 match is discarded, so the statement is safe.
An alternative to this may be the third-party ODBC driver, which offers the security of genuine prepared statements.
| Description: | Keepalive time for idle connections |
|---|---|
| Syntax: | DBDExptime time-in-seconds |
| Default: | DBDExptime 300 |
| Context: | server config, virtual host |
| Status: | Extension |
| Module: | mod_dbd |
Set the time to keep idle connections alive when the number of connections specified in DBDKeep has been exceeded (threaded platforms only).
| Description: | Execute an SQL statement after connecting to a database |
|---|---|
| Syntax: | DBDInitSQL "SQL statement" |
| Context: | server config, virtual host |
| Status: | Extension |
| Module: | mod_dbd |
Modules, that wish it, can have one or more SQL statements executed when a connection to a database is created. Example usage could be initializing certain values or adding a log entry when a new connection is made to the database.