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