1/*******************************************************************************
2 * *****************************************************************************
3 * *
4 * * cache.h
5 * *
6 * * Description:  Header file for cache.c
7 * *
8 * *
9 * * Copyright (C) 2003 Jeremy Rumpf
10 * *
11 * * Redistribution and use in source and binary forms, with or without
12 * * modification, are permitted provided that the following conditions
13 * * are met:
14 * *
15 * * 1. Redistributions of source code must retain the above copyright
16 * *    notice, this list of conditions and the following disclaimer.
17 * *
18 * * 2. Redistributions in binary form must reproduce the above copyright
19 * *    notice, this list of conditions and the following disclaimer in the
20 * *    documentation and/or other materials provided with the distribution.
21 * *
22 * * THIS SOFTWARE IS PROVIDED ``AS IS''. ANY EXPRESS OR IMPLIED WARRANTIES,
23 * * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * * IN NO EVENT SHALL JEREMY RUMPF OR ANY CONTRIBUTER TO THIS SOFTWARE BE
25 * * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * * THE POSSIBILITY OF SUCH DAMAGE
32 * *
33 * * Jeremy Rumpf
34 * * jrumpf@heavyload.net
35 * *
36 * ******************************************************************************
37 ********************************************************************************/
38
39#ifndef _CACHE_H
40#define _CACHE_H
41
42
43/* constant includes */
44#include "saslauthd.h"
45
46
47/****************************************************************
48* * Plug in some autoconf magic to determine what implementation
49* * to use for the table slot (row) locking.
50****************************************************************/
51#ifdef USE_DOORS
52# define CACHE_USE_PTHREAD_RWLOCK
53#else
54# define CACHE_USE_FCNTL
55#endif
56
57
58
59/************************************************/
60#ifdef CACHE_USE_FCNTL
61	/* FCNTL Impl */
62
63struct lock_ctl {
64	char			*flock_file;
65	int			flock_fd;
66};
67
68#endif  /* CACHE_USE_FCNTL */
69/************************************************/
70
71
72
73/************************************************/
74#ifdef CACHE_USE_PTHREAD_RWLOCK
75	/* RWLock Impl */
76
77#include <pthread.h>
78
79struct lock_ctl {
80	pthread_rwlock_t	*rwlock;
81};
82
83#endif  /* CACHE_USE_PTHREAD_RWLOCK */
84/************************************************/
85
86
87
88/* defaults */
89#define CACHE_DEFAULT_TIMEOUT		28800
90#define CACHE_DEFAULT_TABLE_SIZE	1711
91#define CACHE_DEFAULT_FLAGS		0
92#define CACHE_MAX_BUCKETS_PER		6
93#define CACHE_MMAP_FILE			"/cache.mmap"  /* don't forget the "/" */
94#define CACHE_FLOCK_FILE		"/cache.flock" /* don't forget the "/" */
95
96
97
98/* If debugging uncomment this for always verbose  */
99/* #define CACHE_DEFAULT_FLAGS		CACHE_VERBOSE */
100
101
102
103/* max length for cached credential values */
104#define CACHE_MAX_CREDS_LENGTH		60
105
106
107
108/* magic values (must be less than 63 chars!) */
109#define CACHE_CACHE_MAGIC		"SASLAUTHD_CACHE_MAGIC"
110
111
112
113/* return values */
114#define CACHE_OK			0
115#define CACHE_FAIL			1
116#define CACHE_TOO_BIG			2
117
118
119
120/* cache_result status values */
121#define CACHE_NO_FLUSH			0
122#define CACHE_FLUSH			1
123#define CACHE_FLUSH_WITH_RESCAN		2
124
125
126
127/* declarations */
128struct bucket {
129        char            	creds[CACHE_MAX_CREDS_LENGTH];
130        unsigned int		user_offt;
131        unsigned int		realm_offt;
132        unsigned int		service_offt;
133        unsigned char   	pwd_digest[16];
134        time_t          	created;
135};
136
137struct stats {
138        volatile unsigned int   hits;
139        volatile unsigned int   misses;
140        volatile unsigned int   lock_failures;
141        volatile unsigned int   attempts;
142        unsigned int            table_size;
143        unsigned int            max_buckets_per;
144        unsigned int            sizeof_bucket;
145        unsigned int            bytes;
146        unsigned int            timeout;
147};
148
149struct mm_ctl {
150	void			*base;
151	unsigned int		bytes;
152	char			*file;
153};
154
155struct cache_result {
156	struct bucket		bucket;
157	struct bucket   	*read_bucket;
158	unsigned int		hash_offset;
159	int			status;
160};
161
162
163/* cache.c */
164extern int cache_init(void);
165extern int cache_lookup(const char *, const char *, const char *, const char *, struct cache_result *);
166extern void cache_commit(struct cache_result *);
167extern int cache_pjwhash(char *);
168extern void cache_set_table_size(const char *);
169extern void cache_set_timeout(const char *);
170extern unsigned int cache_get_next_prime(unsigned int);
171extern void *cache_alloc_mm(unsigned int);
172extern void cache_cleanup_mm(void);
173extern void cache_cleanup_lock(void);
174extern int cache_init_lock(void);
175extern int cache_get_wlock(unsigned int);
176extern int cache_get_rlock(unsigned int);
177extern int cache_un_lock(unsigned int);
178
179#endif  /* _CACHE_H */
180
181