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 static const authn_provider authn_alias_provider =
184 &authn_alias_check_password,
185 &authn_alias_get_realm_hash,
188 static const authn_provider authn_alias_provider_nodigest =
190 &authn_alias_check_password,
194 static const char *authaliassection(cmd_parms *cmd, void *mconfig, const char *arg)
196 const char *endp = ap_strrchr_c(arg, '>');
198 char *provider_alias;
200 int old_overrides = cmd->override;
202 const authn_provider *provider = NULL;
203 ap_conf_vector_t *new_auth_config = ap_create_per_dir_config(cmd->pool);
204 authn_alias_srv_conf *authcfg =
205 (authn_alias_srv_conf *)ap_get_module_config(cmd->server->module_config,
208 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
214 return apr_pstrcat(cmd->pool, cmd->cmd->name,
215 "> directive missing closing '>'", NULL);
218 args = apr_pstrndup(cmd->temp_pool, arg, endp - arg);
221 return apr_pstrcat(cmd->pool, cmd->cmd->name,
222 "> directive requires additional arguments", NULL);
225 /* Pull the real provider name and the alias name from the block header */
226 provider_name = ap_getword_conf(cmd->pool, &args);
227 provider_alias = ap_getword_conf(cmd->pool, &args);
229 if (!provider_name[0] || !provider_alias[0]) {
230 return apr_pstrcat(cmd->pool, cmd->cmd->name,
231 "> directive requires additional arguments", NULL);
234 if (strcasecmp(provider_name, provider_alias) == 0) {
235 return apr_pstrcat(cmd->pool,
236 "The alias provider name must be different from the base provider name.", NULL);
239 /* Look up the alias provider to make sure that it hasn't already been registered. */
240 provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP, provider_alias,
241 AUTHN_PROVIDER_VERSION);
243 return apr_pstrcat(cmd->pool, "The alias provider ", provider_alias,
244 " has already be registered previously as either a base provider or an alias provider.",
248 /* walk the subsection configuration to get the per_dir config that we will
249 merge just before the real provider is called. */
250 cmd->override = OR_AUTHCFG | ACCESS_CONF;
251 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_auth_config);
252 cmd->override = old_overrides;
255 provider_alias_rec *prvdraliasrec = apr_pcalloc(cmd->pool, sizeof(provider_alias_rec));
256 provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP, provider_name,
257 AUTHN_PROVIDER_VERSION);
260 /* by the time they use it, the provider should be loaded and
261 registered with us. */
262 return apr_psprintf(cmd->pool,
263 "Unknown Authn provider: %s",
267 /* Save off the new directory config along with the original provider name
268 and function pointer data */
269 prvdraliasrec->sec_auth = new_auth_config;
270 prvdraliasrec->provider_name = provider_name;
271 prvdraliasrec->provider_alias = provider_alias;
272 prvdraliasrec->provider = provider;
273 apr_hash_set(authcfg->alias_rec, provider_alias, APR_HASH_KEY_STRING, prvdraliasrec);
275 /* Register the fake provider so that we get called first */
276 ap_register_auth_provider(cmd->pool, AUTHN_PROVIDER_GROUP,
277 provider_alias, AUTHN_PROVIDER_VERSION,
278 provider->get_realm_hash ?
279 &authn_alias_provider :
280 &authn_alias_provider_nodigest,
281 AP_AUTH_INTERNAL_PER_CONF);
288 * Load an authorisation realm into our location configuration, applying the
289 * usual rules that apply to realms.
291 static const char *set_authname(cmd_parms *cmd, void *mconfig,
294 authn_core_dir_conf *aconfig = (authn_core_dir_conf *)mconfig;
296 aconfig->ap_auth_name = ap_escape_quotes(cmd->pool, word1);
300 static const char *set_authtype(cmd_parms *cmd, void *mconfig,
303 authn_core_dir_conf *aconfig = (authn_core_dir_conf *)mconfig;
305 aconfig->auth_type_set = 1;
306 aconfig->ap_auth_type = strcasecmp(word1, "None") ? word1 : NULL;
311 static const char *authn_ap_auth_type(request_rec *r)
313 authn_core_dir_conf *conf;
315 conf = (authn_core_dir_conf *)ap_get_module_config(r->per_dir_config,
318 return conf->ap_auth_type;
321 static const char *authn_ap_auth_name(request_rec *r)
323 authn_core_dir_conf *conf;
325 conf = (authn_core_dir_conf *)ap_get_module_config(r->per_dir_config,
328 return apr_pstrdup(r->pool, conf->ap_auth_name);
331 static const command_rec authn_cmds[] =
333 AP_INIT_TAKE1("AuthType", set_authtype, NULL, OR_AUTHCFG,
334 "an HTTP authorization type (e.g., \"Basic\")"),
335 AP_INIT_TAKE1("AuthName", set_authname, NULL, OR_AUTHCFG,
336 "the authentication realm (e.g. \"Members Only\")"),
337 AP_INIT_RAW_ARGS("<AuthnProviderAlias", authaliassection, NULL, RSRC_CONF,
338 "container for grouping an authentication provider's "
339 "directives under a provider alias"),
343 static int authenticate_no_user(request_rec *r)
345 /* if there isn't an AuthType, then assume that no authentication
346 is required so return OK */
347 if (!ap_auth_type(r)) {
351 /* there's an AuthType configured, but no authentication module
352 * loaded to support it
354 ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_SUCCESS, r,
355 "AuthType %s configured without corresponding module",
358 return HTTP_INTERNAL_SERVER_ERROR;
361 static void register_hooks(apr_pool_t *p)
363 APR_REGISTER_OPTIONAL_FN(authn_ap_auth_type);
364 APR_REGISTER_OPTIONAL_FN(authn_ap_auth_name);
366 ap_hook_check_authn(authenticate_no_user, NULL, NULL, APR_HOOK_LAST,
367 AP_AUTH_INTERNAL_PER_CONF);
370 AP_DECLARE_MODULE(authn_core) =
372 STANDARD20_MODULE_STUFF,
373 create_authn_core_dir_config, /* dir config creater */
374 merge_authn_core_dir_config, /* dir merger --- default is to override */
375 create_authn_alias_svr_config, /* server config */
376 NULL, /* merge server config */
378 register_hooks /* register hooks */