1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "apr_strings.h"
18 #include "apr_lib.h" /* for apr_isspace */
19 #include "apr_base64.h" /* for apr_base64_decode et al */
20 #define APR_WANT_STRFUNC /* for strcasecmp */
23 #include "ap_config.h"
25 #include "http_config.h"
26 #include "http_core.h"
28 #include "http_protocol.h"
29 #include "http_request.h"
31 #include "ap_provider.h"
37 authn_provider_list *providers;
38 char *dir; /* unused variable */
40 ap_expr_info_t *fakeuser;
41 ap_expr_info_t *fakepass;
42 const char *use_digest_algorithm;
43 unsigned int fake_set:1,
44 use_digest_algorithm_set:1,
46 } auth_basic_config_rec;
48 static void *create_auth_basic_dir_config(apr_pool_t *p, char *d)
50 auth_basic_config_rec *conf = apr_pcalloc(p, sizeof(*conf));
52 /* Any failures are fatal. */
53 conf->authoritative = 1;
58 static void *merge_auth_basic_dir_config(apr_pool_t *p, void *basev, void *overridesv)
60 auth_basic_config_rec *newconf = apr_pcalloc(p, sizeof(*newconf));
61 auth_basic_config_rec *base = basev;
62 auth_basic_config_rec *overrides = overridesv;
64 newconf->authoritative =
65 overrides->authoritative_set ? overrides->authoritative :
67 newconf->authoritative_set = overrides->authoritative_set
68 || base->authoritative_set;
71 overrides->fake_set ? overrides->fakeuser : base->fakeuser;
73 overrides->fake_set ? overrides->fakepass : base->fakepass;
74 newconf->fake_set = overrides->fake_set || base->fake_set;
76 newconf->use_digest_algorithm =
77 overrides->use_digest_algorithm_set ? overrides->use_digest_algorithm
78 : base->use_digest_algorithm;
79 newconf->use_digest_algorithm_set =
80 overrides->use_digest_algorithm_set || base->use_digest_algorithm_set;
82 newconf->providers = overrides->providers ? overrides->providers : base->providers;
87 static const char *add_authn_provider(cmd_parms *cmd, void *config,
90 auth_basic_config_rec *conf = (auth_basic_config_rec*)config;
91 authn_provider_list *newp;
93 newp = apr_pcalloc(cmd->pool, sizeof(authn_provider_list));
94 newp->provider_name = arg;
96 /* lookup and cache the actual provider now */
97 newp->provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP,
99 AUTHN_PROVIDER_VERSION);
101 if (newp->provider == NULL) {
102 /* by the time they use it, the provider should be loaded and
103 registered with us. */
104 return apr_psprintf(cmd->pool,
105 "Unknown Authn provider: %s",
106 newp->provider_name);
109 if (!newp->provider->check_password) {
110 /* if it doesn't provide the appropriate function, reject it */
111 return apr_psprintf(cmd->pool,
112 "The '%s' Authn provider doesn't support "
113 "Basic Authentication", newp->provider_name);
116 /* Add it to the list now. */
117 if (!conf->providers) {
118 conf->providers = newp;
121 authn_provider_list *last = conf->providers;
132 static const char *set_authoritative(cmd_parms * cmd, void *config, int flag)
134 auth_basic_config_rec *conf = (auth_basic_config_rec *) config;
136 conf->authoritative = flag;
137 conf->authoritative_set = 1;
142 static const char *add_basic_fake(cmd_parms * cmd, void *config,
143 const char *user, const char *pass)
145 auth_basic_config_rec *conf = (auth_basic_config_rec *) config;
148 if (!strcasecmp(user, "off")) {
149 conf->fakeuser = NULL;
150 conf->fakepass = NULL;
154 /* if password is unspecified, set it to the fixed string "password" to
155 * be compatible with the behaviour of mod_ssl.
162 ap_expr_parse_cmd(cmd, user, AP_EXPR_FLAG_STRING_RESULT,
165 return apr_psprintf(cmd->pool,
166 "Could not parse fake username expression '%s': %s", user,
170 ap_expr_parse_cmd(cmd, pass, AP_EXPR_FLAG_STRING_RESULT,
173 return apr_psprintf(cmd->pool,
174 "Could not parse fake password expression '%s': %s", pass,
183 static const char *set_use_digest_algorithm(cmd_parms *cmd, void *config,
186 auth_basic_config_rec *conf = (auth_basic_config_rec *)config;
188 if (strcasecmp(alg, "Off") && strcasecmp(alg, "MD5")) {
189 return apr_pstrcat(cmd->pool,
190 "Invalid algorithm in "
191 "AuthBasicUseDigestAlgorithm: ", alg, NULL);
194 conf->use_digest_algorithm = alg;
195 conf->use_digest_algorithm_set = 1;
200 static const command_rec auth_basic_cmds[] =
202 AP_INIT_ITERATE("AuthBasicProvider", add_authn_provider, NULL, OR_AUTHCFG,
203 "specify the auth providers for a directory or location"),
204 AP_INIT_FLAG("AuthBasicAuthoritative", set_authoritative, NULL, OR_AUTHCFG,
205 "Set to 'Off' to allow access control to be passed along to "
206 "lower modules if the UserID is not known to this module"),
207 AP_INIT_TAKE12("AuthBasicFake", add_basic_fake, NULL, OR_AUTHCFG,
208 "Fake basic authentication using the given expressions for "
209 "username and password, 'off' to disable. Password defaults "
210 "to 'password' if missing."),
211 AP_INIT_TAKE1("AuthBasicUseDigestAlgorithm", set_use_digest_algorithm,
213 "Set to 'MD5' to use the auth provider's authentication "
214 "check for digest auth, using a hash of 'user:realm:pass'"),
218 module AP_MODULE_DECLARE_DATA auth_basic_module;
220 /* These functions return 0 if client is OK, and proper error status
221 * if not... either HTTP_UNAUTHORIZED, if we made a check, and it failed, or
222 * HTTP_INTERNAL_SERVER_ERROR, if things are so totally confused that we
223 * couldn't figure out how to tell if the client is authorized or not.
225 * If they return DECLINED, and all other modules also decline, that's
226 * treated by the server core as a configuration error, logged and
230 static void note_basic_auth_failure(request_rec *r)
232 apr_table_setn(r->err_headers_out,
233 (PROXYREQ_PROXY == r->proxyreq) ? "Proxy-Authenticate"
234 : "WWW-Authenticate",
235 apr_pstrcat(r->pool, "Basic realm=\"", ap_auth_name(r),
239 static int hook_note_basic_auth_failure(request_rec *r, const char *auth_type)
241 if (ap_casecmpstr(auth_type, "Basic"))
244 note_basic_auth_failure(r);
248 static int get_basic_auth(request_rec *r, const char **user,
251 const char *auth_line;
254 /* Get the appropriate header */
255 auth_line = apr_table_get(r->headers_in, (PROXYREQ_PROXY == r->proxyreq)
256 ? "Proxy-Authorization"
260 note_basic_auth_failure(r);
261 return HTTP_UNAUTHORIZED;
264 if (ap_casecmpstr(ap_getword(r->pool, &auth_line, ' '), "Basic")) {
265 /* Client tried to authenticate using wrong auth scheme */
266 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01614)
267 "client used wrong authentication scheme: %s", r->uri);
268 note_basic_auth_failure(r);
269 return HTTP_UNAUTHORIZED;
272 /* Skip leading spaces. */
273 while (apr_isspace(*auth_line)) {
277 decoded_line = ap_pbase64decode(r->pool, auth_line);
279 *user = ap_getword_nulls(r->pool, (const char**)&decoded_line, ':');
282 /* set the user, even though the user is unauthenticated at this point */
283 r->user = (char *) *user;
288 /* Determine user ID, and check if it really is that user, for HTTP
289 * basic authentication...
291 static int authenticate_basic_user(request_rec *r)
293 auth_basic_config_rec *conf = ap_get_module_config(r->per_dir_config,
295 const char *sent_user, *sent_pw, *current_auth;
296 const char *realm = NULL;
297 const char *digest = NULL;
299 authn_status auth_result;
300 authn_provider_list *current_provider;
302 /* Are we configured to be Basic auth? */
303 current_auth = ap_auth_type(r);
304 if (!current_auth || ap_casecmpstr(current_auth, "Basic")) {
308 /* We need an authentication realm. */
309 if (!ap_auth_name(r)) {
310 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01615)
311 "need AuthName: %s", r->uri);
312 return HTTP_INTERNAL_SERVER_ERROR;
315 r->ap_auth_type = (char*)current_auth;
317 res = get_basic_auth(r, &sent_user, &sent_pw);
322 if (conf->use_digest_algorithm
323 && !strcasecmp(conf->use_digest_algorithm, "MD5")) {
324 realm = ap_auth_name(r);
325 digest = ap_md5(r->pool,
326 (unsigned char *)apr_pstrcat(r->pool, sent_user, ":",
331 current_provider = conf->providers;
333 const authn_provider *provider;
335 /* For now, if a provider isn't set, we'll be nice and use the file
338 if (!current_provider) {
339 provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP,
340 AUTHN_DEFAULT_PROVIDER,
341 AUTHN_PROVIDER_VERSION);
343 if (!provider || !provider->check_password) {
344 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01616)
345 "No Authn provider configured");
346 auth_result = AUTH_GENERAL_ERROR;
349 apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, AUTHN_DEFAULT_PROVIDER);
352 provider = current_provider->provider;
353 apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, current_provider->provider_name);
359 if (!provider->get_realm_hash) {
360 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02493)
361 "Authn provider does not support "
362 "AuthBasicUseDigestAlgorithm");
363 auth_result = AUTH_GENERAL_ERROR;
366 /* We expect the password to be hash of user:realm:password */
367 auth_result = provider->get_realm_hash(r, sent_user, realm,
369 if (auth_result == AUTH_USER_FOUND) {
370 auth_result = strcmp(digest, password) ? AUTH_DENIED
375 auth_result = provider->check_password(r, sent_user, sent_pw);
378 apr_table_unset(r->notes, AUTHN_PROVIDER_NAME_NOTE);
380 /* Something occured. Stop checking. */
381 if (auth_result != AUTH_USER_NOT_FOUND) {
385 /* If we're not really configured for providers, stop now. */
386 if (!conf->providers) {
390 current_provider = current_provider->next;
391 } while (current_provider);
393 if (auth_result != AUTH_GRANTED) {
396 /* If we're not authoritative, then any error is ignored. */
397 if (!(conf->authoritative) && auth_result != AUTH_DENIED) {
401 switch (auth_result) {
403 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01617)
404 "user %s: authentication failure for \"%s\": "
407 return_code = HTTP_UNAUTHORIZED;
409 case AUTH_USER_NOT_FOUND:
410 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01618)
411 "user %s not found: %s", sent_user, r->uri);
412 return_code = HTTP_UNAUTHORIZED;
415 return_code = r->status;
417 case AUTH_GENERAL_ERROR:
419 /* We'll assume that the module has already said what its error
422 return_code = HTTP_INTERNAL_SERVER_ERROR;
426 /* If we're returning 401, tell them to try again. */
427 if (return_code == HTTP_UNAUTHORIZED) {
428 note_basic_auth_failure(r);
436 /* If requested, create a fake basic authentication header for the benefit
437 * of a proxy or application running behind this server.
439 static int authenticate_basic_fake(request_rec *r)
441 const char *auth_line, *user, *pass, *err;
442 auth_basic_config_rec *conf = ap_get_module_config(r->per_dir_config,
445 if (!conf->fakeuser) {
449 user = ap_expr_str_exec(r, conf->fakeuser, &err);
451 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02455)
452 "AuthBasicFake: could not evaluate user expression for URI '%s': %s", r->uri, err);
453 return HTTP_INTERNAL_SERVER_ERROR;
455 if (!user || !*user) {
456 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02458)
457 "AuthBasicFake: empty username expression for URI '%s', ignoring", r->uri);
459 apr_table_unset(r->headers_in, "Authorization");
464 pass = ap_expr_str_exec(r, conf->fakepass, &err);
466 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02456)
467 "AuthBasicFake: could not evaluate password expression for URI '%s': %s", r->uri, err);
468 return HTTP_INTERNAL_SERVER_ERROR;
470 if (!pass || !*pass) {
471 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02459)
472 "AuthBasicFake: empty password expression for URI '%s', ignoring", r->uri);
474 apr_table_unset(r->headers_in, "Authorization");
479 auth_line = apr_pstrcat(r->pool, "Basic ",
480 ap_pbase64encode(r->pool,
481 apr_pstrcat(r->pool, user,
484 apr_table_setn(r->headers_in, "Authorization", auth_line);
486 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02457)
487 "AuthBasicFake: \"Authorization: %s\"",
493 static void register_hooks(apr_pool_t *p)
495 ap_hook_check_authn(authenticate_basic_user, NULL, NULL, APR_HOOK_MIDDLE,
496 AP_AUTH_INTERNAL_PER_CONF);
497 ap_hook_fixups(authenticate_basic_fake, NULL, NULL, APR_HOOK_LAST);
498 ap_hook_note_auth_failure(hook_note_basic_auth_failure, NULL, NULL,
502 AP_DECLARE_MODULE(auth_basic) =
504 STANDARD20_MODULE_STUFF,
505 create_auth_basic_dir_config, /* dir config creater */
506 merge_auth_basic_dir_config, /* dir merger --- default is to override */
507 NULL, /* server config */
508 NULL, /* merge server config */
509 auth_basic_cmds, /* command apr_table_t */
510 register_hooks /* register hooks */