]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
ce2711258681be62bc944ea05c5144f09a1739ab
[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 int
36 ldap_back_open( BackendInfo     *bi )
37 {
38         bi->bi_controls = slap_known_controls;
39         return 0;
40 }
41
42 int
43 ldap_back_initialize( BackendInfo *bi )
44 {
45         bi->bi_open = ldap_back_open;
46         bi->bi_config = 0;
47         bi->bi_close = 0;
48         bi->bi_destroy = 0;
49
50         bi->bi_db_init = ldap_back_db_init;
51         bi->bi_db_config = ldap_back_db_config;
52         bi->bi_db_open = ldap_back_db_open;
53         bi->bi_db_close = 0;
54         bi->bi_db_destroy = ldap_back_db_destroy;
55
56         bi->bi_op_bind = ldap_back_bind;
57         bi->bi_op_unbind = 0;
58         bi->bi_op_search = ldap_back_search;
59         bi->bi_op_compare = ldap_back_compare;
60         bi->bi_op_modify = ldap_back_modify;
61         bi->bi_op_modrdn = ldap_back_modrdn;
62         bi->bi_op_add = ldap_back_add;
63         bi->bi_op_delete = ldap_back_delete;
64         bi->bi_op_abandon = 0;
65
66         bi->bi_extended = ldap_back_extended;
67
68         bi->bi_chk_referrals = 0;
69         bi->bi_entry_get_rw = ldap_back_entry_get;
70
71         bi->bi_connection_init = 0;
72         bi->bi_connection_destroy = ldap_back_conn_destroy;
73
74         return 0;
75 }
76
77 int
78 ldap_back_db_init( Backend *be )
79 {
80         struct ldapinfo *li;
81
82         li = (struct ldapinfo *)ch_calloc( 1, sizeof( struct ldapinfo ) );
83         if ( li == NULL ) {
84                 return -1;
85         }
86
87         BER_BVZERO( &li->acl_authcDN );
88         BER_BVZERO( &li->acl_passwd );
89
90 #ifdef LDAP_BACK_PROXY_AUTHZ
91         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
92
93         BER_BVZERO( &li->idassert_authcID );
94         BER_BVZERO( &li->idassert_authcDN );
95         BER_BVZERO( &li->idassert_passwd );
96
97         BER_BVZERO( &li->idassert_authzID );
98         li->idassert_authz = NULL;
99
100         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
101         li->idassert_sasl_flags = LDAP_SASL_QUIET;
102         BER_BVZERO( &li->idassert_sasl_mech );
103         BER_BVZERO( &li->idassert_sasl_realm );
104
105         li->idassert_ppolicy = 0;
106
107         /* by default, use proxyAuthz control on each operation */
108         li->idassert_flags = LDAP_BACK_AUTH_NONE;
109 #endif /* LDAP_BACK_PROXY_AUTHZ */
110
111         ldap_pvt_thread_mutex_init( &li->conn_mutex );
112
113         be->be_private = li;
114         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_NOLASTMOD;
115
116         return 0;
117 }
118
119 int
120 ldap_back_db_open( BackendDB *be )
121 {
122         struct ldapinfo *li = (struct ldapinfo *)be->be_private;
123
124         Debug( LDAP_DEBUG_TRACE,
125                 "ldap_back_db_open: URI=%s\n", li->url, 0, 0 );
126
127 #ifdef LDAP_BACK_PROXY_AUTHZ
128         /* by default, use proxyAuthz control on each operation */
129         switch ( li->idassert_mode ) {
130         case LDAP_BACK_IDASSERT_LEGACY:
131         case LDAP_BACK_IDASSERT_SELF:
132                 /* however, since admin connections are pooled and shared,
133                  * only static authzIDs can be native */
134                 li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
135                 break;
136
137         default:
138                 break;
139         }
140 #endif /* LDAP_BACK_PROXY_AUTHZ */
141
142 #if 0 && defined(SLAPD_MONITOR)
143         {
144                 struct berval   filter,
145                                 base = BER_BVC( "cn=Databases,cn=Monitor" );
146                 const char      *text;
147                 struct berval   vals[ 2 ];
148                 Attribute       a = { 0 };
149
150                 filter.bv_len = STRLENOF( "(&(namingContexts=)(monitoredInfo=ldap))" )
151                         + be->be_nsuffix[ 0 ].bv_len;
152                 filter.bv_val = ch_malloc( filter.bv_len + 1 );
153                 snprintf( filter.bv_val, filter.bv_len + 1,
154                                 "(&(namingContexts=%s)(monitoredInfo=ldap))",
155                                 be->be_nsuffix[ 0 ].bv_val );
156
157                 a.a_desc = slap_schema.si_ad_labeledURI;
158                 ber_str2bv( li->url, 0, 0, &vals[ 0 ] );
159                 BER_BVZERO( &vals[ 1 ] );
160                 a.a_vals = vals;
161                 a.a_nvals = vals;
162                 if ( monitor_back_register_entry_attrs( NULL, &a, NULL, &base, LDAP_SCOPE_SUBTREE, &filter ) ) {
163                         /* error */
164                 }
165         }
166 #endif /* SLAPD_MONITOR */
167
168         return 0;
169 }
170
171 void
172 ldap_back_conn_free( void *v_lc )
173 {
174         struct ldapconn *lc = v_lc;
175         
176         ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
177         if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
178                 ch_free( lc->lc_bound_ndn.bv_val );
179         }
180         if ( !BER_BVISNULL( &lc->lc_cred ) ) {
181                 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
182                 ch_free( lc->lc_cred.bv_val );
183         }
184         if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
185                 ch_free( lc->lc_local_ndn.bv_val );
186         }
187         ldap_pvt_thread_mutex_destroy( &lc->lc_mutex );
188         ch_free( lc );
189 }
190
191 int
192 ldap_back_db_destroy(
193     Backend     *be
194 )
195 {
196         struct ldapinfo *li;
197
198         if ( be->be_private ) {
199                 li = ( struct ldapinfo * )be->be_private;
200
201                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
202
203                 if ( li->url ) {
204                         ch_free( li->url );
205                         li->url = NULL;
206                 }
207                 if ( li->lud ) {
208                         ldap_free_urldesc( li->lud );
209                         li->lud = NULL;
210                 }
211                 if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
212                         ch_free( li->acl_authcDN.bv_val );
213                         BER_BVZERO( &li->acl_authcDN );
214                 }
215                 if ( !BER_BVISNULL( &li->acl_passwd ) ) {
216                         ch_free( li->acl_passwd.bv_val );
217                         BER_BVZERO( &li->acl_passwd );
218                 }
219 #ifdef LDAP_BACK_PROXY_AUTHZ
220                 if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
221                         ch_free( li->idassert_authcID.bv_val );
222                         BER_BVZERO( &li->idassert_authcID );
223                 }
224                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
225                         ch_free( li->idassert_authcDN.bv_val );
226                         BER_BVZERO( &li->idassert_authcDN );
227                 }
228                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
229                         ch_free( li->idassert_passwd.bv_val );
230                         BER_BVZERO( &li->idassert_passwd );
231                 }
232                 if ( !BER_BVISNULL( &li->idassert_authzID ) ) {
233                         ch_free( li->idassert_authzID.bv_val );
234                         BER_BVZERO( &li->idassert_authzID );
235                 }
236                 if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
237                         ch_free( li->idassert_sasl_mech.bv_val );
238                         BER_BVZERO( &li->idassert_sasl_mech );
239                 }
240                 if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
241                         ch_free( li->idassert_sasl_realm.bv_val );
242                         BER_BVZERO( &li->idassert_sasl_realm );
243                 }
244 #endif /* LDAP_BACK_PROXY_AUTHZ */
245                 if ( li->conntree ) {
246                         avl_free( li->conntree, ldap_back_conn_free );
247                 }
248
249                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
250                 ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
251         }
252
253         ch_free( be->be_private );
254
255         return 0;
256 }
257
258 #if SLAPD_LDAP == SLAPD_MOD_DYNAMIC
259
260 int
261 init_module( int argc, char *argv[] )
262 {
263         BackendInfo     bi;
264
265         memset( &bi, '\0', sizeof( bi ) );
266         bi.bi_type = "ldap";
267         bi.bi_init = ldap_back_initialize;
268
269         backend_add( &bi );
270     
271         return 0;
272 }
273
274 #endif /* SLAPD_LDAP */
275