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