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