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