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