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