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