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