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