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