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