summaryrefslogtreecommitdiffstatsabout
path: root/src/gnutls_cache.c
blob: 90f5a86980c64433b4a39dd2c8ef22eadeca00c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/**
 *  Copyright 2004-2005 Paul Querna
 *  Portions Copyright 2008 Nikos Mavrogiannopoulos
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

#include "mod_gnutls.h"

#if HAVE_APR_MEMCACHE
#include "apr_memcache.h"
#endif

#include "apr_dbm.h"

#include "ap_mpm.h"

#include <unistd.h>
#include <sys/types.h>

#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
#include "unixd.h"
#endif


#define MC_TAG "mod_gnutls:"
#define MC_TAG_LEN sizeof(MC_TAG)
#define STR_SESSION_LEN (GNUTLS_SESSION_ID_STRING_LEN + MC_TAG_LEN)

char *mgs_session_id2sz(unsigned char *id, int idlen,
                               char *str, int strsize)
{
    char *cp;
    int n;

    cp = str;
    for (n = 0; n < idlen && n < GNUTLS_MAX_SESSION_ID; n++) {
        apr_snprintf(cp, strsize - (cp-str), "%02X", id[n]);
        cp += 2;
    }
    *cp = '\0';
    return str;
}


/* Name the Session ID as:
 * server:port.SessionID
 * to disallow resuming sessions on different servers
 */
static int mgs_session_id2dbm(conn_rec* c, unsigned char *id, int idlen,
                               apr_datum_t* dbmkey)
{
char buf[STR_SESSION_LEN];
char *sz;

    sz = mgs_session_id2sz(id, idlen, buf, sizeof(buf));
    if (sz == NULL)
      return -1;

    dbmkey->dptr = apr_psprintf(c->pool, "%s:%d.%s", c->base_server->server_hostname, c->base_server->port, sz);
    dbmkey->dsize = strlen( dbmkey->dptr);

    return 0;
}

#define CTIME "%b %d %k:%M:%S %Y %Z"
char *mgs_time2sz(time_t in_time, char *str, int strsize)
{
    apr_time_exp_t vtm;
    apr_size_t ret_size;
    apr_time_t t;


    apr_time_ansi_put (&t, in_time);
    apr_time_exp_gmt (&vtm, t);
    apr_strftime(str, &ret_size, strsize-1, CTIME, &vtm);

    return str;
}

#if HAVE_APR_MEMCACHE
/* Name the Session ID as:
 * server:port.SessionID
 * to disallow resuming sessions on different servers
 */
static char* mgs_session_id2mc(conn_rec* c, unsigned char *id, int idlen)
{
char buf[STR_SESSION_LEN];
char *sz;

    sz = mgs_session_id2sz(id, idlen, buf, sizeof(buf));
    if (sz == NULL)
      return NULL;

    return apr_psprintf(c->pool, MC_TAG"%s:%d.%s", c->base_server->server_hostname, c->base_server->port, sz);
}

/**
 * GnuTLS Session Cache using libmemcached
 *
 */

/* The underlying apr_memcache system is thread safe... woohoo */
static apr_memcache_t* mc;

static int mc_cache_child_init(apr_pool_t *p, server_rec *s,
                                mgs_srvconf_rec *sc)
{
    apr_status_t rv = APR_SUCCESS;
    int thread_limit = 0;
    int nservers = 0;
    char* cache_config;
    char* split;
    char* tok;

    ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);

    /* Find all the servers in the first run to get a total count */
    cache_config = apr_pstrdup(p, sc->cache_config);
    split = apr_strtok(cache_config, " ", &tok);
    while (split) {
        nservers++;
        split = apr_strtok(NULL," ", &tok);
    }

    rv = apr_memcache_create(p, nservers, 0, &mc);
    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
                        "[gnutls_cache] Failed to create Memcache Object of '%d' size.",
                         nservers);
        return rv;
    }

    /* Now add each server to the memcache */
    cache_config = apr_pstrdup(p, sc->cache_config);
    split = apr_strtok(cache_config, " ", &tok);
    while (split) {
        apr_memcache_server_t* st;
        char* host_str;
        char* scope_id;
        apr_port_t port;

        rv = apr_parse_addr_port(&host_str, &scope_id, &port, split, p);
        if (rv != APR_SUCCESS) {
            ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
                         "[gnutls_cache] Failed to Parse Server: '%s'", split);
            return rv;
        }

        if (host_str == NULL) {
            ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
                         "[gnutls_cache] Failed to Parse Server, "
                         "no hostname specified: '%s'", split);
            return rv;
        }

        if (port == 0) {
            port = 11211; /* default port */
        }

        /* Should Max Conns be (thread_limit / nservers) ? */
        rv = apr_memcache_server_create(p,
                                        host_str, port,
                                        0,
                                        1,
                                        thread_limit,
                                        600,
                                        &st);
        if (rv != APR_SUCCESS) {
            ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
                         "[gnutls_cache] Failed to Create Server: %s:%d",
                         host_str, port);
            return rv;
        }

        rv = apr_memcache_add_server(mc, st);
        if (rv != APR_SUCCESS) {
            ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
                         "[gnutls_cache] Failed to Add Server: %s:%d",
                         host_str, port);
            return rv;
        }

        split = apr_strtok(NULL," ", &tok);
    }
    return rv;
}

static int mc_cache_store(void* baton, gnutls_datum_t key,
                          gnutls_datum_t data)
{
    apr_status_t rv = APR_SUCCESS;
    mgs_handle_t *ctxt = baton;
    char* strkey = NULL;
    apr_uint32_t timeout;

    strkey = mgs_session_id2mc(ctxt->c, key.data, key.size);
    if(!strkey)
        return -1;

    timeout = apr_time_sec(ctxt->sc->cache_timeout);

    rv = apr_memcache_set(mc, strkey, data.data, data.size, timeout, 0);

    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_CRIT, rv,
                     ctxt->c->base_server,
                     "[gnutls_cache] error setting key '%s' "
                     "with %d bytes of data", strkey, data.size);
        return -1;
    }

    return 0;
}

static gnutls_datum_t mc_cache_fetch(void* baton, gnutls_datum_t key)
{
    apr_status_t rv = APR_SUCCESS;
    mgs_handle_t *ctxt = baton;
    char* strkey = NULL;
    char* value;
    apr_size_t value_len;
    gnutls_datum_t data = { NULL, 0 };

    strkey = mgs_session_id2mc(ctxt->c, key.data, key.size);
    if (!strkey) {
        return data;
    }

    rv = apr_memcache_getp(mc, ctxt->c->pool, strkey,
                           &value, &value_len, NULL);

    if (rv != APR_SUCCESS) {
#if MOD_GNUTLS_DEBUG
        ap_log_error(APLOG_MARK, APLOG_DEBUG, rv,
                     ctxt->c->base_server,
                     "[gnutls_cache] error fetching key '%s' ",
                     strkey);
#endif
        data.size = 0;
        data.data = NULL;
        return data;
    }

    /* TODO: Eliminate this memcpy. gnutls-- */
    data.data = gnutls_malloc(value_len);
    if (data.data == NULL)
        return data;

    data.size = value_len;
    memcpy(data.data, value, value_len);

    return data;
}

static int mc_cache_delete(void* baton, gnutls_datum_t key)
{
    apr_status_t rv = APR_SUCCESS;
    mgs_handle_t *ctxt = baton;
    char* strkey = NULL;

    strkey = mgs_session_id2mc(ctxt->c, key.data, key.size);
    if(!strkey)
        return -1;

    rv = apr_memcache_delete(mc, strkey, 0);

    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_DEBUG, rv,
                     ctxt->c->base_server,
                     "[gnutls_cache] error deleting key '%s' ",
                      strkey);
        return -1;
    }

    return 0;
}

#endif /* have_apr_memcache */

#define SSL_DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )

static int dbm_cache_expire(mgs_handle_t *ctxt)
{
    apr_status_t rv;
    apr_dbm_t *dbm;
    apr_datum_t *keylist;
    apr_datum_t dbmkey;
    apr_datum_t dbmval;
    apr_time_t ex;
    apr_time_t dtime;
    apr_pool_t* spool;
    int i = 0;
    int keyidx = 0;
    int should_delete = 0;

    apr_pool_create(&spool, ctxt->c->pool);
    ex = apr_time_now();

    rv = apr_dbm_open(&dbm, ctxt->sc->cache_config, APR_DBM_READONLY,
                      SSL_DBM_FILE_MODE, spool);
    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_NOTICE, rv,
                     ctxt->c->base_server,
                     "[gnutls_cache] error opening cache searcher '%s'",
                     ctxt->sc->cache_config);
        return -1;
    }

#define KEYMAX 128

    keylist = apr_palloc(spool, sizeof(dbmkey)*KEYMAX);

    apr_dbm_firstkey(dbm, &dbmkey);
    while (dbmkey.dptr != NULL) {
        apr_dbm_fetch(dbm, dbmkey, &dbmval);
        if (dbmval.dptr != NULL) {
            if (dbmval.dsize >= sizeof(apr_time_t)) {
                memcpy(&dtime, dbmval.dptr, sizeof(apr_time_t));
                if (dtime < ex) {
                    should_delete = 1;
                }
            }
            else {
                should_delete = 1;
            }

            if (should_delete == 1) {
                should_delete = 0;
                keylist[keyidx].dptr = apr_palloc(spool, dbmkey.dsize) ;
                memcpy(keylist[keyidx].dptr, dbmkey.dptr, dbmkey.dsize);
                keylist[keyidx].dsize = dbmkey.dsize;
                keyidx++;
                if (keyidx == KEYMAX) {
                    break;
                }
            }
            apr_dbm_freedatum( dbm, dbmval);

        }
        apr_dbm_nextkey(dbm, &dbmkey);
    }
    apr_dbm_close(dbm);

    rv = apr_dbm_open(&dbm, ctxt->sc->cache_config,
                  APR_DBM_RWCREATE, SSL_DBM_FILE_MODE, spool);
    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_NOTICE, rv,
                 ctxt->c->base_server,
                 "[gnutls_cache] error opening cache writer '%s'",
                 ctxt->sc->cache_config);
        return -1;
    }

    for (i = 0; i < keyidx; i++) {
        apr_dbm_delete(dbm, keylist[i]);
    }

    apr_dbm_close(dbm);
    apr_pool_destroy(spool);

    return 0;
}

static gnutls_datum_t dbm_cache_fetch(void* baton, gnutls_datum_t key)
{
    gnutls_datum_t data = { NULL, 0 };
    apr_dbm_t *dbm;
    apr_datum_t dbmkey;
    apr_datum_t dbmval;
    mgs_handle_t *ctxt = baton;
    apr_status_t rv;

    if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0)
        return data;

    rv = apr_dbm_open(&dbm, ctxt->sc->cache_config,
	              APR_DBM_RWCREATE, SSL_DBM_FILE_MODE, ctxt->c->pool);
    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_NOTICE, rv,
                     ctxt->c->base_server,
                     "[gnutls_cache] error opening cache '%s'",
                     ctxt->sc->cache_config);
        return data;
    }

    rv = apr_dbm_fetch(dbm, dbmkey, &dbmval);

    if (rv != APR_SUCCESS) {
        apr_dbm_close(dbm);
        return data;
    }

    if (dbmval.dptr == NULL || dbmval.dsize <= sizeof(apr_time_t)) {
        apr_dbm_freedatum( dbm, dbmval);
        apr_dbm_close(dbm);
        return data;
    }

    data.size = dbmval.dsize - sizeof(apr_time_t);

    data.data = gnutls_malloc(data.size);
    if (data.data == NULL) {
        apr_dbm_freedatum( dbm, dbmval);
        apr_dbm_close(dbm);
        return data;
    }

    memcpy(data.data, dbmval.dptr+sizeof(apr_time_t), data.size);

    apr_dbm_freedatum( dbm, dbmval);
    apr_dbm_close(dbm);

    return data;
}

static int dbm_cache_store(void* baton, gnutls_datum_t key,
                          gnutls_datum_t data)
{
    apr_dbm_t *dbm;
    apr_datum_t dbmkey;
    apr_datum_t dbmval;
    mgs_handle_t *ctxt = baton;
    apr_status_t rv;
    apr_time_t expiry;

    if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0)
        return -1;

    /* create DBM value */
    dbmval.dsize = data.size + sizeof(apr_time_t);
    dbmval.dptr  = (char *)malloc(dbmval.dsize);

    expiry = apr_time_now() + ctxt->sc->cache_timeout;

    memcpy((char *)dbmval.dptr, &expiry, sizeof(apr_time_t));
    memcpy((char *)dbmval.dptr+sizeof(apr_time_t),
           data.data, data.size);

    /* we expire dbm only on every store
     */
    dbm_cache_expire(ctxt);

    rv = apr_dbm_open(&dbm, ctxt->sc->cache_config,
	              APR_DBM_RWCREATE, SSL_DBM_FILE_MODE, ctxt->c->pool);
    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_NOTICE, rv,
                     ctxt->c->base_server,
                     "[gnutls_cache] error opening cache '%s'",
                     ctxt->sc->cache_config);
        free(dbmval.dptr);
        return -1;
    }

    rv = apr_dbm_store(dbm, dbmkey, dbmval);

    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_NOTICE, rv,
                     ctxt->c->base_server,
                     "[gnutls_cache] error storing in cache '%s'",
                     ctxt->sc->cache_config);
        apr_dbm_close(dbm);
        free(dbmval.dptr);
        return -1;
    }

    apr_dbm_close(dbm);

    free(dbmval.dptr);

    return 0;
}

static int dbm_cache_delete(void* baton, gnutls_datum_t key)
{
    apr_dbm_t *dbm;
    apr_datum_t dbmkey;
    mgs_handle_t *ctxt = baton;
    apr_status_t rv;

    if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0)
        return -1;

    rv = apr_dbm_open(&dbm, ctxt->sc->cache_config,
	              APR_DBM_RWCREATE, SSL_DBM_FILE_MODE, ctxt->c->pool);
    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_NOTICE, rv,
                     ctxt->c->base_server,
                     "[gnutls_cache] error opening cache '%s'",
                     ctxt->sc->cache_config);
        return -1;
    }

    rv = apr_dbm_delete(dbm, dbmkey);

    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_NOTICE, rv,
                     ctxt->c->base_server,
                     "[gnutls_cache] error deleting from cache '%s'",
                     ctxt->sc->cache_config);
        apr_dbm_close(dbm);
        return -1;
    }

    apr_dbm_close(dbm);

    return 0;
}

static int dbm_cache_post_config(apr_pool_t *p, server_rec *s,
                                mgs_srvconf_rec *sc)
{
    apr_status_t rv;
    apr_dbm_t *dbm;
    const char* path1;
    const char* path2;

    rv = apr_dbm_open(&dbm, sc->cache_config, APR_DBM_RWCREATE,
                      SSL_DBM_FILE_MODE, p);

    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
                     "GnuTLS: Cannot create DBM Cache at `%s'",
                     sc->cache_config);
        return rv;
    }

    apr_dbm_close(dbm);

    apr_dbm_get_usednames(p, sc->cache_config, &path1, &path2);

    /* The Following Code takes logic directly from mod_ssl's DBM Cache */
#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
    /* Running as Root */
    if (geteuid() == 0)  {
        chown(path1, unixd_config.user_id, -1);
        if (path2 != NULL) {
            chown(path2, unixd_config.user_id, -1);
        }
    }
#endif

    return rv;
}

int mgs_cache_post_config(apr_pool_t *p, server_rec *s,
                                 mgs_srvconf_rec *sc)
{
    if (sc->cache_type == mgs_cache_dbm) {
        return dbm_cache_post_config(p, s, sc);
    }
    return 0;
}

int mgs_cache_child_init(apr_pool_t *p, server_rec *s,
                                mgs_srvconf_rec *sc)
{
    if (sc->cache_type == mgs_cache_dbm) {
        return 0;
    }
#if HAVE_APR_MEMCACHE
    else if (sc->cache_type == mgs_cache_memcache) {
        return mc_cache_child_init(p, s, sc);
    }
#endif
    return 0;
}

 #include <assert.h>

int mgs_cache_session_init(mgs_handle_t *ctxt)
{
    if (ctxt->sc->cache_type == mgs_cache_dbm) {
        gnutls_db_set_retrieve_function(ctxt->session, dbm_cache_fetch);
        gnutls_db_set_remove_function(ctxt->session, dbm_cache_delete);
        gnutls_db_set_store_function(ctxt->session, dbm_cache_store);
        gnutls_db_set_ptr(ctxt->session, ctxt);
    }
#if HAVE_APR_MEMCACHE
    else if (ctxt->sc->cache_type == mgs_cache_memcache) {
        gnutls_db_set_retrieve_function(ctxt->session, mc_cache_fetch);
        gnutls_db_set_remove_function(ctxt->session, mc_cache_delete);
        gnutls_db_set_store_function(ctxt->session, mc_cache_store);
        gnutls_db_set_ptr(ctxt->session, ctxt);
    }
#endif

    return 0;
}