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