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