Postfix3.3.1
tlsmgrmem.c
[詳解]
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 
49 static HTABLE *tls_cache;
50 static int cache_enabled = 1;
51 static int cache_count;
52 static int cache_hits;
53 typedef void (*free_func) (void *);
54 static free_func free_value = (free_func) vstring_free;
55 
56 void tlsmgrmem_disable(void)
57 {
58  cache_enabled = 0;
59 }
60 
61 void tlsmgrmem_flush(void)
62 {
63  if (!tls_cache)
64  return;
65  htable_free(tls_cache, free_value);
66 }
67 
68 void 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 
80 int tls_mgr_seed(VSTRING *buf, int len)
81 {
82  return (TLS_MGR_STAT_OK);
83 }
84 
85 int 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 
94 int 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 
105 
106  ++cache_hits;
107  return (TLS_MGR_STAT_OK);
108 }
109 
110 int 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, (void *) 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 
131 int 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
void htable_free(HTABLE *table, void(*free_fn)(void *))
Definition: htable.c:287
void * value
Definition: htable.h:18
HTABLE_INFO * htable_locate(HTABLE *table, const char *key)
Definition: htable.c:242
#define vstring_str(vp)
Definition: vstring.h:71
#define TLS_MGR_STAT_OK
Definition: tls_mgr.h:46
#define TLS_MGR_STAT_ERR
Definition: tls_mgr.h:47
int tls_mgr_lookup(const char *, const char *, VSTRING *)
#define VSTRING_LEN(vp)
Definition: vstring.h:72
Definition: htable.h:25
int tls_mgr_update(const char *, const char *, const char *, ssize_t)
int tls_mgr_seed(VSTRING *, int)
HTABLE * htable_create(ssize_t size)
Definition: htable.c:179
void tlsmgrmem_disable(void)
VSTRING * vstring_alloc(ssize_t len)
Definition: vstring.c:353
void * htable_find(HTABLE *table, const char *key)
Definition: htable.c:227
int tls_mgr_delete(const char *, const char *)
int tls_mgr_policy(const char *, int *, int *)
VSTRING * vstring_free(VSTRING *vp)
Definition: vstring.c:380
void tlsmgrmem_status(int *, int *, int *)
VSTRING * vstring_memcpy(VSTRING *vp, const char *src, ssize_t len)
Definition: vstring.c:483
#define TLS_SESSION_LIFEMIN
Definition: tls_scache.h:36
void htable_delete(HTABLE *table, const char *key, void(*free_fn)(void *))
Definition: htable.c:257
void tlsmgrmem_flush(void)
HTABLE_INFO * htable_enter(HTABLE *table, const char *key, void *value)
Definition: htable.c:212