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-bdb2/external.h"
34 #include "back-passwd/external.h"
37 #include "back-perl/external.h"
40 #include "back-shell/external.h"
43 #include "back-tcl/external.h"
46 #include "back-domain/external.h"
49 #include "back-sql/external.h"
52 static BackendInfo binfo[] = {
53 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
54 {"dnssrv", dnssrv_back_initialize},
56 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
57 {"ldap", ldap_back_initialize},
59 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
60 {"ldbm", ldbm_back_initialize},
62 #if defined(SLAPD_BDB2) && !defined(SLAPD_BDB2_DYNAMIC)
63 {"bdb2", bdb2_back_initialize},
65 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
66 {"passwd", passwd_back_initialize},
68 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
69 {"perl", perl_back_initialize},
71 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
72 {"shell", shell_back_initialize},
74 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
75 {"tcl", tcl_back_initialize},
77 #if defined(SLAPD_NTDOMAIN) && !defined(SLAPD_NTDOMAIN_DYNAMIC)
78 {"ntdom", domain_back_initialize},
80 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
81 {"sql", sql_back_initialize},
87 BackendInfo *backendInfo = NULL;
90 BackendDB *backendDB = NULL;
92 int backend_init(void)
96 if((nBackendInfo != 0) || (backendInfo != NULL)) {
97 /* already initialized */
98 Debug( LDAP_DEBUG_ANY,
99 "backend_init: already initialized.\n", 0, 0, 0 );
104 binfo[nBackendInfo].bi_type != NULL;
107 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
110 Debug( LDAP_DEBUG_ANY,
111 "backend_init: initialized for type \"%s\"\n",
112 binfo[nBackendInfo].bi_type, 0, 0 );
114 /* destroy those we've already inited */
119 if ( binfo[nBackendInfo].bi_destroy ) {
120 binfo[nBackendInfo].bi_destroy(
121 &binfo[nBackendInfo] );
128 if ( nBackendInfo > 0) {
136 Debug( LDAP_DEBUG_ANY,
137 "backend_init: failed\n",
141 #endif /* SLAPD_MODULES */
144 int backend_add(BackendInfo *aBackendInfo)
148 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
149 Debug( LDAP_DEBUG_ANY,
150 "backend_add: initialization for type \"%s\" failed\n",
151 aBackendInfo->bi_type, 0, 0 );
155 /* now add the backend type to the Backend Info List */
157 BackendInfo *newBackendInfo = 0;
159 /* if backendInfo == binfo no deallocation of old backendInfo */
160 if (backendInfo == binfo) {
161 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
162 memcpy(newBackendInfo, backendInfo, sizeof(BackendInfo) *
165 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
168 memcpy(&newBackendInfo[nBackendInfo], aBackendInfo,
169 sizeof(BackendInfo));
170 backendInfo = newBackendInfo;
177 int backend_startup(Backend *be)
182 if( ! ( nBackendDB > 0 ) ) {
184 Debug( LDAP_DEBUG_ANY,
185 "backend_startup: %d databases to startup.\n",
191 /* startup a specific backend database */
192 Debug( LDAP_DEBUG_TRACE,
193 "backend_startup: starting database\n",
196 if ( be->bd_info->bi_open ) {
197 rc = be->bd_info->bi_open( be->bd_info );
201 Debug( LDAP_DEBUG_ANY,
202 "backend_startup: bi_open failed!\n",
207 if ( be->bd_info->bi_db_open ) {
208 rc = be->bd_info->bi_db_open( be );
212 Debug( LDAP_DEBUG_ANY,
213 "backend_startup: bi_db_open failed!\n",
221 /* open each backend type */
222 for( i = 0; i < nBackendInfo; i++ ) {
223 if( backendInfo[i].bi_nDB == 0) {
224 /* no database of this type, don't open */
228 if( backendInfo[i].bi_open ) {
229 rc = backendInfo[i].bi_open(
234 Debug( LDAP_DEBUG_ANY,
235 "backend_startup: bi_open %d failed!\n",
241 /* open each backend database */
242 for( i = 0; i < nBackendDB; i++ ) {
243 /* append global access controls */
244 acl_append( &backendDB[i].be_acl, global_acl );
246 if ( backendDB[i].bd_info->bi_db_open ) {
247 rc = backendDB[i].bd_info->bi_db_open(
252 Debug( LDAP_DEBUG_ANY,
253 "backend_startup: bi_db_open %d failed!\n",
262 int backend_num( Backend *be )
266 if( be == NULL ) return -1;
268 for( i = 0; i < nBackendDB; i++ ) {
269 if( be == &backendDB[i] ) return i;
274 int backend_shutdown( Backend *be )
280 /* shutdown a specific backend database */
282 if ( be->bd_info->bi_nDB == 0 ) {
283 /* no database of this type, we never opened it */
287 if ( be->bd_info->bi_db_close ) {
288 be->bd_info->bi_db_close( be );
291 if( be->bd_info->bi_close ) {
292 be->bd_info->bi_close( be->bd_info );
298 /* close each backend database */
299 for( i = 0; i < nBackendDB; i++ ) {
300 if ( backendDB[i].bd_info->bi_db_close ) {
301 backendDB[i].bd_info->bi_db_close(
306 Debug( LDAP_DEBUG_ANY,
307 "backend_close: bi_close %s failed!\n",
308 backendDB[i].be_type, 0, 0 );
312 /* close each backend type */
313 for( i = 0; i < nBackendInfo; i++ ) {
314 if( backendInfo[i].bi_nDB == 0 ) {
315 /* no database of this type */
319 if( backendInfo[i].bi_close ) {
320 backendInfo[i].bi_close(
328 int backend_destroy(void)
332 /* destroy each backend database */
333 for( i = 0; i < nBackendDB; i++ ) {
334 if ( backendDB[i].bd_info->bi_db_destroy ) {
335 backendDB[i].bd_info->bi_db_destroy(
340 /* destroy each backend type */
341 for( i = 0; i < nBackendInfo; i++ ) {
342 if( backendInfo[i].bi_destroy ) {
343 backendInfo[i].bi_destroy(
349 if (backendInfo != binfo) {
352 #endif /* SLAPD_MODULES */
360 BackendInfo* backend_info(const char *type)
364 /* search for the backend type */
365 for( i = 0; i < nBackendInfo; i++ ) {
366 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
367 return &backendInfo[i];
381 BackendInfo *bi = backend_info(type);
385 fprintf( stderr, "Unrecognized database type (%s)\n", type );
389 backendDB = (BackendDB *) ch_realloc(
391 (nBackendDB + 1) * sizeof(Backend) );
393 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
395 be = &backends[nbackends++];
398 be->be_sizelimit = defsize;
399 be->be_timelimit = deftime;
400 be->be_dfltaccess = global_default_access;
402 /* assign a default depth limit for alias deref */
403 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
405 be->be_realm = global_realm != NULL
406 ? ch_strdup( global_realm ) : NULL;
409 rc = bi->bi_db_init( be );
413 fprintf( stderr, "database init failed (%s)\n", type );
427 for ( i = 0; i < nbackends; i++ ) {
428 if ( backends[i].bd_info->bi_db_close ) {
429 (*backends[i].bd_info->bi_db_close)( &backends[i] );
435 select_backend( const char * dn )
437 int i, j, len, dnlen;
439 dnlen = strlen( dn );
440 for ( i = 0; i < nbackends; i++ ) {
441 for ( j = 0; backends[i].be_nsuffix != NULL &&
442 backends[i].be_nsuffix[j] != NULL; j++ )
444 len = strlen( backends[i].be_nsuffix[j] );
450 if ( strcmp( backends[i].be_nsuffix[j],
451 dn + (dnlen - len) ) == 0 ) {
452 return( &backends[i] );
457 #ifdef LDAP_ALLOW_NULL_SEARCH_BASE
458 /* Add greg@greg.rim.or.jp
459 * It's quick hack for cheap client
460 * Some browser offer a NULL base at ldap_search
462 * Should only be used as a last resort. -Kdz
465 Debug( LDAP_DEBUG_TRACE,
466 "select_backend: use default backend\n", 0, 0, 0 );
467 return( &backends[0] );
469 #endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
482 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
483 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
492 be_isroot( Backend *be, const char *ndn )
496 if ( ndn == NULL || *ndn == '\0' ) {
500 if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
504 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
510 be_root_dn( Backend *be )
512 if ( be->be_root_dn == NULL ) {
516 return be->be_root_dn;
520 be_isroot_pw( Backend *be, const char *ndn, struct berval *cred )
524 if ( ! be_isroot( be, ndn ) ) {
528 if( be->be_root_pw.bv_len == 0 ) {
533 ldap_pvt_thread_mutex_lock( &crypt_mutex );
536 result = lutil_passwd( &be->be_root_pw, cred, NULL );
539 ldap_pvt_thread_mutex_unlock( &crypt_mutex );
546 be_entry_release_rw( Backend *be, Entry *e, int rw )
548 if ( be->be_release ) {
549 /* free and release entry from backend */
550 return be->be_release( be, e, rw );
566 for ( i = 0; i < nbackends; i++ ) {
567 if ( backends[i].be_unbind ) {
568 (*backends[i].be_unbind)( &backends[i], conn, op );
576 backend_connection_init(
582 for ( i = 0; i < nbackends; i++ ) {
583 if ( backends[i].be_connection_init ) {
584 (*backends[i].be_connection_init)( &backends[i], conn);
592 backend_connection_destroy(
598 for ( i = 0; i < nbackends; i++ ) {
599 if ( backends[i].be_connection_destroy ) {
600 (*backends[i].be_connection_destroy)( &backends[i], conn);
608 backend_check_controls(
616 if( ctrls == NULL ) {
620 for( ; *ctrls != NULL ; ctrls++ ) {
621 if( (*ctrls)->ldctl_iscritical &&
622 !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
624 *text = "control unavailable in NamingContext";
625 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
638 const char *objectclassValue,
639 #ifdef SLAPD_SCHEMA_NOT_COMPAT
640 AttributeDescription *group_at
647 return( be->be_group(be, target, gr_ndn, op_ndn,
648 objectclassValue, group_at) );
653 #ifdef SLAPD_SCHEMA_DN
654 Attribute *backend_operational(
658 Attribute *a = ch_malloc( sizeof( Attribute ) );
659 #ifdef SLAPD_SCHEMA_NOT_COMPAT
660 a->a_desc = slap_schema.si_ad_subschemaSubentry;
662 a->a_type = ch_strdup("subschemasubentry");
663 a->a_syntax = SYNTAX_DN | SYNTAX_CIS;
666 /* Should be backend specific */
667 a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
668 a->a_vals[0] = ch_malloc( sizeof( struct berval ) );
669 a->a_vals[0]->bv_val = ch_strdup( SLAPD_SCHEMA_DN );
670 a->a_vals[0]->bv_len = sizeof( SLAPD_SCHEMA_DN ) - 1;