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(
91 &binfo[nBackendInfo] );
94 Debug( LDAP_DEBUG_ANY,
95 "backend_init: initialized for type \"%s\"\n",
96 binfo[nBackendInfo].bi_type, 0, 0 );
98 /* destroy those we've already inited */
103 if ( binfo[nBackendInfo].bi_destroy ) {
104 binfo[nBackendInfo].bi_destroy(
105 &binfo[nBackendInfo] );
112 if ( nBackendInfo > 0) {
120 Debug( LDAP_DEBUG_ANY,
121 "backend_init: failed\n",
125 #endif /* SLAPD_MODULES */
128 int backend_add(BackendInfo *aBackendInfo)
132 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
133 Debug( LDAP_DEBUG_ANY,
134 "backend_add: initialization for type \"%s\" failed\n",
135 aBackendInfo->bi_type, 0, 0 );
139 /* now add the backend type to the Backend Info List */
141 BackendInfo *newBackendInfo = 0;
143 /* if backendInfo == binfo no deallocation of old backendInfo */
144 if (backendInfo == binfo) {
145 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
146 memcpy(newBackendInfo, backendInfo, sizeof(BackendInfo) *
149 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
152 memcpy(&newBackendInfo[nBackendInfo], aBackendInfo,
153 sizeof(BackendInfo));
154 backendInfo = newBackendInfo;
161 int backend_startup(int n)
166 if( ! ( nBackendDB > 0 ) ) {
168 Debug( LDAP_DEBUG_ANY,
169 "backend_startup: %d databases to startup.\n",
175 /* startup a specific backend database */
176 Debug( LDAP_DEBUG_TRACE,
177 "backend_startup: starting database %d\n",
180 /* make sure, n does not exceed the number of backend databases */
181 if ( n >= nbackends ) {
183 Debug( LDAP_DEBUG_ANY,
184 "backend_startup: database number %d exceeding maximum (%d)\n",
189 if ( backendDB[n].bd_info->bi_open ) {
190 rc = backendDB[n].bd_info->bi_open(
191 backendDB[n].bd_info );
195 Debug( LDAP_DEBUG_ANY,
196 "backend_startup: bi_open failed!\n",
201 if ( backendDB[n].bd_info->bi_db_open ) {
202 rc = backendDB[n].bd_info->bi_db_open(
207 Debug( LDAP_DEBUG_ANY,
208 "backend_startup: bi_db_open failed!\n",
216 /* open each backend type */
217 for( i = 0; i < nBackendInfo; i++ ) {
218 if( backendInfo[i].bi_nDB == 0) {
219 /* no database of this type, don't open */
223 if( backendInfo[i].bi_open ) {
224 rc = backendInfo[i].bi_open(
229 Debug( LDAP_DEBUG_ANY,
230 "backend_startup: bi_open %d failed!\n",
236 /* open each backend database */
237 for( i = 0; i < nBackendDB; i++ ) {
238 if ( backendDB[i].bd_info->bi_db_open ) {
239 rc = backendDB[i].bd_info->bi_db_open(
244 Debug( LDAP_DEBUG_ANY,
245 "backend_startup: bi_db_open %d failed!\n",
254 int backend_shutdown(int n)
260 /* shutdown a specific backend database */
262 /* make sure, n does not exceed the number of backend databases */
263 if ( n >= nbackends ) {
265 Debug( LDAP_DEBUG_ANY,
266 "backend_startup: database number %d exceeding maximum (%d)\n",
271 if ( backendDB[n].bd_info->bi_nDB == 0 ) {
272 /* no database of this type, we never opened it */
276 if ( backendDB[n].bd_info->bi_db_close ) {
277 backendDB[n].bd_info->bi_db_close(
281 if( backendDB[n].bd_info->bi_close ) {
282 backendDB[n].bd_info->bi_close(
283 backendDB[n].bd_info );
289 /* close each backend database */
290 for( i = 0; i < nBackendDB; i++ ) {
293 if ( backendDB[i].bd_info->bi_db_close ) {
294 backendDB[i].bd_info->bi_db_close(
299 Debug( LDAP_DEBUG_ANY,
300 "backend_close: bi_close %s failed!\n",
305 /* close each backend type */
306 for( i = 0; i < nBackendInfo; i++ ) {
307 if( backendInfo[i].bi_nDB == 0 ) {
308 /* no database of this type */
312 if( backendInfo[i].bi_close ) {
313 backendInfo[i].bi_close(
321 int backend_destroy(void)
325 /* destroy each backend database */
326 for( i = 0; i < nBackendDB; i++ ) {
327 if ( backendDB[i].bd_info->bi_db_destroy ) {
328 backendDB[i].bd_info->bi_db_destroy(
333 /* destroy each backend type */
334 for( i = 0; i < nBackendInfo; i++ ) {
335 if( backendInfo[i].bi_destroy ) {
336 backendInfo[i].bi_destroy(
342 if (backendInfo != binfo) {
345 #endif /* SLAPD_MODULES */
353 BackendInfo* backend_info(char *type)
357 /* search for the backend type */
358 for( i = 0; i < nBackendInfo; i++ ) {
359 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
360 return &backendInfo[i];
374 BackendInfo *bi = backend_info(type);
378 fprintf( stderr, "Unrecognized database type (%s)\n", type );
382 backendDB = (BackendDB *) ch_realloc(
384 (nBackendDB + 1) * sizeof(Backend) );
386 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
388 be = &backends[nbackends++];
391 be->be_sizelimit = defsize;
392 be->be_timelimit = deftime;
394 be->be_realm = global_realm != NULL
395 ? ch_strdup( global_realm ) : NULL;
398 rc = bi->bi_db_init( be );
402 fprintf( stderr, "database init failed (%s)\n", type );
416 for ( i = 0; i < nbackends; i++ ) {
417 if ( backends[i].bd_info->bi_db_close ) {
418 (*backends[i].bd_info->bi_db_close)( &backends[i] );
424 select_backend( char * dn )
426 int i, j, len, dnlen;
428 dnlen = strlen( dn );
429 for ( i = 0; i < nbackends; i++ ) {
430 for ( j = 0; backends[i].be_nsuffix != NULL &&
431 backends[i].be_nsuffix[j] != NULL; j++ )
433 len = strlen( backends[i].be_nsuffix[j] );
439 if ( strcmp( backends[i].be_nsuffix[j],
440 dn + (dnlen - len) ) == 0 ) {
441 return( &backends[i] );
446 #ifdef LDAP_ALLOW_NULL_SEARCH_BASE
447 /* Add greg@greg.rim.or.jp
448 * It's quick hack for cheap client
449 * Some browser offer a NULL base at ldap_search
451 * Should only be used as a last resort. -Kdz
454 Debug( LDAP_DEBUG_TRACE,
455 "select_backend: use default backend\n", 0, 0, 0 );
456 return( &backends[0] );
458 #endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
471 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
472 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
481 be_isroot( Backend *be, char *ndn )
485 if ( ndn == NULL || be->be_root_ndn == NULL ) {
489 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
495 be_root_dn( Backend *be )
497 if ( be->be_root_dn == NULL ) {
501 return be->be_root_dn;
505 be_isroot_pw( Backend *be, char *ndn, struct berval *cred )
509 if ( ! be_isroot( be, ndn ) ) {
514 ldap_pvt_thread_mutex_lock( &crypt_mutex );
517 result = lutil_passwd( cred->bv_val, be->be_root_pw, NULL );
520 ldap_pvt_thread_mutex_unlock( &crypt_mutex );
527 be_entry_release_rw( Backend *be, Entry *e, int rw )
529 if ( be->be_release ) {
530 /* free and release entry from backend */
531 return be->be_release( be, e, rw );
547 for ( i = 0; i < nbackends; i++ ) {
548 if ( backends[i].be_unbind ) {
549 (*backends[i].be_unbind)( &backends[i], conn, op );
557 backend_connection_init(
563 for ( i = 0; i < nbackends; i++ ) {
564 if ( backends[i].be_connection_init ) {
565 (*backends[i].be_connection_init)( &backends[i], conn);
573 backend_connection_destroy(
579 for ( i = 0; i < nbackends; i++ ) {
580 if ( backends[i].be_connection_destroy ) {
581 (*backends[i].be_connection_destroy)( &backends[i], conn);
594 char *objectclassValue,
599 return( be->be_group(be, target, gr_ndn, op_ndn,
600 objectclassValue, groupattrName) );
605 #ifdef SLAPD_SCHEMA_DN
606 Attribute *backend_subschemasubentry( Backend *be )
608 /* should be backend specific */
609 static struct berval ss_val = {
610 sizeof(SLAPD_SCHEMA_DN)-1,
612 static struct berval *ss_vals[2] = { &ss_val, NULL };
613 static Attribute ss_attr = {
616 SYNTAX_DN | SYNTAX_CIS,