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