]> git.sur5r.net Git - openldap/blob - servers/slapd/bconfig.c
ITS#4241 fix from HEAD
[openldap] / servers / slapd / bconfig.c
1 /* bconfig.c - the config backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2005-2006 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 /* ACKNOWLEDGEMENTS:
17  * This work was originally developed by Howard Chu for inclusion
18  * in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <ac/string.h>
25 #include <ac/ctype.h>
26 #include <ac/errno.h>
27 #include <sys/stat.h>
28
29 #include "slap.h"
30
31 #ifdef LDAP_SLAPI
32 #include "slapi/slapi.h"
33 #endif
34
35 #include <ldif.h>
36 #include <lutil.h>
37
38 #include "config.h"
39
40 static struct berval config_rdn = BER_BVC("cn=config");
41 static struct berval schema_rdn = BER_BVC("cn=schema");
42
43 #ifdef SLAPD_MODULES
44 typedef struct modpath_s {
45         struct modpath_s *mp_next;
46         struct berval mp_path;
47         BerVarray mp_loads;
48 } ModPaths;
49
50 static ModPaths modpaths, *modlast = &modpaths, *modcur = &modpaths;
51 #endif
52
53 typedef struct ConfigFile {
54         struct ConfigFile *c_sibs;
55         struct ConfigFile *c_kids;
56         struct berval c_file;
57         AttributeType *c_at_head, *c_at_tail;
58         ContentRule *c_cr_head, *c_cr_tail;
59         ObjectClass *c_oc_head, *c_oc_tail;
60         OidMacro *c_om_head, *c_om_tail;
61         BerVarray c_dseFiles;
62 } ConfigFile;
63
64 typedef struct {
65         ConfigFile *cb_config;
66         CfEntryInfo *cb_root;
67         BackendDB       cb_db;  /* underlying database */
68         int             cb_got_ldif;
69         int             cb_use_ldif;
70 } CfBackInfo;
71
72 /* These do nothing in slapd, they're kept only to make them
73  * editable here.
74  */
75 static char *replica_pidFile, *replica_argsFile;
76 static int replicationInterval;
77
78 static char     *passwd_salt;
79 static char     *logfileName;
80 #ifdef SLAP_AUTH_REWRITE
81 static BerVarray authz_rewrites;
82 #endif
83
84 static struct berval cfdir;
85
86 /* Private state */
87 static AttributeDescription *cfAd_backend, *cfAd_database, *cfAd_overlay,
88         *cfAd_include;
89
90 static ConfigFile *cfn;
91
92 static Avlnode *CfOcTree;
93
94 static int config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca,
95         SlapReply *rs, int *renumber );
96
97 static ConfigDriver config_fname;
98 static ConfigDriver config_cfdir;
99 static ConfigDriver config_generic;
100 static ConfigDriver config_search_base;
101 static ConfigDriver config_passwd_hash;
102 static ConfigDriver config_schema_dn;
103 static ConfigDriver config_sizelimit;
104 static ConfigDriver config_timelimit;
105 static ConfigDriver config_overlay;
106 static ConfigDriver config_subordinate; 
107 static ConfigDriver config_suffix; 
108 static ConfigDriver config_rootdn;
109 static ConfigDriver config_rootpw;
110 static ConfigDriver config_restrict;
111 static ConfigDriver config_allows;
112 static ConfigDriver config_disallows;
113 static ConfigDriver config_requires;
114 static ConfigDriver config_security;
115 static ConfigDriver config_referral;
116 static ConfigDriver config_loglevel;
117 static ConfigDriver config_replica;
118 static ConfigDriver config_updatedn;
119 static ConfigDriver config_updateref;
120 static ConfigDriver config_include;
121 #ifdef HAVE_TLS
122 static ConfigDriver config_tls_option;
123 static ConfigDriver config_tls_config;
124 #endif
125 extern ConfigDriver syncrepl_config;
126
127 enum {
128         CFG_ACL = 1,
129         CFG_BACKEND,
130         CFG_DATABASE,
131         CFG_TLS_RAND,
132         CFG_TLS_CIPHER,
133         CFG_TLS_CERT_FILE,
134         CFG_TLS_CERT_KEY,
135         CFG_TLS_CA_PATH,
136         CFG_TLS_CA_FILE,
137         CFG_TLS_DH_FILE,
138         CFG_TLS_VERIFY,
139         CFG_TLS_CRLCHECK,
140         CFG_CONCUR,
141         CFG_THREADS,
142         CFG_SALT,
143         CFG_LIMITS,
144         CFG_RO,
145         CFG_REWRITE,
146         CFG_DEPTH,
147         CFG_OID,
148         CFG_OC,
149         CFG_DIT,
150         CFG_ATTR,
151         CFG_ATOPT,
152         CFG_REPLOG,
153         CFG_ROOTDSE,
154         CFG_LOGFILE,
155         CFG_PLUGIN,
156         CFG_MODLOAD,
157         CFG_MODPATH,
158         CFG_LASTMOD,
159         CFG_AZPOLICY,
160         CFG_AZREGEXP,
161         CFG_SASLSECP,
162         CFG_SSTR_IF_MAX,
163         CFG_SSTR_IF_MIN,
164         CFG_TTHREADS,
165
166         CFG_LAST
167 };
168
169 typedef struct {
170         char *name, *oid;
171 } OidRec;
172
173 static OidRec OidMacros[] = {
174         /* OpenLDAProot:666.11.1 */
175         { "OLcfg", "1.3.6.1.4.1.4203.666.11.1" },
176         { "OLcfgAt", "OLcfg:3" },
177         { "OLcfgGlAt", "OLcfgAt:0" },
178         { "OLcfgBkAt", "OLcfgAt:1" },
179         { "OLcfgDbAt", "OLcfgAt:2" },
180         { "OLcfgOvAt", "OLcfgAt:3" },
181         { "OLcfgOc", "OLcfg:4" },
182         { "OLcfgGlOc", "OLcfgOc:0" },
183         { "OLcfgBkOc", "OLcfgOc:1" },
184         { "OLcfgDbOc", "OLcfgOc:2" },
185         { "OLcfgOvOc", "OLcfgOc:3" },
186         { "OMsyn", "1.3.6.1.4.1.1466.115.121.1" },
187         { "OMsInteger", "OMsyn:27" },
188         { "OMsBoolean", "OMsyn:7" },
189         { "OMsDN", "OMsyn:12" },
190         { "OMsDirectoryString", "OMsyn:15" },
191         { "OMsOctetString", "OMsyn:40" },
192         { NULL, NULL }
193 };
194
195 /*
196  * Backend/Database registry
197  *
198  * OLcfg{Bk|Db}{Oc|At}:0                -> common
199  * OLcfg{Bk|Db}{Oc|At}:1                -> bdb
200  * OLcfg{Bk|Db}{Oc|At}:2                -> ldif
201  * OLcfg{Bk|Db}{Oc|At}:3                -> ldap?
202  */
203
204 /*
205  * Overlay registry
206  *
207  * OLcfgOv{Oc|At}:1                     -> syncprov
208  * OLcfgOv{Oc|At}:2                     -> pcache
209  * OLcfgOv{Oc|At}:3                     -> chain
210  * OLcfgOv{Oc|At}:4                     -> accesslog
211  * OLcfgOv{Oc|At}:5                     -> valsort
212  * OLcfgOv{Oc|At}:6                     -> smbk5pwd (use a separate arc for contrib?)
213  */
214
215 /* alphabetical ordering */
216
217 static ConfigTable config_back_cf_table[] = {
218         /* This attr is read-only */
219         { "", "", 0, 0, 0, ARG_MAGIC,
220                 &config_fname, "( OLcfgGlAt:78 NAME 'olcConfigFile' "
221                         "DESC 'File for slapd configuration directives' "
222                         "EQUALITY caseIgnoreMatch "
223                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
224         { "", "", 0, 0, 0, ARG_MAGIC,
225                 &config_cfdir, "( OLcfgGlAt:79 NAME 'olcConfigDir' "
226                         "DESC 'Directory for slapd configuration backend' "
227                         "EQUALITY caseIgnoreMatch "
228                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
229         { "access",     NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_ACL,
230                 &config_generic, "( OLcfgGlAt:1 NAME 'olcAccess' "
231                         "DESC 'Access Control List' "
232                         "EQUALITY caseIgnoreMatch "
233                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
234         { "allows",     "features", 2, 0, 5, ARG_PRE_DB|ARG_MAGIC,
235                 &config_allows, "( OLcfgGlAt:2 NAME 'olcAllows' "
236                         "DESC 'Allowed set of deprecated features' "
237                         "EQUALITY caseIgnoreMatch "
238                         "SYNTAX OMsDirectoryString )", NULL, NULL },
239         { "argsfile", "file", 2, 2, 0, ARG_STRING,
240                 &slapd_args_file, "( OLcfgGlAt:3 NAME 'olcArgsFile' "
241                         "DESC 'File for slapd command line options' "
242                         "EQUALITY caseIgnoreMatch "
243                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
244         { "attributeoptions", NULL, 0, 0, 0, ARG_MAGIC|CFG_ATOPT,
245                 &config_generic, "( OLcfgGlAt:5 NAME 'olcAttributeOptions' "
246                         "EQUALITY caseIgnoreMatch "
247                         "SYNTAX OMsDirectoryString )", NULL, NULL },
248         { "attribute",  "attribute", 2, 0, 9,
249                 ARG_PAREN|ARG_MAGIC|CFG_ATTR|ARG_NO_DELETE|ARG_NO_INSERT,
250                 &config_generic, "( OLcfgGlAt:4 NAME 'olcAttributeTypes' "
251                         "DESC 'OpenLDAP attributeTypes' "
252                         "EQUALITY caseIgnoreMatch "
253                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
254                                 NULL, NULL },
255         { "authid-rewrite", NULL, 2, 0, STRLENOF( "authid-rewrite" ),
256 #ifdef SLAP_AUTH_REWRITE
257                 ARG_MAGIC|CFG_REWRITE|ARG_NO_INSERT, &config_generic,
258 #else
259                 ARG_IGNORED, NULL,
260 #endif
261                  "( OLcfgGlAt:6 NAME 'olcAuthIDRewrite' "
262                         "EQUALITY caseIgnoreMatch "
263                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
264         { "authz-policy", "policy", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_AZPOLICY,
265                 &config_generic, "( OLcfgGlAt:7 NAME 'olcAuthzPolicy' "
266                         "EQUALITY caseIgnoreMatch "
267                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
268         { "authz-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP|ARG_NO_INSERT,
269                 &config_generic, "( OLcfgGlAt:8 NAME 'olcAuthzRegexp' "
270                         "EQUALITY caseIgnoreMatch "
271                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
272         { "backend", "type", 2, 2, 0, ARG_PRE_DB|ARG_MAGIC|CFG_BACKEND,
273                 &config_generic, "( OLcfgGlAt:9 NAME 'olcBackend' "
274                         "DESC 'A type of backend' "
275                         "EQUALITY caseIgnoreMatch "
276                         "SYNTAX OMsDirectoryString SINGLE-VALUE X-ORDERED 'SIBLINGS' )",
277                                 NULL, NULL },
278         { "concurrency", "level", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_CONCUR,
279                 &config_generic, "( OLcfgGlAt:10 NAME 'olcConcurrency' "
280                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
281         { "conn_max_pending", "max", 2, 2, 0, ARG_INT,
282                 &slap_conn_max_pending, "( OLcfgGlAt:11 NAME 'olcConnMaxPending' "
283                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
284         { "conn_max_pending_auth", "max", 2, 2, 0, ARG_INT,
285                 &slap_conn_max_pending_auth, "( OLcfgGlAt:12 NAME 'olcConnMaxPendingAuth' "
286                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
287         { "database", "type", 2, 2, 0, ARG_MAGIC|CFG_DATABASE,
288                 &config_generic, "( OLcfgGlAt:13 NAME 'olcDatabase' "
289                         "DESC 'The backend type for a database instance' "
290                         "SUP olcBackend SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
291         { "defaultSearchBase", "dn", 2, 2, 0, ARG_PRE_BI|ARG_PRE_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
292                 &config_search_base, "( OLcfgGlAt:14 NAME 'olcDefaultSearchBase' "
293                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
294         { "disallows", "features", 2, 0, 8, ARG_PRE_DB|ARG_MAGIC,
295                 &config_disallows, "( OLcfgGlAt:15 NAME 'olcDisallows' "
296                         "EQUALITY caseIgnoreMatch "
297                         "SYNTAX OMsDirectoryString )", NULL, NULL },
298         { "ditcontentrule",     NULL, 0, 0, 0, ARG_MAGIC|CFG_DIT|ARG_NO_DELETE|ARG_NO_INSERT,
299                 &config_generic, "( OLcfgGlAt:16 NAME 'olcDitContentRules' "
300                         "DESC 'OpenLDAP DIT content rules' "
301                         "EQUALITY caseIgnoreMatch "
302                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
303                         NULL, NULL },
304         { "gentlehup", "on|off", 2, 2, 0,
305 #ifdef SIGHUP
306                 ARG_ON_OFF, &global_gentlehup,
307 #else
308                 ARG_IGNORED, NULL,
309 #endif
310                 "( OLcfgGlAt:17 NAME 'olcGentleHUP' "
311                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
312         { "idletimeout", "timeout", 2, 2, 0, ARG_INT,
313                 &global_idletimeout, "( OLcfgGlAt:18 NAME 'olcIdleTimeout' "
314                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
315         { "include", "file", 2, 2, 0, ARG_MAGIC,
316                 &config_include, "( OLcfgGlAt:19 NAME 'olcInclude' "
317                         "SUP labeledURI )", NULL, NULL },
318         { "index_substr_if_minlen", "min", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MIN,
319                 &config_generic, "( OLcfgGlAt:20 NAME 'olcIndexSubstrIfMinLen' "
320                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
321         { "index_substr_if_maxlen", "max", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MAX,
322                 &config_generic, "( OLcfgGlAt:21 NAME 'olcIndexSubstrIfMaxLen' "
323                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
324         { "index_substr_any_len", "len", 2, 2, 0, ARG_INT|ARG_NONZERO,
325                 &index_substr_any_len, "( OLcfgGlAt:22 NAME 'olcIndexSubstrAnyLen' "
326                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
327         { "index_substr_any_step", "step", 2, 2, 0, ARG_INT|ARG_NONZERO,
328                 &index_substr_any_step, "( OLcfgGlAt:23 NAME 'olcIndexSubstrAnyStep' "
329                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
330         { "lastmod", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_LASTMOD,
331                 &config_generic, "( OLcfgDbAt:0.4 NAME 'olcLastMod' "
332                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
333         { "limits", "limits", 2, 0, 0, ARG_DB|ARG_MAGIC|CFG_LIMITS,
334                 &config_generic, "( OLcfgDbAt:0.5 NAME 'olcLimits' "
335                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
336         { "localSSF", "ssf", 2, 2, 0, ARG_INT,
337                 &local_ssf, "( OLcfgGlAt:26 NAME 'olcLocalSSF' "
338                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
339         { "logfile", "file", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_LOGFILE,
340                 &config_generic, "( OLcfgGlAt:27 NAME 'olcLogFile' "
341                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
342         { "loglevel", "level", 2, 0, 0, ARG_MAGIC,
343                 &config_loglevel, "( OLcfgGlAt:28 NAME 'olcLogLevel' "
344                         "SYNTAX OMsDirectoryString )", NULL, NULL },
345         { "maxDerefDepth", "depth", 2, 2, 0, ARG_DB|ARG_INT|ARG_MAGIC|CFG_DEPTH,
346                 &config_generic, "( OLcfgDbAt:0.6 NAME 'olcMaxDerefDepth' "
347                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
348         { "moduleload", "file", 2, 0, 0,
349 #ifdef SLAPD_MODULES
350                 ARG_MAGIC|CFG_MODLOAD, &config_generic,
351 #else
352                 ARG_IGNORED, NULL,
353 #endif
354                 "( OLcfgGlAt:30 NAME 'olcModuleLoad' "
355                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
356         { "modulepath", "path", 2, 2, 0,
357 #ifdef SLAPD_MODULES
358                 ARG_MAGIC|CFG_MODPATH|ARG_NO_DELETE|ARG_NO_INSERT, &config_generic,
359 #else
360                 ARG_IGNORED, NULL,
361 #endif
362                 "( OLcfgGlAt:31 NAME 'olcModulePath' "
363                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
364         { "objectclass", "objectclass", 2, 0, 0, ARG_PAREN|ARG_MAGIC|CFG_OC|ARG_NO_DELETE|ARG_NO_INSERT,
365                 &config_generic, "( OLcfgGlAt:32 NAME 'olcObjectClasses' "
366                 "DESC 'OpenLDAP object classes' "
367                 "EQUALITY caseIgnoreMatch "
368                 "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
369                         NULL, NULL },
370         { "objectidentifier", NULL,     0, 0, 0, ARG_MAGIC|CFG_OID,
371                 &config_generic, "( OLcfgGlAt:33 NAME 'olcObjectIdentifier' "
372                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
373         { "overlay", "overlay", 2, 2, 0, ARG_MAGIC,
374                 &config_overlay, "( OLcfgGlAt:34 NAME 'olcOverlay' "
375                         "SUP olcDatabase SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
376         { "password-crypt-salt-format", "salt", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_SALT,
377                 &config_generic, "( OLcfgGlAt:35 NAME 'olcPasswordCryptSaltFormat' "
378                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
379         { "password-hash", "hash", 2, 2, 0, ARG_MAGIC,
380                 &config_passwd_hash, "( OLcfgGlAt:36 NAME 'olcPasswordHash' "
381                         "SYNTAX OMsDirectoryString )", NULL, NULL },
382         { "pidfile", "file", 2, 2, 0, ARG_STRING,
383                 &slapd_pid_file, "( OLcfgGlAt:37 NAME 'olcPidFile' "
384                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
385         { "plugin", NULL, 0, 0, 0,
386 #ifdef LDAP_SLAPI
387                 ARG_MAGIC|CFG_PLUGIN, &config_generic,
388 #else
389                 ARG_IGNORED, NULL,
390 #endif
391                 "( OLcfgGlAt:38 NAME 'olcPlugin' "
392                         "SYNTAX OMsDirectoryString )", NULL, NULL },
393         { "pluginlog", "filename", 2, 2, 0,
394 #ifdef LDAP_SLAPI
395                 ARG_STRING, &slapi_log_file,
396 #else
397                 ARG_IGNORED, NULL,
398 #endif
399                 "( OLcfgGlAt:39 NAME 'olcPluginLogFile' "
400                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
401         { "readonly", "on|off", 2, 2, 0, ARG_MAY_DB|ARG_ON_OFF|ARG_MAGIC|CFG_RO,
402                 &config_generic, "( OLcfgGlAt:40 NAME 'olcReadOnly' "
403                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
404         { "referral", "url", 2, 2, 0, ARG_MAGIC,
405                 &config_referral, "( OLcfgGlAt:41 NAME 'olcReferral' "
406                         "SUP labeledURI SINGLE-VALUE )", NULL, NULL },
407         { "replica", "host or uri", 2, 0, 0, ARG_DB|ARG_MAGIC,
408                 &config_replica, "( OLcfgDbAt:0.7 NAME 'olcReplica' "
409                         "SUP labeledURI X-ORDERED 'VALUES' )", NULL, NULL },
410         { "replica-argsfile", NULL, 0, 0, 0, ARG_STRING,
411                 &replica_argsFile, "( OLcfgGlAt:43 NAME 'olcReplicaArgsFile' "
412                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
413         { "replica-pidfile", NULL, 0, 0, 0, ARG_STRING,
414                 &replica_pidFile, "( OLcfgGlAt:44 NAME 'olcReplicaPidFile' "
415                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
416         { "replicationInterval", NULL, 0, 0, 0, ARG_INT,
417                 &replicationInterval, "( OLcfgGlAt:45 NAME 'olcReplicationInterval' "
418                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
419         { "replogfile", "filename", 2, 2, 0, ARG_MAY_DB|ARG_MAGIC|ARG_STRING|CFG_REPLOG,
420                 &config_generic, "( OLcfgGlAt:46 NAME 'olcReplogFile' "
421                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
422         { "require", "features", 2, 0, 7, ARG_MAY_DB|ARG_MAGIC,
423                 &config_requires, "( OLcfgGlAt:47 NAME 'olcRequires' "
424                         "SYNTAX OMsDirectoryString )", NULL, NULL },
425         { "restrict", "op_list", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
426                 &config_restrict, "( OLcfgGlAt:48 NAME 'olcRestrict' "
427                         "SYNTAX OMsDirectoryString )", NULL, NULL },
428         { "reverse-lookup", "on|off", 2, 2, 0,
429 #ifdef SLAPD_RLOOKUPS
430                 ARG_ON_OFF, &use_reverse_lookup,
431 #else
432                 ARG_IGNORED, NULL,
433 #endif
434                 "( OLcfgGlAt:49 NAME 'olcReverseLookup' "
435                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
436         { "rootdn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
437                 &config_rootdn, "( OLcfgDbAt:0.8 NAME 'olcRootDN' "
438                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
439         { "rootDSE", "file", 2, 2, 0, ARG_MAGIC|CFG_ROOTDSE,
440                 &config_generic, "( OLcfgGlAt:51 NAME 'olcRootDSE' "
441                         "SYNTAX OMsDirectoryString )", NULL, NULL },
442         { "rootpw", "password", 2, 2, 0, ARG_BERVAL|ARG_DB|ARG_MAGIC,
443                 &config_rootpw, "( OLcfgDbAt:0.9 NAME 'olcRootPW' "
444                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
445         { "sasl-authz-policy", NULL, 2, 2, 0, ARG_MAGIC|CFG_AZPOLICY,
446                 &config_generic, NULL, NULL, NULL },
447         { "sasl-host", "host", 2, 2, 0,
448 #ifdef HAVE_CYRUS_SASL
449                 ARG_STRING|ARG_UNIQUE, &global_host,
450 #else
451                 ARG_IGNORED, NULL,
452 #endif
453                 "( OLcfgGlAt:53 NAME 'olcSaslHost' "
454                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
455         { "sasl-realm", "realm", 2, 2, 0,
456 #ifdef HAVE_CYRUS_SASL
457                 ARG_STRING|ARG_UNIQUE, &global_realm,
458 #else
459                 ARG_IGNORED, NULL,
460 #endif
461                 "( OLcfgGlAt:54 NAME 'olcSaslRealm' "
462                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
463         { "sasl-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
464                 &config_generic, NULL, NULL, NULL },
465         { "sasl-secprops", "properties", 2, 2, 0,
466 #ifdef HAVE_CYRUS_SASL
467                 ARG_MAGIC|CFG_SASLSECP, &config_generic,
468 #else
469                 ARG_IGNORED, NULL,
470 #endif
471                 "( OLcfgGlAt:56 NAME 'olcSaslSecProps' "
472                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
473         { "saslRegexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
474                 &config_generic, NULL, NULL, NULL },
475         { "schemadn", "dn", 2, 2, 0, ARG_MAY_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
476                 &config_schema_dn, "( OLcfgGlAt:58 NAME 'olcSchemaDN' "
477                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
478         { "security", "factors", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
479                 &config_security, "( OLcfgGlAt:59 NAME 'olcSecurity' "
480                         "SYNTAX OMsDirectoryString )", NULL, NULL },
481         { "sizelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
482                 &config_sizelimit, "( OLcfgGlAt:60 NAME 'olcSizeLimit' "
483                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
484         { "sockbuf_max_incoming", "max", 2, 2, 0, ARG_BER_LEN_T,
485                 &sockbuf_max_incoming, "( OLcfgGlAt:61 NAME 'olcSockbufMaxIncoming' "
486                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
487         { "sockbuf_max_incoming_auth", "max", 2, 2, 0, ARG_BER_LEN_T,
488                 &sockbuf_max_incoming_auth, "( OLcfgGlAt:62 NAME 'olcSockbufMaxIncomingAuth' "
489                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
490         { "srvtab", "file", 2, 2, 0,
491 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
492                 ARG_STRING, &ldap_srvtab,
493 #else
494                 ARG_IGNORED, NULL,
495 #endif
496                 "( OLcfgGlAt:63 NAME 'olcSrvtab' "
497                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
498         { "subordinate", "[advertise]", 1, 2, 0, ARG_DB|ARG_MAGIC,
499                 &config_subordinate, "( OLcfgDbAt:0.15 NAME 'olcSubordinate' "
500                         "SYNTAX OMsDirectoryString )", NULL, NULL },
501         { "suffix",     "suffix", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
502                 &config_suffix, "( OLcfgDbAt:0.10 NAME 'olcSuffix' "
503                         "SYNTAX OMsDN )", NULL, NULL },
504         { "syncrepl", NULL, 0, 0, 0, ARG_DB|ARG_MAGIC,
505                 &syncrepl_config, "( OLcfgDbAt:0.11 NAME 'olcSyncrepl' "
506                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
507         { "threads", "count", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_THREADS,
508                 &config_generic, "( OLcfgGlAt:66 NAME 'olcThreads' "
509                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
510         { "timelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
511                 &config_timelimit, "( OLcfgGlAt:67 NAME 'olcTimeLimit' "
512                         "SYNTAX OMsDirectoryString )", NULL, NULL },
513         { "TLSCACertificateFile", NULL, 0, 0, 0,
514 #ifdef HAVE_TLS
515                 CFG_TLS_CA_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
516 #else
517                 ARG_IGNORED, NULL,
518 #endif
519                 "( OLcfgGlAt:68 NAME 'olcTLSCACertificateFile' "
520                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
521         { "TLSCACertificatePath", NULL, 0, 0, 0,
522 #ifdef HAVE_TLS
523                 CFG_TLS_CA_PATH|ARG_STRING|ARG_MAGIC, &config_tls_option,
524 #else
525                 ARG_IGNORED, NULL,
526 #endif
527                 "( OLcfgGlAt:69 NAME 'olcTLSCACertificatePath' "
528                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
529         { "TLSCertificateFile", NULL, 0, 0, 0,
530 #ifdef HAVE_TLS
531                 CFG_TLS_CERT_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
532 #else
533                 ARG_IGNORED, NULL,
534 #endif
535                 "( OLcfgGlAt:70 NAME 'olcTLSCertificateFile' "
536                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
537         { "TLSCertificateKeyFile", NULL, 0, 0, 0,
538 #ifdef HAVE_TLS
539                 CFG_TLS_CERT_KEY|ARG_STRING|ARG_MAGIC, &config_tls_option,
540 #else
541                 ARG_IGNORED, NULL,
542 #endif
543                 "( OLcfgGlAt:71 NAME 'olcTLSCertificateKeyFile' "
544                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
545         { "TLSCipherSuite",     NULL, 0, 0, 0,
546 #ifdef HAVE_TLS
547                 CFG_TLS_CIPHER|ARG_STRING|ARG_MAGIC, &config_tls_option,
548 #else
549                 ARG_IGNORED, NULL,
550 #endif
551                 "( OLcfgGlAt:72 NAME 'olcTLSCipherSuite' "
552                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
553         { "TLSCRLCheck", NULL, 0, 0, 0,
554 #if defined(HAVE_TLS) && defined(HAVE_OPENSSL_CRL)
555                 CFG_TLS_CRLCHECK|ARG_STRING|ARG_MAGIC, &config_tls_config,
556 #else
557                 ARG_IGNORED, NULL,
558 #endif
559                 "( OLcfgGlAt:73 NAME 'olcTLSCRLCheck' "
560                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
561         { "TLSRandFile", NULL, 0, 0, 0,
562 #ifdef HAVE_TLS
563                 CFG_TLS_RAND|ARG_STRING|ARG_MAGIC, &config_tls_option,
564 #else
565                 ARG_IGNORED, NULL,
566 #endif
567                 "( OLcfgGlAt:74 NAME 'olcTLSRandFile' "
568                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
569         { "TLSVerifyClient", NULL, 0, 0, 0,
570 #ifdef HAVE_TLS
571                 CFG_TLS_VERIFY|ARG_STRING|ARG_MAGIC, &config_tls_config,
572 #else
573                 ARG_IGNORED, NULL,
574 #endif
575                 "( OLcfgGlAt:75 NAME 'olcTLSVerifyClient' "
576                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
577         { "TLSDHParamFile", NULL, 0, 0, 0,
578 #ifdef HAVE_TLS
579                 CFG_TLS_DH_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
580 #else
581                 ARG_IGNORED, NULL,
582 #endif
583                 "( OLcfgGlAt:77 NAME 'olcTLSDHParamFile' "
584                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
585         { "tool-threads", "count", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_TTHREADS,
586                 &config_generic, "( OLcfgGlAt:80 NAME 'olcToolThreads' "
587                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
588         { "ucdata-path", "path", 2, 2, 0, ARG_IGNORED,
589                 NULL, NULL, NULL, NULL },
590         { "updatedn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
591                 &config_updatedn, "( OLcfgDbAt:0.12 NAME 'olcUpdateDN' "
592                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
593         { "updateref", "url", 2, 2, 0, ARG_DB|ARG_MAGIC,
594                 &config_updateref, "( OLcfgDbAt:0.13 NAME 'olcUpdateRef' "
595                         "SUP labeledURI )", NULL, NULL },
596         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
597                 NULL, NULL, NULL, NULL }
598 };
599
600 /* Routines to check if a child can be added to this type */
601 static ConfigLDAPadd cfAddSchema, cfAddInclude, cfAddDatabase,
602         cfAddBackend, cfAddModule, cfAddOverlay;
603
604 /* NOTE: be careful when defining array members
605  * that can be conditionally compiled */
606 #define CFOC_GLOBAL     cf_ocs[1]
607 #define CFOC_SCHEMA     cf_ocs[2]
608 #define CFOC_BACKEND    cf_ocs[3]
609 #define CFOC_DATABASE   cf_ocs[4]
610 #define CFOC_OVERLAY    cf_ocs[5]
611 #define CFOC_INCLUDE    cf_ocs[6]
612 #define CFOC_FRONTEND   cf_ocs[7]
613 #ifdef SLAPD_MODULES
614 #define CFOC_MODULE     cf_ocs[8]
615 #endif /* SLAPD_MODULES */
616
617 static ConfigOCs cf_ocs[] = {
618         { "( OLcfgGlOc:0 "
619                 "NAME 'olcConfig' "
620                 "DESC 'OpenLDAP configuration object' "
621                 "ABSTRACT SUP top )", Cft_Abstract, NULL },
622         { "( OLcfgGlOc:1 "
623                 "NAME 'olcGlobal' "
624                 "DESC 'OpenLDAP Global configuration options' "
625                 "SUP olcConfig STRUCTURAL "
626                 "MAY ( cn $ olcConfigFile $ olcConfigDir $ olcAllows $ olcArgsFile $ "
627                  "olcAttributeOptions $ olcAuthIDRewrite $ "
628                  "olcAuthzPolicy $ olcAuthzRegexp $ olcConcurrency $ "
629                  "olcConnMaxPending $ olcConnMaxPendingAuth $ "
630                  "olcDisallows $ olcGentleHUP $ olcIdleTimeout $ "
631                  "olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
632                  "olcIndexSubstrAnyLen $ olcIndexSubstrAnyStep $ olcLocalSSF $ "
633                  "olcLogLevel $ "
634                  "olcPasswordCryptSaltFormat $ olcPasswordHash $ olcPidFile $ "
635                  "olcPluginLogFile $ olcReadOnly $ olcReferral $ "
636                  "olcReplicaPidFile $ olcReplicaArgsFile $ olcReplicationInterval $ "
637                  "olcReplogFile $ olcRequires $ olcRestrict $ olcReverseLookup $ "
638                  "olcRootDSE $ "
639                  "olcSaslHost $ olcSaslRealm $ olcSaslSecProps $ "
640                  "olcSecurity $ olcSizeLimit $ "
641                  "olcSockbufMaxIncoming $ olcSockbufMaxIncomingAuth $ olcSrvtab $ "
642                  "olcThreads $ olcTimeLimit $ olcTLSCACertificateFile $ "
643                  "olcTLSCACertificatePath $ olcTLSCertificateFile $ "
644                  "olcTLSCertificateKeyFile $ olcTLSCipherSuite $ olcTLSCRLCheck $ "
645                  "olcTLSRandFile $ olcTLSVerifyClient $ olcTLSDHParamFile $ "
646                  "olcToolThreads $ "
647                  "olcObjectIdentifier $ olcAttributeTypes $ olcObjectClasses $ "
648                  "olcDitContentRules ) )", Cft_Global },
649         { "( OLcfgGlOc:2 "
650                 "NAME 'olcSchemaConfig' "
651                 "DESC 'OpenLDAP schema object' "
652                 "SUP olcConfig STRUCTURAL "
653                 "MAY ( cn $ olcObjectIdentifier $ olcAttributeTypes $ "
654                  "olcObjectClasses $ olcDitContentRules ) )",
655                         Cft_Schema, NULL, cfAddSchema },
656         { "( OLcfgGlOc:3 "
657                 "NAME 'olcBackendConfig' "
658                 "DESC 'OpenLDAP Backend-specific options' "
659                 "SUP olcConfig STRUCTURAL "
660                 "MUST olcBackend )", Cft_Backend, NULL, cfAddBackend },
661         { "( OLcfgGlOc:4 "
662                 "NAME 'olcDatabaseConfig' "
663                 "DESC 'OpenLDAP Database-specific options' "
664                 "SUP olcConfig STRUCTURAL "
665                 "MUST olcDatabase "
666                 "MAY ( olcSuffix $ olcSubordinate $ olcAccess $ olcLastMod $ olcLimits $ "
667                  "olcMaxDerefDepth $ olcPlugin $ olcReadOnly $ olcReplica $ "
668                  "olcReplogFile $ olcRequires $ olcRestrict $ olcRootDN $ olcRootPW $ "
669                  "olcSchemaDN $ olcSecurity $ olcSizeLimit $ olcSyncrepl $ "
670                  "olcTimeLimit $ olcUpdateDN $ olcUpdateRef ) )",
671                         Cft_Database, NULL, cfAddDatabase },
672         { "( OLcfgGlOc:5 "
673                 "NAME 'olcOverlayConfig' "
674                 "DESC 'OpenLDAP Overlay-specific options' "
675                 "SUP olcConfig STRUCTURAL "
676                 "MUST olcOverlay )", Cft_Overlay, NULL, cfAddOverlay },
677         { "( OLcfgGlOc:6 "
678                 "NAME 'olcIncludeFile' "
679                 "DESC 'OpenLDAP configuration include file' "
680                 "SUP olcConfig STRUCTURAL "
681                 "MUST olcInclude "
682                 "MAY ( cn $ olcRootDSE ) )",
683                 Cft_Include, NULL, cfAddInclude },
684         /* This should be STRUCTURAL like all the other database classes, but
685          * that would mean inheriting all of the olcDatabaseConfig attributes,
686          * which causes them to be merged twice in config_build_entry.
687          */
688         { "( OLcfgGlOc:7 "
689                 "NAME 'olcFrontendConfig' "
690                 "DESC 'OpenLDAP frontend configuration' "
691                 "AUXILIARY "
692                 "MAY olcDefaultSearchBase )",
693                 Cft_Database, NULL, NULL },
694 #ifdef SLAPD_MODULES
695         { "( OLcfgGlOc:8 "
696                 "NAME 'olcModuleList' "
697                 "DESC 'OpenLDAP dynamic module info' "
698                 "SUP olcConfig STRUCTURAL "
699                 "MAY ( cn $ olcModulePath $ olcModuleLoad ) )",
700                 Cft_Module, NULL, cfAddModule },
701 #endif
702         { NULL, 0, NULL }
703 };
704
705 static int
706 config_generic(ConfigArgs *c) {
707         char *p;
708         int i;
709
710         if ( c->op == SLAP_CONFIG_EMIT ) {
711                 int rc = 0;
712                 switch(c->type) {
713                 case CFG_CONCUR:
714                         c->value_int = ldap_pvt_thread_get_concurrency();
715                         break;
716                 case CFG_THREADS:
717                         c->value_int = connection_pool_max;
718                         break;
719                 case CFG_TTHREADS:
720                         c->value_int = slap_tool_thread_max;
721                         break;
722                 case CFG_SALT:
723                         if ( passwd_salt )
724                                 c->value_string = ch_strdup( passwd_salt );
725                         else
726                                 rc = 1;
727                         break;
728                 case CFG_LIMITS:
729                         if ( c->be->be_limits ) {
730                                 char buf[4096*3];
731                                 struct berval bv;
732                                 int i;
733
734                                 for ( i=0; c->be->be_limits[i]; i++ ) {
735                                         bv.bv_len = snprintf( buf, sizeof( buf ), SLAP_X_ORDERED_FMT, i );
736                                         if ( bv.bv_len >= sizeof( buf ) ) {
737                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
738                                                 c->rvalue_vals = NULL;
739                                                 rc = 1;
740                                                 break;
741                                         }
742                                         bv.bv_val = buf + bv.bv_len;
743                                         limits_unparse( c->be->be_limits[i], &bv );
744                                         bv.bv_len += bv.bv_val - buf;
745                                         bv.bv_val = buf;
746                                         value_add_one( &c->rvalue_vals, &bv );
747                                 }
748                         }
749                         if ( !c->rvalue_vals ) rc = 1;
750                         break;
751                 case CFG_RO:
752                         c->value_int = (c->be->be_restrictops & SLAP_RESTRICT_OP_WRITES) ==
753                                 SLAP_RESTRICT_OP_WRITES;
754                         break;
755                 case CFG_AZPOLICY:
756                         c->value_string = ch_strdup( slap_sasl_getpolicy());
757                         break;
758                 case CFG_AZREGEXP:
759                         slap_sasl_regexp_unparse( &c->rvalue_vals );
760                         if ( !c->rvalue_vals ) rc = 1;
761                         break;
762 #ifdef HAVE_CYRUS_SASL
763                 case CFG_SASLSECP: {
764                         struct berval bv = BER_BVNULL;
765                         slap_sasl_secprops_unparse( &bv );
766                         if ( !BER_BVISNULL( &bv )) {
767                                 ber_bvarray_add( &c->rvalue_vals, &bv );
768                         } else {
769                                 rc = 1;
770                         }
771                         }
772                         break;
773 #endif
774                 case CFG_DEPTH:
775                         c->value_int = c->be->be_max_deref_depth;
776                         break;
777                 case CFG_OID: {
778                         ConfigFile *cf = c->private;
779                         if ( !cf )
780                                 oidm_unparse( &c->rvalue_vals, NULL, NULL, 1 );
781                         else if ( cf->c_om_head )
782                                 oidm_unparse( &c->rvalue_vals, cf->c_om_head,
783                                         cf->c_om_tail, 0 );
784                         if ( !c->rvalue_vals )
785                                 rc = 1;
786                         }
787                         break;
788                 case CFG_OC: {
789                         ConfigFile *cf = c->private;
790                         if ( !cf )
791                                 oc_unparse( &c->rvalue_vals, NULL, NULL, 1 );
792                         else if ( cf->c_oc_head )
793                                 oc_unparse( &c->rvalue_vals, cf->c_oc_head,
794                                         cf->c_oc_tail, 0 );
795                         if ( !c->rvalue_vals )
796                                 rc = 1;
797                         }
798                         break;
799                 case CFG_ATTR: {
800                         ConfigFile *cf = c->private;
801                         if ( !cf )
802                                 at_unparse( &c->rvalue_vals, NULL, NULL, 1 );
803                         else if ( cf->c_at_head )
804                                 at_unparse( &c->rvalue_vals, cf->c_at_head,
805                                         cf->c_at_tail, 0 );
806                         if ( !c->rvalue_vals )
807                                 rc = 1;
808                         }
809                         break;
810                 case CFG_DIT: {
811                         ConfigFile *cf = c->private;
812                         if ( !cf )
813                                 cr_unparse( &c->rvalue_vals, NULL, NULL, 1 );
814                         else if ( cf->c_cr_head )
815                                 cr_unparse( &c->rvalue_vals, cf->c_cr_head,
816                                         cf->c_cr_tail, 0 );
817                         if ( !c->rvalue_vals )
818                                 rc = 1;
819                         }
820                         break;
821                         
822                 case CFG_ACL: {
823                         AccessControl *a;
824                         char *src, *dst, ibuf[11];
825                         struct berval bv, abv;
826                         for (i=0, a=c->be->be_acl; a; i++,a=a->acl_next) {
827                                 abv.bv_len = snprintf( ibuf, sizeof( ibuf ), SLAP_X_ORDERED_FMT, i );
828                                 if ( abv.bv_len >= sizeof( ibuf ) ) {
829                                         ber_bvarray_free_x( c->rvalue_vals, NULL );
830                                         c->rvalue_vals = NULL;
831                                         i = 0;
832                                         break;
833                                 }
834                                 acl_unparse( a, &bv );
835                                 abv.bv_val = ch_malloc( abv.bv_len + bv.bv_len + 1 );
836                                 AC_MEMCPY( abv.bv_val, ibuf, abv.bv_len );
837                                 /* Turn TAB / EOL into plain space */
838                                 for (src=bv.bv_val,dst=abv.bv_val+abv.bv_len; *src; src++) {
839                                         if (isspace(*src)) *dst++ = ' ';
840                                         else *dst++ = *src;
841                                 }
842                                 *dst = '\0';
843                                 if (dst[-1] == ' ') {
844                                         dst--;
845                                         *dst = '\0';
846                                 }
847                                 abv.bv_len = dst - abv.bv_val;
848                                 ber_bvarray_add( &c->rvalue_vals, &abv );
849                         }
850                         rc = (!i);
851                         break;
852                 }
853                 case CFG_REPLOG:
854                         if ( c->be->be_replogfile )
855                                 c->value_string = ch_strdup( c->be->be_replogfile );
856                         break;
857                 case CFG_ROOTDSE: {
858                         ConfigFile *cf = c->private;
859                         if ( cf->c_dseFiles ) {
860                                 value_add( &c->rvalue_vals, cf->c_dseFiles );
861                         } else {
862                                 rc = 1;
863                         }
864                         }
865                         break;
866                 case CFG_LOGFILE:
867                         if ( logfileName )
868                                 c->value_string = ch_strdup( logfileName );
869                         else
870                                 rc = 1;
871                         break;
872                 case CFG_LASTMOD:
873                         c->value_int = (SLAP_NOLASTMOD(c->be) == 0);
874                         break;
875                 case CFG_SSTR_IF_MAX:
876                         c->value_int = index_substr_if_maxlen;
877                         break;
878                 case CFG_SSTR_IF_MIN:
879                         c->value_int = index_substr_if_minlen;
880                         break;
881 #ifdef SLAPD_MODULES
882                 case CFG_MODLOAD: {
883                         ModPaths *mp = c->private;
884                         if (mp->mp_loads) {
885                                 int i;
886                                 for (i=0; !BER_BVISNULL(&mp->mp_loads[i]); i++) {
887                                         struct berval bv;
888                                         bv.bv_val = c->log;
889                                         bv.bv_len = snprintf( bv.bv_val, sizeof( c->log ),
890                                                 SLAP_X_ORDERED_FMT "%s", i,
891                                                 mp->mp_loads[i].bv_val );
892                                         if ( bv.bv_len >= sizeof( c->log ) ) {
893                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
894                                                 c->rvalue_vals = NULL;
895                                                 break;
896                                         }
897                                         value_add_one( &c->rvalue_vals, &bv );
898                                 }
899                         }
900
901                         rc = c->rvalue_vals ? 0 : 1;
902                         }
903                         break;
904                 case CFG_MODPATH: {
905                         ModPaths *mp = c->private;
906                         if ( !BER_BVISNULL( &mp->mp_path ))
907                                 value_add_one( &c->rvalue_vals, &mp->mp_path );
908
909                         rc = c->rvalue_vals ? 0 : 1;
910                         }
911                         break;
912 #endif
913 #ifdef LDAP_SLAPI
914                 case CFG_PLUGIN:
915                         slapi_int_plugin_unparse( c->be, &c->rvalue_vals );
916                         if ( !c->rvalue_vals ) rc = 1;
917                         break;
918 #endif
919 #ifdef SLAP_AUTH_REWRITE
920                 case CFG_REWRITE:
921                         if ( authz_rewrites ) {
922                                 struct berval bv, idx;
923                                 char ibuf[32];
924                                 int i;
925
926                                 idx.bv_val = ibuf;
927                                 for ( i=0; !BER_BVISNULL( &authz_rewrites[i] ); i++ ) {
928                                         idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), SLAP_X_ORDERED_FMT, i );
929                                         if ( idx.bv_len >= sizeof( ibuf ) ) {
930                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
931                                                 c->rvalue_vals = NULL;
932                                                 break;
933                                         }
934                                         bv.bv_len = idx.bv_len + authz_rewrites[i].bv_len;
935                                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
936                                         AC_MEMCPY( bv.bv_val, idx.bv_val, idx.bv_len );
937                                         AC_MEMCPY( &bv.bv_val[ idx.bv_len ],
938                                                 authz_rewrites[i].bv_val,
939                                                 authz_rewrites[i].bv_len + 1 );
940                                         ber_bvarray_add( &c->rvalue_vals, &bv );
941                                 }
942                         }
943                         if ( !c->rvalue_vals ) rc = 1;
944                         break;
945 #endif
946                 default:
947                         rc = 1;
948                 }
949                 return rc;
950         } else if ( c->op == LDAP_MOD_DELETE ) {
951                 int rc = 0;
952                 switch(c->type) {
953                 /* single-valued attrs, no-ops */
954                 case CFG_CONCUR:
955                 case CFG_THREADS:
956                 case CFG_TTHREADS:
957                 case CFG_RO:
958                 case CFG_AZPOLICY:
959                 case CFG_DEPTH:
960                 case CFG_LASTMOD:
961                 case CFG_SASLSECP:
962                 case CFG_SSTR_IF_MAX:
963                 case CFG_SSTR_IF_MIN:
964                         break;
965
966                 /* no-ops, requires slapd restart */
967                 case CFG_PLUGIN:
968                 case CFG_MODLOAD:
969                 case CFG_AZREGEXP:
970                 case CFG_REWRITE:
971                         snprintf(c->log, sizeof( c->log ), "change requires slapd restart");
972                         break;
973
974                 case CFG_SALT:
975                         ch_free( passwd_salt );
976                         passwd_salt = NULL;
977                         break;
978
979                 case CFG_REPLOG:
980                         ch_free( c->be->be_replogfile );
981                         c->be->be_replogfile = NULL;
982                         break;
983
984                 case CFG_LOGFILE:
985                         ch_free( logfileName );
986                         logfileName = NULL;
987                         break;
988
989                 case CFG_ACL:
990                         if ( c->valx < 0 ) {
991                                 AccessControl *end;
992                                 if ( c->be == frontendDB )
993                                         end = NULL;
994                                 else
995                                         end = frontendDB->be_acl;
996                                 acl_destroy( c->be->be_acl, end );
997                                 c->be->be_acl = end;
998
999                         } else {
1000                                 AccessControl **prev, *a;
1001                                 int i;
1002                                 for (i=0, prev = &c->be->be_acl; i < c->valx;
1003                                         i++ ) {
1004                                         a = *prev;
1005                                         prev = &a->acl_next;
1006                                 }
1007                                 a = *prev;
1008                                 *prev = a->acl_next;
1009                                 acl_free( a );
1010                         }
1011                         break;
1012
1013                 case CFG_LIMITS:
1014                         /* FIXME: there is no limits_free function */
1015                 case CFG_ATOPT:
1016                         /* FIXME: there is no ad_option_free function */
1017                 case CFG_ROOTDSE:
1018                         /* FIXME: there is no way to remove attributes added by
1019                                 a DSE file */
1020                 case CFG_OID:
1021                 case CFG_OC:
1022                 case CFG_DIT:
1023                 case CFG_ATTR:
1024                 case CFG_MODPATH:
1025                 default:
1026                         rc = 1;
1027                         break;
1028                 }
1029                 return rc;
1030         }
1031
1032         p = strchr(c->line,'(' /*')'*/);
1033
1034         switch(c->type) {
1035                 case CFG_BACKEND:
1036                         if(!(c->bi = backend_info(c->argv[1]))) {
1037                                 snprintf( c->msg, sizeof( c->msg ), "<%s> failed init", c->argv[0] );
1038                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
1039                                         c->log, c->msg, c->argv[1] );
1040                                 return(1);
1041                         }
1042                         break;
1043
1044                 case CFG_DATABASE:
1045                         c->bi = NULL;
1046                         /* NOTE: config is always the first backend!
1047                          */
1048                         if ( !strcasecmp( c->argv[1], "config" )) {
1049                                 c->be = LDAP_STAILQ_FIRST(&backendDB);
1050                         } else if ( !strcasecmp( c->argv[1], "frontend" )) {
1051                                 c->be = frontendDB;
1052                         } else {
1053                                 c->be = backend_db_init(c->argv[1], NULL);
1054                                 if ( !c->be ) {
1055                                         snprintf( c->msg, sizeof( c->msg ), "<%s> failed init", c->argv[0] );
1056                                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
1057                                                 c->log, c->msg, c->argv[1] );
1058                                         return(1);
1059                                 }
1060                         }
1061                         break;
1062
1063                 case CFG_CONCUR:
1064                         ldap_pvt_thread_set_concurrency(c->value_int);
1065                         break;
1066
1067                 case CFG_THREADS:
1068                         if ( c->value_int > 2 * SLAP_MAX_WORKER_THREADS ) {
1069                                 snprintf( c->msg, sizeof( c->msg ),
1070                                         "warning, threads=%d larger than twice the default (2*%d=%d); YMMV",
1071                                         c->value_int, SLAP_MAX_WORKER_THREADS, 2 * SLAP_MAX_WORKER_THREADS );
1072                                 Debug(LDAP_DEBUG_ANY, "%s: %s.\n",
1073                                         c->log, c->msg, 0 );
1074                         }
1075                         if ( slapMode & SLAP_SERVER_MODE )
1076                                 ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
1077                         connection_pool_max = c->value_int;     /* save for reference */
1078                         break;
1079
1080                 case CFG_TTHREADS:
1081                         if ( slapMode & SLAP_TOOL_MODE )
1082                                 ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
1083                         slap_tool_thread_max = c->value_int;    /* save for reference */
1084                         break;
1085
1086                 case CFG_SALT:
1087                         if ( passwd_salt ) ch_free( passwd_salt );
1088                         passwd_salt = c->value_string;
1089                         lutil_salt_format(passwd_salt);
1090                         break;
1091
1092                 case CFG_LIMITS:
1093                         if(limits_parse(c->be, c->fname, c->lineno, c->argc, c->argv))
1094                                 return(1);
1095                         break;
1096
1097                 case CFG_RO:
1098                         if(c->value_int)
1099                                 c->be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
1100                         else
1101                                 c->be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
1102                         break;
1103
1104                 case CFG_AZPOLICY:
1105                         ch_free(c->value_string);
1106                         if (slap_sasl_setpolicy( c->argv[1] )) {
1107                                 snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse value", c->argv[0] );
1108                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1109                                         c->log, c->msg, c->argv[1] );
1110                                 return(1);
1111                         }
1112                         break;
1113                 
1114                 case CFG_AZREGEXP:
1115                         if (slap_sasl_regexp_config( c->argv[1], c->argv[2] ))
1116                                 return(1);
1117                         break;
1118                                 
1119 #ifdef HAVE_CYRUS_SASL
1120                 case CFG_SASLSECP:
1121                         {
1122                         char *txt = slap_sasl_secprops( c->argv[1] );
1123                         if ( txt ) {
1124                                 snprintf( c->msg, sizeof(c->msg), "<%s> %s",
1125                                         c->argv[0], txt );
1126                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
1127                                 return(1);
1128                         }
1129                         break;
1130                         }
1131 #endif
1132
1133                 case CFG_DEPTH:
1134                         c->be->be_max_deref_depth = c->value_int;
1135                         break;
1136
1137                 case CFG_OID: {
1138                         OidMacro *om;
1139
1140                         if(parse_oidm(c->fname, c->lineno, c->argc, c->argv, 1, &om))
1141                                 return(1);
1142                         if (!cfn->c_om_head) cfn->c_om_head = om;
1143                         cfn->c_om_tail = om;
1144                         }
1145                         break;
1146
1147                 case CFG_OC: {
1148                         ObjectClass *oc;
1149
1150                         if(parse_oc(c->fname, c->lineno, p, c->argv, &oc)) return(1);
1151                         if (!cfn->c_oc_head) cfn->c_oc_head = oc;
1152                         cfn->c_oc_tail = oc;
1153                         }
1154                         break;
1155
1156                 case CFG_DIT: {
1157                         ContentRule *cr;
1158
1159                         if(parse_cr(c->fname, c->lineno, p, c->argv, &cr)) return(1);
1160                         if (!cfn->c_cr_head) cfn->c_cr_head = cr;
1161                         cfn->c_cr_tail = cr;
1162                         }
1163                         break;
1164
1165                 case CFG_ATTR: {
1166                         AttributeType *at;
1167
1168                         if(parse_at(c->fname, c->lineno, p, c->argv, &at)) return(1);
1169                         if (!cfn->c_at_head) cfn->c_at_head = at;
1170                         cfn->c_at_tail = at;
1171                         }
1172                         break;
1173
1174                 case CFG_ATOPT:
1175                         ad_define_option(NULL, NULL, 0);
1176                         for(i = 1; i < c->argc; i++)
1177                                 if(ad_define_option(c->argv[i], c->fname, c->lineno))
1178                                         return(1);
1179                         break;
1180
1181                 case CFG_ACL:
1182                         if ( parse_acl(c->be, c->fname, c->lineno, c->argc, c->argv, c->valx) ) {
1183                                 return 1;
1184                         }
1185                         break;
1186
1187                 case CFG_REPLOG:
1188                         if(SLAP_MONITOR(c->be)) {
1189                                 Debug(LDAP_DEBUG_ANY, "%s: "
1190                                         "\"replogfile\" should not be used "
1191                                         "inside monitor database\n",
1192                                         c->log, 0, 0);
1193                                 return(0);      /* FIXME: should this be an error? */
1194                         }
1195
1196                         c->be->be_replogfile = c->value_string;
1197                         break;
1198
1199                 case CFG_ROOTDSE:
1200                         if(read_root_dse_file(c->argv[1])) {
1201                                 snprintf( c->msg, sizeof( c->msg ), "<%s> could not read file", c->argv[0] );
1202                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1203                                         c->log, c->msg, c->argv[1] );
1204                                 return(1);
1205                         }
1206                         {
1207                                 struct berval bv;
1208                                 ber_str2bv( c->argv[1], 0, 1, &bv );
1209                                 ber_bvarray_add( &cfn->c_dseFiles, &bv );
1210                         }
1211                         break;
1212
1213                 case CFG_LOGFILE: {
1214                                 FILE *logfile;
1215                                 if ( logfileName ) ch_free( logfileName );
1216                                 logfileName = c->value_string;
1217                                 logfile = fopen(logfileName, "w");
1218                                 if(logfile) lutil_debug_file(logfile);
1219                         } break;
1220
1221                 case CFG_LASTMOD:
1222                         if(SLAP_NOLASTMODCMD(c->be)) {
1223                                 snprintf( c->msg, sizeof( c->msg ), "<%s> not available for %s database",
1224                                         c->argv[0], c->be->bd_info->bi_type );
1225                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1226                                         c->log, c->msg, 0 );
1227                                 return(1);
1228                         }
1229                         if(c->value_int)
1230                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_NOLASTMOD;
1231                         else
1232                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NOLASTMOD;
1233                         break;
1234
1235                 case CFG_SSTR_IF_MAX:
1236                         if (c->value_int < index_substr_if_minlen) {
1237                                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value", c->argv[0] );
1238                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%d)\n",
1239                                         c->log, c->msg, c->value_int );
1240                                 return(1);
1241                         }
1242                         index_substr_if_maxlen = c->value_int;
1243                         break;
1244
1245                 case CFG_SSTR_IF_MIN:
1246                         if (c->value_int > index_substr_if_maxlen) {
1247                                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value", c->argv[0] );
1248                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%d)\n",
1249                                         c->log, c->msg, c->value_int );
1250                                 return(1);
1251                         }
1252                         index_substr_if_minlen = c->value_int;
1253                         break;
1254
1255 #ifdef SLAPD_MODULES
1256                 case CFG_MODLOAD:
1257                         /* If we're just adding a module on an existing modpath,
1258                          * make sure we've selected the current path.
1259                          */
1260                         if ( c->op == LDAP_MOD_ADD && c->private && modcur != c->private ) {
1261                                 modcur = c->private;
1262                                 /* This should never fail */
1263                                 if ( module_path( modcur->mp_path.bv_val )) {
1264                                         snprintf( c->msg, sizeof( c->msg ), "<%s> module path no longer valid",
1265                                                 c->argv[0] );
1266                                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1267                                                 c->log, c->msg, modcur->mp_path.bv_val );
1268                                         return(1);
1269                                 }
1270                         }
1271                         if(module_load(c->argv[1], c->argc - 2, (c->argc > 2) ? c->argv + 2 : NULL))
1272                                 return(1);
1273                         /* Record this load on the current path */
1274                         {
1275                                 struct berval bv;
1276                                 char *ptr;
1277                                 if ( c->op == SLAP_CONFIG_ADD ) {
1278                                         ptr = c->line + STRLENOF("moduleload");
1279                                         while (!isspace(*ptr)) ptr++;
1280                                         while (isspace(*ptr)) ptr++;
1281                                 } else {
1282                                         ptr = c->line;
1283                                 }
1284                                 ber_str2bv(ptr, 0, 1, &bv);
1285                                 ber_bvarray_add( &modcur->mp_loads, &bv );
1286                         }
1287                         break;
1288
1289                 case CFG_MODPATH:
1290                         if(module_path(c->argv[1])) return(1);
1291                         /* Record which path was used with each module */
1292                         {
1293                                 ModPaths *mp;
1294
1295                                 if (!modpaths.mp_loads) {
1296                                         mp = &modpaths;
1297                                 } else {
1298                                         mp = ch_malloc( sizeof( ModPaths ));
1299                                         modlast->mp_next = mp;
1300                                 }
1301                                 ber_str2bv(c->argv[1], 0, 1, &mp->mp_path);
1302                                 mp->mp_next = NULL;
1303                                 mp->mp_loads = NULL;
1304                                 modlast = mp;
1305                                 c->private = mp;
1306                                 modcur = mp;
1307                         }
1308                         
1309                         break;
1310 #endif
1311
1312 #ifdef LDAP_SLAPI
1313                 case CFG_PLUGIN:
1314                         if(slapi_int_read_config(c->be, c->fname, c->lineno, c->argc, c->argv) != LDAP_SUCCESS)
1315                                 return(1);
1316                         slapi_plugins_used++;
1317                         break;
1318 #endif
1319
1320 #ifdef SLAP_AUTH_REWRITE
1321                 case CFG_REWRITE: {
1322                         struct berval bv;
1323                         char *line;
1324                         
1325                         if(slap_sasl_rewrite_config(c->fname, c->lineno, c->argc, c->argv))
1326                                 return(1);
1327
1328                         if ( c->argc > 1 ) {
1329                                 char    *s;
1330
1331                                 /* quote all args but the first */
1332                                 line = ldap_charray2str( c->argv, "\" \"" );
1333                                 ber_str2bv( line, 0, 0, &bv );
1334                                 s = ber_bvchr( &bv, '"' );
1335                                 assert( s != NULL );
1336                                 /* move the trailing quote of argv[0] to the end */
1337                                 AC_MEMCPY( s, s + 1, bv.bv_len - ( s - bv.bv_val ) );
1338                                 bv.bv_val[ bv.bv_len - 1 ] = '"';
1339
1340                         } else {
1341                                 ber_str2bv( c->argv[ 0 ], 0, 1, &bv );
1342                         }
1343                         
1344                         ber_bvarray_add( &authz_rewrites, &bv );
1345                         }
1346                         break;
1347 #endif
1348
1349
1350                 default:
1351                         Debug( SLAPD_DEBUG_CONFIG_ERROR,
1352                                 "%s: unknown CFG_TYPE %d"
1353                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1354                                 c->log, c->type, 0 );
1355 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
1356                         return 1;
1357 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
1358
1359         }
1360         return(0);
1361 }
1362
1363
1364 static int
1365 config_fname(ConfigArgs *c) {
1366         if(c->op == SLAP_CONFIG_EMIT) {
1367                 if (c->private) {
1368                         ConfigFile *cf = c->private;
1369                         value_add_one( &c->rvalue_vals, &cf->c_file );
1370                         return 0;
1371                 }
1372                 return 1;
1373         }
1374         return(0);
1375 }
1376
1377 static int
1378 config_cfdir(ConfigArgs *c) {
1379         if(c->op == SLAP_CONFIG_EMIT) {
1380                 if ( !BER_BVISEMPTY( &cfdir )) {
1381                         value_add_one( &c->rvalue_vals, &cfdir );
1382                         return 0;
1383                 }
1384                 return 1;
1385         }
1386         return(0);
1387 }
1388
1389 static int
1390 config_search_base(ConfigArgs *c) {
1391         if(c->op == SLAP_CONFIG_EMIT) {
1392                 int rc = 1;
1393                 if (!BER_BVISEMPTY(&default_search_base)) {
1394                         value_add_one(&c->rvalue_vals, &default_search_base);
1395                         value_add_one(&c->rvalue_nvals, &default_search_nbase);
1396                         rc = 0;
1397                 }
1398                 return rc;
1399         } else if( c->op == LDAP_MOD_DELETE ) {
1400                 ch_free( default_search_base.bv_val );
1401                 ch_free( default_search_nbase.bv_val );
1402                 BER_BVZERO( &default_search_base );
1403                 BER_BVZERO( &default_search_nbase );
1404                 return 0;
1405         }
1406
1407         if(c->bi || c->be != frontendDB) {
1408                 Debug(LDAP_DEBUG_ANY, "%s: defaultSearchBase line must appear "
1409                         "prior to any backend or database definition\n",
1410                         c->log, 0, 0);
1411                 return(1);
1412         }
1413
1414         if(default_search_nbase.bv_len) {
1415                 free(default_search_base.bv_val);
1416                 free(default_search_nbase.bv_val);
1417         }
1418
1419         default_search_base = c->value_dn;
1420         default_search_nbase = c->value_ndn;
1421         return(0);
1422 }
1423
1424 static int
1425 config_passwd_hash(ConfigArgs *c) {
1426         int i;
1427         if (c->op == SLAP_CONFIG_EMIT) {
1428                 struct berval bv;
1429                 for (i=0; default_passwd_hash && default_passwd_hash[i]; i++) {
1430                         ber_str2bv(default_passwd_hash[i], 0, 0, &bv);
1431                         value_add_one(&c->rvalue_vals, &bv);
1432                 }
1433                 return i ? 0 : 1;
1434         } else if ( c->op == LDAP_MOD_DELETE ) {
1435                 if ( c->valx < 0 ) {
1436                         ldap_charray_free( default_passwd_hash );
1437                         default_passwd_hash = NULL;
1438                 } else {
1439                         i = c->valx;
1440                         ch_free( default_passwd_hash[i] );
1441                         for (; default_passwd_hash[i]; i++ )
1442                                 default_passwd_hash[i] = default_passwd_hash[i+1];
1443                 }
1444                 return 0;
1445         }
1446         if(default_passwd_hash) {
1447                 Debug(LDAP_DEBUG_ANY, "%s: "
1448                         "already set default password_hash\n",
1449                         c->log, 0, 0);
1450                 return(1);
1451         }
1452         for(i = 1; i < c->argc; i++) {
1453                 if(!lutil_passwd_scheme(c->argv[i])) {
1454                         snprintf( c->msg, sizeof( c->msg ), "<%s> scheme not available", c->argv[0] );
1455                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1456                                 c->log, c->msg, c->argv[i]);
1457                 } else {
1458                         ldap_charray_add(&default_passwd_hash, c->argv[i]);
1459                 }
1460                 if(!default_passwd_hash) {
1461                         snprintf( c->msg, sizeof( c->msg ), "<%s> no valid hashes found", c->argv[0] );
1462                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1463                                 c->log, c->msg, 0 );
1464                         return(1);
1465                 }
1466         }
1467         return(0);
1468 }
1469
1470 static int
1471 config_schema_dn(ConfigArgs *c) {
1472         if ( c->op == SLAP_CONFIG_EMIT ) {
1473                 int rc = 1;
1474                 if ( !BER_BVISEMPTY( &c->be->be_schemadn )) {
1475                         value_add_one(&c->rvalue_vals, &c->be->be_schemadn);
1476                         value_add_one(&c->rvalue_nvals, &c->be->be_schemandn);
1477                         rc = 0;
1478                 }
1479                 return rc;
1480         } else if ( c->op == LDAP_MOD_DELETE ) {
1481                 ch_free( c->be->be_schemadn.bv_val );
1482                 ch_free( c->be->be_schemandn.bv_val );
1483                 BER_BVZERO( &c->be->be_schemadn );
1484                 BER_BVZERO( &c->be->be_schemandn );
1485                 return 0;
1486         }
1487         ch_free( c->be->be_schemadn.bv_val );
1488         ch_free( c->be->be_schemandn.bv_val );
1489         c->be->be_schemadn = c->value_dn;
1490         c->be->be_schemandn = c->value_ndn;
1491         return(0);
1492 }
1493
1494 static int
1495 config_sizelimit(ConfigArgs *c) {
1496         int i, rc = 0;
1497         struct slap_limits_set *lim = &c->be->be_def_limit;
1498         if (c->op == SLAP_CONFIG_EMIT) {
1499                 char buf[8192];
1500                 struct berval bv;
1501                 bv.bv_val = buf;
1502                 bv.bv_len = 0;
1503                 limits_unparse_one( lim, SLAP_LIMIT_SIZE, &bv );
1504                 if ( !BER_BVISEMPTY( &bv ))
1505                         value_add_one( &c->rvalue_vals, &bv );
1506                 else
1507                         rc = 1;
1508                 return rc;
1509         } else if ( c->op == LDAP_MOD_DELETE ) {
1510                 /* Reset to defaults */
1511                 lim->lms_s_soft = SLAPD_DEFAULT_SIZELIMIT;
1512                 lim->lms_s_hard = 0;
1513                 lim->lms_s_unchecked = -1;
1514                 lim->lms_s_pr = 0;
1515                 lim->lms_s_pr_hide = 0;
1516                 lim->lms_s_pr_total = 0;
1517                 return 0;
1518         }
1519         for(i = 1; i < c->argc; i++) {
1520                 if(!strncasecmp(c->argv[i], "size", 4)) {
1521                         rc = limits_parse_one(c->argv[i], lim);
1522                         if ( rc ) {
1523                                 snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse value", c->argv[0] );
1524                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1525                                         c->log, c->msg, c->argv[i]);
1526                                 return(1);
1527                         }
1528                 } else {
1529                         if(!strcasecmp(c->argv[i], "unlimited")) {
1530                                 lim->lms_s_soft = -1;
1531                         } else {
1532                                 if ( lutil_atoix( &lim->lms_s_soft, c->argv[i], 0 ) != 0 ) {
1533                                         snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse limit", c->argv[0]);
1534                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1535                                                 c->log, c->msg, c->argv[i]);
1536                                         return(1);
1537                                 }
1538                         }
1539                         lim->lms_s_hard = 0;
1540                 }
1541         }
1542         return(0);
1543 }
1544
1545 static int
1546 config_timelimit(ConfigArgs *c) {
1547         int i, rc = 0;
1548         struct slap_limits_set *lim = &c->be->be_def_limit;
1549         if (c->op == SLAP_CONFIG_EMIT) {
1550                 char buf[8192];
1551                 struct berval bv;
1552                 bv.bv_val = buf;
1553                 bv.bv_len = 0;
1554                 limits_unparse_one( lim, SLAP_LIMIT_TIME, &bv );
1555                 if ( !BER_BVISEMPTY( &bv ))
1556                         value_add_one( &c->rvalue_vals, &bv );
1557                 else
1558                         rc = 1;
1559                 return rc;
1560         } else if ( c->op == LDAP_MOD_DELETE ) {
1561                 /* Reset to defaults */
1562                 lim->lms_t_soft = SLAPD_DEFAULT_TIMELIMIT;
1563                 lim->lms_t_hard = 0;
1564                 return 0;
1565         }
1566         for(i = 1; i < c->argc; i++) {
1567                 if(!strncasecmp(c->argv[i], "time", 4)) {
1568                         rc = limits_parse_one(c->argv[i], lim);
1569                         if ( rc ) {
1570                                 snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse value", c->argv[0] );
1571                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1572                                         c->log, c->msg, c->argv[i]);
1573                                 return(1);
1574                         }
1575                 } else {
1576                         if(!strcasecmp(c->argv[i], "unlimited")) {
1577                                 lim->lms_t_soft = -1;
1578                         } else {
1579                                 if ( lutil_atoix( &lim->lms_t_soft, c->argv[i], 0 ) != 0 ) {
1580                                         snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse limit", c->argv[0]);
1581                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1582                                                 c->log, c->msg, c->argv[i]);
1583                                         return(1);
1584                                 }
1585                         }
1586                         lim->lms_t_hard = 0;
1587                 }
1588         }
1589         return(0);
1590 }
1591
1592 static int
1593 config_overlay(ConfigArgs *c) {
1594         slap_overinfo *oi;
1595         if (c->op == SLAP_CONFIG_EMIT) {
1596                 return 1;
1597         } else if ( c->op == LDAP_MOD_DELETE ) {
1598                 assert(0);
1599         }
1600         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1])) {
1601                 /* log error */
1602                 Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: (optional) %s overlay \"%s\" configuration failed"
1603                         SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1604                         c->log, c->be == frontendDB ? "global " : "", &c->argv[1][1]);
1605 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
1606                 return 1;
1607 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
1608         } else if(overlay_config(c->be, c->argv[1])) {
1609                 return(1);
1610         }
1611         /* Setup context for subsequent config directives.
1612          * The newly added overlay is at the head of the list.
1613          */
1614         oi = (slap_overinfo *)c->be->bd_info;
1615         c->bi = &oi->oi_list->on_bi;
1616         return(0);
1617 }
1618
1619 static int
1620 config_subordinate(ConfigArgs *c)
1621 {
1622         int rc = 1;
1623         int advertise;
1624
1625         switch( c->op ) {
1626         case SLAP_CONFIG_EMIT:
1627                 if ( SLAP_GLUE_SUBORDINATE( c->be )) {
1628                         struct berval bv;
1629
1630                         bv.bv_val = SLAP_GLUE_ADVERTISE( c->be ) ? "advertise" : "TRUE";
1631                         bv.bv_len = SLAP_GLUE_ADVERTISE( c->be ) ? STRLENOF("advertise") :
1632                                 STRLENOF("TRUE");
1633
1634                         value_add_one( &c->rvalue_vals, &bv );
1635                         rc = 0;
1636                 }
1637                 break;
1638         case LDAP_MOD_DELETE:
1639                 if ( !c->line  || strcasecmp( c->line, "advertise" )) {
1640                         glue_sub_del( c->be );
1641                 } else {
1642                         SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_GLUE_ADVERTISE;
1643                 }
1644                 rc = 0;
1645                 break;
1646         case LDAP_MOD_ADD:
1647         case SLAP_CONFIG_ADD:
1648                 advertise = ( c->argc == 2 && !strcasecmp( c->argv[1], "advertise" ));
1649                 rc = glue_sub_add( c->be, advertise, CONFIG_ONLINE_ADD( c ));
1650                 break;
1651         }
1652         return rc;
1653 }
1654
1655 static int
1656 config_suffix(ConfigArgs *c)
1657 {
1658         Backend *tbe;
1659         struct berval pdn, ndn;
1660         char    *notallowed = NULL;
1661
1662         if ( c->be == frontendDB ) {
1663                 notallowed = "frontend";
1664
1665         } else if ( SLAP_MONITOR(c->be) ) {
1666                 notallowed = "monitor";
1667
1668         } else if ( SLAP_CONFIG(c->be) ) {
1669                 notallowed = "config";
1670         }
1671
1672         if ( notallowed != NULL ) {
1673                 char    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1674
1675                 switch ( c->op ) {
1676                 case LDAP_MOD_ADD:
1677                 case LDAP_MOD_DELETE:
1678                 case LDAP_MOD_REPLACE:
1679                 case LDAP_MOD_INCREMENT:
1680                 case SLAP_CONFIG_ADD:
1681                         if ( !BER_BVISNULL( &c->value_dn ) ) {
1682                                 snprintf( buf, sizeof( buf ), "<%s> ",
1683                                                 c->value_dn.bv_val );
1684                         }
1685
1686                         Debug(LDAP_DEBUG_ANY,
1687                                 "%s: suffix %snot allowed in %s database.\n",
1688                                 c->log, buf, notallowed );
1689                         break;
1690
1691                 case SLAP_CONFIG_EMIT:
1692                         /* don't complain when emitting... */
1693                         break;
1694
1695                 default:
1696                         /* FIXME: don't know what values may be valid;
1697                          * please remove assertion, or add legal values
1698                          * to either block */
1699                         assert( 0 );
1700                         break;
1701                 }
1702
1703                 return 1;
1704         }
1705
1706         if (c->op == SLAP_CONFIG_EMIT) {
1707                 if ( c->be->be_suffix == NULL
1708                                 || BER_BVISNULL( &c->be->be_suffix[0] ) )
1709                 {
1710                         return 1;
1711                 } else {
1712                         value_add( &c->rvalue_vals, c->be->be_suffix );
1713                         value_add( &c->rvalue_nvals, c->be->be_nsuffix );
1714                         return 0;
1715                 }
1716         } else if ( c->op == LDAP_MOD_DELETE ) {
1717                 if ( c->valx < 0 ) {
1718                         ber_bvarray_free( c->be->be_suffix );
1719                         ber_bvarray_free( c->be->be_nsuffix );
1720                         c->be->be_suffix = NULL;
1721                         c->be->be_nsuffix = NULL;
1722                 } else {
1723                         int i = c->valx;
1724                         ch_free( c->be->be_suffix[i].bv_val );
1725                         ch_free( c->be->be_nsuffix[i].bv_val );
1726                         for (; c->be->be_suffix[i].bv_val; i++) {
1727                                 c->be->be_suffix[i] = c->be->be_suffix[i+1];
1728                                 c->be->be_nsuffix[i] = c->be->be_nsuffix[i+1];
1729                         }
1730                 }
1731                 return 0;
1732         }
1733
1734 #ifdef SLAPD_MONITOR_DN
1735         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
1736                 snprintf( c->msg, sizeof( c->msg ), "<%s> DN is reserved for monitoring slapd",
1737                         c->argv[0] );
1738                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1739                         c->log, c->msg, SLAPD_MONITOR_DN);
1740                 return(1);
1741         }
1742 #endif
1743
1744         pdn = c->value_dn;
1745         ndn = c->value_ndn;
1746         tbe = select_backend(&ndn, 0, 0);
1747         if(tbe == c->be) {
1748                 Debug( SLAPD_DEBUG_CONFIG_ERROR,
1749                         "%s: suffix already served by this backend!"
1750                         SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1751                         c->log, 0, 0);
1752 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
1753                 return 1;
1754 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
1755                 free(pdn.bv_val);
1756                 free(ndn.bv_val);
1757         } else if(tbe) {
1758                 char    *type = tbe->bd_info->bi_type;
1759
1760                 if ( overlay_is_over( tbe ) ) {
1761                         slap_overinfo   *oi = (slap_overinfo *)tbe->bd_info->bi_private;
1762                         type = oi->oi_orig->bi_type;
1763                 }
1764
1765                 snprintf( c->msg, sizeof( c->msg ), "<%s> namingContext \"%s\" already served by "
1766                         "a preceding %s database serving namingContext",
1767                         c->argv[0], pdn.bv_val, type );
1768                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1769                         c->log, c->msg, tbe->be_suffix[0].bv_val);
1770                 free(pdn.bv_val);
1771                 free(ndn.bv_val);
1772                 return(1);
1773         } else if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
1774                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
1775                         "base provided \"%s\" (assuming okay)\n",
1776                         c->log, default_search_base.bv_val, 0);
1777         }
1778         ber_bvarray_add(&c->be->be_suffix, &pdn);
1779         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
1780         return(0);
1781 }
1782
1783 static int
1784 config_rootdn(ConfigArgs *c) {
1785         if (c->op == SLAP_CONFIG_EMIT) {
1786                 if ( !BER_BVISNULL( &c->be->be_rootdn )) {
1787                         value_add_one(&c->rvalue_vals, &c->be->be_rootdn);
1788                         value_add_one(&c->rvalue_nvals, &c->be->be_rootndn);
1789                         return 0;
1790                 } else {
1791                         return 1;
1792                 }
1793         } else if ( c->op == LDAP_MOD_DELETE ) {
1794                 ch_free( c->be->be_rootdn.bv_val );
1795                 ch_free( c->be->be_rootndn.bv_val );
1796                 BER_BVZERO( &c->be->be_rootdn );
1797                 BER_BVZERO( &c->be->be_rootndn );
1798                 return 0;
1799         }
1800         if ( !BER_BVISNULL( &c->be->be_rootdn )) {
1801                 ch_free( c->be->be_rootdn.bv_val );
1802                 ch_free( c->be->be_rootndn.bv_val );
1803         }
1804         c->be->be_rootdn = c->value_dn;
1805         c->be->be_rootndn = c->value_ndn;
1806         return(0);
1807 }
1808
1809 static int
1810 config_rootpw(ConfigArgs *c) {
1811         Backend *tbe;
1812
1813         if (c->op == SLAP_CONFIG_EMIT) {
1814                 if (!BER_BVISEMPTY(&c->be->be_rootpw)) {
1815                         /* don't copy, because "rootpw" is marked
1816                          * as CFG_BERVAL */
1817                         c->value_bv = c->be->be_rootpw;
1818                         return 0;
1819                 }
1820                 return 1;
1821         } else if ( c->op == LDAP_MOD_DELETE ) {
1822                 ch_free( c->be->be_rootpw.bv_val );
1823                 BER_BVZERO( &c->be->be_rootpw );
1824                 return 0;
1825         }
1826
1827         tbe = select_backend(&c->be->be_rootndn, 0, 0);
1828         if(tbe != c->be) {
1829                 snprintf( c->msg, sizeof( c->msg ), "<%s> can only be set when rootdn is under suffix",
1830                         c->argv[0] );
1831                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1832                         c->log, c->msg, 0);
1833                 return(1);
1834         }
1835         if ( !BER_BVISNULL( &c->be->be_rootpw ))
1836                 ch_free( c->be->be_rootpw.bv_val );
1837         c->be->be_rootpw = c->value_bv;
1838         return(0);
1839 }
1840
1841 static int
1842 config_restrict(ConfigArgs *c) {
1843         slap_mask_t restrictops = 0;
1844         int i;
1845         slap_verbmasks restrictable_ops[] = {
1846                 { BER_BVC("bind"),              SLAP_RESTRICT_OP_BIND },
1847                 { BER_BVC("add"),               SLAP_RESTRICT_OP_ADD },
1848                 { BER_BVC("modify"),            SLAP_RESTRICT_OP_MODIFY },
1849                 { BER_BVC("rename"),            SLAP_RESTRICT_OP_RENAME },
1850                 { BER_BVC("modrdn"),            0 },
1851                 { BER_BVC("delete"),            SLAP_RESTRICT_OP_DELETE },
1852                 { BER_BVC("search"),            SLAP_RESTRICT_OP_SEARCH },
1853                 { BER_BVC("compare"),   SLAP_RESTRICT_OP_COMPARE },
1854                 { BER_BVC("read"),              SLAP_RESTRICT_OP_READS },
1855                 { BER_BVC("write"),             SLAP_RESTRICT_OP_WRITES },
1856                 { BER_BVC("extended"),  SLAP_RESTRICT_OP_EXTENDED },
1857                 { BER_BVC("extended=" LDAP_EXOP_START_TLS ),            SLAP_RESTRICT_EXOP_START_TLS },
1858                 { BER_BVC("extended=" LDAP_EXOP_MODIFY_PASSWD ),        SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
1859                 { BER_BVC("extended=" LDAP_EXOP_X_WHO_AM_I ),           SLAP_RESTRICT_EXOP_WHOAMI },
1860                 { BER_BVC("extended=" LDAP_EXOP_X_CANCEL ),             SLAP_RESTRICT_EXOP_CANCEL },
1861                 { BER_BVNULL,   0 }
1862         };
1863
1864         if (c->op == SLAP_CONFIG_EMIT) {
1865                 return mask_to_verbs( restrictable_ops, c->be->be_restrictops,
1866                         &c->rvalue_vals );
1867         } else if ( c->op == LDAP_MOD_DELETE ) {
1868                 if ( !c->line ) {
1869                         c->be->be_restrictops = 0;
1870                 } else {
1871                         restrictops = verb_to_mask( c->line, restrictable_ops );
1872                         c->be->be_restrictops ^= restrictops;
1873                 }
1874                 return 0;
1875         }
1876         i = verbs_to_mask( c->argc, c->argv, restrictable_ops, &restrictops );
1877         if ( i ) {
1878                 snprintf( c->msg, sizeof( c->msg ), "<%s> unknown operation", c->argv[0] );
1879                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1880                         c->log, c->msg, c->argv[i]);
1881                 return(1);
1882         }
1883         if ( restrictops & SLAP_RESTRICT_OP_EXTENDED )
1884                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
1885         c->be->be_restrictops |= restrictops;
1886         return(0);
1887 }
1888
1889 static int
1890 config_allows(ConfigArgs *c) {
1891         slap_mask_t allows = 0;
1892         int i;
1893         slap_verbmasks allowable_ops[] = {
1894                 { BER_BVC("bind_v2"),           SLAP_ALLOW_BIND_V2 },
1895                 { BER_BVC("bind_anon_cred"),    SLAP_ALLOW_BIND_ANON_CRED },
1896                 { BER_BVC("bind_anon_dn"),      SLAP_ALLOW_BIND_ANON_DN },
1897                 { BER_BVC("update_anon"),       SLAP_ALLOW_UPDATE_ANON },
1898                 { BER_BVNULL,   0 }
1899         };
1900         if (c->op == SLAP_CONFIG_EMIT) {
1901                 return mask_to_verbs( allowable_ops, global_allows, &c->rvalue_vals );
1902         } else if ( c->op == LDAP_MOD_DELETE ) {
1903                 if ( !c->line ) {
1904                         global_allows = 0;
1905                 } else {
1906                         allows = verb_to_mask( c->line, allowable_ops );
1907                         global_allows ^= allows;
1908                 }
1909                 return 0;
1910         }
1911         i = verbs_to_mask(c->argc, c->argv, allowable_ops, &allows);
1912         if ( i ) {
1913                 snprintf( c->msg, sizeof( c->msg ), "<%s> unknown feature", c->argv[0] );
1914                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1915                         c->log, c->msg, c->argv[i]);
1916                 return(1);
1917         }
1918         global_allows |= allows;
1919         return(0);
1920 }
1921
1922 static int
1923 config_disallows(ConfigArgs *c) {
1924         slap_mask_t disallows = 0;
1925         int i;
1926         slap_verbmasks disallowable_ops[] = {
1927                 { BER_BVC("bind_anon"),         SLAP_DISALLOW_BIND_ANON },
1928                 { BER_BVC("bind_simple"),       SLAP_DISALLOW_BIND_SIMPLE },
1929                 { BER_BVC("bind_krb4"),         SLAP_DISALLOW_BIND_KRBV4 },
1930                 { BER_BVC("tls_2_anon"),                SLAP_DISALLOW_TLS_2_ANON },
1931                 { BER_BVC("tls_authc"),         SLAP_DISALLOW_TLS_AUTHC },
1932                 { BER_BVNULL, 0 }
1933         };
1934         if (c->op == SLAP_CONFIG_EMIT) {
1935                 return mask_to_verbs( disallowable_ops, global_disallows, &c->rvalue_vals );
1936         } else if ( c->op == LDAP_MOD_DELETE ) {
1937                 if ( !c->line ) {
1938                         global_disallows = 0;
1939                 } else {
1940                         disallows = verb_to_mask( c->line, disallowable_ops );
1941                         global_disallows ^= disallows;
1942                 }
1943                 return 0;
1944         }
1945         i = verbs_to_mask(c->argc, c->argv, disallowable_ops, &disallows);
1946         if ( i ) {
1947                 snprintf( c->msg, sizeof( c->msg ), "<%s> unknown feature", c->argv[0] );
1948                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1949                         c->log, c->msg, c->argv[i]);
1950                 return(1);
1951         }
1952         global_disallows |= disallows;
1953         return(0);
1954 }
1955
1956 static int
1957 config_requires(ConfigArgs *c) {
1958         slap_mask_t requires = 0;
1959         int i;
1960         slap_verbmasks requires_ops[] = {
1961                 { BER_BVC("bind"),              SLAP_REQUIRE_BIND },
1962                 { BER_BVC("LDAPv3"),            SLAP_REQUIRE_LDAP_V3 },
1963                 { BER_BVC("authc"),             SLAP_REQUIRE_AUTHC },
1964                 { BER_BVC("sasl"),              SLAP_REQUIRE_SASL },
1965                 { BER_BVC("strong"),            SLAP_REQUIRE_STRONG },
1966                 { BER_BVNULL, 0 }
1967         };
1968         if (c->op == SLAP_CONFIG_EMIT) {
1969                 return mask_to_verbs( requires_ops, c->be->be_requires, &c->rvalue_vals );
1970         } else if ( c->op == LDAP_MOD_DELETE ) {
1971                 if ( !c->line ) {
1972                         c->be->be_requires = 0;
1973                 } else {
1974                         requires = verb_to_mask( c->line, requires_ops );
1975                         c->be->be_requires ^= requires;
1976                 }
1977                 return 0;
1978         }
1979         i = verbs_to_mask(c->argc, c->argv, requires_ops, &requires);
1980         if ( i ) {
1981                 snprintf( c->msg, sizeof( c->msg ), "<%s> unknown feature", c->argv[0] );
1982                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1983                         c->log, c->msg, c->argv[i]);
1984                 return(1);
1985         }
1986         c->be->be_requires = requires;
1987         return(0);
1988 }
1989
1990 static slap_verbmasks   *loglevel_ops;
1991
1992 static int
1993 loglevel_init( void )
1994 {
1995         slap_verbmasks  lo[] = {
1996                 { BER_BVC("Any"),       -1 },
1997                 { BER_BVC("Trace"),     LDAP_DEBUG_TRACE },
1998                 { BER_BVC("Packets"),   LDAP_DEBUG_PACKETS },
1999                 { BER_BVC("Args"),      LDAP_DEBUG_ARGS },
2000                 { BER_BVC("Conns"),     LDAP_DEBUG_CONNS },
2001                 { BER_BVC("BER"),       LDAP_DEBUG_BER },
2002                 { BER_BVC("Filter"),    LDAP_DEBUG_FILTER },
2003                 { BER_BVC("Config"),    LDAP_DEBUG_CONFIG },
2004                 { BER_BVC("ACL"),       LDAP_DEBUG_ACL },
2005                 { BER_BVC("Stats"),     LDAP_DEBUG_STATS },
2006                 { BER_BVC("Stats2"),    LDAP_DEBUG_STATS2 },
2007                 { BER_BVC("Shell"),     LDAP_DEBUG_SHELL },
2008                 { BER_BVC("Parse"),     LDAP_DEBUG_PARSE },
2009                 { BER_BVC("Cache"),     LDAP_DEBUG_CACHE },
2010                 { BER_BVC("Index"),     LDAP_DEBUG_INDEX },
2011                 { BER_BVC("Sync"),      LDAP_DEBUG_SYNC },
2012                 { BER_BVC("None"),      LDAP_DEBUG_NONE },
2013                 { BER_BVNULL,           0 }
2014         };
2015
2016         return slap_verbmasks_init( &loglevel_ops, lo );
2017 }
2018
2019 static void
2020 loglevel_destroy( void )
2021 {
2022         if ( loglevel_ops ) {
2023                 (void)slap_verbmasks_destroy( loglevel_ops );
2024         }
2025         loglevel_ops = NULL;
2026 }
2027
2028 static slap_mask_t      loglevel_ignore[] = { -1, 0 };
2029
2030 int
2031 slap_loglevel_register( slap_mask_t m, struct berval *s )
2032 {
2033         int     rc;
2034
2035         if ( loglevel_ops == NULL ) {
2036                 loglevel_init();
2037         }
2038
2039         rc = slap_verbmasks_append( &loglevel_ops, m, s, loglevel_ignore );
2040
2041         if ( rc != 0 ) {
2042                 Debug( LDAP_DEBUG_ANY, "slap_loglevel_register(%lu, \"%s\") failed\n",
2043                         m, s->bv_val, 0 );
2044         }
2045
2046         return rc;
2047 }
2048
2049 int
2050 slap_loglevel_get( struct berval *s, int *l )
2051 {
2052         int             rc;
2053         unsigned long   i;
2054         slap_mask_t     m;
2055
2056         if ( loglevel_ops == NULL ) {
2057                 loglevel_init();
2058         }
2059
2060         for ( m = 0, i = 1; !BER_BVISNULL( &loglevel_ops[ i ].word ); i++ ) {
2061                 m |= loglevel_ops[ i ].mask;
2062         }
2063
2064         m = ~m;
2065
2066         for ( i = 1; i <= ( 1 << ( sizeof( int ) * 8 - 1 ) ) && !( m & i ); i <<= 1 )
2067                 ;
2068
2069         if ( !( m & i ) ) {
2070                 return -1;
2071         }
2072
2073         rc = slap_verbmasks_append( &loglevel_ops, i, s, loglevel_ignore );
2074
2075         if ( rc != 0 ) {
2076                 Debug( LDAP_DEBUG_ANY, "slap_loglevel_register(%lu, \"%s\") failed\n",
2077                         i, s->bv_val, 0 );
2078
2079         } else {
2080                 *l = i;
2081         }
2082
2083         return rc;
2084 }
2085
2086 int
2087 str2loglevel( const char *s, int *l )
2088 {
2089         int     i;
2090
2091         if ( loglevel_ops == NULL ) {
2092                 loglevel_init();
2093         }
2094
2095         i = verb_to_mask( s, loglevel_ops );
2096
2097         if ( BER_BVISNULL( &loglevel_ops[ i ].word ) ) {
2098                 return -1;
2099         }
2100
2101         *l = loglevel_ops[ i ].mask;
2102
2103         return 0;
2104 }
2105
2106 const char *
2107 loglevel2str( int l )
2108 {
2109         struct berval   bv = BER_BVNULL;
2110
2111         loglevel2bv( l, &bv );
2112
2113         return bv.bv_val;
2114 }
2115
2116 int
2117 loglevel2bv( int l, struct berval *bv )
2118 {
2119         if ( loglevel_ops == NULL ) {
2120                 loglevel_init();
2121         }
2122
2123         BER_BVZERO( bv );
2124
2125         return enum_to_verb( loglevel_ops, l, bv ) == -1;
2126 }
2127
2128 int
2129 loglevel2bvarray( int l, BerVarray *bva )
2130 {
2131         if ( loglevel_ops == NULL ) {
2132                 loglevel_init();
2133         }
2134
2135         return mask_to_verbs( loglevel_ops, l, bva );
2136 }
2137
2138 static int config_syslog;
2139
2140 static int
2141 config_loglevel(ConfigArgs *c) {
2142         int i;
2143
2144         if ( loglevel_ops == NULL ) {
2145                 loglevel_init();
2146         }
2147
2148         if (c->op == SLAP_CONFIG_EMIT) {
2149                 /* Get default or commandline slapd setting */
2150                 if ( ldap_syslog && !config_syslog )
2151                         config_syslog = ldap_syslog;
2152                 return loglevel2bvarray( config_syslog, &c->rvalue_vals );
2153
2154         } else if ( c->op == LDAP_MOD_DELETE ) {
2155                 if ( !c->line ) {
2156                         config_syslog = 0;
2157                 } else {
2158                         int level = verb_to_mask( c->line, loglevel_ops );
2159                         config_syslog ^= level;
2160                 }
2161                 if ( slapMode & SLAP_SERVER_MODE ) {
2162                         ldap_syslog = config_syslog;
2163                 }
2164                 return 0;
2165         }
2166
2167         config_syslog = 0;
2168
2169         for( i=1; i < c->argc; i++ ) {
2170                 int     level;
2171
2172                 if ( isdigit( c->argv[i][0] ) || c->argv[i][0] == '-' ) {
2173                         if( lutil_atoi( &level, c->argv[i] ) != 0 ) {
2174                                 snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse level", c->argv[0] );
2175                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2176                                         c->log, c->msg, c->argv[i]);
2177                                 return( 1 );
2178                         }
2179                 } else {
2180                         if ( str2loglevel( c->argv[i], &level ) ) {
2181                                 snprintf( c->msg, sizeof( c->msg ), "<%s> unknown level", c->argv[0] );
2182                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2183                                         c->log, c->msg, c->argv[i]);
2184                                 return( 1 );
2185                         }
2186                 }
2187                 config_syslog |= level;
2188         }
2189         if ( slapMode & SLAP_SERVER_MODE ) {
2190                 ldap_syslog = config_syslog;
2191         }
2192         return(0);
2193 }
2194
2195 static int
2196 config_referral(ConfigArgs *c) {
2197         struct berval val;
2198         if (c->op == SLAP_CONFIG_EMIT) {
2199                 if ( default_referral ) {
2200                         value_add( &c->rvalue_vals, default_referral );
2201                         return 0;
2202                 } else {
2203                         return 1;
2204                 }
2205         } else if ( c->op == LDAP_MOD_DELETE ) {
2206                 if ( c->valx < 0 ) {
2207                         ber_bvarray_free( default_referral );
2208                         default_referral = NULL;
2209                 } else {
2210                         int i = c->valx;
2211                         ch_free( default_referral[i].bv_val );
2212                         for (; default_referral[i].bv_val; i++ )
2213                                 default_referral[i] = default_referral[i+1];
2214                 }
2215                 return 0;
2216         }
2217         if(validate_global_referral(c->argv[1])) {
2218                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid URL", c->argv[0] );
2219                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2220                         c->log, c->msg, c->argv[1]);
2221                 return(1);
2222         }
2223
2224         ber_str2bv(c->argv[1], 0, 0, &val);
2225         if(value_add_one(&default_referral, &val)) return(LDAP_OTHER);
2226         return(0);
2227 }
2228
2229 static struct {
2230         struct berval key;
2231         int off;
2232 } sec_keys[] = {
2233         { BER_BVC("ssf="), offsetof(slap_ssf_set_t, sss_ssf) },
2234         { BER_BVC("transport="), offsetof(slap_ssf_set_t, sss_transport) },
2235         { BER_BVC("tls="), offsetof(slap_ssf_set_t, sss_tls) },
2236         { BER_BVC("sasl="), offsetof(slap_ssf_set_t, sss_sasl) },
2237         { BER_BVC("update_ssf="), offsetof(slap_ssf_set_t, sss_update_ssf) },
2238         { BER_BVC("update_transport="), offsetof(slap_ssf_set_t, sss_update_transport) },
2239         { BER_BVC("update_tls="), offsetof(slap_ssf_set_t, sss_update_tls) },
2240         { BER_BVC("update_sasl="), offsetof(slap_ssf_set_t, sss_update_sasl) },
2241         { BER_BVC("simple_bind="), offsetof(slap_ssf_set_t, sss_simple_bind) },
2242         { BER_BVNULL, 0 }
2243 };
2244
2245 static int
2246 config_security(ConfigArgs *c) {
2247         slap_ssf_set_t *set = &c->be->be_ssf_set;
2248         char *next;
2249         int i, j;
2250         if (c->op == SLAP_CONFIG_EMIT) {
2251                 char numbuf[32];
2252                 struct berval bv;
2253                 slap_ssf_t *tgt;
2254                 int rc = 1;
2255
2256                 for (i=0; !BER_BVISNULL( &sec_keys[i].key ); i++) {
2257                         tgt = (slap_ssf_t *)((char *)set + sec_keys[i].off);
2258                         if ( *tgt ) {
2259                                 rc = 0;
2260                                 bv.bv_len = snprintf( numbuf, sizeof( numbuf ), "%u", *tgt );
2261                                 if ( bv.bv_len >= sizeof( numbuf ) ) {
2262                                         ber_bvarray_free_x( c->rvalue_vals, NULL );
2263                                         c->rvalue_vals = NULL;
2264                                         rc = 1;
2265                                         break;
2266                                 }
2267                                 bv.bv_len += sec_keys[i].key.bv_len;
2268                                 bv.bv_val = ch_malloc( bv.bv_len + 1);
2269                                 next = lutil_strcopy( bv.bv_val, sec_keys[i].key.bv_val );
2270                                 strcpy( next, numbuf );
2271                                 ber_bvarray_add( &c->rvalue_vals, &bv );
2272                         }
2273                 }
2274                 return rc;
2275         }
2276         for(i = 1; i < c->argc; i++) {
2277                 slap_ssf_t *tgt = NULL;
2278                 char *src;
2279                 for ( j=0; !BER_BVISNULL( &sec_keys[j].key ); j++ ) {
2280                         if(!strncasecmp(c->argv[i], sec_keys[j].key.bv_val,
2281                                 sec_keys[j].key.bv_len)) {
2282                                 src = c->argv[i] + sec_keys[j].key.bv_len;
2283                                 tgt = (slap_ssf_t *)((char *)set + sec_keys[j].off);
2284                                 break;
2285                         }
2286                 }
2287                 if ( !tgt ) {
2288                         snprintf( c->msg, sizeof( c->msg ), "<%s> unknown factor", c->argv[0] );
2289                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
2290                                 c->log, c->msg, c->argv[i]);
2291                         return(1);
2292                 }
2293
2294                 if ( lutil_atou( tgt, src ) != 0 ) {
2295                         snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse factor", c->argv[0] );
2296                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2297                                 c->log, c->msg, c->argv[i]);
2298                         return(1);
2299                 }
2300         }
2301         return(0);
2302 }
2303
2304 char *
2305 anlist_unparse( AttributeName *an, char *ptr ) {
2306         int comma = 0;
2307
2308         for (; !BER_BVISNULL( &an->an_name ); an++) {
2309                 if ( comma ) *ptr++ = ',';
2310                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
2311                 comma = 1;
2312         }
2313         return ptr;
2314 }
2315
2316 static void
2317 replica_unparse( struct slap_replica_info *ri, int i, struct berval *bv )
2318 {
2319         int len;
2320         char *ptr;
2321         struct berval bc = BER_BVNULL;
2322         char numbuf[32];
2323
2324         assert( !BER_BVISNULL( &ri->ri_bindconf.sb_uri ) );
2325         
2326         BER_BVZERO( bv );
2327
2328         len = snprintf(numbuf, sizeof( numbuf ), SLAP_X_ORDERED_FMT, i );
2329         if ( len >= sizeof( numbuf ) ) {
2330                 /* FIXME: how can indicate error? */
2331                 return;
2332         }
2333
2334         if ( ri->ri_nsuffix ) {
2335                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
2336                         len += ri->ri_nsuffix[i].bv_len + STRLENOF(" suffix=\"\"");
2337                 }
2338         }
2339         if ( ri->ri_attrs ) {
2340                 len += STRLENOF(" attrs");
2341                 if ( ri->ri_exclude ) len++;
2342                 for (i=0; !BER_BVISNULL( &ri->ri_attrs[i].an_name ); i++) {
2343                         len += 1 + ri->ri_attrs[i].an_name.bv_len;
2344                 }
2345         }
2346         bindconf_unparse( &ri->ri_bindconf, &bc );
2347         len += bc.bv_len;
2348
2349         bv->bv_val = ch_malloc(len + 1);
2350         bv->bv_len = len;
2351
2352         ptr = lutil_strcopy( bv->bv_val, numbuf );
2353
2354         /* start with URI from bindconf */
2355         assert( !BER_BVISNULL( &bc ) );
2356         if ( bc.bv_val ) {
2357                 strcpy( ptr, bc.bv_val );
2358                 ch_free( bc.bv_val );
2359         }
2360
2361         if ( ri->ri_nsuffix ) {
2362                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
2363                         ptr = lutil_strcopy( ptr, " suffix=\"" );
2364                         ptr = lutil_strcopy( ptr, ri->ri_nsuffix[i].bv_val );
2365                         *ptr++ = '"';
2366                 }
2367         }
2368         if ( ri->ri_attrs ) {
2369                 ptr = lutil_strcopy( ptr, " attrs" );
2370                 if ( ri->ri_exclude ) *ptr++ = '!';
2371                 *ptr++ = '=';
2372                 ptr = anlist_unparse( ri->ri_attrs, ptr );
2373         }
2374 }
2375
2376 static int
2377 config_replica(ConfigArgs *c) {
2378         int i, nr = -1;
2379         char *replicahost = NULL, *replicauri = NULL;
2380         LDAPURLDesc *ludp;
2381
2382         if (c->op == SLAP_CONFIG_EMIT) {
2383                 if (c->be->be_replica) {
2384                         struct berval bv;
2385                         for (i=0;c->be->be_replica[i]; i++) {
2386                                 replica_unparse( c->be->be_replica[i], i, &bv );
2387                                 ber_bvarray_add( &c->rvalue_vals, &bv );
2388                         }
2389                         return 0;
2390                 }
2391                 return 1;
2392         } else if ( c->op == LDAP_MOD_DELETE ) {
2393                 /* FIXME: there is no replica_free function */
2394                 if ( c->valx < 0 ) {
2395                 } else {
2396                 }
2397         }
2398         if(SLAP_MONITOR(c->be)) {
2399                 Debug(LDAP_DEBUG_ANY, "%s: "
2400                         "\"replica\" should not be used inside monitor database\n",
2401                         c->log, 0, 0);
2402                 return(0);      /* FIXME: should this be an error? */
2403         }
2404
2405         for(i = 1; i < c->argc; i++) {
2406                 if(!strncasecmp(c->argv[i], "host=", STRLENOF("host="))) {
2407                         ber_len_t       len;
2408
2409                         if ( replicauri ) {
2410                                 snprintf( c->msg, sizeof( c->msg ), "<%s> replica host/URI already specified", c->argv[0] );
2411                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n", c->log, c->msg, replicauri );
2412                                 return(1);
2413                         }
2414
2415                         replicahost = c->argv[i] + STRLENOF("host=");
2416                         len = strlen( replicahost ) + STRLENOF("ldap://");
2417                         replicauri = ch_malloc( len + 1 );
2418                         snprintf( replicauri, len + 1, "ldap://%s", replicahost );
2419                         replicahost = replicauri + STRLENOF( "ldap://");
2420                         nr = add_replica_info(c->be, replicauri, replicahost);
2421                         break;
2422                 } else if(!strncasecmp(c->argv[i], "uri=", STRLENOF("uri="))) {
2423                         if ( replicauri ) {
2424                                 snprintf( c->msg, sizeof( c->msg ), "<%s> replica host/URI already specified", c->argv[0] );
2425                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n", c->log, c->msg, replicauri );
2426                                 return(1);
2427                         }
2428
2429                         if(ldap_url_parse(c->argv[i] + STRLENOF("uri="), &ludp) != LDAP_SUCCESS) {
2430                                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid uri", c->argv[0] );
2431                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
2432                                 return(1);
2433                         }
2434                         if(!ludp->lud_host) {
2435                                 ldap_free_urldesc(ludp);
2436                                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid uri - missing hostname",
2437                                         c->argv[0] );
2438                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
2439                                 return(1);
2440                         }
2441                         ldap_free_urldesc(ludp);
2442                         replicauri = c->argv[i] + STRLENOF("uri=");
2443                         replicauri = ch_strdup( replicauri );
2444                         replicahost = strchr( replicauri, '/' );
2445                         replicahost += 2;
2446                         nr = add_replica_info(c->be, replicauri, replicahost);
2447                         break;
2448                 }
2449         }
2450         if(i == c->argc) {
2451                 snprintf( c->msg, sizeof( c->msg ), "<%s> missing host or uri", c->argv[0] );
2452                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
2453                 return(1);
2454         } else if(nr == -1) {
2455                 snprintf( c->msg, sizeof( c->msg ), "<%s> unable to add replica", c->argv[0] );
2456                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n", c->log, c->msg,
2457                         replicauri ? replicauri : "" );
2458                 return(1);
2459         } else {
2460                 for(i = 1; i < c->argc; i++) {
2461                         if(!strncasecmp(c->argv[i], "uri=", STRLENOF("uri="))) {
2462                                 /* dealt with separately; don't let it get to bindconf */
2463                                 ;
2464
2465                         } else if(!strncasecmp(c->argv[i], "suffix=", STRLENOF( "suffix="))) {
2466                                 switch(add_replica_suffix(c->be, nr, c->argv[i] + STRLENOF("suffix="))) {
2467                                         case 1:
2468                                                 Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
2469                                                 "suffix \"%s\" in \"replica\" line is not valid for backend"
2470                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
2471                                                 c->log, c->argv[i] + STRLENOF("suffix="), 0);
2472 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
2473                                                 return 1;
2474 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
2475                                                 break;
2476                                         case 2:
2477                                                 Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
2478                                                 "unable to normalize suffix in \"replica\" line"
2479                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
2480                                                 c->log, 0, 0);
2481 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
2482                                                 return 1;
2483 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
2484                                                 break;
2485                                 }
2486
2487                         } else if (!strncasecmp(c->argv[i], "attr", STRLENOF("attr"))
2488                                 || !strncasecmp(c->argv[i], "attrs", STRLENOF("attrs")))
2489                         {
2490                                 int exclude = 0;
2491                                 char *arg = c->argv[i] + STRLENOF("attr");
2492                                 if (arg[0] == 's') {
2493                                         arg++;
2494                                 } else {
2495                                         Debug( LDAP_DEBUG_ANY,
2496                                                 "%s: \"attr\" "
2497                                                 "is deprecated (and undocumented); "
2498                                                 "use \"attrs\" instead.\n",
2499                                                 c->log, 0, 0 );
2500                                 }
2501                                 if(arg[0] == '!') {
2502                                         arg++;
2503                                         exclude = 1;
2504                                 }
2505                                 if(arg[0] != '=') {
2506                                         continue;
2507                                 }
2508                                 if(add_replica_attrs(c->be, nr, arg + 1, exclude)) {
2509                                         snprintf( c->msg, sizeof( c->msg ), "<%s> unknown attribute", c->argv[0] );
2510                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2511                                                 c->log, c->msg, arg + 1);
2512                                         return(1);
2513                                 }
2514                         } else if ( bindconf_parse( c->argv[i],
2515                                         &c->be->be_replica[nr]->ri_bindconf ) ) {
2516                                 return(1);
2517                         }
2518                 }
2519         }
2520         return(0);
2521 }
2522
2523 static int
2524 config_updatedn(ConfigArgs *c) {
2525         if (c->op == SLAP_CONFIG_EMIT) {
2526                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
2527                         value_add_one(&c->rvalue_vals, &c->be->be_update_ndn);
2528                         value_add_one(&c->rvalue_nvals, &c->be->be_update_ndn);
2529                         return 0;
2530                 }
2531                 return 1;
2532         } else if ( c->op == LDAP_MOD_DELETE ) {
2533                 ch_free( c->be->be_update_ndn.bv_val );
2534                 BER_BVZERO( &c->be->be_update_ndn );
2535                 SLAP_DBFLAGS(c->be) ^= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
2536                 return 0;
2537         }
2538         if(SLAP_SHADOW(c->be)) {
2539                 snprintf( c->msg, sizeof( c->msg ), "<%s> database already shadowed", c->argv[0] );
2540                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2541                         c->log, c->msg, 0);
2542                 return(1);
2543         }
2544
2545         ber_memfree_x( c->value_dn.bv_val, NULL );
2546         if ( !BER_BVISNULL( &c->be->be_update_ndn ) ) {
2547                 ber_memfree_x( c->be->be_update_ndn.bv_val, NULL );
2548         }
2549         c->be->be_update_ndn = c->value_ndn;
2550         BER_BVZERO( &c->value_dn );
2551         BER_BVZERO( &c->value_ndn );
2552
2553         return config_slurp_shadow( c );
2554 }
2555
2556 int
2557 config_shadow( ConfigArgs *c, int flag )
2558 {
2559         char    *notallowed = NULL;
2560
2561         if ( c->be == frontendDB ) {
2562                 notallowed = "frontend";
2563
2564         } else if ( SLAP_MONITOR(c->be) ) {
2565                 notallowed = "monitor";
2566
2567         } else if ( SLAP_CONFIG(c->be) ) {
2568                 notallowed = "config";
2569         }
2570
2571         if ( notallowed != NULL ) {
2572                 Debug( LDAP_DEBUG_ANY, "%s: %s database cannot be shadow.\n", c->log, notallowed, 0 );
2573                 return 1;
2574         }
2575
2576         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | flag);
2577
2578         return 0;
2579 }
2580
2581 static int
2582 config_updateref(ConfigArgs *c) {
2583         struct berval val;
2584         if (c->op == SLAP_CONFIG_EMIT) {
2585                 if ( c->be->be_update_refs ) {
2586                         value_add( &c->rvalue_vals, c->be->be_update_refs );
2587                         return 0;
2588                 } else {
2589                         return 1;
2590                 }
2591         } else if ( c->op == LDAP_MOD_DELETE ) {
2592                 if ( c->valx < 0 ) {
2593                         ber_bvarray_free( c->be->be_update_refs );
2594                         c->be->be_update_refs = NULL;
2595                 } else {
2596                         int i = c->valx;
2597                         ch_free( c->be->be_update_refs[i].bv_val );
2598                         for (; c->be->be_update_refs[i].bv_val; i++)
2599                                 c->be->be_update_refs[i] = c->be->be_update_refs[i+1];
2600                 }
2601                 return 0;
2602         }
2603         if(!SLAP_SHADOW(c->be)) {
2604                 snprintf( c->msg, sizeof( c->msg ), "<%s> must appear after syncrepl or updatedn",
2605                         c->argv[0] );
2606                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2607                         c->log, c->msg, 0);
2608                 return(1);
2609         }
2610
2611         if(validate_global_referral(c->argv[1])) {
2612                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid URL", c->argv[0] );
2613                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2614                         c->log, c->msg, c->argv[1]);
2615                 return(1);
2616         }
2617         ber_str2bv(c->argv[1], 0, 0, &val);
2618         if(value_add_one(&c->be->be_update_refs, &val)) return(LDAP_OTHER);
2619         return(0);
2620 }
2621
2622 static int
2623 config_include(ConfigArgs *c) {
2624         int savelineno = c->lineno;
2625         int rc;
2626         ConfigFile *cf;
2627         ConfigFile *cfsave = cfn;
2628         ConfigFile *cf2 = NULL;
2629         if (c->op == SLAP_CONFIG_EMIT) {
2630                 if (c->private) {
2631                         ConfigFile *cf = c->private;
2632                         value_add_one( &c->rvalue_vals, &cf->c_file );
2633                         return 0;
2634                 }
2635                 return 1;
2636         } else if ( c->op == LDAP_MOD_DELETE ) {
2637         }
2638         cf = ch_calloc( 1, sizeof(ConfigFile));
2639         if ( cfn->c_kids ) {
2640                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
2641                 cf2->c_sibs = cf;
2642         } else {
2643                 cfn->c_kids = cf;
2644         }
2645         cfn = cf;
2646         ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
2647         rc = read_config_file(c->argv[1], c->depth + 1, c, config_back_cf_table);
2648         c->lineno = savelineno - 1;
2649         cfn = cfsave;
2650         if ( rc ) {
2651                 if ( cf2 ) cf2->c_sibs = NULL;
2652                 else cfn->c_kids = NULL;
2653                 ch_free( cf->c_file.bv_val );
2654                 ch_free( cf );
2655         } else {
2656                 c->private = cf;
2657         }
2658         return(rc);
2659 }
2660
2661 #ifdef HAVE_TLS
2662 static int
2663 config_tls_option(ConfigArgs *c) {
2664         int flag;
2665         switch(c->type) {
2666         case CFG_TLS_RAND:      flag = LDAP_OPT_X_TLS_RANDOM_FILE;      break;
2667         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
2668         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
2669         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
2670         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
2671         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
2672         case CFG_TLS_DH_FILE:   flag = LDAP_OPT_X_TLS_DHFILE;   break;
2673         default:                Debug(LDAP_DEBUG_ANY, "%s: "
2674                                         "unknown tls_option <0x%x>\n",
2675                                         c->log, c->type, 0);
2676                 return 1;
2677         }
2678         if (c->op == SLAP_CONFIG_EMIT) {
2679                 return ldap_pvt_tls_get_option( NULL, flag, &c->value_string );
2680         } else if ( c->op == LDAP_MOD_DELETE ) {
2681                 return ldap_pvt_tls_set_option( NULL, flag, NULL );
2682         }
2683         ch_free(c->value_string);
2684         return(ldap_pvt_tls_set_option(NULL, flag, c->argv[1]));
2685 }
2686
2687 /* FIXME: this ought to be provided by libldap */
2688 static int
2689 config_tls_config(ConfigArgs *c) {
2690         int i, flag;
2691         slap_verbmasks crlkeys[] = {
2692                 { BER_BVC("none"),      LDAP_OPT_X_TLS_CRL_NONE },
2693                 { BER_BVC("peer"),      LDAP_OPT_X_TLS_CRL_PEER },
2694                 { BER_BVC("all"),       LDAP_OPT_X_TLS_CRL_ALL },
2695                 { BER_BVNULL, 0 }
2696         };
2697         slap_verbmasks vfykeys[] = {
2698                 { BER_BVC("never"),     LDAP_OPT_X_TLS_NEVER },
2699                 { BER_BVC("demand"),    LDAP_OPT_X_TLS_DEMAND },
2700                 { BER_BVC("try"),       LDAP_OPT_X_TLS_TRY },
2701                 { BER_BVC("hard"),      LDAP_OPT_X_TLS_HARD },
2702                 { BER_BVNULL, 0 }
2703         }, *keys;
2704         switch(c->type) {
2705         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK;         keys = crlkeys; break;
2706         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT;     keys = vfykeys; break;
2707         default:
2708                 Debug(LDAP_DEBUG_ANY, "%s: "
2709                                 "unknown tls_option <0x%x>\n",
2710                                 c->log, c->type, 0);
2711                 return 1;
2712         }
2713         if (c->op == SLAP_CONFIG_EMIT) {
2714                 ldap_pvt_tls_get_option( NULL, flag, &c->value_int );
2715                 for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
2716                         if (keys[i].mask == c->value_int) {
2717                                 c->value_string = ch_strdup( keys[i].word.bv_val );
2718                                 return 0;
2719                         }
2720                 }
2721                 return 1;
2722         } else if ( c->op == LDAP_MOD_DELETE ) {
2723                 int i = 0;
2724                 return ldap_pvt_tls_set_option( NULL, flag, &i );
2725         }
2726         ch_free( c->value_string );
2727         if ( isdigit( (unsigned char)c->argv[1][0] ) ) {
2728                 if ( lutil_atoi( &i, c->argv[1] ) != 0 ) {
2729                         Debug(LDAP_DEBUG_ANY, "%s: "
2730                                 "unable to parse %s \"%s\"\n",
2731                                 c->log, c->argv[0], c->argv[1] );
2732                         return 1;
2733                 }
2734                 return(ldap_pvt_tls_set_option(NULL, flag, &i));
2735         } else {
2736                 return(ldap_int_tls_config(NULL, flag, c->argv[1]));
2737         }
2738 }
2739 #endif
2740
2741 static CfEntryInfo *
2742 config_find_base( CfEntryInfo *root, struct berval *dn, CfEntryInfo **last )
2743 {
2744         struct berval cdn;
2745         char *c;
2746
2747         if ( !root ) {
2748                 *last = NULL;
2749                 return NULL;
2750         }
2751
2752         if ( dn_match( &root->ce_entry->e_nname, dn ))
2753                 return root;
2754
2755         c = dn->bv_val+dn->bv_len;
2756         for (;*c != ',';c--);
2757
2758         while(root) {
2759                 *last = root;
2760                 for (--c;c>dn->bv_val && *c != ',';c--);
2761                 cdn.bv_val = c;
2762                 if ( *c == ',' )
2763                         cdn.bv_val++;
2764                 cdn.bv_len = dn->bv_len - (cdn.bv_val - dn->bv_val);
2765
2766                 root = root->ce_kids;
2767
2768                 for (;root;root=root->ce_sibs) {
2769                         if ( dn_match( &root->ce_entry->e_nname, &cdn )) {
2770                                 if ( cdn.bv_val == dn->bv_val ) {
2771                                         return root;
2772                                 }
2773                                 break;
2774                         }
2775                 }
2776         }
2777         return root;
2778 }
2779
2780 typedef struct setup_cookie {
2781         CfBackInfo *cfb;
2782         ConfigArgs *ca;
2783 } setup_cookie;
2784
2785 static int
2786 config_ldif_resp( Operation *op, SlapReply *rs )
2787 {
2788         if ( rs->sr_type == REP_SEARCH ) {
2789                 setup_cookie *sc = op->o_callback->sc_private;
2790
2791                 sc->cfb->cb_got_ldif = 1;
2792                 rs->sr_err = config_add_internal( sc->cfb, rs->sr_entry, sc->ca, NULL, NULL );
2793                 if ( rs->sr_err != LDAP_SUCCESS ) {
2794                         Debug( LDAP_DEBUG_ANY, "config error processing %s: %s\n",
2795                                 rs->sr_entry->e_name.bv_val, sc->ca->msg, 0 );
2796                 }
2797         }
2798         return rs->sr_err;
2799 }
2800
2801 /* Configure and read the underlying back-ldif store */
2802 static int
2803 config_setup_ldif( BackendDB *be, const char *dir, int readit ) {
2804         CfBackInfo *cfb = be->be_private;
2805         ConfigArgs c = {0};
2806         ConfigTable *ct;
2807         char *argv[3];
2808         int rc = 0;
2809         setup_cookie sc;
2810         slap_callback cb = { NULL, config_ldif_resp, NULL, NULL };
2811         Connection conn = {0};
2812         OperationBuffer opbuf;
2813         Operation *op;
2814         SlapReply rs = {REP_RESULT};
2815         Filter filter = { LDAP_FILTER_PRESENT };
2816         struct berval filterstr = BER_BVC("(objectclass=*)");
2817         struct stat st;
2818
2819         /* Is the config directory available? */
2820         if ( stat( dir, &st ) < 0 ) {
2821                 /* No, so don't bother using the backing store.
2822                  * All changes will be in-memory only.
2823                  */
2824                 return 0;
2825         }
2826                 
2827         cfb->cb_db.bd_info = backend_info( "ldif" );
2828         if ( !cfb->cb_db.bd_info )
2829                 return 0;       /* FIXME: eventually this will be a fatal error */
2830
2831         if ( backend_db_init( "ldif", &cfb->cb_db ) == NULL )
2832                 return 1;
2833
2834         cfb->cb_db.be_suffix = be->be_suffix;
2835         cfb->cb_db.be_nsuffix = be->be_nsuffix;
2836
2837         /* The suffix is always "cn=config". The underlying DB's rootdn
2838          * is always the same as the suffix.
2839          */
2840         cfb->cb_db.be_rootdn = be->be_suffix[0];
2841         cfb->cb_db.be_rootndn = be->be_nsuffix[0];
2842
2843         ber_str2bv( dir, 0, 1, &cfdir );
2844
2845         c.be = &cfb->cb_db;
2846         c.fname = "slapd";
2847         c.argc = 2;
2848         argv[0] = "directory";
2849         argv[1] = (char *)dir;
2850         argv[2] = NULL;
2851         c.argv = argv;
2852
2853         ct = config_find_keyword( c.be->be_cf_ocs->co_table, &c );
2854         if ( !ct )
2855                 return 1;
2856
2857         if ( config_add_vals( ct, &c ))
2858                 return 1;
2859
2860         if ( backend_startup_one( &cfb->cb_db ))
2861                 return 1;
2862
2863         if ( readit ) {
2864                 void *thrctx = ldap_pvt_thread_pool_context();
2865
2866                 op = (Operation *) &opbuf;
2867                 connection_fake_init( &conn, op, thrctx );
2868
2869                 filter.f_desc = slap_schema.si_ad_objectClass;
2870
2871                 op->o_tag = LDAP_REQ_SEARCH;
2872
2873                 op->ors_filter = &filter;
2874                 op->ors_filterstr = filterstr;
2875                 op->ors_scope = LDAP_SCOPE_SUBTREE;
2876
2877                 op->o_dn = c.be->be_rootdn;
2878                 op->o_ndn = c.be->be_rootndn;
2879
2880                 op->o_req_dn = be->be_suffix[0];
2881                 op->o_req_ndn = be->be_nsuffix[0];
2882
2883                 op->ors_tlimit = SLAP_NO_LIMIT;
2884                 op->ors_slimit = SLAP_NO_LIMIT;
2885
2886                 op->ors_attrs = slap_anlist_all_attributes;
2887                 op->ors_attrsonly = 0;
2888
2889                 op->o_callback = &cb;
2890                 sc.cfb = cfb;
2891                 sc.ca = &c;
2892                 cb.sc_private = &sc;
2893
2894                 op->o_bd = &cfb->cb_db;
2895                 rc = op->o_bd->be_search( op, &rs );
2896
2897                 ldap_pvt_thread_pool_context_reset( thrctx );
2898         }
2899
2900         /* ITS#4194 - only use if it's present, or we're converting. */
2901         if ( !readit || rc == LDAP_SUCCESS )
2902                 cfb->cb_use_ldif = 1;
2903
2904         return rc;
2905 }
2906
2907 static int
2908 CfOc_cmp( const void *c1, const void *c2 ) {
2909         const ConfigOCs *co1 = c1;
2910         const ConfigOCs *co2 = c2;
2911
2912         return ber_bvcmp( co1->co_name, co2->co_name );
2913 }
2914
2915 int
2916 config_register_schema(ConfigTable *ct, ConfigOCs *ocs) {
2917         int i;
2918
2919         i = init_config_attrs( ct );
2920         if ( i ) return i;
2921
2922         /* set up the objectclasses */
2923         i = init_config_ocs( ocs );
2924         if ( i ) return i;
2925
2926         for (i=0; ocs[i].co_def; i++) {
2927                 if ( ocs[i].co_oc ) {
2928                         ocs[i].co_name = &ocs[i].co_oc->soc_cname;
2929                         if ( !ocs[i].co_table )
2930                                 ocs[i].co_table = ct;
2931                         avl_insert( &CfOcTree, &ocs[i], CfOc_cmp, avl_dup_error );
2932                 }
2933         }
2934         return 0;
2935 }
2936
2937 int
2938 read_config(const char *fname, const char *dir) {
2939         BackendDB *be;
2940         CfBackInfo *cfb;
2941         const char *cfdir, *cfname;
2942         int rc;
2943
2944         /* Setup the config backend */
2945         be = backend_db_init( "config", NULL );
2946         if ( !be )
2947                 return 1;
2948
2949         cfb = be->be_private;
2950
2951         /* If no .conf, or a dir was specified, setup the dir */
2952         if ( !fname || dir ) {
2953                 if ( dir ) {
2954                         /* If explicitly given, check for existence */
2955                         struct stat st;
2956
2957                         if ( stat( dir, &st ) < 0 ) {
2958                                 Debug( LDAP_DEBUG_ANY,
2959                                         "invalid config directory %s, error %d\n",
2960                                                 dir, errno, 0 );
2961                                 return 1;
2962                         }
2963                         cfdir = dir;
2964                 } else {
2965                         cfdir = SLAPD_DEFAULT_CONFIGDIR;
2966                 }
2967                 /* if fname is defaulted, try reading .d */
2968                 rc = config_setup_ldif( be, cfdir, !fname );
2969
2970                 if ( rc ) {
2971                         /* It may be OK if the base object doesn't exist yet. */
2972                         if ( rc != LDAP_NO_SUCH_OBJECT )
2973                                 return 1;
2974                         /* ITS#4194: But if dir was specified and no fname,
2975                          * then we were supposed to read the dir.
2976                          */
2977                         if ( dir && !fname )
2978                                 return 1;
2979                 }
2980
2981                 /* If we read the config from back-ldif, nothing to do here */
2982                 if ( cfb->cb_got_ldif ) {
2983                         rc = 0;
2984                         goto done;
2985                 }
2986         }
2987
2988         if ( fname )
2989                 cfname = fname;
2990         else
2991                 cfname = SLAPD_DEFAULT_CONFIGFILE;
2992
2993         rc = read_config_file(cfname, 0, NULL, config_back_cf_table);
2994
2995         if ( rc == 0 )
2996                 ber_str2bv( cfname, 0, 1, &cfb->cb_config->c_file );
2997
2998         /* If we got this far and failed, it may be a serious problem. In server
2999          * mode, we should never come to this. However, it may be alright if we're
3000          * using slapadd to create the conf dir.
3001          */
3002         while ( rc ) {
3003                 if ( slapMode & (SLAP_SERVER_MODE|SLAP_TOOL_READMAIN|SLAP_TOOL_READONLY))
3004                         break;
3005                 /* If a config file was explicitly given, fail */
3006                 if ( fname )
3007                         break;
3008                 
3009                 /* Seems to be slapadd with a config dir, let it continue */
3010                 if ( cfb->cb_use_ldif ) {
3011                         rc = 0;
3012                         cfb->cb_got_ldif = 1;
3013                 }
3014                 break;
3015         }
3016
3017 done:
3018         if ( rc == 0 && BER_BVISNULL( &frontendDB->be_schemadn ) ) {
3019                 ber_str2bv( SLAPD_SCHEMA_DN, STRLENOF( SLAPD_SCHEMA_DN ), 1,
3020                         &frontendDB->be_schemadn );
3021                 rc = dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
3022                 if ( rc != LDAP_SUCCESS ) {
3023                         Debug(LDAP_DEBUG_ANY, "read_config: "
3024                                 "unable to normalize default schema DN \"%s\"\n",
3025                                 frontendDB->be_schemadn.bv_val, 0, 0 );
3026                         /* must not happen */
3027                         assert( 0 );
3028                 }
3029         }
3030         return rc;
3031 }
3032
3033 static int
3034 config_back_bind( Operation *op, SlapReply *rs )
3035 {
3036         if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op )) {
3037                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ));
3038                 /* frontend sends result */
3039                 return LDAP_SUCCESS;
3040         }
3041
3042         rs->sr_err = LDAP_INVALID_CREDENTIALS;
3043         send_ldap_result( op, rs );
3044
3045         return rs->sr_err;
3046 }
3047
3048 static int
3049 config_send( Operation *op, SlapReply *rs, CfEntryInfo *ce, int depth )
3050 {
3051         int rc = 0;
3052
3053         if ( test_filter( op, ce->ce_entry, op->ors_filter ) == LDAP_COMPARE_TRUE )
3054         {
3055                 rs->sr_attrs = op->ors_attrs;
3056                 rs->sr_entry = ce->ce_entry;
3057                 rs->sr_flags = 0;
3058                 rc = send_search_entry( op, rs );
3059         }
3060         if ( op->ors_scope == LDAP_SCOPE_SUBTREE ) {
3061                 if ( ce->ce_kids ) {
3062                         rc = config_send( op, rs, ce->ce_kids, 1 );
3063                         if ( rc ) return rc;
3064                 }
3065                 if ( depth ) {
3066                         for (ce=ce->ce_sibs; ce; ce=ce->ce_sibs) {
3067                                 rc = config_send( op, rs, ce, 0 );
3068                                 if ( rc ) break;
3069                         }
3070                 }
3071         }
3072         return rc;
3073 }
3074
3075 static ConfigTable *
3076 config_find_table( ConfigOCs **colst, int nocs, AttributeDescription *ad )
3077 {
3078         int i, j;
3079
3080         for (j=0; j<nocs; j++) {
3081                 for (i=0; colst[j]->co_table[i].name; i++)
3082                         if ( colst[j]->co_table[i].ad == ad )
3083                                 return &colst[j]->co_table[i];
3084         }
3085         return NULL;
3086 }
3087
3088 /* Sort the attributes of the entry according to the order defined
3089  * in the objectclass, with required attributes occurring before
3090  * allowed attributes. For any attributes with sequencing dependencies
3091  * (e.g., rootDN must be defined after suffix) the objectclass must
3092  * list the attributes in the desired sequence.
3093  */
3094 static void
3095 sort_attrs( Entry *e, ConfigOCs **colst, int nocs )
3096 {
3097         Attribute *a, *head = NULL, *tail = NULL, **prev;
3098         int i, j;
3099
3100         for (i=0; i<nocs; i++) {
3101                 if ( colst[i]->co_oc->soc_required ) {
3102                         AttributeType **at = colst[i]->co_oc->soc_required;
3103                         for (j=0; at[j]; j++) {
3104                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
3105                                         prev = &(*prev)->a_next, a=a->a_next) {
3106                                         if ( a->a_desc == at[j]->sat_ad ) {
3107                                                 *prev = a->a_next;
3108                                                 if (!head) {
3109                                                         head = a;
3110                                                         tail = a;
3111                                                 } else {
3112                                                         tail->a_next = a;
3113                                                         tail = a;
3114                                                 }
3115                                                 break;
3116                                         }
3117                                 }
3118                         }
3119                 }
3120                 if ( colst[i]->co_oc->soc_allowed ) {
3121                         AttributeType **at = colst[i]->co_oc->soc_allowed;
3122                         for (j=0; at[j]; j++) {
3123                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
3124                                         prev = &(*prev)->a_next, a=a->a_next) {
3125                                         if ( a->a_desc == at[j]->sat_ad ) {
3126                                                 *prev = a->a_next;
3127                                                 if (!head) {
3128                                                         head = a;
3129                                                         tail = a;
3130                                                 } else {
3131                                                         tail->a_next = a;
3132                                                         tail = a;
3133                                                 }
3134                                                 break;
3135                                         }
3136                                 }
3137                         }
3138                 }
3139         }
3140         if ( tail ) {
3141                 tail->a_next = e->e_attrs;
3142                 e->e_attrs = head;
3143         }
3144 }
3145
3146 static int
3147 check_vals( ConfigTable *ct, ConfigArgs *ca, void *ptr, int isAttr )
3148 {
3149         Attribute *a = NULL;
3150         AttributeDescription *ad;
3151         BerVarray vals;
3152
3153         int i, rc = 0, sort = 0;
3154
3155         if ( isAttr ) {
3156                 a = ptr;
3157                 ad = a->a_desc;
3158                 vals = a->a_vals;
3159         } else {
3160                 Modifications *ml = ptr;
3161                 ad = ml->sml_desc;
3162                 vals = ml->sml_values;
3163         }
3164
3165         if ( a && ( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL )) {
3166                 sort = 1;
3167                 rc = ordered_value_sort( a, 1 );
3168                 if ( rc ) {
3169                         snprintf(ca->msg, sizeof( ca->msg ), "ordered_value_sort failed on attr %s\n",
3170                                 ad->ad_cname.bv_val );
3171                         return rc;
3172                 }
3173         }
3174         for ( i=0; vals[i].bv_val; i++ ) {
3175                 ca->line = vals[i].bv_val;
3176                 if ( sort ) {
3177                         char *idx = strchr( ca->line, '}' );
3178                         if ( idx ) ca->line = idx+1;
3179                 }
3180                 rc = config_parse_vals( ct, ca, i );
3181                 if ( rc ) {
3182                         break;
3183                 }
3184         }
3185         return rc;
3186 }
3187
3188 static int
3189 check_name_index( CfEntryInfo *parent, ConfigType ce_type, Entry *e,
3190         SlapReply *rs, int *renum )
3191 {
3192         CfEntryInfo *ce;
3193         int index = -1, gotindex = 0, nsibs;
3194         int renumber = 0, tailindex = 0;
3195         char *ptr1, *ptr2 = NULL;
3196         struct berval rdn;
3197
3198         if ( renum ) *renum = 0;
3199
3200         /* These entries don't get indexed/renumbered */
3201         if ( ce_type == Cft_Global ) return 0;
3202         if ( ce_type == Cft_Schema && parent->ce_type == Cft_Global ) return 0;
3203
3204         if ( ce_type == Cft_Include || ce_type == Cft_Module )
3205                 tailindex = 1;
3206
3207         /* See if the rdn has an index already */
3208         dnRdn( &e->e_name, &rdn );
3209         ptr1 = ber_bvchr( &e->e_name, '{' );
3210         if ( ptr1 && ptr1 - e->e_name.bv_val < rdn.bv_len ) {
3211                 char    *next;
3212                 ptr2 = strchr( ptr1, '}' );
3213                 if (!ptr2 || ptr2 - e->e_name.bv_val > rdn.bv_len)
3214                         return LDAP_NAMING_VIOLATION;
3215                 if ( ptr2-ptr1 == 1)
3216                         return LDAP_NAMING_VIOLATION;
3217                 gotindex = 1;
3218                 index = strtol( ptr1 + 1, &next, 10 );
3219                 if ( next == ptr1 + 1 || next[ 0 ] != '}' ) {
3220                         return LDAP_NAMING_VIOLATION;
3221                 }
3222                 if ( index < 0 ) {
3223                         /* Special case, we allow -1 for the frontendDB */
3224                         if ( index != -1 || ce_type != Cft_Database ||
3225                                 strncmp( ptr2+1, "frontend,", STRLENOF("frontend,") ))
3226
3227                                 return LDAP_NAMING_VIOLATION;
3228                 }
3229         }
3230
3231         /* count related kids */
3232         for (nsibs=0, ce=parent->ce_kids; ce; ce=ce->ce_sibs) {
3233                 if ( ce->ce_type == ce_type ) nsibs++;
3234         }
3235
3236         if ( index != nsibs ) {
3237                 if ( gotindex ) {
3238                         if ( index < nsibs ) {
3239                                 if ( tailindex ) return LDAP_NAMING_VIOLATION;
3240                                 /* Siblings need to be renumbered */
3241                                 renumber = 1;
3242                         }
3243                 }
3244                 if ( !renumber ) {
3245                         struct berval ival, newrdn, nnewrdn;
3246                         struct berval rtype, rval;
3247                         Attribute *a;
3248                         AttributeDescription *ad = NULL;
3249                         char ibuf[32];
3250                         const char *text;
3251
3252                         rval.bv_val = strchr(rdn.bv_val, '=' ) + 1;
3253                         rval.bv_len = rdn.bv_len - (rval.bv_val - rdn.bv_val);
3254                         rtype.bv_val = rdn.bv_val;
3255                         rtype.bv_len = rval.bv_val - rtype.bv_val - 1;
3256
3257                         /* Find attr */
3258                         slap_bv2ad( &rtype, &ad, &text );
3259                         a = attr_find( e->e_attrs, ad );
3260                         if (!a ) return LDAP_NAMING_VIOLATION;
3261
3262                         ival.bv_val = ibuf;
3263                         ival.bv_len = snprintf( ibuf, sizeof( ibuf ), SLAP_X_ORDERED_FMT, nsibs );
3264                         if ( ival.bv_len >= sizeof( ibuf ) ) {
3265                                 return LDAP_NAMING_VIOLATION;
3266                         }
3267                         
3268                         newrdn.bv_len = rdn.bv_len + ival.bv_len;
3269                         newrdn.bv_val = ch_malloc( newrdn.bv_len+1 );
3270
3271                         if ( tailindex ) {
3272                                 ptr1 = lutil_strncopy( newrdn.bv_val, rdn.bv_val, rdn.bv_len );
3273                                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
3274                         } else {
3275                                 int xlen;
3276                                 if ( !gotindex ) {
3277                                         ptr2 = rval.bv_val;
3278                                         xlen = rval.bv_len;
3279                                 } else {
3280                                         xlen = rdn.bv_len - (ptr2 - rdn.bv_val);
3281                                 }
3282                                 ptr1 = lutil_strncopy( newrdn.bv_val, rtype.bv_val,
3283                                         rtype.bv_len );
3284                                 *ptr1++ = '=';
3285                                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
3286                                 ptr1 = lutil_strncopy( ptr1, ptr2, xlen );
3287                                 *ptr1 = '\0';
3288                         }
3289
3290                         /* Do the equivalent of ModRDN */
3291                         /* Replace DN / NDN */
3292                         newrdn.bv_len = ptr1 - newrdn.bv_val;
3293                         rdnNormalize( 0, NULL, NULL, &newrdn, &nnewrdn, NULL );
3294                         free( e->e_name.bv_val );
3295                         build_new_dn( &e->e_name, &parent->ce_entry->e_name,
3296                                 &newrdn, NULL );
3297                         free( e->e_nname.bv_val );
3298                         build_new_dn( &e->e_nname, &parent->ce_entry->e_nname,
3299                                 &nnewrdn, NULL );
3300
3301                         /* Replace attr */
3302                         free( a->a_vals[0].bv_val );
3303                         ptr1 = strchr( newrdn.bv_val, '=' ) + 1;
3304                         a->a_vals[0].bv_len = newrdn.bv_len - (ptr1 - newrdn.bv_val);
3305                         a->a_vals[0].bv_val = ch_malloc( a->a_vals[0].bv_len + 1 );
3306                         strcpy( a->a_vals[0].bv_val, ptr1 );
3307
3308                         if ( a->a_nvals != a->a_vals ) {
3309                                 free( a->a_nvals[0].bv_val );
3310                                 ptr1 = strchr( nnewrdn.bv_val, '=' ) + 1;
3311                                 a->a_nvals[0].bv_len = nnewrdn.bv_len - (ptr1 - nnewrdn.bv_val);
3312                                 a->a_nvals[0].bv_val = ch_malloc( a->a_nvals[0].bv_len + 1 );
3313                                 strcpy( a->a_nvals[0].bv_val, ptr1 );
3314                         }
3315                         free( nnewrdn.bv_val );
3316                         free( newrdn.bv_val );
3317                 }
3318         }
3319         if ( renum ) *renum = renumber;
3320         return 0;
3321 }
3322
3323 static ConfigOCs **
3324 count_ocs( Attribute *oc_at, int *nocs )
3325 {
3326         int i, j, n;
3327         ConfigOCs co, *coptr, **colst;
3328
3329         /* count the objectclasses */
3330         for ( i=0; oc_at->a_nvals[i].bv_val; i++ );
3331         n = i;
3332         colst = (ConfigOCs **)ch_malloc( n * sizeof(ConfigOCs *));
3333
3334         for ( i=0, j=0; i<n; i++) {
3335                 co.co_name = &oc_at->a_nvals[i];
3336                 coptr = avl_find( CfOcTree, &co, CfOc_cmp );
3337                 
3338                 /* ignore non-config objectclasses. probably should be
3339                  * an error, general data doesn't belong here.
3340                  */
3341                 if ( !coptr ) continue;
3342
3343                 /* Ignore the root objectclass, it has no implementation.
3344                  */
3345                 if ( coptr->co_type == Cft_Abstract ) continue;
3346                 colst[j++] = coptr;
3347         }
3348         *nocs = j;
3349         return colst;
3350 }
3351
3352 static int
3353 cfAddInclude( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
3354 {
3355         if ( p->ce_type != Cft_Global && p->ce_type != Cft_Include )
3356                 return LDAP_CONSTRAINT_VIOLATION;
3357
3358         /* If we're reading from a configdir, don't parse this entry */
3359         if ( ca->lineno )
3360                 return LDAP_COMPARE_TRUE;
3361
3362         cfn = p->ce_private;
3363         ca->private = cfn;
3364         return LDAP_SUCCESS;
3365 }
3366
3367 static int
3368 cfAddSchema( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
3369 {
3370         ConfigFile *cfo;
3371
3372         /* This entry is hardcoded, don't re-parse it */
3373         if ( p->ce_type == Cft_Global ) {
3374                 cfn = p->ce_private;
3375                 ca->private = cfn;
3376                 return LDAP_COMPARE_TRUE;
3377         }
3378         if ( p->ce_type != Cft_Schema )
3379                 return LDAP_CONSTRAINT_VIOLATION;
3380
3381         cfn = ch_calloc( 1, sizeof(ConfigFile) );
3382         ca->private = cfn;
3383         cfo = p->ce_private;
3384         cfn->c_sibs = cfo->c_kids;
3385         cfo->c_kids = cfn;
3386         return LDAP_SUCCESS;
3387 }
3388
3389 static int
3390 cfAddDatabase( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
3391 {
3392         if ( p->ce_type != Cft_Global )
3393                 return LDAP_CONSTRAINT_VIOLATION;
3394         ca->be = frontendDB;    /* just to get past check_vals */
3395         return LDAP_SUCCESS;
3396 }
3397
3398 static int
3399 cfAddBackend( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
3400 {
3401         if ( p->ce_type != Cft_Global )
3402                 return LDAP_CONSTRAINT_VIOLATION;
3403         return LDAP_SUCCESS;
3404 }
3405
3406 static int
3407 cfAddModule( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
3408 {
3409         if ( p->ce_type != Cft_Global )
3410                 return LDAP_CONSTRAINT_VIOLATION;
3411         return LDAP_SUCCESS;
3412 }
3413
3414 static int
3415 cfAddOverlay( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
3416 {
3417         if ( p->ce_type != Cft_Database )
3418                 return LDAP_CONSTRAINT_VIOLATION;
3419         ca->be = p->ce_be;
3420         return LDAP_SUCCESS;
3421 }
3422
3423 /* Parse an LDAP entry into config directives */
3424 static int
3425 config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca, SlapReply *rs, int *renum )
3426 {
3427         CfEntryInfo *ce, *last;
3428         ConfigOCs **colst;
3429         Attribute *a, *oc_at;
3430         int i, nocs, rc = 0;
3431         struct berval pdn;
3432         ConfigTable *ct;
3433         char *ptr;
3434
3435         /* Make sure parent exists and entry does not */
3436         ce = config_find_base( cfb->cb_root, &e->e_nname, &last );
3437         if ( ce )
3438                 return LDAP_ALREADY_EXISTS;
3439
3440         dnParent( &e->e_nname, &pdn );
3441
3442         /* If last is NULL, the new entry is the root/suffix entry, 
3443          * otherwise last should be the parent.
3444          */
3445         if ( last && !dn_match( &last->ce_entry->e_nname, &pdn )) {
3446                 if ( rs )
3447                         rs->sr_matched = last->ce_entry->e_name.bv_val;
3448                 return LDAP_NO_SUCH_OBJECT;
3449         }
3450
3451         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
3452         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
3453
3454         memset( ca, 0, sizeof(ConfigArgs));
3455
3456         /* Fake the coordinates based on whether we're part of an
3457          * LDAP Add or if reading the config dir
3458          */
3459         if ( rs ) {
3460                 ca->fname = "slapd";
3461                 ca->lineno = 0;
3462         } else {
3463                 ca->fname = cfdir.bv_val;
3464                 ca->lineno = 1;
3465         }
3466
3467         colst = count_ocs( oc_at, &nocs );
3468
3469         /* Only the root can be Cft_Global, everything else must
3470          * have a parent. Only limited nesting arrangements are allowed.
3471          */
3472         rc = LDAP_CONSTRAINT_VIOLATION;
3473         if ( colst[0]->co_type == Cft_Global && !last ) {
3474                 cfn = cfb->cb_config;
3475                 ca->private = cfn;
3476                 ca->be = frontendDB;    /* just to get past check_vals */
3477                 rc = LDAP_SUCCESS;
3478         }
3479
3480         /* Check whether the Add is allowed by its parent, and do
3481          * any necessary arg setup
3482          */
3483         if ( last ) {
3484                 for ( i=0; i<nocs; i++ ) {
3485                         if ( colst[i]->co_ldadd &&
3486                                 ( rc = colst[i]->co_ldadd( last, e, ca ))
3487                                         != LDAP_CONSTRAINT_VIOLATION ) {
3488                                 break;
3489                         }
3490                 }
3491         }
3492
3493         /* Add the entry but don't parse it, we already have its contents */
3494         if ( rc == LDAP_COMPARE_TRUE ) {
3495                 rc = LDAP_SUCCESS;
3496                 goto ok;
3497         }
3498
3499         if ( rc != LDAP_SUCCESS )
3500                 goto done;
3501
3502         /* Parse all the values and check for simple syntax errors before
3503          * performing any set actions.
3504          *
3505          * If doing an LDAPadd, check for indexed names and any necessary
3506          * renaming/renumbering. Entries that don't need indexed names are
3507          * ignored. Entries that need an indexed name and arrive without one
3508          * are assigned to the end. Entries that arrive with an index may
3509          * cause the following entries to be renumbered/bumped down.
3510          *
3511          * Note that "pseudo-indexed" entries (cn=Include{xx}, cn=Module{xx})
3512          * don't allow Adding an entry with an index that's already in use.
3513          * This is flagged as an error (LDAP_ALREADY_EXISTS) up above.
3514          *
3515          * These entries can have auto-assigned indexes (appended to the end)
3516          * but only the other types support auto-renumbering of siblings.
3517          */
3518         rc = check_name_index( last, colst[0]->co_type, e, rs, renum );
3519         if ( rc )
3520                 goto done;
3521
3522         init_config_argv( ca );
3523
3524         /* Make sure we process attrs in the required order */
3525         sort_attrs( e, colst, nocs );
3526
3527         for ( a=e->e_attrs; a; a=a->a_next ) {
3528                 if ( a == oc_at ) continue;
3529                 ct = config_find_table( colst, nocs, a->a_desc );
3530                 if ( !ct ) continue;    /* user data? */
3531                 rc = check_vals( ct, ca, a, 1 );
3532                 if ( rc ) goto done;
3533         }
3534
3535         /* Basic syntax checks are OK. Do the actual settings. */
3536         for ( a=e->e_attrs; a; a=a->a_next ) {
3537                 if ( a == oc_at ) continue;
3538                 ct = config_find_table( colst, nocs, a->a_desc );
3539                 if ( !ct ) continue;    /* user data? */
3540                 for (i=0; a->a_vals[i].bv_val; i++) {
3541                         ca->line = a->a_vals[i].bv_val;
3542                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
3543                                 ptr = strchr( ca->line, '}' );
3544                                 if ( ptr ) ca->line = ptr+1;
3545                         }
3546                         ca->valx = i;
3547                         rc = config_parse_add( ct, ca );
3548                         if ( rc ) {
3549                                 rc = LDAP_OTHER;
3550                                 goto done;
3551                         }
3552                 }
3553         }
3554 ok:
3555         /* Newly added databases and overlays need to be started up */
3556         if ( CONFIG_ONLINE_ADD( ca )) {
3557                 if ( colst[0]->co_type == Cft_Database ) {
3558                         rc = backend_startup_one( ca->be );
3559
3560                 } else if ( colst[0]->co_type == Cft_Overlay ) {
3561                         if ( ca->bi->bi_db_open ) {
3562                                 BackendInfo *bi_orig = ca->be->bd_info;
3563                                 ca->be->bd_info = ca->bi;
3564                                 rc = ca->bi->bi_db_open( ca->be );
3565                                 ca->be->bd_info = bi_orig;
3566                         }
3567                 }
3568                 if ( rc ) {
3569                         snprintf( ca->msg, sizeof( ca->msg ), "<%s> failed startup", ca->argv[0] );
3570                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
3571                                 ca->log, ca->msg, ca->argv[1] );
3572                         rc = LDAP_OTHER;
3573                         goto done;
3574                 }
3575         }
3576
3577         ce = ch_calloc( 1, sizeof(CfEntryInfo) );
3578         ce->ce_parent = last;
3579         ce->ce_entry = entry_dup( e );
3580         ce->ce_entry->e_private = ce;
3581         ce->ce_type = colst[0]->co_type;
3582         ce->ce_be = ca->be;
3583         ce->ce_bi = ca->bi;
3584         ce->ce_private = ca->private;
3585         if ( !last ) {
3586                 cfb->cb_root = ce;
3587         } else if ( last->ce_kids ) {
3588                 CfEntryInfo *c2;
3589
3590                 for (c2=last->ce_kids; c2 && c2->ce_sibs; c2 = c2->ce_sibs);
3591
3592                 c2->ce_sibs = ce;
3593         } else {
3594                 last->ce_kids = ce;
3595         }
3596
3597 done:
3598         if ( rc ) {
3599                 if ( (colst[0]->co_type == Cft_Database) && ca->be ) {
3600                         if ( ca->be != frontendDB )
3601                                 backend_destroy_one( ca->be, 1 );
3602                 } else if ( (colst[0]->co_type == Cft_Overlay) && ca->bi ) {
3603                         overlay_destroy_one( ca->be, (slap_overinst *)ca->bi );
3604                 }
3605         }
3606
3607         ch_free( ca->argv );
3608         if ( colst ) ch_free( colst );
3609         return rc;
3610 }
3611
3612 /* Parse an LDAP entry into config directives, then store in underlying
3613  * database.
3614  */
3615 static int
3616 config_back_add( Operation *op, SlapReply *rs )
3617 {
3618         CfBackInfo *cfb;
3619         int renumber;
3620         ConfigArgs ca;
3621
3622         if ( !be_isroot( op ) ) {
3623                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3624                 goto out;
3625         }
3626
3627         cfb = (CfBackInfo *)op->o_bd->be_private;
3628
3629         ldap_pvt_thread_pool_pause( &connection_pool );
3630
3631         /* Strategy:
3632          * 1) check for existence of entry
3633          * 2) check for sibling renumbering
3634          * 3) perform internal add
3635          * 4) store entry in underlying database
3636          * 5) perform any necessary renumbering
3637          */
3638         rs->sr_err = config_add_internal( cfb, op->ora_e, &ca, rs, &renumber );
3639         if ( rs->sr_err != LDAP_SUCCESS ) {
3640                 rs->sr_text = ca.msg;
3641         } else if ( cfb->cb_use_ldif ) {
3642                 BackendDB *be = op->o_bd;
3643                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL };
3644                 struct berval dn, ndn;
3645
3646                 op->o_bd = &cfb->cb_db;
3647
3648                 /* Save current rootdn; use the underlying DB's rootdn */
3649                 dn = op->o_dn;
3650                 ndn = op->o_ndn;
3651                 op->o_dn = op->o_bd->be_rootdn;
3652                 op->o_ndn = op->o_bd->be_rootndn;
3653
3654                 sc.sc_next = op->o_callback;
3655                 op->o_callback = &sc;
3656                 op->o_bd->be_add( op, rs );
3657                 op->o_bd = be;
3658                 op->o_callback = sc.sc_next;
3659                 op->o_dn = dn;
3660                 op->o_ndn = ndn;
3661         }
3662         if ( renumber ) {
3663         }
3664
3665         ldap_pvt_thread_pool_resume( &connection_pool );
3666
3667 out:
3668         send_ldap_result( op, rs );
3669         return rs->sr_err;
3670 }
3671
3672 typedef struct delrec {
3673         struct delrec *next;
3674         int nidx;
3675         int idx[1];
3676 } delrec;
3677
3678 static int
3679 config_modify_internal( CfEntryInfo *ce, Operation *op, SlapReply *rs,
3680         ConfigArgs *ca )
3681 {
3682         int rc = LDAP_UNWILLING_TO_PERFORM;
3683         Modifications *ml;
3684         Entry *e = ce->ce_entry;
3685         Attribute *save_attrs = e->e_attrs, *oc_at;
3686         ConfigTable *ct;
3687         ConfigOCs **colst;
3688         int i, nocs;
3689         char *ptr;
3690         delrec *dels = NULL, *deltail = NULL;
3691
3692         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
3693         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
3694
3695         colst = count_ocs( oc_at, &nocs );
3696
3697         e->e_attrs = attrs_dup( e->e_attrs );
3698
3699         init_config_argv( ca );
3700         ca->be = ce->ce_be;
3701         ca->bi = ce->ce_bi;
3702         ca->private = ce->ce_private;
3703         ca->ca_entry = e;
3704         ca->fname = "slapd";
3705         strcpy( ca->log, "back-config" );
3706
3707         for (ml = op->orm_modlist; ml; ml=ml->sml_next) {
3708                 ct = config_find_table( colst, nocs, ml->sml_desc );
3709                 switch (ml->sml_op) {
3710                 case LDAP_MOD_DELETE:
3711                 case LDAP_MOD_REPLACE: {
3712                         BerVarray vals = NULL, nvals = NULL;
3713                         int *idx = NULL;
3714                         if ( ct && ( ct->arg_type & ARG_NO_DELETE )) {
3715                                 rc = LDAP_OTHER;
3716                                 snprintf(ca->msg, sizeof(ca->msg), "cannot delete %s",
3717                                         ml->sml_desc->ad_cname.bv_val );
3718                                 goto out;
3719                         }
3720                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
3721                                 vals = ml->sml_values;
3722                                 nvals = ml->sml_nvalues;
3723                                 ml->sml_values = NULL;
3724                                 ml->sml_nvalues = NULL;
3725                         }
3726                         /* If we're deleting by values, remember the indexes of the
3727                          * values we deleted.
3728                          */
3729                         if ( ct && ml->sml_values ) {
3730                                 delrec *d;
3731                                 for (i=0; ml->sml_values[i].bv_val; i++);
3732                                 d = ch_malloc( sizeof(delrec) + (i - 1)* sizeof(int));
3733                                 d->nidx = i;
3734                                 d->next = NULL;
3735                                 if ( dels ) {
3736                                         deltail->next = d;
3737                                 } else {
3738                                         dels = d;
3739                                 }
3740                                 deltail = d;
3741                                 idx = d->idx;
3742                         }
3743                         rc = modify_delete_vindex(e, &ml->sml_mod,
3744                                 get_permissiveModify(op),
3745                                 &rs->sr_text, ca->msg, sizeof(ca->msg), idx );
3746                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
3747                                 ml->sml_values = vals;
3748                                 ml->sml_nvalues = nvals;
3749                         }
3750                         if ( !vals )
3751                                 break;
3752                         }
3753                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
3754
3755                 case LDAP_MOD_ADD:
3756                 case SLAP_MOD_SOFTADD: {
3757                         int mop = ml->sml_op;
3758                         int navals = -1;
3759                         ml->sml_op = LDAP_MOD_ADD;
3760                         if ( ct ) {
3761                                 if ( ct->arg_type & ARG_NO_INSERT ) {
3762                                         Attribute *a = attr_find( e->e_attrs, ml->sml_desc );
3763                                         if ( a ) {
3764                                                 for (i = 0; a->a_vals[i].bv_val; i++ );
3765                                                 navals = i;
3766                                         }
3767                                 }
3768                                 for ( i=0; !BER_BVISNULL( &ml->sml_values[i] ); i++ ) {
3769                                         if ( ml->sml_values[i].bv_val[0] == '{' &&
3770                                                 navals >= 0 )
3771                                         {
3772                                                 char    *next, *val = ml->sml_values[i].bv_val + 1;
3773                                                 int     j;
3774
3775                                                 j = strtol( val, &next, 0 );
3776                                                 if ( next == val || next[ 0 ] != '}' || j < navals ) {
3777                                                         rc = LDAP_OTHER;
3778                                                         snprintf(ca->msg, sizeof(ca->msg), "cannot insert %s",
3779                                                                 ml->sml_desc->ad_cname.bv_val );
3780                                                         goto out;
3781                                                 }
3782                                         }
3783                                         rc = check_vals( ct, ca, ml, 0 );
3784                                         if ( rc ) goto out;
3785                                 }
3786                         }
3787                         rc = modify_add_values(e, &ml->sml_mod,
3788                                    get_permissiveModify(op),
3789                                    &rs->sr_text, ca->msg, sizeof(ca->msg) );
3790
3791                         /* If value already exists, show success here
3792                          * and ignore this operation down below.
3793                          */
3794                         if ( mop == SLAP_MOD_SOFTADD ) {
3795                                 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS )
3796                                         rc = LDAP_SUCCESS;
3797                                 else
3798                                         mop = LDAP_MOD_ADD;
3799                         }
3800                         ml->sml_op = mop;
3801                         break;
3802                         }
3803
3804                         break;
3805                 case LDAP_MOD_INCREMENT:        /* FIXME */
3806                         break;
3807                 default:
3808                         break;
3809                 }
3810                 if(rc != LDAP_SUCCESS) break;
3811         }
3812         
3813         if(rc == LDAP_SUCCESS) {
3814                 /* check that the entry still obeys the schema */
3815                 rc = entry_schema_check(op, e, NULL, 0,
3816                         &rs->sr_text, ca->msg, sizeof(ca->msg) );
3817         }
3818         if ( rc == LDAP_SUCCESS ) {
3819                 /* Basic syntax checks are OK. Do the actual settings. */
3820                 for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
3821                         ct = config_find_table( colst, nocs, ml->sml_desc );
3822                         if ( !ct ) continue;
3823
3824                         switch (ml->sml_op) {
3825                         case LDAP_MOD_DELETE:
3826                         case LDAP_MOD_REPLACE: {
3827                                 BerVarray vals = NULL, nvals = NULL;
3828                                 Attribute *a;
3829                                 delrec *d = NULL;
3830
3831                                 a = attr_find( e->e_attrs, ml->sml_desc );
3832
3833                                 if ( ml->sml_op == LDAP_MOD_REPLACE ) {
3834                                         vals = ml->sml_values;
3835                                         nvals = ml->sml_nvalues;
3836                                         ml->sml_values = NULL;
3837                                         ml->sml_nvalues = NULL;
3838                                 }
3839
3840                                 if ( ml->sml_values )
3841                                         d = dels;
3842
3843                                 /* If we didn't delete the whole attribute */
3844                                 if ( ml->sml_values && a ) {
3845                                         struct berval *mvals;
3846                                         int j;
3847
3848                                         if ( ml->sml_nvalues )
3849                                                 mvals = ml->sml_nvalues;
3850                                         else
3851                                                 mvals = ml->sml_values;
3852
3853                                         /* use the indexes we saved up above */
3854                                         for (i=0; i < d->nidx; i++) {
3855                                                 struct berval bv = *mvals++;
3856                                                 if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED &&
3857                                                         bv.bv_val[0] == '{' ) {
3858                                                         ptr = strchr( bv.bv_val, '}' ) + 1;
3859                                                         bv.bv_len -= ptr - bv.bv_val;
3860                                                         bv.bv_val = ptr;
3861                                                 }
3862                                                 ca->line = bv.bv_val;
3863                                                 ca->valx = d->idx[i];
3864                                                 rc = config_del_vals( ct, ca );
3865                                                 if ( rc != LDAP_SUCCESS ) break;
3866                                                 for (j=i+1; j < d->nidx; j++)
3867                                                         if ( d->idx[j] >d->idx[i] )
3868                                                                 d->idx[j]--;
3869                                         }
3870                                 } else {
3871                                         ca->valx = -1;
3872                                         ca->line = NULL;
3873                                         rc = config_del_vals( ct, ca );
3874                                         if ( rc ) rc = LDAP_OTHER;
3875                                 }
3876                                 if ( ml->sml_values ) {
3877                                         ch_free( dels );
3878                                         dels = d->next;
3879                                 }
3880                                 if ( ml->sml_op == LDAP_MOD_REPLACE ) {
3881                                         ml->sml_values = vals;
3882                                         ml->sml_nvalues = nvals;
3883                                 }
3884                                 if ( !vals || rc != LDAP_SUCCESS )
3885                                         break;
3886                                 }
3887                                 /* FALLTHRU: LDAP_MOD_REPLACE && vals */
3888
3889                         case LDAP_MOD_ADD:
3890                                 for (i=0; ml->sml_values[i].bv_val; i++) {
3891                                         ca->line = ml->sml_values[i].bv_val;
3892                                         ca->valx = -1;
3893                                         if ( ml->sml_desc->ad_type->sat_flags & SLAP_AT_ORDERED &&
3894                                                 ca->line[0] == '{' )
3895                                         {
3896                                                 ptr = strchr( ca->line + 1, '}' );
3897                                                 if ( ptr ) {
3898                                                         char    *next;
3899
3900                                                         ca->valx = strtol( ca->line + 1, &next, 0 );
3901                                                         if ( next == ca->line + 1 || next[ 0 ] != '}' ) {
3902                                                                 rc = LDAP_OTHER;
3903                                                                 goto out;
3904                                                         }
3905                                                         ca->line = ptr+1;
3906                                                 }
3907                                         }
3908                                         rc = config_parse_add( ct, ca );
3909                                         if ( rc ) {
3910                                                 rc = LDAP_OTHER;
3911                                                 goto out;
3912                                         }
3913                                 }
3914
3915                                 break;
3916                         }
3917                 }
3918         }
3919
3920 out:
3921         if ( ca->cleanup )
3922                 ca->cleanup( ca );
3923         if ( rc == LDAP_SUCCESS ) {
3924                 attrs_free( save_attrs );
3925         } else {
3926                 attrs_free( e->e_attrs );
3927                 e->e_attrs = save_attrs;
3928         }
3929         ch_free( ca->argv );
3930         if ( colst ) ch_free( colst );
3931
3932         return rc;
3933 }
3934
3935 static int
3936 config_back_modify( Operation *op, SlapReply *rs )
3937 {
3938         CfBackInfo *cfb;
3939         CfEntryInfo *ce, *last;
3940         Modifications *ml;
3941         ConfigArgs ca = {0};
3942         struct berval rdn;
3943         char *ptr;
3944         AttributeDescription *rad = NULL;
3945
3946         if ( !be_isroot( op ) ) {
3947                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3948                 goto out;
3949         }
3950
3951         cfb = (CfBackInfo *)op->o_bd->be_private;
3952
3953         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
3954         if ( !ce ) {
3955                 if ( last )
3956                         rs->sr_matched = last->ce_entry->e_name.bv_val;
3957                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
3958                 goto out;
3959         }
3960
3961         /* Get type of RDN */
3962         rdn = ce->ce_entry->e_nname;
3963         ptr = strchr( rdn.bv_val, '=' );
3964         rdn.bv_len = ptr - rdn.bv_val;
3965         slap_bv2ad( &rdn, &rad, &rs->sr_text );
3966
3967         /* Some basic validation... */
3968         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
3969                 /* Don't allow Modify of RDN; must use ModRdn for that. */
3970                 if ( ml->sml_desc == rad ) {
3971                         rs->sr_err = LDAP_NOT_ALLOWED_ON_RDN;
3972                         rs->sr_text = "Use modrdn to change the entry name";
3973                         goto out;
3974                 }
3975         }
3976
3977         ldap_pvt_thread_pool_pause( &connection_pool );
3978
3979         /* Strategy:
3980          * 1) perform the Modify on the cached Entry.
3981          * 2) verify that the Entry still satisfies the schema.
3982          * 3) perform the individual config operations.
3983          * 4) store Modified entry in underlying LDIF backend.
3984          */
3985         rs->sr_err = config_modify_internal( ce, op, rs, &ca );
3986         if ( rs->sr_err ) {
3987                 rs->sr_text = ca.msg;
3988         } else if ( cfb->cb_use_ldif ) {
3989                 BackendDB *be = op->o_bd;
3990                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL };
3991                 struct berval dn, ndn;
3992
3993                 op->o_bd = &cfb->cb_db;
3994
3995                 dn = op->o_dn;
3996                 ndn = op->o_ndn;
3997                 op->o_dn = op->o_bd->be_rootdn;
3998                 op->o_ndn = op->o_bd->be_rootndn;
3999
4000                 sc.sc_next = op->o_callback;
4001                 op->o_callback = &sc;
4002                 op->o_bd->be_modify( op, rs );
4003                 op->o_bd = be;
4004                 op->o_callback = sc.sc_next;
4005                 op->o_dn = dn;
4006                 op->o_ndn = ndn;
4007         }
4008
4009         ldap_pvt_thread_pool_resume( &connection_pool );
4010 out:
4011         send_ldap_result( op, rs );
4012         return rs->sr_err;
4013 }
4014
4015 static int
4016 config_back_modrdn( Operation *op, SlapReply *rs )
4017 {
4018         CfBackInfo *cfb;
4019         CfEntryInfo *ce, *last;
4020
4021         if ( !be_isroot( op ) ) {
4022                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
4023                 goto out;
4024         }
4025
4026         cfb = (CfBackInfo *)op->o_bd->be_private;
4027
4028         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
4029         if ( !ce ) {
4030                 if ( last )
4031                         rs->sr_matched = last->ce_entry->e_name.bv_val;
4032                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
4033                 goto out;
4034         }
4035
4036         /* We don't allow moving objects to new parents.
4037          * Generally we only allow reordering a set of ordered entries.
4038          */
4039         if ( op->orr_newSup ) {
4040                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
4041                 goto out;
4042         }
4043         ldap_pvt_thread_pool_pause( &connection_pool );
4044
4045         ldap_pvt_thread_pool_resume( &connection_pool );
4046 out:
4047         send_ldap_result( op, rs );
4048         return rs->sr_err;
4049 }
4050
4051 static int
4052 config_back_search( Operation *op, SlapReply *rs )
4053 {
4054         CfBackInfo *cfb;
4055         CfEntryInfo *ce, *last;
4056
4057         if ( !be_isroot( op ) ) {
4058                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
4059                 goto out;
4060         }
4061
4062         cfb = (CfBackInfo *)op->o_bd->be_private;
4063
4064         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
4065         if ( !ce ) {
4066                 if ( last )
4067                         rs->sr_matched = last->ce_entry->e_name.bv_val;
4068                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
4069                 goto out;
4070         }
4071         switch ( op->ors_scope ) {
4072         case LDAP_SCOPE_BASE:
4073         case LDAP_SCOPE_SUBTREE:
4074                 config_send( op, rs, ce, 0 );
4075                 break;
4076                 
4077         case LDAP_SCOPE_ONELEVEL:
4078                 for (ce = ce->ce_kids; ce; ce=ce->ce_sibs) {
4079                         config_send( op, rs, ce, 1 );
4080                 }
4081                 break;
4082         }
4083                 
4084         rs->sr_err = LDAP_SUCCESS;
4085 out:
4086         send_ldap_result( op, rs );
4087         return 0;
4088 }
4089
4090 static void
4091 config_build_attrs( Entry *e, AttributeType **at, AttributeDescription *ad,
4092         ConfigTable *ct, ConfigArgs *c )
4093 {
4094         int i, rc;
4095
4096         for (; at && *at; at++) {
4097                 /* Skip the naming attr */
4098                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
4099                         continue;
4100                 for (i=0;ct[i].name;i++) {
4101                         if (ct[i].ad == (*at)->sat_ad) {
4102                                 rc = config_get_vals(&ct[i], c);
4103                                 /* NOTE: tolerate that config_get_vals()
4104                                  * returns success with no values */
4105                                 if (rc == LDAP_SUCCESS && c->rvalue_vals != NULL ) {
4106                                         if ( c->rvalue_nvals )
4107                                                 attr_merge(e, ct[i].ad, c->rvalue_vals,
4108                                                         c->rvalue_nvals);
4109                                         else
4110                                                 attr_merge_normalize(e, ct[i].ad,
4111                                                         c->rvalue_vals, NULL);
4112                                         ber_bvarray_free( c->rvalue_nvals );
4113                                         ber_bvarray_free( c->rvalue_vals );
4114                                 }
4115                                 break;
4116                         }
4117                 }
4118         }
4119 }
4120
4121 Entry *
4122 config_build_entry( Operation *op, SlapReply *rs, CfEntryInfo *parent,
4123         ConfigArgs *c, struct berval *rdn, ConfigOCs *main, ConfigOCs *extra )
4124 {
4125         Entry *e = ch_calloc( 1, sizeof(Entry) );
4126         CfEntryInfo *ce = ch_calloc( 1, sizeof(CfEntryInfo) );
4127         struct berval val;
4128         struct berval ad_name;
4129         AttributeDescription *ad = NULL;
4130         int rc;
4131         char *ptr;
4132         const char *text;
4133         Attribute *oc_at;
4134         struct berval pdn;
4135         ObjectClass *oc;
4136         CfEntryInfo *ceprev = NULL;
4137
4138         e->e_private = ce;
4139         ce->ce_entry = e;
4140         ce->ce_parent = parent;
4141         if ( parent ) {
4142                 pdn = parent->ce_entry->e_nname;
4143                 if ( parent->ce_kids )
4144                         for ( ceprev = parent->ce_kids; ceprev->ce_sibs;
4145                                 ceprev = ceprev->ce_sibs );
4146         } else {
4147                 BER_BVZERO( &pdn );
4148         }
4149
4150         ce->ce_type = main->co_type;
4151         ce->ce_private = c->private;
4152         ce->ce_be = c->be;
4153         ce->ce_bi = c->bi;
4154
4155         build_new_dn( &e->e_name, &pdn, rdn, NULL );
4156         ber_dupbv( &e->e_nname, &e->e_name );
4157
4158         attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
4159                 main->co_name, NULL );
4160         if ( extra )
4161                 attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
4162                         extra->co_name, NULL );
4163         ptr = strchr(rdn->bv_val, '=');
4164         ad_name.bv_val = rdn->bv_val;
4165         ad_name.bv_len = ptr - rdn->bv_val;
4166         rc = slap_bv2ad( &ad_name, &ad, &text );
4167         if ( rc ) {
4168                 return NULL;
4169         }
4170         val.bv_val = ptr+1;
4171         val.bv_len = rdn->bv_len - (val.bv_val - rdn->bv_val);
4172         attr_merge_normalize_one(e, ad, &val, NULL );
4173
4174         oc = main->co_oc;
4175         if ( oc->soc_required )
4176                 config_build_attrs( e, oc->soc_required, ad, main->co_table, c );
4177
4178         if ( oc->soc_allowed )
4179                 config_build_attrs( e, oc->soc_allowed, ad, main->co_table, c );
4180
4181         if ( extra ) {
4182                 oc = extra->co_oc;
4183                 if ( oc->soc_required )
4184                         config_build_attrs( e, oc->soc_required, ad, extra->co_table, c );
4185
4186                 if ( oc->soc_allowed )
4187                         config_build_attrs( e, oc->soc_allowed, ad, extra->co_table, c );
4188         }
4189
4190         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
4191         rc = structural_class(oc_at->a_vals, &val, NULL, &text, c->msg,
4192                 sizeof(c->msg));
4193         attr_merge_normalize_one(e, slap_schema.si_ad_structuralObjectClass, &val, NULL );
4194         if ( op ) {
4195                 op->ora_e = e;
4196                 op->o_bd->be_add( op, rs );
4197         }
4198         if ( ceprev ) {
4199                 ceprev->ce_sibs = ce;
4200         } else if ( parent ) {
4201                 parent->ce_kids = ce;
4202         }
4203
4204         return e;
4205 }
4206
4207 static void
4208 config_build_schema_inc( ConfigArgs *c, CfEntryInfo *ceparent,
4209         Operation *op, SlapReply *rs )
4210 {
4211         Entry *e;
4212         ConfigFile *cf = c->private;
4213         char *ptr;
4214         struct berval bv;
4215
4216         for (; cf; cf=cf->c_sibs, c->depth++) {
4217                 c->value_dn.bv_val = c->log;
4218                 bv.bv_val = strrchr(cf->c_file.bv_val, LDAP_DIRSEP[0]);
4219                 if ( !bv.bv_val ) {
4220                         bv = cf->c_file;
4221                 } else {
4222                         bv.bv_val++;
4223                         bv.bv_len = cf->c_file.bv_len - (bv.bv_val - cf->c_file.bv_val);
4224                 }
4225                 ptr = strchr( bv.bv_val, '.' );
4226                 if ( ptr )
4227                         bv.bv_len = ptr - bv.bv_val;
4228                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=" SLAP_X_ORDERED_FMT, c->depth);
4229                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
4230                         /* FIXME: how can indicate error? */
4231                         return;
4232                 }
4233                 strncpy( c->value_dn.bv_val + c->value_dn.bv_len, bv.bv_val,
4234                         bv.bv_len );
4235                 c->value_dn.bv_len += bv.bv_len;
4236                 c->value_dn.bv_val[c->value_dn.bv_len] ='\0';
4237
4238                 c->private = cf;
4239                 e = config_build_entry( op, rs, ceparent, c, &c->value_dn,
4240                         &CFOC_SCHEMA, NULL );
4241                 if ( e && cf->c_kids ) {
4242                         c->private = cf->c_kids;
4243                         config_build_schema_inc( c, e->e_private, op, rs );
4244                 }
4245         }
4246 }
4247
4248 static void
4249 config_build_includes( ConfigArgs *c, CfEntryInfo *ceparent,
4250         Operation *op, SlapReply *rs )
4251 {
4252         Entry *e;
4253         int i;
4254         ConfigFile *cf = c->private;
4255
4256         for (i=0; cf; cf=cf->c_sibs, i++) {
4257                 c->value_dn.bv_val = c->log;
4258                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=include" SLAP_X_ORDERED_FMT, i);
4259                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
4260                         /* FIXME: how can indicate error? */
4261                         return;
4262                 }
4263                 c->private = cf;
4264                 e = config_build_entry( op, rs, ceparent, c, &c->value_dn,
4265                         &CFOC_INCLUDE, NULL );
4266                 if ( e && cf->c_kids ) {
4267                         c->private = cf->c_kids;
4268                         config_build_includes( c, e->e_private, op, rs );
4269                 }
4270         }
4271 }
4272
4273 #ifdef SLAPD_MODULES
4274
4275 static void
4276 config_build_modules( ConfigArgs *c, CfEntryInfo *ceparent,
4277         Operation *op, SlapReply *rs )
4278 {
4279         int i;
4280         ModPaths *mp;
4281
4282         for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
4283                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
4284                         continue;
4285                 c->value_dn.bv_val = c->log;
4286                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=module" SLAP_X_ORDERED_FMT, i);
4287                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
4288                         /* FIXME: how can indicate error? */
4289                         return;
4290                 }
4291                 c->private = mp;
4292                 config_build_entry( op, rs, ceparent, c, &c->value_dn,
4293                         &CFOC_MODULE, NULL );
4294         }
4295 }
4296 #endif
4297
4298 static int
4299 config_back_db_open( BackendDB *be )
4300 {
4301         CfBackInfo *cfb = be->be_private;
4302         struct berval rdn;
4303         Entry *e, *parent;
4304         CfEntryInfo *ce, *ceparent;
4305         int i, unsupp = 0;
4306         BackendInfo *bi;
4307         ConfigArgs c;
4308         Connection conn = {0};
4309         OperationBuffer opbuf;
4310         Operation *op;
4311         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
4312         SlapReply rs = {REP_RESULT};
4313         void *thrctx = NULL;
4314
4315         /* If we read the config from back-ldif, nothing to do here */
4316         if ( cfb->cb_got_ldif )
4317                 return 0;
4318
4319         if ( cfb->cb_use_ldif ) {
4320                 thrctx = ldap_pvt_thread_pool_context();
4321                 op = (Operation *) &opbuf;
4322                 connection_fake_init( &conn, op, thrctx );
4323
4324                 op->o_tag = LDAP_REQ_ADD;
4325                 op->o_callback = &cb;
4326                 op->o_bd = &cfb->cb_db;
4327                 op->o_dn = op->o_bd->be_rootdn;
4328                 op->o_ndn = op->o_bd->be_rootndn;
4329         } else {
4330                 op = NULL;
4331         }
4332
4333         /* create root of tree */
4334         rdn = config_rdn;
4335         c.private = cfb->cb_config;
4336         c.be = frontendDB;
4337         e = config_build_entry( op, &rs, NULL, &c, &rdn, &CFOC_GLOBAL, NULL );
4338         ce = e->e_private;
4339         cfb->cb_root = ce;
4340
4341         parent = e;
4342         ceparent = ce;
4343
4344         /* Create includeFile nodes */
4345         if ( cfb->cb_config->c_kids ) {
4346                 c.depth = 0;
4347                 c.private = cfb->cb_config->c_kids;
4348                 config_build_includes( &c, ceparent, op, &rs );
4349         }
4350
4351 #ifdef SLAPD_MODULES
4352         /* Create Module nodes... */
4353         if ( modpaths.mp_loads ) {
4354                 config_build_modules( &c, ceparent, op, &rs );
4355         }
4356 #endif
4357
4358         /* Create schema nodes... cn=schema will contain the hardcoded core
4359          * schema, read-only. Child objects will contain runtime loaded schema
4360          * files.
4361          */
4362         rdn = schema_rdn;
4363         c.private = NULL;
4364         e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_SCHEMA, NULL );
4365         ce = e->e_private;
4366
4367         /* Create schema nodes for included schema... */
4368         if ( cfb->cb_config->c_kids ) {
4369                 c.depth = 0;
4370                 c.private = cfb->cb_config->c_kids;
4371                 config_build_schema_inc( &c, ce, op, &rs );
4372         }
4373
4374         /* Create backend nodes. Skip if they don't provide a cf_table.
4375          * There usually aren't any of these.
4376          */
4377         
4378         c.line = 0;
4379         LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next) {
4380                 if (!bi->bi_cf_ocs) {
4381                         /* If it only supports the old config mech, complain. */
4382                         if ( bi->bi_config ) {
4383                                 Debug( LDAP_DEBUG_ANY,
4384                                         "WARNING: No dynamic config support for backend %s.\n",
4385                                         bi->bi_type, 0, 0 );
4386                                 unsupp++;
4387                         }
4388                         continue;
4389                 }
4390                 if (!bi->bi_private) continue;
4391
4392                 rdn.bv_val = c.log;
4393                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
4394                         "%s=%s", cfAd_backend->ad_cname.bv_val, bi->bi_type);
4395                 if ( rdn.bv_len >= sizeof( c.log ) ) {
4396                         /* FIXME: holler ... */ ;
4397                 }
4398                 c.bi = bi;
4399                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_BACKEND,
4400                         bi->bi_cf_ocs );
4401         }
4402
4403         /* Create database nodes... */
4404         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
4405         LDAP_STAILQ_NEXT(frontendDB, be_next) = LDAP_STAILQ_FIRST(&backendDB);
4406         for ( i = -1, be = frontendDB ; be;
4407                 i++, be = LDAP_STAILQ_NEXT( be, be_next )) {
4408                 slap_overinfo *oi = NULL;
4409
4410                 if ( overlay_is_over( be )) {
4411                         oi = be->bd_info->bi_private;
4412                         bi = oi->oi_orig;
4413                 } else {
4414                         bi = be->bd_info;
4415                 }
4416
4417                 /* If this backend supports the old config mechanism, but not
4418                  * the new mech, complain.
4419                  */
4420                 if ( !be->be_cf_ocs && bi->bi_db_config ) {
4421                         Debug( LDAP_DEBUG_ANY,
4422                                 "WARNING: No dynamic config support for database %s.\n",
4423                                 bi->bi_type, 0, 0 );
4424                         unsupp++;
4425                 }
4426                 rdn.bv_val = c.log;
4427                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
4428                         "%s=" SLAP_X_ORDERED_FMT "%s", cfAd_database->ad_cname.bv_val,
4429                         i, bi->bi_type);
4430                 if ( rdn.bv_len >= sizeof( c.log ) ) {
4431                         /* FIXME: holler ... */ ;
4432                 }
4433                 c.be = be;
4434                 c.bi = bi;
4435                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_DATABASE,
4436                         be->be_cf_ocs );
4437                 ce = e->e_private;
4438                 if ( be->be_cf_ocs && be->be_cf_ocs->co_cfadd )
4439                         be->be_cf_ocs->co_cfadd( op, &rs, e, &c );
4440                 /* Iterate through overlays */
4441                 if ( oi ) {
4442                         slap_overinst *on;
4443                         Entry *oe;
4444                         int j;
4445
4446                         for (j=0,on=oi->oi_list; on; j++,on=on->on_next) {
4447                                 if ( on->on_bi.bi_db_config && !on->on_bi.bi_cf_ocs ) {
4448                                         Debug( LDAP_DEBUG_ANY,
4449                                                 "WARNING: No dynamic config support for overlay %s.\n",
4450                                                 on->on_bi.bi_type, 0, 0 );
4451                                         unsupp++;
4452                                 }
4453                                 rdn.bv_val = c.log;
4454                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
4455                                         "%s=" SLAP_X_ORDERED_FMT "%s",
4456                                         cfAd_overlay->ad_cname.bv_val, j, on->on_bi.bi_type );
4457                                 if ( rdn.bv_len >= sizeof( c.log ) ) {
4458                                         /* FIXME: holler ... */ ;
4459                                 }
4460                                 c.be = be;
4461                                 c.bi = &on->on_bi;
4462                                 oe = config_build_entry( op, &rs, ce, &c, &rdn,
4463                                         &CFOC_OVERLAY, c.bi->bi_cf_ocs );
4464                                 if ( c.bi->bi_cf_ocs && c.bi->bi_cf_ocs->co_cfadd )
4465                                         c.bi->bi_cf_ocs->co_cfadd( op, &rs, oe, &c );
4466                         }
4467                 }
4468         }
4469         if ( thrctx )
4470                 ldap_pvt_thread_pool_context_reset( thrctx );
4471
4472         if ( unsupp  && cfb->cb_use_ldif ) {
4473                 Debug( LDAP_DEBUG_ANY, "\nWARNING: The converted cn=config "
4474                         "directory is incomplete and may not work.\n\n", 0, 0, 0 );
4475         }
4476
4477         return 0;
4478 }
4479
4480 static void
4481 cfb_free_cffile( ConfigFile *cf )
4482 {
4483         ConfigFile *next;
4484
4485         for (; cf; cf=next) {
4486                 next = cf->c_sibs;
4487                 if ( cf->c_kids )
4488                         cfb_free_cffile( cf->c_kids );
4489                 ch_free( cf->c_file.bv_val );
4490                 ber_bvarray_free( cf->c_dseFiles );
4491                 ch_free( cf );
4492         }
4493 }
4494
4495 static void
4496 cfb_free_entries( CfEntryInfo *ce )
4497 {
4498         CfEntryInfo *next;
4499
4500         for (; ce; ce=next) {
4501                 next = ce->ce_sibs;
4502                 if ( ce->ce_kids )
4503                         cfb_free_entries( ce->ce_kids );
4504                 ce->ce_entry->e_private = NULL;
4505                 entry_free( ce->ce_entry );
4506                 ch_free( ce );
4507         }
4508 }
4509
4510 static int
4511 config_back_db_close( BackendDB *be )
4512 {
4513         CfBackInfo *cfb = be->be_private;
4514
4515         cfb_free_entries( cfb->cb_root );
4516         cfb->cb_root = NULL;
4517
4518         if ( cfb->cb_db.bd_info ) {
4519                 backend_shutdown( &cfb->cb_db );
4520         }
4521
4522         return 0;
4523 }
4524
4525 static int
4526 config_back_db_destroy( BackendDB *be )
4527 {
4528         CfBackInfo *cfb = be->be_private;
4529
4530         cfb_free_cffile( cfb->cb_config );
4531
4532         ch_free( cfdir.bv_val );
4533
4534         avl_free( CfOcTree, NULL );
4535
4536         if ( cfb->cb_db.bd_info ) {
4537                 cfb->cb_db.be_suffix = NULL;
4538                 cfb->cb_db.be_nsuffix = NULL;
4539                 BER_BVZERO( &cfb->cb_db.be_rootdn );
4540                 BER_BVZERO( &cfb->cb_db.be_rootndn );
4541
4542                 backend_destroy_one( &cfb->cb_db, 0 );
4543         }
4544
4545         free( be->be_private );
4546
4547         loglevel_destroy();
4548
4549         return 0;
4550 }
4551
4552 static int
4553 config_back_db_init( BackendDB *be )
4554 {
4555         struct berval dn;
4556         CfBackInfo *cfb;
4557
4558         cfb = ch_calloc( 1, sizeof(CfBackInfo));
4559         cfb->cb_config = ch_calloc( 1, sizeof(ConfigFile));
4560         cfn = cfb->cb_config;
4561         be->be_private = cfb;
4562
4563         ber_dupbv( &be->be_rootdn, &config_rdn );
4564         ber_dupbv( &be->be_rootndn, &be->be_rootdn );
4565         ber_dupbv( &dn, &be->be_rootdn );
4566         ber_bvarray_add( &be->be_suffix, &dn );
4567         ber_dupbv( &dn, &be->be_rootdn );
4568         ber_bvarray_add( &be->be_nsuffix, &dn );
4569
4570         /* Hide from namingContexts */
4571         SLAP_BFLAGS(be) |= SLAP_BFLAG_CONFIG;
4572
4573         return 0;
4574 }
4575
4576 static int
4577 config_back_destroy( BackendInfo *bi )
4578 {
4579         ldif_must_b64_encode_release();
4580         return 0;
4581 }
4582
4583 static int
4584 config_tool_entry_open( BackendDB *be, int mode )
4585 {
4586         CfBackInfo *cfb = be->be_private;
4587         BackendInfo *bi = cfb->cb_db.bd_info;
4588
4589         if ( bi && bi->bi_tool_entry_open )
4590                 return bi->bi_tool_entry_open( &cfb->cb_db, mode );
4591         else
4592                 return -1;
4593         
4594 }
4595
4596 static int
4597 config_tool_entry_close( BackendDB *be )
4598 {
4599         CfBackInfo *cfb = be->be_private;
4600         BackendInfo *bi = cfb->cb_db.bd_info;
4601
4602         if ( bi && bi->bi_tool_entry_close )
4603                 return bi->bi_tool_entry_close( &cfb->cb_db );
4604         else
4605                 return -1;
4606 }
4607
4608 static ID
4609 config_tool_entry_first( BackendDB *be )
4610 {
4611         CfBackInfo *cfb = be->be_private;
4612         BackendInfo *bi = cfb->cb_db.bd_info;
4613
4614         if ( bi && bi->bi_tool_entry_first )
4615                 return bi->bi_tool_entry_first( &cfb->cb_db );
4616         else
4617                 return NOID;
4618 }
4619
4620 static ID
4621 config_tool_entry_next( BackendDB *be )
4622 {
4623         CfBackInfo *cfb = be->be_private;
4624         BackendInfo *bi = cfb->cb_db.bd_info;
4625
4626         if ( bi && bi->bi_tool_entry_next )
4627                 return bi->bi_tool_entry_next( &cfb->cb_db );
4628         else
4629                 return NOID;
4630 }
4631
4632 static Entry *
4633 config_tool_entry_get( BackendDB *be, ID id )
4634 {
4635         CfBackInfo *cfb = be->be_private;
4636         BackendInfo *bi = cfb->cb_db.bd_info;
4637
4638         if ( bi && bi->bi_tool_entry_get )
4639                 return bi->bi_tool_entry_get( &cfb->cb_db, id );
4640         else
4641                 return NULL;
4642 }
4643
4644 static ID
4645 config_tool_entry_put( BackendDB *be, Entry *e, struct berval *text )
4646 {
4647         CfBackInfo *cfb = be->be_private;
4648         BackendInfo *bi = cfb->cb_db.bd_info;
4649         ConfigArgs ca;
4650
4651         if ( bi && bi->bi_tool_entry_put &&
4652                 config_add_internal( cfb, e, &ca, NULL, NULL ) == 0 )
4653                 return bi->bi_tool_entry_put( &cfb->cb_db, e, text );
4654         else
4655                 return NOID;
4656 }
4657
4658 static struct {
4659         char *name;
4660         AttributeDescription **desc;
4661 } ads[] = {
4662         { "backend", &cfAd_backend },
4663         { "database", &cfAd_database },
4664         { "include", &cfAd_include },
4665         { "overlay", &cfAd_overlay },
4666         { NULL, NULL }
4667 };
4668
4669 /* Notes:
4670  *   add / delete: all types that may be added or deleted must use an
4671  * X-ORDERED attributeType for their RDN. Adding and deleting entries
4672  * should automatically renumber the index of any siblings as needed,
4673  * so that no gaps in the numbering sequence exist after the add/delete
4674  * is completed.
4675  *   What can be added:
4676  *     schema objects
4677  *     backend objects for backend-specific config directives
4678  *     database objects
4679  *     overlay objects
4680  *
4681  *   delete: probably no support this time around.
4682  *
4683  *   modrdn: generally not done. Will be invoked automatically by add/
4684  * delete to update numbering sequence. Perform as an explicit operation
4685  * so that the renumbering effect may be replicated. Subtree rename must
4686  * be supported, since renumbering a database will affect all its child
4687  * overlays.
4688  *
4689  *  modify: must be fully supported. 
4690  */
4691
4692 int
4693 config_back_initialize( BackendInfo *bi )
4694 {
4695         ConfigTable             *ct = config_back_cf_table;
4696         char                    *argv[4];
4697         int                     i;
4698         AttributeDescription    *ad = NULL;
4699         const char              *text;
4700         static char             *controls[] = {
4701                 LDAP_CONTROL_MANAGEDSAIT,
4702                 NULL
4703         };
4704
4705         bi->bi_controls = controls;
4706
4707         bi->bi_open = 0;
4708         bi->bi_close = 0;
4709         bi->bi_config = 0;
4710         bi->bi_destroy = config_back_destroy;
4711
4712         bi->bi_db_init = config_back_db_init;
4713         bi->bi_db_config = 0;
4714         bi->bi_db_open = config_back_db_open;
4715         bi->bi_db_close = config_back_db_close;
4716         bi->bi_db_destroy = config_back_db_destroy;
4717
4718         bi->bi_op_bind = config_back_bind;
4719         bi->bi_op_unbind = 0;
4720         bi->bi_op_search = config_back_search;
4721         bi->bi_op_compare = 0;
4722         bi->bi_op_modify = config_back_modify;
4723         bi->bi_op_modrdn = config_back_modrdn;
4724         bi->bi_op_add = config_back_add;
4725         bi->bi_op_delete = 0;
4726         bi->bi_op_abandon = 0;
4727
4728         bi->bi_extended = 0;
4729
4730         bi->bi_chk_referrals = 0;
4731
4732 #ifdef SLAP_OVERLAY_ACCESS
4733         bi->bi_access_allowed = slap_access_always_allowed;
4734 #endif /* SLAP_OVERLAY_ACCESS */
4735
4736         bi->bi_connection_init = 0;
4737         bi->bi_connection_destroy = 0;
4738
4739         bi->bi_tool_entry_open = config_tool_entry_open;
4740         bi->bi_tool_entry_close = config_tool_entry_close;
4741         bi->bi_tool_entry_first = config_tool_entry_first;
4742         bi->bi_tool_entry_next = config_tool_entry_next;
4743         bi->bi_tool_entry_get = config_tool_entry_get;
4744         bi->bi_tool_entry_put = config_tool_entry_put;
4745
4746         /* Make sure we don't exceed the bits reserved for userland */
4747         assert( ( ( CFG_LAST - 1 ) & ARGS_USERLAND ) == ( CFG_LAST - 1 ) );
4748
4749         argv[3] = NULL;
4750         for (i=0; OidMacros[i].name; i++ ) {
4751                 argv[1] = OidMacros[i].name;
4752                 argv[2] = OidMacros[i].oid;
4753                 parse_oidm( "slapd", i, 3, argv, 0, NULL );
4754         }
4755
4756         bi->bi_cf_ocs = cf_ocs;
4757
4758         i = config_register_schema( ct, cf_ocs );
4759         if ( i ) return i;
4760
4761         /* setup olcRootPW to be base64-encoded when written in LDIF form;
4762          * basically, we don't care if it fails */
4763         i = slap_str2ad( "olcRootPW", &ad, &text );
4764         if ( i ) {
4765                 Debug( LDAP_DEBUG_ANY, "config_back_initialize: "
4766                         "warning, unable to get \"olcRootPW\" "
4767                         "attribute description: %d: %s\n",
4768                         i, text, 0 );
4769         } else {
4770                 (void)ldif_must_b64_encode_register( ad->ad_cname.bv_val,
4771                         ad->ad_type->sat_oid );
4772         }
4773
4774         /* set up the notable AttributeDescriptions */
4775         i = 0;
4776         for (;ct->name;ct++) {
4777                 if (strcmp(ct->name, ads[i].name)) continue;
4778                 *ads[i].desc = ct->ad;
4779                 i++;
4780                 if (!ads[i].name) break;
4781         }
4782
4783         return 0;
4784 }
4785