]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
2f285fb0b32cab32126510c68e6a873e904b3f08
[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 = ldap_back_db_open;
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->acl_authcDN );
102         BER_BVZERO( &li->acl_passwd );
103
104 #ifdef LDAP_BACK_PROXY_AUTHZ
105         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
106
107         BER_BVZERO( &li->idassert_authcID );
108         BER_BVZERO( &li->idassert_authcDN );
109         BER_BVZERO( &li->idassert_passwd );
110
111         BER_BVZERO( &li->idassert_authzID );
112         li->idassert_authz = NULL;
113
114         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
115         li->idassert_sasl_flags = LDAP_SASL_QUIET;
116         BER_BVZERO( &li->idassert_sasl_mech );
117         BER_BVZERO( &li->idassert_sasl_realm );
118
119         li->idassert_ppolicy = 0;
120
121         /* by default, use proxyAuthz control on each operation */
122         li->idassert_flags = LDAP_BACK_AUTH_NONE;
123 #endif /* LDAP_BACK_PROXY_AUTHZ */
124
125 #ifdef ENABLE_REWRITE
126         li->rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
127         if ( li->rwmap.rwm_rw == NULL ) {
128                 ch_free( li );
129                 return -1;
130         }
131
132         {
133                 char    *rargv[3];
134
135                 /*
136                  * the filter rewrite as a string must be disabled
137                  * by default; it can be re-enabled by adding rules;
138                  * this creates an empty rewriteContext
139                  */
140                 rargv[ 0 ] = "rewriteContext";
141                 rargv[ 1 ] = "searchFilter";
142                 rargv[ 2 ] = NULL;
143                 rewrite_parse( li->rwmap.rwm_rw, "<suffix massage>", 
144                                 1, 2, rargv );
145
146                 rargv[ 0 ] = "rewriteContext";
147                 rargv[ 1 ] = "default";
148                 rargv[ 2 ] = NULL;
149                 rewrite_parse( li->rwmap.rwm_rw, "<suffix massage>", 
150                                 1, 2, rargv );
151         }
152 #endif /* ENABLE_REWRITE */
153
154         ldap_pvt_thread_mutex_init( &li->conn_mutex );
155
156         ldap_back_map_init( &li->rwmap.rwm_oc, &mapping );
157         ldap_back_map_init( &li->rwmap.rwm_at, &mapping );
158
159         be->be_private = li;
160         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
161
162         return 0;
163 }
164
165 int
166 ldap_back_db_open( BackendDB *be )
167 {
168         struct ldapinfo *li = (struct ldapinfo *)be->be_private;
169
170 #ifdef LDAP_BACK_PROXY_AUTHZ
171         /* by default, use proxyAuthz control on each operation */
172         switch ( li->idassert_mode ) {
173         case LDAP_BACK_IDASSERT_LEGACY:
174         case LDAP_BACK_IDASSERT_SELF:
175                 /* however, since admin connections are pooled and shared,
176                  * only static authzIDs can be native */
177                 li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
178                 break;
179
180         default:
181                 break;
182         }
183 #endif /* LDAP_BACK_PROXY_AUTHZ */
184
185         return 0;
186 }
187
188 void
189 ldap_back_conn_free( 
190         void *v_lc
191 )
192 {
193         struct ldapconn *lc = v_lc;
194         ldap_unbind( lc->ld );
195         if ( lc->bound_dn.bv_val ) {
196                 ch_free( lc->bound_dn.bv_val );
197         }
198         if ( lc->cred.bv_val ) {
199                 memset( lc->cred.bv_val, 0, lc->cred.bv_len );
200                 ch_free( lc->cred.bv_val );
201         }
202         if ( lc->local_dn.bv_val ) {
203                 ch_free( lc->local_dn.bv_val );
204         }
205         ldap_pvt_thread_mutex_destroy( &lc->lc_mutex );
206         ch_free( lc );
207 }
208
209 void
210 mapping_free( void *v_mapping )
211 {
212         struct ldapmapping *mapping = v_mapping;
213         ch_free( mapping->src.bv_val );
214         ch_free( mapping->dst.bv_val );
215         ch_free( mapping );
216 }
217
218 int
219 ldap_back_db_destroy(
220     Backend     *be
221 )
222 {
223         struct ldapinfo *li;
224
225         if (be->be_private) {
226                 li = (struct ldapinfo *)be->be_private;
227
228                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
229
230                 if (li->url) {
231                         ch_free(li->url);
232                         li->url = NULL;
233                 }
234                 if ( li->lud ) {
235                         ldap_free_urldesc( li->lud );
236                         li->lud = NULL;
237                 }
238                 if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
239                         ch_free( li->acl_authcDN.bv_val );
240                         BER_BVZERO( &li->acl_authcDN );
241                 }
242                 if ( !BER_BVISNULL( &li->acl_passwd ) ) {
243                         ch_free( li->acl_passwd.bv_val );
244                         BER_BVZERO( &li->acl_passwd );
245                 }
246 #ifdef LDAP_BACK_PROXY_AUTHZ
247                 if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
248                         ch_free( li->idassert_authcID.bv_val );
249                         BER_BVZERO( &li->idassert_authcID );
250                 }
251                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
252                         ch_free( li->idassert_authcDN.bv_val );
253                         BER_BVZERO( &li->idassert_authcDN );
254                 }
255                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
256                         ch_free( li->idassert_passwd.bv_val );
257                         BER_BVZERO( &li->idassert_passwd );
258                 }
259                 if ( !BER_BVISNULL( &li->idassert_authzID ) ) {
260                         ch_free( li->idassert_authzID.bv_val );
261                         BER_BVZERO( &li->idassert_authzID );
262                 }
263                 if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
264                         ch_free( li->idassert_sasl_mech.bv_val );
265                         BER_BVZERO( &li->idassert_sasl_mech );
266                 }
267                 if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
268                         ch_free( li->idassert_sasl_realm.bv_val );
269                         BER_BVZERO( &li->idassert_sasl_realm );
270                 }
271 #endif /* LDAP_BACK_PROXY_AUTHZ */
272                 if (li->conntree) {
273                         avl_free( li->conntree, ldap_back_conn_free );
274                 }
275 #ifdef ENABLE_REWRITE
276                 if (li->rwmap.rwm_rw) {
277                         rewrite_info_delete( &li->rwmap.rwm_rw );
278                 }
279 #else /* !ENABLE_REWRITE */
280                 if (li->rwmap.rwm_suffix_massage) {
281                         ber_bvarray_free( li->rwmap.rwm_suffix_massage );
282                 }
283 #endif /* !ENABLE_REWRITE */
284
285                 avl_free( li->rwmap.rwm_oc.remap, NULL );
286                 avl_free( li->rwmap.rwm_oc.map, mapping_free );
287                 avl_free( li->rwmap.rwm_at.remap, NULL );
288                 avl_free( li->rwmap.rwm_at.map, mapping_free );
289                 
290                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
291                 ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
292         }
293
294         ch_free( be->be_private );
295         return 0;
296 }