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