1 | /* ==================================================================== |
---|
2 | * Copyright 2004 Paul Querna |
---|
3 | * |
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
5 | * you may not use this file except in compliance with the License. |
---|
6 | * You may obtain a copy of the License at |
---|
7 | * |
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
9 | * |
---|
10 | * Unless required by applicable law or agreed to in writing, software |
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
13 | * See the License for the specific language governing permissions and |
---|
14 | * limitations under the License. |
---|
15 | * |
---|
16 | */ |
---|
17 | |
---|
18 | #include "mod_gnutls.h" |
---|
19 | #include "ap_mpm.h" |
---|
20 | |
---|
21 | /** |
---|
22 | * GnuTLS Session Cache using libmemcached |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
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) |
---|
31 | { |
---|
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; |
---|
102 | } |
---|
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) |
---|
115 | { |
---|
116 | char *cp; |
---|
117 | int n; |
---|
118 | |
---|
119 | cp = apr_cpystrn(str, MC_TAG, MC_TAG_LEN); |
---|
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; |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | static int cache_store(void* baton, gnutls_datum_t key, gnutls_datum_t data) |
---|
130 | { |
---|
131 | apr_status_t rv = APR_SUCCESS; |
---|
132 | mod_gnutls_handle_t *ctxt = baton; |
---|
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; |
---|
194 | } |
---|
195 | |
---|
196 | static int cache_delete(void* baton, gnutls_datum_t key) |
---|
197 | { |
---|
198 | apr_status_t rv = APR_SUCCESS; |
---|
199 | mod_gnutls_handle_t *ctxt = baton; |
---|
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; |
---|
218 | } |
---|
219 | |
---|
220 | int mod_gnutls_cache_session_init(mod_gnutls_handle_t *ctxt) |
---|
221 | { |
---|
222 | gnutls_db_set_retrieve_function(ctxt->session, cache_fetch); |
---|
223 | gnutls_db_set_remove_function(ctxt->session, cache_delete); |
---|
224 | gnutls_db_set_store_function(ctxt->session, cache_store); |
---|
225 | gnutls_db_set_ptr(ctxt->session, ctxt); |
---|
226 | return 0; |
---|
227 | } |
---|