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