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