Debian Codegraph @

contrib/mod_tls_memcache.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_tls_memcache -- a module which provides shared SSL session
3 * and OCSP response caches using memcached servers
4 * Copyright (c) 2011-2021 TJ Saunders
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders and other respective copyright holders
21 * give permission to link this program with OpenSSL, and distribute the
22 * resulting executable, without including the source code for OpenSSL in the
23 * source distribution.
24 *
25 * This is mod_tls_memcache, contrib software for proftpd 1.3.x and above.
26 * For more information contact TJ Saunders <[email protected]>.
27 */
28
29 #include "conf.h"
30 #include "privs.h"
31 #include "mod_tls.h"
32 #include "json.h"
33 #include "hanson-tpl.h"
34
35 #define MOD_TLS_MEMCACHE_VERSION "mod_tls_memcache/0.2"
36
37 /* Make sure the version of proftpd is as necessary. */
38 #if PROFTPD_VERSION_NUMBER < 0x0001030602
39 # error "ProFTPD 1.3.6rc2 or later required"
40 #endif
41
42 module tls_memcache_module;
43
44 /* For communicating with memcached servers for shared data. */
45 static pr_memcache_t *sess_mcache = NULL;
46
47 /* Assume a maximum SSL session (serialized) length of 10K. Note that this
48 * is different from the SSL_MAX_SSL_SESSION_ID_LENGTH provided by OpenSSL.
49 * There is no limit imposed on the length of the ASN1 description of the
50 * SSL session data.
51 */
52 #ifndef TLS_MAX_SSL_SESSION_SIZE
53 # define TLS_MAX_SSL_SESSION_SIZE 1024 * 10
54 #endif
55
56 static unsigned long sess_cache_opts = 0UL;
57 #define SESS_CACHE_OPT_USE_JSON 0x0001
58
59 struct sesscache_entry {
60 uint32_t expires;
61 unsigned int sess_datalen;
62 unsigned char sess_data[TLS_MAX_SSL_SESSION_SIZE];
63 };
64
65 /* These are tpl format strings */
66 #define SESS_CACHE_TPL_KEY_FMT "s"
67 #define SESS_CACHE_TPL_VALUE_FMT "S(uic#)"
68
69 /* These are the JSON format field names */
70 #define SESS_CACHE_JSON_KEY_EXPIRES "expires"
71 #define SESS_CACHE_JSON_KEY_DATA "data"
72 #define SESS_CACHE_JSON_KEY_DATA_LENGTH "data_len"
73
74 /* The difference between sesscache_entry and sesscache_large_entry is that the
75 * buffers in the latter are dynamically allocated from the heap, not
76 * stored in memcached (given that memcached has limits on how much it can
77 * store). The large_entry struct is used for storing sessions which don't
78 * fit into memcached; this also means that these large entries are NOT shared
79 * across processes.
80 */
81 struct sesscache_large_entry {
82 time_t expires;
83 unsigned int sess_id_len;
84 const unsigned char *sess_id;
85 unsigned int sess_datalen;
86 const unsigned char *sess_data;
87 };
88
89 /* These stats are stored in memcached as well, so that the status command can
90 * be run on _any_ proftpd in the cluster.
91 */
92 struct sesscache_key {
93 const char *key;
94 const char *desc;
95 };
96
97 static struct sesscache_key sesscache_keys[] = {
98 { "cache_hits", "Cache lifetime hits" },
99 { "cache_misses", "Cache lifetime misses" },
100 { "cache_stores", "Cache lifetime sessions stored" },
101 { "cache_deletes", "Cache lifetime sessions deleted" },
102 { "cache_errors", "Cache lifetime errors handling sessions in cache" },
103 { "cache_exceeds", "Cache lifetime sessions exceeding max entry size" },
104 { "cache_max_sess_len", "Largest session exceeding max entry size" },
105 { NULL, NULL }
106 };
107
108 /* Indexes into the sesscache_keys array */
109 #define SESSCACHE_KEY_HITS 0
110 #define SESSCACHE_KEY_MISSES 1
111 #define SESSCACHE_KEY_STORES 2
112 #define SESSCACHE_KEY_DELETES 3
113 #define SESSCACHE_KEY_ERRORS 4
114 #define SESSCACHE_KEY_EXCEEDS 5
115 #define SESSCACHE_KEY_MAX_LEN 6
116
117 static tls_sess_cache_t sess_cache;
118 static array_header *sesscache_sess_list = NULL;
119
120 #if defined(PR_USE_OPENSSL_OCSP)
121 static pr_memcache_t *ocsp_mcache = NULL;
122
123 /* Assume a maximum OCSP response (serialized) length of 4K. */
124 # ifndef TLS_MAX_OCSP_RESPONSE_SIZE
125 # define TLS_MAX_OCSP_RESPONSE_SIZE 1024 * 4
126 # endif
127
128 struct ocspcache_entry {
129 time_t age;
130 unsigned int fingerprint_len;
131 char fingerprint[EVP_MAX_MD_SIZE];
132 unsigned int resp_derlen;
133 unsigned char resp_der[TLS_MAX_OCSP_RESPONSE_SIZE];
134 };
135
136 /* These are the JSON format field names */
137 #define OCSP_CACHE_JSON_KEY_AGE "expires"
138 #define OCSP_CACHE_JSON_KEY_RESPONSE "response"
139 #define OCSP_CACHE_JSON_KEY_RESPONSE_LENGTH "response_len"
140
141 /* The difference between ocspcache_entry and ocspcache_large_entry is that the
142 * buffers in the latter are dynamically allocated from the heap, not
143 * stored in memcached (given that memcached has limits on how much it can
144 * store). The large_entry struct is used for storing responses which don't
145 * fit into memcached; this also means that these large entries are NOT shared
146 * across processes.
147 */
148 struct ocspcache_large_entry {
149 time_t age;
150 unsigned int fingerprint_len;
151 char *fingerprint;
152 unsigned int resp_derlen;
153 unsigned char *resp_der;
154 };
155
156 /* These stats are stored in memcached as well, so that the status command can
157 * be run on _any_ proftpd in the cluster.
158 */
159 struct ocspcache_key {
160 const char *key;
161 const char *desc;
162 };
163
164 static struct ocspcache_key ocspcache_keys[] = {
165 { "cache_hits", "Cache lifetime hits" },
166 { "cache_misses", "Cache lifetime misses" },
167 { "cache_stores", "Cache lifetime responses stored" },
168 { "cache_deletes", "Cache lifetime responses deleted" },
169 { "cache_errors", "Cache lifetime errors handling responses in cache" },
170 { "cache_exceeds", "Cache lifetime responses exceeding max entry size" },
171 { "cache_max_resp_len", "Largest response exceeding max entry size" },
172 { NULL, NULL }
173 };
174
175 /* Indexes into the ocspcache_keys array */
176 #define OCSPCACHE_KEY_HITS 0
177 #define OCSPCACHE_KEY_MISSES 1
178 #define OCSPCACHE_KEY_STORES 2
179 #define OCSPCACHE_KEY_DELETES 3
180 #define OCSPCACHE_KEY_ERRORS 4
181 #define OCSPCACHE_KEY_EXCEEDS 5
182 #define OCSPCACHE_KEY_MAX_LEN 6
183
184 static tls_ocsp_cache_t ocsp_cache;
185 static array_header *ocspcache_resp_list = NULL;
186 #endif
187
188 static const char *trace_channel = "tls.memcache";
189
190 static int sess_cache_close(tls_sess_cache_t *);
191 #if defined(PR_USE_OPENSSL_OCSP)
192 static int ocsp_cache_close(tls_ocsp_cache_t *);
193 #endif /* PR_USE_OPENSSL_OCSP */
194 static int tls_mcache_sess_init(void);
195
196 static const char *mcache_get_errors(void) {
197 unsigned int count = 0;
198 unsigned long error_code;
199 BIO *bio = NULL;
200 char *data = NULL;
201 long datalen;
202 const char *error_data = NULL, *str = "(unknown)";
203 int error_flags = 0;
204
205 /* Use ERR_print_errors() and a memory BIO to build up a string with
206 * all of the error messages from the error queue.
207 */
208
209 error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags);
210 if (error_code) {
211 bio = BIO_new(BIO_s_mem());
212 }
213
214 while (error_code) {
215 pr_signals_handle();
216
217 if (error_flags & ERR_TXT_STRING) {
218 BIO_printf(bio, "\n (%u) %s [%s]", ++count,
219 ERR_error_string(error_code, NULL), error_data);
220
221 } else {
222 BIO_printf(bio, "\n (%u) %s", ++count,
223 ERR_error_string(error_code, NULL));
224 }
225
226 error_data = NULL;
227 error_flags = 0;
228 error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags);
229 }
230
231 datalen = BIO_get_mem_data(bio, &data);
232 if (data) {
233 data[datalen] = '\0';
234 str = pstrdup(permanent_pool, data);
235 }
236
237 if (bio != NULL) {
238 BIO_free(bio);
239 }
240
241 return str;
242 }
243
244 /* SSL session cache implementation callbacks.
245 */
246
247 /* Functions for marshalling key/value data to/from memcached. */
248
249 static int sess_cache_get_tpl_key(pool *p, const unsigned char *sess_id,
250 unsigned int sess_id_len, void **key, size_t *keysz) {
251 char *sess_id_hex;
252 void *data = NULL;
253 size_t datasz = 0;
254 int res;
255
256 sess_id_hex = pr_str_bin2hex(p, sess_id, sess_id_len, 0);
257
258 res = tpl_jot(TPL_MEM, &data, &datasz, SESS_CACHE_TPL_KEY_FMT, &sess_id_hex);
259 if (res < 0) {
260 return -1;
261 }
262
263 *keysz = datasz;
264 *key = palloc(p, datasz);
265 memcpy(*key, data, datasz);
266 free(data);
267
268 return 0;
269 }
270
271 static int sess_cache_get_json_key(pool *p, const unsigned char *sess_id,
272 unsigned int sess_id_len, void **key, size_t *keysz) {
273 char *sess_id_hex, *json_text;
274 pr_json_object_t *json;
275
276 sess_id_hex = pr_str_bin2hex(p, sess_id, sess_id_len, 0);
277 json = pr_json_object_alloc(p);
278 (void) pr_json_object_set_string(p, json, "id", sess_id_hex);
279
280 json_text = pr_json_object_to_text(p, json, "");
281
282 /* Include the terminating NUL in the key. */
283 *keysz = strlen(json_text) + 1;
284 *key = pstrndup(p, json_text, *keysz - 1);
285 (void) pr_json_object_free(json);
286
287 return 0;
288 }
289
290 static int sess_cache_get_key(pool *p, const unsigned char *sess_id,
291 unsigned int sess_id_len, void **key, size_t *keysz) {
292 int res;
293 const char *key_type = "unknown";
294
295 if (sess_cache_opts & SESS_CACHE_OPT_USE_JSON) {
296 key_type = "JSON";
297 res = sess_cache_get_json_key(p, sess_id, sess_id_len, key, keysz);
298
299 } else {
300 key_type = "TPL";
301 res = sess_cache_get_tpl_key(p, sess_id, sess_id_len, key, keysz);
302 }
303
304 if (res < 0) {
305 pr_trace_msg(trace_channel, 3,
306 "error constructing cache %s lookup key for session ID (%lu bytes)",
307 key_type, (unsigned long) keysz);
308 return -1;
309 }
310
311 return 0;
312 }
313
314 static int sess_cache_entry_decode_tpl(pool *p, void *value, size_t valuesz,
315 struct sesscache_entry *se) {
316 int res;
317 tpl_node *tn;
318
319 tn = tpl_map(SESS_CACHE_TPL_VALUE_FMT, se, TLS_MAX_SSL_SESSION_SIZE);
320 if (tn == NULL) {
321 tls_log(MOD_TLS_MEMCACHE_VERSION
322 ": error allocating tpl_map for format '%s'", SESS_CACHE_TPL_VALUE_FMT);
323 errno = ENOMEM;
324 return -1;
325 }
326
327 res = tpl_load(tn, TPL_MEM, value, valuesz);
328 if (res < 0) {
329 pr_trace_msg(trace_channel, 3, "%s",
330 "error loading TPL memcache session data");
331 tpl_free(tn);
332 errno = EINVAL;
333 return -1;
334 }
335
336 res = tpl_unpack(tn, 0);
337 if (res < 0) {
338 pr_trace_msg(trace_channel, 3, "%s",
339 "error unpacking TPL memcache session data");
340 tpl_free(tn);
341 errno = EINVAL;
342 return -1;
343 }
344
345 tpl_free(tn);
346
347 return 0;
348 }
349
350 static int entry_get_json_number(pool *p, pr_json_object_t *json,
351 const char *key, double *val, const char *text) {
352 if (pr_json_object_get_number(p, json, key, val) < 0) {
353 if (errno == EEXIST) {
354 pr_trace_msg(trace_channel, 3,
355 "ignoring non-number '%s' JSON field in '%s'", key, text);
356
357 } else {
358 tls_log(MOD_TLS_MEMCACHE_VERSION
359 ": missing required '%s' JSON field in '%s'", key, text);
360 }
361
362 (void) pr_json_object_free(json);
363 errno = EINVAL;
364 return -1;
365 }
366
367 return 0;
368 }
369
370 static int entry_get_json_string(pool *p, pr_json_object_t *json,
371 const char *key, char **val, const char *text) {
372 if (pr_json_object_get_string(p, json, key, val) < 0) {
373 if (errno == EEXIST) {
374 pr_trace_msg(trace_channel, 3,
375 "ignoring non-string '%s' JSON field in '%s'", key, text);
376
377 } else {
378 tls_log(MOD_TLS_MEMCACHE_VERSION
379 ": missing required '%s' JSON field in '%s'", key, text);
380 }
381
382 (void) pr_json_object_free(json);
383 errno = EINVAL;
384 return -1;
385 }
386
387 return 0;
388 }
389
390 static int sess_cache_entry_decode_json(pool *p, void *value, size_t valuesz,
391 struct sesscache_entry *se) {
392 int res;
393 pr_json_object_t *json;
394 const char *key;
395 char *entry, *text;
396 double number = 0;
397
398 entry = value;
399 if (pr_json_text_validate(p, entry) == FALSE) {
400 tls_log(MOD_TLS_MEMCACHE_VERSION
401 ": unable to decode invalid JSON session cache entry: '%s'", entry);
402 errno = EINVAL;
403 return -1;
404 }
405
406 json = pr_json_object_from_text(p, entry);
407 if (json == NULL) {
408 return -1;
409 }
410
411 key = SESS_CACHE_JSON_KEY_EXPIRES;
412 res = entry_get_json_number(p, json, key, &number, entry);
413 if (res < 0) {
414 int xerrno = errno;
415
416 (void) pr_json_object_free(json);
417 errno = xerrno;
418 return -1;
419 }
420 se->expires = (uint32_t) number;
421
422 key = SESS_CACHE_JSON_KEY_DATA;
423 res = entry_get_json_string(p, json, key, &text, entry);
424 if (res == 0) {
425 pool *tmp_pool = NULL;
426 int have_padding = FALSE;
427 char *base64_data;
428 size_t base64_datalen;
429 unsigned char *data;
430
431 tmp_pool = make_sub_pool(p);
432 pr_pool_tag(tmp_pool, "TLS Memcache session cache base64 data pool");
433
434 base64_data = text;
435 base64_datalen = strlen(base64_data);
436
437 /* Due to Base64's padding, we need to detect if the last block was
438 * padded with zeros; we do this by looking for '=' characters at the
439 * end of the text being decoded. If we see these characters, then we
440 * will "trim" off any trailing zero values in the decoded data, on the
441 * ASSUMPTION that they are the auto-added padding bytes.
442 */
443 if (base64_data[base64_datalen-1] == '=') {
444 have_padding = TRUE;
445 }
446
447 data = palloc(tmp_pool, base64_datalen);
448 res = EVP_DecodeBlock(data, (unsigned char *) base64_data,
449 (int) base64_datalen);
450 if (res <= 0) {
451 /* Base64-decoding error. */
452 pr_trace_msg(trace_channel, 5,
453 "error base64-decoding session data in '%s', rejecting", entry);
454 (void) pr_json_object_free(json);
455
456 destroy_pool(tmp_pool);
457 errno = EINVAL;
458 return -1;
459 }
460
461 if (have_padding == TRUE) {
462 /* Assume that only one or two zero bytes of padding were added. */
463 if (data[res-1] == '\0') {
464 res -= 1;
465
466 if (data[res-1] == '\0') {
467 res -= 1;
468 }
469 }
470 }
471
472 if (res <= TLS_MAX_SSL_SESSION_SIZE) {
473 /* Base64-decoded data will fit into our buffer as expected. */
474 memcpy(se->sess_data, data, res);
475
476 } else {
477 tls_log(MOD_TLS_MEMCACHE_VERSION
478 ": decoded JSON session cache entry too large, ignoring");
479 (void) pr_json_object_free(json);
480
481 destroy_pool(tmp_pool);
482 errno = EINVAL;
483 return -1;
484 }
485
486 destroy_pool(tmp_pool);
487
488 } else {
489 int xerrno = errno;
490
491 (void) pr_json_object_free(json);
492 errno = xerrno;
493 return -1;
494 }
495
496 key = SESS_CACHE_JSON_KEY_DATA_LENGTH;
497 res = entry_get_json_number(p, json, key, &number, entry);
498 if (res < 0) {
499 int xerrno = errno;
500
501 (void) pr_json_object_free(json);
502 errno = xerrno;
503 return -1;
504 }
505 se->sess_datalen = (unsigned int) number;
506
507 (void) pr_json_object_free(json);
508 return 0;
509 }
510
511 static int sess_cache_mcache_entry_get(pool *p, const unsigned char *sess_id,
512 unsigned int sess_id_len, struct sesscache_entry *se) {
513 int res;
514 void *key = NULL, *value = NULL;
515 size_t keysz = 0, valuesz = 0;
516 uint32_t flags = 0;
517
518 res = sess_cache_get_key(p, sess_id, sess_id_len, &key, &keysz);
519 if (res < 0) {
520 pr_trace_msg(trace_channel, 1,
521 "unable to get cache entry: error getting cache key: %s",
522 strerror(errno));
523
524 return -1;
525 }
526
527 value = pr_memcache_kget(sess_mcache, &tls_memcache_module,
528 (const char *) key, keysz, &valuesz, &flags);
529 if (value == NULL) {
530 pr_trace_msg(trace_channel, 3,
531 "no matching memcache entry found for session ID (%lu bytes)",
532 (unsigned long) keysz);
533 errno = ENOENT;
534 return -1;
535 }
536
537 /* Decode the cached session data. */
538 if (sess_cache_opts & SESS_CACHE_OPT_USE_JSON) {
539 res = sess_cache_entry_decode_json(p, value, valuesz, se);
540
541 } else {
542 res = sess_cache_entry_decode_tpl(p, value, valuesz, se);
543 }
544
545 if (res == 0) {
546 time_t now;
547
548 /* Check for expired cache entries. */
549 time(&now);
550
551 if (se->expires <= now) {
552 pr_trace_msg(trace_channel, 4,
553 "ignoring expired cached session data (expires %lu <= now %lu)",
554 (unsigned long) se->expires, (unsigned long) now);
555 errno = EPERM;
556 return -1;
557 }
558
559 pr_trace_msg(trace_channel, 9, "retrieved session data from cache using %s",
560 sess_cache_opts & SESS_CACHE_OPT_USE_JSON ? "JSON" : "TPL");
561 }
562
563 return 0;
564 }
565
566 static int sess_cache_mcache_entry_delete(pool *p, const unsigned char *sess_id,
567 unsigned int sess_id_len) {
568 int res;
569 void *key = NULL;
570 size_t keysz = 0;
571
572 res = sess_cache_get_key(p, sess_id, sess_id_len, &key, &keysz);
573 if (res < 0) {
574 pr_trace_msg(trace_channel, 1,
575 "unable to remove cache entry: error getting cache key: %s",
576 strerror(errno));
577
578 return -1;
579 }
580
581 res = pr_memcache_kremove(sess_mcache, &tls_memcache_module,
582 (const char *) key, keysz, 0);
583 if (res < 0) {
584 int xerrno = errno;
585
586 pr_trace_msg(trace_channel, 2,
587 "unable to remove memcache entry for session ID (%lu bytes): %s",
588 (unsigned long) keysz, strerror(xerrno));
589
590 errno = xerrno;
591 return -1;
592 }
593
594 return 0;
595 }
596
597 static int sess_cache_entry_encode_tpl(pool *p, void **value, size_t *valuesz,
598 struct sesscache_entry *se) {
599 int res;
600 tpl_node *tn;
601 void *ptr = NULL;
602
603 tn = tpl_map(SESS_CACHE_TPL_VALUE_FMT, se, TLS_MAX_SSL_SESSION_SIZE);
604 if (tn == NULL) {
605 pr_trace_msg(trace_channel, 1,
606 "error allocating tpl_map for format '%s'", SESS_CACHE_TPL_VALUE_FMT);
607 return -1;
608 }
609
610 res = tpl_pack(tn, 0);
611 if (res < 0) {
612 pr_trace_msg(trace_channel, 1, "%s",
613 "error marshalling TPL memcache session data");
614 return -1;
615 }
616
617 res = tpl_dump(tn, TPL_MEM, &ptr, valuesz);
618 if (res < 0) {
619 pr_trace_msg(trace_channel, 1, "%s",
620 "error dumping marshalled TPL memcache session data");
621 return -1;
622 }
623
624 /* Duplicate the value using the given pool, so that we can free up the
625 * memory allocated by tpl_dump().
626 */
627 *value = palloc(p, *valuesz);
628 memcpy(*value, ptr, *valuesz);
629
630 tpl_free(tn);
631 free(ptr);
632
633 return 0;
634 }
635
636 static int sess_cache_entry_encode_json(pool *p, void **value, size_t *valuesz,
637 struct sesscache_entry *se) {
638 pr_json_object_t *json;
639 pool *tmp_pool;
640 char *base64_data = NULL, *json_text;
641
642 json = pr_json_object_alloc(p);
643 (void) pr_json_object_set_number(p, json, SESS_CACHE_JSON_KEY_EXPIRES,
644 (double) se->expires);
645
646 /* Base64-encode the session data. Note that EVP_EncodeBlock does
647 * NUL-terminate the encoded data.
648 */
649 tmp_pool = make_sub_pool(p);
650 base64_data = pcalloc(tmp_pool, se->sess_datalen * 2);
651
652 EVP_EncodeBlock((unsigned char *) base64_data, se->sess_data,
653 (int) se->sess_datalen);
654 (void) pr_json_object_set_string(p, json, SESS_CACHE_JSON_KEY_DATA,
655 base64_data);
656 (void) pr_json_object_set_number(p, json, SESS_CACHE_JSON_KEY_DATA_LENGTH,
657 (double) se->sess_datalen);
658
659 destroy_pool(tmp_pool);
660
661 json_text = pr_json_object_to_text(p, json, "");
662 (void) pr_json_object_free(json);
663
664 if (json_text == NULL) {
665 errno = ENOMEM;
666 return -1;
667 }
668
669 /* Safety check */
670 if (pr_json_text_validate(p, json_text) == FALSE) {
671 pr_trace_msg(trace_channel, 1, "invalid JSON emitted: '%s'", json_text);
672 errno = EINVAL;
673 return -1;
674 }
675
676 /* Include the terminating NUL in the value. */
677 *valuesz = strlen(json_text) + 1;
678 *value = pstrndup(p, json_text, *valuesz - 1);
679
680 return 0;
681 }
682
683 static int sess_cache_mcache_entry_set(pool *p, const unsigned char *sess_id,
684 unsigned int sess_id_len, struct sesscache_entry *se) {
685 int res, xerrno = 0;
686 void *key = NULL, *value = NULL;
687 size_t keysz = 0, valuesz = 0;
688 uint32_t flags = 0;
689
690 /* Encode the SSL session data. */
691 if (sess_cache_opts & SESS_CACHE_OPT_USE_JSON) {
692 res = sess_cache_entry_encode_json(p, &value, &valuesz, se);
693
694 } else {
695 res = sess_cache_entry_encode_tpl(p, &value, &valuesz, se);
696 }
697
698 if (res < 0) {
699 xerrno = errno;
700
701 pr_trace_msg(trace_channel, 4, "error %s encoding session data: %s",
702 sess_cache_opts & SESS_CACHE_OPT_USE_JSON ? "JSON" : "TPL",
703 strerror(xerrno));
704
705 errno = xerrno;
706 return -1;
707 }
708
709 res = sess_cache_get_key(p, sess_id, sess_id_len, &key, &keysz);
710 xerrno = errno;
711 if (res < 0) {
712 pr_trace_msg(trace_channel, 1,
713 "unable to set cache entry: error getting cache key: %s",
714 strerror(xerrno));
715
716 errno = xerrno;
717 return -1;
718 }
719
720 res = pr_memcache_kset(sess_mcache, &tls_memcache_module, (const char *) key,
721 keysz, value, valuesz, se->expires, flags);
722 xerrno = errno;
723
724 if (res < 0) {
725 pr_trace_msg(trace_channel, 2,
726 "unable to add memcache entry for session ID (%lu bytes): %s",
727 (unsigned long) keysz, strerror(xerrno));
728
729 errno = xerrno;
730 return -1;
731 }
732
733 pr_trace_msg(trace_channel, 9, "stored session data in cache using %s",
734 sess_cache_opts & SESS_CACHE_OPT_USE_JSON ? "JSON" : "TPL");
735 return 0;
736 }
737
738 static int sess_cache_open(tls_sess_cache_t *cache, char *info, long timeout) {
739 config_rec *c;
740
741 cache->cache_pool = make_sub_pool(permanent_pool);
742 pr_pool_tag(cache->cache_pool, MOD_TLS_MEMCACHE_VERSION);
743
744 pr_trace_msg(trace_channel, 9, "opening memcache cache %p (info '%s')",
745 cache, info ? info : "(none)");
746
747 /* This is a little messy, but necessary. The mod_memcache module does
748 * not set the configured list of memcached servers until a connection
749 * arrives. But mod_tls opens its session cache prior to that, when the
750 * server is starting up. Thus we need to set the configured list of
751 * memcached servers ourselves.
752 */
753 c = find_config(main_server->conf, CONF_PARAM, "MemcacheEngine", FALSE);
754 if (c != NULL) {
755 int engine;
756
757 engine = *((int *) c->argv[0]);
758 if (engine == FALSE) {
759 pr_trace_msg(trace_channel, 2, "%s",
760 "memcache support disabled (see MemcacheEngine directive)");
761 errno = EPERM;
762 return -1;
763 }
764 }
765
766 sess_mcache = pr_memcache_conn_new(cache->cache_pool,
767 &tls_memcache_module, 0, 0);
768 if (sess_mcache == NULL) {
769 pr_trace_msg(trace_channel, 2,
770 "error connecting to memcached: %s", strerror(errno));
771 errno = EPERM;
772 return -1;
773 }
774
775 /* Configure a namespace prefix for our memcached keys. */
776 if (pr_memcache_conn_set_namespace(sess_mcache, &tls_memcache_module,
777 "mod_tls_memcache.sessions.") < 0) {
778 pr_trace_msg(trace_channel, 2,
779 "error setting memcache namespace prefix: %s", strerror(errno));
780 }
781
782 cache->cache_timeout = timeout;
783
784 if (info != NULL &&
785 strcasecmp(info, "/json") == 0) {
786 sess_cache_opts |= SESS_CACHE_OPT_USE_JSON;
787 }
788
789 return 0;
790 }
791
792 static int sess_cache_close(tls_sess_cache_t *cache) {
793 pr_trace_msg(trace_channel, 9, "closing memcache session cache %p", cache);
794
795 if (cache != NULL &&
796 cache->cache_pool != NULL) {
797
798 /* We do NOT destroy the cache_pool here or close the mcache connection;
799 * both were created at daemon startup, and should live as long as
800 * the daemon lives.
801 */
802
803 if (sesscache_sess_list != NULL) {
804 register unsigned int i;
805 struct sesscache_large_entry *entries;
806
807 entries = sesscache_sess_list->elts;
808 for (i = 0; i < sesscache_sess_list->nelts; i++) {
809 struct sesscache_large_entry *entry;
810
811 entry = &(entries[i]);
812 if (entry->expires > 0) {
813 pr_memscrub((void *) entry->sess_data, entry->sess_datalen);
814 }
815 }
816
817 clear_array(sesscache_sess_list);
818 }
819 }
820
821 return 0;
822 }
823
824 static int sess_cache_add_large_sess(tls_sess_cache_t *cache,
825 const unsigned char *sess_id, unsigned int sess_id_len, time_t expires,
826 SSL_SESSION *sess, int sess_len) {
827 struct sesscache_large_entry *entry = NULL;
828
829 if (sess_len > TLS_MAX_SSL_SESSION_SIZE) {
830 const char *exceeds_key = sesscache_keys[SESSCACHE_KEY_EXCEEDS].key,
831 *max_len_key = sesscache_keys[SESSCACHE_KEY_MAX_LEN].key;
832 void *value = NULL;
833 size_t valuesz = 0;
834
835 if (pr_memcache_incr(sess_mcache, &tls_memcache_module, exceeds_key,
836 1, NULL) < 0) {
837 pr_trace_msg(trace_channel, 2,
838 "error incrementing '%s' value: %s", exceeds_key, strerror(errno));
839 }
840
841 /* XXX Yes, this is subject to race conditions; other proftpd servers
842 * might also be modifying this value in memcached. Oh well.
843 */
844
845 value = pr_memcache_get(sess_mcache, &tls_memcache_module, max_len_key,
846 &valuesz, NULL);
847 if (value != NULL) {
848 uint64_t max_len;
849
850 memcpy(&max_len, value, valuesz);
851 if ((uint64_t) sess_len > max_len) {
852 if (pr_memcache_set(sess_mcache, &tls_memcache_module, max_len_key,
853 &max_len, sizeof(max_len), 0, 0) < 0) {
854 pr_trace_msg(trace_channel, 2,
855 "error setting '%s' value: %s", max_len_key, strerror(errno));
856 }
857 }
858
859 } else {
860 pr_trace_msg(trace_channel, 2,
861 "error getting '%s' value: %s", max_len_key, strerror(errno));
862 }
863 }
864
865 if (sesscache_sess_list != NULL) {
866 register unsigned int i;
867 struct sesscache_large_entry *entries;
868 time_t now;
869 int ok = FALSE;
870
871 /* Look for any expired sessions in the list to overwrite/reuse. */
872 entries = sesscache_sess_list->elts;
873 time(&now);
874 for (i = 0; i < sesscache_sess_list->nelts; i++) {
875 entry = &(entries[i]);
876
877 if (entry->expires <= now) {
878 /* This entry has expired; clear and reuse its slot. */
879 entry->expires = 0;
880 pr_memscrub((void *) entry->sess_data, entry->sess_datalen);
881
882 ok = TRUE;
883 break;
884 }
885 }
886
887 if (!ok) {
888 /* We didn't find an open slot in the list. Need to add one. */
889 entry = push_array(sesscache_sess_list);
890 }
891
892 } else {
893 sesscache_sess_list = make_array(cache->cache_pool, 1,
894 sizeof(struct sesscache_large_entry));
895 entry = push_array(sesscache_sess_list);
896 }
897
898 entry->expires = expires;
899 entry->sess_id_len = sess_id_len;
900 entry->sess_id = palloc(cache->cache_pool, sess_id_len);
901 memcpy((unsigned char *) entry->sess_id, sess_id, sess_id_len);
902 entry->sess_datalen = sess_len;
903 entry->sess_data = palloc(cache->cache_pool, sess_len);
904 i2d_SSL_SESSION(sess, (unsigned char **) &(entry->sess_data));
905
906 return 0;
907 }
908
909 static int sess_cache_add(tls_sess_cache_t *cache, const unsigned char *sess_id,
910 unsigned int sess_id_len, time_t expires, SSL_SESSION *sess) {
911 struct sesscache_entry entry;
912 int sess_len;
913 unsigned char *ptr;
914 time_t now;
915
916 time(&now);
917 pr_trace_msg(trace_channel, 9,
918 "adding session to memcache cache %p (expires = %lu, now = %lu)", cache,
919 (unsigned long) expires, (unsigned long) now);
920
921 /* First we need to find out how much space is needed for the serialized
922 * session data. There is no known maximum size for SSL session data;
923 * this module is currently designed to allow only up to a certain size.
924 */
925 sess_len = i2d_SSL_SESSION(sess, NULL);
926 if (sess_len > TLS_MAX_SSL_SESSION_SIZE) {
927 pr_trace_msg(trace_channel, 2,
928 "length of serialized SSL session data (%d) exceeds maximum size (%u), "
929 "unable to add to shared memcache, adding to list", sess_len,
930 TLS_MAX_SSL_SESSION_SIZE);
931
932 /* Instead of rejecting the add here, we add the session to a "large
933 * session" list. Thus the large session would still be cached per process
934 * and will not be lost.
935 */
936
937 return sess_cache_add_large_sess(cache, sess_id, sess_id_len, expires,
938 sess, sess_len);
939 }
940
941 entry.expires = expires;
942 entry.sess_datalen = sess_len;
943 ptr = entry.sess_data;
944 i2d_SSL_SESSION(sess, &ptr);
945
946 if (sess_cache_mcache_entry_set(cache->cache_pool, sess_id, sess_id_len,
947 &entry) < 0) {
948 pr_trace_msg(trace_channel, 2,
949 "error adding session to memcache: %s", strerror(errno));
950
951 /* Add this session to the "large session" list instead as a fallback. */
952 return sess_cache_add_large_sess(cache, sess_id, sess_id_len, expires,
953 sess, sess_len);
954
955 } else {
956 const char *key = sesscache_keys[SESSCACHE_KEY_STORES].key;
957
958 if (pr_memcache_incr(sess_mcache, &tls_memcache_module, key, 1, NULL) < 0) {
959 pr_trace_msg(trace_channel, 2,
960 "error incrementing '%s' value: %s", key, strerror(errno));
961 }
962 }
963
964 return 0;
965 }
966
967 static SSL_SESSION *sess_cache_get(tls_sess_cache_t *cache,
968 const unsigned char *sess_id, unsigned int sess_id_len) {
969 struct sesscache_entry entry;
970 time_t now;
971 SSL_SESSION *sess = NULL;
972
973 pr_trace_msg(trace_channel, 9, "getting session from memcache cache %p",
974 cache);
975
976 /* Look for the requested session in the "large session" list first. */
977 if (sesscache_sess_list != NULL) {
978 register unsigned int i;
979 struct sesscache_large_entry *entries;
980
981 entries = sesscache_sess_list->elts;
982 for (i = 0; i < sesscache_sess_list->nelts; i++) {
983 struct sesscache_large_entry *large_entry;
984
985 large_entry = &(entries[i]);
986 if (large_entry->expires > 0 &&
987 large_entry->sess_id_len == sess_id_len &&
988 memcmp(large_entry->sess_id, sess_id,
989 large_entry->sess_id_len) == 0) {
990
991 now = time(NULL);
992 if (large_entry->expires > now) {
993 TLS_D2I_SSL_SESSION_CONST unsigned char *ptr;
994
995 ptr = large_entry->sess_data;
996 sess = d2i_SSL_SESSION(NULL, &ptr, large_entry->sess_datalen);
997 if (sess == NULL) {
998 pr_trace_msg(trace_channel, 2,
999 "error retrieving session from cache: %s", mcache_get_errors());
1000
1001 } else {
1002 break;
1003 }
1004 }
1005 }
1006 }
1007 }
1008
1009 if (sess != NULL) {
1010 return sess;
1011 }
1012
1013 if (sess_cache_mcache_entry_get(cache->cache_pool, sess_id, sess_id_len,
1014 &entry) < 0) {
1015 return NULL;
1016 }
1017
1018 now = time(NULL);
1019 if (entry.expires > now) {
1020 TLS_D2I_SSL_SESSION_CONST unsigned char *ptr;
1021
1022 ptr = entry.sess_data;
1023 sess = d2i_SSL_SESSION(NULL, &ptr, entry.sess_datalen);
1024 if (sess != NULL) {
1025 const char *key = sesscache_keys[SESSCACHE_KEY_HITS].key;
1026
1027 if (pr_memcache_incr(sess_mcache, &tls_memcache_module, key, 1,
1028 NULL) < 0) {
1029 pr_trace_msg(trace_channel, 2,
1030 "error incrementing '%s' value: %s", key, strerror(errno));
1031 }
1032
1033 } else {
1034 const char *key = sesscache_keys[SESSCACHE_KEY_ERRORS].key;
1035
1036 pr_trace_msg(trace_channel, 2,
1037 "error retrieving session from cache: %s", mcache_get_errors());
1038
1039 if (pr_memcache_incr(sess_mcache, &tls_memcache_module, key, 1,
1040 NULL) < 0) {
1041 pr_trace_msg(trace_channel, 2,
1042 "error incrementing '%s' value: %s", key, strerror(errno));
1043 }
1044 }
1045 }
1046
1047 if (sess == NULL) {
1048 const char *key = sesscache_keys[SESSCACHE_KEY_MISSES].key;
1049
1050 if (pr_memcache_incr(sess_mcache, &tls_memcache_module, key, 1, NULL) < 0) {
1051 pr_trace_msg(trace_channel, 2,
1052 "error incrementing '%s' value: %s", key, strerror(errno));
1053 }
1054
1055 errno = ENOENT;
1056 }
1057
1058 return sess;
1059 }
1060
1061 static int sess_cache_delete(tls_sess_cache_t *cache,
1062 const unsigned char *sess_id, unsigned int sess_id_len) {
1063 const char *key = sesscache_keys[SESSCACHE_KEY_DELETES].key;
1064 int res;
1065
1066 pr_trace_msg(trace_channel, 9, "removing session from memcache cache %p",
1067 cache);
1068
1069 /* Look for the requested session in the "large session" list first. */
1070 if (sesscache_sess_list != NULL) {
1071 register unsigned int i;
1072 struct sesscache_large_entry *entries;
1073
1074 entries = sesscache_sess_list->elts;
1075 for (i = 0; i < sesscache_sess_list->nelts; i++) {
1076 struct sesscache_large_entry *entry;
1077
1078 entry = &(entries[i]);
1079 if (entry->sess_id_len == sess_id_len &&
1080 memcmp(entry->sess_id, sess_id, entry->sess_id_len) == 0) {
1081
1082 pr_memscrub((void *) entry->sess_data, entry->sess_datalen);
1083 entry->expires = 0;
1084 return 0;
1085 }
1086 }
1087 }
1088
1089 res = sess_cache_mcache_entry_delete(cache->cache_pool, sess_id, sess_id_len);
1090 if (res < 0) {
1091 return -1;
1092 }
1093
1094 /* Don't forget to update the stats. */
1095
1096 if (pr_memcache_incr(sess_mcache, &tls_memcache_module, key, 1, NULL) < 0) {
1097 pr_trace_msg(trace_channel, 2,
1098 "error incrementing '%s' value: %s", key, strerror(errno));
1099 }
1100
1101 return res;
1102 }
1103
1104 static int sess_cache_clear(tls_sess_cache_t *cache) {
1105 register unsigned int i;
1106 int res = 0;
1107
1108 if (sess_mcache == NULL) {
1109 pr_trace_msg(trace_channel, 9, "missing required memcached connection");
1110 errno = EINVAL;
1111 return -1;
1112 }
1113
1114 pr_trace_msg(trace_channel, 9, "clearing memcache session cache %p", cache);
1115
1116 if (sesscache_sess_list != NULL) {
1117 struct sesscache_large_entry *entries;
1118
1119 entries = sesscache_sess_list->elts;
1120 for (i = 0; i < sesscache_sess_list->nelts; i++) {
1121 struct sesscache_large_entry *entry;
1122
1123 entry = &(entries[i]);
1124 entry->expires = 0;
1125 pr_memscrub((void *) entry->sess_data, entry->sess_datalen);
1126 }
1127 }
1128
1129 /* XXX iterate through keys, kremoving any "mod_tls_memcache" prefixed keys */
1130
1131 return res;
1132 }
1133
1134 static int sess_cache_remove(tls_sess_cache_t *cache) {
1135 int res;
1136
1137 pr_trace_msg(trace_channel, 9, "removing memcache session cache %p", cache);
1138
1139 res = sess_cache_clear(cache);
1140 /* XXX close memcache conn */
1141
1142 return res;
1143 }
1144
1145 static int sess_cache_status(tls_sess_cache_t *cache,
1146 void (*statusf)(void *, const char *, ...), void *arg, int flags) {
1147 register unsigned int i;
1148 pool *tmp_pool;
1149
1150 pr_trace_msg(trace_channel, 9, "checking memcache session cache %p", cache);
1151
1152 tmp_pool = make_sub_pool(permanent_pool);
1153
1154 statusf(arg, "%s", "Memcache SSL session cache provided by "
1155 MOD_TLS_MEMCACHE_VERSION);
1156 statusf(arg, "%s", "");
1157 statusf(arg, "Memcache servers: ");
1158
1159 for (i = 0; sesscache_keys[i].key != NULL; i++) {
1160 const char *key, *desc;
1161 void *value = NULL;
1162 size_t valuesz = 0;
1163 uint32_t stat_flags = 0;
1164
1165 key = sesscache_keys[i].key;
1166 desc = sesscache_keys[i].desc;
1167
1168 value = pr_memcache_get(sess_mcache, &tls_memcache_module, key, &valuesz,
1169 &stat_flags);
1170 if (value != NULL) {
1171 uint64_t num = 0;
1172 memcpy(&num, value, valuesz);
1173 statusf(arg, "%s: %lu", desc, (unsigned long) num);
1174 }
1175 }
1176
1177 /* XXX run stats on memcached servers? */
1178
1179 #if 0
1180 if (flags & TLS_SESS_CACHE_STATUS_FL_SHOW_SESSIONS) {
1181 statusf(arg, "%s", "");
1182 statusf(arg, "%s", "Cached sessions:");
1183
1184 /* XXX Get keys, looking for our namespace prefix, dump each one */
1185
1186 /* We _could_ use SSL_SESSION_print(), which is what the sess_id
1187 * command-line tool does. The problem is that SSL_SESSION_print() shows
1188 * too much (particularly, it shows the master secret). And
1189 * SSL_SESSION_print() does not support a flags argument to use for
1190 * specifying which bits of the session we want to print.
1191 *
1192 * Instead, we get to do the more dangerous (compatibility-wise) approach
1193 * of rolling our own printing function.
1194 */
1195
1196 for (i = 0; i < 0; i++) {
1197 struct sesscache_entry *entry;
1198
1199 pr_signals_handle();
1200
1201 /* XXX Get entries */
1202 if (entry->expires > 0) {
1203 SSL_SESSION *sess;
1204 TLS_D2I_SSL_SESSION_CONST unsigned char *ptr;
1205 time_t ts;
1206
1207 ptr = entry->sess_data;
1208 sess = d2i_SSL_SESSION(NULL, &ptr, entry->sess_datalen);
1209 if (sess == NULL) {
1210 pr_log_pri(PR_LOG_NOTICE, MOD_TLS_MEMCACHE_VERSION
1211 ": error retrieving session from cache: %s", mcache_get_errors());
1212 continue;
1213 }
1214
1215 statusf(arg, "%s", " -----BEGIN SSL SESSION PARAMETERS-----");
1216
1217 /* XXX Directly accessing these fields cannot be a Good Thing. */
1218 if (sess->session_id_length > 0) {
1219 char *sess_id_str;
1220
1221 sess_id_str = pr_str2hex(tmp_pool, sess->session_id,
1222 sess->session_id_length, PR_STR_FL_HEX_USE_UC);
1223
1224 statusf(arg, " Session ID: %s", sess_id_str);
1225 }
1226
1227 if (sess->sid_ctx_length > 0) {
1228 char *sid_ctx_str;
1229
1230 sid_ctx_str = pr_str2hex(tmp_pool, sess->sid_ctx,
1231 sess->sid_ctx_length, PR_STR_FL_HEX_USE_UC);
1232
1233 statusf(arg, " Session ID Context: %s", sid_ctx_str);
1234 }
1235
1236 switch (sess->ssl_version) {
1237 case SSL3_VERSION:
1238 statusf(arg, " Protocol: %s", "SSLv3");
1239 break;
1240
1241 case TLS1_VERSION:
1242 statusf(arg, " Protocol: %s", "TLSv1");
1243 break;
1244
1245 #if defined(TLS1_1_VERSION)
1246 case TLS1_1_VERSION:
1247 statusf(arg, " Protocol: %s", "TLSv1.1");
1248 break;
1249 #endif /* TLS1_1_VERSION */
1250
1251 #if defined(TLS1_2_VERSION)
1252 case TLS1_2_VERSION:
1253 statusf(arg, " Protocol: %s", "TLSv1.2");
1254 break;
1255 #endif /* TLS1_2_VERSION */
1256
1257 #if defined(TLS1_3_VERSION)
1258 case TLS1_3_VERSION:
1259 statusf(arg, " Protocol: %s", "TLSv1.3");
1260 break;
1261 #endif /* TLS1_3_VERSION */
1262
1263 default:
1264 statusf(arg, " Protocol: %s", "unknown");
1265 }
1266
1267 ts = SSL_SESSION_get_time(sess);
1268 statusf(arg, " Started: %s", pr_strtime3(tmp_pool, ts, FALSE));
1269 ts = entry->expires;
1270 statusf(arg, " Expires: %s (%u secs)",
1271 pr_strtime3(tmp_pool, ts, FALSE), SSL_SESSION_get_timeout(sess));
1272
1273 SSL_SESSION_free(sess);
1274 statusf(arg, "%s", " -----END SSL SESSION PARAMETERS-----");
1275 statusf(arg, "%s", "");
1276 }
1277 }
1278 }
1279 #endif
1280
1281 destroy_pool(tmp_pool);
1282 return 0;
1283 }
1284
1285 #if defined(PR_USE_OPENSSL_OCSP)
1286 /* OCSP response cache implementation callbacks.
1287 */
1288
1289 /* Functions for marshalling key/value data to/from memcached. */
1290
1291 static int ocsp_cache_get_json_key(pool *p, const char *fingerprint,
1292 void **key, size_t *keysz) {
1293 pr_json_object_t *json;
1294 char *json_text;
1295
1296 json = pr_json_object_alloc(p);
1297 (void) pr_json_object_set_string(p, json, "fingerprint", fingerprint);
1298
1299 json_text = pr_json_object_to_text(p, json, "");
1300 (void) pr_json_object_free(json);
1301
1302 /* Include the terminating NUL in the key. */
1303 *keysz = strlen(json_text) + 1;
1304 *key = pstrndup(p, json_text, *keysz - 1);
1305
1306 return 0;
1307 }
1308
1309 static int ocsp_cache_get_key(pool *p, const char *fingerprint, void **key,
1310 size_t *keysz) {
1311 int res;
1312
1313 res = ocsp_cache_get_json_key(p, fingerprint, key, keysz);
1314 if (res < 0) {
1315 pr_trace_msg(trace_channel, 3,
1316 "error constructing ocsp cache JSON lookup key for fingerprint '%s'",
1317 fingerprint);
1318 return -1;
1319 }
1320
1321 return 0;
1322 }
1323
1324 static int ocsp_cache_entry_decode_json(pool *p, void *value, size_t valuesz,
1325 struct ocspcache_entry *oe) {
1326 int res;
1327 pr_json_object_t *json;
1328 const char *key;
1329 char *entry, *text;
1330 double number = 0;
1331
1332 entry = value;
1333 if (pr_json_text_validate(p, entry) == FALSE) {
1334 tls_log(MOD_TLS_MEMCACHE_VERSION
1335 ": unable to decode invalid JSON ocsp cache entry: '%s'", entry);
1336 errno = EINVAL;
1337 return -1;
1338 }
1339
1340 json = pr_json_object_from_text(p, entry);
1341 if (json == NULL) {
1342 return -1;
1343 }
1344
1345 key = OCSP_CACHE_JSON_KEY_AGE;
1346 res = entry_get_json_number(p, json, key, &number, entry);
1347 if (res < 0) {
1348 int xerrno = errno;
1349
1350 (void) pr_json_object_free(json);
1351 errno = xerrno;
1352 return -1;
1353 }
1354 oe->age = (uint32_t) number;
1355
1356 key = OCSP_CACHE_JSON_KEY_RESPONSE;
1357 res = entry_get_json_string(p, json, key, &text, entry);
1358 if (res == 0) {
1359 pool *tmp_pool = NULL;
1360 int have_padding = FALSE;
1361 char *base64_data;
1362 size_t base64_datalen;
1363 unsigned char *data;
1364
1365 tmp_pool = make_sub_pool(p);
1366 pr_pool_tag(tmp_pool, "TLS Memcache OCSP cache base64 data pool");
1367
1368 base64_data = text;
1369 base64_datalen = strlen(base64_data);
1370
1371 /* Due to Base64's padding, we need to detect if the last block was
1372 * padded with zeros; we do this by looking for '=' characters at the
1373 * end of the text being decoded. If we see these characters, then we
1374 * will "trim" off any trailing zero values in the decoded data, on the
1375 * ASSUMPTION that they are the auto-added padding bytes.
1376 */
1377 if (base64_data[base64_datalen-1] == '=') {
1378 have_padding = TRUE;
1379 }
1380
1381 data = palloc(tmp_pool, base64_datalen);
1382 res = EVP_DecodeBlock(data, (unsigned char *) base64_data,
1383 (int) base64_datalen);
1384 if (res <= 0) {
1385 /* Base64-decoding error. */
1386 pr_trace_msg(trace_channel, 5,
1387 "error base64-decoding OCSP data in '%s', rejecting", entry);
1388 (void) pr_json_object_free(json);
1389
1390 destroy_pool(tmp_pool);
1391 errno = EINVAL;
1392 return -1;
1393 }
1394
1395 if (have_padding == TRUE) {
1396 /* Assume that only one or two zero bytes of padding were added. */
1397 if (data[res-1] == '\0') {
1398 res -= 1;
1399
1400 if (data[res-1] == '\0') {
1401 res -= 1;
1402 }
1403 }
1404 }
1405
1406 if (res <= TLS_MAX_OCSP_RESPONSE_SIZE) {
1407 /* Base64-decoded data will fit into our buffer as expected. */
1408 memcpy(oe->resp_der, data, res);
1409
1410 } else {
1411 tls_log(MOD_TLS_MEMCACHE_VERSION
1412 ": decoded JSON OCSP cache entry too large, ignoring");
1413 (void) pr_json_object_free(json);
1414
1415 destroy_pool(tmp_pool);
1416 errno = EINVAL;
1417 return -1;
1418 }
1419
1420 destroy_pool(tmp_pool);
1421
1422 } else {
1423 int xerrno = errno;
1424
1425 (void) pr_json_object_free(json);
1426 errno = xerrno;
1427 return -1;
1428 }
1429
1430 key = OCSP_CACHE_JSON_KEY_RESPONSE_LENGTH;
1431 res = entry_get_json_number(p, json, key, &number, entry);
1432 if (res < 0) {
1433 int xerrno = errno;
1434
1435 (void) pr_json_object_free(json);
1436 errno = xerrno;
1437 return -1;
1438 }
1439 oe->resp_derlen = (unsigned int) number;
1440
1441 (void) pr_json_object_free(json);
1442 return 0;
1443 }
1444
1445 static int ocsp_cache_mcache_entry_get(pool *p, const char *fingerprint,
1446 struct ocspcache_entry *oe) {
1447 int res;
1448 void *key = NULL, *value = NULL;
1449 size_t keysz = 0, valuesz = 0;
1450 uint32_t flags = 0;
1451
1452 res = ocsp_cache_get_key(p, fingerprint, &key, &keysz);
1453 if (res < 0) {
1454 pr_trace_msg(trace_channel, 1,
1455 "unable to get ocsp cache entry: error getting cache key: %s",
1456 strerror(errno));
1457
1458 return -1;
1459 }
1460
1461 value = pr_memcache_kget(ocsp_mcache, &tls_memcache_module,
1462 (const char *) key, keysz, &valuesz, &flags);
1463 if (value == NULL) {
1464 pr_trace_msg(trace_channel, 3,
1465 "no matching memcache entry found for fingerprint '%s'", fingerprint);
1466 errno = ENOENT;
1467 return -1;
1468 }
1469
1470 /* Decode the cached response data. */
1471 res = ocsp_cache_entry_decode_json(p, value, valuesz, oe);
1472 if (res == 0) {
1473 pr_trace_msg(trace_channel, 9,
1474 "retrieved response data from cache using JSON");
1475 }
1476
1477 return 0;
1478 }
1479
1480 static int ocsp_cache_mcache_entry_delete(pool *p, const char *fingerprint) {
1481 int res;
1482 void *key = NULL;
1483 size_t keysz = 0;
1484
1485 res = ocsp_cache_get_key(p, fingerprint, &key, &keysz);
1486 if (res < 0) {
1487 pr_trace_msg(trace_channel, 1,
1488 "unable to remove ocsp cache entry: error getting cache key: %s",
1489 strerror(errno));
1490
1491 return -1;
1492 }
1493
1494 res = pr_memcache_kremove(ocsp_mcache, &tls_memcache_module,
1495 (const char *) key, keysz, 0);
1496 if (res < 0) {
1497 int xerrno = errno;
1498
1499 pr_trace_msg(trace_channel, 2,
1500 "unable to remove memcache entry for fingerpring '%s': %s", fingerprint,
1501 strerror(xerrno));
1502
1503 errno = xerrno;
1504 return -1;
1505 }
1506
1507 return 0;
1508 }
1509
1510 static int ocsp_cache_entry_encode_json(pool *p, void **value, size_t *valuesz,
1511 struct ocspcache_entry *oe) {
1512 pr_json_object_t *json;
1513 pool *tmp_pool;
1514 char *base64_data = NULL, *json_text;
1515
1516 json = pr_json_object_alloc(p);
1517 (void) pr_json_object_set_number(p, json, OCSP_CACHE_JSON_KEY_AGE,
1518 (double) oe->age);
1519
1520 /* Base64-encode the response data. Note that EVP_EncodeBlock does
1521 * NUL-terminate the encoded data.
1522 */
1523 tmp_pool = make_sub_pool(p);
1524 base64_data = pcalloc(tmp_pool, (oe->resp_derlen * 2) + 1);
1525
1526 EVP_EncodeBlock((unsigned char *) base64_data, oe->resp_der,
1527 (int) oe->resp_derlen);
1528 (void) pr_json_object_set_string(p, json, OCSP_CACHE_JSON_KEY_RESPONSE,
1529 base64_data);
1530 (void) pr_json_object_set_number(p, json, OCSP_CACHE_JSON_KEY_RESPONSE_LENGTH,
1531 (double) oe->resp_derlen);
1532 destroy_pool(tmp_pool);
1533
1534 json_text = pr_json_object_to_text(p, json, "");
1535 (void) pr_json_object_free(json);
1536
1537 /* Safety check */
1538 if (pr_json_text_validate(p, json_text) == FALSE) {
1539 pr_trace_msg(trace_channel, 1, "invalid JSON emitted: '%s'", json_text);
1540 errno = EINVAL;
1541 return -1;
1542 }
1543
1544 /* Include the terminating NUL in the value. */
1545 *valuesz = strlen(json_text) + 1;
1546 *value = pstrndup(p, json_text, *valuesz - 1);
1547
1548 return 0;
1549 }
1550
1551 static int ocsp_cache_mcache_entry_set(pool *p, const char *fingerprint,
1552 struct ocspcache_entry *oe) {
1553 int res, xerrno = 0;
1554 void *key = NULL, *value = NULL;
1555 size_t keysz = 0, valuesz = 0;
1556 uint32_t flags = 0;
1557
1558 /* Encode the OCSP response data. */
1559 res = ocsp_cache_entry_encode_json(p, &value, &valuesz, oe);
1560 if (res < 0) {
1561 xerrno = errno;
1562
1563 pr_trace_msg(trace_channel, 4, "error JSON encoding OCSP response data: %s",
1564 strerror(xerrno));
1565
1566 errno = xerrno;
1567 return -1;
1568 }
1569
1570 res = ocsp_cache_get_key(p, fingerprint, &key, &keysz);
1571 xerrno = errno;
1572 if (res < 0) {
1573 pr_trace_msg(trace_channel, 1,
1574 "unable to set ocsp cache entry: error getting cache key: %s",
1575 strerror(xerrno));
1576
1577 errno = xerrno;
1578 return -1;
1579 }
1580
1581 res = pr_memcache_kset(ocsp_mcache, &tls_memcache_module, (const char *) key,
1582 keysz, value, valuesz, 0, flags);
1583 xerrno = errno;
1584
1585 if (res < 0) {
1586 pr_trace_msg(trace_channel, 2,
1587 "unable to add memcache entry for fingerprint '%s': %s", fingerprint,
1588 strerror(xerrno));
1589
1590 errno = xerrno;
1591 return -1;
1592 }
1593
1594 pr_trace_msg(trace_channel, 9,
1595 "stored OCSP response data in cache using JSON");
1596 return 0;
1597 }
1598
1599 static int ocsp_cache_open(tls_ocsp_cache_t *cache, char *info) {
1600 config_rec *c;
1601
1602 pr_trace_msg(trace_channel, 9, "opening memcache cache %p (info '%s')",
1603 cache, info ? info : "(none)");
1604
1605 cache->cache_pool = make_sub_pool(permanent_pool);
1606 pr_pool_tag(cache->cache_pool, MOD_TLS_MEMCACHE_VERSION);
1607
1608 /* This is a little messy, but necessary. The mod_memcache module does
1609 * not set the configured list of memcached servers until a connection
1610 * arrives. But mod_tls opens its session cache prior to that, when the
1611 * server is starting up. Thus we need to set the configured list of
1612 * memcached servers ourselves.
1613 */
1614 c = find_config(main_server->conf, CONF_PARAM, "MemcacheEngine", FALSE);
1615 if (c != NULL) {
1616 int engine;
1617
1618 engine = *((int *) c->argv[0]);
1619 if (engine == FALSE) {
1620 pr_trace_msg(trace_channel, 2, "%s",
1621 "memcache support disabled (see MemcacheEngine directive)");
1622 errno = EPERM;
1623 return -1;
1624 }
1625 }
1626
1627 ocsp_mcache = pr_memcache_conn_new(cache->cache_pool,
1628 &tls_memcache_module, 0, 0);
1629 if (ocsp_mcache == NULL) {
1630 pr_trace_msg(trace_channel, 2,
1631 "error connecting to memcached: %s", strerror(errno));
1632 errno = EPERM;
1633 return -1;
1634 }
1635
1636 /* Configure a namespace prefix for our memcached keys. */
1637 if (pr_memcache_conn_set_namespace(ocsp_mcache, &tls_memcache_module,
1638 "mod_tls_memcache.ocsp.") < 0) {
1639 pr_trace_msg(trace_channel, 2,
1640 "error setting memcache namespace prefix: %s", strerror(errno));
1641 }
1642
1643 return 0;
1644 }
1645
1646 static int ocsp_cache_close(tls_ocsp_cache_t *cache) {
1647 pr_trace_msg(trace_channel, 9, "closing memcache ocsp cache %p", cache);
1648
1649 if (cache != NULL &&
1650 cache->cache_pool != NULL) {
1651
1652 /* We do NOT destroy the cache_pool here or close the mcache connection;
1653 * both were created at daemon startup, and should live as long as
1654 * the daemon lives.
1655 */
1656
1657 if (ocspcache_resp_list != NULL) {
1658 register unsigned int i;
1659 struct ocspcache_large_entry *entries;
1660
1661 entries = ocspcache_resp_list->elts;
1662 for (i = 0; i < ocspcache_resp_list->nelts; i++) {
1663 struct ocspcache_large_entry *entry;
1664
1665 entry = &(entries[i]);
1666 pr_memscrub(entry->resp_der, entry->resp_derlen);
1667 entry->resp_derlen = 0;
1668 pr_memscrub(entry->fingerprint, entry->fingerprint_len);
1669 entry->fingerprint_len = 0;
1670 entry->age = 0;
1671 }
1672
1673 clear_array(ocspcache_resp_list);
1674 }
1675 }
1676
1677 return 0;
1678 }
1679
1680 static int ocsp_cache_add_large_resp(tls_ocsp_cache_t *cache,
1681 const char *fingerprint, OCSP_RESPONSE *resp, time_t resp_age) {
1682 struct ocspcache_large_entry *entry = NULL;
1683 int resp_derlen;
1684 unsigned char *ptr;
1685
1686 resp_derlen = i2d_OCSP_RESPONSE(resp, NULL);
1687 if (resp_derlen > TLS_MAX_OCSP_RESPONSE_SIZE) {
1688 const char *exceeds_key = ocspcache_keys[OCSPCACHE_KEY_EXCEEDS].key,
1689 *max_len_key = ocspcache_keys[OCSPCACHE_KEY_MAX_LEN].key;
1690 void *value = NULL;
1691 size_t valuesz = 0;
1692
1693 if (pr_memcache_incr(ocsp_mcache, &tls_memcache_module, exceeds_key,
1694 1, NULL) < 0) {
1695 pr_trace_msg(trace_channel, 2,
1696 "error incrementing '%s' value: %s", exceeds_key, strerror(errno));
1697 }
1698
1699 /* XXX Yes, this is subject to race conditions; other proftpd servers
1700 * might also be modifying this value in memcached. Oh well.
1701 */
1702
1703 value = pr_memcache_get(ocsp_mcache, &tls_memcache_module, max_len_key,
1704 &valuesz, NULL);
1705 if (value != NULL) {
1706 uint64_t max_len;
1707
1708 memcpy(&max_len, value, valuesz);
1709 if ((uint64_t) resp_derlen > max_len) {
1710 if (pr_memcache_set(ocsp_mcache, &tls_memcache_module, max_len_key,
1711 &max_len, sizeof(max_len), 0, 0) < 0) {
1712 pr_trace_msg(trace_channel, 2,
1713 "error setting '%s' value: %s", max_len_key, strerror(errno));
1714 }
1715 }
1716
1717 } else {
1718 pr_trace_msg(trace_channel, 2,
1719 "error getting '%s' value: %s", max_len_key, strerror(errno));
1720 }
1721 }
1722
1723 if (ocspcache_resp_list != NULL) {
1724 register unsigned int i;
1725 struct ocspcache_large_entry *entries;
1726 time_t now;
1727 int ok = FALSE;
1728
1729 /* Look for any expired sessions in the list to overwrite/reuse. */
1730 entries = ocspcache_resp_list->elts;
1731 time(&now);
1732 for (i = 0; i < ocspcache_resp_list->nelts; i++) {
1733 entry = &(entries[i]);
1734
1735 if (entry->age > (now - 3600)) {
1736 /* This entry has expired; clear and reuse its slot. */
1737 entry->age = 0;
1738 pr_memscrub(entry->resp_der, entry->resp_derlen);
1739 entry->resp_derlen = 0;
1740 pr_memscrub(entry->fingerprint, entry->fingerprint_len);
1741 entry->fingerprint_len = 0;
1742
1743 ok = TRUE;
1744 break;
1745 }
1746 }
1747
1748 if (!ok) {
1749 /* We didn't find an open slot in the list. Need to add one. */
1750 entry = push_array(ocspcache_resp_list);
1751 }
1752
1753 } else {
1754 ocspcache_resp_list = make_array(cache->cache_pool, 1,
1755 sizeof(struct ocspcache_large_entry));
1756 entry = push_array(ocspcache_resp_list);
1757 }
1758
1759 entry->age = resp_age;
1760 entry->fingerprint_len = strlen(fingerprint);
1761 entry->fingerprint = pstrdup(cache->cache_pool, fingerprint);
1762 entry->resp_derlen = resp_derlen;
1763 entry->resp_der = ptr = palloc(cache->cache_pool, resp_derlen);
1764 i2d_OCSP_RESPONSE(resp, &ptr);
1765
1766 return 0;
1767 }
1768
1769 static int ocsp_cache_add(tls_ocsp_cache_t *cache, const char *fingerprint,
1770 OCSP_RESPONSE *resp, time_t resp_age) {
1771 struct ocspcache_entry entry;
1772 int resp_derlen;
1773 unsigned char *ptr;
1774
1775 pr_trace_msg(trace_channel, 9,
1776 "adding response to memcache ocsp cache %p", cache);
1777
1778 /* First we need to find out how much space is needed for the serialized
1779 * response data. There is no known maximum size for OCSP response data;
1780 * this module is currently designed to allow only up to a certain size.
1781 */
1782 resp_derlen = i2d_OCSP_RESPONSE(resp, NULL);
1783 if (resp_derlen > TLS_MAX_OCSP_RESPONSE_SIZE) {
1784 pr_trace_msg(trace_channel, 2,
1785 "length of serialized OCSP response data (%d) exceeds maximum size (%u), "
1786 "unable to add to shared memcache, adding to list", resp_derlen,
1787 TLS_MAX_OCSP_RESPONSE_SIZE);
1788
1789 /* Instead of rejecting the add here, we add the response to a "large
1790 * response" list. Thus the large response would still be cached per
1791 * process and will not be lost.
1792 */
1793
1794 return ocsp_cache_add_large_resp(cache, fingerprint, resp, resp_age);
1795 }
1796
1797 entry.age = resp_age;
1798 entry.resp_derlen = resp_derlen;
1799 ptr = entry.resp_der;
1800 i2d_OCSP_RESPONSE(resp, &ptr);
1801
1802 if (ocsp_cache_mcache_entry_set(cache->cache_pool, fingerprint, &entry) < 0) {
1803 pr_trace_msg(trace_channel, 2,
1804 "error adding response to memcache: %s", strerror(errno));
1805
1806 /* Add this response to the "large response" list instead as a fallback. */
1807 return ocsp_cache_add_large_resp(cache, fingerprint, resp, resp_age);
1808
1809 } else {
1810 const char *key = ocspcache_keys[OCSPCACHE_KEY_STORES].key;
1811
1812 if (pr_memcache_incr(ocsp_mcache, &tls_memcache_module, key, 1, NULL) < 0) {
1813 pr_trace_msg(trace_channel, 2,
1814 "error incrementing '%s' value: %s", key, strerror(errno));
1815 }
1816 }
1817
1818 return 0;
1819 }
1820
1821 static OCSP_RESPONSE *ocsp_cache_get(tls_ocsp_cache_t *cache,
1822 const char *fingerprint, time_t *resp_age) {
1823 struct ocspcache_entry entry;
1824 OCSP_RESPONSE *resp = NULL;
1825 size_t fingerprint_len;
1826 const unsigned char *ptr;
1827
1828 pr_trace_msg(trace_channel, 9, "getting response from memcache ocsp cache %p",
1829 cache);
1830
1831 fingerprint_len = strlen(fingerprint);
1832
1833 /* Look for the requested response in the "large response" list first. */
1834 if (ocspcache_resp_list != NULL) {
1835 register unsigned int i;
1836 struct ocspcache_large_entry *entries;
1837
1838 entries = ocspcache_resp_list->elts;
1839 for (i = 0; i < ocspcache_resp_list->nelts; i++) {
1840 struct ocspcache_large_entry *large_entry;
1841
1842 large_entry = &(entries[i]);
1843 if (large_entry->fingerprint_len > 0 &&
1844 large_entry->fingerprint_len == fingerprint_len &&
1845 memcmp(large_entry->fingerprint, fingerprint, fingerprint_len) == 0) {
1846 ptr = large_entry->resp_der;
1847 resp = d2i_OCSP_RESPONSE(NULL, &ptr, large_entry->resp_derlen);
1848 if (resp == NULL) {
1849 pr_trace_msg(trace_channel, 2,
1850 "error retrieving response from ocsp cache: %s",
1851 mcache_get_errors());
1852
1853 } else {
1854 *resp_age = large_entry->age;
1855 break;
1856 }
1857 }
1858 }
1859 }
1860
1861 if (resp) {
1862 return resp;
1863 }
1864
1865 if (ocsp_cache_mcache_entry_get(cache->cache_pool, fingerprint, &entry) < 0) {
1866 return NULL;
1867 }
1868
1869 ptr = entry.resp_der;
1870 resp = d2i_OCSP_RESPONSE(NULL, &ptr, entry.resp_derlen);
1871 if (resp != NULL) {
1872 const char *key = ocspcache_keys[OCSPCACHE_KEY_HITS].key;
1873
1874 *resp_age = entry.age;
1875
1876 if (pr_memcache_incr(ocsp_mcache, &tls_memcache_module, key, 1, NULL) < 0) {
1877 pr_trace_msg(trace_channel, 2,
1878 "error incrementing '%s' value: %s", key, strerror(errno));
1879 }
1880
1881 } else {
1882 const char *key = ocspcache_keys[OCSPCACHE_KEY_ERRORS].key;
1883
1884 pr_trace_msg(trace_channel, 2,
1885 "error retrieving response from ocsp cache: %s", mcache_get_errors());
1886
1887 if (pr_memcache_incr(ocsp_mcache, &tls_memcache_module, key, 1, NULL) < 0) {
1888 pr_trace_msg(trace_channel, 2,
1889 "error incrementing '%s' value: %s", key, strerror(errno));
1890 }
1891 }
1892
1893 if (resp == NULL) {
1894 const char *key = ocspcache_keys[OCSPCACHE_KEY_MISSES].key;
1895
1896 if (pr_memcache_incr(ocsp_mcache, &tls_memcache_module, key, 1, NULL) < 0) {
1897 pr_trace_msg(trace_channel, 2,
1898 "error incrementing '%s' value: %s", key, strerror(errno));
1899 }
1900
1901 errno = ENOENT;
1902 }
1903
1904 return resp;
1905 }
1906
1907 static int ocsp_cache_delete(tls_ocsp_cache_t *cache,
1908 const char *fingerprint) {
1909 const char *key = ocspcache_keys[OCSPCACHE_KEY_DELETES].key;
1910 int res;
1911 size_t fingerprint_len;
1912
1913 pr_trace_msg(trace_channel, 9,
1914 "deleting response from memcache ocsp cache %p", cache);
1915
1916 fingerprint_len = strlen(fingerprint);
1917
1918 /* Look for the requested response in the "large response" list first. */
1919 if (ocspcache_resp_list != NULL) {
1920 register unsigned int i;
1921 struct ocspcache_large_entry *entries;
1922
1923 entries = ocspcache_resp_list->elts;
1924 for (i = 0; i < ocspcache_resp_list->nelts; i++) {
1925 struct ocspcache_large_entry *entry;
1926
1927 entry = &(entries[i]);
1928 if (entry->fingerprint_len == fingerprint_len &&
1929 memcmp(entry->fingerprint, fingerprint, fingerprint_len) == 0) {
1930
1931 pr_memscrub(entry->resp_der, entry->resp_derlen);
1932 entry->resp_derlen = 0;
1933 pr_memscrub(entry->fingerprint, entry->fingerprint_len);
1934 entry->fingerprint_len = 0;
1935 entry->age = 0;
1936
1937 return 0;
1938 }
1939 }
1940 }
1941
1942 res = ocsp_cache_mcache_entry_delete(cache->cache_pool, fingerprint);
1943 if (res < 0) {
1944 return -1;
1945 }
1946
1947 /* Don't forget to update the stats. */
1948
1949 if (pr_memcache_incr(ocsp_mcache, &tls_memcache_module, key, 1, NULL) < 0) {
1950 pr_trace_msg(trace_channel, 2,
1951 "error incrementing '%s' value: %s", key, strerror(errno));
1952 }
1953
1954 return res;
1955 }
1956
1957 static int ocsp_cache_clear(tls_ocsp_cache_t *cache) {
1958 register unsigned int i;
1959 int res = 0;
1960
1961 if (ocsp_mcache == NULL) {
1962 pr_trace_msg(trace_channel, 9, "missing required memcached connection");
1963 errno = EINVAL;
1964 return -1;
1965 }
1966
1967 pr_trace_msg(trace_channel, 9, "clearing memcache ocsp cache %p", cache);
1968
1969 if (ocspcache_resp_list != NULL) {
1970 struct ocspcache_large_entry *entries;
1971
1972 entries = ocspcache_resp_list->elts;
1973 for (i = 0; i < ocspcache_resp_list->nelts; i++) {
1974 struct ocspcache_large_entry *entry;
1975
1976 entry = &(entries[i]);
1977 entry->age = 0;
1978 pr_memscrub(entry->resp_der, entry->resp_derlen);
1979 entry->resp_derlen = 0;
1980 pr_memscrub(entry->fingerprint, entry->fingerprint_len);
1981 entry->fingerprint_len = 0;
1982 }
1983 }
1984
1985 /* XXX iterate through keys, kremoving any "mod_tls_memcache" prefixed keys */
1986
1987 return res;
1988 }
1989
1990 static int ocsp_cache_remove(tls_ocsp_cache_t *cache) {
1991 int res;
1992
1993 pr_trace_msg(trace_channel, 9, "removing memcache ocsp cache %p", cache);
1994
1995 res = ocsp_cache_clear(cache);
1996 /* XXX close memcache conn */
1997
1998 return res;
1999 }
2000
2001 static int ocsp_cache_status(tls_ocsp_cache_t *cache,
2002 void (*statusf)(void *, const char *, ...), void *arg, int flags) {
2003 register unsigned int i;
2004 pool *tmp_pool;
2005
2006 pr_trace_msg(trace_channel, 9, "checking memcache ocsp cache %p", cache);
2007
2008 tmp_pool = make_sub_pool(permanent_pool);
2009
2010 statusf(arg, "%s", "Memcache OCSP response cache provided by "
2011 MOD_TLS_MEMCACHE_VERSION);
2012 statusf(arg, "%s", "");
2013 statusf(arg, "Memcache servers: ");
2014
2015 for (i = 0; ocspcache_keys[i].key != NULL; i++) {
2016 const char *key, *desc;
2017 void *value = NULL;
2018 size_t valuesz = 0;
2019 uint32_t stat_flags = 0;
2020
2021 key = ocspcache_keys[i].key;
2022 desc = ocspcache_keys[i].desc;
2023
2024 value = pr_memcache_get(ocsp_mcache, &tls_memcache_module, key, &valuesz,
2025 &stat_flags);
2026 if (value != NULL) {
2027 uint64_t num = 0;
2028 memcpy(&num, value, valuesz);
2029 statusf(arg, "%s: %lu", desc, (unsigned long) num);
2030 }
2031 }
2032
2033 /* XXX run stats on memcached servers? */
2034
2035 destroy_pool(tmp_pool);
2036 return 0;
2037 }
2038 #endif /* PR_USE_OPENSSL_OCSP */
2039
2040 /* Event Handlers
2041 */
2042
2043 #if defined(PR_SHARED_MODULE)
2044 static void tls_mcache_mod_unload_ev(const void *event_data, void *user_data) {
2045 if (strcmp("mod_tls_memcache.c", (const char *) event_data) == 0) {
2046 pr_event_unregister(&tls_memcache_module, NULL, NULL);
2047 tls_sess_cache_unregister("memcache");
2048 # if defined(PR_USE_OPENSSL_OCSP)
2049 tls_ocsp_cache_unregister("memcache");
2050 # endif /* PR_USE_OPENSSL_OCSP */
2051 }
2052 }
2053 #endif /* !PR_SHARED_MODULE */
2054
2055 /* Initialization functions
2056 */
2057
2058 static int tls_mcache_init(void) {
2059 #if defined(PR_SHARED_MODULE)
2060 pr_event_register(&tls_memcache_module, "core.module-unload",
2061 tls_mcache_mod_unload_ev, NULL);
2062 #endif /* !PR_SHARED_MODULE */
2063
2064 /* Prepare our SSL session cache handler. */
2065 memset(&sess_cache, 0, sizeof(sess_cache));
2066
2067 sess_cache.cache_name = "memcache";
2068 pr_pool_tag(sess_cache.cache_pool, MOD_TLS_MEMCACHE_VERSION);
2069
2070 sess_cache.open = sess_cache_open;
2071 sess_cache.close = sess_cache_close;
2072 sess_cache.add = sess_cache_add;
2073 sess_cache.get = sess_cache_get;
2074 sess_cache.delete = sess_cache_delete;
2075 sess_cache.clear = sess_cache_clear;
2076 sess_cache.remove = sess_cache_remove;
2077 sess_cache.status = sess_cache_status;
2078
2079 #ifdef SSL_SESS_CACHE_NO_INTERNAL
2080 /* Take a chance, and inform OpenSSL that it does not need to use its own
2081 * internal session cache lookups/storage; using the external session cache
2082 * (i.e. us) will be enough.
2083 */
2084 sess_cache.cache_mode = SSL_SESS_CACHE_NO_INTERNAL;
2085 #endif
2086
2087 #if defined(PR_USE_OPENSSL_OCSP)
2088 /* Prepare our OCSP response cache handler. */
2089 memset(&ocsp_cache, 0, sizeof(ocsp_cache));
2090
2091 ocsp_cache.cache_name = "memcache";
2092 pr_pool_tag(ocsp_cache.cache_pool, MOD_TLS_MEMCACHE_VERSION);
2093
2094 ocsp_cache.open = ocsp_cache_open;
2095 ocsp_cache.close = ocsp_cache_close;
2096 ocsp_cache.add = ocsp_cache_add;
2097 ocsp_cache.get = ocsp_cache_get;
2098 ocsp_cache.delete = ocsp_cache_delete;
2099 ocsp_cache.clear = ocsp_cache_clear;
2100 ocsp_cache.remove = ocsp_cache_remove;
2101 ocsp_cache.status = ocsp_cache_status;
2102 #endif /* PR_USE_OPENSSL_OCSP */
2103
2104 #ifdef PR_USE_MEMCACHE
2105 if (tls_sess_cache_register("memcache", &sess_cache) < 0) {
2106 pr_log_debug(DEBUG1, MOD_TLS_MEMCACHE_VERSION
2107 ": notice: error registering 'memcache' SSL session cache: %s",
2108 strerror(errno));
2109 return -1;
2110 }
2111
2112 # if defined(PR_USE_OPENSSL_OCSP)
2113 if (tls_ocsp_cache_register("memcache", &ocsp_cache) < 0) {
2114 pr_log_debug(DEBUG1, MOD_TLS_MEMCACHE_VERSION
2115 ": notice: error registering 'memcache' OCSP response cache: %s",
2116 strerror(errno));
2117 return -1;
2118 }
2119 # endif /* PR_USE_OPENSSL_OCSP */
2120
2121 #else
2122 pr_log_debug(DEBUG1, MOD_TLS_MEMCACHE_VERSION
2123 ": unable to register 'memcache' SSL session cache: Memcache support not enabled");
2124 # if defined(PR_USE_OPENSSL_OCSP)
2125 pr_log_debug(DEBUG1, MOD_TLS_MEMCACHE_VERSION
2126 ": unable to register 'memcache' OCSP response cache: Memcache support not enabled");
2127 # endif /* PR_USE_OPENSSL_OCSP */
2128 #endif /* PR_USE_MEMCACHE */
2129
2130 return 0;
2131 }
2132
2133 static int tls_mcache_sess_init(void) {
2134 /* Reset our memcache handles. */
2135
2136 if (sess_mcache != NULL) {
2137 if (pr_memcache_conn_clone(session.pool, sess_mcache) < 0) {
2138 tls_log(MOD_TLS_MEMCACHE_VERSION
2139 ": error resetting memcache handle: %s", strerror(errno));
2140 }
2141 }
2142
2143 #if defined(PR_USE_OPENSSL_OCSP)
2144 if (ocsp_mcache != NULL) {
2145 if (pr_memcache_conn_clone(session.pool, ocsp_mcache) < 0) {
2146 tls_log(MOD_TLS_MEMCACHE_VERSION
2147 ": error resetting memcache handle: %s", strerror(errno));
2148 }
2149 }
2150 #endif /* PR_USE_OPENSSL_OCSP */
2151
2152 return 0;
2153 }
2154
2155 /* Module API tables
2156 */
2157
2158 module tls_memcache_module = {
2159 NULL, NULL,
2160
2161 /* Module API version 2.0 */
2162 0x20,
2163
2164 /* Module name */
2165 "tls_memcache",
2166
2167 /* Module configuration handler table */
2168 NULL,
2169
2170 /* Module command handler table */
2171 NULL,
2172
2173 /* Module authentication handler table */
2174 NULL,
2175
2176 /* Module initialization function */
2177 tls_mcache_init,
2178
2179 /* Session initialization function */
2180 tls_mcache_sess_init,
2181
2182 /* Module version */
2183 MOD_TLS_MEMCACHE_VERSION
2184 };