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