]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
ITS#3470 don't propagate error if hasSubordinates fails, it's not that
[openldap] / servers / slapd / back-bdb / init.c
1 /* init.c - initialize bdb backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
23
24 #include "back-bdb.h"
25 #include <lutil.h>
26 #include <ldap_rq.h>
27
28 static const struct bdbi_database {
29         char *file;
30         char *name;
31         int type;
32         int flags;
33 } bdbi_databases[] = {
34         { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
35         { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
36         { NULL, NULL, 0, 0 }
37 };
38
39 struct berval bdb_uuid = BER_BVNULL;
40
41 typedef void * db_malloc(size_t);
42 typedef void * db_realloc(void *, size_t);
43
44 #if 0
45 static int
46 bdb_open( BackendInfo *bi )
47 {
48         return 0;
49 }
50
51 static int
52 bdb_destroy( BackendInfo *bi )
53 {
54         return 0;
55 }
56
57 static int
58 bdb_close( BackendInfo *bi )
59 {
60         /* terminate the underlying database system */
61         return 0;
62 }
63 #endif
64
65 static int
66 bdb_db_init( BackendDB *be )
67 {
68         struct bdb_info *bdb;
69
70         Debug( LDAP_DEBUG_ANY,
71                 LDAP_XSTRING(bdb_db_init) ": Initializing "
72                 BDB_UCTYPE " database\n", 0, 0, 0 );
73
74         /* allocate backend-database-specific stuff */
75         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
76
77         /* DBEnv parameters */
78         bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
79         bdb->bi_dbenv_xflags = 0;
80         bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
81
82         bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
83
84         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
85         bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
86         bdb->bi_search_stack = NULL;
87
88 #ifdef BDB_PSEARCH
89         LDAP_LIST_INIT (&bdb->bi_psearch_list);
90         ldap_pvt_thread_rdwr_init ( &bdb->bi_pslist_rwlock );
91 #endif
92
93         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
94         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
95         ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
96         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
97         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
98
99         be->be_private = bdb;
100
101         return 0;
102 }
103
104 int
105 bdb_bt_compare(
106         DB *db, 
107         const DBT *usrkey,
108         const DBT *curkey )
109 {
110         unsigned char *u, *c;
111         int i, x;
112
113         u = usrkey->data;
114         c = curkey->data;
115
116 #ifdef WORDS_BIGENDIAN
117         for( i = 0; i < (int)sizeof(ID); i++)
118 #else
119         for( i = sizeof(ID)-1; i >= 0; i--)
120 #endif
121         {
122                 x = u[i] - c[i];
123                 if( x ) return x;
124         }
125
126         return 0;
127 }
128
129 static void *
130 bdb_checkpoint( void *ctx, void *arg )
131 {
132         struct re_s *rtask = arg;
133         struct bdb_info *bdb = rtask->arg;
134         
135         TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
136                 bdb->bi_txn_cp_min, 0 );
137         return NULL;
138 }
139
140 static int
141 bdb_db_open( BackendDB *be )
142 {
143         int rc, i;
144         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
145         u_int32_t flags;
146 #ifdef HAVE_EBCDIC
147         char path[MAXPATHLEN];
148 #endif
149
150         Debug( LDAP_DEBUG_ARGS,
151                 "bdb_db_open: %s\n",
152                 be->be_suffix[0].bv_val, 0, 0 );
153
154 #ifndef BDB_MULTIPLE_SUFFIXES
155         if ( be->be_suffix[1].bv_val ) {
156         Debug( LDAP_DEBUG_ANY,
157                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
158                 return -1;
159         }
160 #endif
161         /* we should check existance of dbenv_home and db_directory */
162
163         rc = db_env_create( &bdb->bi_dbenv, 0 );
164         if( rc != 0 ) {
165                 Debug( LDAP_DEBUG_ANY,
166                         "bdb_db_open: db_env_create failed: %s (%d)\n",
167                         db_strerror(rc), rc, 0 );
168                 return rc;
169         }
170
171         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
172                 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
173         
174 #if 0
175         /* Never do automatic recovery, must perform it manually.
176          * Otherwise restarting with gentlehup will corrupt the
177          * database.
178          */
179         if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
180 #endif
181
182         /* If a key was set, use shared memory for the BDB environment */
183         if ( bdb->bi_shm_key ) {
184                 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
185                 flags |= DB_SYSTEM_MEM;
186         }
187
188         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
189         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
190         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
191
192         /* One long-lived TXN per thread, two TXNs per write op */
193         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
194
195 #ifdef SLAP_ZONE_ALLOC
196         if ( bdb->bi_cache.c_maxsize ) {
197                 bdb->bi_cache.c_zctx = slap_zn_mem_create(
198                                                                 SLAP_ZONE_INITSIZE,
199                                                                 SLAP_ZONE_MAXSIZE,
200                                                                 SLAP_ZONE_DELTA,
201                                                                 SLAP_ZONE_SIZE);
202         }
203 #endif
204
205         if ( bdb->bi_idl_cache_max_size ) {
206                 bdb->bi_idl_tree = NULL;
207                 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
208                 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
209                 bdb->bi_idl_cache_size = 0;
210         }
211
212 #ifdef BDB_SUBDIRS
213         {
214                 char dir[MAXPATHLEN], *ptr;
215                 
216                 if (bdb->bi_dbenv_home[0] == '.') {
217                         /* If home is a relative path, relative subdirs
218                          * are just concat'd by BDB. We don't want the
219                          * path to be concat'd twice, e.g.
220                          * ./test-db/./test-db/tmp
221                          */
222                         ptr = dir;
223                 } else {
224                         ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
225                         *ptr++ = LDAP_DIRSEP[0];
226 #ifdef HAVE_EBCDIC
227                         __atoe( dir );
228 #endif
229                 }
230
231                 strcpy( ptr, BDB_TMP_SUBDIR );
232 #ifdef HAVE_EBCDIC
233                 __atoe( ptr );
234 #endif
235                 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
236                 if( rc != 0 ) {
237                         Debug( LDAP_DEBUG_ANY,
238                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
239                                 dir, db_strerror(rc), rc );
240                         return rc;
241                 }
242
243                 strcpy( ptr, BDB_LG_SUBDIR );
244 #ifdef HAVE_EBCDIC
245                 __atoe( ptr );
246 #endif
247                 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
248                 if( rc != 0 ) {
249                         Debug( LDAP_DEBUG_ANY,
250                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
251                                 dir, db_strerror(rc), rc );
252                         return rc;
253                 }
254
255                 strcpy( ptr, BDB_DATA_SUBDIR );
256 #ifdef HAVE_EBCDIC
257                 __atoe( ptr );
258 #endif
259                 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
260                 if( rc != 0 ) {
261                         Debug( LDAP_DEBUG_ANY,
262                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
263                                 dir, db_strerror(rc), rc );
264                         return rc;
265                 }
266         }
267 #endif
268
269         if( bdb->bi_dbenv_xflags != 0 ) {
270                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
271                         bdb->bi_dbenv_xflags, 1);
272                 if( rc != 0 ) {
273                         Debug( LDAP_DEBUG_ANY,
274                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
275                                 db_strerror(rc), rc, 0 );
276                         return rc;
277                 }
278         }
279
280         Debug( LDAP_DEBUG_TRACE,
281                 "bdb_db_open: dbenv_open(%s)\n",
282                 bdb->bi_dbenv_home, 0, 0);
283
284 #ifdef HAVE_EBCDIC
285         strcpy( path, bdb->bi_dbenv_home );
286         __atoe( path );
287         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
288                 path,
289                 flags,
290                 bdb->bi_dbenv_mode );
291 #else
292         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
293                 bdb->bi_dbenv_home,
294                 flags,
295                 bdb->bi_dbenv_mode );
296 #endif
297         if( rc != 0 ) {
298                 Debug( LDAP_DEBUG_ANY,
299                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
300                         db_strerror(rc), rc, 0 );
301                 return rc;
302         }
303
304         flags = DB_THREAD | bdb->bi_db_opflags;
305
306         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
307                 BDB_INDICES * sizeof(struct bdb_db_info *) );
308
309         /* open (and create) main database */
310         for( i = 0; bdbi_databases[i].name; i++ ) {
311                 struct bdb_db_info *db;
312
313                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
314
315                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
316                 if( rc != 0 ) {
317                         Debug( LDAP_DEBUG_ANY,
318                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
319                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
320                         return rc;
321                 }
322
323                 if( i == BDB_ID2ENTRY ) {
324 #if 0
325                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
326                                 bdb_bt_compare );
327 #endif
328                         rc = db->bdi_db->set_pagesize( db->bdi_db,
329                                 BDB_ID2ENTRY_PAGESIZE );
330                         if ( slapMode & SLAP_TOOL_READMAIN ) {
331                                 flags |= DB_RDONLY;
332                         } else {
333                                 flags |= DB_CREATE;
334                         }
335                 } else {
336                         rc = db->bdi_db->set_flags( db->bdi_db, 
337                                 DB_DUP | DB_DUPSORT );
338 #ifndef BDB_HIER
339 #if 0
340                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
341                                 bdb_bt_compare );
342 #endif
343                         if ( slapMode & SLAP_TOOL_READONLY ) {
344                                 flags |= DB_RDONLY;
345                         } else {
346                                 flags |= DB_CREATE;
347                         }
348 #else
349                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
350                                 bdb_dup_compare );
351                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
352                                 bdb_bt_compare );
353                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
354                                 flags |= DB_RDONLY;
355                         } else {
356                                 flags |= DB_CREATE;
357                         }
358 #endif
359                         rc = db->bdi_db->set_pagesize( db->bdi_db,
360                                 BDB_PAGESIZE );
361                 }
362
363 #ifdef HAVE_EBCDIC
364                 strcpy( path, bdbi_databases[i].file );
365                 __atoe( path );
366                 rc = DB_OPEN( db->bdi_db,
367                         path,
368                 /*      bdbi_databases[i].name, */ NULL,
369                         bdbi_databases[i].type,
370                         bdbi_databases[i].flags | flags,
371                         bdb->bi_dbenv_mode );
372 #else
373                 rc = DB_OPEN( db->bdi_db,
374                         bdbi_databases[i].file,
375                 /*      bdbi_databases[i].name, */ NULL,
376                         bdbi_databases[i].type,
377                         bdbi_databases[i].flags | flags,
378                         bdb->bi_dbenv_mode );
379 #endif
380
381                 if ( rc != 0 ) {
382                         char    buf[SLAP_TEXT_BUFLEN];
383
384                         snprintf( buf, sizeof(buf), "%s/%s", 
385                                 bdb->bi_dbenv_home, bdbi_databases[i].file );
386                         Debug( LDAP_DEBUG_ANY,
387                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
388                                 buf, db_strerror(rc), rc );
389                         return rc;
390                 }
391
392                 flags &= ~(DB_CREATE | DB_RDONLY);
393                 db->bdi_name = bdbi_databases[i].name;
394                 bdb->bi_databases[i] = db;
395         }
396
397         bdb->bi_databases[i] = NULL;
398         bdb->bi_ndatabases = i;
399
400         /* get nextid */
401         rc = bdb_last_id( be, NULL );
402         if( rc != 0 ) {
403                 Debug( LDAP_DEBUG_ANY,
404                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
405                         bdb->bi_dbenv_home, db_strerror(rc), rc );
406                 return rc;
407         }
408
409         XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
410
411         /* If we're in server mode and time-based checkpointing is enabled,
412          * submit a task to perform periodic checkpoints.
413          */
414         if ( slapMode & SLAP_SERVER_MODE && bdb->bi_txn_cp &&
415                 bdb->bi_txn_cp_min )  {
416                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
417                 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
418                         bdb_checkpoint, bdb );
419                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
420         }
421
422         /* <insert> open (and create) index databases */
423         return 0;
424 }
425
426 static int
427 bdb_db_close( BackendDB *be )
428 {
429         int rc;
430         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
431         struct bdb_db_info *db;
432         bdb_idl_cache_entry_t *entry, *next_entry;
433
434         while( bdb->bi_ndatabases-- ) {
435                 db = bdb->bi_databases[bdb->bi_ndatabases];
436                 rc = db->bdi_db->close( db->bdi_db, 0 );
437                 /* Lower numbered names are not strdup'd */
438                 if( bdb->bi_ndatabases >= BDB_NDB )
439                         free( db->bdi_name );
440                 free( db );
441         }
442         free( bdb->bi_databases );
443         bdb_attr_index_destroy( bdb->bi_attrs );
444
445         bdb_cache_release_all (&bdb->bi_cache);
446
447         if ( bdb->bi_idl_cache_max_size ) {
448                 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
449                 avl_free( bdb->bi_idl_tree, NULL );
450                 entry = bdb->bi_idl_lru_head;
451                 while ( entry != NULL ) {
452                         next_entry = entry->idl_lru_next;
453                         if ( entry->idl )
454                                 free( entry->idl );
455                         free( entry->kstr.bv_val );
456                         free( entry );
457                         entry = next_entry;
458                 }
459                 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
460         }
461
462         XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
463
464         return 0;
465 }
466
467 static int
468 bdb_db_destroy( BackendDB *be )
469 {
470         int rc;
471         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
472         Operation *ps = NULL;
473         Operation *psn = NULL;
474         void *saved_tmpmemctx = NULL;
475
476         /* close db environment */
477         if( bdb->bi_dbenv ) {
478                 /* force a checkpoint */
479                 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
480                 if( rc != 0 ) {
481                         Debug( LDAP_DEBUG_ANY,
482                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
483                                 db_strerror(rc), rc, 0 );
484                 }
485
486                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
487                 bdb->bi_dbenv = NULL;
488                 if( rc != 0 ) {
489                         Debug( LDAP_DEBUG_ANY,
490                                 "bdb_db_destroy: close failed: %s (%d)\n",
491                                 db_strerror(rc), rc, 0 );
492                         return rc;
493                 }
494         }
495
496         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
497
498         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
499         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
500         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
501         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
502         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
503         if ( bdb->bi_idl_cache_max_size ) {
504                 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
505                 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
506         }
507
508 #ifdef BDB_PSEARCH
509         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
510         ps = LDAP_LIST_FIRST( &bdb->bi_psearch_list );
511
512         if ( ps ) {
513                 psn = LDAP_LIST_NEXT( ps, o_ps_link );
514
515                 saved_tmpmemctx = ps->o_tmpmemctx;
516
517                 if (!BER_BVISNULL(&ps->o_req_dn)) {
518                         slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
519                 }
520                 if (!BER_BVISNULL(&ps->o_req_ndn)) {
521                         slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
522                 }
523                 if (!BER_BVISNULL(&ps->ors_filterstr)) {
524                         slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
525                 }
526                 if (ps->ors_filter != NULL) {
527                         filter_free_x(ps, ps->ors_filter);
528                 }
529                 if ( ps->ors_attrs != NULL) {
530                         ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
531                 }
532
533                 slap_op_free( ps );
534
535                 if ( saved_tmpmemctx ) {
536                         slap_sl_mem_destroy( NULL, saved_tmpmemctx );
537                 }
538         }
539
540         while ( psn ) {
541                 ps = psn;
542                 psn = LDAP_LIST_NEXT( ps, o_ps_link );
543
544                 saved_tmpmemctx = ps->o_tmpmemctx;
545
546                 if (!BER_BVISNULL(&ps->o_req_dn)) {
547                         slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
548                 }
549                 if (!BER_BVISNULL(&ps->o_req_ndn)) {
550                         slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
551                 }
552                 if (!BER_BVISNULL(&ps->ors_filterstr)) {
553                         slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
554                 }
555                 if (ps->ors_filter != NULL) {
556                         filter_free_x(ps, ps->ors_filter);
557                 }
558                 if ( ps->ors_attrs != NULL) {
559                         ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
560                 }
561
562                 slap_op_free( ps );
563
564                 if ( saved_tmpmemctx ) {
565                         slap_sl_mem_destroy( NULL, saved_tmpmemctx );
566                 }
567         }
568 #endif
569
570         ch_free( bdb );
571         be->be_private = NULL;
572
573         return 0;
574 }
575
576 int
577 bdb_back_initialize(
578         BackendInfo     *bi )
579 {
580         static char *controls[] = {
581                 LDAP_CONTROL_ASSERT,
582                 LDAP_CONTROL_MANAGEDSAIT,
583                 LDAP_CONTROL_NOOP,
584                 LDAP_CONTROL_PAGEDRESULTS,
585 #ifdef LDAP_CONTROL_SUBENTRIES
586                 LDAP_CONTROL_SUBENTRIES,
587 #endif
588                 LDAP_CONTROL_VALUESRETURNFILTER,
589 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
590                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
591 #endif
592                 NULL
593         };
594
595         /* initialize the underlying database system */
596         Debug( LDAP_DEBUG_TRACE,
597                 LDAP_XSTRING(bdb_back_initialize) ": initialize " 
598                 BDB_UCTYPE " backend\n", 0, 0, 0 );
599
600         bi->bi_flags |=
601                 SLAP_BFLAG_INCREMENT |
602 #ifdef BDB_SUBENTRIES
603                 SLAP_BFLAG_SUBENTRIES |
604 #endif
605                 SLAP_BFLAG_ALIASES |
606                 SLAP_BFLAG_REFERRALS;
607
608         bi->bi_controls = controls;
609
610         {       /* version check */
611                 int major, minor, patch, ver;
612                 char *version = db_version( &major, &minor, &patch );
613 #ifdef HAVE_EBCDIC
614                 char v2[1024];
615
616                 /* All our stdio does an ASCII to EBCDIC conversion on
617                  * the output. Strings from the BDB library are already
618                  * in EBCDIC; we have to go back and forth...
619                  */
620                 strcpy( v2, version );
621                 __etoa( v2 );
622                 version = v2;
623 #endif
624
625                 ver = (major << 24) | (minor << 16) | patch;
626                 if( ver < DB_VERSION_FULL )
627                 {
628                         Debug( LDAP_DEBUG_ANY,
629                                 LDAP_XSTRING(bdb_back_initialize) ": "
630                                 "BDB library version mismatch:"
631                                 " expected " DB_VERSION_STRING ","
632                                 " got %s\n", version, 0, 0 );
633                 }
634
635                 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_back_initialize)
636                         ": %s\n", version, 0, 0 );
637         }
638
639         db_env_set_func_free( ber_memfree );
640         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
641         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
642 #ifndef NO_THREAD
643         /* This is a no-op on a NO_THREAD build. Leave the default
644          * alone so that BDB will sleep on interprocess conflicts.
645          */
646         db_env_set_func_yield( ldap_pvt_thread_yield );
647 #endif
648
649         {
650                 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
651
652                 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
653                 bdb_uuid.bv_val = uuidbuf;
654         }
655
656         bi->bi_open = 0;
657         bi->bi_close = 0;
658         bi->bi_config = 0;
659         bi->bi_destroy = 0;
660
661         bi->bi_db_init = bdb_db_init;
662         bi->bi_db_config = bdb_db_config;
663         bi->bi_db_open = bdb_db_open;
664         bi->bi_db_close = bdb_db_close;
665         bi->bi_db_destroy = bdb_db_destroy;
666
667         bi->bi_op_add = bdb_add;
668         bi->bi_op_bind = bdb_bind;
669         bi->bi_op_compare = bdb_compare;
670         bi->bi_op_delete = bdb_delete;
671         bi->bi_op_modify = bdb_modify;
672         bi->bi_op_modrdn = bdb_modrdn;
673         bi->bi_op_search = bdb_search;
674
675         bi->bi_op_unbind = 0;
676
677 #if 0   /* DELETE ME */
678         bi->bi_op_abandon = bdb_abandon;
679         bi->bi_op_cancel = bdb_cancel;
680 #endif
681
682         bi->bi_extended = bdb_extended;
683
684         bi->bi_chk_referrals = bdb_referrals;
685         bi->bi_operational = bdb_operational;
686         bi->bi_has_subordinates = bdb_hasSubordinates;
687         bi->bi_entry_release_rw = bdb_entry_release;
688         bi->bi_entry_get_rw = bdb_entry_get;
689
690         /*
691          * hooks for slap tools
692          */
693         bi->bi_tool_entry_open = bdb_tool_entry_open;
694         bi->bi_tool_entry_close = bdb_tool_entry_close;
695         bi->bi_tool_entry_first = bdb_tool_entry_next;
696         bi->bi_tool_entry_next = bdb_tool_entry_next;
697         bi->bi_tool_entry_get = bdb_tool_entry_get;
698         bi->bi_tool_entry_put = bdb_tool_entry_put;
699         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
700         bi->bi_tool_sync = 0;
701         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
702         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
703         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
704
705         bi->bi_connection_init = 0;
706         bi->bi_connection_destroy = 0;
707
708         return 0;
709 }
710
711 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
712         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
713
714 /* conditionally define the init_module() function */
715 #ifdef BDB_HIER
716 SLAP_BACKEND_INIT_MODULE( hdb )
717 #else /* !BDB_HIER */
718 SLAP_BACKEND_INIT_MODULE( bdb )
719 #endif /* !BDB_HIER */
720
721 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
722