]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/dncache.c
Do not return pointers into BerElement we do not own
[openldap] / servers / slapd / back-meta / dncache.c
1 /*
2  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  *
5  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
6  *
7  * This work has been developed to fulfill the requirements
8  * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
9  * to the OpenLDAP Foundation in the hope that it may be useful
10  * to the Open Source community, but WITHOUT ANY WARRANTY.
11  *
12  * Permission is granted to anyone to use this software for any purpose
13  * on any computer system, and to alter it and redistribute it, subject
14  * to the following restrictions:
15  *
16  * 1. The author and SysNet s.n.c. are not responsible for the consequences
17  *    of use of this software, no matter how awful, even if they arise from 
18  *    flaws in it.
19  *
20  * 2. The origin of this software must not be misrepresented, either by
21  *    explicit claim or by omission.  Since few users ever read sources,
22  *    credits should appear in the documentation.
23  *
24  * 3. Altered versions must be plainly marked as such, and must not be
25  *    misrepresented as being the original software.  Since few users
26  *    ever read sources, credits should appear in the documentation.
27  *    SysNet s.n.c. cannot be responsible for the consequences of the
28  *    alterations.
29  *
30  * 4. This notice may not be removed or altered.
31  *
32  *
33  * This software is based on the backend back-ldap, implemented
34  * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
35  * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
36  * contributors. The contribution of the original software to the present
37  * implementation is acknowledged in this copyright statement.
38  *
39  * A special acknowledgement goes to Howard for the overall architecture
40  * (and for borrowing large pieces of code), and to Mark, who implemented
41  * from scratch the attribute/objectclass mapping.
42  *
43  * The original copyright statement follows.
44  *
45  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
46  *
47  * Permission is granted to anyone to use this software for any purpose
48  * on any computer system, and to alter it and redistribute it, subject
49  * to the following restrictions:
50  *
51  * 1. The author is not responsible for the consequences of use of this
52  *    software, no matter how awful, even if they arise from flaws in it.
53  *
54  * 2. The origin of this software must not be misrepresented, either by
55  *    explicit claim or by omission.  Since few users ever read sources,
56  *    credits should appear in the documentation.
57  *
58  * 3. Altered versions must be plainly marked as such, and must not be
59  *    misrepresented as being the original software.  Since few users
60  *    ever read sources, credits should appear in the
61  *    documentation.
62  *
63  * 4. This notice may not be removed or altered.
64  *
65  */
66
67 #include "portable.h"
68
69 #include <stdio.h>
70
71 #include "slap.h"
72 #include "../back-ldap/back-ldap.h"
73 #include "back-meta.h"
74
75 /*
76  * The dncache, at present, maps an entry to the target that holds it.
77  */
78
79 struct metadncacheentry {
80         struct berval   dn;
81         int             target;
82
83         time_t          lastupdated;
84 };
85
86 /*
87  * meta_dncache_cmp
88  *
89  * compares two struct metadncacheentry; used by avl stuff
90  * FIXME: modify avl stuff to delete an entry based on cmp
91  * (e.g. when ttl expired?)
92  */
93 int
94 meta_dncache_cmp(
95                 const void *c1,
96                 const void *c2
97 )
98 {
99         struct metadncacheentry *cc1 = ( struct metadncacheentry * )c1;
100         struct metadncacheentry *cc2 = ( struct metadncacheentry * )c2;
101
102         /*
103          * case sensitive, because the dn MUST be normalized
104          */
105         return ber_bvcmp( &cc1->dn, &cc2->dn);
106 }
107
108 /*
109  * meta_dncache_dup
110  *
111  * returns -1 in case a duplicate struct metadncacheentry has been inserted;
112  * used by avl stuff
113  */
114 int
115 meta_dncache_dup(
116                 void *c1,
117                 void *c2
118 )
119 {
120         struct metadncacheentry *cc1 = ( struct metadncacheentry * )c1;
121         struct metadncacheentry *cc2 = ( struct metadncacheentry * )c2;
122         
123         /*
124          * case sensitive, because the dn MUST be normalized
125          */
126         return ( ber_bvcmp( &cc1->dn, &cc2->dn ) == 0 ) ? -1 : 0;
127 }
128
129 /*
130  * meta_dncache_get_target
131  *
132  * returns the target a dn belongs to, or -1 in case the dn is not
133  * in the cache
134  */
135 int
136 meta_dncache_get_target(
137                 struct metadncache      *cache,
138                 struct berval           *ndn
139 )
140 {
141         struct metadncacheentry tmp_entry, *entry;
142         time_t curr_time;
143         int target = -1;
144
145         assert( cache );
146         assert( ndn );
147
148         tmp_entry.dn = *ndn;
149         ldap_pvt_thread_mutex_lock( &cache->mutex );
150         entry = ( struct metadncacheentry * )avl_find( cache->tree,
151                         ( caddr_t )&tmp_entry, meta_dncache_cmp );
152
153         if ( entry != NULL ) {
154                 
155                 /*
156                  * if cache->ttl < 0, cache never expires;
157                  * if cache->ttl = 0 no cache is used; shouldn't get here
158                  * else, cache is used with ttl
159                  */
160                 if ( cache->ttl < 0 ) { 
161                         target = entry->target;
162                 } else {
163
164                         /*
165                          * Need mutex?
166                          */     
167                         curr_time = time( NULL );
168
169                         if ( entry->lastupdated+cache->ttl > curr_time ) {
170                                 target = entry->target;
171                         }
172                 }
173         }
174         ldap_pvt_thread_mutex_unlock( &cache->mutex );
175
176         return target;
177 }
178
179 /*
180  * meta_dncache_update_entry
181  *
182  * updates target and lastupdated of a struct metadncacheentry if exists,
183  * otherwise it gets created; returns -1 in case of error
184  */
185 int
186 meta_dncache_update_entry(
187                 struct metadncache      *cache,
188                 struct berval           *ndn,
189                 int                     target
190 )
191 {
192         struct metadncacheentry *entry, tmp_entry;
193         time_t curr_time = 0L;
194         int err = 0;
195
196         assert( cache );
197         assert( ndn );
198
199         /*
200          * if cache->ttl < 0, cache never expires;
201          * if cache->ttl = 0 no cache is used; shouldn't get here
202          * else, cache is used with ttl
203          */
204         if ( cache->ttl > 0 ) {
205
206                 /*
207                  * Need mutex?
208                  */
209                 curr_time = time( NULL );
210         }
211
212         tmp_entry.dn = *ndn;
213
214         ldap_pvt_thread_mutex_lock( &cache->mutex );
215         entry = ( struct metadncacheentry * )avl_find( cache->tree,
216                         ( caddr_t )&tmp_entry, meta_dncache_cmp );
217
218         if ( entry != NULL ) {
219                 entry->target = target;
220                 entry->lastupdated = curr_time;
221         } else {
222                 entry = ch_calloc( sizeof( struct metadncacheentry ), 1 );
223                 if ( entry == NULL ) {
224                         ldap_pvt_thread_mutex_unlock( &cache->mutex );
225                         return -1;
226                 }
227
228                 ber_dupbv( &entry->dn, ndn );
229                 if ( entry->dn.bv_val == NULL ) {
230                         ldap_pvt_thread_mutex_unlock( &cache->mutex );
231                         return -1;
232                 }
233                 entry->target = target;
234                 entry->lastupdated = curr_time;
235
236                 err = avl_insert( &cache->tree, ( caddr_t )entry,
237                                 meta_dncache_cmp, meta_dncache_dup );
238         }
239         ldap_pvt_thread_mutex_unlock( &cache->mutex );
240
241         return err;
242 }
243
244 /*
245  * meta_dncache_update_entry
246  *
247  * updates target and lastupdated of a struct metadncacheentry if exists,
248  * otherwise it gets created; returns -1 in case of error
249  */
250 int
251 meta_dncache_delete_entry(
252                 struct metadncache      *cache,
253                 struct berval           *ndn
254 )
255 {
256         struct metadncacheentry *entry, tmp_entry;
257
258         assert( cache );
259         assert( ndn );
260
261         tmp_entry.dn = *ndn;
262
263         ldap_pvt_thread_mutex_lock( &cache->mutex );
264         entry = avl_delete( &cache->tree, ( caddr_t )&tmp_entry,
265                         meta_dncache_cmp );
266         ldap_pvt_thread_mutex_unlock( &cache->mutex );
267
268         if ( entry != NULL ) {
269                 meta_dncache_free( ( void * )entry );
270         }
271
272         return 0;
273 }
274
275 /*
276  * meta_dncache_free
277  *
278  * frees an entry
279  * 
280  */
281 void
282 meta_dncache_free(
283                 void *e
284 )
285 {
286         struct metadncacheentry *entry = ( struct metadncacheentry * )e;
287
288         free( entry->dn.bv_val );
289 }
290