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