]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/init.c
more on error handling reworking; should address ITS#3672 and ITS#3676
[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-2005 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "slap.h"
26 #include "../back-ldap/back-ldap.h"
27 #include "back-meta.h"
28
29 int
30 meta_back_open(
31         BackendInfo     *bi )
32 {
33         bi->bi_controls = slap_known_controls;
34         return 0;
35 }
36
37 int
38 meta_back_initialize(
39         BackendInfo     *bi )
40 {
41         bi->bi_open = meta_back_open;
42         bi->bi_config = 0;
43         bi->bi_close = 0;
44         bi->bi_destroy = 0;
45
46         bi->bi_db_init = meta_back_db_init;
47         bi->bi_db_config = meta_back_db_config;
48         bi->bi_db_open = 0;
49         bi->bi_db_close = 0;
50         bi->bi_db_destroy = meta_back_db_destroy;
51
52         bi->bi_op_bind = meta_back_bind;
53         bi->bi_op_unbind = 0;
54         bi->bi_op_search = meta_back_search;
55         bi->bi_op_compare = meta_back_compare;
56         bi->bi_op_modify = meta_back_modify;
57         bi->bi_op_modrdn = meta_back_modrdn;
58         bi->bi_op_add = meta_back_add;
59         bi->bi_op_delete = meta_back_delete;
60         bi->bi_op_abandon = 0;
61
62         bi->bi_extended = 0;
63
64         bi->bi_chk_referrals = 0;
65
66         bi->bi_connection_init = 0;
67         bi->bi_connection_destroy = meta_back_conn_destroy;
68
69         return 0;
70 }
71
72 int
73 meta_back_db_init(
74         Backend         *be )
75 {
76         metainfo_t      *mi;
77
78         mi = ch_malloc( sizeof( metainfo_t ) );
79         if ( mi == NULL ) {
80                 return -1;
81         }
82         memset( mi, 0, sizeof( metainfo_t ) );
83
84         /*
85          * At present the default is no default target;
86          * this may change
87          */
88         mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
89
90         ldap_pvt_thread_mutex_init( &mi->mi_conn_mutex );
91         ldap_pvt_thread_mutex_init( &mi->mi_cache.mutex );
92         be->be_private = mi;
93
94         return 0;
95 }
96
97 static void
98 conn_free( 
99         void            *v_mc )
100 {
101         metaconn_t              *mc = v_mc;
102         metasingleconn_t        *msc;
103
104         assert( mc->mc_conns != NULL );
105
106         for ( msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); msc++ ) {
107                 if ( msc->msc_ld != NULL ) {
108                         ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
109                 }
110                 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
111                         ber_memfree( msc->msc_bound_ndn.bv_val );
112                 }
113                 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
114                         /* destroy sensitive data */
115                         memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
116                         ber_memfree( msc->msc_cred.bv_val );
117                 }
118         }
119
120         free( mc );
121 }
122
123 static void
124 mapping_free(
125         void            *v_mapping )
126 {
127         struct ldapmapping *mapping = v_mapping;
128         ch_free( mapping->src.bv_val );
129         ch_free( mapping->dst.bv_val );
130         ch_free( mapping );
131 }
132
133 static void
134 target_free(
135         metatarget_t    *mt )
136 {
137         if ( mt->mt_uri ) {
138                 free( mt->mt_uri );
139         }
140         if ( !BER_BVISNULL( &mt->mt_psuffix ) ) {
141                 free( mt->mt_psuffix.bv_val );
142         }
143         if ( !BER_BVISNULL( &mt->mt_nsuffix ) ) {
144                 free( mt->mt_nsuffix.bv_val );
145         }
146         if ( !BER_BVISNULL( &mt->mt_binddn ) ) {
147                 free( mt->mt_binddn.bv_val );
148         }
149         if ( !BER_BVISNULL( &mt->mt_bindpw ) ) {
150                 free( mt->mt_bindpw.bv_val );
151         }
152         if ( !BER_BVISNULL( &mt->mt_pseudorootdn ) ) {
153                 free( mt->mt_pseudorootdn.bv_val );
154         }
155         if ( !BER_BVISNULL( &mt->mt_pseudorootpw ) ) {
156                 free( mt->mt_pseudorootpw.bv_val );
157         }
158         if ( mt->mt_rwmap.rwm_rw ) {
159                 rewrite_info_delete( &mt->mt_rwmap.rwm_rw );
160         }
161         avl_free( mt->mt_rwmap.rwm_oc.remap, NULL );
162         avl_free( mt->mt_rwmap.rwm_oc.map, mapping_free );
163         avl_free( mt->mt_rwmap.rwm_at.remap, NULL );
164         avl_free( mt->mt_rwmap.rwm_at.map, mapping_free );
165 }
166
167 int
168 meta_back_db_destroy(
169         Backend         *be )
170 {
171         metainfo_t      *mi;
172
173         if ( be->be_private ) {
174                 int i;
175
176                 mi = ( metainfo_t * )be->be_private;
177
178                 /*
179                  * Destroy the connection tree
180                  */
181                 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
182
183                 if ( mi->mi_conntree ) {
184                         avl_free( mi->mi_conntree, conn_free );
185                 }
186
187                 /*
188                  * Destroy the per-target stuff (assuming there's at
189                  * least one ...)
190                  */
191                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
192                         target_free( mi->mi_targets[ i ] );
193                         free( mi->mi_targets[ i ] );
194                 }
195
196                 free( mi->mi_targets );
197
198                 ldap_pvt_thread_mutex_lock( &mi->mi_cache.mutex );
199                 if ( mi->mi_cache.tree ) {
200                         avl_free( mi->mi_cache.tree, meta_dncache_free );
201                 }
202                 
203                 ldap_pvt_thread_mutex_unlock( &mi->mi_cache.mutex );
204                 ldap_pvt_thread_mutex_destroy( &mi->mi_cache.mutex );
205
206                 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
207                 ldap_pvt_thread_mutex_destroy( &mi->mi_conn_mutex );
208
209                 if ( mi->mi_candidates != NULL ) {
210                         ber_memfree_x( mi->mi_candidates, NULL );
211                 }
212         }
213
214         free( be->be_private );
215         return 0;
216 }
217
218 #if SLAPD_META == SLAPD_MOD_DYNAMIC
219
220 /* conditionally define the init_module() function */
221 SLAP_BACKEND_INIT_MODULE( meta )
222
223 #endif /* SLAPD_META == SLAPD_MOD_DYNAMIC */
224
225