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