diff options
author | Paul Querna | 2004-12-10 06:08:52 +0000 |
---|---|---|
committer | Paul Querna | 2004-12-10 06:08:52 +0000 |
commit | a66e1478c77fc8c8a7ab831916ab3d1c3aacb539 (patch) | |
tree | 328f6b0d0ef4303719cab9f2949c5a7a2c65681e /src/gnutls_cache.c | |
parent | 76bd3bfed0fda6fb670cb14c16bcdfd47453dfe5 (diff) |
working support for a ssl session cache via memcached.
Diffstat (limited to 'src/gnutls_cache.c')
-rw-r--r-- | src/gnutls_cache.c | 206 |
1 files changed, 186 insertions, 20 deletions
diff --git a/src/gnutls_cache.c b/src/gnutls_cache.c index 683cdf4..bc13440 100644 --- a/src/gnutls_cache.c +++ b/src/gnutls_cache.c | |||
@@ -16,46 +16,212 @@ | |||
16 | */ | 16 | */ |
17 | 17 | ||
18 | #include "mod_gnutls.h" | 18 | #include "mod_gnutls.h" |
19 | #include "ap_mpm.h" | ||
19 | 20 | ||
20 | /** | 21 | /** |
21 | * GnuTLS Session Cache using libmemcached | 22 | * GnuTLS Session Cache using libmemcached |
22 | * | 23 | * |
23 | */ | 24 | */ |
24 | /* | ||
25 | #include "memcache.h" | ||
26 | 25 | ||
27 | int mod_gnutls_cache_init() | 26 | /* The underlying apr_memcache system is thread safe... woohoo */ |
27 | static apr_memcache_t* mc; | ||
28 | |||
29 | int mod_gnutls_cache_child_init(apr_pool_t *p, server_rec *s, | ||
30 | mod_gnutls_srvconf_rec *sc) | ||
28 | { | 31 | { |
29 | return 0; | 32 | apr_status_t rv = APR_SUCCESS; |
33 | int thread_limit = 0; | ||
34 | int nservers = 0; | ||
35 | char* cache_config; | ||
36 | char* split; | ||
37 | char* tok; | ||
38 | |||
39 | ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit); | ||
40 | |||
41 | /* Find all the servers in the first run to get a total count */ | ||
42 | cache_config = apr_pstrdup(p, sc->cache_config); | ||
43 | split = apr_strtok(cache_config, " ", &tok); | ||
44 | while (split) { | ||
45 | nservers++; | ||
46 | split = apr_strtok(NULL," ", &tok); | ||
47 | } | ||
48 | |||
49 | rv = apr_memcache_create(p, nservers, 0, &mc); | ||
50 | if (rv != APR_SUCCESS) { | ||
51 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, | ||
52 | "[gnutls_cache] Failed to create Memcache Object of '%d' size.", | ||
53 | nservers); | ||
54 | return rv; | ||
55 | } | ||
56 | |||
57 | /* Now add each server to the memcache */ | ||
58 | cache_config = apr_pstrdup(p, sc->cache_config); | ||
59 | split = apr_strtok(cache_config, " ", &tok); | ||
60 | while (split) { | ||
61 | apr_memcache_server_t* st; | ||
62 | char* split2; | ||
63 | char* host_str; | ||
64 | char* port_str; | ||
65 | int port; | ||
66 | |||
67 | host_str = apr_strtok(split,":", &split2); | ||
68 | port_str = apr_strtok(NULL,":", &split2); | ||
69 | if (!port_str) { | ||
70 | port = 11211; /* default port */ | ||
71 | } | ||
72 | else { | ||
73 | port = atoi(port_str); | ||
74 | } | ||
75 | |||
76 | /* Should Max Conns be (thread_limit / nservers) ? */ | ||
77 | rv = apr_memcache_server_create(p, | ||
78 | host_str, port, | ||
79 | 0, | ||
80 | 1, | ||
81 | thread_limit, | ||
82 | 600, | ||
83 | &st); | ||
84 | if(rv != APR_SUCCESS) { | ||
85 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, | ||
86 | "[gnutls_cache] Failed to Create Server: %s:%d", | ||
87 | host_str, port); | ||
88 | return rv; | ||
89 | } | ||
90 | |||
91 | rv = apr_memcache_add_server(mc, st); | ||
92 | if(rv != APR_SUCCESS) { | ||
93 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, | ||
94 | "[gnutls_cache] Failed to Add Server: %s:%d", | ||
95 | host_str, port); | ||
96 | return rv; | ||
97 | } | ||
98 | |||
99 | split = apr_strtok(NULL," ", &tok); | ||
100 | } | ||
101 | return rv; | ||
30 | } | 102 | } |
31 | static int cache_store((void* baton, gnutls_datum_t key, gnutls_datum_t data) | 103 | |
104 | /* thanks mod_ssl */ | ||
105 | #define GNUTLS_SESSION_ID_STRING_LEN \ | ||
106 | ((GNUTLS_MAX_SESSION_ID + 1) * 2) | ||
107 | #define MC_TAG "mod_gnutls:" | ||
108 | #define MC_TAG_LEN \ | ||
109 | (sizeof(MC_TAG)) | ||
110 | #define STR_SESSION_LEN (GNUTLS_SESSION_ID_STRING_LEN + MC_TAG_LEN) | ||
111 | |||
112 | |||
113 | static char *gnutls_session_id2sz(unsigned char *id, int idlen, | ||
114 | char *str, int strsize) | ||
32 | { | 115 | { |
33 | mc_set(struct memcache *mc, | 116 | char *cp; |
34 | key->data, key->size, | 117 | int n; |
35 | data->data, data->size, | 118 | |
36 | 3600, 0); | 119 | cp = apr_cpystrn(str, MC_TAG, MC_TAG_LEN); |
37 | return 0; | 120 | for (n = 0; n < idlen && n < GNUTLS_MAX_SESSION_ID; n++) { |
121 | apr_snprintf(cp, strsize - (cp-str), "%02X", id[n]); | ||
122 | cp += 2; | ||
123 | } | ||
124 | *cp = '\0'; | ||
125 | return str; | ||
38 | } | 126 | } |
39 | 127 | ||
40 | static int cache_fetch(void* baton, gnutls_datum_t key) | 128 | |
129 | static int cache_store(void* baton, gnutls_datum_t key, gnutls_datum_t data) | ||
41 | { | 130 | { |
131 | apr_status_t rv = APR_SUCCESS; | ||
42 | mod_gnutls_handle_t *ctxt = baton; | 132 | mod_gnutls_handle_t *ctxt = baton; |
43 | return 0; | 133 | char buf[STR_SESSION_LEN]; |
134 | char* strkey = NULL; | ||
135 | apr_uint32_t timeout; | ||
136 | |||
137 | strkey = gnutls_session_id2sz(key.data, key.size, buf, sizeof(buf)); | ||
138 | if(!strkey) | ||
139 | return -1; | ||
140 | |||
141 | timeout = 3600; | ||
142 | |||
143 | rv = apr_memcache_set(mc, strkey, data.data, data.size, timeout, 0); | ||
144 | |||
145 | if(rv != APR_SUCCESS) { | ||
146 | ap_log_error(APLOG_MARK, APLOG_CRIT, rv, | ||
147 | ctxt->c->base_server, | ||
148 | "[gnutls_cache] error setting key '%s' " | ||
149 | "with %d bytes of data", strkey, data.size); | ||
150 | return -1; | ||
151 | } | ||
152 | |||
153 | return 0; | ||
154 | } | ||
155 | |||
156 | static gnutls_datum_t cache_fetch(void* baton, gnutls_datum_t key) | ||
157 | { | ||
158 | apr_status_t rv = APR_SUCCESS; | ||
159 | mod_gnutls_handle_t *ctxt = baton; | ||
160 | char buf[STR_SESSION_LEN]; | ||
161 | char* strkey = NULL; | ||
162 | char* value; | ||
163 | apr_size_t value_len; | ||
164 | gnutls_datum_t data = { NULL, 0 }; | ||
165 | |||
166 | strkey = gnutls_session_id2sz(key.data, key.size, buf, sizeof(buf)); | ||
167 | if(!strkey) { | ||
168 | return data; | ||
169 | } | ||
170 | |||
171 | rv = apr_memcache_getp(mc, ctxt->c->pool, strkey, | ||
172 | &value, &value_len, NULL); | ||
173 | |||
174 | if(rv != APR_SUCCESS) { | ||
175 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, | ||
176 | ctxt->c->base_server, | ||
177 | "[gnutls_cache] error fetching key '%s' ", | ||
178 | strkey); | ||
179 | |||
180 | data.size = 0; | ||
181 | data.data = NULL; | ||
182 | return data; | ||
183 | } | ||
184 | |||
185 | /* TODO: Eliminate this memcpy. ffs. gnutls-- */ | ||
186 | data.data = gnutls_malloc(value_len); | ||
187 | if (data.data == NULL) | ||
188 | return data; | ||
189 | |||
190 | data.size = value_len; | ||
191 | memcpy(data.data, value, value_len); | ||
192 | |||
193 | return data; | ||
44 | } | 194 | } |
45 | 195 | ||
46 | static int cache_delete(void* baton, gnutls_datum_t key) | 196 | static int cache_delete(void* baton, gnutls_datum_t key) |
47 | { | 197 | { |
198 | apr_status_t rv = APR_SUCCESS; | ||
48 | mod_gnutls_handle_t *ctxt = baton; | 199 | mod_gnutls_handle_t *ctxt = baton; |
49 | return 0; | 200 | char buf[STR_SESSION_LEN]; |
201 | char* strkey = NULL; | ||
202 | |||
203 | strkey = gnutls_session_id2sz(key.data, key.size, buf, sizeof(buf)); | ||
204 | if(!strkey) | ||
205 | return -1; | ||
206 | |||
207 | rv = apr_memcache_delete(mc, strkey, 0); | ||
208 | |||
209 | if(rv != APR_SUCCESS) { | ||
210 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, | ||
211 | ctxt->c->base_server, | ||
212 | "[gnutls_cache] error deleting key '%s' ", | ||
213 | strkey); | ||
214 | return -1; | ||
215 | } | ||
216 | |||
217 | return 0; | ||
50 | } | 218 | } |
51 | 219 | ||
52 | int mod_gnutls_cache_session_init(mod_gnutls_handle_t *ctxt) | 220 | int mod_gnutls_cache_session_init(mod_gnutls_handle_t *ctxt) |
53 | { | 221 | { |
54 | gnutls_db_set_cache_expiration | 222 | gnutls_db_set_retrieve_function(ctxt->session, cache_fetch); |
55 | gnutls_db_set_retrieve_function(session, cache_fetch); | 223 | gnutls_db_set_remove_function(ctxt->session, cache_delete); |
56 | gnutls_db_set_remove_function(session, cache_delete); | 224 | gnutls_db_set_store_function(ctxt->session, cache_store); |
57 | gnutls_db_set_store_function(session, cache_store); | 225 | gnutls_db_set_ptr(ctxt->session, ctxt); |
58 | gnutls_db_set_ptr(session, NULL); | 226 | return 0; |
59 | return 0; | ||
60 | } | 227 | } |
61 | */ | ||