mod_auth_basic.c revision 6f61c36ed87866636fbc8bae4da3842205d81e5b
28ec5e4f5c4a71428affc986304e894eda600925takashi/* Licensed to the Apache Software Foundation (ASF) under one or more
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive * contributor license agreements. See the NOTICE file distributed with
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive * this work for additional information regarding copyright ownership.
5f5d1b4cc970b7f06ff8ef6526128e9a27303d88nd * The ASF licenses this file to You under the Apache License, Version 2.0
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive * (the "License"); you may not use this file except in compliance with
acc36ab93565d2880447d535da6ca6e5feac7a70nd * the License. You may obtain a copy of the License at
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding * Unless required by applicable law or agreed to in writing, software
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding * distributed under the License is distributed on an "AS IS" BASIS,
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
acc36ab93565d2880447d535da6ca6e5feac7a70nd * See the License for the specific language governing permissions and
acc36ab93565d2880447d535da6ca6e5feac7a70nd * limitations under the License.
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slivetypedef struct {
e41a440220bb41526fac54001a3ee94abb0d07a6rbowenstatic void *create_auth_basic_dir_config(apr_pool_t *p, char *d)
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive auth_basic_config_rec *conf = apr_pcalloc(p, sizeof(*conf));
3114b83fa8f03492a3e36809e875dec55b68a79dpepper /* Any failures are fatal. */
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slivestatic void *merge_auth_basic_dir_config(apr_pool_t *p, void *basev, void *overridesv)
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive auth_basic_config_rec *newconf = apr_pcalloc(p, sizeof(*newconf));
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive overrides->authoritative_set ? overrides->authoritative :
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive newconf->authoritative_set = overrides->authoritative_set
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive overrides->fake_set ? overrides->fakeuser : base->fakeuser;
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive overrides->fake_set ? overrides->fakepass : base->fakepass;
e41a440220bb41526fac54001a3ee94abb0d07a6rbowen newconf->fake_set = overrides->fake_set || base->fake_set;
e41a440220bb41526fac54001a3ee94abb0d07a6rbowen overrides->use_digest_algorithm_set ? overrides->use_digest_algorithm
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive overrides->use_digest_algorithm_set || base->use_digest_algorithm_set;
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive newconf->providers = overrides->providers ? overrides->providers : base->providers;
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slivestatic const char *add_authn_provider(cmd_parms *cmd, void *config,
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive const char *arg)
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive auth_basic_config_rec *conf = (auth_basic_config_rec*)config;
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive newp = apr_pcalloc(cmd->pool, sizeof(authn_provider_list));
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive /* lookup and cache the actual provider now */
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive newp->provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP,
e41a440220bb41526fac54001a3ee94abb0d07a6rbowen /* by the time they use it, the provider should be loaded and
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive registered with us. */
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive "Unknown Authn provider: %s",
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive /* if it doesn't provide the appropriate function, reject it */
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive "The '%s' Authn provider doesn't support "
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive /* Add it to the list now. */
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slivestatic const char *set_authoritative(cmd_parms * cmd, void *config, int flag)
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive auth_basic_config_rec *conf = (auth_basic_config_rec *) config;
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgunstatic const char *add_basic_fake(cmd_parms * cmd, void *config,
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive auth_basic_config_rec *conf = (auth_basic_config_rec *) config;
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive const char *err;
41dd95074cc6924ee56c53ba11aa6faf59b2ee13erikabele /* if password is unspecified, set it to the fixed string "password" to
41dd95074cc6924ee56c53ba11aa6faf59b2ee13erikabele * be compatible with the behaviour of mod_ssl.
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun ap_expr_parse_cmd(cmd, user, AP_EXPR_FLAG_STRING_RESULT,
181e56d8b348d301d615ccf5465ae600fee2867berikabele "Could not parse fake username expression '%s': %s", user,
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun ap_expr_parse_cmd(cmd, pass, AP_EXPR_FLAG_STRING_RESULT,
181e56d8b348d301d615ccf5465ae600fee2867berikabele "Could not parse fake password expression '%s': %s", user,
181e56d8b348d301d615ccf5465ae600fee2867berikabelestatic const char *set_use_digest_algorithm(cmd_parms *cmd, void *config,
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive const char *alg)
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive auth_basic_config_rec *conf = (auth_basic_config_rec *)config;
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun if (strcasecmp(alg, "Off") && strcasecmp(alg, "MD5")) {
181e56d8b348d301d615ccf5465ae600fee2867berikabele "Invalid algorithm in "
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive conf->use_digest_algorithm = apr_pstrdup(cmd->pool, alg);
181e56d8b348d301d615ccf5465ae600fee2867berikabele AP_INIT_ITERATE("AuthBasicProvider", add_authn_provider, NULL, OR_AUTHCFG,
5f48875017569cc7610b17d852c44e02684d9d5aerikabele "specify the auth providers for a directory or location"),
5f48875017569cc7610b17d852c44e02684d9d5aerikabele AP_INIT_FLAG("AuthBasicAuthoritative", set_authoritative, NULL, OR_AUTHCFG,
181e56d8b348d301d615ccf5465ae600fee2867berikabele "Set to 'Off' to allow access control to be passed along to "
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive "lower modules if the UserID is not known to this module"),
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive AP_INIT_TAKE12("AuthBasicFake", add_basic_fake, NULL, OR_AUTHCFG,
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive "Fake basic authentication using the given expressions for "
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive "username and password, 'off' to disable. Password defaults "
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun "to 'password' if missing."),
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive AP_INIT_TAKE1("AuthBasicUseDigestAlgorithm", set_use_digest_algorithm,
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive "Set to 'MD5' to use the auth provider's authentication "
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive "check for digest auth, using a hash of 'user:realm:pass'"),
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive/* These functions return 0 if client is OK, and proper error status
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive * if not... either HTTP_UNAUTHORIZED, if we made a check, and it failed, or
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun * HTTP_INTERNAL_SERVER_ERROR, if things are so totally confused that we
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive * couldn't figure out how to tell if the client is authorized or not.
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive * If they return DECLINED, and all other modules also decline, that's
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive * treated by the server core as a configuration error, logged and
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive * reported as such.
5f48875017569cc7610b17d852c44e02684d9d5aerikabele (PROXYREQ_PROXY == r->proxyreq) ? "Proxy-Authenticate"
5f48875017569cc7610b17d852c44e02684d9d5aerikabele : "WWW-Authenticate",
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive apr_pstrcat(r->pool, "Basic realm=\"", ap_auth_name(r),
181e56d8b348d301d615ccf5465ae600fee2867berikabelestatic int hook_note_basic_auth_failure(request_rec *r, const char *auth_type)
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgunstatic int get_basic_auth(request_rec *r, const char **user,
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun const char **pw)
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive /* Get the appropriate header */
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive auth_line = apr_table_get(r->headers_in, (PROXYREQ_PROXY == r->proxyreq)
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun ? "Proxy-Authorization"
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive : "Authorization");
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive if (strcasecmp(ap_getword(r->pool, &auth_line, ' '), "Basic")) {
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive /* Client tried to authenticate using wrong auth scheme */
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01614)
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive "client used wrong authentication scheme: %s", r->uri);
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive /* Skip leading spaces. */
181e56d8b348d301d615ccf5465ae600fee2867berikabele decoded_line = apr_palloc(r->pool, apr_base64_decode_len(auth_line) + 1);
41dd95074cc6924ee56c53ba11aa6faf59b2ee13erikabele /* Null-terminate the string. */
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive *user = ap_getword_nulls(r->pool, (const char**)&decoded_line, ':');
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun /* set the user, even though the user is unauthenticated at this point */
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive/* Determine user ID, and check if it really is that user, for HTTP
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive * basic authentication...
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive auth_basic_config_rec *conf = ap_get_module_config(r->per_dir_config,
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive /* Are we configured to be Basic auth? */
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive if (!current_auth || strcasecmp(current_auth, "Basic")) {
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive /* We need an authentication realm. */
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01615)
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive /* For now, if a provider isn't set, we'll be nice and use the file
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive * provider.
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01616)
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive "No Authn provider configured");
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, AUTHN_DEFAULT_PROVIDER);
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, current_provider->provider_name);
645377709717e10daeb3b57531340ce14424c7c0jorton ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02493)
645377709717e10daeb3b57531340ce14424c7c0jorton "Authn provider does not support "
645377709717e10daeb3b57531340ce14424c7c0jorton "AuthBasicUseDigestAlgorithm");
645377709717e10daeb3b57531340ce14424c7c0jorton /* We expect the password to be hash of user:realm:password */
645377709717e10daeb3b57531340ce14424c7c0jorton auth_result = provider->get_realm_hash(r, sent_user, realm,
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive auth_result = provider->check_password(r, sent_user, sent_pw);
c9cf90a8732db50bf51da5166f3c23f076d8174bpoirier /* Something occured. Stop checking. */
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive /* If we're not really configured for providers, stop now. */
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun /* If we're not authoritative, then any error is ignored. */
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive if (!(conf->authoritative) && auth_result != AUTH_DENIED) {
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01617)
e41a440220bb41526fac54001a3ee94abb0d07a6rbowen "user %s: authentication failure for \"%s\": "
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun "Password Mismatch",
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01618)
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive /* We'll assume that the module has already said what its error
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive * was in the logs.
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive /* If we're returning 403, tell them to try again. */
645377709717e10daeb3b57531340ce14424c7c0jorton/* If requested, create a fake basic authentication header for the benefit
645377709717e10daeb3b57531340ce14424c7c0jorton * of a proxy or application running behind this server.
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive auth_basic_config_rec *conf = ap_get_module_config(r->per_dir_config,
e41a440220bb41526fac54001a3ee94abb0d07a6rbowen ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02455)
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun "AuthBasicFake: could not evaluate user expression for URI '%s': %s", r->uri, err);
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02458)
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun "AuthBasicFake: empty username expression for URI '%s', ignoring", r->uri);
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02456)
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun "AuthBasicFake: could not evaluate password expression for URI '%s': %s", r->uri, err);
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02459)
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun "AuthBasicFake: empty password expression for URI '%s', ignoring", r->uri);
b47bddbe88fb1489893591d69d4ccab9b873af68humbedooh apr_table_setn(r->headers_in, "Authorization", auth_line);
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02457)
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun "AuthBasicFake: \"Authorization: %s\"",
e41a440220bb41526fac54001a3ee94abb0d07a6rbowen ap_hook_check_authn(authenticate_basic_user, NULL, NULL, APR_HOOK_MIDDLE,
60fdc5709bfeedd9539e279b5a5f46b3c610483bnilgun ap_hook_fixups(authenticate_basic_fake, NULL, NULL, APR_HOOK_LAST);
ca0e3098838c1f9aa77bcdfc3df99cf9aa0f9383slive ap_hook_note_auth_failure(hook_note_basic_auth_failure, NULL, NULL,
5f48875017569cc7610b17d852c44e02684d9d5aerikabele merge_auth_basic_dir_config, /* dir merger --- default is to override */