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