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