]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
4645b5195e461c4fc12026c2ba97fe2fe69ac31b
[openldap] / servers / slapd / back-ldap / init.c
1 /* init.c - initialize ldap backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2004 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "back-ldap.h"
33
34 #if SLAPD_LDAP == SLAPD_MOD_DYNAMIC
35
36 int init_module(int argc, char *argv[]) {
37     BackendInfo bi;
38
39     memset( &bi, '\0', sizeof(bi) );
40     bi.bi_type = "ldap";
41     bi.bi_init = ldap_back_initialize;
42
43     backend_add(&bi);
44     return 0;
45 }
46
47 #endif /* SLAPD_LDAP */
48
49 int
50 ldap_back_initialize(
51     BackendInfo *bi
52 )
53 {
54         bi->bi_controls = slap_known_controls;
55
56         bi->bi_open = 0;
57         bi->bi_config = 0;
58         bi->bi_close = 0;
59         bi->bi_destroy = 0;
60
61         bi->bi_db_init = ldap_back_db_init;
62         bi->bi_db_config = ldap_back_db_config;
63         bi->bi_db_open = 0;
64         bi->bi_db_close = 0;
65         bi->bi_db_destroy = ldap_back_db_destroy;
66
67         bi->bi_op_bind = ldap_back_bind;
68         bi->bi_op_unbind = 0;
69         bi->bi_op_search = ldap_back_search;
70         bi->bi_op_compare = ldap_back_compare;
71         bi->bi_op_modify = ldap_back_modify;
72         bi->bi_op_modrdn = ldap_back_modrdn;
73         bi->bi_op_add = ldap_back_add;
74         bi->bi_op_delete = ldap_back_delete;
75         bi->bi_op_abandon = 0;
76
77         bi->bi_extended = ldap_back_extended;
78
79         bi->bi_chk_referrals = 0;
80         bi->bi_entry_get_rw = ldap_back_entry_get;
81
82         bi->bi_connection_init = 0;
83         bi->bi_connection_destroy = ldap_back_conn_destroy;
84
85         return 0;
86 }
87
88 int
89 ldap_back_db_init(
90     Backend     *be
91 )
92 {
93         struct ldapinfo *li;
94         struct ldapmapping *mapping;
95
96         li = (struct ldapinfo *) ch_calloc( 1, sizeof(struct ldapinfo) );
97         if ( li == NULL ) {
98                 return -1;
99         }
100
101         BER_BVZERO( &li->binddn );
102         BER_BVZERO( &li->bindpw );
103
104 #ifdef LDAP_BACK_PROXY_AUTHZ
105         BER_BVZERO( &li->proxyauthzdn );
106         BER_BVZERO( &li->proxyauthzpw );
107
108         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
109         BER_BVZERO( &li->idassert_id );
110 #endif /* LDAP_BACK_PROXY_AUTHZ */
111
112 #ifdef ENABLE_REWRITE
113         li->rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
114         if ( li->rwmap.rwm_rw == NULL ) {
115                 ch_free( li );
116                 return -1;
117         }
118
119         {
120                 char    *rargv[3];
121
122                 /*
123                  * the filter rewrite as a string must be disabled
124                  * by default; it can be re-enabled by adding rules;
125                  * this creates an empty rewriteContext
126                  */
127                 rargv[ 0 ] = "rewriteContext";
128                 rargv[ 1 ] = "searchFilter";
129                 rargv[ 2 ] = NULL;
130                 rewrite_parse( li->rwmap.rwm_rw, "<suffix massage>", 
131                                 1, 2, rargv );
132
133                 rargv[ 0 ] = "rewriteContext";
134                 rargv[ 1 ] = "default";
135                 rargv[ 2 ] = NULL;
136                 rewrite_parse( li->rwmap.rwm_rw, "<suffix massage>", 
137                                 1, 2, rargv );
138         }
139 #endif /* ENABLE_REWRITE */
140
141         ldap_pvt_thread_mutex_init( &li->conn_mutex );
142
143         ldap_back_map_init( &li->rwmap.rwm_oc, &mapping );
144         ldap_back_map_init( &li->rwmap.rwm_at, &mapping );
145
146         li->be = be;
147         be->be_private = li;
148         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
149
150         return 0;
151 }
152
153 void
154 ldap_back_conn_free( 
155         void *v_lc
156 )
157 {
158         struct ldapconn *lc = v_lc;
159         ldap_unbind( lc->ld );
160         if ( lc->bound_dn.bv_val ) {
161                 ch_free( lc->bound_dn.bv_val );
162         }
163         if ( lc->cred.bv_val ) {
164                 memset( lc->cred.bv_val, 0, lc->cred.bv_len );
165                 ch_free( lc->cred.bv_val );
166         }
167         if ( lc->local_dn.bv_val ) {
168                 ch_free( lc->local_dn.bv_val );
169         }
170         ldap_pvt_thread_mutex_destroy( &lc->lc_mutex );
171         ch_free( lc );
172 }
173
174 void
175 mapping_free( void *v_mapping )
176 {
177         struct ldapmapping *mapping = v_mapping;
178         ch_free( mapping->src.bv_val );
179         ch_free( mapping->dst.bv_val );
180         ch_free( mapping );
181 }
182
183 int
184 ldap_back_db_destroy(
185     Backend     *be
186 )
187 {
188         struct ldapinfo *li;
189
190         if (be->be_private) {
191                 li = (struct ldapinfo *)be->be_private;
192
193                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
194
195                 if (li->url) {
196                         ch_free(li->url);
197                         li->url = NULL;
198                 }
199                 if ( li->lud ) {
200                         ldap_free_urldesc( li->lud );
201                         li->lud = NULL;
202                 }
203                 if ( !BER_BVISNULL( &li->binddn ) ) {
204                         ch_free( li->binddn.bv_val );
205                         BER_BVZERO( &li->binddn );
206                 }
207                 if ( !BER_BVISNULL( &li->bindpw ) ) {
208                         ch_free( li->bindpw.bv_val );
209                         BER_BVZERO( &li->bindpw );
210                 }
211 #ifdef LDAP_BACK_PROXY_AUTHZ
212                 if ( !BER_BVISNULL( &li->proxyauthzdn ) ) {
213                         ch_free( li->proxyauthzdn.bv_val );
214                         BER_BVZERO( &li->proxyauthzdn );
215                 }
216                 if ( !BER_BVISNULL( &li->proxyauthzpw ) ) {
217                         ch_free( li->proxyauthzpw.bv_val );
218                         BER_BVZERO( &li->proxyauthzpw );
219                 }
220                 if ( !BER_BVISNULL( &li->idassert_id ) ) {
221                         ch_free( li->idassert_id.bv_val );
222                         BER_BVZERO( &li->idassert_id );
223                 }
224 #endif /* LDAP_BACK_PROXY_AUTHZ */
225                 if (li->conntree) {
226                         avl_free( li->conntree, ldap_back_conn_free );
227                 }
228 #ifdef ENABLE_REWRITE
229                 if (li->rwmap.rwm_rw) {
230                         rewrite_info_delete( &li->rwmap.rwm_rw );
231                 }
232 #else /* !ENABLE_REWRITE */
233                 if (li->rwmap.rwm_suffix_massage) {
234                         ber_bvarray_free( li->rwmap.rwm_suffix_massage );
235                 }
236 #endif /* !ENABLE_REWRITE */
237
238                 avl_free( li->rwmap.rwm_oc.remap, NULL );
239                 avl_free( li->rwmap.rwm_oc.map, mapping_free );
240                 avl_free( li->rwmap.rwm_at.remap, NULL );
241                 avl_free( li->rwmap.rwm_at.map, mapping_free );
242                 
243                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
244                 ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
245         }
246
247         ch_free( be->be_private );
248         return 0;
249 }