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