]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
5bf32766f276f1d0d5953880892204e8a86af27c
[openldap] / servers / slapd / back-ldap / init.c
1 /* init.c - initialize ldap backend */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7 /* This is an altered version */
8 /*
9  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
10  * 
11  * Permission is granted to anyone to use this software for any purpose
12  * on any computer system, and to alter it and redistribute it, subject
13  * to the following restrictions:
14  * 
15  * 1. The author is not responsible for the consequences of use of this
16  *    software, no matter how awful, even if they arise from flaws in it.
17  * 
18  * 2. The origin of this software must not be misrepresented, either by
19  *    explicit claim or by omission.  Since few users ever read sources,
20  *    credits should appear in the documentation.
21  * 
22  * 3. Altered versions must be plainly marked as such, and must not be
23  *    misrepresented as being the original software.  Since few users
24  *    ever read sources, credits should appear in the documentation.
25  * 
26  * 4. This notice may not be removed or altered.
27  *
28  *
29  *
30  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
31  * 
32  * This software is being modified by Pierangelo Masarati.
33  * The previously reported conditions apply to the modified code as well.
34  * Changes in the original code are highlighted where required.
35  * Credits for the original code go to the author, Howard Chu.
36  */
37
38 #include "portable.h"
39
40 #include <stdio.h>
41
42 #include <ac/string.h>
43 #include <ac/socket.h>
44
45 #include "slap.h"
46 #include "back-ldap.h"
47
48 #ifdef SLAPD_LDAP_DYNAMIC
49
50 int back_ldap_LTX_init_module(int argc, char *argv[]) {
51     BackendInfo bi;
52
53     memset( &bi, '\0', sizeof(bi) );
54     bi.bi_type = "ldap";
55     bi.bi_init = ldap_back_initialize;
56
57     backend_add(&bi);
58     return 0;
59 }
60
61 #endif /* SLAPD_LDAP_DYNAMIC */
62
63 int
64 ldap_back_initialize(
65     BackendInfo *bi
66 )
67 {
68         bi->bi_controls = slap_known_controls;
69
70         bi->bi_open = 0;
71         bi->bi_config = 0;
72         bi->bi_close = 0;
73         bi->bi_destroy = 0;
74
75         bi->bi_db_init = ldap_back_db_init;
76         bi->bi_db_config = ldap_back_db_config;
77         bi->bi_db_open = 0;
78         bi->bi_db_close = 0;
79         bi->bi_db_destroy = ldap_back_db_destroy;
80
81         bi->bi_op_bind = ldap_back_bind;
82         bi->bi_op_unbind = 0;
83         bi->bi_op_search = ldap_back_search;
84         bi->bi_op_compare = ldap_back_compare;
85         bi->bi_op_modify = ldap_back_modify;
86         bi->bi_op_modrdn = ldap_back_modrdn;
87         bi->bi_op_add = ldap_back_add;
88         bi->bi_op_delete = ldap_back_delete;
89         bi->bi_op_abandon = 0;
90
91         bi->bi_extended = ldap_back_extended;
92
93         bi->bi_chk_referrals = 0;
94         bi->bi_entry_get_rw = ldap_back_entry_get;
95
96         bi->bi_connection_init = 0;
97         bi->bi_connection_destroy = ldap_back_conn_destroy;
98
99         return 0;
100 }
101
102 int
103 ldap_back_db_init(
104     Backend     *be
105 )
106 {
107         struct ldapinfo *li;
108         struct ldapmapping *mapping;
109
110         li = (struct ldapinfo *) ch_calloc( 1, sizeof(struct ldapinfo) );
111         if ( li == NULL ) {
112                 return -1;
113         }
114
115         li->binddn.bv_val = NULL;
116         li->binddn.bv_len = 0;
117         li->bindpw.bv_val = NULL;
118         li->bindpw.bv_len = 0;
119
120 #ifdef ENABLE_REWRITE
121         li->rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
122         if ( li->rwmap.rwm_rw == NULL ) {
123                 ch_free( li );
124                 return -1;
125         }
126 #endif /* ENABLE_REWRITE */
127
128         ldap_pvt_thread_mutex_init( &li->conn_mutex );
129
130         ldap_back_map_init( &li->rwmap.rwm_oc, &mapping );
131         ldap_back_map_init( &li->rwmap.rwm_at, &mapping );
132
133         li->be = be;
134         be->be_private = li;
135         be->be_flags |= SLAP_BFLAG_NOLASTMOD;
136
137         return 0;
138 }
139
140 void
141 ldap_back_conn_free( 
142         void *v_lc
143 )
144 {
145         struct ldapconn *lc = v_lc;
146         ldap_unbind( lc->ld );
147         if ( lc->bound_dn.bv_val ) {
148                 ch_free( lc->bound_dn.bv_val );
149         }
150         if ( lc->cred.bv_val ) {
151                 memset( lc->cred.bv_val, 0, lc->cred.bv_len );
152                 ch_free( lc->cred.bv_val );
153         }
154         if ( lc->local_dn.bv_val ) {
155                 ch_free( lc->local_dn.bv_val );
156         }
157         ldap_pvt_thread_mutex_destroy( &lc->lc_mutex );
158         ch_free( lc );
159 }
160
161 void
162 mapping_free( void *v_mapping )
163 {
164         struct ldapmapping *mapping = v_mapping;
165         ch_free( mapping->src.bv_val );
166         ch_free( mapping->dst.bv_val );
167         ch_free( mapping );
168 }
169
170 int
171 ldap_back_db_destroy(
172     Backend     *be
173 )
174 {
175         struct ldapinfo *li;
176
177         if (be->be_private) {
178                 li = (struct ldapinfo *)be->be_private;
179
180                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
181
182                 if (li->url) {
183                         ch_free(li->url);
184                         li->url = NULL;
185                 }
186                 if (li->binddn.bv_val) {
187                         ch_free(li->binddn.bv_val);
188                         li->binddn.bv_val = NULL;
189                 }
190                 if (li->bindpw.bv_val) {
191                         ch_free(li->bindpw.bv_val);
192                         li->bindpw.bv_val = NULL;
193                 }
194                 if (li->conntree) {
195                         avl_free( li->conntree, ldap_back_conn_free );
196                 }
197 #ifdef ENABLE_REWRITE
198                 if (li->rwmap.rwm_rw) {
199                         rewrite_info_delete( li->rwmap.rwm_rw );
200                 }
201 #else /* !ENABLE_REWRITE */
202                 if (li->rwmap.rwm_suffix_massage) {
203                         ber_bvarray_free( li->rwmap.rwm_suffix_massage );
204                 }
205 #endif /* !ENABLE_REWRITE */
206
207                 avl_free( li->rwmap.rwm_oc.remap, NULL );
208                 avl_free( li->rwmap.rwm_oc.map, mapping_free );
209                 avl_free( li->rwmap.rwm_at.remap, NULL );
210                 avl_free( li->rwmap.rwm_at.map, mapping_free );
211                 
212                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
213                 ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
214         }
215
216         ch_free( be->be_private );
217         return 0;
218 }