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>
20 #include "ldap_defaults.h"
23 #include "back-ldap/external.h"
26 #include "back-ldbm/external.h"
29 #include "back-bdb2/external.h"
32 #include "back-passwd/external.h"
35 #include "back-perl/external.h"
38 #include "back-shell/external.h"
41 #include "back-tcl/external.h"
44 static BackendInfo binfo[] = {
45 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
46 {"ldap", ldap_back_initialize},
48 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
49 {"ldbm", ldbm_back_initialize},
51 #if defined(SLAPD_BDB2) && !defined(SLAPD_BDB2_DYNAMIC)
52 {"bdb2", bdb2_back_initialize},
54 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
55 {"passwd", passwd_back_initialize},
57 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
58 {"perl", perl_back_initialize},
60 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
61 {"shell", shell_back_initialize},
63 #if defined(SLAPD_TCL) && !defined(SLAPD_LDAP_TCL)
64 {"tcl", tcl_back_initialize},
70 BackendInfo *backendInfo = NULL;
73 BackendDB *backendDB = NULL;
75 int backend_init(void)
79 if((nBackendInfo != 0) || (backendInfo != NULL)) {
80 /* already initialized */
81 Debug( LDAP_DEBUG_ANY,
82 "backend_init: already initialized.\n", 0, 0, 0 );
87 binfo[nBackendInfo].bi_type != NULL;
90 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
93 Debug( LDAP_DEBUG_ANY,
94 "backend_init: initialized for type \"%s\"\n",
95 binfo[nBackendInfo].bi_type, 0, 0 );
97 /* destroy those we've already inited */
102 if ( binfo[nBackendInfo].bi_destroy ) {
103 binfo[nBackendInfo].bi_destroy(
104 &binfo[nBackendInfo] );
111 if ( nBackendInfo > 0) {
119 Debug( LDAP_DEBUG_ANY,
120 "backend_init: failed\n",
124 #endif /* SLAPD_MODULES */
127 int backend_add(BackendInfo *aBackendInfo)
131 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
132 Debug( LDAP_DEBUG_ANY,
133 "backend_add: initialization for type \"%s\" failed\n",
134 aBackendInfo->bi_type, 0, 0 );
138 /* now add the backend type to the Backend Info List */
140 BackendInfo *newBackendInfo = 0;
142 /* if backendInfo == binfo no deallocation of old backendInfo */
143 if (backendInfo == binfo) {
144 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
145 memcpy(newBackendInfo, backendInfo, sizeof(BackendInfo) *
148 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
151 memcpy(&newBackendInfo[nBackendInfo], aBackendInfo,
152 sizeof(BackendInfo));
153 backendInfo = newBackendInfo;
160 int backend_startup(Backend *be)
165 if( ! ( nBackendDB > 0 ) ) {
167 Debug( LDAP_DEBUG_ANY,
168 "backend_startup: %d databases to startup.\n",
174 /* startup a specific backend database */
175 Debug( LDAP_DEBUG_TRACE,
176 "backend_startup: starting database\n",
179 if ( be->bd_info->bi_open ) {
180 rc = be->bd_info->bi_open( be->bd_info );
184 Debug( LDAP_DEBUG_ANY,
185 "backend_startup: bi_open failed!\n",
190 if ( be->bd_info->bi_db_open ) {
191 rc = be->bd_info->bi_db_open( be );
195 Debug( LDAP_DEBUG_ANY,
196 "backend_startup: bi_db_open failed!\n",
204 /* open each backend type */
205 for( i = 0; i < nBackendInfo; i++ ) {
206 if( backendInfo[i].bi_nDB == 0) {
207 /* no database of this type, don't open */
211 if( backendInfo[i].bi_open ) {
212 rc = backendInfo[i].bi_open(
217 Debug( LDAP_DEBUG_ANY,
218 "backend_startup: bi_open %d failed!\n",
224 /* open each backend database */
225 for( i = 0; i < nBackendDB; i++ ) {
226 if ( backendDB[i].bd_info->bi_db_open ) {
227 rc = backendDB[i].bd_info->bi_db_open(
232 Debug( LDAP_DEBUG_ANY,
233 "backend_startup: bi_db_open %d failed!\n",
242 int backend_num( Backend *be )
246 if( be == NULL ) return -1;
248 for( i = 0; i < nBackendDB; i++ ) {
249 if( be == &backendDB[i] ) return i;
254 int backend_shutdown( Backend *be )
260 /* shutdown a specific backend database */
262 if ( be->bd_info->bi_nDB == 0 ) {
263 /* no database of this type, we never opened it */
267 if ( be->bd_info->bi_db_close ) {
268 be->bd_info->bi_db_close( be );
271 if( be->bd_info->bi_close ) {
272 be->bd_info->bi_close( be->bd_info );
278 /* close each backend database */
279 for( i = 0; i < nBackendDB; i++ ) {
282 if ( backendDB[i].bd_info->bi_db_close ) {
283 backendDB[i].bd_info->bi_db_close(
288 Debug( LDAP_DEBUG_ANY,
289 "backend_close: bi_close %s failed!\n",
294 /* close each backend type */
295 for( i = 0; i < nBackendInfo; i++ ) {
296 if( backendInfo[i].bi_nDB == 0 ) {
297 /* no database of this type */
301 if( backendInfo[i].bi_close ) {
302 backendInfo[i].bi_close(
310 int backend_destroy(void)
314 /* destroy each backend database */
315 for( i = 0; i < nBackendDB; i++ ) {
316 if ( backendDB[i].bd_info->bi_db_destroy ) {
317 backendDB[i].bd_info->bi_db_destroy(
322 /* destroy each backend type */
323 for( i = 0; i < nBackendInfo; i++ ) {
324 if( backendInfo[i].bi_destroy ) {
325 backendInfo[i].bi_destroy(
331 if (backendInfo != binfo) {
334 #endif /* SLAPD_MODULES */
342 BackendInfo* backend_info(const char *type)
346 /* search for the backend type */
347 for( i = 0; i < nBackendInfo; i++ ) {
348 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
349 return &backendInfo[i];
363 BackendInfo *bi = backend_info(type);
367 fprintf( stderr, "Unrecognized database type (%s)\n", type );
371 backendDB = (BackendDB *) ch_realloc(
373 (nBackendDB + 1) * sizeof(Backend) );
375 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
377 be = &backends[nbackends++];
380 be->be_sizelimit = defsize;
381 be->be_timelimit = deftime;
383 /* assign a default depth limit for alias deref */
384 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
386 be->be_realm = global_realm != NULL
387 ? ch_strdup( global_realm ) : NULL;
390 rc = bi->bi_db_init( be );
394 fprintf( stderr, "database init failed (%s)\n", type );
408 for ( i = 0; i < nbackends; i++ ) {
409 if ( backends[i].bd_info->bi_db_close ) {
410 (*backends[i].bd_info->bi_db_close)( &backends[i] );
416 select_backend( const char * dn )
418 int i, j, len, dnlen;
420 dnlen = strlen( dn );
421 for ( i = 0; i < nbackends; i++ ) {
422 for ( j = 0; backends[i].be_nsuffix != NULL &&
423 backends[i].be_nsuffix[j] != NULL; j++ )
425 len = strlen( backends[i].be_nsuffix[j] );
431 if ( strcmp( backends[i].be_nsuffix[j],
432 dn + (dnlen - len) ) == 0 ) {
433 return( &backends[i] );
438 #ifdef LDAP_ALLOW_NULL_SEARCH_BASE
439 /* Add greg@greg.rim.or.jp
440 * It's quick hack for cheap client
441 * Some browser offer a NULL base at ldap_search
443 * Should only be used as a last resort. -Kdz
446 Debug( LDAP_DEBUG_TRACE,
447 "select_backend: use default backend\n", 0, 0, 0 );
448 return( &backends[0] );
450 #endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
463 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
464 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
473 be_isroot( Backend *be, const char *ndn )
477 if ( ndn == NULL || be->be_root_ndn == NULL ) {
481 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
487 be_root_dn( Backend *be )
489 if ( be->be_root_dn == NULL ) {
493 return be->be_root_dn;
497 be_isroot_pw( Backend *be, const char *ndn, struct berval *cred )
501 if ( ! be_isroot( be, ndn ) ) {
506 ldap_pvt_thread_mutex_lock( &crypt_mutex );
509 result = lutil_passwd( cred->bv_val, be->be_root_pw, NULL );
512 ldap_pvt_thread_mutex_unlock( &crypt_mutex );
519 be_entry_release_rw( Backend *be, Entry *e, int rw )
521 if ( be->be_release ) {
522 /* free and release entry from backend */
523 return be->be_release( be, e, rw );
539 for ( i = 0; i < nbackends; i++ ) {
540 if ( backends[i].be_unbind ) {
541 (*backends[i].be_unbind)( &backends[i], conn, op );
549 backend_connection_init(
555 for ( i = 0; i < nbackends; i++ ) {
556 if ( backends[i].be_connection_init ) {
557 (*backends[i].be_connection_init)( &backends[i], conn);
565 backend_connection_destroy(
571 for ( i = 0; i < nbackends; i++ ) {
572 if ( backends[i].be_connection_destroy ) {
573 (*backends[i].be_connection_destroy)( &backends[i], conn);
586 const char *objectclassValue,
587 const char *groupattrName
591 return( be->be_group(be, target, gr_ndn, op_ndn,
592 objectclassValue, groupattrName) );
597 #ifdef SLAPD_SCHEMA_DN
598 Attribute *backend_subschemasubentry( Backend *be )
600 /* should be backend specific */
601 static struct berval ss_val = {
602 sizeof(SLAPD_SCHEMA_DN)-1,
604 static struct berval *ss_vals[2] = { &ss_val, NULL };
605 static Attribute ss_attr = {
608 SYNTAX_DN | SYNTAX_CIS,