Check User ID
int module_check_user_id(request_rec *pReq)
This function is responsible for acquiring and
checking a user ID. The user ID should be stored in
pReq->connection->user
. The function should
return OK
, DECLINED
, or a
status code. Of particular interest is
HTTP_UNAUTHORIZED
(formerly known as
AUTH_REQUIRED
), which should be returned if the
authorization fails (either because the user agent presented no
credentials or because those presented were not correct). All modules
are polled until one returns something other than
DECLINED
. If all decline, a configuration error is
logged, and an error is returned to the user agent. When
HTTP_UNAUTHORIZED
is returned, an appropriate
header should be set to inform the user agent of the type of
credentials to present when it retries. Currently, the appropriate
header is WWW-Authenticate
(see the HTTP 1.1
specification for details). Unfortunately, Apache’s
modularity is not quite as good as it might be in this area. So this
hook usually provides alternate ways of accessing the user/password
database, rather than changing the way authorization is actually
done, as evidenced by the fact that the protocol side of
authorization is currently dealt with in
http_protocol.c, rather than in the module. Note
that this function checks the validity of the username and password
and not whether the particular user has permission to access the URL.
An obvious user of this hook is mod_auth.c, as shown in Example 21-18.
Example 21-18. mod_auth.c
static int authenticate_basic_user(request_rec *r) { auth_config_rec *sec = (auth_config_rec *) ap_get_module_config(r->per_dir_config, &auth_module); conn_rec *c = r->connection; const char *sent_pw; char *real_pw; char *invalid_pw; int res; if ((res = ap_get_basic_auth_pw(r, &sent_pw))) return res; if (!sec->auth_pwfile) return DECLINED; if (!(real_pw = get_pw(r, c->user, sec->auth_pwfile))) { if (!(sec->auth_authoritative)) return DECLINED; ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r, "user %s not found: %s", c->user, r->uri); ap_note_basic_auth_failure(r); return AUTH_REQUIRED; } invalid_pw = ap_validate_password(sent_pw, real_pw); if (invalid_pw != NULL) { ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r, "user %s: authentication failure for \"%s\": %s", c->user, r->uri, invalid_pw); ap_note_basic_auth_failure(r); return AUTH_REQUIRED; } return OK; }
This function is essentially the same for 2.0, except that
AUTH_REQUIRED
has become
HTTP_UNAUTHORIZED
.