]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/init.c
23e12fe289ece6b852de4da33db6c274b908529c
[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-2004 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 #if SLAPD_META == SLAPD_MOD_DYNAMIC
30
31 int
32 init_module( int argc, char *argv[] ) {
33     BackendInfo bi;
34
35     memset( &bi, '\0', sizeof( bi ) );
36     bi.bi_type = "meta";
37     bi.bi_init = meta_back_initialize;
38
39     backend_add( &bi );
40     return 0;
41 }
42
43 #endif /* SLAPD_META */
44
45 int
46 meta_back_open(
47         BackendInfo *bi
48 )
49 {
50         bi->bi_controls = slap_known_controls;
51         return 0;
52 }
53
54 int
55 meta_back_initialize(
56                 BackendInfo     *bi
57 )
58 {
59         bi->bi_open = meta_back_open;
60         bi->bi_config = 0;
61         bi->bi_close = 0;
62         bi->bi_destroy = 0;
63
64         bi->bi_db_init = meta_back_db_init;
65         bi->bi_db_config = meta_back_db_config;
66         bi->bi_db_open = 0;
67         bi->bi_db_close = 0;
68         bi->bi_db_destroy = meta_back_db_destroy;
69
70         bi->bi_op_bind = meta_back_bind;
71         bi->bi_op_unbind = 0;
72         bi->bi_op_search = meta_back_search;
73         bi->bi_op_compare = meta_back_compare;
74         bi->bi_op_modify = meta_back_modify;
75         bi->bi_op_modrdn = meta_back_modrdn;
76         bi->bi_op_add = meta_back_add;
77         bi->bi_op_delete = meta_back_delete;
78         bi->bi_op_abandon = 0;
79
80         bi->bi_extended = 0;
81
82         bi->bi_chk_referrals = 0;
83
84         bi->bi_connection_init = 0;
85         bi->bi_connection_destroy = meta_back_conn_destroy;
86
87         return 0;
88 }
89
90 int
91 meta_back_db_init(
92                 Backend *be
93 )
94 {
95         struct metainfo *li;
96
97         struct rewrite_info     *rwinfo;
98
99         rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
100         if ( rwinfo == NULL ) {
101                 return -1;
102         }
103
104         li = ch_calloc( 1, sizeof( struct metainfo ) );
105         if ( li == NULL ) {
106                 rewrite_info_delete( &rwinfo );
107                 return -1;
108         }
109
110         /*
111          * At present the default is no default target;
112          * this may change
113          */
114         li->defaulttarget = META_DEFAULT_TARGET_NONE;
115         li->rwinfo = rwinfo;
116
117         ldap_pvt_thread_mutex_init( &li->conn_mutex );
118         ldap_pvt_thread_mutex_init( &li->cache.mutex );
119         be->be_private = li;
120
121         return 0;
122 }
123
124 static void
125 conn_free( 
126         void *v_lc
127 )
128 {
129         struct metaconn *lc = v_lc;
130         struct metasingleconn *lsc;
131
132         for ( lsc = lc->conns; !META_LAST(lsc); lsc++ ) {
133                 if ( lsc->ld != NULL ) {
134                         ldap_unbind( lsc->ld );
135                 }
136                 if ( lsc->bound_dn.bv_val ) {
137                         ber_memfree( lsc->bound_dn.bv_val );
138                 }
139                 if ( lsc->cred.bv_val ) {
140                         memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
141                         ber_memfree( lsc->cred.bv_val );
142                 }
143         }
144         free( lc->conns );
145         free( lc );
146 }
147
148 static void
149 target_free(
150                 struct metatarget *lt
151 )
152 {
153         if ( lt->uri ) {
154                 free( lt->uri );
155         }
156         if ( lt->psuffix.bv_val ) {
157                 free( lt->psuffix.bv_val );
158         }
159         if ( lt->suffix.bv_val ) {
160                 free( lt->suffix.bv_val );
161         }
162         if ( lt->binddn.bv_val ) {
163                 free( lt->binddn.bv_val );
164         }
165         if ( lt->bindpw.bv_val ) {
166                 free( lt->bindpw.bv_val );
167         }
168         if ( lt->pseudorootdn.bv_val ) {
169                 free( lt->pseudorootdn.bv_val );
170         }
171         if ( lt->pseudorootpw.bv_val ) {
172                 free( lt->pseudorootpw.bv_val );
173         }
174         if ( lt->rwmap.rwm_rw ) {
175                 rewrite_info_delete( &lt->rwmap.rwm_rw );
176         }
177         avl_free( lt->rwmap.rwm_oc.remap, NULL );
178         avl_free( lt->rwmap.rwm_oc.map, mapping_free );
179         avl_free( lt->rwmap.rwm_at.remap, NULL );
180         avl_free( lt->rwmap.rwm_at.map, mapping_free );
181 }
182
183 int
184 meta_back_db_destroy(
185     Backend     *be
186 )
187 {
188         struct metainfo *li;
189
190         if ( be->be_private ) {
191                 int i;
192
193                 li = ( struct metainfo * )be->be_private;
194
195                 /*
196                  * Destroy the connection tree
197                  */
198                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
199
200                 if ( li->conntree ) {
201                         avl_free( li->conntree, conn_free );
202                 }
203
204                 /*
205                  * Destroy the per-target stuff (assuming there's at
206                  * least one ...)
207                  */
208                 for ( i = 0; i < li->ntargets; i++ ) {
209                         target_free( li->targets[ i ] );
210                         free( li->targets[ i ] );
211                 }
212
213                 free( li->targets );
214
215                 ldap_pvt_thread_mutex_lock( &li->cache.mutex );
216                 if ( li->cache.tree ) {
217                         avl_free( li->cache.tree, meta_dncache_free );
218                 }
219                 
220                 ldap_pvt_thread_mutex_unlock( &li->cache.mutex );
221                 ldap_pvt_thread_mutex_destroy( &li->cache.mutex );
222
223                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
224                 ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
225         }
226
227         free( be->be_private );
228         return 0;
229 }
230