summaryrefslogtreecommitdiffstatsabout
path: root/src/gnutls_cache.c
blob: 868568b25ec8017cf39537e0ff7cf7eb93b48e5b (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
/**
 *  Copyright 2004-2005 Paul Querna
 *
 *  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 GNUTLS_SESSION_ID_STRING_LEN \
    ((GNUTLS_MAX_SESSION_ID + 1) * 2)
#define MC_TAG "mod_gnutls:"
#define MC_TAG_LEN \
    (sizeof(MC_TAG))
#define STR_SESSION_LEN (GNUTLS_SESSION_ID_STRING_LEN + MC_TAG_LEN)

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

    cp = apr_cpystrn(str, MC_TAG, MC_TAG_LEN);
    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;
}


#if HAVE_APR_MEMCACHE

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

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

int mc_cache_child_init(apr_pool_t *p, server_rec *s,
                                mod_gnutls_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;
    mod_gnutls_handle_t *ctxt = baton;
    char buf[STR_SESSION_LEN];
    char* strkey = NULL;
    apr_uint32_t timeout;

    strkey = gnutls_session_id2sz(key.data, key.size, buf, sizeof(buf));
    if(!strkey)
        return -1;

    timeout = 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;
    mod_gnutls_handle_t *ctxt = baton;
    char buf[STR_SESSION_LEN];
    char* strkey = NULL;
    char* value;
    apr_size_t value_len;
    gnutls_datum_t data = { NULL, 0 };

    strkey = gnutls_session_id2sz(key.data, key.size, buf, sizeof(buf));
    if(!strkey) {
        return data;
    }

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

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

        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;
    mod_gnutls_handle_t *ctxt = baton;
    char buf[STR_SESSION_LEN];
    char* strkey = NULL;

    strkey = gnutls_session_id2sz(key.data, key.size, buf, sizeof(buf));
    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(mod_gnutls_handle_t *ctxt)
{
    apr_status_t rv;
    apr_dbm_t *dbm;

    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;
    }
    apr_dbm_close(dbm);

    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;
    mod_gnutls_handle_t *ctxt = baton;
    apr_status_t rv;

    dbmkey.dptr  = key.data;
    dbmkey.dsize = key.size;

    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_close(dbm);
        return data;
    }

    data.data = gnutls_malloc(dbmval.dsize - sizeof(apr_time_t));
    if (data.data == NULL)
        return data;

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

    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;
    mod_gnutls_handle_t *ctxt = baton;
    apr_status_t rv;
    apr_time_t timeout;

    dbmkey.dptr  = (char *)key.data;
    dbmkey.dsize = key.size;

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

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

    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;
    mod_gnutls_handle_t *ctxt = baton;
    apr_status_t rv;

    dbmkey.dptr  = (char *)key.data;
    dbmkey.dsize = key.size;

    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 storing in cache '%s'",
                     ctxt->sc->cache_config);
        apr_dbm_close(dbm);
        return -1;
    }

    apr_dbm_close(dbm);

    return 0;
}

static int dbm_cache_child_init(apr_pool_t *p, server_rec *s,
                                mod_gnutls_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 mod_gnutls_cache_post_config(apr_pool_t *p, server_rec *s,
                                 mod_gnutls_srvconf_rec *sc)
{
    if (sc->cache_type == mod_gnutls_cache_dbm) {
        return dbm_cache_child_init(p, s, sc);
    }
    return 0;
}

int mod_gnutls_cache_child_init(apr_pool_t *p, server_rec *s,
                                mod_gnutls_srvconf_rec *sc)
{
    if (sc->cache_type == mod_gnutls_cache_dbm) {
        return 0;
    }
#if HAVE_APR_MEMCACHE
    else if (sc->cache_type == mod_gnutls_cache_memcache) {
        return mc_cache_child_init(p, s, sc);
    }
#endif
    return 0;
}

 #include <assert.h>

int mod_gnutls_cache_session_init(mod_gnutls_handle_t *ctxt)
{
    printf("Type: %d Params: %s\n",ctxt->sc->cache_type, ctxt->sc->cache_config);
    if (ctxt->sc->cache_type == mod_gnutls_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 == mod_gnutls_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
    else {
        assert(1);
        /* No Session Cache is Available. Opps. */
    }
    return 0;
}