]> git.sur5r.net Git - openldap/blob - servers/slapd/bconfig.c
Add substring matching rules for some of the olc-schema attributes.
[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                                                 oc_next( &oc );
1630                                         }
1631                                 } else
1632                                 /* If adding the first, and head exists, find its prev */
1633                                         if (cfn->c_oc_head) {
1634                                         for ( oc_start( &oc ); oc != cfn->c_oc_head; ) {
1635                                                 prev = oc;
1636                                                 oc_next( &oc );
1637                                         }
1638                                 }
1639                                 /* else prev is NULL, append to end of global list */
1640                         }
1641                         if(parse_oc(c, &oc, prev)) return(1);
1642                         if (!cfn->c_oc_head) cfn->c_oc_head = oc;
1643                         if (cfn->c_oc_tail == prev) cfn->c_oc_tail = oc;
1644                         }
1645                         break;
1646
1647                 case CFG_ATTR: {
1648                         AttributeType *at, *prev;
1649
1650                         if ( c->op == LDAP_MOD_ADD && c->ca_private && cfn != c->ca_private )
1651                                 cfn = c->ca_private;
1652                         if ( c->valx < 0 ) {
1653                                 prev = cfn->c_at_tail;
1654                         } else {
1655                                 prev = NULL;
1656                                 /* If adding anything after the first, prev is easy */
1657                                 if ( c->valx ) {
1658                                         int i;
1659                                         for (i=0, at = cfn->c_at_head; i<c->valx; i++) {
1660                                                 prev = at;
1661                                                 at_next( &at );
1662                                         }
1663                                 } else
1664                                 /* If adding the first, and head exists, find its prev */
1665                                         if (cfn->c_at_head) {
1666                                         for ( at_start( &at ); at != cfn->c_at_head; ) {
1667                                                 prev = at;
1668                                                 at_next( &at );
1669                                         }
1670                                 }
1671                                 /* else prev is NULL, append to end of global list */
1672                         }
1673                         if(parse_at(c, &at, prev)) return(1);
1674                         if (!cfn->c_at_head) cfn->c_at_head = at;
1675                         if (cfn->c_at_tail == prev) cfn->c_at_tail = at;
1676                         }
1677                         break;
1678
1679                 case CFG_SYNTAX: {
1680                         Syntax *syn, *prev;
1681
1682                         if ( c->op == LDAP_MOD_ADD && c->ca_private && cfn != c->ca_private )
1683                                 cfn = c->ca_private;
1684                         if ( c->valx < 0 ) {
1685                                 prev = cfn->c_syn_tail;
1686                         } else {
1687                                 prev = NULL;
1688                                 /* If adding anything after the first, prev is easy */
1689                                 if ( c->valx ) {
1690                                         int i;
1691                                         for ( i = 0, syn = cfn->c_syn_head; i < c->valx; i++ ) {
1692                                                 prev = syn;
1693                                                 syn_next( &syn );
1694                                         }
1695                                 } else
1696                                 /* If adding the first, and head exists, find its prev */
1697                                         if (cfn->c_syn_head) {
1698                                         for ( syn_start( &syn ); syn != cfn->c_syn_head; ) {
1699                                                 prev = syn;
1700                                                 syn_next( &syn );
1701                                         }
1702                                 }
1703                                 /* else prev is NULL, append to end of global list */
1704                         }
1705                         if ( parse_syn( c, &syn, prev ) ) return(1);
1706                         if ( !cfn->c_syn_head ) cfn->c_syn_head = syn;
1707                         if ( cfn->c_syn_tail == prev ) cfn->c_syn_tail = syn;
1708                         }
1709                         break;
1710
1711                 case CFG_DIT: {
1712                         ContentRule *cr;
1713
1714                         if ( c->op == LDAP_MOD_ADD && c->ca_private && cfn != c->ca_private )
1715                                 cfn = c->ca_private;
1716                         if(parse_cr(c, &cr)) return(1);
1717                         if (!cfn->c_cr_head) cfn->c_cr_head = cr;
1718                         cfn->c_cr_tail = cr;
1719                         }
1720                         break;
1721
1722                 case CFG_ATOPT:
1723                         ad_define_option(NULL, NULL, 0);
1724                         for(i = 1; i < c->argc; i++)
1725                                 if(ad_define_option(c->argv[i], c->fname, c->lineno))
1726                                         return(1);
1727                         break;
1728
1729                 case CFG_IX_INTLEN:
1730                         if ( c->value_int < SLAP_INDEX_INTLEN_DEFAULT )
1731                                 c->value_int = SLAP_INDEX_INTLEN_DEFAULT;
1732                         else if ( c->value_int > 255 )
1733                                 c->value_int = 255;
1734                         index_intlen = c->value_int;
1735                         index_intlen_strlen = SLAP_INDEX_INTLEN_STRLEN(
1736                                 index_intlen );
1737                         break;
1738                         
1739                 case CFG_SORTVALS: {
1740                         ADlist *svnew = NULL, *svtail, *sv;
1741
1742                         for ( i = 1; i < c->argc; i++ ) {
1743                                 AttributeDescription *ad = NULL;
1744                                 const char *text;
1745                                 int rc;
1746
1747                                 rc = slap_str2ad( c->argv[i], &ad, &text );
1748                                 if ( rc ) {
1749                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown attribute type #%d",
1750                                                 c->argv[0], i );
1751 sortval_reject:
1752                                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1753                                                 c->log, c->cr_msg, c->argv[i] );
1754                                         for ( sv = svnew; sv; sv = svnew ) {
1755                                                 svnew = sv->al_next;
1756                                                 ch_free( sv );
1757                                         }
1758                                         return 1;
1759                                 }
1760                                 if (( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) ||
1761                                         ad->ad_type->sat_single_value ) {
1762                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> inappropriate attribute type #%d",
1763                                                 c->argv[0], i );
1764                                         goto sortval_reject;
1765                                 }
1766                                 sv = ch_malloc( sizeof( ADlist ));
1767                                 sv->al_desc = ad;
1768                                 if ( !svnew ) {
1769                                         svnew = sv;
1770                                 } else {
1771                                         svtail->al_next = sv;
1772                                 }
1773                                 svtail = sv;
1774                         }
1775                         sv->al_next = NULL;
1776                         for ( sv = svnew; sv; sv = sv->al_next )
1777                                 sv->al_desc->ad_type->sat_flags |= SLAP_AT_SORTED_VAL;
1778                         for ( sv = sortVals; sv && sv->al_next; sv = sv->al_next );
1779                         if ( sv )
1780                                 sv->al_next = svnew;
1781                         else
1782                                 sortVals = svnew;
1783                         }
1784                         break;
1785
1786                 case CFG_ACL:
1787                         /* Don't append to the global ACL if we're on a specific DB */
1788                         i = c->valx;
1789                         if ( c->valx == -1 ) {
1790                                 AccessControl *a;
1791                                 i = 0;
1792                                 for ( a=c->be->be_acl; a; a = a->acl_next )
1793                                         i++;
1794                         }
1795                         if ( parse_acl(c->be, c->fname, c->lineno, c->argc, c->argv, i ) ) {
1796                                 return 1;
1797                         }
1798                         break;
1799
1800                 case CFG_ACL_ADD:
1801                         if(c->value_int)
1802                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_ACL_ADD;
1803                         else
1804                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_ACL_ADD;
1805                         break;
1806
1807                 case CFG_ROOTDSE:
1808                         if(root_dse_read_file(c->argv[1])) {
1809                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> could not read file", c->argv[0] );
1810                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1811                                         c->log, c->cr_msg, c->argv[1] );
1812                                 return(1);
1813                         }
1814                         {
1815                                 struct berval bv;
1816                                 ber_str2bv( c->argv[1], 0, 1, &bv );
1817                                 if ( c->op == LDAP_MOD_ADD && c->ca_private && cfn != c->ca_private )
1818                                         cfn = c->ca_private;
1819                                 ber_bvarray_add( &cfn->c_dseFiles, &bv );
1820                         }
1821                         break;
1822
1823                 case CFG_SERVERID:
1824                         {
1825                                 ServerID *si, **sip;
1826                                 LDAPURLDesc *lud;
1827                                 int num;
1828                                 if (( lutil_atoi( &num, c->argv[1] ) && 
1829                                         lutil_atoix( &num, c->argv[1], 16 )) ||
1830                                         num < 0 || num > SLAP_SYNC_SID_MAX )
1831                                 {
1832                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1833                                                 "<%s> illegal server ID", c->argv[0] );
1834                                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1835                                                 c->log, c->cr_msg, c->argv[1] );
1836                                         return 1;
1837                                 }
1838                                 /* only one value allowed if no URL is given */
1839                                 if ( c->argc > 2 ) {
1840                                         int len;
1841
1842                                         if ( sid_list && BER_BVISEMPTY( &sid_list->si_url )) {
1843                                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1844                                                         "<%s> only one server ID allowed now", c->argv[0] );
1845                                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1846                                                         c->log, c->cr_msg, c->argv[1] );
1847                                                 return 1;
1848                                         }
1849
1850                                         if ( ldap_url_parse( c->argv[2], &lud )) {
1851                                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1852                                                         "<%s> invalid URL", c->argv[0] );
1853                                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1854                                                         c->log, c->cr_msg, c->argv[2] );
1855                                                 return 1;
1856                                         }
1857                                         len = strlen( c->argv[2] );
1858                                         si = ch_malloc( sizeof(ServerID) + len + 1 );
1859                                         si->si_url.bv_val = (char *)(si+1);
1860                                         si->si_url.bv_len = len;
1861                                         strcpy( si->si_url.bv_val, c->argv[2] );
1862                                 } else {
1863                                         if ( sid_list ) {
1864                                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1865                                                         "<%s> unqualified server ID not allowed now", c->argv[0] );
1866                                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1867                                                         c->log, c->cr_msg, c->argv[1] );
1868                                                 return 1;
1869                                         }
1870                                         si = ch_malloc( sizeof(ServerID) );
1871                                         BER_BVZERO( &si->si_url );
1872                                         slap_serverID = num;
1873                                         Debug( LDAP_DEBUG_CONFIG,
1874                                                 "%s: SID=0x%03x\n",
1875                                                 c->log, slap_serverID, 0 );
1876                                 }
1877                                 si->si_next = NULL;
1878                                 si->si_num = num;
1879                                 for ( sip = &sid_list; *sip; sip = &(*sip)->si_next );
1880                                 *sip = si;
1881
1882                                 if (( slapMode & SLAP_SERVER_MODE ) && c->argc > 2 ) {
1883                                         Listener *l = config_check_my_url( c->argv[2], lud );
1884                                         if ( l ) {
1885                                                 slap_serverID = si->si_num;
1886                                                 Debug( LDAP_DEBUG_CONFIG,
1887                                                         "%s: SID=0x%03x (listener=%s)\n",
1888                                                         c->log, slap_serverID,
1889                                                         l->sl_url.bv_val );
1890                                         }
1891                                 }
1892                                 if ( c->argc > 2 )
1893                                         ldap_free_urldesc( lud );
1894                         }
1895                         break;
1896                 case CFG_LOGFILE: {
1897                                 if ( logfileName ) ch_free( logfileName );
1898                                 logfileName = c->value_string;
1899                                 logfile = fopen(logfileName, "w");
1900                                 if(logfile) lutil_debug_file(logfile);
1901                         } break;
1902
1903                 case CFG_LASTMOD:
1904                         if(SLAP_NOLASTMODCMD(c->be)) {
1905                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> not available for %s database",
1906                                         c->argv[0], c->be->bd_info->bi_type );
1907                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1908                                         c->log, c->cr_msg, 0 );
1909                                 return(1);
1910                         }
1911                         if(c->value_int)
1912                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_NOLASTMOD;
1913                         else
1914                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NOLASTMOD;
1915                         break;
1916
1917                 case CFG_MIRRORMODE:
1918                         if(c->value_int && !SLAP_SHADOW(c->be)) {
1919                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> database is not a shadow",
1920                                         c->argv[0] );
1921                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1922                                         c->log, c->cr_msg, 0 );
1923                                 return(1);
1924                         }
1925                         if(c->value_int)
1926                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_SINGLE_SHADOW;
1927                         else
1928                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_SINGLE_SHADOW;
1929                         break;
1930
1931                 case CFG_MONITORING:
1932                         if(c->value_int)
1933                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_MONITORING;
1934                         else
1935                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_MONITORING;
1936                         break;
1937
1938                 case CFG_HIDDEN:
1939                         if (c->value_int)
1940                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_HIDDEN;
1941                         else
1942                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_HIDDEN;
1943                         break;
1944
1945                 case CFG_SYNC_SUBENTRY:
1946                         if (c->value_int)
1947                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_SYNC_SUBENTRY;
1948                         else
1949                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_SYNC_SUBENTRY;
1950                         break;
1951
1952                 case CFG_SSTR_IF_MAX:
1953                         if (c->value_uint < index_substr_if_minlen) {
1954                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value", c->argv[0] );
1955                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%d)\n",
1956                                         c->log, c->cr_msg, c->value_int );
1957                                 return(1);
1958                         }
1959                         index_substr_if_maxlen = c->value_uint;
1960                         break;
1961
1962                 case CFG_SSTR_IF_MIN:
1963                         if (c->value_uint > index_substr_if_maxlen) {
1964                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value", c->argv[0] );
1965                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%d)\n",
1966                                         c->log, c->cr_msg, c->value_int );
1967                                 return(1);
1968                         }
1969                         index_substr_if_minlen = c->value_uint;
1970                         break;
1971
1972 #ifdef SLAPD_MODULES
1973                 case CFG_MODLOAD:
1974                         /* If we're just adding a module on an existing modpath,
1975                          * make sure we've selected the current path.
1976                          */
1977                         if ( c->op == LDAP_MOD_ADD && c->ca_private && modcur != c->ca_private ) {
1978                                 modcur = c->ca_private;
1979                                 /* This should never fail */
1980                                 if ( module_path( modcur->mp_path.bv_val )) {
1981                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> module path no longer valid",
1982                                                 c->argv[0] );
1983                                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1984                                                 c->log, c->cr_msg, modcur->mp_path.bv_val );
1985                                         return(1);
1986                                 }
1987                         }
1988                         if(module_load(c->argv[1], c->argc - 2, (c->argc > 2) ? c->argv + 2 : NULL))
1989                                 return(1);
1990                         /* Record this load on the current path */
1991                         {
1992                                 struct berval bv;
1993                                 char *ptr;
1994                                 if ( c->op == SLAP_CONFIG_ADD ) {
1995                                         ptr = c->line + STRLENOF("moduleload");
1996                                         while (!isspace((unsigned char) *ptr)) ptr++;
1997                                         while (isspace((unsigned char) *ptr)) ptr++;
1998                                 } else {
1999                                         ptr = c->line;
2000                                 }
2001                                 ber_str2bv(ptr, 0, 1, &bv);
2002                                 ber_bvarray_add( &modcur->mp_loads, &bv );
2003                         }
2004                         /* Check for any new hardcoded schema */
2005                         if ( c->op == LDAP_MOD_ADD && CONFIG_ONLINE_ADD( c )) {
2006                                 config_check_schema( NULL, &cfBackInfo );
2007                         }
2008                         break;
2009
2010                 case CFG_MODPATH:
2011                         if(module_path(c->argv[1])) return(1);
2012                         /* Record which path was used with each module */
2013                         {
2014                                 ModPaths *mp;
2015
2016                                 if (!modpaths.mp_loads) {
2017                                         mp = &modpaths;
2018                                 } else {
2019                                         mp = ch_malloc( sizeof( ModPaths ));
2020                                         modlast->mp_next = mp;
2021                                 }
2022                                 ber_str2bv(c->argv[1], 0, 1, &mp->mp_path);
2023                                 mp->mp_next = NULL;
2024                                 mp->mp_loads = NULL;
2025                                 modlast = mp;
2026                                 c->ca_private = mp;
2027                                 modcur = mp;
2028                         }
2029                         
2030                         break;
2031 #endif
2032
2033 #ifdef LDAP_SLAPI
2034                 case CFG_PLUGIN:
2035                         if(slapi_int_read_config(c->be, c->fname, c->lineno, c->argc, c->argv) != LDAP_SUCCESS)
2036                                 return(1);
2037                         slapi_plugins_used++;
2038                         break;
2039 #endif
2040
2041 #ifdef SLAP_AUTH_REWRITE
2042                 case CFG_REWRITE: {
2043                         struct berval bv;
2044                         char *line;
2045                         int rc = 0;
2046
2047                         if ( c->op == LDAP_MOD_ADD ) {
2048                                 c->argv++;
2049                                 c->argc--;
2050                         }
2051                         if(slap_sasl_rewrite_config(c->fname, c->lineno, c->argc, c->argv))
2052                                 rc = 1;
2053                         if ( rc == 0 ) {
2054
2055                                 if ( c->argc > 1 ) {
2056                                         char    *s;
2057
2058                                         /* quote all args but the first */
2059                                         line = ldap_charray2str( c->argv, "\" \"" );
2060                                         ber_str2bv( line, 0, 0, &bv );
2061                                         s = ber_bvchr( &bv, '"' );
2062                                         assert( s != NULL );
2063                                         /* move the trailing quote of argv[0] to the end */
2064                                         AC_MEMCPY( s, s + 1, bv.bv_len - ( s - bv.bv_val ) );
2065                                         bv.bv_val[ bv.bv_len - 1 ] = '"';
2066
2067                                 } else {
2068                                         ber_str2bv( c->argv[ 0 ], 0, 1, &bv );
2069                                 }
2070
2071                                 ber_bvarray_add( &authz_rewrites, &bv );
2072                         }
2073                         if ( c->op == LDAP_MOD_ADD ) {
2074                                 c->argv--;
2075                                 c->argc++;
2076                         }
2077                         return rc;
2078                         }
2079 #endif
2080
2081
2082                 default:
2083                         Debug( LDAP_DEBUG_ANY,
2084                                 "%s: unknown CFG_TYPE %d.\n",
2085                                 c->log, c->type, 0 );
2086                         return 1;
2087
2088         }
2089         return(0);
2090 }
2091
2092
2093 static int
2094 config_fname(ConfigArgs *c) {
2095         if(c->op == SLAP_CONFIG_EMIT) {
2096                 if (c->ca_private) {
2097                         ConfigFile *cf = c->ca_private;
2098                         value_add_one( &c->rvalue_vals, &cf->c_file );
2099                         return 0;
2100                 }
2101                 return 1;
2102         }
2103         return(0);
2104 }
2105
2106 static int
2107 config_cfdir(ConfigArgs *c) {
2108         if(c->op == SLAP_CONFIG_EMIT) {
2109                 if ( !BER_BVISEMPTY( &cfdir )) {
2110                         value_add_one( &c->rvalue_vals, &cfdir );
2111                         return 0;
2112                 }
2113                 return 1;
2114         }
2115         return(0);
2116 }
2117
2118 static int
2119 config_search_base(ConfigArgs *c) {
2120         if(c->op == SLAP_CONFIG_EMIT) {
2121                 int rc = 1;
2122                 if (!BER_BVISEMPTY(&default_search_base)) {
2123                         value_add_one(&c->rvalue_vals, &default_search_base);
2124                         value_add_one(&c->rvalue_nvals, &default_search_nbase);
2125                         rc = 0;
2126                 }
2127                 return rc;
2128         } else if( c->op == LDAP_MOD_DELETE ) {
2129                 ch_free( default_search_base.bv_val );
2130                 ch_free( default_search_nbase.bv_val );
2131                 BER_BVZERO( &default_search_base );
2132                 BER_BVZERO( &default_search_nbase );
2133                 return 0;
2134         }
2135
2136         if(c->bi || c->be != frontendDB) {
2137                 Debug(LDAP_DEBUG_ANY, "%s: defaultSearchBase line must appear "
2138                         "prior to any backend or database definition\n",
2139                         c->log, 0, 0);
2140                 return(1);
2141         }
2142
2143         if(default_search_nbase.bv_len) {
2144                 free(default_search_base.bv_val);
2145                 free(default_search_nbase.bv_val);
2146         }
2147
2148         default_search_base = c->value_dn;
2149         default_search_nbase = c->value_ndn;
2150         return(0);
2151 }
2152
2153 /* For RE23 compatibility we allow this in the global entry
2154  * but we now defer it to the frontend entry to allow modules
2155  * to load new hash types.
2156  */
2157 static int
2158 config_passwd_hash(ConfigArgs *c) {
2159         int i;
2160         if (c->op == SLAP_CONFIG_EMIT) {
2161                 struct berval bv;
2162                 /* Don't generate it in the global entry */
2163                 if ( c->table == Cft_Global )
2164                         return 1;
2165                 for (i=0; default_passwd_hash && default_passwd_hash[i]; i++) {
2166                         ber_str2bv(default_passwd_hash[i], 0, 0, &bv);
2167                         value_add_one(&c->rvalue_vals, &bv);
2168                 }
2169                 return i ? 0 : 1;
2170         } else if ( c->op == LDAP_MOD_DELETE ) {
2171                 /* Deleting from global is a no-op, only the frontendDB entry matters */
2172                 if ( c->table == Cft_Global )
2173                         return 0;
2174                 if ( c->valx < 0 ) {
2175                         ldap_charray_free( default_passwd_hash );
2176                         default_passwd_hash = NULL;
2177                 } else {
2178                         i = c->valx;
2179                         ch_free( default_passwd_hash[i] );
2180                         for (; default_passwd_hash[i]; i++ )
2181                                 default_passwd_hash[i] = default_passwd_hash[i+1];
2182                 }
2183                 return 0;
2184         }
2185         for(i = 1; i < c->argc; i++) {
2186                 if(!lutil_passwd_scheme(c->argv[i])) {
2187                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> scheme not available", c->argv[0] );
2188                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2189                                 c->log, c->cr_msg, c->argv[i]);
2190                 } else {
2191                         ldap_charray_add(&default_passwd_hash, c->argv[i]);
2192                 }
2193         }
2194         if(!default_passwd_hash) {
2195                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> no valid hashes found", c->argv[0] );
2196                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2197                         c->log, c->cr_msg, 0 );
2198                 return(1);
2199         }
2200         return(0);
2201 }
2202
2203 static int
2204 config_schema_dn(ConfigArgs *c) {
2205         if ( c->op == SLAP_CONFIG_EMIT ) {
2206                 int rc = 1;
2207                 if ( !BER_BVISEMPTY( &c->be->be_schemadn )) {
2208                         value_add_one(&c->rvalue_vals, &c->be->be_schemadn);
2209                         value_add_one(&c->rvalue_nvals, &c->be->be_schemandn);
2210                         rc = 0;
2211                 }
2212                 return rc;
2213         } else if ( c->op == LDAP_MOD_DELETE ) {
2214                 ch_free( c->be->be_schemadn.bv_val );
2215                 ch_free( c->be->be_schemandn.bv_val );
2216                 BER_BVZERO( &c->be->be_schemadn );
2217                 BER_BVZERO( &c->be->be_schemandn );
2218                 return 0;
2219         }
2220         ch_free( c->be->be_schemadn.bv_val );
2221         ch_free( c->be->be_schemandn.bv_val );
2222         c->be->be_schemadn = c->value_dn;
2223         c->be->be_schemandn = c->value_ndn;
2224         return(0);
2225 }
2226
2227 static int
2228 config_sizelimit(ConfigArgs *c) {
2229         int i, rc = 0;
2230         struct slap_limits_set *lim = &c->be->be_def_limit;
2231         if (c->op == SLAP_CONFIG_EMIT) {
2232                 char buf[8192];
2233                 struct berval bv;
2234                 bv.bv_val = buf;
2235                 bv.bv_len = 0;
2236                 limits_unparse_one( lim, SLAP_LIMIT_SIZE, &bv, sizeof( buf ) );
2237                 if ( !BER_BVISEMPTY( &bv ))
2238                         value_add_one( &c->rvalue_vals, &bv );
2239                 else
2240                         rc = 1;
2241                 return rc;
2242         } else if ( c->op == LDAP_MOD_DELETE ) {
2243                 /* Reset to defaults or values from frontend */
2244                 if ( c->be == frontendDB ) {
2245                         lim->lms_s_soft = SLAPD_DEFAULT_SIZELIMIT;
2246                         lim->lms_s_hard = 0;
2247                         lim->lms_s_unchecked = -1;
2248                         lim->lms_s_pr = 0;
2249                         lim->lms_s_pr_hide = 0;
2250                         lim->lms_s_pr_total = 0;
2251                 } else {
2252                         lim->lms_s_soft = frontendDB->be_def_limit.lms_s_soft;
2253                         lim->lms_s_hard = frontendDB->be_def_limit.lms_s_hard;
2254                         lim->lms_s_unchecked = frontendDB->be_def_limit.lms_s_unchecked;
2255                         lim->lms_s_pr = frontendDB->be_def_limit.lms_s_pr;
2256                         lim->lms_s_pr_hide = frontendDB->be_def_limit.lms_s_pr_hide;
2257                         lim->lms_s_pr_total = frontendDB->be_def_limit.lms_s_pr_total;
2258                 }
2259                 goto ok;
2260         }
2261         for(i = 1; i < c->argc; i++) {
2262                 if(!strncasecmp(c->argv[i], "size", 4)) {
2263                         rc = limits_parse_one(c->argv[i], lim);
2264                         if ( rc ) {
2265                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse value", c->argv[0] );
2266                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2267                                         c->log, c->cr_msg, c->argv[i]);
2268                                 return(1);
2269                         }
2270                 } else {
2271                         if(!strcasecmp(c->argv[i], "unlimited")) {
2272                                 lim->lms_s_soft = -1;
2273                         } else {
2274                                 if ( lutil_atoix( &lim->lms_s_soft, c->argv[i], 0 ) != 0 ) {
2275                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse limit", c->argv[0]);
2276                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2277                                                 c->log, c->cr_msg, c->argv[i]);
2278                                         return(1);
2279                                 }
2280                         }
2281                         lim->lms_s_hard = 0;
2282                 }
2283         }
2284
2285 ok:
2286         if ( ( c->be == frontendDB ) && ( c->ca_entry ) ) {
2287                 /* This is a modification to the global limits apply it to
2288                  * the other databases as needed */
2289                 AttributeDescription *ad=NULL;
2290                 const char *text = NULL;
2291                 CfEntryInfo *ce = c->ca_entry->e_private;
2292
2293                 slap_str2ad(c->argv[0], &ad, &text);
2294                 /* if we got here... */
2295                 assert( ad != NULL );
2296
2297                 if ( ce->ce_type == Cft_Global ){
2298                         ce = ce->ce_kids;
2299                 }
2300                 for (; ce; ce=ce->ce_sibs) {
2301                         Entry *dbe = ce->ce_entry;
2302                         if ( (ce->ce_type == Cft_Database) && (ce->ce_be != frontendDB)
2303                                         && (!attr_find(dbe->e_attrs, ad)) ) {
2304                                 ce->ce_be->be_def_limit.lms_s_soft = lim->lms_s_soft;
2305                                 ce->ce_be->be_def_limit.lms_s_hard = lim->lms_s_hard;
2306                                 ce->ce_be->be_def_limit.lms_s_unchecked =lim->lms_s_unchecked;
2307                                 ce->ce_be->be_def_limit.lms_s_pr =lim->lms_s_pr;
2308                                 ce->ce_be->be_def_limit.lms_s_pr_hide =lim->lms_s_pr_hide;
2309                                 ce->ce_be->be_def_limit.lms_s_pr_total =lim->lms_s_pr_total;
2310                         }
2311                 }
2312         }
2313         return(0);
2314 }
2315
2316 static int
2317 config_timelimit(ConfigArgs *c) {
2318         int i, rc = 0;
2319         struct slap_limits_set *lim = &c->be->be_def_limit;
2320         if (c->op == SLAP_CONFIG_EMIT) {
2321                 char buf[8192];
2322                 struct berval bv;
2323                 bv.bv_val = buf;
2324                 bv.bv_len = 0;
2325                 limits_unparse_one( lim, SLAP_LIMIT_TIME, &bv, sizeof( buf ) );
2326                 if ( !BER_BVISEMPTY( &bv ))
2327                         value_add_one( &c->rvalue_vals, &bv );
2328                 else
2329                         rc = 1;
2330                 return rc;
2331         } else if ( c->op == LDAP_MOD_DELETE ) {
2332                 /* Reset to defaults or values from frontend */
2333                 if ( c->be == frontendDB ) {
2334                         lim->lms_t_soft = SLAPD_DEFAULT_TIMELIMIT;
2335                         lim->lms_t_hard = 0;
2336                 } else {
2337                         lim->lms_t_soft = frontendDB->be_def_limit.lms_t_soft;
2338                         lim->lms_t_hard = frontendDB->be_def_limit.lms_t_hard;
2339                 }
2340                 goto ok;
2341         }
2342         for(i = 1; i < c->argc; i++) {
2343                 if(!strncasecmp(c->argv[i], "time", 4)) {
2344                         rc = limits_parse_one(c->argv[i], lim);
2345                         if ( rc ) {
2346                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse value", c->argv[0] );
2347                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2348                                         c->log, c->cr_msg, c->argv[i]);
2349                                 return(1);
2350                         }
2351                 } else {
2352                         if(!strcasecmp(c->argv[i], "unlimited")) {
2353                                 lim->lms_t_soft = -1;
2354                         } else {
2355                                 if ( lutil_atoix( &lim->lms_t_soft, c->argv[i], 0 ) != 0 ) {
2356                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse limit", c->argv[0]);
2357                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2358                                                 c->log, c->cr_msg, c->argv[i]);
2359                                         return(1);
2360                                 }
2361                         }
2362                         lim->lms_t_hard = 0;
2363                 }
2364         }
2365
2366 ok:
2367         if ( ( c->be == frontendDB ) && ( c->ca_entry ) ) {
2368                 /* This is a modification to the global limits apply it to
2369                  * the other databases as needed */
2370                 AttributeDescription *ad=NULL;
2371                 const char *text = NULL;
2372                 CfEntryInfo *ce = c->ca_entry->e_private;
2373
2374                 slap_str2ad(c->argv[0], &ad, &text);
2375                 /* if we got here... */
2376                 assert( ad != NULL );
2377
2378                 if ( ce->ce_type == Cft_Global ){
2379                         ce = ce->ce_kids;
2380                 }
2381                 for (; ce; ce=ce->ce_sibs) {
2382                         Entry *dbe = ce->ce_entry;
2383                         if ( (ce->ce_type == Cft_Database) && (ce->ce_be != frontendDB)
2384                                         && (!attr_find(dbe->e_attrs, ad)) ) {
2385                                 ce->ce_be->be_def_limit.lms_t_soft = lim->lms_t_soft;
2386                                 ce->ce_be->be_def_limit.lms_t_hard = lim->lms_t_hard;
2387                         }
2388                 }
2389         }
2390         return(0);
2391 }
2392
2393 static int
2394 config_overlay(ConfigArgs *c) {
2395         if (c->op == SLAP_CONFIG_EMIT) {
2396                 return 1;
2397         } else if ( c->op == LDAP_MOD_DELETE ) {
2398                 assert(0);
2399         }
2400         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1],
2401                 c->valx, &c->bi, &c->reply)) {
2402                 /* log error */
2403                 Debug( LDAP_DEBUG_ANY,
2404                         "%s: (optional) %s overlay \"%s\" configuration failed.\n",
2405                         c->log, c->be == frontendDB ? "global " : "", &c->argv[1][1]);
2406                 return 1;
2407         } else if(overlay_config(c->be, c->argv[1], c->valx, &c->bi, &c->reply)) {
2408                 return(1);
2409         }
2410         return(0);
2411 }
2412
2413 static int
2414 config_subordinate(ConfigArgs *c)
2415 {
2416         int rc = 1;
2417         int advertise = 0;
2418
2419         switch( c->op ) {
2420         case SLAP_CONFIG_EMIT:
2421                 if ( SLAP_GLUE_SUBORDINATE( c->be )) {
2422                         struct berval bv;
2423
2424                         bv.bv_val = SLAP_GLUE_ADVERTISE( c->be ) ? "advertise" : "TRUE";
2425                         bv.bv_len = SLAP_GLUE_ADVERTISE( c->be ) ? STRLENOF("advertise") :
2426                                 STRLENOF("TRUE");
2427
2428                         value_add_one( &c->rvalue_vals, &bv );
2429                         rc = 0;
2430                 }
2431                 break;
2432         case LDAP_MOD_DELETE:
2433                 if ( !c->line  || strcasecmp( c->line, "advertise" )) {
2434                         glue_sub_del( c->be );
2435                 } else {
2436                         SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_GLUE_ADVERTISE;
2437                 }
2438                 rc = 0;
2439                 break;
2440         case LDAP_MOD_ADD:
2441         case SLAP_CONFIG_ADD:
2442                 if ( c->be->be_nsuffix == NULL ) {
2443                         /* log error */
2444                         snprintf( c->cr_msg, sizeof( c->cr_msg),
2445                                 "subordinate configuration needs a suffix" );
2446                         Debug( LDAP_DEBUG_ANY,
2447                                 "%s: %s.\n",
2448                                 c->log, c->cr_msg, 0 );
2449                         rc = 1;
2450                         break;
2451                 }
2452
2453                 if ( c->argc == 2 ) {
2454                         if ( strcasecmp( c->argv[1], "advertise" ) == 0 ) {
2455                                 advertise = 1;
2456
2457                         } else if ( strcasecmp( c->argv[1], "TRUE" ) != 0 ) {
2458                                 /* log error */
2459                                 snprintf( c->cr_msg, sizeof( c->cr_msg),
2460                                         "subordinate must be \"TRUE\" or \"advertise\"" );
2461                                 Debug( LDAP_DEBUG_ANY,
2462                                         "%s: suffix \"%s\": %s.\n",
2463                                         c->log, c->be->be_suffix[0].bv_val, c->cr_msg );
2464                                 rc = 1;
2465                                 break;
2466                         }
2467                 }
2468
2469                 rc = glue_sub_add( c->be, advertise, CONFIG_ONLINE_ADD( c ));
2470                 break;
2471         }
2472
2473         return rc;
2474 }
2475
2476 /*
2477  * [listener=<listener>] [{read|write}=]<size>
2478  */
2479
2480 #ifdef LDAP_TCP_BUFFER
2481 static BerVarray tcp_buffer;
2482 int tcp_buffer_num;
2483
2484 #define SLAP_TCP_RMEM (0x1U)
2485 #define SLAP_TCP_WMEM (0x2U)
2486
2487 static int
2488 tcp_buffer_parse( struct berval *val, int argc, char **argv,
2489                 int *size, int *rw, Listener **l )
2490 {
2491         int i, rc = LDAP_SUCCESS;
2492         LDAPURLDesc *lud = NULL;
2493         char *ptr;
2494
2495         if ( val != NULL && argv == NULL ) {
2496                 char *s = val->bv_val;
2497
2498                 argv = ldap_str2charray( s, " \t" );
2499                 if ( argv == NULL ) {
2500                         return LDAP_OTHER;
2501                 }
2502         }
2503
2504         i = 0;
2505         if ( strncasecmp( argv[ i ], "listener=", STRLENOF( "listener=" ) )
2506                 == 0 )
2507         {
2508                 char *url = argv[ i ] + STRLENOF( "listener=" );
2509                 
2510                 if ( ldap_url_parse( url, &lud ) ) {
2511                         rc = LDAP_INVALID_SYNTAX;
2512                         goto done;
2513                 }
2514
2515                 *l = config_check_my_url( url, lud );
2516                 if ( *l == NULL ) {
2517                         rc = LDAP_NO_SUCH_ATTRIBUTE;
2518                         goto done;
2519                 }
2520
2521                 i++;
2522         }
2523
2524         ptr = argv[ i ];
2525         if ( strncasecmp( ptr, "read=", STRLENOF( "read=" ) ) == 0 ) {
2526                 *rw |= SLAP_TCP_RMEM;
2527                 ptr += STRLENOF( "read=" );
2528
2529         } else if ( strncasecmp( ptr, "write=", STRLENOF( "write=" ) ) == 0 ) {
2530                 *rw |= SLAP_TCP_WMEM;
2531                 ptr += STRLENOF( "write=" );
2532
2533         } else {
2534                 *rw |= ( SLAP_TCP_RMEM | SLAP_TCP_WMEM );
2535         }
2536
2537         /* accept any base */
2538         if ( lutil_atoix( size, ptr, 0 ) ) {
2539                 rc = LDAP_INVALID_SYNTAX;
2540                 goto done;
2541         }
2542
2543 done:;
2544         if ( val != NULL && argv != NULL ) {
2545                 ldap_charray_free( argv );
2546         }
2547
2548         if ( lud != NULL ) {
2549                 ldap_free_urldesc( lud );
2550         }
2551
2552         return rc;
2553 }
2554
2555 static int
2556 tcp_buffer_delete_one( struct berval *val )
2557 {
2558         int rc = 0;
2559         int size = -1, rw = 0;
2560         Listener *l = NULL;
2561
2562         rc = tcp_buffer_parse( val, 0, NULL, &size, &rw, &l );
2563         if ( rc != 0 ) {
2564                 return rc;
2565         }
2566
2567         if ( l != NULL ) {
2568                 int i;
2569                 Listener **ll = slapd_get_listeners();
2570
2571                 for ( i = 0; ll[ i ] != NULL; i++ ) {
2572                         if ( ll[ i ] == l ) break;
2573                 }
2574
2575                 if ( ll[ i ] == NULL ) {
2576                         return LDAP_NO_SUCH_ATTRIBUTE;
2577                 }
2578
2579                 if ( rw & SLAP_TCP_RMEM ) l->sl_tcp_rmem = -1;
2580                 if ( rw & SLAP_TCP_WMEM ) l->sl_tcp_wmem = -1;
2581
2582                 for ( i++ ; ll[ i ] != NULL && bvmatch( &l->sl_url, &ll[ i ]->sl_url ); i++ ) {
2583                         if ( rw & SLAP_TCP_RMEM ) ll[ i ]->sl_tcp_rmem = -1;
2584                         if ( rw & SLAP_TCP_WMEM ) ll[ i ]->sl_tcp_wmem = -1;
2585                 }
2586
2587         } else {
2588                 /* NOTE: this affects listeners without a specific setting,
2589                  * does not reset all listeners.  If a listener without
2590                  * specific settings was assigned a buffer because of
2591                  * a global setting, it will not be reset.  In any case,
2592                  * buffer changes will only take place at restart. */
2593                 if ( rw & SLAP_TCP_RMEM ) slapd_tcp_rmem = -1;
2594                 if ( rw & SLAP_TCP_WMEM ) slapd_tcp_wmem = -1;
2595         }
2596
2597         return rc;
2598 }
2599
2600 static int
2601 tcp_buffer_delete( BerVarray vals )
2602 {
2603         int i;
2604
2605         for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
2606                 tcp_buffer_delete_one( &vals[ i ] );
2607         }
2608
2609         return 0;
2610 }
2611
2612 static int
2613 tcp_buffer_unparse( int size, int rw, Listener *l, struct berval *val )
2614 {
2615         char buf[sizeof("2147483648")], *ptr;
2616
2617         /* unparse for later use */
2618         val->bv_len = snprintf( buf, sizeof( buf ), "%d", size );
2619         if ( l != NULL ) {
2620                 val->bv_len += STRLENOF( "listener=" " " ) + l->sl_url.bv_len;
2621         }
2622
2623         if ( rw != ( SLAP_TCP_RMEM | SLAP_TCP_WMEM ) ) {
2624                 if ( rw & SLAP_TCP_RMEM ) {
2625                         val->bv_len += STRLENOF( "read=" );
2626                 } else if ( rw & SLAP_TCP_WMEM ) {
2627                         val->bv_len += STRLENOF( "write=" );
2628                 }
2629         }
2630
2631         val->bv_val = SLAP_MALLOC( val->bv_len + 1 );
2632
2633         ptr = val->bv_val;
2634
2635         if ( l != NULL ) {
2636                 ptr = lutil_strcopy( ptr, "listener=" );
2637                 ptr = lutil_strncopy( ptr, l->sl_url.bv_val, l->sl_url.bv_len );
2638                 *ptr++ = ' ';
2639         }
2640
2641         if ( rw != ( SLAP_TCP_RMEM | SLAP_TCP_WMEM ) ) {
2642                 if ( rw & SLAP_TCP_RMEM ) {
2643                         ptr = lutil_strcopy( ptr, "read=" );
2644                 } else if ( rw & SLAP_TCP_WMEM ) {
2645                         ptr = lutil_strcopy( ptr, "write=" );
2646                 }
2647         }
2648
2649         ptr = lutil_strcopy( ptr, buf );
2650         *ptr = '\0';
2651
2652         assert( val->bv_val + val->bv_len == ptr );
2653
2654         return LDAP_SUCCESS;
2655 }
2656
2657 static int
2658 tcp_buffer_add_one( int argc, char **argv )
2659 {
2660         int rc = 0;
2661         int size = -1, rw = 0;
2662         Listener *l = NULL;
2663
2664         struct berval val;
2665
2666         /* parse */
2667         rc = tcp_buffer_parse( NULL, argc, argv, &size, &rw, &l );
2668         if ( rc != 0 ) {
2669                 return rc;
2670         }
2671
2672         /* unparse for later use */
2673         rc = tcp_buffer_unparse( size, rw, l, &val );
2674         if ( rc != LDAP_SUCCESS ) {
2675                 return rc;
2676         }
2677
2678         /* use parsed values */
2679         if ( l != NULL ) {
2680                 int i;
2681                 Listener **ll = slapd_get_listeners();
2682
2683                 for ( i = 0; ll[ i ] != NULL; i++ ) {
2684                         if ( ll[ i ] == l ) break;
2685                 }
2686
2687                 if ( ll[ i ] == NULL ) {
2688                         return LDAP_NO_SUCH_ATTRIBUTE;
2689                 }
2690
2691                 /* buffer only applies to TCP listeners;
2692                  * we do not do any check here, and delegate them
2693                  * to setsockopt(2) */
2694                 if ( rw & SLAP_TCP_RMEM ) l->sl_tcp_rmem = size;
2695                 if ( rw & SLAP_TCP_WMEM ) l->sl_tcp_wmem = size;
2696
2697                 for ( i++ ; ll[ i ] != NULL && bvmatch( &l->sl_url, &ll[ i ]->sl_url ); i++ ) {
2698                         if ( rw & SLAP_TCP_RMEM ) ll[ i ]->sl_tcp_rmem = size;
2699                         if ( rw & SLAP_TCP_WMEM ) ll[ i ]->sl_tcp_wmem = size;
2700                 }
2701
2702         } else {
2703                 /* NOTE: this affects listeners without a specific setting,
2704                  * does not set all listeners */
2705                 if ( rw & SLAP_TCP_RMEM ) slapd_tcp_rmem = size;
2706                 if ( rw & SLAP_TCP_WMEM ) slapd_tcp_wmem = size;
2707         }
2708
2709         tcp_buffer = SLAP_REALLOC( tcp_buffer, sizeof( struct berval ) * ( tcp_buffer_num + 2 ) );
2710         /* append */
2711         tcp_buffer[ tcp_buffer_num ] = val;
2712
2713         tcp_buffer_num++;
2714         BER_BVZERO( &tcp_buffer[ tcp_buffer_num ] );
2715
2716         return rc;
2717 }
2718
2719 static int
2720 config_tcp_buffer( ConfigArgs *c )
2721 {
2722         if ( c->op == SLAP_CONFIG_EMIT ) {
2723                 if ( tcp_buffer == NULL || BER_BVISNULL( &tcp_buffer[ 0 ] ) ) {
2724                         return 1;
2725                 }
2726                 value_add( &c->rvalue_vals, tcp_buffer );
2727                 value_add( &c->rvalue_nvals, tcp_buffer );
2728                 
2729         } else if ( c->op == LDAP_MOD_DELETE ) {
2730                 if ( !c->line  ) {
2731                         tcp_buffer_delete( tcp_buffer );
2732                         ber_bvarray_free( tcp_buffer );
2733                         tcp_buffer = NULL;
2734                         tcp_buffer_num = 0;
2735
2736                 } else {
2737                         int rc = 0;
2738                         int size = -1, rw = 0;
2739                         Listener *l = NULL;
2740
2741                         struct berval val = BER_BVNULL;
2742
2743                         int i;
2744
2745                         if ( tcp_buffer_num == 0 ) {
2746                                 return 1;
2747                         }
2748
2749                         /* parse */
2750                         rc = tcp_buffer_parse( NULL, c->argc - 1, &c->argv[ 1 ], &size, &rw, &l );
2751                         if ( rc != 0 ) {
2752                                 return 1;
2753                         }
2754
2755                         /* unparse for later use */
2756                         rc = tcp_buffer_unparse( size, rw, l, &val );
2757                         if ( rc != LDAP_SUCCESS ) {
2758                                 return 1;
2759                         }
2760
2761                         for ( i = 0; !BER_BVISNULL( &tcp_buffer[ i ] ); i++ ) {
2762                                 if ( bvmatch( &tcp_buffer[ i ], &val ) ) {
2763                                         break;
2764                                 }
2765                         }
2766
2767                         if ( BER_BVISNULL( &tcp_buffer[ i ] ) ) {
2768                                 /* not found */
2769                                 rc = 1;
2770                                 goto done;
2771                         }
2772
2773                         tcp_buffer_delete_one( &tcp_buffer[ i ] );
2774                         ber_memfree( tcp_buffer[ i ].bv_val );
2775                         for ( ; i < tcp_buffer_num; i++ ) {
2776                                 tcp_buffer[ i ] = tcp_buffer[ i + 1 ];
2777                         }
2778                         tcp_buffer_num--;
2779
2780 done:;
2781                         if ( !BER_BVISNULL( &val ) ) {
2782                                 SLAP_FREE( val.bv_val );
2783                         }
2784         
2785                 }
2786
2787         } else {
2788                 int rc;
2789
2790                 rc = tcp_buffer_add_one( c->argc - 1, &c->argv[ 1 ] );
2791                 if ( rc ) {
2792                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
2793                                 "<%s> unable to add value #%d",
2794                                 c->argv[0], tcp_buffer_num );
2795                         Debug( LDAP_DEBUG_ANY, "%s: %s\n",
2796                                 c->log, c->cr_msg, 0 );
2797                         return 1;
2798                 }
2799         }
2800
2801         return 0;
2802 }
2803 #endif /* LDAP_TCP_BUFFER */
2804
2805 static int
2806 config_suffix(ConfigArgs *c)
2807 {
2808         Backend *tbe;
2809         struct berval pdn, ndn;
2810         char    *notallowed = NULL;
2811
2812         if ( c->be == frontendDB ) {
2813                 notallowed = "frontend";
2814
2815         } else if ( SLAP_MONITOR(c->be) ) {
2816                 notallowed = "monitor";
2817
2818         } else if ( SLAP_CONFIG(c->be) ) {
2819                 notallowed = "config";
2820         }
2821
2822         if ( notallowed != NULL ) {
2823                 char    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
2824
2825                 switch ( c->op ) {
2826                 case LDAP_MOD_ADD:
2827                 case LDAP_MOD_DELETE:
2828                 case LDAP_MOD_REPLACE:
2829                 case LDAP_MOD_INCREMENT:
2830                 case SLAP_CONFIG_ADD:
2831                         if ( !BER_BVISNULL( &c->value_dn ) ) {
2832                                 snprintf( buf, sizeof( buf ), "<%s> ",
2833                                                 c->value_dn.bv_val );
2834                         }
2835
2836                         Debug(LDAP_DEBUG_ANY,
2837                                 "%s: suffix %snot allowed in %s database.\n",
2838                                 c->log, buf, notallowed );
2839                         break;
2840
2841                 case SLAP_CONFIG_EMIT:
2842                         /* don't complain when emitting... */
2843                         break;
2844
2845                 default:
2846                         /* FIXME: don't know what values may be valid;
2847                          * please remove assertion, or add legal values
2848                          * to either block */
2849                         assert( 0 );
2850                         break;
2851                 }
2852
2853                 return 1;
2854         }
2855
2856         if (c->op == SLAP_CONFIG_EMIT) {
2857                 if ( c->be->be_suffix == NULL
2858                                 || BER_BVISNULL( &c->be->be_suffix[0] ) )
2859                 {
2860                         return 1;
2861                 } else {
2862                         value_add( &c->rvalue_vals, c->be->be_suffix );
2863                         value_add( &c->rvalue_nvals, c->be->be_nsuffix );
2864                         return 0;
2865                 }
2866         } else if ( c->op == LDAP_MOD_DELETE ) {
2867                 if ( c->valx < 0 ) {
2868                         ber_bvarray_free( c->be->be_suffix );
2869                         ber_bvarray_free( c->be->be_nsuffix );
2870                         c->be->be_suffix = NULL;
2871                         c->be->be_nsuffix = NULL;
2872                 } else {
2873                         int i = c->valx;
2874                         ch_free( c->be->be_suffix[i].bv_val );
2875                         ch_free( c->be->be_nsuffix[i].bv_val );
2876                         do {
2877                                 c->be->be_suffix[i] = c->be->be_suffix[i+1];
2878                                 c->be->be_nsuffix[i] = c->be->be_nsuffix[i+1];
2879                                 i++;
2880                         } while ( !BER_BVISNULL( &c->be->be_suffix[i] ) );
2881                 }
2882                 return 0;
2883         }
2884
2885 #ifdef SLAPD_MONITOR_DN
2886         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
2887                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> DN is reserved for monitoring slapd",
2888                         c->argv[0] );
2889                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2890                         c->log, c->cr_msg, SLAPD_MONITOR_DN);
2891                 return(1);
2892         }
2893 #endif
2894
2895         if (SLAP_DB_ONE_SUFFIX( c->be ) && c->be->be_suffix ) {
2896                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> Only one suffix is allowed on this %s backend",
2897                         c->argv[0], c->be->bd_info->bi_type );
2898                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2899                         c->log, c->cr_msg, 0);
2900                 return(1);
2901         }
2902
2903         pdn = c->value_dn;
2904         ndn = c->value_ndn;
2905
2906         if (SLAP_DBHIDDEN( c->be ))
2907                 tbe = NULL;
2908         else
2909                 tbe = select_backend(&ndn, 0);
2910         if(tbe == c->be) {
2911                 Debug( LDAP_DEBUG_ANY, "%s: suffix already served by this backend!.\n",
2912                         c->log, 0, 0);
2913                 return 1;
2914                 free(pdn.bv_val);
2915                 free(ndn.bv_val);
2916         } else if(tbe) {
2917                 BackendDB *b2 = tbe;
2918
2919                 /* Does tbe precede be? */
2920                 while (( b2 = LDAP_STAILQ_NEXT(b2, be_next )) && b2 && b2 != c->be );
2921
2922                 if ( b2 ) {
2923                         char    *type = tbe->bd_info->bi_type;
2924
2925                         if ( overlay_is_over( tbe ) ) {
2926                                 slap_overinfo   *oi = (slap_overinfo *)tbe->bd_info->bi_private;
2927                                 type = oi->oi_orig->bi_type;
2928                         }
2929
2930                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> namingContext \"%s\" "
2931                                 "already served by a preceding %s database",
2932                                 c->argv[0], pdn.bv_val, type );
2933                         Debug(LDAP_DEBUG_ANY, "%s: %s serving namingContext \"%s\"\n",
2934                                 c->log, c->cr_msg, tbe->be_suffix[0].bv_val);
2935                         free(pdn.bv_val);
2936                         free(ndn.bv_val);
2937                         return(1);
2938                 }
2939         }
2940         if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
2941                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
2942                         "base provided \"%s\" (assuming okay)\n",
2943                         c->log, default_search_base.bv_val, 0);
2944         }
2945         ber_bvarray_add(&c->be->be_suffix, &pdn);
2946         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
2947         return(0);
2948 }
2949
2950 static int
2951 config_rootdn(ConfigArgs *c) {
2952         if (c->op == SLAP_CONFIG_EMIT) {
2953                 if ( !BER_BVISNULL( &c->be->be_rootdn )) {
2954                         value_add_one(&c->rvalue_vals, &c->be->be_rootdn);
2955                         value_add_one(&c->rvalue_nvals, &c->be->be_rootndn);
2956                         return 0;
2957                 } else {
2958                         return 1;
2959                 }
2960         } else if ( c->op == LDAP_MOD_DELETE ) {
2961                 ch_free( c->be->be_rootdn.bv_val );
2962                 ch_free( c->be->be_rootndn.bv_val );
2963                 BER_BVZERO( &c->be->be_rootdn );
2964                 BER_BVZERO( &c->be->be_rootndn );
2965                 return 0;
2966         }
2967         if ( !BER_BVISNULL( &c->be->be_rootdn )) {
2968                 ch_free( c->be->be_rootdn.bv_val );
2969                 ch_free( c->be->be_rootndn.bv_val );
2970         }
2971         c->be->be_rootdn = c->value_dn;
2972         c->be->be_rootndn = c->value_ndn;
2973         return(0);
2974 }
2975
2976 static int
2977 config_rootpw(ConfigArgs *c) {
2978         Backend *tbe;
2979
2980         if (c->op == SLAP_CONFIG_EMIT) {
2981                 if (!BER_BVISEMPTY(&c->be->be_rootpw)) {
2982                         /* don't copy, because "rootpw" is marked
2983                          * as CFG_BERVAL */
2984                         c->value_bv = c->be->be_rootpw;
2985                         return 0;
2986                 }
2987                 return 1;
2988         } else if ( c->op == LDAP_MOD_DELETE ) {
2989                 ch_free( c->be->be_rootpw.bv_val );
2990                 BER_BVZERO( &c->be->be_rootpw );
2991                 return 0;
2992         }
2993
2994         tbe = select_backend(&c->be->be_rootndn, 0);
2995         if(tbe != c->be) {
2996                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> can only be set when rootdn is under suffix",
2997                         c->argv[0] );
2998                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2999                         c->log, c->cr_msg, 0);
3000                 return(1);
3001         }
3002         if ( !BER_BVISNULL( &c->be->be_rootpw ))
3003                 ch_free( c->be->be_rootpw.bv_val );
3004         c->be->be_rootpw = c->value_bv;
3005         return(0);
3006 }
3007
3008 static int
3009 config_restrict(ConfigArgs *c) {
3010         slap_mask_t restrictops = 0;
3011         int i;
3012         slap_verbmasks restrictable_ops[] = {
3013                 { BER_BVC("bind"),              SLAP_RESTRICT_OP_BIND },
3014                 { BER_BVC("add"),               SLAP_RESTRICT_OP_ADD },
3015                 { BER_BVC("modify"),            SLAP_RESTRICT_OP_MODIFY },
3016                 { BER_BVC("rename"),            SLAP_RESTRICT_OP_RENAME },
3017                 { BER_BVC("modrdn"),            0 },
3018                 { BER_BVC("delete"),            SLAP_RESTRICT_OP_DELETE },
3019                 { BER_BVC("search"),            SLAP_RESTRICT_OP_SEARCH },
3020                 { BER_BVC("compare"),           SLAP_RESTRICT_OP_COMPARE },
3021                 { BER_BVC("read"),              SLAP_RESTRICT_OP_READS },
3022                 { BER_BVC("write"),             SLAP_RESTRICT_OP_WRITES },
3023                 { BER_BVC("extended"),          SLAP_RESTRICT_OP_EXTENDED },
3024                 { BER_BVC("extended=" LDAP_EXOP_START_TLS ),            SLAP_RESTRICT_EXOP_START_TLS },
3025                 { BER_BVC("extended=" LDAP_EXOP_MODIFY_PASSWD ),        SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
3026                 { BER_BVC("extended=" LDAP_EXOP_X_WHO_AM_I ),           SLAP_RESTRICT_EXOP_WHOAMI },
3027                 { BER_BVC("extended=" LDAP_EXOP_X_CANCEL ),             SLAP_RESTRICT_EXOP_CANCEL },
3028                 { BER_BVC("all"),               SLAP_RESTRICT_OP_ALL },
3029                 { BER_BVNULL,   0 }
3030         };
3031
3032         if (c->op == SLAP_CONFIG_EMIT) {
3033                 return mask_to_verbs( restrictable_ops, c->be->be_restrictops,
3034                         &c->rvalue_vals );
3035         } else if ( c->op == LDAP_MOD_DELETE ) {
3036                 if ( !c->line ) {
3037                         c->be->be_restrictops = 0;
3038                 } else {
3039                         restrictops = verb_to_mask( c->line, restrictable_ops );
3040                         c->be->be_restrictops ^= restrictops;
3041                 }
3042                 return 0;
3043         }
3044         i = verbs_to_mask( c->argc, c->argv, restrictable_ops, &restrictops );
3045         if ( i ) {
3046                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown operation", c->argv[0] );
3047                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
3048                         c->log, c->cr_msg, c->argv[i]);
3049                 return(1);
3050         }
3051         if ( restrictops & SLAP_RESTRICT_OP_EXTENDED )
3052                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
3053         c->be->be_restrictops |= restrictops;
3054         return(0);
3055 }
3056
3057 static int
3058 config_allows(ConfigArgs *c) {
3059         slap_mask_t allows = 0;
3060         int i;
3061         slap_verbmasks allowable_ops[] = {
3062                 { BER_BVC("bind_v2"),           SLAP_ALLOW_BIND_V2 },
3063                 { BER_BVC("bind_anon_cred"),    SLAP_ALLOW_BIND_ANON_CRED },
3064                 { BER_BVC("bind_anon_dn"),      SLAP_ALLOW_BIND_ANON_DN },
3065                 { BER_BVC("update_anon"),       SLAP_ALLOW_UPDATE_ANON },
3066                 { BER_BVC("proxy_authz_anon"),  SLAP_ALLOW_PROXY_AUTHZ_ANON },
3067                 { BER_BVNULL,   0 }
3068         };
3069         if (c->op == SLAP_CONFIG_EMIT) {
3070                 return mask_to_verbs( allowable_ops, global_allows, &c->rvalue_vals );
3071         } else if ( c->op == LDAP_MOD_DELETE ) {
3072                 if ( !c->line ) {
3073                         global_allows = 0;
3074                 } else {
3075                         allows = verb_to_mask( c->line, allowable_ops );
3076                         global_allows ^= allows;
3077                 }
3078                 return 0;
3079         }
3080         i = verbs_to_mask(c->argc, c->argv, allowable_ops, &allows);
3081         if ( i ) {
3082                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown feature", c->argv[0] );
3083                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
3084                         c->log, c->cr_msg, c->argv[i]);
3085                 return(1);
3086         }
3087         global_allows |= allows;
3088         return(0);
3089 }
3090
3091 static int
3092 config_disallows(ConfigArgs *c) {
3093         slap_mask_t disallows = 0;
3094         int i;
3095         slap_verbmasks disallowable_ops[] = {
3096                 { BER_BVC("bind_anon"),         SLAP_DISALLOW_BIND_ANON },
3097                 { BER_BVC("bind_simple"),       SLAP_DISALLOW_BIND_SIMPLE },
3098                 { BER_BVC("tls_2_anon"),                SLAP_DISALLOW_TLS_2_ANON },
3099                 { BER_BVC("tls_authc"),         SLAP_DISALLOW_TLS_AUTHC },
3100                 { BER_BVC("proxy_authz_non_critical"),  SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT },
3101                 { BER_BVC("dontusecopy_non_critical"),  SLAP_DISALLOW_DONTUSECOPY_N_CRIT },
3102                 { BER_BVNULL, 0 }
3103         };
3104         if (c->op == SLAP_CONFIG_EMIT) {
3105                 return mask_to_verbs( disallowable_ops, global_disallows, &c->rvalue_vals );
3106         } else if ( c->op == LDAP_MOD_DELETE ) {
3107                 if ( !c->line ) {
3108                         global_disallows = 0;
3109                 } else {
3110                         disallows = verb_to_mask( c->line, disallowable_ops );
3111                         global_disallows ^= disallows;
3112                 }
3113                 return 0;
3114         }
3115         i = verbs_to_mask(c->argc, c->argv, disallowable_ops, &disallows);
3116         if ( i ) {
3117                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown feature", c->argv[0] );
3118                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
3119                         c->log, c->cr_msg, c->argv[i]);
3120                 return(1);
3121         }
3122         global_disallows |= disallows;
3123         return(0);
3124 }
3125
3126 static int
3127 config_requires(ConfigArgs *c) {
3128         slap_mask_t requires = frontendDB->be_requires;
3129         int i, argc = c->argc;
3130         char **argv = c->argv;
3131
3132         slap_verbmasks requires_ops[] = {
3133                 { BER_BVC("bind"),              SLAP_REQUIRE_BIND },
3134                 { BER_BVC("LDAPv3"),            SLAP_REQUIRE_LDAP_V3 },
3135                 { BER_BVC("authc"),             SLAP_REQUIRE_AUTHC },
3136                 { BER_BVC("sasl"),              SLAP_REQUIRE_SASL },
3137                 { BER_BVC("strong"),            SLAP_REQUIRE_STRONG },
3138                 { BER_BVNULL, 0 }
3139         };
3140         if (c->op == SLAP_CONFIG_EMIT) {
3141                 return mask_to_verbs( requires_ops, c->be->be_requires, &c->rvalue_vals );
3142         } else if ( c->op == LDAP_MOD_DELETE ) {
3143                 if ( !c->line ) {
3144                         c->be->be_requires = 0;
3145                 } else {
3146                         requires = verb_to_mask( c->line, requires_ops );
3147                         c->be->be_requires ^= requires;
3148                 }
3149                 return 0;
3150         }
3151         /* "none" can only be first, to wipe out default/global values */
3152         if ( strcasecmp( c->argv[ 1 ], "none" ) == 0 ) {
3153                 argv++;
3154                 argc--;
3155                 requires = 0;
3156         }
3157         i = verbs_to_mask(argc, argv, requires_ops, &requires);
3158         if ( i ) {
3159                 if (strcasecmp( c->argv[ i ], "none" ) == 0 ) {
3160                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> \"none\" (#%d) must be listed first", c->argv[0], i - 1 );
3161                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
3162                                 c->log, c->cr_msg, 0);
3163                 } else {
3164                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown feature #%d", c->argv[0], i - 1 );
3165                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
3166                                 c->log, c->cr_msg, c->argv[i]);
3167                 }
3168                 return(1);
3169         }
3170         c->be->be_requires = requires;
3171         return(0);
3172 }
3173
3174 static slap_verbmasks   *loglevel_ops;
3175
3176 static int
3177 loglevel_init( void )
3178 {
3179         slap_verbmasks  lo[] = {
3180                 { BER_BVC("Any"),       (slap_mask_t) LDAP_DEBUG_ANY },
3181                 { BER_BVC("Trace"),     LDAP_DEBUG_TRACE },
3182                 { BER_BVC("Packets"),   LDAP_DEBUG_PACKETS },
3183                 { BER_BVC("Args"),      LDAP_DEBUG_ARGS },
3184                 { BER_BVC("Conns"),     LDAP_DEBUG_CONNS },
3185                 { BER_BVC("BER"),       LDAP_DEBUG_BER },
3186                 { BER_BVC("Filter"),    LDAP_DEBUG_FILTER },
3187                 { BER_BVC("Config"),    LDAP_DEBUG_CONFIG },
3188                 { BER_BVC("ACL"),       LDAP_DEBUG_ACL },
3189                 { BER_BVC("Stats"),     LDAP_DEBUG_STATS },
3190                 { BER_BVC("Stats2"),    LDAP_DEBUG_STATS2 },
3191                 { BER_BVC("Shell"),     LDAP_DEBUG_SHELL },
3192                 { BER_BVC("Parse"),     LDAP_DEBUG_PARSE },
3193 #if 0   /* no longer used (nor supported) */
3194                 { BER_BVC("Cache"),     LDAP_DEBUG_CACHE },
3195                 { BER_BVC("Index"),     LDAP_DEBUG_INDEX },
3196 #endif
3197                 { BER_BVC("Sync"),      LDAP_DEBUG_SYNC },
3198                 { BER_BVC("None"),      LDAP_DEBUG_NONE },
3199                 { BER_BVNULL,           0 }
3200         };
3201
3202         return slap_verbmasks_init( &loglevel_ops, lo );
3203 }
3204
3205 static void
3206 loglevel_destroy( void )
3207 {
3208         if ( loglevel_ops ) {
3209                 (void)slap_verbmasks_destroy( loglevel_ops );
3210         }
3211         loglevel_ops = NULL;
3212 }
3213
3214 static slap_mask_t      loglevel_ignore[] = { -1, 0 };
3215
3216 int
3217 slap_loglevel_register( slap_mask_t m, struct berval *s )
3218 {
3219         int     rc;
3220
3221         if ( loglevel_ops == NULL ) {
3222                 loglevel_init();
3223         }
3224
3225         rc = slap_verbmasks_append( &loglevel_ops, m, s, loglevel_ignore );
3226
3227         if ( rc != 0 ) {
3228                 Debug( LDAP_DEBUG_ANY, "slap_loglevel_register(%lu, \"%s\") failed\n",
3229                         m, s->bv_val, 0 );
3230         }
3231
3232         return rc;
3233 }
3234
3235 int
3236 slap_loglevel_get( struct berval *s, int *l )
3237 {
3238         int             rc;
3239         slap_mask_t     m, i;
3240
3241         if ( loglevel_ops == NULL ) {
3242                 loglevel_init();
3243         }
3244
3245         for ( m = 0, i = 1; !BER_BVISNULL( &loglevel_ops[ i ].word ); i++ ) {
3246                 m |= loglevel_ops[ i ].mask;
3247         }
3248
3249         for ( i = 1; m & i; i <<= 1 )
3250                 ;
3251
3252         if ( i == 0 ) {
3253                 return -1;
3254         }
3255
3256         rc = slap_verbmasks_append( &loglevel_ops, i, s, loglevel_ignore );
3257
3258         if ( rc != 0 ) {
3259                 Debug( LDAP_DEBUG_ANY, "slap_loglevel_get(%lu, \"%s\") failed\n",
3260                         i, s->bv_val, 0 );
3261
3262         } else {
3263                 *l = i;
3264         }
3265
3266         return rc;
3267 }
3268
3269 int
3270 str2loglevel( const char *s, int *l )
3271 {
3272         int     i;
3273
3274         if ( loglevel_ops == NULL ) {
3275                 loglevel_init();
3276         }
3277
3278         i = verb_to_mask( s, loglevel_ops );
3279
3280         if ( BER_BVISNULL( &loglevel_ops[ i ].word ) ) {
3281                 return -1;
3282         }
3283
3284         *l = loglevel_ops[ i ].mask;
3285
3286         return 0;
3287 }
3288
3289 const char *
3290 loglevel2str( int l )
3291 {
3292         struct berval   bv = BER_BVNULL;
3293
3294         loglevel2bv( l, &bv );
3295
3296         return bv.bv_val;
3297 }
3298
3299 int
3300 loglevel2bv( int l, struct berval *bv )
3301 {
3302         if ( loglevel_ops == NULL ) {
3303                 loglevel_init();
3304         }
3305
3306         BER_BVZERO( bv );
3307
3308         return enum_to_verb( loglevel_ops, l, bv ) == -1;
3309 }
3310
3311 int
3312 loglevel2bvarray( int l, BerVarray *bva )
3313 {
3314         if ( loglevel_ops == NULL ) {
3315                 loglevel_init();
3316         }
3317
3318         return mask_to_verbs( loglevel_ops, l, bva );
3319 }
3320
3321 int
3322 loglevel_print( FILE *out )
3323 {
3324         int     i;
3325
3326         if ( loglevel_ops == NULL ) {
3327                 loglevel_init();
3328         }
3329
3330         fprintf( out, "Installed log subsystems:\n\n" );
3331         for ( i = 0; !BER_BVISNULL( &loglevel_ops[ i ].word ); i++ ) {
3332                 unsigned mask = loglevel_ops[ i ].mask & 0xffffffffUL;
3333                 fprintf( out,
3334                         (mask == ((slap_mask_t) -1 & 0xffffffffUL)
3335                          ? "\t%-30s (-1, 0xffffffff)\n" : "\t%-30s (%u, 0x%x)\n"),
3336                         loglevel_ops[ i ].word.bv_val, mask, mask );
3337         }
3338
3339         fprintf( out, "\nNOTE: custom log subsystems may be later installed "
3340                 "by specific code\n\n" );
3341
3342         return 0;
3343 }
3344
3345 static int config_syslog;
3346
3347 static int
3348 config_loglevel(ConfigArgs *c) {
3349         int i;
3350
3351         if ( loglevel_ops == NULL ) {
3352                 loglevel_init();
3353         }
3354
3355         if (c->op == SLAP_CONFIG_EMIT) {
3356                 /* Get default or commandline slapd setting */
3357                 if ( ldap_syslog && !config_syslog )
3358                         config_syslog = ldap_syslog;
3359                 return loglevel2bvarray( config_syslog, &c->rvalue_vals );
3360
3361         } else if ( c->op == LDAP_MOD_DELETE ) {
3362                 if ( !c->line ) {
3363                         config_syslog = 0;
3364                 } else {
3365                         int level = verb_to_mask( c->line, loglevel_ops );
3366                         config_syslog ^= level;
3367                 }
3368                 if ( slapMode & SLAP_SERVER_MODE ) {
3369                         ldap_syslog = config_syslog;
3370                 }
3371                 return 0;
3372         }
3373
3374         for( i=1; i < c->argc; i++ ) {
3375                 int     level;
3376
3377                 if ( isdigit((unsigned char)c->argv[i][0]) || c->argv[i][0] == '-' ) {
3378                         if( lutil_atoix( &level, c->argv[i], 0 ) != 0 ) {
3379                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse level", c->argv[0] );
3380                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
3381                                         c->log, c->cr_msg, c->argv[i]);
3382                                 return( 1 );
3383                         }
3384                 } else {
3385                         if ( str2loglevel( c->argv[i], &level ) ) {
3386                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown level", c->argv[0] );
3387                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
3388                                         c->log, c->cr_msg, c->argv[i]);
3389                                 return( 1 );
3390                         }
3391                 }
3392                 /* Explicitly setting a zero clears all the levels */
3393                 if ( level )
3394                         config_syslog |= level;
3395                 else
3396                         config_syslog = 0;
3397         }
3398         if ( slapMode & SLAP_SERVER_MODE ) {
3399                 ldap_syslog = config_syslog;
3400         }
3401         return(0);
3402 }
3403
3404 static int
3405 config_referral(ConfigArgs *c) {
3406         struct berval val;
3407         if (c->op == SLAP_CONFIG_EMIT) {
3408                 if ( default_referral ) {
3409                         value_add( &c->rvalue_vals, default_referral );
3410                         return 0;
3411                 } else {
3412                         return 1;
3413                 }
3414         } else if ( c->op == LDAP_MOD_DELETE ) {
3415                 if ( c->valx < 0 ) {
3416                         ber_bvarray_free( default_referral );
3417                         default_referral = NULL;
3418                 } else {
3419                         int i = c->valx;
3420                         ch_free( default_referral[i].bv_val );
3421                         for (; default_referral[i].bv_val; i++ )
3422                                 default_referral[i] = default_referral[i+1];
3423                 }
3424                 return 0;
3425         }
3426         if(validate_global_referral(c->argv[1])) {
3427                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid URL", c->argv[0] );
3428                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
3429                         c->log, c->cr_msg, c->argv[1]);
3430                 return(1);
3431         }
3432
3433         ber_str2bv(c->argv[1], 0, 0, &val);
3434         if(value_add_one(&default_referral, &val)) return(LDAP_OTHER);
3435         return(0);
3436 }
3437
3438 static struct {
3439         struct berval key;
3440         int off;
3441 } sec_keys[] = {
3442         { BER_BVC("ssf="), offsetof(slap_ssf_set_t, sss_ssf) },
3443         { BER_BVC("transport="), offsetof(slap_ssf_set_t, sss_transport) },
3444         { BER_BVC("tls="), offsetof(slap_ssf_set_t, sss_tls) },
3445         { BER_BVC("sasl="), offsetof(slap_ssf_set_t, sss_sasl) },
3446         { BER_BVC("update_ssf="), offsetof(slap_ssf_set_t, sss_update_ssf) },
3447         { BER_BVC("update_transport="), offsetof(slap_ssf_set_t, sss_update_transport) },
3448         { BER_BVC("update_tls="), offsetof(slap_ssf_set_t, sss_update_tls) },
3449         { BER_BVC("update_sasl="), offsetof(slap_ssf_set_t, sss_update_sasl) },
3450         { BER_BVC("simple_bind="), offsetof(slap_ssf_set_t, sss_simple_bind) },
3451         { BER_BVNULL, 0 }
3452 };
3453
3454 static int
3455 config_security(ConfigArgs *c) {
3456         slap_ssf_set_t *set = &c->be->be_ssf_set;
3457         char *next;
3458         int i, j;
3459         if (c->op == SLAP_CONFIG_EMIT) {
3460                 char numbuf[32];
3461                 struct berval bv;
3462                 slap_ssf_t *tgt;
3463                 int rc = 1;
3464
3465                 for (i=0; !BER_BVISNULL( &sec_keys[i].key ); i++) {
3466                         tgt = (slap_ssf_t *)((char *)set + sec_keys[i].off);
3467                         if ( *tgt ) {
3468                                 rc = 0;
3469                                 bv.bv_len = snprintf( numbuf, sizeof( numbuf ), "%u", *tgt );
3470                                 if ( bv.bv_len >= sizeof( numbuf ) ) {
3471                                         ber_bvarray_free_x( c->rvalue_vals, NULL );
3472                                         c->rvalue_vals = NULL;
3473                                         rc = 1;
3474                                         break;
3475                                 }
3476                                 bv.bv_len += sec_keys[i].key.bv_len;
3477                                 bv.bv_val = ch_malloc( bv.bv_len + 1);
3478                                 next = lutil_strcopy( bv.bv_val, sec_keys[i].key.bv_val );
3479                                 strcpy( next, numbuf );
3480                                 ber_bvarray_add( &c->rvalue_vals, &bv );
3481                         }
3482                 }
3483                 return rc;
3484         }
3485         for(i = 1; i < c->argc; i++) {
3486                 slap_ssf_t *tgt = NULL;
3487                 char *src = NULL;
3488                 for ( j=0; !BER_BVISNULL( &sec_keys[j].key ); j++ ) {
3489                         if(!strncasecmp(c->argv[i], sec_keys[j].key.bv_val,
3490                                 sec_keys[j].key.bv_len)) {
3491                                 src = c->argv[i] + sec_keys[j].key.bv_len;
3492                                 tgt = (slap_ssf_t *)((char *)set + sec_keys[j].off);
3493                                 break;
3494                         }
3495                 }
3496                 if ( !tgt ) {
3497                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown factor", c->argv[0] );
3498                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
3499                                 c->log, c->cr_msg, c->argv[i]);
3500                         return(1);
3501                 }
3502
3503                 if ( lutil_atou( tgt, src ) != 0 ) {
3504                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse factor", c->argv[0] );
3505                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
3506                                 c->log, c->cr_msg, c->argv[i]);
3507                         return(1);
3508                 }
3509         }
3510         return(0);
3511 }
3512
3513 char *
3514 anlist_unparse( AttributeName *an, char *ptr, ber_len_t buflen ) {
3515         int comma = 0;
3516         char *start = ptr;
3517
3518         for (; !BER_BVISNULL( &an->an_name ); an++) {
3519                 /* if buflen == 0, assume the buffer size has been 
3520                  * already checked otherwise */
3521                 if ( buflen > 0 && buflen - ( ptr - start ) < comma + an->an_name.bv_len ) return NULL;
3522                 if ( comma ) *ptr++ = ',';
3523                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
3524                 comma = 1;
3525         }
3526         return ptr;
3527 }
3528
3529 static int
3530 config_updatedn(ConfigArgs *c) {
3531         if (c->op == SLAP_CONFIG_EMIT) {
3532                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
3533                         value_add_one(&c->rvalue_vals, &c->be->be_update_ndn);
3534                         value_add_one(&c->rvalue_nvals, &c->be->be_update_ndn);
3535                         return 0;
3536                 }
3537                 return 1;
3538         } else if ( c->op == LDAP_MOD_DELETE ) {
3539                 ch_free( c->be->be_update_ndn.bv_val );
3540                 BER_BVZERO( &c->be->be_update_ndn );
3541                 SLAP_DBFLAGS(c->be) ^= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
3542                 return 0;
3543         }
3544         if(SLAP_SHADOW(c->be)) {
3545                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> database already shadowed", c->argv[0] );
3546                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
3547                         c->log, c->cr_msg, 0);
3548                 return(1);
3549         }
3550
3551         ber_memfree_x( c->value_dn.bv_val, NULL );
3552         if ( !BER_BVISNULL( &c->be->be_update_ndn ) ) {
3553                 ber_memfree_x( c->be->be_update_ndn.bv_val, NULL );
3554         }
3555         c->be->be_update_ndn = c->value_ndn;
3556         BER_BVZERO( &c->value_dn );
3557         BER_BVZERO( &c->value_ndn );
3558
3559         return config_slurp_shadow( c );
3560 }
3561
3562 int
3563 config_shadow( ConfigArgs *c, slap_mask_t flag )
3564 {
3565         char    *notallowed = NULL;
3566
3567         if ( c->be == frontendDB ) {
3568                 notallowed = "frontend";
3569
3570         } else if ( SLAP_MONITOR(c->be) ) {
3571                 notallowed = "monitor";
3572         }
3573
3574         if ( notallowed != NULL ) {
3575                 Debug( LDAP_DEBUG_ANY, "%s: %s database cannot be shadow.\n", c->log, notallowed, 0 );
3576                 return 1;
3577         }
3578
3579         if ( SLAP_SHADOW(c->be) ) {
3580                 /* if already shadow, only check consistency */
3581                 if ( ( SLAP_DBFLAGS(c->be) & flag ) != flag ) {
3582                         Debug( LDAP_DEBUG_ANY, "%s: inconsistent shadow flag 0x%lx.\n",
3583                                 c->log, flag, 0 );
3584                         return 1;
3585                 }
3586
3587         } else {
3588                 SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SINGLE_SHADOW | flag);
3589         }
3590
3591         return 0;
3592 }
3593
3594 static int
3595 config_updateref(ConfigArgs *c) {
3596         struct berval val;
3597         if (c->op == SLAP_CONFIG_EMIT) {
3598                 if ( c->be->be_update_refs ) {
3599                         value_add( &c->rvalue_vals, c->be->be_update_refs );
3600                         return 0;
3601                 } else {
3602                         return 1;
3603                 }
3604         } else if ( c->op == LDAP_MOD_DELETE ) {
3605                 if ( c->valx < 0 ) {
3606                         ber_bvarray_free( c->be->be_update_refs );
3607                         c->be->be_update_refs = NULL;
3608                 } else {
3609                         int i = c->valx;
3610                         ch_free( c->be->be_update_refs[i].bv_val );
3611                         for (; c->be->be_update_refs[i].bv_val; i++)
3612                                 c->be->be_update_refs[i] = c->be->be_update_refs[i+1];
3613                 }
3614                 return 0;
3615         }
3616         if(!SLAP_SHADOW(c->be) && !c->be->be_syncinfo) {
3617                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must appear after syncrepl or updatedn",
3618                         c->argv[0] );
3619                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
3620                         c->log, c->cr_msg, 0);
3621                 return(1);
3622         }
3623
3624         if(validate_global_referral(c->argv[1])) {
3625                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid URL", c->argv[0] );
3626                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
3627                         c->log, c->cr_msg, c->argv[1]);
3628                 return(1);
3629         }
3630         ber_str2bv(c->argv[1], 0, 0, &val);
3631         if(value_add_one(&c->be->be_update_refs, &val)) return(LDAP_OTHER);
3632         return(0);
3633 }
3634
3635 static int
3636 config_obsolete(ConfigArgs *c) {
3637         if (c->op == SLAP_CONFIG_EMIT)
3638                 return 1;
3639
3640         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> keyword is obsolete (ignored)",
3641                 c->argv[0] );
3642         Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0);
3643         return(0);
3644 }
3645
3646 static int
3647 config_include(ConfigArgs *c) {
3648         int savelineno = c->lineno;
3649         int rc;
3650         ConfigFile *cf;
3651         ConfigFile *cfsave = cfn;
3652         ConfigFile *cf2 = NULL;
3653
3654         /* Leftover from RE23. No dynamic config for include files */
3655         if ( c->op == SLAP_CONFIG_EMIT || c->op == LDAP_MOD_DELETE )
3656                 return 1;
3657
3658         cf = ch_calloc( 1, sizeof(ConfigFile));
3659         if ( cfn->c_kids ) {
3660                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
3661                 cf2->c_sibs = cf;
3662         } else {
3663                 cfn->c_kids = cf;
3664         }
3665         cfn = cf;
3666         ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
3667         rc = read_config_file(c->argv[1], c->depth + 1, c, config_back_cf_table);
3668         c->lineno = savelineno - 1;
3669         cfn = cfsave;
3670         if ( rc ) {
3671                 if ( cf2 ) cf2->c_sibs = NULL;
3672                 else cfn->c_kids = NULL;
3673                 ch_free( cf->c_file.bv_val );
3674                 ch_free( cf );
3675         } else {
3676                 c->ca_private = cf;
3677         }
3678         return(rc);
3679 }
3680
3681 #ifdef HAVE_TLS
3682 static int
3683 config_tls_cleanup(ConfigArgs *c) {
3684         int rc = 0;
3685
3686         if ( slap_tls_ld ) {
3687                 int opt = 1;
3688
3689                 ldap_pvt_tls_ctx_free( slap_tls_ctx );
3690
3691                 /* Force new ctx to be created */
3692                 rc = ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
3693                 if( rc == 0 ) {
3694                         /* The ctx's refcount is bumped up here */
3695                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
3696                         /* This is a no-op if it's already loaded */
3697                         load_extop( &slap_EXOP_START_TLS, 0, starttls_extop );
3698                 }
3699         }
3700         return rc;
3701 }
3702
3703 static int
3704 config_tls_option(ConfigArgs *c) {
3705         int flag;
3706         LDAP *ld = slap_tls_ld;
3707         switch(c->type) {
3708         case CFG_TLS_RAND:      flag = LDAP_OPT_X_TLS_RANDOM_FILE;      ld = NULL; break;
3709         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
3710         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
3711         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
3712         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
3713         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
3714         case CFG_TLS_DH_FILE:   flag = LDAP_OPT_X_TLS_DHFILE;   break;
3715 #ifdef HAVE_GNUTLS
3716         case CFG_TLS_CRL_FILE:  flag = LDAP_OPT_X_TLS_CRLFILE;  break;
3717 #endif
3718         default:                Debug(LDAP_DEBUG_ANY, "%s: "
3719                                         "unknown tls_option <0x%x>\n",
3720                                         c->log, c->type, 0);
3721                 return 1;
3722         }
3723         if (c->op == SLAP_CONFIG_EMIT) {
3724                 return ldap_pvt_tls_get_option( ld, flag, &c->value_string );
3725         } else if ( c->op == LDAP_MOD_DELETE ) {
3726                 c->cleanup = config_tls_cleanup;
3727                 return ldap_pvt_tls_set_option( ld, flag, NULL );
3728         }
3729         ch_free(c->value_string);
3730         c->cleanup = config_tls_cleanup;
3731         return(ldap_pvt_tls_set_option(ld, flag, c->argv[1]));
3732 }
3733
3734 /* FIXME: this ought to be provided by libldap */
3735 static int
3736 config_tls_config(ConfigArgs *c) {
3737         int i, flag;
3738         switch(c->type) {
3739         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK; break;
3740         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT; break;
3741         case CFG_TLS_PROTOCOL_MIN: flag = LDAP_OPT_X_TLS_PROTOCOL_MIN; break;
3742         default:
3743                 Debug(LDAP_DEBUG_ANY, "%s: "
3744                                 "unknown tls_option <0x%x>\n",
3745                                 c->log, c->type, 0);
3746                 return 1;
3747         }
3748         if (c->op == SLAP_CONFIG_EMIT) {
3749                 return slap_tls_get_config( slap_tls_ld, flag, &c->value_string );
3750         } else if ( c->op == LDAP_MOD_DELETE ) {
3751                 int i = 0;
3752                 c->cleanup = config_tls_cleanup;
3753                 return ldap_pvt_tls_set_option( slap_tls_ld, flag, &i );
3754         }
3755         ch_free( c->value_string );
3756         c->cleanup = config_tls_cleanup;
3757         if ( isdigit( (unsigned char)c->argv[1][0] ) ) {
3758                 if ( lutil_atoi( &i, c->argv[1] ) != 0 ) {
3759                         Debug(LDAP_DEBUG_ANY, "%s: "
3760                                 "unable to parse %s \"%s\"\n",
3761                                 c->log, c->argv[0], c->argv[1] );
3762                         return 1;
3763                 }
3764                 return(ldap_pvt_tls_set_option(slap_tls_ld, flag, &i));
3765         } else {
3766                 return(ldap_int_tls_config(slap_tls_ld, flag, c->argv[1]));
3767         }
3768 }
3769 #endif
3770
3771 static CfEntryInfo *
3772 config_find_base( CfEntryInfo *root, struct berval *dn, CfEntryInfo **last )
3773 {
3774         struct berval cdn;
3775         char *c;
3776
3777         if ( !root ) {
3778                 *last = NULL;
3779                 return NULL;
3780         }
3781
3782         if ( dn_match( &root->ce_entry->e_nname, dn ))
3783                 return root;
3784
3785         c = dn->bv_val+dn->bv_len;
3786         for (;*c != ',';c--);
3787
3788         while(root) {
3789                 *last = root;
3790                 for (--c;c>dn->bv_val && *c != ',';c--);
3791                 cdn.bv_val = c;
3792                 if ( *c == ',' )
3793                         cdn.bv_val++;
3794                 cdn.bv_len = dn->bv_len - (cdn.bv_val - dn->bv_val);
3795
3796                 root = root->ce_kids;
3797
3798                 for (;root;root=root->ce_sibs) {
3799                         if ( dn_match( &root->ce_entry->e_nname, &cdn )) {
3800                                 if ( cdn.bv_val == dn->bv_val ) {
3801                                         return root;
3802                                 }
3803                                 break;
3804                         }
3805                 }
3806         }
3807         return root;
3808 }
3809
3810 typedef struct setup_cookie {
3811         CfBackInfo *cfb;
3812         ConfigArgs *ca;
3813         Entry *frontend;
3814         Entry *config;
3815         int got_frontend;
3816         int got_config;
3817 } setup_cookie;
3818
3819 static int
3820 config_ldif_resp( Operation *op, SlapReply *rs )
3821 {
3822         if ( rs->sr_type == REP_SEARCH ) {
3823                 setup_cookie *sc = op->o_callback->sc_private;
3824                 struct berval pdn;
3825
3826                 sc->cfb->cb_got_ldif = 1;
3827                 /* Does the frontend exist? */
3828                 if ( !sc->got_frontend ) {
3829                         if ( !strncmp( rs->sr_entry->e_nname.bv_val,
3830                                 "olcDatabase", STRLENOF( "olcDatabase" )))
3831                         {
3832                                 if ( strncmp( rs->sr_entry->e_nname.bv_val +
3833                                         STRLENOF( "olcDatabase" ), "={-1}frontend",
3834                                         STRLENOF( "={-1}frontend" )))
3835                                 {
3836                                         struct berval rdn;
3837                                         int i = op->o_noop;
3838                                         sc->ca->be = frontendDB;
3839                                         sc->ca->bi = frontendDB->bd_info;
3840                                         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
3841                                         rdn.bv_val = sc->ca->log;
3842                                         rdn.bv_len = snprintf(rdn.bv_val, sizeof( sc->ca->log ),
3843                                                 "%s=" SLAP_X_ORDERED_FMT "%s",
3844                                                 cfAd_database->ad_cname.bv_val, -1,
3845                                                 sc->ca->bi->bi_type);
3846                                         op->o_noop = 1;
3847                                         sc->frontend = config_build_entry( op, rs,
3848                                                 sc->cfb->cb_root, sc->ca, &rdn, &CFOC_DATABASE,
3849                                                 sc->ca->be->be_cf_ocs );
3850                                         op->o_noop = i;
3851                                         sc->got_frontend++;
3852                                 } else {
3853                                         sc->got_frontend++;
3854                                         goto ok;
3855                                 }
3856                         }
3857                 }
3858
3859                 dnParent( &rs->sr_entry->e_nname, &pdn );
3860
3861                 /* Does the configDB exist? */
3862                 if ( sc->got_frontend && !sc->got_config &&
3863                         !strncmp( rs->sr_entry->e_nname.bv_val,
3864                         "olcDatabase", STRLENOF( "olcDatabase" )) &&
3865                         dn_match( &config_rdn, &pdn ) )
3866                 {
3867                         if ( strncmp( rs->sr_entry->e_nname.bv_val +
3868                                 STRLENOF( "olcDatabase" ), "={0}config",
3869                                 STRLENOF( "={0}config" )))
3870                         {
3871                                 struct berval rdn;
3872                                 int i = op->o_noop;
3873                                 sc->ca->be = LDAP_STAILQ_FIRST( &backendDB );
3874                                 sc->ca->bi = sc->ca->be->bd_info;
3875                                 rdn.bv_val = sc->ca->log;
3876                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( sc->ca->log ),
3877                                         "%s=" SLAP_X_ORDERED_FMT "%s",
3878                                         cfAd_database->ad_cname.bv_val, 0,
3879                                         sc->ca->bi->bi_type);
3880                                 op->o_noop = 1;
3881                                 sc->config = config_build_entry( op, rs, sc->cfb->cb_root,
3882                                         sc->ca, &rdn, &CFOC_DATABASE, sc->ca->be->be_cf_ocs );
3883                                 op->o_noop = i;
3884                         }
3885                         sc->got_config++;
3886                 }
3887
3888 ok:
3889                 rs->sr_err = config_add_internal( sc->cfb, rs->sr_entry, sc->ca, NULL, NULL, NULL );
3890                 if ( rs->sr_err != LDAP_SUCCESS ) {
3891                         Debug( LDAP_DEBUG_ANY, "config error processing %s: %s\n",
3892                                 rs->sr_entry->e_name.bv_val, sc->ca->cr_msg, 0 );
3893                 }
3894         }
3895         return rs->sr_err;
3896 }
3897
3898 /* Configure and read the underlying back-ldif store */
3899 static int
3900 config_setup_ldif( BackendDB *be, const char *dir, int readit ) {
3901         CfBackInfo *cfb = be->be_private;
3902         ConfigArgs c = {0};
3903         ConfigTable *ct;
3904         char *argv[3];
3905         int rc = 0;
3906         setup_cookie sc;
3907         slap_callback cb = { NULL, config_ldif_resp, NULL, NULL };
3908         Connection conn = {0};
3909         OperationBuffer opbuf;
3910         Operation *op;
3911         SlapReply rs = {REP_RESULT};
3912         Filter filter = { LDAP_FILTER_PRESENT };
3913         struct berval filterstr = BER_BVC("(objectclass=*)");
3914         struct stat st;
3915
3916         /* Is the config directory available? */
3917         if ( stat( dir, &st ) < 0 ) {
3918                 /* No, so don't bother using the backing store.
3919                  * All changes will be in-memory only.
3920                  */
3921                 return 0;
3922         }
3923                 
3924         cfb->cb_db.bd_info = backend_info( "ldif" );
3925         if ( !cfb->cb_db.bd_info )
3926                 return 0;       /* FIXME: eventually this will be a fatal error */
3927
3928         if ( backend_db_init( "ldif", &cfb->cb_db, -1, NULL ) == NULL )
3929                 return 1;
3930
3931         cfb->cb_db.be_suffix = be->be_suffix;
3932         cfb->cb_db.be_nsuffix = be->be_nsuffix;
3933
3934         /* The suffix is always "cn=config". The underlying DB's rootdn
3935          * is always the same as the suffix.
3936          */
3937         cfb->cb_db.be_rootdn = be->be_suffix[0];
3938         cfb->cb_db.be_rootndn = be->be_nsuffix[0];
3939
3940         ber_str2bv( dir, 0, 1, &cfdir );
3941
3942         c.be = &cfb->cb_db;
3943         c.fname = "slapd";
3944         c.argc = 2;
3945         argv[0] = "directory";
3946         argv[1] = (char *)dir;
3947         argv[2] = NULL;
3948         c.argv = argv;
3949         c.reply.err = 0;
3950         c.reply.msg[0] = 0;
3951         c.table = Cft_Database;
3952
3953         ct = config_find_keyword( c.be->be_cf_ocs->co_table, &c );
3954         if ( !ct )
3955                 return 1;
3956
3957         if ( config_add_vals( ct, &c ))
3958                 return 1;
3959
3960         if ( backend_startup_one( &cfb->cb_db, &c.reply ))
3961                 return 1;
3962
3963         if ( readit ) {
3964                 void *thrctx = ldap_pvt_thread_pool_context();
3965                 int prev_DN_strict;
3966
3967                 connection_fake_init( &conn, &opbuf, thrctx );
3968                 op = &opbuf.ob_op;
3969
3970                 filter.f_desc = slap_schema.si_ad_objectClass;
3971
3972                 op->o_tag = LDAP_REQ_SEARCH;
3973
3974                 op->ors_filter = &filter;
3975                 op->ors_filterstr = filterstr;
3976                 op->ors_scope = LDAP_SCOPE_SUBTREE;
3977
3978                 op->o_dn = c.be->be_rootdn;
3979                 op->o_ndn = c.be->be_rootndn;
3980
3981                 op->o_req_dn = be->be_suffix[0];
3982                 op->o_req_ndn = be->be_nsuffix[0];
3983
3984                 op->ors_tlimit = SLAP_NO_LIMIT;
3985                 op->ors_slimit = SLAP_NO_LIMIT;
3986
3987                 op->ors_attrs = slap_anlist_all_attributes;
3988                 op->ors_attrsonly = 0;
3989
3990                 op->o_callback = &cb;
3991                 sc.cfb = cfb;
3992                 sc.ca = &c;
3993                 cb.sc_private = &sc;
3994                 sc.got_frontend = 0;
3995                 sc.got_config = 0;
3996                 sc.frontend = NULL;
3997                 sc.config = NULL;
3998
3999                 op->o_bd = &cfb->cb_db;
4000                 
4001                 /* Allow unknown attrs in DNs */
4002                 prev_DN_strict = slap_DN_strict;
4003                 slap_DN_strict = 0;
4004
4005                 rc = op->o_bd->be_search( op, &rs );
4006
4007                 /* Restore normal DN validation */
4008                 slap_DN_strict = prev_DN_strict;
4009
4010                 op->o_tag = LDAP_REQ_ADD;
4011                 if ( rc == LDAP_SUCCESS && sc.frontend ) {
4012                         rs_reinit( &rs, REP_RESULT );
4013                         op->ora_e = sc.frontend;
4014                         rc = op->o_bd->be_add( op, &rs );
4015                 }
4016                 if ( rc == LDAP_SUCCESS && sc.config ) {
4017                         rs_reinit( &rs, REP_RESULT );
4018                         op->ora_e = sc.config;
4019                         rc = op->o_bd->be_add( op, &rs );
4020                 }
4021                 ldap_pvt_thread_pool_context_reset( thrctx );
4022         }
4023
4024         /* ITS#4194 - only use if it's present, or we're converting. */
4025         if ( !readit || rc == LDAP_SUCCESS )
4026                 cfb->cb_use_ldif = 1;
4027
4028         return rc;
4029 }
4030
4031 static int
4032 CfOc_cmp( const void *c1, const void *c2 ) {
4033         const ConfigOCs *co1 = c1;
4034         const ConfigOCs *co2 = c2;
4035
4036         return ber_bvcmp( co1->co_name, co2->co_name );
4037 }
4038
4039 int
4040 config_register_schema(ConfigTable *ct, ConfigOCs *ocs) {
4041         int i;
4042
4043         i = init_config_attrs( ct );
4044         if ( i ) return i;
4045
4046         /* set up the objectclasses */
4047         i = init_config_ocs( ocs );
4048         if ( i ) return i;
4049
4050         for (i=0; ocs[i].co_def; i++) {
4051                 if ( ocs[i].co_oc ) {
4052                         ocs[i].co_name = &ocs[i].co_oc->soc_cname;
4053                         if ( !ocs[i].co_table )
4054                                 ocs[i].co_table = ct;
4055                         avl_insert( &CfOcTree, &ocs[i], CfOc_cmp, avl_dup_error );
4056                 }
4057         }
4058         return 0;
4059 }
4060
4061 int
4062 read_config(const char *fname, const char *dir) {
4063         BackendDB *be;
4064         CfBackInfo *cfb;
4065         const char *cfdir, *cfname;
4066         int rc;
4067
4068         /* Setup the config backend */
4069         be = backend_db_init( "config", NULL, 0, NULL );
4070         if ( !be )
4071                 return 1;
4072
4073         cfb = be->be_private;
4074         be->be_dfltaccess = ACL_NONE;
4075
4076         /* If no .conf, or a dir was specified, setup the dir */
4077         if ( !fname || dir ) {
4078                 if ( dir ) {
4079                         /* If explicitly given, check for existence */
4080                         struct stat st;
4081
4082                         if ( stat( dir, &st ) < 0 ) {
4083                                 Debug( LDAP_DEBUG_ANY,
4084                                         "invalid config directory %s, error %d\n",
4085                                                 dir, errno, 0 );
4086                                 return 1;
4087                         }
4088                         cfdir = dir;
4089                 } else {
4090                         cfdir = SLAPD_DEFAULT_CONFIGDIR;
4091                 }
4092                 /* if fname is defaulted, try reading .d */
4093                 rc = config_setup_ldif( be, cfdir, !fname );
4094
4095                 if ( rc ) {
4096                         /* It may be OK if the base object doesn't exist yet. */
4097                         if ( rc != LDAP_NO_SUCH_OBJECT )
4098                                 return 1;
4099                         /* ITS#4194: But if dir was specified and no fname,
4100                          * then we were supposed to read the dir. Unless we're
4101                          * trying to slapadd the dir...
4102                          */
4103                         if ( dir && !fname ) {
4104                                 if ( slapMode & (SLAP_SERVER_MODE|SLAP_TOOL_READMAIN|SLAP_TOOL_READONLY))
4105                                         return 1;
4106                                 /* Assume it's slapadd with a config dir, let it continue */
4107                                 rc = 0;
4108                                 cfb->cb_got_ldif = 1;
4109                                 cfb->cb_use_ldif = 1;
4110                                 goto done;
4111                         }
4112                 }
4113
4114                 /* If we read the config from back-ldif, nothing to do here */
4115                 if ( cfb->cb_got_ldif ) {
4116                         rc = 0;
4117                         goto done;
4118                 }
4119         }
4120
4121         if ( fname )
4122                 cfname = fname;
4123         else
4124                 cfname = SLAPD_DEFAULT_CONFIGFILE;
4125
4126         rc = read_config_file(cfname, 0, NULL, config_back_cf_table);
4127
4128         if ( rc == 0 )
4129                 ber_str2bv( cfname, 0, 1, &cfb->cb_config->c_file );
4130
4131 done:
4132         if ( rc == 0 && BER_BVISNULL( &frontendDB->be_schemadn ) ) {
4133                 ber_str2bv( SLAPD_SCHEMA_DN, STRLENOF( SLAPD_SCHEMA_DN ), 1,
4134                         &frontendDB->be_schemadn );
4135                 rc = dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
4136                 if ( rc != LDAP_SUCCESS ) {
4137                         Debug(LDAP_DEBUG_ANY, "read_config: "
4138                                 "unable to normalize default schema DN \"%s\"\n",
4139                                 frontendDB->be_schemadn.bv_val, 0, 0 );
4140                         /* must not happen */
4141                         assert( 0 );
4142                 }
4143         }
4144         return rc;
4145 }
4146
4147 static int
4148 config_back_bind( Operation *op, SlapReply *rs )
4149 {
4150         if ( be_isroot_pw( op ) ) {
4151                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ));
4152                 /* frontend sends result */
4153                 return LDAP_SUCCESS;
4154         }
4155
4156         rs->sr_err = LDAP_INVALID_CREDENTIALS;
4157         send_ldap_result( op, rs );
4158
4159         return rs->sr_err;
4160 }
4161
4162 static int
4163 config_send( Operation *op, SlapReply *rs, CfEntryInfo *ce, int depth )
4164 {
4165         int rc = 0;
4166
4167         if ( test_filter( op, ce->ce_entry, op->ors_filter ) == LDAP_COMPARE_TRUE )
4168         {
4169                 rs->sr_attrs = op->ors_attrs;
4170                 rs->sr_entry = ce->ce_entry;
4171                 rs->sr_flags = 0;
4172                 rc = send_search_entry( op, rs );
4173                 if ( rc != LDAP_SUCCESS ) {
4174                         return rc;
4175                 }
4176         }
4177         if ( op->ors_scope == LDAP_SCOPE_SUBTREE ) {
4178                 if ( ce->ce_kids ) {
4179                         rc = config_send( op, rs, ce->ce_kids, 1 );
4180                         if ( rc ) return rc;
4181                 }
4182                 if ( depth ) {
4183                         for (ce=ce->ce_sibs; ce; ce=ce->ce_sibs) {
4184                                 rc = config_send( op, rs, ce, 0 );
4185                                 if ( rc ) break;
4186                         }
4187                 }
4188         }
4189         return rc;
4190 }
4191
4192 static ConfigTable *
4193 config_find_table( ConfigOCs **colst, int nocs, AttributeDescription *ad,
4194         ConfigArgs *ca )
4195 {
4196         int i, j;
4197
4198         for (j=0; j<nocs; j++) {
4199                 for (i=0; colst[j]->co_table[i].name; i++)
4200                         if ( colst[j]->co_table[i].ad == ad ) {
4201                                 ca->table = colst[j]->co_type;
4202                                 return &colst[j]->co_table[i];
4203                         }
4204         }
4205         return NULL;
4206 }
4207
4208 /* Sort the attributes of the entry according to the order defined
4209  * in the objectclass, with required attributes occurring before
4210  * allowed attributes. For any attributes with sequencing dependencies
4211  * (e.g., rootDN must be defined after suffix) the objectclass must
4212  * list the attributes in the desired sequence.
4213  */
4214 static void
4215 sort_attrs( Entry *e, ConfigOCs **colst, int nocs )
4216 {
4217         Attribute *a, *head = NULL, *tail = NULL, **prev;
4218         int i, j;
4219
4220         for (i=0; i<nocs; i++) {
4221                 if ( colst[i]->co_oc->soc_required ) {
4222                         AttributeType **at = colst[i]->co_oc->soc_required;
4223                         for (j=0; at[j]; j++) {
4224                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
4225                                         prev = &(*prev)->a_next, a=a->a_next) {
4226                                         if ( a->a_desc == at[j]->sat_ad ) {
4227                                                 *prev = a->a_next;
4228                                                 if (!head) {
4229                                                         head = a;
4230                                                         tail = a;
4231                                                 } else {
4232                                                         tail->a_next = a;
4233                                                         tail = a;
4234                                                 }
4235                                                 break;
4236                                         }
4237                                 }
4238                         }
4239                 }
4240                 if ( colst[i]->co_oc->soc_allowed ) {
4241                         AttributeType **at = colst[i]->co_oc->soc_allowed;
4242                         for (j=0; at[j]; j++) {
4243                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
4244                                         prev = &(*prev)->a_next, a=a->a_next) {
4245                                         if ( a->a_desc == at[j]->sat_ad ) {
4246                                                 *prev = a->a_next;
4247                                                 if (!head) {
4248                                                         head = a;
4249                                                         tail = a;
4250                                                 } else {
4251                                                         tail->a_next = a;
4252                                                         tail = a;
4253                                                 }
4254                                                 break;
4255                                         }
4256                                 }
4257                         }
4258                 }
4259         }
4260         if ( tail ) {
4261                 tail->a_next = e->e_attrs;
4262                 e->e_attrs = head;
4263         }
4264 }
4265
4266 static int
4267 check_vals( ConfigTable *ct, ConfigArgs *ca, void *ptr, int isAttr )
4268 {
4269         Attribute *a = NULL;
4270         AttributeDescription *ad;
4271         BerVarray vals;
4272
4273         int i, rc = 0;
4274
4275         if ( isAttr ) {
4276                 a = ptr;
4277                 ad = a->a_desc;
4278                 vals = a->a_vals;
4279         } else {
4280                 Modifications *ml = ptr;
4281                 ad = ml->sml_desc;
4282                 vals = ml->sml_values;
4283         }
4284
4285         if ( a && ( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL )) {
4286                 rc = ordered_value_sort( a, 1 );
4287                 if ( rc ) {
4288                         snprintf(ca->cr_msg, sizeof( ca->cr_msg ), "ordered_value_sort failed on attr %s\n",
4289                                 ad->ad_cname.bv_val );
4290                         return rc;
4291                 }
4292         }
4293         for ( i=0; vals[i].bv_val; i++ ) {
4294                 ca->line = vals[i].bv_val;
4295                 if (( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) &&
4296                         ca->line[0] == '{' ) {
4297                         char *idx = strchr( ca->line, '}' );
4298                         if ( idx ) ca->line = idx+1;
4299                 }
4300                 rc = config_parse_vals( ct, ca, i );
4301                 if ( rc ) {
4302                         break;
4303                 }
4304         }
4305         return rc;
4306 }
4307
4308 static int
4309 config_rename_attr( SlapReply *rs, Entry *e, struct berval *rdn,
4310         Attribute **at )
4311 {
4312         struct berval rtype, rval;
4313         Attribute *a;
4314         AttributeDescription *ad = NULL;
4315
4316         dnRdn( &e->e_name, rdn );
4317         rval.bv_val = strchr(rdn->bv_val, '=' ) + 1;
4318         rval.bv_len = rdn->bv_len - (rval.bv_val - rdn->bv_val);
4319         rtype.bv_val = rdn->bv_val;
4320         rtype.bv_len = rval.bv_val - rtype.bv_val - 1;
4321
4322         /* Find attr */
4323         slap_bv2ad( &rtype, &ad, &rs->sr_text );
4324         a = attr_find( e->e_attrs, ad );
4325         if (!a ) return LDAP_NAMING_VIOLATION;
4326         *at = a;
4327
4328         return 0;
4329 }
4330
4331 static void
4332 config_rename_kids( CfEntryInfo *ce )
4333 {
4334         CfEntryInfo *ce2;
4335         struct berval rdn, nrdn;
4336
4337         for (ce2 = ce->ce_kids; ce2; ce2 = ce2->ce_sibs) {
4338                 struct berval newdn, newndn;
4339                 dnRdn ( &ce2->ce_entry->e_name, &rdn );
4340                 dnRdn ( &ce2->ce_entry->e_nname, &nrdn );
4341                 build_new_dn( &newdn, &ce->ce_entry->e_name, &rdn, NULL );
4342                 build_new_dn( &newndn, &ce->ce_entry->e_nname, &nrdn, NULL );
4343                 free( ce2->ce_entry->e_name.bv_val );
4344                 free( ce2->ce_entry->e_nname.bv_val );
4345                 ce2->ce_entry->e_name = newdn;
4346                 ce2->ce_entry->e_nname = newndn;
4347                 config_rename_kids( ce2 );
4348         }
4349 }
4350
4351 static int
4352 config_rename_one( Operation *op, SlapReply *rs, Entry *e,
4353         CfEntryInfo *parent, Attribute *a, struct berval *newrdn,
4354         struct berval *nnewrdn, int use_ldif )
4355 {
4356         char *ptr1;
4357         int rc = 0;
4358         struct berval odn, ondn;
4359
4360         odn = e->e_name;
4361         ondn = e->e_nname;
4362         build_new_dn( &e->e_name, &parent->ce_entry->e_name, newrdn, NULL );
4363         build_new_dn( &e->e_nname, &parent->ce_entry->e_nname, nnewrdn, NULL );
4364
4365         /* Replace attr */
4366         free( a->a_vals[0].bv_val );
4367         ptr1 = strchr( newrdn->bv_val, '=' ) + 1;
4368         a->a_vals[0].bv_len = newrdn->bv_len - (ptr1 - newrdn->bv_val);
4369         a->a_vals[0].bv_val = ch_malloc( a->a_vals[0].bv_len + 1 );
4370         strcpy( a->a_vals[0].bv_val, ptr1 );
4371
4372         if ( a->a_nvals != a->a_vals ) {
4373                 free( a->a_nvals[0].bv_val );
4374                 ptr1 = strchr( nnewrdn->bv_val, '=' ) + 1;
4375                 a->a_nvals[0].bv_len = nnewrdn->bv_len - (ptr1 - nnewrdn->bv_val);
4376                 a->a_nvals[0].bv_val = ch_malloc( a->a_nvals[0].bv_len + 1 );
4377                 strcpy( a->a_nvals[0].bv_val, ptr1 );
4378         }
4379         if ( use_ldif ) {
4380                 CfBackInfo *cfb = (CfBackInfo *)op->o_bd->be_private;
4381                 BackendDB *be = op->o_bd;
4382                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
4383                 struct berval dn, ndn, xdn, xndn;
4384
4385                 op->o_bd = &cfb->cb_db;
4386
4387                 /* Save current rootdn; use the underlying DB's rootdn */
4388                 dn = op->o_dn;
4389                 ndn = op->o_ndn;
4390                 xdn = op->o_req_dn;
4391                 xndn = op->o_req_ndn;
4392                 op->o_dn = op->o_bd->be_rootdn;
4393                 op->o_ndn = op->o_bd->be_rootndn;
4394                 op->o_req_dn = odn;
4395                 op->o_req_ndn = ondn;
4396
4397                 scp = op->o_callback;
4398                 op->o_callback = &sc;
4399                 op->orr_newrdn = *newrdn;
4400                 op->orr_nnewrdn = *nnewrdn;
4401                 op->orr_newSup = NULL;
4402                 op->orr_nnewSup = NULL;
4403                 op->orr_deleteoldrdn = 1;
4404                 op->orr_modlist = NULL;
4405                 slap_modrdn2mods( op, rs );
4406                 slap_mods_opattrs( op, &op->orr_modlist, 1 );
4407                 rc = op->o_bd->be_modrdn( op, rs );
4408                 slap_mods_free( op->orr_modlist, 1 );
4409
4410                 op->o_bd = be;
4411                 op->o_callback = scp;
4412                 op->o_dn = dn;
4413                 op->o_ndn = ndn;
4414                 op->o_req_dn = xdn;
4415                 op->o_req_ndn = xndn;
4416         }
4417         free( odn.bv_val );
4418         free( ondn.bv_val );
4419         if ( e->e_private )
4420                 config_rename_kids( e->e_private );
4421         return rc;
4422 }
4423
4424 static int
4425 config_renumber_one( Operation *op, SlapReply *rs, CfEntryInfo *parent, 
4426         Entry *e, int idx, int tailindex, int use_ldif )
4427 {
4428         struct berval ival, newrdn, nnewrdn;
4429         struct berval rdn;
4430         Attribute *a;
4431         char ibuf[32], *ptr1, *ptr2 = NULL;
4432         int rc = 0;
4433
4434         rc = config_rename_attr( rs, e, &rdn, &a );
4435         if ( rc ) return rc;
4436
4437         ival.bv_val = ibuf;
4438         ival.bv_len = snprintf( ibuf, sizeof( ibuf ), SLAP_X_ORDERED_FMT, idx );
4439         if ( ival.bv_len >= sizeof( ibuf ) ) {
4440                 return LDAP_NAMING_VIOLATION;
4441         }
4442         
4443         newrdn.bv_len = rdn.bv_len + ival.bv_len;
4444         newrdn.bv_val = ch_malloc( newrdn.bv_len+1 );
4445
4446         if ( tailindex ) {
4447                 ptr1 = lutil_strncopy( newrdn.bv_val, rdn.bv_val, rdn.bv_len );
4448                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
4449         } else {
4450                 int xlen;
4451                 ptr2 = ber_bvchr( &rdn, '}' );
4452                 if ( ptr2 ) {
4453                         ptr2++;
4454                 } else {
4455                         ptr2 = rdn.bv_val + a->a_desc->ad_cname.bv_len + 1;
4456                 }
4457                 xlen = rdn.bv_len - (ptr2 - rdn.bv_val);
4458                 ptr1 = lutil_strncopy( newrdn.bv_val, a->a_desc->ad_cname.bv_val,
4459                         a->a_desc->ad_cname.bv_len );
4460                 *ptr1++ = '=';
4461                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
4462                 ptr1 = lutil_strncopy( ptr1, ptr2, xlen );
4463                 *ptr1 = '\0';
4464         }
4465
4466         /* Do the equivalent of ModRDN */
4467         /* Replace DN / NDN */
4468         newrdn.bv_len = ptr1 - newrdn.bv_val;
4469         rdnNormalize( 0, NULL, NULL, &newrdn, &nnewrdn, NULL );
4470         rc = config_rename_one( op, rs, e, parent, a, &newrdn, &nnewrdn, use_ldif );
4471
4472         free( nnewrdn.bv_val );
4473         free( newrdn.bv_val );
4474         return rc;
4475 }
4476
4477 static int
4478 check_name_index( CfEntryInfo *parent, ConfigType ce_type, Entry *e,
4479         SlapReply *rs, int *renum, int *ibase )
4480 {
4481         CfEntryInfo *ce;
4482         int index = -1, gotindex = 0, nsibs, rc = 0;
4483         int renumber = 0, tailindex = 0, isfrontend = 0, isconfig = 0;
4484         char *ptr1, *ptr2 = NULL;
4485         struct berval rdn;
4486
4487         if ( renum ) *renum = 0;
4488
4489         /* These entries don't get indexed/renumbered */
4490         if ( ce_type == Cft_Global ) return 0;
4491         if ( ce_type == Cft_Schema && parent->ce_type == Cft_Global ) return 0;
4492
4493         if ( ce_type == Cft_Module )
4494                 tailindex = 1;
4495
4496         /* See if the rdn has an index already */
4497         dnRdn( &e->e_name, &rdn );
4498         if ( ce_type == Cft_Database ) {
4499                 if ( !strncmp( rdn.bv_val + rdn.bv_len - STRLENOF("frontend"),
4500                                 "frontend", STRLENOF("frontend") )) 
4501                         isfrontend = 1;
4502                 else if ( !strncmp( rdn.bv_val + rdn.bv_len - STRLENOF("config"),
4503                                 "config", STRLENOF("config") )) 
4504                         isconfig = 1;
4505         }
4506         ptr1 = ber_bvchr( &e->e_name, '{' );
4507         if ( ptr1 && ptr1 < &e->e_name.bv_val[ rdn.bv_len ] ) {
4508                 char    *next;
4509                 ptr2 = strchr( ptr1, '}' );
4510                 if ( !ptr2 || ptr2 > &e->e_name.bv_val[ rdn.bv_len ] )
4511                         return LDAP_NAMING_VIOLATION;
4512                 if ( ptr2-ptr1 == 1)
4513                         return LDAP_NAMING_VIOLATION;
4514                 gotindex = 1;
4515                 index = strtol( ptr1 + 1, &next, 10 );
4516                 if ( next == ptr1 + 1 || next[ 0 ] != '}' ) {
4517                         return LDAP_NAMING_VIOLATION;
4518                 }
4519                 if ( index < 0 ) {
4520                         /* Special case, we allow -1 for the frontendDB */
4521                         if ( index != -1 || !isfrontend )
4522                                 return LDAP_NAMING_VIOLATION;
4523                 }
4524                 if ( isconfig && index != 0 ){
4525                         return LDAP_NAMING_VIOLATION;
4526                 }
4527         }
4528
4529         /* count related kids */
4530         for (nsibs=0, ce=parent->ce_kids; ce; ce=ce->ce_sibs) {
4531                 if ( ce->ce_type == ce_type ) nsibs++;
4532         }
4533
4534         /* account for -1 frontend */
4535         if ( ce_type == Cft_Database )
4536                 nsibs--;
4537
4538         if ( index != nsibs ) {
4539                 if ( gotindex ) {
4540                         if ( index < nsibs ) {
4541                                 if ( tailindex ) return LDAP_NAMING_VIOLATION;
4542                                 /* Siblings need to be renumbered */
4543                                 if ( index != -1 || !isfrontend )
4544                                         renumber = 1;
4545                         }
4546                 }
4547                 /* config DB is always "0" */
4548                 if ( isconfig && index == -1 ) {
4549                         index = 0;
4550                 }
4551                 if (( !isfrontend && index == -1 ) || ( index > nsibs ) ){
4552                         index = nsibs;
4553                 }
4554
4555                 /* just make index = nsibs */
4556                 if ( !renumber ) {
4557                         rc = config_renumber_one( NULL, rs, parent, e, index, tailindex, 0 );
4558                 }
4559         }
4560         if ( ibase ) *ibase = index;
4561         if ( renum ) *renum = renumber;
4562         return rc;
4563 }
4564
4565 static int
4566 count_oc( ObjectClass *oc, ConfigOCs ***copp, int *nocs )
4567 {
4568         ConfigOCs       co, *cop;
4569         ObjectClass     **sups;
4570
4571         co.co_name = &oc->soc_cname;
4572         cop = avl_find( CfOcTree, &co, CfOc_cmp );
4573         if ( cop ) {
4574                 int     i;
4575
4576                 /* check for duplicates */
4577                 for ( i = 0; i < *nocs; i++ ) {
4578                         if ( *copp && (*copp)[i] == cop ) {
4579                                 break;
4580                         }
4581                 }
4582
4583                 if ( i == *nocs ) {
4584                         ConfigOCs **tmp = ch_realloc( *copp, (*nocs + 1)*sizeof( ConfigOCs * ) );
4585                         if ( tmp == NULL ) {
4586                                 return -1;
4587                         }
4588                         *copp = tmp;
4589                         (*copp)[*nocs] = cop;
4590                         (*nocs)++;
4591                 }
4592         }
4593
4594         for ( sups = oc->soc_sups; sups && *sups; sups++ ) {
4595                 if ( count_oc( *sups, copp, nocs ) ) {
4596                         return -1;
4597                 }
4598         }
4599
4600         return 0;
4601 }
4602
4603 static ConfigOCs **
4604 count_ocs( Attribute *oc_at, int *nocs )
4605 {
4606         int             i;
4607         ConfigOCs       **colst = NULL;
4608
4609         *nocs = 0;
4610
4611         for ( i = 0; !BER_BVISNULL( &oc_at->a_nvals[i] ); i++ )
4612                 /* count attrs */ ;
4613
4614         for ( ; 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         return colst;
4625 }
4626
4627 static int
4628 cfAddInclude( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
4629 {
4630         /* Leftover from RE23. Never parse this entry */
4631         return LDAP_COMPARE_TRUE;
4632 }
4633
4634 static int
4635 cfAddSchema( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
4636 {
4637         ConfigFile *cfo;
4638
4639         /* This entry is hardcoded, don't re-parse it */
4640         if ( p->ce_type == Cft_Global ) {
4641                 cfn = p->ce_private;
4642                 ca->ca_private = cfn;
4643                 return LDAP_COMPARE_TRUE;
4644         }
4645         if ( p->ce_type != Cft_Schema )
4646                 return LDAP_CONSTRAINT_VIOLATION;
4647
4648         cfn = ch_calloc( 1, sizeof(ConfigFile) );
4649         ca->ca_private = cfn;
4650         cfo = p->ce_private;
4651         cfn->c_sibs = cfo->c_kids;
4652         cfo->c_kids = cfn;
4653         return LDAP_SUCCESS;
4654 }
4655
4656 static int
4657 cfAddDatabase( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4658 {
4659         if ( p->ce_type != Cft_Global ) {
4660                 return LDAP_CONSTRAINT_VIOLATION;
4661         }
4662         /* config must be {0}, nothing else allowed */
4663         if ( !strncmp( e->e_nname.bv_val, "olcDatabase={0}", STRLENOF("olcDatabase={0}")) &&
4664                 strncmp( e->e_nname.bv_val + STRLENOF("olcDatabase={0}"), "config,", STRLENOF("config,") )) {
4665                 return LDAP_CONSTRAINT_VIOLATION;
4666         }
4667         ca->be = frontendDB;    /* just to get past check_vals */
4668         return LDAP_SUCCESS;
4669 }
4670
4671 static int
4672 cfAddBackend( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4673 {
4674         if ( p->ce_type != Cft_Global ) {
4675                 return LDAP_CONSTRAINT_VIOLATION;
4676         }
4677         return LDAP_SUCCESS;
4678 }
4679
4680 static int
4681 cfAddModule( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4682 {
4683         if ( p->ce_type != Cft_Global ) {
4684                 return LDAP_CONSTRAINT_VIOLATION;
4685         }
4686         return LDAP_SUCCESS;
4687 }
4688
4689 static int
4690 cfAddOverlay( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4691 {
4692         if ( p->ce_type != Cft_Database ) {
4693                 return LDAP_CONSTRAINT_VIOLATION;
4694         }
4695         ca->be = p->ce_be;
4696         return LDAP_SUCCESS;
4697 }
4698
4699 static void
4700 schema_destroy_one( ConfigArgs *ca, ConfigOCs **colst, int nocs,
4701         CfEntryInfo *p )
4702 {
4703         ConfigTable *ct;
4704         ConfigFile *cfo;
4705         AttributeDescription *ad;
4706         const char *text;
4707
4708         ca->valx = -1;
4709         ca->line = NULL;
4710         ca->argc = 1;
4711         if ( cfn->c_cr_head ) {
4712                 struct berval bv = BER_BVC("olcDitContentRules");
4713                 ad = NULL;
4714                 slap_bv2ad( &bv, &ad, &text );
4715                 ct = config_find_table( colst, nocs, ad, ca );
4716                 config_del_vals( ct, ca );
4717         }
4718         if ( cfn->c_oc_head ) {
4719                 struct berval bv = BER_BVC("olcObjectClasses");
4720                 ad = NULL;
4721                 slap_bv2ad( &bv, &ad, &text );
4722                 ct = config_find_table( colst, nocs, ad, ca );
4723                 config_del_vals( ct, ca );
4724         }
4725         if ( cfn->c_at_head ) {
4726                 struct berval bv = BER_BVC("olcAttributeTypes");
4727                 ad = NULL;
4728                 slap_bv2ad( &bv, &ad, &text );
4729                 ct = config_find_table( colst, nocs, ad, ca );
4730                 config_del_vals( ct, ca );
4731         }
4732         if ( cfn->c_syn_head ) {
4733                 struct berval bv = BER_BVC("olcLdapSyntaxes");
4734                 ad = NULL;
4735                 slap_bv2ad( &bv, &ad, &text );
4736                 ct = config_find_table( colst, nocs, ad, ca );
4737                 config_del_vals( ct, ca );
4738         }
4739         if ( cfn->c_om_head ) {
4740                 struct berval bv = BER_BVC("olcObjectIdentifier");
4741                 ad = NULL;
4742                 slap_bv2ad( &bv, &ad, &text );
4743                 ct = config_find_table( colst, nocs, ad, ca );
4744                 config_del_vals( ct, ca );
4745         }
4746         cfo = p->ce_private;
4747         cfo->c_kids = cfn->c_sibs;
4748         ch_free( cfn );
4749 }
4750
4751 static int
4752 config_add_oc( ConfigOCs **cop, CfEntryInfo *last, Entry *e, ConfigArgs *ca )
4753 {
4754         int             rc = LDAP_CONSTRAINT_VIOLATION;
4755         ObjectClass     **ocp;
4756
4757         if ( (*cop)->co_ldadd ) {
4758                 rc = (*cop)->co_ldadd( last, e, ca );
4759                 if ( rc != LDAP_CONSTRAINT_VIOLATION ) {
4760                         return rc;
4761                 }
4762         }
4763
4764         for ( ocp = (*cop)->co_oc->soc_sups; ocp && *ocp; ocp++ ) {
4765                 ConfigOCs       co = { 0 };
4766
4767                 co.co_name = &(*ocp)->soc_cname;
4768                 *cop = avl_find( CfOcTree, &co, CfOc_cmp );
4769                 if ( *cop == NULL ) {
4770                         return rc;
4771                 }
4772
4773                 rc = config_add_oc( cop, last, e, ca );
4774                 if ( rc != LDAP_CONSTRAINT_VIOLATION ) {
4775                         return rc;
4776                 }
4777         }
4778
4779         return rc;
4780 }
4781
4782 /* Parse an LDAP entry into config directives */
4783 static int
4784 config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca, SlapReply *rs,
4785         int *renum, Operation *op )
4786 {
4787         CfEntryInfo     *ce, *last = NULL;
4788         ConfigOCs       co, *coptr, **colst;
4789         Attribute       *a, *oc_at, *soc_at;
4790         int             i, ibase = -1, nocs, rc = 0;
4791         struct berval   pdn;
4792         ConfigTable     *ct;
4793         char            *ptr, *log_prefix = op ? op->o_log_prefix : "";
4794
4795         memset( ca, 0, sizeof(ConfigArgs));
4796
4797         /* Make sure parent exists and entry does not. But allow
4798          * Databases and Overlays to be inserted. Don't do any
4799          * auto-renumbering if manageDSAit control is present.
4800          */
4801         ce = config_find_base( cfb->cb_root, &e->e_nname, &last );
4802         if ( ce ) {
4803                 if ( ( op && op->o_managedsait ) ||
4804                         ( ce->ce_type != Cft_Database && ce->ce_type != Cft_Overlay &&
4805                           ce->ce_type != Cft_Module ) )
4806                 {
4807                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4808                                 "DN=\"%s\" already exists\n",
4809                                 log_prefix, e->e_name.bv_val, 0 );
4810                         /* global schema ignores all writes */
4811                         if ( ce->ce_type == Cft_Schema && ce->ce_parent->ce_type == Cft_Global )
4812                                 return LDAP_COMPARE_TRUE;
4813                         return LDAP_ALREADY_EXISTS;
4814                 }
4815         }
4816
4817         dnParent( &e->e_nname, &pdn );
4818
4819         /* If last is NULL, the new entry is the root/suffix entry, 
4820          * otherwise last should be the parent.
4821          */
4822         if ( last && !dn_match( &last->ce_entry->e_nname, &pdn ) ) {
4823                 if ( rs ) {
4824                         rs->sr_matched = last->ce_entry->e_name.bv_val;
4825                 }
4826                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4827                         "DN=\"%s\" not child of DN=\"%s\"\n",
4828                         log_prefix, e->e_name.bv_val,
4829                         last->ce_entry->e_name.bv_val );
4830                 return LDAP_NO_SUCH_OBJECT;
4831         }
4832
4833         if ( op ) {
4834                 /* No parent, must be root. This will never happen... */
4835                 if ( !last && !be_isroot( op ) && !be_shadow_update( op ) ) {
4836                         return LDAP_NO_SUCH_OBJECT;
4837                 }
4838
4839                 if ( last && !access_allowed( op, last->ce_entry,
4840                         slap_schema.si_ad_children, NULL, ACL_WADD, NULL ) )
4841                 {
4842                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4843                                 "DN=\"%s\" no write access to \"children\" of parent\n",
4844                                 log_prefix, e->e_name.bv_val, 0 );
4845                         return LDAP_INSUFFICIENT_ACCESS;
4846                 }
4847         }
4848
4849         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
4850         if ( !oc_at ) {
4851                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4852                         "DN=\"%s\" no objectClass\n",
4853                         log_prefix, e->e_name.bv_val, 0 );
4854                 return LDAP_OBJECT_CLASS_VIOLATION;
4855         }
4856
4857         soc_at = attr_find( e->e_attrs, slap_schema.si_ad_structuralObjectClass );
4858         if ( !soc_at ) {
4859                 ObjectClass     *soc = NULL;
4860                 char            textbuf[ SLAP_TEXT_BUFLEN ];
4861                 const char      *text = textbuf;
4862
4863                 /* FIXME: check result */
4864                 rc = structural_class( oc_at->a_nvals, &soc, NULL,
4865                         &text, textbuf, sizeof(textbuf), NULL );
4866                 if ( rc != LDAP_SUCCESS ) {
4867                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4868                                 "DN=\"%s\" no structural objectClass (%s)\n",
4869                                 log_prefix, e->e_name.bv_val, text );
4870                         return rc;
4871                 }
4872                 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &soc->soc_cname, NULL );
4873                 soc_at = attr_find( e->e_attrs, slap_schema.si_ad_structuralObjectClass );
4874                 if ( soc_at == NULL ) {
4875                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4876                                 "DN=\"%s\" no structural objectClass; "
4877                                 "unable to merge computed class %s\n",
4878                                 log_prefix, e->e_name.bv_val,
4879                                 soc->soc_cname.bv_val );
4880                         return LDAP_OBJECT_CLASS_VIOLATION;
4881                 }
4882
4883                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4884                         "DN=\"%s\" no structural objectClass; "
4885                         "computed objectClass %s merged\n",
4886                         log_prefix, e->e_name.bv_val,
4887                         soc->soc_cname.bv_val );
4888         }
4889
4890         /* Fake the coordinates based on whether we're part of an
4891          * LDAP Add or if reading the config dir
4892          */
4893         if ( rs ) {
4894                 ca->fname = "slapd";
4895                 ca->lineno = 0;
4896         } else {
4897                 ca->fname = cfdir.bv_val;
4898                 ca->lineno = 1;
4899         }
4900         ca->ca_op = op;
4901
4902         co.co_name = &soc_at->a_nvals[0];
4903         coptr = avl_find( CfOcTree, &co, CfOc_cmp );
4904         if ( coptr == NULL ) {
4905                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4906                         "DN=\"%s\" no structural objectClass in configuration table\n",
4907                         log_prefix, e->e_name.bv_val, 0 );
4908                 return LDAP_OBJECT_CLASS_VIOLATION;
4909         }
4910
4911         /* Only the root can be Cft_Global, everything else must
4912          * have a parent. Only limited nesting arrangements are allowed.
4913          */
4914         rc = LDAP_CONSTRAINT_VIOLATION;
4915         if ( coptr->co_type == Cft_Global && !last ) {
4916                 cfn = cfb->cb_config;
4917                 ca->ca_private = cfn;
4918                 ca->be = frontendDB;    /* just to get past check_vals */
4919                 rc = LDAP_SUCCESS;
4920         }
4921
4922         colst = count_ocs( oc_at, &nocs );
4923
4924         /* Check whether the Add is allowed by its parent, and do
4925          * any necessary arg setup
4926          */
4927         if ( last ) {
4928                 rc = config_add_oc( &coptr, last, e, ca );
4929                 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
4930                         for ( i = 0; i<nocs; i++ ) {
4931                                 /* Already checked these */
4932                                 if ( colst[i]->co_oc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
4933                                         continue;
4934                                 if ( colst[i]->co_ldadd &&
4935                                         ( rc = colst[i]->co_ldadd( last, e, ca ))
4936                                                 != LDAP_CONSTRAINT_VIOLATION ) {
4937                                         coptr = colst[i];
4938                                         break;
4939                                 }
4940                         }
4941                 }
4942                 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
4943                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4944                                 "DN=\"%s\" no structural objectClass add function\n",
4945                                 log_prefix, e->e_name.bv_val, 0 );
4946                         return LDAP_OBJECT_CLASS_VIOLATION;
4947                 }
4948         }
4949
4950         /* Add the entry but don't parse it, we already have its contents */
4951         if ( rc == LDAP_COMPARE_TRUE ) {
4952                 rc = LDAP_SUCCESS;
4953                 goto ok;
4954         }
4955
4956         if ( rc != LDAP_SUCCESS )
4957                 goto done_noop;
4958
4959         /* Parse all the values and check for simple syntax errors before
4960          * performing any set actions.
4961          *
4962          * If doing an LDAPadd, check for indexed names and any necessary
4963          * renaming/renumbering. Entries that don't need indexed names are
4964          * ignored. Entries that need an indexed name and arrive without one
4965          * are assigned to the end. Entries that arrive with an index may
4966          * cause the following entries to be renumbered/bumped down.
4967          *
4968          * Note that "pseudo-indexed" entries (cn=Include{xx}, cn=Module{xx})
4969          * don't allow Adding an entry with an index that's already in use.
4970          * This is flagged as an error (LDAP_ALREADY_EXISTS) up above.
4971          *
4972          * These entries can have auto-assigned indexes (appended to the end)
4973          * but only the other types support auto-renumbering of siblings.
4974          */
4975         {
4976                 rc = check_name_index( last, coptr->co_type, e, rs, renum,
4977                         &ibase );
4978                 if ( rc ) {
4979                         goto done_noop;
4980                 }
4981                 if ( renum && *renum && coptr->co_type != Cft_Database &&
4982                         coptr->co_type != Cft_Overlay )
4983                 {
4984                         snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
4985                                 "operation requires sibling renumbering" );
4986                         rc = LDAP_UNWILLING_TO_PERFORM;
4987                         goto done_noop;
4988                 }
4989         }
4990
4991         init_config_argv( ca );
4992
4993         /* Make sure we process attrs in the required order */
4994         sort_attrs( e, colst, nocs );
4995
4996         for ( a = e->e_attrs; a; a = a->a_next ) {
4997                 if ( a == oc_at ) continue;
4998                 ct = config_find_table( colst, nocs, a->a_desc, ca );
4999                 if ( !ct ) continue;    /* user data? */
5000                 rc = check_vals( ct, ca, a, 1 );
5001                 if ( rc ) goto done_noop;
5002         }
5003
5004         /* Basic syntax checks are OK. Do the actual settings. */
5005         for ( a=e->e_attrs; a; a=a->a_next ) {
5006                 if ( a == oc_at ) continue;
5007                 ct = config_find_table( colst, nocs, a->a_desc, ca );
5008                 if ( !ct ) continue;    /* user data? */
5009                 for (i=0; a->a_vals[i].bv_val; i++) {
5010                         char *iptr = NULL;
5011                         ca->valx = -1;
5012                         ca->line = a->a_vals[i].bv_val;
5013                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
5014                                 ptr = strchr( ca->line, '}' );
5015                                 if ( ptr ) {
5016                                         iptr = strchr( ca->line, '{' );
5017                                         ca->line = ptr+1;
5018                                 }
5019                         }
5020                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED_SIB ) {
5021                                 if ( iptr ) {
5022                                         ca->valx = strtol( iptr+1, NULL, 0 );
5023                                 }
5024                         } else {
5025                                 ca->valx = i;
5026                         }
5027                         rc = config_parse_add( ct, ca, i );
5028                         if ( rc ) {
5029                                 rc = LDAP_OTHER;
5030                                 goto done;
5031                         }
5032                 }
5033         }
5034 ok:
5035         /* Newly added databases and overlays need to be started up */
5036         if ( CONFIG_ONLINE_ADD( ca )) {
5037                 if ( coptr->co_type == Cft_Database ) {
5038                         rc = backend_startup_one( ca->be, &ca->reply );
5039
5040                 } else if ( coptr->co_type == Cft_Overlay ) {
5041                         if ( ca->bi->bi_db_open ) {
5042                                 BackendInfo *bi_orig = ca->be->bd_info;
5043                                 ca->be->bd_info = ca->bi;
5044                                 rc = ca->bi->bi_db_open( ca->be, &ca->reply );
5045                                 ca->be->bd_info = bi_orig;
5046                         }
5047                 } else if ( ca->cleanup ) {
5048                         rc = ca->cleanup( ca );
5049                 }
5050                 if ( rc ) {
5051                         if (ca->cr_msg[0] == '\0')
5052                                 snprintf( ca->cr_msg, sizeof( ca->cr_msg ), "<%s> failed startup", ca->argv[0] );
5053
5054                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
5055                                 ca->log, ca->cr_msg, ca->argv[1] );
5056                         rc = LDAP_OTHER;
5057                         goto done;
5058                 }
5059         }
5060
5061         ca->valx = ibase;
5062         ce = ch_calloc( 1, sizeof(CfEntryInfo) );
5063         ce->ce_parent = last;
5064         ce->ce_entry = entry_dup( e );
5065         ce->ce_entry->e_private = ce;
5066         ce->ce_type = coptr->co_type;
5067         ce->ce_be = ca->be;
5068         ce->ce_bi = ca->bi;
5069         ce->ce_private = ca->ca_private;
5070         ca->ca_entry = ce->ce_entry;
5071         if ( !last ) {
5072                 cfb->cb_root = ce;
5073         } else if ( last->ce_kids ) {
5074                 CfEntryInfo *c2, **cprev;
5075
5076                 /* Advance to first of this type */
5077                 cprev = &last->ce_kids;
5078                 for ( c2 = *cprev; c2 && c2->ce_type < ce->ce_type; ) {
5079                         cprev = &c2->ce_sibs;
5080                         c2 = c2->ce_sibs;
5081                 }
5082                 /* Account for the (-1) frontendDB entry */
5083                 if ( ce->ce_type == Cft_Database ) {
5084                         if ( ca->be == frontendDB )
5085                                 ibase = 0;
5086                         else if ( ibase != -1 )
5087                                 ibase++;
5088                 }
5089                 /* Append */
5090                 if ( ibase < 0 ) {
5091                         for (c2 = *cprev; c2 && c2->ce_type == ce->ce_type;) {
5092                                 cprev = &c2->ce_sibs;
5093                                 c2 = c2->ce_sibs;
5094                         }
5095                 } else {
5096                 /* Insert */
5097                         int i;
5098                         for ( i=0; i<ibase; i++ ) {
5099                                 c2 = *cprev;
5100                                 cprev = &c2->ce_sibs;
5101                         }
5102                 }
5103                 ce->ce_sibs = *cprev;
5104                 *cprev = ce;
5105         } else {
5106                 last->ce_kids = ce;
5107         }
5108
5109 done:
5110         if ( rc ) {
5111                 if ( (coptr->co_type == Cft_Database) && ca->be ) {
5112                         if ( ca->be != frontendDB )
5113                                 backend_destroy_one( ca->be, 1 );
5114                 } else if ( (coptr->co_type == Cft_Overlay) && ca->bi ) {
5115                         overlay_destroy_one( ca->be, (slap_overinst *)ca->bi );
5116                 } else if ( coptr->co_type == Cft_Schema ) {
5117                         schema_destroy_one( ca, colst, nocs, last );
5118                 }
5119         }
5120 done_noop:
5121
5122         ch_free( ca->argv );
5123         if ( colst ) ch_free( colst );
5124         return rc;
5125 }
5126
5127 #define BIGTMP  10000
5128 static int
5129 config_rename_add( Operation *op, SlapReply *rs, CfEntryInfo *ce,
5130         int base, int rebase, int max, int use_ldif )
5131 {
5132         CfEntryInfo *ce2, *ce3, *cetmp = NULL, *cerem = NULL;
5133         ConfigType etype = ce->ce_type;
5134         int count = 0, rc = 0;
5135
5136         /* Reverse ce list */
5137         for (ce2 = ce->ce_sibs;ce2;ce2 = ce3) {
5138                 if (ce2->ce_type != etype) {
5139                         cerem = ce2;
5140                         break;
5141                 }
5142                 ce3 = ce2->ce_sibs;
5143                 ce2->ce_sibs = cetmp;
5144                 cetmp = ce2;
5145                 count++;
5146                 if ( max && count >= max ) {
5147                         cerem = ce3;
5148                         break;
5149                 }
5150         }
5151
5152         /* Move original to a temp name until increments are done */
5153         if ( rebase ) {
5154                 ce->ce_entry->e_private = NULL;
5155                 rc = config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
5156                         base+BIGTMP, 0, use_ldif );
5157                 ce->ce_entry->e_private = ce;
5158         }
5159         /* start incrementing */
5160         for (ce2=cetmp; ce2; ce2=ce3) {
5161                 ce3 = ce2->ce_sibs;
5162                 ce2->ce_sibs = cerem;
5163                 cerem = ce2;
5164                 if ( rc == 0 ) 
5165                         rc = config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
5166                                 count+base, 0, use_ldif );
5167                 count--;
5168         }
5169         if ( rebase )
5170                 rc = config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
5171                         base, 0, use_ldif );
5172         return rc;
5173 }
5174
5175 static int
5176 config_rename_del( Operation *op, SlapReply *rs, CfEntryInfo *ce,
5177         CfEntryInfo *ce2, int old, int use_ldif )
5178 {
5179         int count = 0;
5180
5181         /* Renumber original to a temp value */
5182         ce->ce_entry->e_private = NULL;
5183         config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
5184                 old+BIGTMP, 0, use_ldif );
5185         ce->ce_entry->e_private = ce;
5186
5187         /* start decrementing */
5188         for (; ce2 != ce; ce2=ce2->ce_sibs) {
5189                 config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
5190                         count+old, 0, use_ldif );
5191                 count++;
5192         }
5193         return config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
5194                 count+old, 0, use_ldif );
5195 }
5196
5197 /* Parse an LDAP entry into config directives, then store in underlying
5198  * database.
5199  */
5200 static int
5201 config_back_add( Operation *op, SlapReply *rs )
5202 {
5203         CfBackInfo *cfb;
5204         int renumber;
5205         ConfigArgs ca;
5206
5207         if ( !access_allowed( op, op->ora_e, slap_schema.si_ad_entry,
5208                 NULL, ACL_WADD, NULL )) {
5209                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5210                 goto out;
5211         }
5212
5213         /*
5214          * Check for attribute ACL
5215          */
5216         if ( !acl_check_modlist( op, op->ora_e, op->orm_modlist )) {
5217                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5218                 rs->sr_text = "no write access to attribute";
5219                 goto out;
5220         }
5221
5222         cfb = (CfBackInfo *)op->o_bd->be_private;
5223
5224         /* add opattrs for syncprov */
5225         {
5226                 char textbuf[SLAP_TEXT_BUFLEN];
5227                 size_t textlen = sizeof textbuf;
5228                 rs->sr_err = entry_schema_check(op, op->ora_e, NULL, 0, 1, NULL,
5229                         &rs->sr_text, textbuf, sizeof( textbuf ) );
5230                 if ( rs->sr_err != LDAP_SUCCESS )
5231                         goto out;
5232                 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
5233                 if ( rs->sr_err != LDAP_SUCCESS ) {
5234                         Debug( LDAP_DEBUG_TRACE,
5235                                 LDAP_XSTRING(config_back_add) ": entry failed op attrs add: "
5236                                 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
5237                         goto out;
5238                 }
5239         }
5240
5241         if ( op->o_abandon ) {
5242                 rs->sr_err = SLAPD_ABANDON;
5243                 goto out;
5244         }
5245         ldap_pvt_thread_pool_pause( &connection_pool );
5246
5247         /* Strategy:
5248          * 1) check for existence of entry
5249          * 2) check for sibling renumbering
5250          * 3) perform internal add
5251          * 4) perform any necessary renumbering
5252          * 5) store entry in underlying database
5253          */
5254         rs->sr_err = config_add_internal( cfb, op->ora_e, &ca, rs, &renumber, op );
5255         if ( rs->sr_err != LDAP_SUCCESS ) {
5256                 rs->sr_text = ca.cr_msg;
5257                 goto out2;
5258         }
5259
5260         if ( renumber ) {
5261                 CfEntryInfo *ce = ca.ca_entry->e_private;
5262                 req_add_s addr = op->oq_add;
5263                 op->o_tag = LDAP_REQ_MODRDN;
5264                 rs->sr_err = config_rename_add( op, rs, ce, ca.valx, 0, 0, cfb->cb_use_ldif );
5265                 op->o_tag = LDAP_REQ_ADD;
5266                 op->oq_add = addr;
5267                 if ( rs->sr_err != LDAP_SUCCESS ) {
5268                         goto out2;
5269                 }
5270         }
5271
5272         if ( cfb->cb_use_ldif ) {
5273                 BackendDB *be = op->o_bd;
5274                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
5275                 struct berval dn, ndn;
5276
5277                 op->o_bd = &cfb->cb_db;
5278
5279                 /* Save current rootdn; use the underlying DB's rootdn */
5280                 dn = op->o_dn;
5281                 ndn = op->o_ndn;
5282                 op->o_dn = op->o_bd->be_rootdn;
5283                 op->o_ndn = op->o_bd->be_rootndn;
5284
5285                 scp = op->o_callback;
5286                 op->o_callback = &sc;
5287                 op->o_bd->be_add( op, rs );
5288                 op->o_bd = be;
5289                 op->o_callback = scp;
5290                 op->o_dn = dn;
5291                 op->o_ndn = ndn;
5292         }
5293
5294 out2:;
5295         ldap_pvt_thread_pool_resume( &connection_pool );
5296
5297 out:;
5298         {       int repl = op->o_dont_replicate;
5299                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
5300                         rs->sr_text = NULL; /* Set after config_add_internal */
5301                         rs->sr_err = LDAP_SUCCESS;
5302                         op->o_dont_replicate = 1;
5303                 }
5304                 send_ldap_result( op, rs );
5305                 op->o_dont_replicate = repl;
5306         }
5307         slap_graduate_commit_csn( op );
5308         return rs->sr_err;
5309 }
5310
5311 typedef struct delrec {
5312         struct delrec *next;
5313         int nidx;
5314         int idx[1];
5315 } delrec;
5316
5317 static int
5318 config_modify_add( ConfigTable *ct, ConfigArgs *ca, AttributeDescription *ad,
5319         int i )
5320 {
5321         int rc;
5322
5323         ca->valx = -1;
5324         if (ad->ad_type->sat_flags & SLAP_AT_ORDERED &&
5325                 ca->line[0] == '{' )
5326         {
5327                 char *ptr = strchr( ca->line + 1, '}' );
5328                 if ( ptr ) {
5329                         char    *next;
5330
5331                         ca->valx = strtol( ca->line + 1, &next, 0 );
5332                         if ( next == ca->line + 1 || next[ 0 ] != '}' ) {
5333                                 return LDAP_OTHER;
5334                         }
5335                         ca->line = ptr+1;
5336                 }
5337         }
5338         rc = config_parse_add( ct, ca, i );
5339         if ( rc ) {
5340                 rc = LDAP_OTHER;
5341         }
5342         return rc;
5343 }
5344
5345 static int
5346 config_modify_internal( CfEntryInfo *ce, Operation *op, SlapReply *rs,
5347         ConfigArgs *ca )
5348 {
5349         int rc = LDAP_UNWILLING_TO_PERFORM;
5350         Modifications *ml;
5351         Entry *e = ce->ce_entry;
5352         Attribute *save_attrs = e->e_attrs, *oc_at, *s, *a;
5353         ConfigTable *ct;
5354         ConfigOCs **colst;
5355         int i, nocs;
5356         char *ptr;
5357         delrec *dels = NULL, *deltail = NULL;
5358
5359         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
5360         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
5361
5362         colst = count_ocs( oc_at, &nocs );
5363
5364         /* make sure add/del flags are clear; should always be true */
5365         for ( s = save_attrs; s; s = s->a_next ) {
5366                 s->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
5367         }
5368
5369         e->e_attrs = attrs_dup( e->e_attrs );
5370
5371         init_config_argv( ca );
5372         ca->be = ce->ce_be;
5373         ca->bi = ce->ce_bi;
5374         ca->ca_private = ce->ce_private;
5375         ca->ca_entry = e;
5376         ca->fname = "slapd";
5377         ca->ca_op = op;
5378         strcpy( ca->log, "back-config" );
5379
5380         for (ml = op->orm_modlist; ml; ml=ml->sml_next) {
5381                 ct = config_find_table( colst, nocs, ml->sml_desc, ca );
5382                 switch (ml->sml_op) {
5383                 case LDAP_MOD_DELETE:
5384                 case LDAP_MOD_REPLACE: {
5385                         BerVarray vals = NULL, nvals = NULL;
5386                         int *idx = NULL;
5387                         if ( ct && ( ct->arg_type & ARG_NO_DELETE )) {
5388                                 rc = LDAP_OTHER;
5389                                 snprintf(ca->cr_msg, sizeof(ca->cr_msg), "cannot delete %s",
5390                                         ml->sml_desc->ad_cname.bv_val );
5391                                 goto out_noop;
5392                         }
5393                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5394                                 vals = ml->sml_values;
5395                                 nvals = ml->sml_nvalues;
5396                                 ml->sml_values = NULL;
5397                                 ml->sml_nvalues = NULL;
5398                         }
5399                         /* If we're deleting by values, remember the indexes of the
5400                          * values we deleted.
5401                          */
5402                         if ( ct && ml->sml_values ) {
5403                                 delrec *d;
5404                                 i = ml->sml_numvals;
5405                                 d = ch_malloc( sizeof(delrec) + (i - 1)* sizeof(int));
5406                                 d->nidx = i;
5407                                 d->next = NULL;
5408                                 if ( dels ) {
5409                                         deltail->next = d;
5410                                 } else {
5411                                         dels = d;
5412                                 }
5413                                 deltail = d;
5414                                 idx = d->idx;
5415                         }
5416                         rc = modify_delete_vindex(e, &ml->sml_mod,
5417                                 get_permissiveModify(op),
5418                                 &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg), idx );
5419                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5420                                 ml->sml_values = vals;
5421                                 ml->sml_nvalues = nvals;
5422                         }
5423                         if ( !vals )
5424                                 break;
5425                         }
5426                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
5427
5428                 case LDAP_MOD_ADD:
5429                 case SLAP_MOD_SOFTADD: {
5430                         int mop = ml->sml_op;
5431                         int navals = -1;
5432                         ml->sml_op = LDAP_MOD_ADD;
5433                         if ( ct ) {
5434                                 if ( ct->arg_type & ARG_NO_INSERT ) {
5435                                         Attribute *a = attr_find( e->e_attrs, ml->sml_desc );
5436                                         if ( a ) {
5437                                                 navals = a->a_numvals;
5438                                         }
5439                                 }
5440                                 for ( i=0; !BER_BVISNULL( &ml->sml_values[i] ); i++ ) {
5441                                         if ( ml->sml_values[i].bv_val[0] == '{' &&
5442                                                 navals >= 0 )
5443                                         {
5444                                                 char    *next, *val = ml->sml_values[i].bv_val + 1;
5445                                                 int     j;
5446
5447                                                 j = strtol( val, &next, 0 );
5448                                                 if ( next == val || next[ 0 ] != '}' || j < navals ) {
5449                                                         rc = LDAP_OTHER;
5450                                                         snprintf(ca->cr_msg, sizeof(ca->cr_msg), "cannot insert %s",
5451                                                                 ml->sml_desc->ad_cname.bv_val );
5452                                                         goto out_noop;
5453                                                 }
5454                                         }
5455                                         rc = check_vals( ct, ca, ml, 0 );
5456                                         if ( rc ) goto out_noop;
5457                                 }
5458                         }
5459                         rc = modify_add_values(e, &ml->sml_mod,
5460                                    get_permissiveModify(op),
5461                                    &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg) );
5462
5463                         /* If value already exists, show success here
5464                          * and ignore this operation down below.
5465                          */
5466                         if ( mop == SLAP_MOD_SOFTADD ) {
5467                                 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS )
5468                                         rc = LDAP_SUCCESS;
5469                                 else
5470                                         mop = LDAP_MOD_ADD;
5471                         }
5472                         ml->sml_op = mop;
5473                         break;
5474                         }
5475
5476                         break;
5477                 case LDAP_MOD_INCREMENT:        /* FIXME */
5478                         break;
5479                 default:
5480                         break;
5481                 }
5482                 if(rc != LDAP_SUCCESS) break;
5483         }
5484         
5485         if ( rc == LDAP_SUCCESS) {
5486                 /* check that the entry still obeys the schema */
5487                 rc = entry_schema_check(op, e, NULL, 0, 0, NULL,
5488                         &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg) );
5489         }
5490         if ( rc ) goto out_noop;
5491
5492         /* Basic syntax checks are OK. Do the actual settings. */
5493         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
5494                 ct = config_find_table( colst, nocs, ml->sml_desc, ca );
5495                 if ( !ct ) continue;
5496
5497                 s = attr_find( save_attrs, ml->sml_desc );
5498                 a = attr_find( e->e_attrs, ml->sml_desc );
5499
5500                 switch (ml->sml_op) {
5501                 case LDAP_MOD_DELETE:
5502                 case LDAP_MOD_REPLACE: {
5503                         BerVarray vals = NULL, nvals = NULL;
5504                         delrec *d = NULL;
5505
5506                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5507                                 vals = ml->sml_values;
5508                                 nvals = ml->sml_nvalues;
5509                                 ml->sml_values = NULL;
5510                                 ml->sml_nvalues = NULL;
5511                         }
5512
5513                         if ( ml->sml_values )
5514                                 d = dels;
5515
5516                         /* If we didn't delete the whole attribute */
5517                         if ( ml->sml_values && a ) {
5518                                 struct berval *mvals;
5519                                 int j;
5520
5521                                 if ( ml->sml_nvalues )
5522                                         mvals = ml->sml_nvalues;
5523                                 else
5524                                         mvals = ml->sml_values;
5525
5526                                 /* use the indexes we saved up above */
5527                                 for (i=0; i < d->nidx; i++) {
5528                                         struct berval bv = *mvals++;
5529                                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED &&
5530                                                 bv.bv_val[0] == '{' ) {
5531                                                 ptr = strchr( bv.bv_val, '}' ) + 1;
5532                                                 bv.bv_len -= ptr - bv.bv_val;
5533                                                 bv.bv_val = ptr;
5534                                         }
5535                                         ca->line = bv.bv_val;
5536                                         ca->valx = d->idx[i];
5537                                         config_parse_vals(ct, ca, d->idx[i] );
5538                                         rc = config_del_vals( ct, ca );
5539                                         if ( rc != LDAP_SUCCESS ) break;
5540                                         if ( s )
5541                                                 s->a_flags |= SLAP_ATTR_IXDEL;
5542                                         for (j=i+1; j < d->nidx; j++)
5543                                                 if ( d->idx[j] >d->idx[i] )
5544                                                         d->idx[j]--;
5545                                 }
5546                         } else {
5547                                 ca->valx = -1;
5548                                 ca->line = NULL;
5549                                 ca->argc = 1;
5550                                 rc = config_del_vals( ct, ca );
5551                                 if ( rc ) rc = LDAP_OTHER;
5552                                 if ( s )
5553                                         s->a_flags |= SLAP_ATTR_IXDEL;
5554                         }
5555                         if ( ml->sml_values ) {
5556                                 d = d->next;
5557                                 ch_free( dels );
5558                                 dels = d;
5559                         }
5560                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5561                                 ml->sml_values = vals;
5562                                 ml->sml_nvalues = nvals;
5563                         }
5564                         if ( !vals || rc != LDAP_SUCCESS )
5565                                 break;
5566                         }
5567                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
5568
5569                 case LDAP_MOD_ADD:
5570                         for (i=0; ml->sml_values[i].bv_val; i++) {
5571                                 ca->line = ml->sml_values[i].bv_val;
5572                                 ca->valx = -1;
5573                                 rc = config_modify_add( ct, ca, ml->sml_desc, i );
5574                                 if ( rc )
5575                                         goto out;
5576                                 a->a_flags |= SLAP_ATTR_IXADD;
5577                         }
5578                         break;
5579                 }
5580         }
5581
5582 out:
5583         /* Undo for a failed operation */
5584         if ( rc != LDAP_SUCCESS ) {
5585                 ConfigReply msg = ca->reply;
5586                 for ( s = save_attrs; s; s = s->a_next ) {
5587                         if ( s->a_flags & SLAP_ATTR_IXDEL ) {
5588                                 s->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
5589                                 ct = config_find_table( colst, nocs, s->a_desc, ca );
5590                                 a = attr_find( e->e_attrs, s->a_desc );
5591                                 if ( a ) {
5592                                         /* clear the flag so the add check below will skip it */
5593                                         a->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
5594                                         ca->valx = -1;
5595                                         ca->line = NULL;
5596                                         ca->argc = 1;
5597                                         config_del_vals( ct, ca );
5598                                 }
5599                                 for ( i=0; !BER_BVISNULL( &s->a_vals[i] ); i++ ) {
5600                                         ca->line = s->a_vals[i].bv_val;
5601                                         ca->valx = -1;
5602                                         config_modify_add( ct, ca, s->a_desc, i );
5603                                 }
5604                         }
5605                 }
5606                 for ( a = e->e_attrs; a; a = a->a_next ) {
5607                         if ( a->a_flags & SLAP_ATTR_IXADD ) {
5608                                 ct = config_find_table( colst, nocs, a->a_desc, ca );
5609                                 ca->valx = -1;
5610                                 ca->line = NULL;
5611                                 ca->argc = 1;
5612                                 config_del_vals( ct, ca );
5613                                 s = attr_find( save_attrs, a->a_desc );
5614                                 if ( s ) {
5615                                         s->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
5616                                         for ( i=0; !BER_BVISNULL( &s->a_vals[i] ); i++ ) {
5617                                                 ca->line = s->a_vals[i].bv_val;
5618                                                 ca->valx = -1;
5619                                                 config_modify_add( ct, ca, s->a_desc, i );
5620                                         }
5621                                 }
5622                         }
5623                 }
5624                 ca->reply = msg;
5625         }
5626
5627         if ( ca->cleanup )
5628                 ca->cleanup( ca );
5629 out_noop:
5630         if ( rc == LDAP_SUCCESS ) {
5631                 attrs_free( save_attrs );
5632                 rs->sr_text = NULL;
5633         } else {
5634                 attrs_free( e->e_attrs );
5635                 e->e_attrs = save_attrs;
5636         }
5637         ch_free( ca->argv );
5638         if ( colst ) ch_free( colst );
5639         while( dels ) {
5640                 deltail = dels->next;
5641                 ch_free( dels );
5642                 dels = deltail;
5643         }
5644
5645         return rc;
5646 }
5647
5648 static int
5649 config_back_modify( Operation *op, SlapReply *rs )
5650 {
5651         CfBackInfo *cfb;
5652         CfEntryInfo *ce, *last;
5653         Modifications *ml;
5654         ConfigArgs ca = {0};
5655         struct berval rdn;
5656         char *ptr;
5657         AttributeDescription *rad = NULL;
5658         int do_pause = 1;
5659
5660         cfb = (CfBackInfo *)op->o_bd->be_private;
5661
5662         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5663         if ( !ce ) {
5664                 if ( last )
5665                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5666                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5667                 goto out;
5668         }
5669
5670         if ( !acl_check_modlist( op, ce->ce_entry, op->orm_modlist )) {
5671                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5672                 goto out;
5673         }
5674
5675         /* Get type of RDN */
5676         rdn = ce->ce_entry->e_nname;
5677         ptr = strchr( rdn.bv_val, '=' );
5678         rdn.bv_len = ptr - rdn.bv_val;
5679         rs->sr_err = slap_bv2ad( &rdn, &rad, &rs->sr_text );
5680         if ( rs->sr_err != LDAP_SUCCESS ) {
5681                 goto out;
5682         }
5683
5684         /* Some basic validation... */
5685         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
5686                 /* Don't allow Modify of RDN; must use ModRdn for that. */
5687                 if ( ml->sml_desc == rad ) {
5688                         rs->sr_err = LDAP_NOT_ALLOWED_ON_RDN;
5689                         rs->sr_text = "Use modrdn to change the entry name";
5690                         goto out;
5691                 }
5692                 /* Internal update of contextCSN? */
5693                 if ( ml->sml_desc == slap_schema.si_ad_contextCSN && op->o_conn->c_conn_idx == -1 ) {
5694                         do_pause = 0;
5695                         break;
5696                 }
5697         }
5698
5699         slap_mods_opattrs( op, &op->orm_modlist, 1 );
5700
5701         if ( do_pause ) {
5702                 if ( op->o_abandon ) {
5703                         rs->sr_err = SLAPD_ABANDON;
5704                         goto out;
5705                 }
5706                 ldap_pvt_thread_pool_pause( &connection_pool );
5707         }
5708
5709         /* Strategy:
5710          * 1) perform the Modify on the cached Entry.
5711          * 2) verify that the Entry still satisfies the schema.
5712          * 3) perform the individual config operations.
5713          * 4) store Modified entry in underlying LDIF backend.
5714          */
5715         rs->sr_err = config_modify_internal( ce, op, rs, &ca );
5716         if ( rs->sr_err ) {
5717                 rs->sr_text = ca.cr_msg;
5718         } else if ( cfb->cb_use_ldif ) {
5719                 BackendDB *be = op->o_bd;
5720                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
5721                 struct berval dn, ndn;
5722
5723                 op->o_bd = &cfb->cb_db;
5724
5725                 dn = op->o_dn;
5726                 ndn = op->o_ndn;
5727                 op->o_dn = op->o_bd->be_rootdn;
5728                 op->o_ndn = op->o_bd->be_rootndn;
5729
5730                 scp = op->o_callback;
5731                 op->o_callback = &sc;
5732                 op->o_bd->be_modify( op, rs );
5733                 op->o_bd = be;
5734                 op->o_callback = scp;
5735                 op->o_dn = dn;
5736                 op->o_ndn = ndn;
5737         }
5738
5739         if ( do_pause )
5740                 ldap_pvt_thread_pool_resume( &connection_pool );
5741 out:
5742         send_ldap_result( op, rs );
5743         slap_graduate_commit_csn( op );
5744         return rs->sr_err;
5745 }
5746
5747 static int
5748 config_back_modrdn( Operation *op, SlapReply *rs )
5749 {
5750         CfBackInfo *cfb;
5751         CfEntryInfo *ce, *last;
5752         struct berval rdn;
5753         int ixold, ixnew;
5754
5755         cfb = (CfBackInfo *)op->o_bd->be_private;
5756
5757         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5758         if ( !ce ) {
5759                 if ( last )
5760                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5761                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5762                 goto out;
5763         }
5764         if ( !access_allowed( op, ce->ce_entry, slap_schema.si_ad_entry,
5765                 NULL, ACL_WRITE, NULL )) {
5766                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5767                 goto out;
5768         }
5769         { Entry *parent;
5770                 if ( ce->ce_parent )
5771                         parent = ce->ce_parent->ce_entry;
5772                 else
5773                         parent = (Entry *)&slap_entry_root;
5774                 if ( !access_allowed( op, parent, slap_schema.si_ad_children,
5775                         NULL, ACL_WRITE, NULL )) {
5776                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5777                         goto out;
5778                 }
5779         }
5780
5781         /* We don't allow moving objects to new parents.
5782          * Generally we only allow reordering a set of ordered entries.
5783          */
5784         if ( op->orr_newSup ) {
5785                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5786                 goto out;
5787         }
5788
5789         /* If newRDN == oldRDN, quietly succeed */
5790         dnRdn( &op->o_req_ndn, &rdn );
5791         if ( dn_match( &rdn, &op->orr_nnewrdn )) {
5792                 rs->sr_err = LDAP_SUCCESS;
5793                 goto out;
5794         }
5795
5796         /* Current behavior, subject to change as needed:
5797          *
5798          * For backends and overlays, we only allow renumbering.
5799          * For schema, we allow renaming with the same number.
5800          * Otherwise, the op is not allowed.
5801          */
5802
5803         if ( ce->ce_type == Cft_Schema ) {
5804                 char *ptr1, *ptr2;
5805                 int len;
5806
5807                 /* Can't alter the main cn=schema entry */
5808                 if ( ce->ce_parent->ce_type == Cft_Global ) {
5809                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5810                         rs->sr_text = "renaming not allowed for this entry";
5811                         goto out;
5812                 }
5813
5814                 /* We could support this later if desired */
5815                 ptr1 = ber_bvchr( &rdn, '}' );
5816                 ptr2 = ber_bvchr( &op->orr_newrdn, '}' );
5817                 len = ptr1 - rdn.bv_val;
5818                 if ( len != ptr2 - op->orr_newrdn.bv_val ||
5819                         strncmp( rdn.bv_val, op->orr_newrdn.bv_val, len )) {
5820                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5821                         rs->sr_text = "schema reordering not supported";
5822                         goto out;
5823                 }
5824         } else if ( ce->ce_type == Cft_Database ||
5825                 ce->ce_type == Cft_Overlay ) {
5826                 char *ptr1, *ptr2, *iptr1, *iptr2;
5827                 int len1, len2;
5828
5829                 iptr2 = ber_bvchr( &op->orr_newrdn, '=' ) + 1;
5830                 if ( *iptr2 != '{' ) {
5831                         rs->sr_err = LDAP_NAMING_VIOLATION;
5832                         rs->sr_text = "new ordering index is required";
5833                         goto out;
5834                 }
5835                 iptr2++;
5836                 iptr1 = ber_bvchr( &rdn, '{' ) + 1;
5837                 ptr1 = ber_bvchr( &rdn, '}' );
5838                 ptr2 = ber_bvchr( &op->orr_newrdn, '}' );
5839                 if ( !ptr2 ) {
5840                         rs->sr_err = LDAP_NAMING_VIOLATION;
5841                         rs->sr_text = "new ordering index is required";
5842                         goto out;
5843                 }
5844
5845                 len1 = ptr1 - rdn.bv_val;
5846                 len2 = ptr2 - op->orr_newrdn.bv_val;
5847
5848                 if ( rdn.bv_len - len1 != op->orr_newrdn.bv_len - len2 ||
5849                         strncmp( ptr1, ptr2, rdn.bv_len - len1 )) {
5850                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5851                         rs->sr_text = "changing database/overlay type not allowed";
5852                         goto out;
5853                 }
5854                 ixold = strtol( iptr1, NULL, 0 );
5855                 ixnew = strtol( iptr2, &ptr1, 0 );
5856                 if ( ptr1 != ptr2 || ixold < 0 || ixnew < 0 ) {
5857                         rs->sr_err = LDAP_NAMING_VIOLATION;
5858                         goto out;
5859                 }
5860                 /* config DB is always 0, cannot be changed */
5861                 if ( ce->ce_type == Cft_Database && ( ixold == 0 || ixnew == 0 )) {
5862                         rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
5863                         goto out;
5864                 }
5865         } else {
5866                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5867                 rs->sr_text = "renaming not supported for this entry";
5868                 goto out;
5869         }
5870
5871         if ( op->o_abandon ) {
5872                 rs->sr_err = SLAPD_ABANDON;
5873                 goto out;
5874         }
5875         ldap_pvt_thread_pool_pause( &connection_pool );
5876
5877         if ( ce->ce_type == Cft_Schema ) {
5878                 req_modrdn_s modr = op->oq_modrdn;
5879                 struct berval rdn;
5880                 Attribute *a;
5881                 rs->sr_err = config_rename_attr( rs, ce->ce_entry, &rdn, &a );
5882                 if ( rs->sr_err == LDAP_SUCCESS ) {
5883                         rs->sr_err = config_rename_one( op, rs, ce->ce_entry,
5884                                 ce->ce_parent, a, &op->orr_newrdn, &op->orr_nnewrdn,
5885                                 cfb->cb_use_ldif );
5886                 }
5887                 op->oq_modrdn = modr;
5888         } else {
5889                 CfEntryInfo *ce2, *cebase, **cprev, **cbprev, *ceold;
5890                 req_modrdn_s modr = op->oq_modrdn;
5891                 int i;
5892
5893                 /* Advance to first of this type */
5894                 cprev = &ce->ce_parent->ce_kids;
5895                 for ( ce2 = *cprev; ce2 && ce2->ce_type != ce->ce_type; ) {
5896                         cprev = &ce2->ce_sibs;
5897                         ce2 = ce2->ce_sibs;
5898                 }
5899                 /* Skip the -1 entry */
5900                 if ( ce->ce_type == Cft_Database ) {
5901                         cprev = &ce2->ce_sibs;
5902                         ce2 = ce2->ce_sibs;
5903                 }
5904                 cebase = ce2;
5905                 cbprev = cprev;
5906
5907                 /* Remove from old slot */
5908                 for ( ce2 = *cprev; ce2 && ce2 != ce; ce2 = ce2->ce_sibs )
5909                         cprev = &ce2->ce_sibs;
5910                 *cprev = ce->ce_sibs;
5911                 ceold = ce->ce_sibs;
5912
5913                 /* Insert into new slot */
5914                 cprev = cbprev;
5915                 for ( i=0; i<ixnew; i++ ) {
5916                         ce2 = *cprev;
5917                         if ( !ce2 )
5918                                 break;
5919                         cprev = &ce2->ce_sibs;
5920                 }
5921                 ce->ce_sibs = *cprev;
5922                 *cprev = ce;
5923
5924                 ixnew = i;
5925
5926                 /* NOTE: These should be encoded in the OC tables, not inline here */
5927                 if ( ce->ce_type == Cft_Database )
5928                         backend_db_move( ce->ce_be, ixnew );
5929                 else if ( ce->ce_type == Cft_Overlay )
5930                         overlay_move( ce->ce_be, (slap_overinst *)ce->ce_bi, ixnew );
5931                         
5932                 if ( ixold < ixnew ) {
5933                         rs->sr_err = config_rename_del( op, rs, ce, ceold, ixold,
5934                                 cfb->cb_use_ldif );
5935                 } else {
5936                         rs->sr_err = config_rename_add( op, rs, ce, ixnew, 1,
5937                                 ixold - ixnew, cfb->cb_use_ldif );
5938                 }
5939                 op->oq_modrdn = modr;
5940         }
5941
5942         ldap_pvt_thread_pool_resume( &connection_pool );
5943 out:
5944         send_ldap_result( op, rs );
5945         return rs->sr_err;
5946 }
5947
5948 static int
5949 config_back_delete( Operation *op, SlapReply *rs )
5950 {
5951 #ifdef SLAP_CONFIG_DELETE
5952         CfBackInfo *cfb;
5953         CfEntryInfo *ce, *last, *ce2;
5954
5955         cfb = (CfBackInfo *)op->o_bd->be_private;
5956
5957         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5958         if ( !ce ) {
5959                 if ( last )
5960                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5961                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5962         } else if ( ce->ce_kids ) {
5963                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5964         } else if ( op->o_abandon ) {
5965                 rs->sr_err = SLAPD_ABANDON;
5966         } else if ( ce->ce_type == Cft_Overlay ){
5967                 char *iptr;
5968                 int count, ixold;
5969
5970                 ldap_pvt_thread_pool_pause( &connection_pool );
5971                 
5972                 overlay_remove( ce->ce_be, (slap_overinst *)ce->ce_bi );
5973
5974                 /* remove CfEntryInfo from the siblings list */
5975                 if ( ce->ce_parent->ce_kids == ce ) {
5976                         ce->ce_parent->ce_kids = ce->ce_sibs;
5977                 } else {
5978                         for ( ce2 = ce->ce_parent->ce_kids ; ce2; ce2 = ce2->ce_sibs ) {
5979                                 if ( ce2->ce_sibs == ce ) {
5980                                         ce2->ce_sibs = ce->ce_sibs;
5981                                         break;
5982                                 }
5983                         }
5984                 }
5985
5986                 /* remove from underlying database */
5987                 if ( cfb->cb_use_ldif ) {
5988                         BackendDB *be = op->o_bd;
5989                         slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
5990                         struct berval dn, ndn, req_dn, req_ndn;
5991
5992                         op->o_bd = &cfb->cb_db;
5993
5994                         dn = op->o_dn;
5995                         ndn = op->o_ndn;
5996                         req_dn = op->o_req_dn;
5997                         req_ndn = op->o_req_ndn;
5998
5999                         op->o_dn = op->o_bd->be_rootdn;
6000                         op->o_ndn = op->o_bd->be_rootndn;
6001                         op->o_req_dn = ce->ce_entry->e_name;
6002                         op->o_req_ndn = ce->ce_entry->e_nname;
6003
6004                         scp = op->o_callback;
6005                         op->o_callback = &sc;
6006                         op->o_bd->be_delete( op, rs );
6007                         op->o_bd = be;
6008                         op->o_callback = scp;
6009                         op->o_dn = dn;
6010                         op->o_ndn = ndn;
6011                         op->o_req_dn = req_dn;
6012                         op->o_req_ndn = req_ndn;
6013                 }
6014
6015                 /* renumber siblings */
6016                 iptr = ber_bvchr( &op->o_req_ndn, '{' ) + 1;
6017                 ixold = strtol( iptr, NULL, 0 );
6018                 for (ce2 = ce->ce_sibs, count=0; ce2; ce2=ce2->ce_sibs) {
6019                         config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
6020                                 count+ixold, 0, cfb->cb_use_ldif );
6021                         count++;
6022                 }
6023
6024                 ce->ce_entry->e_private=NULL;
6025                 entry_free(ce->ce_entry);
6026                 ch_free(ce);
6027                 ldap_pvt_thread_pool_resume( &connection_pool );
6028         } else {
6029                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6030         }
6031 #else
6032         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6033 #endif /* SLAP_CONFIG_DELETE */
6034         send_ldap_result( op, rs );
6035         return rs->sr_err;
6036 }
6037
6038 static int
6039 config_back_search( Operation *op, SlapReply *rs )
6040 {
6041         CfBackInfo *cfb;
6042         CfEntryInfo *ce, *last;
6043         slap_mask_t mask;
6044
6045         cfb = (CfBackInfo *)op->o_bd->be_private;
6046
6047         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
6048         if ( !ce ) {
6049                 if ( last )
6050                         rs->sr_matched = last->ce_entry->e_name.bv_val;
6051                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
6052                 goto out;
6053         }
6054         if ( !access_allowed_mask( op, ce->ce_entry, slap_schema.si_ad_entry, NULL,
6055                 ACL_SEARCH, NULL, &mask ))
6056         {
6057                 if ( !ACL_GRANT( mask, ACL_DISCLOSE )) {
6058                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
6059                 } else {
6060                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
6061                 }
6062                 goto out;
6063         }
6064         switch ( op->ors_scope ) {
6065         case LDAP_SCOPE_BASE:
6066         case LDAP_SCOPE_SUBTREE:
6067                 rs->sr_err = config_send( op, rs, ce, 0 );
6068                 break;
6069                 
6070         case LDAP_SCOPE_ONELEVEL:
6071                 for (ce = ce->ce_kids; ce; ce=ce->ce_sibs) {
6072                         rs->sr_err = config_send( op, rs, ce, 1 );
6073                         if ( rs->sr_err ) {
6074                                 break;
6075                         }
6076                 }
6077                 break;
6078         }
6079
6080 out:
6081         send_ldap_result( op, rs );
6082         return rs->sr_err;
6083 }
6084
6085 /* no-op, we never free entries */
6086 int config_entry_release(
6087         Operation *op,
6088         Entry *e,
6089         int rw )
6090 {
6091         if ( !e->e_private ) {
6092                 entry_free( e );
6093         }
6094         return LDAP_SUCCESS;
6095 }
6096
6097 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
6098  */
6099 int config_back_entry_get(
6100         Operation *op,
6101         struct berval *ndn,
6102         ObjectClass *oc,
6103         AttributeDescription *at,
6104         int rw,
6105         Entry **ent )
6106 {
6107         CfBackInfo *cfb;
6108         CfEntryInfo *ce, *last;
6109         int rc = LDAP_NO_SUCH_OBJECT;
6110
6111         cfb = (CfBackInfo *)op->o_bd->be_private;
6112
6113         ce = config_find_base( cfb->cb_root, ndn, &last );
6114         if ( ce ) {
6115                 *ent = ce->ce_entry;
6116                 if ( *ent ) {
6117                         rc = LDAP_SUCCESS;
6118                         if ( oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
6119                                 rc = LDAP_NO_SUCH_ATTRIBUTE;
6120                                 *ent = NULL;
6121                         }
6122                 }
6123         }
6124
6125         return rc;
6126 }
6127
6128 static int
6129 config_build_attrs( Entry *e, AttributeType **at, AttributeDescription *ad,
6130         ConfigTable *ct, ConfigArgs *c )
6131 {
6132         int i, rc;
6133
6134         for (; at && *at; at++) {
6135                 /* Skip the naming attr */
6136                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
6137                         continue;
6138                 for (i=0;ct[i].name;i++) {
6139                         if (ct[i].ad == (*at)->sat_ad) {
6140                                 rc = config_get_vals(&ct[i], c);
6141                                 /* NOTE: tolerate that config_get_vals()
6142                                  * returns success with no values */
6143                                 if (rc == LDAP_SUCCESS && c->rvalue_vals != NULL ) {
6144                                         if ( c->rvalue_nvals )
6145                                                 rc = attr_merge(e, ct[i].ad, c->rvalue_vals,
6146                                                         c->rvalue_nvals);
6147                                         else {
6148                                                 slap_syntax_validate_func *validate =
6149                                                         ct[i].ad->ad_type->sat_syntax->ssyn_validate;
6150                                                 if ( validate ) {
6151                                                         int j;
6152                                                         for ( j=0; c->rvalue_vals[j].bv_val; j++ ) {
6153                                                                 rc = ordered_value_validate( ct[i].ad,
6154                                                                         &c->rvalue_vals[j], LDAP_MOD_ADD );
6155                                                                 if ( rc ) {
6156                                                                         Debug( LDAP_DEBUG_ANY,
6157                                                                                 "config_build_attrs: error %d on %s value #%d\n",
6158                                                                                 rc, ct[i].ad->ad_cname.bv_val, j );
6159                                                                         return rc;
6160                                                                 }
6161                                                         }
6162                                                 }
6163                                                         
6164                                                 rc = attr_merge_normalize(e, ct[i].ad,
6165                                                         c->rvalue_vals, NULL);
6166                                         }
6167                                         ber_bvarray_free( c->rvalue_nvals );
6168                                         ber_bvarray_free( c->rvalue_vals );
6169                                         if ( rc ) {
6170                                                 Debug( LDAP_DEBUG_ANY,
6171                                                         "config_build_attrs: error %d on %s\n",
6172                                                         rc, ct[i].ad->ad_cname.bv_val, 0 );
6173                                                 return rc;
6174                                         }
6175                                 }
6176                                 break;
6177                         }
6178                 }
6179         }
6180         return 0;
6181 }
6182
6183 /* currently (2010) does not access rs except possibly writing rs->sr_err */
6184
6185 Entry *
6186 config_build_entry( Operation *op, SlapReply *rs, CfEntryInfo *parent,
6187         ConfigArgs *c, struct berval *rdn, ConfigOCs *main, ConfigOCs *extra )
6188 {
6189         Entry *e = entry_alloc();
6190         CfEntryInfo *ce = ch_calloc( 1, sizeof(CfEntryInfo) );
6191         struct berval val;
6192         struct berval ad_name;
6193         AttributeDescription *ad = NULL;
6194         int rc;
6195         char *ptr;
6196         const char *text = "";
6197         Attribute *oc_at;
6198         struct berval pdn;
6199         ObjectClass *oc;
6200         CfEntryInfo *ceprev = NULL;
6201
6202         Debug( LDAP_DEBUG_TRACE, "config_build_entry: \"%s\"\n", rdn->bv_val, 0, 0);
6203         e->e_private = ce;
6204         ce->ce_entry = e;
6205         ce->ce_type = main->co_type;
6206         ce->ce_parent = parent;
6207         if ( parent ) {
6208                 pdn = parent->ce_entry->e_nname;
6209                 if ( parent->ce_kids && parent->ce_kids->ce_type <= ce->ce_type )
6210                         for ( ceprev = parent->ce_kids; ceprev->ce_sibs &&
6211                                 ceprev->ce_type <= ce->ce_type;
6212                                 ceprev = ceprev->ce_sibs );
6213         } else {
6214                 BER_BVZERO( &pdn );
6215         }
6216
6217         ce->ce_private = c->ca_private;
6218         ce->ce_be = c->be;
6219         ce->ce_bi = c->bi;
6220
6221         build_new_dn( &e->e_name, &pdn, rdn, NULL );
6222         ber_dupbv( &e->e_nname, &e->e_name );
6223
6224         attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
6225                 main->co_name, NULL );
6226         if ( extra )
6227                 attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
6228                         extra->co_name, NULL );
6229         ptr = strchr(rdn->bv_val, '=');
6230         ad_name.bv_val = rdn->bv_val;
6231         ad_name.bv_len = ptr - rdn->bv_val;
6232         rc = slap_bv2ad( &ad_name, &ad, &text );
6233         if ( rc ) {
6234                 goto fail;
6235         }
6236         val.bv_val = ptr+1;
6237         val.bv_len = rdn->bv_len - (val.bv_val - rdn->bv_val);
6238         attr_merge_normalize_one(e, ad, &val, NULL );
6239
6240         oc = main->co_oc;
6241         c->table = main->co_type;
6242         if ( oc->soc_required ) {
6243                 rc = config_build_attrs( e, oc->soc_required, ad, main->co_table, c );
6244                 if ( rc ) goto fail;
6245         }
6246
6247         if ( oc->soc_allowed ) {
6248                 rc = config_build_attrs( e, oc->soc_allowed, ad, main->co_table, c );
6249                 if ( rc ) goto fail;
6250         }
6251
6252         if ( extra ) {
6253                 oc = extra->co_oc;
6254                 c->table = extra->co_type;
6255                 if ( oc->soc_required ) {
6256                         rc = config_build_attrs( e, oc->soc_required, ad, extra->co_table, c );
6257                         if ( rc ) goto fail;
6258                 }
6259
6260                 if ( oc->soc_allowed ) {
6261                         rc = config_build_attrs( e, oc->soc_allowed, ad, extra->co_table, c );
6262                         if ( rc ) goto fail;
6263                 }
6264         }
6265
6266         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
6267         rc = structural_class(oc_at->a_vals, &oc, NULL, &text, c->cr_msg,
6268                 sizeof(c->cr_msg), op ? op->o_tmpmemctx : NULL );
6269         if ( rc != LDAP_SUCCESS ) {
6270 fail:
6271                 Debug( LDAP_DEBUG_ANY,
6272                         "config_build_entry: build \"%s\" failed: \"%s\"\n",
6273                         rdn->bv_val, text, 0);
6274                 return NULL;
6275         }
6276         attr_merge_normalize_one(e, slap_schema.si_ad_structuralObjectClass, &oc->soc_cname, NULL );
6277         if ( op ) {
6278                 op->ora_e = e;
6279                 op->ora_modlist = NULL;
6280                 slap_add_opattrs( op, NULL, NULL, 0, 0 );
6281                 if ( !op->o_noop ) {
6282                         SlapReply rs2 = {REP_RESULT};
6283                         op->o_bd->be_add( op, &rs2 );
6284                         rs->sr_err = rs2.sr_err;
6285                         rs_assert_done( &rs2 );
6286                         if ( ( rs2.sr_err != LDAP_SUCCESS ) 
6287                                         && (rs2.sr_err != LDAP_ALREADY_EXISTS) ) {
6288                                 goto fail;
6289                         }
6290                 }
6291         }
6292         if ( ceprev ) {
6293                 ce->ce_sibs = ceprev->ce_sibs;
6294                 ceprev->ce_sibs = ce;
6295         } else if ( parent ) {
6296                 ce->ce_sibs = parent->ce_kids;
6297                 parent->ce_kids = ce;
6298         }
6299
6300         return e;
6301 }
6302
6303 static int
6304 config_build_schema_inc( ConfigArgs *c, CfEntryInfo *ceparent,
6305         Operation *op, SlapReply *rs )
6306 {
6307         Entry *e;
6308         ConfigFile *cf = c->ca_private;
6309         char *ptr;
6310         struct berval bv, rdn;
6311
6312         for (; cf; cf=cf->c_sibs, c->depth++) {
6313                 if ( !cf->c_at_head && !cf->c_cr_head && !cf->c_oc_head &&
6314                         !cf->c_om_head && !cf->c_syn_head ) continue;
6315                 c->value_dn.bv_val = c->log;
6316                 LUTIL_SLASHPATH( cf->c_file.bv_val );
6317                 bv.bv_val = strrchr(cf->c_file.bv_val, LDAP_DIRSEP[0]);
6318                 if ( !bv.bv_val ) {
6319                         bv = cf->c_file;
6320                 } else {
6321                         bv.bv_val++;
6322                         bv.bv_len = cf->c_file.bv_len - (bv.bv_val - cf->c_file.bv_val);
6323                 }
6324                 ptr = strchr( bv.bv_val, '.' );
6325                 if ( ptr )
6326                         bv.bv_len = ptr - bv.bv_val;
6327                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=" SLAP_X_ORDERED_FMT, c->depth);
6328                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
6329                         /* FIXME: how can indicate error? */
6330                         return -1;
6331                 }
6332                 strncpy( c->value_dn.bv_val + c->value_dn.bv_len, bv.bv_val,
6333                         bv.bv_len );
6334                 c->value_dn.bv_len += bv.bv_len;
6335                 c->value_dn.bv_val[c->value_dn.bv_len] ='\0';
6336                 rdn = c->value_dn;
6337
6338                 c->ca_private = cf;
6339                 e = config_build_entry( op, rs, ceparent, c, &rdn,
6340                         &CFOC_SCHEMA, NULL );
6341                 if ( !e ) {
6342                         return -1;
6343                 } else if ( e && cf->c_kids ) {
6344                         c->ca_private = cf->c_kids;
6345                         config_build_schema_inc( c, e->e_private, op, rs );
6346                 }
6347         }
6348         return 0;
6349 }
6350
6351 #ifdef SLAPD_MODULES
6352
6353 static int
6354 config_build_modules( ConfigArgs *c, CfEntryInfo *ceparent,
6355         Operation *op, SlapReply *rs )
6356 {
6357         int i;
6358         ModPaths *mp;
6359
6360         for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
6361                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
6362                         continue;
6363                 c->value_dn.bv_val = c->log;
6364                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=module" SLAP_X_ORDERED_FMT, i);
6365                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
6366                         /* FIXME: how can indicate error? */
6367                         return -1;
6368                 }
6369                 c->ca_private = mp;
6370                 if ( ! config_build_entry( op, rs, ceparent, c, &c->value_dn, &CFOC_MODULE, NULL )) {
6371                         return -1;
6372                 }
6373         }
6374         return 0;
6375 }
6376 #endif
6377
6378 static int
6379 config_check_schema(Operation *op, CfBackInfo *cfb)
6380 {
6381         struct berval schema_dn = BER_BVC(SCHEMA_RDN "," CONFIG_RDN);
6382         ConfigArgs c = {0};
6383         CfEntryInfo *ce, *last;
6384         Entry *e;
6385
6386         /* If there's no root entry, we must be in the midst of converting */
6387         if ( !cfb->cb_root )
6388                 return 0;
6389
6390         /* Make sure the main schema entry exists */
6391         ce = config_find_base( cfb->cb_root, &schema_dn, &last );
6392         if ( ce ) {
6393                 Attribute *a;
6394                 struct berval *bv;
6395
6396                 e = ce->ce_entry;
6397
6398                 /* Make sure it's up to date */
6399                 if ( cf_om_tail != om_sys_tail ) {
6400                         a = attr_find( e->e_attrs, cfAd_om );
6401                         if ( a ) {
6402                                 if ( a->a_nvals != a->a_vals )
6403                                         ber_bvarray_free( a->a_nvals );
6404                                 ber_bvarray_free( a->a_vals );
6405                                 a->a_vals = NULL;
6406                                 a->a_nvals = NULL;
6407                                 a->a_numvals = 0;
6408                         }
6409                         oidm_unparse( &bv, NULL, NULL, 1 );
6410                         attr_merge_normalize( e, cfAd_om, bv, NULL );
6411                         ber_bvarray_free( bv );
6412                         cf_om_tail = om_sys_tail;
6413                 }
6414                 if ( cf_at_tail != at_sys_tail ) {
6415                         a = attr_find( e->e_attrs, cfAd_attr );
6416                         if ( a ) {
6417                                 if ( a->a_nvals != a->a_vals )
6418                                         ber_bvarray_free( a->a_nvals );
6419                                 ber_bvarray_free( a->a_vals );
6420                                 a->a_vals = NULL;
6421                                 a->a_nvals = NULL;
6422                                 a->a_numvals = 0;
6423                         }
6424                         at_unparse( &bv, NULL, NULL, 1 );
6425                         attr_merge_normalize( e, cfAd_attr, bv, NULL );
6426                         ber_bvarray_free( bv );
6427                         cf_at_tail = at_sys_tail;
6428                 }
6429                 if ( cf_oc_tail != oc_sys_tail ) {
6430                         a = attr_find( e->e_attrs, cfAd_oc );
6431                         if ( a ) {
6432                                 if ( a->a_nvals != a->a_vals )
6433                                         ber_bvarray_free( a->a_nvals );
6434                                 ber_bvarray_free( a->a_vals );
6435                                 a->a_vals = NULL;
6436                                 a->a_nvals = NULL;
6437                                 a->a_numvals = 0;
6438                         }
6439                         oc_unparse( &bv, NULL, NULL, 1 );
6440                         attr_merge_normalize( e, cfAd_oc, bv, NULL );
6441                         ber_bvarray_free( bv );
6442                         cf_oc_tail = oc_sys_tail;
6443                 }
6444                 if ( cf_syn_tail != syn_sys_tail ) {
6445                         a = attr_find( e->e_attrs, cfAd_syntax );
6446                         if ( a ) {
6447                                 if ( a->a_nvals != a->a_vals )
6448                                         ber_bvarray_free( a->a_nvals );
6449                                 ber_bvarray_free( a->a_vals );
6450                                 a->a_vals = NULL;
6451                                 a->a_nvals = NULL;
6452                                 a->a_numvals = 0;
6453                         }
6454                         syn_unparse( &bv, NULL, NULL, 1 );
6455                         attr_merge_normalize( e, cfAd_syntax, bv, NULL );
6456                         ber_bvarray_free( bv );
6457                         cf_syn_tail = syn_sys_tail;
6458                 }
6459         } else {
6460                 SlapReply rs = {REP_RESULT};
6461                 c.ca_private = NULL;
6462                 e = config_build_entry( op, &rs, cfb->cb_root, &c, &schema_rdn,
6463                         &CFOC_SCHEMA, NULL );
6464                 if ( !e ) {
6465                         return -1;
6466                 }
6467                 ce = e->e_private;
6468                 ce->ce_private = cfb->cb_config;
6469                 cf_at_tail = at_sys_tail;
6470                 cf_oc_tail = oc_sys_tail;
6471                 cf_om_tail = om_sys_tail;
6472                 cf_syn_tail = syn_sys_tail;
6473         }
6474         return 0;
6475 }
6476
6477 static const char *defacl[] = {
6478         NULL, "to", "*", "by", "*", "none", NULL
6479 };
6480
6481 static int
6482 config_back_db_open( BackendDB *be, ConfigReply *cr )
6483 {
6484         CfBackInfo *cfb = be->be_private;
6485         struct berval rdn;
6486         Entry *e, *parent;
6487         CfEntryInfo *ce, *ceparent;
6488         int i, unsupp = 0;
6489         BackendInfo *bi;
6490         ConfigArgs c;
6491         Connection conn = {0};
6492         OperationBuffer opbuf;
6493         Operation *op;
6494         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
6495         SlapReply rs = {REP_RESULT};
6496         void *thrctx = NULL;
6497
6498         Debug( LDAP_DEBUG_TRACE, "config_back_db_open\n", 0, 0, 0);
6499
6500         /* If we have no explicitly configured ACLs, don't just use
6501          * the global ACLs. Explicitly deny access to everything.
6502          */
6503         if ( !be->bd_self->be_acl ) {
6504                 parse_acl(be->bd_self, "config_back_db_open", 0, 6, (char **)defacl, 0 );
6505         }
6506
6507         thrctx = ldap_pvt_thread_pool_context();
6508         connection_fake_init( &conn, &opbuf, thrctx );
6509         op = &opbuf.ob_op;
6510
6511         op->o_tag = LDAP_REQ_ADD;
6512         op->o_callback = &cb;
6513         op->o_bd = &cfb->cb_db;
6514         op->o_dn = op->o_bd->be_rootdn;
6515         op->o_ndn = op->o_bd->be_rootndn;
6516
6517         if ( !cfb->cb_use_ldif ) {
6518                 op->o_noop = 1;
6519         }
6520
6521         /* If we read the config from back-ldif, do some quick sanity checks */
6522         if ( cfb->cb_got_ldif ) {
6523                 return config_check_schema( op, cfb );
6524         }
6525
6526         /* create root of tree */
6527         rdn = config_rdn;
6528         c.ca_private = cfb->cb_config;
6529         c.be = frontendDB;
6530         e = config_build_entry( op, &rs, NULL, &c, &rdn, &CFOC_GLOBAL, NULL );
6531         if ( !e ) {
6532                 return -1;
6533         }
6534         ce = e->e_private;
6535         cfb->cb_root = ce;
6536
6537         parent = e;
6538         ceparent = ce;
6539
6540 #ifdef SLAPD_MODULES
6541         /* Create Module nodes... */
6542         if ( modpaths.mp_loads ) {
6543                 if ( config_build_modules( &c, ceparent, op, &rs ) ){
6544                         return -1;
6545                 }
6546         }
6547 #endif
6548
6549         /* Create schema nodes... cn=schema will contain the hardcoded core
6550          * schema, read-only. Child objects will contain runtime loaded schema
6551          * files.
6552          */
6553         rdn = schema_rdn;
6554         c.ca_private = NULL;
6555         e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_SCHEMA, NULL );
6556         if ( !e ) {
6557                 return -1;
6558         }
6559         ce = e->e_private;
6560         ce->ce_private = cfb->cb_config;
6561         cf_at_tail = at_sys_tail;
6562         cf_oc_tail = oc_sys_tail;
6563         cf_om_tail = om_sys_tail;
6564         cf_syn_tail = syn_sys_tail;
6565
6566         /* Create schema nodes for included schema... */
6567         if ( cfb->cb_config->c_kids ) {
6568                 int rc;
6569                 c.depth = 0;
6570                 c.ca_private = cfb->cb_config->c_kids;
6571                 rc = config_build_schema_inc( &c, ce, op, &rs );
6572                 if ( rc ) {
6573                         return -1;
6574                 }
6575         }
6576
6577         /* Create backend nodes. Skip if they don't provide a cf_table.
6578          * There usually aren't any of these.
6579          */
6580         
6581         c.line = 0;
6582         LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next) {
6583                 if (!bi->bi_cf_ocs) {
6584                         /* If it only supports the old config mech, complain. */
6585                         if ( bi->bi_config ) {
6586                                 Debug( LDAP_DEBUG_ANY,
6587                                         "WARNING: No dynamic config support for backend %s.\n",
6588                                         bi->bi_type, 0, 0 );
6589                                 unsupp++;
6590                         }
6591                         continue;
6592                 }
6593                 if (!bi->bi_private) continue;
6594
6595                 rdn.bv_val = c.log;
6596                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
6597                         "%s=%s", cfAd_backend->ad_cname.bv_val, bi->bi_type);
6598                 if ( rdn.bv_len >= sizeof( c.log ) ) {
6599                         /* FIXME: holler ... */ ;
6600                 }
6601                 c.bi = bi;
6602                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_BACKEND,
6603                         bi->bi_cf_ocs );
6604                 if ( !e ) {
6605                         return -1;
6606                 }
6607         }
6608
6609         /* Create database nodes... */
6610         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
6611         LDAP_STAILQ_NEXT(frontendDB, be_next) = LDAP_STAILQ_FIRST(&backendDB);
6612         for ( i = -1, be = frontendDB ; be;
6613                 i++, be = LDAP_STAILQ_NEXT( be, be_next )) {
6614                 slap_overinfo *oi = NULL;
6615
6616                 if ( overlay_is_over( be )) {
6617                         oi = be->bd_info->bi_private;
6618                         bi = oi->oi_orig;
6619                 } else {
6620                         bi = be->bd_info;
6621                 }
6622
6623                 /* If this backend supports the old config mechanism, but not
6624                  * the new mech, complain.
6625                  */
6626                 if ( !be->be_cf_ocs && bi->bi_db_config ) {
6627                         Debug( LDAP_DEBUG_ANY,
6628                                 "WARNING: No dynamic config support for database %s.\n",
6629                                 bi->bi_type, 0, 0 );
6630                         unsupp++;
6631                 }
6632                 rdn.bv_val = c.log;
6633                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
6634                         "%s=" SLAP_X_ORDERED_FMT "%s", cfAd_database->ad_cname.bv_val,
6635                         i, bi->bi_type);
6636                 if ( rdn.bv_len >= sizeof( c.log ) ) {
6637                         /* FIXME: holler ... */ ;
6638                 }
6639                 c.be = be;
6640                 c.bi = bi;
6641                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_DATABASE,
6642                         be->be_cf_ocs );
6643                 if ( !e ) {
6644                         return -1;
6645                 }
6646                 ce = e->e_private;
6647                 if ( be->be_cf_ocs && be->be_cf_ocs->co_cfadd ) {
6648                         rs_reinit( &rs, REP_RESULT );
6649                         be->be_cf_ocs->co_cfadd( op, &rs, e, &c );
6650                 }
6651                 /* Iterate through overlays */
6652                 if ( oi ) {
6653                         slap_overinst *on;
6654                         Entry *oe;
6655                         int j;
6656                         voidList *vl, *v0 = NULL;
6657
6658                         /* overlays are in LIFO order, must reverse stack */
6659                         for (on=oi->oi_list; on; on=on->on_next) {
6660                                 vl = ch_malloc( sizeof( voidList ));
6661                                 vl->vl_next = v0;
6662                                 v0 = vl;
6663                                 vl->vl_ptr = on;
6664                         }
6665                         for (j=0; vl; j++,vl=v0) {
6666                                 on = vl->vl_ptr;
6667                                 v0 = vl->vl_next;
6668                                 ch_free( vl );
6669                                 if ( on->on_bi.bi_db_config && !on->on_bi.bi_cf_ocs ) {
6670                                         Debug( LDAP_DEBUG_ANY,
6671                                                 "WARNING: No dynamic config support for overlay %s.\n",
6672                                                 on->on_bi.bi_type, 0, 0 );
6673                                         unsupp++;
6674                                 }
6675                                 rdn.bv_val = c.log;
6676                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
6677                                         "%s=" SLAP_X_ORDERED_FMT "%s",
6678                                         cfAd_overlay->ad_cname.bv_val, j, on->on_bi.bi_type );
6679                                 if ( rdn.bv_len >= sizeof( c.log ) ) {
6680                                         /* FIXME: holler ... */ ;
6681                                 }
6682                                 c.be = be;
6683                                 c.bi = &on->on_bi;
6684                                 oe = config_build_entry( op, &rs, ce, &c, &rdn,
6685                                         &CFOC_OVERLAY, c.bi->bi_cf_ocs );
6686                                 if ( !oe ) {
6687                                         return -1;
6688                                 }
6689                                 if ( c.bi->bi_cf_ocs && c.bi->bi_cf_ocs->co_cfadd ) {
6690                                         rs_reinit( &rs, REP_RESULT );
6691                                         c.bi->bi_cf_ocs->co_cfadd( op, &rs, oe, &c );
6692                                 }
6693                         }
6694                 }
6695         }
6696         if ( thrctx )
6697                 ldap_pvt_thread_pool_context_reset( thrctx );
6698
6699         if ( unsupp  && cfb->cb_use_ldif ) {
6700                 Debug( LDAP_DEBUG_ANY, "\nWARNING: The converted cn=config "
6701                         "directory is incomplete and may not work.\n\n", 0, 0, 0 );
6702         }
6703
6704         return 0;
6705 }
6706
6707 static void
6708 cfb_free_cffile( ConfigFile *cf )
6709 {
6710         ConfigFile *next;
6711
6712         for (; cf; cf=next) {
6713                 next = cf->c_sibs;
6714                 if ( cf->c_kids )
6715                         cfb_free_cffile( cf->c_kids );
6716                 ch_free( cf->c_file.bv_val );
6717                 ber_bvarray_free( cf->c_dseFiles );
6718                 ch_free( cf );
6719         }
6720 }
6721
6722 static void
6723 cfb_free_entries( CfEntryInfo *ce )
6724 {
6725         CfEntryInfo *next;
6726
6727         for (; ce; ce=next) {
6728                 next = ce->ce_sibs;
6729                 if ( ce->ce_kids )
6730                         cfb_free_entries( ce->ce_kids );
6731                 ce->ce_entry->e_private = NULL;
6732                 entry_free( ce->ce_entry );
6733                 ch_free( ce );
6734         }
6735 }
6736
6737 static int
6738 config_back_db_close( BackendDB *be, ConfigReply *cr )
6739 {
6740         CfBackInfo *cfb = be->be_private;
6741
6742         cfb_free_entries( cfb->cb_root );
6743         cfb->cb_root = NULL;
6744
6745         if ( cfb->cb_db.bd_info ) {
6746                 backend_shutdown( &cfb->cb_db );
6747         }
6748
6749         return 0;
6750 }
6751
6752 static int
6753 config_back_db_destroy( BackendDB *be, ConfigReply *cr )
6754 {
6755         CfBackInfo *cfb = be->be_private;
6756
6757         cfb_free_cffile( cfb->cb_config );
6758
6759         ch_free( cfdir.bv_val );
6760
6761         avl_free( CfOcTree, NULL );
6762
6763         if ( cfb->cb_db.bd_info ) {
6764                 cfb->cb_db.be_suffix = NULL;
6765                 cfb->cb_db.be_nsuffix = NULL;
6766                 BER_BVZERO( &cfb->cb_db.be_rootdn );
6767                 BER_BVZERO( &cfb->cb_db.be_rootndn );
6768
6769                 backend_destroy_one( &cfb->cb_db, 0 );
6770         }
6771
6772         loglevel_destroy();
6773
6774         return 0;
6775 }
6776
6777 static int
6778 config_back_db_init( BackendDB *be, ConfigReply* cr )
6779 {
6780         struct berval dn;
6781         CfBackInfo *cfb;
6782
6783         cfb = &cfBackInfo;
6784         cfb->cb_config = ch_calloc( 1, sizeof(ConfigFile));
6785         cfn = cfb->cb_config;
6786         be->be_private = cfb;
6787
6788         ber_dupbv( &be->be_rootdn, &config_rdn );
6789         ber_dupbv( &be->be_rootndn, &be->be_rootdn );
6790         ber_dupbv( &dn, &be->be_rootdn );
6791         ber_bvarray_add( &be->be_suffix, &dn );
6792         ber_dupbv( &dn, &be->be_rootdn );
6793         ber_bvarray_add( &be->be_nsuffix, &dn );
6794
6795         /* Hide from namingContexts */
6796         SLAP_BFLAGS(be) |= SLAP_BFLAG_CONFIG;
6797
6798         /* Check ACLs on content of Adds by default */
6799         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_ACL_ADD;
6800
6801         return 0;
6802 }
6803
6804 static int
6805 config_back_destroy( BackendInfo *bi )
6806 {
6807         ldif_must_b64_encode_release();
6808         return 0;
6809 }
6810
6811 static int
6812 config_tool_entry_open( BackendDB *be, int mode )
6813 {
6814         CfBackInfo *cfb = be->be_private;
6815         BackendInfo *bi = cfb->cb_db.bd_info;
6816
6817         if ( bi && bi->bi_tool_entry_open )
6818                 return bi->bi_tool_entry_open( &cfb->cb_db, mode );
6819         else
6820                 return -1;
6821         
6822 }
6823
6824 static int
6825 config_tool_entry_close( BackendDB *be )
6826 {
6827         CfBackInfo *cfb = be->be_private;
6828         BackendInfo *bi = cfb->cb_db.bd_info;
6829
6830         if ( bi && bi->bi_tool_entry_close )
6831                 return bi->bi_tool_entry_close( &cfb->cb_db );
6832         else
6833                 return -1;
6834 }
6835
6836 static ID
6837 config_tool_entry_first( BackendDB *be )
6838 {
6839         CfBackInfo *cfb = be->be_private;
6840         BackendInfo *bi = cfb->cb_db.bd_info;
6841
6842         if ( bi && bi->bi_tool_entry_first ) {
6843                 return bi->bi_tool_entry_first( &cfb->cb_db );
6844         }
6845         if ( bi && bi->bi_tool_entry_first_x ) {
6846                 return bi->bi_tool_entry_first_x( &cfb->cb_db,
6847                         NULL, LDAP_SCOPE_DEFAULT, NULL );
6848         }
6849         return NOID;
6850 }
6851
6852 static ID
6853 config_tool_entry_first_x(
6854         BackendDB *be,
6855         struct berval *base,
6856         int scope,
6857         Filter *f )
6858 {
6859         CfBackInfo *cfb = be->be_private;
6860         BackendInfo *bi = cfb->cb_db.bd_info;
6861
6862         if ( bi && bi->bi_tool_entry_first_x ) {
6863                 return bi->bi_tool_entry_first_x( &cfb->cb_db, base, scope, f );
6864         }
6865         return NOID;
6866 }
6867
6868 static ID
6869 config_tool_entry_next( BackendDB *be )
6870 {
6871         CfBackInfo *cfb = be->be_private;
6872         BackendInfo *bi = cfb->cb_db.bd_info;
6873
6874         if ( bi && bi->bi_tool_entry_next )
6875                 return bi->bi_tool_entry_next( &cfb->cb_db );
6876         else
6877                 return NOID;
6878 }
6879
6880 static Entry *
6881 config_tool_entry_get( BackendDB *be, ID id )
6882 {
6883         CfBackInfo *cfb = be->be_private;
6884         BackendInfo *bi = cfb->cb_db.bd_info;
6885
6886         if ( bi && bi->bi_tool_entry_get )
6887                 return bi->bi_tool_entry_get( &cfb->cb_db, id );
6888         else
6889                 return NULL;
6890 }
6891
6892 static int entry_put_got_frontend=0;
6893 static int entry_put_got_config=0;
6894 static ID
6895 config_tool_entry_put( BackendDB *be, Entry *e, struct berval *text )
6896 {
6897         CfBackInfo *cfb = be->be_private;
6898         BackendInfo *bi = cfb->cb_db.bd_info;
6899         int rc;
6900         struct berval rdn, vals[ 2 ];
6901         ConfigArgs ca;
6902         OperationBuffer opbuf;
6903         Entry *ce;
6904         Connection conn = {0};
6905         Operation *op = NULL;
6906         void *thrctx;
6907         int isFrontend = 0;
6908
6909         /* Create entry for frontend database if it does not exist already */
6910         if ( !entry_put_got_frontend ) {
6911                 if ( !strncmp( e->e_nname.bv_val, "olcDatabase", 
6912                                 STRLENOF( "olcDatabase" ))) {
6913                         if ( strncmp( e->e_nname.bv_val + 
6914                                         STRLENOF( "olcDatabase" ), "={-1}frontend",
6915                                         STRLENOF( "={-1}frontend" )) && 
6916                                         strncmp( e->e_nname.bv_val + 
6917                                         STRLENOF( "olcDatabase" ), "=frontend",
6918                                         STRLENOF( "=frontend" ))) {
6919                                 vals[1].bv_len = 0;
6920                                 vals[1].bv_val = NULL;
6921                                 memset( &ca, 0, sizeof(ConfigArgs));
6922                                 ca.be = frontendDB;
6923                                 ca.bi = frontendDB->bd_info;
6924                                 ca.be->be_cf_ocs = &CFOC_FRONTEND;
6925                                 rdn.bv_val = ca.log;
6926                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( ca.log ),
6927                                         "%s=" SLAP_X_ORDERED_FMT "%s",
6928                                         cfAd_database->ad_cname.bv_val, -1,
6929                                         ca.bi->bi_type);
6930                                 ce = config_build_entry( NULL, NULL, cfb->cb_root, &ca, &rdn,
6931                                                 &CFOC_DATABASE, ca.be->be_cf_ocs );
6932                                 thrctx = ldap_pvt_thread_pool_context();
6933                                 connection_fake_init2( &conn, &opbuf, thrctx,0 );
6934                                 op = &opbuf.ob_op;
6935                                 op->o_bd = &cfb->cb_db;
6936                                 op->o_tag = LDAP_REQ_ADD;
6937                                 op->ora_e = ce;
6938                                 op->o_dn = be->be_rootdn;
6939                                 op->o_ndn = be->be_rootndn;
6940                                 rc = slap_add_opattrs(op, NULL, NULL, 0, 0);
6941                                 if ( rc != LDAP_SUCCESS ) {
6942                                         text->bv_val = "autocreation of \"olcDatabase={-1}frontend\" failed";
6943                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={-1}frontend\" failed");
6944                                         return NOID;
6945                                 }
6946
6947                                 if ( ce && bi && bi->bi_tool_entry_put && 
6948                                                 bi->bi_tool_entry_put( &cfb->cb_db, ce, text ) != NOID ) {
6949                                         entry_put_got_frontend++;
6950                                 } else {
6951                                         text->bv_val = "autocreation of \"olcDatabase={-1}frontend\" failed";
6952                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={-1}frontend\" failed");
6953                                         return NOID;
6954                                 }
6955                         } else {
6956                                 entry_put_got_frontend++;
6957                                 isFrontend = 1;
6958                         }
6959                 }
6960         }
6961         /* Create entry for config database if it does not exist already */
6962         if ( !entry_put_got_config && !isFrontend ) {
6963                 if ( !strncmp( e->e_nname.bv_val, "olcDatabase",
6964                                 STRLENOF( "olcDatabase" ))) {
6965                         if ( strncmp( e->e_nname.bv_val +
6966                                         STRLENOF( "olcDatabase" ), "={0}config",
6967                                         STRLENOF( "={0}config" )) &&
6968                                         strncmp( e->e_nname.bv_val +
6969                                         STRLENOF( "olcDatabase" ), "=config",
6970                                         STRLENOF( "=config" )) ) {
6971                                 vals[1].bv_len = 0;
6972                                 vals[1].bv_val = NULL;
6973                                 memset( &ca, 0, sizeof(ConfigArgs));
6974                                 ca.be = LDAP_STAILQ_FIRST( &backendDB );
6975                                 ca.bi = ca.be->bd_info;
6976                                 rdn.bv_val = ca.log;
6977                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( ca.log ),
6978                                         "%s=" SLAP_X_ORDERED_FMT "%s",
6979                                         cfAd_database->ad_cname.bv_val, 0,
6980                                         ca.bi->bi_type);
6981                                 ce = config_build_entry( NULL, NULL, cfb->cb_root, &ca, &rdn, &CFOC_DATABASE,
6982                                                 ca.be->be_cf_ocs );
6983                                 if ( ! op ) {
6984                                         thrctx = ldap_pvt_thread_pool_context();
6985                                         connection_fake_init2( &conn, &opbuf, thrctx,0 );
6986                                         op = &opbuf.ob_op;
6987                                         op->o_bd = &cfb->cb_db;
6988                                         op->o_tag = LDAP_REQ_ADD;
6989                                         op->o_dn = be->be_rootdn;
6990                                         op->o_ndn = be->be_rootndn;
6991                                 }
6992                                 op->ora_e = ce;
6993                                 rc = slap_add_opattrs(op, NULL, NULL, 0, 0);
6994                                 if ( rc != LDAP_SUCCESS ) {
6995                                         text->bv_val = "autocreation of \"olcDatabase={0}config\" failed";
6996                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={0}config\" failed");
6997                                         return NOID;
6998                                 }
6999                                 if (ce && bi && bi->bi_tool_entry_put &&
7000                                                 bi->bi_tool_entry_put( &cfb->cb_db, ce, text ) != NOID ) {
7001                                         entry_put_got_config++;
7002                                 } else {
7003                                         text->bv_val = "autocreation of \"olcDatabase={0}config\" failed";
7004                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={0}config\" failed");
7005                                         return NOID;
7006                                 }
7007                         } else {
7008                                 entry_put_got_config++;
7009                         }
7010                 }
7011         }
7012         if ( bi && bi->bi_tool_entry_put &&
7013                 config_add_internal( cfb, e, &ca, NULL, NULL, NULL ) == 0 )
7014                 return bi->bi_tool_entry_put( &cfb->cb_db, e, text );
7015         else
7016                 return NOID;
7017 }
7018
7019 static struct {
7020         char *name;
7021         AttributeDescription **desc;
7022 } ads[] = {
7023         { "attribute", &cfAd_attr },
7024         { "backend", &cfAd_backend },
7025         { "database", &cfAd_database },
7026         { "include", &cfAd_include },
7027         { "ldapsyntax", &cfAd_syntax },
7028         { "objectclass", &cfAd_oc },
7029         { "objectidentifier", &cfAd_om },
7030         { "overlay", &cfAd_overlay },
7031         { NULL, NULL }
7032 };
7033
7034 /* Notes:
7035  *   add / delete: all types that may be added or deleted must use an
7036  * X-ORDERED attributeType for their RDN. Adding and deleting entries
7037  * should automatically renumber the index of any siblings as needed,
7038  * so that no gaps in the numbering sequence exist after the add/delete
7039  * is completed.
7040  *   What can be added:
7041  *     schema objects
7042  *     backend objects for backend-specific config directives
7043  *     database objects
7044  *     overlay objects
7045  *
7046  *   delete: probably no support this time around.
7047  *
7048  *   modrdn: generally not done. Will be invoked automatically by add/
7049  * delete to update numbering sequence. Perform as an explicit operation
7050  * so that the renumbering effect may be replicated. Subtree rename must
7051  * be supported, since renumbering a database will affect all its child
7052  * overlays.
7053  *
7054  *  modify: must be fully supported. 
7055  */
7056
7057 int
7058 config_back_initialize( BackendInfo *bi )
7059 {
7060         ConfigTable             *ct = config_back_cf_table;
7061         ConfigArgs ca;
7062         char                    *argv[4];
7063         int                     i;
7064         AttributeDescription    *ad = NULL;
7065         const char              *text;
7066         static char             *controls[] = {
7067                 LDAP_CONTROL_MANAGEDSAIT,
7068                 NULL
7069         };
7070
7071         /* Make sure we don't exceed the bits reserved for userland */
7072         config_check_userland( CFG_LAST );
7073
7074         bi->bi_controls = controls;
7075
7076         bi->bi_open = 0;
7077         bi->bi_close = 0;
7078         bi->bi_config = 0;
7079         bi->bi_destroy = config_back_destroy;
7080
7081         bi->bi_db_init = config_back_db_init;
7082         bi->bi_db_config = 0;
7083         bi->bi_db_open = config_back_db_open;
7084         bi->bi_db_close = config_back_db_close;
7085         bi->bi_db_destroy = config_back_db_destroy;
7086
7087         bi->bi_op_bind = config_back_bind;
7088         bi->bi_op_unbind = 0;
7089         bi->bi_op_search = config_back_search;
7090         bi->bi_op_compare = 0;
7091         bi->bi_op_modify = config_back_modify;
7092         bi->bi_op_modrdn = config_back_modrdn;
7093         bi->bi_op_add = config_back_add;
7094         bi->bi_op_delete = config_back_delete;
7095         bi->bi_op_abandon = 0;
7096
7097         bi->bi_extended = 0;
7098
7099         bi->bi_chk_referrals = 0;
7100
7101         bi->bi_access_allowed = slap_access_allowed;
7102
7103         bi->bi_connection_init = 0;
7104         bi->bi_connection_destroy = 0;
7105
7106         bi->bi_entry_release_rw = config_entry_release;
7107         bi->bi_entry_get_rw = config_back_entry_get;
7108
7109         bi->bi_tool_entry_open = config_tool_entry_open;
7110         bi->bi_tool_entry_close = config_tool_entry_close;
7111         bi->bi_tool_entry_first = config_tool_entry_first;
7112         bi->bi_tool_entry_first_x = config_tool_entry_first_x;
7113         bi->bi_tool_entry_next = config_tool_entry_next;
7114         bi->bi_tool_entry_get = config_tool_entry_get;
7115         bi->bi_tool_entry_put = config_tool_entry_put;
7116
7117         ca.argv = argv;
7118         argv[ 0 ] = "slapd";
7119         ca.argv = argv;
7120         ca.argc = 3;
7121         ca.fname = argv[0];
7122
7123         argv[3] = NULL;
7124         for (i=0; OidMacros[i].name; i++ ) {
7125                 argv[1] = OidMacros[i].name;
7126                 argv[2] = OidMacros[i].oid;
7127                 parse_oidm( &ca, 0, NULL );
7128         }
7129
7130         bi->bi_cf_ocs = cf_ocs;
7131
7132         i = config_register_schema( ct, cf_ocs );
7133         if ( i ) return i;
7134
7135         i = slap_str2ad( "olcDatabase", &olcDatabaseDummy[0].ad, &text );
7136         if ( i ) return i;
7137
7138         /* setup olcRootPW to be base64-encoded when written in LDIF form;
7139          * basically, we don't care if it fails */
7140         i = slap_str2ad( "olcRootPW", &ad, &text );
7141         if ( i ) {
7142                 Debug( LDAP_DEBUG_ANY, "config_back_initialize: "
7143                         "warning, unable to get \"olcRootPW\" "
7144                         "attribute description: %d: %s\n",
7145                         i, text, 0 );
7146         } else {
7147                 (void)ldif_must_b64_encode_register( ad->ad_cname.bv_val,
7148                         ad->ad_type->sat_oid );
7149         }
7150
7151         /* set up the notable AttributeDescriptions */
7152         i = 0;
7153         for (;ct->name;ct++) {
7154                 if (strcmp(ct->name, ads[i].name)) continue;
7155                 *ads[i].desc = ct->ad;
7156                 i++;
7157                 if (!ads[i].name) break;
7158         }
7159
7160         return 0;
7161 }
7162