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