]> git.sur5r.net Git - openldap/blob - servers/slapd/bconfig.c
ITS#6220 don't check write timeouts when global_writetimeout isn't set
[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-2009 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 <ldif.h>
36 #include <lutil.h>
37
38 #include "config.h"
39
40 #define CONFIG_RDN      "cn=config"
41 #define SCHEMA_RDN      "cn=schema"
42
43 static struct berval config_rdn = BER_BVC(CONFIG_RDN);
44 static struct berval schema_rdn = BER_BVC(SCHEMA_RDN);
45
46 extern int slap_DN_strict;      /* dn.c */
47
48 #ifdef SLAPD_MODULES
49 typedef struct modpath_s {
50         struct modpath_s *mp_next;
51         struct berval mp_path;
52         BerVarray mp_loads;
53 } ModPaths;
54
55 static ModPaths modpaths, *modlast = &modpaths, *modcur = &modpaths;
56 #endif
57
58 typedef struct ConfigFile {
59         struct ConfigFile *c_sibs;
60         struct ConfigFile *c_kids;
61         struct berval c_file;
62         AttributeType *c_at_head, *c_at_tail;
63         ContentRule *c_cr_head, *c_cr_tail;
64         ObjectClass *c_oc_head, *c_oc_tail;
65         OidMacro *c_om_head, *c_om_tail;
66         Syntax *c_syn_head, *c_syn_tail;
67         BerVarray c_dseFiles;
68 } ConfigFile;
69
70 typedef struct {
71         ConfigFile *cb_config;
72         CfEntryInfo *cb_root;
73         BackendDB       cb_db;  /* underlying database */
74         int             cb_got_ldif;
75         int             cb_use_ldif;
76 } CfBackInfo;
77
78 static CfBackInfo cfBackInfo;
79
80 static char     *passwd_salt;
81 static FILE *logfile;
82 static char     *logfileName;
83 #ifdef SLAP_AUTH_REWRITE
84 static BerVarray authz_rewrites;
85 #endif
86
87 static struct berval cfdir;
88
89 /* Private state */
90 static AttributeDescription *cfAd_backend, *cfAd_database, *cfAd_overlay,
91         *cfAd_include, *cfAd_attr, *cfAd_oc, *cfAd_om, *cfAd_syntax;
92
93 static ConfigFile *cfn;
94
95 static Avlnode *CfOcTree;
96
97 /* System schema state */
98 extern AttributeType *at_sys_tail;      /* at.c */
99 extern ObjectClass *oc_sys_tail;        /* oc.c */
100 extern OidMacro *om_sys_tail;   /* oidm.c */
101 extern Syntax *syn_sys_tail;    /* syntax.c */
102 static AttributeType *cf_at_tail;
103 static ObjectClass *cf_oc_tail;
104 static OidMacro *cf_om_tail;
105 static Syntax *cf_syn_tail;
106
107 static int config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca,
108         SlapReply *rs, int *renumber, Operation *op );
109
110 static int config_check_schema( Operation *op, CfBackInfo *cfb );
111
112 static ConfigDriver config_fname;
113 static ConfigDriver config_cfdir;
114 static ConfigDriver config_generic;
115 static ConfigDriver config_search_base;
116 static ConfigDriver config_passwd_hash;
117 static ConfigDriver config_schema_dn;
118 static ConfigDriver config_sizelimit;
119 static ConfigDriver config_timelimit;
120 static ConfigDriver config_overlay;
121 static ConfigDriver config_subordinate; 
122 static ConfigDriver config_suffix; 
123 static ConfigDriver config_rootdn;
124 static ConfigDriver config_rootpw;
125 static ConfigDriver config_restrict;
126 static ConfigDriver config_allows;
127 static ConfigDriver config_disallows;
128 static ConfigDriver config_requires;
129 static ConfigDriver config_security;
130 static ConfigDriver config_referral;
131 static ConfigDriver config_loglevel;
132 static ConfigDriver config_updatedn;
133 static ConfigDriver config_updateref;
134 static ConfigDriver config_include;
135 static ConfigDriver config_obsolete;
136 #ifdef HAVE_TLS
137 static ConfigDriver config_tls_option;
138 static ConfigDriver config_tls_config;
139 #endif
140 extern ConfigDriver syncrepl_config;
141
142 enum {
143         CFG_ACL = 1,
144         CFG_BACKEND,
145         CFG_DATABASE,
146         CFG_TLS_RAND,
147         CFG_TLS_CIPHER,
148         CFG_TLS_PROTOCOL_MIN,
149         CFG_TLS_CERT_FILE,
150         CFG_TLS_CERT_KEY,
151         CFG_TLS_CA_PATH,
152         CFG_TLS_CA_FILE,
153         CFG_TLS_DH_FILE,
154         CFG_TLS_VERIFY,
155         CFG_TLS_CRLCHECK,
156         CFG_TLS_CRL_FILE,
157         CFG_CONCUR,
158         CFG_THREADS,
159         CFG_SALT,
160         CFG_LIMITS,
161         CFG_RO,
162         CFG_REWRITE,
163         CFG_DEPTH,
164         CFG_OID,
165         CFG_OC,
166         CFG_DIT,
167         CFG_ATTR,
168         CFG_ATOPT,
169         CFG_ROOTDSE,
170         CFG_LOGFILE,
171         CFG_PLUGIN,
172         CFG_MODLOAD,
173         CFG_MODPATH,
174         CFG_LASTMOD,
175         CFG_AZPOLICY,
176         CFG_AZREGEXP,
177         CFG_SASLSECP,
178         CFG_SSTR_IF_MAX,
179         CFG_SSTR_IF_MIN,
180         CFG_TTHREADS,
181         CFG_MIRRORMODE,
182         CFG_HIDDEN,
183         CFG_MONITORING,
184         CFG_SERVERID,
185         CFG_SORTVALS,
186         CFG_IX_INTLEN,
187         CFG_SYNTAX,
188         CFG_ACL_ADD,
189
190         CFG_LAST
191 };
192
193 typedef struct {
194         char *name, *oid;
195 } OidRec;
196
197 static OidRec OidMacros[] = {
198         /* OpenLDAProot:1.12.2 */
199         { "OLcfg", "1.3.6.1.4.1.4203.1.12.2" },
200         { "OLcfgAt", "OLcfg:3" },
201         { "OLcfgGlAt", "OLcfgAt:0" },
202         { "OLcfgBkAt", "OLcfgAt:1" },
203         { "OLcfgDbAt", "OLcfgAt:2" },
204         { "OLcfgOvAt", "OLcfgAt:3" },
205         { "OLcfgCtAt", "OLcfgAt:4" },   /* contrib modules */
206         { "OLcfgOc", "OLcfg:4" },
207         { "OLcfgGlOc", "OLcfgOc:0" },
208         { "OLcfgBkOc", "OLcfgOc:1" },
209         { "OLcfgDbOc", "OLcfgOc:2" },
210         { "OLcfgOvOc", "OLcfgOc:3" },
211         { "OLcfgCtOc", "OLcfgOc:4" },   /* contrib modules */
212
213         /* Syntaxes. We should just start using the standard names and
214          * document that they are predefined and available for users
215          * to reference in their own schema. Defining schema without
216          * OID macros is for masochists...
217          */
218         { "OMsyn", "1.3.6.1.4.1.1466.115.121.1" },
219         { "OMsBoolean", "OMsyn:7" },
220         { "OMsDN", "OMsyn:12" },
221         { "OMsDirectoryString", "OMsyn:15" },
222         { "OMsIA5String", "OMsyn:26" },
223         { "OMsInteger", "OMsyn:27" },
224         { "OMsOID", "OMsyn:38" },
225         { "OMsOctetString", "OMsyn:40" },
226         { NULL, NULL }
227 };
228
229 /*
230  * Backend/Database registry
231  *
232  * OLcfg{Bk|Db}{Oc|At}:0                -> common
233  * OLcfg{Bk|Db}{Oc|At}:1                -> back-bdb(/back-hdb)
234  * OLcfg{Bk|Db}{Oc|At}:2                -> back-ldif
235  * OLcfg{Bk|Db}{Oc|At}:3                -> back-ldap
236  * OLcfg{Bk|Db}{Oc|At}:4                -> back-monitor
237  * OLcfg{Bk|Db}{Oc|At}:5                -> back-relay
238  * OLcfg{Bk|Db}{Oc|At}:6                -> back-sql
239  * OLcfg{Bk|Db}{Oc|At}:7                -> back-sock
240  */
241
242 /*
243  * Overlay registry
244  *
245  * OLcfgOv{Oc|At}:1                     -> syncprov
246  * OLcfgOv{Oc|At}:2                     -> pcache
247  * OLcfgOv{Oc|At}:3                     -> chain
248  * OLcfgOv{Oc|At}:4                     -> accesslog
249  * OLcfgOv{Oc|At}:5                     -> valsort
250  * OLcfgOv{Oc|At}:7                     -> distproc
251  * OLcfgOv{Oc|At}:8                     -> dynlist
252  * OLcfgOv{Oc|At}:9                     -> dds
253  * OLcfgOv{Oc|At}:10                    -> unique
254  * OLcfgOv{Oc|At}:11                    -> refint
255  * OLcfgOv{Oc|At}:12                    -> ppolicy
256  * OLcfgOv{Oc|At}:13                    -> constraint
257  * OLcfgOv{Oc|At}:14                    -> translucent
258  * OLcfgOv{Oc|At}:15                    -> auditlog
259  * OLcfgOv{Oc|At}:16                    -> rwm
260  * OLcfgOv{Oc|At}:17                    -> dyngroup
261  * OLcfgOv{Oc|At}:18                    -> memberof
262  * OLcfgOv{Oc|At}:19                    -> collect
263  * OLcfgOv{Oc|At}:20                    -> retcode
264  * OLcfgOv{Oc|At}:21                    -> sssvlv
265  */
266
267 /* alphabetical ordering */
268
269 static ConfigTable config_back_cf_table[] = {
270         /* This attr is read-only */
271         { "", "", 0, 0, 0, ARG_MAGIC,
272                 &config_fname, "( OLcfgGlAt:78 NAME 'olcConfigFile' "
273                         "DESC 'File for slapd configuration directives' "
274                         "EQUALITY caseIgnoreMatch "
275                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
276         { "", "", 0, 0, 0, ARG_MAGIC,
277                 &config_cfdir, "( OLcfgGlAt:79 NAME 'olcConfigDir' "
278                         "DESC 'Directory for slapd configuration backend' "
279                         "EQUALITY caseIgnoreMatch "
280                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
281         { "access",     NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_ACL,
282                 &config_generic, "( OLcfgGlAt:1 NAME 'olcAccess' "
283                         "DESC 'Access Control List' "
284                         "EQUALITY caseIgnoreMatch "
285                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
286         { "add_content_acl",    NULL, 0, 0, 0, ARG_MAY_DB|ARG_ON_OFF|ARG_MAGIC|CFG_ACL_ADD,
287                 &config_generic, "( OLcfgGlAt:86 NAME 'olcAddContentAcl' "
288                         "DESC 'Check ACLs against content of Add ops' "
289                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
290         { "allows",     "features", 2, 0, 5, ARG_PRE_DB|ARG_MAGIC,
291                 &config_allows, "( OLcfgGlAt:2 NAME 'olcAllows' "
292                         "DESC 'Allowed set of deprecated features' "
293                         "EQUALITY caseIgnoreMatch "
294                         "SYNTAX OMsDirectoryString )", NULL, NULL },
295         { "argsfile", "file", 2, 2, 0, ARG_STRING,
296                 &slapd_args_file, "( OLcfgGlAt:3 NAME 'olcArgsFile' "
297                         "DESC 'File for slapd command line options' "
298                         "EQUALITY caseIgnoreMatch "
299                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
300         { "attributeoptions", NULL, 0, 0, 0, ARG_MAGIC|CFG_ATOPT,
301                 &config_generic, "( OLcfgGlAt:5 NAME 'olcAttributeOptions' "
302                         "EQUALITY caseIgnoreMatch "
303                         "SYNTAX OMsDirectoryString )", NULL, NULL },
304         { "attribute",  "attribute", 2, 0, STRLENOF( "attribute" ),
305                 ARG_PAREN|ARG_MAGIC|CFG_ATTR,
306                 &config_generic, "( OLcfgGlAt:4 NAME 'olcAttributeTypes' "
307                         "DESC 'OpenLDAP attributeTypes' "
308                         "EQUALITY caseIgnoreMatch "
309                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
310                                 NULL, NULL },
311         { "authid-rewrite", NULL, 2, 0, STRLENOF( "authid-rewrite" ),
312 #ifdef SLAP_AUTH_REWRITE
313                 ARG_MAGIC|CFG_REWRITE|ARG_NO_INSERT, &config_generic,
314 #else
315                 ARG_IGNORED, NULL,
316 #endif
317                  "( OLcfgGlAt:6 NAME 'olcAuthIDRewrite' "
318                         "EQUALITY caseIgnoreMatch "
319                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
320         { "authz-policy", "policy", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_AZPOLICY,
321                 &config_generic, "( OLcfgGlAt:7 NAME 'olcAuthzPolicy' "
322                         "EQUALITY caseIgnoreMatch "
323                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
324         { "authz-regexp", "regexp> <DN", 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP|ARG_NO_INSERT,
325                 &config_generic, "( OLcfgGlAt:8 NAME 'olcAuthzRegexp' "
326                         "EQUALITY caseIgnoreMatch "
327                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
328         { "backend", "type", 2, 2, 0, ARG_PRE_DB|ARG_MAGIC|CFG_BACKEND,
329                 &config_generic, "( OLcfgGlAt:9 NAME 'olcBackend' "
330                         "DESC 'A type of backend' "
331                         "EQUALITY caseIgnoreMatch "
332                         "SYNTAX OMsDirectoryString SINGLE-VALUE X-ORDERED 'SIBLINGS' )",
333                                 NULL, NULL },
334         { "concurrency", "level", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_CONCUR,
335                 &config_generic, "( OLcfgGlAt:10 NAME 'olcConcurrency' "
336                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
337         { "conn_max_pending", "max", 2, 2, 0, ARG_INT,
338                 &slap_conn_max_pending, "( OLcfgGlAt:11 NAME 'olcConnMaxPending' "
339                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
340         { "conn_max_pending_auth", "max", 2, 2, 0, ARG_INT,
341                 &slap_conn_max_pending_auth, "( OLcfgGlAt:12 NAME 'olcConnMaxPendingAuth' "
342                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
343         { "database", "type", 2, 2, 0, ARG_MAGIC|CFG_DATABASE,
344                 &config_generic, "( OLcfgGlAt:13 NAME 'olcDatabase' "
345                         "DESC 'The backend type for a database instance' "
346                         "SUP olcBackend SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
347         { "defaultSearchBase", "dn", 2, 2, 0, ARG_PRE_BI|ARG_PRE_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
348                 &config_search_base, "( OLcfgGlAt:14 NAME 'olcDefaultSearchBase' "
349                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
350         { "disallows", "features", 2, 0, 8, ARG_PRE_DB|ARG_MAGIC,
351                 &config_disallows, "( OLcfgGlAt:15 NAME 'olcDisallows' "
352                         "EQUALITY caseIgnoreMatch "
353                         "SYNTAX OMsDirectoryString )", NULL, NULL },
354         { "ditcontentrule",     NULL, 0, 0, 0, ARG_MAGIC|CFG_DIT|ARG_NO_DELETE|ARG_NO_INSERT,
355                 &config_generic, "( OLcfgGlAt:16 NAME 'olcDitContentRules' "
356                         "DESC 'OpenLDAP DIT content rules' "
357                         "EQUALITY caseIgnoreMatch "
358                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
359                         NULL, NULL },
360         { "gentlehup", "on|off", 2, 2, 0,
361 #ifdef SIGHUP
362                 ARG_ON_OFF, &global_gentlehup,
363 #else
364                 ARG_IGNORED, NULL,
365 #endif
366                 "( OLcfgGlAt:17 NAME 'olcGentleHUP' "
367                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
368         { "hidden", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_HIDDEN,
369                 &config_generic, "( OLcfgDbAt:0.17 NAME 'olcHidden' "
370                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
371         { "idletimeout", "timeout", 2, 2, 0, ARG_INT,
372                 &global_idletimeout, "( OLcfgGlAt:18 NAME 'olcIdleTimeout' "
373                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
374         { "include", "file", 2, 2, 0, ARG_MAGIC,
375                 &config_include, "( OLcfgGlAt:19 NAME 'olcInclude' "
376                         "SUP labeledURI )", NULL, NULL },
377         { "index_substr_if_minlen", "min", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MIN,
378                 &config_generic, "( OLcfgGlAt:20 NAME 'olcIndexSubstrIfMinLen' "
379                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
380         { "index_substr_if_maxlen", "max", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MAX,
381                 &config_generic, "( OLcfgGlAt:21 NAME 'olcIndexSubstrIfMaxLen' "
382                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
383         { "index_substr_any_len", "len", 2, 2, 0, ARG_INT|ARG_NONZERO,
384                 &index_substr_any_len, "( OLcfgGlAt:22 NAME 'olcIndexSubstrAnyLen' "
385                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
386         { "index_substr_any_step", "step", 2, 2, 0, ARG_INT|ARG_NONZERO,
387                 &index_substr_any_step, "( OLcfgGlAt:23 NAME 'olcIndexSubstrAnyStep' "
388                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
389         { "index_intlen", "len", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_IX_INTLEN,
390                 &config_generic, "( OLcfgGlAt:84 NAME 'olcIndexIntLen' "
391                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
392         { "lastmod", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_LASTMOD,
393                 &config_generic, "( OLcfgDbAt:0.4 NAME 'olcLastMod' "
394                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
395         { "ldapsyntax", "syntax", 2, 0, 0,
396                 ARG_PAREN|ARG_MAGIC|CFG_SYNTAX,
397                 &config_generic, "( OLcfgGlAt:85 NAME 'olcLdapSyntaxes' "
398                         "DESC 'OpenLDAP ldapSyntax' "
399                         "EQUALITY caseIgnoreMatch "
400                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
401                                 NULL, NULL },
402         { "limits", "limits", 2, 0, 0, ARG_DB|ARG_MAGIC|CFG_LIMITS,
403                 &config_generic, "( OLcfgDbAt:0.5 NAME 'olcLimits' "
404                         "EQUALITY caseIgnoreMatch "
405                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
406         { "localSSF", "ssf", 2, 2, 0, ARG_INT,
407                 &local_ssf, "( OLcfgGlAt:26 NAME 'olcLocalSSF' "
408                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
409         { "logfile", "file", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_LOGFILE,
410                 &config_generic, "( OLcfgGlAt:27 NAME 'olcLogFile' "
411                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
412         { "loglevel", "level", 2, 0, 0, ARG_MAGIC,
413                 &config_loglevel, "( OLcfgGlAt:28 NAME 'olcLogLevel' "
414                         "EQUALITY caseIgnoreMatch "
415                         "SYNTAX OMsDirectoryString )", NULL, NULL },
416         { "maxDerefDepth", "depth", 2, 2, 0, ARG_DB|ARG_INT|ARG_MAGIC|CFG_DEPTH,
417                 &config_generic, "( OLcfgDbAt:0.6 NAME 'olcMaxDerefDepth' "
418                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
419         { "mirrormode", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_MIRRORMODE,
420                 &config_generic, "( OLcfgDbAt:0.16 NAME 'olcMirrorMode' "
421                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
422         { "moduleload", "file", 2, 0, 0,
423 #ifdef SLAPD_MODULES
424                 ARG_MAGIC|CFG_MODLOAD|ARG_NO_DELETE, &config_generic,
425 #else
426                 ARG_IGNORED, NULL,
427 #endif
428                 "( OLcfgGlAt:30 NAME 'olcModuleLoad' "
429                         "EQUALITY caseIgnoreMatch "
430                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
431         { "modulepath", "path", 2, 2, 0,
432 #ifdef SLAPD_MODULES
433                 ARG_MAGIC|CFG_MODPATH|ARG_NO_DELETE|ARG_NO_INSERT, &config_generic,
434 #else
435                 ARG_IGNORED, NULL,
436 #endif
437                 "( OLcfgGlAt:31 NAME 'olcModulePath' "
438                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
439         { "monitoring", "TRUE|FALSE", 2, 2, 0,
440                 ARG_MAGIC|CFG_MONITORING|ARG_DB|ARG_ON_OFF, &config_generic,
441                 "( OLcfgDbAt:0.18 NAME 'olcMonitoring' "
442                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
443         { "objectclass", "objectclass", 2, 0, 0, ARG_PAREN|ARG_MAGIC|CFG_OC,
444                 &config_generic, "( OLcfgGlAt:32 NAME 'olcObjectClasses' "
445                 "DESC 'OpenLDAP object classes' "
446                 "EQUALITY caseIgnoreMatch "
447                 "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
448                         NULL, NULL },
449         { "objectidentifier", "name> <oid",     3, 3, 0, ARG_MAGIC|CFG_OID,
450                 &config_generic, "( OLcfgGlAt:33 NAME 'olcObjectIdentifier' "
451                         "EQUALITY caseIgnoreMatch "
452                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
453         { "overlay", "overlay", 2, 2, 0, ARG_MAGIC,
454                 &config_overlay, "( OLcfgGlAt:34 NAME 'olcOverlay' "
455                         "SUP olcDatabase SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
456         { "password-crypt-salt-format", "salt", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_SALT,
457                 &config_generic, "( OLcfgGlAt:35 NAME 'olcPasswordCryptSaltFormat' "
458                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
459         { "password-hash", "hash", 2, 0, 0, ARG_MAGIC,
460                 &config_passwd_hash, "( OLcfgGlAt:36 NAME 'olcPasswordHash' "
461                         "EQUALITY caseIgnoreMatch "
462                         "SYNTAX OMsDirectoryString )", NULL, NULL },
463         { "pidfile", "file", 2, 2, 0, ARG_STRING,
464                 &slapd_pid_file, "( OLcfgGlAt:37 NAME 'olcPidFile' "
465                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
466         { "plugin", NULL, 0, 0, 0,
467 #ifdef LDAP_SLAPI
468                 ARG_MAGIC|CFG_PLUGIN, &config_generic,
469 #else
470                 ARG_IGNORED, NULL,
471 #endif
472                 "( OLcfgGlAt:38 NAME 'olcPlugin' "
473                         "EQUALITY caseIgnoreMatch "
474                         "SYNTAX OMsDirectoryString )", NULL, NULL },
475         { "pluginlog", "filename", 2, 2, 0,
476 #ifdef LDAP_SLAPI
477                 ARG_STRING, &slapi_log_file,
478 #else
479                 ARG_IGNORED, NULL,
480 #endif
481                 "( OLcfgGlAt:39 NAME 'olcPluginLogFile' "
482                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
483         { "readonly", "on|off", 2, 2, 0, ARG_MAY_DB|ARG_ON_OFF|ARG_MAGIC|CFG_RO,
484                 &config_generic, "( OLcfgGlAt:40 NAME 'olcReadOnly' "
485                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
486         { "referral", "url", 2, 2, 0, ARG_MAGIC,
487                 &config_referral, "( OLcfgGlAt:41 NAME 'olcReferral' "
488                         "SUP labeledURI SINGLE-VALUE )", NULL, NULL },
489         { "replica", "host or uri", 2, 0, 0, ARG_DB|ARG_MAGIC,
490                 &config_obsolete, "( OLcfgDbAt:0.7 NAME 'olcReplica' "
491                         "EQUALITY caseIgnoreMatch "
492                         "SUP labeledURI X-ORDERED 'VALUES' )", NULL, NULL },
493         { "replica-argsfile", NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC,
494                 &config_obsolete, "( OLcfgGlAt:43 NAME 'olcReplicaArgsFile' "
495                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
496         { "replica-pidfile", NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC,
497                 &config_obsolete, "( OLcfgGlAt:44 NAME 'olcReplicaPidFile' "
498                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
499         { "replicationInterval", NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC,
500                 &config_obsolete, "( OLcfgGlAt:45 NAME 'olcReplicationInterval' "
501                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
502         { "replogfile", "filename", 2, 2, 0, ARG_MAY_DB|ARG_MAGIC,
503                 &config_obsolete, "( OLcfgGlAt:46 NAME 'olcReplogFile' "
504                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
505         { "require", "features", 2, 0, 7, ARG_MAY_DB|ARG_MAGIC,
506                 &config_requires, "( OLcfgGlAt:47 NAME 'olcRequires' "
507                         "EQUALITY caseIgnoreMatch "
508                         "SYNTAX OMsDirectoryString )", NULL, NULL },
509         { "restrict", "op_list", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
510                 &config_restrict, "( OLcfgGlAt:48 NAME 'olcRestrict' "
511                         "EQUALITY caseIgnoreMatch "
512                         "SYNTAX OMsDirectoryString )", NULL, NULL },
513         { "reverse-lookup", "on|off", 2, 2, 0,
514 #ifdef SLAPD_RLOOKUPS
515                 ARG_ON_OFF, &use_reverse_lookup,
516 #else
517                 ARG_IGNORED, NULL,
518 #endif
519                 "( OLcfgGlAt:49 NAME 'olcReverseLookup' "
520                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
521         { "rootdn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
522                 &config_rootdn, "( OLcfgDbAt:0.8 NAME 'olcRootDN' "
523                         "EQUALITY distinguishedNameMatch "
524                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
525         { "rootDSE", "file", 2, 2, 0, ARG_MAGIC|CFG_ROOTDSE,
526                 &config_generic, "( OLcfgGlAt:51 NAME 'olcRootDSE' "
527                         "EQUALITY caseIgnoreMatch "
528                         "SYNTAX OMsDirectoryString )", NULL, NULL },
529         { "rootpw", "password", 2, 2, 0, ARG_BERVAL|ARG_DB|ARG_MAGIC,
530                 &config_rootpw, "( OLcfgDbAt:0.9 NAME 'olcRootPW' "
531                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
532         { "sasl-authz-policy", NULL, 2, 2, 0, ARG_MAGIC|CFG_AZPOLICY,
533                 &config_generic, NULL, NULL, NULL },
534         { "sasl-auxprops", NULL, 2, 0, 0,
535 #ifdef HAVE_CYRUS_SASL
536                 ARG_STRING|ARG_UNIQUE, &slap_sasl_auxprops,
537 #else
538                 ARG_IGNORED, NULL,
539 #endif
540                 "( OLcfgGlAt:89 NAME 'olcSaslAuxprops' "
541                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
542         { "sasl-host", "host", 2, 2, 0,
543 #ifdef HAVE_CYRUS_SASL
544                 ARG_STRING|ARG_UNIQUE, &sasl_host,
545 #else
546                 ARG_IGNORED, NULL,
547 #endif
548                 "( OLcfgGlAt:53 NAME 'olcSaslHost' "
549                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
550         { "sasl-realm", "realm", 2, 2, 0,
551 #ifdef HAVE_CYRUS_SASL
552                 ARG_STRING|ARG_UNIQUE, &global_realm,
553 #else
554                 ARG_IGNORED, NULL,
555 #endif
556                 "( OLcfgGlAt:54 NAME 'olcSaslRealm' "
557                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
558         { "sasl-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
559                 &config_generic, NULL, NULL, NULL },
560         { "sasl-secprops", "properties", 2, 2, 0,
561 #ifdef HAVE_CYRUS_SASL
562                 ARG_MAGIC|CFG_SASLSECP, &config_generic,
563 #else
564                 ARG_IGNORED, NULL,
565 #endif
566                 "( OLcfgGlAt:56 NAME 'olcSaslSecProps' "
567                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
568         { "saslRegexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
569                 &config_generic, NULL, NULL, NULL },
570         { "schemadn", "dn", 2, 2, 0, ARG_MAY_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
571                 &config_schema_dn, "( OLcfgGlAt:58 NAME 'olcSchemaDN' "
572                         "EQUALITY distinguishedNameMatch "
573                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
574         { "security", "factors", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
575                 &config_security, "( OLcfgGlAt:59 NAME 'olcSecurity' "
576                         "EQUALITY caseIgnoreMatch "
577                         "SYNTAX OMsDirectoryString )", NULL, NULL },
578         { "serverID", "number> <[URI]", 2, 3, 0, ARG_MAGIC|CFG_SERVERID,
579                 &config_generic, "( OLcfgGlAt:81 NAME 'olcServerID' "
580                         "EQUALITY caseIgnoreMatch "
581                         "SYNTAX OMsDirectoryString )", NULL, NULL },
582         { "sizelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
583                 &config_sizelimit, "( OLcfgGlAt:60 NAME 'olcSizeLimit' "
584                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
585         { "sockbuf_max_incoming", "max", 2, 2, 0, ARG_BER_LEN_T,
586                 &sockbuf_max_incoming, "( OLcfgGlAt:61 NAME 'olcSockbufMaxIncoming' "
587                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
588         { "sockbuf_max_incoming_auth", "max", 2, 2, 0, ARG_BER_LEN_T,
589                 &sockbuf_max_incoming_auth, "( OLcfgGlAt:62 NAME 'olcSockbufMaxIncomingAuth' "
590                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
591         { "sortvals", "attr", 2, 0, 0, ARG_MAGIC|CFG_SORTVALS,
592                 &config_generic, "( OLcfgGlAt:83 NAME 'olcSortVals' "
593                         "DESC 'Attributes whose values will always be sorted' "
594                         "EQUALITY caseIgnoreMatch "
595                         "SYNTAX OMsDirectoryString )", NULL, NULL },
596         { "subordinate", "[advertise]", 1, 2, 0, ARG_DB|ARG_MAGIC,
597                 &config_subordinate, "( OLcfgDbAt:0.15 NAME 'olcSubordinate' "
598                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
599         { "suffix",     "suffix", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
600                 &config_suffix, "( OLcfgDbAt:0.10 NAME 'olcSuffix' "
601                         "EQUALITY distinguishedNameMatch "
602                         "SYNTAX OMsDN )", NULL, NULL },
603         { "syncrepl", NULL, 0, 0, 0, ARG_DB|ARG_MAGIC,
604                 &syncrepl_config, "( OLcfgDbAt:0.11 NAME 'olcSyncrepl' "
605                         "EQUALITY caseIgnoreMatch "
606                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
607         { "threads", "count", 2, 2, 0,
608 #ifdef NO_THREADS
609                 ARG_IGNORED, NULL,
610 #else
611                 ARG_INT|ARG_MAGIC|CFG_THREADS, &config_generic,
612 #endif
613                 "( OLcfgGlAt:66 NAME 'olcThreads' "
614                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
615         { "timelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
616                 &config_timelimit, "( OLcfgGlAt:67 NAME 'olcTimeLimit' "
617                         "SYNTAX OMsDirectoryString )", NULL, NULL },
618         { "TLSCACertificateFile", NULL, 0, 0, 0,
619 #ifdef HAVE_TLS
620                 CFG_TLS_CA_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
621 #else
622                 ARG_IGNORED, NULL,
623 #endif
624                 "( OLcfgGlAt:68 NAME 'olcTLSCACertificateFile' "
625                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
626         { "TLSCACertificatePath", NULL, 0, 0, 0,
627 #ifdef HAVE_TLS
628                 CFG_TLS_CA_PATH|ARG_STRING|ARG_MAGIC, &config_tls_option,
629 #else
630                 ARG_IGNORED, NULL,
631 #endif
632                 "( OLcfgGlAt:69 NAME 'olcTLSCACertificatePath' "
633                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
634         { "TLSCertificateFile", NULL, 0, 0, 0,
635 #ifdef HAVE_TLS
636                 CFG_TLS_CERT_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
637 #else
638                 ARG_IGNORED, NULL,
639 #endif
640                 "( OLcfgGlAt:70 NAME 'olcTLSCertificateFile' "
641                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
642         { "TLSCertificateKeyFile", NULL, 0, 0, 0,
643 #ifdef HAVE_TLS
644                 CFG_TLS_CERT_KEY|ARG_STRING|ARG_MAGIC, &config_tls_option,
645 #else
646                 ARG_IGNORED, NULL,
647 #endif
648                 "( OLcfgGlAt:71 NAME 'olcTLSCertificateKeyFile' "
649                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
650         { "TLSCipherSuite",     NULL, 0, 0, 0,
651 #ifdef HAVE_TLS
652                 CFG_TLS_CIPHER|ARG_STRING|ARG_MAGIC, &config_tls_option,
653 #else
654                 ARG_IGNORED, NULL,
655 #endif
656                 "( OLcfgGlAt:72 NAME 'olcTLSCipherSuite' "
657                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
658         { "TLSCRLCheck", NULL, 0, 0, 0,
659 #if defined(HAVE_TLS) && defined(HAVE_OPENSSL_CRL)
660                 CFG_TLS_CRLCHECK|ARG_STRING|ARG_MAGIC, &config_tls_config,
661 #else
662                 ARG_IGNORED, NULL,
663 #endif
664                 "( OLcfgGlAt:73 NAME 'olcTLSCRLCheck' "
665                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
666         { "TLSCRLFile", NULL, 0, 0, 0,
667 #if defined(HAVE_GNUTLS)
668                 CFG_TLS_CRL_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
669 #else
670                 ARG_IGNORED, NULL,
671 #endif
672                 "( OLcfgGlAt:82 NAME 'olcTLSCRLFile' "
673                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
674         { "TLSRandFile", NULL, 0, 0, 0,
675 #ifdef HAVE_TLS
676                 CFG_TLS_RAND|ARG_STRING|ARG_MAGIC, &config_tls_option,
677 #else
678                 ARG_IGNORED, NULL,
679 #endif
680                 "( OLcfgGlAt:74 NAME 'olcTLSRandFile' "
681                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
682         { "TLSVerifyClient", NULL, 0, 0, 0,
683 #ifdef HAVE_TLS
684                 CFG_TLS_VERIFY|ARG_STRING|ARG_MAGIC, &config_tls_config,
685 #else
686                 ARG_IGNORED, NULL,
687 #endif
688                 "( OLcfgGlAt:75 NAME 'olcTLSVerifyClient' "
689                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
690         { "TLSDHParamFile", NULL, 0, 0, 0,
691 #ifdef HAVE_TLS
692                 CFG_TLS_DH_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
693 #else
694                 ARG_IGNORED, NULL,
695 #endif
696                 "( OLcfgGlAt:77 NAME 'olcTLSDHParamFile' "
697                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
698         { "TLSProtocolMin",     NULL, 0, 0, 0,
699 #ifdef HAVE_TLS
700                 CFG_TLS_PROTOCOL_MIN|ARG_STRING|ARG_MAGIC, &config_tls_config,
701 #else
702                 ARG_IGNORED, NULL,
703 #endif
704                 "( OLcfgGlAt:87 NAME 'olcTLSProtocolMin' "
705                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
706         { "tool-threads", "count", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_TTHREADS,
707                 &config_generic, "( OLcfgGlAt:80 NAME 'olcToolThreads' "
708                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
709         { "ucdata-path", "path", 2, 2, 0, ARG_IGNORED,
710                 NULL, NULL, NULL, NULL },
711         { "updatedn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
712                 &config_updatedn, "( OLcfgDbAt:0.12 NAME 'olcUpdateDN' "
713                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
714         { "updateref", "url", 2, 2, 0, ARG_DB|ARG_MAGIC,
715                 &config_updateref, "( OLcfgDbAt:0.13 NAME 'olcUpdateRef' "
716                         "EQUALITY caseIgnoreMatch "
717                         "SUP labeledURI )", NULL, NULL },
718         { "writetimeout", "timeout", 2, 2, 0, ARG_INT,
719                 &global_writetimeout, "( OLcfgGlAt:88 NAME 'olcWriteTimeout' "
720                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
721         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
722                 NULL, NULL, NULL, NULL }
723 };
724
725 /* Need to no-op this keyword for dynamic config */
726 ConfigTable olcDatabaseDummy[] = {
727         { "", "", 0, 0, 0, ARG_IGNORED,
728                 NULL, "( OLcfgGlAt:13 NAME 'olcDatabase' "
729                         "DESC 'The backend type for a database instance' "
730                         "SUP olcBackend SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
731         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
732 };
733
734 /* Routines to check if a child can be added to this type */
735 static ConfigLDAPadd cfAddSchema, cfAddInclude, cfAddDatabase,
736         cfAddBackend, cfAddModule, cfAddOverlay;
737
738 /* NOTE: be careful when defining array members
739  * that can be conditionally compiled */
740 #define CFOC_GLOBAL     cf_ocs[1]
741 #define CFOC_SCHEMA     cf_ocs[2]
742 #define CFOC_BACKEND    cf_ocs[3]
743 #define CFOC_DATABASE   cf_ocs[4]
744 #define CFOC_OVERLAY    cf_ocs[5]
745 #define CFOC_INCLUDE    cf_ocs[6]
746 #define CFOC_FRONTEND   cf_ocs[7]
747 #ifdef SLAPD_MODULES
748 #define CFOC_MODULE     cf_ocs[8]
749 #endif /* SLAPD_MODULES */
750
751 static ConfigOCs cf_ocs[] = {
752         { "( OLcfgGlOc:0 "
753                 "NAME 'olcConfig' "
754                 "DESC 'OpenLDAP configuration object' "
755                 "ABSTRACT SUP top )", Cft_Abstract, NULL },
756         { "( OLcfgGlOc:1 "
757                 "NAME 'olcGlobal' "
758                 "DESC 'OpenLDAP Global configuration options' "
759                 "SUP olcConfig STRUCTURAL "
760                 "MAY ( cn $ olcConfigFile $ olcConfigDir $ olcAllows $ olcArgsFile $ "
761                  "olcAttributeOptions $ olcAuthIDRewrite $ "
762                  "olcAuthzPolicy $ olcAuthzRegexp $ olcConcurrency $ "
763                  "olcConnMaxPending $ olcConnMaxPendingAuth $ "
764                  "olcDisallows $ olcGentleHUP $ olcIdleTimeout $ "
765                  "olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
766                  "olcIndexSubstrAnyLen $ olcIndexSubstrAnyStep $ olcIndexIntLen $ "
767                  "olcLocalSSF $ olcLogFile $ olcLogLevel $ "
768                  "olcPasswordCryptSaltFormat $ olcPasswordHash $ olcPidFile $ "
769                  "olcPluginLogFile $ olcReadOnly $ olcReferral $ "
770                  "olcReplogFile $ olcRequires $ olcRestrict $ olcReverseLookup $ "
771                  "olcRootDSE $ "
772                  "olcSaslAuxprops $ olcSaslHost $ olcSaslRealm $ olcSaslSecProps $ "
773                  "olcSecurity $ olcServerID $ olcSizeLimit $ "
774                  "olcSockbufMaxIncoming $ olcSockbufMaxIncomingAuth $ "
775                  "olcThreads $ olcTimeLimit $ olcTLSCACertificateFile $ "
776                  "olcTLSCACertificatePath $ olcTLSCertificateFile $ "
777                  "olcTLSCertificateKeyFile $ olcTLSCipherSuite $ olcTLSCRLCheck $ "
778                  "olcTLSRandFile $ olcTLSVerifyClient $ olcTLSDHParamFile $ "
779                  "olcTLSCRLFile $ olcToolThreads $ olcWriteTimeout $ "
780                  "olcObjectIdentifier $ olcAttributeTypes $ olcObjectClasses $ "
781                  "olcDitContentRules $ olcLdapSyntaxes ) )", Cft_Global },
782         { "( OLcfgGlOc:2 "
783                 "NAME 'olcSchemaConfig' "
784                 "DESC 'OpenLDAP schema object' "
785                 "SUP olcConfig STRUCTURAL "
786                 "MAY ( cn $ olcObjectIdentifier $ olcAttributeTypes $ "
787                  "olcObjectClasses $ olcDitContentRules $ olcLdapSyntaxes ) )",
788                         Cft_Schema, NULL, cfAddSchema },
789         { "( OLcfgGlOc:3 "
790                 "NAME 'olcBackendConfig' "
791                 "DESC 'OpenLDAP Backend-specific options' "
792                 "SUP olcConfig STRUCTURAL "
793                 "MUST olcBackend )", Cft_Backend, NULL, cfAddBackend },
794         { "( OLcfgGlOc:4 "
795                 "NAME 'olcDatabaseConfig' "
796                 "DESC 'OpenLDAP Database-specific options' "
797                 "SUP olcConfig STRUCTURAL "
798                 "MUST olcDatabase "
799                 "MAY ( olcHidden $ olcSuffix $ olcSubordinate $ olcAccess $ "
800                  "olcAddContentAcl $ olcLastMod $ olcLimits $ "
801                  "olcMaxDerefDepth $ olcPlugin $ olcReadOnly $ olcReplica $ "
802                  "olcReplicaArgsFile $ olcReplicaPidFile $ olcReplicationInterval $ "
803                  "olcReplogFile $ olcRequires $ olcRestrict $ olcRootDN $ olcRootPW $ "
804                  "olcSchemaDN $ olcSecurity $ olcSizeLimit $ olcSyncrepl $ "
805                  "olcTimeLimit $ olcUpdateDN $ olcUpdateRef $ olcMirrorMode $ "
806                  "olcMonitoring ) )",
807                         Cft_Database, NULL, cfAddDatabase },
808         { "( OLcfgGlOc:5 "
809                 "NAME 'olcOverlayConfig' "
810                 "DESC 'OpenLDAP Overlay-specific options' "
811                 "SUP olcConfig STRUCTURAL "
812                 "MUST olcOverlay )", Cft_Overlay, NULL, cfAddOverlay },
813         { "( OLcfgGlOc:6 "
814                 "NAME 'olcIncludeFile' "
815                 "DESC 'OpenLDAP configuration include file' "
816                 "SUP olcConfig STRUCTURAL "
817                 "MUST olcInclude "
818                 "MAY ( cn $ olcRootDSE ) )",
819                 /* Used to be Cft_Include, that def has been removed */
820                 Cft_Abstract, NULL, cfAddInclude },
821         /* This should be STRUCTURAL like all the other database classes, but
822          * that would mean inheriting all of the olcDatabaseConfig attributes,
823          * which causes them to be merged twice in config_build_entry.
824          */
825         { "( OLcfgGlOc:7 "
826                 "NAME 'olcFrontendConfig' "
827                 "DESC 'OpenLDAP frontend configuration' "
828                 "AUXILIARY "
829                 "MAY ( olcDefaultSearchBase $ olcPasswordHash $ olcSortVals ) )",
830                 Cft_Database, NULL, NULL },
831 #ifdef SLAPD_MODULES
832         { "( OLcfgGlOc:8 "
833                 "NAME 'olcModuleList' "
834                 "DESC 'OpenLDAP dynamic module info' "
835                 "SUP olcConfig STRUCTURAL "
836                 "MAY ( cn $ olcModulePath $ olcModuleLoad ) )",
837                 Cft_Module, NULL, cfAddModule },
838 #endif
839         { NULL, 0, NULL }
840 };
841
842 typedef struct ServerID {
843         struct ServerID *si_next;
844         struct berval si_url;
845         int si_num;
846 } ServerID;
847
848 static ServerID *sid_list;
849
850 typedef struct voidList {
851         struct voidList *vl_next;
852         void *vl_ptr;
853 } voidList;
854
855 typedef struct ADlist {
856         struct ADlist *al_next;
857         AttributeDescription *al_desc;
858 } ADlist;
859
860 static ADlist *sortVals;
861
862 static int
863 config_generic(ConfigArgs *c) {
864         int i;
865
866         if ( c->op == SLAP_CONFIG_EMIT ) {
867                 int rc = 0;
868                 switch(c->type) {
869                 case CFG_CONCUR:
870                         c->value_int = ldap_pvt_thread_get_concurrency();
871                         break;
872                 case CFG_THREADS:
873                         c->value_int = connection_pool_max;
874                         break;
875                 case CFG_TTHREADS:
876                         c->value_int = slap_tool_thread_max;
877                         break;
878                 case CFG_SALT:
879                         if ( passwd_salt )
880                                 c->value_string = ch_strdup( passwd_salt );
881                         else
882                                 rc = 1;
883                         break;
884                 case CFG_LIMITS:
885                         if ( c->be->be_limits ) {
886                                 char buf[4096*3];
887                                 struct berval bv;
888
889                                 for ( i=0; c->be->be_limits[i]; i++ ) {
890                                         bv.bv_len = snprintf( buf, sizeof( buf ), SLAP_X_ORDERED_FMT, i );
891                                         if ( bv.bv_len >= sizeof( buf ) ) {
892                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
893                                                 c->rvalue_vals = NULL;
894                                                 rc = 1;
895                                                 break;
896                                         }
897                                         bv.bv_val = buf + bv.bv_len;
898                                         limits_unparse( c->be->be_limits[i], &bv,
899                                                         sizeof( buf ) - ( bv.bv_val - buf ) );
900                                         bv.bv_len += bv.bv_val - buf;
901                                         bv.bv_val = buf;
902                                         value_add_one( &c->rvalue_vals, &bv );
903                                 }
904                         }
905                         if ( !c->rvalue_vals ) rc = 1;
906                         break;
907                 case CFG_RO:
908                         c->value_int = (c->be->be_restrictops & SLAP_RESTRICT_READONLY);
909                         break;
910                 case CFG_AZPOLICY:
911                         c->value_string = ch_strdup( slap_sasl_getpolicy());
912                         break;
913                 case CFG_AZREGEXP:
914                         slap_sasl_regexp_unparse( &c->rvalue_vals );
915                         if ( !c->rvalue_vals ) rc = 1;
916                         break;
917 #ifdef HAVE_CYRUS_SASL
918                 case CFG_SASLSECP: {
919                         struct berval bv = BER_BVNULL;
920                         slap_sasl_secprops_unparse( &bv );
921                         if ( !BER_BVISNULL( &bv )) {
922                                 ber_bvarray_add( &c->rvalue_vals, &bv );
923                         } else {
924                                 rc = 1;
925                         }
926                         }
927                         break;
928 #endif
929                 case CFG_DEPTH:
930                         c->value_int = c->be->be_max_deref_depth;
931                         break;
932                 case CFG_HIDDEN:
933                         if ( SLAP_DBHIDDEN( c->be )) {
934                                 c->value_int = 1;
935                         } else {
936                                 rc = 1;
937                         }
938                         break;
939                 case CFG_OID: {
940                         ConfigFile *cf = c->ca_private;
941                         if ( !cf )
942                                 oidm_unparse( &c->rvalue_vals, NULL, NULL, 1 );
943                         else if ( cf->c_om_head )
944                                 oidm_unparse( &c->rvalue_vals, cf->c_om_head,
945                                         cf->c_om_tail, 0 );
946                         if ( !c->rvalue_vals )
947                                 rc = 1;
948                         }
949                         break;
950                 case CFG_ATOPT:
951                         ad_unparse_options( &c->rvalue_vals );
952                         break;
953                 case CFG_OC: {
954                         ConfigFile *cf = c->ca_private;
955                         if ( !cf )
956                                 oc_unparse( &c->rvalue_vals, NULL, NULL, 1 );
957                         else if ( cf->c_oc_head )
958                                 oc_unparse( &c->rvalue_vals, cf->c_oc_head,
959                                         cf->c_oc_tail, 0 );
960                         if ( !c->rvalue_vals )
961                                 rc = 1;
962                         }
963                         break;
964                 case CFG_ATTR: {
965                         ConfigFile *cf = c->ca_private;
966                         if ( !cf )
967                                 at_unparse( &c->rvalue_vals, NULL, NULL, 1 );
968                         else if ( cf->c_at_head )
969                                 at_unparse( &c->rvalue_vals, cf->c_at_head,
970                                         cf->c_at_tail, 0 );
971                         if ( !c->rvalue_vals )
972                                 rc = 1;
973                         }
974                         break;
975                 case CFG_SYNTAX: {
976                         ConfigFile *cf = c->ca_private;
977                         if ( !cf )
978                                 syn_unparse( &c->rvalue_vals, NULL, NULL, 1 );
979                         else if ( cf->c_syn_head )
980                                 syn_unparse( &c->rvalue_vals, cf->c_syn_head,
981                                         cf->c_syn_tail, 0 );
982                         if ( !c->rvalue_vals )
983                                 rc = 1;
984                         }
985                         break;
986                 case CFG_DIT: {
987                         ConfigFile *cf = c->ca_private;
988                         if ( !cf )
989                                 cr_unparse( &c->rvalue_vals, NULL, NULL, 1 );
990                         else if ( cf->c_cr_head )
991                                 cr_unparse( &c->rvalue_vals, cf->c_cr_head,
992                                         cf->c_cr_tail, 0 );
993                         if ( !c->rvalue_vals )
994                                 rc = 1;
995                         }
996                         break;
997                         
998                 case CFG_ACL: {
999                         AccessControl *a;
1000                         char *src, *dst, ibuf[11];
1001                         struct berval bv, abv;
1002                         for (i=0, a=c->be->be_acl; a; i++,a=a->acl_next) {
1003                                 abv.bv_len = snprintf( ibuf, sizeof( ibuf ), SLAP_X_ORDERED_FMT, i );
1004                                 if ( abv.bv_len >= sizeof( ibuf ) ) {
1005                                         ber_bvarray_free_x( c->rvalue_vals, NULL );
1006                                         c->rvalue_vals = NULL;
1007                                         i = 0;
1008                                         break;
1009                                 }
1010                                 acl_unparse( a, &bv );
1011                                 abv.bv_val = ch_malloc( abv.bv_len + bv.bv_len + 1 );
1012                                 AC_MEMCPY( abv.bv_val, ibuf, abv.bv_len );
1013                                 /* Turn TAB / EOL into plain space */
1014                                 for (src=bv.bv_val,dst=abv.bv_val+abv.bv_len; *src; src++) {
1015                                         if (isspace((unsigned char)*src)) *dst++ = ' ';
1016                                         else *dst++ = *src;
1017                                 }
1018                                 *dst = '\0';
1019                                 if (dst[-1] == ' ') {
1020                                         dst--;
1021                                         *dst = '\0';
1022                                 }
1023                                 abv.bv_len = dst - abv.bv_val;
1024                                 ber_bvarray_add( &c->rvalue_vals, &abv );
1025                         }
1026                         rc = (!i);
1027                         break;
1028                 }
1029                 case CFG_ACL_ADD:
1030                         c->value_int = (SLAP_DBACL_ADD(c->be) != 0);
1031                         break;
1032                 case CFG_ROOTDSE: {
1033                         ConfigFile *cf = c->ca_private;
1034                         if ( cf->c_dseFiles ) {
1035                                 value_add( &c->rvalue_vals, cf->c_dseFiles );
1036                         } else {
1037                                 rc = 1;
1038                         }
1039                         }
1040                         break;
1041                 case CFG_SERVERID:
1042                         if ( sid_list ) {
1043                                 ServerID *si;
1044                                 struct berval bv;
1045
1046                                 for ( si = sid_list; si; si=si->si_next ) {
1047                                         assert( si->si_num >= 0 && si->si_num <= SLAP_SYNC_SID_MAX );
1048                                         if ( !BER_BVISEMPTY( &si->si_url )) {
1049                                                 bv.bv_len = si->si_url.bv_len + 6;
1050                                                 bv.bv_val = ch_malloc( bv.bv_len );
1051                                                 bv.bv_len = sprintf( bv.bv_val, "%d %s", si->si_num,
1052                                                         si->si_url.bv_val );
1053                                                 ber_bvarray_add( &c->rvalue_vals, &bv );
1054                                         } else {
1055                                                 char buf[5];
1056                                                 bv.bv_val = buf;
1057                                                 bv.bv_len = sprintf( buf, "%d", si->si_num );
1058                                                 value_add_one( &c->rvalue_vals, &bv );
1059                                         }
1060                                 }
1061                         } else {
1062                                 rc = 1;
1063                         }
1064                         break;
1065                 case CFG_LOGFILE:
1066                         if ( logfileName )
1067                                 c->value_string = ch_strdup( logfileName );
1068                         else
1069                                 rc = 1;
1070                         break;
1071                 case CFG_LASTMOD:
1072                         c->value_int = (SLAP_NOLASTMOD(c->be) == 0);
1073                         break;
1074                 case CFG_MIRRORMODE:
1075                         if ( SLAP_SHADOW(c->be))
1076                                 c->value_int = (SLAP_SINGLE_SHADOW(c->be) == 0);
1077                         else
1078                                 rc = 1;
1079                         break;
1080                 case CFG_MONITORING:
1081                         c->value_int = (SLAP_DBMONITORING(c->be) != 0);
1082                         break;
1083                 case CFG_SSTR_IF_MAX:
1084                         c->value_int = index_substr_if_maxlen;
1085                         break;
1086                 case CFG_SSTR_IF_MIN:
1087                         c->value_int = index_substr_if_minlen;
1088                         break;
1089                 case CFG_IX_INTLEN:
1090                         c->value_int = index_intlen;
1091                         break;
1092                 case CFG_SORTVALS: {
1093                         ADlist *sv;
1094                         rc = 1;
1095                         for ( sv = sortVals; sv; sv = sv->al_next ) {
1096                                 value_add_one( &c->rvalue_vals, &sv->al_desc->ad_cname );
1097                                 rc = 0;
1098                         }
1099                         } break;
1100 #ifdef SLAPD_MODULES
1101                 case CFG_MODLOAD: {
1102                         ModPaths *mp = c->ca_private;
1103                         if (mp->mp_loads) {
1104                                 int i;
1105                                 for (i=0; !BER_BVISNULL(&mp->mp_loads[i]); i++) {
1106                                         struct berval bv;
1107                                         bv.bv_val = c->log;
1108                                         bv.bv_len = snprintf( bv.bv_val, sizeof( c->log ),
1109                                                 SLAP_X_ORDERED_FMT "%s", i,
1110                                                 mp->mp_loads[i].bv_val );
1111                                         if ( bv.bv_len >= sizeof( c->log ) ) {
1112                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
1113                                                 c->rvalue_vals = NULL;
1114                                                 break;
1115                                         }
1116                                         value_add_one( &c->rvalue_vals, &bv );
1117                                 }
1118                         }
1119
1120                         rc = c->rvalue_vals ? 0 : 1;
1121                         }
1122                         break;
1123                 case CFG_MODPATH: {
1124                         ModPaths *mp = c->ca_private;
1125                         if ( !BER_BVISNULL( &mp->mp_path ))
1126                                 value_add_one( &c->rvalue_vals, &mp->mp_path );
1127
1128                         rc = c->rvalue_vals ? 0 : 1;
1129                         }
1130                         break;
1131 #endif
1132 #ifdef LDAP_SLAPI
1133                 case CFG_PLUGIN:
1134                         slapi_int_plugin_unparse( c->be, &c->rvalue_vals );
1135                         if ( !c->rvalue_vals ) rc = 1;
1136                         break;
1137 #endif
1138 #ifdef SLAP_AUTH_REWRITE
1139                 case CFG_REWRITE:
1140                         if ( authz_rewrites ) {
1141                                 struct berval bv, idx;
1142                                 char ibuf[32];
1143                                 int i;
1144
1145                                 idx.bv_val = ibuf;
1146                                 for ( i=0; !BER_BVISNULL( &authz_rewrites[i] ); i++ ) {
1147                                         idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), SLAP_X_ORDERED_FMT, i );
1148                                         if ( idx.bv_len >= sizeof( ibuf ) ) {
1149                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
1150                                                 c->rvalue_vals = NULL;
1151                                                 break;
1152                                         }
1153                                         bv.bv_len = idx.bv_len + authz_rewrites[i].bv_len;
1154                                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
1155                                         AC_MEMCPY( bv.bv_val, idx.bv_val, idx.bv_len );
1156                                         AC_MEMCPY( &bv.bv_val[ idx.bv_len ],
1157                                                 authz_rewrites[i].bv_val,
1158                                                 authz_rewrites[i].bv_len + 1 );
1159                                         ber_bvarray_add( &c->rvalue_vals, &bv );
1160                                 }
1161                         }
1162                         if ( !c->rvalue_vals ) rc = 1;
1163                         break;
1164 #endif
1165                 default:
1166                         rc = 1;
1167                 }
1168                 return rc;
1169         } else if ( c->op == LDAP_MOD_DELETE ) {
1170                 int rc = 0;
1171                 switch(c->type) {
1172                 /* single-valued attrs, no-ops */
1173                 case CFG_CONCUR:
1174                 case CFG_THREADS:
1175                 case CFG_TTHREADS:
1176                 case CFG_RO:
1177                 case CFG_AZPOLICY:
1178                 case CFG_DEPTH:
1179                 case CFG_LASTMOD:
1180                 case CFG_MIRRORMODE:
1181                 case CFG_MONITORING:
1182                 case CFG_SASLSECP:
1183                 case CFG_SSTR_IF_MAX:
1184                 case CFG_SSTR_IF_MIN:
1185                 case CFG_ACL_ADD:
1186                         break;
1187
1188                 /* no-ops, requires slapd restart */
1189                 case CFG_PLUGIN:
1190                 case CFG_MODLOAD:
1191                 case CFG_AZREGEXP:
1192                 case CFG_REWRITE:
1193                         snprintf(c->log, sizeof( c->log ), "change requires slapd restart");
1194                         break;
1195
1196                 case CFG_SALT:
1197                         ch_free( passwd_salt );
1198                         passwd_salt = NULL;
1199                         break;
1200
1201                 case CFG_LOGFILE:
1202                         ch_free( logfileName );
1203                         logfileName = NULL;
1204                         if ( logfile ) {
1205                                 fclose( logfile );
1206                                 logfile = NULL;
1207                         }
1208                         break;
1209
1210                 case CFG_SERVERID: {
1211                         ServerID *si, **sip;
1212
1213                         for ( i=0, si = sid_list, sip = &sid_list;
1214                                 si; si = *sip, i++ ) {
1215                                 if ( c->valx == -1 || i == c->valx ) {
1216                                         *sip = si->si_next;
1217                                         ch_free( si );
1218                                         if ( c->valx >= 0 )
1219                                                 break;
1220                                 } else {
1221                                         sip = &si->si_next;
1222                                 }
1223                         }
1224                         }
1225                         break;
1226                 case CFG_HIDDEN:
1227                         c->be->be_flags &= ~SLAP_DBFLAG_HIDDEN;
1228                         break;
1229
1230                 case CFG_IX_INTLEN:
1231                         index_intlen = SLAP_INDEX_INTLEN_DEFAULT;
1232                         index_intlen_strlen = SLAP_INDEX_INTLEN_STRLEN(
1233                                 SLAP_INDEX_INTLEN_DEFAULT );
1234                         break;
1235
1236                 case CFG_ACL:
1237                         if ( c->valx < 0 ) {
1238                                 acl_destroy( c->be->be_acl );
1239                                 c->be->be_acl = NULL;
1240
1241                         } else {
1242                                 AccessControl **prev, *a;
1243                                 int i;
1244                                 for (i=0, prev = &c->be->be_acl; i < c->valx;
1245                                         i++ ) {
1246                                         a = *prev;
1247                                         prev = &a->acl_next;
1248                                 }
1249                                 a = *prev;
1250                                 *prev = a->acl_next;
1251                                 acl_free( a );
1252                         }
1253                         break;
1254
1255                 case CFG_OC: {
1256                         CfEntryInfo *ce;
1257                         /* Can be NULL when undoing a failed add */
1258                         if ( c->ca_entry ) {
1259                                 ce = c->ca_entry->e_private;
1260                                 /* can't modify the hardcoded schema */
1261                                 if ( ce->ce_parent->ce_type == Cft_Global )
1262                                         return 1;
1263                                 }
1264                         }
1265                         cfn = c->ca_private;
1266                         if ( c->valx < 0 ) {
1267                                 ObjectClass *oc;
1268
1269                                 for( oc = cfn->c_oc_head; oc; oc_next( &oc )) {
1270                                         oc_delete( oc );
1271                                         if ( oc  == cfn->c_oc_tail )
1272                                                 break;
1273                                 }
1274                                 cfn->c_oc_head = cfn->c_oc_tail = NULL;
1275                         } else {
1276                                 ObjectClass *oc, *prev = NULL;
1277
1278                                 for ( i=0, oc=cfn->c_oc_head; i<c->valx; i++) {
1279                                         prev = oc;
1280                                         oc_next( &oc );
1281                                 }
1282                                 oc_delete( oc );
1283                                 if ( cfn->c_oc_tail == oc ) {
1284                                         cfn->c_oc_tail = prev;
1285                                 }
1286                                 if ( cfn->c_oc_head == oc ) {
1287                                         oc_next( &oc );
1288                                         cfn->c_oc_head = oc;
1289                                 }
1290                         }
1291                         break;
1292
1293                 case CFG_ATTR: {
1294                         CfEntryInfo *ce;
1295                         /* Can be NULL when undoing a failed add */
1296                         if ( c->ca_entry ) {
1297                                 ce = c->ca_entry->e_private;
1298                                 /* can't modify the hardcoded schema */
1299                                 if ( ce->ce_parent->ce_type == Cft_Global )
1300                                         return 1;
1301                                 }
1302                         }
1303                         cfn = c->ca_private;
1304                         if ( c->valx < 0 ) {
1305                                 AttributeType *at;
1306
1307                                 for( at = cfn->c_at_head; at; at_next( &at )) {
1308                                         at_delete( at );
1309                                         if ( at  == cfn->c_at_tail )
1310                                                 break;
1311                                 }
1312                                 cfn->c_at_head = cfn->c_at_tail = NULL;
1313                         } else {
1314                                 AttributeType *at, *prev = NULL;
1315
1316                                 for ( i=0, at=cfn->c_at_head; i<c->valx; i++) {
1317                                         prev = at;
1318                                         at_next( &at );
1319                                 }
1320                                 at_delete( at );
1321                                 if ( cfn->c_at_tail == at ) {
1322                                         cfn->c_at_tail = prev;
1323                                 }
1324                                 if ( cfn->c_at_head == at ) {
1325                                         at_next( &at );
1326                                         cfn->c_at_head = at;
1327                                 }
1328                         }
1329                         break;
1330
1331                 case CFG_SYNTAX: {
1332                         CfEntryInfo *ce;
1333                         /* Can be NULL when undoing a failed add */
1334                         if ( c->ca_entry ) {
1335                                 ce = c->ca_entry->e_private;
1336                                 /* can't modify the hardcoded schema */
1337                                 if ( ce->ce_parent->ce_type == Cft_Global )
1338                                         return 1;
1339                                 }
1340                         }
1341                         cfn = c->ca_private;
1342                         if ( c->valx < 0 ) {
1343                                 Syntax *syn;
1344
1345                                 for( syn = cfn->c_syn_head; syn; syn_next( &syn )) {
1346                                         syn_delete( syn );
1347                                         if ( syn == cfn->c_syn_tail )
1348                                                 break;
1349                                 }
1350                                 cfn->c_syn_head = cfn->c_syn_tail = NULL;
1351                         } else {
1352                                 Syntax *syn, *prev = NULL;
1353
1354                                 for ( i = 0, syn = cfn->c_syn_head; i < c->valx; i++) {
1355                                         prev = syn;
1356                                         syn_next( &syn );
1357                                 }
1358                                 syn_delete( syn );
1359                                 if ( cfn->c_syn_tail == syn ) {
1360                                         cfn->c_syn_tail = prev;
1361                                 }
1362                                 if ( cfn->c_syn_head == syn ) {
1363                                         syn_next( &syn );
1364                                         cfn->c_syn_head = syn;
1365                                 }
1366                         }
1367                         break;
1368                 case CFG_SORTVALS:
1369                         if ( c->valx < 0 ) {
1370                                 ADlist *sv;
1371                                 for ( sv = sortVals; sv; sv = sortVals ) {
1372                                         sortVals = sv->al_next;
1373                                         sv->al_desc->ad_type->sat_flags &= ~SLAP_AT_SORTED_VAL;
1374                                         ch_free( sv );
1375                                 }
1376                         } else {
1377                                 ADlist *sv, **prev;
1378                                 int i = 0;
1379
1380                                 for ( prev = &sortVals, sv = sortVals; i < c->valx; i++ ) {
1381                                         prev = &sv->al_next;
1382                                         sv = sv->al_next;
1383                                 }
1384                                 sv->al_desc->ad_type->sat_flags &= ~SLAP_AT_SORTED_VAL;
1385                                 *prev = sv->al_next;
1386                                 ch_free( sv );
1387                         }
1388                         break;
1389
1390                 case CFG_LIMITS:
1391                         /* FIXME: there is no limits_free function */
1392                         if ( c->valx < 0 ) {
1393                                 limits_destroy( c->be->be_limits );
1394                                 c->be->be_limits = NULL;
1395
1396                         } else {
1397                                 int cnt, num = -1;
1398
1399                                 if ( c->be->be_limits ) {
1400                                         for ( num = 0; c->be->be_limits[ num ]; num++ )
1401                                                 /* just count */ ;
1402                                 }
1403
1404                                 if ( c->valx >= num ) {
1405                                         return 1;
1406                                 }
1407
1408                                 if ( num == 1 ) {
1409                                         limits_destroy( c->be->be_limits );
1410                                         c->be->be_limits = NULL;
1411
1412                                 } else {
1413                                         limits_free_one( c->be->be_limits[ c->valx ] );
1414
1415                                         for ( cnt = c->valx; cnt < num; cnt++ ) {
1416                                                 c->be->be_limits[ cnt ] = c->be->be_limits[ cnt + 1 ];
1417                                         }
1418                                 }
1419                         }
1420                         break;
1421
1422                 case CFG_ATOPT:
1423                         /* FIXME: there is no ad_option_free function */
1424                 case CFG_ROOTDSE:
1425                         /* FIXME: there is no way to remove attributes added by
1426                                 a DSE file */
1427                 case CFG_OID:
1428                 case CFG_DIT:
1429                 case CFG_MODPATH:
1430                 default:
1431                         rc = 1;
1432                         break;
1433                 }
1434                 return rc;
1435         }
1436
1437         switch(c->type) {
1438                 case CFG_BACKEND:
1439                         if(!(c->bi = backend_info(c->argv[1]))) {
1440                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> failed init", c->argv[0] );
1441                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
1442                                         c->log, c->cr_msg, c->argv[1] );
1443                                 return(1);
1444                         }
1445                         break;
1446
1447                 case CFG_DATABASE:
1448                         c->bi = NULL;
1449                         /* NOTE: config is always the first backend!
1450                          */
1451                         if ( !strcasecmp( c->argv[1], "config" )) {
1452                                 c->be = LDAP_STAILQ_FIRST(&backendDB);
1453                         } else if ( !strcasecmp( c->argv[1], "frontend" )) {
1454                                 c->be = frontendDB;
1455                         } else {
1456                                 c->be = backend_db_init(c->argv[1], NULL, c->valx, &c->reply);
1457                                 if ( !c->be ) {
1458                                         if ( c->cr_msg[0] == 0 )
1459                                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> failed init", c->argv[0] );
1460                                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n", c->log, c->cr_msg, c->argv[1] );
1461                                         return(1);
1462                                 }
1463                         }
1464                         break;
1465
1466                 case CFG_CONCUR:
1467                         ldap_pvt_thread_set_concurrency(c->value_int);
1468                         break;
1469
1470                 case CFG_THREADS:
1471                         if ( c->value_int < 2 ) {
1472                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1473                                         "threads=%d smaller than minimum value 2",
1474                                         c->value_int );
1475                                 Debug(LDAP_DEBUG_ANY, "%s: %s.\n",
1476                                         c->log, c->cr_msg, 0 );
1477                                 return 1;
1478
1479                         } else if ( c->value_int > 2 * SLAP_MAX_WORKER_THREADS ) {
1480                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1481                                         "warning, threads=%d larger than twice the default (2*%d=%d); YMMV",
1482                                         c->value_int, SLAP_MAX_WORKER_THREADS, 2 * SLAP_MAX_WORKER_THREADS );
1483                                 Debug(LDAP_DEBUG_ANY, "%s: %s.\n",
1484                                         c->log, c->cr_msg, 0 );
1485                         }
1486                         if ( slapMode & SLAP_SERVER_MODE )
1487                                 ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
1488                         connection_pool_max = c->value_int;     /* save for reference */
1489                         break;
1490
1491                 case CFG_TTHREADS:
1492                         if ( slapMode & SLAP_TOOL_MODE )
1493                                 ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
1494                         slap_tool_thread_max = c->value_int;    /* save for reference */
1495                         break;
1496
1497                 case CFG_SALT:
1498                         if ( passwd_salt ) ch_free( passwd_salt );
1499                         passwd_salt = c->value_string;
1500                         lutil_salt_format(passwd_salt);
1501                         break;
1502
1503                 case CFG_LIMITS:
1504                         if(limits_parse(c->be, c->fname, c->lineno, c->argc, c->argv))
1505                                 return(1);
1506                         break;
1507
1508                 case CFG_RO:
1509                         if(c->value_int)
1510                                 c->be->be_restrictops |= SLAP_RESTRICT_READONLY;
1511                         else
1512                                 c->be->be_restrictops &= ~SLAP_RESTRICT_READONLY;
1513                         break;
1514
1515                 case CFG_AZPOLICY:
1516                         ch_free(c->value_string);
1517                         if (slap_sasl_setpolicy( c->argv[1] )) {
1518                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse value", c->argv[0] );
1519                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1520                                         c->log, c->cr_msg, c->argv[1] );
1521                                 return(1);
1522                         }
1523                         break;
1524                 
1525                 case CFG_AZREGEXP:
1526                         if (slap_sasl_regexp_config( c->argv[1], c->argv[2] ))
1527                                 return(1);
1528                         break;
1529                                 
1530 #ifdef HAVE_CYRUS_SASL
1531                 case CFG_SASLSECP:
1532                         {
1533                         char *txt = slap_sasl_secprops( c->argv[1] );
1534                         if ( txt ) {
1535                                 snprintf( c->cr_msg, sizeof(c->cr_msg), "<%s> %s",
1536                                         c->argv[0], txt );
1537                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
1538                                 return(1);
1539                         }
1540                         break;
1541                         }
1542 #endif
1543
1544                 case CFG_DEPTH:
1545                         c->be->be_max_deref_depth = c->value_int;
1546                         break;
1547
1548                 case CFG_OID: {
1549                         OidMacro *om;
1550
1551                         if ( c->op == LDAP_MOD_ADD && c->ca_private && cfn != c->ca_private )
1552                                 cfn = c->ca_private;
1553                         if(parse_oidm(c, 1, &om))
1554                                 return(1);
1555                         if (!cfn->c_om_head) cfn->c_om_head = om;
1556                         cfn->c_om_tail = om;
1557                         }
1558                         break;
1559
1560                 case CFG_OC: {
1561                         ObjectClass *oc, *prev;
1562
1563                         if ( c->op == LDAP_MOD_ADD && c->ca_private && cfn != c->ca_private )
1564                                 cfn = c->ca_private;
1565                         if ( c->valx < 0 ) {
1566                                 prev = cfn->c_oc_tail;
1567                         } else {
1568                                 prev = NULL;
1569                                 /* If adding anything after the first, prev is easy */
1570                                 if ( c->valx ) {
1571                                         int i;
1572                                         for (i=0, oc = cfn->c_oc_head; i<c->valx; i++) {
1573                                                 prev = oc;
1574                                                 oc_next( &oc );
1575                                         }
1576                                 } else
1577                                 /* If adding the first, and head exists, find its prev */
1578                                         if (cfn->c_oc_head) {
1579                                         for ( oc_start( &oc ); oc != cfn->c_oc_head; ) {
1580                                                 prev = oc;
1581                                                 oc_next( &oc );
1582                                         }
1583                                 }
1584                                 /* else prev is NULL, append to end of global list */
1585                         }
1586                         if(parse_oc(c, &oc, prev)) return(1);
1587                         if (!cfn->c_oc_head) cfn->c_oc_head = oc;
1588                         if (cfn->c_oc_tail == prev) cfn->c_oc_tail = oc;
1589                         }
1590                         break;
1591
1592                 case CFG_ATTR: {
1593                         AttributeType *at, *prev;
1594
1595                         if ( c->op == LDAP_MOD_ADD && c->ca_private && cfn != c->ca_private )
1596                                 cfn = c->ca_private;
1597                         if ( c->valx < 0 ) {
1598                                 prev = cfn->c_at_tail;
1599                         } else {
1600                                 prev = NULL;
1601                                 /* If adding anything after the first, prev is easy */
1602                                 if ( c->valx ) {
1603                                         int i;
1604                                         for (i=0, at = cfn->c_at_head; i<c->valx; i++) {
1605                                                 prev = at;
1606                                                 at_next( &at );
1607                                         }
1608                                 } else
1609                                 /* If adding the first, and head exists, find its prev */
1610                                         if (cfn->c_at_head) {
1611                                         for ( at_start( &at ); at != cfn->c_at_head; ) {
1612                                                 prev = at;
1613                                                 at_next( &at );
1614                                         }
1615                                 }
1616                                 /* else prev is NULL, append to end of global list */
1617                         }
1618                         if(parse_at(c, &at, prev)) return(1);
1619                         if (!cfn->c_at_head) cfn->c_at_head = at;
1620                         if (cfn->c_at_tail == prev) cfn->c_at_tail = at;
1621                         }
1622                         break;
1623
1624                 case CFG_SYNTAX: {
1625                         Syntax *syn, *prev;
1626
1627                         if ( c->op == LDAP_MOD_ADD && c->ca_private && cfn != c->ca_private )
1628                                 cfn = c->ca_private;
1629                         if ( c->valx < 0 ) {
1630                                 prev = cfn->c_syn_tail;
1631                         } else {
1632                                 prev = NULL;
1633                                 /* If adding anything after the first, prev is easy */
1634                                 if ( c->valx ) {
1635                                         int i;
1636                                         for ( i = 0, syn = cfn->c_syn_head; i < c->valx; i++ ) {
1637                                                 prev = syn;
1638                                                 syn_next( &syn );
1639                                         }
1640                                 } else
1641                                 /* If adding the first, and head exists, find its prev */
1642                                         if (cfn->c_syn_head) {
1643                                         for ( syn_start( &syn ); syn != cfn->c_syn_head; ) {
1644                                                 prev = syn;
1645                                                 syn_next( &syn );
1646                                         }
1647                                 }
1648                                 /* else prev is NULL, append to end of global list */
1649                         }
1650                         if ( parse_syn( c, &syn, prev ) ) return(1);
1651                         if ( !cfn->c_syn_head ) cfn->c_syn_head = syn;
1652                         if ( cfn->c_syn_tail == prev ) cfn->c_syn_tail = syn;
1653                         }
1654                         break;
1655
1656                 case CFG_DIT: {
1657                         ContentRule *cr;
1658
1659                         if ( c->op == LDAP_MOD_ADD && c->ca_private && cfn != c->ca_private )
1660                                 cfn = c->ca_private;
1661                         if(parse_cr(c, &cr)) return(1);
1662                         if (!cfn->c_cr_head) cfn->c_cr_head = cr;
1663                         cfn->c_cr_tail = cr;
1664                         }
1665                         break;
1666
1667                 case CFG_ATOPT:
1668                         ad_define_option(NULL, NULL, 0);
1669                         for(i = 1; i < c->argc; i++)
1670                                 if(ad_define_option(c->argv[i], c->fname, c->lineno))
1671                                         return(1);
1672                         break;
1673
1674                 case CFG_IX_INTLEN:
1675                         if ( c->value_int < SLAP_INDEX_INTLEN_DEFAULT )
1676                                 c->value_int = SLAP_INDEX_INTLEN_DEFAULT;
1677                         else if ( c->value_int > 255 )
1678                                 c->value_int = 255;
1679                         index_intlen = c->value_int;
1680                         index_intlen_strlen = SLAP_INDEX_INTLEN_STRLEN(
1681                                 index_intlen );
1682                         break;
1683                         
1684                 case CFG_SORTVALS: {
1685                         ADlist *svnew = NULL, *svtail, *sv;
1686
1687                         for ( i = 1; i < c->argc; i++ ) {
1688                                 AttributeDescription *ad = NULL;
1689                                 const char *text;
1690                                 int rc;
1691
1692                                 rc = slap_str2ad( c->argv[i], &ad, &text );
1693                                 if ( rc ) {
1694                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown attribute type #%d",
1695                                                 c->argv[0], i );
1696 sortval_reject:
1697                                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1698                                                 c->log, c->cr_msg, c->argv[i] );
1699                                         for ( sv = svnew; sv; sv = svnew ) {
1700                                                 svnew = sv->al_next;
1701                                                 ch_free( sv );
1702                                         }
1703                                         return 1;
1704                                 }
1705                                 if (( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) ||
1706                                         ad->ad_type->sat_single_value ) {
1707                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> inappropriate attribute type #%d",
1708                                                 c->argv[0], i );
1709                                         goto sortval_reject;
1710                                 }
1711                                 sv = ch_malloc( sizeof( ADlist ));
1712                                 sv->al_desc = ad;
1713                                 if ( !svnew ) {
1714                                         svnew = sv;
1715                                 } else {
1716                                         svtail->al_next = sv;
1717                                 }
1718                                 svtail = sv;
1719                         }
1720                         sv->al_next = NULL;
1721                         for ( sv = svnew; sv; sv = sv->al_next )
1722                                 sv->al_desc->ad_type->sat_flags |= SLAP_AT_SORTED_VAL;
1723                         for ( sv = sortVals; sv && sv->al_next; sv = sv->al_next );
1724                         if ( sv )
1725                                 sv->al_next = svnew;
1726                         else
1727                                 sortVals = svnew;
1728                         }
1729                         break;
1730
1731                 case CFG_ACL:
1732                         /* Don't append to the global ACL if we're on a specific DB */
1733                         i = c->valx;
1734                         if ( c->valx == -1 ) {
1735                                 AccessControl *a;
1736                                 i = 0;
1737                                 for ( a=c->be->be_acl; a; a = a->acl_next )
1738                                         i++;
1739                         }
1740                         if ( parse_acl(c->be, c->fname, c->lineno, c->argc, c->argv, i ) ) {
1741                                 return 1;
1742                         }
1743                         break;
1744
1745                 case CFG_ACL_ADD:
1746                         if(c->value_int)
1747                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_ACL_ADD;
1748                         else
1749                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_ACL_ADD;
1750                         break;
1751
1752                 case CFG_ROOTDSE:
1753                         if(root_dse_read_file(c->argv[1])) {
1754                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> could not read file", c->argv[0] );
1755                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1756                                         c->log, c->cr_msg, c->argv[1] );
1757                                 return(1);
1758                         }
1759                         {
1760                                 struct berval bv;
1761                                 ber_str2bv( c->argv[1], 0, 1, &bv );
1762                                 if ( c->op == LDAP_MOD_ADD && c->ca_private && cfn != c->ca_private )
1763                                         cfn = c->ca_private;
1764                                 ber_bvarray_add( &cfn->c_dseFiles, &bv );
1765                         }
1766                         break;
1767
1768                 case CFG_SERVERID:
1769                         {
1770                                 ServerID *si, **sip;
1771                                 LDAPURLDesc *lud;
1772                                 int num;
1773                                 if ( lutil_atoi( &num, c->argv[1] ) ||
1774                                         num < 0 || num > SLAP_SYNC_SID_MAX )
1775                                 {
1776                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1777                                                 "<%s> illegal server ID", c->argv[0] );
1778                                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1779                                                 c->log, c->cr_msg, c->argv[1] );
1780                                         return 1;
1781                                 }
1782                                 /* only one value allowed if no URL is given */
1783                                 if ( c->argc > 2 ) {
1784                                         int len;
1785
1786                                         if ( sid_list && BER_BVISEMPTY( &sid_list->si_url )) {
1787                                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1788                                                         "<%s> only one server ID allowed now", c->argv[0] );
1789                                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1790                                                         c->log, c->cr_msg, c->argv[1] );
1791                                                 return 1;
1792                                         }
1793
1794                                         if ( ldap_url_parse( c->argv[2], &lud )) {
1795                                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1796                                                         "<%s> invalid URL", c->argv[0] );
1797                                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1798                                                         c->log, c->cr_msg, c->argv[2] );
1799                                                 return 1;
1800                                         }
1801                                         len = strlen( c->argv[2] );
1802                                         si = ch_malloc( sizeof(ServerID) + len + 1 );
1803                                         si->si_url.bv_val = (char *)(si+1);
1804                                         si->si_url.bv_len = len;
1805                                         strcpy( si->si_url.bv_val, c->argv[2] );
1806                                 } else {
1807                                         if ( sid_list ) {
1808                                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1809                                                         "<%s> unqualified server ID not allowed now", c->argv[0] );
1810                                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1811                                                         c->log, c->cr_msg, c->argv[1] );
1812                                                 return 1;
1813                                         }
1814                                         si = ch_malloc( sizeof(ServerID) );
1815                                         BER_BVZERO( &si->si_url );
1816                                         slap_serverID = num;
1817                                         Debug( LDAP_DEBUG_CONFIG,
1818                                                 "%s: SID=%d\n",
1819                                                 c->log, slap_serverID, 0 );
1820                                 }
1821                                 si->si_next = NULL;
1822                                 si->si_num = num;
1823                                 for ( sip = &sid_list; *sip; sip = &(*sip)->si_next );
1824                                 *sip = si;
1825
1826                                 if (( slapMode & SLAP_SERVER_MODE ) && c->argc > 2 ) {
1827                                         Listener **l = slapd_get_listeners();
1828                                         int i, isMe = 0;
1829
1830                                         /* Try a straight compare with Listener strings */
1831                                         for ( i=0; l && l[i]; i++ ) {
1832                                                 if ( !strcasecmp( c->argv[2], l[i]->sl_url.bv_val )) {
1833                                                         isMe = 1;
1834                                                         break;
1835                                                 }
1836                                         }
1837
1838                                         /* If hostname is empty, or is localhost, or matches
1839                                          * our hostname, this serverID refers to this host.
1840                                          * Compare it against listeners and ports.
1841                                          */
1842                                         if ( !isMe && ( !lud->lud_host || !lud->lud_host[0] ||
1843                                                 !strncasecmp("localhost", lud->lud_host,
1844                                                         STRLENOF("localhost")) ||
1845                                                 !strcasecmp( global_host, lud->lud_host ))) {
1846
1847                                                 for ( i=0; l && l[i]; i++ ) {
1848                                                         LDAPURLDesc *lu2;
1849                                                         ldap_url_parse( l[i]->sl_url.bv_val, &lu2 );
1850                                                         do {
1851                                                                 if ( strcasecmp( lud->lud_scheme,
1852                                                                         lu2->lud_scheme ))
1853                                                                         break;
1854                                                                 if ( lud->lud_port != lu2->lud_port )
1855                                                                         break;
1856                                                                 /* Listener on ANY address */
1857                                                                 if ( !lu2->lud_host || !lu2->lud_host[0] ) {
1858                                                                         isMe = 1;
1859                                                                         break;
1860                                                                 }
1861                                                                 /* URL on ANY address */
1862                                                                 if ( !lud->lud_host || !lud->lud_host[0] ) {
1863                                                                         isMe = 1;
1864                                                                         break;
1865                                                                 }
1866                                                                 /* Listener has specific host, must
1867                                                                  * match it
1868                                                                  */
1869                                                                 if ( !strcasecmp( lud->lud_host,
1870                                                                         lu2->lud_host )) {
1871                                                                         isMe = 1;
1872                                                                         break;
1873                                                                 }
1874                                                         } while(0);
1875                                                         ldap_free_urldesc( lu2 );
1876                                                         if ( isMe ) {
1877                                                                 break;
1878                                                         }
1879                                                 }
1880                                         }
1881                                         if ( isMe ) {
1882                                                 slap_serverID = si->si_num;
1883                                                 Debug( LDAP_DEBUG_CONFIG,
1884                                                         "%s: SID=%d (listener=%s)\n",
1885                                                         c->log, slap_serverID,
1886                                                         l[i]->sl_url.bv_val );
1887                                         }
1888                                 }
1889                                 if ( c->argc > 2 )
1890                                         ldap_free_urldesc( lud );
1891                         }
1892                         break;
1893                 case CFG_LOGFILE: {
1894                                 if ( logfileName ) ch_free( logfileName );
1895                                 logfileName = c->value_string;
1896                                 logfile = fopen(logfileName, "w");
1897                                 if(logfile) lutil_debug_file(logfile);
1898                         } break;
1899
1900                 case CFG_LASTMOD:
1901                         if(SLAP_NOLASTMODCMD(c->be)) {
1902                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> not available for %s database",
1903                                         c->argv[0], c->be->bd_info->bi_type );
1904                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1905                                         c->log, c->cr_msg, 0 );
1906                                 return(1);
1907                         }
1908                         if(c->value_int)
1909                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_NOLASTMOD;
1910                         else
1911                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NOLASTMOD;
1912                         break;
1913
1914                 case CFG_MIRRORMODE:
1915                         if(!SLAP_SHADOW(c->be)) {
1916                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> database is not a shadow",
1917                                         c->argv[0] );
1918                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1919                                         c->log, c->cr_msg, 0 );
1920                                 return(1);
1921                         }
1922                         if(c->value_int)
1923                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_SINGLE_SHADOW;
1924                         else
1925                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_SINGLE_SHADOW;
1926                         break;
1927
1928                 case CFG_MONITORING:
1929                         if(c->value_int)
1930                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_MONITORING;
1931                         else
1932                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_MONITORING;
1933                         break;
1934
1935                 case CFG_HIDDEN:
1936                         if (c->value_int)
1937                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_HIDDEN;
1938                         else
1939                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_HIDDEN;
1940                         break;
1941
1942                 case CFG_SSTR_IF_MAX:
1943                         if (c->value_int < index_substr_if_minlen) {
1944                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value", c->argv[0] );
1945                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%d)\n",
1946                                         c->log, c->cr_msg, c->value_int );
1947                                 return(1);
1948                         }
1949                         index_substr_if_maxlen = c->value_int;
1950                         break;
1951
1952                 case CFG_SSTR_IF_MIN:
1953                         if (c->value_int > index_substr_if_maxlen) {
1954                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value", c->argv[0] );
1955                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%d)\n",
1956                                         c->log, c->cr_msg, c->value_int );
1957                                 return(1);
1958                         }
1959                         index_substr_if_minlen = c->value_int;
1960                         break;
1961
1962 #ifdef SLAPD_MODULES
1963                 case CFG_MODLOAD:
1964                         /* If we're just adding a module on an existing modpath,
1965                          * make sure we've selected the current path.
1966                          */
1967                         if ( c->op == LDAP_MOD_ADD && c->ca_private && modcur != c->ca_private ) {
1968                                 modcur = c->ca_private;
1969                                 /* This should never fail */
1970                                 if ( module_path( modcur->mp_path.bv_val )) {
1971                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> module path no longer valid",
1972                                                 c->argv[0] );
1973                                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1974                                                 c->log, c->cr_msg, modcur->mp_path.bv_val );
1975                                         return(1);
1976                                 }
1977                         }
1978                         if(module_load(c->argv[1], c->argc - 2, (c->argc > 2) ? c->argv + 2 : NULL))
1979                                 return(1);
1980                         /* Record this load on the current path */
1981                         {
1982                                 struct berval bv;
1983                                 char *ptr;
1984                                 if ( c->op == SLAP_CONFIG_ADD ) {
1985                                         ptr = c->line + STRLENOF("moduleload");
1986                                         while (!isspace((unsigned char) *ptr)) ptr++;
1987                                         while (isspace((unsigned char) *ptr)) ptr++;
1988                                 } else {
1989                                         ptr = c->line;
1990                                 }
1991                                 ber_str2bv(ptr, 0, 1, &bv);
1992                                 ber_bvarray_add( &modcur->mp_loads, &bv );
1993                         }
1994                         /* Check for any new hardcoded schema */
1995                         if ( c->op == LDAP_MOD_ADD && CONFIG_ONLINE_ADD( c )) {
1996                                 config_check_schema( NULL, &cfBackInfo );
1997                         }
1998                         break;
1999
2000                 case CFG_MODPATH:
2001                         if(module_path(c->argv[1])) return(1);
2002                         /* Record which path was used with each module */
2003                         {
2004                                 ModPaths *mp;
2005
2006                                 if (!modpaths.mp_loads) {
2007                                         mp = &modpaths;
2008                                 } else {
2009                                         mp = ch_malloc( sizeof( ModPaths ));
2010                                         modlast->mp_next = mp;
2011                                 }
2012                                 ber_str2bv(c->argv[1], 0, 1, &mp->mp_path);
2013                                 mp->mp_next = NULL;
2014                                 mp->mp_loads = NULL;
2015                                 modlast = mp;
2016                                 c->ca_private = mp;
2017                                 modcur = mp;
2018                         }
2019                         
2020                         break;
2021 #endif
2022
2023 #ifdef LDAP_SLAPI
2024                 case CFG_PLUGIN:
2025                         if(slapi_int_read_config(c->be, c->fname, c->lineno, c->argc, c->argv) != LDAP_SUCCESS)
2026                                 return(1);
2027                         slapi_plugins_used++;
2028                         break;
2029 #endif
2030
2031 #ifdef SLAP_AUTH_REWRITE
2032                 case CFG_REWRITE: {
2033                         struct berval bv;
2034                         char *line;
2035                         
2036                         if(slap_sasl_rewrite_config(c->fname, c->lineno, c->argc, c->argv))
2037                                 return(1);
2038
2039                         if ( c->argc > 1 ) {
2040                                 char    *s;
2041
2042                                 /* quote all args but the first */
2043                                 line = ldap_charray2str( c->argv, "\" \"" );
2044                                 ber_str2bv( line, 0, 0, &bv );
2045                                 s = ber_bvchr( &bv, '"' );
2046                                 assert( s != NULL );
2047                                 /* move the trailing quote of argv[0] to the end */
2048                                 AC_MEMCPY( s, s + 1, bv.bv_len - ( s - bv.bv_val ) );
2049                                 bv.bv_val[ bv.bv_len - 1 ] = '"';
2050
2051                         } else {
2052                                 ber_str2bv( c->argv[ 0 ], 0, 1, &bv );
2053                         }
2054                         
2055                         ber_bvarray_add( &authz_rewrites, &bv );
2056                         }
2057                         break;
2058 #endif
2059
2060
2061                 default:
2062                         Debug( LDAP_DEBUG_ANY,
2063                                 "%s: unknown CFG_TYPE %d.\n",
2064                                 c->log, c->type, 0 );
2065                         return 1;
2066
2067         }
2068         return(0);
2069 }
2070
2071
2072 static int
2073 config_fname(ConfigArgs *c) {
2074         if(c->op == SLAP_CONFIG_EMIT) {
2075                 if (c->ca_private) {
2076                         ConfigFile *cf = c->ca_private;
2077                         value_add_one( &c->rvalue_vals, &cf->c_file );
2078                         return 0;
2079                 }
2080                 return 1;
2081         }
2082         return(0);
2083 }
2084
2085 static int
2086 config_cfdir(ConfigArgs *c) {
2087         if(c->op == SLAP_CONFIG_EMIT) {
2088                 if ( !BER_BVISEMPTY( &cfdir )) {
2089                         value_add_one( &c->rvalue_vals, &cfdir );
2090                         return 0;
2091                 }
2092                 return 1;
2093         }
2094         return(0);
2095 }
2096
2097 static int
2098 config_search_base(ConfigArgs *c) {
2099         if(c->op == SLAP_CONFIG_EMIT) {
2100                 int rc = 1;
2101                 if (!BER_BVISEMPTY(&default_search_base)) {
2102                         value_add_one(&c->rvalue_vals, &default_search_base);
2103                         value_add_one(&c->rvalue_nvals, &default_search_nbase);
2104                         rc = 0;
2105                 }
2106                 return rc;
2107         } else if( c->op == LDAP_MOD_DELETE ) {
2108                 ch_free( default_search_base.bv_val );
2109                 ch_free( default_search_nbase.bv_val );
2110                 BER_BVZERO( &default_search_base );
2111                 BER_BVZERO( &default_search_nbase );
2112                 return 0;
2113         }
2114
2115         if(c->bi || c->be != frontendDB) {
2116                 Debug(LDAP_DEBUG_ANY, "%s: defaultSearchBase line must appear "
2117                         "prior to any backend or database definition\n",
2118                         c->log, 0, 0);
2119                 return(1);
2120         }
2121
2122         if(default_search_nbase.bv_len) {
2123                 free(default_search_base.bv_val);
2124                 free(default_search_nbase.bv_val);
2125         }
2126
2127         default_search_base = c->value_dn;
2128         default_search_nbase = c->value_ndn;
2129         return(0);
2130 }
2131
2132 /* For RE23 compatibility we allow this in the global entry
2133  * but we now defer it to the frontend entry to allow modules
2134  * to load new hash types.
2135  */
2136 static int
2137 config_passwd_hash(ConfigArgs *c) {
2138         int i;
2139         if (c->op == SLAP_CONFIG_EMIT) {
2140                 struct berval bv;
2141                 /* Don't generate it in the global entry */
2142                 if ( c->table == Cft_Global )
2143                         return 1;
2144                 for (i=0; default_passwd_hash && default_passwd_hash[i]; i++) {
2145                         ber_str2bv(default_passwd_hash[i], 0, 0, &bv);
2146                         value_add_one(&c->rvalue_vals, &bv);
2147                 }
2148                 return i ? 0 : 1;
2149         } else if ( c->op == LDAP_MOD_DELETE ) {
2150                 /* Deleting from global is a no-op, only the frontendDB entry matters */
2151                 if ( c->table == Cft_Global )
2152                         return 0;
2153                 if ( c->valx < 0 ) {
2154                         ldap_charray_free( default_passwd_hash );
2155                         default_passwd_hash = NULL;
2156                 } else {
2157                         i = c->valx;
2158                         ch_free( default_passwd_hash[i] );
2159                         for (; default_passwd_hash[i]; i++ )
2160                                 default_passwd_hash[i] = default_passwd_hash[i+1];
2161                 }
2162                 return 0;
2163         }
2164         for(i = 1; i < c->argc; i++) {
2165                 if(!lutil_passwd_scheme(c->argv[i])) {
2166                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> scheme not available", c->argv[0] );
2167                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2168                                 c->log, c->cr_msg, c->argv[i]);
2169                 } else {
2170                         ldap_charray_add(&default_passwd_hash, c->argv[i]);
2171                 }
2172         }
2173         if(!default_passwd_hash) {
2174                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> no valid hashes found", c->argv[0] );
2175                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2176                         c->log, c->cr_msg, 0 );
2177                 return(1);
2178         }
2179         return(0);
2180 }
2181
2182 static int
2183 config_schema_dn(ConfigArgs *c) {
2184         if ( c->op == SLAP_CONFIG_EMIT ) {
2185                 int rc = 1;
2186                 if ( !BER_BVISEMPTY( &c->be->be_schemadn )) {
2187                         value_add_one(&c->rvalue_vals, &c->be->be_schemadn);
2188                         value_add_one(&c->rvalue_nvals, &c->be->be_schemandn);
2189                         rc = 0;
2190                 }
2191                 return rc;
2192         } else if ( c->op == LDAP_MOD_DELETE ) {
2193                 ch_free( c->be->be_schemadn.bv_val );
2194                 ch_free( c->be->be_schemandn.bv_val );
2195                 BER_BVZERO( &c->be->be_schemadn );
2196                 BER_BVZERO( &c->be->be_schemandn );
2197                 return 0;
2198         }
2199         ch_free( c->be->be_schemadn.bv_val );
2200         ch_free( c->be->be_schemandn.bv_val );
2201         c->be->be_schemadn = c->value_dn;
2202         c->be->be_schemandn = c->value_ndn;
2203         return(0);
2204 }
2205
2206 static int
2207 config_sizelimit(ConfigArgs *c) {
2208         int i, rc = 0;
2209         struct slap_limits_set *lim = &c->be->be_def_limit;
2210         if (c->op == SLAP_CONFIG_EMIT) {
2211                 char buf[8192];
2212                 struct berval bv;
2213                 bv.bv_val = buf;
2214                 bv.bv_len = 0;
2215                 limits_unparse_one( lim, SLAP_LIMIT_SIZE, &bv, sizeof( buf ) );
2216                 if ( !BER_BVISEMPTY( &bv ))
2217                         value_add_one( &c->rvalue_vals, &bv );
2218                 else
2219                         rc = 1;
2220                 return rc;
2221         } else if ( c->op == LDAP_MOD_DELETE ) {
2222                 /* Reset to defaults */
2223                 lim->lms_s_soft = SLAPD_DEFAULT_SIZELIMIT;
2224                 lim->lms_s_hard = 0;
2225                 lim->lms_s_unchecked = -1;
2226                 lim->lms_s_pr = 0;
2227                 lim->lms_s_pr_hide = 0;
2228                 lim->lms_s_pr_total = 0;
2229                 return 0;
2230         }
2231         for(i = 1; i < c->argc; i++) {
2232                 if(!strncasecmp(c->argv[i], "size", 4)) {
2233                         rc = limits_parse_one(c->argv[i], lim);
2234                         if ( rc ) {
2235                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse value", c->argv[0] );
2236                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2237                                         c->log, c->cr_msg, c->argv[i]);
2238                                 return(1);
2239                         }
2240                 } else {
2241                         if(!strcasecmp(c->argv[i], "unlimited")) {
2242                                 lim->lms_s_soft = -1;
2243                         } else {
2244                                 if ( lutil_atoix( &lim->lms_s_soft, c->argv[i], 0 ) != 0 ) {
2245                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse limit", c->argv[0]);
2246                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2247                                                 c->log, c->cr_msg, c->argv[i]);
2248                                         return(1);
2249                                 }
2250                         }
2251                         lim->lms_s_hard = 0;
2252                 }
2253         }
2254         return(0);
2255 }
2256
2257 static int
2258 config_timelimit(ConfigArgs *c) {
2259         int i, rc = 0;
2260         struct slap_limits_set *lim = &c->be->be_def_limit;
2261         if (c->op == SLAP_CONFIG_EMIT) {
2262                 char buf[8192];
2263                 struct berval bv;
2264                 bv.bv_val = buf;
2265                 bv.bv_len = 0;
2266                 limits_unparse_one( lim, SLAP_LIMIT_TIME, &bv, sizeof( buf ) );
2267                 if ( !BER_BVISEMPTY( &bv ))
2268                         value_add_one( &c->rvalue_vals, &bv );
2269                 else
2270                         rc = 1;
2271                 return rc;
2272         } else if ( c->op == LDAP_MOD_DELETE ) {
2273                 /* Reset to defaults */
2274                 lim->lms_t_soft = SLAPD_DEFAULT_TIMELIMIT;
2275                 lim->lms_t_hard = 0;
2276                 return 0;
2277         }
2278         for(i = 1; i < c->argc; i++) {
2279                 if(!strncasecmp(c->argv[i], "time", 4)) {
2280                         rc = limits_parse_one(c->argv[i], lim);
2281                         if ( rc ) {
2282                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse value", c->argv[0] );
2283                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2284                                         c->log, c->cr_msg, c->argv[i]);
2285                                 return(1);
2286                         }
2287                 } else {
2288                         if(!strcasecmp(c->argv[i], "unlimited")) {
2289                                 lim->lms_t_soft = -1;
2290                         } else {
2291                                 if ( lutil_atoix( &lim->lms_t_soft, c->argv[i], 0 ) != 0 ) {
2292                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse limit", c->argv[0]);
2293                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2294                                                 c->log, c->cr_msg, c->argv[i]);
2295                                         return(1);
2296                                 }
2297                         }
2298                         lim->lms_t_hard = 0;
2299                 }
2300         }
2301         return(0);
2302 }
2303
2304 static int
2305 config_overlay(ConfigArgs *c) {
2306         if (c->op == SLAP_CONFIG_EMIT) {
2307                 return 1;
2308         } else if ( c->op == LDAP_MOD_DELETE ) {
2309                 assert(0);
2310         }
2311         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1],
2312                 c->valx, &c->bi, &c->reply)) {
2313                 /* log error */
2314                 Debug( LDAP_DEBUG_ANY,
2315                         "%s: (optional) %s overlay \"%s\" configuration failed.\n",
2316                         c->log, c->be == frontendDB ? "global " : "", &c->argv[1][1]);
2317                 return 1;
2318         } else if(overlay_config(c->be, c->argv[1], c->valx, &c->bi, &c->reply)) {
2319                 return(1);
2320         }
2321         return(0);
2322 }
2323
2324 static int
2325 config_subordinate(ConfigArgs *c)
2326 {
2327         int rc = 1;
2328         int advertise = 0;
2329
2330         switch( c->op ) {
2331         case SLAP_CONFIG_EMIT:
2332                 if ( SLAP_GLUE_SUBORDINATE( c->be )) {
2333                         struct berval bv;
2334
2335                         bv.bv_val = SLAP_GLUE_ADVERTISE( c->be ) ? "advertise" : "TRUE";
2336                         bv.bv_len = SLAP_GLUE_ADVERTISE( c->be ) ? STRLENOF("advertise") :
2337                                 STRLENOF("TRUE");
2338
2339                         value_add_one( &c->rvalue_vals, &bv );
2340                         rc = 0;
2341                 }
2342                 break;
2343         case LDAP_MOD_DELETE:
2344                 if ( !c->line  || strcasecmp( c->line, "advertise" )) {
2345                         glue_sub_del( c->be );
2346                 } else {
2347                         SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_GLUE_ADVERTISE;
2348                 }
2349                 rc = 0;
2350                 break;
2351         case LDAP_MOD_ADD:
2352         case SLAP_CONFIG_ADD:
2353                 if ( c->be->be_nsuffix == NULL ) {
2354                         /* log error */
2355                         snprintf( c->cr_msg, sizeof( c->cr_msg),
2356                                 "subordinate configuration needs a suffix" );
2357                         Debug( LDAP_DEBUG_ANY,
2358                                 "%s: %s.\n",
2359                                 c->log, c->cr_msg, 0 );
2360                         rc = 1;
2361                         break;
2362                 }
2363
2364                 if ( c->argc == 2 ) {
2365                         if ( strcasecmp( c->argv[1], "advertise" ) == 0 ) {
2366                                 advertise = 1;
2367
2368                         } else if ( strcasecmp( c->argv[1], "TRUE" ) != 0 ) {
2369                                 /* log error */
2370                                 snprintf( c->cr_msg, sizeof( c->cr_msg),
2371                                         "subordinate must be \"TRUE\" or \"advertise\"" );
2372                                 Debug( LDAP_DEBUG_ANY,
2373                                         "%s: suffix \"%s\": %s.\n",
2374                                         c->log, c->be->be_suffix[0].bv_val, c->cr_msg );
2375                                 rc = 1;
2376                                 break;
2377                         }
2378                 }
2379
2380                 rc = glue_sub_add( c->be, advertise, CONFIG_ONLINE_ADD( c ));
2381                 break;
2382         }
2383
2384         return rc;
2385 }
2386
2387 static int
2388 config_suffix(ConfigArgs *c)
2389 {
2390         Backend *tbe;
2391         struct berval pdn, ndn;
2392         char    *notallowed = NULL;
2393
2394         if ( c->be == frontendDB ) {
2395                 notallowed = "frontend";
2396
2397         } else if ( SLAP_MONITOR(c->be) ) {
2398                 notallowed = "monitor";
2399
2400         } else if ( SLAP_CONFIG(c->be) ) {
2401                 notallowed = "config";
2402         }
2403
2404         if ( notallowed != NULL ) {
2405                 char    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
2406
2407                 switch ( c->op ) {
2408                 case LDAP_MOD_ADD:
2409                 case LDAP_MOD_DELETE:
2410                 case LDAP_MOD_REPLACE:
2411                 case LDAP_MOD_INCREMENT:
2412                 case SLAP_CONFIG_ADD:
2413                         if ( !BER_BVISNULL( &c->value_dn ) ) {
2414                                 snprintf( buf, sizeof( buf ), "<%s> ",
2415                                                 c->value_dn.bv_val );
2416                         }
2417
2418                         Debug(LDAP_DEBUG_ANY,
2419                                 "%s: suffix %snot allowed in %s database.\n",
2420                                 c->log, buf, notallowed );
2421                         break;
2422
2423                 case SLAP_CONFIG_EMIT:
2424                         /* don't complain when emitting... */
2425                         break;
2426
2427                 default:
2428                         /* FIXME: don't know what values may be valid;
2429                          * please remove assertion, or add legal values
2430                          * to either block */
2431                         assert( 0 );
2432                         break;
2433                 }
2434
2435                 return 1;
2436         }
2437
2438         if (c->op == SLAP_CONFIG_EMIT) {
2439                 if ( c->be->be_suffix == NULL
2440                                 || BER_BVISNULL( &c->be->be_suffix[0] ) )
2441                 {
2442                         return 1;
2443                 } else {
2444                         value_add( &c->rvalue_vals, c->be->be_suffix );
2445                         value_add( &c->rvalue_nvals, c->be->be_nsuffix );
2446                         return 0;
2447                 }
2448         } else if ( c->op == LDAP_MOD_DELETE ) {
2449                 if ( c->valx < 0 ) {
2450                         ber_bvarray_free( c->be->be_suffix );
2451                         ber_bvarray_free( c->be->be_nsuffix );
2452                         c->be->be_suffix = NULL;
2453                         c->be->be_nsuffix = NULL;
2454                 } else {
2455                         int i = c->valx;
2456                         ch_free( c->be->be_suffix[i].bv_val );
2457                         ch_free( c->be->be_nsuffix[i].bv_val );
2458                         do {
2459                                 c->be->be_suffix[i] = c->be->be_suffix[i+1];
2460                                 c->be->be_nsuffix[i] = c->be->be_nsuffix[i+1];
2461                                 i++;
2462                         } while ( !BER_BVISNULL( &c->be->be_suffix[i] ) );
2463                 }
2464                 return 0;
2465         }
2466
2467 #ifdef SLAPD_MONITOR_DN
2468         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
2469                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> DN is reserved for monitoring slapd",
2470                         c->argv[0] );
2471                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2472                         c->log, c->cr_msg, SLAPD_MONITOR_DN);
2473                 return(1);
2474         }
2475 #endif
2476
2477         if (SLAP_DB_ONE_SUFFIX( c->be ) && c->be->be_suffix ) {
2478                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> Only one suffix is allowed on this %s backend",
2479                         c->argv[0], c->be->bd_info->bi_type );
2480                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2481                         c->log, c->cr_msg, 0);
2482                 return(1);
2483         }
2484
2485         pdn = c->value_dn;
2486         ndn = c->value_ndn;
2487
2488         if (SLAP_DBHIDDEN( c->be ))
2489                 tbe = NULL;
2490         else
2491                 tbe = select_backend(&ndn, 0);
2492         if(tbe == c->be) {
2493                 Debug( LDAP_DEBUG_ANY, "%s: suffix already served by this backend!.\n",
2494                         c->log, 0, 0);
2495                 return 1;
2496                 free(pdn.bv_val);
2497                 free(ndn.bv_val);
2498         } else if(tbe) {
2499                 BackendDB *b2 = tbe;
2500
2501                 /* Does tbe precede be? */
2502                 while (( b2 = LDAP_STAILQ_NEXT(b2, be_next )) && b2 && b2 != c->be );
2503
2504                 if ( b2 ) {
2505                         char    *type = tbe->bd_info->bi_type;
2506
2507                         if ( overlay_is_over( tbe ) ) {
2508                                 slap_overinfo   *oi = (slap_overinfo *)tbe->bd_info->bi_private;
2509                                 type = oi->oi_orig->bi_type;
2510                         }
2511
2512                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> namingContext \"%s\" "
2513                                 "already served by a preceding %s database",
2514                                 c->argv[0], pdn.bv_val, type );
2515                         Debug(LDAP_DEBUG_ANY, "%s: %s serving namingContext \"%s\"\n",
2516                                 c->log, c->cr_msg, tbe->be_suffix[0].bv_val);
2517                         free(pdn.bv_val);
2518                         free(ndn.bv_val);
2519                         return(1);
2520                 }
2521         }
2522         if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
2523                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
2524                         "base provided \"%s\" (assuming okay)\n",
2525                         c->log, default_search_base.bv_val, 0);
2526         }
2527         ber_bvarray_add(&c->be->be_suffix, &pdn);
2528         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
2529         return(0);
2530 }
2531
2532 static int
2533 config_rootdn(ConfigArgs *c) {
2534         if (c->op == SLAP_CONFIG_EMIT) {
2535                 if ( !BER_BVISNULL( &c->be->be_rootdn )) {
2536                         value_add_one(&c->rvalue_vals, &c->be->be_rootdn);
2537                         value_add_one(&c->rvalue_nvals, &c->be->be_rootndn);
2538                         return 0;
2539                 } else {
2540                         return 1;
2541                 }
2542         } else if ( c->op == LDAP_MOD_DELETE ) {
2543                 ch_free( c->be->be_rootdn.bv_val );
2544                 ch_free( c->be->be_rootndn.bv_val );
2545                 BER_BVZERO( &c->be->be_rootdn );
2546                 BER_BVZERO( &c->be->be_rootndn );
2547                 return 0;
2548         }
2549         if ( !BER_BVISNULL( &c->be->be_rootdn )) {
2550                 ch_free( c->be->be_rootdn.bv_val );
2551                 ch_free( c->be->be_rootndn.bv_val );
2552         }
2553         c->be->be_rootdn = c->value_dn;
2554         c->be->be_rootndn = c->value_ndn;
2555         return(0);
2556 }
2557
2558 static int
2559 config_rootpw(ConfigArgs *c) {
2560         Backend *tbe;
2561
2562         if (c->op == SLAP_CONFIG_EMIT) {
2563                 if (!BER_BVISEMPTY(&c->be->be_rootpw)) {
2564                         /* don't copy, because "rootpw" is marked
2565                          * as CFG_BERVAL */
2566                         c->value_bv = c->be->be_rootpw;
2567                         return 0;
2568                 }
2569                 return 1;
2570         } else if ( c->op == LDAP_MOD_DELETE ) {
2571                 ch_free( c->be->be_rootpw.bv_val );
2572                 BER_BVZERO( &c->be->be_rootpw );
2573                 return 0;
2574         }
2575
2576         tbe = select_backend(&c->be->be_rootndn, 0);
2577         if(tbe != c->be) {
2578                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> can only be set when rootdn is under suffix",
2579                         c->argv[0] );
2580                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2581                         c->log, c->cr_msg, 0);
2582                 return(1);
2583         }
2584         if ( !BER_BVISNULL( &c->be->be_rootpw ))
2585                 ch_free( c->be->be_rootpw.bv_val );
2586         c->be->be_rootpw = c->value_bv;
2587         return(0);
2588 }
2589
2590 static int
2591 config_restrict(ConfigArgs *c) {
2592         slap_mask_t restrictops = 0;
2593         int i;
2594         slap_verbmasks restrictable_ops[] = {
2595                 { BER_BVC("bind"),              SLAP_RESTRICT_OP_BIND },
2596                 { BER_BVC("add"),               SLAP_RESTRICT_OP_ADD },
2597                 { BER_BVC("modify"),            SLAP_RESTRICT_OP_MODIFY },
2598                 { BER_BVC("rename"),            SLAP_RESTRICT_OP_RENAME },
2599                 { BER_BVC("modrdn"),            0 },
2600                 { BER_BVC("delete"),            SLAP_RESTRICT_OP_DELETE },
2601                 { BER_BVC("search"),            SLAP_RESTRICT_OP_SEARCH },
2602                 { BER_BVC("compare"),           SLAP_RESTRICT_OP_COMPARE },
2603                 { BER_BVC("read"),              SLAP_RESTRICT_OP_READS },
2604                 { BER_BVC("write"),             SLAP_RESTRICT_OP_WRITES },
2605                 { BER_BVC("extended"),          SLAP_RESTRICT_OP_EXTENDED },
2606                 { BER_BVC("extended=" LDAP_EXOP_START_TLS ),            SLAP_RESTRICT_EXOP_START_TLS },
2607                 { BER_BVC("extended=" LDAP_EXOP_MODIFY_PASSWD ),        SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
2608                 { BER_BVC("extended=" LDAP_EXOP_X_WHO_AM_I ),           SLAP_RESTRICT_EXOP_WHOAMI },
2609                 { BER_BVC("extended=" LDAP_EXOP_X_CANCEL ),             SLAP_RESTRICT_EXOP_CANCEL },
2610                 { BER_BVC("all"),               SLAP_RESTRICT_OP_ALL },
2611                 { BER_BVNULL,   0 }
2612         };
2613
2614         if (c->op == SLAP_CONFIG_EMIT) {
2615                 return mask_to_verbs( restrictable_ops, c->be->be_restrictops,
2616                         &c->rvalue_vals );
2617         } else if ( c->op == LDAP_MOD_DELETE ) {
2618                 if ( !c->line ) {
2619                         c->be->be_restrictops = 0;
2620                 } else {
2621                         restrictops = verb_to_mask( c->line, restrictable_ops );
2622                         c->be->be_restrictops ^= restrictops;
2623                 }
2624                 return 0;
2625         }
2626         i = verbs_to_mask( c->argc, c->argv, restrictable_ops, &restrictops );
2627         if ( i ) {
2628                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown operation", c->argv[0] );
2629                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
2630                         c->log, c->cr_msg, c->argv[i]);
2631                 return(1);
2632         }
2633         if ( restrictops & SLAP_RESTRICT_OP_EXTENDED )
2634                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
2635         c->be->be_restrictops |= restrictops;
2636         return(0);
2637 }
2638
2639 static int
2640 config_allows(ConfigArgs *c) {
2641         slap_mask_t allows = 0;
2642         int i;
2643         slap_verbmasks allowable_ops[] = {
2644                 { BER_BVC("bind_v2"),           SLAP_ALLOW_BIND_V2 },
2645                 { BER_BVC("bind_anon_cred"),    SLAP_ALLOW_BIND_ANON_CRED },
2646                 { BER_BVC("bind_anon_dn"),      SLAP_ALLOW_BIND_ANON_DN },
2647                 { BER_BVC("update_anon"),       SLAP_ALLOW_UPDATE_ANON },
2648                 { BER_BVC("proxy_authz_anon"),  SLAP_ALLOW_PROXY_AUTHZ_ANON },
2649                 { BER_BVNULL,   0 }
2650         };
2651         if (c->op == SLAP_CONFIG_EMIT) {
2652                 return mask_to_verbs( allowable_ops, global_allows, &c->rvalue_vals );
2653         } else if ( c->op == LDAP_MOD_DELETE ) {
2654                 if ( !c->line ) {
2655                         global_allows = 0;
2656                 } else {
2657                         allows = verb_to_mask( c->line, allowable_ops );
2658                         global_allows ^= allows;
2659                 }
2660                 return 0;
2661         }
2662         i = verbs_to_mask(c->argc, c->argv, allowable_ops, &allows);
2663         if ( i ) {
2664                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown feature", c->argv[0] );
2665                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
2666                         c->log, c->cr_msg, c->argv[i]);
2667                 return(1);
2668         }
2669         global_allows |= allows;
2670         return(0);
2671 }
2672
2673 static int
2674 config_disallows(ConfigArgs *c) {
2675         slap_mask_t disallows = 0;
2676         int i;
2677         slap_verbmasks disallowable_ops[] = {
2678                 { BER_BVC("bind_anon"),         SLAP_DISALLOW_BIND_ANON },
2679                 { BER_BVC("bind_simple"),       SLAP_DISALLOW_BIND_SIMPLE },
2680                 { BER_BVC("tls_2_anon"),                SLAP_DISALLOW_TLS_2_ANON },
2681                 { BER_BVC("tls_authc"),         SLAP_DISALLOW_TLS_AUTHC },
2682                 { BER_BVC("proxy_authz_non_critical"),  SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT },
2683                 { BER_BVC("dontusecopy_non_critical"),  SLAP_DISALLOW_DONTUSECOPY_N_CRIT },
2684                 { BER_BVNULL, 0 }
2685         };
2686         if (c->op == SLAP_CONFIG_EMIT) {
2687                 return mask_to_verbs( disallowable_ops, global_disallows, &c->rvalue_vals );
2688         } else if ( c->op == LDAP_MOD_DELETE ) {
2689                 if ( !c->line ) {
2690                         global_disallows = 0;
2691                 } else {
2692                         disallows = verb_to_mask( c->line, disallowable_ops );
2693                         global_disallows ^= disallows;
2694                 }
2695                 return 0;
2696         }
2697         i = verbs_to_mask(c->argc, c->argv, disallowable_ops, &disallows);
2698         if ( i ) {
2699                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown feature", c->argv[0] );
2700                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
2701                         c->log, c->cr_msg, c->argv[i]);
2702                 return(1);
2703         }
2704         global_disallows |= disallows;
2705         return(0);
2706 }
2707
2708 static int
2709 config_requires(ConfigArgs *c) {
2710         slap_mask_t requires = frontendDB->be_requires;
2711         int i, argc = c->argc;
2712         char **argv = c->argv;
2713
2714         slap_verbmasks requires_ops[] = {
2715                 { BER_BVC("bind"),              SLAP_REQUIRE_BIND },
2716                 { BER_BVC("LDAPv3"),            SLAP_REQUIRE_LDAP_V3 },
2717                 { BER_BVC("authc"),             SLAP_REQUIRE_AUTHC },
2718                 { BER_BVC("sasl"),              SLAP_REQUIRE_SASL },
2719                 { BER_BVC("strong"),            SLAP_REQUIRE_STRONG },
2720                 { BER_BVNULL, 0 }
2721         };
2722         if (c->op == SLAP_CONFIG_EMIT) {
2723                 return mask_to_verbs( requires_ops, c->be->be_requires, &c->rvalue_vals );
2724         } else if ( c->op == LDAP_MOD_DELETE ) {
2725                 if ( !c->line ) {
2726                         c->be->be_requires = 0;
2727                 } else {
2728                         requires = verb_to_mask( c->line, requires_ops );
2729                         c->be->be_requires ^= requires;
2730                 }
2731                 return 0;
2732         }
2733         /* "none" can only be first, to wipe out default/global values */
2734         if ( strcasecmp( c->argv[ 1 ], "none" ) == 0 ) {
2735                 argv++;
2736                 argc--;
2737                 requires = 0;
2738         }
2739         i = verbs_to_mask(argc, argv, requires_ops, &requires);
2740         if ( i ) {
2741                 if (strcasecmp( c->argv[ i ], "none" ) == 0 ) {
2742                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> \"none\" (#%d) must be listed first", c->argv[0], i - 1 );
2743                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2744                                 c->log, c->cr_msg, 0);
2745                 } else {
2746                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown feature #%d", c->argv[0], i - 1 );
2747                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2748                                 c->log, c->cr_msg, c->argv[i]);
2749                 }
2750                 return(1);
2751         }
2752         c->be->be_requires = requires;
2753         return(0);
2754 }
2755
2756 static slap_verbmasks   *loglevel_ops;
2757
2758 static int
2759 loglevel_init( void )
2760 {
2761         slap_verbmasks  lo[] = {
2762                 { BER_BVC("Any"),       -1 },
2763                 { BER_BVC("Trace"),     LDAP_DEBUG_TRACE },
2764                 { BER_BVC("Packets"),   LDAP_DEBUG_PACKETS },
2765                 { BER_BVC("Args"),      LDAP_DEBUG_ARGS },
2766                 { BER_BVC("Conns"),     LDAP_DEBUG_CONNS },
2767                 { BER_BVC("BER"),       LDAP_DEBUG_BER },
2768                 { BER_BVC("Filter"),    LDAP_DEBUG_FILTER },
2769                 { BER_BVC("Config"),    LDAP_DEBUG_CONFIG },
2770                 { BER_BVC("ACL"),       LDAP_DEBUG_ACL },
2771                 { BER_BVC("Stats"),     LDAP_DEBUG_STATS },
2772                 { BER_BVC("Stats2"),    LDAP_DEBUG_STATS2 },
2773                 { BER_BVC("Shell"),     LDAP_DEBUG_SHELL },
2774                 { BER_BVC("Parse"),     LDAP_DEBUG_PARSE },
2775 #if 0   /* no longer used (nor supported) */
2776                 { BER_BVC("Cache"),     LDAP_DEBUG_CACHE },
2777                 { BER_BVC("Index"),     LDAP_DEBUG_INDEX },
2778 #endif
2779                 { BER_BVC("Sync"),      LDAP_DEBUG_SYNC },
2780                 { BER_BVC("None"),      LDAP_DEBUG_NONE },
2781                 { BER_BVNULL,           0 }
2782         };
2783
2784         return slap_verbmasks_init( &loglevel_ops, lo );
2785 }
2786
2787 static void
2788 loglevel_destroy( void )
2789 {
2790         if ( loglevel_ops ) {
2791                 (void)slap_verbmasks_destroy( loglevel_ops );
2792         }
2793         loglevel_ops = NULL;
2794 }
2795
2796 static slap_mask_t      loglevel_ignore[] = { -1, 0 };
2797
2798 int
2799 slap_loglevel_register( slap_mask_t m, struct berval *s )
2800 {
2801         int     rc;
2802
2803         if ( loglevel_ops == NULL ) {
2804                 loglevel_init();
2805         }
2806
2807         rc = slap_verbmasks_append( &loglevel_ops, m, s, loglevel_ignore );
2808
2809         if ( rc != 0 ) {
2810                 Debug( LDAP_DEBUG_ANY, "slap_loglevel_register(%lu, \"%s\") failed\n",
2811                         m, s->bv_val, 0 );
2812         }
2813
2814         return rc;
2815 }
2816
2817 int
2818 slap_loglevel_get( struct berval *s, int *l )
2819 {
2820         int             rc;
2821         slap_mask_t     m, i;
2822
2823         if ( loglevel_ops == NULL ) {
2824                 loglevel_init();
2825         }
2826
2827         for ( m = 0, i = 1; !BER_BVISNULL( &loglevel_ops[ i ].word ); i++ ) {
2828                 m |= loglevel_ops[ i ].mask;
2829         }
2830
2831         for ( i = 1; m & i; i <<= 1 )
2832                 ;
2833
2834         if ( i == 0 ) {
2835                 return -1;
2836         }
2837
2838         rc = slap_verbmasks_append( &loglevel_ops, i, s, loglevel_ignore );
2839
2840         if ( rc != 0 ) {
2841                 Debug( LDAP_DEBUG_ANY, "slap_loglevel_get(%lu, \"%s\") failed\n",
2842                         i, s->bv_val, 0 );
2843
2844         } else {
2845                 *l = i;
2846         }
2847
2848         return rc;
2849 }
2850
2851 int
2852 str2loglevel( const char *s, int *l )
2853 {
2854         int     i;
2855
2856         if ( loglevel_ops == NULL ) {
2857                 loglevel_init();
2858         }
2859
2860         i = verb_to_mask( s, loglevel_ops );
2861
2862         if ( BER_BVISNULL( &loglevel_ops[ i ].word ) ) {
2863                 return -1;
2864         }
2865
2866         *l = loglevel_ops[ i ].mask;
2867
2868         return 0;
2869 }
2870
2871 const char *
2872 loglevel2str( int l )
2873 {
2874         struct berval   bv = BER_BVNULL;
2875
2876         loglevel2bv( l, &bv );
2877
2878         return bv.bv_val;
2879 }
2880
2881 int
2882 loglevel2bv( int l, struct berval *bv )
2883 {
2884         if ( loglevel_ops == NULL ) {
2885                 loglevel_init();
2886         }
2887
2888         BER_BVZERO( bv );
2889
2890         return enum_to_verb( loglevel_ops, l, bv ) == -1;
2891 }
2892
2893 int
2894 loglevel2bvarray( int l, BerVarray *bva )
2895 {
2896         if ( loglevel_ops == NULL ) {
2897                 loglevel_init();
2898         }
2899
2900         return mask_to_verbs( loglevel_ops, l, bva );
2901 }
2902
2903 int
2904 loglevel_print( FILE *out )
2905 {
2906         int     i;
2907
2908         if ( loglevel_ops == NULL ) {
2909                 loglevel_init();
2910         }
2911
2912         fprintf( out, "Installed log subsystems:\n\n" );
2913         for ( i = 0; !BER_BVISNULL( &loglevel_ops[ i ].word ); i++ ) {
2914                 fprintf( out, "\t%-30s (%lu)\n",
2915                         loglevel_ops[ i ].word.bv_val,
2916                         loglevel_ops[ i ].mask );
2917         }
2918
2919         fprintf( out, "\nNOTE: custom log subsystems may be later installed "
2920                 "by specific code\n\n" );
2921
2922         return 0;
2923 }
2924
2925 static int config_syslog;
2926
2927 static int
2928 config_loglevel(ConfigArgs *c) {
2929         int i;
2930
2931         if ( loglevel_ops == NULL ) {
2932                 loglevel_init();
2933         }
2934
2935         if (c->op == SLAP_CONFIG_EMIT) {
2936                 /* Get default or commandline slapd setting */
2937                 if ( ldap_syslog && !config_syslog )
2938                         config_syslog = ldap_syslog;
2939                 return loglevel2bvarray( config_syslog, &c->rvalue_vals );
2940
2941         } else if ( c->op == LDAP_MOD_DELETE ) {
2942                 if ( !c->line ) {
2943                         config_syslog = 0;
2944                 } else {
2945                         int level = verb_to_mask( c->line, loglevel_ops );
2946                         config_syslog ^= level;
2947                 }
2948                 if ( slapMode & SLAP_SERVER_MODE ) {
2949                         ldap_syslog = config_syslog;
2950                 }
2951                 return 0;
2952         }
2953
2954         for( i=1; i < c->argc; i++ ) {
2955                 int     level;
2956
2957                 if ( isdigit((unsigned char)c->argv[i][0]) || c->argv[i][0] == '-' ) {
2958                         if( lutil_atoix( &level, c->argv[i], 0 ) != 0 ) {
2959                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse level", c->argv[0] );
2960                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2961                                         c->log, c->cr_msg, c->argv[i]);
2962                                 return( 1 );
2963                         }
2964                 } else {
2965                         if ( str2loglevel( c->argv[i], &level ) ) {
2966                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown level", c->argv[0] );
2967                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2968                                         c->log, c->cr_msg, c->argv[i]);
2969                                 return( 1 );
2970                         }
2971                 }
2972                 /* Explicitly setting a zero clears all the levels */
2973                 if ( level )
2974                         config_syslog |= level;
2975                 else
2976                         config_syslog = 0;
2977         }
2978         if ( slapMode & SLAP_SERVER_MODE ) {
2979                 ldap_syslog = config_syslog;
2980         }
2981         return(0);
2982 }
2983
2984 static int
2985 config_referral(ConfigArgs *c) {
2986         struct berval val;
2987         if (c->op == SLAP_CONFIG_EMIT) {
2988                 if ( default_referral ) {
2989                         value_add( &c->rvalue_vals, default_referral );
2990                         return 0;
2991                 } else {
2992                         return 1;
2993                 }
2994         } else if ( c->op == LDAP_MOD_DELETE ) {
2995                 if ( c->valx < 0 ) {
2996                         ber_bvarray_free( default_referral );
2997                         default_referral = NULL;
2998                 } else {
2999                         int i = c->valx;
3000                         ch_free( default_referral[i].bv_val );
3001                         for (; default_referral[i].bv_val; i++ )
3002                                 default_referral[i] = default_referral[i+1];
3003                 }
3004                 return 0;
3005         }
3006         if(validate_global_referral(c->argv[1])) {
3007                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid URL", c->argv[0] );
3008                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
3009                         c->log, c->cr_msg, c->argv[1]);
3010                 return(1);
3011         }
3012
3013         ber_str2bv(c->argv[1], 0, 0, &val);
3014         if(value_add_one(&default_referral, &val)) return(LDAP_OTHER);
3015         return(0);
3016 }
3017
3018 static struct {
3019         struct berval key;
3020         int off;
3021 } sec_keys[] = {
3022         { BER_BVC("ssf="), offsetof(slap_ssf_set_t, sss_ssf) },
3023         { BER_BVC("transport="), offsetof(slap_ssf_set_t, sss_transport) },
3024         { BER_BVC("tls="), offsetof(slap_ssf_set_t, sss_tls) },
3025         { BER_BVC("sasl="), offsetof(slap_ssf_set_t, sss_sasl) },
3026         { BER_BVC("update_ssf="), offsetof(slap_ssf_set_t, sss_update_ssf) },
3027         { BER_BVC("update_transport="), offsetof(slap_ssf_set_t, sss_update_transport) },
3028         { BER_BVC("update_tls="), offsetof(slap_ssf_set_t, sss_update_tls) },
3029         { BER_BVC("update_sasl="), offsetof(slap_ssf_set_t, sss_update_sasl) },
3030         { BER_BVC("simple_bind="), offsetof(slap_ssf_set_t, sss_simple_bind) },
3031         { BER_BVNULL, 0 }
3032 };
3033
3034 static int
3035 config_security(ConfigArgs *c) {
3036         slap_ssf_set_t *set = &c->be->be_ssf_set;
3037         char *next;
3038         int i, j;
3039         if (c->op == SLAP_CONFIG_EMIT) {
3040                 char numbuf[32];
3041                 struct berval bv;
3042                 slap_ssf_t *tgt;
3043                 int rc = 1;
3044
3045                 for (i=0; !BER_BVISNULL( &sec_keys[i].key ); i++) {
3046                         tgt = (slap_ssf_t *)((char *)set + sec_keys[i].off);
3047                         if ( *tgt ) {
3048                                 rc = 0;
3049                                 bv.bv_len = snprintf( numbuf, sizeof( numbuf ), "%u", *tgt );
3050                                 if ( bv.bv_len >= sizeof( numbuf ) ) {
3051                                         ber_bvarray_free_x( c->rvalue_vals, NULL );
3052                                         c->rvalue_vals = NULL;
3053                                         rc = 1;
3054                                         break;
3055                                 }
3056                                 bv.bv_len += sec_keys[i].key.bv_len;
3057                                 bv.bv_val = ch_malloc( bv.bv_len + 1);
3058                                 next = lutil_strcopy( bv.bv_val, sec_keys[i].key.bv_val );
3059                                 strcpy( next, numbuf );
3060                                 ber_bvarray_add( &c->rvalue_vals, &bv );
3061                         }
3062                 }
3063                 return rc;
3064         }
3065         for(i = 1; i < c->argc; i++) {
3066                 slap_ssf_t *tgt = NULL;
3067                 char *src = NULL;
3068                 for ( j=0; !BER_BVISNULL( &sec_keys[j].key ); j++ ) {
3069                         if(!strncasecmp(c->argv[i], sec_keys[j].key.bv_val,
3070                                 sec_keys[j].key.bv_len)) {
3071                                 src = c->argv[i] + sec_keys[j].key.bv_len;
3072                                 tgt = (slap_ssf_t *)((char *)set + sec_keys[j].off);
3073                                 break;
3074                         }
3075                 }
3076                 if ( !tgt ) {
3077                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown factor", c->argv[0] );
3078                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
3079                                 c->log, c->cr_msg, c->argv[i]);
3080                         return(1);
3081                 }
3082
3083                 if ( lutil_atou( tgt, src ) != 0 ) {
3084                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse factor", c->argv[0] );
3085                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
3086                                 c->log, c->cr_msg, c->argv[i]);
3087                         return(1);
3088                 }
3089         }
3090         return(0);
3091 }
3092
3093 char *
3094 anlist_unparse( AttributeName *an, char *ptr, ber_len_t buflen ) {
3095         int comma = 0;
3096         char *start = ptr;
3097
3098         for (; !BER_BVISNULL( &an->an_name ); an++) {
3099                 /* if buflen == 0, assume the buffer size has been 
3100                  * already checked otherwise */
3101                 if ( buflen > 0 && buflen - ( ptr - start ) < comma + an->an_name.bv_len ) return NULL;
3102                 if ( comma ) *ptr++ = ',';
3103                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
3104                 comma = 1;
3105         }
3106         return ptr;
3107 }
3108
3109 static int
3110 config_updatedn(ConfigArgs *c) {
3111         if (c->op == SLAP_CONFIG_EMIT) {
3112                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
3113                         value_add_one(&c->rvalue_vals, &c->be->be_update_ndn);
3114                         value_add_one(&c->rvalue_nvals, &c->be->be_update_ndn);
3115                         return 0;
3116                 }
3117                 return 1;
3118         } else if ( c->op == LDAP_MOD_DELETE ) {
3119                 ch_free( c->be->be_update_ndn.bv_val );
3120                 BER_BVZERO( &c->be->be_update_ndn );
3121                 SLAP_DBFLAGS(c->be) ^= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
3122                 return 0;
3123         }
3124         if(SLAP_SHADOW(c->be)) {
3125                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> database already shadowed", c->argv[0] );
3126                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
3127                         c->log, c->cr_msg, 0);
3128                 return(1);
3129         }
3130
3131         ber_memfree_x( c->value_dn.bv_val, NULL );
3132         if ( !BER_BVISNULL( &c->be->be_update_ndn ) ) {
3133                 ber_memfree_x( c->be->be_update_ndn.bv_val, NULL );
3134         }
3135         c->be->be_update_ndn = c->value_ndn;
3136         BER_BVZERO( &c->value_dn );
3137         BER_BVZERO( &c->value_ndn );
3138
3139         return config_slurp_shadow( c );
3140 }
3141
3142 int
3143 config_shadow( ConfigArgs *c, int flag )
3144 {
3145         char    *notallowed = NULL;
3146
3147         if ( c->be == frontendDB ) {
3148                 notallowed = "frontend";
3149
3150         } else if ( SLAP_MONITOR(c->be) ) {
3151                 notallowed = "monitor";
3152         }
3153
3154         if ( notallowed != NULL ) {
3155                 Debug( LDAP_DEBUG_ANY, "%s: %s database cannot be shadow.\n", c->log, notallowed, 0 );
3156                 return 1;
3157         }
3158
3159         if ( SLAP_SHADOW(c->be) ) {
3160                 /* if already shadow, only check consistency */
3161                 if ( ( SLAP_DBFLAGS(c->be) & flag ) != flag ) {
3162                         Debug( LDAP_DEBUG_ANY, "%s: inconsistent shadow flag 0x%x.\n", c->log, flag, 0 );
3163                         return 1;
3164                 }
3165
3166         } else {
3167                 SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SINGLE_SHADOW | flag);
3168         }
3169
3170         return 0;
3171 }
3172
3173 static int
3174 config_updateref(ConfigArgs *c) {
3175         struct berval val;
3176         if (c->op == SLAP_CONFIG_EMIT) {
3177                 if ( c->be->be_update_refs ) {
3178                         value_add( &c->rvalue_vals, c->be->be_update_refs );
3179                         return 0;
3180                 } else {
3181                         return 1;
3182                 }
3183         } else if ( c->op == LDAP_MOD_DELETE ) {
3184                 if ( c->valx < 0 ) {
3185                         ber_bvarray_free( c->be->be_update_refs );
3186                         c->be->be_update_refs = NULL;
3187                 } else {
3188                         int i = c->valx;
3189                         ch_free( c->be->be_update_refs[i].bv_val );
3190                         for (; c->be->be_update_refs[i].bv_val; i++)
3191                                 c->be->be_update_refs[i] = c->be->be_update_refs[i+1];
3192                 }
3193                 return 0;
3194         }
3195         if(!SLAP_SHADOW(c->be) && !c->be->be_syncinfo) {
3196                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must appear after syncrepl or updatedn",
3197                         c->argv[0] );
3198                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
3199                         c->log, c->cr_msg, 0);
3200                 return(1);
3201         }
3202
3203         if(validate_global_referral(c->argv[1])) {
3204                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid URL", c->argv[0] );
3205                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
3206                         c->log, c->cr_msg, c->argv[1]);
3207                 return(1);
3208         }
3209         ber_str2bv(c->argv[1], 0, 0, &val);
3210         if(value_add_one(&c->be->be_update_refs, &val)) return(LDAP_OTHER);
3211         return(0);
3212 }
3213
3214 static int
3215 config_obsolete(ConfigArgs *c) {
3216         if (c->op == SLAP_CONFIG_EMIT)
3217                 return 1;
3218
3219         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> keyword is obsolete (ignored)",
3220                 c->argv[0] );
3221         Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0);
3222         return(0);
3223 }
3224
3225 static int
3226 config_include(ConfigArgs *c) {
3227         int savelineno = c->lineno;
3228         int rc;
3229         ConfigFile *cf;
3230         ConfigFile *cfsave = cfn;
3231         ConfigFile *cf2 = NULL;
3232
3233         /* Leftover from RE23. No dynamic config for include files */
3234         if ( c->op == SLAP_CONFIG_EMIT || c->op == LDAP_MOD_DELETE )
3235                 return 1;
3236
3237         cf = ch_calloc( 1, sizeof(ConfigFile));
3238         if ( cfn->c_kids ) {
3239                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
3240                 cf2->c_sibs = cf;
3241         } else {
3242                 cfn->c_kids = cf;
3243         }
3244         cfn = cf;
3245         ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
3246         rc = read_config_file(c->argv[1], c->depth + 1, c, config_back_cf_table);
3247         c->lineno = savelineno - 1;
3248         cfn = cfsave;
3249         if ( rc ) {
3250                 if ( cf2 ) cf2->c_sibs = NULL;
3251                 else cfn->c_kids = NULL;
3252                 ch_free( cf->c_file.bv_val );
3253                 ch_free( cf );
3254         } else {
3255                 c->ca_private = cf;
3256         }
3257         return(rc);
3258 }
3259
3260 #ifdef HAVE_TLS
3261 static int
3262 config_tls_cleanup(ConfigArgs *c) {
3263         int rc = 0;
3264
3265         if ( slap_tls_ld ) {
3266                 int opt = 1;
3267
3268                 ldap_pvt_tls_ctx_free( slap_tls_ctx );
3269
3270                 /* Force new ctx to be created */
3271                 rc = ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
3272                 if( rc == 0 ) {
3273                         /* The ctx's refcount is bumped up here */
3274                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
3275                         /* This is a no-op if it's already loaded */
3276                         load_extop( &slap_EXOP_START_TLS, 0, starttls_extop );
3277                 }
3278         }
3279         return rc;
3280 }
3281
3282 static int
3283 config_tls_option(ConfigArgs *c) {
3284         int flag;
3285         LDAP *ld = slap_tls_ld;
3286         switch(c->type) {
3287         case CFG_TLS_RAND:      flag = LDAP_OPT_X_TLS_RANDOM_FILE;      ld = NULL; break;
3288         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
3289         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
3290         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
3291         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
3292         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
3293         case CFG_TLS_DH_FILE:   flag = LDAP_OPT_X_TLS_DHFILE;   break;
3294 #ifdef HAVE_GNUTLS
3295         case CFG_TLS_CRL_FILE:  flag = LDAP_OPT_X_TLS_CRLFILE;  break;
3296 #endif
3297         default:                Debug(LDAP_DEBUG_ANY, "%s: "
3298                                         "unknown tls_option <0x%x>\n",
3299                                         c->log, c->type, 0);
3300                 return 1;
3301         }
3302         if (c->op == SLAP_CONFIG_EMIT) {
3303                 return ldap_pvt_tls_get_option( ld, flag, &c->value_string );
3304         } else if ( c->op == LDAP_MOD_DELETE ) {
3305                 c->cleanup = config_tls_cleanup;
3306                 return ldap_pvt_tls_set_option( ld, flag, NULL );
3307         }
3308         ch_free(c->value_string);
3309         c->cleanup = config_tls_cleanup;
3310         return(ldap_pvt_tls_set_option(ld, flag, c->argv[1]));
3311 }
3312
3313 /* FIXME: this ought to be provided by libldap */
3314 static int
3315 config_tls_config(ConfigArgs *c) {
3316         int i, flag;
3317         switch(c->type) {
3318         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK; break;
3319         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT; break;
3320         case CFG_TLS_PROTOCOL_MIN: flag = LDAP_OPT_X_TLS_PROTOCOL_MIN; break;
3321         default:
3322                 Debug(LDAP_DEBUG_ANY, "%s: "
3323                                 "unknown tls_option <0x%x>\n",
3324                                 c->log, c->type, 0);
3325                 return 1;
3326         }
3327         if (c->op == SLAP_CONFIG_EMIT) {
3328                 return slap_tls_get_config( slap_tls_ld, flag, &c->value_string );
3329         } else if ( c->op == LDAP_MOD_DELETE ) {
3330                 int i = 0;
3331                 c->cleanup = config_tls_cleanup;
3332                 return ldap_pvt_tls_set_option( slap_tls_ld, flag, &i );
3333         }
3334         ch_free( c->value_string );
3335         c->cleanup = config_tls_cleanup;
3336         if ( isdigit( (unsigned char)c->argv[1][0] ) ) {
3337                 if ( lutil_atoi( &i, c->argv[1] ) != 0 ) {
3338                         Debug(LDAP_DEBUG_ANY, "%s: "
3339                                 "unable to parse %s \"%s\"\n",
3340                                 c->log, c->argv[0], c->argv[1] );
3341                         return 1;
3342                 }
3343                 return(ldap_pvt_tls_set_option(slap_tls_ld, flag, &i));
3344         } else {
3345                 return(ldap_pvt_tls_config(slap_tls_ld, flag, c->argv[1]));
3346         }
3347 }
3348 #endif
3349
3350 static CfEntryInfo *
3351 config_find_base( CfEntryInfo *root, struct berval *dn, CfEntryInfo **last )
3352 {
3353         struct berval cdn;
3354         char *c;
3355
3356         if ( !root ) {
3357                 *last = NULL;
3358                 return NULL;
3359         }
3360
3361         if ( dn_match( &root->ce_entry->e_nname, dn ))
3362                 return root;
3363
3364         c = dn->bv_val+dn->bv_len;
3365         for (;*c != ',';c--);
3366
3367         while(root) {
3368                 *last = root;
3369                 for (--c;c>dn->bv_val && *c != ',';c--);
3370                 cdn.bv_val = c;
3371                 if ( *c == ',' )
3372                         cdn.bv_val++;
3373                 cdn.bv_len = dn->bv_len - (cdn.bv_val - dn->bv_val);
3374
3375                 root = root->ce_kids;
3376
3377                 for (;root;root=root->ce_sibs) {
3378                         if ( dn_match( &root->ce_entry->e_nname, &cdn )) {
3379                                 if ( cdn.bv_val == dn->bv_val ) {
3380                                         return root;
3381                                 }
3382                                 break;
3383                         }
3384                 }
3385         }
3386         return root;
3387 }
3388
3389 typedef struct setup_cookie {
3390         CfBackInfo *cfb;
3391         ConfigArgs *ca;
3392         Entry *frontend;
3393         Entry *config;
3394         int got_frontend;
3395         int got_config;
3396 } setup_cookie;
3397
3398 static int
3399 config_ldif_resp( Operation *op, SlapReply *rs )
3400 {
3401         if ( rs->sr_type == REP_SEARCH ) {
3402                 setup_cookie *sc = op->o_callback->sc_private;
3403                 struct berval pdn;
3404
3405                 sc->cfb->cb_got_ldif = 1;
3406                 /* Does the frontend exist? */
3407                 if ( !sc->got_frontend ) {
3408                         if ( !strncmp( rs->sr_entry->e_nname.bv_val,
3409                                 "olcDatabase", STRLENOF( "olcDatabase" )))
3410                         {
3411                                 if ( strncmp( rs->sr_entry->e_nname.bv_val +
3412                                         STRLENOF( "olcDatabase" ), "={-1}frontend",
3413                                         STRLENOF( "={-1}frontend" )))
3414                                 {
3415                                         struct berval rdn;
3416                                         int i = op->o_noop;
3417                                         sc->ca->be = frontendDB;
3418                                         sc->ca->bi = frontendDB->bd_info;
3419                                         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
3420                                         rdn.bv_val = sc->ca->log;
3421                                         rdn.bv_len = snprintf(rdn.bv_val, sizeof( sc->ca->log ),
3422                                                 "%s=" SLAP_X_ORDERED_FMT "%s",
3423                                                 cfAd_database->ad_cname.bv_val, -1,
3424                                                 sc->ca->bi->bi_type);
3425                                         op->o_noop = 1;
3426                                         sc->frontend = config_build_entry( op, rs,
3427                                                 sc->cfb->cb_root, sc->ca, &rdn, &CFOC_DATABASE,
3428                                                 sc->ca->be->be_cf_ocs );
3429                                         op->o_noop = i;
3430                                         sc->got_frontend++;
3431                                 } else {
3432                                         sc->got_frontend++;
3433                                         goto ok;
3434                                 }
3435                         }
3436                 }
3437
3438                 dnParent( &rs->sr_entry->e_nname, &pdn );
3439
3440                 /* Does the configDB exist? */
3441                 if ( sc->got_frontend && !sc->got_config &&
3442                         !strncmp( rs->sr_entry->e_nname.bv_val,
3443                         "olcDatabase", STRLENOF( "olcDatabase" )) &&
3444                         dn_match( &config_rdn, &pdn ) )
3445                 {
3446                         if ( strncmp( rs->sr_entry->e_nname.bv_val +
3447                                 STRLENOF( "olcDatabase" ), "={0}config",
3448                                 STRLENOF( "={0}config" )))
3449                         {
3450                                 struct berval rdn;
3451                                 int i = op->o_noop;
3452                                 sc->ca->be = LDAP_STAILQ_FIRST( &backendDB );
3453                                 sc->ca->bi = sc->ca->be->bd_info;
3454                                 rdn.bv_val = sc->ca->log;
3455                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( sc->ca->log ),
3456                                         "%s=" SLAP_X_ORDERED_FMT "%s",
3457                                         cfAd_database->ad_cname.bv_val, 0,
3458                                         sc->ca->bi->bi_type);
3459                                 op->o_noop = 1;
3460                                 sc->config = config_build_entry( op, rs, sc->cfb->cb_root,
3461                                         sc->ca, &rdn, &CFOC_DATABASE, sc->ca->be->be_cf_ocs );
3462                                 op->o_noop = i;
3463                         }
3464                         sc->got_config++;
3465                 }
3466
3467 ok:
3468                 rs->sr_err = config_add_internal( sc->cfb, rs->sr_entry, sc->ca, NULL, NULL, NULL );
3469                 if ( rs->sr_err != LDAP_SUCCESS ) {
3470                         Debug( LDAP_DEBUG_ANY, "config error processing %s: %s\n",
3471                                 rs->sr_entry->e_name.bv_val, sc->ca->cr_msg, 0 );
3472                 }
3473         }
3474         return rs->sr_err;
3475 }
3476
3477 /* Configure and read the underlying back-ldif store */
3478 static int
3479 config_setup_ldif( BackendDB *be, const char *dir, int readit ) {
3480         CfBackInfo *cfb = be->be_private;
3481         ConfigArgs c = {0};
3482         ConfigTable *ct;
3483         char *argv[3];
3484         int rc = 0;
3485         setup_cookie sc;
3486         slap_callback cb = { NULL, config_ldif_resp, NULL, NULL };
3487         Connection conn = {0};
3488         OperationBuffer opbuf;
3489         Operation *op;
3490         SlapReply rs = {REP_RESULT};
3491         Filter filter = { LDAP_FILTER_PRESENT };
3492         struct berval filterstr = BER_BVC("(objectclass=*)");
3493         struct stat st;
3494
3495         /* Is the config directory available? */
3496         if ( stat( dir, &st ) < 0 ) {
3497                 /* No, so don't bother using the backing store.
3498                  * All changes will be in-memory only.
3499                  */
3500                 return 0;
3501         }
3502                 
3503         cfb->cb_db.bd_info = backend_info( "ldif" );
3504         if ( !cfb->cb_db.bd_info )
3505                 return 0;       /* FIXME: eventually this will be a fatal error */
3506
3507         if ( backend_db_init( "ldif", &cfb->cb_db, -1, NULL ) == NULL )
3508                 return 1;
3509
3510         cfb->cb_db.be_suffix = be->be_suffix;
3511         cfb->cb_db.be_nsuffix = be->be_nsuffix;
3512
3513         /* The suffix is always "cn=config". The underlying DB's rootdn
3514          * is always the same as the suffix.
3515          */
3516         cfb->cb_db.be_rootdn = be->be_suffix[0];
3517         cfb->cb_db.be_rootndn = be->be_nsuffix[0];
3518
3519         ber_str2bv( dir, 0, 1, &cfdir );
3520
3521         c.be = &cfb->cb_db;
3522         c.fname = "slapd";
3523         c.argc = 2;
3524         argv[0] = "directory";
3525         argv[1] = (char *)dir;
3526         argv[2] = NULL;
3527         c.argv = argv;
3528         c.reply.err = 0;
3529         c.reply.msg[0] = 0;
3530         c.table = Cft_Database;
3531
3532         ct = config_find_keyword( c.be->be_cf_ocs->co_table, &c );
3533         if ( !ct )
3534                 return 1;
3535
3536         if ( config_add_vals( ct, &c ))
3537                 return 1;
3538
3539         if ( backend_startup_one( &cfb->cb_db, &c.reply ))
3540                 return 1;
3541
3542         if ( readit ) {
3543                 void *thrctx = ldap_pvt_thread_pool_context();
3544                 int prev_DN_strict;
3545
3546                 connection_fake_init( &conn, &opbuf, thrctx );
3547                 op = &opbuf.ob_op;
3548
3549                 filter.f_desc = slap_schema.si_ad_objectClass;
3550
3551                 op->o_tag = LDAP_REQ_SEARCH;
3552
3553                 op->ors_filter = &filter;
3554                 op->ors_filterstr = filterstr;
3555                 op->ors_scope = LDAP_SCOPE_SUBTREE;
3556
3557                 op->o_dn = c.be->be_rootdn;
3558                 op->o_ndn = c.be->be_rootndn;
3559
3560                 op->o_req_dn = be->be_suffix[0];
3561                 op->o_req_ndn = be->be_nsuffix[0];
3562
3563                 op->ors_tlimit = SLAP_NO_LIMIT;
3564                 op->ors_slimit = SLAP_NO_LIMIT;
3565
3566                 op->ors_attrs = slap_anlist_all_attributes;
3567                 op->ors_attrsonly = 0;
3568
3569                 op->o_callback = &cb;
3570                 sc.cfb = cfb;
3571                 sc.ca = &c;
3572                 cb.sc_private = &sc;
3573                 sc.got_frontend = 0;
3574                 sc.got_config = 0;
3575                 sc.frontend = NULL;
3576                 sc.config = NULL;
3577
3578                 op->o_bd = &cfb->cb_db;
3579                 
3580                 /* Allow unknown attrs in DNs */
3581                 prev_DN_strict = slap_DN_strict;
3582                 slap_DN_strict = 0;
3583
3584                 rc = op->o_bd->be_search( op, &rs );
3585
3586                 /* Restore normal DN validation */
3587                 slap_DN_strict = prev_DN_strict;
3588
3589                 op->o_tag = LDAP_REQ_ADD;
3590                 if ( rc == LDAP_SUCCESS && sc.frontend ) {
3591                         op->ora_e = sc.frontend;
3592                         rc = op->o_bd->be_add( op, &rs );
3593                 }
3594                 if ( rc == LDAP_SUCCESS && sc.config ) {
3595                         op->ora_e = sc.config;
3596                         rc = op->o_bd->be_add( op, &rs );
3597                 }
3598                 ldap_pvt_thread_pool_context_reset( thrctx );
3599         }
3600
3601         /* ITS#4194 - only use if it's present, or we're converting. */
3602         if ( !readit || rc == LDAP_SUCCESS )
3603                 cfb->cb_use_ldif = 1;
3604
3605         return rc;
3606 }
3607
3608 static int
3609 CfOc_cmp( const void *c1, const void *c2 ) {
3610         const ConfigOCs *co1 = c1;
3611         const ConfigOCs *co2 = c2;
3612
3613         return ber_bvcmp( co1->co_name, co2->co_name );
3614 }
3615
3616 int
3617 config_register_schema(ConfigTable *ct, ConfigOCs *ocs) {
3618         int i;
3619
3620         i = init_config_attrs( ct );
3621         if ( i ) return i;
3622
3623         /* set up the objectclasses */
3624         i = init_config_ocs( ocs );
3625         if ( i ) return i;
3626
3627         for (i=0; ocs[i].co_def; i++) {
3628                 if ( ocs[i].co_oc ) {
3629                         ocs[i].co_name = &ocs[i].co_oc->soc_cname;
3630                         if ( !ocs[i].co_table )
3631                                 ocs[i].co_table = ct;
3632                         avl_insert( &CfOcTree, &ocs[i], CfOc_cmp, avl_dup_error );
3633                 }
3634         }
3635         return 0;
3636 }
3637
3638 int
3639 read_config(const char *fname, const char *dir) {
3640         BackendDB *be;
3641         CfBackInfo *cfb;
3642         const char *cfdir, *cfname;
3643         int rc;
3644
3645         /* Setup the config backend */
3646         be = backend_db_init( "config", NULL, 0, NULL );
3647         if ( !be )
3648                 return 1;
3649
3650         cfb = be->be_private;
3651         be->be_dfltaccess = ACL_NONE;
3652
3653         /* If no .conf, or a dir was specified, setup the dir */
3654         if ( !fname || dir ) {
3655                 if ( dir ) {
3656                         /* If explicitly given, check for existence */
3657                         struct stat st;
3658
3659                         if ( stat( dir, &st ) < 0 ) {
3660                                 Debug( LDAP_DEBUG_ANY,
3661                                         "invalid config directory %s, error %d\n",
3662                                                 dir, errno, 0 );
3663                                 return 1;
3664                         }
3665                         cfdir = dir;
3666                 } else {
3667                         cfdir = SLAPD_DEFAULT_CONFIGDIR;
3668                 }
3669                 /* if fname is defaulted, try reading .d */
3670                 rc = config_setup_ldif( be, cfdir, !fname );
3671
3672                 if ( rc ) {
3673                         /* It may be OK if the base object doesn't exist yet. */
3674                         if ( rc != LDAP_NO_SUCH_OBJECT )
3675                                 return 1;
3676                         /* ITS#4194: But if dir was specified and no fname,
3677                          * then we were supposed to read the dir. Unless we're
3678                          * trying to slapadd the dir...
3679                          */
3680                         if ( dir && !fname ) {
3681                                 if ( slapMode & (SLAP_SERVER_MODE|SLAP_TOOL_READMAIN|SLAP_TOOL_READONLY))
3682                                         return 1;
3683                                 /* Assume it's slapadd with a config dir, let it continue */
3684                                 rc = 0;
3685                                 cfb->cb_got_ldif = 1;
3686                                 cfb->cb_use_ldif = 1;
3687                                 goto done;
3688                         }
3689                 }
3690
3691                 /* If we read the config from back-ldif, nothing to do here */
3692                 if ( cfb->cb_got_ldif ) {
3693                         rc = 0;
3694                         goto done;
3695                 }
3696         }
3697
3698         if ( fname )
3699                 cfname = fname;
3700         else
3701                 cfname = SLAPD_DEFAULT_CONFIGFILE;
3702
3703         rc = read_config_file(cfname, 0, NULL, config_back_cf_table);
3704
3705         if ( rc == 0 )
3706                 ber_str2bv( cfname, 0, 1, &cfb->cb_config->c_file );
3707
3708 done:
3709         if ( rc == 0 && BER_BVISNULL( &frontendDB->be_schemadn ) ) {
3710                 ber_str2bv( SLAPD_SCHEMA_DN, STRLENOF( SLAPD_SCHEMA_DN ), 1,
3711                         &frontendDB->be_schemadn );
3712                 rc = dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
3713                 if ( rc != LDAP_SUCCESS ) {
3714                         Debug(LDAP_DEBUG_ANY, "read_config: "
3715                                 "unable to normalize default schema DN \"%s\"\n",
3716                                 frontendDB->be_schemadn.bv_val, 0, 0 );
3717                         /* must not happen */
3718                         assert( 0 );
3719                 }
3720         }
3721         return rc;
3722 }
3723
3724 static int
3725 config_back_bind( Operation *op, SlapReply *rs )
3726 {
3727         if ( be_isroot_pw( op ) ) {
3728                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ));
3729                 /* frontend sends result */
3730                 return LDAP_SUCCESS;
3731         }
3732
3733         rs->sr_err = LDAP_INVALID_CREDENTIALS;
3734         send_ldap_result( op, rs );
3735
3736         return rs->sr_err;
3737 }
3738
3739 static int
3740 config_send( Operation *op, SlapReply *rs, CfEntryInfo *ce, int depth )
3741 {
3742         int rc = 0;
3743
3744         if ( test_filter( op, ce->ce_entry, op->ors_filter ) == LDAP_COMPARE_TRUE )
3745         {
3746                 rs->sr_attrs = op->ors_attrs;
3747                 rs->sr_entry = ce->ce_entry;
3748                 rs->sr_flags = 0;
3749                 rc = send_search_entry( op, rs );
3750                 if ( rc != LDAP_SUCCESS ) {
3751                         return rc;
3752                 }
3753         }
3754         if ( op->ors_scope == LDAP_SCOPE_SUBTREE ) {
3755                 if ( ce->ce_kids ) {
3756                         rc = config_send( op, rs, ce->ce_kids, 1 );
3757                         if ( rc ) return rc;
3758                 }
3759                 if ( depth ) {
3760                         for (ce=ce->ce_sibs; ce; ce=ce->ce_sibs) {
3761                                 rc = config_send( op, rs, ce, 0 );
3762                                 if ( rc ) break;
3763                         }
3764                 }
3765         }
3766         return rc;
3767 }
3768
3769 static ConfigTable *
3770 config_find_table( ConfigOCs **colst, int nocs, AttributeDescription *ad,
3771         ConfigArgs *ca )
3772 {
3773         int i, j;
3774
3775         for (j=0; j<nocs; j++) {
3776                 for (i=0; colst[j]->co_table[i].name; i++)
3777                         if ( colst[j]->co_table[i].ad == ad ) {
3778                                 ca->table = colst[j]->co_type;
3779                                 return &colst[j]->co_table[i];
3780                         }
3781         }
3782         return NULL;
3783 }
3784
3785 /* Sort the attributes of the entry according to the order defined
3786  * in the objectclass, with required attributes occurring before
3787  * allowed attributes. For any attributes with sequencing dependencies
3788  * (e.g., rootDN must be defined after suffix) the objectclass must
3789  * list the attributes in the desired sequence.
3790  */
3791 static void
3792 sort_attrs( Entry *e, ConfigOCs **colst, int nocs )
3793 {
3794         Attribute *a, *head = NULL, *tail = NULL, **prev;
3795         int i, j;
3796
3797         for (i=0; i<nocs; i++) {
3798                 if ( colst[i]->co_oc->soc_required ) {
3799                         AttributeType **at = colst[i]->co_oc->soc_required;
3800                         for (j=0; at[j]; j++) {
3801                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
3802                                         prev = &(*prev)->a_next, a=a->a_next) {
3803                                         if ( a->a_desc == at[j]->sat_ad ) {
3804                                                 *prev = a->a_next;
3805                                                 if (!head) {
3806                                                         head = a;
3807                                                         tail = a;
3808                                                 } else {
3809                                                         tail->a_next = a;
3810                                                         tail = a;
3811                                                 }
3812                                                 break;
3813                                         }
3814                                 }
3815                         }
3816                 }
3817                 if ( colst[i]->co_oc->soc_allowed ) {
3818                         AttributeType **at = colst[i]->co_oc->soc_allowed;
3819                         for (j=0; at[j]; j++) {
3820                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
3821                                         prev = &(*prev)->a_next, a=a->a_next) {
3822                                         if ( a->a_desc == at[j]->sat_ad ) {
3823                                                 *prev = a->a_next;
3824                                                 if (!head) {
3825                                                         head = a;
3826                                                         tail = a;
3827                                                 } else {
3828                                                         tail->a_next = a;
3829                                                         tail = a;
3830                                                 }
3831                                                 break;
3832                                         }
3833                                 }
3834                         }
3835                 }
3836         }
3837         if ( tail ) {
3838                 tail->a_next = e->e_attrs;
3839                 e->e_attrs = head;
3840         }
3841 }
3842
3843 static int
3844 check_vals( ConfigTable *ct, ConfigArgs *ca, void *ptr, int isAttr )
3845 {
3846         Attribute *a = NULL;
3847         AttributeDescription *ad;
3848         BerVarray vals;
3849
3850         int i, rc = 0;
3851
3852         if ( isAttr ) {
3853                 a = ptr;
3854                 ad = a->a_desc;
3855                 vals = a->a_vals;
3856         } else {
3857                 Modifications *ml = ptr;
3858                 ad = ml->sml_desc;
3859                 vals = ml->sml_values;
3860         }
3861
3862         if ( a && ( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL )) {
3863                 rc = ordered_value_sort( a, 1 );
3864                 if ( rc ) {
3865                         snprintf(ca->cr_msg, sizeof( ca->cr_msg ), "ordered_value_sort failed on attr %s\n",
3866                                 ad->ad_cname.bv_val );
3867                         return rc;
3868                 }
3869         }
3870         for ( i=0; vals[i].bv_val; i++ ) {
3871                 ca->line = vals[i].bv_val;
3872                 if (( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) &&
3873                         ca->line[0] == '{' ) {
3874                         char *idx = strchr( ca->line, '}' );
3875                         if ( idx ) ca->line = idx+1;
3876                 }
3877                 rc = config_parse_vals( ct, ca, i );
3878                 if ( rc ) {
3879                         break;
3880                 }
3881         }
3882         return rc;
3883 }
3884
3885 static int
3886 config_rename_attr( SlapReply *rs, Entry *e, struct berval *rdn,
3887         Attribute **at )
3888 {
3889         struct berval rtype, rval;
3890         Attribute *a;
3891         AttributeDescription *ad = NULL;
3892
3893         dnRdn( &e->e_name, rdn );
3894         rval.bv_val = strchr(rdn->bv_val, '=' ) + 1;
3895         rval.bv_len = rdn->bv_len - (rval.bv_val - rdn->bv_val);
3896         rtype.bv_val = rdn->bv_val;
3897         rtype.bv_len = rval.bv_val - rtype.bv_val - 1;
3898
3899         /* Find attr */
3900         slap_bv2ad( &rtype, &ad, &rs->sr_text );
3901         a = attr_find( e->e_attrs, ad );
3902         if (!a ) return LDAP_NAMING_VIOLATION;
3903         *at = a;
3904
3905         return 0;
3906 }
3907
3908 static void
3909 config_rename_kids( CfEntryInfo *ce )
3910 {
3911         CfEntryInfo *ce2;
3912         struct berval rdn, nrdn;
3913
3914         for (ce2 = ce->ce_kids; ce2; ce2 = ce2->ce_sibs) {
3915                 struct berval newdn, newndn;
3916                 dnRdn ( &ce2->ce_entry->e_name, &rdn );
3917                 dnRdn ( &ce2->ce_entry->e_nname, &nrdn );
3918                 build_new_dn( &newdn, &ce->ce_entry->e_name, &rdn, NULL );
3919                 build_new_dn( &newndn, &ce->ce_entry->e_nname, &nrdn, NULL );
3920                 free( ce2->ce_entry->e_name.bv_val );
3921                 free( ce2->ce_entry->e_nname.bv_val );
3922                 ce2->ce_entry->e_name = newdn;
3923                 ce2->ce_entry->e_nname = newndn;
3924                 config_rename_kids( ce2 );
3925         }
3926 }
3927
3928 static int
3929 config_rename_one( Operation *op, SlapReply *rs, Entry *e,
3930         CfEntryInfo *parent, Attribute *a, struct berval *newrdn,
3931         struct berval *nnewrdn, int use_ldif )
3932 {
3933         char *ptr1;
3934         int rc = 0;
3935         struct berval odn, ondn;
3936
3937         odn = e->e_name;
3938         ondn = e->e_nname;
3939         build_new_dn( &e->e_name, &parent->ce_entry->e_name, newrdn, NULL );
3940         build_new_dn( &e->e_nname, &parent->ce_entry->e_nname, nnewrdn, NULL );
3941
3942         /* Replace attr */
3943         free( a->a_vals[0].bv_val );
3944         ptr1 = strchr( newrdn->bv_val, '=' ) + 1;
3945         a->a_vals[0].bv_len = newrdn->bv_len - (ptr1 - newrdn->bv_val);
3946         a->a_vals[0].bv_val = ch_malloc( a->a_vals[0].bv_len + 1 );
3947         strcpy( a->a_vals[0].bv_val, ptr1 );
3948
3949         if ( a->a_nvals != a->a_vals ) {
3950                 free( a->a_nvals[0].bv_val );
3951                 ptr1 = strchr( nnewrdn->bv_val, '=' ) + 1;
3952                 a->a_nvals[0].bv_len = nnewrdn->bv_len - (ptr1 - nnewrdn->bv_val);
3953                 a->a_nvals[0].bv_val = ch_malloc( a->a_nvals[0].bv_len + 1 );
3954                 strcpy( a->a_nvals[0].bv_val, ptr1 );
3955         }
3956         if ( use_ldif ) {
3957                 CfBackInfo *cfb = (CfBackInfo *)op->o_bd->be_private;
3958                 BackendDB *be = op->o_bd;
3959                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
3960                 struct berval dn, ndn, xdn, xndn;
3961
3962                 op->o_bd = &cfb->cb_db;
3963
3964                 /* Save current rootdn; use the underlying DB's rootdn */
3965                 dn = op->o_dn;
3966                 ndn = op->o_ndn;
3967                 xdn = op->o_req_dn;
3968                 xndn = op->o_req_ndn;
3969                 op->o_dn = op->o_bd->be_rootdn;
3970                 op->o_ndn = op->o_bd->be_rootndn;
3971                 op->o_req_dn = odn;
3972                 op->o_req_ndn = ondn;
3973
3974                 scp = op->o_callback;
3975                 op->o_callback = &sc;
3976                 op->orr_newrdn = *newrdn;
3977                 op->orr_nnewrdn = *nnewrdn;
3978                 op->orr_newSup = NULL;
3979                 op->orr_nnewSup = NULL;
3980                 op->orr_deleteoldrdn = 1;
3981                 op->orr_modlist = NULL;
3982                 slap_modrdn2mods( op, rs );
3983                 slap_mods_opattrs( op, &op->orr_modlist, 1 );
3984                 rc = op->o_bd->be_modrdn( op, rs );
3985                 slap_mods_free( op->orr_modlist, 1 );
3986
3987                 op->o_bd = be;
3988                 op->o_callback = scp;
3989                 op->o_dn = dn;
3990                 op->o_ndn = ndn;
3991                 op->o_req_dn = xdn;
3992                 op->o_req_ndn = xndn;
3993         }
3994         free( odn.bv_val );
3995         free( ondn.bv_val );
3996         if ( e->e_private )
3997                 config_rename_kids( e->e_private );
3998         return rc;
3999 }
4000
4001 static int
4002 config_renumber_one( Operation *op, SlapReply *rs, CfEntryInfo *parent, 
4003         Entry *e, int idx, int tailindex, int use_ldif )
4004 {
4005         struct berval ival, newrdn, nnewrdn;
4006         struct berval rdn;
4007         Attribute *a;
4008         char ibuf[32], *ptr1, *ptr2 = NULL;
4009         int rc = 0;
4010
4011         rc = config_rename_attr( rs, e, &rdn, &a );
4012         if ( rc ) return rc;
4013
4014         ival.bv_val = ibuf;
4015         ival.bv_len = snprintf( ibuf, sizeof( ibuf ), SLAP_X_ORDERED_FMT, idx );
4016         if ( ival.bv_len >= sizeof( ibuf ) ) {
4017                 return LDAP_NAMING_VIOLATION;
4018         }
4019         
4020         newrdn.bv_len = rdn.bv_len + ival.bv_len;
4021         newrdn.bv_val = ch_malloc( newrdn.bv_len+1 );
4022
4023         if ( tailindex ) {
4024                 ptr1 = lutil_strncopy( newrdn.bv_val, rdn.bv_val, rdn.bv_len );
4025                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
4026         } else {
4027                 int xlen;
4028                 ptr2 = ber_bvchr( &rdn, '}' );
4029                 if ( ptr2 ) {
4030                         ptr2++;
4031                 } else {
4032                         ptr2 = rdn.bv_val + a->a_desc->ad_cname.bv_len + 1;
4033                 }
4034                 xlen = rdn.bv_len - (ptr2 - rdn.bv_val);
4035                 ptr1 = lutil_strncopy( newrdn.bv_val, a->a_desc->ad_cname.bv_val,
4036                         a->a_desc->ad_cname.bv_len );
4037                 *ptr1++ = '=';
4038                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
4039                 ptr1 = lutil_strncopy( ptr1, ptr2, xlen );
4040                 *ptr1 = '\0';
4041         }
4042
4043         /* Do the equivalent of ModRDN */
4044         /* Replace DN / NDN */
4045         newrdn.bv_len = ptr1 - newrdn.bv_val;
4046         rdnNormalize( 0, NULL, NULL, &newrdn, &nnewrdn, NULL );
4047         rc = config_rename_one( op, rs, e, parent, a, &newrdn, &nnewrdn, use_ldif );
4048
4049         free( nnewrdn.bv_val );
4050         free( newrdn.bv_val );
4051         return rc;
4052 }
4053
4054 static int
4055 check_name_index( CfEntryInfo *parent, ConfigType ce_type, Entry *e,
4056         SlapReply *rs, int *renum, int *ibase )
4057 {
4058         CfEntryInfo *ce;
4059         int index = -1, gotindex = 0, nsibs, rc = 0;
4060         int renumber = 0, tailindex = 0, isfrontend = 0, isconfig = 0;
4061         char *ptr1, *ptr2 = NULL;
4062         struct berval rdn;
4063
4064         if ( renum ) *renum = 0;
4065
4066         /* These entries don't get indexed/renumbered */
4067         if ( ce_type == Cft_Global ) return 0;
4068         if ( ce_type == Cft_Schema && parent->ce_type == Cft_Global ) return 0;
4069
4070         if ( ce_type == Cft_Module )
4071                 tailindex = 1;
4072
4073         /* See if the rdn has an index already */
4074         dnRdn( &e->e_name, &rdn );
4075         if ( ce_type == Cft_Database ) {
4076                 if ( !strncmp( rdn.bv_val + rdn.bv_len - STRLENOF("frontend"),
4077                                 "frontend", STRLENOF("frontend") )) 
4078                         isfrontend = 1;
4079                 else if ( !strncmp( rdn.bv_val + rdn.bv_len - STRLENOF("config"),
4080                                 "config", STRLENOF("config") )) 
4081                         isconfig = 1;
4082         }
4083         ptr1 = ber_bvchr( &e->e_name, '{' );
4084         if ( ptr1 && ptr1 < &e->e_name.bv_val[ rdn.bv_len ] ) {
4085                 char    *next;
4086                 ptr2 = strchr( ptr1, '}' );
4087                 if ( !ptr2 || ptr2 > &e->e_name.bv_val[ rdn.bv_len ] )
4088                         return LDAP_NAMING_VIOLATION;
4089                 if ( ptr2-ptr1 == 1)
4090                         return LDAP_NAMING_VIOLATION;
4091                 gotindex = 1;
4092                 index = strtol( ptr1 + 1, &next, 10 );
4093                 if ( next == ptr1 + 1 || next[ 0 ] != '}' ) {
4094                         return LDAP_NAMING_VIOLATION;
4095                 }
4096                 if ( index < 0 ) {
4097                         /* Special case, we allow -1 for the frontendDB */
4098                         if ( index != -1 || !isfrontend )
4099                                 return LDAP_NAMING_VIOLATION;
4100                 }
4101                 if ( isconfig && index != 0 ){
4102                         return LDAP_NAMING_VIOLATION;
4103                 }
4104         }
4105
4106         /* count related kids */
4107         for (nsibs=0, ce=parent->ce_kids; ce; ce=ce->ce_sibs) {
4108                 if ( ce->ce_type == ce_type ) nsibs++;
4109         }
4110
4111         /* account for -1 frontend */
4112         if ( ce_type == Cft_Database )
4113                 nsibs--;
4114
4115         if ( index != nsibs ) {
4116                 if ( gotindex ) {
4117                         if ( index < nsibs ) {
4118                                 if ( tailindex ) return LDAP_NAMING_VIOLATION;
4119                                 /* Siblings need to be renumbered */
4120                                 if ( index != -1 || !isfrontend )
4121                                         renumber = 1;
4122                         }
4123                 }
4124                 /* config DB is always "0" */
4125                 if ( isconfig && index == -1 ) {
4126                         index = 0;
4127                 }
4128                 if (( !isfrontend && index == -1 ) || ( index > nsibs ) ){
4129                         index = nsibs;
4130                 }
4131
4132                 /* just make index = nsibs */
4133                 if ( !renumber ) {
4134                         rc = config_renumber_one( NULL, rs, parent, e, index, tailindex, 0 );
4135                 }
4136         }
4137         if ( ibase ) *ibase = index;
4138         if ( renum ) *renum = renumber;
4139         return rc;
4140 }
4141
4142 static int
4143 count_oc( ObjectClass *oc, ConfigOCs ***copp, int *nocs )
4144 {
4145         ConfigOCs       co, *cop;
4146         ObjectClass     **sups;
4147
4148         co.co_name = &oc->soc_cname;
4149         cop = avl_find( CfOcTree, &co, CfOc_cmp );
4150         if ( cop ) {
4151                 int     i;
4152
4153                 /* check for duplicates */
4154                 for ( i = 0; i < *nocs; i++ ) {
4155                         if ( *copp && (*copp)[i] == cop ) {
4156                                 break;
4157                         }
4158                 }
4159
4160                 if ( i == *nocs ) {
4161                         ConfigOCs **tmp = ch_realloc( *copp, (*nocs + 1)*sizeof( ConfigOCs * ) );
4162                         if ( tmp == NULL ) {
4163                                 return -1;
4164                         }
4165                         *copp = tmp;
4166                         (*copp)[*nocs] = cop;
4167                         (*nocs)++;
4168                 }
4169         }
4170
4171         for ( sups = oc->soc_sups; sups && *sups; sups++ ) {
4172                 if ( count_oc( *sups, copp, nocs ) ) {
4173                         return -1;
4174                 }
4175         }
4176
4177         return 0;
4178 }
4179
4180 static ConfigOCs **
4181 count_ocs( Attribute *oc_at, int *nocs )
4182 {
4183         int             i;
4184         ConfigOCs       **colst = NULL;
4185
4186         *nocs = 0;
4187
4188         for ( i = 0; !BER_BVISNULL( &oc_at->a_nvals[i] ); i++ )
4189                 /* count attrs */ ;
4190
4191         for ( ; i--; ) {
4192                 ObjectClass     *oc = oc_bvfind( &oc_at->a_nvals[i] );
4193
4194                 assert( oc != NULL );
4195                 if ( count_oc( oc, &colst, nocs ) ) {
4196                         ch_free( colst );
4197                         return NULL;
4198                 }
4199         }
4200
4201         return colst;
4202 }
4203
4204 static int
4205 cfAddInclude( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
4206 {
4207         /* Leftover from RE23. Never parse this entry */
4208         return LDAP_COMPARE_TRUE;
4209 }
4210
4211 static int
4212 cfAddSchema( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
4213 {
4214         ConfigFile *cfo;
4215
4216         /* This entry is hardcoded, don't re-parse it */
4217         if ( p->ce_type == Cft_Global ) {
4218                 cfn = p->ce_private;
4219                 ca->ca_private = cfn;
4220                 return LDAP_COMPARE_TRUE;
4221         }
4222         if ( p->ce_type != Cft_Schema )
4223                 return LDAP_CONSTRAINT_VIOLATION;
4224
4225         cfn = ch_calloc( 1, sizeof(ConfigFile) );
4226         ca->ca_private = cfn;
4227         cfo = p->ce_private;
4228         cfn->c_sibs = cfo->c_kids;
4229         cfo->c_kids = cfn;
4230         return LDAP_SUCCESS;
4231 }
4232
4233 static int
4234 cfAddDatabase( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4235 {
4236         if ( p->ce_type != Cft_Global ) {
4237                 return LDAP_CONSTRAINT_VIOLATION;
4238         }
4239         /* config must be {0}, nothing else allowed */
4240         if ( !strncmp( e->e_nname.bv_val, "olcDatabase={0}", STRLENOF("olcDatabase={0}")) &&
4241                 strncmp( e->e_nname.bv_val + STRLENOF("olcDatabase={0}"), "config,", STRLENOF("config,") )) {
4242                 return LDAP_CONSTRAINT_VIOLATION;
4243         }
4244         ca->be = frontendDB;    /* just to get past check_vals */
4245         return LDAP_SUCCESS;
4246 }
4247
4248 static int
4249 cfAddBackend( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4250 {
4251         if ( p->ce_type != Cft_Global ) {
4252                 return LDAP_CONSTRAINT_VIOLATION;
4253         }
4254         return LDAP_SUCCESS;
4255 }
4256
4257 static int
4258 cfAddModule( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4259 {
4260         if ( p->ce_type != Cft_Global ) {
4261                 return LDAP_CONSTRAINT_VIOLATION;
4262         }
4263         return LDAP_SUCCESS;
4264 }
4265
4266 static int
4267 cfAddOverlay( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4268 {
4269         if ( p->ce_type != Cft_Database ) {
4270                 return LDAP_CONSTRAINT_VIOLATION;
4271         }
4272         ca->be = p->ce_be;
4273         return LDAP_SUCCESS;
4274 }
4275
4276 static void
4277 schema_destroy_one( ConfigArgs *ca, ConfigOCs **colst, int nocs,
4278         CfEntryInfo *p )
4279 {
4280         ConfigTable *ct;
4281         ConfigFile *cfo;
4282         AttributeDescription *ad;
4283         const char *text;
4284
4285         ca->valx = -1;
4286         ca->line = NULL;
4287         if ( cfn->c_cr_head ) {
4288                 struct berval bv = BER_BVC("olcDitContentRules");
4289                 ad = NULL;
4290                 slap_bv2ad( &bv, &ad, &text );
4291                 ct = config_find_table( colst, nocs, ad, ca );
4292                 config_del_vals( ct, ca );
4293         }
4294         if ( cfn->c_oc_head ) {
4295                 struct berval bv = BER_BVC("olcObjectClasses");
4296                 ad = NULL;
4297                 slap_bv2ad( &bv, &ad, &text );
4298                 ct = config_find_table( colst, nocs, ad, ca );
4299                 config_del_vals( ct, ca );
4300         }
4301         if ( cfn->c_at_head ) {
4302                 struct berval bv = BER_BVC("olcAttributeTypes");
4303                 ad = NULL;
4304                 slap_bv2ad( &bv, &ad, &text );
4305                 ct = config_find_table( colst, nocs, ad, ca );
4306                 config_del_vals( ct, ca );
4307         }
4308         if ( cfn->c_syn_head ) {
4309                 struct berval bv = BER_BVC("olcLdapSyntaxes");
4310                 ad = NULL;
4311                 slap_bv2ad( &bv, &ad, &text );
4312                 ct = config_find_table( colst, nocs, ad, ca );
4313                 config_del_vals( ct, ca );
4314         }
4315         if ( cfn->c_om_head ) {
4316                 struct berval bv = BER_BVC("olcObjectIdentifier");
4317                 ad = NULL;
4318                 slap_bv2ad( &bv, &ad, &text );
4319                 ct = config_find_table( colst, nocs, ad, ca );
4320                 config_del_vals( ct, ca );
4321         }
4322         cfo = p->ce_private;
4323         cfo->c_kids = cfn->c_sibs;
4324         ch_free( cfn );
4325 }
4326
4327 static int
4328 config_add_oc( ConfigOCs **cop, CfEntryInfo *last, Entry *e, ConfigArgs *ca )
4329 {
4330         int             rc = LDAP_CONSTRAINT_VIOLATION;
4331         ObjectClass     **ocp;
4332
4333         if ( (*cop)->co_ldadd ) {
4334                 rc = (*cop)->co_ldadd( last, e, ca );
4335                 if ( rc != LDAP_CONSTRAINT_VIOLATION ) {
4336                         return rc;
4337                 }
4338         }
4339
4340         for ( ocp = (*cop)->co_oc->soc_sups; ocp && *ocp; ocp++ ) {
4341                 ConfigOCs       co = { 0 };
4342
4343                 co.co_name = &(*ocp)->soc_cname;
4344                 *cop = avl_find( CfOcTree, &co, CfOc_cmp );
4345                 if ( *cop == NULL ) {
4346                         return rc;
4347                 }
4348
4349                 rc = config_add_oc( cop, last, e, ca );
4350                 if ( rc != LDAP_CONSTRAINT_VIOLATION ) {
4351                         return rc;
4352                 }
4353         }
4354
4355         return rc;
4356 }
4357
4358 /* Parse an LDAP entry into config directives */
4359 static int
4360 config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca, SlapReply *rs,
4361         int *renum, Operation *op )
4362 {
4363         CfEntryInfo     *ce, *last = NULL;
4364         ConfigOCs       co, *coptr, **colst;
4365         Attribute       *a, *oc_at, *soc_at;
4366         int             i, ibase = -1, nocs, rc = 0;
4367         struct berval   pdn;
4368         ConfigTable     *ct;
4369         char            *ptr, *log_prefix = op ? op->o_log_prefix : "";
4370
4371         memset( ca, 0, sizeof(ConfigArgs));
4372
4373         /* Make sure parent exists and entry does not. But allow
4374          * Databases and Overlays to be inserted. Don't do any
4375          * auto-renumbering if manageDSAit control is present.
4376          */
4377         ce = config_find_base( cfb->cb_root, &e->e_nname, &last );
4378         if ( ce ) {
4379                 if ( ( op && op->o_managedsait ) ||
4380                         ( ce->ce_type != Cft_Database && ce->ce_type != Cft_Overlay &&
4381                           ce->ce_type != Cft_Module ) )
4382                 {
4383                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4384                                 "DN=\"%s\" already exists\n",
4385                                 log_prefix, e->e_name.bv_val, 0 );
4386                         return LDAP_ALREADY_EXISTS;
4387                 }
4388         }
4389
4390         dnParent( &e->e_nname, &pdn );
4391
4392         /* If last is NULL, the new entry is the root/suffix entry, 
4393          * otherwise last should be the parent.
4394          */
4395         if ( last && !dn_match( &last->ce_entry->e_nname, &pdn ) ) {
4396                 if ( rs ) {
4397                         rs->sr_matched = last->ce_entry->e_name.bv_val;
4398                 }
4399                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4400                         "DN=\"%s\" not child of DN=\"%s\"\n",
4401                         log_prefix, e->e_name.bv_val,
4402                         last->ce_entry->e_name.bv_val );
4403                 return LDAP_NO_SUCH_OBJECT;
4404         }
4405
4406         if ( op ) {
4407                 /* No parent, must be root. This will never happen... */
4408                 if ( !last && !be_isroot( op ) && !be_shadow_update( op ) ) {
4409                         return LDAP_NO_SUCH_OBJECT;
4410                 }
4411
4412                 if ( last && !access_allowed( op, last->ce_entry,
4413                         slap_schema.si_ad_children, NULL, ACL_WADD, NULL ) )
4414                 {
4415                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4416                                 "DN=\"%s\" no write access to \"children\" of parent\n",
4417                                 log_prefix, e->e_name.bv_val, 0 );
4418                         return LDAP_INSUFFICIENT_ACCESS;
4419                 }
4420         }
4421
4422         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
4423         if ( !oc_at ) {
4424                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4425                         "DN=\"%s\" no objectClass\n",
4426                         log_prefix, e->e_name.bv_val, 0 );
4427                 return LDAP_OBJECT_CLASS_VIOLATION;
4428         }
4429
4430         soc_at = attr_find( e->e_attrs, slap_schema.si_ad_structuralObjectClass );
4431         if ( !soc_at ) {
4432                 ObjectClass     *soc = NULL;
4433                 char            textbuf[ SLAP_TEXT_BUFLEN ];
4434                 const char      *text = textbuf;
4435
4436                 /* FIXME: check result */
4437                 rc = structural_class( oc_at->a_nvals, &soc, NULL,
4438                         &text, textbuf, sizeof(textbuf), NULL );
4439                 if ( rc != LDAP_SUCCESS ) {
4440                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4441                                 "DN=\"%s\" no structural objectClass (%s)\n",
4442                                 log_prefix, e->e_name.bv_val, text );
4443                         return rc;
4444                 }
4445                 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &soc->soc_cname, NULL );
4446                 soc_at = attr_find( e->e_attrs, slap_schema.si_ad_structuralObjectClass );
4447                 if ( soc_at == NULL ) {
4448                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4449                                 "DN=\"%s\" no structural objectClass; "
4450                                 "unable to merge computed class %s\n",
4451                                 log_prefix, e->e_name.bv_val,
4452                                 soc->soc_cname.bv_val );
4453                         return LDAP_OBJECT_CLASS_VIOLATION;
4454                 }
4455
4456                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4457                         "DN=\"%s\" no structural objectClass; "
4458                         "computed objectClass %s merged\n",
4459                         log_prefix, e->e_name.bv_val,
4460                         soc->soc_cname.bv_val );
4461         }
4462
4463         /* Fake the coordinates based on whether we're part of an
4464          * LDAP Add or if reading the config dir
4465          */
4466         if ( rs ) {
4467                 ca->fname = "slapd";
4468                 ca->lineno = 0;
4469         } else {
4470                 ca->fname = cfdir.bv_val;
4471                 ca->lineno = 1;
4472         }
4473         ca->ca_op = op;
4474
4475         co.co_name = &soc_at->a_nvals[0];
4476         coptr = avl_find( CfOcTree, &co, CfOc_cmp );
4477         if ( coptr == NULL ) {
4478                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4479                         "DN=\"%s\" no structural objectClass in configuration table\n",
4480                         log_prefix, e->e_name.bv_val, 0 );
4481                 return LDAP_OBJECT_CLASS_VIOLATION;
4482         }
4483
4484         /* Only the root can be Cft_Global, everything else must
4485          * have a parent. Only limited nesting arrangements are allowed.
4486          */
4487         rc = LDAP_CONSTRAINT_VIOLATION;
4488         if ( coptr->co_type == Cft_Global && !last ) {
4489                 cfn = cfb->cb_config;
4490                 ca->ca_private = cfn;
4491                 ca->be = frontendDB;    /* just to get past check_vals */
4492                 rc = LDAP_SUCCESS;
4493         }
4494
4495         colst = count_ocs( oc_at, &nocs );
4496
4497         /* Check whether the Add is allowed by its parent, and do
4498          * any necessary arg setup
4499          */
4500         if ( last ) {
4501                 rc = config_add_oc( &coptr, last, e, ca );
4502                 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
4503                         for ( i = 0; i<nocs; i++ ) {
4504                                 /* Already checked these */
4505                                 if ( colst[i]->co_oc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
4506                                         continue;
4507                                 if ( colst[i]->co_ldadd &&
4508                                         ( rc = colst[i]->co_ldadd( last, e, ca ))
4509                                                 != LDAP_CONSTRAINT_VIOLATION ) {
4510                                         coptr = colst[i];
4511                                         break;
4512                                 }
4513                         }
4514                 }
4515                 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
4516                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4517                                 "DN=\"%s\" no structural objectClass add function\n",
4518                                 log_prefix, e->e_name.bv_val, 0 );
4519                         return LDAP_OBJECT_CLASS_VIOLATION;
4520                 }
4521         }
4522
4523         /* Add the entry but don't parse it, we already have its contents */
4524         if ( rc == LDAP_COMPARE_TRUE ) {
4525                 rc = LDAP_SUCCESS;
4526                 goto ok;
4527         }
4528
4529         if ( rc != LDAP_SUCCESS )
4530                 goto done_noop;
4531
4532         /* Parse all the values and check for simple syntax errors before
4533          * performing any set actions.
4534          *
4535          * If doing an LDAPadd, check for indexed names and any necessary
4536          * renaming/renumbering. Entries that don't need indexed names are
4537          * ignored. Entries that need an indexed name and arrive without one
4538          * are assigned to the end. Entries that arrive with an index may
4539          * cause the following entries to be renumbered/bumped down.
4540          *
4541          * Note that "pseudo-indexed" entries (cn=Include{xx}, cn=Module{xx})
4542          * don't allow Adding an entry with an index that's already in use.
4543          * This is flagged as an error (LDAP_ALREADY_EXISTS) up above.
4544          *
4545          * These entries can have auto-assigned indexes (appended to the end)
4546          * but only the other types support auto-renumbering of siblings.
4547          */
4548         {
4549                 rc = check_name_index( last, coptr->co_type, e, rs, renum,
4550                         &ibase );
4551                 if ( rc ) {
4552                         goto done_noop;
4553                 }
4554                 if ( renum && *renum && coptr->co_type != Cft_Database &&
4555                         coptr->co_type != Cft_Overlay )
4556                 {
4557                         snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
4558                                 "operation requires sibling renumbering" );
4559                         rc = LDAP_UNWILLING_TO_PERFORM;
4560                         goto done_noop;
4561                 }
4562         }
4563
4564         init_config_argv( ca );
4565
4566         /* Make sure we process attrs in the required order */
4567         sort_attrs( e, colst, nocs );
4568
4569         for ( a = e->e_attrs; a; a = a->a_next ) {
4570                 if ( a == oc_at ) continue;
4571                 ct = config_find_table( colst, nocs, a->a_desc, ca );
4572                 if ( !ct ) continue;    /* user data? */
4573                 rc = check_vals( ct, ca, a, 1 );
4574                 if ( rc ) goto done_noop;
4575         }
4576
4577         /* Basic syntax checks are OK. Do the actual settings. */
4578         for ( a=e->e_attrs; a; a=a->a_next ) {
4579                 if ( a == oc_at ) continue;
4580                 ct = config_find_table( colst, nocs, a->a_desc, ca );
4581                 if ( !ct ) continue;    /* user data? */
4582                 for (i=0; a->a_vals[i].bv_val; i++) {
4583                         char *iptr = NULL;
4584                         ca->valx = -1;
4585                         ca->line = a->a_vals[i].bv_val;
4586                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
4587                                 ptr = strchr( ca->line, '}' );
4588                                 if ( ptr ) {
4589                                         iptr = strchr( ca->line, '{' );
4590                                         ca->line = ptr+1;
4591                                 }
4592                         }
4593                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED_SIB ) {
4594                                 if ( iptr ) {
4595                                         ca->valx = strtol( iptr+1, NULL, 0 );
4596                                 }
4597                         } else {
4598                                 ca->valx = i;
4599                         }
4600                         rc = config_parse_add( ct, ca, i );
4601                         if ( rc ) {
4602                                 rc = LDAP_OTHER;
4603                                 goto done;
4604                         }
4605                 }
4606         }
4607 ok:
4608         /* Newly added databases and overlays need to be started up */
4609         if ( CONFIG_ONLINE_ADD( ca )) {
4610                 if ( colst[0]->co_type == Cft_Database ) {
4611                         rc = backend_startup_one( ca->be, &ca->reply );
4612
4613                 } else if ( colst[0]->co_type == Cft_Overlay ) {
4614                         if ( ca->bi->bi_db_open ) {
4615                                 BackendInfo *bi_orig = ca->be->bd_info;
4616                                 ca->be->bd_info = ca->bi;
4617                                 rc = ca->bi->bi_db_open( ca->be, &ca->reply );
4618                                 ca->be->bd_info = bi_orig;
4619                         }
4620                 } else if ( ca->cleanup ) {
4621                         rc = ca->cleanup( ca );
4622                 }
4623                 if ( rc ) {
4624                         if (ca->cr_msg[0] == '\0')
4625                                 snprintf( ca->cr_msg, sizeof( ca->cr_msg ), "<%s> failed startup", ca->argv[0] );
4626
4627                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
4628                                 ca->log, ca->cr_msg, ca->argv[1] );
4629                         rc = LDAP_OTHER;
4630                         goto done;
4631                 }
4632         }
4633
4634         ca->valx = ibase;
4635         ce = ch_calloc( 1, sizeof(CfEntryInfo) );
4636         ce->ce_parent = last;
4637         ce->ce_entry = entry_dup( e );
4638         ce->ce_entry->e_private = ce;
4639         ce->ce_type = colst[0]->co_type;
4640         ce->ce_be = ca->be;
4641         ce->ce_bi = ca->bi;
4642         ce->ce_private = ca->ca_private;
4643         ca->ca_entry = ce->ce_entry;
4644         if ( !last ) {
4645                 cfb->cb_root = ce;
4646         } else if ( last->ce_kids ) {
4647                 CfEntryInfo *c2, **cprev;
4648
4649                 /* Advance to first of this type */
4650                 cprev = &last->ce_kids;
4651                 for ( c2 = *cprev; c2 && c2->ce_type < ce->ce_type; ) {
4652                         cprev = &c2->ce_sibs;
4653                         c2 = c2->ce_sibs;
4654                 }
4655                 /* Account for the (-1) frontendDB entry */
4656                 if ( ce->ce_type == Cft_Database ) {
4657                         if ( ca->be == frontendDB )
4658                                 ibase = 0;
4659                         else if ( ibase != -1 )
4660                                 ibase++;
4661                 }
4662                 /* Append */
4663                 if ( ibase < 0 ) {
4664                         for (c2 = *cprev; c2 && c2->ce_type == ce->ce_type;) {
4665                                 cprev = &c2->ce_sibs;
4666                                 c2 = c2->ce_sibs;
4667                         }
4668                 } else {
4669                 /* Insert */
4670                         int i;
4671                         for ( i=0; i<ibase; i++ ) {
4672                                 c2 = *cprev;
4673                                 cprev = &c2->ce_sibs;
4674                         }
4675                 }
4676                 ce->ce_sibs = *cprev;
4677                 *cprev = ce;
4678         } else {
4679                 last->ce_kids = ce;
4680         }
4681
4682 done:
4683         if ( rc ) {
4684                 if ( (colst[0]->co_type == Cft_Database) && ca->be ) {
4685                         if ( ca->be != frontendDB )
4686                                 backend_destroy_one( ca->be, 1 );
4687                 } else if ( (colst[0]->co_type == Cft_Overlay) && ca->bi ) {
4688                         overlay_destroy_one( ca->be, (slap_overinst *)ca->bi );
4689                 } else if ( colst[0]->co_type == Cft_Schema ) {
4690                         schema_destroy_one( ca, colst, nocs, last );
4691                 }
4692         }
4693 done_noop:
4694
4695         ch_free( ca->argv );
4696         if ( colst ) ch_free( colst );
4697         return rc;
4698 }
4699
4700 #define BIGTMP  10000
4701 static int
4702 config_rename_add( Operation *op, SlapReply *rs, CfEntryInfo *ce,
4703         int base, int rebase, int max, int use_ldif )
4704 {
4705         CfEntryInfo *ce2, *ce3, *cetmp = NULL, *cerem = NULL;
4706         ConfigType etype = ce->ce_type;
4707         int count = 0, rc = 0;
4708
4709         /* Reverse ce list */
4710         for (ce2 = ce->ce_sibs;ce2;ce2 = ce3) {
4711                 if (ce2->ce_type != etype) {
4712                         cerem = ce2;
4713                         break;
4714                 }
4715                 ce3 = ce2->ce_sibs;
4716                 ce2->ce_sibs = cetmp;
4717                 cetmp = ce2;
4718                 count++;
4719                 if ( max && count >= max ) {
4720                         cerem = ce3;
4721                         break;
4722                 }
4723         }
4724
4725         /* Move original to a temp name until increments are done */
4726         if ( rebase ) {
4727                 ce->ce_entry->e_private = NULL;
4728                 rc = config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
4729                         base+BIGTMP, 0, use_ldif );
4730                 ce->ce_entry->e_private = ce;
4731         }
4732         /* start incrementing */
4733         for (ce2=cetmp; ce2; ce2=ce3) {
4734                 ce3 = ce2->ce_sibs;
4735                 ce2->ce_sibs = cerem;
4736                 cerem = ce2;
4737                 if ( rc == 0 ) 
4738                         rc = config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
4739                                 count+base, 0, use_ldif );
4740                 count--;
4741         }
4742         if ( rebase )
4743                 rc = config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
4744                         base, 0, use_ldif );
4745         return rc;
4746 }
4747
4748 static int
4749 config_rename_del( Operation *op, SlapReply *rs, CfEntryInfo *ce,
4750         CfEntryInfo *ce2, int old, int use_ldif )
4751 {
4752         int count = 0;
4753
4754         /* Renumber original to a temp value */
4755         ce->ce_entry->e_private = NULL;
4756         config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
4757                 old+BIGTMP, 0, use_ldif );
4758         ce->ce_entry->e_private = ce;
4759
4760         /* start decrementing */
4761         for (; ce2 != ce; ce2=ce2->ce_sibs) {
4762                 config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
4763                         count+old, 0, use_ldif );
4764                 count++;
4765         }
4766         return config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
4767                 count+old, 0, use_ldif );
4768 }
4769
4770 /* Parse an LDAP entry into config directives, then store in underlying
4771  * database.
4772  */
4773 static int
4774 config_back_add( Operation *op, SlapReply *rs )
4775 {
4776         CfBackInfo *cfb;
4777         int renumber;
4778         ConfigArgs ca;
4779
4780         if ( !access_allowed( op, op->ora_e, slap_schema.si_ad_entry,
4781                 NULL, ACL_WADD, NULL )) {
4782                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
4783                 goto out;
4784         }
4785
4786         /*
4787          * Check for attribute ACL
4788          */
4789         if ( !acl_check_modlist( op, op->ora_e, op->orm_modlist )) {
4790                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
4791                 rs->sr_text = "no write access to attribute";
4792                 goto out;
4793         }
4794
4795         cfb = (CfBackInfo *)op->o_bd->be_private;
4796
4797         /* add opattrs for syncprov */
4798         {
4799                 char textbuf[SLAP_TEXT_BUFLEN];
4800                 size_t textlen = sizeof textbuf;
4801                 rs->sr_err = entry_schema_check(op, op->ora_e, NULL, 0, 1, NULL,
4802                         &rs->sr_text, textbuf, sizeof( textbuf ) );
4803                 if ( rs->sr_err != LDAP_SUCCESS )
4804                         goto out;
4805                 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
4806                 if ( rs->sr_err != LDAP_SUCCESS ) {
4807                         Debug( LDAP_DEBUG_TRACE,
4808                                 LDAP_XSTRING(config_back_add) ": entry failed op attrs add: "
4809                                 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
4810                         goto out;
4811                 }
4812         }
4813
4814         if ( op->o_abandon ) {
4815                 rs->sr_err = SLAPD_ABANDON;
4816                 goto out;
4817         }
4818         ldap_pvt_thread_pool_pause( &connection_pool );
4819
4820         /* Strategy:
4821          * 1) check for existence of entry
4822          * 2) check for sibling renumbering
4823          * 3) perform internal add
4824          * 4) perform any necessary renumbering
4825          * 5) store entry in underlying database
4826          */
4827         rs->sr_err = config_add_internal( cfb, op->ora_e, &ca, rs, &renumber, op );
4828         if ( rs->sr_err != LDAP_SUCCESS ) {
4829                 rs->sr_text = ca.cr_msg;
4830                 goto out2;
4831         }
4832
4833         if ( renumber ) {
4834                 CfEntryInfo *ce = ca.ca_entry->e_private;
4835                 req_add_s addr = op->oq_add;
4836                 op->o_tag = LDAP_REQ_MODRDN;
4837                 rs->sr_err = config_rename_add( op, rs, ce, ca.valx, 0, 0, cfb->cb_use_ldif );
4838                 op->o_tag = LDAP_REQ_ADD;
4839                 op->oq_add = addr;
4840                 if ( rs->sr_err != LDAP_SUCCESS ) {
4841                         goto out2;
4842                 }
4843         }
4844
4845         if ( cfb->cb_use_ldif ) {
4846                 BackendDB *be = op->o_bd;
4847                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
4848                 struct berval dn, ndn;
4849
4850                 op->o_bd = &cfb->cb_db;
4851
4852                 /* Save current rootdn; use the underlying DB's rootdn */
4853                 dn = op->o_dn;
4854                 ndn = op->o_ndn;
4855                 op->o_dn = op->o_bd->be_rootdn;
4856                 op->o_ndn = op->o_bd->be_rootndn;
4857
4858                 scp = op->o_callback;
4859                 op->o_callback = &sc;
4860                 op->o_bd->be_add( op, rs );
4861                 op->o_bd = be;
4862                 op->o_callback = scp;
4863                 op->o_dn = dn;
4864                 op->o_ndn = ndn;
4865         }
4866
4867 out2:;
4868         ldap_pvt_thread_pool_resume( &connection_pool );
4869
4870 out:;
4871         send_ldap_result( op, rs );
4872         slap_graduate_commit_csn( op );
4873         return rs->sr_err;
4874 }
4875
4876 typedef struct delrec {
4877         struct delrec *next;
4878         int nidx;
4879         int idx[1];
4880 } delrec;
4881
4882 static int
4883 config_modify_add( ConfigTable *ct, ConfigArgs *ca, AttributeDescription *ad,
4884         int i )
4885 {
4886         int rc;
4887
4888         ca->valx = -1;
4889         if (ad->ad_type->sat_flags & SLAP_AT_ORDERED &&
4890                 ca->line[0] == '{' )
4891         {
4892                 char *ptr = strchr( ca->line + 1, '}' );
4893                 if ( ptr ) {
4894                         char    *next;
4895
4896                         ca->valx = strtol( ca->line + 1, &next, 0 );
4897                         if ( next == ca->line + 1 || next[ 0 ] != '}' ) {
4898                                 return LDAP_OTHER;
4899                         }
4900                         ca->line = ptr+1;
4901                 }
4902         }
4903         rc = config_parse_add( ct, ca, i );
4904         if ( rc ) {
4905                 rc = LDAP_OTHER;
4906         }
4907         return rc;
4908 }
4909
4910 static int
4911 config_modify_internal( CfEntryInfo *ce, Operation *op, SlapReply *rs,
4912         ConfigArgs *ca )
4913 {
4914         int rc = LDAP_UNWILLING_TO_PERFORM;
4915         Modifications *ml;
4916         Entry *e = ce->ce_entry;
4917         Attribute *save_attrs = e->e_attrs, *oc_at, *s, *a;
4918         ConfigTable *ct;
4919         ConfigOCs **colst;
4920         int i, nocs;
4921         char *ptr;
4922         delrec *dels = NULL, *deltail = NULL;
4923
4924         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
4925         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
4926
4927         colst = count_ocs( oc_at, &nocs );
4928
4929         /* make sure add/del flags are clear; should always be true */
4930         for ( s = save_attrs; s; s = s->a_next ) {
4931                 s->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
4932         }
4933
4934         e->e_attrs = attrs_dup( e->e_attrs );
4935
4936         init_config_argv( ca );
4937         ca->be = ce->ce_be;
4938         ca->bi = ce->ce_bi;
4939         ca->ca_private = ce->ce_private;
4940         ca->ca_entry = e;
4941         ca->fname = "slapd";
4942         ca->ca_op = op;
4943         strcpy( ca->log, "back-config" );
4944
4945         for (ml = op->orm_modlist; ml; ml=ml->sml_next) {
4946                 ct = config_find_table( colst, nocs, ml->sml_desc, ca );
4947                 switch (ml->sml_op) {
4948                 case LDAP_MOD_DELETE:
4949                 case LDAP_MOD_REPLACE: {
4950                         BerVarray vals = NULL, nvals = NULL;
4951                         int *idx = NULL;
4952                         if ( ct && ( ct->arg_type & ARG_NO_DELETE )) {
4953                                 rc = LDAP_OTHER;
4954                                 snprintf(ca->cr_msg, sizeof(ca->cr_msg), "cannot delete %s",
4955                                         ml->sml_desc->ad_cname.bv_val );
4956                                 goto out_noop;
4957                         }
4958                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
4959                                 vals = ml->sml_values;
4960                                 nvals = ml->sml_nvalues;
4961                                 ml->sml_values = NULL;
4962                                 ml->sml_nvalues = NULL;
4963                         }
4964                         /* If we're deleting by values, remember the indexes of the
4965                          * values we deleted.
4966                          */
4967                         if ( ct && ml->sml_values ) {
4968                                 delrec *d;
4969                                 i = ml->sml_numvals;
4970                                 d = ch_malloc( sizeof(delrec) + (i - 1)* sizeof(int));
4971                                 d->nidx = i;
4972                                 d->next = NULL;
4973                                 if ( dels ) {
4974                                         deltail->next = d;
4975                                 } else {
4976                                         dels = d;
4977                                 }
4978                                 deltail = d;
4979                                 idx = d->idx;
4980                         }
4981                         rc = modify_delete_vindex(e, &ml->sml_mod,
4982                                 get_permissiveModify(op),
4983                                 &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg), idx );
4984                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
4985                                 ml->sml_values = vals;
4986                                 ml->sml_nvalues = nvals;
4987                         }
4988                         if ( !vals )
4989                                 break;
4990                         }
4991                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
4992
4993                 case LDAP_MOD_ADD:
4994                 case SLAP_MOD_SOFTADD: {
4995                         int mop = ml->sml_op;
4996                         int navals = -1;
4997                         ml->sml_op = LDAP_MOD_ADD;
4998                         if ( ct ) {
4999                                 if ( ct->arg_type & ARG_NO_INSERT ) {
5000                                         Attribute *a = attr_find( e->e_attrs, ml->sml_desc );
5001                                         if ( a ) {
5002                                                 navals = a->a_numvals;
5003                                         }
5004                                 }
5005                                 for ( i=0; !BER_BVISNULL( &ml->sml_values[i] ); i++ ) {
5006                                         if ( ml->sml_values[i].bv_val[0] == '{' &&
5007                                                 navals >= 0 )
5008                                         {
5009                                                 char    *next, *val = ml->sml_values[i].bv_val + 1;
5010                                                 int     j;
5011
5012                                                 j = strtol( val, &next, 0 );
5013                                                 if ( next == val || next[ 0 ] != '}' || j < navals ) {
5014                                                         rc = LDAP_OTHER;
5015                                                         snprintf(ca->cr_msg, sizeof(ca->cr_msg), "cannot insert %s",
5016                                                                 ml->sml_desc->ad_cname.bv_val );
5017                                                         goto out_noop;
5018                                                 }
5019                                         }
5020                                         rc = check_vals( ct, ca, ml, 0 );
5021                                         if ( rc ) goto out_noop;
5022                                 }
5023                         }
5024                         rc = modify_add_values(e, &ml->sml_mod,
5025                                    get_permissiveModify(op),
5026                                    &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg) );
5027
5028                         /* If value already exists, show success here
5029                          * and ignore this operation down below.
5030                          */
5031                         if ( mop == SLAP_MOD_SOFTADD ) {
5032                                 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS )
5033                                         rc = LDAP_SUCCESS;
5034                                 else
5035                                         mop = LDAP_MOD_ADD;
5036                         }
5037                         ml->sml_op = mop;
5038                         break;
5039                         }
5040
5041                         break;
5042                 case LDAP_MOD_INCREMENT:        /* FIXME */
5043                         break;
5044                 default:
5045                         break;
5046                 }
5047                 if(rc != LDAP_SUCCESS) break;
5048         }
5049         
5050         if ( rc == LDAP_SUCCESS) {
5051                 /* check that the entry still obeys the schema */
5052                 rc = entry_schema_check(op, e, NULL, 0, 0, NULL,
5053                         &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg) );
5054         }
5055         if ( rc ) goto out_noop;
5056
5057         /* Basic syntax checks are OK. Do the actual settings. */
5058         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
5059                 ct = config_find_table( colst, nocs, ml->sml_desc, ca );
5060                 if ( !ct ) continue;
5061
5062                 s = attr_find( save_attrs, ml->sml_desc );
5063                 a = attr_find( e->e_attrs, ml->sml_desc );
5064
5065                 switch (ml->sml_op) {
5066                 case LDAP_MOD_DELETE:
5067                 case LDAP_MOD_REPLACE: {
5068                         BerVarray vals = NULL, nvals = NULL;
5069                         delrec *d = NULL;
5070
5071                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5072                                 vals = ml->sml_values;
5073                                 nvals = ml->sml_nvalues;
5074                                 ml->sml_values = NULL;
5075                                 ml->sml_nvalues = NULL;
5076                         }
5077
5078                         if ( ml->sml_values )
5079                                 d = dels;
5080
5081                         /* If we didn't delete the whole attribute */
5082                         if ( ml->sml_values && a ) {
5083                                 struct berval *mvals;
5084                                 int j;
5085
5086                                 if ( ml->sml_nvalues )
5087                                         mvals = ml->sml_nvalues;
5088                                 else
5089                                         mvals = ml->sml_values;
5090
5091                                 /* use the indexes we saved up above */
5092                                 for (i=0; i < d->nidx; i++) {
5093                                         struct berval bv = *mvals++;
5094                                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED &&
5095                                                 bv.bv_val[0] == '{' ) {
5096                                                 ptr = strchr( bv.bv_val, '}' ) + 1;
5097                                                 bv.bv_len -= ptr - bv.bv_val;
5098                                                 bv.bv_val = ptr;
5099                                         }
5100                                         ca->line = bv.bv_val;
5101                                         ca->valx = d->idx[i];
5102                                         rc = config_del_vals( ct, ca );
5103                                         if ( rc != LDAP_SUCCESS ) break;
5104                                         if ( s )
5105                                                 s->a_flags |= SLAP_ATTR_IXDEL;
5106                                         for (j=i+1; j < d->nidx; j++)
5107                                                 if ( d->idx[j] >d->idx[i] )
5108                                                         d->idx[j]--;
5109                                 }
5110                         } else {
5111                                 ca->valx = -1;
5112                                 ca->line = NULL;
5113                                 rc = config_del_vals( ct, ca );
5114                                 if ( rc ) rc = LDAP_OTHER;
5115                                 if ( s )
5116                                         s->a_flags |= SLAP_ATTR_IXDEL;
5117                         }
5118                         if ( ml->sml_values ) {
5119                                 d = d->next;
5120                                 ch_free( dels );
5121                                 dels = d;
5122                         }
5123                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5124                                 ml->sml_values = vals;
5125                                 ml->sml_nvalues = nvals;
5126                         }
5127                         if ( !vals || rc != LDAP_SUCCESS )
5128                                 break;
5129                         }
5130                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
5131
5132                 case LDAP_MOD_ADD:
5133                         for (i=0; ml->sml_values[i].bv_val; i++) {
5134                                 ca->line = ml->sml_values[i].bv_val;
5135                                 ca->valx = -1;
5136                                 rc = config_modify_add( ct, ca, ml->sml_desc, i );
5137                                 if ( rc )
5138                                         goto out;
5139                                 a->a_flags |= SLAP_ATTR_IXADD;
5140                         }
5141                         break;
5142                 }
5143         }
5144
5145 out:
5146         /* Undo for a failed operation */
5147         if ( rc != LDAP_SUCCESS ) {
5148                 ConfigReply msg = ca->reply;
5149                 for ( s = save_attrs; s; s = s->a_next ) {
5150                         if ( s->a_flags & SLAP_ATTR_IXDEL ) {
5151                                 s->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
5152                                 ct = config_find_table( colst, nocs, s->a_desc, ca );
5153                                 a = attr_find( e->e_attrs, s->a_desc );
5154                                 if ( a ) {
5155                                         /* clear the flag so the add check below will skip it */
5156                                         a->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
5157                                         ca->valx = -1;
5158                                         ca->line = NULL;
5159                                         config_del_vals( ct, ca );
5160                                 }
5161                                 for ( i=0; !BER_BVISNULL( &s->a_vals[i] ); i++ ) {
5162                                         ca->line = s->a_vals[i].bv_val;
5163                                         ca->valx = -1;
5164                                         config_modify_add( ct, ca, s->a_desc, i );
5165                                 }
5166                         }
5167                 }
5168                 for ( a = e->e_attrs; a; a = a->a_next ) {
5169                         if ( a->a_flags & SLAP_ATTR_IXADD ) {
5170                                 ct = config_find_table( colst, nocs, a->a_desc, ca );
5171                                 ca->valx = -1;
5172                                 ca->line = NULL;
5173                                 config_del_vals( ct, ca );
5174                                 s = attr_find( save_attrs, a->a_desc );
5175                                 if ( s ) {
5176                                         s->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
5177                                         for ( i=0; !BER_BVISNULL( &s->a_vals[i] ); i++ ) {
5178                                                 ca->line = s->a_vals[i].bv_val;
5179                                                 ca->valx = -1;
5180                                                 config_modify_add( ct, ca, s->a_desc, i );
5181                                         }
5182                                 }
5183                         }
5184                 }
5185                 ca->reply = msg;
5186         }
5187
5188         if ( ca->cleanup )
5189                 ca->cleanup( ca );
5190 out_noop:
5191         if ( rc == LDAP_SUCCESS ) {
5192                 attrs_free( save_attrs );
5193         } else {
5194                 attrs_free( e->e_attrs );
5195                 e->e_attrs = save_attrs;
5196         }
5197         ch_free( ca->argv );
5198         if ( colst ) ch_free( colst );
5199         while( dels ) {
5200                 deltail = dels->next;
5201                 ch_free( dels );
5202                 dels = deltail;
5203         }
5204
5205         return rc;
5206 }
5207
5208 static int
5209 config_back_modify( Operation *op, SlapReply *rs )
5210 {
5211         CfBackInfo *cfb;
5212         CfEntryInfo *ce, *last;
5213         Modifications *ml;
5214         ConfigArgs ca = {0};
5215         struct berval rdn;
5216         char *ptr;
5217         AttributeDescription *rad = NULL;
5218         int do_pause = 1;
5219
5220         cfb = (CfBackInfo *)op->o_bd->be_private;
5221
5222         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5223         if ( !ce ) {
5224                 if ( last )
5225                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5226                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5227                 goto out;
5228         }
5229
5230         if ( !acl_check_modlist( op, ce->ce_entry, op->orm_modlist )) {
5231                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5232                 goto out;
5233         }
5234
5235         /* Get type of RDN */
5236         rdn = ce->ce_entry->e_nname;
5237         ptr = strchr( rdn.bv_val, '=' );
5238         rdn.bv_len = ptr - rdn.bv_val;
5239         slap_bv2ad( &rdn, &rad, &rs->sr_text );
5240
5241         /* Some basic validation... */
5242         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
5243                 /* Don't allow Modify of RDN; must use ModRdn for that. */
5244                 if ( ml->sml_desc == rad ) {
5245                         rs->sr_err = LDAP_NOT_ALLOWED_ON_RDN;
5246                         rs->sr_text = "Use modrdn to change the entry name";
5247                         goto out;
5248                 }
5249                 /* Internal update of contextCSN? */
5250                 if ( ml->sml_desc == slap_schema.si_ad_contextCSN && op->o_conn->c_conn_idx == -1 ) {
5251                         do_pause = 0;
5252                         break;
5253                 }
5254         }
5255
5256         slap_mods_opattrs( op, &op->orm_modlist, 1 );
5257
5258         if ( do_pause ) {
5259                 if ( op->o_abandon ) {
5260                         rs->sr_err = SLAPD_ABANDON;
5261                         goto out;
5262                 }
5263                 ldap_pvt_thread_pool_pause( &connection_pool );
5264         }
5265
5266         /* Strategy:
5267          * 1) perform the Modify on the cached Entry.
5268          * 2) verify that the Entry still satisfies the schema.
5269          * 3) perform the individual config operations.
5270          * 4) store Modified entry in underlying LDIF backend.
5271          */
5272         rs->sr_err = config_modify_internal( ce, op, rs, &ca );
5273         if ( rs->sr_err ) {
5274                 rs->sr_text = ca.cr_msg;
5275         } else if ( cfb->cb_use_ldif ) {
5276                 BackendDB *be = op->o_bd;
5277                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
5278                 struct berval dn, ndn;
5279
5280                 op->o_bd = &cfb->cb_db;
5281
5282                 dn = op->o_dn;
5283                 ndn = op->o_ndn;
5284                 op->o_dn = op->o_bd->be_rootdn;
5285                 op->o_ndn = op->o_bd->be_rootndn;
5286
5287                 scp = op->o_callback;
5288                 op->o_callback = &sc;
5289                 op->o_bd->be_modify( op, rs );
5290                 op->o_bd = be;
5291                 op->o_callback = scp;
5292                 op->o_dn = dn;
5293                 op->o_ndn = ndn;
5294         }
5295
5296         if ( do_pause )
5297                 ldap_pvt_thread_pool_resume( &connection_pool );
5298 out:
5299         send_ldap_result( op, rs );
5300         slap_graduate_commit_csn( op );
5301         return rs->sr_err;
5302 }
5303
5304 static int
5305 config_back_modrdn( Operation *op, SlapReply *rs )
5306 {
5307         CfBackInfo *cfb;
5308         CfEntryInfo *ce, *last;
5309         struct berval rdn;
5310         int ixold, ixnew;
5311
5312         cfb = (CfBackInfo *)op->o_bd->be_private;
5313
5314         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5315         if ( !ce ) {
5316                 if ( last )
5317                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5318                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5319                 goto out;
5320         }
5321         if ( !access_allowed( op, ce->ce_entry, slap_schema.si_ad_entry,
5322                 NULL, ACL_WRITE, NULL )) {
5323                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5324                 goto out;
5325         }
5326         { Entry *parent;
5327                 if ( ce->ce_parent )
5328                         parent = ce->ce_parent->ce_entry;
5329                 else
5330                         parent = (Entry *)&slap_entry_root;
5331                 if ( !access_allowed( op, parent, slap_schema.si_ad_children,
5332                         NULL, ACL_WRITE, NULL )) {
5333                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5334                         goto out;
5335                 }
5336         }
5337
5338         /* We don't allow moving objects to new parents.
5339          * Generally we only allow reordering a set of ordered entries.
5340          */
5341         if ( op->orr_newSup ) {
5342                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5343                 goto out;
5344         }
5345
5346         /* If newRDN == oldRDN, quietly succeed */
5347         dnRdn( &op->o_req_ndn, &rdn );
5348         if ( dn_match( &rdn, &op->orr_nnewrdn )) {
5349                 rs->sr_err = LDAP_SUCCESS;
5350                 goto out;
5351         }
5352
5353         /* Current behavior, subject to change as needed:
5354          *
5355          * For backends and overlays, we only allow renumbering.
5356          * For schema, we allow renaming with the same number.
5357          * Otherwise, the op is not allowed.
5358          */
5359
5360         if ( ce->ce_type == Cft_Schema ) {
5361                 char *ptr1, *ptr2;
5362                 int len;
5363
5364                 /* Can't alter the main cn=schema entry */
5365                 if ( ce->ce_parent->ce_type == Cft_Global ) {
5366                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5367                         rs->sr_text = "renaming not allowed for this entry";
5368                         goto out;
5369                 }
5370
5371                 /* We could support this later if desired */
5372                 ptr1 = ber_bvchr( &rdn, '}' );
5373                 ptr2 = ber_bvchr( &op->orr_newrdn, '}' );
5374                 len = ptr1 - rdn.bv_val;
5375                 if ( len != ptr2 - op->orr_newrdn.bv_val ||
5376                         strncmp( rdn.bv_val, op->orr_newrdn.bv_val, len )) {
5377                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5378                         rs->sr_text = "schema reordering not supported";
5379                         goto out;
5380                 }
5381         } else if ( ce->ce_type == Cft_Database ||
5382                 ce->ce_type == Cft_Overlay ) {
5383                 char *ptr1, *ptr2, *iptr1, *iptr2;
5384                 int len1, len2;
5385
5386                 iptr2 = ber_bvchr( &op->orr_newrdn, '=' ) + 1;
5387                 if ( *iptr2 != '{' ) {
5388                         rs->sr_err = LDAP_NAMING_VIOLATION;
5389                         rs->sr_text = "new ordering index is required";
5390                         goto out;
5391                 }
5392                 iptr2++;
5393                 iptr1 = ber_bvchr( &rdn, '{' ) + 1;
5394                 ptr1 = ber_bvchr( &rdn, '}' );
5395                 ptr2 = ber_bvchr( &op->orr_newrdn, '}' );
5396                 if ( !ptr2 ) {
5397                         rs->sr_err = LDAP_NAMING_VIOLATION;
5398                         rs->sr_text = "new ordering index is required";
5399                         goto out;
5400                 }
5401
5402                 len1 = ptr1 - rdn.bv_val;
5403                 len2 = ptr2 - op->orr_newrdn.bv_val;
5404
5405                 if ( rdn.bv_len - len1 != op->orr_newrdn.bv_len - len2 ||
5406                         strncmp( ptr1, ptr2, rdn.bv_len - len1 )) {
5407                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5408                         rs->sr_text = "changing database/overlay type not allowed";
5409                         goto out;
5410                 }
5411                 ixold = strtol( iptr1, NULL, 0 );
5412                 ixnew = strtol( iptr2, &ptr1, 0 );
5413                 if ( ptr1 != ptr2 || ixold < 0 || ixnew < 0 ) {
5414                         rs->sr_err = LDAP_NAMING_VIOLATION;
5415                         goto out;
5416                 }
5417                 /* config DB is always 0, cannot be changed */
5418                 if ( ce->ce_type == Cft_Database && ( ixold == 0 || ixnew == 0 )) {
5419                         rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
5420                         goto out;
5421                 }
5422         } else {
5423                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5424                 rs->sr_text = "renaming not supported for this entry";
5425                 goto out;
5426         }
5427
5428         if ( op->o_abandon ) {
5429                 rs->sr_err = SLAPD_ABANDON;
5430                 goto out;
5431         }
5432         ldap_pvt_thread_pool_pause( &connection_pool );
5433
5434         if ( ce->ce_type == Cft_Schema ) {
5435                 req_modrdn_s modr = op->oq_modrdn;
5436                 struct berval rdn;
5437                 Attribute *a;
5438                 rs->sr_err = config_rename_attr( rs, ce->ce_entry, &rdn, &a );
5439                 if ( rs->sr_err == LDAP_SUCCESS ) {
5440                         rs->sr_err = config_rename_one( op, rs, ce->ce_entry,
5441                                 ce->ce_parent, a, &op->orr_newrdn, &op->orr_nnewrdn,
5442                                 cfb->cb_use_ldif );
5443                 }
5444                 op->oq_modrdn = modr;
5445         } else {
5446                 CfEntryInfo *ce2, *cebase, **cprev, **cbprev, *ceold;
5447                 req_modrdn_s modr = op->oq_modrdn;
5448                 int i;
5449
5450                 /* Advance to first of this type */
5451                 cprev = &ce->ce_parent->ce_kids;
5452                 for ( ce2 = *cprev; ce2 && ce2->ce_type != ce->ce_type; ) {
5453                         cprev = &ce2->ce_sibs;
5454                         ce2 = ce2->ce_sibs;
5455                 }
5456                 /* Skip the -1 entry */
5457                 if ( ce->ce_type == Cft_Database ) {
5458                         cprev = &ce2->ce_sibs;
5459                         ce2 = ce2->ce_sibs;
5460                 }
5461                 cebase = ce2;
5462                 cbprev = cprev;
5463
5464                 /* Remove from old slot */
5465                 for ( ce2 = *cprev; ce2 && ce2 != ce; ce2 = ce2->ce_sibs )
5466                         cprev = &ce2->ce_sibs;
5467                 *cprev = ce->ce_sibs;
5468                 ceold = ce->ce_sibs;
5469
5470                 /* Insert into new slot */
5471                 cprev = cbprev;
5472                 for ( i=0; i<ixnew; i++ ) {
5473                         ce2 = *cprev;
5474                         if ( !ce2 )
5475                                 break;
5476                         cprev = &ce2->ce_sibs;
5477                 }
5478                 ce->ce_sibs = *cprev;
5479                 *cprev = ce;
5480
5481                 ixnew = i;
5482
5483                 /* NOTE: These should be encoded in the OC tables, not inline here */
5484                 if ( ce->ce_type == Cft_Database )
5485                         backend_db_move( ce->ce_be, ixnew );
5486                 else if ( ce->ce_type == Cft_Overlay )
5487                         overlay_move( ce->ce_be, (slap_overinst *)ce->ce_bi, ixnew );
5488                         
5489                 if ( ixold < ixnew ) {
5490                         rs->sr_err = config_rename_del( op, rs, ce, ceold, ixold,
5491                                 cfb->cb_use_ldif );
5492                 } else {
5493                         rs->sr_err = config_rename_add( op, rs, ce, ixnew, 1,
5494                                 ixold - ixnew, cfb->cb_use_ldif );
5495                 }
5496                 op->oq_modrdn = modr;
5497         }
5498
5499         ldap_pvt_thread_pool_resume( &connection_pool );
5500 out:
5501         send_ldap_result( op, rs );
5502         return rs->sr_err;
5503 }
5504
5505 static int
5506 config_back_delete( Operation *op, SlapReply *rs )
5507 {
5508 #ifdef SLAP_CONFIG_DELETE
5509         CfBackInfo *cfb;
5510         CfEntryInfo *ce, *last, *ce2;
5511
5512         cfb = (CfBackInfo *)op->o_bd->be_private;
5513
5514         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5515         if ( !ce ) {
5516                 if ( last )
5517                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5518                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5519         } else if ( ce->ce_kids ) {
5520                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5521         } else if ( op->o_abandon ) {
5522                 rs->sr_err = SLAPD_ABANDON;
5523         } else if ( ce->ce_type == Cft_Overlay || ce->ce_type == Cft_Database ){
5524                 char *iptr;
5525                 int count, ixold;
5526
5527                 ldap_pvt_thread_pool_pause( &connection_pool );
5528
5529                 if ( ce->ce_type == Cft_Overlay ){
5530                         overlay_remove( ce->ce_be, (slap_overinst *)ce->ce_bi );
5531                 } else { /* Cft_Database*/
5532                         if ( ce->ce_be == frontendDB || ce->ce_be == op->o_bd ){
5533                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5534                                 rs->sr_text = "Cannot delete config or frontend database";
5535                                 ldap_pvt_thread_pool_resume( &connection_pool );
5536                                 goto out;
5537                         } 
5538                         if ( ce->ce_be->bd_info->bi_db_close ) {
5539                                 ce->ce_be->bd_info->bi_db_close( ce->ce_be, NULL );
5540                         }
5541                         backend_destroy_one( ce->ce_be, 1);
5542                 }
5543
5544                 /* remove CfEntryInfo from the siblings list */
5545                 if ( ce->ce_parent->ce_kids == ce ) {
5546                         ce->ce_parent->ce_kids = ce->ce_sibs;
5547                 } else {
5548                         for ( ce2 = ce->ce_parent->ce_kids ; ce2; ce2 = ce2->ce_sibs ) {
5549                                 if ( ce2->ce_sibs == ce ) {
5550                                         ce2->ce_sibs = ce->ce_sibs;
5551                                         break;
5552                                 }
5553                         }
5554                 }
5555
5556                 /* remove from underlying database */
5557                 if ( cfb->cb_use_ldif ) {
5558                         BackendDB *be = op->o_bd;
5559                         slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
5560                         struct berval dn, ndn, req_dn, req_ndn;
5561
5562                         op->o_bd = &cfb->cb_db;
5563
5564                         dn = op->o_dn;
5565                         ndn = op->o_ndn;
5566                         req_dn = op->o_req_dn;
5567                         req_ndn = op->o_req_ndn;
5568
5569                         op->o_dn = op->o_bd->be_rootdn;
5570                         op->o_ndn = op->o_bd->be_rootndn;
5571                         op->o_req_dn = ce->ce_entry->e_name;
5572                         op->o_req_ndn = ce->ce_entry->e_nname;
5573
5574                         scp = op->o_callback;
5575                         op->o_callback = &sc;
5576                         op->o_bd->be_delete( op, rs );
5577                         op->o_bd = be;
5578                         op->o_callback = scp;
5579                         op->o_dn = dn;
5580                         op->o_ndn = ndn;
5581                         op->o_req_dn = req_dn;
5582                         op->o_req_ndn = req_ndn;
5583                 }
5584
5585                 /* renumber siblings */
5586                 iptr = ber_bvchr( &op->o_req_ndn, '{' ) + 1;
5587                 ixold = strtol( iptr, NULL, 0 );
5588                 for (ce2 = ce->ce_sibs, count=0; ce2; ce2=ce2->ce_sibs) {
5589                         config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
5590                                 count+ixold, 0, cfb->cb_use_ldif );
5591                         count++;
5592                 }
5593
5594                 ce->ce_entry->e_private=NULL;
5595                 entry_free(ce->ce_entry);
5596                 ch_free(ce);
5597                 ldap_pvt_thread_pool_resume( &connection_pool );
5598         } else {
5599                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5600         }
5601 #else
5602         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5603 #endif /* SLAP_CONFIG_DELETE */
5604 out:
5605         send_ldap_result( op, rs );
5606         return rs->sr_err;
5607 }
5608
5609 static int
5610 config_back_search( Operation *op, SlapReply *rs )
5611 {
5612         CfBackInfo *cfb;
5613         CfEntryInfo *ce, *last;
5614         slap_mask_t mask;
5615
5616         cfb = (CfBackInfo *)op->o_bd->be_private;
5617
5618         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5619         if ( !ce ) {
5620                 if ( last )
5621                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5622                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5623                 goto out;
5624         }
5625         if ( !access_allowed_mask( op, ce->ce_entry, slap_schema.si_ad_entry, NULL,
5626                 ACL_SEARCH, NULL, &mask ))
5627         {
5628                 if ( !ACL_GRANT( mask, ACL_DISCLOSE )) {
5629                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
5630                 } else {
5631                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5632                 }
5633                 goto out;
5634         }
5635         switch ( op->ors_scope ) {
5636         case LDAP_SCOPE_BASE:
5637         case LDAP_SCOPE_SUBTREE:
5638                 rs->sr_err = config_send( op, rs, ce, 0 );
5639                 break;
5640                 
5641         case LDAP_SCOPE_ONELEVEL:
5642                 for (ce = ce->ce_kids; ce; ce=ce->ce_sibs) {
5643                         rs->sr_err = config_send( op, rs, ce, 1 );
5644                         if ( rs->sr_err ) {
5645                                 break;
5646                         }
5647                 }
5648                 break;
5649         }
5650
5651 out:
5652         send_ldap_result( op, rs );
5653         return rs->sr_err;
5654 }
5655
5656 /* no-op, we never free entries */
5657 int config_entry_release(
5658         Operation *op,
5659         Entry *e,
5660         int rw )
5661 {
5662         if ( !e->e_private ) {
5663                 entry_free( e );
5664         }
5665         return LDAP_SUCCESS;
5666 }
5667
5668 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
5669  */
5670 int config_back_entry_get(
5671         Operation *op,
5672         struct berval *ndn,
5673         ObjectClass *oc,
5674         AttributeDescription *at,
5675         int rw,
5676         Entry **ent )
5677 {
5678         CfBackInfo *cfb;
5679         CfEntryInfo *ce, *last;
5680         int rc = LDAP_NO_SUCH_OBJECT;
5681
5682         cfb = (CfBackInfo *)op->o_bd->be_private;
5683
5684         ce = config_find_base( cfb->cb_root, ndn, &last );
5685         if ( ce ) {
5686                 *ent = ce->ce_entry;
5687                 if ( *ent ) {
5688                         rc = LDAP_SUCCESS;
5689                         if ( oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
5690                                 rc = LDAP_NO_SUCH_ATTRIBUTE;
5691                                 *ent = NULL;
5692                         }
5693                 }
5694         }
5695
5696         return rc;
5697 }
5698
5699 static int
5700 config_build_attrs( Entry *e, AttributeType **at, AttributeDescription *ad,
5701         ConfigTable *ct, ConfigArgs *c )
5702 {
5703         int i, rc;
5704
5705         for (; at && *at; at++) {
5706                 /* Skip the naming attr */
5707                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
5708                         continue;
5709                 for (i=0;ct[i].name;i++) {
5710                         if (ct[i].ad == (*at)->sat_ad) {
5711                                 rc = config_get_vals(&ct[i], c);
5712                                 /* NOTE: tolerate that config_get_vals()
5713                                  * returns success with no values */
5714                                 if (rc == LDAP_SUCCESS && c->rvalue_vals != NULL ) {
5715                                         if ( c->rvalue_nvals )
5716                                                 rc = attr_merge(e, ct[i].ad, c->rvalue_vals,
5717                                                         c->rvalue_nvals);
5718                                         else {
5719                                                 slap_syntax_validate_func *validate =
5720                                                         ct[i].ad->ad_type->sat_syntax->ssyn_validate;
5721                                                 if ( validate ) {
5722                                                         int j;
5723                                                         for ( j=0; c->rvalue_vals[j].bv_val; j++ ) {
5724                                                                 rc = ordered_value_validate( ct[i].ad,
5725                                                                         &c->rvalue_vals[j], LDAP_MOD_ADD );
5726                                                                 if ( rc ) {
5727                                                                         Debug( LDAP_DEBUG_ANY,
5728                                                                                 "config_build_attrs: error %d on %s value #%d\n",
5729                                                                                 rc, ct[i].ad->ad_cname.bv_val, j );
5730                                                                         return rc;
5731                                                                 }
5732                                                         }
5733                                                 }
5734                                                         
5735                                                 rc = attr_merge_normalize(e, ct[i].ad,
5736                                                         c->rvalue_vals, NULL);
5737                                         }
5738                                         ber_bvarray_free( c->rvalue_nvals );
5739                                         ber_bvarray_free( c->rvalue_vals );
5740                                         if ( rc ) {
5741                                                 Debug( LDAP_DEBUG_ANY,
5742                                                         "config_build_attrs: error %d on %s\n",
5743                                                         rc, ct[i].ad->ad_cname.bv_val, 0 );
5744                                                 return rc;
5745                                         }
5746                                 }
5747                                 break;
5748                         }
5749                 }
5750         }
5751         return 0;
5752 }
5753
5754 Entry *
5755 config_build_entry( Operation *op, SlapReply *rs, CfEntryInfo *parent,
5756         ConfigArgs *c, struct berval *rdn, ConfigOCs *main, ConfigOCs *extra )
5757 {
5758         Entry *e = entry_alloc();
5759         CfEntryInfo *ce = ch_calloc( 1, sizeof(CfEntryInfo) );
5760         struct berval val;
5761         struct berval ad_name;
5762         AttributeDescription *ad = NULL;
5763         int rc;
5764         char *ptr;
5765         const char *text = "";
5766         Attribute *oc_at;
5767         struct berval pdn;
5768         ObjectClass *oc;
5769         CfEntryInfo *ceprev = NULL;
5770
5771         Debug( LDAP_DEBUG_TRACE, "config_build_entry: \"%s\"\n", rdn->bv_val, 0, 0);
5772         e->e_private = ce;
5773         ce->ce_entry = e;
5774         ce->ce_type = main->co_type;
5775         ce->ce_parent = parent;
5776         if ( parent ) {
5777                 pdn = parent->ce_entry->e_nname;
5778                 if ( parent->ce_kids && parent->ce_kids->ce_type <= ce->ce_type )
5779                         for ( ceprev = parent->ce_kids; ceprev->ce_sibs &&
5780                                 ceprev->ce_type <= ce->ce_type;
5781                                 ceprev = ceprev->ce_sibs );
5782         } else {
5783                 BER_BVZERO( &pdn );
5784         }
5785
5786         ce->ce_private = c->ca_private;
5787         ce->ce_be = c->be;
5788         ce->ce_bi = c->bi;
5789
5790         build_new_dn( &e->e_name, &pdn, rdn, NULL );
5791         ber_dupbv( &e->e_nname, &e->e_name );
5792
5793         attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
5794                 main->co_name, NULL );
5795         if ( extra )
5796                 attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
5797                         extra->co_name, NULL );
5798         ptr = strchr(rdn->bv_val, '=');
5799         ad_name.bv_val = rdn->bv_val;
5800         ad_name.bv_len = ptr - rdn->bv_val;
5801         rc = slap_bv2ad( &ad_name, &ad, &text );
5802         if ( rc ) {
5803                 goto fail;
5804         }
5805         val.bv_val = ptr+1;
5806         val.bv_len = rdn->bv_len - (val.bv_val - rdn->bv_val);
5807         attr_merge_normalize_one(e, ad, &val, NULL );
5808
5809         oc = main->co_oc;
5810         c->table = main->co_type;
5811         if ( oc->soc_required ) {
5812                 rc = config_build_attrs( e, oc->soc_required, ad, main->co_table, c );
5813                 if ( rc ) goto fail;
5814         }
5815
5816         if ( oc->soc_allowed ) {
5817                 rc = config_build_attrs( e, oc->soc_allowed, ad, main->co_table, c );
5818                 if ( rc ) goto fail;
5819         }
5820
5821         if ( extra ) {
5822                 oc = extra->co_oc;
5823                 c->table = extra->co_type;
5824                 if ( oc->soc_required ) {
5825                         rc = config_build_attrs( e, oc->soc_required, ad, extra->co_table, c );
5826                         if ( rc ) goto fail;
5827                 }
5828
5829                 if ( oc->soc_allowed ) {
5830                         rc = config_build_attrs( e, oc->soc_allowed, ad, extra->co_table, c );
5831                         if ( rc ) goto fail;
5832                 }
5833         }
5834
5835         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
5836         rc = structural_class(oc_at->a_vals, &oc, NULL, &text, c->cr_msg,
5837                 sizeof(c->cr_msg), op ? op->o_tmpmemctx : NULL );
5838         if ( rc != LDAP_SUCCESS ) {
5839 fail:
5840                 Debug( LDAP_DEBUG_ANY,
5841                         "config_build_entry: build \"%s\" failed: \"%s\"\n",
5842                         rdn->bv_val, text, 0);
5843                 return NULL;
5844         }
5845         attr_merge_normalize_one(e, slap_schema.si_ad_structuralObjectClass, &oc->soc_cname, NULL );
5846         if ( op ) {
5847                 op->ora_e = e;
5848                 op->ora_modlist = NULL;
5849                 slap_add_opattrs( op, NULL, NULL, 0, 0 );
5850                 if ( !op->o_noop ) {
5851                         op->o_bd->be_add( op, rs );
5852                         if ( ( rs->sr_err != LDAP_SUCCESS ) 
5853                                         && (rs->sr_err != LDAP_ALREADY_EXISTS) ) {
5854                                 goto fail;
5855                         }
5856                 }
5857         }
5858         if ( ceprev ) {
5859                 ce->ce_sibs = ceprev->ce_sibs;
5860                 ceprev->ce_sibs = ce;
5861         } else if ( parent ) {
5862                 ce->ce_sibs = parent->ce_kids;
5863                 parent->ce_kids = ce;
5864         }
5865
5866         return e;
5867 }
5868
5869 static int
5870 config_build_schema_inc( ConfigArgs *c, CfEntryInfo *ceparent,
5871         Operation *op, SlapReply *rs )
5872 {
5873         Entry *e;
5874         ConfigFile *cf = c->ca_private;
5875         char *ptr;
5876         struct berval bv, rdn;
5877
5878         for (; cf; cf=cf->c_sibs, c->depth++) {
5879                 if ( !cf->c_at_head && !cf->c_cr_head && !cf->c_oc_head &&
5880                         !cf->c_om_head && !cf->c_syn_head ) continue;
5881                 c->value_dn.bv_val = c->log;
5882                 LUTIL_SLASHPATH( cf->c_file.bv_val );
5883                 bv.bv_val = strrchr(cf->c_file.bv_val, LDAP_DIRSEP[0]);
5884                 if ( !bv.bv_val ) {
5885                         bv = cf->c_file;
5886                 } else {
5887                         bv.bv_val++;
5888                         bv.bv_len = cf->c_file.bv_len - (bv.bv_val - cf->c_file.bv_val);
5889                 }
5890                 ptr = strchr( bv.bv_val, '.' );
5891                 if ( ptr )
5892                         bv.bv_len = ptr - bv.bv_val;
5893                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=" SLAP_X_ORDERED_FMT, c->depth);
5894                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
5895                         /* FIXME: how can indicate error? */
5896                         return -1;
5897                 }
5898                 strncpy( c->value_dn.bv_val + c->value_dn.bv_len, bv.bv_val,
5899                         bv.bv_len );
5900                 c->value_dn.bv_len += bv.bv_len;
5901                 c->value_dn.bv_val[c->value_dn.bv_len] ='\0';
5902                 rdn = c->value_dn;
5903
5904                 c->ca_private = cf;
5905                 e = config_build_entry( op, rs, ceparent, c, &rdn,
5906                         &CFOC_SCHEMA, NULL );
5907                 if ( !e ) {
5908                         return -1;
5909                 } else if ( e && cf->c_kids ) {
5910                         c->ca_private = cf->c_kids;
5911                         config_build_schema_inc( c, e->e_private, op, rs );
5912                 }
5913         }
5914         return 0;
5915 }
5916
5917 #ifdef SLAPD_MODULES
5918
5919 static int
5920 config_build_modules( ConfigArgs *c, CfEntryInfo *ceparent,
5921         Operation *op, SlapReply *rs )
5922 {
5923         int i;
5924         ModPaths *mp;
5925
5926         for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
5927                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
5928                         continue;
5929                 c->value_dn.bv_val = c->log;
5930                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=module" SLAP_X_ORDERED_FMT, i);
5931                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
5932                         /* FIXME: how can indicate error? */
5933                         return -1;
5934                 }
5935                 c->ca_private = mp;
5936                 if ( ! config_build_entry( op, rs, ceparent, c, &c->value_dn, &CFOC_MODULE, NULL )) {
5937                         return -1;
5938                 }
5939         }
5940         return 0;
5941 }
5942 #endif
5943
5944 static int
5945 config_check_schema(Operation *op, CfBackInfo *cfb)
5946 {
5947         struct berval schema_dn = BER_BVC(SCHEMA_RDN "," CONFIG_RDN);
5948         ConfigArgs c = {0};
5949         CfEntryInfo *ce, *last;
5950         Entry *e;
5951
5952         /* If there's no root entry, we must be in the midst of converting */
5953         if ( !cfb->cb_root )
5954                 return 0;
5955
5956         /* Make sure the main schema entry exists */
5957         ce = config_find_base( cfb->cb_root, &schema_dn, &last );
5958         if ( ce ) {
5959                 Attribute *a;
5960                 struct berval *bv;
5961
5962                 e = ce->ce_entry;
5963
5964                 /* Make sure it's up to date */
5965                 if ( cf_om_tail != om_sys_tail ) {
5966                         a = attr_find( e->e_attrs, cfAd_om );
5967                         if ( a ) {
5968                                 if ( a->a_nvals != a->a_vals )
5969                                         ber_bvarray_free( a->a_nvals );
5970                                 ber_bvarray_free( a->a_vals );
5971                                 a->a_vals = NULL;
5972                                 a->a_nvals = NULL;
5973                                 a->a_numvals = 0;
5974                         }
5975                         oidm_unparse( &bv, NULL, NULL, 1 );
5976                         attr_merge_normalize( e, cfAd_om, bv, NULL );
5977                         ber_bvarray_free( bv );
5978                         cf_om_tail = om_sys_tail;
5979                 }
5980                 if ( cf_at_tail != at_sys_tail ) {
5981                         a = attr_find( e->e_attrs, cfAd_attr );
5982                         if ( a ) {
5983                                 if ( a->a_nvals != a->a_vals )
5984                                         ber_bvarray_free( a->a_nvals );
5985                                 ber_bvarray_free( a->a_vals );
5986                                 a->a_vals = NULL;
5987                                 a->a_nvals = NULL;
5988                                 a->a_numvals = 0;
5989                         }
5990                         at_unparse( &bv, NULL, NULL, 1 );
5991                         attr_merge_normalize( e, cfAd_attr, bv, NULL );
5992                         ber_bvarray_free( bv );
5993                         cf_at_tail = at_sys_tail;
5994                 }
5995                 if ( cf_oc_tail != oc_sys_tail ) {
5996                         a = attr_find( e->e_attrs, cfAd_oc );
5997                         if ( a ) {
5998                                 if ( a->a_nvals != a->a_vals )
5999                                         ber_bvarray_free( a->a_nvals );
6000                                 ber_bvarray_free( a->a_vals );
6001                                 a->a_vals = NULL;
6002                                 a->a_nvals = NULL;
6003                                 a->a_numvals = 0;
6004                         }
6005                         oc_unparse( &bv, NULL, NULL, 1 );
6006                         attr_merge_normalize( e, cfAd_oc, bv, NULL );
6007                         ber_bvarray_free( bv );
6008                         cf_oc_tail = oc_sys_tail;
6009                 }
6010                 if ( cf_syn_tail != syn_sys_tail ) {
6011                         a = attr_find( e->e_attrs, cfAd_syntax );
6012                         if ( a ) {
6013                                 if ( a->a_nvals != a->a_vals )
6014                                         ber_bvarray_free( a->a_nvals );
6015                                 ber_bvarray_free( a->a_vals );
6016                                 a->a_vals = NULL;
6017                                 a->a_nvals = NULL;
6018                                 a->a_numvals = 0;
6019                         }
6020                         syn_unparse( &bv, NULL, NULL, 1 );
6021                         attr_merge_normalize( e, cfAd_syntax, bv, NULL );
6022                         ber_bvarray_free( bv );
6023                         cf_syn_tail = syn_sys_tail;
6024                 }
6025         } else {
6026                 SlapReply rs = {REP_RESULT};
6027                 c.ca_private = NULL;
6028                 e = config_build_entry( op, &rs, cfb->cb_root, &c, &schema_rdn,
6029                         &CFOC_SCHEMA, NULL );
6030                 if ( !e ) {
6031                         return -1;
6032                 }
6033                 ce = e->e_private;
6034                 ce->ce_private = cfb->cb_config;
6035                 cf_at_tail = at_sys_tail;
6036                 cf_oc_tail = oc_sys_tail;
6037                 cf_om_tail = om_sys_tail;
6038                 cf_syn_tail = syn_sys_tail;
6039         }
6040         return 0;
6041 }
6042
6043 static const char *defacl[] = {
6044         NULL, "to", "*", "by", "*", "none", NULL
6045 };
6046
6047 static int
6048 config_back_db_open( BackendDB *be, ConfigReply *cr )
6049 {
6050         CfBackInfo *cfb = be->be_private;
6051         struct berval rdn;
6052         Entry *e, *parent;
6053         CfEntryInfo *ce, *ceparent;
6054         int i, unsupp = 0;
6055         BackendInfo *bi;
6056         ConfigArgs c;
6057         Connection conn = {0};
6058         OperationBuffer opbuf;
6059         Operation *op;
6060         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
6061         SlapReply rs = {REP_RESULT};
6062         void *thrctx = NULL;
6063
6064         Debug( LDAP_DEBUG_TRACE, "config_back_db_open\n", 0, 0, 0);
6065
6066         /* If we have no explicitly configured ACLs, don't just use
6067          * the global ACLs. Explicitly deny access to everything.
6068          */
6069         if ( !be->be_acl ) {
6070                 parse_acl(be, "config_back_db_open", 0, 6, (char **)defacl, 0 );
6071         }
6072
6073         thrctx = ldap_pvt_thread_pool_context();
6074         connection_fake_init( &conn, &opbuf, thrctx );
6075         op = &opbuf.ob_op;
6076
6077         op->o_tag = LDAP_REQ_ADD;
6078         op->o_callback = &cb;
6079         op->o_bd = &cfb->cb_db;
6080         op->o_dn = op->o_bd->be_rootdn;
6081         op->o_ndn = op->o_bd->be_rootndn;
6082
6083         if ( !cfb->cb_use_ldif ) {
6084                 op->o_noop = 1;
6085         }
6086
6087         /* If we read the config from back-ldif, do some quick sanity checks */
6088         if ( cfb->cb_got_ldif ) {
6089                 return config_check_schema( op, cfb );
6090         }
6091
6092         /* create root of tree */
6093         rdn = config_rdn;
6094         c.ca_private = cfb->cb_config;
6095         c.be = frontendDB;
6096         e = config_build_entry( op, &rs, NULL, &c, &rdn, &CFOC_GLOBAL, NULL );
6097         if ( !e ) {
6098                 return -1;
6099         }
6100         ce = e->e_private;
6101         cfb->cb_root = ce;
6102
6103         parent = e;
6104         ceparent = ce;
6105
6106 #ifdef SLAPD_MODULES
6107         /* Create Module nodes... */
6108         if ( modpaths.mp_loads ) {
6109                 if ( config_build_modules( &c, ceparent, op, &rs ) ){
6110                         return -1;
6111                 }
6112         }
6113 #endif
6114
6115         /* Create schema nodes... cn=schema will contain the hardcoded core
6116          * schema, read-only. Child objects will contain runtime loaded schema
6117          * files.
6118          */
6119         rdn = schema_rdn;
6120         c.ca_private = NULL;
6121         e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_SCHEMA, NULL );
6122         if ( !e ) {
6123                 return -1;
6124         }
6125         ce = e->e_private;
6126         ce->ce_private = cfb->cb_config;
6127         cf_at_tail = at_sys_tail;
6128         cf_oc_tail = oc_sys_tail;
6129         cf_om_tail = om_sys_tail;
6130         cf_syn_tail = syn_sys_tail;
6131
6132         /* Create schema nodes for included schema... */
6133         if ( cfb->cb_config->c_kids ) {
6134                 c.depth = 0;
6135                 c.ca_private = cfb->cb_config->c_kids;
6136                 if (config_build_schema_inc( &c, ce, op, &rs )) {
6137                         return -1;
6138                 }
6139         }
6140
6141         /* Create backend nodes. Skip if they don't provide a cf_table.
6142          * There usually aren't any of these.
6143          */
6144         
6145         c.line = 0;
6146         LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next) {
6147                 if (!bi->bi_cf_ocs) {
6148                         /* If it only supports the old config mech, complain. */
6149                         if ( bi->bi_config ) {
6150                                 Debug( LDAP_DEBUG_ANY,
6151                                         "WARNING: No dynamic config support for backend %s.\n",
6152                                         bi->bi_type, 0, 0 );
6153                                 unsupp++;
6154                         }
6155                         continue;
6156                 }
6157                 if (!bi->bi_private) continue;
6158
6159                 rdn.bv_val = c.log;
6160                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
6161                         "%s=%s", cfAd_backend->ad_cname.bv_val, bi->bi_type);
6162                 if ( rdn.bv_len >= sizeof( c.log ) ) {
6163                         /* FIXME: holler ... */ ;
6164                 }
6165                 c.bi = bi;
6166                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_BACKEND,
6167                         bi->bi_cf_ocs );
6168                 if ( !e ) {
6169                         return -1;
6170                 }
6171         }
6172
6173         /* Create database nodes... */
6174         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
6175         LDAP_STAILQ_NEXT(frontendDB, be_next) = LDAP_STAILQ_FIRST(&backendDB);
6176         for ( i = -1, be = frontendDB ; be;
6177                 i++, be = LDAP_STAILQ_NEXT( be, be_next )) {
6178                 slap_overinfo *oi = NULL;
6179
6180                 if ( overlay_is_over( be )) {
6181                         oi = be->bd_info->bi_private;
6182                         bi = oi->oi_orig;
6183                 } else {
6184                         bi = be->bd_info;
6185                 }
6186
6187                 /* If this backend supports the old config mechanism, but not
6188                  * the new mech, complain.
6189                  */
6190                 if ( !be->be_cf_ocs && bi->bi_db_config ) {
6191                         Debug( LDAP_DEBUG_ANY,
6192                                 "WARNING: No dynamic config support for database %s.\n",
6193                                 bi->bi_type, 0, 0 );
6194                         unsupp++;
6195                 }
6196                 rdn.bv_val = c.log;
6197                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
6198                         "%s=" SLAP_X_ORDERED_FMT "%s", cfAd_database->ad_cname.bv_val,
6199                         i, bi->bi_type);
6200                 if ( rdn.bv_len >= sizeof( c.log ) ) {
6201                         /* FIXME: holler ... */ ;
6202                 }
6203                 c.be = be;
6204                 c.bi = bi;
6205                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_DATABASE,
6206                         be->be_cf_ocs );
6207                 if ( !e ) {
6208                         return -1;
6209                 }
6210                 ce = e->e_private;
6211                 if ( be->be_cf_ocs && be->be_cf_ocs->co_cfadd )
6212                         be->be_cf_ocs->co_cfadd( op, &rs, e, &c );
6213                 /* Iterate through overlays */
6214                 if ( oi ) {
6215                         slap_overinst *on;
6216                         Entry *oe;
6217                         int j;
6218                         voidList *vl, *v0 = NULL;
6219
6220                         /* overlays are in LIFO order, must reverse stack */
6221                         for (on=oi->oi_list; on; on=on->on_next) {
6222                                 vl = ch_malloc( sizeof( voidList ));
6223                                 vl->vl_next = v0;
6224                                 v0 = vl;
6225                                 vl->vl_ptr = on;
6226                         }
6227                         for (j=0; vl; j++,vl=v0) {
6228                                 on = vl->vl_ptr;
6229                                 v0 = vl->vl_next;
6230                                 ch_free( vl );
6231                                 if ( on->on_bi.bi_db_config && !on->on_bi.bi_cf_ocs ) {
6232                                         Debug( LDAP_DEBUG_ANY,
6233                                                 "WARNING: No dynamic config support for overlay %s.\n",
6234                                                 on->on_bi.bi_type, 0, 0 );
6235                                         unsupp++;
6236                                 }
6237                                 rdn.bv_val = c.log;
6238                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
6239                                         "%s=" SLAP_X_ORDERED_FMT "%s",
6240                                         cfAd_overlay->ad_cname.bv_val, j, on->on_bi.bi_type );
6241                                 if ( rdn.bv_len >= sizeof( c.log ) ) {
6242                                         /* FIXME: holler ... */ ;
6243                                 }
6244                                 c.be = be;
6245                                 c.bi = &on->on_bi;
6246                                 oe = config_build_entry( op, &rs, ce, &c, &rdn,
6247                                         &CFOC_OVERLAY, c.bi->bi_cf_ocs );
6248                                 if ( !oe ) {
6249                                         return -1;
6250                                 }
6251                                 if ( c.bi->bi_cf_ocs && c.bi->bi_cf_ocs->co_cfadd )
6252                                         c.bi->bi_cf_ocs->co_cfadd( op, &rs, oe, &c );
6253                         }
6254                 }
6255         }
6256         if ( thrctx )
6257                 ldap_pvt_thread_pool_context_reset( thrctx );
6258
6259         if ( unsupp  && cfb->cb_use_ldif ) {
6260                 Debug( LDAP_DEBUG_ANY, "\nWARNING: The converted cn=config "
6261                         "directory is incomplete and may not work.\n\n", 0, 0, 0 );
6262         }
6263
6264         return 0;
6265 }
6266
6267 static void
6268 cfb_free_cffile( ConfigFile *cf )
6269 {
6270         ConfigFile *next;
6271
6272         for (; cf; cf=next) {
6273                 next = cf->c_sibs;
6274                 if ( cf->c_kids )
6275                         cfb_free_cffile( cf->c_kids );
6276                 ch_free( cf->c_file.bv_val );
6277                 ber_bvarray_free( cf->c_dseFiles );
6278                 ch_free( cf );
6279         }
6280 }
6281
6282 static void
6283 cfb_free_entries( CfEntryInfo *ce )
6284 {
6285         CfEntryInfo *next;
6286
6287         for (; ce; ce=next) {
6288                 next = ce->ce_sibs;
6289                 if ( ce->ce_kids )
6290                         cfb_free_entries( ce->ce_kids );
6291                 ce->ce_entry->e_private = NULL;
6292                 entry_free( ce->ce_entry );
6293                 ch_free( ce );
6294         }
6295 }
6296
6297 static int
6298 config_back_db_close( BackendDB *be, ConfigReply *cr )
6299 {
6300         CfBackInfo *cfb = be->be_private;
6301
6302         cfb_free_entries( cfb->cb_root );
6303         cfb->cb_root = NULL;
6304
6305         if ( cfb->cb_db.bd_info ) {
6306                 backend_shutdown( &cfb->cb_db );
6307         }
6308
6309         return 0;
6310 }
6311
6312 static int
6313 config_back_db_destroy( BackendDB *be, ConfigReply *cr )
6314 {
6315         CfBackInfo *cfb = be->be_private;
6316
6317         cfb_free_cffile( cfb->cb_config );
6318
6319         ch_free( cfdir.bv_val );
6320
6321         avl_free( CfOcTree, NULL );
6322
6323         if ( cfb->cb_db.bd_info ) {
6324                 cfb->cb_db.be_suffix = NULL;
6325                 cfb->cb_db.be_nsuffix = NULL;
6326                 BER_BVZERO( &cfb->cb_db.be_rootdn );
6327                 BER_BVZERO( &cfb->cb_db.be_rootndn );
6328
6329                 backend_destroy_one( &cfb->cb_db, 0 );
6330         }
6331
6332         loglevel_destroy();
6333
6334         return 0;
6335 }
6336
6337 static int
6338 config_back_db_init( BackendDB *be, ConfigReply* cr )
6339 {
6340         struct berval dn;
6341         CfBackInfo *cfb;
6342
6343         cfb = &cfBackInfo;
6344         cfb->cb_config = ch_calloc( 1, sizeof(ConfigFile));
6345         cfn = cfb->cb_config;
6346         be->be_private = cfb;
6347
6348         ber_dupbv( &be->be_rootdn, &config_rdn );
6349         ber_dupbv( &be->be_rootndn, &be->be_rootdn );
6350         ber_dupbv( &dn, &be->be_rootdn );
6351         ber_bvarray_add( &be->be_suffix, &dn );
6352         ber_dupbv( &dn, &be->be_rootdn );
6353         ber_bvarray_add( &be->be_nsuffix, &dn );
6354
6355         /* Hide from namingContexts */
6356         SLAP_BFLAGS(be) |= SLAP_BFLAG_CONFIG;
6357
6358         /* Check ACLs on content of Adds by default */
6359         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_ACL_ADD;
6360
6361         return 0;
6362 }
6363
6364 static int
6365 config_back_destroy( BackendInfo *bi )
6366 {
6367         ldif_must_b64_encode_release();
6368         return 0;
6369 }
6370
6371 static int
6372 config_tool_entry_open( BackendDB *be, int mode )
6373 {
6374         CfBackInfo *cfb = be->be_private;
6375         BackendInfo *bi = cfb->cb_db.bd_info;
6376
6377         if ( bi && bi->bi_tool_entry_open )
6378                 return bi->bi_tool_entry_open( &cfb->cb_db, mode );
6379         else
6380                 return -1;
6381         
6382 }
6383
6384 static int
6385 config_tool_entry_close( BackendDB *be )
6386 {
6387         CfBackInfo *cfb = be->be_private;
6388         BackendInfo *bi = cfb->cb_db.bd_info;
6389
6390         if ( bi && bi->bi_tool_entry_close )
6391                 return bi->bi_tool_entry_close( &cfb->cb_db );
6392         else
6393                 return -1;
6394 }
6395
6396 static ID
6397 config_tool_entry_first( BackendDB *be )
6398 {
6399         CfBackInfo *cfb = be->be_private;
6400         BackendInfo *bi = cfb->cb_db.bd_info;
6401
6402         if ( bi && bi->bi_tool_entry_first )
6403                 return bi->bi_tool_entry_first( &cfb->cb_db );
6404         else
6405                 return NOID;
6406 }
6407
6408 static ID
6409 config_tool_entry_next( BackendDB *be )
6410 {
6411         CfBackInfo *cfb = be->be_private;
6412         BackendInfo *bi = cfb->cb_db.bd_info;
6413
6414         if ( bi && bi->bi_tool_entry_next )
6415                 return bi->bi_tool_entry_next( &cfb->cb_db );
6416         else
6417                 return NOID;
6418 }
6419
6420 static Entry *
6421 config_tool_entry_get( BackendDB *be, ID id )
6422 {
6423         CfBackInfo *cfb = be->be_private;
6424         BackendInfo *bi = cfb->cb_db.bd_info;
6425
6426         if ( bi && bi->bi_tool_entry_get )
6427                 return bi->bi_tool_entry_get( &cfb->cb_db, id );
6428         else
6429                 return NULL;
6430 }
6431
6432 static int entry_put_got_frontend=0;
6433 static int entry_put_got_config=0;
6434 static ID
6435 config_tool_entry_put( BackendDB *be, Entry *e, struct berval *text )
6436 {
6437         CfBackInfo *cfb = be->be_private;
6438         BackendInfo *bi = cfb->cb_db.bd_info;
6439         int rc;
6440         struct berval rdn, vals[ 2 ];
6441         ConfigArgs ca;
6442         OperationBuffer opbuf;
6443         Entry *ce;
6444         Connection conn = {0};
6445         Operation *op = NULL;
6446         void *thrctx;
6447         int isFrontend = 0;
6448
6449         /* Create entry for frontend database if it does not exist already */
6450         if ( !entry_put_got_frontend ) {
6451                 if ( !strncmp( e->e_nname.bv_val, "olcDatabase", 
6452                                 STRLENOF( "olcDatabase" ))) {
6453                         if ( strncmp( e->e_nname.bv_val + 
6454                                         STRLENOF( "olcDatabase" ), "={-1}frontend",
6455                                         STRLENOF( "={-1}frontend" )) && 
6456                                         strncmp( e->e_nname.bv_val + 
6457                                         STRLENOF( "olcDatabase" ), "=frontend",
6458                                         STRLENOF( "=frontend" ))) {
6459                                 vals[1].bv_len = 0;
6460                                 vals[1].bv_val = NULL;
6461                                 memset( &ca, 0, sizeof(ConfigArgs));
6462                                 ca.be = frontendDB;
6463                                 ca.bi = frontendDB->bd_info;
6464                                 ca.be->be_cf_ocs = &CFOC_FRONTEND;
6465                                 rdn.bv_val = ca.log;
6466                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( ca.log ),
6467                                         "%s=" SLAP_X_ORDERED_FMT "%s",
6468                                         cfAd_database->ad_cname.bv_val, -1,
6469                                         ca.bi->bi_type);
6470                                 ce = config_build_entry( NULL, NULL, cfb->cb_root, &ca, &rdn,
6471                                                 &CFOC_DATABASE, ca.be->be_cf_ocs );
6472                                 thrctx = ldap_pvt_thread_pool_context();
6473                                 connection_fake_init2( &conn, &opbuf, thrctx,0 );
6474                                 op = &opbuf.ob_op;
6475                                 op->o_bd = &cfb->cb_db;
6476                                 op->o_tag = LDAP_REQ_ADD;
6477                                 op->ora_e = ce;
6478                                 op->o_dn = be->be_rootdn;
6479                                 op->o_ndn = be->be_rootndn;
6480                                 rc = slap_add_opattrs(op, NULL, NULL, 0, 0);
6481                                 if ( rc != LDAP_SUCCESS ) {
6482                                         text->bv_val = "autocreation of \"olcDatabase={-1}frontend\" failed";
6483                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={-1}frontend\" failed");
6484                                         return NOID;
6485                                 }
6486
6487                                 if ( ce && bi && bi->bi_tool_entry_put && 
6488                                                 bi->bi_tool_entry_put( &cfb->cb_db, ce, text ) != NOID ) {
6489                                         entry_put_got_frontend++;
6490                                 } else {
6491                                         text->bv_val = "autocreation of \"olcDatabase={-1}frontend\" failed";
6492                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={-1}frontend\" failed");
6493                                         return NOID;
6494                                 }
6495                         } else {
6496                                 entry_put_got_frontend++;
6497                                 isFrontend = 1;
6498                         }
6499                 }
6500         }
6501         /* Create entry for config database if it does not exist already */
6502         if ( !entry_put_got_config && !isFrontend ) {
6503                 if ( !strncmp( e->e_nname.bv_val, "olcDatabase",
6504                                 STRLENOF( "olcDatabase" ))) {
6505                         if ( strncmp( e->e_nname.bv_val +
6506                                         STRLENOF( "olcDatabase" ), "={0}config",
6507                                         STRLENOF( "={0}config" )) &&
6508                                         strncmp( e->e_nname.bv_val +
6509                                         STRLENOF( "olcDatabase" ), "=config",
6510                                         STRLENOF( "=config" )) ) {
6511                                 vals[1].bv_len = 0;
6512                                 vals[1].bv_val = NULL;
6513                                 memset( &ca, 0, sizeof(ConfigArgs));
6514                                 ca.be = LDAP_STAILQ_FIRST( &backendDB );
6515                                 ca.bi = ca.be->bd_info;
6516                                 rdn.bv_val = ca.log;
6517                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( ca.log ),
6518                                         "%s=" SLAP_X_ORDERED_FMT "%s",
6519                                         cfAd_database->ad_cname.bv_val, 0,
6520                                         ca.bi->bi_type);
6521                                 ce = config_build_entry( NULL, NULL, cfb->cb_root, &ca, &rdn, &CFOC_DATABASE,
6522                                                 ca.be->be_cf_ocs );
6523                                 if ( ! op ) {
6524                                         thrctx = ldap_pvt_thread_pool_context();
6525                                         connection_fake_init2( &conn, &opbuf, thrctx,0 );
6526                                         op = &opbuf.ob_op;
6527                                         op->o_bd = &cfb->cb_db;
6528                                         op->o_tag = LDAP_REQ_ADD;
6529                                         op->o_dn = be->be_rootdn;
6530                                         op->o_ndn = be->be_rootndn;
6531                                 }
6532                                 op->ora_e = ce;
6533                                 rc = slap_add_opattrs(op, NULL, NULL, 0, 0);
6534                                 if ( rc != LDAP_SUCCESS ) {
6535                                         text->bv_val = "autocreation of \"olcDatabase={0}config\" failed";
6536                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={0}config\" failed");
6537                                         return NOID;
6538                                 }
6539                                 if (ce && bi && bi->bi_tool_entry_put &&
6540                                                 bi->bi_tool_entry_put( &cfb->cb_db, ce, text ) != NOID ) {
6541                                         entry_put_got_config++;
6542                                 } else {
6543                                         text->bv_val = "autocreation of \"olcDatabase={0}config\" failed";
6544                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={0}config\" failed");
6545                                         return NOID;
6546                                 }
6547                         } else {
6548                                 entry_put_got_config++;
6549                         }
6550                 }
6551         }
6552         if ( bi && bi->bi_tool_entry_put &&
6553                 config_add_internal( cfb, e, &ca, NULL, NULL, NULL ) == 0 )
6554                 return bi->bi_tool_entry_put( &cfb->cb_db, e, text );
6555         else
6556                 return NOID;
6557 }
6558
6559 static struct {
6560         char *name;
6561         AttributeDescription **desc;
6562 } ads[] = {
6563         { "attribute", &cfAd_attr },
6564         { "backend", &cfAd_backend },
6565         { "database", &cfAd_database },
6566         { "include", &cfAd_include },
6567         { "ldapsyntax", &cfAd_syntax },
6568         { "objectclass", &cfAd_oc },
6569         { "objectidentifier", &cfAd_om },
6570         { "overlay", &cfAd_overlay },
6571         { NULL, NULL }
6572 };
6573
6574 /* Notes:
6575  *   add / delete: all types that may be added or deleted must use an
6576  * X-ORDERED attributeType for their RDN. Adding and deleting entries
6577  * should automatically renumber the index of any siblings as needed,
6578  * so that no gaps in the numbering sequence exist after the add/delete
6579  * is completed.
6580  *   What can be added:
6581  *     schema objects
6582  *     backend objects for backend-specific config directives
6583  *     database objects
6584  *     overlay objects
6585  *
6586  *   delete: probably no support this time around.
6587  *
6588  *   modrdn: generally not done. Will be invoked automatically by add/
6589  * delete to update numbering sequence. Perform as an explicit operation
6590  * so that the renumbering effect may be replicated. Subtree rename must
6591  * be supported, since renumbering a database will affect all its child
6592  * overlays.
6593  *
6594  *  modify: must be fully supported. 
6595  */
6596
6597 int
6598 config_back_initialize( BackendInfo *bi )
6599 {
6600         ConfigTable             *ct = config_back_cf_table;
6601         ConfigArgs ca;
6602         char                    *argv[4];
6603         int                     i;
6604         AttributeDescription    *ad = NULL;
6605         const char              *text;
6606         static char             *controls[] = {
6607                 LDAP_CONTROL_MANAGEDSAIT,
6608                 NULL
6609         };
6610
6611         /* Make sure we don't exceed the bits reserved for userland */
6612         config_check_userland( CFG_LAST );
6613
6614         bi->bi_controls = controls;
6615
6616         bi->bi_open = 0;
6617         bi->bi_close = 0;
6618         bi->bi_config = 0;
6619         bi->bi_destroy = config_back_destroy;
6620
6621         bi->bi_db_init = config_back_db_init;
6622         bi->bi_db_config = 0;
6623         bi->bi_db_open = config_back_db_open;
6624         bi->bi_db_close = config_back_db_close;
6625         bi->bi_db_destroy = config_back_db_destroy;
6626
6627         bi->bi_op_bind = config_back_bind;
6628         bi->bi_op_unbind = 0;
6629         bi->bi_op_search = config_back_search;
6630         bi->bi_op_compare = 0;
6631         bi->bi_op_modify = config_back_modify;
6632         bi->bi_op_modrdn = config_back_modrdn;
6633         bi->bi_op_add = config_back_add;
6634         bi->bi_op_delete = config_back_delete;
6635         bi->bi_op_abandon = 0;
6636
6637         bi->bi_extended = 0;
6638
6639         bi->bi_chk_referrals = 0;
6640
6641         bi->bi_access_allowed = slap_access_allowed;
6642
6643         bi->bi_connection_init = 0;
6644         bi->bi_connection_destroy = 0;
6645
6646         bi->bi_entry_release_rw = config_entry_release;
6647         bi->bi_entry_get_rw = config_back_entry_get;
6648
6649         bi->bi_tool_entry_open = config_tool_entry_open;
6650         bi->bi_tool_entry_close = config_tool_entry_close;
6651         bi->bi_tool_entry_first = config_tool_entry_first;
6652         bi->bi_tool_entry_next = config_tool_entry_next;
6653         bi->bi_tool_entry_get = config_tool_entry_get;
6654         bi->bi_tool_entry_put = config_tool_entry_put;
6655
6656         ca.argv = argv;
6657         argv[ 0 ] = "slapd";
6658         ca.argv = argv;
6659         ca.argc = 3;
6660         ca.fname = argv[0];
6661
6662         argv[3] = NULL;
6663         for (i=0; OidMacros[i].name; i++ ) {
6664                 argv[1] = OidMacros[i].name;
6665                 argv[2] = OidMacros[i].oid;
6666                 parse_oidm( &ca, 0, NULL );
6667         }
6668
6669         bi->bi_cf_ocs = cf_ocs;
6670
6671         i = config_register_schema( ct, cf_ocs );
6672         if ( i ) return i;
6673
6674         i = slap_str2ad( "olcDatabase", &olcDatabaseDummy[0].ad, &text );
6675         if ( i ) return i;
6676
6677         /* setup olcRootPW to be base64-encoded when written in LDIF form;
6678          * basically, we don't care if it fails */
6679         i = slap_str2ad( "olcRootPW", &ad, &text );
6680         if ( i ) {
6681                 Debug( LDAP_DEBUG_ANY, "config_back_initialize: "
6682                         "warning, unable to get \"olcRootPW\" "
6683                         "attribute description: %d: %s\n",
6684                         i, text, 0 );
6685         } else {
6686                 (void)ldif_must_b64_encode_register( ad->ad_cname.bv_val,
6687                         ad->ad_type->sat_oid );
6688         }
6689
6690         /* set up the notable AttributeDescriptions */
6691         i = 0;
6692         for (;ct->name;ct++) {
6693                 if (strcmp(ct->name, ads[i].name)) continue;
6694                 *ads[i].desc = ct->ad;
6695                 i++;
6696                 if (!ads[i].name) break;
6697         }
6698
6699         return 0;
6700 }