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