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