]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
bbf35ef531e3bd1fbab427a69786eb18796e189b
[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 #include "external.h"
34
35 #if SLAPD_LDAP == SLAPD_MOD_DYNAMIC
36
37 int init_module(int argc, char *argv[]) {
38     BackendInfo bi;
39
40     memset( &bi, '\0', sizeof(bi) );
41     bi.bi_type = "ldap";
42     bi.bi_init = ldap_back_initialize;
43
44     backend_add(&bi);
45     return 0;
46 }
47
48 #endif /* SLAPD_LDAP */
49
50 int
51 ldap_back_open(
52         BackendInfo *bi
53 )
54 {
55         bi->bi_controls = slap_known_controls;
56         return 0;
57 }
58
59 int
60 ldap_back_initialize(
61     BackendInfo *bi
62 )
63 {
64         bi->bi_open = ldap_back_open;
65         bi->bi_config = 0;
66         bi->bi_close = 0;
67         bi->bi_destroy = 0;
68
69         bi->bi_db_init = ldap_back_db_init;
70         bi->bi_db_config = ldap_back_db_config;
71         bi->bi_db_open = ldap_back_db_open;
72         bi->bi_db_close = 0;
73         bi->bi_db_destroy = ldap_back_db_destroy;
74
75         bi->bi_op_bind = ldap_back_bind;
76         bi->bi_op_unbind = 0;
77         bi->bi_op_search = ldap_back_search;
78         bi->bi_op_compare = ldap_back_compare;
79         bi->bi_op_modify = ldap_back_modify;
80         bi->bi_op_modrdn = ldap_back_modrdn;
81         bi->bi_op_add = ldap_back_add;
82         bi->bi_op_delete = ldap_back_delete;
83         bi->bi_op_abandon = 0;
84
85         bi->bi_extended = ldap_back_extended;
86
87         bi->bi_chk_referrals = 0;
88         bi->bi_entry_get_rw = ldap_back_entry_get;
89
90         bi->bi_connection_init = 0;
91         bi->bi_connection_destroy = ldap_back_conn_destroy;
92
93         return 0;
94 }
95
96 int
97 ldap_back_db_init(
98     Backend     *be
99 )
100 {
101         struct ldapinfo *li;
102         struct ldapmapping *mapping;
103
104         li = (struct ldapinfo *) ch_calloc( 1, sizeof(struct ldapinfo) );
105         if ( li == NULL ) {
106                 return -1;
107         }
108
109         BER_BVZERO( &li->acl_authcDN );
110         BER_BVZERO( &li->acl_passwd );
111
112 #ifdef LDAP_BACK_PROXY_AUTHZ
113         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
114
115         BER_BVZERO( &li->idassert_authcID );
116         BER_BVZERO( &li->idassert_authcDN );
117         BER_BVZERO( &li->idassert_passwd );
118
119         BER_BVZERO( &li->idassert_authzID );
120         li->idassert_authz = NULL;
121
122         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
123         li->idassert_sasl_flags = LDAP_SASL_QUIET;
124         BER_BVZERO( &li->idassert_sasl_mech );
125         BER_BVZERO( &li->idassert_sasl_realm );
126
127         li->idassert_ppolicy = 0;
128
129         /* by default, use proxyAuthz control on each operation */
130         li->idassert_flags = LDAP_BACK_AUTH_NONE;
131 #endif /* LDAP_BACK_PROXY_AUTHZ */
132
133 #ifdef ENABLE_REWRITE
134         li->rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
135         if ( li->rwmap.rwm_rw == NULL ) {
136                 ch_free( li );
137                 return -1;
138         }
139
140         {
141                 char    *rargv[3];
142
143                 /*
144                  * the filter rewrite as a string must be disabled
145                  * by default; it can be re-enabled by adding rules;
146                  * this creates an empty rewriteContext
147                  */
148                 rargv[ 0 ] = "rewriteContext";
149                 rargv[ 1 ] = "searchFilter";
150                 rargv[ 2 ] = NULL;
151                 rewrite_parse( li->rwmap.rwm_rw, "<suffix massage>", 
152                                 1, 2, rargv );
153
154                 rargv[ 0 ] = "rewriteContext";
155                 rargv[ 1 ] = "default";
156                 rargv[ 2 ] = NULL;
157                 rewrite_parse( li->rwmap.rwm_rw, "<suffix massage>", 
158                                 1, 2, rargv );
159         }
160 #endif /* ENABLE_REWRITE */
161
162         ldap_pvt_thread_mutex_init( &li->conn_mutex );
163
164         ldap_back_map_init( &li->rwmap.rwm_oc, &mapping );
165         ldap_back_map_init( &li->rwmap.rwm_at, &mapping );
166
167         be->be_private = li;
168         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
169
170         return 0;
171 }
172
173 int
174 ldap_back_db_open( BackendDB *be )
175 {
176         struct ldapinfo *li = (struct ldapinfo *)be->be_private;
177
178         Debug( LDAP_DEBUG_TRACE,
179                 "ldap_back_db_open: URI=%s\n",  li->url, 0, 0 );
180
181 #ifdef LDAP_BACK_PROXY_AUTHZ
182         /* by default, use proxyAuthz control on each operation */
183         switch ( li->idassert_mode ) {
184         case LDAP_BACK_IDASSERT_LEGACY:
185         case LDAP_BACK_IDASSERT_SELF:
186                 /* however, since admin connections are pooled and shared,
187                  * only static authzIDs can be native */
188                 li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
189                 break;
190
191         default:
192                 break;
193         }
194 #endif /* LDAP_BACK_PROXY_AUTHZ */
195
196         return 0;
197 }
198
199 void
200 ldap_back_conn_free( 
201         void *v_lc
202 )
203 {
204         struct ldapconn *lc = v_lc;
205         ldap_unbind( lc->ld );
206         if ( lc->bound_dn.bv_val ) {
207                 ch_free( lc->bound_dn.bv_val );
208         }
209         if ( lc->cred.bv_val ) {
210                 memset( lc->cred.bv_val, 0, lc->cred.bv_len );
211                 ch_free( lc->cred.bv_val );
212         }
213         if ( lc->local_dn.bv_val ) {
214                 ch_free( lc->local_dn.bv_val );
215         }
216         ldap_pvt_thread_mutex_destroy( &lc->lc_mutex );
217         ch_free( lc );
218 }
219
220 static void
221 mapping_free( void *v_mapping )
222 {
223         struct ldapmapping *mapping = v_mapping;
224         ch_free( mapping->src.bv_val );
225         ch_free( mapping->dst.bv_val );
226         ch_free( mapping );
227 }
228
229 int
230 ldap_back_db_destroy(
231     Backend     *be
232 )
233 {
234         struct ldapinfo *li;
235
236         if (be->be_private) {
237                 li = (struct ldapinfo *)be->be_private;
238
239                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
240
241                 if (li->url) {
242                         ch_free(li->url);
243                         li->url = NULL;
244                 }
245                 if ( li->lud ) {
246                         ldap_free_urldesc( li->lud );
247                         li->lud = NULL;
248                 }
249                 if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
250                         ch_free( li->acl_authcDN.bv_val );
251                         BER_BVZERO( &li->acl_authcDN );
252                 }
253                 if ( !BER_BVISNULL( &li->acl_passwd ) ) {
254                         ch_free( li->acl_passwd.bv_val );
255                         BER_BVZERO( &li->acl_passwd );
256                 }
257 #ifdef LDAP_BACK_PROXY_AUTHZ
258                 if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
259                         ch_free( li->idassert_authcID.bv_val );
260                         BER_BVZERO( &li->idassert_authcID );
261                 }
262                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
263                         ch_free( li->idassert_authcDN.bv_val );
264                         BER_BVZERO( &li->idassert_authcDN );
265                 }
266                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
267                         ch_free( li->idassert_passwd.bv_val );
268                         BER_BVZERO( &li->idassert_passwd );
269                 }
270                 if ( !BER_BVISNULL( &li->idassert_authzID ) ) {
271                         ch_free( li->idassert_authzID.bv_val );
272                         BER_BVZERO( &li->idassert_authzID );
273                 }
274                 if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
275                         ch_free( li->idassert_sasl_mech.bv_val );
276                         BER_BVZERO( &li->idassert_sasl_mech );
277                 }
278                 if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
279                         ch_free( li->idassert_sasl_realm.bv_val );
280                         BER_BVZERO( &li->idassert_sasl_realm );
281                 }
282 #endif /* LDAP_BACK_PROXY_AUTHZ */
283                 if (li->conntree) {
284                         avl_free( li->conntree, ldap_back_conn_free );
285                 }
286 #ifdef ENABLE_REWRITE
287                 if (li->rwmap.rwm_rw) {
288                         rewrite_info_delete( &li->rwmap.rwm_rw );
289                 }
290 #else /* !ENABLE_REWRITE */
291                 if (li->rwmap.rwm_suffix_massage) {
292                         ber_bvarray_free( li->rwmap.rwm_suffix_massage );
293                 }
294 #endif /* !ENABLE_REWRITE */
295
296                 avl_free( li->rwmap.rwm_oc.remap, NULL );
297                 avl_free( li->rwmap.rwm_oc.map, mapping_free );
298                 avl_free( li->rwmap.rwm_at.remap, NULL );
299                 avl_free( li->rwmap.rwm_at.map, mapping_free );
300                 
301                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
302                 ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
303         }
304
305         ch_free( be->be_private );
306         return 0;
307 }