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