1 /* $NetBSD: tlsmgrmem.c,v 1.2 2017/02/14 01:16:47 christos Exp $ */
2
3 /*++
4 /* NAME
5 /* tlsmgrmem 3
6 /* SUMMARY
7 /* Memory-based TLS manager interface for tlsfinger(1).
8 /* SYNOPSIS
9 /* #ifdef USE_TLS
10 /* #include <tlsmgrmem.h>
11 /*
12 /* void tlsmgrmem_disable()
13 /*
14 /* void tlsmgrmem_status(enable, count, hits)
15 /* int *enable;
16 /* int *count;
17 /* int *hits;
18 /*
19 /* void tlsmgrmem_flush()
20 /* #endif
21 /* DESCRIPTION
22 /* tlsmgrmem_disable() disables the in-memory TLS session cache.
23 /*
24 /* tlsmgrmem_status() reports whether the cache is enabled, the
25 /* number of entries in the cache, and the number of cache hits.
26 /* If any of the return pointers are null, that item is not reported.
27 /*
28 /* tlsmgrmem_flush() flushes any cached data and frees the cache.
29 /* LICENSE
30 /* .ad
31 /* .fi
32 /* The Secure Mailer license must be distributed with this software.
33 /* AUTHOR(S)
34 /* Wietse Venema
35 /* IBM T.J. Watson Research
36 /* P.O. Box 704
37 /* Yorktown Heights, NY 10598, USA
38 /*
39 /* Viktor Dukhovni
40 /*--*/
41
42 #include <sys_defs.h>
43
44 #ifdef USE_TLS
45 #include <htable.h>
46 #include <vstring.h>
47 #include <tls_mgr.h>
48
49 #include "tlsmgrmem.h"
50
51 static HTABLE *tls_cache;
52 static int cache_enabled = 1;
53 static int cache_count;
54 static int cache_hits;
55 typedef void (*free_func) (void *);
56 static free_func free_value = (free_func) vstring_free;
57
tlsmgrmem_disable(void)58 void tlsmgrmem_disable(void)
59 {
60 cache_enabled = 0;
61 }
62
tlsmgrmem_flush(void)63 void tlsmgrmem_flush(void)
64 {
65 if (!tls_cache)
66 return;
67 htable_free(tls_cache, free_value);
68 }
69
tlsmgrmem_status(int * enabled,int * count,int * hits)70 void tlsmgrmem_status(int *enabled, int *count, int *hits)
71 {
72 if (enabled)
73 *enabled = cache_enabled;
74 if (count)
75 *count = cache_count;
76 if (hits)
77 *hits = cache_hits;
78 }
79
80 /* tls_mgr_* - Local cache and stubs that do not talk to the TLS manager */
81
tls_mgr_seed(VSTRING * buf,int len)82 int tls_mgr_seed(VSTRING *buf, int len)
83 {
84 return (TLS_MGR_STAT_OK);
85 }
86
tls_mgr_policy(const char * unused_type,int * cachable,int * timeout)87 int tls_mgr_policy(const char *unused_type, int *cachable, int *timeout)
88 {
89 if (cache_enabled && tls_cache == 0)
90 tls_cache = htable_create(1);
91 *cachable = cache_enabled;
92 *timeout = TLS_SESSION_LIFEMIN;
93 return (TLS_MGR_STAT_OK);
94 }
95
tls_mgr_lookup(const char * unused_type,const char * key,VSTRING * buf)96 int tls_mgr_lookup(const char *unused_type, const char *key, VSTRING *buf)
97 {
98 VSTRING *s;
99
100 if (tls_cache == 0)
101 return TLS_MGR_STAT_ERR;
102
103 if ((s = (VSTRING *) htable_find(tls_cache, key)) == 0)
104 return TLS_MGR_STAT_ERR;
105
106 vstring_memcpy(buf, vstring_str(s), VSTRING_LEN(s));
107
108 ++cache_hits;
109 return (TLS_MGR_STAT_OK);
110 }
111
tls_mgr_update(const char * unused_type,const char * key,const char * buf,ssize_t len)112 int tls_mgr_update(const char *unused_type, const char *key,
113 const char *buf, ssize_t len)
114 {
115 HTABLE_INFO *ent;
116 VSTRING *s;
117
118 if (tls_cache == 0)
119 return TLS_MGR_STAT_ERR;
120
121 if ((ent = htable_locate(tls_cache, key)) == 0) {
122 s = vstring_alloc(len);
123 ent = htable_enter(tls_cache, key, (void *) s);
124 } else {
125 s = (VSTRING *) ent->value;
126 }
127 vstring_memcpy(s, buf, len);
128
129 ++cache_count;
130 return (TLS_MGR_STAT_OK);
131 }
132
tls_mgr_delete(const char * unused_type,const char * key)133 int tls_mgr_delete(const char *unused_type, const char *key)
134 {
135 if (tls_cache == 0)
136 return TLS_MGR_STAT_ERR;
137
138 if (htable_locate(tls_cache, key)) {
139 htable_delete(tls_cache, key, free_value);
140 --cache_count;
141 }
142 return (TLS_MGR_STAT_OK);
143 }
144
145 #endif
146