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