1/* config.c - bdb backend configuration file routine */
2/* $OpenLDAP: pkg/ldap/servers/slapd/back-bdb/config.c,v 1.91.2.19 2010/04/13 20:23:23 kurt Exp $ */
3/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2000-2010 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/ctype.h>
21#include <ac/string.h>
22#include <ac/errno.h>
23
24#include "back-bdb.h"
25
26#include "config.h"
27
28#include "lutil.h"
29#include "ldap_rq.h"
30
31#ifdef DB_DIRTY_READ
32#	define	SLAP_BDB_ALLOW_DIRTY_READ
33#endif
34
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)
39
40static ConfigDriver bdb_cf_gen;
41
42enum {
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,
53	BDB_MODE,
54	BDB_PGSIZE,
55	BDB_CHECKSUM,
56	BDB_DISABLE_FULLFSYNC_MODE
57};
58
59static ConfigTable bdbcfg[] = {
60	{ "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_DIRECTORY,
61		bdb_cf_gen, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
62			"DESC 'Directory for database content' "
63			"EQUALITY caseIgnoreMatch "
64			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
65	{ "cachefree", "size", 2, 2, 0, ARG_ULONG|ARG_OFFSET,
66		(void *)offsetof(struct bdb_info, bi_cache.c_minfree),
67		"( OLcfgDbAt:1.11 NAME 'olcDbCacheFree' "
68			"DESC 'Number of extra entries to free when max is reached' "
69			"SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
70	{ "cachesize", "size", 2, 2, 0, ARG_ULONG|ARG_OFFSET,
71		(void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
72		"( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
73			"DESC 'Entry cache size in entries' "
74			"SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
75	{ "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
76		bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
77			"DESC 'Database checkpoint interval in kbytes and minutes' "
78			"SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
79	{ "checksum", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_CHECKSUM,
80		bdb_cf_gen, "( OLcfgDbAt:1.16 NAME 'olcDbChecksum' "
81			"DESC 'Enable database checksum validation' "
82			"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
83	{ "cryptfile", "file", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_CRYPTFILE,
84		bdb_cf_gen, "( OLcfgDbAt:1.13 NAME 'olcDbCryptFile' "
85			"DESC 'Pathname of file containing the DB encryption key' "
86			"SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
87	{ "cryptkey", "key", 2, 2, 0, ARG_BERVAL|ARG_MAGIC|BDB_CRYPTKEY,
88		bdb_cf_gen, "( OLcfgDbAt:1.14 NAME 'olcDbCryptKey' "
89			"DESC 'DB encryption key' "
90			"SYNTAX OMsOctetString SINGLE-VALUE )",NULL, NULL },
91	{ "dbconfig", "DB_CONFIG setting", 1, 0, 0, ARG_MAGIC|BDB_CONFIG,
92		bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
93			"DESC 'BerkeleyDB DB_CONFIG configuration directives' "
94			"SYNTAX OMsIA5String X-ORDERED 'VALUES' )", NULL, NULL },
95	{ "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
96		bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
97			"DESC 'Disable synchronous database writes' "
98			"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
99	{ "dbpagesize", "db> <size", 3, 3, 0, ARG_MAGIC|BDB_PGSIZE,
100		bdb_cf_gen, "( OLcfgDbAt:1.15 NAME 'olcDbPageSize' "
101			"DESC 'Page size of specified DB, in Kbytes' "
102			"EQUALITY caseExactMatch "
103			"SYNTAX OMsDirectoryString )", NULL, NULL },
104	{ "dirtyread", NULL, 1, 2, 0,
105#ifdef SLAP_BDB_ALLOW_DIRTY_READ
106		ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
107#else
108		ARG_IGNORED, NULL,
109#endif
110		"( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
111		"DESC 'Allow reads of uncommitted data' "
112		"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
113	{ "dncachesize", "size", 2, 2, 0, ARG_ULONG|ARG_OFFSET,
114		(void *)offsetof(struct bdb_info, bi_cache.c_eimax),
115		"( OLcfgDbAt:1.12 NAME 'olcDbDNcacheSize' "
116			"DESC 'DN cache size' "
117			"SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
118	{ "idlcachesize", "size", 2, 2, 0, ARG_ULONG|ARG_OFFSET,
119		(void *)offsetof(struct bdb_info, bi_idl_cache_max_size),
120		"( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
121		"DESC 'IDL cache size in IDLs' "
122		"SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
123	{ "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
124		bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
125		"DESC 'Attribute index parameters' "
126		"EQUALITY caseIgnoreMatch "
127		"SYNTAX OMsDirectoryString )", NULL, NULL },
128	{ "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
129		(void *)offsetof(struct bdb_info, bi_linear_index),
130		"( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
131		"DESC 'Index attributes one at a time' "
132		"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
133	{ "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
134		bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
135		"DESC 'Deadlock detection algorithm' "
136		"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
137	{ "mode", "mode", 2, 2, 0, ARG_MAGIC|BDB_MODE,
138		bdb_cf_gen, "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
139		"DESC 'Unix permissions of database files' "
140		"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
141	{ "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
142		bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
143		"DESC 'Depth of search stack in IDLs' "
144		"SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
145	{ "shm_key", "key", 2, 2, 0, ARG_LONG|ARG_OFFSET,
146		(void *)offsetof(struct bdb_info, bi_shm_key),
147		"( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
148		"DESC 'Key for shared memory region' "
149		"SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
150	{ "fullfsync", NULL, 1, 2, 0,
151#ifdef __APPLE__
152                ARG_ON_OFF|ARG_MAGIC|BDB_DISABLE_FULLFSYNC_MODE, bdb_cf_gen,
153#else
154                ARG_IGNORED, NULL,
155#endif
156                "( OLcfgDbAt:701 NAME 'olcDbDisableFullFSyncMode' "
157                "DESC 'Disable fullfsync mode for writes' "
158                "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
159	{ NULL, NULL, 0, 0, 0, ARG_IGNORED,
160		NULL, NULL, NULL, NULL }
161};
162
163static ConfigOCs bdbocs[] = {
164	{
165#ifdef BDB_HIER
166		"( OLcfgDbOc:1.2 "
167		"NAME 'olcHdbConfig' "
168		"DESC 'HDB backend configuration' "
169#else
170		"( OLcfgDbOc:1.1 "
171		"NAME 'olcBdbConfig' "
172		"DESC 'BDB backend configuration' "
173#endif
174		"SUP olcDatabaseConfig "
175		"MUST olcDbDirectory "
176		"MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
177		"olcDbCryptFile $ olcDbCryptKey $ "
178		"olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
179		"olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
180		"olcDbMode $ olcDbSearchStack $ olcDbShmKey $ "
181		"olcDbCacheFree $ olcDbDNcacheSize $ olcDbPageSize $ "
182		"olcDbDisableFullFSyncMode) )",Cft_Database, bdbcfg },
183	{ NULL, 0, NULL }
184};
185
186static slap_verbmasks bdb_lockd[] = {
187	{ BER_BVC("default"), DB_LOCK_DEFAULT },
188	{ BER_BVC("oldest"), DB_LOCK_OLDEST },
189	{ BER_BVC("random"), DB_LOCK_RANDOM },
190	{ BER_BVC("youngest"), DB_LOCK_YOUNGEST },
191	{ BER_BVC("fewest"), DB_LOCK_MINLOCKS },
192	{ BER_BVNULL, 0 }
193};
194
195/* perform periodic checkpoints */
196static void *
197bdb_checkpoint( void *ctx, void *arg )
198{
199	struct re_s *rtask = arg;
200	struct bdb_info *bdb = rtask->arg;
201
202	TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
203		bdb->bi_txn_cp_min, 0 );
204	if(bdb->bi_disable_fullfsync_mode && !slapAddMode)
205                Debug( LDAP_DEBUG_ANY, "slapd is running in import mode - only use if importing large data \n", 0, 0, 0 );
206	ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
207	ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
208	ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
209	return NULL;
210}
211
212/* reindex entries on the fly */
213static void *
214bdb_online_index( void *ctx, void *arg )
215{
216	struct re_s *rtask = arg;
217	BackendDB *be = rtask->arg;
218	struct bdb_info *bdb = be->be_private;
219
220	Connection conn = {0};
221	OperationBuffer opbuf;
222	Operation *op;
223
224	DBC *curs;
225	DBT key, data;
226	DB_TXN *txn;
227	DB_LOCK lock;
228	ID id, nid;
229	EntryInfo *ei;
230	int rc, getnext = 1;
231	int i;
232
233	connection_fake_init( &conn, &opbuf, ctx );
234	op = &opbuf.ob_op;
235
236	op->o_bd = be;
237
238	DBTzero( &key );
239	DBTzero( &data );
240
241	id = 1;
242	key.data = &nid;
243	key.size = key.ulen = sizeof(ID);
244	key.flags = DB_DBT_USERMEM;
245
246	data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
247	data.dlen = data.ulen = 0;
248
249	while ( 1 ) {
250		if ( slapd_shutdown )
251			break;
252
253		rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
254		if ( rc )
255			break;
256		if ( getnext ) {
257			getnext = 0;
258			BDB_ID2DISK( id, &nid );
259			rc = bdb->bi_id2entry->bdi_db->cursor(
260				bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
261			if ( rc ) {
262				TXN_ABORT( txn );
263				break;
264			}
265			rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
266			curs->c_close( curs );
267			if ( rc ) {
268				TXN_ABORT( txn );
269				if ( rc == DB_NOTFOUND )
270					rc = 0;
271				if ( rc == DB_LOCK_DEADLOCK ) {
272					ldap_pvt_thread_yield();
273					continue;
274				}
275				break;
276			}
277			BDB_DISK2ID( &nid, &id );
278		}
279
280		ei = NULL;
281		rc = bdb_cache_find_id( op, txn, id, &ei, 0, &lock );
282		if ( rc ) {
283			TXN_ABORT( txn );
284			if ( rc == DB_LOCK_DEADLOCK ) {
285				ldap_pvt_thread_yield();
286				continue;
287			}
288			if ( rc == DB_NOTFOUND ) {
289				id++;
290				getnext = 1;
291				continue;
292			}
293			break;
294		}
295		if ( ei->bei_e ) {
296			rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
297			if ( rc == DB_LOCK_DEADLOCK ) {
298				TXN_ABORT( txn );
299				ldap_pvt_thread_yield();
300				continue;
301			}
302			if ( rc == 0 ) {
303				rc = TXN_COMMIT( txn, 0 );
304				txn = NULL;
305			}
306			if ( rc )
307				break;
308		}
309		id++;
310		getnext = 1;
311	}
312
313	for ( i = 0; i < bdb->bi_nattrs; i++ ) {
314		if ( bdb->bi_attrs[ i ]->ai_indexmask & BDB_INDEX_DELETING
315			|| bdb->bi_attrs[ i ]->ai_newmask == 0 )
316		{
317			continue;
318		}
319		bdb->bi_attrs[ i ]->ai_indexmask = bdb->bi_attrs[ i ]->ai_newmask;
320		bdb->bi_attrs[ i ]->ai_newmask = 0;
321	}
322
323	ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
324	ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
325	bdb->bi_index_task = NULL;
326	ldap_pvt_runqueue_remove( &slapd_rq, rtask );
327	ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
328
329	return NULL;
330}
331
332/* Cleanup loose ends after Modify completes */
333static int
334bdb_cf_cleanup( ConfigArgs *c )
335{
336	struct bdb_info *bdb = c->be->be_private;
337	int rc = 0;
338
339	if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
340		if ( bdb->bi_db_config ) {
341			int i;
342			FILE *f = fopen( bdb->bi_db_config_path, "w" );
343			if ( f ) {
344				for (i=0; bdb->bi_db_config[i].bv_val; i++)
345					fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
346				fclose( f );
347			}
348		} else {
349			unlink( bdb->bi_db_config_path );
350		}
351		bdb->bi_flags ^= BDB_UPD_CONFIG;
352	}
353
354	if ( bdb->bi_flags & BDB_DEL_INDEX ) {
355		bdb_attr_flush( bdb );
356		bdb->bi_flags ^= BDB_DEL_INDEX;
357	}
358
359	if ( bdb->bi_flags & BDB_RE_OPEN ) {
360		bdb->bi_flags ^= BDB_RE_OPEN;
361		rc = c->be->bd_info->bi_db_close( c->be, &c->reply );
362		if ( rc == 0 )
363			rc = c->be->bd_info->bi_db_open( c->be, &c->reply );
364		/* If this fails, we need to restart */
365		if ( rc ) {
366			slapd_shutdown = 2;
367			snprintf( c->cr_msg, sizeof( c->cr_msg ),
368				"failed to reopen database, rc=%d", rc );
369			Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
370				": %s\n", c->cr_msg, 0, 0 );
371			rc = LDAP_OTHER;
372		}
373	}
374	return rc;
375}
376
377static int
378bdb_cf_gen( ConfigArgs *c )
379{
380	struct bdb_info *bdb = c->be->be_private;
381	int rc;
382
383	if ( c->op == SLAP_CONFIG_EMIT ) {
384		rc = 0;
385		switch( c->type ) {
386		case BDB_MODE: {
387			char buf[64];
388			struct berval bv;
389			bv.bv_len = snprintf( buf, sizeof(buf), "0%o", bdb->bi_dbenv_mode );
390			if ( bv.bv_len > 0 && bv.bv_len < sizeof(buf) ) {
391				bv.bv_val = buf;
392				value_add_one( &c->rvalue_vals, &bv );
393			} else {
394				rc = 1;
395			}
396			} break;
397
398		case BDB_CHKPT:
399			if ( bdb->bi_txn_cp ) {
400				char buf[64];
401				struct berval bv;
402				bv.bv_len = snprintf( buf, sizeof(buf), "%ld %ld",
403					(long) bdb->bi_txn_cp_kbyte, (long) bdb->bi_txn_cp_min );
404				if ( bv.bv_len > 0 && bv.bv_len < sizeof(buf) ) {
405					bv.bv_val = buf;
406					value_add_one( &c->rvalue_vals, &bv );
407				} else {
408					rc = 1;
409				}
410			} else {
411				rc = 1;
412			}
413			break;
414
415		case BDB_CRYPTFILE:
416			if ( bdb->bi_db_crypt_file ) {
417				c->value_string = ch_strdup( bdb->bi_db_crypt_file );
418			} else {
419				rc = 1;
420			}
421			break;
422
423		/* If a crypt file has been set, its contents are copied here.
424		 * But we don't want the key to be incorporated here.
425		 */
426		case BDB_CRYPTKEY:
427			if ( !bdb->bi_db_crypt_file && !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
428				value_add_one( &c->rvalue_vals, &bdb->bi_db_crypt_key );
429			} else {
430				rc = 1;
431			}
432			break;
433
434		case BDB_DIRECTORY:
435			if ( bdb->bi_dbenv_home ) {
436				c->value_string = ch_strdup( bdb->bi_dbenv_home );
437			} else {
438				rc = 1;
439			}
440			break;
441
442		case BDB_CONFIG:
443			if ( !( bdb->bi_flags & BDB_IS_OPEN )
444				&& !bdb->bi_db_config )
445			{
446				char	buf[SLAP_TEXT_BUFLEN];
447				FILE *f = fopen( bdb->bi_db_config_path, "r" );
448				struct berval bv;
449
450				if ( f ) {
451					bdb->bi_flags |= BDB_HAS_CONFIG;
452					while ( fgets( buf, sizeof(buf), f )) {
453						ber_str2bv( buf, 0, 1, &bv );
454						if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
455							bv.bv_len--;
456							bv.bv_val[bv.bv_len] = '\0';
457						}
458						/* shouldn't need this, but ... */
459						if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
460							bv.bv_len--;
461							bv.bv_val[bv.bv_len] = '\0';
462						}
463						ber_bvarray_add( &bdb->bi_db_config, &bv );
464					}
465					fclose( f );
466				}
467			}
468			if ( bdb->bi_db_config ) {
469				int i;
470				struct berval bv;
471
472				bv.bv_val = c->log;
473				for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
474					bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
475						bdb->bi_db_config[i].bv_val );
476					value_add_one( &c->rvalue_vals, &bv );
477				}
478			}
479			if ( !c->rvalue_vals ) rc = 1;
480			break;
481
482		case BDB_NOSYNC:
483			if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
484				c->value_int = 1;
485			break;
486
487		case BDB_DISABLE_FULLFSYNC_MODE:
488                        if ( bdb->bi_disable_fullfsync_mode )
489                                c->value_int = 1;
490                        break;
491		case BDB_CHECKSUM:
492			if ( bdb->bi_flags & BDB_CHKSUM )
493				c->value_int = 1;
494			break;
495
496		case BDB_INDEX:
497			bdb_attr_index_unparse( bdb, &c->rvalue_vals );
498			if ( !c->rvalue_vals ) rc = 1;
499			break;
500
501		case BDB_LOCKD:
502			rc = 1;
503			if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
504				int i;
505				for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
506					if ( bdb->bi_lock_detect == (u_int32_t)bdb_lockd[i].mask ) {
507						value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
508						rc = 0;
509						break;
510					}
511				}
512			}
513			break;
514
515		case BDB_SSTACK:
516			c->value_int = bdb->bi_search_stack_depth;
517			break;
518
519		case BDB_PGSIZE: {
520				struct bdb_db_pgsize *ps;
521				char buf[SLAP_TEXT_BUFLEN];
522				struct berval bv;
523				int rc = 1;
524
525				bv.bv_val = buf;
526				for ( ps = bdb->bi_pagesizes; ps; ps = ps->bdp_next ) {
527					bv.bv_len = sprintf( buf, "%s %d", ps->bdp_name.bv_val,
528						ps->bdp_size / 1024 );
529					value_add_one( &c->rvalue_vals, &bv );
530					rc = 0;
531
532				}
533				break;
534			}
535		}
536		return rc;
537	} else if ( c->op == LDAP_MOD_DELETE ) {
538		rc = 0;
539		switch( c->type ) {
540		case BDB_MODE:
541#if 0
542			/* FIXME: does it make any sense to change the mode,
543			 * if we don't exec a chmod()? */
544			bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
545			break;
546#endif
547
548		/* single-valued no-ops */
549		case BDB_LOCKD:
550		case BDB_SSTACK:
551			break;
552
553		case BDB_CHKPT:
554			if ( bdb->bi_txn_cp_task ) {
555				struct re_s *re = bdb->bi_txn_cp_task;
556				bdb->bi_txn_cp_task = NULL;
557				ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
558				if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
559					ldap_pvt_runqueue_stoptask( &slapd_rq, re );
560				ldap_pvt_runqueue_remove( &slapd_rq, re );
561				ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
562			}
563			bdb->bi_txn_cp = 0;
564			break;
565		case BDB_CONFIG:
566			if ( c->valx < 0 ) {
567				ber_bvarray_free( bdb->bi_db_config );
568				bdb->bi_db_config = NULL;
569			} else {
570				int i = c->valx;
571				ch_free( bdb->bi_db_config[i].bv_val );
572				for (; bdb->bi_db_config[i].bv_val; i++)
573					bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
574			}
575			bdb->bi_flags |= BDB_UPD_CONFIG;
576			c->cleanup = bdb_cf_cleanup;
577			break;
578		/* Doesn't really make sense to change these on the fly;
579		 * the entire DB must be dumped and reloaded
580		 */
581		case BDB_CRYPTFILE:
582			if ( bdb->bi_db_crypt_file ) {
583				ch_free( bdb->bi_db_crypt_file );
584				bdb->bi_db_crypt_file = NULL;
585			}
586			/* FALLTHRU */
587		case BDB_CRYPTKEY:
588			if ( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
589				ch_free( bdb->bi_db_crypt_key.bv_val );
590				BER_BVZERO( &bdb->bi_db_crypt_key );
591			}
592			break;
593		case BDB_DIRECTORY:
594			bdb->bi_flags |= BDB_RE_OPEN;
595			bdb->bi_flags ^= BDB_HAS_CONFIG;
596			ch_free( bdb->bi_dbenv_home );
597			bdb->bi_dbenv_home = NULL;
598			ch_free( bdb->bi_db_config_path );
599			bdb->bi_db_config_path = NULL;
600			c->cleanup = bdb_cf_cleanup;
601			ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
602			break;
603		case BDB_NOSYNC:
604			bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
605			break;
606		case BDB_DISABLE_FULLFSYNC_MODE:
607                        bdb->bi_disable_fullfsync_mode = 0;
608                        break;
609		case BDB_CHECKSUM:
610			bdb->bi_flags &= ~BDB_CHKSUM;
611			break;
612		case BDB_INDEX:
613			if ( c->valx == -1 ) {
614				int i;
615
616				/* delete all (FIXME) */
617				for ( i = 0; i < bdb->bi_nattrs; i++ ) {
618					bdb->bi_attrs[i]->ai_indexmask |= BDB_INDEX_DELETING;
619				}
620				bdb->bi_flags |= BDB_DEL_INDEX;
621				c->cleanup = bdb_cf_cleanup;
622
623			} else {
624				struct berval bv, def = BER_BVC("default");
625				char *ptr;
626
627				for (ptr = c->line; !isspace( (unsigned char) *ptr ); ptr++);
628
629				bv.bv_val = c->line;
630				bv.bv_len = ptr - bv.bv_val;
631				if ( bvmatch( &bv, &def )) {
632					bdb->bi_defaultmask = 0;
633
634				} else {
635					int i;
636					char **attrs;
637					char sep;
638
639					sep = bv.bv_val[ bv.bv_len ];
640					bv.bv_val[ bv.bv_len ] = '\0';
641					attrs = ldap_str2charray( bv.bv_val, "," );
642
643					for ( i = 0; attrs[ i ]; i++ ) {
644						AttributeDescription *ad = NULL;
645						const char *text;
646						AttrInfo *ai;
647
648						slap_str2ad( attrs[ i ], &ad, &text );
649						/* if we got here... */
650						assert( ad != NULL );
651
652						ai = bdb_attr_mask( bdb, ad );
653						/* if we got here... */
654						assert( ai != NULL );
655
656						ai->ai_indexmask |= BDB_INDEX_DELETING;
657						bdb->bi_flags |= BDB_DEL_INDEX;
658						c->cleanup = bdb_cf_cleanup;
659					}
660
661					bv.bv_val[ bv.bv_len ] = sep;
662					ldap_charray_free( attrs );
663				}
664			}
665			break;
666		/* doesn't make sense on the fly; the DB file must be
667		 * recreated
668		 */
669		case BDB_PGSIZE: {
670				struct bdb_db_pgsize *ps, **prev;
671				int i;
672
673				for ( i = 0, prev = &bdb->bi_pagesizes, ps = *prev; ps;
674					prev = &ps->bdp_next, ps = ps->bdp_next, i++ ) {
675					if ( c->valx == -1 || i == c->valx ) {
676						*prev = ps->bdp_next;
677						ch_free( ps );
678						ps = *prev;
679						if ( i == c->valx ) break;
680					}
681				}
682			}
683			break;
684		}
685		return rc;
686	}
687
688	switch( c->type ) {
689	case BDB_MODE:
690		if ( ASCII_DIGIT( c->argv[1][0] ) ) {
691			long mode;
692			char *next;
693			errno = 0;
694			mode = strtol( c->argv[1], &next, 0 );
695			if ( errno != 0 || next == c->argv[1] || next[0] != '\0' ) {
696				fprintf( stderr, "%s: "
697					"unable to parse mode=\"%s\".\n",
698					c->log, c->argv[1] );
699				return 1;
700			}
701			bdb->bi_dbenv_mode = mode;
702
703		} else {
704			char *m = c->argv[1];
705			int who, what, mode = 0;
706
707			if ( strlen( m ) != STRLENOF("-rwxrwxrwx") ) {
708				return 1;
709			}
710
711			if ( m[0] != '-' ) {
712				return 1;
713			}
714
715			m++;
716			for ( who = 0; who < 3; who++ ) {
717				for ( what = 0; what < 3; what++, m++ ) {
718					if ( m[0] == '-' ) {
719						continue;
720					} else if ( m[0] != "rwx"[what] ) {
721						return 1;
722					}
723					mode += ((1 << (2 - what)) << 3*(2 - who));
724				}
725			}
726			bdb->bi_dbenv_mode = mode;
727		}
728		break;
729	case BDB_CHKPT: {
730		long	l;
731		bdb->bi_txn_cp = 1;
732		if ( lutil_atolx( &l, c->argv[1], 0 ) != 0 ) {
733			fprintf( stderr, "%s: "
734				"invalid kbyte \"%s\" in \"checkpoint\".\n",
735				c->log, c->argv[1] );
736			return 1;
737		}
738		bdb->bi_txn_cp_kbyte = l;
739		if ( lutil_atolx( &l, c->argv[2], 0 ) != 0 ) {
740			fprintf( stderr, "%s: "
741				"invalid minutes \"%s\" in \"checkpoint\".\n",
742				c->log, c->argv[2] );
743			return 1;
744		}
745		bdb->bi_txn_cp_min = l;
746		/* If we're in server mode and time-based checkpointing is enabled,
747		 * submit a task to perform periodic checkpoints.
748		 */
749		if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
750			struct re_s *re = bdb->bi_txn_cp_task;
751			if ( re ) {
752				re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
753			} else {
754				if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
755					fprintf( stderr, "%s: "
756						"\"checkpoint\" must occur after \"suffix\".\n",
757						c->log );
758					return 1;
759				}
760				ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
761				bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
762					bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
763					LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
764				ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
765			}
766		}
767		} break;
768
769	case BDB_CONFIG: {
770		char *ptr = c->line;
771		struct berval bv;
772
773		if ( c->op == SLAP_CONFIG_ADD ) {
774			ptr += STRLENOF("dbconfig");
775			while (!isspace((unsigned char)*ptr)) ptr++;
776			while (isspace((unsigned char)*ptr)) ptr++;
777		}
778
779		if ( bdb->bi_flags & BDB_IS_OPEN ) {
780			bdb->bi_flags |= BDB_UPD_CONFIG;
781			c->cleanup = bdb_cf_cleanup;
782		} else {
783		/* If we're just starting up...
784		 */
785			FILE *f;
786			/* If a DB_CONFIG file exists, or we don't know the path
787			 * to the DB_CONFIG file, ignore these directives
788			 */
789			if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
790				break;
791			f = fopen( bdb->bi_db_config_path, "a" );
792			if ( f ) {
793				/* FIXME: EBCDIC probably needs special handling */
794				fprintf( f, "%s\n", ptr );
795				fclose( f );
796			}
797		}
798		ber_str2bv( ptr, 0, 1, &bv );
799		ber_bvarray_add( &bdb->bi_db_config, &bv );
800		}
801		break;
802
803	case BDB_CRYPTFILE:
804		rc = lutil_get_filed_password( c->value_string, &bdb->bi_db_crypt_key );
805		if ( rc == 0 ) {
806			bdb->bi_db_crypt_file = c->value_string;
807		}
808		break;
809
810	/* Cannot set key if file was already set */
811	case BDB_CRYPTKEY:
812		if ( bdb->bi_db_crypt_file ) {
813			rc = 1;
814		} else {
815			bdb->bi_db_crypt_key = c->value_bv;
816		}
817		break;
818
819	case BDB_DIRECTORY: {
820		FILE *f;
821		char *ptr, *testpath;
822		int len;
823
824		len = strlen( c->value_string );
825		testpath = ch_malloc( len + STRLENOF(LDAP_DIRSEP) + STRLENOF("DUMMY") + 1 );
826		ptr = lutil_strcopy( testpath, c->value_string );
827		*ptr++ = LDAP_DIRSEP[0];
828		strcpy( ptr, "DUMMY" );
829		f = fopen( testpath, "w" );
830		if ( f ) {
831			fclose( f );
832			unlink( testpath );
833		}
834		ch_free( testpath );
835		if ( !f ) {
836			snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: invalid path: %s",
837				c->log, strerror( errno ));
838			Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
839			return -1;
840		}
841
842		if ( bdb->bi_dbenv_home )
843			ch_free( bdb->bi_dbenv_home );
844		bdb->bi_dbenv_home = c->value_string;
845
846		/* See if a DB_CONFIG file already exists here */
847		if ( bdb->bi_db_config_path )
848			ch_free( bdb->bi_db_config_path );
849		bdb->bi_db_config_path = ch_malloc( len +
850			STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
851		ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
852		*ptr++ = LDAP_DIRSEP[0];
853		strcpy( ptr, "DB_CONFIG" );
854
855		f = fopen( bdb->bi_db_config_path, "r" );
856		if ( f ) {
857			bdb->bi_flags |= BDB_HAS_CONFIG;
858			fclose(f);
859		}
860		}
861		break;
862
863	case BDB_NOSYNC:
864		if ( c->value_int )
865			bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
866		else
867			bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
868		if ( bdb->bi_flags & BDB_IS_OPEN ) {
869			bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
870				c->value_int );
871		}
872		break;
873
874	case BDB_CHECKSUM:
875		if ( c->value_int )
876			bdb->bi_flags |= BDB_CHKSUM;
877		else
878			bdb->bi_flags &= ~BDB_CHKSUM;
879		break;
880
881	case BDB_DISABLE_FULLFSYNC_MODE:
882                if ( c->value_int )
883                        bdb->bi_disable_fullfsync_mode = 1;
884                else
885                        bdb->bi_disable_fullfsync_mode = 0;
886                break;
887
888	case BDB_INDEX:
889		rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
890			c->argc - 1, &c->argv[1], &c->reply);
891
892		if( rc != LDAP_SUCCESS ) return 1;
893		if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
894			/* Start the task as soon as we finish here. Set a long
895			 * interval (10 hours) so that it only gets scheduled once.
896			 */
897			if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
898				fprintf( stderr, "%s: "
899					"\"index\" must occur after \"suffix\".\n",
900					c->log );
901				return 1;
902			}
903			ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
904			bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
905				bdb_online_index, c->be,
906				LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
907			ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
908		}
909		break;
910
911	case BDB_LOCKD:
912		rc = verb_to_mask( c->argv[1], bdb_lockd );
913		if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
914			fprintf( stderr, "%s: "
915				"bad policy (%s) in \"lockDetect <policy>\" line\n",
916				c->log, c->argv[1] );
917			return 1;
918		}
919		bdb->bi_lock_detect = (u_int32_t)rc;
920		break;
921
922	case BDB_SSTACK:
923		if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
924			fprintf( stderr,
925		"%s: depth %d too small, using %d\n",
926			c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
927			c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
928		}
929		bdb->bi_search_stack_depth = c->value_int;
930		break;
931
932	case BDB_PGSIZE: {
933		struct bdb_db_pgsize *ps, **prev;
934		int i, s;
935
936		s = atoi(c->argv[2]);
937		if ( s < 1 || s > 64 ) {
938			snprintf( c->cr_msg, sizeof( c->cr_msg ),
939				"%s: size must be > 0 and <= 64: %d",
940				c->log, s );
941			Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
942			return -1;
943		}
944		i = strlen(c->argv[1]);
945		ps = ch_malloc( sizeof(struct bdb_db_pgsize) + i + 1 );
946		ps->bdp_next = NULL;
947		ps->bdp_name.bv_len = i;
948		ps->bdp_name.bv_val = (char *)(ps+1);
949		strcpy( ps->bdp_name.bv_val, c->argv[1] );
950		ps->bdp_size = s * 1024;
951		for ( prev = &bdb->bi_pagesizes; *prev; prev = &(*prev)->bdp_next )
952			;
953		*prev = ps;
954		}
955		break;
956	}
957	return 0;
958}
959
960int bdb_back_init_cf( BackendInfo *bi )
961{
962	int rc;
963	bi->bi_cf_ocs = bdbocs;
964
965	rc = config_register_schema( bdbcfg, bdbocs );
966	if ( rc ) return rc;
967	return 0;
968}
969