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