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.
18 * Security options etc.
20 * Module derived from code originally written by Rob McCool
24 #include "apr_strings.h"
25 #include "apr_network_io.h"
26 #define APR_WANT_STRFUNC
27 #define APR_WANT_BYTEFUNC
30 #include "ap_config.h"
32 #include "http_config.h"
33 #include "http_core.h"
35 #include "http_request.h"
36 #include "http_protocol.h"
37 #include "ap_provider.h"
41 #if APR_HAVE_NETINET_IN_H
42 #include <netinet/in.h>
47 - Track down all of the references to r->ap_auth_type
48 and change them to ap_auth_type()
49 - Remove ap_auth_type and ap_auth_name from the
55 const char *ap_auth_type;
57 const char *ap_auth_name;
58 } authn_core_dir_conf;
60 typedef struct provider_alias_rec {
63 ap_conf_vector_t *sec_auth;
64 const authn_provider *provider;
67 typedef struct authn_alias_srv_conf {
68 apr_hash_t *alias_rec;
69 } authn_alias_srv_conf;
72 module AP_MODULE_DECLARE_DATA authn_core_module;
74 static void *create_authn_core_dir_config(apr_pool_t *p, char *dummy)
76 authn_core_dir_conf *conf =
77 (authn_core_dir_conf *)apr_pcalloc(p, sizeof(authn_core_dir_conf));
82 static void *merge_authn_core_dir_config(apr_pool_t *a, void *basev, void *newv)
84 authn_core_dir_conf *base = (authn_core_dir_conf *)basev;
85 authn_core_dir_conf *new = (authn_core_dir_conf *)newv;
86 authn_core_dir_conf *conf =
87 (authn_core_dir_conf *)apr_pcalloc(a, sizeof(authn_core_dir_conf));
89 if (new->auth_type_set) {
90 conf->ap_auth_type = new->ap_auth_type;
91 conf->auth_type_set = 1;
94 conf->ap_auth_type = base->ap_auth_type;
95 conf->auth_type_set = base->auth_type_set;
98 if (new->ap_auth_name) {
99 conf->ap_auth_name = new->ap_auth_name;
101 conf->ap_auth_name = base->ap_auth_name;
107 static authn_status authn_alias_check_password(request_rec *r, const char *user,
108 const char *password)
110 /* Look up the provider alias in the alias list */
111 /* Get the the dir_config and call ap_Merge_per_dir_configs() */
112 /* Call the real provider->check_password() function */
113 /* return the result of the above function call */
115 const char *provider_name = apr_table_get(r->notes, AUTHN_PROVIDER_NAME_NOTE);
116 authn_status ret = AUTH_USER_NOT_FOUND;
117 authn_alias_srv_conf *authcfg =
118 (authn_alias_srv_conf *)ap_get_module_config(r->server->module_config,
122 provider_alias_rec *prvdraliasrec = apr_hash_get(authcfg->alias_rec,
123 provider_name, APR_HASH_KEY_STRING);
124 ap_conf_vector_t *orig_dir_config = r->per_dir_config;
126 /* If we found the alias provider in the list, then merge the directory
127 configurations and call the real provider */
129 r->per_dir_config = ap_merge_per_dir_configs(r->pool, orig_dir_config,
130 prvdraliasrec->sec_auth);
131 ret = prvdraliasrec->provider->check_password(r,user,password);
132 r->per_dir_config = orig_dir_config;
139 static authn_status authn_alias_get_realm_hash(request_rec *r, const char *user,
140 const char *realm, char **rethash)
142 /* Look up the provider alias in the alias list */
143 /* Get the the dir_config and call ap_Merge_per_dir_configs() */
144 /* Call the real provider->get_realm_hash() function */
145 /* return the result of the above function call */
147 const char *provider_name = apr_table_get(r->notes, AUTHN_PROVIDER_NAME_NOTE);
148 authn_status ret = AUTH_USER_NOT_FOUND;
149 authn_alias_srv_conf *authcfg =
150 (authn_alias_srv_conf *)ap_get_module_config(r->server->module_config,
154 provider_alias_rec *prvdraliasrec = apr_hash_get(authcfg->alias_rec,
155 provider_name, APR_HASH_KEY_STRING);
156 ap_conf_vector_t *orig_dir_config = r->per_dir_config;
158 /* If we found the alias provider in the list, then merge the directory
159 configurations and call the real provider */
161 r->per_dir_config = ap_merge_per_dir_configs(r->pool, orig_dir_config,
162 prvdraliasrec->sec_auth);
163 ret = prvdraliasrec->provider->get_realm_hash(r,user,realm,rethash);
164 r->per_dir_config = orig_dir_config;
171 static void *create_authn_alias_svr_config(apr_pool_t *p, server_rec *s)
174 authn_alias_srv_conf *authcfg;
176 authcfg = (authn_alias_srv_conf *) apr_pcalloc(p, sizeof(authn_alias_srv_conf));
177 authcfg->alias_rec = apr_hash_make(p);
179 return (void *) authcfg;
182 /* Only per-server directive we have is GLOBAL_ONLY */
183 static void *merge_authn_alias_svr_config(apr_pool_t *p, void *basev, void *overridesv)
188 static const authn_provider authn_alias_provider =
190 &authn_alias_check_password,
191 &authn_alias_get_realm_hash,
194 static const authn_provider authn_alias_provider_nodigest =
196 &authn_alias_check_password,
200 static const char *authaliassection(cmd_parms *cmd, void *mconfig, const char *arg)
202 const char *endp = ap_strrchr_c(arg, '>');
204 char *provider_alias;
206 int old_overrides = cmd->override;
208 const authn_provider *provider = NULL;
209 ap_conf_vector_t *new_auth_config = ap_create_per_dir_config(cmd->pool);
210 authn_alias_srv_conf *authcfg =
211 (authn_alias_srv_conf *)ap_get_module_config(cmd->server->module_config,
214 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
220 return apr_pstrcat(cmd->pool, cmd->cmd->name,
221 "> directive missing closing '>'", NULL);
224 args = apr_pstrndup(cmd->temp_pool, arg, endp - arg);
227 return apr_pstrcat(cmd->pool, cmd->cmd->name,
228 "> directive requires additional arguments", NULL);
231 /* Pull the real provider name and the alias name from the block header */
232 provider_name = ap_getword_conf(cmd->pool, &args);
233 provider_alias = ap_getword_conf(cmd->pool, &args);
235 if (!provider_name[0] || !provider_alias[0]) {
236 return apr_pstrcat(cmd->pool, cmd->cmd->name,
237 "> directive requires additional arguments", NULL);
240 if (strcasecmp(provider_name, provider_alias) == 0) {
241 return apr_pstrcat(cmd->pool,
242 "The alias provider name must be different from the base provider name.", NULL);
245 /* Look up the alias provider to make sure that it hasn't already been registered. */
246 provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP, provider_alias,
247 AUTHN_PROVIDER_VERSION);
249 return apr_pstrcat(cmd->pool, "The alias provider ", provider_alias,
250 " has already be registered previously as either a base provider or an alias provider.",
254 /* walk the subsection configuration to get the per_dir config that we will
255 merge just before the real provider is called. */
256 cmd->override = OR_AUTHCFG | ACCESS_CONF;
257 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_auth_config);
258 cmd->override = old_overrides;
261 provider_alias_rec *prvdraliasrec = apr_pcalloc(cmd->pool, sizeof(provider_alias_rec));
262 provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP, provider_name,
263 AUTHN_PROVIDER_VERSION);
266 /* by the time they use it, the provider should be loaded and
267 registered with us. */
268 return apr_psprintf(cmd->pool,
269 "Unknown Authn provider: %s",
273 /* Save off the new directory config along with the original provider name
274 and function pointer data */
275 prvdraliasrec->sec_auth = new_auth_config;
276 prvdraliasrec->provider_name = provider_name;
277 prvdraliasrec->provider_alias = provider_alias;
278 prvdraliasrec->provider = provider;
279 apr_hash_set(authcfg->alias_rec, provider_alias, APR_HASH_KEY_STRING, prvdraliasrec);
281 /* Register the fake provider so that we get called first */
282 ap_register_auth_provider(cmd->pool, AUTHN_PROVIDER_GROUP,
283 provider_alias, AUTHN_PROVIDER_VERSION,
284 provider->get_realm_hash ?
285 &authn_alias_provider :
286 &authn_alias_provider_nodigest,
287 AP_AUTH_INTERNAL_PER_CONF);
294 * Load an authorisation realm into our location configuration, applying the
295 * usual rules that apply to realms.
297 static const char *set_authname(cmd_parms *cmd, void *mconfig,
300 authn_core_dir_conf *aconfig = (authn_core_dir_conf *)mconfig;
302 aconfig->ap_auth_name = ap_escape_quotes(cmd->pool, word1);
306 static const char *set_authtype(cmd_parms *cmd, void *mconfig,
309 authn_core_dir_conf *aconfig = (authn_core_dir_conf *)mconfig;
311 aconfig->auth_type_set = 1;
312 aconfig->ap_auth_type = strcasecmp(word1, "None") ? word1 : NULL;
317 static const char *authn_ap_auth_type(request_rec *r)
319 authn_core_dir_conf *conf;
321 conf = (authn_core_dir_conf *)ap_get_module_config(r->per_dir_config,
324 return conf->ap_auth_type;
327 static const char *authn_ap_auth_name(request_rec *r)
329 authn_core_dir_conf *conf;
331 conf = (authn_core_dir_conf *)ap_get_module_config(r->per_dir_config,
334 return apr_pstrdup(r->pool, conf->ap_auth_name);
337 static const command_rec authn_cmds[] =
339 AP_INIT_TAKE1("AuthType", set_authtype, NULL, OR_AUTHCFG,
340 "an HTTP authorization type (e.g., \"Basic\")"),
341 AP_INIT_TAKE1("AuthName", set_authname, NULL, OR_AUTHCFG,
342 "the authentication realm (e.g. \"Members Only\")"),
343 AP_INIT_RAW_ARGS("<AuthnProviderAlias", authaliassection, NULL, RSRC_CONF,
344 "container for grouping an authentication provider's "
345 "directives under a provider alias"),
349 static int authenticate_no_user(request_rec *r)
351 /* if there isn't an AuthType, then assume that no authentication
352 is required so return OK */
353 if (!ap_auth_type(r)) {
357 /* there's an AuthType configured, but no authentication module
358 * loaded to support it
360 ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_SUCCESS, r, APLOGNO(01796)
361 "AuthType %s configured without corresponding module",
364 return HTTP_INTERNAL_SERVER_ERROR;
367 static void register_hooks(apr_pool_t *p)
369 APR_REGISTER_OPTIONAL_FN(authn_ap_auth_type);
370 APR_REGISTER_OPTIONAL_FN(authn_ap_auth_name);
372 ap_hook_check_authn(authenticate_no_user, NULL, NULL, APR_HOOK_LAST,
373 AP_AUTH_INTERNAL_PER_CONF);
376 AP_DECLARE_MODULE(authn_core) =
378 STANDARD20_MODULE_STUFF,
379 create_authn_core_dir_config, /* dir config creater */
380 merge_authn_core_dir_config, /* dir merger --- default is to override */
381 create_authn_alias_svr_config, /* server config */
382 merge_authn_alias_svr_config, /* merge server config */
384 register_hooks /* register hooks */