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