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