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