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