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