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