Apache HTTPD
mod_authn_core.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/*
18 * Security options etc.
19 *
20 * Module derived from code originally written by Rob McCool
21 *
22 */
23
24#include "apr_strings.h"
25#include "apr_network_io.h"
26#define APR_WANT_STRFUNC
27#define APR_WANT_BYTEFUNC
28#include "apr_want.h"
29
30#include "ap_config.h"
31#include "httpd.h"
32#include "http_config.h"
33#include "http_core.h"
34#include "http_log.h"
35#include "http_request.h"
36#include "http_protocol.h"
37#include "ap_expr.h"
38#include "ap_provider.h"
39
40#include "mod_auth.h"
41
42#if APR_HAVE_NETINET_IN_H
43#include <netinet/in.h>
44#endif
45
46/* TODO List
47
48- Track down all of the references to r->ap_auth_type
49 and change them to ap_auth_type()
50- Remove ap_auth_type and ap_auth_name from the
51 request_rec
52
53*/
54
60
67
71
72
73module AP_MODULE_DECLARE_DATA authn_core_module;
74
76{
79
80 return (void *)conf;
81}
82
84{
89
90 if (new->auth_type_set) {
91 conf->ap_auth_type = new->ap_auth_type;
92 conf->auth_type_set = 1;
93 }
94 else {
95 conf->ap_auth_type = base->ap_auth_type;
96 conf->auth_type_set = base->auth_type_set;
97 }
98
99 if (new->ap_auth_name) {
100 conf->ap_auth_name = new->ap_auth_name;
101 } else {
102 conf->ap_auth_name = base->ap_auth_name;
103 }
104
105 return (void*)conf;
106}
107
109 const char *password)
110{
111 /* Look up the provider alias in the alias list */
112 /* Get the dir_config and call ap_Merge_per_dir_configs() */
113 /* Call the real provider->check_password() function */
114 /* return the result of the above function call */
115
116 const char *provider_name = apr_table_get(r->notes, AUTHN_PROVIDER_NAME_NOTE);
120 &authn_core_module);
121
122 if (provider_name) {
124 provider_name, APR_HASH_KEY_STRING);
126
127 /* If we found the alias provider in the list, then merge the directory
128 configurations and call the real provider */
129 if (prvdraliasrec) {
131 prvdraliasrec->sec_auth);
132 ret = prvdraliasrec->provider->check_password(r,user,password);
134 }
135 }
136
137 return ret;
138}
139
141 const char *realm, char **rethash)
142{
143 /* Look up the provider alias in the alias list */
144 /* Get the dir_config and call ap_Merge_per_dir_configs() */
145 /* Call the real provider->get_realm_hash() function */
146 /* return the result of the above function call */
147
148 const char *provider_name = apr_table_get(r->notes, AUTHN_PROVIDER_NAME_NOTE);
152 &authn_core_module);
153
154 if (provider_name) {
156 provider_name, APR_HASH_KEY_STRING);
158
159 /* If we found the alias provider in the list, then merge the directory
160 configurations and call the real provider */
161 if (prvdraliasrec) {
163 prvdraliasrec->sec_auth);
164 ret = prvdraliasrec->provider->get_realm_hash(r,user,realm,rethash);
166 }
167 }
168
169 return ret;
170}
171
182
183/* Only per-server directive we have is GLOBAL_ONLY */
185{
186 return basev;
187}
188
194
200
201static const char *authaliassection(cmd_parms *cmd, void *mconfig, const char *arg)
202{
203 const char *endp = ap_strrchr_c(arg, '>');
204 const char *args;
205 char *provider_alias;
206 char *provider_name;
207 int old_overrides = cmd->override;
208 const char *errmsg;
209 const authn_provider *provider = NULL;
212 (authn_alias_srv_conf *)ap_get_module_config(cmd->server->module_config,
213 &authn_core_module);
214
215 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
216 if (err != NULL) {
217 return err;
218 }
219
220 if (endp == NULL) {
221 return apr_pstrcat(cmd->pool, cmd->cmd->name,
222 "> directive missing closing '>'", NULL);
223 }
224
225 args = apr_pstrndup(cmd->temp_pool, arg, endp - arg);
226
227 if (!args[0]) {
228 return apr_pstrcat(cmd->pool, cmd->cmd->name,
229 "> directive requires additional arguments", NULL);
230 }
231
232 /* Pull the real provider name and the alias name from the block header */
233 provider_name = ap_getword_conf(cmd->pool, &args);
234 provider_alias = ap_getword_conf(cmd->pool, &args);
235
236 if (!provider_name[0] || !provider_alias[0]) {
237 return apr_pstrcat(cmd->pool, cmd->cmd->name,
238 "> directive requires additional arguments", NULL);
239 }
240
241 if (strcasecmp(provider_name, provider_alias) == 0) {
242 return apr_pstrcat(cmd->pool,
243 "The alias provider name must be different from the base provider name.", NULL);
244 }
245
246 /* Look up the alias provider to make sure that it hasn't already been registered. */
247 provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP, provider_alias,
249 if (provider) {
250 return apr_pstrcat(cmd->pool, "The alias provider ", provider_alias,
251 " has already be registered previously as either a base provider or an alias provider.",
252 NULL);
253 }
254
255 /* walk the subsection configuration to get the per_dir config that we will
256 merge just before the real provider is called. */
257 cmd->override = OR_AUTHCFG | ACCESS_CONF;
258 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_auth_config);
259 cmd->override = old_overrides;
260
261 if (!errmsg) {
263 provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP, provider_name,
265
266 if (!provider) {
267 /* by the time they use it, the provider should be loaded and
268 registered with us. */
269 return apr_psprintf(cmd->pool,
270 "Unknown Authn provider: %s",
271 provider_name);
272 }
273
274 /* Save off the new directory config along with the original provider name
275 and function pointer data */
276 prvdraliasrec->sec_auth = new_auth_config;
277 prvdraliasrec->provider_name = provider_name;
278 prvdraliasrec->provider_alias = provider_alias;
279 prvdraliasrec->provider = provider;
280 apr_hash_set(authcfg->alias_rec, provider_alias, APR_HASH_KEY_STRING, prvdraliasrec);
281
282 /* Register the fake provider so that we get called first */
284 provider_alias, AUTHN_PROVIDER_VERSION,
285 provider->get_realm_hash ?
289 }
290
291 return errmsg;
292}
293
294/*
295 * Load an authorisation realm into our location configuration, applying the
296 * usual rules that apply to realms.
297 */
298static const char *set_authname(cmd_parms *cmd, void *mconfig,
299 const char *word1)
300{
302 const char *expr_err = NULL;
303
305 &expr_err, NULL);
306 if (expr_err) {
307 return apr_pstrcat(cmd->temp_pool,
308 "Cannot parse expression '", word1, "' in AuthName: ",
309 expr_err, NULL);
310 }
311
312 return NULL;
313}
314
315static const char *set_authtype(cmd_parms *cmd, void *mconfig,
316 const char *word1)
317{
319 const char *expr_err = NULL;
320
322 &expr_err, NULL);
323 if (expr_err) {
324 return apr_pstrcat(cmd->temp_pool,
325 "Cannot parse expression '", word1, "' in AuthType: ",
326 expr_err, NULL);
327 }
328
329 aconfig->auth_type_set = 1;
330
331 return NULL;
332}
333
334static const char *authn_ap_auth_type(request_rec *r)
335{
337
339 &authn_core_module);
340
341 if (conf->ap_auth_type) {
342 const char *err = NULL, *type;
344 if (err) {
346 APLOG_MARK, APLOG_ERR, APR_SUCCESS, r, APLOGNO(02834) "AuthType expression could not be evaluated: %s", err);
347 return NULL;
348 }
349
350 return strcasecmp(type, "None") ? type : NULL;
351 }
352
353 return NULL;
354}
355
356static const char *authn_ap_auth_name(request_rec *r)
357{
359 const char *err = NULL, *name;
360
362 &authn_core_module);
363
364 if (conf->ap_auth_name) {
366 if (err) {
368 APLOG_MARK, APLOG_ERR, APR_SUCCESS, r, APLOGNO(02835) "AuthName expression could not be evaluated: %s", err);
369 return NULL;
370 }
371
372 return ap_escape_quotes(r->pool, name);
373 }
374
375 return NULL;
376}
377
378static const command_rec authn_cmds[] =
379{
381 "an HTTP authorization type (e.g., \"Basic\")"),
383 "the authentication realm (e.g. \"Members Only\")"),
384 AP_INIT_RAW_ARGS("<AuthnProviderAlias", authaliassection, NULL, RSRC_CONF,
385 "container for grouping an authentication provider's "
386 "directives under a provider alias"),
387 {NULL}
388};
389
391{
392 /* if there isn't an AuthType, then assume that no authentication
393 is required so return OK */
394 if (!ap_auth_type(r)) {
395 return OK;
396 }
397
398 /* there's an AuthType configured, but no authentication module
399 * loaded to support it
400 */
402 "AuthType %s configured without corresponding module",
403 ap_auth_type(r));
404
406}
407
416
418{
420 create_authn_core_dir_config, /* dir config creater */
421 merge_authn_core_dir_config, /* dir merger --- default is to override */
422 create_authn_alias_svr_config, /* server config */
423 merge_authn_alias_svr_config, /* merge server config */
425 register_hooks /* register hooks */
426};
427
Symbol export macros and hook functions.
Expression parser.
Apache Provider API.
APR Network library.
APR Strings library.
APR Standard Headers Support.
ap_conf_vector_t * ap_merge_per_dir_configs(apr_pool_t *p, ap_conf_vector_t *base, ap_conf_vector_t *new_conf)
Definition config.c:285
ap_conf_vector_t * ap_create_per_dir_config(apr_pool_t *p)
Definition config.c:366
static apr_OFN_authn_ap_auth_type_t * authn_ap_auth_type
Definition core.c:791
static apr_OFN_authn_ap_auth_name_t * authn_ap_auth_name
Definition core.c:805
#define AP_INIT_TAKE1(directive, func, mconfig, where, help)
#define ap_get_module_config(v, m)
struct ap_conf_vector_t ap_conf_vector_t
#define AP_DECLARE_MODULE(foo)
ap_conf_vector_t * base
#define AP_INIT_RAW_ARGS(directive, func, mconfig, where, help)
request_rec * r
const char * ap_walk_config(ap_directive_t *conftree, cmd_parms *parms, ap_conf_vector_t *section_vector)
Definition config.c:1360
#define OK
Definition httpd.h:456
const char * ap_auth_type(request_rec *r)
Definition core.c:793
#define APLOGNO(n)
Definition http_log.h:117
#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_lookup_provider(const char *provider_group, const char *provider_name, const char *provider_version)
Definition provider.c:99
apr_status_t ap_register_auth_provider(apr_pool_t *pool, const char *provider_group, const char *provider_name, const char *provider_version, const void *provider, int type)
Definition request.c:2179
#define AP_AUTH_INTERNAL_PER_CONF
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 * dummy
Definition http_vhost.h:62
void const char * arg
Definition http_vhost.h:63
apr_bucket apr_bucket_brigade * a
#define APR_HOOK_LAST
Definition apr_hooks.h:305
#define APR_REGISTER_OPTIONAL_FN(name)
#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 ACCESS_CONF
#define RSRC_CONF
#define OR_AUTHCFG
#define HTTP_INTERNAL_SERVER_ERROR
Definition httpd.h:535
#define STANDARD20_MODULE_STUFF
#define ap_strrchr_c(s, c)
Definition httpd.h:2357
char * ap_escape_quotes(apr_pool_t *p, const char *instring)
Definition util.c:2524
char * ap_getword_conf(apr_pool_t *p, const char **line)
Definition util.c:833
#define GLOBAL_ONLY
const char * ap_check_cmd_context(cmd_parms *cmd, unsigned forbidden)
Definition core.c:1301
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int type
int strcasecmp(const char *a, const char *b)
#define APR_HASH_KEY_STRING
Definition apr_hash.h:47
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
const char * s
Definition apr_strings.h:95
const char const char * password
apr_int32_t apr_int32_t apr_int32_t err
apr_cmdtype_e cmd
const char const char *const * args
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
authn_status
Definition mod_auth.h:64
@ AUTH_USER_NOT_FOUND
Definition mod_auth.h:68
static void * create_authn_core_dir_config(apr_pool_t *p, char *dummy)
struct provider_alias_rec provider_alias_rec
static void * create_authn_alias_svr_config(apr_pool_t *p, server_rec *s)
static const char * authaliassection(cmd_parms *cmd, void *mconfig, const char *arg)
static const authn_provider authn_alias_provider
static const command_rec authn_cmds[]
static int authenticate_no_user(request_rec *r)
static void register_hooks(apr_pool_t *p)
static void * merge_authn_alias_svr_config(apr_pool_t *p, void *basev, void *overridesv)
static const char * set_authname(cmd_parms *cmd, void *mconfig, const char *word1)
static const authn_provider authn_alias_provider_nodigest
static authn_status authn_alias_check_password(request_rec *r, const char *user, const char *password)
static void * merge_authn_core_dir_config(apr_pool_t *a, void *basev, void *newv)
static authn_status authn_alias_get_realm_hash(request_rec *r, const char *user, const char *realm, char **rethash)
static const char * set_authtype(cmd_parms *cmd, void *mconfig, const char *word1)
return NULL
Definition mod_so.c:359
char * name
ap_expr_info_t * ap_auth_name
ap_expr_info_t * ap_auth_type
authn_status(* get_realm_hash)(request_rec *r, const char *user, const char *realm, char **rethash)
Definition mod_auth.h:90
const authn_provider * provider
ap_conf_vector_t * sec_auth
A structure that represents the current request.
Definition httpd.h:845
apr_table_t * notes
Definition httpd.h:985
apr_pool_t * pool
Definition httpd.h:847
server_rec * server
Definition httpd.h:851
struct ap_conf_vector_t * per_dir_config
Definition httpd.h:1047
A structure to store information for each virtual server.
Definition httpd.h:1322
struct ap_conf_vector_t * module_config
Definition httpd.h:1341