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