1 /* backend.c - routines for dealing with back-end databases */
16 #include "back-ldbm/external.h"
19 #include "back-bdb2/external.h"
22 #include "back-passwd/external.h"
25 #include "back-perl/external.h"
28 #include "back-shell/external.h"
31 static BackendInfo binfo[] = {
33 {"ldbm", ldbm_back_initialize},
36 {"bdb2", bdb2_back_initialize},
39 {"passwd", passwd_back_initialize},
42 {"perl", perl_back_initialize},
45 {"shell", shell_back_initialize},
51 BackendInfo *backendInfo = NULL;
54 BackendDB *backendDB = NULL;
56 int backend_init(void)
60 if((nBackendInfo != 0) || (backendInfo != NULL)) {
61 /* already initialized */
62 Debug( LDAP_DEBUG_ANY,
63 "backend_init: already initialized.\n", 0, 0, 0 );
68 binfo[nBackendInfo].bi_type != NULL;
71 rc = binfo[nBackendInfo].bi_init(
72 &binfo[nBackendInfo] );
75 Debug( LDAP_DEBUG_ANY,
76 "backend_init: initialized for type \"%s\"\n",
77 binfo[nBackendInfo].bi_type, 0, 0 );
79 /* destroy those we've already inited */
84 if ( binfo[nBackendInfo].bi_destroy ) {
85 binfo[nBackendInfo].bi_destroy(
86 &binfo[nBackendInfo] );
93 if ( nBackendInfo > 0) {
98 Debug( LDAP_DEBUG_ANY,
99 "backend_init: failed\n",
105 int backend_startup(int n)
110 if( ! ( nBackendDB > 0 ) ) {
112 Debug( LDAP_DEBUG_ANY,
113 "backend_startup: %d databases to startup.\n",
119 /* startup a specific backend database */
120 Debug( LDAP_DEBUG_TRACE,
121 "backend_startup: starting database %d\n",
124 /* make sure, n does not exceed the number of backend databases */
125 if ( n >= nbackends ) {
127 Debug( LDAP_DEBUG_ANY,
128 "backend_startup: database number %d exceeding maximum (%d)\n",
133 if ( backendDB[n].bd_info->bi_open ) {
134 rc = backendDB[n].bd_info->bi_open(
135 backendDB[n].bd_info );
139 Debug( LDAP_DEBUG_ANY,
140 "backend_startup: bi_open failed!\n",
145 if ( backendDB[n].bd_info->bi_db_open ) {
146 rc = backendDB[n].bd_info->bi_db_open(
151 Debug( LDAP_DEBUG_ANY,
152 "backend_startup: bi_db_open failed!\n",
160 /* open each backend type */
161 for( i = 0; i < nBackendInfo; i++ ) {
162 if( backendInfo[i].bi_nDB == 0) {
163 /* no database of this type, don't open */
167 if( backendInfo[i].bi_open ) {
168 rc = backendInfo[i].bi_open(
173 Debug( LDAP_DEBUG_ANY,
174 "backend_startup: bi_open %d failed!\n",
180 /* open each backend database */
181 for( i = 0; i < nBackendDB; i++ ) {
182 if ( backendDB[i].bd_info->bi_db_open ) {
183 rc = backendDB[i].bd_info->bi_db_open(
188 Debug( LDAP_DEBUG_ANY,
189 "backend_startup: bi_db_open %d failed!\n",
198 int backend_shutdown(int n)
204 /* shutdown a specific backend database */
206 /* make sure, n does not exceed the number of backend databases */
207 if ( n >= nbackends ) {
209 Debug( LDAP_DEBUG_ANY,
210 "backend_startup: database number %d exceeding maximum (%d)\n",
215 if ( backendDB[n].bd_info->bi_nDB == 0 ) {
216 /* no database of this type, we never opened it */
220 if ( backendDB[n].bd_info->bi_db_close ) {
221 backendDB[n].bd_info->bi_db_close(
225 if( backendDB[n].bd_info->bi_close ) {
226 backendDB[n].bd_info->bi_close(
227 backendDB[n].bd_info );
233 /* close each backend database */
234 for( i = 0; i < nBackendDB; i++ ) {
237 if ( backendDB[i].bd_info->bi_db_close ) {
238 backendDB[i].bd_info->bi_db_close(
243 Debug( LDAP_DEBUG_ANY,
244 "backend_close: bi_close %s failed!\n",
249 /* close each backend type */
250 for( i = 0; i < nBackendInfo; i++ ) {
251 if( backendInfo[i].bi_nDB == 0 ) {
252 /* no database of this type */
256 if( backendInfo[i].bi_close ) {
257 backendInfo[i].bi_close(
265 int backend_destroy(void)
269 /* destroy each backend database */
270 for( i = 0; i < nBackendDB; i++ ) {
271 if ( backendDB[i].bd_info->bi_db_destroy ) {
272 backendDB[i].bd_info->bi_db_destroy(
277 /* destroy each backend type */
278 for( i = 0; i < nBackendInfo; i++ ) {
279 if( backendInfo[i].bi_destroy ) {
280 backendInfo[i].bi_destroy(
288 BackendInfo* backend_info(char *type)
292 /* search for the backend type */
293 for( i = 0; i < nBackendInfo; i++ ) {
294 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
295 return &backendInfo[i];
309 BackendInfo *bi = backend_info(type);
313 fprintf( stderr, "Unrecognized database type (%s)\n", type );
317 backendDB = (BackendDB *) ch_realloc(
319 (nBackendDB + 1) * sizeof(Backend) );
321 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
323 be = &backends[nbackends++];
326 be->be_sizelimit = defsize;
327 be->be_timelimit = deftime;
330 rc = bi->bi_db_init( be );
334 fprintf( stderr, "database init failed (%s)\n", type );
348 for ( i = 0; i < nbackends; i++ ) {
349 if ( backends[i].bd_info->bi_db_close != NULL ) {
350 (*backends[i].bd_info->bi_db_close)( &backends[i] );
356 select_backend( char * dn )
358 int i, j, len, dnlen;
360 dnlen = strlen( dn );
361 for ( i = 0; i < nbackends; i++ ) {
362 for ( j = 0; backends[i].be_suffix != NULL &&
363 backends[i].be_suffix[j] != NULL; j++ )
365 len = strlen( backends[i].be_suffix[j] );
371 if ( strcmp( backends[i].be_suffix[j],
372 dn + (dnlen - len) ) == 0 ) {
373 return( &backends[i] );
378 /* if no proper suffix could be found then check for aliases */
379 for ( i = 0; i < nbackends; i++ ) {
381 backends[i].be_suffixAlias != NULL &&
382 backends[i].be_suffixAlias[j] != NULL;
385 len = strlen( backends[i].be_suffixAlias[j] );
391 if ( strcmp( backends[i].be_suffixAlias[j],
392 dn + (dnlen - len) ) == 0 ) {
393 return( &backends[i] );
398 #ifdef LDAP_ALLOW_NULL_SEARCH_BASE
399 /* Add greg@greg.rim.or.jp
400 * It's quick hack for cheap client
401 * Some browser offer a NULL base at ldap_search
403 * Should only be used as a last resort. -Kdz
406 Debug( LDAP_DEBUG_TRACE,
407 "select_backend: use default backend\n", 0, 0, 0 );
408 return( &backends[0] );
410 #endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
423 for ( i = 0; be->be_suffix != NULL && be->be_suffix[i] != NULL; i++ ) {
424 if ( strcmp( be->be_suffix[i], suffix ) == 0 ) {
433 be_isroot( Backend *be, char *ndn )
437 if ( ndn == NULL || be->be_root_ndn == NULL ) {
441 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
447 be_root_dn( Backend *be )
451 if ( be->be_root_dn == NULL ) {
455 return be->be_root_dn;
459 be_isroot_pw( Backend *be, char *ndn, struct berval *cred )
463 if ( ! be_isroot( be, ndn ) ) {
468 ldap_pvt_thread_mutex_lock( &crypt_mutex );
471 result = lutil_passwd( cred->bv_val, be->be_root_pw );
474 ldap_pvt_thread_mutex_unlock( &crypt_mutex );
488 for ( i = 0; i < nbackends; i++ ) {
489 if ( backends[i].be_unbind != NULL ) {
490 (*backends[i].be_unbind)( &backends[i], conn, op );
497 #ifdef SLAPD_ACLGROUPS
504 char *objectclassValue,
509 return( be->be_group(be, target, gr_ndn, op_ndn,
510 objectclassValue, groupattrName) );