Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-bdb / config.c
blob02b254e07215d0536a293e0a7dbde89bf648cdf9
1 /* config.c - bdb backend configuration file routine */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/back-bdb/config.c,v 1.91.2.11 2008/04/14 21:28:42 quanah Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 The OpenLDAP Foundation.
6 * All rights reserved.
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.
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>.
17 #include "portable.h"
19 #include <stdio.h>
20 #include <ac/ctype.h>
21 #include <ac/string.h>
22 #include <ac/errno.h>
24 #include "back-bdb.h"
26 #include "config.h"
28 #include "lutil.h"
29 #include "ldap_rq.h"
31 #ifdef DB_DIRTY_READ
32 # define SLAP_BDB_ALLOW_DIRTY_READ
33 #endif
35 #define bdb_cf_gen BDB_SYMBOL(cf_gen)
36 #define bdb_cf_cleanup BDB_SYMBOL(cf_cleanup)
37 #define bdb_checkpoint BDB_SYMBOL(checkpoint)
38 #define bdb_online_index BDB_SYMBOL(online_index)
40 static ConfigDriver bdb_cf_gen;
42 enum {
43 BDB_CHKPT = 1,
44 BDB_CONFIG,
45 BDB_CRYPTFILE,
46 BDB_CRYPTKEY,
47 BDB_DIRECTORY,
48 BDB_NOSYNC,
49 BDB_DIRTYR,
50 BDB_INDEX,
51 BDB_LOCKD,
52 BDB_SSTACK
55 static ConfigTable bdbcfg[] = {
56 { "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_DIRECTORY,
57 bdb_cf_gen, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
58 "DESC 'Directory for database content' "
59 "EQUALITY caseIgnoreMatch "
60 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
61 { "cachefree", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
62 (void *)offsetof(struct bdb_info, bi_cache.c_minfree),
63 "( OLcfgDbAt:1.11 NAME 'olcDbCacheFree' "
64 "DESC 'Number of extra entries to free when max is reached' "
65 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
66 { "cachesize", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
67 (void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
68 "( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
69 "DESC 'Entry cache size in entries' "
70 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
71 { "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
72 bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
73 "DESC 'Database checkpoint interval in kbytes and minutes' "
74 "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
75 { "cryptfile", "file", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_CRYPTFILE,
76 bdb_cf_gen, "( OLcfgDbAt:1.13 NAME 'olcDbCryptFile' "
77 "DESC 'Pathname of file containing the DB encryption key' "
78 "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
79 { "cryptkey", "key", 2, 2, 0, ARG_BERVAL|ARG_MAGIC|BDB_CRYPTKEY,
80 bdb_cf_gen, "( OLcfgDbAt:1.14 NAME 'olcDbCryptKey' "
81 "DESC 'DB encryption key' "
82 "SYNTAX OMsOctetString SINGLE-VALUE )",NULL, NULL },
83 { "dbconfig", "DB_CONFIG setting", 1, 0, 0, ARG_MAGIC|BDB_CONFIG,
84 bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
85 "DESC 'BerkeleyDB DB_CONFIG configuration directives' "
86 "SYNTAX OMsIA5String X-ORDERED 'VALUES' )", NULL, NULL },
87 { "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
88 bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
89 "DESC 'Disable synchronous database writes' "
90 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
91 { "dirtyread", NULL, 1, 2, 0,
92 #ifdef SLAP_BDB_ALLOW_DIRTY_READ
93 ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
94 #else
95 ARG_IGNORED, NULL,
96 #endif
97 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
98 "DESC 'Allow reads of uncommitted data' "
99 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
100 { "dncachesize", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
101 (void *)offsetof(struct bdb_info, bi_cache.c_eimax),
102 "( OLcfgDbAt:1.12 NAME 'olcDbDNcacheSize' "
103 "DESC 'DN cache size' "
104 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
105 { "idlcachesize", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
106 (void *)offsetof(struct bdb_info, bi_idl_cache_max_size),
107 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
108 "DESC 'IDL cache size in IDLs' "
109 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
110 { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
111 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
112 "DESC 'Attribute index parameters' "
113 "EQUALITY caseIgnoreMatch "
114 "SYNTAX OMsDirectoryString )", NULL, NULL },
115 { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
116 (void *)offsetof(struct bdb_info, bi_linear_index),
117 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
118 "DESC 'Index attributes one at a time' "
119 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
120 { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
121 bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
122 "DESC 'Deadlock detection algorithm' "
123 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
124 { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
125 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
126 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
127 "DESC 'Unix permissions of database files' "
128 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
129 { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
130 bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
131 "DESC 'Depth of search stack in IDLs' "
132 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
133 { "shm_key", "key", 2, 2, 0, ARG_LONG|ARG_OFFSET,
134 (void *)offsetof(struct bdb_info, bi_shm_key),
135 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
136 "DESC 'Key for shared memory region' "
137 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
138 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
139 NULL, NULL, NULL, NULL }
142 static ConfigOCs bdbocs[] = {
144 #ifdef BDB_HIER
145 "( OLcfgDbOc:1.2 "
146 "NAME 'olcHdbConfig' "
147 "DESC 'HDB backend configuration' "
148 #else
149 "( OLcfgDbOc:1.1 "
150 "NAME 'olcBdbConfig' "
151 "DESC 'BDB backend configuration' "
152 #endif
153 "SUP olcDatabaseConfig "
154 "MUST olcDbDirectory "
155 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
156 "olcDbCryptFile $ olcDbCryptKey $ "
157 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
158 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
159 "olcDbMode $ olcDbSearchStack $ olcDbShmKey $ "
160 "olcDbCacheFree $ olcDbDNcacheSize ) )",
161 Cft_Database, bdbcfg },
162 { NULL, 0, NULL }
165 static slap_verbmasks bdb_lockd[] = {
166 { BER_BVC("default"), DB_LOCK_DEFAULT },
167 { BER_BVC("oldest"), DB_LOCK_OLDEST },
168 { BER_BVC("random"), DB_LOCK_RANDOM },
169 { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
170 { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
171 { BER_BVNULL, 0 }
174 /* perform periodic checkpoints */
175 static void *
176 bdb_checkpoint( void *ctx, void *arg )
178 struct re_s *rtask = arg;
179 struct bdb_info *bdb = rtask->arg;
181 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
182 bdb->bi_txn_cp_min, 0 );
183 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
184 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
185 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
186 return NULL;
189 /* reindex entries on the fly */
190 static void *
191 bdb_online_index( void *ctx, void *arg )
193 struct re_s *rtask = arg;
194 BackendDB *be = rtask->arg;
195 struct bdb_info *bdb = be->be_private;
197 Connection conn = {0};
198 OperationBuffer opbuf;
199 Operation *op;
201 DBC *curs;
202 DBT key, data;
203 DB_TXN *txn;
204 DB_LOCK lock;
205 BDB_LOCKER locker;
206 ID id, nid;
207 EntryInfo *ei;
208 int rc, getnext = 1;
209 int i;
211 connection_fake_init( &conn, &opbuf, ctx );
212 op = &opbuf.ob_op;
214 op->o_bd = be;
216 DBTzero( &key );
217 DBTzero( &data );
219 id = 1;
220 key.data = &nid;
221 key.size = key.ulen = sizeof(ID);
222 key.flags = DB_DBT_USERMEM;
224 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
225 data.dlen = data.ulen = 0;
227 while ( 1 ) {
228 if ( slapd_shutdown )
229 break;
231 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
232 if ( rc )
233 break;
234 locker = TXN_ID( txn );
235 if ( getnext ) {
236 getnext = 0;
237 BDB_ID2DISK( id, &nid );
238 rc = bdb->bi_id2entry->bdi_db->cursor(
239 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
240 if ( rc ) {
241 TXN_ABORT( txn );
242 break;
244 rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
245 curs->c_close( curs );
246 if ( rc ) {
247 TXN_ABORT( txn );
248 if ( rc == DB_NOTFOUND )
249 rc = 0;
250 if ( rc == DB_LOCK_DEADLOCK ) {
251 ldap_pvt_thread_yield();
252 continue;
254 break;
256 BDB_DISK2ID( &nid, &id );
259 ei = NULL;
260 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
261 if ( rc ) {
262 TXN_ABORT( txn );
263 if ( rc == DB_LOCK_DEADLOCK ) {
264 ldap_pvt_thread_yield();
265 continue;
267 if ( rc == DB_NOTFOUND ) {
268 id++;
269 getnext = 1;
270 continue;
272 break;
274 if ( ei->bei_e ) {
275 rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
276 if ( rc == DB_LOCK_DEADLOCK ) {
277 TXN_ABORT( txn );
278 ldap_pvt_thread_yield();
279 continue;
281 if ( rc == 0 ) {
282 rc = TXN_COMMIT( txn, 0 );
283 txn = NULL;
285 if ( rc )
286 break;
288 id++;
289 getnext = 1;
292 for ( i = 0; i < bdb->bi_nattrs; i++ ) {
293 if ( bdb->bi_attrs[ i ]->ai_indexmask & BDB_INDEX_DELETING
294 || bdb->bi_attrs[ i ]->ai_newmask == 0 )
296 continue;
298 bdb->bi_attrs[ i ]->ai_indexmask = bdb->bi_attrs[ i ]->ai_newmask;
299 bdb->bi_attrs[ i ]->ai_newmask = 0;
302 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
303 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
304 bdb->bi_index_task = NULL;
305 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
306 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
308 return NULL;
311 /* Cleanup loose ends after Modify completes */
312 static int
313 bdb_cf_cleanup( ConfigArgs *c )
315 struct bdb_info *bdb = c->be->be_private;
316 int rc = 0;
318 if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
319 if ( bdb->bi_db_config ) {
320 int i;
321 FILE *f = fopen( bdb->bi_db_config_path, "w" );
322 if ( f ) {
323 for (i=0; bdb->bi_db_config[i].bv_val; i++)
324 fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
325 fclose( f );
327 } else {
328 unlink( bdb->bi_db_config_path );
330 bdb->bi_flags ^= BDB_UPD_CONFIG;
333 if ( bdb->bi_flags & BDB_DEL_INDEX ) {
334 bdb_attr_flush( bdb );
335 bdb->bi_flags ^= BDB_DEL_INDEX;
338 if ( bdb->bi_flags & BDB_RE_OPEN ) {
339 bdb->bi_flags ^= BDB_RE_OPEN;
340 rc = c->be->bd_info->bi_db_close( c->be, &c->reply );
341 if ( rc == 0 )
342 rc = c->be->bd_info->bi_db_open( c->be, &c->reply );
343 /* If this fails, we need to restart */
344 if ( rc ) {
345 slapd_shutdown = 2;
346 snprintf( c->cr_msg, sizeof( c->cr_msg ),
347 "failed to reopen database, rc=%d", rc );
348 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
349 ": %s\n", c->cr_msg, 0, 0 );
350 rc = LDAP_OTHER;
353 return rc;
356 static int
357 bdb_cf_gen( ConfigArgs *c )
359 struct bdb_info *bdb = c->be->be_private;
360 int rc;
362 if ( c->op == SLAP_CONFIG_EMIT ) {
363 rc = 0;
364 switch( c->type ) {
365 case BDB_CHKPT:
366 if ( bdb->bi_txn_cp ) {
367 char buf[64];
368 struct berval bv;
369 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
370 bdb->bi_txn_cp_min );
371 bv.bv_val = buf;
372 value_add_one( &c->rvalue_vals, &bv );
373 } else{
374 rc = 1;
376 break;
378 case BDB_CRYPTFILE:
379 if ( bdb->bi_db_crypt_file ) {
380 c->value_string = ch_strdup( bdb->bi_db_crypt_file );
381 } else {
382 rc = 1;
384 break;
386 /* If a crypt file has been set, its contents are copied here.
387 * But we don't want the key to be incorporated here.
389 case BDB_CRYPTKEY:
390 if ( !bdb->bi_db_crypt_file && !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
391 value_add_one( &c->rvalue_vals, &bdb->bi_db_crypt_key );
392 } else {
393 rc = 1;
395 break;
397 case BDB_DIRECTORY:
398 if ( bdb->bi_dbenv_home ) {
399 c->value_string = ch_strdup( bdb->bi_dbenv_home );
400 } else {
401 rc = 1;
403 break;
405 case BDB_CONFIG:
406 if ( !( bdb->bi_flags & BDB_IS_OPEN )
407 && !bdb->bi_db_config )
409 char buf[SLAP_TEXT_BUFLEN];
410 FILE *f = fopen( bdb->bi_db_config_path, "r" );
411 struct berval bv;
413 if ( f ) {
414 bdb->bi_flags |= BDB_HAS_CONFIG;
415 while ( fgets( buf, sizeof(buf), f )) {
416 ber_str2bv( buf, 0, 1, &bv );
417 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
418 bv.bv_len--;
419 bv.bv_val[bv.bv_len] = '\0';
421 /* shouldn't need this, but ... */
422 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
423 bv.bv_len--;
424 bv.bv_val[bv.bv_len] = '\0';
426 ber_bvarray_add( &bdb->bi_db_config, &bv );
428 fclose( f );
431 if ( bdb->bi_db_config ) {
432 int i;
433 struct berval bv;
435 bv.bv_val = c->log;
436 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
437 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
438 bdb->bi_db_config[i].bv_val );
439 value_add_one( &c->rvalue_vals, &bv );
442 if ( !c->rvalue_vals ) rc = 1;
443 break;
445 case BDB_NOSYNC:
446 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
447 c->value_int = 1;
448 break;
450 case BDB_INDEX:
451 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
452 if ( !c->rvalue_vals ) rc = 1;
453 break;
455 case BDB_LOCKD:
456 rc = 1;
457 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
458 int i;
459 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
460 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
461 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
462 rc = 0;
463 break;
467 break;
469 case BDB_SSTACK:
470 c->value_int = bdb->bi_search_stack_depth;
471 break;
473 return rc;
474 } else if ( c->op == LDAP_MOD_DELETE ) {
475 rc = 0;
476 switch( c->type ) {
477 /* single-valued no-ops */
478 case BDB_LOCKD:
479 case BDB_SSTACK:
480 break;
482 case BDB_CHKPT:
483 if ( bdb->bi_txn_cp_task ) {
484 struct re_s *re = bdb->bi_txn_cp_task;
485 bdb->bi_txn_cp_task = NULL;
486 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
487 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
488 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
489 ldap_pvt_runqueue_remove( &slapd_rq, re );
490 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
492 bdb->bi_txn_cp = 0;
493 break;
494 case BDB_CONFIG:
495 if ( c->valx < 0 ) {
496 ber_bvarray_free( bdb->bi_db_config );
497 bdb->bi_db_config = NULL;
498 } else {
499 int i = c->valx;
500 ch_free( bdb->bi_db_config[i].bv_val );
501 for (; bdb->bi_db_config[i].bv_val; i++)
502 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
504 bdb->bi_flags |= BDB_UPD_CONFIG;
505 c->cleanup = bdb_cf_cleanup;
506 break;
507 /* Doesn't really make sense to change these on the fly;
508 * the entire DB must be dumped and reloaded
510 case BDB_CRYPTFILE:
511 if ( bdb->bi_db_crypt_file ) {
512 ch_free( bdb->bi_db_crypt_file );
513 bdb->bi_db_crypt_file = NULL;
515 /* FALLTHRU */
516 case BDB_CRYPTKEY:
517 if ( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
518 ch_free( bdb->bi_db_crypt_key.bv_val );
519 BER_BVZERO( &bdb->bi_db_crypt_key );
521 break;
522 case BDB_DIRECTORY:
523 bdb->bi_flags |= BDB_RE_OPEN;
524 bdb->bi_flags ^= BDB_HAS_CONFIG;
525 ch_free( bdb->bi_dbenv_home );
526 bdb->bi_dbenv_home = NULL;
527 ch_free( bdb->bi_db_config_path );
528 bdb->bi_db_config_path = NULL;
529 c->cleanup = bdb_cf_cleanup;
530 ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
531 break;
532 case BDB_NOSYNC:
533 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
534 break;
535 case BDB_INDEX:
536 if ( c->valx == -1 ) {
537 int i;
539 /* delete all (FIXME) */
540 for ( i = 0; i < bdb->bi_nattrs; i++ ) {
541 bdb->bi_attrs[i]->ai_indexmask |= BDB_INDEX_DELETING;
543 bdb->bi_flags |= BDB_DEL_INDEX;
544 c->cleanup = bdb_cf_cleanup;
546 } else {
547 struct berval bv, def = BER_BVC("default");
548 char *ptr;
550 for (ptr = c->line; !isspace( (unsigned char) *ptr ); ptr++);
552 bv.bv_val = c->line;
553 bv.bv_len = ptr - bv.bv_val;
554 if ( bvmatch( &bv, &def )) {
555 bdb->bi_defaultmask = 0;
557 } else {
558 int i;
559 char **attrs;
560 char sep;
562 sep = bv.bv_val[ bv.bv_len ];
563 bv.bv_val[ bv.bv_len ] = '\0';
564 attrs = ldap_str2charray( bv.bv_val, "," );
566 for ( i = 0; attrs[ i ]; i++ ) {
567 AttributeDescription *ad = NULL;
568 const char *text;
569 AttrInfo *ai;
571 slap_str2ad( attrs[ i ], &ad, &text );
572 /* if we got here... */
573 assert( ad != NULL );
575 ai = bdb_attr_mask( bdb, ad );
576 /* if we got here... */
577 assert( ai != NULL );
579 ai->ai_indexmask |= BDB_INDEX_DELETING;
580 bdb->bi_flags |= BDB_DEL_INDEX;
581 c->cleanup = bdb_cf_cleanup;
584 bv.bv_val[ bv.bv_len ] = sep;
585 ldap_charray_free( attrs );
588 break;
590 return rc;
593 switch( c->type ) {
594 case BDB_CHKPT: {
595 long l;
596 bdb->bi_txn_cp = 1;
597 if ( lutil_atolx( &l, c->argv[1], 0 ) != 0 ) {
598 fprintf( stderr, "%s: "
599 "invalid kbyte \"%s\" in \"checkpoint\".\n",
600 c->log, c->argv[1] );
601 return 1;
603 bdb->bi_txn_cp_kbyte = l;
604 if ( lutil_atolx( &l, c->argv[2], 0 ) != 0 ) {
605 fprintf( stderr, "%s: "
606 "invalid minutes \"%s\" in \"checkpoint\".\n",
607 c->log, c->argv[2] );
608 return 1;
610 bdb->bi_txn_cp_min = l;
611 /* If we're in server mode and time-based checkpointing is enabled,
612 * submit a task to perform periodic checkpoints.
614 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
615 struct re_s *re = bdb->bi_txn_cp_task;
616 if ( re ) {
617 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
618 } else {
619 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
620 fprintf( stderr, "%s: "
621 "\"checkpoint\" must occur after \"suffix\".\n",
622 c->log );
623 return 1;
625 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
626 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
627 bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
628 LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
629 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
632 } break;
634 case BDB_CONFIG: {
635 char *ptr = c->line;
636 struct berval bv;
638 if ( c->op == SLAP_CONFIG_ADD ) {
639 ptr += STRLENOF("dbconfig");
640 while (!isspace((unsigned char)*ptr)) ptr++;
641 while (isspace((unsigned char)*ptr)) ptr++;
644 if ( bdb->bi_flags & BDB_IS_OPEN ) {
645 bdb->bi_flags |= BDB_UPD_CONFIG;
646 c->cleanup = bdb_cf_cleanup;
647 } else {
648 /* If we're just starting up...
650 FILE *f;
651 /* If a DB_CONFIG file exists, or we don't know the path
652 * to the DB_CONFIG file, ignore these directives
654 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
655 break;
656 f = fopen( bdb->bi_db_config_path, "a" );
657 if ( f ) {
658 /* FIXME: EBCDIC probably needs special handling */
659 fprintf( f, "%s\n", ptr );
660 fclose( f );
663 ber_str2bv( ptr, 0, 1, &bv );
664 ber_bvarray_add( &bdb->bi_db_config, &bv );
666 break;
668 case BDB_CRYPTFILE:
669 rc = lutil_get_filed_password( c->value_string, &bdb->bi_db_crypt_key );
670 if ( rc == 0 ) {
671 bdb->bi_db_crypt_file = c->value_string;
673 break;
675 /* Cannot set key if file was already set */
676 case BDB_CRYPTKEY:
677 if ( bdb->bi_db_crypt_file ) {
678 rc = 1;
679 } else {
680 bdb->bi_db_crypt_key = c->value_bv;
682 break;
684 case BDB_DIRECTORY: {
685 FILE *f;
686 char *ptr, *testpath;
687 int len;
689 len = strlen( c->value_string );
690 testpath = ch_malloc( len + STRLENOF(LDAP_DIRSEP) + STRLENOF("DUMMY") + 1 );
691 ptr = lutil_strcopy( testpath, c->value_string );
692 *ptr++ = LDAP_DIRSEP[0];
693 strcpy( ptr, "DUMMY" );
694 f = fopen( testpath, "w" );
695 if ( f ) {
696 fclose( f );
697 unlink( testpath );
699 ch_free( testpath );
700 if ( !f ) {
701 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: invalid path: %s",
702 c->log, strerror( errno ));
703 Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
704 return -1;
707 if ( bdb->bi_dbenv_home )
708 ch_free( bdb->bi_dbenv_home );
709 bdb->bi_dbenv_home = c->value_string;
711 /* See if a DB_CONFIG file already exists here */
712 if ( bdb->bi_db_config_path )
713 ch_free( bdb->bi_db_config_path );
714 bdb->bi_db_config_path = ch_malloc( len +
715 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
716 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
717 *ptr++ = LDAP_DIRSEP[0];
718 strcpy( ptr, "DB_CONFIG" );
720 f = fopen( bdb->bi_db_config_path, "r" );
721 if ( f ) {
722 bdb->bi_flags |= BDB_HAS_CONFIG;
723 fclose(f);
726 break;
728 case BDB_NOSYNC:
729 if ( c->value_int )
730 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
731 else
732 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
733 if ( bdb->bi_flags & BDB_IS_OPEN ) {
734 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
735 c->value_int );
737 break;
739 case BDB_INDEX:
740 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
741 c->argc - 1, &c->argv[1] );
743 if( rc != LDAP_SUCCESS ) return 1;
744 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
745 /* Start the task as soon as we finish here. Set a long
746 * interval (10 hours) so that it only gets scheduled once.
748 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
749 fprintf( stderr, "%s: "
750 "\"index\" must occur after \"suffix\".\n",
751 c->log );
752 return 1;
754 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
755 bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
756 bdb_online_index, c->be,
757 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
758 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
760 break;
762 case BDB_LOCKD:
763 rc = verb_to_mask( c->argv[1], bdb_lockd );
764 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
765 fprintf( stderr, "%s: "
766 "bad policy (%s) in \"lockDetect <policy>\" line\n",
767 c->log, c->argv[1] );
768 return 1;
770 bdb->bi_lock_detect = rc;
771 break;
773 case BDB_SSTACK:
774 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
775 fprintf( stderr,
776 "%s: depth %d too small, using %d\n",
777 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
778 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
780 bdb->bi_search_stack_depth = c->value_int;
781 break;
783 return 0;
786 int bdb_back_init_cf( BackendInfo *bi )
788 int rc;
789 bi->bi_cf_ocs = bdbocs;
791 rc = config_register_schema( bdbcfg, bdbocs );
792 if ( rc ) return rc;
793 return 0;