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