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