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