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