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