]> granicus.if.org Git - apache/blob - modules/aaa/mod_authn_core.c
Added many log numbers to log statements that
[apache] / modules / aaa / mod_authn_core.c
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
55 typedef struct {
56     ap_expr_info_t *ap_auth_type;
57     int auth_type_set;
58     ap_expr_info_t *ap_auth_name;
59 } authn_core_dir_conf;
60
61 typedef struct provider_alias_rec {
62     char *provider_name;
63     char *provider_alias;
64     ap_conf_vector_t *sec_auth;
65     const authn_provider *provider;
66 } provider_alias_rec;
67
68 typedef struct authn_alias_srv_conf {
69     apr_hash_t *alias_rec;
70 } authn_alias_srv_conf;
71
72
73 module AP_MODULE_DECLARE_DATA authn_core_module;
74
75 static void *create_authn_core_dir_config(apr_pool_t *p, char *dummy)
76 {
77     authn_core_dir_conf *conf =
78             (authn_core_dir_conf *)apr_pcalloc(p, sizeof(authn_core_dir_conf));
79
80     return (void *)conf;
81 }
82
83 static void *merge_authn_core_dir_config(apr_pool_t *a, void *basev, void *newv)
84 {
85     authn_core_dir_conf *base = (authn_core_dir_conf *)basev;
86     authn_core_dir_conf *new = (authn_core_dir_conf *)newv;
87     authn_core_dir_conf *conf =
88         (authn_core_dir_conf *)apr_pcalloc(a, sizeof(authn_core_dir_conf));
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
108 static authn_status authn_alias_check_password(request_rec *r, const char *user,
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);
117     authn_status ret = AUTH_USER_NOT_FOUND;
118     authn_alias_srv_conf *authcfg =
119         (authn_alias_srv_conf *)ap_get_module_config(r->server->module_config,
120                                                      &authn_core_module);
121
122     if (provider_name) {
123         provider_alias_rec *prvdraliasrec = apr_hash_get(authcfg->alias_rec,
124                                                          provider_name, APR_HASH_KEY_STRING);
125         ap_conf_vector_t *orig_dir_config = r->per_dir_config;
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) {
130             r->per_dir_config = ap_merge_per_dir_configs(r->pool, orig_dir_config,
131                                                          prvdraliasrec->sec_auth);
132             ret = prvdraliasrec->provider->check_password(r,user,password);
133             r->per_dir_config = orig_dir_config;
134         }
135     }
136
137     return ret;
138 }
139
140 static authn_status authn_alias_get_realm_hash(request_rec *r, const char *user,
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);
149     authn_status ret = AUTH_USER_NOT_FOUND;
150     authn_alias_srv_conf *authcfg =
151         (authn_alias_srv_conf *)ap_get_module_config(r->server->module_config,
152                                                      &authn_core_module);
153
154     if (provider_name) {
155         provider_alias_rec *prvdraliasrec = apr_hash_get(authcfg->alias_rec,
156                                                          provider_name, APR_HASH_KEY_STRING);
157         ap_conf_vector_t *orig_dir_config = r->per_dir_config;
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) {
162             r->per_dir_config = ap_merge_per_dir_configs(r->pool, orig_dir_config,
163                                                          prvdraliasrec->sec_auth);
164             ret = prvdraliasrec->provider->get_realm_hash(r,user,realm,rethash);
165             r->per_dir_config = orig_dir_config;
166         }
167     }
168
169     return ret;
170 }
171
172 static void *create_authn_alias_svr_config(apr_pool_t *p, server_rec *s)
173 {
174
175     authn_alias_srv_conf *authcfg;
176
177     authcfg = (authn_alias_srv_conf *) apr_pcalloc(p, sizeof(authn_alias_srv_conf));
178     authcfg->alias_rec = apr_hash_make(p);
179
180     return (void *) authcfg;
181 }
182
183 /* Only per-server directive we have is GLOBAL_ONLY */
184 static void *merge_authn_alias_svr_config(apr_pool_t *p, void *basev, void *overridesv)
185 {
186     return basev;
187 }
188
189 static const authn_provider authn_alias_provider =
190 {
191     &authn_alias_check_password,
192     &authn_alias_get_realm_hash,
193 };
194
195 static const authn_provider authn_alias_provider_nodigest =
196 {
197     &authn_alias_check_password,
198     NULL,
199 };
200
201 static 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;
210     ap_conf_vector_t *new_auth_config = ap_create_per_dir_config(cmd->pool);
211     authn_alias_srv_conf *authcfg =
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,
248                                   AUTHN_PROVIDER_VERSION);
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) {
262         provider_alias_rec *prvdraliasrec = apr_pcalloc(cmd->pool, sizeof(provider_alias_rec));
263         provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP, provider_name,
264                                       AUTHN_PROVIDER_VERSION);
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 */
283         ap_register_auth_provider(cmd->pool, AUTHN_PROVIDER_GROUP,
284                                   provider_alias, AUTHN_PROVIDER_VERSION,
285                                   provider->get_realm_hash ?
286                                       &authn_alias_provider :
287                                       &authn_alias_provider_nodigest,
288                                   AP_AUTH_INTERNAL_PER_CONF);
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  */
298 static const char *set_authname(cmd_parms *cmd, void *mconfig,
299                                 const char *word1)
300 {
301     authn_core_dir_conf *aconfig = (authn_core_dir_conf *)mconfig;
302     const char *expr_err = NULL;
303
304     aconfig->ap_auth_name = ap_expr_parse_cmd(cmd, word1, AP_EXPR_FLAG_STRING_RESULT,
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
315 static const char *set_authtype(cmd_parms *cmd, void *mconfig,
316                                 const char *word1)
317 {
318     authn_core_dir_conf *aconfig = (authn_core_dir_conf *)mconfig;
319     const char *expr_err = NULL;
320
321     aconfig->ap_auth_type = ap_expr_parse_cmd(cmd, word1, AP_EXPR_FLAG_STRING_RESULT,
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
334 static const char *authn_ap_auth_type(request_rec *r)
335 {
336     authn_core_dir_conf *conf;
337
338     conf = (authn_core_dir_conf *) ap_get_module_config(r->per_dir_config,
339             &authn_core_module);
340
341     if (conf->ap_auth_type) {
342         const char *err = NULL, *type;
343         type = ap_expr_str_exec(r, conf->ap_auth_type, &err);
344         if (err) {
345             ap_log_rerror(
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 ap_casecmpstr(type, "None") ? type : NULL;
351     }
352
353     return NULL;
354 }
355
356 static const char *authn_ap_auth_name(request_rec *r)
357 {
358     authn_core_dir_conf *conf;
359     const char *err = NULL, *name;
360
361     conf = (authn_core_dir_conf *) ap_get_module_config(r->per_dir_config,
362             &authn_core_module);
363
364     if (conf->ap_auth_name) {
365         name = ap_expr_str_exec(r, conf->ap_auth_name, &err);
366         if (err) {
367             ap_log_rerror(
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
378 static const command_rec authn_cmds[] =
379 {
380     AP_INIT_TAKE1("AuthType", set_authtype, NULL, OR_AUTHCFG,
381                   "an HTTP authorization type (e.g., \"Basic\")"),
382     AP_INIT_TAKE1("AuthName", set_authname, NULL, OR_AUTHCFG,
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
390 static int authenticate_no_user(request_rec *r)
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      */
401     ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_SUCCESS, r, APLOGNO(01796)
402                   "AuthType %s configured without corresponding module",
403                   ap_auth_type(r));
404
405     return HTTP_INTERNAL_SERVER_ERROR;
406 }
407
408 static void register_hooks(apr_pool_t *p)
409 {
410     APR_REGISTER_OPTIONAL_FN(authn_ap_auth_type);
411     APR_REGISTER_OPTIONAL_FN(authn_ap_auth_name);
412
413     ap_hook_check_authn(authenticate_no_user, NULL, NULL, APR_HOOK_LAST,
414                         AP_AUTH_INTERNAL_PER_CONF);
415 }
416
417 AP_DECLARE_MODULE(authn_core) =
418 {
419     STANDARD20_MODULE_STUFF,
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 */
424     authn_cmds,
425     register_hooks                  /* register hooks */
426 };
427