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