3 * Copyright 1998-1999 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-ldap/external.h"
25 #include "back-ldbm/external.h"
28 #include "back-bdb2/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 static BackendInfo binfo[] = {
47 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
48 {"ldap", ldap_back_initialize},
50 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
51 {"ldbm", ldbm_back_initialize},
53 #if defined(SLAPD_BDB2) && !defined(SLAPD_BDB2_DYNAMIC)
54 {"bdb2", bdb2_back_initialize},
56 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
57 {"passwd", passwd_back_initialize},
59 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
60 {"perl", perl_back_initialize},
62 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
63 {"shell", shell_back_initialize},
65 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
66 {"tcl", tcl_back_initialize},
68 #if defined(SLAPD_NTDOMAIN) && !defined(SLAPD_NTDOMAIN_DYNAMIC)
69 {"ntdom", domain_back_initialize},
75 BackendInfo *backendInfo = NULL;
78 BackendDB *backendDB = NULL;
80 int backend_init(void)
84 if((nBackendInfo != 0) || (backendInfo != NULL)) {
85 /* already initialized */
86 Debug( LDAP_DEBUG_ANY,
87 "backend_init: already initialized.\n", 0, 0, 0 );
92 binfo[nBackendInfo].bi_type != NULL;
95 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
98 Debug( LDAP_DEBUG_ANY,
99 "backend_init: initialized for type \"%s\"\n",
100 binfo[nBackendInfo].bi_type, 0, 0 );
102 /* destroy those we've already inited */
107 if ( binfo[nBackendInfo].bi_destroy ) {
108 binfo[nBackendInfo].bi_destroy(
109 &binfo[nBackendInfo] );
116 if ( nBackendInfo > 0) {
124 Debug( LDAP_DEBUG_ANY,
125 "backend_init: failed\n",
129 #endif /* SLAPD_MODULES */
132 int backend_add(BackendInfo *aBackendInfo)
136 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
137 Debug( LDAP_DEBUG_ANY,
138 "backend_add: initialization for type \"%s\" failed\n",
139 aBackendInfo->bi_type, 0, 0 );
143 /* now add the backend type to the Backend Info List */
145 BackendInfo *newBackendInfo = 0;
147 /* if backendInfo == binfo no deallocation of old backendInfo */
148 if (backendInfo == binfo) {
149 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
150 memcpy(newBackendInfo, backendInfo, sizeof(BackendInfo) *
153 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
156 memcpy(&newBackendInfo[nBackendInfo], aBackendInfo,
157 sizeof(BackendInfo));
158 backendInfo = newBackendInfo;
165 int backend_startup(Backend *be)
170 if( ! ( nBackendDB > 0 ) ) {
172 Debug( LDAP_DEBUG_ANY,
173 "backend_startup: %d databases to startup.\n",
179 /* startup a specific backend database */
180 Debug( LDAP_DEBUG_TRACE,
181 "backend_startup: starting database\n",
184 if ( be->bd_info->bi_open ) {
185 rc = be->bd_info->bi_open( be->bd_info );
189 Debug( LDAP_DEBUG_ANY,
190 "backend_startup: bi_open failed!\n",
195 if ( be->bd_info->bi_db_open ) {
196 rc = be->bd_info->bi_db_open( be );
200 Debug( LDAP_DEBUG_ANY,
201 "backend_startup: bi_db_open failed!\n",
209 /* open each backend type */
210 for( i = 0; i < nBackendInfo; i++ ) {
211 if( backendInfo[i].bi_nDB == 0) {
212 /* no database of this type, don't open */
216 if( backendInfo[i].bi_open ) {
217 rc = backendInfo[i].bi_open(
222 Debug( LDAP_DEBUG_ANY,
223 "backend_startup: bi_open %d failed!\n",
229 /* open each backend database */
230 for( i = 0; i < nBackendDB; i++ ) {
231 /* append global access controls */
232 acl_append( &backendDB[i].be_acl, global_acl );
234 if ( backendDB[i].bd_info->bi_db_open ) {
235 rc = backendDB[i].bd_info->bi_db_open(
240 Debug( LDAP_DEBUG_ANY,
241 "backend_startup: bi_db_open %d failed!\n",
250 int backend_num( Backend *be )
254 if( be == NULL ) return -1;
256 for( i = 0; i < nBackendDB; i++ ) {
257 if( be == &backendDB[i] ) return i;
262 int backend_shutdown( Backend *be )
268 /* shutdown a specific backend database */
270 if ( be->bd_info->bi_nDB == 0 ) {
271 /* no database of this type, we never opened it */
275 if ( be->bd_info->bi_db_close ) {
276 be->bd_info->bi_db_close( be );
279 if( be->bd_info->bi_close ) {
280 be->bd_info->bi_close( be->bd_info );
286 /* close each backend database */
287 for( i = 0; i < nBackendDB; i++ ) {
288 if ( backendDB[i].bd_info->bi_db_close ) {
289 backendDB[i].bd_info->bi_db_close(
294 Debug( LDAP_DEBUG_ANY,
295 "backend_close: bi_close %s failed!\n",
296 backendDB[i].be_type, 0, 0 );
300 /* close each backend type */
301 for( i = 0; i < nBackendInfo; i++ ) {
302 if( backendInfo[i].bi_nDB == 0 ) {
303 /* no database of this type */
307 if( backendInfo[i].bi_close ) {
308 backendInfo[i].bi_close(
316 int backend_destroy(void)
320 /* destroy each backend database */
321 for( i = 0; i < nBackendDB; i++ ) {
322 if ( backendDB[i].bd_info->bi_db_destroy ) {
323 backendDB[i].bd_info->bi_db_destroy(
328 /* destroy each backend type */
329 for( i = 0; i < nBackendInfo; i++ ) {
330 if( backendInfo[i].bi_destroy ) {
331 backendInfo[i].bi_destroy(
337 if (backendInfo != binfo) {
340 #endif /* SLAPD_MODULES */
348 BackendInfo* backend_info(const char *type)
352 /* search for the backend type */
353 for( i = 0; i < nBackendInfo; i++ ) {
354 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
355 return &backendInfo[i];
369 BackendInfo *bi = backend_info(type);
373 fprintf( stderr, "Unrecognized database type (%s)\n", type );
377 backendDB = (BackendDB *) ch_realloc(
379 (nBackendDB + 1) * sizeof(Backend) );
381 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
383 be = &backends[nbackends++];
386 be->be_sizelimit = defsize;
387 be->be_timelimit = deftime;
388 be->be_dfltaccess = global_default_access;
390 /* assign a default depth limit for alias deref */
391 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
393 be->be_realm = global_realm != NULL
394 ? ch_strdup( global_realm ) : NULL;
397 rc = bi->bi_db_init( be );
401 fprintf( stderr, "database init failed (%s)\n", type );
415 for ( i = 0; i < nbackends; i++ ) {
416 if ( backends[i].bd_info->bi_db_close ) {
417 (*backends[i].bd_info->bi_db_close)( &backends[i] );
423 select_backend( const char * dn )
425 int i, j, len, dnlen;
427 dnlen = strlen( dn );
428 for ( i = 0; i < nbackends; i++ ) {
429 for ( j = 0; backends[i].be_nsuffix != NULL &&
430 backends[i].be_nsuffix[j] != NULL; j++ )
432 len = strlen( backends[i].be_nsuffix[j] );
438 if ( strcmp( backends[i].be_nsuffix[j],
439 dn + (dnlen - len) ) == 0 ) {
440 return( &backends[i] );
445 #ifdef LDAP_ALLOW_NULL_SEARCH_BASE
446 /* Add greg@greg.rim.or.jp
447 * It's quick hack for cheap client
448 * Some browser offer a NULL base at ldap_search
450 * Should only be used as a last resort. -Kdz
453 Debug( LDAP_DEBUG_TRACE,
454 "select_backend: use default backend\n", 0, 0, 0 );
455 return( &backends[0] );
457 #endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
470 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
471 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
480 be_isroot( Backend *be, const char *ndn )
484 if ( ndn == NULL || be->be_root_ndn == NULL ) {
488 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
494 be_root_dn( Backend *be )
496 if ( be->be_root_dn == NULL ) {
500 return be->be_root_dn;
504 be_isroot_pw( Backend *be, const char *ndn, struct berval *cred )
508 if ( ! be_isroot( be, ndn ) ) {
512 if( be->be_root_pw.bv_len == 0 ) {
517 ldap_pvt_thread_mutex_lock( &crypt_mutex );
520 result = lutil_passwd( &be->be_root_pw, cred, NULL );
523 ldap_pvt_thread_mutex_unlock( &crypt_mutex );
530 be_entry_release_rw( Backend *be, Entry *e, int rw )
532 if ( be->be_release ) {
533 /* free and release entry from backend */
534 return be->be_release( be, e, rw );
550 for ( i = 0; i < nbackends; i++ ) {
551 if ( backends[i].be_unbind ) {
552 (*backends[i].be_unbind)( &backends[i], conn, op );
560 backend_connection_init(
566 for ( i = 0; i < nbackends; i++ ) {
567 if ( backends[i].be_connection_init ) {
568 (*backends[i].be_connection_init)( &backends[i], conn);
576 backend_connection_destroy(
582 for ( i = 0; i < nbackends; i++ ) {
583 if ( backends[i].be_connection_destroy ) {
584 (*backends[i].be_connection_destroy)( &backends[i], conn);
592 backend_check_controls(
599 if( ctrls == NULL ) {
603 for( ; *ctrls != NULL ; ctrls++ ) {
604 if( (*ctrls)->ldctl_iscritical &&
605 !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
607 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
620 const char *objectclassValue,
621 #ifdef SLAPD_SCHEMA_NOT_COMPAT
622 AttributeDescription *group_at
629 return( be->be_group(be, target, gr_ndn, op_ndn,
630 objectclassValue, group_at) );
635 #ifdef SLAPD_SCHEMA_DN
636 Attribute *backend_operational(
640 Attribute *a = ch_malloc( sizeof( Attribute ) );
641 #ifdef SLAPD_SCHEMA_NOT_COMPAT
642 /* not yet implemented */
644 a->a_type = "subschemasubentry";
645 a->a_syntax = SYNTAX_DN | SYNTAX_CIS;
648 /* Should be backend specific */
649 a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
650 a->a_vals[0] = ch_malloc( sizeof( struct berval ) );
651 a->a_vals[0]->bv_val = ch_strdup( SLAPD_SCHEMA_DN );
652 a->a_vals[0]->bv_len = sizeof( SLAPD_SCHEMA_DN ) - 1;