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