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