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