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_md5.h" /* for apr_password_validate */
19
#include "apr_lib.h" /* for apr_isspace */
20
#include "apr_base64.h" /* for apr_base64_decode et al */
21
#define APR_WANT_STRFUNC /* for strcasecmp */
24
#include "ap_config.h"
26
#include "http_config.h"
27
#include "http_core.h"
29
#include "http_protocol.h"
30
#include "http_request.h"
31
#include "ap_provider.h"
36
authn_provider_list *providers;
39
} auth_basic_config_rec;
41
static void *create_auth_basic_dir_config(apr_pool_t *p, char *d)
43
auth_basic_config_rec *conf = apr_pcalloc(p, sizeof(*conf));
46
/* Any failures are fatal. */
47
conf->authoritative = 1;
52
static const char *add_authn_provider(cmd_parms *cmd, void *config,
55
auth_basic_config_rec *conf = (auth_basic_config_rec*)config;
56
authn_provider_list *newp;
58
newp = apr_pcalloc(cmd->pool, sizeof(authn_provider_list));
59
newp->provider_name = apr_pstrdup(cmd->pool, arg);
61
/* lookup and cache the actual provider now */
62
newp->provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP,
63
newp->provider_name, "0");
65
if (newp->provider == NULL) {
66
/* by the time they use it, the provider should be loaded and
67
registered with us. */
68
return apr_psprintf(cmd->pool,
69
"Unknown Authn provider: %s",
73
if (!newp->provider->check_password) {
74
/* if it doesn't provide the appropriate function, reject it */
75
return apr_psprintf(cmd->pool,
76
"The '%s' Authn provider doesn't support "
77
"Basic Authentication", newp->provider_name);
80
/* Add it to the list now. */
81
if (!conf->providers) {
82
conf->providers = newp;
85
authn_provider_list *last = conf->providers;
96
static const command_rec auth_basic_cmds[] =
98
AP_INIT_ITERATE("AuthBasicProvider", add_authn_provider, NULL, OR_AUTHCFG,
99
"specify the auth providers for a directory or location"),
100
AP_INIT_FLAG("AuthBasicAuthoritative", ap_set_flag_slot,
101
(void *)APR_OFFSETOF(auth_basic_config_rec, authoritative),
103
"Set to 'Off' to allow access control to be passed along to "
104
"lower modules if the UserID is not known to this module"),
108
module AP_MODULE_DECLARE_DATA auth_basic_module;
110
/* These functions return 0 if client is OK, and proper error status
111
* if not... either HTTP_UNAUTHORIZED, if we made a check, and it failed, or
112
* HTTP_INTERNAL_SERVER_ERROR, if things are so totally confused that we
113
* couldn't figure out how to tell if the client is authorized or not.
115
* If they return DECLINED, and all other modules also decline, that's
116
* treated by the server core as a configuration error, logged and
120
static void note_basic_auth_failure(request_rec *r)
122
apr_table_setn(r->err_headers_out,
123
(PROXYREQ_PROXY == r->proxyreq) ? "Proxy-Authenticate"
124
: "WWW-Authenticate",
125
apr_pstrcat(r->pool, "Basic realm=\"", ap_auth_name(r),
129
static int get_basic_auth(request_rec *r, const char **user,
132
const char *auth_line;
136
/* Get the appropriate header */
137
auth_line = apr_table_get(r->headers_in, (PROXYREQ_PROXY == r->proxyreq)
138
? "Proxy-Authorization"
142
note_basic_auth_failure(r);
143
return HTTP_UNAUTHORIZED;
146
if (strcasecmp(ap_getword(r->pool, &auth_line, ' '), "Basic")) {
147
/* Client tried to authenticate using wrong auth scheme */
148
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
149
"client used wrong authentication scheme: %s", r->uri);
150
note_basic_auth_failure(r);
151
return HTTP_UNAUTHORIZED;
154
/* Skip leading spaces. */
155
while (apr_isspace(*auth_line)) {
159
decoded_line = apr_palloc(r->pool, apr_base64_decode_len(auth_line) + 1);
160
length = apr_base64_decode(decoded_line, auth_line);
161
/* Null-terminate the string. */
162
decoded_line[length] = '\0';
164
*user = ap_getword_nulls(r->pool, (const char**)&decoded_line, ':');
167
/* set the user, even though the user is unauthenticated at this point */
168
r->user = (char *) *user;
173
/* Determine user ID, and check if it really is that user, for HTTP
174
* basic authentication...
176
static int authenticate_basic_user(request_rec *r)
178
auth_basic_config_rec *conf = ap_get_module_config(r->per_dir_config,
180
const char *sent_user, *sent_pw, *current_auth;
182
authn_status auth_result;
183
authn_provider_list *current_provider;
185
/* Are we configured to be Basic auth? */
186
current_auth = ap_auth_type(r);
187
if (!current_auth || strcasecmp(current_auth, "Basic")) {
191
/* We need an authentication realm. */
192
if (!ap_auth_name(r)) {
193
ap_log_rerror(APLOG_MARK, APLOG_ERR,
194
0, r, "need AuthName: %s", r->uri);
195
return HTTP_INTERNAL_SERVER_ERROR;
198
r->ap_auth_type = "Basic";
200
res = get_basic_auth(r, &sent_user, &sent_pw);
205
current_provider = conf->providers;
207
const authn_provider *provider;
209
/* For now, if a provider isn't set, we'll be nice and use the file
212
if (!current_provider) {
213
provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP,
214
AUTHN_DEFAULT_PROVIDER, "0");
216
if (!provider || !provider->check_password) {
217
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
218
"No Authn provider configured");
219
auth_result = AUTH_GENERAL_ERROR;
222
apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, AUTHN_DEFAULT_PROVIDER);
225
provider = current_provider->provider;
226
apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, current_provider->provider_name);
230
auth_result = provider->check_password(r, sent_user, sent_pw);
232
apr_table_unset(r->notes, AUTHN_PROVIDER_NAME_NOTE);
234
/* Something occured. Stop checking. */
235
if (auth_result != AUTH_USER_NOT_FOUND) {
239
/* If we're not really configured for providers, stop now. */
240
if (!conf->providers) {
244
current_provider = current_provider->next;
245
} while (current_provider);
247
if (auth_result != AUTH_GRANTED) {
250
/* If we're not authoritative, then any error is ignored. */
251
if (!(conf->authoritative) && auth_result != AUTH_DENIED) {
255
switch (auth_result) {
257
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
258
"user %s: authentication failure for \"%s\": "
261
return_code = HTTP_UNAUTHORIZED;
263
case AUTH_USER_NOT_FOUND:
264
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
265
"user %s not found: %s", sent_user, r->uri);
266
return_code = HTTP_UNAUTHORIZED;
268
case AUTH_GENERAL_ERROR:
270
/* We'll assume that the module has already said what its error
273
return_code = HTTP_INTERNAL_SERVER_ERROR;
277
/* If we're returning 403, tell them to try again. */
278
if (return_code == HTTP_UNAUTHORIZED) {
279
note_basic_auth_failure(r);
287
static void register_hooks(apr_pool_t *p)
289
ap_hook_check_user_id(authenticate_basic_user,NULL,NULL,APR_HOOK_MIDDLE);
292
module AP_MODULE_DECLARE_DATA auth_basic_module =
294
STANDARD20_MODULE_STUFF,
295
create_auth_basic_dir_config, /* dir config creater */
296
NULL, /* dir merger --- default is to override */
297
NULL, /* server config */
298
NULL, /* merge server config */
299
auth_basic_cmds, /* command apr_table_t */
300
register_hooks /* register hooks */