]> granicus.if.org Git - apache/blob - modules/aaa/mod_authn_core.c
Introduce a per connection "peer_ip" and a per request "client_ip" to
[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_provider.h"
38
39 #include "mod_auth.h"
40
41 #if APR_HAVE_NETINET_IN_H
42 #include <netinet/in.h>
43 #endif
44
45 /* TODO List
46
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
50    request_rec
51
52 */
53
54 typedef struct {
55     const char *ap_auth_type;
56     int auth_type_set;
57     const char *ap_auth_name;
58 } authn_core_dir_conf;
59
60 typedef struct provider_alias_rec {
61     char *provider_name;
62     char *provider_alias;
63     ap_conf_vector_t *sec_auth;
64     const authn_provider *provider;
65 } provider_alias_rec;
66
67 typedef struct authn_alias_srv_conf {
68     apr_hash_t *alias_rec;
69 } authn_alias_srv_conf;
70
71
72 module AP_MODULE_DECLARE_DATA authn_core_module;
73
74 static void *create_authn_core_dir_config(apr_pool_t *p, char *dummy)
75 {
76     authn_core_dir_conf *conf =
77             (authn_core_dir_conf *)apr_pcalloc(p, sizeof(authn_core_dir_conf));
78
79     return (void *)conf;
80 }
81
82 static void *merge_authn_core_dir_config(apr_pool_t *a, void *basev, void *newv)
83 {
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));
88
89     if (new->auth_type_set) {
90         conf->ap_auth_type = new->ap_auth_type;
91         conf->auth_type_set = 1;
92     }
93     else {
94         conf->ap_auth_type = base->ap_auth_type;
95         conf->auth_type_set = base->auth_type_set;
96     }
97
98     if (new->ap_auth_name) {
99         conf->ap_auth_name = new->ap_auth_name;
100     } else {
101         conf->ap_auth_name = base->ap_auth_name;
102     }
103
104     return (void*)conf;
105 }
106
107 static authn_status authn_alias_check_password(request_rec *r, const char *user,
108                                               const char *password)
109 {
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 */
114
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,
119                                                      &authn_core_module);
120
121     if (provider_name) {
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;
125
126         /* If we found the alias provider in the list, then merge the directory
127            configurations and call the real provider */
128         if (prvdraliasrec) {
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;
133         }
134     }
135
136     return ret;
137 }
138
139 static authn_status authn_alias_get_realm_hash(request_rec *r, const char *user,
140                                                const char *realm, char **rethash)
141 {
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 */
146
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,
151                                                      &authn_core_module);
152
153     if (provider_name) {
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;
157
158         /* If we found the alias provider in the list, then merge the directory
159            configurations and call the real provider */
160         if (prvdraliasrec) {
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;
165         }
166     }
167
168     return ret;
169 }
170
171 static void *create_authn_alias_svr_config(apr_pool_t *p, server_rec *s)
172 {
173
174     authn_alias_srv_conf *authcfg;
175
176     authcfg = (authn_alias_srv_conf *) apr_pcalloc(p, sizeof(authn_alias_srv_conf));
177     authcfg->alias_rec = apr_hash_make(p);
178
179     return (void *) authcfg;
180 }
181
182 static const authn_provider authn_alias_provider =
183 {
184     &authn_alias_check_password,
185     &authn_alias_get_realm_hash,
186 };
187
188 static const authn_provider authn_alias_provider_nodigest =
189 {
190     &authn_alias_check_password,
191     NULL,
192 };
193
194 static const char *authaliassection(cmd_parms *cmd, void *mconfig, const char *arg)
195 {
196     const char *endp = ap_strrchr_c(arg, '>');
197     const char *args;
198     char *provider_alias;
199     char *provider_name;
200     int old_overrides = cmd->override;
201     const char *errmsg;
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,
206                                                      &authn_core_module);
207
208     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
209     if (err != NULL) {
210         return err;
211     }
212
213     if (endp == NULL) {
214         return apr_pstrcat(cmd->pool, cmd->cmd->name,
215                            "> directive missing closing '>'", NULL);
216     }
217
218     args = apr_pstrndup(cmd->temp_pool, arg, endp - arg);
219
220     if (!args[0]) {
221         return apr_pstrcat(cmd->pool, cmd->cmd->name,
222                            "> directive requires additional arguments", NULL);
223     }
224
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);
228
229     if (!provider_name[0] || !provider_alias[0]) {
230         return apr_pstrcat(cmd->pool, cmd->cmd->name,
231                            "> directive requires additional arguments", NULL);
232     }
233
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);
237     }
238
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);
242     if (provider) {
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.",
245                            NULL);
246     }
247
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;
253
254     if (!errmsg) {
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);
258
259         if (!provider) {
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",
264                                 provider_name);
265         }
266
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);
274
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);
282     }
283
284     return errmsg;
285 }
286
287 /*
288  * Load an authorisation realm into our location configuration, applying the
289  * usual rules that apply to realms.
290  */
291 static const char *set_authname(cmd_parms *cmd, void *mconfig,
292                                 const char *word1)
293 {
294     authn_core_dir_conf *aconfig = (authn_core_dir_conf *)mconfig;
295
296     aconfig->ap_auth_name = ap_escape_quotes(cmd->pool, word1);
297     return NULL;
298 }
299
300 static const char *set_authtype(cmd_parms *cmd, void *mconfig,
301                                 const char *word1)
302 {
303     authn_core_dir_conf *aconfig = (authn_core_dir_conf *)mconfig;
304
305     aconfig->auth_type_set = 1;
306     aconfig->ap_auth_type = strcasecmp(word1, "None") ? word1 : NULL;
307
308     return NULL;
309 }
310
311 static const char *authn_ap_auth_type(request_rec *r)
312 {
313     authn_core_dir_conf *conf;
314
315     conf = (authn_core_dir_conf *)ap_get_module_config(r->per_dir_config,
316         &authn_core_module);
317
318     return conf->ap_auth_type;
319 }
320
321 static const char *authn_ap_auth_name(request_rec *r)
322 {
323     authn_core_dir_conf *conf;
324
325     conf = (authn_core_dir_conf *)ap_get_module_config(r->per_dir_config,
326         &authn_core_module);
327
328     return apr_pstrdup(r->pool, conf->ap_auth_name);
329 }
330
331 static const command_rec authn_cmds[] =
332 {
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"),
340     {NULL}
341 };
342
343 static int authenticate_no_user(request_rec *r)
344 {
345     /* if there isn't an AuthType, then assume that no authentication
346         is required so return OK */
347     if (!ap_auth_type(r)) {
348         return OK;
349     }
350
351     /* there's an AuthType configured, but no authentication module
352      * loaded to support it
353      */
354     ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_SUCCESS, r,
355                   "AuthType %s configured without corresponding module",
356                   ap_auth_type(r));
357
358     return HTTP_INTERNAL_SERVER_ERROR;
359 }
360
361 static void register_hooks(apr_pool_t *p)
362 {
363     APR_REGISTER_OPTIONAL_FN(authn_ap_auth_type);
364     APR_REGISTER_OPTIONAL_FN(authn_ap_auth_name);
365
366     ap_hook_check_authn(authenticate_no_user, NULL, NULL, APR_HOOK_LAST,
367                         AP_AUTH_INTERNAL_PER_CONF);
368 }
369
370 AP_DECLARE_MODULE(authn_core) =
371 {
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 */
377     authn_cmds,
378     register_hooks                  /* register hooks */
379 };
380