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