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