]> git.sur5r.net Git - openldap/blob - servers/slapd/bconfig.c
Merge remote-tracking branch 'origin/mdb.master'
[openldap] / servers / slapd / bconfig.c
1 /* bconfig.c - the config backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2005-2013 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was originally developed by Howard Chu for inclusion
18  * in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <ac/string.h>
25 #include <ac/ctype.h>
26 #include <ac/errno.h>
27 #include <sys/stat.h>
28 #include <ac/unistd.h>
29
30 #include "slap.h"
31
32 #ifdef LDAP_SLAPI
33 #include "slapi/slapi.h"
34 #endif
35
36 #include <ldif.h>
37 #include <lutil.h>
38
39 #include "config.h"
40
41 #define CONFIG_RDN      "cn=config"
42 #define SCHEMA_RDN      "cn=schema"
43
44 static struct berval config_rdn = BER_BVC(CONFIG_RDN);
45 static struct berval schema_rdn = BER_BVC(SCHEMA_RDN);
46
47 extern int slap_DN_strict;      /* dn.c */
48
49 #ifdef SLAPD_MODULES
50 typedef struct modpath_s {
51         struct modpath_s *mp_next;
52         struct berval mp_path;
53         BerVarray mp_loads;
54 } ModPaths;
55
56 static ModPaths modpaths, *modlast = &modpaths, *modcur = &modpaths;
57 #endif
58
59 typedef struct ConfigFile {
60         struct ConfigFile *c_sibs;
61         struct ConfigFile *c_kids;
62         struct berval c_file;
63         AttributeType *c_at_head, *c_at_tail;
64         ContentRule *c_cr_head, *c_cr_tail;
65         ObjectClass *c_oc_head, *c_oc_tail;
66         OidMacro *c_om_head, *c_om_tail;
67         Syntax *c_syn_head, *c_syn_tail;
68         BerVarray c_dseFiles;
69 } ConfigFile;
70
71 typedef struct {
72         ConfigFile *cb_config;
73         CfEntryInfo *cb_root;
74         BackendDB       cb_db;  /* underlying database */
75         int             cb_got_ldif;
76         int             cb_use_ldif;
77 } CfBackInfo;
78
79 static CfBackInfo cfBackInfo;
80
81 static char     *passwd_salt;
82 static FILE *logfile;
83 static char     *logfileName;
84 #ifdef SLAP_AUTH_REWRITE
85 static BerVarray authz_rewrites;
86 #endif
87 static AccessControl *defacl_parsed = NULL;
88
89 static struct berval cfdir;
90
91 /* Private state */
92 static AttributeDescription *cfAd_backend, *cfAd_database, *cfAd_overlay,
93         *cfAd_include, *cfAd_attr, *cfAd_oc, *cfAd_om, *cfAd_syntax;
94
95 static ConfigFile *cfn;
96
97 static Avlnode *CfOcTree;
98
99 /* System schema state */
100 extern AttributeType *at_sys_tail;      /* at.c */
101 extern ObjectClass *oc_sys_tail;        /* oc.c */
102 extern OidMacro *om_sys_tail;   /* oidm.c */
103 extern Syntax *syn_sys_tail;    /* syntax.c */
104 static AttributeType *cf_at_tail;
105 static ObjectClass *cf_oc_tail;
106 static OidMacro *cf_om_tail;
107 static Syntax *cf_syn_tail;
108
109 static int config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca,
110         SlapReply *rs, int *renumber, Operation *op );
111
112 static int config_check_schema( Operation *op, CfBackInfo *cfb );
113
114 static ConfigDriver config_fname;
115 static ConfigDriver config_cfdir;
116 static ConfigDriver config_generic;
117 static ConfigDriver config_search_base;
118 static ConfigDriver config_passwd_hash;
119 static ConfigDriver config_schema_dn;
120 static ConfigDriver config_sizelimit;
121 static ConfigDriver config_timelimit;
122 static ConfigDriver config_overlay;
123 static ConfigDriver config_subordinate; 
124 static ConfigDriver config_suffix; 
125 #ifdef LDAP_TCP_BUFFER
126 static ConfigDriver config_tcp_buffer; 
127 #endif /* LDAP_TCP_BUFFER */
128 static ConfigDriver config_rootdn;
129 static ConfigDriver config_rootpw;
130 static ConfigDriver config_restrict;
131 static ConfigDriver config_allows;
132 static ConfigDriver config_disallows;
133 static ConfigDriver config_requires;
134 static ConfigDriver config_security;
135 static ConfigDriver config_referral;
136 static ConfigDriver config_loglevel;
137 static ConfigDriver config_updatedn;
138 static ConfigDriver config_updateref;
139 static ConfigDriver config_extra_attrs;
140 static ConfigDriver config_include;
141 static ConfigDriver config_obsolete;
142 #ifdef HAVE_TLS
143 static ConfigDriver config_tls_option;
144 static ConfigDriver config_tls_config;
145 #endif
146 extern ConfigDriver syncrepl_config;
147
148 enum {
149         CFG_ACL = 1,
150         CFG_BACKEND,
151         CFG_DATABASE,
152         CFG_TLS_RAND,
153         CFG_TLS_CIPHER,
154         CFG_TLS_PROTOCOL_MIN,
155         CFG_TLS_CERT_FILE,
156         CFG_TLS_CERT_KEY,
157         CFG_TLS_CA_PATH,
158         CFG_TLS_CA_FILE,
159         CFG_TLS_DH_FILE,
160         CFG_TLS_VERIFY,
161         CFG_TLS_CRLCHECK,
162         CFG_TLS_CRL_FILE,
163         CFG_CONCUR,
164         CFG_THREADS,
165         CFG_SALT,
166         CFG_LIMITS,
167         CFG_RO,
168         CFG_REWRITE,
169         CFG_DEPTH,
170         CFG_OID,
171         CFG_OC,
172         CFG_DIT,
173         CFG_ATTR,
174         CFG_ATOPT,
175         CFG_ROOTDSE,
176         CFG_LOGFILE,
177         CFG_PLUGIN,
178         CFG_MODLOAD,
179         CFG_MODPATH,
180         CFG_LASTMOD,
181         CFG_AZPOLICY,
182         CFG_AZREGEXP,
183         CFG_AZDUC,
184         CFG_AZDUC_IGNORE,
185         CFG_SASLSECP,
186         CFG_SSTR_IF_MAX,
187         CFG_SSTR_IF_MIN,
188         CFG_TTHREADS,
189         CFG_MIRRORMODE,
190         CFG_HIDDEN,
191         CFG_MONITORING,
192         CFG_SERVERID,
193         CFG_SORTVALS,
194         CFG_IX_INTLEN,
195         CFG_SYNTAX,
196         CFG_ACL_ADD,
197         CFG_SYNC_SUBENTRY,
198         CFG_LTHREADS,
199         CFG_IX_HASH64,
200         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                 return 1;
3207                 free(pdn.bv_val);
3208                 free(ndn.bv_val);
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) {
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                 return value_add_one( bva, ber_bvstr( "0" ) );
3665         }
3666
3667         return mask_to_verbs( loglevel_ops, l, bva );
3668 }
3669
3670 int
3671 loglevel_print( FILE *out )
3672 {
3673         int     i;
3674
3675         if ( loglevel_ops == NULL ) {
3676                 loglevel_init();
3677         }
3678
3679         fprintf( out, "Installed log subsystems:\n\n" );
3680         for ( i = 0; !BER_BVISNULL( &loglevel_ops[ i ].word ); i++ ) {
3681                 unsigned mask = loglevel_ops[ i ].mask & 0xffffffffUL;
3682                 fprintf( out,
3683                         (mask == ((slap_mask_t) -1 & 0xffffffffUL)
3684                          ? "\t%-30s (-1, 0xffffffff)\n" : "\t%-30s (%u, 0x%x)\n"),
3685                         loglevel_ops[ i ].word.bv_val, mask, mask );
3686         }
3687
3688         fprintf( out, "\nNOTE: custom log subsystems may be later installed "
3689                 "by specific code\n\n" );
3690
3691         return 0;
3692 }
3693
3694 static int config_syslog;
3695
3696 static int
3697 config_loglevel(ConfigArgs *c) {
3698         int i;
3699
3700         if ( loglevel_ops == NULL ) {
3701                 loglevel_init();
3702         }
3703
3704         if (c->op == SLAP_CONFIG_EMIT) {
3705                 /* Get default or commandline slapd setting */
3706                 if ( ldap_syslog && !config_syslog )
3707                         config_syslog = ldap_syslog;
3708                 return loglevel2bvarray( config_syslog, &c->rvalue_vals );
3709
3710         } else if ( c->op == LDAP_MOD_DELETE ) {
3711                 if ( !c->line ) {
3712                         config_syslog = 0;
3713                 } else {
3714                         i = verb_to_mask( c->line, loglevel_ops );
3715                         config_syslog &= ~loglevel_ops[i].mask;
3716                 }
3717                 if ( slapMode & SLAP_SERVER_MODE ) {
3718                         ldap_syslog = config_syslog;
3719                 }
3720                 return 0;
3721         }
3722
3723         for( i=1; i < c->argc; i++ ) {
3724                 int     level;
3725
3726                 if ( isdigit((unsigned char)c->argv[i][0]) || c->argv[i][0] == '-' ) {
3727                         if( lutil_atoix( &level, c->argv[i], 0 ) != 0 ) {
3728                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse level", c->argv[0] );
3729                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
3730                                         c->log, c->cr_msg, c->argv[i]);
3731                                 return( 1 );
3732                         }
3733                 } else {
3734                         if ( str2loglevel( c->argv[i], &level ) ) {
3735                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown level", c->argv[0] );
3736                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
3737                                         c->log, c->cr_msg, c->argv[i]);
3738                                 return( 1 );
3739                         }
3740                 }
3741                 /* Explicitly setting a zero clears all the levels */
3742                 if ( level )
3743                         config_syslog |= level;
3744                 else
3745                         config_syslog = 0;
3746         }
3747         if ( slapMode & SLAP_SERVER_MODE ) {
3748                 ldap_syslog = config_syslog;
3749         }
3750         return(0);
3751 }
3752
3753 static int
3754 config_referral(ConfigArgs *c) {
3755         struct berval val;
3756         if (c->op == SLAP_CONFIG_EMIT) {
3757                 if ( default_referral ) {
3758                         value_add( &c->rvalue_vals, default_referral );
3759                         return 0;
3760                 } else {
3761                         return 1;
3762                 }
3763         } else if ( c->op == LDAP_MOD_DELETE ) {
3764                 if ( c->valx < 0 ) {
3765                         ber_bvarray_free( default_referral );
3766                         default_referral = NULL;
3767                 } else {
3768                         int i = c->valx;
3769                         ch_free( default_referral[i].bv_val );
3770                         for (; default_referral[i].bv_val; i++ )
3771                                 default_referral[i] = default_referral[i+1];
3772                 }
3773                 return 0;
3774         }
3775         if(validate_global_referral(c->argv[1])) {
3776                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid URL", c->argv[0] );
3777                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
3778                         c->log, c->cr_msg, c->argv[1]);
3779                 return(1);
3780         }
3781
3782         ber_str2bv(c->argv[1], 0, 0, &val);
3783         if(value_add_one(&default_referral, &val)) return(LDAP_OTHER);
3784         return(0);
3785 }
3786
3787 static struct {
3788         struct berval key;
3789         int off;
3790 } sec_keys[] = {
3791         { BER_BVC("ssf="), offsetof(slap_ssf_set_t, sss_ssf) },
3792         { BER_BVC("transport="), offsetof(slap_ssf_set_t, sss_transport) },
3793         { BER_BVC("tls="), offsetof(slap_ssf_set_t, sss_tls) },
3794         { BER_BVC("sasl="), offsetof(slap_ssf_set_t, sss_sasl) },
3795         { BER_BVC("update_ssf="), offsetof(slap_ssf_set_t, sss_update_ssf) },
3796         { BER_BVC("update_transport="), offsetof(slap_ssf_set_t, sss_update_transport) },
3797         { BER_BVC("update_tls="), offsetof(slap_ssf_set_t, sss_update_tls) },
3798         { BER_BVC("update_sasl="), offsetof(slap_ssf_set_t, sss_update_sasl) },
3799         { BER_BVC("simple_bind="), offsetof(slap_ssf_set_t, sss_simple_bind) },
3800         { BER_BVNULL, 0 }
3801 };
3802
3803 static int
3804 config_security(ConfigArgs *c) {
3805         slap_ssf_set_t *set = &c->be->be_ssf_set;
3806         char *next;
3807         int i, j;
3808         if (c->op == SLAP_CONFIG_EMIT) {
3809                 char numbuf[32];
3810                 struct berval bv;
3811                 slap_ssf_t *tgt;
3812                 int rc = 1;
3813
3814                 for (i=0; !BER_BVISNULL( &sec_keys[i].key ); i++) {
3815                         tgt = (slap_ssf_t *)((char *)set + sec_keys[i].off);
3816                         if ( *tgt ) {
3817                                 rc = 0;
3818                                 bv.bv_len = snprintf( numbuf, sizeof( numbuf ), "%u", *tgt );
3819                                 if ( bv.bv_len >= sizeof( numbuf ) ) {
3820                                         ber_bvarray_free_x( c->rvalue_vals, NULL );
3821                                         c->rvalue_vals = NULL;
3822                                         rc = 1;
3823                                         break;
3824                                 }
3825                                 bv.bv_len += sec_keys[i].key.bv_len;
3826                                 bv.bv_val = ch_malloc( bv.bv_len + 1);
3827                                 next = lutil_strcopy( bv.bv_val, sec_keys[i].key.bv_val );
3828                                 strcpy( next, numbuf );
3829                                 ber_bvarray_add( &c->rvalue_vals, &bv );
3830                         }
3831                 }
3832                 return rc;
3833         }
3834         for(i = 1; i < c->argc; i++) {
3835                 slap_ssf_t *tgt = NULL;
3836                 char *src = NULL;
3837                 for ( j=0; !BER_BVISNULL( &sec_keys[j].key ); j++ ) {
3838                         if(!strncasecmp(c->argv[i], sec_keys[j].key.bv_val,
3839                                 sec_keys[j].key.bv_len)) {
3840                                 src = c->argv[i] + sec_keys[j].key.bv_len;
3841                                 tgt = (slap_ssf_t *)((char *)set + sec_keys[j].off);
3842                                 break;
3843                         }
3844                 }
3845                 if ( !tgt ) {
3846                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown factor", c->argv[0] );
3847                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
3848                                 c->log, c->cr_msg, c->argv[i]);
3849                         return(1);
3850                 }
3851
3852                 if ( lutil_atou( tgt, src ) != 0 ) {
3853                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse factor", c->argv[0] );
3854                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
3855                                 c->log, c->cr_msg, c->argv[i]);
3856                         return(1);
3857                 }
3858         }
3859         return(0);
3860 }
3861
3862 char *
3863 anlist_unparse( AttributeName *an, char *ptr, ber_len_t buflen ) {
3864         int comma = 0;
3865         char *start = ptr;
3866
3867         for (; !BER_BVISNULL( &an->an_name ); an++) {
3868                 /* if buflen == 0, assume the buffer size has been 
3869                  * already checked otherwise */
3870                 if ( buflen > 0 && buflen - ( ptr - start ) < comma + an->an_name.bv_len ) return NULL;
3871                 if ( comma ) *ptr++ = ',';
3872                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
3873                 comma = 1;
3874         }
3875         return ptr;
3876 }
3877
3878 static int
3879 config_updatedn(ConfigArgs *c) {
3880         if (c->op == SLAP_CONFIG_EMIT) {
3881                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
3882                         value_add_one(&c->rvalue_vals, &c->be->be_update_ndn);
3883                         value_add_one(&c->rvalue_nvals, &c->be->be_update_ndn);
3884                         return 0;
3885                 }
3886                 return 1;
3887         } else if ( c->op == LDAP_MOD_DELETE ) {
3888                 ch_free( c->be->be_update_ndn.bv_val );
3889                 BER_BVZERO( &c->be->be_update_ndn );
3890                 SLAP_DBFLAGS(c->be) ^= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
3891                 return 0;
3892         }
3893         if(SLAP_SHADOW(c->be)) {
3894                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> database already shadowed", c->argv[0] );
3895                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
3896                         c->log, c->cr_msg, 0);
3897                 return(1);
3898         }
3899
3900         ber_memfree_x( c->value_dn.bv_val, NULL );
3901         if ( !BER_BVISNULL( &c->be->be_update_ndn ) ) {
3902                 ber_memfree_x( c->be->be_update_ndn.bv_val, NULL );
3903         }
3904         c->be->be_update_ndn = c->value_ndn;
3905         BER_BVZERO( &c->value_dn );
3906         BER_BVZERO( &c->value_ndn );
3907
3908         return config_slurp_shadow( c );
3909 }
3910
3911 int
3912 config_shadow( ConfigArgs *c, slap_mask_t flag )
3913 {
3914         char    *notallowed = NULL;
3915
3916         if ( c->be == frontendDB ) {
3917                 notallowed = "frontend";
3918
3919         } else if ( SLAP_MONITOR(c->be) ) {
3920                 notallowed = "monitor";
3921         }
3922
3923         if ( notallowed != NULL ) {
3924                 Debug( LDAP_DEBUG_ANY, "%s: %s database cannot be shadow.\n", c->log, notallowed, 0 );
3925                 return 1;
3926         }
3927
3928         if ( SLAP_SHADOW(c->be) ) {
3929                 /* if already shadow, only check consistency */
3930                 if ( ( SLAP_DBFLAGS(c->be) & flag ) != flag ) {
3931                         Debug( LDAP_DEBUG_ANY, "%s: inconsistent shadow flag 0x%lx.\n",
3932                                 c->log, flag, 0 );
3933                         return 1;
3934                 }
3935
3936         } else {
3937                 SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | flag);
3938                 if ( !SLAP_MULTIMASTER( c->be ))
3939                         SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_SINGLE_SHADOW;
3940         }
3941
3942         return 0;
3943 }
3944
3945 static int
3946 config_updateref(ConfigArgs *c) {
3947         struct berval val;
3948         if (c->op == SLAP_CONFIG_EMIT) {
3949                 if ( c->be->be_update_refs ) {
3950                         value_add( &c->rvalue_vals, c->be->be_update_refs );
3951                         return 0;
3952                 } else {
3953                         return 1;
3954                 }
3955         } else if ( c->op == LDAP_MOD_DELETE ) {
3956                 if ( c->valx < 0 ) {
3957                         ber_bvarray_free( c->be->be_update_refs );
3958                         c->be->be_update_refs = NULL;
3959                 } else {
3960                         int i = c->valx;
3961                         ch_free( c->be->be_update_refs[i].bv_val );
3962                         for (; c->be->be_update_refs[i].bv_val; i++)
3963                                 c->be->be_update_refs[i] = c->be->be_update_refs[i+1];
3964                 }
3965                 return 0;
3966         }
3967         if(!SLAP_SHADOW(c->be) && !c->be->be_syncinfo) {
3968                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must appear after syncrepl or updatedn",
3969                         c->argv[0] );
3970                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
3971                         c->log, c->cr_msg, 0);
3972                 return(1);
3973         }
3974
3975         if(validate_global_referral(c->argv[1])) {
3976                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid URL", c->argv[0] );
3977                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
3978                         c->log, c->cr_msg, c->argv[1]);
3979                 return(1);
3980         }
3981         ber_str2bv(c->argv[1], 0, 0, &val);
3982         if(value_add_one(&c->be->be_update_refs, &val)) return(LDAP_OTHER);
3983         return(0);
3984 }
3985
3986 static int
3987 config_obsolete(ConfigArgs *c) {
3988         if (c->op == SLAP_CONFIG_EMIT)
3989                 return 1;
3990
3991         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> keyword is obsolete (ignored)",
3992                 c->argv[0] );
3993         Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0);
3994         return(0);
3995 }
3996
3997 static int
3998 config_include(ConfigArgs *c) {
3999         int savelineno = c->lineno;
4000         int rc;
4001         ConfigFile *cf;
4002         ConfigFile *cfsave = cfn;
4003         ConfigFile *cf2 = NULL;
4004
4005         /* Leftover from RE23. No dynamic config for include files */
4006         if ( c->op == SLAP_CONFIG_EMIT || c->op == LDAP_MOD_DELETE )
4007                 return 1;
4008
4009         cf = ch_calloc( 1, sizeof(ConfigFile));
4010         if ( cfn->c_kids ) {
4011                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
4012                 cf2->c_sibs = cf;
4013         } else {
4014                 cfn->c_kids = cf;
4015         }
4016         cfn = cf;
4017         ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
4018         rc = read_config_file(c->argv[1], c->depth + 1, c, config_back_cf_table);
4019         c->lineno = savelineno - 1;
4020         cfn = cfsave;
4021         if ( rc ) {
4022                 if ( cf2 ) cf2->c_sibs = NULL;
4023                 else cfn->c_kids = NULL;
4024                 ch_free( cf->c_file.bv_val );
4025                 ch_free( cf );
4026         } else {
4027                 c->ca_private = cf;
4028         }
4029         return(rc);
4030 }
4031
4032 #ifdef HAVE_TLS
4033 static int
4034 config_tls_cleanup(ConfigArgs *c) {
4035         int rc = 0;
4036
4037         if ( slap_tls_ld ) {
4038                 int opt = 1;
4039
4040                 ldap_pvt_tls_ctx_free( slap_tls_ctx );
4041                 slap_tls_ctx = NULL;
4042
4043                 /* Force new ctx to be created */
4044                 rc = ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
4045                 if( rc == 0 ) {
4046                         /* The ctx's refcount is bumped up here */
4047                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
4048                         /* This is a no-op if it's already loaded */
4049                         load_extop( &slap_EXOP_START_TLS, 0, starttls_extop );
4050                 } else {
4051                         if ( rc == LDAP_NOT_SUPPORTED )
4052                                 rc = LDAP_UNWILLING_TO_PERFORM;
4053                         else
4054                                 rc = LDAP_OTHER;
4055                 }
4056         }
4057         return rc;
4058 }
4059
4060 static int
4061 config_tls_option(ConfigArgs *c) {
4062         int flag;
4063         LDAP *ld = slap_tls_ld;
4064         switch(c->type) {
4065         case CFG_TLS_RAND:      flag = LDAP_OPT_X_TLS_RANDOM_FILE;      ld = NULL; break;
4066         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
4067         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
4068         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
4069         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
4070         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
4071         case CFG_TLS_DH_FILE:   flag = LDAP_OPT_X_TLS_DHFILE;   break;
4072         case CFG_TLS_ECNAME:    flag = LDAP_OPT_X_TLS_ECNAME;   break;
4073 #ifdef HAVE_GNUTLS
4074         case CFG_TLS_CRL_FILE:  flag = LDAP_OPT_X_TLS_CRLFILE;  break;
4075 #endif
4076         default:                Debug(LDAP_DEBUG_ANY, "%s: "
4077                                         "unknown tls_option <0x%x>\n",
4078                                         c->log, c->type, 0);
4079                 return 1;
4080         }
4081         if (c->op == SLAP_CONFIG_EMIT) {
4082                 return ldap_pvt_tls_get_option( ld, flag, &c->value_string );
4083         } else if ( c->op == LDAP_MOD_DELETE ) {
4084                 c->cleanup = config_tls_cleanup;
4085                 return ldap_pvt_tls_set_option( ld, flag, NULL );
4086         }
4087         ch_free(c->value_string);
4088         c->cleanup = config_tls_cleanup;
4089         return(ldap_pvt_tls_set_option(ld, flag, c->argv[1]));
4090 }
4091
4092 /* FIXME: this ought to be provided by libldap */
4093 static int
4094 config_tls_config(ConfigArgs *c) {
4095         int i, flag;
4096         switch(c->type) {
4097         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK; break;
4098         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT; break;
4099         case CFG_TLS_PROTOCOL_MIN: flag = LDAP_OPT_X_TLS_PROTOCOL_MIN; break;
4100         default:
4101                 Debug(LDAP_DEBUG_ANY, "%s: "
4102                                 "unknown tls_option <0x%x>\n",
4103                                 c->log, c->type, 0);
4104                 return 1;
4105         }
4106         if (c->op == SLAP_CONFIG_EMIT) {
4107                 return slap_tls_get_config( slap_tls_ld, flag, &c->value_string );
4108         } else if ( c->op == LDAP_MOD_DELETE ) {
4109                 int i = 0;
4110                 c->cleanup = config_tls_cleanup;
4111                 return ldap_pvt_tls_set_option( slap_tls_ld, flag, &i );
4112         }
4113         ch_free( c->value_string );
4114         c->cleanup = config_tls_cleanup;
4115         if ( isdigit( (unsigned char)c->argv[1][0] ) && c->type != CFG_TLS_PROTOCOL_MIN ) {
4116                 if ( lutil_atoi( &i, c->argv[1] ) != 0 ) {
4117                         Debug(LDAP_DEBUG_ANY, "%s: "
4118                                 "unable to parse %s \"%s\"\n",
4119                                 c->log, c->argv[0], c->argv[1] );
4120                         return 1;
4121                 }
4122                 return(ldap_pvt_tls_set_option(slap_tls_ld, flag, &i));
4123         } else {
4124                 return(ldap_pvt_tls_config(slap_tls_ld, flag, c->argv[1]));
4125         }
4126 }
4127 #endif
4128
4129 static CfEntryInfo *
4130 config_find_base( CfEntryInfo *root, struct berval *dn, CfEntryInfo **last )
4131 {
4132         struct berval cdn;
4133         char *c;
4134
4135         if ( !root ) {
4136                 *last = NULL;
4137                 return NULL;
4138         }
4139
4140         if ( dn_match( &root->ce_entry->e_nname, dn ))
4141                 return root;
4142
4143         c = dn->bv_val+dn->bv_len;
4144         for (;*c != ',';c--);
4145
4146         while(root) {
4147                 *last = root;
4148                 for (--c;c>dn->bv_val && *c != ',';c--);
4149                 cdn.bv_val = c;
4150                 if ( *c == ',' )
4151                         cdn.bv_val++;
4152                 cdn.bv_len = dn->bv_len - (cdn.bv_val - dn->bv_val);
4153
4154                 root = root->ce_kids;
4155
4156                 for (;root;root=root->ce_sibs) {
4157                         if ( dn_match( &root->ce_entry->e_nname, &cdn )) {
4158                                 if ( cdn.bv_val == dn->bv_val ) {
4159                                         return root;
4160                                 }
4161                                 break;
4162                         }
4163                 }
4164         }
4165         return root;
4166 }
4167
4168 typedef struct setup_cookie {
4169         CfBackInfo *cfb;
4170         ConfigArgs *ca;
4171         Entry *frontend;
4172         Entry *config;
4173         int got_frontend;
4174         int got_config;
4175 } setup_cookie;
4176
4177 static int
4178 config_ldif_resp( Operation *op, SlapReply *rs )
4179 {
4180         if ( rs->sr_type == REP_SEARCH ) {
4181                 setup_cookie *sc = op->o_callback->sc_private;
4182                 struct berval pdn;
4183
4184                 sc->cfb->cb_got_ldif = 1;
4185                 /* Does the frontend exist? */
4186                 if ( !sc->got_frontend ) {
4187                         if ( !strncmp( rs->sr_entry->e_nname.bv_val,
4188                                 "olcDatabase", STRLENOF( "olcDatabase" )))
4189                         {
4190                                 if ( strncmp( rs->sr_entry->e_nname.bv_val +
4191                                         STRLENOF( "olcDatabase" ), "={-1}frontend",
4192                                         STRLENOF( "={-1}frontend" )))
4193                                 {
4194                                         struct berval rdn;
4195                                         int i = op->o_noop;
4196                                         sc->ca->be = frontendDB;
4197                                         sc->ca->bi = frontendDB->bd_info;
4198                                         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
4199                                         rdn.bv_val = sc->ca->log;
4200                                         rdn.bv_len = snprintf(rdn.bv_val, sizeof( sc->ca->log ),
4201                                                 "%s=" SLAP_X_ORDERED_FMT "%s",
4202                                                 cfAd_database->ad_cname.bv_val, -1,
4203                                                 sc->ca->bi->bi_type);
4204                                         op->o_noop = 1;
4205                                         sc->frontend = config_build_entry( op, rs,
4206                                                 sc->cfb->cb_root, sc->ca, &rdn, &CFOC_DATABASE,
4207                                                 sc->ca->be->be_cf_ocs );
4208                                         op->o_noop = i;
4209                                         sc->got_frontend++;
4210                                 } else {
4211                                         sc->got_frontend++;
4212                                         goto ok;
4213                                 }
4214                         }
4215                 }
4216
4217                 dnParent( &rs->sr_entry->e_nname, &pdn );
4218
4219                 /* Does the configDB exist? */
4220                 if ( sc->got_frontend && !sc->got_config &&
4221                         !strncmp( rs->sr_entry->e_nname.bv_val,
4222                         "olcDatabase", STRLENOF( "olcDatabase" )) &&
4223                         dn_match( &config_rdn, &pdn ) )
4224                 {
4225                         if ( strncmp( rs->sr_entry->e_nname.bv_val +
4226                                 STRLENOF( "olcDatabase" ), "={0}config",
4227                                 STRLENOF( "={0}config" )))
4228                         {
4229                                 struct berval rdn;
4230                                 int i = op->o_noop;
4231                                 sc->ca->be = LDAP_STAILQ_FIRST( &backendDB );
4232                                 sc->ca->bi = sc->ca->be->bd_info;
4233                                 rdn.bv_val = sc->ca->log;
4234                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( sc->ca->log ),
4235                                         "%s=" SLAP_X_ORDERED_FMT "%s",
4236                                         cfAd_database->ad_cname.bv_val, 0,
4237                                         sc->ca->bi->bi_type);
4238                                 op->o_noop = 1;
4239                                 sc->config = config_build_entry( op, rs, sc->cfb->cb_root,
4240                                         sc->ca, &rdn, &CFOC_DATABASE, sc->ca->be->be_cf_ocs );
4241                                 op->o_noop = i;
4242                         }
4243                         sc->got_config++;
4244                 }
4245
4246 ok:
4247                 rs->sr_err = config_add_internal( sc->cfb, rs->sr_entry, sc->ca, NULL, NULL, NULL );
4248                 if ( rs->sr_err != LDAP_SUCCESS ) {
4249                         Debug( LDAP_DEBUG_ANY, "config error processing %s: %s\n",
4250                                 rs->sr_entry->e_name.bv_val, sc->ca->cr_msg, 0 );
4251                 }
4252         }
4253         return rs->sr_err;
4254 }
4255
4256 /* Configure and read the underlying back-ldif store */
4257 static int
4258 config_setup_ldif( BackendDB *be, const char *dir, int readit ) {
4259         CfBackInfo *cfb = be->be_private;
4260         ConfigArgs c = {0};
4261         ConfigTable *ct;
4262         char *argv[3];
4263         int rc = 0;
4264         setup_cookie sc;
4265         slap_callback cb = { NULL, config_ldif_resp, NULL, NULL };
4266         Connection conn = {0};
4267         OperationBuffer opbuf;
4268         Operation *op;
4269         SlapReply rs = {REP_RESULT};
4270         Filter filter = { LDAP_FILTER_PRESENT };
4271         struct berval filterstr = BER_BVC("(objectclass=*)");
4272         struct stat st;
4273
4274         /* Is the config directory available? */
4275         if ( stat( dir, &st ) < 0 ) {
4276                 /* No, so don't bother using the backing store.
4277                  * All changes will be in-memory only.
4278                  */
4279                 return 0;
4280         }
4281                 
4282         cfb->cb_db.bd_info = backend_info( "ldif" );
4283         if ( !cfb->cb_db.bd_info )
4284                 return 0;       /* FIXME: eventually this will be a fatal error */
4285
4286         if ( backend_db_init( "ldif", &cfb->cb_db, -1, NULL ) == NULL )
4287                 return 1;
4288
4289         cfb->cb_db.be_suffix = be->be_suffix;
4290         cfb->cb_db.be_nsuffix = be->be_nsuffix;
4291
4292         /* The suffix is always "cn=config". The underlying DB's rootdn
4293          * is always the same as the suffix.
4294          */
4295         cfb->cb_db.be_rootdn = be->be_suffix[0];
4296         cfb->cb_db.be_rootndn = be->be_nsuffix[0];
4297
4298         ber_str2bv( dir, 0, 1, &cfdir );
4299
4300         c.be = &cfb->cb_db;
4301         c.fname = "slapd";
4302         c.argc = 2;
4303         argv[0] = "directory";
4304         argv[1] = (char *)dir;
4305         argv[2] = NULL;
4306         c.argv = argv;
4307         c.reply.err = 0;
4308         c.reply.msg[0] = 0;
4309         c.table = Cft_Database;
4310
4311         ct = config_find_keyword( c.be->be_cf_ocs->co_table, &c );
4312         if ( !ct )
4313                 return 1;
4314
4315         if ( config_add_vals( ct, &c ))
4316                 return 1;
4317
4318         if ( backend_startup_one( &cfb->cb_db, &c.reply ))
4319                 return 1;
4320
4321         if ( readit ) {
4322                 void *thrctx = ldap_pvt_thread_pool_context();
4323                 int prev_DN_strict;
4324
4325                 connection_fake_init( &conn, &opbuf, thrctx );
4326                 op = &opbuf.ob_op;
4327
4328                 filter.f_desc = slap_schema.si_ad_objectClass;
4329
4330                 op->o_tag = LDAP_REQ_SEARCH;
4331
4332                 op->ors_filter = &filter;
4333                 op->ors_filterstr = filterstr;
4334                 op->ors_scope = LDAP_SCOPE_SUBTREE;
4335
4336                 op->o_dn = c.be->be_rootdn;
4337                 op->o_ndn = c.be->be_rootndn;
4338
4339                 op->o_req_dn = be->be_suffix[0];
4340                 op->o_req_ndn = be->be_nsuffix[0];
4341
4342                 op->ors_tlimit = SLAP_NO_LIMIT;
4343                 op->ors_slimit = SLAP_NO_LIMIT;
4344
4345                 op->ors_attrs = slap_anlist_all_attributes;
4346                 op->ors_attrsonly = 0;
4347
4348                 op->o_callback = &cb;
4349                 sc.cfb = cfb;
4350                 sc.ca = &c;
4351                 cb.sc_private = &sc;
4352                 sc.got_frontend = 0;
4353                 sc.got_config = 0;
4354                 sc.frontend = NULL;
4355                 sc.config = NULL;
4356
4357                 op->o_bd = &cfb->cb_db;
4358                 
4359                 /* Allow unknown attrs in DNs */
4360                 prev_DN_strict = slap_DN_strict;
4361                 slap_DN_strict = 0;
4362
4363                 rc = op->o_bd->be_search( op, &rs );
4364
4365                 /* Restore normal DN validation */
4366                 slap_DN_strict = prev_DN_strict;
4367
4368                 op->o_tag = LDAP_REQ_ADD;
4369                 if ( rc == LDAP_SUCCESS && sc.frontend ) {
4370                         rs_reinit( &rs, REP_RESULT );
4371                         op->ora_e = sc.frontend;
4372                         rc = op->o_bd->be_add( op, &rs );
4373                 }
4374                 if ( rc == LDAP_SUCCESS && sc.config ) {
4375                         rs_reinit( &rs, REP_RESULT );
4376                         op->ora_e = sc.config;
4377                         rc = op->o_bd->be_add( op, &rs );
4378                 }
4379                 ldap_pvt_thread_pool_context_reset( thrctx );
4380         }
4381
4382         /* ITS#4194 - only use if it's present, or we're converting. */
4383         if ( !readit || rc == LDAP_SUCCESS )
4384                 cfb->cb_use_ldif = 1;
4385
4386         return rc;
4387 }
4388
4389 static int
4390 CfOc_cmp( const void *c1, const void *c2 ) {
4391         const ConfigOCs *co1 = c1;
4392         const ConfigOCs *co2 = c2;
4393
4394         return ber_bvcmp( co1->co_name, co2->co_name );
4395 }
4396
4397 int
4398 config_register_schema(ConfigTable *ct, ConfigOCs *ocs) {
4399         int i;
4400
4401         i = init_config_attrs( ct );
4402         if ( i ) return i;
4403
4404         /* set up the objectclasses */
4405         i = init_config_ocs( ocs );
4406         if ( i ) return i;
4407
4408         for (i=0; ocs[i].co_def; i++) {
4409                 if ( ocs[i].co_oc ) {
4410                         ocs[i].co_name = &ocs[i].co_oc->soc_cname;
4411                         if ( !ocs[i].co_table )
4412                                 ocs[i].co_table = ct;
4413                         avl_insert( &CfOcTree, &ocs[i], CfOc_cmp, avl_dup_error );
4414                 }
4415         }
4416         return 0;
4417 }
4418
4419 int
4420 read_config(const char *fname, const char *dir) {
4421         BackendDB *be;
4422         CfBackInfo *cfb;
4423         const char *cfdir, *cfname;
4424         int rc;
4425
4426         /* Setup the config backend */
4427         be = backend_db_init( "config", NULL, 0, NULL );
4428         if ( !be )
4429                 return 1;
4430
4431         cfb = be->be_private;
4432         be->be_dfltaccess = ACL_NONE;
4433
4434         /* If no .conf, or a dir was specified, setup the dir */
4435         if ( !fname || dir ) {
4436                 if ( dir ) {
4437                         /* If explicitly given, check for existence */
4438                         struct stat st;
4439
4440                         if ( stat( dir, &st ) < 0 ) {
4441                                 Debug( LDAP_DEBUG_ANY,
4442                                         "invalid config directory %s, error %d\n",
4443                                                 dir, errno, 0 );
4444                                 return 1;
4445                         }
4446                         cfdir = dir;
4447                 } else {
4448                         cfdir = SLAPD_DEFAULT_CONFIGDIR;
4449                 }
4450                 /* if fname is defaulted, try reading .d */
4451                 rc = config_setup_ldif( be, cfdir, !fname );
4452
4453                 if ( rc ) {
4454                         /* It may be OK if the base object doesn't exist yet. */
4455                         if ( rc != LDAP_NO_SUCH_OBJECT )
4456                                 return 1;
4457                         /* ITS#4194: But if dir was specified and no fname,
4458                          * then we were supposed to read the dir. Unless we're
4459                          * trying to slapadd the dir...
4460                          */
4461                         if ( dir && !fname ) {
4462                                 if ( slapMode & (SLAP_SERVER_MODE|SLAP_TOOL_READMAIN|SLAP_TOOL_READONLY))
4463                                         return 1;
4464                                 /* Assume it's slapadd with a config dir, let it continue */
4465                                 rc = 0;
4466                                 cfb->cb_got_ldif = 1;
4467                                 cfb->cb_use_ldif = 1;
4468                                 goto done;
4469                         }
4470                 }
4471
4472                 /* If we read the config from back-ldif, nothing to do here */
4473                 if ( cfb->cb_got_ldif ) {
4474                         rc = 0;
4475                         goto done;
4476                 }
4477         }
4478
4479         if ( fname )
4480                 cfname = fname;
4481         else
4482                 cfname = SLAPD_DEFAULT_CONFIGFILE;
4483
4484         rc = read_config_file(cfname, 0, NULL, config_back_cf_table);
4485
4486         if ( rc == 0 )
4487                 ber_str2bv( cfname, 0, 1, &cfb->cb_config->c_file );
4488
4489 done:
4490         if ( rc == 0 && BER_BVISNULL( &frontendDB->be_schemadn ) ) {
4491                 ber_str2bv( SLAPD_SCHEMA_DN, STRLENOF( SLAPD_SCHEMA_DN ), 1,
4492                         &frontendDB->be_schemadn );
4493                 rc = dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
4494                 if ( rc != LDAP_SUCCESS ) {
4495                         Debug(LDAP_DEBUG_ANY, "read_config: "
4496                                 "unable to normalize default schema DN \"%s\"\n",
4497                                 frontendDB->be_schemadn.bv_val, 0, 0 );
4498                         /* must not happen */
4499                         assert( 0 );
4500                 }
4501         }
4502         if ( rc == 0 && ( slapMode & SLAP_SERVER_MODE ) && sid_list ) {
4503                 if ( !BER_BVISEMPTY( &sid_list->si_url ) && !sid_set ) {
4504                         Debug(LDAP_DEBUG_ANY, "read_config: no serverID / URL match found. "
4505                                 "Check slapd -h arguments.\n", 0,0,0 );
4506                         rc = LDAP_OTHER;
4507                 }
4508         }
4509         return rc;
4510 }
4511
4512 static int
4513 config_back_bind( Operation *op, SlapReply *rs )
4514 {
4515         if ( be_isroot_pw( op ) ) {
4516                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ));
4517                 /* frontend sends result */
4518                 return LDAP_SUCCESS;
4519         }
4520
4521         rs->sr_err = LDAP_INVALID_CREDENTIALS;
4522         send_ldap_result( op, rs );
4523
4524         return rs->sr_err;
4525 }
4526
4527 static int
4528 config_send( Operation *op, SlapReply *rs, CfEntryInfo *ce, int depth )
4529 {
4530         int rc = 0;
4531
4532         if ( test_filter( op, ce->ce_entry, op->ors_filter ) == LDAP_COMPARE_TRUE )
4533         {
4534                 rs->sr_attrs = op->ors_attrs;
4535                 rs->sr_entry = ce->ce_entry;
4536                 rs->sr_flags = 0;
4537                 rc = send_search_entry( op, rs );
4538                 if ( rc != LDAP_SUCCESS ) {
4539                         return rc;
4540                 }
4541         }
4542         if ( op->ors_scope == LDAP_SCOPE_SUBTREE ) {
4543                 if ( ce->ce_kids ) {
4544                         rc = config_send( op, rs, ce->ce_kids, 1 );
4545                         if ( rc ) return rc;
4546                 }
4547                 if ( depth ) {
4548                         for (ce=ce->ce_sibs; ce; ce=ce->ce_sibs) {
4549                                 rc = config_send( op, rs, ce, 0 );
4550                                 if ( rc ) break;
4551                         }
4552                 }
4553         }
4554         return rc;
4555 }
4556
4557 static ConfigTable *
4558 config_find_table( ConfigOCs **colst, int nocs, AttributeDescription *ad,
4559         ConfigArgs *ca )
4560 {
4561         int i, j;
4562
4563         for (j=0; j<nocs; j++) {
4564                 for (i=0; colst[j]->co_table[i].name; i++)
4565                         if ( colst[j]->co_table[i].ad == ad ) {
4566                                 ca->table = colst[j]->co_type;
4567                                 return &colst[j]->co_table[i];
4568                         }
4569         }
4570         return NULL;
4571 }
4572
4573 /* Sort the attributes of the entry according to the order defined
4574  * in the objectclass, with required attributes occurring before
4575  * allowed attributes. For any attributes with sequencing dependencies
4576  * (e.g., rootDN must be defined after suffix) the objectclass must
4577  * list the attributes in the desired sequence.
4578  */
4579 static void
4580 sort_attrs( Entry *e, ConfigOCs **colst, int nocs )
4581 {
4582         Attribute *a, *head = NULL, *tail = NULL, **prev;
4583         int i, j;
4584
4585         for (i=0; i<nocs; i++) {
4586                 if ( colst[i]->co_oc->soc_required ) {
4587                         AttributeType **at = colst[i]->co_oc->soc_required;
4588                         for (j=0; at[j]; j++) {
4589                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
4590                                         prev = &(*prev)->a_next, a=a->a_next) {
4591                                         if ( a->a_desc == at[j]->sat_ad ) {
4592                                                 *prev = a->a_next;
4593                                                 if (!head) {
4594                                                         head = a;
4595                                                         tail = a;
4596                                                 } else {
4597                                                         tail->a_next = a;
4598                                                         tail = a;
4599                                                 }
4600                                                 break;
4601                                         }
4602                                 }
4603                         }
4604                 }
4605                 if ( colst[i]->co_oc->soc_allowed ) {
4606                         AttributeType **at = colst[i]->co_oc->soc_allowed;
4607                         for (j=0; at[j]; j++) {
4608                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
4609                                         prev = &(*prev)->a_next, a=a->a_next) {
4610                                         if ( a->a_desc == at[j]->sat_ad ) {
4611                                                 *prev = a->a_next;
4612                                                 if (!head) {
4613                                                         head = a;
4614                                                         tail = a;
4615                                                 } else {
4616                                                         tail->a_next = a;
4617                                                         tail = a;
4618                                                 }
4619                                                 break;
4620                                         }
4621                                 }
4622                         }
4623                 }
4624         }
4625         if ( tail ) {
4626                 tail->a_next = e->e_attrs;
4627                 e->e_attrs = head;
4628         }
4629 }
4630
4631 static int
4632 check_vals( ConfigTable *ct, ConfigArgs *ca, void *ptr, int isAttr )
4633 {
4634         Attribute *a = NULL;
4635         AttributeDescription *ad;
4636         BerVarray vals;
4637
4638         int i, rc = 0;
4639
4640         if ( isAttr ) {
4641                 a = ptr;
4642                 ad = a->a_desc;
4643                 vals = a->a_vals;
4644         } else {
4645                 Modifications *ml = ptr;
4646                 ad = ml->sml_desc;
4647                 vals = ml->sml_values;
4648         }
4649
4650         if ( a && ( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL )) {
4651                 rc = ordered_value_sort( a, 1 );
4652                 if ( rc ) {
4653                         snprintf(ca->cr_msg, sizeof( ca->cr_msg ), "ordered_value_sort failed on attr %s\n",
4654                                 ad->ad_cname.bv_val );
4655                         return rc;
4656                 }
4657         }
4658         for ( i=0; vals[i].bv_val; i++ ) {
4659                 ca->line = vals[i].bv_val;
4660                 if (( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) &&
4661                         ca->line[0] == '{' ) {
4662                         char *idx = strchr( ca->line, '}' );
4663                         if ( idx ) ca->line = idx+1;
4664                 }
4665                 rc = config_parse_vals( ct, ca, i );
4666                 if ( rc ) {
4667                         break;
4668                 }
4669         }
4670         return rc;
4671 }
4672
4673 static int
4674 config_rename_attr( SlapReply *rs, Entry *e, struct berval *rdn,
4675         Attribute **at )
4676 {
4677         struct berval rtype, rval;
4678         Attribute *a;
4679         AttributeDescription *ad = NULL;
4680
4681         dnRdn( &e->e_name, rdn );
4682         rval.bv_val = strchr(rdn->bv_val, '=' ) + 1;
4683         rval.bv_len = rdn->bv_len - (rval.bv_val - rdn->bv_val);
4684         rtype.bv_val = rdn->bv_val;
4685         rtype.bv_len = rval.bv_val - rtype.bv_val - 1;
4686
4687         /* Find attr */
4688         slap_bv2ad( &rtype, &ad, &rs->sr_text );
4689         a = attr_find( e->e_attrs, ad );
4690         if (!a ) return LDAP_NAMING_VIOLATION;
4691         *at = a;
4692
4693         return 0;
4694 }
4695
4696 static void
4697 config_rename_kids( CfEntryInfo *ce )
4698 {
4699         CfEntryInfo *ce2;
4700         struct berval rdn, nrdn;
4701
4702         for (ce2 = ce->ce_kids; ce2; ce2 = ce2->ce_sibs) {
4703                 struct berval newdn, newndn;
4704                 dnRdn ( &ce2->ce_entry->e_name, &rdn );
4705                 dnRdn ( &ce2->ce_entry->e_nname, &nrdn );
4706                 build_new_dn( &newdn, &ce->ce_entry->e_name, &rdn, NULL );
4707                 build_new_dn( &newndn, &ce->ce_entry->e_nname, &nrdn, NULL );
4708                 free( ce2->ce_entry->e_name.bv_val );
4709                 free( ce2->ce_entry->e_nname.bv_val );
4710                 ce2->ce_entry->e_name = newdn;
4711                 ce2->ce_entry->e_nname = newndn;
4712                 config_rename_kids( ce2 );
4713         }
4714 }
4715
4716 static int
4717 config_rename_one( Operation *op, SlapReply *rs, Entry *e,
4718         CfEntryInfo *parent, Attribute *a, struct berval *newrdn,
4719         struct berval *nnewrdn, int use_ldif )
4720 {
4721         char *ptr1;
4722         int rc = 0;
4723         struct berval odn, ondn;
4724
4725         odn = e->e_name;
4726         ondn = e->e_nname;
4727         build_new_dn( &e->e_name, &parent->ce_entry->e_name, newrdn, NULL );
4728         build_new_dn( &e->e_nname, &parent->ce_entry->e_nname, nnewrdn, NULL );
4729
4730         /* Replace attr */
4731         free( a->a_vals[0].bv_val );
4732         ptr1 = strchr( newrdn->bv_val, '=' ) + 1;
4733         a->a_vals[0].bv_len = newrdn->bv_len - (ptr1 - newrdn->bv_val);
4734         a->a_vals[0].bv_val = ch_malloc( a->a_vals[0].bv_len + 1 );
4735         strcpy( a->a_vals[0].bv_val, ptr1 );
4736
4737         if ( a->a_nvals != a->a_vals ) {
4738                 free( a->a_nvals[0].bv_val );
4739                 ptr1 = strchr( nnewrdn->bv_val, '=' ) + 1;
4740                 a->a_nvals[0].bv_len = nnewrdn->bv_len - (ptr1 - nnewrdn->bv_val);
4741                 a->a_nvals[0].bv_val = ch_malloc( a->a_nvals[0].bv_len + 1 );
4742                 strcpy( a->a_nvals[0].bv_val, ptr1 );
4743         }
4744         if ( use_ldif ) {
4745                 CfBackInfo *cfb = (CfBackInfo *)op->o_bd->be_private;
4746                 BackendDB *be = op->o_bd;
4747                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
4748                 struct berval dn, ndn, xdn, xndn;
4749
4750                 op->o_bd = &cfb->cb_db;
4751
4752                 /* Save current rootdn; use the underlying DB's rootdn */
4753                 dn = op->o_dn;
4754                 ndn = op->o_ndn;
4755                 xdn = op->o_req_dn;
4756                 xndn = op->o_req_ndn;
4757                 op->o_dn = op->o_bd->be_rootdn;
4758                 op->o_ndn = op->o_bd->be_rootndn;
4759                 op->o_req_dn = odn;
4760                 op->o_req_ndn = ondn;
4761
4762                 scp = op->o_callback;
4763                 op->o_callback = &sc;
4764                 op->orr_newrdn = *newrdn;
4765                 op->orr_nnewrdn = *nnewrdn;
4766                 op->orr_newSup = NULL;
4767                 op->orr_nnewSup = NULL;
4768                 op->orr_deleteoldrdn = 1;
4769                 op->orr_modlist = NULL;
4770                 slap_modrdn2mods( op, rs );
4771                 slap_mods_opattrs( op, &op->orr_modlist, 1 );
4772                 rc = op->o_bd->be_modrdn( op, rs );
4773                 slap_mods_free( op->orr_modlist, 1 );
4774
4775                 op->o_bd = be;
4776                 op->o_callback = scp;
4777                 op->o_dn = dn;
4778                 op->o_ndn = ndn;
4779                 op->o_req_dn = xdn;
4780                 op->o_req_ndn = xndn;
4781         }
4782         free( odn.bv_val );
4783         free( ondn.bv_val );
4784         if ( e->e_private )
4785                 config_rename_kids( e->e_private );
4786         return rc;
4787 }
4788
4789 static int
4790 config_renumber_one( Operation *op, SlapReply *rs, CfEntryInfo *parent, 
4791         Entry *e, int idx, int tailindex, int use_ldif )
4792 {
4793         struct berval ival, newrdn, nnewrdn;
4794         struct berval rdn;
4795         Attribute *a;
4796         char ibuf[32], *ptr1, *ptr2 = NULL;
4797         int rc = 0;
4798
4799         rc = config_rename_attr( rs, e, &rdn, &a );
4800         if ( rc ) return rc;
4801
4802         ival.bv_val = ibuf;
4803         ival.bv_len = snprintf( ibuf, sizeof( ibuf ), SLAP_X_ORDERED_FMT, idx );
4804         if ( ival.bv_len >= sizeof( ibuf ) ) {
4805                 return LDAP_NAMING_VIOLATION;
4806         }
4807         
4808         newrdn.bv_len = rdn.bv_len + ival.bv_len;
4809         newrdn.bv_val = ch_malloc( newrdn.bv_len+1 );
4810
4811         if ( tailindex ) {
4812                 ptr1 = lutil_strncopy( newrdn.bv_val, rdn.bv_val, rdn.bv_len );
4813                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
4814         } else {
4815                 int xlen;
4816                 ptr2 = ber_bvchr( &rdn, '}' );
4817                 if ( ptr2 ) {
4818                         ptr2++;
4819                 } else {
4820                         ptr2 = rdn.bv_val + a->a_desc->ad_cname.bv_len + 1;
4821                 }
4822                 xlen = rdn.bv_len - (ptr2 - rdn.bv_val);
4823                 ptr1 = lutil_strncopy( newrdn.bv_val, a->a_desc->ad_cname.bv_val,
4824                         a->a_desc->ad_cname.bv_len );
4825                 *ptr1++ = '=';
4826                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
4827                 ptr1 = lutil_strncopy( ptr1, ptr2, xlen );
4828                 *ptr1 = '\0';
4829         }
4830
4831         /* Do the equivalent of ModRDN */
4832         /* Replace DN / NDN */
4833         newrdn.bv_len = ptr1 - newrdn.bv_val;
4834         rdnNormalize( 0, NULL, NULL, &newrdn, &nnewrdn, NULL );
4835         rc = config_rename_one( op, rs, e, parent, a, &newrdn, &nnewrdn, use_ldif );
4836
4837         free( nnewrdn.bv_val );
4838         free( newrdn.bv_val );
4839         return rc;
4840 }
4841
4842 static int
4843 check_name_index( CfEntryInfo *parent, ConfigType ce_type, Entry *e,
4844         SlapReply *rs, int *renum, int *ibase )
4845 {
4846         CfEntryInfo *ce;
4847         int index = -1, gotindex = 0, nsibs, rc = 0;
4848         int renumber = 0, tailindex = 0, isfrontend = 0, isconfig = 0;
4849         char *ptr1, *ptr2 = NULL;
4850         struct berval rdn;
4851
4852         if ( renum ) *renum = 0;
4853
4854         /* These entries don't get indexed/renumbered */
4855         if ( ce_type == Cft_Global ) return 0;
4856         if ( ce_type == Cft_Schema && parent->ce_type == Cft_Global ) return 0;
4857
4858         if ( ce_type == Cft_Module )
4859                 tailindex = 1;
4860
4861         /* See if the rdn has an index already */
4862         dnRdn( &e->e_name, &rdn );
4863         if ( ce_type == Cft_Database ) {
4864                 if ( !strncmp( rdn.bv_val + rdn.bv_len - STRLENOF("frontend"),
4865                                 "frontend", STRLENOF("frontend") )) 
4866                         isfrontend = 1;
4867                 else if ( !strncmp( rdn.bv_val + rdn.bv_len - STRLENOF("config"),
4868                                 "config", STRLENOF("config") )) 
4869                         isconfig = 1;
4870         }
4871         ptr1 = ber_bvchr( &e->e_name, '{' );
4872         if ( ptr1 && ptr1 < &e->e_name.bv_val[ rdn.bv_len ] ) {
4873                 char    *next;
4874                 ptr2 = strchr( ptr1, '}' );
4875                 if ( !ptr2 || ptr2 > &e->e_name.bv_val[ rdn.bv_len ] )
4876                         return LDAP_NAMING_VIOLATION;
4877                 if ( ptr2-ptr1 == 1)
4878                         return LDAP_NAMING_VIOLATION;
4879                 gotindex = 1;
4880                 index = strtol( ptr1 + 1, &next, 10 );
4881                 if ( next == ptr1 + 1 || next[ 0 ] != '}' ) {
4882                         return LDAP_NAMING_VIOLATION;
4883                 }
4884                 if ( index < 0 ) {
4885                         /* Special case, we allow -1 for the frontendDB */
4886                         if ( index != -1 || !isfrontend )
4887                                 return LDAP_NAMING_VIOLATION;
4888                 }
4889                 if ( isconfig && index != 0 ){
4890                         return LDAP_NAMING_VIOLATION;
4891                 }
4892         }
4893
4894         /* count related kids.
4895          * For entries of type Cft_Misc, only count siblings with same RDN type
4896          */
4897         if ( ce_type == Cft_Misc ) {
4898                 rdn.bv_val = e->e_nname.bv_val;
4899                 ptr1 = strchr( rdn.bv_val, '=' );
4900                 assert( ptr1 != NULL );
4901
4902                 rdn.bv_len = ptr1 - rdn.bv_val;
4903
4904                 for (nsibs=0, ce=parent->ce_kids; ce; ce=ce->ce_sibs) {
4905                         struct berval rdn2;
4906                         if ( ce->ce_type != ce_type )
4907                                 continue;
4908
4909                         dnRdn( &ce->ce_entry->e_nname, &rdn2 );
4910
4911                         ptr1 = strchr( rdn2.bv_val, '=' );
4912                         assert( ptr1 != NULL );
4913
4914                         rdn2.bv_len = ptr1 - rdn2.bv_val;
4915                         if ( bvmatch( &rdn, &rdn2 ))
4916                                 nsibs++;
4917                 }
4918         } else {
4919                 for (nsibs=0, ce=parent->ce_kids; ce; ce=ce->ce_sibs) {
4920                         if ( ce->ce_type == ce_type ) nsibs++;
4921                 }
4922         }
4923
4924         /* account for -1 frontend */
4925         if ( ce_type == Cft_Database )
4926                 nsibs--;
4927
4928         if ( index != nsibs ) {
4929                 if ( gotindex ) {
4930                         if ( index < nsibs ) {
4931                                 if ( tailindex ) return LDAP_NAMING_VIOLATION;
4932                                 /* Siblings need to be renumbered */
4933                                 if ( index != -1 || !isfrontend )
4934                                         renumber = 1;
4935                         }
4936                 }
4937                 /* config DB is always "0" */
4938                 if ( isconfig && index == -1 ) {
4939                         index = 0;
4940                 }
4941                 if (( !isfrontend && index == -1 ) || ( index > nsibs ) ){
4942                         index = nsibs;
4943                 }
4944
4945                 /* just make index = nsibs */
4946                 if ( !renumber ) {
4947                         rc = config_renumber_one( NULL, rs, parent, e, index, tailindex, 0 );
4948                 }
4949         }
4950         if ( ibase ) *ibase = index;
4951         if ( renum ) *renum = renumber;
4952         return rc;
4953 }
4954
4955 /* Insert all superior classes of the given class */
4956 static int
4957 count_oc( ObjectClass *oc, ConfigOCs ***copp, int *nocs )
4958 {
4959         ConfigOCs       co, *cop;
4960         ObjectClass     **sups;
4961
4962         for ( sups = oc->soc_sups; sups && *sups; sups++ ) {
4963                 if ( count_oc( *sups, copp, nocs ) ) {
4964                         return -1;
4965                 }
4966         }
4967
4968         co.co_name = &oc->soc_cname;
4969         cop = avl_find( CfOcTree, &co, CfOc_cmp );
4970         if ( cop ) {
4971                 int     i;
4972
4973                 /* check for duplicates */
4974                 for ( i = 0; i < *nocs; i++ ) {
4975                         if ( *copp && (*copp)[i] == cop ) {
4976                                 break;
4977                         }
4978                 }
4979
4980                 if ( i == *nocs ) {
4981                         ConfigOCs **tmp = ch_realloc( *copp, (*nocs + 1)*sizeof( ConfigOCs * ) );
4982                         if ( tmp == NULL ) {
4983                                 return -1;
4984                         }
4985                         *copp = tmp;
4986                         (*copp)[*nocs] = cop;
4987                         (*nocs)++;
4988                 }
4989         }
4990
4991         return 0;
4992 }
4993
4994 /* Find all superior classes of the given objectclasses,
4995  * return list in order of most-subordinate first.
4996  *
4997  * Special / auxiliary / Cft_Misc classes always take precedence.
4998  */
4999 static ConfigOCs **
5000 count_ocs( Attribute *oc_at, int *nocs )
5001 {
5002         int             i, j, misc = -1;
5003         ConfigOCs       **colst = NULL;
5004
5005         *nocs = 0;
5006
5007         for ( i = oc_at->a_numvals; i--; ) {
5008                 ObjectClass     *oc = oc_bvfind( &oc_at->a_nvals[i] );
5009
5010                 assert( oc != NULL );
5011                 if ( count_oc( oc, &colst, nocs ) ) {
5012                         ch_free( colst );
5013                         return NULL;
5014                 }
5015         }
5016
5017         /* invert order */
5018         i = 0;
5019         j = *nocs - 1;
5020         while ( i < j ) {
5021                 ConfigOCs *tmp = colst[i];
5022                 colst[i] = colst[j];
5023                 colst[j] = tmp;
5024                 if (tmp->co_type == Cft_Misc)
5025                         misc = j;
5026                 i++; j--;
5027         }
5028         /* Move misc class to front of list */
5029         if (misc > 0) {
5030                 ConfigOCs *tmp = colst[misc];
5031                 for (i=misc; i>0; i--)
5032                         colst[i] = colst[i-1];
5033                 colst[0] = tmp;
5034         }
5035
5036         return colst;
5037 }
5038
5039 static int
5040 cfAddInclude( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
5041 {
5042         /* Leftover from RE23. Never parse this entry */
5043         return LDAP_COMPARE_TRUE;
5044 }
5045
5046 static int
5047 cfAddSchema( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
5048 {
5049         ConfigFile *cfo;
5050
5051         /* This entry is hardcoded, don't re-parse it */
5052         if ( p->ce_type == Cft_Global ) {
5053                 cfn = p->ce_private;
5054                 ca->ca_private = cfn;
5055                 return LDAP_COMPARE_TRUE;
5056         }
5057         if ( p->ce_type != Cft_Schema )
5058                 return LDAP_CONSTRAINT_VIOLATION;
5059
5060         cfn = ch_calloc( 1, sizeof(ConfigFile) );
5061         ca->ca_private = cfn;
5062         cfo = p->ce_private;
5063         cfn->c_sibs = cfo->c_kids;
5064         cfo->c_kids = cfn;
5065         return LDAP_SUCCESS;
5066 }
5067
5068 static int
5069 cfAddDatabase( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
5070 {
5071         if ( p->ce_type != Cft_Global ) {
5072                 return LDAP_CONSTRAINT_VIOLATION;
5073         }
5074         /* config must be {0}, nothing else allowed */
5075         if ( !strncmp( e->e_nname.bv_val, "olcDatabase={0}", STRLENOF("olcDatabase={0}")) &&
5076                 strncmp( e->e_nname.bv_val + STRLENOF("olcDatabase={0}"), "config,", STRLENOF("config,") )) {
5077                 return LDAP_CONSTRAINT_VIOLATION;
5078         }
5079         ca->be = frontendDB;    /* just to get past check_vals */
5080         return LDAP_SUCCESS;
5081 }
5082
5083 static int
5084 cfAddBackend( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
5085 {
5086         if ( p->ce_type != Cft_Global ) {
5087                 return LDAP_CONSTRAINT_VIOLATION;
5088         }
5089         return LDAP_SUCCESS;
5090 }
5091
5092 static int
5093 cfAddModule( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
5094 {
5095         if ( p->ce_type != Cft_Global ) {
5096                 return LDAP_CONSTRAINT_VIOLATION;
5097         }
5098         return LDAP_SUCCESS;
5099 }
5100
5101 static int
5102 cfAddOverlay( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
5103 {
5104         if ( p->ce_type != Cft_Database ) {
5105                 return LDAP_CONSTRAINT_VIOLATION;
5106         }
5107         ca->be = p->ce_be;
5108         return LDAP_SUCCESS;
5109 }
5110
5111 static void
5112 schema_destroy_one( ConfigArgs *ca, ConfigOCs **colst, int nocs,
5113         CfEntryInfo *p )
5114 {
5115         ConfigTable *ct;
5116         ConfigFile *cfo;
5117         AttributeDescription *ad;
5118         const char *text;
5119
5120         ca->valx = -1;
5121         ca->line = NULL;
5122         ca->argc = 1;
5123         if ( cfn->c_cr_head ) {
5124                 struct berval bv = BER_BVC("olcDitContentRules");
5125                 ad = NULL;
5126                 slap_bv2ad( &bv, &ad, &text );
5127                 ct = config_find_table( colst, nocs, ad, ca );
5128                 config_del_vals( ct, ca );
5129         }
5130         if ( cfn->c_oc_head ) {
5131                 struct berval bv = BER_BVC("olcObjectClasses");
5132                 ad = NULL;
5133                 slap_bv2ad( &bv, &ad, &text );
5134                 ct = config_find_table( colst, nocs, ad, ca );
5135                 config_del_vals( ct, ca );
5136         }
5137         if ( cfn->c_at_head ) {
5138                 struct berval bv = BER_BVC("olcAttributeTypes");
5139                 ad = NULL;
5140                 slap_bv2ad( &bv, &ad, &text );
5141                 ct = config_find_table( colst, nocs, ad, ca );
5142                 config_del_vals( ct, ca );
5143         }
5144         if ( cfn->c_syn_head ) {
5145                 struct berval bv = BER_BVC("olcLdapSyntaxes");
5146                 ad = NULL;
5147                 slap_bv2ad( &bv, &ad, &text );
5148                 ct = config_find_table( colst, nocs, ad, ca );
5149                 config_del_vals( ct, ca );
5150         }
5151         if ( cfn->c_om_head ) {
5152                 struct berval bv = BER_BVC("olcObjectIdentifier");
5153                 ad = NULL;
5154                 slap_bv2ad( &bv, &ad, &text );
5155                 ct = config_find_table( colst, nocs, ad, ca );
5156                 config_del_vals( ct, ca );
5157         }
5158         cfo = p->ce_private;
5159         cfo->c_kids = cfn->c_sibs;
5160         ch_free( cfn );
5161 }
5162
5163 static int
5164 config_add_oc( ConfigOCs **cop, CfEntryInfo *last, Entry *e, ConfigArgs *ca )
5165 {
5166         int             rc = LDAP_CONSTRAINT_VIOLATION;
5167         ObjectClass     **ocp;
5168
5169         if ( (*cop)->co_ldadd ) {
5170                 rc = (*cop)->co_ldadd( last, e, ca );
5171                 if ( rc != LDAP_CONSTRAINT_VIOLATION ) {
5172                         return rc;
5173                 }
5174         }
5175
5176         for ( ocp = (*cop)->co_oc->soc_sups; ocp && *ocp; ocp++ ) {
5177                 ConfigOCs       co = { 0 };
5178
5179                 co.co_name = &(*ocp)->soc_cname;
5180                 *cop = avl_find( CfOcTree, &co, CfOc_cmp );
5181                 if ( *cop == NULL ) {
5182                         return rc;
5183                 }
5184
5185                 rc = config_add_oc( cop, last, e, ca );
5186                 if ( rc != LDAP_CONSTRAINT_VIOLATION ) {
5187                         return rc;
5188                 }
5189         }
5190
5191         return rc;
5192 }
5193
5194 /* Parse an LDAP entry into config directives */
5195 static int
5196 config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca, SlapReply *rs,
5197         int *renum, Operation *op )
5198 {
5199         CfEntryInfo     *ce, *last = NULL;
5200         ConfigOCs       co, *coptr, **colst;
5201         Attribute       *a, *oc_at, *soc_at;
5202         int             i, ibase = -1, nocs, rc = 0;
5203         struct berval   pdn;
5204         ConfigTable     *ct;
5205         char            *ptr, *log_prefix = op ? op->o_log_prefix : "";
5206
5207         memset( ca, 0, sizeof(ConfigArgs));
5208
5209         /* Make sure parent exists and entry does not. But allow
5210          * Databases and Overlays to be inserted. Don't do any
5211          * auto-renumbering if manageDSAit control is present.
5212          */
5213         ce = config_find_base( cfb->cb_root, &e->e_nname, &last );
5214         if ( ce ) {
5215                 if ( ( op && op->o_managedsait ) ||
5216                         ( ce->ce_type != Cft_Database && ce->ce_type != Cft_Overlay &&
5217                           ce->ce_type != Cft_Module ) )
5218                 {
5219                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
5220                                 "DN=\"%s\" already exists\n",
5221                                 log_prefix, e->e_name.bv_val, 0 );
5222                         /* global schema ignores all writes */
5223                         if ( ce->ce_type == Cft_Schema && ce->ce_parent->ce_type == Cft_Global )
5224                                 return LDAP_COMPARE_TRUE;
5225                         return LDAP_ALREADY_EXISTS;
5226                 }
5227         }
5228
5229         dnParent( &e->e_nname, &pdn );
5230
5231         /* If last is NULL, the new entry is the root/suffix entry, 
5232          * otherwise last should be the parent.
5233          */
5234         if ( last && !dn_match( &last->ce_entry->e_nname, &pdn ) ) {
5235                 if ( rs ) {
5236                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5237                 }
5238                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
5239                         "DN=\"%s\" not child of DN=\"%s\"\n",
5240                         log_prefix, e->e_name.bv_val,
5241                         last->ce_entry->e_name.bv_val );
5242                 return LDAP_NO_SUCH_OBJECT;
5243         }
5244
5245         if ( op ) {
5246                 /* No parent, must be root. This will never happen... */
5247                 if ( !last && !be_isroot( op ) && !be_shadow_update( op ) ) {
5248                         return LDAP_NO_SUCH_OBJECT;
5249                 }
5250
5251                 if ( last && !access_allowed( op, last->ce_entry,
5252                         slap_schema.si_ad_children, NULL, ACL_WADD, NULL ) )
5253                 {
5254                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
5255                                 "DN=\"%s\" no write access to \"children\" of parent\n",
5256                                 log_prefix, e->e_name.bv_val, 0 );
5257                         return LDAP_INSUFFICIENT_ACCESS;
5258                 }
5259         }
5260
5261         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
5262         if ( !oc_at ) {
5263                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
5264                         "DN=\"%s\" no objectClass\n",
5265                         log_prefix, e->e_name.bv_val, 0 );
5266                 return LDAP_OBJECT_CLASS_VIOLATION;
5267         }
5268
5269         soc_at = attr_find( e->e_attrs, slap_schema.si_ad_structuralObjectClass );
5270         if ( !soc_at ) {
5271                 ObjectClass     *soc = NULL;
5272                 char            textbuf[ SLAP_TEXT_BUFLEN ];
5273                 const char      *text = textbuf;
5274
5275                 /* FIXME: check result */
5276                 rc = structural_class( oc_at->a_nvals, &soc, NULL,
5277                         &text, textbuf, sizeof(textbuf), NULL );
5278                 if ( rc != LDAP_SUCCESS ) {
5279                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
5280                                 "DN=\"%s\" no structural objectClass (%s)\n",
5281                                 log_prefix, e->e_name.bv_val, text );
5282                         return rc;
5283                 }
5284                 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &soc->soc_cname, NULL );
5285                 soc_at = attr_find( e->e_attrs, slap_schema.si_ad_structuralObjectClass );
5286                 if ( soc_at == NULL ) {
5287                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
5288                                 "DN=\"%s\" no structural objectClass; "
5289                                 "unable to merge computed class %s\n",
5290                                 log_prefix, e->e_name.bv_val,
5291                                 soc->soc_cname.bv_val );
5292                         return LDAP_OBJECT_CLASS_VIOLATION;
5293                 }
5294
5295                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
5296                         "DN=\"%s\" no structural objectClass; "
5297                         "computed objectClass %s merged\n",
5298                         log_prefix, e->e_name.bv_val,
5299                         soc->soc_cname.bv_val );
5300         }
5301
5302         /* Fake the coordinates based on whether we're part of an
5303          * LDAP Add or if reading the config dir
5304          */
5305         if ( rs ) {
5306                 ca->fname = "slapd";
5307                 ca->lineno = 0;
5308         } else {
5309                 ca->fname = cfdir.bv_val;
5310                 ca->lineno = 1;
5311         }
5312         ca->ca_op = op;
5313
5314         co.co_name = &soc_at->a_nvals[0];
5315         coptr = avl_find( CfOcTree, &co, CfOc_cmp );
5316         if ( coptr == NULL ) {
5317                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
5318                         "DN=\"%s\" no structural objectClass in configuration table\n",
5319                         log_prefix, e->e_name.bv_val, 0 );
5320                 return LDAP_OBJECT_CLASS_VIOLATION;
5321         }
5322
5323         /* Only the root can be Cft_Global, everything else must
5324          * have a parent. Only limited nesting arrangements are allowed.
5325          */
5326         rc = LDAP_CONSTRAINT_VIOLATION;
5327         if ( coptr->co_type == Cft_Global && !last ) {
5328                 cfn = cfb->cb_config;
5329                 ca->ca_private = cfn;
5330                 ca->be = frontendDB;    /* just to get past check_vals */
5331                 rc = LDAP_SUCCESS;
5332         }
5333
5334         colst = count_ocs( oc_at, &nocs );
5335
5336         /* Check whether the Add is allowed by its parent, and do
5337          * any necessary arg setup
5338          */
5339         if ( last ) {
5340                 rc = config_add_oc( &coptr, last, e, ca );
5341                 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
5342                         for ( i = 0; i<nocs; i++ ) {
5343                                 /* Already checked these */
5344                                 if ( colst[i]->co_oc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
5345                                         continue;
5346                                 if ( colst[i]->co_ldadd &&
5347                                         ( rc = colst[i]->co_ldadd( last, e, ca ))
5348                                                 != LDAP_CONSTRAINT_VIOLATION ) {
5349                                         coptr = colst[i];
5350                                         break;
5351                                 }
5352                         }
5353                 }
5354                 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
5355                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
5356                                 "DN=\"%s\" no structural objectClass add function\n",
5357                                 log_prefix, e->e_name.bv_val, 0 );
5358                         return LDAP_OBJECT_CLASS_VIOLATION;
5359                 }
5360         }
5361
5362         /* Add the entry but don't parse it, we already have its contents */
5363         if ( rc == LDAP_COMPARE_TRUE ) {
5364                 rc = LDAP_SUCCESS;
5365                 goto ok;
5366         }
5367
5368         if ( rc != LDAP_SUCCESS )
5369                 goto done_noop;
5370
5371         /* Parse all the values and check for simple syntax errors before
5372          * performing any set actions.
5373          *
5374          * If doing an LDAPadd, check for indexed names and any necessary
5375          * renaming/renumbering. Entries that don't need indexed names are
5376          * ignored. Entries that need an indexed name and arrive without one
5377          * are assigned to the end. Entries that arrive with an index may
5378          * cause the following entries to be renumbered/bumped down.
5379          *
5380          * Note that "pseudo-indexed" entries (cn=Include{xx}, cn=Module{xx})
5381          * don't allow Adding an entry with an index that's already in use.
5382          * This is flagged as an error (LDAP_ALREADY_EXISTS) up above.
5383          *
5384          * These entries can have auto-assigned indexes (appended to the end)
5385          * but only the other types support auto-renumbering of siblings.
5386          */
5387         {
5388                 rc = check_name_index( last, coptr->co_type, e, rs, renum,
5389                         &ibase );
5390                 if ( rc ) {
5391                         goto done_noop;
5392                 }
5393                 if ( renum && *renum && coptr->co_type != Cft_Database &&
5394                         coptr->co_type != Cft_Overlay )
5395                 {
5396                         snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
5397                                 "operation requires sibling renumbering" );
5398                         rc = LDAP_UNWILLING_TO_PERFORM;
5399                         goto done_noop;
5400                 }
5401         }
5402
5403         init_config_argv( ca );
5404
5405         /* Make sure we process attrs in the required order */
5406         sort_attrs( e, colst, nocs );
5407
5408         for ( a = e->e_attrs; a; a = a->a_next ) {
5409                 if ( a == oc_at ) continue;
5410                 ct = config_find_table( colst, nocs, a->a_desc, ca );
5411                 if ( !ct ) continue;    /* user data? */
5412                 rc = check_vals( ct, ca, a, 1 );
5413                 if ( rc ) goto done_noop;
5414         }
5415
5416         /* Basic syntax checks are OK. Do the actual settings. */
5417         for ( a=e->e_attrs; a; a=a->a_next ) {
5418                 if ( a == oc_at ) continue;
5419                 ct = config_find_table( colst, nocs, a->a_desc, ca );
5420                 if ( !ct ) continue;    /* user data? */
5421                 for (i=0; a->a_vals[i].bv_val; i++) {
5422                         char *iptr = NULL;
5423                         ca->valx = -1;
5424                         ca->line = a->a_vals[i].bv_val;
5425                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
5426                                 ptr = strchr( ca->line, '}' );
5427                                 if ( ptr ) {
5428                                         iptr = strchr( ca->line, '{' );
5429                                         ca->line = ptr+1;
5430                                 }
5431                         }
5432                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED_SIB ) {
5433                                 if ( iptr ) {
5434                                         ca->valx = strtol( iptr+1, NULL, 0 );
5435                                 }
5436                         } else {
5437                                 ca->valx = i;
5438                         }
5439                         rc = config_parse_add( ct, ca, i );
5440                         if ( rc ) {
5441                                 rc = LDAP_OTHER;
5442                                 goto done;
5443                         }
5444                 }
5445         }
5446 ok:
5447         /* Newly added databases and overlays need to be started up */
5448         if ( CONFIG_ONLINE_ADD( ca )) {
5449                 if ( coptr->co_type == Cft_Database ) {
5450                         rc = backend_startup_one( ca->be, &ca->reply );
5451
5452                 } else if ( coptr->co_type == Cft_Overlay ) {
5453                         if ( ca->bi->bi_db_open ) {
5454                                 BackendInfo *bi_orig = ca->be->bd_info;
5455                                 ca->be->bd_info = ca->bi;
5456                                 rc = ca->bi->bi_db_open( ca->be, &ca->reply );
5457                                 ca->be->bd_info = bi_orig;
5458                         }
5459                 } else if ( ca->cleanup ) {
5460                         rc = ca->cleanup( ca );
5461                 }
5462                 if ( rc ) {
5463                         if (ca->cr_msg[0] == '\0')
5464                                 snprintf( ca->cr_msg, sizeof( ca->cr_msg ), "<%s> failed startup", ca->argv[0] );
5465
5466                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
5467                                 ca->log, ca->cr_msg, ca->argv[1] );
5468                         rc = LDAP_OTHER;
5469                         goto done;
5470                 }
5471         }
5472
5473         ca->valx = ibase;
5474         ce = ch_calloc( 1, sizeof(CfEntryInfo) );
5475         ce->ce_parent = last;
5476         ce->ce_entry = entry_dup( e );
5477         ce->ce_entry->e_private = ce;
5478         ce->ce_type = coptr->co_type;
5479         ce->ce_be = ca->be;
5480         ce->ce_bi = ca->bi;
5481         ce->ce_private = ca->ca_private;
5482         ca->ca_entry = ce->ce_entry;
5483         if ( !last ) {
5484                 cfb->cb_root = ce;
5485         } else if ( last->ce_kids ) {
5486                 CfEntryInfo *c2, **cprev;
5487
5488                 /* Advance to first of this type */
5489                 cprev = &last->ce_kids;
5490                 for ( c2 = *cprev; c2 && c2->ce_type < ce->ce_type; ) {
5491                         cprev = &c2->ce_sibs;
5492                         c2 = c2->ce_sibs;
5493                 }
5494                 /* Account for the (-1) frontendDB entry */
5495                 if ( ce->ce_type == Cft_Database ) {
5496                         if ( ca->be == frontendDB )
5497                                 ibase = 0;
5498                         else if ( ibase != -1 )
5499                                 ibase++;
5500                 }
5501                 /* Append */
5502                 if ( ibase < 0 ) {
5503                         for (c2 = *cprev; c2 && c2->ce_type == ce->ce_type;) {
5504                                 cprev = &c2->ce_sibs;
5505                                 c2 = c2->ce_sibs;
5506                         }
5507                 } else {
5508                 /* Insert */
5509                         int i;
5510                         for ( i=0; i<ibase; i++ ) {
5511                                 c2 = *cprev;
5512                                 cprev = &c2->ce_sibs;
5513                         }
5514                 }
5515                 ce->ce_sibs = *cprev;
5516                 *cprev = ce;
5517         } else {
5518                 last->ce_kids = ce;
5519         }
5520
5521 done:
5522         if ( rc ) {
5523                 if ( (coptr->co_type == Cft_Database) && ca->be ) {
5524                         if ( ca->be != frontendDB )
5525                                 backend_destroy_one( ca->be, 1 );
5526                 } else if ( (coptr->co_type == Cft_Overlay) && ca->bi ) {
5527                         overlay_destroy_one( ca->be, (slap_overinst *)ca->bi );
5528                 } else if ( coptr->co_type == Cft_Schema ) {
5529                         schema_destroy_one( ca, colst, nocs, last );
5530                 }
5531         }
5532 done_noop:
5533
5534         ch_free( ca->argv );
5535         if ( colst ) ch_free( colst );
5536         return rc;
5537 }
5538
5539 #define BIGTMP  10000
5540 static int
5541 config_rename_add( Operation *op, SlapReply *rs, CfEntryInfo *ce,
5542         int base, int rebase, int max, int use_ldif )
5543 {
5544         CfEntryInfo *ce2, *ce3, *cetmp = NULL, *cerem = NULL;
5545         ConfigType etype = ce->ce_type;
5546         int count = 0, rc = 0;
5547
5548         /* Reverse ce list */
5549         for (ce2 = ce->ce_sibs;ce2;ce2 = ce3) {
5550                 if (ce2->ce_type != etype) {
5551                         cerem = ce2;
5552                         break;
5553                 }
5554                 ce3 = ce2->ce_sibs;
5555                 ce2->ce_sibs = cetmp;
5556                 cetmp = ce2;
5557                 count++;
5558                 if ( max && count >= max ) {
5559                         cerem = ce3;
5560                         break;
5561                 }
5562         }
5563
5564         /* Move original to a temp name until increments are done */
5565         if ( rebase ) {
5566                 ce->ce_entry->e_private = NULL;
5567                 rc = config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
5568                         base+BIGTMP, 0, use_ldif );
5569                 ce->ce_entry->e_private = ce;
5570         }
5571         /* start incrementing */
5572         for (ce2=cetmp; ce2; ce2=ce3) {
5573                 ce3 = ce2->ce_sibs;
5574                 ce2->ce_sibs = cerem;
5575                 cerem = ce2;
5576                 if ( rc == 0 ) 
5577                         rc = config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
5578                                 count+base, 0, use_ldif );
5579                 count--;
5580         }
5581         if ( rebase )
5582                 rc = config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
5583                         base, 0, use_ldif );
5584         return rc;
5585 }
5586
5587 static int
5588 config_rename_del( Operation *op, SlapReply *rs, CfEntryInfo *ce,
5589         CfEntryInfo *ce2, int old, int use_ldif )
5590 {
5591         int count = 0;
5592
5593         /* Renumber original to a temp value */
5594         ce->ce_entry->e_private = NULL;
5595         config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
5596                 old+BIGTMP, 0, use_ldif );
5597         ce->ce_entry->e_private = ce;
5598
5599         /* start decrementing */
5600         for (; ce2 != ce; ce2=ce2->ce_sibs) {
5601                 config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
5602                         count+old, 0, use_ldif );
5603                 count++;
5604         }
5605         return config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
5606                 count+old, 0, use_ldif );
5607 }
5608
5609 /* Parse an LDAP entry into config directives, then store in underlying
5610  * database.
5611  */
5612 static int
5613 config_back_add( Operation *op, SlapReply *rs )
5614 {
5615         CfBackInfo *cfb;
5616         int renumber;
5617         ConfigArgs ca;
5618
5619         if ( !access_allowed( op, op->ora_e, slap_schema.si_ad_entry,
5620                 NULL, ACL_WADD, NULL )) {
5621                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5622                 goto out;
5623         }
5624
5625         /*
5626          * Check for attribute ACL
5627          */
5628         if ( !acl_check_modlist( op, op->ora_e, op->orm_modlist )) {
5629                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5630                 rs->sr_text = "no write access to attribute";
5631                 goto out;
5632         }
5633
5634         cfb = (CfBackInfo *)op->o_bd->be_private;
5635
5636         /* add opattrs for syncprov */
5637         {
5638                 char textbuf[SLAP_TEXT_BUFLEN];
5639                 size_t textlen = sizeof textbuf;
5640                 rs->sr_err = entry_schema_check(op, op->ora_e, NULL, 0, 1, NULL,
5641                         &rs->sr_text, textbuf, sizeof( textbuf ) );
5642                 if ( rs->sr_err != LDAP_SUCCESS )
5643                         goto out;
5644                 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
5645                 if ( rs->sr_err != LDAP_SUCCESS ) {
5646                         Debug( LDAP_DEBUG_TRACE,
5647                                 LDAP_XSTRING(config_back_add) ": entry failed op attrs add: "
5648                                 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
5649                         goto out;
5650                 }
5651         }
5652
5653         if ( op->o_abandon ) {
5654                 rs->sr_err = SLAPD_ABANDON;
5655                 goto out;
5656         }
5657         ldap_pvt_thread_pool_pause( &connection_pool );
5658
5659         /* Strategy:
5660          * 1) check for existence of entry
5661          * 2) check for sibling renumbering
5662          * 3) perform internal add
5663          * 4) perform any necessary renumbering
5664          * 5) store entry in underlying database
5665          */
5666         rs->sr_err = config_add_internal( cfb, op->ora_e, &ca, rs, &renumber, op );
5667         if ( rs->sr_err != LDAP_SUCCESS ) {
5668                 rs->sr_text = ca.cr_msg;
5669                 goto out2;
5670         }
5671
5672         if ( renumber ) {
5673                 CfEntryInfo *ce = ca.ca_entry->e_private;
5674                 req_add_s addr = op->oq_add;
5675                 op->o_tag = LDAP_REQ_MODRDN;
5676                 rs->sr_err = config_rename_add( op, rs, ce, ca.valx, 0, 0, cfb->cb_use_ldif );
5677                 op->o_tag = LDAP_REQ_ADD;
5678                 op->oq_add = addr;
5679                 if ( rs->sr_err != LDAP_SUCCESS ) {
5680                         goto out2;
5681                 }
5682         }
5683
5684         if ( cfb->cb_use_ldif ) {
5685                 BackendDB *be = op->o_bd;
5686                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
5687                 struct berval dn, ndn;
5688
5689                 op->o_bd = &cfb->cb_db;
5690
5691                 /* Save current rootdn; use the underlying DB's rootdn */
5692                 dn = op->o_dn;
5693                 ndn = op->o_ndn;
5694                 op->o_dn = op->o_bd->be_rootdn;
5695                 op->o_ndn = op->o_bd->be_rootndn;
5696
5697                 scp = op->o_callback;
5698                 op->o_callback = &sc;
5699                 op->o_bd->be_add( op, rs );
5700                 op->o_bd = be;
5701                 op->o_callback = scp;
5702                 op->o_dn = dn;
5703                 op->o_ndn = ndn;
5704         }
5705
5706 out2:;
5707         ldap_pvt_thread_pool_resume( &connection_pool );
5708
5709 out:;
5710         {       int repl = op->o_dont_replicate;
5711                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
5712                         rs->sr_text = NULL; /* Set after config_add_internal */
5713                         rs->sr_err = LDAP_SUCCESS;
5714                         op->o_dont_replicate = 1;
5715                 }
5716                 send_ldap_result( op, rs );
5717                 op->o_dont_replicate = repl;
5718         }
5719         slap_graduate_commit_csn( op );
5720         return rs->sr_err;
5721 }
5722
5723 typedef struct delrec {
5724         struct delrec *next;
5725         int nidx;
5726         int idx[1];
5727 } delrec;
5728
5729 static int
5730 config_modify_add( ConfigTable *ct, ConfigArgs *ca, AttributeDescription *ad,
5731         int i )
5732 {
5733         int rc;
5734
5735         ca->valx = -1;
5736         if (ad->ad_type->sat_flags & SLAP_AT_ORDERED &&
5737                 ca->line[0] == '{' )
5738         {
5739                 char *ptr = strchr( ca->line + 1, '}' );
5740                 if ( ptr ) {
5741                         char    *next;
5742
5743                         ca->valx = strtol( ca->line + 1, &next, 0 );
5744                         if ( next == ca->line + 1 || next[ 0 ] != '}' ) {
5745                                 return LDAP_OTHER;
5746                         }
5747                         ca->line = ptr+1;
5748                 }
5749         }
5750         rc = config_parse_add( ct, ca, i );
5751         if ( rc ) {
5752                 rc = LDAP_OTHER;
5753         }
5754         return rc;
5755 }
5756
5757 static int
5758 config_modify_internal( CfEntryInfo *ce, Operation *op, SlapReply *rs,
5759         ConfigArgs *ca )
5760 {
5761         int rc = LDAP_UNWILLING_TO_PERFORM;
5762         Modifications *ml;
5763         Entry *e = ce->ce_entry;
5764         Attribute *save_attrs = e->e_attrs, *oc_at, *s, *a;
5765         ConfigTable *ct;
5766         ConfigOCs **colst;
5767         int i, nocs;
5768         char *ptr;
5769         delrec *dels = NULL, *deltail = NULL;
5770
5771         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
5772         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
5773
5774         for (ml = op->orm_modlist; ml; ml=ml->sml_next) {
5775                 if (ml->sml_desc == slap_schema.si_ad_objectClass)
5776                         return rc;
5777         }
5778
5779         colst = count_ocs( oc_at, &nocs );
5780
5781         /* make sure add/del flags are clear; should always be true */
5782         for ( s = save_attrs; s; s = s->a_next ) {
5783                 s->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
5784         }
5785
5786         e->e_attrs = attrs_dup( e->e_attrs );
5787
5788         init_config_argv( ca );
5789         ca->be = ce->ce_be;
5790         ca->bi = ce->ce_bi;
5791         ca->ca_private = ce->ce_private;
5792         ca->ca_entry = e;
5793         ca->fname = "slapd";
5794         ca->ca_op = op;
5795         strcpy( ca->log, "back-config" );
5796
5797         for (ml = op->orm_modlist; ml; ml=ml->sml_next) {
5798                 ct = config_find_table( colst, nocs, ml->sml_desc, ca );
5799                 switch (ml->sml_op) {
5800                 case LDAP_MOD_DELETE:
5801                 case LDAP_MOD_REPLACE:
5802                 case SLAP_MOD_SOFTDEL:
5803                 {
5804                         BerVarray vals = NULL, nvals = NULL;
5805                         int *idx = NULL;
5806                         if ( ct && ( ct->arg_type & ARG_NO_DELETE )) {
5807                                 rc = LDAP_OTHER;
5808                                 snprintf(ca->cr_msg, sizeof(ca->cr_msg), "cannot delete %s",
5809                                         ml->sml_desc->ad_cname.bv_val );
5810                                 goto out_noop;
5811                         }
5812                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5813                                 vals = ml->sml_values;
5814                                 nvals = ml->sml_nvalues;
5815                                 ml->sml_values = NULL;
5816                                 ml->sml_nvalues = NULL;
5817                         }
5818                         /* If we're deleting by values, remember the indexes of the
5819                          * values we deleted.
5820                          */
5821                         if ( ct && ml->sml_values ) {
5822                                 delrec *d;
5823                                 i = ml->sml_numvals;
5824                                 d = ch_malloc( sizeof(delrec) + (i - 1)* sizeof(int));
5825                                 d->nidx = i;
5826                                 d->next = NULL;
5827                                 if ( dels ) {
5828                                         deltail->next = d;
5829                                 } else {
5830                                         dels = d;
5831                                 }
5832                                 deltail = d;
5833                                 idx = d->idx;
5834                         }
5835                         rc = modify_delete_vindex(e, &ml->sml_mod,
5836                                 get_permissiveModify(op),
5837                                 &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg), idx );
5838                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5839                                 ml->sml_values = vals;
5840                                 ml->sml_nvalues = nvals;
5841                         }
5842                         if ( rc == LDAP_NO_SUCH_ATTRIBUTE && ml->sml_op == SLAP_MOD_SOFTDEL )
5843                         {
5844                                 rc = LDAP_SUCCESS;
5845                         }
5846                         /* FIXME: check rc before fallthru? */
5847                         if ( !vals )
5848                                 break;
5849                 }
5850                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
5851
5852                 case SLAP_MOD_ADD_IF_NOT_PRESENT:
5853                         if ( ml->sml_op == SLAP_MOD_ADD_IF_NOT_PRESENT
5854                                 && attr_find( e->e_attrs, ml->sml_desc ) )
5855                         {
5856                                 rc = LDAP_SUCCESS;
5857                                 break;
5858                         }
5859
5860                 case LDAP_MOD_ADD:
5861                 case SLAP_MOD_SOFTADD: {
5862                         int mop = ml->sml_op;
5863                         int navals = -1;
5864                         ml->sml_op = LDAP_MOD_ADD;
5865                         if ( ct ) {
5866                                 if ( ct->arg_type & ARG_NO_INSERT ) {
5867                                         Attribute *a = attr_find( e->e_attrs, ml->sml_desc );
5868                                         if ( a ) {
5869                                                 navals = a->a_numvals;
5870                                         }
5871                                 }
5872                                 for ( i=0; !BER_BVISNULL( &ml->sml_values[i] ); i++ ) {
5873                                         if ( ml->sml_values[i].bv_val[0] == '{' &&
5874                                                 navals >= 0 )
5875                                         {
5876                                                 char    *next, *val = ml->sml_values[i].bv_val + 1;
5877                                                 int     j;
5878
5879                                                 j = strtol( val, &next, 0 );
5880                                                 if ( next == val || next[ 0 ] != '}' || j < navals ) {
5881                                                         rc = LDAP_OTHER;
5882                                                         snprintf(ca->cr_msg, sizeof(ca->cr_msg), "cannot insert %s",
5883                                                                 ml->sml_desc->ad_cname.bv_val );
5884                                                         goto out_noop;
5885                                                 }
5886                                         }
5887                                         rc = check_vals( ct, ca, ml, 0 );
5888                                         if ( rc ) goto out_noop;
5889                                 }
5890                         }
5891                         rc = modify_add_values(e, &ml->sml_mod,
5892                                    get_permissiveModify(op),
5893                                    &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg) );
5894
5895                         /* If value already exists, show success here
5896                          * and ignore this operation down below.
5897                          */
5898                         if ( mop == SLAP_MOD_SOFTADD ) {
5899                                 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS )
5900                                         rc = LDAP_SUCCESS;
5901                                 else
5902                                         mop = LDAP_MOD_ADD;
5903                         }
5904                         ml->sml_op = mop;
5905                         break;
5906                         }
5907
5908                         break;
5909                 case LDAP_MOD_INCREMENT:        /* FIXME */
5910                         break;
5911                 default:
5912                         break;
5913                 }
5914                 if(rc != LDAP_SUCCESS) break;
5915         }
5916         
5917         if ( rc == LDAP_SUCCESS) {
5918                 /* check that the entry still obeys the schema */
5919                 rc = entry_schema_check(op, e, NULL, 0, 0, NULL,
5920                         &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg) );
5921         }
5922         if ( rc ) goto out_noop;
5923
5924         /* Basic syntax checks are OK. Do the actual settings. */
5925         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
5926                 ct = config_find_table( colst, nocs, ml->sml_desc, ca );
5927                 if ( !ct ) continue;
5928
5929                 s = attr_find( save_attrs, ml->sml_desc );
5930                 a = attr_find( e->e_attrs, ml->sml_desc );
5931
5932                 switch (ml->sml_op) {
5933                 case LDAP_MOD_DELETE:
5934                 case LDAP_MOD_REPLACE: {
5935                         BerVarray vals = NULL, nvals = NULL;
5936                         delrec *d = NULL;
5937
5938                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5939                                 vals = ml->sml_values;
5940                                 nvals = ml->sml_nvalues;
5941                                 ml->sml_values = NULL;
5942                                 ml->sml_nvalues = NULL;
5943                         }
5944
5945                         if ( ml->sml_values )
5946                                 d = dels;
5947
5948                         /* If we didn't delete the whole attribute */
5949                         if ( ml->sml_values && a ) {
5950                                 struct berval *mvals;
5951                                 int j;
5952
5953                                 if ( ml->sml_nvalues )
5954                                         mvals = ml->sml_nvalues;
5955                                 else
5956                                         mvals = ml->sml_values;
5957
5958                                 /* use the indexes we saved up above */
5959                                 for (i=0; i < d->nidx; i++) {
5960                                         struct berval bv = *mvals++;
5961                                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED &&
5962                                                 bv.bv_val[0] == '{' ) {
5963                                                 ptr = strchr( bv.bv_val, '}' ) + 1;
5964                                                 bv.bv_len -= ptr - bv.bv_val;
5965                                                 bv.bv_val = ptr;
5966                                         }
5967                                         ca->line = bv.bv_val;
5968                                         ca->valx = d->idx[i];
5969                                         config_parse_vals(ct, ca, d->idx[i] );
5970                                         rc = config_del_vals( ct, ca );
5971                                         if ( rc != LDAP_SUCCESS ) break;
5972                                         if ( s )
5973                                                 s->a_flags |= SLAP_ATTR_IXDEL;
5974                                         for (j=i+1; j < d->nidx; j++)
5975                                                 if ( d->idx[j] >d->idx[i] )
5976                                                         d->idx[j]--;
5977                                 }
5978                         } else {
5979                                 ca->valx = -1;
5980                                 ca->line = NULL;
5981                                 ca->argc = 1;
5982                                 rc = config_del_vals( ct, ca );
5983                                 if ( rc ) rc = LDAP_OTHER;
5984                                 if ( s )
5985                                         s->a_flags |= SLAP_ATTR_IXDEL;
5986                         }
5987                         if ( ml->sml_values ) {
5988                                 d = d->next;
5989                                 ch_free( dels );
5990                                 dels = d;
5991                         }
5992                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5993                                 ml->sml_values = vals;
5994                                 ml->sml_nvalues = nvals;
5995                         }
5996                         if ( !vals || rc != LDAP_SUCCESS )
5997                                 break;
5998                         }
5999                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
6000
6001                 case LDAP_MOD_ADD:
6002                         if ( !a )
6003                                 break;
6004                         for (i=0; ml->sml_values[i].bv_val; i++) {
6005                                 ca->line = ml->sml_values[i].bv_val;
6006                                 ca->valx = -1;
6007                                 rc = config_modify_add( ct, ca, ml->sml_desc, i );
6008                                 if ( rc )
6009                                         goto out;
6010                                 a->a_flags |= SLAP_ATTR_IXADD;
6011                         }
6012                         break;
6013                 }
6014         }
6015
6016 out:
6017         /* Undo for a failed operation */
6018         if ( rc != LDAP_SUCCESS ) {
6019                 ConfigReply msg = ca->reply;
6020                 for ( s = save_attrs; s; s = s->a_next ) {
6021                         if ( s->a_flags & SLAP_ATTR_IXDEL ) {
6022                                 s->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
6023                                 ct = config_find_table( colst, nocs, s->a_desc, ca );
6024                                 a = attr_find( e->e_attrs, s->a_desc );
6025                                 if ( a ) {
6026                                         /* clear the flag so the add check below will skip it */
6027                                         a->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
6028                                         ca->valx = -1;
6029                                         ca->line = NULL;
6030                                         ca->argc = 1;
6031                                         config_del_vals( ct, ca );
6032                                 }
6033                                 for ( i=0; !BER_BVISNULL( &s->a_vals[i] ); i++ ) {
6034                                         ca->line = s->a_vals[i].bv_val;
6035                                         ca->valx = -1;
6036                                         config_modify_add( ct, ca, s->a_desc, i );
6037                                 }
6038                         }
6039                 }
6040                 for ( a = e->e_attrs; a; a = a->a_next ) {
6041                         if ( a->a_flags & SLAP_ATTR_IXADD ) {
6042                                 ct = config_find_table( colst, nocs, a->a_desc, ca );
6043                                 ca->valx = -1;
6044                                 ca->line = NULL;
6045                                 ca->argc = 1;
6046                                 config_del_vals( ct, ca );
6047                                 s = attr_find( save_attrs, a->a_desc );
6048                                 if ( s ) {
6049                                         s->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
6050                                         for ( i=0; !BER_BVISNULL( &s->a_vals[i] ); i++ ) {
6051                                                 ca->line = s->a_vals[i].bv_val;
6052                                                 ca->valx = -1;
6053                                                 config_modify_add( ct, ca, s->a_desc, i );
6054                                         }
6055                                 }
6056                         }
6057                 }
6058                 ca->reply = msg;
6059         }
6060
6061         if ( ca->cleanup ) {
6062                 i = ca->cleanup( ca );
6063                 if (rc == LDAP_SUCCESS)
6064                         rc = i;
6065         }
6066 out_noop:
6067         if ( rc == LDAP_SUCCESS ) {
6068                 attrs_free( save_attrs );
6069                 rs->sr_text = NULL;
6070         } else {
6071                 attrs_free( e->e_attrs );
6072                 e->e_attrs = save_attrs;
6073         }
6074         ch_free( ca->argv );
6075         if ( colst ) ch_free( colst );
6076         while( dels ) {
6077                 deltail = dels->next;
6078                 ch_free( dels );
6079                 dels = deltail;
6080         }
6081
6082         return rc;
6083 }
6084
6085 static int
6086 config_back_modify( Operation *op, SlapReply *rs )
6087 {
6088         CfBackInfo *cfb;
6089         CfEntryInfo *ce, *last;
6090         Modifications *ml;
6091         ConfigArgs ca = {0};
6092         struct berval rdn;
6093         char *ptr;
6094         AttributeDescription *rad = NULL;
6095         int do_pause = 1;
6096
6097         cfb = (CfBackInfo *)op->o_bd->be_private;
6098
6099         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
6100         if ( !ce ) {
6101                 if ( last )
6102                         rs->sr_matched = last->ce_entry->e_name.bv_val;
6103                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
6104                 goto out;
6105         }
6106
6107         if ( !acl_check_modlist( op, ce->ce_entry, op->orm_modlist )) {
6108                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
6109                 goto out;
6110         }
6111
6112         /* Get type of RDN */
6113         rdn = ce->ce_entry->e_nname;
6114         ptr = strchr( rdn.bv_val, '=' );
6115         rdn.bv_len = ptr - rdn.bv_val;
6116         rs->sr_err = slap_bv2ad( &rdn, &rad, &rs->sr_text );
6117         if ( rs->sr_err != LDAP_SUCCESS ) {
6118                 goto out;
6119         }
6120
6121         /* Some basic validation... */
6122         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
6123                 /* Don't allow Modify of RDN; must use ModRdn for that. */
6124                 if ( ml->sml_desc == rad ) {
6125                         rs->sr_err = LDAP_NOT_ALLOWED_ON_RDN;
6126                         rs->sr_text = "Use modrdn to change the entry name";
6127                         goto out;
6128                 }
6129                 /* Internal update of contextCSN? */
6130                 if ( ml->sml_desc == slap_schema.si_ad_contextCSN && op->o_conn->c_conn_idx == -1 ) {
6131                         do_pause = 0;
6132                         break;
6133                 }
6134         }
6135
6136         slap_mods_opattrs( op, &op->orm_modlist, 1 );
6137
6138         if ( do_pause ) {
6139                 if ( op->o_abandon ) {
6140                         rs->sr_err = SLAPD_ABANDON;
6141                         goto out;
6142                 }
6143                 ldap_pvt_thread_pool_pause( &connection_pool );
6144         }
6145
6146         /* Strategy:
6147          * 1) perform the Modify on the cached Entry.
6148          * 2) verify that the Entry still satisfies the schema.
6149          * 3) perform the individual config operations.
6150          * 4) store Modified entry in underlying LDIF backend.
6151          */
6152         rs->sr_err = config_modify_internal( ce, op, rs, &ca );
6153         if ( rs->sr_err ) {
6154                 rs->sr_text = ca.cr_msg;
6155         } else if ( cfb->cb_use_ldif ) {
6156                 BackendDB *be = op->o_bd;
6157                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
6158                 struct berval dn, ndn;
6159
6160                 op->o_bd = &cfb->cb_db;
6161
6162                 dn = op->o_dn;
6163                 ndn = op->o_ndn;
6164                 op->o_dn = op->o_bd->be_rootdn;
6165                 op->o_ndn = op->o_bd->be_rootndn;
6166
6167                 scp = op->o_callback;
6168                 op->o_callback = &sc;
6169                 op->o_bd->be_modify( op, rs );
6170                 op->o_bd = be;
6171                 op->o_callback = scp;
6172                 op->o_dn = dn;
6173                 op->o_ndn = ndn;
6174         }
6175
6176         if ( do_pause )
6177                 ldap_pvt_thread_pool_resume( &connection_pool );
6178 out:
6179         send_ldap_result( op, rs );
6180         slap_graduate_commit_csn( op );
6181         return rs->sr_err;
6182 }
6183
6184 static int
6185 config_back_modrdn( Operation *op, SlapReply *rs )
6186 {
6187         CfBackInfo *cfb;
6188         CfEntryInfo *ce, *last;
6189         struct berval rdn;
6190         int ixold, ixnew;
6191
6192         cfb = (CfBackInfo *)op->o_bd->be_private;
6193
6194         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
6195         if ( !ce ) {
6196                 if ( last )
6197                         rs->sr_matched = last->ce_entry->e_name.bv_val;
6198                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
6199                 goto out;
6200         }
6201         if ( !access_allowed( op, ce->ce_entry, slap_schema.si_ad_entry,
6202                 NULL, ACL_WRITE, NULL )) {
6203                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
6204                 goto out;
6205         }
6206         { Entry *parent;
6207                 if ( ce->ce_parent )
6208                         parent = ce->ce_parent->ce_entry;
6209                 else
6210                         parent = (Entry *)&slap_entry_root;
6211                 if ( !access_allowed( op, parent, slap_schema.si_ad_children,
6212                         NULL, ACL_WRITE, NULL )) {
6213                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
6214                         goto out;
6215                 }
6216         }
6217
6218         /* We don't allow moving objects to new parents.
6219          * Generally we only allow reordering a set of ordered entries.
6220          */
6221         if ( op->orr_newSup ) {
6222                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6223                 goto out;
6224         }
6225
6226         /* If newRDN == oldRDN, quietly succeed */
6227         dnRdn( &op->o_req_ndn, &rdn );
6228         if ( dn_match( &rdn, &op->orr_nnewrdn )) {
6229                 rs->sr_err = LDAP_SUCCESS;
6230                 goto out;
6231         }
6232
6233         /* Current behavior, subject to change as needed:
6234          *
6235          * For backends and overlays, we only allow renumbering.
6236          * For schema, we allow renaming with the same number.
6237          * Otherwise, the op is not allowed.
6238          */
6239
6240         if ( ce->ce_type == Cft_Schema ) {
6241                 char *ptr1, *ptr2;
6242                 int len;
6243
6244                 /* Can't alter the main cn=schema entry */
6245                 if ( ce->ce_parent->ce_type == Cft_Global ) {
6246                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6247                         rs->sr_text = "renaming not allowed for this entry";
6248                         goto out;
6249                 }
6250
6251                 /* We could support this later if desired */
6252                 ptr1 = ber_bvchr( &rdn, '}' );
6253                 ptr2 = ber_bvchr( &op->orr_newrdn, '}' );
6254                 len = ptr1 - rdn.bv_val;
6255                 if ( len != ptr2 - op->orr_newrdn.bv_val ||
6256                         strncmp( rdn.bv_val, op->orr_newrdn.bv_val, len )) {
6257                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6258                         rs->sr_text = "schema reordering not supported";
6259                         goto out;
6260                 }
6261         } else if ( ce->ce_type == Cft_Database ||
6262                 ce->ce_type == Cft_Overlay ) {
6263                 char *ptr1, *ptr2, *iptr1, *iptr2;
6264                 int len1, len2;
6265
6266                 iptr2 = ber_bvchr( &op->orr_newrdn, '=' ) + 1;
6267                 if ( *iptr2 != '{' ) {
6268                         rs->sr_err = LDAP_NAMING_VIOLATION;
6269                         rs->sr_text = "new ordering index is required";
6270                         goto out;
6271                 }
6272                 iptr2++;
6273                 iptr1 = ber_bvchr( &rdn, '{' ) + 1;
6274                 ptr1 = ber_bvchr( &rdn, '}' );
6275                 ptr2 = ber_bvchr( &op->orr_newrdn, '}' );
6276                 if ( !ptr2 ) {
6277                         rs->sr_err = LDAP_NAMING_VIOLATION;
6278                         rs->sr_text = "new ordering index is required";
6279                         goto out;
6280                 }
6281
6282                 len1 = ptr1 - rdn.bv_val;
6283                 len2 = ptr2 - op->orr_newrdn.bv_val;
6284
6285                 if ( rdn.bv_len - len1 != op->orr_newrdn.bv_len - len2 ||
6286                         strncmp( ptr1, ptr2, rdn.bv_len - len1 )) {
6287                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6288                         rs->sr_text = "changing database/overlay type not allowed";
6289                         goto out;
6290                 }
6291                 ixold = strtol( iptr1, NULL, 0 );
6292                 ixnew = strtol( iptr2, &ptr1, 0 );
6293                 if ( ptr1 != ptr2 || ixold < 0 || ixnew < 0 ) {
6294                         rs->sr_err = LDAP_NAMING_VIOLATION;
6295                         goto out;
6296                 }
6297                 /* config DB is always 0, cannot be changed */
6298                 if ( ce->ce_type == Cft_Database && ( ixold == 0 || ixnew == 0 )) {
6299                         rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
6300                         goto out;
6301                 }
6302         } else {
6303                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6304                 rs->sr_text = "renaming not supported for this entry";
6305                 goto out;
6306         }
6307
6308         if ( op->o_abandon ) {
6309                 rs->sr_err = SLAPD_ABANDON;
6310                 goto out;
6311         }
6312         ldap_pvt_thread_pool_pause( &connection_pool );
6313
6314         if ( ce->ce_type == Cft_Schema ) {
6315                 req_modrdn_s modr = op->oq_modrdn;
6316                 struct berval rdn;
6317                 Attribute *a;
6318                 rs->sr_err = config_rename_attr( rs, ce->ce_entry, &rdn, &a );
6319                 if ( rs->sr_err == LDAP_SUCCESS ) {
6320                         rs->sr_err = config_rename_one( op, rs, ce->ce_entry,
6321                                 ce->ce_parent, a, &op->orr_newrdn, &op->orr_nnewrdn,
6322                                 cfb->cb_use_ldif );
6323                 }
6324                 op->oq_modrdn = modr;
6325         } else {
6326                 CfEntryInfo *ce2, *cebase, **cprev, **cbprev, *ceold;
6327                 req_modrdn_s modr = op->oq_modrdn;
6328                 int i;
6329
6330                 /* Advance to first of this type */
6331                 cprev = &ce->ce_parent->ce_kids;
6332                 for ( ce2 = *cprev; ce2 && ce2->ce_type != ce->ce_type; ) {
6333                         cprev = &ce2->ce_sibs;
6334                         ce2 = ce2->ce_sibs;
6335                 }
6336                 /* Skip the -1 entry */
6337                 if ( ce->ce_type == Cft_Database ) {
6338                         cprev = &ce2->ce_sibs;
6339                         ce2 = ce2->ce_sibs;
6340                 }
6341                 cebase = ce2;
6342                 cbprev = cprev;
6343
6344                 /* Remove from old slot */
6345                 for ( ce2 = *cprev; ce2 && ce2 != ce; ce2 = ce2->ce_sibs )
6346                         cprev = &ce2->ce_sibs;
6347                 *cprev = ce->ce_sibs;
6348                 ceold = ce->ce_sibs;
6349
6350                 /* Insert into new slot */
6351                 cprev = cbprev;
6352                 for ( i=0; i<ixnew; i++ ) {
6353                         ce2 = *cprev;
6354                         if ( !ce2 )
6355                                 break;
6356                         cprev = &ce2->ce_sibs;
6357                 }
6358                 ce->ce_sibs = *cprev;
6359                 *cprev = ce;
6360
6361                 ixnew = i;
6362
6363                 /* NOTE: These should be encoded in the OC tables, not inline here */
6364                 if ( ce->ce_type == Cft_Database )
6365                         backend_db_move( ce->ce_be, ixnew );
6366                 else if ( ce->ce_type == Cft_Overlay )
6367                         overlay_move( ce->ce_be, (slap_overinst *)ce->ce_bi, ixnew );
6368                         
6369                 if ( ixold < ixnew ) {
6370                         rs->sr_err = config_rename_del( op, rs, ce, ceold, ixold,
6371                                 cfb->cb_use_ldif );
6372                 } else {
6373                         rs->sr_err = config_rename_add( op, rs, ce, ixnew, 1,
6374                                 ixold - ixnew, cfb->cb_use_ldif );
6375                 }
6376                 op->oq_modrdn = modr;
6377         }
6378
6379         ldap_pvt_thread_pool_resume( &connection_pool );
6380 out:
6381         send_ldap_result( op, rs );
6382         return rs->sr_err;
6383 }
6384
6385 static int
6386 config_back_delete( Operation *op, SlapReply *rs )
6387 {
6388 #ifdef SLAP_CONFIG_DELETE
6389         CfBackInfo *cfb;
6390         CfEntryInfo *ce, *last, *ce2;
6391
6392         cfb = (CfBackInfo *)op->o_bd->be_private;
6393
6394         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
6395         if ( !ce ) {
6396                 if ( last )
6397                         rs->sr_matched = last->ce_entry->e_name.bv_val;
6398                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
6399         } else if ( ce->ce_kids ) {
6400                 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
6401         } else if ( op->o_abandon ) {
6402                 rs->sr_err = SLAPD_ABANDON;
6403         } else if ( ce->ce_type == Cft_Overlay ||
6404                         ce->ce_type == Cft_Database ||
6405                         ce->ce_type == Cft_Misc ){
6406                 char *iptr;
6407                 int count, ixold;
6408
6409                 ldap_pvt_thread_pool_pause( &connection_pool );
6410
6411                 if ( ce->ce_type == Cft_Overlay ){
6412                         overlay_remove( ce->ce_be, (slap_overinst *)ce->ce_bi, op );
6413                 } else if ( ce->ce_type == Cft_Misc ) {
6414                         /*
6415                          * only Cft_Misc objects that have a co_lddel handler set in
6416                          * the ConfigOCs struct can be deleted. This code also
6417                          * assumes that the entry can be only have one objectclass
6418                          * with co_type == Cft_Misc
6419                          */
6420                         ConfigOCs co, *coptr;
6421                         Attribute *oc_at;
6422                         int i;
6423
6424                         oc_at = attr_find( ce->ce_entry->e_attrs,
6425                                         slap_schema.si_ad_objectClass );
6426                         if ( !oc_at ) {
6427                                 rs->sr_err = LDAP_OTHER;
6428                                 rs->sr_text = "objectclass not found";
6429                                 ldap_pvt_thread_pool_resume( &connection_pool );
6430                                 goto out;
6431                         }
6432                         for ( i=0; !BER_BVISNULL(&oc_at->a_nvals[i]); i++ ) {
6433                                 co.co_name = &oc_at->a_nvals[i];
6434                                 coptr = avl_find( CfOcTree, &co, CfOc_cmp );
6435                                 if ( coptr == NULL || coptr->co_type != Cft_Misc ) {
6436                                         continue;
6437                                 }
6438                                 if ( ! coptr->co_lddel || coptr->co_lddel( ce, op ) ){
6439                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6440                                         if ( ! coptr->co_lddel ) {
6441                                                 rs->sr_text = "No delete handler found";
6442                                         } else {
6443                                                 rs->sr_err = LDAP_OTHER;
6444                                                 /* FIXME: We should return a helpful error message
6445                                                  * here */
6446                                         }
6447                                         ldap_pvt_thread_pool_resume( &connection_pool );
6448                                         goto out;
6449                                 }
6450                                 break;
6451                         }
6452                 } else if (ce->ce_type == Cft_Database ) {
6453                         if ( ce->ce_be == frontendDB || ce->ce_be == op->o_bd ){
6454                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6455                                 rs->sr_text = "Cannot delete config or frontend database";
6456                                 ldap_pvt_thread_pool_resume( &connection_pool );
6457                                 goto out;
6458                         }
6459                         if ( ce->ce_be->bd_info->bi_db_close ) {
6460                                 ce->ce_be->bd_info->bi_db_close( ce->ce_be, NULL );
6461                         }
6462                         backend_destroy_one( ce->ce_be, 1);
6463                 }
6464
6465                 /* remove CfEntryInfo from the siblings list */
6466                 if ( ce->ce_parent->ce_kids == ce ) {
6467                         ce->ce_parent->ce_kids = ce->ce_sibs;
6468                 } else {
6469                         for ( ce2 = ce->ce_parent->ce_kids ; ce2; ce2 = ce2->ce_sibs ) {
6470                                 if ( ce2->ce_sibs == ce ) {
6471                                         ce2->ce_sibs = ce->ce_sibs;
6472                                         break;
6473                                 }
6474                         }
6475                 }
6476
6477                 /* remove from underlying database */
6478                 if ( cfb->cb_use_ldif ) {
6479                         BackendDB *be = op->o_bd;
6480                         slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
6481                         struct berval dn, ndn, req_dn, req_ndn;
6482
6483                         op->o_bd = &cfb->cb_db;
6484
6485                         dn = op->o_dn;
6486                         ndn = op->o_ndn;
6487                         req_dn = op->o_req_dn;
6488                         req_ndn = op->o_req_ndn;
6489
6490                         op->o_dn = op->o_bd->be_rootdn;
6491                         op->o_ndn = op->o_bd->be_rootndn;
6492                         op->o_req_dn = ce->ce_entry->e_name;
6493                         op->o_req_ndn = ce->ce_entry->e_nname;
6494
6495                         scp = op->o_callback;
6496                         op->o_callback = &sc;
6497                         op->o_bd->be_delete( op, rs );
6498                         op->o_bd = be;
6499                         op->o_callback = scp;
6500                         op->o_dn = dn;
6501                         op->o_ndn = ndn;
6502                         op->o_req_dn = req_dn;
6503                         op->o_req_ndn = req_ndn;
6504                 }
6505
6506                 /* renumber siblings */
6507                 iptr = ber_bvchr( &op->o_req_ndn, '{' ) + 1;
6508                 ixold = strtol( iptr, NULL, 0 );
6509                 for (ce2 = ce->ce_sibs, count=0; ce2; ce2=ce2->ce_sibs) {
6510                         config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
6511                                 count+ixold, 0, cfb->cb_use_ldif );
6512                         count++;
6513                 }
6514
6515                 ce->ce_entry->e_private=NULL;
6516                 entry_free(ce->ce_entry);
6517                 ch_free(ce);
6518                 ldap_pvt_thread_pool_resume( &connection_pool );
6519         } else {
6520                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6521         }
6522 out:
6523 #else
6524         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6525 #endif /* SLAP_CONFIG_DELETE */
6526         send_ldap_result( op, rs );
6527         return rs->sr_err;
6528 }
6529
6530 static int
6531 config_back_search( Operation *op, SlapReply *rs )
6532 {
6533         CfBackInfo *cfb;
6534         CfEntryInfo *ce, *last;
6535         slap_mask_t mask;
6536
6537         cfb = (CfBackInfo *)op->o_bd->be_private;
6538
6539         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
6540         if ( !ce ) {
6541                 if ( last )
6542                         rs->sr_matched = last->ce_entry->e_name.bv_val;
6543                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
6544                 goto out;
6545         }
6546         if ( !access_allowed_mask( op, ce->ce_entry, slap_schema.si_ad_entry, NULL,
6547                 ACL_SEARCH, NULL, &mask ))
6548         {
6549                 if ( !ACL_GRANT( mask, ACL_DISCLOSE )) {
6550                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
6551                 } else {
6552                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
6553                 }
6554                 goto out;
6555         }
6556         switch ( op->ors_scope ) {
6557         case LDAP_SCOPE_BASE:
6558         case LDAP_SCOPE_SUBTREE:
6559                 rs->sr_err = config_send( op, rs, ce, 0 );
6560                 break;
6561                 
6562         case LDAP_SCOPE_ONELEVEL:
6563                 for (ce = ce->ce_kids; ce; ce=ce->ce_sibs) {
6564                         rs->sr_err = config_send( op, rs, ce, 1 );
6565                         if ( rs->sr_err ) {
6566                                 break;
6567                         }
6568                 }
6569                 break;
6570         }
6571
6572 out:
6573         send_ldap_result( op, rs );
6574         return rs->sr_err;
6575 }
6576
6577 /* no-op, we never free entries */
6578 int config_entry_release(
6579         Operation *op,
6580         Entry *e,
6581         int rw )
6582 {
6583         if ( !e->e_private ) {
6584                 entry_free( e );
6585         }
6586         return LDAP_SUCCESS;
6587 }
6588
6589 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
6590  */
6591 int config_back_entry_get(
6592         Operation *op,
6593         struct berval *ndn,
6594         ObjectClass *oc,
6595         AttributeDescription *at,
6596         int rw,
6597         Entry **ent )
6598 {
6599         CfBackInfo *cfb;
6600         CfEntryInfo *ce, *last;
6601         int rc = LDAP_NO_SUCH_OBJECT;
6602
6603         cfb = (CfBackInfo *)op->o_bd->be_private;
6604
6605         ce = config_find_base( cfb->cb_root, ndn, &last );
6606         if ( ce ) {
6607                 *ent = ce->ce_entry;
6608                 if ( *ent ) {
6609                         rc = LDAP_SUCCESS;
6610                         if ( oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
6611                                 rc = LDAP_NO_SUCH_ATTRIBUTE;
6612                                 *ent = NULL;
6613                         }
6614                 }
6615         }
6616
6617         return rc;
6618 }
6619
6620 static int
6621 config_build_attrs( Entry *e, AttributeType **at, AttributeDescription *ad,
6622         ConfigTable *ct, ConfigArgs *c )
6623 {
6624         int i, rc;
6625
6626         for (; at && *at; at++) {
6627                 /* Skip the naming attr */
6628                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
6629                         continue;
6630                 for (i=0;ct[i].name;i++) {
6631                         if (ct[i].ad == (*at)->sat_ad) {
6632                                 rc = config_get_vals(&ct[i], c);
6633                                 /* NOTE: tolerate that config_get_vals()
6634                                  * returns success with no values */
6635                                 if (rc == LDAP_SUCCESS && c->rvalue_vals != NULL ) {
6636                                         if ( c->rvalue_nvals )
6637                                                 rc = attr_merge(e, ct[i].ad, c->rvalue_vals,
6638                                                         c->rvalue_nvals);
6639                                         else {
6640                                                 slap_syntax_validate_func *validate =
6641                                                         ct[i].ad->ad_type->sat_syntax->ssyn_validate;
6642                                                 if ( validate ) {
6643                                                         int j;
6644                                                         for ( j=0; c->rvalue_vals[j].bv_val; j++ ) {
6645                                                                 rc = ordered_value_validate( ct[i].ad,
6646                                                                         &c->rvalue_vals[j], LDAP_MOD_ADD );
6647                                                                 if ( rc ) {
6648                                                                         Debug( LDAP_DEBUG_ANY,
6649                                                                                 "config_build_attrs: error %d on %s value #%d\n",
6650                                                                                 rc, ct[i].ad->ad_cname.bv_val, j );
6651                                                                         return rc;
6652                                                                 }
6653                                                         }
6654                                                 }
6655                                                         
6656                                                 rc = attr_merge_normalize(e, ct[i].ad,
6657                                                         c->rvalue_vals, NULL);
6658                                         }
6659                                         ber_bvarray_free( c->rvalue_nvals );
6660                                         ber_bvarray_free( c->rvalue_vals );
6661                                         if ( rc ) {
6662                                                 Debug( LDAP_DEBUG_ANY,
6663                                                         "config_build_attrs: error %d on %s\n",
6664                                                         rc, ct[i].ad->ad_cname.bv_val, 0 );
6665                                                 return rc;
6666                                         }
6667                                 }
6668                                 break;
6669                         }
6670                 }
6671         }
6672         return 0;
6673 }
6674
6675 /* currently (2010) does not access rs except possibly writing rs->sr_err */
6676
6677 Entry *
6678 config_build_entry( Operation *op, SlapReply *rs, CfEntryInfo *parent,
6679         ConfigArgs *c, struct berval *rdn, ConfigOCs *main, ConfigOCs *extra )
6680 {
6681         Entry *e = entry_alloc();
6682         CfEntryInfo *ce = ch_calloc( 1, sizeof(CfEntryInfo) );
6683         struct berval val;
6684         struct berval ad_name;
6685         AttributeDescription *ad = NULL;
6686         int rc;
6687         char *ptr;
6688         const char *text = "";
6689         Attribute *oc_at;
6690         struct berval pdn;
6691         ObjectClass *oc;
6692         CfEntryInfo *ceprev = NULL;
6693
6694         Debug( LDAP_DEBUG_TRACE, "config_build_entry: \"%s\"\n", rdn->bv_val, 0, 0);
6695         e->e_private = ce;
6696         ce->ce_entry = e;
6697         ce->ce_type = main->co_type;
6698         ce->ce_parent = parent;
6699         if ( parent ) {
6700                 pdn = parent->ce_entry->e_nname;
6701                 if ( parent->ce_kids && parent->ce_kids->ce_type <= ce->ce_type )
6702                         for ( ceprev = parent->ce_kids; ceprev->ce_sibs &&
6703                                 ceprev->ce_type <= ce->ce_type;
6704                                 ceprev = ceprev->ce_sibs );
6705         } else {
6706                 BER_BVZERO( &pdn );
6707         }
6708
6709         ce->ce_private = c->ca_private;
6710         ce->ce_be = c->be;
6711         ce->ce_bi = c->bi;
6712
6713         build_new_dn( &e->e_name, &pdn, rdn, NULL );
6714         ber_dupbv( &e->e_nname, &e->e_name );
6715
6716         attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
6717                 main->co_name, NULL );
6718         if ( extra )
6719                 attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
6720                         extra->co_name, NULL );
6721         ptr = strchr(rdn->bv_val, '=');
6722         ad_name.bv_val = rdn->bv_val;
6723         ad_name.bv_len = ptr - rdn->bv_val;
6724         rc = slap_bv2ad( &ad_name, &ad, &text );
6725         if ( rc ) {
6726                 goto fail;
6727         }
6728         val.bv_val = ptr+1;
6729         val.bv_len = rdn->bv_len - (val.bv_val - rdn->bv_val);
6730         attr_merge_normalize_one(e, ad, &val, NULL );
6731
6732         oc = main->co_oc;
6733         c->table = main->co_type;
6734         if ( oc->soc_required ) {
6735                 rc = config_build_attrs( e, oc->soc_required, ad, main->co_table, c );
6736                 if ( rc ) goto fail;
6737         }
6738
6739         if ( oc->soc_allowed ) {
6740                 rc = config_build_attrs( e, oc->soc_allowed, ad, main->co_table, c );
6741                 if ( rc ) goto fail;
6742         }
6743
6744         if ( extra ) {
6745                 oc = extra->co_oc;
6746                 c->table = extra->co_type;
6747                 if ( oc->soc_required ) {
6748                         rc = config_build_attrs( e, oc->soc_required, ad, extra->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, extra->co_table, c );
6754                         if ( rc ) goto fail;
6755                 }
6756         }
6757
6758         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
6759         rc = structural_class(oc_at->a_vals, &oc, NULL, &text, c->cr_msg,
6760                 sizeof(c->cr_msg), op ? op->o_tmpmemctx : NULL );
6761         if ( rc != LDAP_SUCCESS ) {
6762 fail:
6763                 Debug( LDAP_DEBUG_ANY,
6764                         "config_build_entry: build \"%s\" failed: \"%s\"\n",
6765                         rdn->bv_val, text, 0);
6766                 return NULL;
6767         }
6768         attr_merge_normalize_one(e, slap_schema.si_ad_structuralObjectClass, &oc->soc_cname, NULL );
6769         if ( op ) {
6770                 op->ora_e = e;
6771                 op->ora_modlist = NULL;
6772                 slap_add_opattrs( op, NULL, NULL, 0, 0 );
6773                 if ( !op->o_noop ) {
6774                         SlapReply rs2 = {REP_RESULT};
6775                         op->o_bd->be_add( op, &rs2 );
6776                         rs->sr_err = rs2.sr_err;
6777                         rs_assert_done( &rs2 );
6778                         if ( ( rs2.sr_err != LDAP_SUCCESS ) 
6779                                         && (rs2.sr_err != LDAP_ALREADY_EXISTS) ) {
6780                                 goto fail;
6781                         }
6782                 }
6783         }
6784         if ( ceprev ) {
6785                 ce->ce_sibs = ceprev->ce_sibs;
6786                 ceprev->ce_sibs = ce;
6787         } else if ( parent ) {
6788                 ce->ce_sibs = parent->ce_kids;
6789                 parent->ce_kids = ce;
6790         }
6791
6792         return e;
6793 }
6794
6795 static int
6796 config_build_schema_inc( ConfigArgs *c, CfEntryInfo *ceparent,
6797         Operation *op, SlapReply *rs )
6798 {
6799         Entry *e;
6800         ConfigFile *cf = c->ca_private;
6801         char *ptr;
6802         struct berval bv, rdn;
6803
6804         for (; cf; cf=cf->c_sibs, c->depth++) {
6805                 if ( !cf->c_at_head && !cf->c_cr_head && !cf->c_oc_head &&
6806                         !cf->c_om_head && !cf->c_syn_head && !cf->c_kids ) continue;
6807                 c->value_dn.bv_val = c->log;
6808                 LUTIL_SLASHPATH( cf->c_file.bv_val );
6809                 bv.bv_val = strrchr(cf->c_file.bv_val, LDAP_DIRSEP[0]);
6810                 if ( !bv.bv_val ) {
6811                         bv = cf->c_file;
6812                 } else {
6813                         bv.bv_val++;
6814                         bv.bv_len = cf->c_file.bv_len - (bv.bv_val - cf->c_file.bv_val);
6815                 }
6816                 ptr = strchr( bv.bv_val, '.' );
6817                 if ( ptr )
6818                         bv.bv_len = ptr - bv.bv_val;
6819                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=" SLAP_X_ORDERED_FMT, c->depth);
6820                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
6821                         /* FIXME: how can indicate error? */
6822                         return -1;
6823                 }
6824                 strncpy( c->value_dn.bv_val + c->value_dn.bv_len, bv.bv_val,
6825                         bv.bv_len );
6826                 c->value_dn.bv_len += bv.bv_len;
6827                 c->value_dn.bv_val[c->value_dn.bv_len] ='\0';
6828                 rdnNormalize( 0, NULL, NULL, &c->value_dn, &rdn, NULL );
6829
6830                 c->ca_private = cf;
6831                 e = config_build_entry( op, rs, ceparent, c, &rdn,
6832                         &CFOC_SCHEMA, NULL );
6833                 ch_free( rdn.bv_val );
6834                 if ( !e ) {
6835                         return -1;
6836                 } else if ( e && cf->c_kids ) {
6837                         c->ca_private = cf->c_kids;
6838                         config_build_schema_inc( c, e->e_private, op, rs );
6839                 }
6840         }
6841         return 0;
6842 }
6843
6844 #ifdef SLAPD_MODULES
6845
6846 static int
6847 config_build_modules( ConfigArgs *c, CfEntryInfo *ceparent,
6848         Operation *op, SlapReply *rs )
6849 {
6850         int i;
6851         ModPaths *mp;
6852
6853         for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
6854                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
6855                         continue;
6856                 c->value_dn.bv_val = c->log;
6857                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=module" SLAP_X_ORDERED_FMT, i);
6858                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
6859                         /* FIXME: how can indicate error? */
6860                         return -1;
6861                 }
6862                 c->ca_private = mp;
6863                 if ( ! config_build_entry( op, rs, ceparent, c, &c->value_dn, &CFOC_MODULE, NULL )) {
6864                         return -1;
6865                 }
6866         }
6867         return 0;
6868 }
6869 #endif
6870
6871 static int
6872 config_check_schema(Operation *op, CfBackInfo *cfb)
6873 {
6874         struct berval schema_dn = BER_BVC(SCHEMA_RDN "," CONFIG_RDN);
6875         ConfigArgs c = {0};
6876         CfEntryInfo *ce, *last;
6877         Entry *e;
6878
6879         /* If there's no root entry, we must be in the midst of converting */
6880         if ( !cfb->cb_root )
6881                 return 0;
6882
6883         /* Make sure the main schema entry exists */
6884         ce = config_find_base( cfb->cb_root, &schema_dn, &last );
6885         if ( ce ) {
6886                 Attribute *a;
6887                 struct berval *bv;
6888
6889                 e = ce->ce_entry;
6890
6891                 /* Make sure it's up to date */
6892                 if ( cf_om_tail != om_sys_tail ) {
6893                         a = attr_find( e->e_attrs, cfAd_om );
6894                         if ( a ) {
6895                                 if ( a->a_nvals != a->a_vals )
6896                                         ber_bvarray_free( a->a_nvals );
6897                                 ber_bvarray_free( a->a_vals );
6898                                 a->a_vals = NULL;
6899                                 a->a_nvals = NULL;
6900                                 a->a_numvals = 0;
6901                         }
6902                         oidm_unparse( &bv, NULL, NULL, 1 );
6903                         attr_merge_normalize( e, cfAd_om, bv, NULL );
6904                         ber_bvarray_free( bv );
6905                         cf_om_tail = om_sys_tail;
6906                 }
6907                 if ( cf_at_tail != at_sys_tail ) {
6908                         a = attr_find( e->e_attrs, cfAd_attr );
6909                         if ( a ) {
6910                                 if ( a->a_nvals != a->a_vals )
6911                                         ber_bvarray_free( a->a_nvals );
6912                                 ber_bvarray_free( a->a_vals );
6913                                 a->a_vals = NULL;
6914                                 a->a_nvals = NULL;
6915                                 a->a_numvals = 0;
6916                         }
6917                         at_unparse( &bv, NULL, NULL, 1 );
6918                         attr_merge_normalize( e, cfAd_attr, bv, NULL );
6919                         ber_bvarray_free( bv );
6920                         cf_at_tail = at_sys_tail;
6921                 }
6922                 if ( cf_oc_tail != oc_sys_tail ) {
6923                         a = attr_find( e->e_attrs, cfAd_oc );
6924                         if ( a ) {
6925                                 if ( a->a_nvals != a->a_vals )
6926                                         ber_bvarray_free( a->a_nvals );
6927                                 ber_bvarray_free( a->a_vals );
6928                                 a->a_vals = NULL;
6929                                 a->a_nvals = NULL;
6930                                 a->a_numvals = 0;
6931                         }
6932                         oc_unparse( &bv, NULL, NULL, 1 );
6933                         attr_merge_normalize( e, cfAd_oc, bv, NULL );
6934                         ber_bvarray_free( bv );
6935                         cf_oc_tail = oc_sys_tail;
6936                 }
6937                 if ( cf_syn_tail != syn_sys_tail ) {
6938                         a = attr_find( e->e_attrs, cfAd_syntax );
6939                         if ( a ) {
6940                                 if ( a->a_nvals != a->a_vals )
6941                                         ber_bvarray_free( a->a_nvals );
6942                                 ber_bvarray_free( a->a_vals );
6943                                 a->a_vals = NULL;
6944                                 a->a_nvals = NULL;
6945                                 a->a_numvals = 0;
6946                         }
6947                         syn_unparse( &bv, NULL, NULL, 1 );
6948                         attr_merge_normalize( e, cfAd_syntax, bv, NULL );
6949                         ber_bvarray_free( bv );
6950                         cf_syn_tail = syn_sys_tail;
6951                 }
6952         } else {
6953                 SlapReply rs = {REP_RESULT};
6954                 c.ca_private = NULL;
6955                 e = config_build_entry( op, &rs, cfb->cb_root, &c, &schema_rdn,
6956                         &CFOC_SCHEMA, NULL );
6957                 if ( !e ) {
6958                         return -1;
6959                 }
6960                 ce = e->e_private;
6961                 ce->ce_private = cfb->cb_config;
6962                 cf_at_tail = at_sys_tail;
6963                 cf_oc_tail = oc_sys_tail;
6964                 cf_om_tail = om_sys_tail;
6965                 cf_syn_tail = syn_sys_tail;
6966         }
6967         return 0;
6968 }
6969
6970 static const char *defacl[] = {
6971         NULL, "to", "*", "by", "*", "none", NULL
6972 };
6973
6974 static int
6975 config_back_db_open( BackendDB *be, ConfigReply *cr )
6976 {
6977         CfBackInfo *cfb = be->be_private;
6978         struct berval rdn;
6979         Entry *e, *parent;
6980         CfEntryInfo *ce, *ceparent;
6981         int i, unsupp = 0;
6982         BackendInfo *bi;
6983         ConfigArgs c;
6984         Connection conn = {0};
6985         OperationBuffer opbuf;
6986         Operation *op;
6987         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
6988         SlapReply rs = {REP_RESULT};
6989         void *thrctx = NULL;
6990         AccessControl *save_access;
6991
6992         Debug( LDAP_DEBUG_TRACE, "config_back_db_open\n", 0, 0, 0);
6993
6994         /* If we have no explicitly configured ACLs, don't just use
6995          * the global ACLs. Explicitly deny access to everything.
6996          */
6997         save_access = be->bd_self->be_acl;
6998         be->bd_self->be_acl = NULL;
6999         parse_acl(be->bd_self, "config_back_db_open", 0, 6, (char **)defacl, 0 );
7000         defacl_parsed = be->bd_self->be_acl;
7001         if ( save_access ) {
7002                 be->bd_self->be_acl = save_access;
7003         } else {
7004                 Debug( LDAP_DEBUG_CONFIG, "config_back_db_open: "
7005                                 "No explicit ACL for back-config configured. "
7006                                 "Using hardcoded default\n", 0, 0, 0 );
7007         }
7008
7009         thrctx = ldap_pvt_thread_pool_context();
7010         connection_fake_init( &conn, &opbuf, thrctx );
7011         op = &opbuf.ob_op;
7012
7013         op->o_tag = LDAP_REQ_ADD;
7014         op->o_callback = &cb;
7015         op->o_bd = &cfb->cb_db;
7016         op->o_dn = op->o_bd->be_rootdn;
7017         op->o_ndn = op->o_bd->be_rootndn;
7018
7019         if ( !cfb->cb_use_ldif ) {
7020                 op->o_noop = 1;
7021         }
7022
7023         /* If we read the config from back-ldif, do some quick sanity checks */
7024         if ( cfb->cb_got_ldif ) {
7025                 return config_check_schema( op, cfb );
7026         }
7027
7028         /* create root of tree */
7029         rdn = config_rdn;
7030         c.ca_private = cfb->cb_config;
7031         c.be = frontendDB;
7032         e = config_build_entry( op, &rs, NULL, &c, &rdn, &CFOC_GLOBAL, NULL );
7033         if ( !e ) {
7034                 return -1;
7035         }
7036         ce = e->e_private;
7037         cfb->cb_root = ce;
7038
7039         parent = e;
7040         ceparent = ce;
7041
7042 #ifdef SLAPD_MODULES
7043         /* Create Module nodes... */
7044         if ( modpaths.mp_loads ) {
7045                 if ( config_build_modules( &c, ceparent, op, &rs ) ){
7046                         return -1;
7047                 }
7048         }
7049 #endif
7050
7051         /* Create schema nodes... cn=schema will contain the hardcoded core
7052          * schema, read-only. Child objects will contain runtime loaded schema
7053          * files.
7054          */
7055         rdn = schema_rdn;
7056         c.ca_private = NULL;
7057         e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_SCHEMA, NULL );
7058         if ( !e ) {
7059                 return -1;
7060         }
7061         ce = e->e_private;
7062         ce->ce_private = cfb->cb_config;
7063         cf_at_tail = at_sys_tail;
7064         cf_oc_tail = oc_sys_tail;
7065         cf_om_tail = om_sys_tail;
7066         cf_syn_tail = syn_sys_tail;
7067
7068         /* Create schema nodes for included schema... */
7069         if ( cfb->cb_config->c_kids ) {
7070                 int rc;
7071                 c.depth = 0;
7072                 c.ca_private = cfb->cb_config->c_kids;
7073                 rc = config_build_schema_inc( &c, ce, op, &rs );
7074                 if ( rc ) {
7075                         return -1;
7076                 }
7077         }
7078
7079         /* Create backend nodes. Skip if they don't provide a cf_table.
7080          * There usually aren't any of these.
7081          */
7082         
7083         c.line = 0;
7084         LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next) {
7085                 if (!bi->bi_cf_ocs) {
7086                         /* If it only supports the old config mech, complain. */
7087                         if ( bi->bi_config ) {
7088                                 Debug( LDAP_DEBUG_ANY,
7089                                         "WARNING: No dynamic config support for backend %s.\n",
7090                                         bi->bi_type, 0, 0 );
7091                                 unsupp++;
7092                         }
7093                         continue;
7094                 }
7095                 if (!bi->bi_private) continue;
7096
7097                 rdn.bv_val = c.log;
7098                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
7099                         "%s=%s", cfAd_backend->ad_cname.bv_val, bi->bi_type);
7100                 if ( rdn.bv_len >= sizeof( c.log ) ) {
7101                         /* FIXME: holler ... */ ;
7102                 }
7103                 c.bi = bi;
7104                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_BACKEND,
7105                         bi->bi_cf_ocs );
7106                 if ( !e ) {
7107                         return -1;
7108                 }
7109         }
7110
7111         /* Create database nodes... */
7112         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
7113         LDAP_STAILQ_NEXT(frontendDB, be_next) = LDAP_STAILQ_FIRST(&backendDB);
7114         for ( i = -1, be = frontendDB ; be;
7115                 i++, be = LDAP_STAILQ_NEXT( be, be_next )) {
7116                 slap_overinfo *oi = NULL;
7117
7118                 if ( overlay_is_over( be )) {
7119                         oi = be->bd_info->bi_private;
7120                         bi = oi->oi_orig;
7121                 } else {
7122                         bi = be->bd_info;
7123                 }
7124
7125                 /* If this backend supports the old config mechanism, but not
7126                  * the new mech, complain.
7127                  */
7128                 if ( !be->be_cf_ocs && bi->bi_db_config ) {
7129                         Debug( LDAP_DEBUG_ANY,
7130                                 "WARNING: No dynamic config support for database %s.\n",
7131                                 bi->bi_type, 0, 0 );
7132                         unsupp++;
7133                 }
7134                 rdn.bv_val = c.log;
7135                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
7136                         "%s=" SLAP_X_ORDERED_FMT "%s", cfAd_database->ad_cname.bv_val,
7137                         i, bi->bi_type);
7138                 if ( rdn.bv_len >= sizeof( c.log ) ) {
7139                         /* FIXME: holler ... */ ;
7140                 }
7141                 c.be = be;
7142                 c.bi = bi;
7143                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_DATABASE,
7144                         be->be_cf_ocs );
7145                 if ( !e ) {
7146                         return -1;
7147                 }
7148                 ce = e->e_private;
7149                 if ( be->be_cf_ocs && be->be_cf_ocs->co_cfadd ) {
7150                         rs_reinit( &rs, REP_RESULT );
7151                         be->be_cf_ocs->co_cfadd( op, &rs, e, &c );
7152                 }
7153                 /* Iterate through overlays */
7154                 if ( oi ) {
7155                         slap_overinst *on;
7156                         Entry *oe;
7157                         int j;
7158                         voidList *vl, *v0 = NULL;
7159
7160                         /* overlays are in LIFO order, must reverse stack */
7161                         for (on=oi->oi_list; on; on=on->on_next) {
7162                                 vl = ch_malloc( sizeof( voidList ));
7163                                 vl->vl_next = v0;
7164                                 v0 = vl;
7165                                 vl->vl_ptr = on;
7166                         }
7167                         for (j=0; vl; j++,vl=v0) {
7168                                 on = vl->vl_ptr;
7169                                 v0 = vl->vl_next;
7170                                 ch_free( vl );
7171                                 if ( on->on_bi.bi_db_config && !on->on_bi.bi_cf_ocs ) {
7172                                         Debug( LDAP_DEBUG_ANY,
7173                                                 "WARNING: No dynamic config support for overlay %s.\n",
7174                                                 on->on_bi.bi_type, 0, 0 );
7175                                         unsupp++;
7176                                 }
7177                                 rdn.bv_val = c.log;
7178                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
7179                                         "%s=" SLAP_X_ORDERED_FMT "%s",
7180                                         cfAd_overlay->ad_cname.bv_val, j, on->on_bi.bi_type );
7181                                 if ( rdn.bv_len >= sizeof( c.log ) ) {
7182                                         /* FIXME: holler ... */ ;
7183                                 }
7184                                 c.be = be;
7185                                 c.bi = &on->on_bi;
7186                                 oe = config_build_entry( op, &rs, ce, &c, &rdn,
7187                                         &CFOC_OVERLAY, c.bi->bi_cf_ocs );
7188                                 if ( !oe ) {
7189                                         return -1;
7190                                 }
7191                                 if ( c.bi->bi_cf_ocs && c.bi->bi_cf_ocs->co_cfadd ) {
7192                                         rs_reinit( &rs, REP_RESULT );
7193                                         c.bi->bi_cf_ocs->co_cfadd( op, &rs, oe, &c );
7194                                 }
7195                         }
7196                 }
7197         }
7198         if ( thrctx )
7199                 ldap_pvt_thread_pool_context_reset( thrctx );
7200
7201         if ( unsupp  && cfb->cb_use_ldif ) {
7202                 Debug( LDAP_DEBUG_ANY, "\nWARNING: The converted cn=config "
7203                         "directory is incomplete and may not work.\n\n", 0, 0, 0 );
7204         }
7205
7206         return 0;
7207 }
7208
7209 static void
7210 cfb_free_cffile( ConfigFile *cf )
7211 {
7212         ConfigFile *next;
7213
7214         for (; cf; cf=next) {
7215                 next = cf->c_sibs;
7216                 if ( cf->c_kids )
7217                         cfb_free_cffile( cf->c_kids );
7218                 ch_free( cf->c_file.bv_val );
7219                 ber_bvarray_free( cf->c_dseFiles );
7220                 ch_free( cf );
7221         }
7222 }
7223
7224 static void
7225 cfb_free_entries( CfEntryInfo *ce )
7226 {
7227         CfEntryInfo *next;
7228
7229         for (; ce; ce=next) {
7230                 next = ce->ce_sibs;
7231                 if ( ce->ce_kids )
7232                         cfb_free_entries( ce->ce_kids );
7233                 ce->ce_entry->e_private = NULL;
7234                 entry_free( ce->ce_entry );
7235                 ch_free( ce );
7236         }
7237 }
7238
7239 static int
7240 config_back_db_close( BackendDB *be, ConfigReply *cr )
7241 {
7242         CfBackInfo *cfb = be->be_private;
7243
7244         cfb_free_entries( cfb->cb_root );
7245         cfb->cb_root = NULL;
7246
7247         if ( cfb->cb_db.bd_info ) {
7248                 backend_shutdown( &cfb->cb_db );
7249         }
7250
7251         if ( defacl_parsed && be->be_acl != defacl_parsed ) {
7252                 acl_free( defacl_parsed );
7253                 defacl_parsed = NULL;
7254         }
7255
7256         return 0;
7257 }
7258
7259 static int
7260 config_back_db_destroy( BackendDB *be, ConfigReply *cr )
7261 {
7262         CfBackInfo *cfb = be->be_private;
7263
7264         cfb_free_cffile( cfb->cb_config );
7265
7266         ch_free( cfdir.bv_val );
7267
7268         avl_free( CfOcTree, NULL );
7269
7270         if ( cfb->cb_db.bd_info ) {
7271                 cfb->cb_db.be_suffix = NULL;
7272                 cfb->cb_db.be_nsuffix = NULL;
7273                 BER_BVZERO( &cfb->cb_db.be_rootdn );
7274                 BER_BVZERO( &cfb->cb_db.be_rootndn );
7275
7276                 backend_destroy_one( &cfb->cb_db, 0 );
7277         }
7278
7279         loglevel_destroy();
7280
7281         return 0;
7282 }
7283
7284 static int
7285 config_back_db_init( BackendDB *be, ConfigReply* cr )
7286 {
7287         struct berval dn;
7288         CfBackInfo *cfb;
7289
7290         cfb = &cfBackInfo;
7291         cfb->cb_config = ch_calloc( 1, sizeof(ConfigFile));
7292         cfn = cfb->cb_config;
7293         be->be_private = cfb;
7294
7295         ber_dupbv( &be->be_rootdn, &config_rdn );
7296         ber_dupbv( &be->be_rootndn, &be->be_rootdn );
7297         ber_dupbv( &dn, &be->be_rootdn );
7298         ber_bvarray_add( &be->be_suffix, &dn );
7299         ber_dupbv( &dn, &be->be_rootdn );
7300         ber_bvarray_add( &be->be_nsuffix, &dn );
7301
7302         /* Hide from namingContexts */
7303         SLAP_BFLAGS(be) |= SLAP_BFLAG_CONFIG;
7304
7305         /* Check ACLs on content of Adds by default */
7306         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_ACL_ADD;
7307
7308         return 0;
7309 }
7310
7311 static int
7312 config_back_destroy( BackendInfo *bi )
7313 {
7314         ldif_must_b64_encode_release();
7315         return 0;
7316 }
7317
7318 static int
7319 config_tool_entry_open( BackendDB *be, int mode )
7320 {
7321         CfBackInfo *cfb = be->be_private;
7322         BackendInfo *bi = cfb->cb_db.bd_info;
7323
7324         if ( bi && bi->bi_tool_entry_open )
7325                 return bi->bi_tool_entry_open( &cfb->cb_db, mode );
7326         else
7327                 return -1;
7328         
7329 }
7330
7331 static int
7332 config_tool_entry_close( BackendDB *be )
7333 {
7334         CfBackInfo *cfb = be->be_private;
7335         BackendInfo *bi = cfb->cb_db.bd_info;
7336
7337         if ( bi && bi->bi_tool_entry_close )
7338                 return bi->bi_tool_entry_close( &cfb->cb_db );
7339         else
7340                 return -1;
7341 }
7342
7343 static ID
7344 config_tool_entry_first( BackendDB *be )
7345 {
7346         CfBackInfo *cfb = be->be_private;
7347         BackendInfo *bi = cfb->cb_db.bd_info;
7348
7349         if ( bi && bi->bi_tool_entry_first ) {
7350                 return bi->bi_tool_entry_first( &cfb->cb_db );
7351         }
7352         if ( bi && bi->bi_tool_entry_first_x ) {
7353                 return bi->bi_tool_entry_first_x( &cfb->cb_db,
7354                         NULL, LDAP_SCOPE_DEFAULT, NULL );
7355         }
7356         return NOID;
7357 }
7358
7359 static ID
7360 config_tool_entry_first_x(
7361         BackendDB *be,
7362         struct berval *base,
7363         int scope,
7364         Filter *f )
7365 {
7366         CfBackInfo *cfb = be->be_private;
7367         BackendInfo *bi = cfb->cb_db.bd_info;
7368
7369         if ( bi && bi->bi_tool_entry_first_x ) {
7370                 return bi->bi_tool_entry_first_x( &cfb->cb_db, base, scope, f );
7371         }
7372         return NOID;
7373 }
7374
7375 static ID
7376 config_tool_entry_next( BackendDB *be )
7377 {
7378         CfBackInfo *cfb = be->be_private;
7379         BackendInfo *bi = cfb->cb_db.bd_info;
7380
7381         if ( bi && bi->bi_tool_entry_next )
7382                 return bi->bi_tool_entry_next( &cfb->cb_db );
7383         else
7384                 return NOID;
7385 }
7386
7387 static Entry *
7388 config_tool_entry_get( BackendDB *be, ID id )
7389 {
7390         CfBackInfo *cfb = be->be_private;
7391         BackendInfo *bi = cfb->cb_db.bd_info;
7392
7393         if ( bi && bi->bi_tool_entry_get )
7394                 return bi->bi_tool_entry_get( &cfb->cb_db, id );
7395         else
7396                 return NULL;
7397 }
7398
7399 static int entry_put_got_frontend=0;
7400 static int entry_put_got_config=0;
7401 static ID
7402 config_tool_entry_put( BackendDB *be, Entry *e, struct berval *text )
7403 {
7404         CfBackInfo *cfb = be->be_private;
7405         BackendInfo *bi = cfb->cb_db.bd_info;
7406         int rc;
7407         struct berval rdn, vals[ 2 ];
7408         ConfigArgs ca;
7409         OperationBuffer opbuf;
7410         Entry *ce;
7411         Connection conn = {0};
7412         Operation *op = NULL;
7413         void *thrctx;
7414         int isFrontend = 0;
7415         int isFrontendChild = 0;
7416
7417         /* Create entry for frontend database if it does not exist already */
7418         if ( !entry_put_got_frontend ) {
7419                 if ( !strncmp( e->e_nname.bv_val, "olcDatabase", 
7420                                 STRLENOF( "olcDatabase" ))) {
7421                         if ( strncmp( e->e_nname.bv_val + 
7422                                         STRLENOF( "olcDatabase" ), "={-1}frontend",
7423                                         STRLENOF( "={-1}frontend" )) && 
7424                                         strncmp( e->e_nname.bv_val + 
7425                                         STRLENOF( "olcDatabase" ), "=frontend",
7426                                         STRLENOF( "=frontend" ))) {
7427                                 vals[1].bv_len = 0;
7428                                 vals[1].bv_val = NULL;
7429                                 memset( &ca, 0, sizeof(ConfigArgs));
7430                                 ca.be = frontendDB;
7431                                 ca.bi = frontendDB->bd_info;
7432                                 ca.be->be_cf_ocs = &CFOC_FRONTEND;
7433                                 rdn.bv_val = ca.log;
7434                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( ca.log ),
7435                                         "%s=" SLAP_X_ORDERED_FMT "%s",
7436                                         cfAd_database->ad_cname.bv_val, -1,
7437                                         ca.bi->bi_type);
7438                                 ce = config_build_entry( NULL, NULL, cfb->cb_root, &ca, &rdn,
7439                                                 &CFOC_DATABASE, ca.be->be_cf_ocs );
7440                                 thrctx = ldap_pvt_thread_pool_context();
7441                                 connection_fake_init2( &conn, &opbuf, thrctx,0 );
7442                                 op = &opbuf.ob_op;
7443                                 op->o_bd = &cfb->cb_db;
7444                                 op->o_tag = LDAP_REQ_ADD;
7445                                 op->ora_e = ce;
7446                                 op->o_dn = be->be_rootdn;
7447                                 op->o_ndn = be->be_rootndn;
7448                                 rc = slap_add_opattrs(op, NULL, NULL, 0, 0);
7449                                 if ( rc != LDAP_SUCCESS ) {
7450                                         text->bv_val = "autocreation of \"olcDatabase={-1}frontend\" failed";
7451                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={-1}frontend\" failed");
7452                                         return NOID;
7453                                 }
7454
7455                                 if ( ce && bi && bi->bi_tool_entry_put && 
7456                                                 bi->bi_tool_entry_put( &cfb->cb_db, ce, text ) != NOID ) {
7457                                         entry_put_got_frontend++;
7458                                 } else {
7459                                         text->bv_val = "autocreation of \"olcDatabase={-1}frontend\" failed";
7460                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={-1}frontend\" failed");
7461                                         return NOID;
7462                                 }
7463                         } else {
7464                                 if ( !strncmp( e->e_nname.bv_val + 
7465                                         STRLENOF( "olcDatabase" ), "=frontend",
7466                                         STRLENOF( "=frontend" ) ) )
7467                                 {
7468                                         struct berval rdn, pdn, ndn;
7469                                         dnParent( &e->e_nname, &pdn );
7470                                         rdn.bv_val = ca.log;
7471                                         rdn.bv_len = snprintf(rdn.bv_val, sizeof( ca.log ),
7472                                                 "%s=" SLAP_X_ORDERED_FMT "%s",
7473                                                 cfAd_database->ad_cname.bv_val, -1,
7474                                                 frontendDB->bd_info->bi_type );
7475                                         build_new_dn( &ndn, &pdn, &rdn, NULL );
7476                                         ber_memfree( e->e_name.bv_val );
7477                                         e->e_name = ndn;
7478                                         ber_bvreplace( &e->e_nname, &e->e_name );
7479                                 }
7480                                 entry_put_got_frontend++;
7481                                 isFrontend = 1;
7482                         }
7483                 }
7484         }
7485
7486         /* Child entries of the frontend database, e.g. slapo-chain's back-ldap
7487          * instances, may appear before the config database entry in the ldif, skip
7488          * auto-creation of olcDatabase={0}config in such a case */
7489         if ( !entry_put_got_config &&
7490                         !strncmp( e->e_nname.bv_val, "olcDatabase", STRLENOF( "olcDatabase" ))) {
7491                 struct berval pdn;
7492                 dnParent( &e->e_nname, &pdn );
7493                 while ( pdn.bv_len ) {
7494                         if ( !strncmp( pdn.bv_val, "olcDatabase",
7495                                         STRLENOF( "olcDatabase" ))) {
7496                                 if ( !strncmp( pdn.bv_val +
7497                                                 STRLENOF( "olcDatabase" ), "={-1}frontend",
7498                                                 STRLENOF( "={-1}frontend" )) ||
7499                                                 !strncmp( pdn.bv_val +
7500                                                 STRLENOF( "olcDatabase" ), "=frontend",
7501                                                 STRLENOF( "=frontend" ))) {
7502
7503                                         isFrontendChild = 1;
7504                                         break;
7505                                 }
7506                         }
7507                         dnParent( &pdn, &pdn );
7508                 }
7509         }
7510
7511         /* Create entry for config database if it does not exist already */
7512         if ( !entry_put_got_config && !isFrontend && !isFrontendChild ) {
7513                 if ( !strncmp( e->e_nname.bv_val, "olcDatabase",
7514                                 STRLENOF( "olcDatabase" ))) {
7515                         if ( strncmp( e->e_nname.bv_val +
7516                                         STRLENOF( "olcDatabase" ), "={0}config",
7517                                         STRLENOF( "={0}config" )) &&
7518                                         strncmp( e->e_nname.bv_val +
7519                                         STRLENOF( "olcDatabase" ), "=config",
7520                                         STRLENOF( "=config" )) ) {
7521                                 vals[1].bv_len = 0;
7522                                 vals[1].bv_val = NULL;
7523                                 memset( &ca, 0, sizeof(ConfigArgs));
7524                                 ca.be = LDAP_STAILQ_FIRST( &backendDB );
7525                                 ca.bi = ca.be->bd_info;
7526                                 rdn.bv_val = ca.log;
7527                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( ca.log ),
7528                                         "%s=" SLAP_X_ORDERED_FMT "%s",
7529                                         cfAd_database->ad_cname.bv_val, 0,
7530                                         ca.bi->bi_type);
7531                                 ce = config_build_entry( NULL, NULL, cfb->cb_root, &ca, &rdn, &CFOC_DATABASE,
7532                                                 ca.be->be_cf_ocs );
7533                                 if ( ! op ) {
7534                                         thrctx = ldap_pvt_thread_pool_context();
7535                                         connection_fake_init2( &conn, &opbuf, thrctx,0 );
7536                                         op = &opbuf.ob_op;
7537                                         op->o_bd = &cfb->cb_db;
7538                                         op->o_tag = LDAP_REQ_ADD;
7539                                         op->o_dn = be->be_rootdn;
7540                                         op->o_ndn = be->be_rootndn;
7541                                 }
7542                                 op->ora_e = ce;
7543                                 rc = slap_add_opattrs(op, NULL, NULL, 0, 0);
7544                                 if ( rc != LDAP_SUCCESS ) {
7545                                         text->bv_val = "autocreation of \"olcDatabase={0}config\" failed";
7546                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={0}config\" failed");
7547                                         return NOID;
7548                                 }
7549                                 if (ce && bi && bi->bi_tool_entry_put &&
7550                                                 bi->bi_tool_entry_put( &cfb->cb_db, ce, text ) != NOID ) {
7551                                         entry_put_got_config++;
7552                                 } else {
7553                                         text->bv_val = "autocreation of \"olcDatabase={0}config\" failed";
7554                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={0}config\" failed");
7555                                         return NOID;
7556                                 }
7557                         } else {
7558                                 entry_put_got_config++;
7559                         }
7560                 }
7561         }
7562         if ( bi && bi->bi_tool_entry_put &&
7563                 config_add_internal( cfb, e, &ca, NULL, NULL, NULL ) == 0 )
7564                 return bi->bi_tool_entry_put( &cfb->cb_db, e, text );
7565         else
7566                 return NOID;
7567 }
7568
7569 static struct {
7570         char *name;
7571         AttributeDescription **desc;
7572 } ads[] = {
7573         { "attribute", &cfAd_attr },
7574         { "backend", &cfAd_backend },
7575         { "database", &cfAd_database },
7576         { "include", &cfAd_include },
7577         { "ldapsyntax", &cfAd_syntax },
7578         { "objectclass", &cfAd_oc },
7579         { "objectidentifier", &cfAd_om },
7580         { "overlay", &cfAd_overlay },
7581         { NULL, NULL }
7582 };
7583
7584 /* Notes:
7585  *   add / delete: all types that may be added or deleted must use an
7586  * X-ORDERED attributeType for their RDN. Adding and deleting entries
7587  * should automatically renumber the index of any siblings as needed,
7588  * so that no gaps in the numbering sequence exist after the add/delete
7589  * is completed.
7590  *   What can be added:
7591  *     schema objects
7592  *     backend objects for backend-specific config directives
7593  *     database objects
7594  *     overlay objects
7595  *
7596  *   delete: probably no support this time around.
7597  *
7598  *   modrdn: generally not done. Will be invoked automatically by add/
7599  * delete to update numbering sequence. Perform as an explicit operation
7600  * so that the renumbering effect may be replicated. Subtree rename must
7601  * be supported, since renumbering a database will affect all its child
7602  * overlays.
7603  *
7604  *  modify: must be fully supported. 
7605  */
7606
7607 int
7608 config_back_initialize( BackendInfo *bi )
7609 {
7610         ConfigTable             *ct = config_back_cf_table;
7611         ConfigArgs ca;
7612         char                    *argv[4];
7613         int                     i;
7614         AttributeDescription    *ad = NULL;
7615         const char              *text;
7616         static char             *controls[] = {
7617                 LDAP_CONTROL_MANAGEDSAIT,
7618                 NULL
7619         };
7620
7621         /* Make sure we don't exceed the bits reserved for userland */
7622         config_check_userland( CFG_LAST );
7623
7624         bi->bi_controls = controls;
7625
7626         bi->bi_open = 0;
7627         bi->bi_close = 0;
7628         bi->bi_config = 0;
7629         bi->bi_destroy = config_back_destroy;
7630
7631         bi->bi_db_init = config_back_db_init;
7632         bi->bi_db_config = 0;
7633         bi->bi_db_open = config_back_db_open;
7634         bi->bi_db_close = config_back_db_close;
7635         bi->bi_db_destroy = config_back_db_destroy;
7636
7637         bi->bi_op_bind = config_back_bind;
7638         bi->bi_op_unbind = 0;
7639         bi->bi_op_search = config_back_search;
7640         bi->bi_op_compare = 0;
7641         bi->bi_op_modify = config_back_modify;
7642         bi->bi_op_modrdn = config_back_modrdn;
7643         bi->bi_op_add = config_back_add;
7644         bi->bi_op_delete = config_back_delete;
7645         bi->bi_op_abandon = 0;
7646
7647         bi->bi_extended = 0;
7648
7649         bi->bi_chk_referrals = 0;
7650
7651         bi->bi_access_allowed = slap_access_allowed;
7652
7653         bi->bi_connection_init = 0;
7654         bi->bi_connection_destroy = 0;
7655
7656         bi->bi_entry_release_rw = config_entry_release;
7657         bi->bi_entry_get_rw = config_back_entry_get;
7658
7659         bi->bi_tool_entry_open = config_tool_entry_open;
7660         bi->bi_tool_entry_close = config_tool_entry_close;
7661         bi->bi_tool_entry_first = config_tool_entry_first;
7662         bi->bi_tool_entry_first_x = config_tool_entry_first_x;
7663         bi->bi_tool_entry_next = config_tool_entry_next;
7664         bi->bi_tool_entry_get = config_tool_entry_get;
7665         bi->bi_tool_entry_put = config_tool_entry_put;
7666
7667         ca.argv = argv;
7668         argv[ 0 ] = "slapd";
7669         ca.argv = argv;
7670         ca.argc = 3;
7671         ca.fname = argv[0];
7672
7673         argv[3] = NULL;
7674         for (i=0; OidMacros[i].name; i++ ) {
7675                 argv[1] = OidMacros[i].name;
7676                 argv[2] = OidMacros[i].oid;
7677                 parse_oidm( &ca, 0, NULL );
7678         }
7679
7680         bi->bi_cf_ocs = cf_ocs;
7681
7682         i = config_register_schema( ct, cf_ocs );
7683         if ( i ) return i;
7684
7685         i = slap_str2ad( "olcDatabase", &olcDatabaseDummy[0].ad, &text );
7686         if ( i ) return i;
7687
7688         /* setup olcRootPW to be base64-encoded when written in LDIF form;
7689          * basically, we don't care if it fails */
7690         i = slap_str2ad( "olcRootPW", &ad, &text );
7691         if ( i ) {
7692                 Debug( LDAP_DEBUG_ANY, "config_back_initialize: "
7693                         "warning, unable to get \"olcRootPW\" "
7694                         "attribute description: %d: %s\n",
7695                         i, text, 0 );
7696         } else {
7697                 (void)ldif_must_b64_encode_register( ad->ad_cname.bv_val,
7698                         ad->ad_type->sat_oid );
7699         }
7700
7701         /* set up the notable AttributeDescriptions */
7702         i = 0;
7703         for (;ct->name;ct++) {
7704                 if (strcmp(ct->name, ads[i].name)) continue;
7705                 *ads[i].desc = ct->ad;
7706                 i++;
7707                 if (!ads[i].name) break;
7708         }
7709
7710         return 0;
7711 }