2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 /* backend.c - routines for dealing with back-end databases */
12 #include <ac/string.h>
13 #include <ac/socket.h>
21 #include "back-ldap/external.h"
24 #include "back-ldbm/external.h"
27 #include "back-bdb2/external.h"
30 #include "back-passwd/external.h"
33 #include "back-perl/external.h"
36 #include "back-shell/external.h"
39 #include "back-tcl/external.h"
42 static BackendInfo binfo[] = {
43 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
44 {"ldap", ldap_back_initialize},
46 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
47 {"ldbm", ldbm_back_initialize},
49 #if defined(SLAPD_BDB2) && !defined(SLAPD_BDB2_DYNAMIC)
50 {"bdb2", bdb2_back_initialize},
52 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
53 {"passwd", passwd_back_initialize},
55 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
56 {"perl", perl_back_initialize},
58 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
59 {"shell", shell_back_initialize},
61 #if defined(SLAPD_TCL) && !defined(SLAPD_LDAP_TCL)
62 {"tcl", tcl_back_initialize},
68 BackendInfo *backendInfo = NULL;
71 BackendDB *backendDB = NULL;
73 int backend_init(void)
77 if((nBackendInfo != 0) || (backendInfo != NULL)) {
78 /* already initialized */
79 Debug( LDAP_DEBUG_ANY,
80 "backend_init: already initialized.\n", 0, 0, 0 );
85 binfo[nBackendInfo].bi_type != NULL;
88 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
91 Debug( LDAP_DEBUG_ANY,
92 "backend_init: initialized for type \"%s\"\n",
93 binfo[nBackendInfo].bi_type, 0, 0 );
95 /* destroy those we've already inited */
100 if ( binfo[nBackendInfo].bi_destroy ) {
101 binfo[nBackendInfo].bi_destroy(
102 &binfo[nBackendInfo] );
109 if ( nBackendInfo > 0) {
117 Debug( LDAP_DEBUG_ANY,
118 "backend_init: failed\n",
122 #endif /* SLAPD_MODULES */
125 int backend_add(BackendInfo *aBackendInfo)
129 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
130 Debug( LDAP_DEBUG_ANY,
131 "backend_add: initialization for type \"%s\" failed\n",
132 aBackendInfo->bi_type, 0, 0 );
136 /* now add the backend type to the Backend Info List */
138 BackendInfo *newBackendInfo = 0;
140 /* if backendInfo == binfo no deallocation of old backendInfo */
141 if (backendInfo == binfo) {
142 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
143 memcpy(newBackendInfo, backendInfo, sizeof(BackendInfo) *
146 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
149 memcpy(&newBackendInfo[nBackendInfo], aBackendInfo,
150 sizeof(BackendInfo));
151 backendInfo = newBackendInfo;
158 int backend_startup(Backend *be)
163 if( ! ( nBackendDB > 0 ) ) {
165 Debug( LDAP_DEBUG_ANY,
166 "backend_startup: %d databases to startup.\n",
172 /* startup a specific backend database */
173 Debug( LDAP_DEBUG_TRACE,
174 "backend_startup: starting database\n",
177 if ( be->bd_info->bi_open ) {
178 rc = be->bd_info->bi_open( be->bd_info );
182 Debug( LDAP_DEBUG_ANY,
183 "backend_startup: bi_open failed!\n",
188 if ( be->bd_info->bi_db_open ) {
189 rc = be->bd_info->bi_db_open( be );
193 Debug( LDAP_DEBUG_ANY,
194 "backend_startup: bi_db_open failed!\n",
202 /* open each backend type */
203 for( i = 0; i < nBackendInfo; i++ ) {
204 if( backendInfo[i].bi_nDB == 0) {
205 /* no database of this type, don't open */
209 if( backendInfo[i].bi_open ) {
210 rc = backendInfo[i].bi_open(
215 Debug( LDAP_DEBUG_ANY,
216 "backend_startup: bi_open %d failed!\n",
222 /* open each backend database */
223 for( i = 0; i < nBackendDB; i++ ) {
224 if ( backendDB[i].bd_info->bi_db_open ) {
225 rc = backendDB[i].bd_info->bi_db_open(
230 Debug( LDAP_DEBUG_ANY,
231 "backend_startup: bi_db_open %d failed!\n",
240 int backend_num( Backend *be )
244 if( be == NULL ) return -1;
246 for( i = 0; i < nBackendDB; i++ ) {
247 if( be == &backendDB[i] ) return i;
252 int backend_shutdown( Backend *be )
258 /* shutdown a specific backend database */
260 if ( be->bd_info->bi_nDB == 0 ) {
261 /* no database of this type, we never opened it */
265 if ( be->bd_info->bi_db_close ) {
266 be->bd_info->bi_db_close( be );
269 if( be->bd_info->bi_close ) {
270 be->bd_info->bi_close( be->bd_info );
276 /* close each backend database */
277 for( i = 0; i < nBackendDB; i++ ) {
278 if ( backendDB[i].bd_info->bi_db_close ) {
279 backendDB[i].bd_info->bi_db_close(
284 Debug( LDAP_DEBUG_ANY,
285 "backend_close: bi_close %s failed!\n",
286 backendDB[i].be_type, 0, 0 );
290 /* close each backend type */
291 for( i = 0; i < nBackendInfo; i++ ) {
292 if( backendInfo[i].bi_nDB == 0 ) {
293 /* no database of this type */
297 if( backendInfo[i].bi_close ) {
298 backendInfo[i].bi_close(
306 int backend_destroy(void)
310 /* destroy each backend database */
311 for( i = 0; i < nBackendDB; i++ ) {
312 if ( backendDB[i].bd_info->bi_db_destroy ) {
313 backendDB[i].bd_info->bi_db_destroy(
318 /* destroy each backend type */
319 for( i = 0; i < nBackendInfo; i++ ) {
320 if( backendInfo[i].bi_destroy ) {
321 backendInfo[i].bi_destroy(
327 if (backendInfo != binfo) {
330 #endif /* SLAPD_MODULES */
338 BackendInfo* backend_info(const char *type)
342 /* search for the backend type */
343 for( i = 0; i < nBackendInfo; i++ ) {
344 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
345 return &backendInfo[i];
359 BackendInfo *bi = backend_info(type);
363 fprintf( stderr, "Unrecognized database type (%s)\n", type );
367 backendDB = (BackendDB *) ch_realloc(
369 (nBackendDB + 1) * sizeof(Backend) );
371 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
373 be = &backends[nbackends++];
376 be->be_sizelimit = defsize;
377 be->be_timelimit = deftime;
379 /* assign a default depth limit for alias deref */
380 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
382 be->be_realm = global_realm != NULL
383 ? ch_strdup( global_realm ) : NULL;
386 rc = bi->bi_db_init( be );
390 fprintf( stderr, "database init failed (%s)\n", type );
404 for ( i = 0; i < nbackends; i++ ) {
405 if ( backends[i].bd_info->bi_db_close ) {
406 (*backends[i].bd_info->bi_db_close)( &backends[i] );
412 select_backend( const char * dn )
414 int i, j, len, dnlen;
416 dnlen = strlen( dn );
417 for ( i = 0; i < nbackends; i++ ) {
418 for ( j = 0; backends[i].be_nsuffix != NULL &&
419 backends[i].be_nsuffix[j] != NULL; j++ )
421 len = strlen( backends[i].be_nsuffix[j] );
427 if ( strcmp( backends[i].be_nsuffix[j],
428 dn + (dnlen - len) ) == 0 ) {
429 return( &backends[i] );
434 #ifdef LDAP_ALLOW_NULL_SEARCH_BASE
435 /* Add greg@greg.rim.or.jp
436 * It's quick hack for cheap client
437 * Some browser offer a NULL base at ldap_search
439 * Should only be used as a last resort. -Kdz
442 Debug( LDAP_DEBUG_TRACE,
443 "select_backend: use default backend\n", 0, 0, 0 );
444 return( &backends[0] );
446 #endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
459 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
460 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
469 be_isroot( Backend *be, const char *ndn )
473 if ( ndn == NULL || be->be_root_ndn == NULL ) {
477 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
483 be_root_dn( Backend *be )
485 if ( be->be_root_dn == NULL ) {
489 return be->be_root_dn;
493 be_isroot_pw( Backend *be, const char *ndn, struct berval *cred )
497 if ( ! be_isroot( be, ndn ) ) {
502 ldap_pvt_thread_mutex_lock( &crypt_mutex );
505 result = lutil_passwd( cred->bv_val, be->be_root_pw, NULL );
508 ldap_pvt_thread_mutex_unlock( &crypt_mutex );
515 be_entry_release_rw( Backend *be, Entry *e, int rw )
517 if ( be->be_release ) {
518 /* free and release entry from backend */
519 return be->be_release( be, e, rw );
535 for ( i = 0; i < nbackends; i++ ) {
536 if ( backends[i].be_unbind ) {
537 (*backends[i].be_unbind)( &backends[i], conn, op );
545 backend_connection_init(
551 for ( i = 0; i < nbackends; i++ ) {
552 if ( backends[i].be_connection_init ) {
553 (*backends[i].be_connection_init)( &backends[i], conn);
561 backend_connection_destroy(
567 for ( i = 0; i < nbackends; i++ ) {
568 if ( backends[i].be_connection_destroy ) {
569 (*backends[i].be_connection_destroy)( &backends[i], conn);
582 const char *objectclassValue,
583 const char *groupattrName
587 return( be->be_group(be, target, gr_ndn, op_ndn,
588 objectclassValue, groupattrName) );
593 #ifdef SLAPD_SCHEMA_DN
594 Attribute *backend_subschemasubentry( Backend *be )
596 /* should be backend specific */
597 static struct berval ss_val = {
598 sizeof(SLAPD_SCHEMA_DN)-1,
600 static struct berval *ss_vals[2] = { &ss_val, NULL };
601 static Attribute ss_attr = {
604 SYNTAX_DN | SYNTAX_CIS,