]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/init.c
4978ee98b4cd68ca3b5a59b19e110e767aeefa28
[openldap] / servers / slapd / back-meta / init.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 <ac/string.h>
72 #include <ac/socket.h>
73
74 #include "slap.h"
75 #include "../back-ldap/back-ldap.h"
76 #include "back-meta.h"
77
78 #ifdef SLAPD_META_DYNAMIC
79
80 int
81 back_meta_LTX_init_module( int argc, char *argv[] ) {
82     BackendInfo bi;
83
84     memset( &bi, '\0', sizeof( bi ) );
85     bi.bi_type = "meta";
86     bi.bi_init = meta_back_initialize;
87
88     backend_add( &bi );
89     return 0;
90 }
91
92 #endif /* SLAPD_META_DYNAMIC */
93
94 int
95 meta_back_initialize(
96                 BackendInfo     *bi
97 )
98 {
99         bi->bi_controls = slap_known_controls;
100
101         bi->bi_open = 0;
102         bi->bi_config = 0;
103         bi->bi_close = 0;
104         bi->bi_destroy = 0;
105
106         bi->bi_db_init = meta_back_db_init;
107         bi->bi_db_config = meta_back_db_config;
108         bi->bi_db_open = 0;
109         bi->bi_db_close = 0;
110         bi->bi_db_destroy = meta_back_db_destroy;
111
112         bi->bi_op_bind = meta_back_bind;
113         bi->bi_op_unbind = 0;
114         bi->bi_op_search = meta_back_search;
115         bi->bi_op_compare = meta_back_compare;
116         bi->bi_op_modify = meta_back_modify;
117         bi->bi_op_modrdn = meta_back_modrdn;
118         bi->bi_op_add = meta_back_add;
119         bi->bi_op_delete = meta_back_delete;
120         bi->bi_op_abandon = 0;
121
122         bi->bi_extended = 0;
123
124         bi->bi_chk_referrals = 0;
125
126         bi->bi_connection_init = 0;
127         bi->bi_connection_destroy = meta_back_conn_destroy;
128
129         return 0;
130 }
131
132 int
133 meta_back_db_init(
134                 Backend *be
135 )
136 {
137         struct metainfo *li;
138
139 #ifdef LDAP_CACHING
140         struct rewrite_info     *rwinfo;
141         cache_manager           *cm;
142         query_manager           *qm;
143
144         rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
145         if ( rwinfo == NULL ) {
146                 return -1;
147         }
148                 
149         cm = (cache_manager *)ch_malloc(sizeof(cache_manager)); 
150         if ( cm == NULL ) {
151                 rewrite_info_delete( rwinfo );
152                 return -1;
153         }
154
155         qm = (query_manager*)ch_malloc(sizeof(query_manager)); 
156         if ( qm == NULL ) {
157                 rewrite_info_delete( rwinfo );
158                 ch_free( cm );
159                 return -1;
160         }
161
162         cm->caching = 0; 
163         cm->qm = qm; 
164         cm->numattrsets = 0; 
165         cm->numtemplates = 0;   
166         cm->num_entries_limit = 5;
167         cm->cache_size = 0;
168         cm->thresh_hi = 500000;
169         cm->thresh_lo = 700000;
170         cm->num_cached_queries = 0; 
171         cm->total_entries = 0; 
172         cm->max_queries = 10000; 
173         cm->threads = 0; 
174         cm->cc_thread_started = 0; 
175         cm->cc_period = 1000; 
176        
177         qm->attr_sets = NULL; 
178         qm->templates = NULL; 
179         qm->lru_top = NULL;
180         qm->lru_bottom = NULL;
181
182         qm->qcfunc = query_containment; 
183         qm->crfunc = cache_replacement; 
184         qm->addfunc = add_query; 
185         ldap_pvt_thread_mutex_init(&qm->lru_mutex); 
186         
187         ldap_pvt_thread_mutex_init(&cm->cache_mutex); 
188         ldap_pvt_thread_mutex_init(&cm->remove_mutex); 
189         ldap_pvt_thread_mutex_init( &cm->cc_mutex );
190 #endif /* LDAP_CACHING */
191
192         li = ch_calloc( 1, sizeof( struct metainfo ) );
193         if ( li == NULL ) {
194                 return -1;
195         }
196         
197         /*
198          * At present the default is no default target;
199          * this may change
200          */
201         li->defaulttarget = META_DEFAULT_TARGET_NONE;
202 #ifdef LDAP_CACHING
203         li->cm = cm; 
204         li->rwinfo = rwinfo;
205         /* FIXME: what about qm ? */
206 #endif /* LDAP_CACHING */
207
208         ldap_pvt_thread_mutex_init( &li->conn_mutex );
209         ldap_pvt_thread_mutex_init( &li->cache.mutex );
210         be->be_private = li;
211
212         return 0;
213 }
214
215 static void
216 conn_free( 
217         void *v_lc
218 )
219 {
220         struct metaconn *lc = v_lc;
221         struct metasingleconn *lsc;
222
223         for ( lsc = lc->conns; !META_LAST(lsc); lsc++ ) {
224                 if ( lsc->ld != NULL ) {
225                         ldap_unbind( lsc->ld );
226                 }
227                 if ( lsc->bound_dn.bv_val ) {
228                         ber_memfree( lsc->bound_dn.bv_val );
229                 }
230                 if ( lsc->cred.bv_val ) {
231                         memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
232                         ber_memfree( lsc->cred.bv_val );
233                 }
234         }
235         free( lc->conns );
236         free( lc );
237 }
238
239 static void
240 target_free(
241                 struct metatarget *lt
242 )
243 {
244         if ( lt->uri ) {
245                 free( lt->uri );
246         }
247         if ( lt->psuffix.bv_val ) {
248                 free( lt->psuffix.bv_val );
249         }
250         if ( lt->suffix.bv_val ) {
251                 free( lt->suffix.bv_val );
252         }
253         if ( lt->binddn.bv_val ) {
254                 free( lt->binddn.bv_val );
255         }
256         if ( lt->bindpw.bv_val ) {
257                 free( lt->bindpw.bv_val );
258         }
259         if ( lt->pseudorootdn.bv_val ) {
260                 free( lt->pseudorootdn.bv_val );
261         }
262         if ( lt->pseudorootpw.bv_val ) {
263                 free( lt->pseudorootpw.bv_val );
264         }
265         if ( lt->rwmap.rwm_rw ) {
266                 rewrite_info_delete( lt->rwmap.rwm_rw );
267         }
268         avl_free( lt->rwmap.rwm_oc.remap, NULL );
269         avl_free( lt->rwmap.rwm_oc.map, mapping_free );
270         avl_free( lt->rwmap.rwm_at.remap, NULL );
271         avl_free( lt->rwmap.rwm_at.map, mapping_free );
272 }
273
274 int
275 meta_back_db_destroy(
276     Backend     *be
277 )
278 {
279         struct metainfo *li;
280
281         if ( be->be_private ) {
282                 int i;
283
284                 li = ( struct metainfo * )be->be_private;
285
286                 /*
287                  * Destroy the connection tree
288                  */
289                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
290
291                 if ( li->conntree ) {
292                         avl_free( li->conntree, conn_free );
293                 }
294
295                 /*
296                  * Destroy the per-target stuff (assuming there's at
297                  * least one ...)
298                  */
299                 for ( i = 0; i < li->ntargets; i++ ) {
300                         target_free( li->targets[ i ] );
301                         free( li->targets[ i ] );
302                 }
303
304                 free( li->targets );
305
306                 ldap_pvt_thread_mutex_lock( &li->cache.mutex );
307                 if ( li->cache.tree ) {
308                         avl_free( li->cache.tree, meta_dncache_free );
309                 }
310                 
311                 ldap_pvt_thread_mutex_unlock( &li->cache.mutex );
312                 ldap_pvt_thread_mutex_destroy( &li->cache.mutex );
313
314                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
315                 ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
316         }
317
318         free( be->be_private );
319         return 0;
320 }
321