]> granicus.if.org Git - apache/blob - modules/aaa/mod_authn_socache.c
Added many log numbers to log statements that
[apache] / modules / aaa / mod_authn_socache.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 #include "apr_strings.h"
18
19 #include "ap_config.h"
20 #include "ap_provider.h"
21 #include "httpd.h"
22 #include "http_config.h"
23 #include "http_core.h"
24 #include "http_log.h"
25 #include "http_protocol.h"
26 #include "http_request.h"
27
28 #include "mod_auth.h"
29
30 #include "ap_socache.h"
31 #include "util_mutex.h"
32 #include "apr_optional.h"
33
34 module AP_MODULE_DECLARE_DATA authn_socache_module;
35
36 typedef struct authn_cache_dircfg {
37     apr_interval_time_t timeout;
38     apr_array_header_t *providers;
39     const char *context;
40 } authn_cache_dircfg;
41
42 /* FIXME:
43  * I think the cache and mutex should be global
44  */
45 static apr_global_mutex_t *authn_cache_mutex = NULL;
46 static ap_socache_provider_t *socache_provider = NULL;
47 static ap_socache_instance_t *socache_instance = NULL;
48 static const char *const authn_cache_id = "authn-socache";
49 static int configured;
50
51 static apr_status_t remove_lock(void *data)
52 {
53     if (authn_cache_mutex) {
54         apr_global_mutex_destroy(authn_cache_mutex);
55         authn_cache_mutex = NULL;
56     }
57     return APR_SUCCESS;
58 }
59
60 static apr_status_t destroy_cache(void *data)
61 {
62     if (socache_instance) {
63         socache_provider->destroy(socache_instance, (server_rec*)data);
64         socache_instance = NULL;
65     }
66     return APR_SUCCESS;
67 }
68
69 static int authn_cache_precfg(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptmp)
70 {
71     apr_status_t rv = ap_mutex_register(pconf, authn_cache_id,
72                                         NULL, APR_LOCK_DEFAULT, 0);
73     if (rv != APR_SUCCESS) {
74         ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, plog, APLOGNO(01673)
75                       "failed to register %s mutex", authn_cache_id);
76         return 500; /* An HTTP status would be a misnomer! */
77     }
78     socache_provider = ap_lookup_provider(AP_SOCACHE_PROVIDER_GROUP,
79                                           AP_SOCACHE_DEFAULT_PROVIDER,
80                                           AP_SOCACHE_PROVIDER_VERSION);
81     configured = 0;
82     return OK;
83 }
84
85 static int authn_cache_post_config(apr_pool_t *pconf, apr_pool_t *plog,
86                                    apr_pool_t *ptmp, server_rec *s)
87 {
88     apr_status_t rv;
89     static struct ap_socache_hints authn_cache_hints = {64, 32, 60000000};
90     const char *errmsg;
91
92     if (!configured) {
93         return OK;    /* don't waste the overhead of creating mutex & cache */
94     }
95     if (socache_provider == NULL) {
96         ap_log_perror(APLOG_MARK, APLOG_CRIT, 0, plog, APLOGNO(01674)
97                       "Please select a socache provider with AuthnCacheSOCache "
98                       "(no default found on this platform). Maybe you need to "
99                       "load mod_socache_shmcb or another socache module first");
100         return 500; /* An HTTP status would be a misnomer! */
101     }
102
103     /* We have socache_provider, but do not have socache_instance. This should
104      * happen only when using "default" socache_provider, so create default
105      * socache_instance in this case. */
106     if (socache_instance == NULL) {
107         errmsg = socache_provider->create(&socache_instance, NULL,
108                                           ptmp, pconf);
109         if (errmsg) {
110             ap_log_perror(APLOG_MARK, APLOG_CRIT, 0, plog, APLOGNO(02612)
111                         "failed to create mod_socache_shmcb socache "
112                         "instance: %s", errmsg);
113             return 500;
114         }
115     }
116
117     rv = ap_global_mutex_create(&authn_cache_mutex, NULL,
118                                 authn_cache_id, NULL, s, pconf, 0);
119     if (rv != APR_SUCCESS) {
120         ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, plog, APLOGNO(01675)
121                       "failed to create %s mutex", authn_cache_id);
122         return 500; /* An HTTP status would be a misnomer! */
123     }
124     apr_pool_cleanup_register(pconf, NULL, remove_lock, apr_pool_cleanup_null);
125
126     rv = socache_provider->init(socache_instance, authn_cache_id,
127                                 &authn_cache_hints, s, pconf);
128     if (rv != APR_SUCCESS) {
129         ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, plog, APLOGNO(01677)
130                       "failed to initialise %s cache", authn_cache_id);
131         return 500; /* An HTTP status would be a misnomer! */
132     }
133     apr_pool_cleanup_register(pconf, (void*)s, destroy_cache, apr_pool_cleanup_null);
134     return OK;
135 }
136
137 static void authn_cache_child_init(apr_pool_t *p, server_rec *s)
138 {
139     const char *lock;
140     apr_status_t rv;
141     if (!configured) {
142         return;       /* don't waste the overhead of creating mutex & cache */
143     }
144     lock = apr_global_mutex_lockfile(authn_cache_mutex);
145     rv = apr_global_mutex_child_init(&authn_cache_mutex, lock, p);
146     if (rv != APR_SUCCESS) {
147         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, APLOGNO(01678)
148                      "failed to initialise mutex in child_init");
149     }
150 }
151
152 static const char *authn_cache_socache(cmd_parms *cmd, void *CFG,
153                                        const char *arg)
154 {
155     const char *errmsg = ap_check_cmd_context(cmd, GLOBAL_ONLY);
156     const char *sep, *name;
157
158     if (errmsg)
159         return errmsg;
160
161     /* Argument is of form 'name:args' or just 'name'. */
162     sep = ap_strchr_c(arg, ':');
163     if (sep) {
164         name = apr_pstrmemdup(cmd->pool, arg, sep - arg);
165         sep++;
166     }
167     else {
168         name = arg;
169     }
170
171     socache_provider = ap_lookup_provider(AP_SOCACHE_PROVIDER_GROUP, name,
172                                           AP_SOCACHE_PROVIDER_VERSION);
173     if (socache_provider == NULL) {
174         errmsg = apr_psprintf(cmd->pool,
175                               "Unknown socache provider '%s'. Maybe you need "
176                               "to load the appropriate socache module "
177                               "(mod_socache_%s?)", arg, arg);
178     }
179     else {
180         errmsg = socache_provider->create(&socache_instance, sep,
181                                           cmd->temp_pool, cmd->pool);
182     }
183
184     if (errmsg) {
185         errmsg = apr_psprintf(cmd->pool, "AuthnCacheSOCache: %s", errmsg);
186     }
187     return errmsg;
188 }
189
190 static const char *authn_cache_enable(cmd_parms *cmd, void *CFG)
191 {
192     const char *errmsg = ap_check_cmd_context(cmd, GLOBAL_ONLY);
193     configured = 1;
194     return errmsg;
195 }
196
197 static const char *const directory = "directory";
198 static void* authn_cache_dircfg_create(apr_pool_t *pool, char *s)
199 {
200     authn_cache_dircfg *ret = apr_palloc(pool, sizeof(authn_cache_dircfg));
201     ret->timeout = apr_time_from_sec(300);
202     ret->providers = NULL;
203     ret->context = directory;
204     return ret;
205 }
206
207 /* not sure we want this.  Might be safer to document use-all-or-none */
208 static void* authn_cache_dircfg_merge(apr_pool_t *pool, void *BASE, void *ADD)
209 {
210     authn_cache_dircfg *base = BASE;
211     authn_cache_dircfg *add = ADD;
212     authn_cache_dircfg *ret = apr_pmemdup(pool, add, sizeof(authn_cache_dircfg));
213     /* preserve context and timeout if not defaults */
214     if (add->context == directory) {
215         ret->context = base->context;
216     }
217     if (add->timeout == apr_time_from_sec(300)) {
218         ret->timeout = base->timeout;
219     }
220     if (add->providers == NULL) {
221         ret->providers = base->providers;
222     }
223     return ret;
224 }
225
226 static const char *authn_cache_setprovider(cmd_parms *cmd, void *CFG,
227                                            const char *arg)
228 {
229     authn_cache_dircfg *cfg = CFG;
230     if (cfg->providers == NULL) {
231         cfg->providers = apr_array_make(cmd->pool, 4, sizeof(const char*));
232     }
233     APR_ARRAY_PUSH(cfg->providers, const char*) = arg;
234     configured = 1;
235     return NULL;
236 }
237
238 static const char *authn_cache_timeout(cmd_parms *cmd, void *CFG,
239                                        const char *arg)
240 {
241     authn_cache_dircfg *cfg = CFG;
242     int secs = atoi(arg);
243     cfg->timeout = apr_time_from_sec(secs);
244     return NULL;
245 }
246
247 static const command_rec authn_cache_cmds[] =
248 {
249     /* global stuff: cache and mutex */
250     AP_INIT_TAKE1("AuthnCacheSOCache", authn_cache_socache, NULL, RSRC_CONF,
251                   "socache provider for authn cache"),
252     AP_INIT_NO_ARGS("AuthnCacheEnable", authn_cache_enable, NULL, RSRC_CONF,
253                     "enable socache configuration in htaccess even if not enabled anywhere else"),
254     /* per-dir stuff */
255     AP_INIT_ITERATE("AuthnCacheProvideFor", authn_cache_setprovider, NULL,
256                     OR_AUTHCFG, "Determine what authn providers to cache for"),
257     AP_INIT_TAKE1("AuthnCacheTimeout", authn_cache_timeout, NULL,
258                   OR_AUTHCFG, "Timeout (secs) for cached credentials"),
259     AP_INIT_TAKE1("AuthnCacheContext", ap_set_string_slot,
260                   (void*)APR_OFFSETOF(authn_cache_dircfg, context),
261                   ACCESS_CONF, "Context for authn cache"),
262     {NULL}
263 };
264
265 static const char *construct_key(request_rec *r, const char *context,
266                                  const char *user, const char *realm)
267 {
268     /* handle "special" context values */
269     if (!strcmp(context, directory)) {
270         /* FIXME: are we at risk of this blowing up? */
271         char *new_context;
272         char *slash = strrchr(r->uri, '/');
273         new_context = apr_palloc(r->pool, slash - r->uri +
274                                  strlen(r->server->server_hostname) + 1);
275         strcpy(new_context, r->server->server_hostname);
276         strncat(new_context, r->uri, slash - r->uri);
277         context = new_context;
278     }
279     else if (!strcmp(context, "server")) {
280         context = r->server->server_hostname;
281     }
282     /* any other context value is literal */
283
284     if (realm == NULL) {                              /* basic auth */
285         return apr_pstrcat(r->pool, context, ":", user, NULL);
286     }
287     else {                                            /* digest auth */
288         return apr_pstrcat(r->pool, context, ":", user, ":", realm, NULL);
289     }
290 }
291
292 static void ap_authn_cache_store(request_rec *r, const char *module,
293                                  const char *user, const char *realm,
294                                  const char* data)
295 {
296     apr_status_t rv;
297     authn_cache_dircfg *dcfg;
298     const char *key;
299     apr_time_t expiry;
300     int i;
301     int use_cache = 0;
302
303     /* first check whether we're cacheing for this module */
304     dcfg = ap_get_module_config(r->per_dir_config, &authn_socache_module);
305     if (!configured || !dcfg->providers) {
306         return;
307     }
308     for (i = 0; i < dcfg->providers->nelts; ++i) {
309         if (!strcmp(module, APR_ARRAY_IDX(dcfg->providers, i, const char*))) {
310             use_cache = 1;
311             break;
312         }
313     }
314     if (!use_cache) {
315         return;
316     }
317
318     /* OK, we're on.  Grab mutex to do our business */
319     rv = apr_global_mutex_trylock(authn_cache_mutex);
320     if (APR_STATUS_IS_EBUSY(rv)) {
321         /* don't wait around; just abandon it */
322         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r, APLOGNO(01679)
323                       "authn credentials for %s not cached (mutex busy)", user);
324         return;
325     }
326     else if (rv != APR_SUCCESS) {
327         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01680)
328                       "Failed to cache authn credentials for %s in %s",
329                       module, dcfg->context);
330         return;
331     }
332
333     /* We have the mutex, so go ahead */
334     /* first build our key and determine expiry time */
335     key = construct_key(r, dcfg->context, user, realm);
336     expiry = apr_time_now() + dcfg->timeout;
337
338     /* store it */
339     rv = socache_provider->store(socache_instance, r->server,
340                                  (unsigned char*)key, strlen(key), expiry,
341                                  (unsigned char*)data, strlen(data), r->pool);
342     if (rv == APR_SUCCESS) {
343         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01681)
344                       "Cached authn credentials for %s in %s",
345                       user, dcfg->context);
346     }
347     else {
348         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01682)
349                       "Failed to cache authn credentials for %s in %s",
350                       module, dcfg->context);
351     }
352
353     /* We're done with the mutex */
354     rv = apr_global_mutex_unlock(authn_cache_mutex);
355     if (rv != APR_SUCCESS) {
356         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01683) "Failed to release mutex!");
357     }
358 }
359
360 #define MAX_VAL_LEN 100
361 static authn_status check_password(request_rec *r, const char *user,
362                                    const char *password)
363 {
364     /* construct key
365      * look it up
366      * if found, test password
367      *
368      * mutexing here would be a big performance drag.
369      * It's definitely unnecessary with some backends (like ndbm or gdbm)
370      * Is there a risk in the general case?  I guess the only risk we
371      * care about is a race condition that gets us a dangling pointer
372      * to no-longer-defined memory.  Hmmm ...
373      */
374     apr_status_t rv;
375     const char *key;
376     authn_cache_dircfg *dcfg;
377     unsigned char val[MAX_VAL_LEN];
378     unsigned int vallen = MAX_VAL_LEN - 1;
379     dcfg = ap_get_module_config(r->per_dir_config, &authn_socache_module);
380     if (!configured || !dcfg->providers) {
381         return AUTH_USER_NOT_FOUND;
382     }
383     key = construct_key(r, dcfg->context, user, NULL);
384     rv = socache_provider->retrieve(socache_instance, r->server,
385                                     (unsigned char*)key, strlen(key),
386                                     val, &vallen, r->pool);
387
388     if (APR_STATUS_IS_NOTFOUND(rv)) {
389         /* not found - just return */
390         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01684)
391                       "Authn cache: no credentials found for %s", user);
392         return AUTH_USER_NOT_FOUND;
393     }
394     else if (rv == APR_SUCCESS) {
395         /* OK, we got a value */
396         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01685)
397                       "Authn cache: found credentials for %s", user);
398         val[vallen] = 0;
399     }
400     else {
401         /* error: give up and pass the buck */
402         /* FIXME: getting this for NOTFOUND - prolly a bug in mod_socache */
403         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01686)
404                       "Error accessing authentication cache");
405         return AUTH_USER_NOT_FOUND;
406     }
407
408     rv = ap_password_validate(r, user, password, (char*) val);
409     if (rv != APR_SUCCESS) {
410         return AUTH_DENIED;
411     }
412
413     return AUTH_GRANTED;
414 }
415
416 static authn_status get_realm_hash(request_rec *r, const char *user,
417                                    const char *realm, char **rethash)
418 {
419     apr_status_t rv;
420     const char *key;
421     authn_cache_dircfg *dcfg;
422     unsigned char val[MAX_VAL_LEN];
423     unsigned int vallen = MAX_VAL_LEN - 1;
424     dcfg = ap_get_module_config(r->per_dir_config, &authn_socache_module);
425     if (!configured || !dcfg->providers) {
426         return AUTH_USER_NOT_FOUND;
427     }
428     key = construct_key(r, dcfg->context, user, realm);
429     rv = socache_provider->retrieve(socache_instance, r->server,
430                                     (unsigned char*)key, strlen(key),
431                                     val, &vallen, r->pool);
432
433     if (APR_STATUS_IS_NOTFOUND(rv)) {
434         /* not found - just return */
435         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01687)
436                       "Authn cache: no credentials found for %s", user);
437         return AUTH_USER_NOT_FOUND;
438     }
439     else if (rv == APR_SUCCESS) {
440         /* OK, we got a value */
441         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01688)
442                       "Authn cache: found credentials for %s", user);
443     }
444     else {
445         /* error: give up and pass the buck */
446         /* FIXME: getting this for NOTFOUND - prolly a bug in mod_socache */
447         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01689)
448                       "Error accessing authentication cache");
449         return AUTH_USER_NOT_FOUND;
450     }
451     *rethash = apr_pstrmemdup(r->pool, (char *)val, vallen);
452
453     return AUTH_USER_FOUND;
454 }
455
456 static const authn_provider authn_cache_provider =
457 {
458     &check_password,
459     &get_realm_hash,
460 };
461
462 static void register_hooks(apr_pool_t *p)
463 {
464     ap_register_auth_provider(p, AUTHN_PROVIDER_GROUP, "socache",
465                               AUTHN_PROVIDER_VERSION,
466                               &authn_cache_provider, AP_AUTH_INTERNAL_PER_CONF);
467     APR_REGISTER_OPTIONAL_FN(ap_authn_cache_store);
468     ap_hook_pre_config(authn_cache_precfg, NULL, NULL, APR_HOOK_MIDDLE);
469     ap_hook_post_config(authn_cache_post_config, NULL, NULL, APR_HOOK_MIDDLE);
470     ap_hook_child_init(authn_cache_child_init, NULL, NULL, APR_HOOK_MIDDLE);
471 }
472
473 AP_DECLARE_MODULE(authn_socache) =
474 {
475     STANDARD20_MODULE_STUFF,
476     authn_cache_dircfg_create,
477     authn_cache_dircfg_merge,
478     NULL,
479     NULL,
480     authn_cache_cmds,
481     register_hooks
482 };