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