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