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