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