1 | /** |
---|
2 | * Copyright 2004-2005 Paul Querna |
---|
3 | * Copyright 2008 Nikos Mavrogiannopoulos |
---|
4 | * Copyright 2011 Dash Shendy |
---|
5 | * Copyright 2015-2016 Thomas Klute |
---|
6 | * |
---|
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
8 | * you may not use this file except in compliance with the License. |
---|
9 | * You may obtain a copy of the License at |
---|
10 | * |
---|
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
12 | * |
---|
13 | * Unless required by applicable law or agreed to in writing, software |
---|
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
16 | * See the License for the specific language governing permissions and |
---|
17 | * limitations under the License. |
---|
18 | * |
---|
19 | */ |
---|
20 | |
---|
21 | #include "gnutls_cache.h" |
---|
22 | #include "mod_gnutls.h" |
---|
23 | |
---|
24 | #if HAVE_APR_MEMCACHE |
---|
25 | #include "apr_memcache.h" |
---|
26 | #endif |
---|
27 | |
---|
28 | #include "apr_dbm.h" |
---|
29 | #include <apr_escape.h> |
---|
30 | |
---|
31 | #include "ap_mpm.h" |
---|
32 | #include <util_mutex.h> |
---|
33 | |
---|
34 | #include <unistd.h> |
---|
35 | #include <sys/types.h> |
---|
36 | |
---|
37 | #if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE) |
---|
38 | #include "unixd.h" |
---|
39 | #endif |
---|
40 | |
---|
41 | /* default cache timeout */ |
---|
42 | #define MGS_DEFAULT_CACHE_TIMEOUT 300 |
---|
43 | |
---|
44 | /* it seems the default has some strange errors. Use SDBM |
---|
45 | */ |
---|
46 | #define MC_TAG "mod_gnutls:" |
---|
47 | /* two characters per byte, plus one more for '\0' */ |
---|
48 | #define GNUTLS_SESSION_ID_STRING_LEN ((GNUTLS_MAX_SESSION_ID_SIZE * 2) + 1) |
---|
49 | |
---|
50 | #if MODULE_MAGIC_NUMBER_MAJOR < 20081201 |
---|
51 | #define ap_unixd_config unixd_config |
---|
52 | #endif |
---|
53 | |
---|
54 | #ifdef APLOG_USE_MODULE |
---|
55 | APLOG_USE_MODULE(gnutls); |
---|
56 | #endif |
---|
57 | |
---|
58 | /* Name the Session ID as: |
---|
59 | * server:port.SessionID |
---|
60 | * to disallow resuming sessions on different servers |
---|
61 | */ |
---|
62 | static int mgs_session_id2dbm(conn_rec *c, unsigned char *id, int idlen, |
---|
63 | gnutls_datum_t *dbmkey) |
---|
64 | { |
---|
65 | char sz[GNUTLS_SESSION_ID_STRING_LEN]; |
---|
66 | apr_status_t rv = apr_escape_hex(sz, id, idlen, 0, NULL); |
---|
67 | if (rv != APR_SUCCESS) |
---|
68 | return -1; |
---|
69 | |
---|
70 | char *newkey = apr_psprintf(c->pool, "%s:%d.%s", |
---|
71 | c->base_server->server_hostname, |
---|
72 | c->base_server->port, sz); |
---|
73 | dbmkey->size = strlen(newkey); |
---|
74 | /* signedness does not matter for arbitrary bits */ |
---|
75 | dbmkey->data = (unsigned char*) newkey; |
---|
76 | return 0; |
---|
77 | } |
---|
78 | |
---|
79 | #define CTIME "%b %d %k:%M:%S %Y %Z" |
---|
80 | |
---|
81 | char *mgs_time2sz(time_t in_time, char *str, int strsize) { |
---|
82 | apr_time_exp_t vtm; |
---|
83 | apr_size_t ret_size; |
---|
84 | apr_time_t t; |
---|
85 | |
---|
86 | |
---|
87 | apr_time_ansi_put(&t, in_time); |
---|
88 | apr_time_exp_gmt(&vtm, t); |
---|
89 | apr_strftime(str, &ret_size, strsize - 1, CTIME, &vtm); |
---|
90 | |
---|
91 | return str; |
---|
92 | } |
---|
93 | |
---|
94 | #if HAVE_APR_MEMCACHE |
---|
95 | |
---|
96 | /* Name the Session ID as: |
---|
97 | * server:port.SessionID |
---|
98 | * to disallow resuming sessions on different servers |
---|
99 | */ |
---|
100 | static char *mgs_session_id2mc(conn_rec * c, unsigned char *id, int idlen) |
---|
101 | { |
---|
102 | char sz[GNUTLS_SESSION_ID_STRING_LEN]; |
---|
103 | apr_status_t rv = apr_escape_hex(sz, id, idlen, 0, NULL); |
---|
104 | if (rv != APR_SUCCESS) |
---|
105 | return NULL; |
---|
106 | |
---|
107 | return apr_psprintf(c->pool, MC_TAG "%s:%d.%s", |
---|
108 | c->base_server->server_hostname, |
---|
109 | c->base_server->port, sz); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * GnuTLS Session Cache using libmemcached |
---|
114 | * |
---|
115 | */ |
---|
116 | |
---|
117 | /* The underlying apr_memcache system is thread safe... woohoo */ |
---|
118 | static apr_memcache_t *mc; |
---|
119 | |
---|
120 | static int mc_cache_child_init(apr_pool_t * p, server_rec * s, |
---|
121 | mgs_srvconf_rec * sc) { |
---|
122 | apr_status_t rv = APR_SUCCESS; |
---|
123 | int thread_limit = 0; |
---|
124 | int nservers = 0; |
---|
125 | char *cache_config; |
---|
126 | char *split; |
---|
127 | char *tok; |
---|
128 | |
---|
129 | ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit); |
---|
130 | |
---|
131 | /* Find all the servers in the first run to get a total count */ |
---|
132 | cache_config = apr_pstrdup(p, sc->cache_config); |
---|
133 | split = apr_strtok(cache_config, " ", &tok); |
---|
134 | while (split) { |
---|
135 | nservers++; |
---|
136 | split = apr_strtok(NULL, " ", &tok); |
---|
137 | } |
---|
138 | |
---|
139 | rv = apr_memcache_create(p, nservers, 0, &mc); |
---|
140 | if (rv != APR_SUCCESS) { |
---|
141 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
142 | "[gnutls_cache] Failed to create Memcache Object of '%d' size.", |
---|
143 | nservers); |
---|
144 | return rv; |
---|
145 | } |
---|
146 | |
---|
147 | /* Now add each server to the memcache */ |
---|
148 | cache_config = apr_pstrdup(p, sc->cache_config); |
---|
149 | split = apr_strtok(cache_config, " ", &tok); |
---|
150 | while (split) { |
---|
151 | apr_memcache_server_t *st; |
---|
152 | char *host_str; |
---|
153 | char *scope_id; |
---|
154 | apr_port_t port; |
---|
155 | |
---|
156 | rv = apr_parse_addr_port(&host_str, &scope_id, &port, |
---|
157 | split, p); |
---|
158 | if (rv != APR_SUCCESS) { |
---|
159 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
160 | "[gnutls_cache] Failed to Parse Server: '%s'", |
---|
161 | split); |
---|
162 | return rv; |
---|
163 | } |
---|
164 | |
---|
165 | if (host_str == NULL) { |
---|
166 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
167 | "[gnutls_cache] Failed to Parse Server, " |
---|
168 | "no hostname specified: '%s'", split); |
---|
169 | return rv; |
---|
170 | } |
---|
171 | |
---|
172 | if (port == 0) { |
---|
173 | port = 11211; /* default port */ |
---|
174 | } |
---|
175 | |
---|
176 | /* Should Max Conns be (thread_limit / nservers) ? */ |
---|
177 | rv = apr_memcache_server_create(p, |
---|
178 | host_str, port, |
---|
179 | 0, |
---|
180 | 1, thread_limit, 600, &st); |
---|
181 | if (rv != APR_SUCCESS) { |
---|
182 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
183 | "[gnutls_cache] Failed to Create Server: %s:%d", |
---|
184 | host_str, port); |
---|
185 | return rv; |
---|
186 | } |
---|
187 | |
---|
188 | rv = apr_memcache_add_server(mc, st); |
---|
189 | if (rv != APR_SUCCESS) { |
---|
190 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
191 | "[gnutls_cache] Failed to Add Server: %s:%d", |
---|
192 | host_str, port); |
---|
193 | return rv; |
---|
194 | } |
---|
195 | |
---|
196 | split = apr_strtok(NULL, " ", &tok); |
---|
197 | } |
---|
198 | return rv; |
---|
199 | } |
---|
200 | |
---|
201 | static int mc_cache_store(void *baton, gnutls_datum_t key, |
---|
202 | gnutls_datum_t data) { |
---|
203 | apr_status_t rv = APR_SUCCESS; |
---|
204 | mgs_handle_t *ctxt = baton; |
---|
205 | char *strkey = NULL; |
---|
206 | apr_uint32_t timeout; |
---|
207 | |
---|
208 | strkey = mgs_session_id2mc(ctxt->c, key.data, key.size); |
---|
209 | if (!strkey) |
---|
210 | return -1; |
---|
211 | |
---|
212 | timeout = apr_time_sec(ctxt->sc->cache_timeout); |
---|
213 | |
---|
214 | rv = apr_memcache_set(mc, strkey, (char *) data.data, data.size, timeout, |
---|
215 | 0); |
---|
216 | |
---|
217 | if (rv != APR_SUCCESS) { |
---|
218 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, |
---|
219 | ctxt->c->base_server, |
---|
220 | "[gnutls_cache] error setting key '%s' " |
---|
221 | "with %d bytes of data", strkey, data.size); |
---|
222 | return -1; |
---|
223 | } |
---|
224 | |
---|
225 | return 0; |
---|
226 | } |
---|
227 | |
---|
228 | static gnutls_datum_t mc_cache_fetch(void *baton, gnutls_datum_t key) { |
---|
229 | apr_status_t rv = APR_SUCCESS; |
---|
230 | mgs_handle_t *ctxt = baton; |
---|
231 | char *strkey = NULL; |
---|
232 | char *value; |
---|
233 | apr_size_t value_len; |
---|
234 | gnutls_datum_t data = {NULL, 0}; |
---|
235 | |
---|
236 | strkey = mgs_session_id2mc(ctxt->c, key.data, key.size); |
---|
237 | if (!strkey) { |
---|
238 | return data; |
---|
239 | } |
---|
240 | |
---|
241 | rv = apr_memcache_getp(mc, ctxt->c->pool, strkey, |
---|
242 | &value, &value_len, NULL); |
---|
243 | |
---|
244 | if (rv != APR_SUCCESS) { |
---|
245 | #if MOD_GNUTLS_DEBUG |
---|
246 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, |
---|
247 | ctxt->c->base_server, |
---|
248 | "[gnutls_cache] error fetching key '%s' ", |
---|
249 | strkey); |
---|
250 | #endif |
---|
251 | data.size = 0; |
---|
252 | data.data = NULL; |
---|
253 | return data; |
---|
254 | } |
---|
255 | |
---|
256 | /* TODO: Eliminate this memcpy. gnutls-- */ |
---|
257 | data.data = gnutls_malloc(value_len); |
---|
258 | if (data.data == NULL) |
---|
259 | return data; |
---|
260 | |
---|
261 | data.size = value_len; |
---|
262 | memcpy(data.data, value, value_len); |
---|
263 | |
---|
264 | return data; |
---|
265 | } |
---|
266 | |
---|
267 | static int mc_cache_delete(void *baton, gnutls_datum_t key) { |
---|
268 | apr_status_t rv = APR_SUCCESS; |
---|
269 | mgs_handle_t *ctxt = baton; |
---|
270 | char *strkey = NULL; |
---|
271 | |
---|
272 | strkey = mgs_session_id2mc(ctxt->c, key.data, key.size); |
---|
273 | if (!strkey) |
---|
274 | return -1; |
---|
275 | |
---|
276 | rv = apr_memcache_delete(mc, strkey, 0); |
---|
277 | |
---|
278 | if (rv != APR_SUCCESS) { |
---|
279 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, |
---|
280 | ctxt->c->base_server, |
---|
281 | "[gnutls_cache] error deleting key '%s' ", |
---|
282 | strkey); |
---|
283 | return -1; |
---|
284 | } |
---|
285 | |
---|
286 | return 0; |
---|
287 | } |
---|
288 | |
---|
289 | #endif /* have_apr_memcache */ |
---|
290 | |
---|
291 | static const char *db_type(mgs_srvconf_rec * sc) { |
---|
292 | if (sc->cache_type == mgs_cache_gdbm) |
---|
293 | return "gdbm"; |
---|
294 | else |
---|
295 | return "db"; |
---|
296 | } |
---|
297 | |
---|
298 | #define SSL_DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD ) |
---|
299 | |
---|
300 | /*** |
---|
301 | * The signatures of the dbm_cache_...() functions may be a bit |
---|
302 | * confusing: "store" and "expire" take a server_rec, "fetch" an |
---|
303 | * mgs_handle_t, and "delete" the void* required for a |
---|
304 | * gnutls_db_remove_func. The first two have matching ..._session |
---|
305 | * functions to fit their respective GnuTLS session cache signatures. |
---|
306 | * |
---|
307 | * This is because "store", "expire", and "fetch" are also needed for |
---|
308 | * the OCSP cache. Their ..._session variants have been created to |
---|
309 | * take care of the session cache specific parts, mainly calculating |
---|
310 | * the DB key from the session ID. They have to match the appropriate |
---|
311 | * GnuTLS DB function signatures. |
---|
312 | * |
---|
313 | * To update cached OCSP responses independent of client connections, |
---|
314 | * "store" and "expire" have to work without a connection context. On |
---|
315 | * the other hand "fetch" does not need to do that, because cached |
---|
316 | * OCSP responses will be retrieved for use in client connections. |
---|
317 | ***/ |
---|
318 | |
---|
319 | static void dbm_cache_expire(server_rec *s) |
---|
320 | { |
---|
321 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
322 | ap_get_module_config(s->module_config, &gnutls_module); |
---|
323 | |
---|
324 | apr_status_t rv; |
---|
325 | apr_dbm_t *dbm; |
---|
326 | apr_datum_t dbmkey; |
---|
327 | apr_datum_t dbmval; |
---|
328 | apr_time_t dtime; |
---|
329 | apr_pool_t *spool; |
---|
330 | int total, deleted; |
---|
331 | |
---|
332 | apr_time_t now = apr_time_now(); |
---|
333 | |
---|
334 | if (now - sc->last_cache_check < (sc->cache_timeout) / 2) |
---|
335 | return; |
---|
336 | |
---|
337 | sc->last_cache_check = now; |
---|
338 | |
---|
339 | apr_pool_create(&spool, NULL); |
---|
340 | |
---|
341 | total = 0; |
---|
342 | deleted = 0; |
---|
343 | |
---|
344 | apr_global_mutex_lock(sc->cache_mutex); |
---|
345 | |
---|
346 | rv = apr_dbm_open_ex(&dbm, db_type(sc), |
---|
347 | sc->cache_config, APR_DBM_RWCREATE, |
---|
348 | SSL_DBM_FILE_MODE, spool); |
---|
349 | if (rv != APR_SUCCESS) { |
---|
350 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, s, |
---|
351 | "[gnutls_cache] error opening cache '%s'", |
---|
352 | sc->cache_config); |
---|
353 | apr_global_mutex_unlock(sc->cache_mutex); |
---|
354 | apr_pool_destroy(spool); |
---|
355 | return; |
---|
356 | } |
---|
357 | |
---|
358 | apr_dbm_firstkey(dbm, &dbmkey); |
---|
359 | while (dbmkey.dptr != NULL) { |
---|
360 | apr_dbm_fetch(dbm, dbmkey, &dbmval); |
---|
361 | if (dbmval.dptr != NULL |
---|
362 | && dbmval.dsize >= sizeof (apr_time_t)) { |
---|
363 | memcpy(&dtime, dbmval.dptr, sizeof (apr_time_t)); |
---|
364 | |
---|
365 | if (now >= dtime) { |
---|
366 | apr_dbm_delete(dbm, dbmkey); |
---|
367 | deleted++; |
---|
368 | } |
---|
369 | apr_dbm_freedatum(dbm, dbmval); |
---|
370 | } else { |
---|
371 | apr_dbm_delete(dbm, dbmkey); |
---|
372 | deleted++; |
---|
373 | } |
---|
374 | total++; |
---|
375 | apr_dbm_nextkey(dbm, &dbmkey); |
---|
376 | } |
---|
377 | apr_dbm_close(dbm); |
---|
378 | |
---|
379 | rv = apr_global_mutex_unlock(sc->cache_mutex); |
---|
380 | |
---|
381 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, s, |
---|
382 | "[gnutls_cache] Cleaned up cache '%s'. Deleted %d and left %d", |
---|
383 | sc->cache_config, deleted, total - deleted); |
---|
384 | |
---|
385 | apr_pool_destroy(spool); |
---|
386 | |
---|
387 | return; |
---|
388 | } |
---|
389 | |
---|
390 | gnutls_datum_t dbm_cache_fetch(mgs_handle_t *ctxt, gnutls_datum_t key) |
---|
391 | { |
---|
392 | gnutls_datum_t data = {NULL, 0}; |
---|
393 | apr_dbm_t *dbm; |
---|
394 | apr_datum_t dbmkey = {(char*) key.data, key.size}; |
---|
395 | apr_datum_t dbmval; |
---|
396 | apr_time_t expiry = 0; |
---|
397 | apr_status_t rv; |
---|
398 | |
---|
399 | /* check if it is time for cache expiration */ |
---|
400 | dbm_cache_expire(ctxt->c->base_server); |
---|
401 | |
---|
402 | apr_global_mutex_lock(ctxt->sc->cache_mutex); |
---|
403 | |
---|
404 | rv = apr_dbm_open_ex(&dbm, db_type(ctxt->sc), |
---|
405 | ctxt->sc->cache_config, APR_DBM_READONLY, |
---|
406 | SSL_DBM_FILE_MODE, ctxt->c->pool); |
---|
407 | if (rv != APR_SUCCESS) { |
---|
408 | ap_log_cerror(APLOG_MARK, APLOG_NOTICE, rv, ctxt->c, |
---|
409 | "error opening cache '%s'", |
---|
410 | ctxt->sc->cache_config); |
---|
411 | apr_global_mutex_unlock(ctxt->sc->cache_mutex); |
---|
412 | return data; |
---|
413 | } |
---|
414 | |
---|
415 | rv = apr_dbm_fetch(dbm, dbmkey, &dbmval); |
---|
416 | |
---|
417 | if (rv != APR_SUCCESS) |
---|
418 | goto close_db; |
---|
419 | |
---|
420 | if (dbmval.dptr == NULL || dbmval.dsize <= sizeof (apr_time_t)) |
---|
421 | goto cleanup; |
---|
422 | |
---|
423 | data.size = dbmval.dsize - sizeof (apr_time_t); |
---|
424 | /* get data expiration tag */ |
---|
425 | expiry = *((apr_time_t *) dbmval.dptr); |
---|
426 | |
---|
427 | data.data = gnutls_malloc(data.size); |
---|
428 | if (data.data == NULL) |
---|
429 | goto cleanup; |
---|
430 | |
---|
431 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, ctxt->c, |
---|
432 | "fetched %ld bytes from cache", |
---|
433 | dbmval.dsize); |
---|
434 | |
---|
435 | memcpy(data.data, dbmval.dptr + sizeof (apr_time_t), data.size); |
---|
436 | |
---|
437 | cleanup: |
---|
438 | apr_dbm_freedatum(dbm, dbmval); |
---|
439 | close_db: |
---|
440 | apr_dbm_close(dbm); |
---|
441 | apr_global_mutex_unlock(ctxt->sc->cache_mutex); |
---|
442 | |
---|
443 | /* cache entry might have expired since last cache cleanup */ |
---|
444 | if (expiry != 0 && expiry < apr_time_now()) |
---|
445 | { |
---|
446 | gnutls_free(data.data); |
---|
447 | data.data = NULL; |
---|
448 | data.size = 0; |
---|
449 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, ctxt->c, |
---|
450 | "dropped expired cache data"); |
---|
451 | } |
---|
452 | |
---|
453 | return data; |
---|
454 | } |
---|
455 | |
---|
456 | static gnutls_datum_t dbm_cache_fetch_session(void *baton, gnutls_datum_t key) |
---|
457 | { |
---|
458 | gnutls_datum_t data = {NULL, 0}; |
---|
459 | gnutls_datum_t dbmkey; |
---|
460 | mgs_handle_t *ctxt = baton; |
---|
461 | |
---|
462 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0) |
---|
463 | return data; |
---|
464 | |
---|
465 | return dbm_cache_fetch(ctxt, dbmkey); |
---|
466 | } |
---|
467 | |
---|
468 | int dbm_cache_store(server_rec *s, gnutls_datum_t key, |
---|
469 | gnutls_datum_t data, apr_time_t expiry) |
---|
470 | { |
---|
471 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
472 | ap_get_module_config(s->module_config, &gnutls_module); |
---|
473 | |
---|
474 | apr_dbm_t *dbm; |
---|
475 | apr_datum_t dbmkey = {(char*) key.data, key.size}; |
---|
476 | apr_datum_t dbmval; |
---|
477 | apr_status_t rv; |
---|
478 | apr_pool_t *spool; |
---|
479 | |
---|
480 | /* check if it is time for cache expiration */ |
---|
481 | dbm_cache_expire(s); |
---|
482 | |
---|
483 | apr_pool_create(&spool, NULL); |
---|
484 | |
---|
485 | /* create DBM value */ |
---|
486 | dbmval.dsize = data.size + sizeof (apr_time_t); |
---|
487 | dbmval.dptr = (char *) apr_palloc(spool, dbmval.dsize); |
---|
488 | |
---|
489 | /* prepend expiration time */ |
---|
490 | memcpy((char *) dbmval.dptr, &expiry, sizeof (apr_time_t)); |
---|
491 | memcpy((char *) dbmval.dptr + sizeof (apr_time_t), |
---|
492 | data.data, data.size); |
---|
493 | |
---|
494 | apr_global_mutex_lock(sc->cache_mutex); |
---|
495 | |
---|
496 | rv = apr_dbm_open_ex(&dbm, db_type(sc), |
---|
497 | sc->cache_config, APR_DBM_RWCREATE, |
---|
498 | SSL_DBM_FILE_MODE, spool); |
---|
499 | if (rv != APR_SUCCESS) |
---|
500 | { |
---|
501 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, s, |
---|
502 | "error opening cache '%s'", |
---|
503 | sc->cache_config); |
---|
504 | apr_global_mutex_unlock(sc->cache_mutex); |
---|
505 | apr_pool_destroy(spool); |
---|
506 | return -1; |
---|
507 | } |
---|
508 | |
---|
509 | rv = apr_dbm_store(dbm, dbmkey, dbmval); |
---|
510 | if (rv != APR_SUCCESS) |
---|
511 | { |
---|
512 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, s, |
---|
513 | "error storing in cache '%s'", |
---|
514 | sc->cache_config); |
---|
515 | apr_dbm_close(dbm); |
---|
516 | apr_global_mutex_unlock(sc->cache_mutex); |
---|
517 | apr_pool_destroy(spool); |
---|
518 | return -1; |
---|
519 | } |
---|
520 | |
---|
521 | apr_dbm_close(dbm); |
---|
522 | apr_global_mutex_unlock(sc->cache_mutex); |
---|
523 | |
---|
524 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, s, |
---|
525 | "stored %ld bytes of data (%ld byte key) in cache '%s'", |
---|
526 | dbmval.dsize, dbmkey.dsize, sc->cache_config); |
---|
527 | |
---|
528 | apr_pool_destroy(spool); |
---|
529 | |
---|
530 | return 0; |
---|
531 | } |
---|
532 | |
---|
533 | static int dbm_cache_store_session(void *baton, gnutls_datum_t key, |
---|
534 | gnutls_datum_t data) |
---|
535 | { |
---|
536 | mgs_handle_t *ctxt = baton; |
---|
537 | gnutls_datum_t dbmkey; |
---|
538 | |
---|
539 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0) |
---|
540 | return -1; |
---|
541 | |
---|
542 | apr_time_t expiry = apr_time_now() + ctxt->sc->cache_timeout; |
---|
543 | |
---|
544 | return dbm_cache_store(ctxt->c->base_server, dbmkey, data, expiry); |
---|
545 | } |
---|
546 | |
---|
547 | static int dbm_cache_delete(void *baton, gnutls_datum_t key) |
---|
548 | { |
---|
549 | apr_dbm_t *dbm; |
---|
550 | gnutls_datum_t tmpkey; |
---|
551 | mgs_handle_t *ctxt = baton; |
---|
552 | apr_status_t rv; |
---|
553 | |
---|
554 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &tmpkey) < 0) |
---|
555 | return -1; |
---|
556 | apr_datum_t dbmkey = {(char*) tmpkey.data, tmpkey.size}; |
---|
557 | |
---|
558 | apr_global_mutex_lock(ctxt->sc->cache_mutex); |
---|
559 | |
---|
560 | rv = apr_dbm_open_ex(&dbm, db_type(ctxt->sc), |
---|
561 | ctxt->sc->cache_config, APR_DBM_RWCREATE, |
---|
562 | SSL_DBM_FILE_MODE, ctxt->c->pool); |
---|
563 | if (rv != APR_SUCCESS) { |
---|
564 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, |
---|
565 | ctxt->c->base_server, |
---|
566 | "[gnutls_cache] error opening cache '%s'", |
---|
567 | ctxt->sc->cache_config); |
---|
568 | apr_global_mutex_unlock(ctxt->sc->cache_mutex); |
---|
569 | return -1; |
---|
570 | } |
---|
571 | |
---|
572 | rv = apr_dbm_delete(dbm, dbmkey); |
---|
573 | |
---|
574 | if (rv != APR_SUCCESS) { |
---|
575 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, |
---|
576 | ctxt->c->base_server, |
---|
577 | "[gnutls_cache] error deleting from cache '%s'", |
---|
578 | ctxt->sc->cache_config); |
---|
579 | apr_dbm_close(dbm); |
---|
580 | apr_global_mutex_unlock(ctxt->sc->cache_mutex); |
---|
581 | return -1; |
---|
582 | } |
---|
583 | |
---|
584 | apr_dbm_close(dbm); |
---|
585 | apr_global_mutex_unlock(ctxt->sc->cache_mutex); |
---|
586 | |
---|
587 | return 0; |
---|
588 | } |
---|
589 | |
---|
590 | static int dbm_cache_post_config(apr_pool_t * p, server_rec * s, |
---|
591 | mgs_srvconf_rec * sc) { |
---|
592 | apr_status_t rv; |
---|
593 | apr_dbm_t *dbm; |
---|
594 | const char *path1; |
---|
595 | const char *path2; |
---|
596 | |
---|
597 | rv = apr_dbm_open_ex(&dbm, db_type(sc), sc->cache_config, |
---|
598 | APR_DBM_RWCREATE, SSL_DBM_FILE_MODE, p); |
---|
599 | |
---|
600 | if (rv != APR_SUCCESS) { |
---|
601 | ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, |
---|
602 | "GnuTLS: Cannot create DBM Cache at `%s'", |
---|
603 | sc->cache_config); |
---|
604 | return rv; |
---|
605 | } |
---|
606 | |
---|
607 | apr_dbm_close(dbm); |
---|
608 | |
---|
609 | apr_dbm_get_usednames_ex(p, db_type(sc), sc->cache_config, &path1, |
---|
610 | &path2); |
---|
611 | |
---|
612 | /* The Following Code takes logic directly from mod_ssl's DBM Cache */ |
---|
613 | #if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE) |
---|
614 | /* Running as Root */ |
---|
615 | if (path1 && geteuid() == 0) { |
---|
616 | if (0 != chown(path1, ap_unixd_config.user_id, -1)) |
---|
617 | ap_log_error(APLOG_MARK, APLOG_NOTICE, -1, s, |
---|
618 | "GnuTLS: could not chown cache path1 `%s' to uid %d (errno: %d)", |
---|
619 | path1, ap_unixd_config.user_id, errno); |
---|
620 | if (path2 != NULL) { |
---|
621 | if (0 != chown(path2, ap_unixd_config.user_id, -1)) |
---|
622 | ap_log_error(APLOG_MARK, APLOG_NOTICE, -1, s, |
---|
623 | "GnuTLS: could not chown cache path2 `%s' to uid %d (errno: %d)", |
---|
624 | path2, ap_unixd_config.user_id, errno); |
---|
625 | } |
---|
626 | } |
---|
627 | #endif |
---|
628 | |
---|
629 | return rv; |
---|
630 | } |
---|
631 | |
---|
632 | int mgs_cache_post_config(apr_pool_t * p, server_rec * s, |
---|
633 | mgs_srvconf_rec * sc) { |
---|
634 | |
---|
635 | /* if GnuTLSCache was never explicitly set: */ |
---|
636 | if (sc->cache_type == mgs_cache_unset) |
---|
637 | sc->cache_type = mgs_cache_none; |
---|
638 | /* if GnuTLSCacheTimeout was never explicitly set: */ |
---|
639 | if (sc->cache_timeout == -1) |
---|
640 | sc->cache_timeout = apr_time_from_sec(MGS_DEFAULT_CACHE_TIMEOUT); |
---|
641 | |
---|
642 | /* initialize mutex only once */ |
---|
643 | if (sc->cache_mutex == NULL) |
---|
644 | { |
---|
645 | apr_status_t rv = ap_global_mutex_create(&sc->cache_mutex, NULL, |
---|
646 | MGS_CACHE_MUTEX_NAME, |
---|
647 | NULL, s, p, 0); |
---|
648 | if (rv != APR_SUCCESS) |
---|
649 | return rv; |
---|
650 | } |
---|
651 | |
---|
652 | if (sc->cache_type == mgs_cache_dbm |
---|
653 | || sc->cache_type == mgs_cache_gdbm) { |
---|
654 | return dbm_cache_post_config(p, s, sc); |
---|
655 | } |
---|
656 | |
---|
657 | return APR_SUCCESS; |
---|
658 | } |
---|
659 | |
---|
660 | #if HAVE_APR_MEMCACHE |
---|
661 | int mgs_cache_child_init(apr_pool_t * p, |
---|
662 | server_rec * s, |
---|
663 | mgs_srvconf_rec * sc) |
---|
664 | #else |
---|
665 | int mgs_cache_child_init(apr_pool_t * p __attribute__((unused)), |
---|
666 | server_rec * s __attribute__((unused)), |
---|
667 | mgs_srvconf_rec * sc) |
---|
668 | #endif |
---|
669 | { |
---|
670 | if (sc->cache_type == mgs_cache_dbm |
---|
671 | || sc->cache_type == mgs_cache_gdbm) { |
---|
672 | return 0; |
---|
673 | } |
---|
674 | #if HAVE_APR_MEMCACHE |
---|
675 | else if (sc->cache_type == mgs_cache_memcache) { |
---|
676 | return mc_cache_child_init(p, s, sc); |
---|
677 | } |
---|
678 | #endif |
---|
679 | return 0; |
---|
680 | } |
---|
681 | |
---|
682 | #include <assert.h> |
---|
683 | |
---|
684 | int mgs_cache_session_init(mgs_handle_t * ctxt) { |
---|
685 | if (ctxt->sc->cache_type == mgs_cache_dbm |
---|
686 | || ctxt->sc->cache_type == mgs_cache_gdbm) { |
---|
687 | gnutls_db_set_retrieve_function(ctxt->session, |
---|
688 | dbm_cache_fetch_session); |
---|
689 | gnutls_db_set_remove_function(ctxt->session, |
---|
690 | dbm_cache_delete); |
---|
691 | gnutls_db_set_store_function(ctxt->session, |
---|
692 | dbm_cache_store_session); |
---|
693 | gnutls_db_set_ptr(ctxt->session, ctxt); |
---|
694 | } |
---|
695 | #if HAVE_APR_MEMCACHE |
---|
696 | else if (ctxt->sc->cache_type == mgs_cache_memcache) { |
---|
697 | gnutls_db_set_retrieve_function(ctxt->session, |
---|
698 | mc_cache_fetch); |
---|
699 | gnutls_db_set_remove_function(ctxt->session, |
---|
700 | mc_cache_delete); |
---|
701 | gnutls_db_set_store_function(ctxt->session, |
---|
702 | mc_cache_store); |
---|
703 | gnutls_db_set_ptr(ctxt->session, ctxt); |
---|
704 | } |
---|
705 | #endif |
---|
706 | |
---|
707 | return 0; |
---|
708 | } |
---|