]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/config.c
Happy New Year
[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-2018 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 $ olcDbChecksum $ "
167                 "olcDbConfig $ 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                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_online_index) ": txn id: %x\n",
245                         txn->id(txn), 0, 0 );
246                 if ( getnext ) {
247                         getnext = 0;
248                         BDB_ID2DISK( id, &nid );
249                         rc = bdb->bi_id2entry->bdi_db->cursor(
250                                 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
251                         if ( rc ) {
252                                 TXN_ABORT( txn );
253                                 break;
254                         }
255                         rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
256                         curs->c_close( curs );
257                         if ( rc ) {
258                                 TXN_ABORT( txn );
259                                 if ( rc == DB_NOTFOUND )
260                                         rc = 0;
261                                 if ( rc == DB_LOCK_DEADLOCK ) {
262                                         ldap_pvt_thread_yield();
263                                         continue;
264                                 }
265                                 break;
266                         }
267                         BDB_DISK2ID( &nid, &id );
268                 }
269
270                 ei = NULL;
271                 rc = bdb_cache_find_id( op, txn, id, &ei, 0, &lock );
272                 if ( rc ) {
273                         TXN_ABORT( txn );
274                         if ( rc == DB_LOCK_DEADLOCK ) {
275                                 ldap_pvt_thread_yield();
276                                 continue;
277                         }
278                         if ( rc == DB_NOTFOUND ) {
279                                 id++;
280                                 getnext = 1;
281                                 continue;
282                         }
283                         break;
284                 }
285                 if ( ei->bei_e ) {
286                         rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
287                         if ( rc ) {
288                                 TXN_ABORT( txn );
289                                 if ( rc == DB_LOCK_DEADLOCK ) {
290                                         ldap_pvt_thread_yield();
291                                         continue;
292                                 }
293                                 break;
294                         }
295                         rc = TXN_COMMIT( txn, 0 );
296                         txn = NULL;
297                 }
298                 id++;
299                 getnext = 1;
300         }
301
302         for ( i = 0; i < bdb->bi_nattrs; i++ ) {
303                 if ( bdb->bi_attrs[ i ]->ai_indexmask & BDB_INDEX_DELETING
304                         || bdb->bi_attrs[ i ]->ai_newmask == 0 )
305                 {
306                         continue;
307                 }
308                 bdb->bi_attrs[ i ]->ai_indexmask = bdb->bi_attrs[ i ]->ai_newmask;
309                 bdb->bi_attrs[ i ]->ai_newmask = 0;
310         }
311
312         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
313         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
314         bdb->bi_index_task = NULL;
315         ldap_pvt_runqueue_remove( &slapd_rq, rtask );
316         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
317
318         return NULL;
319 }
320
321 /* Cleanup loose ends after Modify completes */
322 static int
323 bdb_cf_cleanup( ConfigArgs *c )
324 {
325         struct bdb_info *bdb = c->be->be_private;
326         int rc = 0;
327         BerVarray bva;
328
329         if ( bdb->bi_flags & BDB_DEL_INDEX ) {
330                 bdb_attr_flush( bdb );
331                 bdb->bi_flags ^= BDB_DEL_INDEX;
332         }
333
334         if ( bdb->bi_flags & BDB_RE_OPEN ) {
335                 bdb->bi_flags ^= BDB_RE_OPEN;
336                 bva = bdb->bi_db_config;
337                 bdb->bi_db_config = NULL;
338                 rc = c->be->bd_info->bi_db_close( c->be, &c->reply );
339                 if ( rc == 0 ) {
340                         if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
341                                 if ( bva ) {
342                                         int i;
343                                         FILE *f = fopen( bdb->bi_db_config_path, "w" );
344                                         if ( f ) {
345                                                 bdb->bi_db_config = bva;
346                                                 bva = NULL;
347                                                 for (i=0; bdb->bi_db_config[i].bv_val; i++)
348                                                         fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
349                                                 fclose( f );
350                                         } else {
351                                                 ber_bvarray_free( bva );
352                                         }
353                                 } else {
354                                         unlink( bdb->bi_db_config_path );
355                                 }
356                                 bdb->bi_flags ^= BDB_UPD_CONFIG;
357                         }
358                         rc = c->be->bd_info->bi_db_open( c->be, &c->reply );
359                 }
360                 /* If this fails, we need to restart */
361                 if ( rc ) {
362                         slapd_shutdown = 2;
363                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
364                                 "failed to reopen database, rc=%d", rc );
365                         Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
366                                 ": %s\n", c->cr_msg, 0, 0 );
367                         rc = LDAP_OTHER;
368                 }
369         }
370         return rc;
371 }
372
373 static int
374 bdb_cf_gen( ConfigArgs *c )
375 {
376         struct bdb_info *bdb = c->be->be_private;
377         int rc;
378
379         if ( c->op == SLAP_CONFIG_EMIT ) {
380                 rc = 0;
381                 switch( c->type ) {
382                 case BDB_MODE: {
383                         char buf[64];
384                         struct berval bv;
385                         bv.bv_len = snprintf( buf, sizeof(buf), "0%o", bdb->bi_dbenv_mode );
386                         if ( bv.bv_len > 0 && bv.bv_len < sizeof(buf) ) {
387                                 bv.bv_val = buf;
388                                 value_add_one( &c->rvalue_vals, &bv );
389                         } else {
390                                 rc = 1;
391                         }
392                         } break;
393
394                 case BDB_CHKPT:
395                         if ( bdb->bi_txn_cp ) {
396                                 char buf[64];
397                                 struct berval bv;
398                                 bv.bv_len = snprintf( buf, sizeof(buf), "%ld %ld",
399                                         (long) bdb->bi_txn_cp_kbyte, (long) bdb->bi_txn_cp_min );
400                                 if ( bv.bv_len > 0 && bv.bv_len < sizeof(buf) ) {
401                                         bv.bv_val = buf;
402                                         value_add_one( &c->rvalue_vals, &bv );
403                                 } else {
404                                         rc = 1;
405                                 }
406                         } else {
407                                 rc = 1;
408                         }
409                         break;
410
411                 case BDB_CRYPTFILE:
412                         if ( bdb->bi_db_crypt_file ) {
413                                 c->value_string = ch_strdup( bdb->bi_db_crypt_file );
414                         } else {
415                                 rc = 1;
416                         }
417                         break;
418
419                 /* If a crypt file has been set, its contents are copied here.
420                  * But we don't want the key to be incorporated here.
421                  */
422                 case BDB_CRYPTKEY:
423                         if ( !bdb->bi_db_crypt_file && !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
424                                 value_add_one( &c->rvalue_vals, &bdb->bi_db_crypt_key );
425                         } else {
426                                 rc = 1;
427                         }
428                         break;
429
430                 case BDB_DIRECTORY:
431                         if ( bdb->bi_dbenv_home ) {
432                                 c->value_string = ch_strdup( bdb->bi_dbenv_home );
433                         } else {
434                                 rc = 1;
435                         }
436                         break;
437
438                 case BDB_CONFIG:
439                         if ( !( bdb->bi_flags & BDB_IS_OPEN )
440                                 && !bdb->bi_db_config )
441                         {
442                                 char    buf[SLAP_TEXT_BUFLEN];
443                                 FILE *f = fopen( bdb->bi_db_config_path, "r" );
444                                 struct berval bv;
445
446                                 if ( f ) {
447                                         bdb->bi_flags |= BDB_HAS_CONFIG;
448                                         while ( fgets( buf, sizeof(buf), f )) {
449                                                 ber_str2bv( buf, 0, 1, &bv );
450                                                 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
451                                                         bv.bv_len--;
452                                                         bv.bv_val[bv.bv_len] = '\0';
453                                                 }
454                                                 /* shouldn't need this, but ... */
455                                                 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
456                                                         bv.bv_len--;
457                                                         bv.bv_val[bv.bv_len] = '\0';
458                                                 }
459                                                 ber_bvarray_add( &bdb->bi_db_config, &bv );
460                                         }
461                                         fclose( f );
462                                 }
463                         }
464                         if ( bdb->bi_db_config ) {
465                                 int i;
466                                 struct berval bv;
467
468                                 bv.bv_val = c->log;
469                                 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
470                                         bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
471                                                 bdb->bi_db_config[i].bv_val );
472                                         value_add_one( &c->rvalue_vals, &bv );
473                                 }
474                         }
475                         if ( !c->rvalue_vals ) rc = 1;
476                         break;
477
478                 case BDB_NOSYNC:
479                         if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
480                                 c->value_int = 1;
481                         break;
482                         
483                 case BDB_CHECKSUM:
484                         if ( bdb->bi_flags & BDB_CHKSUM )
485                                 c->value_int = 1;
486                         break;
487
488                 case BDB_INDEX:
489                         bdb_attr_index_unparse( bdb, &c->rvalue_vals );
490                         if ( !c->rvalue_vals ) rc = 1;
491                         break;
492
493                 case BDB_LOCKD:
494                         rc = 1;
495                         if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
496                                 int i;
497                                 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
498                                         if ( bdb->bi_lock_detect == (u_int32_t)bdb_lockd[i].mask ) {
499                                                 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
500                                                 rc = 0;
501                                                 break;
502                                         }
503                                 }
504                         }
505                         break;
506
507                 case BDB_SSTACK:
508                         c->value_int = bdb->bi_search_stack_depth;
509                         break;
510
511                 case BDB_PGSIZE: {
512                                 struct bdb_db_pgsize *ps;
513                                 char buf[SLAP_TEXT_BUFLEN];
514                                 struct berval bv;
515                                 int rc = 1;
516
517                                 bv.bv_val = buf;
518                                 for ( ps = bdb->bi_pagesizes; ps; ps = ps->bdp_next ) {
519                                         bv.bv_len = sprintf( buf, "%s %d", ps->bdp_name.bv_val,
520                                                 ps->bdp_size / 1024 );
521                                         value_add_one( &c->rvalue_vals, &bv );
522                                         rc = 0;
523
524                                 }
525                                 break;
526                         }
527                 }
528                 return rc;
529         } else if ( c->op == LDAP_MOD_DELETE ) {
530                 rc = 0;
531                 switch( c->type ) {
532                 case BDB_MODE:
533 #if 0
534                         /* FIXME: does it make any sense to change the mode,
535                          * if we don't exec a chmod()? */
536                         bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
537                         break;
538 #endif
539
540                 /* single-valued no-ops */
541                 case BDB_LOCKD:
542                 case BDB_SSTACK:
543                         break;
544
545                 case BDB_CHKPT:
546                         if ( bdb->bi_txn_cp_task ) {
547                                 struct re_s *re = bdb->bi_txn_cp_task;
548                                 bdb->bi_txn_cp_task = NULL;
549                                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
550                                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
551                                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
552                                 ldap_pvt_runqueue_remove( &slapd_rq, re );
553                                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
554                         }
555                         bdb->bi_txn_cp = 0;
556                         break;
557                 case BDB_CONFIG:
558                         if ( c->valx < 0 ) {
559                                 ber_bvarray_free( bdb->bi_db_config );
560                                 bdb->bi_db_config = NULL;
561                         } else {
562                                 int i = c->valx;
563                                 ch_free( bdb->bi_db_config[i].bv_val );
564                                 for (; bdb->bi_db_config[i].bv_val; i++)
565                                         bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
566                         }
567                         bdb->bi_flags |= BDB_UPD_CONFIG|BDB_RE_OPEN;
568                         c->cleanup = bdb_cf_cleanup;
569                         break;
570                 /* Doesn't really make sense to change these on the fly;
571                  * the entire DB must be dumped and reloaded
572                  */
573                 case BDB_CRYPTFILE:
574                         if ( bdb->bi_db_crypt_file ) {
575                                 ch_free( bdb->bi_db_crypt_file );
576                                 bdb->bi_db_crypt_file = NULL;
577                         }
578                         /* FALLTHRU */
579                 case BDB_CRYPTKEY:
580                         if ( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
581                                 ch_free( bdb->bi_db_crypt_key.bv_val );
582                                 BER_BVZERO( &bdb->bi_db_crypt_key );
583                         }
584                         break;
585                 case BDB_DIRECTORY:
586                         bdb->bi_flags |= BDB_RE_OPEN;
587                         bdb->bi_flags ^= BDB_HAS_CONFIG;
588                         ch_free( bdb->bi_dbenv_home );
589                         bdb->bi_dbenv_home = NULL;
590                         ch_free( bdb->bi_db_config_path );
591                         bdb->bi_db_config_path = NULL;
592                         c->cleanup = bdb_cf_cleanup;
593                         ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
594                         break;
595                 case BDB_NOSYNC:
596                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
597                         break;
598                 case BDB_CHECKSUM:
599                         bdb->bi_flags &= ~BDB_CHKSUM;
600                         break;
601                 case BDB_INDEX:
602                         if ( c->valx == -1 ) {
603                                 int i;
604
605                                 /* delete all (FIXME) */
606                                 for ( i = 0; i < bdb->bi_nattrs; i++ ) {
607                                         bdb->bi_attrs[i]->ai_indexmask |= BDB_INDEX_DELETING;
608                                 }
609                                 bdb->bi_flags |= BDB_DEL_INDEX;
610                                 c->cleanup = bdb_cf_cleanup;
611
612                         } else {
613                                 struct berval bv, def = BER_BVC("default");
614                                 char *ptr;
615
616                                 for (ptr = c->line; !isspace( (unsigned char) *ptr ); ptr++);
617
618                                 bv.bv_val = c->line;
619                                 bv.bv_len = ptr - bv.bv_val;
620                                 if ( bvmatch( &bv, &def )) {
621                                         bdb->bi_defaultmask = 0;
622
623                                 } else {
624                                         int i;
625                                         char **attrs;
626                                         char sep;
627
628                                         sep = bv.bv_val[ bv.bv_len ];
629                                         bv.bv_val[ bv.bv_len ] = '\0';
630                                         attrs = ldap_str2charray( bv.bv_val, "," );
631
632                                         for ( i = 0; attrs[ i ]; i++ ) {
633                                                 AttributeDescription *ad = NULL;
634                                                 const char *text;
635                                                 AttrInfo *ai;
636
637                                                 slap_str2ad( attrs[ i ], &ad, &text );
638                                                 /* if we got here... */
639                                                 assert( ad != NULL );
640
641                                                 ai = bdb_attr_mask( bdb, ad );
642                                                 /* if we got here... */
643                                                 assert( ai != NULL );
644
645                                                 ai->ai_indexmask |= BDB_INDEX_DELETING;
646                                                 bdb->bi_flags |= BDB_DEL_INDEX;
647                                                 c->cleanup = bdb_cf_cleanup;
648                                         }
649
650                                         bv.bv_val[ bv.bv_len ] = sep;
651                                         ldap_charray_free( attrs );
652                                 }
653                         }
654                         break;
655                 /* doesn't make sense on the fly; the DB file must be
656                  * recreated
657                  */
658                 case BDB_PGSIZE: {
659                                 struct bdb_db_pgsize *ps, **prev;
660                                 int i;
661
662                                 for ( i = 0, prev = &bdb->bi_pagesizes, ps = *prev; ps;
663                                         prev = &ps->bdp_next, ps = ps->bdp_next, i++ ) {
664                                         if ( c->valx == -1 || i == c->valx ) {
665                                                 *prev = ps->bdp_next;
666                                                 ch_free( ps );
667                                                 ps = *prev;
668                                                 if ( i == c->valx ) break;
669                                         }
670                                 }
671                         }
672                         break;
673                 }
674                 return rc;
675         }
676
677         switch( c->type ) {
678         case BDB_MODE:
679                 if ( ASCII_DIGIT( c->argv[1][0] ) ) {
680                         long mode;
681                         char *next;
682                         errno = 0;
683                         mode = strtol( c->argv[1], &next, 0 );
684                         if ( errno != 0 || next == c->argv[1] || next[0] != '\0' ) {
685                                 fprintf( stderr, "%s: "
686                                         "unable to parse mode=\"%s\".\n",
687                                         c->log, c->argv[1] );
688                                 return 1;
689                         }
690                         bdb->bi_dbenv_mode = mode;
691
692                 } else {
693                         char *m = c->argv[1];
694                         int who, what, mode = 0;
695
696                         if ( strlen( m ) != STRLENOF("-rwxrwxrwx") ) {
697                                 return 1;
698                         }
699
700                         if ( m[0] != '-' ) {
701                                 return 1;
702                         }
703
704                         m++;
705                         for ( who = 0; who < 3; who++ ) {
706                                 for ( what = 0; what < 3; what++, m++ ) {
707                                         if ( m[0] == '-' ) {
708                                                 continue;
709                                         } else if ( m[0] != "rwx"[what] ) {
710                                                 return 1;
711                                         }
712                                         mode += ((1 << (2 - what)) << 3*(2 - who));
713                                 }
714                         }
715                         bdb->bi_dbenv_mode = mode;
716                 }
717                 break;
718         case BDB_CHKPT: {
719                 long    l;
720                 bdb->bi_txn_cp = 1;
721                 if ( lutil_atolx( &l, c->argv[1], 0 ) != 0 ) {
722                         fprintf( stderr, "%s: "
723                                 "invalid kbyte \"%s\" in \"checkpoint\".\n",
724                                 c->log, c->argv[1] );
725                         return 1;
726                 }
727                 bdb->bi_txn_cp_kbyte = l;
728                 if ( lutil_atolx( &l, c->argv[2], 0 ) != 0 ) {
729                         fprintf( stderr, "%s: "
730                                 "invalid minutes \"%s\" in \"checkpoint\".\n",
731                                 c->log, c->argv[2] );
732                         return 1;
733                 }
734                 bdb->bi_txn_cp_min = l;
735                 /* If we're in server mode and time-based checkpointing is enabled,
736                  * submit a task to perform periodic checkpoints.
737                  */
738                 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
739                         struct re_s *re = bdb->bi_txn_cp_task;
740                         if ( re ) {
741                                 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
742                         } else {
743                                 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
744                                         fprintf( stderr, "%s: "
745                                                 "\"checkpoint\" must occur after \"suffix\".\n",
746                                                 c->log );
747                                         return 1;
748                                 }
749                                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
750                                 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
751                                         bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
752                                         LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
753                                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
754                         }
755                 }
756                 } break;
757
758         case BDB_CONFIG: {
759                 char *ptr = c->line;
760                 struct berval bv;
761
762                 if ( c->op == SLAP_CONFIG_ADD ) {
763                         ptr += STRLENOF("dbconfig");
764                         while (!isspace((unsigned char)*ptr)) ptr++;
765                         while (isspace((unsigned char)*ptr)) ptr++;
766                 }
767
768                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
769                         bdb->bi_flags |= BDB_UPD_CONFIG|BDB_RE_OPEN;
770                         c->cleanup = bdb_cf_cleanup;
771                 } else {
772                 /* If we're just starting up...
773                  */
774                         FILE *f;
775                         /* If a DB_CONFIG file exists, or we don't know the path
776                          * to the DB_CONFIG file, ignore these directives
777                          */
778                         if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
779                                 break;
780                         f = fopen( bdb->bi_db_config_path, "a" );
781                         if ( f ) {
782                                 /* FIXME: EBCDIC probably needs special handling */
783                                 fprintf( f, "%s\n", ptr );
784                                 fclose( f );
785                         }
786                 }
787                 ber_str2bv( ptr, 0, 1, &bv );
788                 ber_bvarray_add( &bdb->bi_db_config, &bv );
789                 }
790                 break;
791
792         case BDB_CRYPTFILE:
793                 rc = lutil_get_filed_password( c->value_string, &bdb->bi_db_crypt_key );
794                 if ( rc == 0 ) {
795                         bdb->bi_db_crypt_file = c->value_string;
796                 }
797                 break;
798
799         /* Cannot set key if file was already set */
800         case BDB_CRYPTKEY:
801                 if ( bdb->bi_db_crypt_file ) {
802                         rc = 1;
803                 } else {
804                         bdb->bi_db_crypt_key = c->value_bv;
805                 }
806                 break;
807
808         case BDB_DIRECTORY: {
809                 FILE *f;
810                 char *ptr, *testpath;
811                 int len;
812
813                 len = strlen( c->value_string );
814                 testpath = ch_malloc( len + STRLENOF(LDAP_DIRSEP) + STRLENOF("DUMMY") + 1 );
815                 ptr = lutil_strcopy( testpath, c->value_string );
816                 *ptr++ = LDAP_DIRSEP[0];
817                 strcpy( ptr, "DUMMY" );
818                 f = fopen( testpath, "w" );
819                 if ( f ) {
820                         fclose( f );
821                         unlink( testpath );
822                 }
823                 ch_free( testpath );
824                 if ( !f ) {
825                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: invalid path: %s",
826                                 c->log, strerror( errno ));
827                         Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
828                         return -1;
829                 }
830
831                 if ( bdb->bi_dbenv_home )
832                         ch_free( bdb->bi_dbenv_home );
833                 bdb->bi_dbenv_home = c->value_string;
834
835                 /* See if a DB_CONFIG file already exists here */
836                 if ( bdb->bi_db_config_path )
837                         ch_free( bdb->bi_db_config_path );
838                 bdb->bi_db_config_path = ch_malloc( len +
839                         STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
840                 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
841                 *ptr++ = LDAP_DIRSEP[0];
842                 strcpy( ptr, "DB_CONFIG" );
843
844                 f = fopen( bdb->bi_db_config_path, "r" );
845                 if ( f ) {
846                         bdb->bi_flags |= BDB_HAS_CONFIG;
847                         fclose(f);
848                 }
849                 }
850                 break;
851
852         case BDB_NOSYNC:
853                 if ( c->value_int )
854                         bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
855                 else
856                         bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
857                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
858                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
859                                 c->value_int );
860                 }
861                 break;
862
863         case BDB_CHECKSUM:
864                 if ( c->value_int )
865                         bdb->bi_flags |= BDB_CHKSUM;
866                 else
867                         bdb->bi_flags &= ~BDB_CHKSUM;
868                 break;
869
870         case BDB_INDEX:
871                 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
872                         c->argc - 1, &c->argv[1], &c->reply);
873
874                 if( rc != LDAP_SUCCESS ) return 1;
875                 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
876                         /* Start the task as soon as we finish here. Set a long
877                          * interval (10 hours) so that it only gets scheduled once.
878                          */
879                         if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
880                                 fprintf( stderr, "%s: "
881                                         "\"index\" must occur after \"suffix\".\n",
882                                         c->log );
883                                 return 1;
884                         }
885                         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
886                         bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
887                                 bdb_online_index, c->be,
888                                 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
889                         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
890                 }
891                 break;
892
893         case BDB_LOCKD:
894                 rc = verb_to_mask( c->argv[1], bdb_lockd );
895                 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
896                         fprintf( stderr, "%s: "
897                                 "bad policy (%s) in \"lockDetect <policy>\" line\n",
898                                 c->log, c->argv[1] );
899                         return 1;
900                 }
901                 bdb->bi_lock_detect = (u_int32_t)rc;
902                 break;
903
904         case BDB_SSTACK:
905                 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
906                         fprintf( stderr,
907                 "%s: depth %d too small, using %d\n",
908                         c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
909                         c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
910                 }
911                 bdb->bi_search_stack_depth = c->value_int;
912                 break;
913
914         case BDB_PGSIZE: {
915                 struct bdb_db_pgsize *ps, **prev;
916                 int i, s;
917                 
918                 s = atoi(c->argv[2]);
919                 if ( s < 1 || s > 64 ) {
920                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
921                                 "%s: size must be > 0 and <= 64: %d",
922                                 c->log, s );
923                         Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
924                         return -1;
925                 }
926                 i = strlen(c->argv[1]);
927                 ps = ch_malloc( sizeof(struct bdb_db_pgsize) + i + 1 );
928                 ps->bdp_next = NULL;
929                 ps->bdp_name.bv_len = i;
930                 ps->bdp_name.bv_val = (char *)(ps+1);
931                 strcpy( ps->bdp_name.bv_val, c->argv[1] );
932                 ps->bdp_size = s * 1024;
933                 for ( prev = &bdb->bi_pagesizes; *prev; prev = &(*prev)->bdp_next )
934                         ;
935                 *prev = ps;
936                 }
937                 break;
938         }
939         return 0;
940 }
941
942 int bdb_back_init_cf( BackendInfo *bi )
943 {
944         int rc;
945         bi->bi_cf_ocs = bdbocs;
946
947         rc = config_register_schema( bdbcfg, bdbocs );
948         if ( rc ) return rc;
949         return 0;
950 }