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