[fcb122d] | 1 | /** |
---|
| 2 | * Copyright 2004-2005 Paul Querna |
---|
[e183628] | 3 | * Copyright 2008 Nikos Mavrogiannopoulos |
---|
| 4 | * Copyright 2011 Dash Shendy |
---|
[8913410] | 5 | * Copyright 2015-2016 Thomas Klute |
---|
[0b3bc05] | 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 | |
---|
[04e6e65] | 21 | #include "gnutls_cache.h" |
---|
[0b3bc05] | 22 | #include "mod_gnutls.h" |
---|
[6e0bfd6] | 23 | |
---|
| 24 | #if HAVE_APR_MEMCACHE |
---|
| 25 | #include "apr_memcache.h" |
---|
| 26 | #endif |
---|
| 27 | |
---|
[fcb122d] | 28 | #include "apr_dbm.h" |
---|
| 29 | |
---|
[a66e147] | 30 | #include "ap_mpm.h" |
---|
[0b3bc05] | 31 | |
---|
[fcb122d] | 32 | #include <unistd.h> |
---|
| 33 | #include <sys/types.h> |
---|
| 34 | |
---|
| 35 | #if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE) |
---|
| 36 | #include "unixd.h" |
---|
| 37 | #endif |
---|
| 38 | |
---|
[671b64f] | 39 | /* it seems the default has some strange errors. Use SDBM |
---|
[03a9a6b] | 40 | */ |
---|
[6e0bfd6] | 41 | #define MC_TAG "mod_gnutls:" |
---|
[c055502] | 42 | #define MC_TAG_LEN sizeof(MC_TAG) |
---|
[6e0bfd6] | 43 | #define STR_SESSION_LEN (GNUTLS_SESSION_ID_STRING_LEN + MC_TAG_LEN) |
---|
| 44 | |
---|
[7e67487] | 45 | #if MODULE_MAGIC_NUMBER_MAJOR < 20081201 |
---|
| 46 | #define ap_unixd_config unixd_config |
---|
| 47 | #endif |
---|
| 48 | |
---|
[55dc3f0] | 49 | #ifdef APLOG_USE_MODULE |
---|
| 50 | APLOG_USE_MODULE(gnutls); |
---|
| 51 | #endif |
---|
| 52 | |
---|
[c055502] | 53 | char *mgs_session_id2sz(unsigned char *id, int idlen, |
---|
[e183628] | 54 | char *str, int strsize) { |
---|
| 55 | char *cp; |
---|
| 56 | int n; |
---|
| 57 | |
---|
| 58 | cp = str; |
---|
| 59 | for (n = 0; n < idlen && n < GNUTLS_MAX_SESSION_ID; n++) { |
---|
| 60 | apr_snprintf(cp, strsize - (cp - str), "%02X", id[n]); |
---|
| 61 | cp += 2; |
---|
| 62 | } |
---|
| 63 | *cp = '\0'; |
---|
| 64 | return str; |
---|
[6e0bfd6] | 65 | } |
---|
[c055502] | 66 | |
---|
| 67 | /* Name the Session ID as: |
---|
[c223c85] | 68 | * server:port.SessionID |
---|
[c055502] | 69 | * to disallow resuming sessions on different servers |
---|
| 70 | */ |
---|
[e02dd8c] | 71 | static int mgs_session_id2dbm(conn_rec * c, unsigned char *id, int idlen, |
---|
[e183628] | 72 | apr_datum_t * dbmkey) { |
---|
| 73 | char buf[STR_SESSION_LEN]; |
---|
| 74 | char *sz; |
---|
| 75 | |
---|
| 76 | sz = mgs_session_id2sz(id, idlen, buf, sizeof (buf)); |
---|
| 77 | if (sz == NULL) |
---|
| 78 | return -1; |
---|
| 79 | |
---|
| 80 | dbmkey->dptr = |
---|
| 81 | apr_psprintf(c->pool, "%s:%d.%s", |
---|
| 82 | c->base_server->server_hostname, |
---|
| 83 | c->base_server->port, sz); |
---|
| 84 | dbmkey->dsize = strlen(dbmkey->dptr); |
---|
| 85 | |
---|
| 86 | return 0; |
---|
[c055502] | 87 | } |
---|
[7bebb42] | 88 | |
---|
| 89 | #define CTIME "%b %d %k:%M:%S %Y %Z" |
---|
[e02dd8c] | 90 | |
---|
[e183628] | 91 | char *mgs_time2sz(time_t in_time, char *str, int strsize) { |
---|
| 92 | apr_time_exp_t vtm; |
---|
| 93 | apr_size_t ret_size; |
---|
| 94 | apr_time_t t; |
---|
[e02dd8c] | 95 | |
---|
| 96 | |
---|
[e183628] | 97 | apr_time_ansi_put(&t, in_time); |
---|
| 98 | apr_time_exp_gmt(&vtm, t); |
---|
| 99 | apr_strftime(str, &ret_size, strsize - 1, CTIME, &vtm); |
---|
| 100 | |
---|
| 101 | return str; |
---|
[7bebb42] | 102 | } |
---|
[6e0bfd6] | 103 | |
---|
[c055502] | 104 | #if HAVE_APR_MEMCACHE |
---|
[e183628] | 105 | |
---|
[c055502] | 106 | /* Name the Session ID as: |
---|
[c223c85] | 107 | * server:port.SessionID |
---|
[c055502] | 108 | * to disallow resuming sessions on different servers |
---|
| 109 | */ |
---|
[e183628] | 110 | static char *mgs_session_id2mc(conn_rec * c, unsigned char *id, int idlen) { |
---|
| 111 | char buf[STR_SESSION_LEN]; |
---|
| 112 | char *sz; |
---|
| 113 | |
---|
| 114 | sz = mgs_session_id2sz(id, idlen, buf, sizeof (buf)); |
---|
| 115 | if (sz == NULL) |
---|
| 116 | return NULL; |
---|
| 117 | |
---|
| 118 | return apr_psprintf(c->pool, MC_TAG "%s:%d.%s", |
---|
| 119 | c->base_server->server_hostname, |
---|
| 120 | c->base_server->port, sz); |
---|
[42307a9] | 121 | } |
---|
| 122 | |
---|
[0b3bc05] | 123 | /** |
---|
| 124 | * GnuTLS Session Cache using libmemcached |
---|
| 125 | * |
---|
| 126 | */ |
---|
| 127 | |
---|
[a66e147] | 128 | /* The underlying apr_memcache system is thread safe... woohoo */ |
---|
[e02dd8c] | 129 | static apr_memcache_t *mc; |
---|
[a66e147] | 130 | |
---|
[e02dd8c] | 131 | static int mc_cache_child_init(apr_pool_t * p, server_rec * s, |
---|
[e183628] | 132 | mgs_srvconf_rec * sc) { |
---|
| 133 | apr_status_t rv = APR_SUCCESS; |
---|
| 134 | int thread_limit = 0; |
---|
| 135 | int nservers = 0; |
---|
| 136 | char *cache_config; |
---|
| 137 | char *split; |
---|
| 138 | char *tok; |
---|
| 139 | |
---|
| 140 | ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit); |
---|
| 141 | |
---|
| 142 | /* Find all the servers in the first run to get a total count */ |
---|
| 143 | cache_config = apr_pstrdup(p, sc->cache_config); |
---|
| 144 | split = apr_strtok(cache_config, " ", &tok); |
---|
| 145 | while (split) { |
---|
| 146 | nservers++; |
---|
| 147 | split = apr_strtok(NULL, " ", &tok); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | rv = apr_memcache_create(p, nservers, 0, &mc); |
---|
| 151 | if (rv != APR_SUCCESS) { |
---|
| 152 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
| 153 | "[gnutls_cache] Failed to create Memcache Object of '%d' size.", |
---|
| 154 | nservers); |
---|
| 155 | return rv; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | /* Now add each server to the memcache */ |
---|
| 159 | cache_config = apr_pstrdup(p, sc->cache_config); |
---|
| 160 | split = apr_strtok(cache_config, " ", &tok); |
---|
| 161 | while (split) { |
---|
| 162 | apr_memcache_server_t *st; |
---|
| 163 | char *host_str; |
---|
| 164 | char *scope_id; |
---|
| 165 | apr_port_t port; |
---|
| 166 | |
---|
| 167 | rv = apr_parse_addr_port(&host_str, &scope_id, &port, |
---|
| 168 | split, p); |
---|
| 169 | if (rv != APR_SUCCESS) { |
---|
| 170 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
| 171 | "[gnutls_cache] Failed to Parse Server: '%s'", |
---|
| 172 | split); |
---|
| 173 | return rv; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | if (host_str == NULL) { |
---|
| 177 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
| 178 | "[gnutls_cache] Failed to Parse Server, " |
---|
| 179 | "no hostname specified: '%s'", split); |
---|
| 180 | return rv; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | if (port == 0) { |
---|
| 184 | port = 11211; /* default port */ |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /* Should Max Conns be (thread_limit / nservers) ? */ |
---|
| 188 | rv = apr_memcache_server_create(p, |
---|
| 189 | host_str, port, |
---|
| 190 | 0, |
---|
| 191 | 1, thread_limit, 600, &st); |
---|
| 192 | if (rv != APR_SUCCESS) { |
---|
| 193 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
| 194 | "[gnutls_cache] Failed to Create Server: %s:%d", |
---|
| 195 | host_str, port); |
---|
| 196 | return rv; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | rv = apr_memcache_add_server(mc, st); |
---|
| 200 | if (rv != APR_SUCCESS) { |
---|
| 201 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
| 202 | "[gnutls_cache] Failed to Add Server: %s:%d", |
---|
| 203 | host_str, port); |
---|
| 204 | return rv; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | split = apr_strtok(NULL, " ", &tok); |
---|
| 208 | } |
---|
| 209 | return rv; |
---|
[32f2e60] | 210 | } |
---|
[a66e147] | 211 | |
---|
[e02dd8c] | 212 | static int mc_cache_store(void *baton, gnutls_datum_t key, |
---|
[e183628] | 213 | gnutls_datum_t data) { |
---|
| 214 | apr_status_t rv = APR_SUCCESS; |
---|
| 215 | mgs_handle_t *ctxt = baton; |
---|
| 216 | char *strkey = NULL; |
---|
| 217 | apr_uint32_t timeout; |
---|
| 218 | |
---|
| 219 | strkey = mgs_session_id2mc(ctxt->c, key.data, key.size); |
---|
| 220 | if (!strkey) |
---|
| 221 | return -1; |
---|
| 222 | |
---|
| 223 | timeout = apr_time_sec(ctxt->sc->cache_timeout); |
---|
| 224 | |
---|
| 225 | rv = apr_memcache_set(mc, strkey, (char *) data.data, data.size, timeout, |
---|
| 226 | 0); |
---|
| 227 | |
---|
| 228 | if (rv != APR_SUCCESS) { |
---|
| 229 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, |
---|
| 230 | ctxt->c->base_server, |
---|
| 231 | "[gnutls_cache] error setting key '%s' " |
---|
| 232 | "with %d bytes of data", strkey, data.size); |
---|
| 233 | return -1; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | return 0; |
---|
[a66e147] | 237 | } |
---|
| 238 | |
---|
[e183628] | 239 | static gnutls_datum_t mc_cache_fetch(void *baton, gnutls_datum_t key) { |
---|
| 240 | apr_status_t rv = APR_SUCCESS; |
---|
| 241 | mgs_handle_t *ctxt = baton; |
---|
| 242 | char *strkey = NULL; |
---|
| 243 | char *value; |
---|
| 244 | apr_size_t value_len; |
---|
| 245 | gnutls_datum_t data = {NULL, 0}; |
---|
[e02dd8c] | 246 | |
---|
[e183628] | 247 | strkey = mgs_session_id2mc(ctxt->c, key.data, key.size); |
---|
| 248 | if (!strkey) { |
---|
| 249 | return data; |
---|
| 250 | } |
---|
[e02dd8c] | 251 | |
---|
[e183628] | 252 | rv = apr_memcache_getp(mc, ctxt->c->pool, strkey, |
---|
| 253 | &value, &value_len, NULL); |
---|
[e02dd8c] | 254 | |
---|
[e183628] | 255 | if (rv != APR_SUCCESS) { |
---|
[316bd8c] | 256 | #if MOD_GNUTLS_DEBUG |
---|
[e183628] | 257 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, |
---|
| 258 | ctxt->c->base_server, |
---|
| 259 | "[gnutls_cache] error fetching key '%s' ", |
---|
| 260 | strkey); |
---|
[316bd8c] | 261 | #endif |
---|
[e183628] | 262 | data.size = 0; |
---|
| 263 | data.data = NULL; |
---|
| 264 | return data; |
---|
| 265 | } |
---|
[a66e147] | 266 | |
---|
[e183628] | 267 | /* TODO: Eliminate this memcpy. gnutls-- */ |
---|
| 268 | data.data = gnutls_malloc(value_len); |
---|
| 269 | if (data.data == NULL) |
---|
| 270 | return data; |
---|
[a66e147] | 271 | |
---|
[e183628] | 272 | data.size = value_len; |
---|
| 273 | memcpy(data.data, value, value_len); |
---|
[a66e147] | 274 | |
---|
[e183628] | 275 | return data; |
---|
[32f2e60] | 276 | } |
---|
| 277 | |
---|
[e183628] | 278 | static int mc_cache_delete(void *baton, gnutls_datum_t key) { |
---|
| 279 | apr_status_t rv = APR_SUCCESS; |
---|
| 280 | mgs_handle_t *ctxt = baton; |
---|
| 281 | char *strkey = NULL; |
---|
[a66e147] | 282 | |
---|
[e183628] | 283 | strkey = mgs_session_id2mc(ctxt->c, key.data, key.size); |
---|
| 284 | if (!strkey) |
---|
| 285 | return -1; |
---|
[a66e147] | 286 | |
---|
[e183628] | 287 | rv = apr_memcache_delete(mc, strkey, 0); |
---|
[a66e147] | 288 | |
---|
[e183628] | 289 | if (rv != APR_SUCCESS) { |
---|
| 290 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, |
---|
| 291 | ctxt->c->base_server, |
---|
| 292 | "[gnutls_cache] error deleting key '%s' ", |
---|
| 293 | strkey); |
---|
| 294 | return -1; |
---|
| 295 | } |
---|
[a66e147] | 296 | |
---|
[e183628] | 297 | return 0; |
---|
[32f2e60] | 298 | } |
---|
| 299 | |
---|
[410d216] | 300 | #endif /* have_apr_memcache */ |
---|
[6e0bfd6] | 301 | |
---|
[410d216] | 302 | static const char *db_type(mgs_srvconf_rec * sc) { |
---|
[e183628] | 303 | if (sc->cache_type == mgs_cache_gdbm) |
---|
| 304 | return "gdbm"; |
---|
| 305 | else |
---|
| 306 | return "db"; |
---|
[771ca63] | 307 | } |
---|
| 308 | |
---|
[fcb122d] | 309 | #define SSL_DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD ) |
---|
| 310 | |
---|
[e183628] | 311 | static void dbm_cache_expire(mgs_handle_t * ctxt) { |
---|
| 312 | apr_status_t rv; |
---|
| 313 | apr_dbm_t *dbm; |
---|
| 314 | apr_datum_t dbmkey; |
---|
| 315 | apr_datum_t dbmval; |
---|
| 316 | apr_time_t now; |
---|
| 317 | apr_time_t dtime; |
---|
| 318 | apr_pool_t *spool; |
---|
| 319 | int total, deleted; |
---|
| 320 | |
---|
| 321 | now = apr_time_now(); |
---|
| 322 | |
---|
| 323 | if (now - ctxt->sc->last_cache_check < |
---|
| 324 | (ctxt->sc->cache_timeout) / 2) |
---|
| 325 | return; |
---|
| 326 | |
---|
| 327 | ctxt->sc->last_cache_check = now; |
---|
| 328 | |
---|
| 329 | apr_pool_create(&spool, ctxt->c->pool); |
---|
| 330 | |
---|
| 331 | total = 0; |
---|
| 332 | deleted = 0; |
---|
| 333 | |
---|
| 334 | rv = apr_dbm_open_ex(&dbm, db_type(ctxt->sc), |
---|
| 335 | ctxt->sc->cache_config, APR_DBM_RWCREATE, |
---|
| 336 | SSL_DBM_FILE_MODE, spool); |
---|
| 337 | if (rv != APR_SUCCESS) { |
---|
| 338 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, |
---|
| 339 | ctxt->c->base_server, |
---|
| 340 | "[gnutls_cache] error opening cache searcher '%s'", |
---|
| 341 | ctxt->sc->cache_config); |
---|
| 342 | apr_pool_destroy(spool); |
---|
| 343 | return; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | apr_dbm_firstkey(dbm, &dbmkey); |
---|
| 347 | while (dbmkey.dptr != NULL) { |
---|
| 348 | apr_dbm_fetch(dbm, dbmkey, &dbmval); |
---|
| 349 | if (dbmval.dptr != NULL |
---|
| 350 | && dbmval.dsize >= sizeof (apr_time_t)) { |
---|
| 351 | memcpy(&dtime, dbmval.dptr, sizeof (apr_time_t)); |
---|
| 352 | |
---|
| 353 | if (now >= dtime) { |
---|
| 354 | apr_dbm_delete(dbm, dbmkey); |
---|
| 355 | deleted++; |
---|
| 356 | } |
---|
| 357 | apr_dbm_freedatum(dbm, dbmval); |
---|
| 358 | } else { |
---|
| 359 | apr_dbm_delete(dbm, dbmkey); |
---|
| 360 | deleted++; |
---|
| 361 | } |
---|
| 362 | total++; |
---|
| 363 | apr_dbm_nextkey(dbm, &dbmkey); |
---|
| 364 | } |
---|
| 365 | apr_dbm_close(dbm); |
---|
| 366 | |
---|
| 367 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, |
---|
| 368 | ctxt->c->base_server, |
---|
| 369 | "[gnutls_cache] Cleaned up cache '%s'. Deleted %d and left %d", |
---|
| 370 | ctxt->sc->cache_config, deleted, total - deleted); |
---|
| 371 | |
---|
| 372 | apr_pool_destroy(spool); |
---|
| 373 | |
---|
| 374 | return; |
---|
[fcb122d] | 375 | } |
---|
| 376 | |
---|
[e183628] | 377 | static gnutls_datum_t dbm_cache_fetch(void *baton, gnutls_datum_t key) { |
---|
| 378 | gnutls_datum_t data = {NULL, 0}; |
---|
| 379 | apr_dbm_t *dbm; |
---|
| 380 | apr_datum_t dbmkey; |
---|
| 381 | apr_datum_t dbmval; |
---|
| 382 | mgs_handle_t *ctxt = baton; |
---|
| 383 | apr_status_t rv; |
---|
| 384 | |
---|
| 385 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0) |
---|
| 386 | return data; |
---|
| 387 | |
---|
| 388 | rv = apr_dbm_open_ex(&dbm, db_type(ctxt->sc), |
---|
| 389 | ctxt->sc->cache_config, APR_DBM_READONLY, |
---|
| 390 | SSL_DBM_FILE_MODE, ctxt->c->pool); |
---|
| 391 | if (rv != APR_SUCCESS) { |
---|
| 392 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, |
---|
| 393 | ctxt->c->base_server, |
---|
| 394 | "[gnutls_cache] error opening cache '%s'", |
---|
| 395 | ctxt->sc->cache_config); |
---|
| 396 | return data; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | rv = apr_dbm_fetch(dbm, dbmkey, &dbmval); |
---|
| 400 | |
---|
| 401 | if (rv != APR_SUCCESS) { |
---|
| 402 | apr_dbm_close(dbm); |
---|
| 403 | return data; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | if (dbmval.dptr == NULL || dbmval.dsize <= sizeof (apr_time_t)) { |
---|
| 407 | apr_dbm_freedatum(dbm, dbmval); |
---|
| 408 | apr_dbm_close(dbm); |
---|
| 409 | return data; |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | data.size = dbmval.dsize - sizeof (apr_time_t); |
---|
| 413 | |
---|
| 414 | data.data = gnutls_malloc(data.size); |
---|
| 415 | if (data.data == NULL) { |
---|
| 416 | apr_dbm_freedatum(dbm, dbmval); |
---|
| 417 | apr_dbm_close(dbm); |
---|
| 418 | return data; |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | memcpy(data.data, dbmval.dptr + sizeof (apr_time_t), data.size); |
---|
| 422 | |
---|
| 423 | apr_dbm_freedatum(dbm, dbmval); |
---|
| 424 | apr_dbm_close(dbm); |
---|
| 425 | |
---|
| 426 | return data; |
---|
[fcb122d] | 427 | } |
---|
| 428 | |
---|
[e02dd8c] | 429 | static int dbm_cache_store(void *baton, gnutls_datum_t key, |
---|
[e183628] | 430 | gnutls_datum_t data) { |
---|
| 431 | apr_dbm_t *dbm; |
---|
| 432 | apr_datum_t dbmkey; |
---|
| 433 | apr_datum_t dbmval; |
---|
| 434 | mgs_handle_t *ctxt = baton; |
---|
| 435 | apr_status_t rv; |
---|
| 436 | apr_time_t expiry; |
---|
| 437 | apr_pool_t *spool; |
---|
| 438 | |
---|
| 439 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0) |
---|
| 440 | return -1; |
---|
| 441 | |
---|
| 442 | /* we expire dbm only on every store |
---|
| 443 | */ |
---|
| 444 | dbm_cache_expire(ctxt); |
---|
| 445 | |
---|
| 446 | apr_pool_create(&spool, ctxt->c->pool); |
---|
| 447 | |
---|
| 448 | /* create DBM value */ |
---|
| 449 | dbmval.dsize = data.size + sizeof (apr_time_t); |
---|
| 450 | dbmval.dptr = (char *) apr_palloc(spool, dbmval.dsize); |
---|
| 451 | |
---|
| 452 | expiry = apr_time_now() + ctxt->sc->cache_timeout; |
---|
| 453 | |
---|
| 454 | memcpy((char *) dbmval.dptr, &expiry, sizeof (apr_time_t)); |
---|
| 455 | memcpy((char *) dbmval.dptr + sizeof (apr_time_t), |
---|
| 456 | data.data, data.size); |
---|
| 457 | |
---|
| 458 | rv = apr_dbm_open_ex(&dbm, db_type(ctxt->sc), |
---|
| 459 | ctxt->sc->cache_config, APR_DBM_RWCREATE, |
---|
| 460 | SSL_DBM_FILE_MODE, ctxt->c->pool); |
---|
| 461 | if (rv != APR_SUCCESS) { |
---|
| 462 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, |
---|
| 463 | ctxt->c->base_server, |
---|
| 464 | "[gnutls_cache] error opening cache '%s'", |
---|
| 465 | ctxt->sc->cache_config); |
---|
| 466 | apr_pool_destroy(spool); |
---|
| 467 | return -1; |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | rv = apr_dbm_store(dbm, dbmkey, dbmval); |
---|
| 471 | |
---|
| 472 | if (rv != APR_SUCCESS) { |
---|
| 473 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, |
---|
| 474 | ctxt->c->base_server, |
---|
| 475 | "[gnutls_cache] error storing in cache '%s'", |
---|
| 476 | ctxt->sc->cache_config); |
---|
| 477 | apr_dbm_close(dbm); |
---|
| 478 | apr_pool_destroy(spool); |
---|
| 479 | return -1; |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | apr_dbm_close(dbm); |
---|
| 483 | |
---|
| 484 | apr_pool_destroy(spool); |
---|
| 485 | |
---|
| 486 | return 0; |
---|
[fcb122d] | 487 | } |
---|
| 488 | |
---|
[e183628] | 489 | static int dbm_cache_delete(void *baton, gnutls_datum_t key) { |
---|
| 490 | apr_dbm_t *dbm; |
---|
| 491 | apr_datum_t dbmkey; |
---|
| 492 | mgs_handle_t *ctxt = baton; |
---|
| 493 | apr_status_t rv; |
---|
| 494 | |
---|
| 495 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0) |
---|
| 496 | return -1; |
---|
| 497 | |
---|
| 498 | rv = apr_dbm_open_ex(&dbm, db_type(ctxt->sc), |
---|
| 499 | ctxt->sc->cache_config, APR_DBM_RWCREATE, |
---|
| 500 | SSL_DBM_FILE_MODE, ctxt->c->pool); |
---|
| 501 | if (rv != APR_SUCCESS) { |
---|
| 502 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, |
---|
| 503 | ctxt->c->base_server, |
---|
| 504 | "[gnutls_cache] error opening cache '%s'", |
---|
| 505 | ctxt->sc->cache_config); |
---|
| 506 | return -1; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | rv = apr_dbm_delete(dbm, dbmkey); |
---|
| 510 | |
---|
| 511 | if (rv != APR_SUCCESS) { |
---|
| 512 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, |
---|
| 513 | ctxt->c->base_server, |
---|
| 514 | "[gnutls_cache] error deleting from cache '%s'", |
---|
| 515 | ctxt->sc->cache_config); |
---|
| 516 | apr_dbm_close(dbm); |
---|
| 517 | return -1; |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | apr_dbm_close(dbm); |
---|
| 521 | |
---|
| 522 | return 0; |
---|
[fcb122d] | 523 | } |
---|
| 524 | |
---|
[e02dd8c] | 525 | static int dbm_cache_post_config(apr_pool_t * p, server_rec * s, |
---|
[e183628] | 526 | mgs_srvconf_rec * sc) { |
---|
| 527 | apr_status_t rv; |
---|
| 528 | apr_dbm_t *dbm; |
---|
| 529 | const char *path1; |
---|
| 530 | const char *path2; |
---|
[fcb122d] | 531 | |
---|
[e183628] | 532 | rv = apr_dbm_open_ex(&dbm, db_type(sc), sc->cache_config, |
---|
| 533 | APR_DBM_RWCREATE, SSL_DBM_FILE_MODE, p); |
---|
[fcb122d] | 534 | |
---|
[e183628] | 535 | if (rv != APR_SUCCESS) { |
---|
| 536 | ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, |
---|
| 537 | "GnuTLS: Cannot create DBM Cache at `%s'", |
---|
| 538 | sc->cache_config); |
---|
| 539 | return rv; |
---|
| 540 | } |
---|
[fcb122d] | 541 | |
---|
[e183628] | 542 | apr_dbm_close(dbm); |
---|
[fcb122d] | 543 | |
---|
[e183628] | 544 | apr_dbm_get_usednames_ex(p, db_type(sc), sc->cache_config, &path1, |
---|
| 545 | &path2); |
---|
[fcb122d] | 546 | |
---|
[e183628] | 547 | /* The Following Code takes logic directly from mod_ssl's DBM Cache */ |
---|
[fcb122d] | 548 | #if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE) |
---|
[e183628] | 549 | /* Running as Root */ |
---|
| 550 | if (path1 && geteuid() == 0) { |
---|
[422f5b7] | 551 | if (0 != chown(path1, ap_unixd_config.user_id, -1)) |
---|
| 552 | ap_log_error(APLOG_MARK, APLOG_NOTICE, -1, s, |
---|
| 553 | "GnuTLS: could not chown cache path1 `%s' to uid %d (errno: %d)", |
---|
| 554 | path1, ap_unixd_config.user_id, errno); |
---|
[e183628] | 555 | if (path2 != NULL) { |
---|
[422f5b7] | 556 | if (0 != chown(path2, ap_unixd_config.user_id, -1)) |
---|
| 557 | ap_log_error(APLOG_MARK, APLOG_NOTICE, -1, s, |
---|
| 558 | "GnuTLS: could not chown cache path2 `%s' to uid %d (errno: %d)", |
---|
| 559 | path2, ap_unixd_config.user_id, errno); |
---|
[e183628] | 560 | } |
---|
| 561 | } |
---|
[fcb122d] | 562 | #endif |
---|
| 563 | |
---|
[e183628] | 564 | return rv; |
---|
[fcb122d] | 565 | } |
---|
| 566 | |
---|
[e02dd8c] | 567 | int mgs_cache_post_config(apr_pool_t * p, server_rec * s, |
---|
[e183628] | 568 | mgs_srvconf_rec * sc) { |
---|
[040387c] | 569 | |
---|
| 570 | /* if GnuTLSCache was never explicitly set: */ |
---|
| 571 | if (sc->cache_type == mgs_cache_unset) |
---|
| 572 | sc->cache_type = mgs_cache_none; |
---|
| 573 | /* if GnuTLSCacheTimeout was never explicitly set: */ |
---|
[671b64f] | 574 | if (sc->cache_timeout == -1) |
---|
[040387c] | 575 | sc->cache_timeout = apr_time_from_sec(300); |
---|
| 576 | |
---|
[e183628] | 577 | if (sc->cache_type == mgs_cache_dbm |
---|
| 578 | || sc->cache_type == mgs_cache_gdbm) { |
---|
| 579 | return dbm_cache_post_config(p, s, sc); |
---|
| 580 | } |
---|
| 581 | return 0; |
---|
[fcb122d] | 582 | } |
---|
| 583 | |
---|
[e765670] | 584 | #if HAVE_APR_MEMCACHE |
---|
| 585 | int mgs_cache_child_init(apr_pool_t * p, |
---|
| 586 | server_rec * s, |
---|
| 587 | mgs_srvconf_rec * sc) |
---|
| 588 | #else |
---|
| 589 | int mgs_cache_child_init(apr_pool_t * p __attribute__((unused)), |
---|
| 590 | server_rec * s __attribute__((unused)), |
---|
| 591 | mgs_srvconf_rec * sc) |
---|
| 592 | #endif |
---|
| 593 | { |
---|
[e183628] | 594 | if (sc->cache_type == mgs_cache_dbm |
---|
| 595 | || sc->cache_type == mgs_cache_gdbm) { |
---|
| 596 | return 0; |
---|
| 597 | } |
---|
[6e0bfd6] | 598 | #if HAVE_APR_MEMCACHE |
---|
[e183628] | 599 | else if (sc->cache_type == mgs_cache_memcache) { |
---|
| 600 | return mc_cache_child_init(p, s, sc); |
---|
| 601 | } |
---|
[6e0bfd6] | 602 | #endif |
---|
[e183628] | 603 | return 0; |
---|
[6e0bfd6] | 604 | } |
---|
| 605 | |
---|
[e02dd8c] | 606 | #include <assert.h> |
---|
[fcb122d] | 607 | |
---|
[e183628] | 608 | int mgs_cache_session_init(mgs_handle_t * ctxt) { |
---|
| 609 | if (ctxt->sc->cache_type == mgs_cache_dbm |
---|
| 610 | || ctxt->sc->cache_type == mgs_cache_gdbm) { |
---|
| 611 | gnutls_db_set_retrieve_function(ctxt->session, |
---|
| 612 | dbm_cache_fetch); |
---|
| 613 | gnutls_db_set_remove_function(ctxt->session, |
---|
| 614 | dbm_cache_delete); |
---|
| 615 | gnutls_db_set_store_function(ctxt->session, |
---|
| 616 | dbm_cache_store); |
---|
| 617 | gnutls_db_set_ptr(ctxt->session, ctxt); |
---|
| 618 | } |
---|
[6e0bfd6] | 619 | #if HAVE_APR_MEMCACHE |
---|
[e183628] | 620 | else if (ctxt->sc->cache_type == mgs_cache_memcache) { |
---|
| 621 | gnutls_db_set_retrieve_function(ctxt->session, |
---|
| 622 | mc_cache_fetch); |
---|
| 623 | gnutls_db_set_remove_function(ctxt->session, |
---|
| 624 | mc_cache_delete); |
---|
| 625 | gnutls_db_set_store_function(ctxt->session, |
---|
| 626 | mc_cache_store); |
---|
| 627 | gnutls_db_set_ptr(ctxt->session, ctxt); |
---|
| 628 | } |
---|
[6e0bfd6] | 629 | #endif |
---|
[42307a9] | 630 | |
---|
[e183628] | 631 | return 0; |
---|
[32f2e60] | 632 | } |
---|