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