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