3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* backend.c - routines for dealing with back-end databases */
13 #include <ac/string.h>
14 #include <ac/socket.h>
22 #include "back-dnssrv/external.h"
25 #include "back-ldap/external.h"
28 #include "back-ldbm/external.h"
31 #include "back-passwd/external.h"
34 #include "back-perl/external.h"
37 #include "back-shell/external.h"
40 #include "back-tcl/external.h"
43 #include "back-domain/external.h"
46 #include "back-sql/external.h"
49 static BackendInfo binfo[] = {
50 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
51 {"dnssrv", dnssrv_back_initialize},
53 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
54 {"ldap", ldap_back_initialize},
56 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
57 {"ldbm", ldbm_back_initialize},
59 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
60 {"passwd", passwd_back_initialize},
62 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
63 {"perl", perl_back_initialize},
65 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
66 {"shell", shell_back_initialize},
68 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
69 {"tcl", tcl_back_initialize},
71 #if defined(SLAPD_NTDOMAIN) && !defined(SLAPD_NTDOMAIN_DYNAMIC)
72 {"ntdom", domain_back_initialize},
74 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
75 {"sql", sql_back_initialize},
81 BackendInfo *backendInfo = NULL;
84 BackendDB *backendDB = NULL;
86 int backend_init(void)
90 if((nBackendInfo != 0) || (backendInfo != NULL)) {
91 /* already initialized */
92 Debug( LDAP_DEBUG_ANY,
93 "backend_init: already initialized.\n", 0, 0, 0 );
98 binfo[nBackendInfo].bi_type != NULL;
101 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
104 Debug( LDAP_DEBUG_ANY,
105 "backend_init: initialized for type \"%s\"\n",
106 binfo[nBackendInfo].bi_type, 0, 0 );
108 /* destroy those we've already inited */
113 if ( binfo[nBackendInfo].bi_destroy ) {
114 binfo[nBackendInfo].bi_destroy(
115 &binfo[nBackendInfo] );
122 if ( nBackendInfo > 0) {
130 Debug( LDAP_DEBUG_ANY,
131 "backend_init: failed\n",
135 #endif /* SLAPD_MODULES */
138 int backend_add(BackendInfo *aBackendInfo)
142 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
143 Debug( LDAP_DEBUG_ANY,
144 "backend_add: initialization for type \"%s\" failed\n",
145 aBackendInfo->bi_type, 0, 0 );
149 /* now add the backend type to the Backend Info List */
151 BackendInfo *newBackendInfo = 0;
153 /* if backendInfo == binfo no deallocation of old backendInfo */
154 if (backendInfo == binfo) {
155 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
156 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
159 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
162 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
163 sizeof(BackendInfo));
164 backendInfo = newBackendInfo;
171 int backend_startup(Backend *be)
176 if( ! ( nBackendDB > 0 ) ) {
178 Debug( LDAP_DEBUG_ANY,
179 "backend_startup: %d databases to startup.\n",
185 /* startup a specific backend database */
186 Debug( LDAP_DEBUG_TRACE,
187 "backend_startup: starting database\n",
190 if ( be->bd_info->bi_open ) {
191 rc = be->bd_info->bi_open( be->bd_info );
195 Debug( LDAP_DEBUG_ANY,
196 "backend_startup: bi_open failed!\n",
201 if ( be->bd_info->bi_db_open ) {
202 rc = be->bd_info->bi_db_open( be );
206 Debug( LDAP_DEBUG_ANY,
207 "backend_startup: bi_db_open failed!\n",
215 /* open each backend type */
216 for( i = 0; i < nBackendInfo; i++ ) {
217 if( backendInfo[i].bi_nDB == 0) {
218 /* no database of this type, don't open */
222 if( backendInfo[i].bi_open ) {
223 rc = backendInfo[i].bi_open(
228 Debug( LDAP_DEBUG_ANY,
229 "backend_startup: bi_open %d failed!\n",
235 /* open each backend database */
236 for( i = 0; i < nBackendDB; i++ ) {
237 /* append global access controls */
238 acl_append( &backendDB[i].be_acl, global_acl );
240 if ( backendDB[i].bd_info->bi_db_open ) {
241 rc = backendDB[i].bd_info->bi_db_open(
246 Debug( LDAP_DEBUG_ANY,
247 "backend_startup: bi_db_open %d failed!\n",
256 int backend_num( Backend *be )
260 if( be == NULL ) return -1;
262 for( i = 0; i < nBackendDB; i++ ) {
263 if( be == &backendDB[i] ) return i;
268 int backend_shutdown( Backend *be )
274 /* shutdown a specific backend database */
276 if ( be->bd_info->bi_nDB == 0 ) {
277 /* no database of this type, we never opened it */
281 if ( be->bd_info->bi_db_close ) {
282 be->bd_info->bi_db_close( be );
285 if( be->bd_info->bi_close ) {
286 be->bd_info->bi_close( be->bd_info );
292 /* close each backend database */
293 for( i = 0; i < nBackendDB; i++ ) {
294 if ( backendDB[i].bd_info->bi_db_close ) {
295 backendDB[i].bd_info->bi_db_close(
300 Debug( LDAP_DEBUG_ANY,
301 "backend_close: bi_close %s failed!\n",
302 backendDB[i].be_type, 0, 0 );
306 /* close each backend type */
307 for( i = 0; i < nBackendInfo; i++ ) {
308 if( backendInfo[i].bi_nDB == 0 ) {
309 /* no database of this type */
313 if( backendInfo[i].bi_close ) {
314 backendInfo[i].bi_close(
322 int backend_destroy(void)
326 /* destroy each backend database */
327 for( i = 0; i < nBackendDB; i++ ) {
328 if ( backendDB[i].bd_info->bi_db_destroy ) {
329 backendDB[i].bd_info->bi_db_destroy(
334 /* destroy each backend type */
335 for( i = 0; i < nBackendInfo; i++ ) {
336 if( backendInfo[i].bi_destroy ) {
337 backendInfo[i].bi_destroy(
343 if (backendInfo != binfo) {
346 #endif /* SLAPD_MODULES */
354 BackendInfo* backend_info(const char *type)
358 /* search for the backend type */
359 for( i = 0; i < nBackendInfo; i++ ) {
360 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
361 return &backendInfo[i];
375 BackendInfo *bi = backend_info(type);
379 fprintf( stderr, "Unrecognized database type (%s)\n", type );
383 backendDB = (BackendDB *) ch_realloc(
385 (nBackendDB + 1) * sizeof(Backend) );
387 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
389 be = &backends[nbackends++];
392 be->be_sizelimit = defsize;
393 be->be_timelimit = deftime;
394 be->be_dfltaccess = global_default_access;
396 /* assign a default depth limit for alias deref */
397 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
399 be->be_realm = global_realm != NULL
400 ? ch_strdup( global_realm ) : NULL;
403 rc = bi->bi_db_init( be );
407 fprintf( stderr, "database init failed (%s)\n", type );
421 for ( i = 0; i < nbackends; i++ ) {
422 if ( backends[i].bd_info->bi_db_close ) {
423 (*backends[i].bd_info->bi_db_close)( &backends[i] );
429 select_backend( const char * dn )
431 int i, j, len, dnlen;
433 dnlen = strlen( dn );
434 for ( i = 0; i < nbackends; i++ ) {
435 for ( j = 0; backends[i].be_nsuffix != NULL &&
436 backends[i].be_nsuffix[j] != NULL; j++ )
438 len = strlen( backends[i].be_nsuffix[j] );
444 if ( strcmp( backends[i].be_nsuffix[j],
445 dn + (dnlen - len) ) == 0 ) {
446 return( &backends[i] );
462 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
463 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
472 be_isroot( Backend *be, const char *ndn )
476 if ( ndn == NULL || *ndn == '\0' ) {
480 if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
484 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
490 be_root_dn( Backend *be )
492 if ( be->be_root_dn == NULL ) {
496 return be->be_root_dn;
500 be_isroot_pw( Backend *be, const char *ndn, struct berval *cred )
504 if ( ! be_isroot( be, ndn ) ) {
508 if( be->be_root_pw.bv_len == 0 ) {
513 ldap_pvt_thread_mutex_lock( &crypt_mutex );
516 result = lutil_passwd( &be->be_root_pw, cred, NULL );
519 ldap_pvt_thread_mutex_unlock( &crypt_mutex );
526 be_entry_release_rw( Backend *be, Entry *e, int rw )
528 if ( be->be_release ) {
529 /* free and release entry from backend */
530 return be->be_release( be, e, rw );
546 for ( i = 0; i < nbackends; i++ ) {
547 if ( backends[i].be_unbind ) {
548 (*backends[i].be_unbind)( &backends[i], conn, op );
556 backend_connection_init(
562 for ( i = 0; i < nbackends; i++ ) {
563 if ( backends[i].be_connection_init ) {
564 (*backends[i].be_connection_init)( &backends[i], conn);
572 backend_connection_destroy(
578 for ( i = 0; i < nbackends; i++ ) {
579 if ( backends[i].be_connection_destroy ) {
580 (*backends[i].be_connection_destroy)( &backends[i], conn);
588 backend_check_controls(
596 if( ctrls == NULL ) {
600 for( ; *ctrls != NULL ; ctrls++ ) {
601 if( (*ctrls)->ldctl_iscritical &&
602 !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
604 *text = "control unavailable in NamingContext";
605 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
612 int backend_check_referrals(
619 int rc = LDAP_SUCCESS;
621 if( be->be_chk_referrals ) {
624 rc = be->be_chk_referrals( be,
625 conn, op, dn, ndn, &text );
627 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
628 send_ldap_result( conn, op, rc,
629 NULL, text, NULL, NULL );
642 ObjectClass *group_oc,
643 AttributeDescription *group_at
646 if( strcmp( target->e_ndn, gr_ndn ) != 0 ) {
647 /* we won't attempt to send it to a different backend */
649 be = select_backend(gr_ndn);
652 return LDAP_NO_SUCH_OBJECT;
657 return be->be_group( be, target, gr_ndn, op_ndn,
658 group_oc, group_at );
661 return LDAP_UNWILLING_TO_PERFORM;
671 AttributeDescription *entry_at,
672 struct berval ***vals
675 if( target == NULL || strcmp( target->e_ndn, e_ndn ) != 0 ) {
676 /* we won't attempt to send it to a different backend */
678 be = select_backend(e_ndn);
681 return LDAP_NO_SUCH_OBJECT;
685 if( be->be_attribute ) {
686 return be->be_attribute( be, conn, op, target, e_ndn,
690 return LDAP_UNWILLING_TO_PERFORM;
693 Attribute *backend_operational(
699 #ifdef SLAPD_SCHEMA_DN
700 a = ch_malloc( sizeof( Attribute ) );
701 a->a_desc = ad_dup( slap_schema.si_ad_subschemaSubentry );
703 /* Should be backend specific */
704 a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
705 a->a_vals[0] = ber_bvstrdup( SLAPD_SCHEMA_DN );