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