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