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