Main Page | Modules | Namespace List | Class List | Directories | File List | Class Members | File Members | Related Pages | Examples

mod_authn_dbd.c File Reference

#include "ap_provider.h"
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "apr_lib.h"
#include "apr_dbd.h"
#include "mod_dbd.h"
#include "apr_strings.h"
#include "mod_auth.h"
#include "apr_md5.h"
#include "apu_version.h"

Go to the source code of this file.

Classes

struct  authn_dbd_conf
struct  authn_dbd_rec

Functions

static void * authn_dbd_cr_conf (apr_pool_t *pool, char *dummy)
static void * authn_dbd_merge_conf (apr_pool_t *pool, void *BASE, void *ADD)
static const char * authn_dbd_prepare (cmd_parms *cmd, void *cfg, const char *query)
static authn_status authn_dbd_password (request_rec *r, const char *user, const char *password)
static authn_status authn_dbd_realm (request_rec *r, const char *user, const char *realm, char **rethash)
static void authn_dbd_hooks (apr_pool_t *p)

Variables

module AP_MODULE_DECLARE_DATA authn_dbd_module
static ap_dbd_t *(* authn_dbd_acquire_fn )(request_rec *) = NULL
static void(* authn_dbd_prepare_fn )(server_rec *, const char *, const char *) = NULL
static const command_rec authn_dbd_cmds []


Function Documentation

static void* authn_dbd_cr_conf apr_pool_t pool,
char *  dummy
[static]
 

Definition at line 44 of file mod_authn_dbd.c.

References apr_pcalloc.

00045 {
00046     authn_dbd_conf *ret = apr_pcalloc(pool, sizeof(authn_dbd_conf));
00047     return ret;
00048 }

static void authn_dbd_hooks apr_pool_t p  )  [static]
 

Definition at line 254 of file mod_authn_dbd.c.

References authn_dbd_password(), authn_dbd_realm(), and AUTHN_PROVIDER_GROUP.

00255 {
00256     static const authn_provider authn_dbd_provider = {
00257         &authn_dbd_password,
00258         &authn_dbd_realm
00259     };
00260 
00261     ap_register_provider(p, AUTHN_PROVIDER_GROUP, "dbd", "0", &authn_dbd_provider);
00262 }

static void* authn_dbd_merge_conf apr_pool_t pool,
void *  BASE,
void *  ADD
[static]
 

Definition at line 49 of file mod_authn_dbd.c.

References base, NULL, authn_dbd_conf::realm, and authn_dbd_conf::user.

00050 {
00051     authn_dbd_conf *add = ADD;
00052     authn_dbd_conf *base = BASE;
00053     authn_dbd_conf *ret = apr_palloc(pool, sizeof(authn_dbd_conf));
00054     ret->user = (add->user == NULL) ? base->user : add->user;
00055     ret->realm = (add->realm == NULL) ? base->realm : add->realm;
00056     return ret;
00057 }

static authn_status authn_dbd_password request_rec r,
const char *  user,
const char *  password
[static]
 

Definition at line 87 of file mod_authn_dbd.c.

References ap_get_module_config, APLOG_ERR, APLOG_MARK, APR_HASH_KEY_STRING, apr_isalnum, APR_SUCCESS, apr_toupper, AUTH_DENIED, AUTH_GENERAL_ERROR, AUTH_GRANTED, AUTH_USER_NOT_FOUND, authn_dbd_acquire_fn, authn_dbd_module, AUTHN_PREFIX, conf, ap_dbd_t::driver, ap_dbd_t::handle, name, NULL, request_rec::per_dir_config, request_rec::pool, ap_dbd_t::prepared, res, row, statement, str, request_rec::subprocess_env, and authn_dbd_conf::user.

Referenced by authn_dbd_hooks().

00089 {
00090     apr_status_t rv;
00091     const char *dbd_password = NULL;
00092     apr_dbd_prepared_t *statement;
00093     apr_dbd_results_t *res = NULL;
00094     apr_dbd_row_t *row = NULL;
00095 
00096     authn_dbd_conf *conf = ap_get_module_config(r->per_dir_config,
00097                                                 &authn_dbd_module);
00098     ap_dbd_t *dbd = authn_dbd_acquire_fn(r);
00099     if (dbd == NULL) {
00100         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
00101                       "Error looking up %s in database", user);
00102         return AUTH_GENERAL_ERROR;
00103     }
00104 
00105     if (conf->user == NULL) {
00106         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "No AuthDBDUserPWQuery has been specified.");
00107         return AUTH_GENERAL_ERROR;
00108     }
00109 
00110     statement = apr_hash_get(dbd->prepared, conf->user, APR_HASH_KEY_STRING);
00111     if (statement == NULL) {
00112         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "A prepared statement could not be found for AuthDBDUserPWQuery, key '%s'.", conf->user);
00113         return AUTH_GENERAL_ERROR;
00114     }
00115     if (apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle, &res, statement,
00116                               0, user, NULL) != 0) {
00117         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
00118                       "Error looking up %s in database", user);
00119         return AUTH_GENERAL_ERROR;
00120     }
00121     for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
00122          rv != -1;
00123          rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
00124         if (rv != 0) {
00125             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
00126                       "Error looking up %s in database", user);
00127             return AUTH_GENERAL_ERROR;
00128         }
00129         if (dbd_password == NULL) {
00130             dbd_password = apr_dbd_get_entry(dbd->driver, row, 0);
00131 
00132 #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 3)
00133             /* add the rest of the columns to the environment */
00134             int i = 1;
00135             const char *name;
00136             for (name = apr_dbd_get_name(dbd->driver, res, i);
00137                  name != NULL;
00138                  name = apr_dbd_get_name(dbd->driver, res, i)) {
00139 
00140                 char *str = apr_pstrcat(r->pool, AUTHN_PREFIX,
00141                                         name,
00142                                         NULL);
00143                 int j = sizeof(AUTHN_PREFIX)-1; /* string length of "AUTHENTICATE_", excluding the trailing NIL */
00144                 while (str[j]) {
00145                     if (!apr_isalnum(str[j])) {
00146                         str[j] = '_';
00147                     }
00148                     else {
00149                         str[j] = apr_toupper(str[j]);
00150                     }
00151                     j++;
00152                 }
00153                 apr_table_set(r->subprocess_env, str,
00154                               apr_dbd_get_entry(dbd->driver, row, i));
00155                 i++;
00156             }
00157 #endif
00158         }
00159         /* we can't break out here or row won't get cleaned up */
00160     }
00161 
00162     if (!dbd_password) {
00163         return AUTH_USER_NOT_FOUND;
00164     }
00165 
00166     rv = apr_password_validate(password, dbd_password);
00167 
00168     if (rv != APR_SUCCESS) {
00169         return AUTH_DENIED;
00170     }
00171 
00172     return AUTH_GRANTED;
00173 }

static const char* authn_dbd_prepare cmd_parms cmd,
void *  cfg,
const char *  query
[static]
 

Definition at line 58 of file mod_authn_dbd.c.

References APR_RETRIEVE_OPTIONAL_FN, authn_dbd_prepare_fn, label, NULL, cmd_parms_struct::pool, and cmd_parms_struct::server.

00059 {
00060     static unsigned int label_num = 0;
00061     char *label;
00062 
00063     if (authn_dbd_prepare_fn == NULL) {
00064         authn_dbd_prepare_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_prepare);
00065         if (authn_dbd_prepare_fn == NULL) {
00066             return "You must load mod_dbd to enable AuthDBD functions";
00067         }
00068         authn_dbd_acquire_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
00069     }
00070     label = apr_psprintf(cmd->pool, "authn_dbd_%d", ++label_num);
00071 
00072     authn_dbd_prepare_fn(cmd->server, query, label);
00073 
00074     /* save the label here for our own use */
00075     return ap_set_string_slot(cmd, cfg, label);
00076 }

static authn_status authn_dbd_realm request_rec r,
const char *  user,
const char *  realm,
char **  rethash
[static]
 

Definition at line 174 of file mod_authn_dbd.c.

References ap_get_module_config, APLOG_ERR, APLOG_MARK, APR_HASH_KEY_STRING, apr_isalnum, apr_toupper, AUTH_GENERAL_ERROR, AUTH_USER_FOUND, AUTH_USER_NOT_FOUND, authn_dbd_acquire_fn, authn_dbd_module, AUTHN_PREFIX, conf, ap_dbd_t::driver, ap_dbd_t::handle, name, NULL, request_rec::per_dir_config, request_rec::pool, ap_dbd_t::prepared, authn_dbd_conf::realm, res, row, statement, str, and request_rec::subprocess_env.

Referenced by authn_dbd_hooks().

00176 {
00177     apr_status_t rv;
00178     const char *dbd_hash = NULL;
00179     apr_dbd_prepared_t *statement;
00180     apr_dbd_results_t *res = NULL;
00181     apr_dbd_row_t *row = NULL;
00182 
00183     authn_dbd_conf *conf = ap_get_module_config(r->per_dir_config,
00184                                                 &authn_dbd_module);
00185     ap_dbd_t *dbd = authn_dbd_acquire_fn(r);
00186     if (dbd == NULL) {
00187         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
00188                       "Error looking up %s in database", user);
00189         return AUTH_GENERAL_ERROR;
00190     }
00191     if (conf->realm == NULL) {
00192         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "No AuthDBDUserRealmQuery has been specified.");
00193         return AUTH_GENERAL_ERROR;
00194     }
00195     statement = apr_hash_get(dbd->prepared, conf->realm, APR_HASH_KEY_STRING);
00196     if (statement == NULL) {
00197         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "A prepared statement could not be found for AuthDBDUserRealmQuery, key '%s'.", conf->realm);
00198         return AUTH_GENERAL_ERROR;
00199     }
00200     if (apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle, &res, statement,
00201                               0, user, realm, NULL) != 0) {
00202         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
00203                       "Error looking up %s:%s in database", user, realm);
00204         return AUTH_GENERAL_ERROR;
00205     }
00206     for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
00207          rv != -1;
00208          rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
00209         if (rv != 0) {
00210             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
00211                       "Error looking up %s in database", user);
00212             return AUTH_GENERAL_ERROR;
00213         }
00214         if (dbd_hash == NULL) {
00215             dbd_hash = apr_dbd_get_entry(dbd->driver, row, 0);
00216 
00217 #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 3)
00218             /* add the rest of the columns to the environment */
00219             int i = 1;
00220             const char *name;
00221             for (name = apr_dbd_get_name(dbd->driver, res, i);
00222                  name != NULL;
00223                  name = apr_dbd_get_name(dbd->driver, res, i)) {
00224 
00225                 char *str = apr_pstrcat(r->pool, AUTHN_PREFIX,
00226                                         name,
00227                                         NULL);
00228                 int j = sizeof(AUTHN_PREFIX)-1; /* string length of "AUTHENTICATE_", excluding the trailing NIL */
00229                 while (str[j]) {
00230                     if (!apr_isalnum(str[j])) {
00231                         str[j] = '_';
00232                     }
00233                     else {
00234                         str[j] = apr_toupper(str[j]);
00235                     }
00236                     j++;
00237                 }
00238                 apr_table_set(r->subprocess_env, str,
00239                               apr_dbd_get_entry(dbd->driver, row, i));
00240                 i++;
00241             }
00242 #endif
00243         }
00244         /* we can't break out here or row won't get cleaned up */
00245     }
00246 
00247     if (!dbd_hash) {
00248         return AUTH_USER_NOT_FOUND;
00249     }
00250 
00251     *rethash = apr_pstrdup(r->pool, dbd_hash);
00252     return AUTH_USER_FOUND;
00253 }


Variable Documentation

ap_dbd_t*(* authn_dbd_acquire_fn)(request_rec *) = NULL [static]
 

Definition at line 41 of file mod_authn_dbd.c.

Referenced by authn_dbd_password(), and authn_dbd_realm().

const command_rec authn_dbd_cmds[] [static]
 

Initial value:

{
    AP_INIT_TAKE1("AuthDBDUserPWQuery", authn_dbd_prepare,
                  (void *)APR_OFFSETOF(authn_dbd_conf, user), ACCESS_CONF,
                  "Query used to fetch password for user"),
    AP_INIT_TAKE1("AuthDBDUserRealmQuery", authn_dbd_prepare,
                  (void *)APR_OFFSETOF(authn_dbd_conf, realm), ACCESS_CONF,
                  "Query used to fetch password for user+realm"),
    {NULL}
}

Definition at line 77 of file mod_authn_dbd.c.

module AP_MODULE_DECLARE_DATA authn_dbd_module
 

Initial value:

Definition at line 263 of file mod_authn_dbd.c.

Referenced by authn_dbd_password(), and authn_dbd_realm().

void(* authn_dbd_prepare_fn)(server_rec *, const char *, const char *) = NULL [static]
 

Definition at line 42 of file mod_authn_dbd.c.

Referenced by authn_dbd_prepare().


© sourcejam.com 2005-2008