[de80d66] | 1 | /** |
---|
| 2 | * Copyright 2004-2005 Paul Querna |
---|
[8df5b25] | 3 | * Portions Copyright 2008 Nikos Mavrogiannopoulos |
---|
[de80d66] | 4 | * |
---|
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 6 | * you may not use this file except in compliance with the License. |
---|
| 7 | * You may obtain a copy of the License at |
---|
| 8 | * |
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | * |
---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | * See the License for the specific language governing permissions and |
---|
| 15 | * limitations under the License. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include "mod_gnutls.h" |
---|
| 20 | |
---|
| 21 | #if HAVE_APR_MEMCACHE |
---|
| 22 | #include "apr_memcache.h" |
---|
| 23 | #endif |
---|
| 24 | |
---|
| 25 | #include "apr_dbm.h" |
---|
| 26 | |
---|
| 27 | #include "ap_mpm.h" |
---|
| 28 | |
---|
| 29 | #include <unistd.h> |
---|
| 30 | #include <sys/types.h> |
---|
| 31 | |
---|
| 32 | #if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE) |
---|
| 33 | #include "unixd.h" |
---|
| 34 | #endif |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | #define MC_TAG "mod_gnutls:" |
---|
[2a2272d] | 38 | #define MC_TAG_LEN sizeof(MC_TAG) |
---|
[de80d66] | 39 | #define STR_SESSION_LEN (GNUTLS_SESSION_ID_STRING_LEN + MC_TAG_LEN) |
---|
| 40 | |
---|
[66b608e] | 41 | #if MODULE_MAGIC_NUMBER_MAJOR < 20081201 |
---|
| 42 | #define ap_unixd_config unixd_config |
---|
| 43 | #endif |
---|
| 44 | |
---|
[2a2272d] | 45 | char *mgs_session_id2sz(unsigned char *id, int idlen, |
---|
[de80d66] | 46 | char *str, int strsize) |
---|
| 47 | { |
---|
| 48 | char *cp; |
---|
| 49 | int n; |
---|
[2a2272d] | 50 | |
---|
| 51 | cp = str; |
---|
[de80d66] | 52 | for (n = 0; n < idlen && n < GNUTLS_MAX_SESSION_ID; n++) { |
---|
| 53 | apr_snprintf(cp, strsize - (cp-str), "%02X", id[n]); |
---|
| 54 | cp += 2; |
---|
| 55 | } |
---|
| 56 | *cp = '\0'; |
---|
| 57 | return str; |
---|
| 58 | } |
---|
[2a2272d] | 59 | |
---|
| 60 | |
---|
| 61 | /* Name the Session ID as: |
---|
| 62 | * server:port.SessionID |
---|
| 63 | * to disallow resuming sessions on different servers |
---|
| 64 | */ |
---|
| 65 | static int mgs_session_id2dbm(conn_rec* c, unsigned char *id, int idlen, |
---|
| 66 | apr_datum_t* dbmkey) |
---|
| 67 | { |
---|
| 68 | char buf[STR_SESSION_LEN]; |
---|
| 69 | char *sz; |
---|
| 70 | |
---|
| 71 | sz = mgs_session_id2sz(id, idlen, buf, sizeof(buf)); |
---|
| 72 | if (sz == NULL) |
---|
| 73 | return -1; |
---|
| 74 | |
---|
| 75 | dbmkey->dptr = apr_psprintf(c->pool, "%s:%d.%s", c->base_server->server_hostname, c->base_server->port, sz); |
---|
| 76 | dbmkey->dsize = strlen( dbmkey->dptr); |
---|
| 77 | |
---|
| 78 | return 0; |
---|
| 79 | } |
---|
[70c2d86] | 80 | |
---|
| 81 | #define CTIME "%b %d %k:%M:%S %Y %Z" |
---|
| 82 | char *mgs_time2sz(time_t in_time, char *str, int strsize) |
---|
| 83 | { |
---|
| 84 | apr_time_exp_t vtm; |
---|
| 85 | apr_size_t ret_size; |
---|
| 86 | apr_time_t t; |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | apr_time_ansi_put (&t, in_time); |
---|
| 90 | apr_time_exp_gmt (&vtm, t); |
---|
| 91 | apr_strftime(str, &ret_size, strsize-1, CTIME, &vtm); |
---|
| 92 | |
---|
| 93 | return str; |
---|
| 94 | } |
---|
[de80d66] | 95 | |
---|
[2a2272d] | 96 | #if HAVE_APR_MEMCACHE |
---|
| 97 | /* Name the Session ID as: |
---|
| 98 | * server:port.SessionID |
---|
| 99 | * to disallow resuming sessions on different servers |
---|
| 100 | */ |
---|
| 101 | static char* mgs_session_id2mc(conn_rec* c, unsigned char *id, int idlen) |
---|
[de80d66] | 102 | { |
---|
[2a2272d] | 103 | char buf[STR_SESSION_LEN]; |
---|
| 104 | char *sz; |
---|
[de80d66] | 105 | |
---|
[2a2272d] | 106 | sz = mgs_session_id2sz(id, idlen, buf, sizeof(buf)); |
---|
| 107 | if (sz == NULL) |
---|
| 108 | return NULL; |
---|
| 109 | |
---|
| 110 | return apr_psprintf(c->pool, MC_TAG"%s:%d.%s", c->base_server->server_hostname, c->base_server->port, sz); |
---|
[de80d66] | 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * GnuTLS Session Cache using libmemcached |
---|
| 115 | * |
---|
| 116 | */ |
---|
| 117 | |
---|
| 118 | /* The underlying apr_memcache system is thread safe... woohoo */ |
---|
| 119 | static apr_memcache_t* mc; |
---|
| 120 | |
---|
[3e94bd3] | 121 | static int mc_cache_child_init(apr_pool_t *p, server_rec *s, |
---|
| 122 | mgs_srvconf_rec *sc) |
---|
[de80d66] | 123 | { |
---|
| 124 | apr_status_t rv = APR_SUCCESS; |
---|
| 125 | int thread_limit = 0; |
---|
| 126 | int nservers = 0; |
---|
| 127 | char* cache_config; |
---|
| 128 | char* split; |
---|
| 129 | char* tok; |
---|
| 130 | |
---|
| 131 | ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit); |
---|
| 132 | |
---|
| 133 | /* Find all the servers in the first run to get a total count */ |
---|
| 134 | cache_config = apr_pstrdup(p, sc->cache_config); |
---|
| 135 | split = apr_strtok(cache_config, " ", &tok); |
---|
| 136 | while (split) { |
---|
| 137 | nservers++; |
---|
| 138 | split = apr_strtok(NULL," ", &tok); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | rv = apr_memcache_create(p, nservers, 0, &mc); |
---|
| 142 | if (rv != APR_SUCCESS) { |
---|
| 143 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
| 144 | "[gnutls_cache] Failed to create Memcache Object of '%d' size.", |
---|
| 145 | nservers); |
---|
| 146 | return rv; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /* Now add each server to the memcache */ |
---|
| 150 | cache_config = apr_pstrdup(p, sc->cache_config); |
---|
| 151 | split = apr_strtok(cache_config, " ", &tok); |
---|
| 152 | while (split) { |
---|
| 153 | apr_memcache_server_t* st; |
---|
| 154 | char* host_str; |
---|
| 155 | char* scope_id; |
---|
| 156 | apr_port_t port; |
---|
| 157 | |
---|
| 158 | rv = apr_parse_addr_port(&host_str, &scope_id, &port, split, p); |
---|
| 159 | if (rv != APR_SUCCESS) { |
---|
| 160 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
| 161 | "[gnutls_cache] Failed to Parse Server: '%s'", 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, |
---|
| 181 | thread_limit, |
---|
| 182 | 600, |
---|
| 183 | &st); |
---|
| 184 | if (rv != APR_SUCCESS) { |
---|
| 185 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
| 186 | "[gnutls_cache] Failed to Create Server: %s:%d", |
---|
| 187 | host_str, port); |
---|
| 188 | return rv; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | rv = apr_memcache_add_server(mc, st); |
---|
| 192 | if (rv != APR_SUCCESS) { |
---|
| 193 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
---|
| 194 | "[gnutls_cache] Failed to Add Server: %s:%d", |
---|
| 195 | host_str, port); |
---|
| 196 | return rv; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | split = apr_strtok(NULL," ", &tok); |
---|
| 200 | } |
---|
| 201 | return rv; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | static int mc_cache_store(void* baton, gnutls_datum_t key, |
---|
| 205 | gnutls_datum_t data) |
---|
| 206 | { |
---|
| 207 | apr_status_t rv = APR_SUCCESS; |
---|
[3e94bd3] | 208 | mgs_handle_t *ctxt = baton; |
---|
[de80d66] | 209 | char* strkey = NULL; |
---|
| 210 | apr_uint32_t timeout; |
---|
| 211 | |
---|
[2a2272d] | 212 | strkey = mgs_session_id2mc(ctxt->c, key.data, key.size); |
---|
[de80d66] | 213 | if(!strkey) |
---|
| 214 | return -1; |
---|
| 215 | |
---|
| 216 | timeout = apr_time_sec(ctxt->sc->cache_timeout); |
---|
| 217 | |
---|
| 218 | rv = apr_memcache_set(mc, strkey, data.data, data.size, timeout, 0); |
---|
| 219 | |
---|
| 220 | if (rv != APR_SUCCESS) { |
---|
| 221 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, |
---|
| 222 | ctxt->c->base_server, |
---|
| 223 | "[gnutls_cache] error setting key '%s' " |
---|
| 224 | "with %d bytes of data", strkey, data.size); |
---|
| 225 | return -1; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | return 0; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | static gnutls_datum_t mc_cache_fetch(void* baton, gnutls_datum_t key) |
---|
| 232 | { |
---|
| 233 | apr_status_t rv = APR_SUCCESS; |
---|
[3e94bd3] | 234 | mgs_handle_t *ctxt = baton; |
---|
[de80d66] | 235 | char* strkey = NULL; |
---|
| 236 | char* value; |
---|
| 237 | apr_size_t value_len; |
---|
| 238 | gnutls_datum_t data = { NULL, 0 }; |
---|
| 239 | |
---|
[2a2272d] | 240 | strkey = mgs_session_id2mc(ctxt->c, key.data, key.size); |
---|
[de80d66] | 241 | if (!strkey) { |
---|
| 242 | return data; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | rv = apr_memcache_getp(mc, ctxt->c->pool, strkey, |
---|
| 246 | &value, &value_len, NULL); |
---|
| 247 | |
---|
| 248 | if (rv != APR_SUCCESS) { |
---|
[3e94bd3] | 249 | #if MOD_GNUTLS_DEBUG |
---|
[de80d66] | 250 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, |
---|
| 251 | ctxt->c->base_server, |
---|
| 252 | "[gnutls_cache] error fetching key '%s' ", |
---|
| 253 | strkey); |
---|
[3e94bd3] | 254 | #endif |
---|
[de80d66] | 255 | data.size = 0; |
---|
| 256 | data.data = NULL; |
---|
| 257 | return data; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | /* TODO: Eliminate this memcpy. gnutls-- */ |
---|
| 261 | data.data = gnutls_malloc(value_len); |
---|
| 262 | if (data.data == NULL) |
---|
| 263 | return data; |
---|
| 264 | |
---|
| 265 | data.size = value_len; |
---|
| 266 | memcpy(data.data, value, value_len); |
---|
| 267 | |
---|
| 268 | return data; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | static int mc_cache_delete(void* baton, gnutls_datum_t key) |
---|
| 272 | { |
---|
| 273 | apr_status_t rv = APR_SUCCESS; |
---|
[3e94bd3] | 274 | mgs_handle_t *ctxt = baton; |
---|
[de80d66] | 275 | char* strkey = NULL; |
---|
| 276 | |
---|
[2a2272d] | 277 | strkey = mgs_session_id2mc(ctxt->c, key.data, key.size); |
---|
[de80d66] | 278 | if(!strkey) |
---|
| 279 | return -1; |
---|
| 280 | |
---|
| 281 | rv = apr_memcache_delete(mc, strkey, 0); |
---|
| 282 | |
---|
| 283 | if (rv != APR_SUCCESS) { |
---|
| 284 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, |
---|
| 285 | ctxt->c->base_server, |
---|
| 286 | "[gnutls_cache] error deleting key '%s' ", |
---|
| 287 | strkey); |
---|
| 288 | return -1; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | return 0; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | #endif /* have_apr_memcache */ |
---|
| 295 | |
---|
| 296 | #define SSL_DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD ) |
---|
| 297 | |
---|
[3e94bd3] | 298 | static int dbm_cache_expire(mgs_handle_t *ctxt) |
---|
[de80d66] | 299 | { |
---|
| 300 | apr_status_t rv; |
---|
| 301 | apr_dbm_t *dbm; |
---|
| 302 | apr_datum_t *keylist; |
---|
| 303 | apr_datum_t dbmkey; |
---|
| 304 | apr_datum_t dbmval; |
---|
| 305 | apr_time_t ex; |
---|
| 306 | apr_time_t dtime; |
---|
| 307 | apr_pool_t* spool; |
---|
| 308 | int i = 0; |
---|
| 309 | int keyidx = 0; |
---|
| 310 | int should_delete = 0; |
---|
| 311 | |
---|
| 312 | apr_pool_create(&spool, ctxt->c->pool); |
---|
| 313 | ex = apr_time_now(); |
---|
| 314 | |
---|
| 315 | rv = apr_dbm_open(&dbm, ctxt->sc->cache_config, APR_DBM_READONLY, |
---|
| 316 | SSL_DBM_FILE_MODE, spool); |
---|
| 317 | if (rv != APR_SUCCESS) { |
---|
| 318 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, |
---|
| 319 | ctxt->c->base_server, |
---|
| 320 | "[gnutls_cache] error opening cache searcher '%s'", |
---|
| 321 | ctxt->sc->cache_config); |
---|
| 322 | return -1; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | #define KEYMAX 128 |
---|
| 326 | |
---|
| 327 | keylist = apr_palloc(spool, sizeof(dbmkey)*KEYMAX); |
---|
| 328 | |
---|
| 329 | apr_dbm_firstkey(dbm, &dbmkey); |
---|
| 330 | while (dbmkey.dptr != NULL) { |
---|
| 331 | apr_dbm_fetch(dbm, dbmkey, &dbmval); |
---|
| 332 | if (dbmval.dptr != NULL) { |
---|
| 333 | if (dbmval.dsize >= sizeof(apr_time_t)) { |
---|
| 334 | memcpy(&dtime, dbmval.dptr, sizeof(apr_time_t)); |
---|
| 335 | if (dtime < ex) { |
---|
| 336 | should_delete = 1; |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | else { |
---|
| 340 | should_delete = 1; |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | if (should_delete == 1) { |
---|
| 344 | should_delete = 0; |
---|
| 345 | keylist[keyidx].dptr = apr_palloc(spool, dbmkey.dsize) ; |
---|
| 346 | memcpy(keylist[keyidx].dptr, dbmkey.dptr, dbmkey.dsize); |
---|
| 347 | keylist[keyidx].dsize = dbmkey.dsize; |
---|
| 348 | keyidx++; |
---|
| 349 | if (keyidx == KEYMAX) { |
---|
| 350 | break; |
---|
| 351 | } |
---|
| 352 | } |
---|
[8df5b25] | 353 | apr_dbm_freedatum( dbm, dbmval); |
---|
[de80d66] | 354 | |
---|
| 355 | } |
---|
| 356 | apr_dbm_nextkey(dbm, &dbmkey); |
---|
| 357 | } |
---|
| 358 | apr_dbm_close(dbm); |
---|
| 359 | |
---|
| 360 | rv = apr_dbm_open(&dbm, ctxt->sc->cache_config, |
---|
| 361 | APR_DBM_RWCREATE, SSL_DBM_FILE_MODE, spool); |
---|
| 362 | if (rv != APR_SUCCESS) { |
---|
| 363 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, |
---|
| 364 | ctxt->c->base_server, |
---|
| 365 | "[gnutls_cache] error opening cache writer '%s'", |
---|
| 366 | ctxt->sc->cache_config); |
---|
| 367 | return -1; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | for (i = 0; i < keyidx; i++) { |
---|
| 371 | apr_dbm_delete(dbm, keylist[i]); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | apr_dbm_close(dbm); |
---|
| 375 | apr_pool_destroy(spool); |
---|
| 376 | |
---|
| 377 | return 0; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | static gnutls_datum_t dbm_cache_fetch(void* baton, gnutls_datum_t key) |
---|
| 381 | { |
---|
| 382 | gnutls_datum_t data = { NULL, 0 }; |
---|
| 383 | apr_dbm_t *dbm; |
---|
| 384 | apr_datum_t dbmkey; |
---|
| 385 | apr_datum_t dbmval; |
---|
[3e94bd3] | 386 | mgs_handle_t *ctxt = baton; |
---|
[de80d66] | 387 | apr_status_t rv; |
---|
| 388 | |
---|
[2a2272d] | 389 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0) |
---|
| 390 | return data; |
---|
[de80d66] | 391 | |
---|
| 392 | rv = apr_dbm_open(&dbm, ctxt->sc->cache_config, |
---|
| 393 | APR_DBM_RWCREATE, SSL_DBM_FILE_MODE, ctxt->c->pool); |
---|
| 394 | if (rv != APR_SUCCESS) { |
---|
| 395 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, |
---|
| 396 | ctxt->c->base_server, |
---|
| 397 | "[gnutls_cache] error opening cache '%s'", |
---|
| 398 | ctxt->sc->cache_config); |
---|
| 399 | return data; |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | rv = apr_dbm_fetch(dbm, dbmkey, &dbmval); |
---|
| 403 | |
---|
| 404 | if (rv != APR_SUCCESS) { |
---|
| 405 | apr_dbm_close(dbm); |
---|
| 406 | return data; |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | if (dbmval.dptr == NULL || dbmval.dsize <= sizeof(apr_time_t)) { |
---|
[8df5b25] | 410 | apr_dbm_freedatum( dbm, dbmval); |
---|
[de80d66] | 411 | apr_dbm_close(dbm); |
---|
| 412 | return data; |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | data.size = dbmval.dsize - sizeof(apr_time_t); |
---|
| 416 | |
---|
| 417 | data.data = gnutls_malloc(data.size); |
---|
| 418 | if (data.data == NULL) { |
---|
[8df5b25] | 419 | apr_dbm_freedatum( dbm, dbmval); |
---|
| 420 | apr_dbm_close(dbm); |
---|
[de80d66] | 421 | return data; |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | memcpy(data.data, dbmval.dptr+sizeof(apr_time_t), data.size); |
---|
| 425 | |
---|
[8df5b25] | 426 | apr_dbm_freedatum( dbm, dbmval); |
---|
| 427 | apr_dbm_close(dbm); |
---|
| 428 | |
---|
[de80d66] | 429 | return data; |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | static int dbm_cache_store(void* baton, gnutls_datum_t key, |
---|
| 433 | gnutls_datum_t data) |
---|
| 434 | { |
---|
| 435 | apr_dbm_t *dbm; |
---|
| 436 | apr_datum_t dbmkey; |
---|
| 437 | apr_datum_t dbmval; |
---|
[3e94bd3] | 438 | mgs_handle_t *ctxt = baton; |
---|
[de80d66] | 439 | apr_status_t rv; |
---|
| 440 | apr_time_t expiry; |
---|
[2a2272d] | 441 | |
---|
| 442 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0) |
---|
| 443 | return -1; |
---|
[de80d66] | 444 | |
---|
| 445 | /* create DBM value */ |
---|
| 446 | dbmval.dsize = data.size + sizeof(apr_time_t); |
---|
| 447 | dbmval.dptr = (char *)malloc(dbmval.dsize); |
---|
| 448 | |
---|
| 449 | expiry = apr_time_now() + ctxt->sc->cache_timeout; |
---|
| 450 | |
---|
| 451 | memcpy((char *)dbmval.dptr, &expiry, sizeof(apr_time_t)); |
---|
| 452 | memcpy((char *)dbmval.dptr+sizeof(apr_time_t), |
---|
| 453 | data.data, data.size); |
---|
| 454 | |
---|
[70c2d86] | 455 | /* we expire dbm only on every store |
---|
| 456 | */ |
---|
[de80d66] | 457 | dbm_cache_expire(ctxt); |
---|
| 458 | |
---|
| 459 | rv = apr_dbm_open(&dbm, ctxt->sc->cache_config, |
---|
| 460 | APR_DBM_RWCREATE, 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 | free(dbmval.dptr); |
---|
| 467 | return -1; |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | rv = apr_dbm_store(dbm, dbmkey, dbmval); |
---|
| 471 | |
---|
| 472 | if (rv != APR_SUCCESS) { |
---|
[8df5b25] | 473 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, |
---|
[de80d66] | 474 | ctxt->c->base_server, |
---|
| 475 | "[gnutls_cache] error storing in cache '%s'", |
---|
| 476 | ctxt->sc->cache_config); |
---|
| 477 | apr_dbm_close(dbm); |
---|
| 478 | free(dbmval.dptr); |
---|
| 479 | return -1; |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | apr_dbm_close(dbm); |
---|
| 483 | |
---|
| 484 | free(dbmval.dptr); |
---|
| 485 | |
---|
| 486 | return 0; |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | static int dbm_cache_delete(void* baton, gnutls_datum_t key) |
---|
| 490 | { |
---|
| 491 | apr_dbm_t *dbm; |
---|
| 492 | apr_datum_t dbmkey; |
---|
[3e94bd3] | 493 | mgs_handle_t *ctxt = baton; |
---|
[de80d66] | 494 | apr_status_t rv; |
---|
[2a2272d] | 495 | |
---|
| 496 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0) |
---|
| 497 | return -1; |
---|
[de80d66] | 498 | |
---|
| 499 | rv = apr_dbm_open(&dbm, ctxt->sc->cache_config, |
---|
| 500 | APR_DBM_RWCREATE, 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; |
---|
| 523 | } |
---|
| 524 | |
---|
| 525 | static int dbm_cache_post_config(apr_pool_t *p, server_rec *s, |
---|
[3e94bd3] | 526 | mgs_srvconf_rec *sc) |
---|
[de80d66] | 527 | { |
---|
| 528 | apr_status_t rv; |
---|
| 529 | apr_dbm_t *dbm; |
---|
| 530 | const char* path1; |
---|
| 531 | const char* path2; |
---|
| 532 | |
---|
| 533 | rv = apr_dbm_open(&dbm, sc->cache_config, APR_DBM_RWCREATE, |
---|
| 534 | SSL_DBM_FILE_MODE, p); |
---|
| 535 | |
---|
| 536 | if (rv != APR_SUCCESS) { |
---|
| 537 | ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, |
---|
| 538 | "GnuTLS: Cannot create DBM Cache at `%s'", |
---|
| 539 | sc->cache_config); |
---|
| 540 | return rv; |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | apr_dbm_close(dbm); |
---|
| 544 | |
---|
| 545 | apr_dbm_get_usednames(p, sc->cache_config, &path1, &path2); |
---|
| 546 | |
---|
| 547 | /* The Following Code takes logic directly from mod_ssl's DBM Cache */ |
---|
| 548 | #if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE) |
---|
| 549 | /* Running as Root */ |
---|
| 550 | if (geteuid() == 0) { |
---|
[66b608e] | 551 | chown(path1, ap_unixd_config.user_id, -1); |
---|
[de80d66] | 552 | if (path2 != NULL) { |
---|
[66b608e] | 553 | chown(path2, ap_unixd_config.user_id, -1); |
---|
[de80d66] | 554 | } |
---|
| 555 | } |
---|
| 556 | #endif |
---|
| 557 | |
---|
| 558 | return rv; |
---|
| 559 | } |
---|
| 560 | |
---|
[3e94bd3] | 561 | int mgs_cache_post_config(apr_pool_t *p, server_rec *s, |
---|
| 562 | mgs_srvconf_rec *sc) |
---|
[de80d66] | 563 | { |
---|
[3e94bd3] | 564 | if (sc->cache_type == mgs_cache_dbm) { |
---|
[de80d66] | 565 | return dbm_cache_post_config(p, s, sc); |
---|
| 566 | } |
---|
| 567 | return 0; |
---|
| 568 | } |
---|
| 569 | |
---|
[3e94bd3] | 570 | int mgs_cache_child_init(apr_pool_t *p, server_rec *s, |
---|
| 571 | mgs_srvconf_rec *sc) |
---|
[de80d66] | 572 | { |
---|
[3e94bd3] | 573 | if (sc->cache_type == mgs_cache_dbm) { |
---|
[de80d66] | 574 | return 0; |
---|
| 575 | } |
---|
| 576 | #if HAVE_APR_MEMCACHE |
---|
[3e94bd3] | 577 | else if (sc->cache_type == mgs_cache_memcache) { |
---|
[de80d66] | 578 | return mc_cache_child_init(p, s, sc); |
---|
| 579 | } |
---|
| 580 | #endif |
---|
| 581 | return 0; |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | #include <assert.h> |
---|
| 585 | |
---|
[3e94bd3] | 586 | int mgs_cache_session_init(mgs_handle_t *ctxt) |
---|
[de80d66] | 587 | { |
---|
[3e94bd3] | 588 | if (ctxt->sc->cache_type == mgs_cache_dbm) { |
---|
[de80d66] | 589 | gnutls_db_set_retrieve_function(ctxt->session, dbm_cache_fetch); |
---|
| 590 | gnutls_db_set_remove_function(ctxt->session, dbm_cache_delete); |
---|
| 591 | gnutls_db_set_store_function(ctxt->session, dbm_cache_store); |
---|
| 592 | gnutls_db_set_ptr(ctxt->session, ctxt); |
---|
| 593 | } |
---|
| 594 | #if HAVE_APR_MEMCACHE |
---|
[3e94bd3] | 595 | else if (ctxt->sc->cache_type == mgs_cache_memcache) { |
---|
[de80d66] | 596 | gnutls_db_set_retrieve_function(ctxt->session, mc_cache_fetch); |
---|
| 597 | gnutls_db_set_remove_function(ctxt->session, mc_cache_delete); |
---|
| 598 | gnutls_db_set_store_function(ctxt->session, mc_cache_store); |
---|
| 599 | gnutls_db_set_ptr(ctxt->session, ctxt); |
---|
| 600 | } |
---|
| 601 | #endif |
---|
| 602 | |
---|
| 603 | return 0; |
---|
| 604 | } |
---|