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