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