summaryrefslogtreecommitdiffstats
path: root/modules/ssl/ssl_scache_memcache.c
blob: f542e07bfa81e0bcebdd0d0de551ad598e697e4e (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
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/


/*                      _             _
 *  _ __ ___   ___   __| |    ___ ___| |  mod_ssl
 * | '_ ` _ \ / _ \ / _` |   / __/ __| |  Apache Interface to OpenSSL
 * | | | | | | (_) | (_| |   \__ \__ \ |
 * |_| |_| |_|\___/ \__,_|___|___/___/_|
 *                      |_____|
 *  ssl_scache_memcache.c
 *  Distributed Session Cache on top of memcached
 */

#include "ssl_private.h"

#ifdef HAVE_SSL_CACHE_MEMCACHE

#include "apr_memcache.h"
#include "ap_mpm.h"

/*
 * SSL Session Caching using memcached as a backend.
 */

/*
**
** High-Level "handlers" as per ssl_scache.c
**
*/


/* The underlying apr_memcache system is thread safe.. */
#define MC_TAG "mod_ssl:"
#define MC_TAG_LEN \
    (sizeof(MC_TAG))

#define MC_KEY_LEN 254

#ifndef MC_DEFAULT_SERVER_PORT
#define MC_DEFAULT_SERVER_PORT 11211
#endif


#ifndef MC_DEFAULT_SERVER_MIN
#define MC_DEFAULT_SERVER_MIN 0
#endif

#ifndef MC_DEFAULT_SERVER_SMAX
#define MC_DEFAULT_SERVER_SMAX 1
#endif

#ifndef MC_DEFAULT_SERVER_TTL
#define MC_DEFAULT_SERVER_TTL 600
#endif

struct context {
    const char *servers;
    apr_memcache_t *mc;
};

static const char *ssl_scache_mc_create(void **context, const char *arg, 
                                        apr_pool_t *tmp, apr_pool_t *p)
{
    struct context *ctx;
    
    *context = ctx = apr_palloc(p, sizeof *ctx);

    ctx->servers = apr_pstrdup(p, arg);

    return NULL;
}

static apr_status_t ssl_scache_mc_init(void *context, server_rec *s, apr_pool_t *p)
{
    apr_status_t rv;
    int thread_limit = 0;
    int nservers = 0;
    char *cache_config;
    char *split;
    char *tok;
    struct context *ctx = context;

    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, ctx->servers);
    split = apr_strtok(cache_config, ",", &tok);
    while (split) {
        nservers++;
        split = apr_strtok(NULL,",", &tok);
    }

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

    /* Now add each server to the memcache */
    cache_config = apr_pstrdup(p, ctx->servers);
    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,
                         "SSLSessionCache: Failed to Parse Server: '%s'", split);
            return rv;
        }

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

        if (port == 0) {
            port = MC_DEFAULT_SERVER_PORT;
        }

        rv = apr_memcache_server_create(p,
                                        host_str, port,
                                        MC_DEFAULT_SERVER_MIN,
                                        MC_DEFAULT_SERVER_SMAX,
                                        thread_limit,
                                        MC_DEFAULT_SERVER_TTL,
                                        &st);
        if (rv != APR_SUCCESS) {
            ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
                         "SSLSessionCache: Failed to Create Server: %s:%d", 
                         host_str, port);
            return rv;
        }

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

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

    return APR_SUCCESS;
}

static void ssl_scache_mc_kill(void *context, server_rec *s)
{
    /* noop. */
}

static char *mc_session_id2sz(const unsigned char *id, unsigned int idlen,
                              char *str, int strsize)
{
    char *cp;
    int n;
    int maxlen = (strsize - MC_TAG_LEN)/2;

    cp = apr_cpystrn(str, MC_TAG, MC_TAG_LEN);
    for (n = 0; n < idlen && n < maxlen; n++) {
        apr_snprintf(cp, 3, "%02X", (unsigned) id[n]);
        cp += 2;
    }

    *cp = '\0';

    return str;
}

static apr_status_t ssl_scache_mc_store(void *context, server_rec *s, 
                                        const unsigned char *id, unsigned int idlen,
                                        time_t timeout,
                                        unsigned char *ucaData, unsigned int nData)
{
    struct context *ctx = context;
    char buf[MC_KEY_LEN];
    char *strkey = NULL;
    apr_status_t rv;

    strkey = mc_session_id2sz(id, idlen, buf, sizeof(buf));
    if(!strkey) {
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "scache_mc: Key generation borked.");
        return APR_EGENERAL;
    }

    rv = apr_memcache_set(ctx->mc, strkey, (char*)ucaData, nData, timeout, 0);

    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
                     "scache_mc: error setting key '%s' "
                     "with %d bytes of data", strkey, nData);
        return rv;
    }

    return APR_SUCCESS;
}

static apr_status_t ssl_scache_mc_retrieve(void *context, server_rec *s, 
                                           const unsigned char *id, unsigned int idlen,
                                           unsigned char *dest, unsigned int *destlen,
                                           apr_pool_t *p)
{
    struct context *ctx = context;
    apr_size_t der_len;
    char buf[MC_KEY_LEN], *der;
    char *strkey = NULL;
    apr_status_t rv;

    strkey = mc_session_id2sz(id, idlen, buf, sizeof(buf));

    if (!strkey) {
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
                     "scache_mc: Key generation borked.");
        return APR_EGENERAL;
    }

    /* ### this could do with a subpool, but _getp looks like it will
     * eat memory like it's going out of fashion anyway. */

    rv = apr_memcache_getp(ctx->mc, p, strkey,
                           &der, &der_len, NULL);
    if (rv) {
        if (rv != APR_NOTFOUND) {
            ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
                         "scache_mc: 'get_session' FAIL");
        }
        return rv;
    }
    else if (der_len > *destlen) {
        ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
                     "scache_mc: 'get_session' OVERFLOW");
        return rv;
    }    

    memcpy(dest, der, der_len);
    *destlen = der_len;

    return APR_SUCCESS;
}

static void ssl_scache_mc_remove(void *context, server_rec *s, 
                                 const unsigned char *id, unsigned int idlen,
                                 apr_pool_t *p)
{
    struct context *ctx = context;
    char buf[MC_KEY_LEN];
    char* strkey = NULL;
    apr_status_t rv;

    strkey = mc_session_id2sz(id, idlen, buf, sizeof(buf));
    if(!strkey) {
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "scache_mc: Key generation borked.");
        return;
    }

    rv = apr_memcache_delete(ctx->mc, strkey, 0);

    if (rv != APR_SUCCESS) {
        ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, s,
                     "scache_mc: error deleting key '%s' ",
                     strkey);
        return;
    }
}

static void ssl_scache_mc_status(void *context, request_rec *r, int flags)
{
    /* SSLModConfigRec *mc = myModConfig(r->server); */
    /* TODO: Make a mod_status handler. meh. */
}

const modssl_sesscache_provider modssl_sesscache_mc = {
    "memcache",
    0,
    ssl_scache_mc_create,
    ssl_scache_mc_init,
    ssl_scache_mc_kill,
    ssl_scache_mc_store,
    ssl_scache_mc_retrieve,
    ssl_scache_mc_remove,
    ssl_scache_mc_status
};
#endif