]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
f0fbb962e38ed37b1ec718901bdee2630fdc007e
[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/socket.h>
43
44 #include "slap.h"
45 #include "back-ldap.h"
46
47 #ifdef SLAPD_LDAP_DYNAMIC
48
49 int back_ldap_LTX_init_module(int argc, char *argv[]) {
50     BackendInfo bi;
51
52     memset( &bi, '\0', sizeof(bi) );
53     bi.bi_type = "ldap";
54     bi.bi_init = ldap_back_initialize;
55
56     backend_add(&bi);
57     return 0;
58 }
59
60 #endif /* SLAPD_LDAP_DYNAMIC */
61
62 int
63 ldap_back_initialize(
64     BackendInfo *bi
65 )
66 {
67         bi->bi_controls = slap_known_controls;
68
69         bi->bi_open = 0;
70         bi->bi_config = 0;
71         bi->bi_close = 0;
72         bi->bi_destroy = 0;
73
74         bi->bi_db_init = ldap_back_db_init;
75         bi->bi_db_config = ldap_back_db_config;
76         bi->bi_db_open = 0;
77         bi->bi_db_close = 0;
78         bi->bi_db_destroy = ldap_back_db_destroy;
79
80         bi->bi_op_bind = ldap_back_bind;
81         bi->bi_op_unbind = 0;
82         bi->bi_op_search = ldap_back_search;
83         bi->bi_op_compare = ldap_back_compare;
84         bi->bi_op_modify = ldap_back_modify;
85         bi->bi_op_modrdn = ldap_back_modrdn;
86         bi->bi_op_add = ldap_back_add;
87         bi->bi_op_delete = ldap_back_delete;
88         bi->bi_op_abandon = 0;
89
90         bi->bi_extended = ldap_back_extended;
91
92         bi->bi_chk_referrals = 0;
93         bi->bi_entry_get_rw = ldap_back_entry_get;
94
95         bi->bi_connection_init = 0;
96         bi->bi_connection_destroy = ldap_back_conn_destroy;
97
98         return 0;
99 }
100
101 int
102 ldap_back_db_init(
103     Backend     *be
104 )
105 {
106         struct ldapinfo *li;
107         struct ldapmapping *mapping;
108
109         li = (struct ldapinfo *) ch_calloc( 1, sizeof(struct ldapinfo) );
110         if ( li == NULL ) {
111                 return -1;
112         }
113
114         li->binddn.bv_val = NULL;
115         li->binddn.bv_len = 0;
116         li->bindpw.bv_val = NULL;
117         li->bindpw.bv_len = 0;
118
119 #ifdef ENABLE_REWRITE
120         li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
121         if ( li->rwinfo == NULL ) {
122                 ch_free( li );
123                 return -1;
124         }
125 #endif /* ENABLE_REWRITE */
126
127         ldap_pvt_thread_mutex_init( &li->conn_mutex );
128
129         ldap_back_map_init( &li->at_map, &mapping );
130
131         li->be = be;
132         be->be_private = li;
133         be->be_flags |= SLAP_BFLAG_NOLASTMOD;
134
135         return 0;
136 }
137
138 void
139 ldap_back_conn_free( 
140         void *v_lc
141 )
142 {
143         struct ldapconn *lc = v_lc;
144         ldap_unbind( lc->ld );
145         if ( lc->bound_dn.bv_val ) {
146                 ch_free( lc->bound_dn.bv_val );
147         }
148         if ( lc->cred.bv_val ) {
149                 ch_free( lc->cred.bv_val );
150         }
151         if ( lc->local_dn.bv_val ) {
152                 ch_free( lc->local_dn.bv_val );
153         }
154         ldap_pvt_thread_mutex_destroy( &lc->lc_mutex );
155         ch_free( lc );
156 }
157
158 void
159 mapping_free( void *v_mapping )
160 {
161         struct ldapmapping *mapping = v_mapping;
162         ch_free( mapping->src.bv_val );
163         ch_free( mapping->dst.bv_val );
164         ch_free( mapping );
165 }
166
167 int
168 ldap_back_db_destroy(
169     Backend     *be
170 )
171 {
172         struct ldapinfo *li;
173
174         if (be->be_private) {
175                 li = (struct ldapinfo *)be->be_private;
176
177                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
178
179                 if (li->url) {
180                         ch_free(li->url);
181                         li->url = NULL;
182                 }
183                 if (li->binddn.bv_val) {
184                         ch_free(li->binddn.bv_val);
185                         li->binddn.bv_val = NULL;
186                 }
187                 if (li->bindpw.bv_val) {
188                         ch_free(li->bindpw.bv_val);
189                         li->bindpw.bv_val = NULL;
190                 }
191                 if (li->conntree) {
192                         avl_free( li->conntree, ldap_back_conn_free );
193                 }
194 #ifdef ENABLE_REWRITE
195                 if (li->rwinfo) {
196                         rewrite_info_delete( li->rwinfo );
197                 }
198 #else /* !ENABLE_REWRITE */
199                 if (li->suffix_massage) {
200                         ber_bvarray_free( li->suffix_massage );
201                 }
202 #endif /* !ENABLE_REWRITE */
203
204                 avl_free( li->oc_map.remap, NULL );
205                 avl_free( li->oc_map.map, mapping_free );
206                 avl_free( li->at_map.remap, NULL );
207                 avl_free( li->at_map.map, mapping_free );
208                 
209                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
210                 ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
211         }
212
213         ch_free( be->be_private );
214         return 0;
215 }