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