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