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