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