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