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