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