Apache HTTPD
mod_auth_basic.c
Go to the documentation of this file.
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
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 */
21#include "apr_want.h"
22
23#include "ap_config.h"
24#include "httpd.h"
25#include "http_config.h"
26#include "http_core.h"
27#include "http_log.h"
28#include "http_protocol.h"
29#include "http_request.h"
30#include "util_md5.h"
31#include "ap_provider.h"
32#include "ap_expr.h"
33
34#include "mod_auth.h"
35
47
49{
50 auth_basic_config_rec *conf = apr_pcalloc(p, sizeof(*conf));
51
52 /* Any failures are fatal. */
53 conf->authoritative = 1;
54
55 return conf;
56}
57
58static void *merge_auth_basic_dir_config(apr_pool_t *p, void *basev, void *overridesv)
59{
60 auth_basic_config_rec *newconf = apr_pcalloc(p, sizeof(*newconf));
62 auth_basic_config_rec *overrides = overridesv;
63
64 newconf->authoritative =
65 overrides->authoritative_set ? overrides->authoritative :
66 base->authoritative;
67 newconf->authoritative_set = overrides->authoritative_set
68 || base->authoritative_set;
69
70 newconf->fakeuser =
71 overrides->fake_set ? overrides->fakeuser : base->fakeuser;
72 newconf->fakepass =
73 overrides->fake_set ? overrides->fakepass : base->fakepass;
74 newconf->fake_set = overrides->fake_set || base->fake_set;
75
76 newconf->use_digest_algorithm =
78 : base->use_digest_algorithm;
80 overrides->use_digest_algorithm_set || base->use_digest_algorithm_set;
81
82 newconf->providers = overrides->providers ? overrides->providers : base->providers;
83
84 return newconf;
85}
86
87static const char *add_authn_provider(cmd_parms *cmd, void *config,
88 const char *arg)
89{
92
93 newp = apr_pcalloc(cmd->pool, sizeof(authn_provider_list));
94 newp->provider_name = arg;
95
96 /* lookup and cache the actual provider now */
98 newp->provider_name,
100
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);
107 }
108
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);
114 }
115
116 /* Add it to the list now. */
117 if (!conf->providers) {
118 conf->providers = newp;
119 }
120 else {
122
123 while (last->next) {
124 last = last->next;
125 }
126 last->next = newp;
127 }
128
129 return NULL;
130}
131
132static const char *set_authoritative(cmd_parms * cmd, void *config, int flag)
133{
135
136 conf->authoritative = flag;
137 conf->authoritative_set = 1;
138
139 return NULL;
140}
141
142static const char *add_basic_fake(cmd_parms * cmd, void *config,
143 const char *user, const char *pass)
144{
146 const char *err;
147
148 if (!strcasecmp(user, "off")) {
149 conf->fakeuser = NULL;
150 conf->fakepass = NULL;
151 conf->fake_set = 1;
152 }
153 else {
154 /* if password is unspecified, set it to the fixed string "password" to
155 * be compatible with the behaviour of mod_ssl.
156 */
157 if (!pass) {
158 pass = "password";
159 }
160
161 conf->fakeuser =
163 &err, NULL);
164 if (err) {
165 return apr_psprintf(cmd->pool,
166 "Could not parse fake username expression '%s': %s", user,
167 err);
168 }
169 conf->fakepass =
171 &err, NULL);
172 if (err) {
173 return apr_psprintf(cmd->pool,
174 "Could not parse fake password expression associated to user '%s': %s",
175 user, err);
176 }
177 conf->fake_set = 1;
178 }
179
180 return NULL;
181}
182
183static const char *set_use_digest_algorithm(cmd_parms *cmd, void *config,
184 const char *alg)
185{
187
188 if (strcasecmp(alg, "Off") && strcasecmp(alg, "MD5")) {
189 return apr_pstrcat(cmd->pool,
190 "Invalid algorithm in "
191 "AuthBasicUseDigestAlgorithm: ", alg, NULL);
192 }
193
194 conf->use_digest_algorithm = alg;
195 conf->use_digest_algorithm_set = 1;
196
197 return NULL;
198}
199
201{
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'"),
215 {NULL}
216};
217
218module AP_MODULE_DECLARE_DATA auth_basic_module;
219
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.
224 *
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
227 * reported as such.
228 */
229
231{
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),
236 "\"", NULL));
237}
238
239static int hook_note_basic_auth_failure(request_rec *r, const char *auth_type)
240{
241 if (ap_cstr_casecmp(auth_type, "Basic"))
242 return DECLINED;
243
245 return OK;
246}
247
248static int get_basic_auth(request_rec *r, const char **user,
249 const char **pw)
250{
251 const char *auth_line;
252 char *decoded_line;
253
254 /* Get the appropriate header */
255 auth_line = apr_table_get(r->headers_in, (PROXYREQ_PROXY == r->proxyreq)
256 ? "Proxy-Authorization"
257 : "Authorization");
258
259 if (!auth_line) {
261 return HTTP_UNAUTHORIZED;
262 }
263
264 if (ap_cstr_casecmp(ap_getword(r->pool, &auth_line, ' '), "Basic")) {
265 /* Client tried to authenticate using wrong auth scheme */
267 "client used wrong authentication scheme: %s", r->uri);
269 return HTTP_UNAUTHORIZED;
270 }
271
272 /* Skip leading spaces. */
273 while (*auth_line == ' ' || *auth_line == '\t') {
274 auth_line++;
275 }
276
277 decoded_line = ap_pbase64decode(r->pool, auth_line);
278
279 *user = ap_getword_nulls(r->pool, (const char**)&decoded_line, ':');
280 *pw = decoded_line;
281
282 /* set the user, even though the user is unauthenticated at this point */
283 r->user = (char *) *user;
284
285 return OK;
286}
287
288/* Determine user ID, and check if it really is that user, for HTTP
289 * basic authentication...
290 */
292{
294 &auth_basic_module);
295 const char *sent_user, *sent_pw, *current_auth;
296 const char *realm = NULL;
297 const char *digest = NULL;
298 int res;
299 authn_status auth_result;
300 authn_provider_list *current_provider;
301
302 /* Are we configured to be Basic auth? */
303 current_auth = ap_auth_type(r);
304 if (!current_auth || ap_cstr_casecmp(current_auth, "Basic")) {
305 return DECLINED;
306 }
307
308 /* We need an authentication realm. */
309 if (!ap_auth_name(r)) {
311 "need AuthName: %s", r->uri);
313 }
314
315 r->ap_auth_type = (char*)current_auth;
316
317 res = get_basic_auth(r, &sent_user, &sent_pw);
318 if (res) {
319 return res;
320 }
321
322 if (conf->use_digest_algorithm
323 && !ap_cstr_casecmp(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, ":",
327 realm, ":",
328 sent_pw, NULL));
329 }
330
331 current_provider = conf->providers;
332 do {
333 const authn_provider *provider;
334
335 /* For now, if a provider isn't set, we'll be nice and use the file
336 * provider.
337 */
338 if (!current_provider) {
342
343 if (!provider || !provider->check_password) {
345 "No Authn provider configured");
346 auth_result = AUTH_GENERAL_ERROR;
347 break;
348 }
350 }
351 else {
352 provider = current_provider->provider;
353 apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, current_provider->provider_name);
354 }
355
356 if (digest) {
357 char *password;
358
359 if (!provider->get_realm_hash) {
361 "Authn provider does not support "
362 "AuthBasicUseDigestAlgorithm");
363 auth_result = AUTH_GENERAL_ERROR;
364 break;
365 }
366 /* We expect the password to be hash of user:realm:password */
367 auth_result = provider->get_realm_hash(r, sent_user, realm,
368 &password);
369 if (auth_result == AUTH_USER_FOUND) {
370 auth_result = strcmp(digest, password) ? AUTH_DENIED
371 : AUTH_GRANTED;
372 }
373 }
374 else {
375 auth_result = provider->check_password(r, sent_user, sent_pw);
376 }
377
378 apr_table_unset(r->notes, AUTHN_PROVIDER_NAME_NOTE);
379
380 /* Something occurred. Stop checking. */
381 if (auth_result != AUTH_USER_NOT_FOUND) {
382 break;
383 }
384
385 /* If we're not really configured for providers, stop now. */
386 if (!conf->providers) {
387 break;
388 }
389
390 current_provider = current_provider->next;
391 } while (current_provider);
392
393 if (auth_result != AUTH_GRANTED) {
394 int return_code;
395
396 /* If we're not authoritative, then any error is ignored. */
397 if (!(conf->authoritative) && auth_result != AUTH_DENIED) {
398 return DECLINED;
399 }
400
401 switch (auth_result) {
402 case AUTH_DENIED:
404 "user %s: authentication failure for \"%s\": "
405 "Password Mismatch",
406 sent_user, r->uri);
407 return_code = HTTP_UNAUTHORIZED;
408 break;
411 "user %s not found: %s", sent_user, r->uri);
412 return_code = HTTP_UNAUTHORIZED;
413 break;
415 default:
416 /* We'll assume that the module has already said what its error
417 * was in the logs.
418 */
419 return_code = HTTP_INTERNAL_SERVER_ERROR;
420 break;
421 }
422
423 /* If we're returning 401, tell them to try again. */
424 if (return_code == HTTP_UNAUTHORIZED) {
426 }
427 return return_code;
428 }
429
430 return OK;
431}
432
433/* If requested, create a fake basic authentication header for the benefit
434 * of a proxy or application running behind this server.
435 */
437{
438 const char *auth_line, *user, *pass, *err;
440 &auth_basic_module);
441
442 if (!conf->fakeuser) {
443 return DECLINED;
444 }
445
446 user = ap_expr_str_exec(r, conf->fakeuser, &err);
447 if (err) {
449 "AuthBasicFake: could not evaluate user expression for URI '%s': %s", r->uri, err);
451 }
452 if (!user || !*user) {
454 "AuthBasicFake: empty username expression for URI '%s', ignoring", r->uri);
455
456 apr_table_unset(r->headers_in, "Authorization");
457
458 return DECLINED;
459 }
460
461 pass = ap_expr_str_exec(r, conf->fakepass, &err);
462 if (err) {
464 "AuthBasicFake: could not evaluate password expression for URI '%s': %s", r->uri, err);
466 }
467 if (!pass || !*pass) {
469 "AuthBasicFake: empty password expression for URI '%s', ignoring", r->uri);
470
471 apr_table_unset(r->headers_in, "Authorization");
472
473 return DECLINED;
474 }
475
476 auth_line = apr_pstrcat(r->pool, "Basic ",
478 apr_pstrcat(r->pool, user,
479 ":", pass, NULL)),
480 NULL);
481 apr_table_setn(r->headers_in, "Authorization", auth_line);
482
484 "AuthBasicFake: \"Authorization: %s\"",
485 auth_line);
486
487 return OK;
488}
489
498
499AP_DECLARE_MODULE(auth_basic) =
500{
502 create_auth_basic_dir_config, /* dir config creater */
503 merge_auth_basic_dir_config, /* dir merger --- default is to override */
504 NULL, /* server config */
505 NULL, /* merge server config */
506 auth_basic_cmds, /* command apr_table_t */
507 register_hooks /* register hooks */
508};
Symbol export macros and hook functions.
Expression parser.
Apache Provider API.
APR-UTIL Base64 Encoding.
APR general purpose library routines.
apr_size_t const unsigned char unsigned int unsigned int d
Definition apr_siphash.h:72
APR Strings library.
APR Standard Headers Support.
#define AP_INIT_TAKE1(directive, func, mconfig, where, help)
#define ap_get_module_config(v, m)
#define AP_DECLARE_MODULE(foo)
#define AP_INIT_FLAG(directive, func, mconfig, where, help)
ap_conf_vector_t * base
#define AP_INIT_ITERATE(directive, func, mconfig, where, help)
#define AP_INIT_TAKE12(directive, func, mconfig, where, help)
request_rec * r
#define DECLINED
Definition httpd.h:457
#define OK
Definition httpd.h:456
const char * ap_auth_name(request_rec *r)
Definition core.c:807
const char * ap_auth_type(request_rec *r)
Definition core.c:793
#define APLOGNO(n)
Definition http_log.h:117
#define APLOG_INFO
Definition http_log.h:70
#define ap_log_rerror
Definition http_log.h:454
#define APLOG_ERR
Definition http_log.h:67
#define APLOG_MARK
Definition http_log.h:283
void ap_hook_note_auth_failure(ap_HOOK_note_auth_failure_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition protocol.c:2594
void * ap_lookup_provider(const char *provider_group, const char *provider_name, const char *provider_version)
Definition provider.c:99
#define AP_AUTH_INTERNAL_PER_CONF
void ap_hook_fixups(ap_HOOK_fixups_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition request.c:87
void ap_hook_check_authn(ap_HOOK_check_user_id_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder, int type)
Definition request.c:2218
void const char * arg
Definition http_vhost.h:63
apr_pool_t apr_dbd_t apr_dbd_results_t ** res
Definition apr_dbd.h:287
#define APR_HOOK_LAST
Definition apr_hooks.h:305
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
#define AP_EXPR_FLAG_STRING_RESULT
Definition ap_expr.h:68
#define ap_expr_parse_cmd(cmd, expr, flags, err, lookup_fn)
Definition ap_expr.h:340
const char * ap_expr_str_exec(request_rec *r, const ap_expr_info_t *expr, const char **err)
#define OR_AUTHCFG
#define HTTP_INTERNAL_SERVER_ERROR
Definition httpd.h:535
#define HTTP_UNAUTHORIZED
Definition httpd.h:509
#define STANDARD20_MODULE_STUFF
int ap_cstr_casecmp(const char *s1, const char *s2)
Definition util.c:3542
char * ap_pbase64encode(apr_pool_t *p, char *string)
Definition util.c:2487
char * ap_getword(apr_pool_t *p, const char **line, char stop)
Definition util.c:723
char * ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
Definition util.c:2477
char * ap_getword_nulls(apr_pool_t *p, const char **line, char stop)
Definition util.c:779
#define PROXYREQ_PROXY
Definition httpd.h:1134
const char apr_int32_t flag
int strcasecmp(const char *a, const char *b)
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
const char char ** last
const char const char * password
apr_int32_t apr_int32_t apr_int32_t err
apr_cmdtype_e cmd
Apache Configuration.
CORE HTTP Daemon.
Apache Logging library.
HTTP protocol handling.
Apache Request library.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
Authentication and Authorization Extension for Apache.
#define AUTHN_PROVIDER_NAME_NOTE
Definition mod_auth.h:45
#define AUTHN_PROVIDER_VERSION
Definition mod_auth.h:41
#define AUTHN_PROVIDER_GROUP
Definition mod_auth.h:39
#define AUTHN_DEFAULT_PROVIDER
Definition mod_auth.h:43
authn_status
Definition mod_auth.h:64
@ AUTH_GRANTED
Definition mod_auth.h:66
@ AUTH_DENIED
Definition mod_auth.h:65
@ AUTH_USER_FOUND
Definition mod_auth.h:67
@ AUTH_GENERAL_ERROR
Definition mod_auth.h:69
@ AUTH_USER_NOT_FOUND
Definition mod_auth.h:68
static int authenticate_basic_fake(request_rec *r)
static void note_basic_auth_failure(request_rec *r)
static void * merge_auth_basic_dir_config(apr_pool_t *p, void *basev, void *overridesv)
static const char * add_authn_provider(cmd_parms *cmd, void *config, const char *arg)
static void register_hooks(apr_pool_t *p)
static const char * add_basic_fake(cmd_parms *cmd, void *config, const char *user, const char *pass)
static int hook_note_basic_auth_failure(request_rec *r, const char *auth_type)
static const char * set_authoritative(cmd_parms *cmd, void *config, int flag)
static const command_rec auth_basic_cmds[]
static void * create_auth_basic_dir_config(apr_pool_t *p, char *d)
static int get_basic_auth(request_rec *r, const char **user, const char **pw)
static int authenticate_basic_user(request_rec *r)
static const char * set_use_digest_algorithm(cmd_parms *cmd, void *config, const char *alg)
return NULL
Definition mod_so.c:359
authn_provider_list * providers
unsigned int use_digest_algorithm_set
ap_expr_info_t * fakeuser
ap_expr_info_t * fakepass
const char * use_digest_algorithm
unsigned int authoritative_set
authn_provider_list * next
Definition mod_auth.h:100
const authn_provider * provider
Definition mod_auth.h:99
const char * provider_name
Definition mod_auth.h:98
authn_status(* get_realm_hash)(request_rec *r, const char *user, const char *realm, char **rethash)
Definition mod_auth.h:90
authn_status(* check_password)(request_rec *r, const char *user, const char *password)
Definition mod_auth.h:84
A structure that represents the current request.
Definition httpd.h:845
char * user
Definition httpd.h:1005
char * uri
Definition httpd.h:1016
apr_table_t * notes
Definition httpd.h:985
apr_pool_t * pool
Definition httpd.h:847
int proxyreq
Definition httpd.h:873
apr_table_t * err_headers_out
Definition httpd.h:981
apr_table_t * headers_in
Definition httpd.h:976
struct ap_conf_vector_t * per_dir_config
Definition httpd.h:1047
char * ap_auth_type
Definition httpd.h:1007
const char * digest
Definition testmd5.c:30
char * ap_md5(apr_pool_t *p, const unsigned char *string)
Definition util_md5.c:75
Apache MD5 library.