]> git.sur5r.net Git - openldap/blob - servers/slapd/bconfig.c
ITS#5572 cleanup unneeded code
[openldap] / servers / slapd / bconfig.c
1 /* bconfig.c - the config backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2005-2008 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was originally developed by Howard Chu for inclusion
18  * in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <ac/string.h>
25 #include <ac/ctype.h>
26 #include <ac/errno.h>
27 #include <sys/stat.h>
28
29 #include "slap.h"
30
31 #ifdef LDAP_SLAPI
32 #include "slapi/slapi.h"
33 #endif
34
35 #include <ldif.h>
36 #include <lutil.h>
37
38 #include "config.h"
39
40 #define CONFIG_RDN      "cn=config"
41 #define SCHEMA_RDN      "cn=schema"
42
43 static struct berval config_rdn = BER_BVC(CONFIG_RDN);
44 static struct berval schema_rdn = BER_BVC(SCHEMA_RDN);
45
46 extern int slap_DN_strict;      /* dn.c */
47
48 #ifdef SLAPD_MODULES
49 typedef struct modpath_s {
50         struct modpath_s *mp_next;
51         struct berval mp_path;
52         BerVarray mp_loads;
53 } ModPaths;
54
55 static ModPaths modpaths, *modlast = &modpaths, *modcur = &modpaths;
56 #endif
57
58 typedef struct ConfigFile {
59         struct ConfigFile *c_sibs;
60         struct ConfigFile *c_kids;
61         struct berval c_file;
62         AttributeType *c_at_head, *c_at_tail;
63         ContentRule *c_cr_head, *c_cr_tail;
64         ObjectClass *c_oc_head, *c_oc_tail;
65         OidMacro *c_om_head, *c_om_tail;
66         Syntax *c_syn_head, *c_syn_tail;
67         BerVarray c_dseFiles;
68 } ConfigFile;
69
70 typedef struct {
71         ConfigFile *cb_config;
72         CfEntryInfo *cb_root;
73         BackendDB       cb_db;  /* underlying database */
74         int             cb_got_ldif;
75         int             cb_use_ldif;
76 } CfBackInfo;
77
78 static CfBackInfo cfBackInfo;
79
80 static char     *passwd_salt;
81 static FILE *logfile;
82 static char     *logfileName;
83 #ifdef SLAP_AUTH_REWRITE
84 static BerVarray authz_rewrites;
85 #endif
86
87 static struct berval cfdir;
88
89 /* Private state */
90 static AttributeDescription *cfAd_backend, *cfAd_database, *cfAd_overlay,
91         *cfAd_include, *cfAd_attr, *cfAd_oc, *cfAd_om, *cfAd_syntax;
92
93 static ConfigFile *cfn;
94
95 static Avlnode *CfOcTree;
96
97 /* System schema state */
98 extern AttributeType *at_sys_tail;      /* at.c */
99 extern ObjectClass *oc_sys_tail;        /* oc.c */
100 extern OidMacro *om_sys_tail;   /* oidm.c */
101 extern Syntax *syn_sys_tail;    /* syntax.c */
102 static AttributeType *cf_at_tail;
103 static ObjectClass *cf_oc_tail;
104 static OidMacro *cf_om_tail;
105 static Syntax *cf_syn_tail;
106
107 static int config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca,
108         SlapReply *rs, int *renumber, Operation *op );
109
110 static int config_check_schema( Operation *op, CfBackInfo *cfb );
111
112 static ConfigDriver config_fname;
113 static ConfigDriver config_cfdir;
114 static ConfigDriver config_generic;
115 static ConfigDriver config_search_base;
116 static ConfigDriver config_passwd_hash;
117 static ConfigDriver config_schema_dn;
118 static ConfigDriver config_sizelimit;
119 static ConfigDriver config_timelimit;
120 static ConfigDriver config_overlay;
121 static ConfigDriver config_subordinate; 
122 static ConfigDriver config_suffix; 
123 static ConfigDriver config_rootdn;
124 static ConfigDriver config_rootpw;
125 static ConfigDriver config_restrict;
126 static ConfigDriver config_allows;
127 static ConfigDriver config_disallows;
128 static ConfigDriver config_requires;
129 static ConfigDriver config_security;
130 static ConfigDriver config_referral;
131 static ConfigDriver config_loglevel;
132 static ConfigDriver config_updatedn;
133 static ConfigDriver config_updateref;
134 static ConfigDriver config_include;
135 static ConfigDriver config_obsolete;
136 #ifdef HAVE_TLS
137 static ConfigDriver config_tls_option;
138 static ConfigDriver config_tls_config;
139 #endif
140 extern ConfigDriver syncrepl_config;
141
142 enum {
143         CFG_ACL = 1,
144         CFG_BACKEND,
145         CFG_DATABASE,
146         CFG_TLS_RAND,
147         CFG_TLS_CIPHER,
148         CFG_TLS_CERT_FILE,
149         CFG_TLS_CERT_KEY,
150         CFG_TLS_CA_PATH,
151         CFG_TLS_CA_FILE,
152         CFG_TLS_DH_FILE,
153         CFG_TLS_VERIFY,
154         CFG_TLS_CRLCHECK,
155         CFG_TLS_CRL_FILE,
156         CFG_CONCUR,
157         CFG_THREADS,
158         CFG_SALT,
159         CFG_LIMITS,
160         CFG_RO,
161         CFG_REWRITE,
162         CFG_DEPTH,
163         CFG_OID,
164         CFG_OC,
165         CFG_DIT,
166         CFG_ATTR,
167         CFG_ATOPT,
168         CFG_ROOTDSE,
169         CFG_LOGFILE,
170         CFG_PLUGIN,
171         CFG_MODLOAD,
172         CFG_MODPATH,
173         CFG_LASTMOD,
174         CFG_AZPOLICY,
175         CFG_AZREGEXP,
176         CFG_SASLSECP,
177         CFG_SSTR_IF_MAX,
178         CFG_SSTR_IF_MIN,
179         CFG_TTHREADS,
180         CFG_MIRRORMODE,
181         CFG_HIDDEN,
182         CFG_MONITORING,
183         CFG_SERVERID,
184         CFG_SORTVALS,
185         CFG_IX_INTLEN,
186         CFG_SYNTAX,
187         CFG_ACL_ADD,
188
189         CFG_LAST
190 };
191
192 typedef struct {
193         char *name, *oid;
194 } OidRec;
195
196 static OidRec OidMacros[] = {
197         /* OpenLDAProot:1.12.2 */
198         { "OLcfg", "1.3.6.1.4.1.4203.1.12.2" },
199         { "OLcfgAt", "OLcfg:3" },
200         { "OLcfgGlAt", "OLcfgAt:0" },
201         { "OLcfgBkAt", "OLcfgAt:1" },
202         { "OLcfgDbAt", "OLcfgAt:2" },
203         { "OLcfgOvAt", "OLcfgAt:3" },
204         { "OLcfgCtAt", "OLcfgAt:4" },   /* contrib modules */
205         { "OLcfgOc", "OLcfg:4" },
206         { "OLcfgGlOc", "OLcfgOc:0" },
207         { "OLcfgBkOc", "OLcfgOc:1" },
208         { "OLcfgDbOc", "OLcfgOc:2" },
209         { "OLcfgOvOc", "OLcfgOc:3" },
210         { "OLcfgCtOc", "OLcfgOc:4" },   /* contrib modules */
211
212         /* Syntaxes. We should just start using the standard names and
213          * document that they are predefined and available for users
214          * to reference in their own schema. Defining schema without
215          * OID macros is for masochists...
216          */
217         { "OMsyn", "1.3.6.1.4.1.1466.115.121.1" },
218         { "OMsBoolean", "OMsyn:7" },
219         { "OMsDN", "OMsyn:12" },
220         { "OMsDirectoryString", "OMsyn:15" },
221         { "OMsIA5String", "OMsyn:26" },
222         { "OMsInteger", "OMsyn:27" },
223         { "OMsOID", "OMsyn:38" },
224         { "OMsOctetString", "OMsyn:40" },
225         { NULL, NULL }
226 };
227
228 /*
229  * Backend/Database registry
230  *
231  * OLcfg{Bk|Db}{Oc|At}:0                -> common
232  * OLcfg{Bk|Db}{Oc|At}:1                -> back-bdb(/back-hdb)
233  * OLcfg{Bk|Db}{Oc|At}:2                -> back-ldif
234  * OLcfg{Bk|Db}{Oc|At}:3                -> back-ldap
235  * OLcfg{Bk|Db}{Oc|At}:4                -> back-monitor
236  * OLcfg{Bk|Db}{Oc|At}:5                -> back-relay
237  * OLcfg{Bk|Db}{Oc|At}:6                -> back-sql
238  * OLcfg{Bk|Db}{Oc|At}:7                -> back-sock
239  */
240
241 /*
242  * Overlay registry
243  *
244  * OLcfgOv{Oc|At}:1                     -> syncprov
245  * OLcfgOv{Oc|At}:2                     -> pcache
246  * OLcfgOv{Oc|At}:3                     -> chain
247  * OLcfgOv{Oc|At}:4                     -> accesslog
248  * OLcfgOv{Oc|At}:5                     -> valsort
249  * OLcfgOv{Oc|At}:7                     -> distproc
250  * OLcfgOv{Oc|At}:8                     -> dynlist
251  * OLcfgOv{Oc|At}:9                     -> dds
252  * OLcfgOv{Oc|At}:10                    -> unique
253  * OLcfgOv{Oc|At}:11                    -> refint
254  * OLcfgOv{Oc|At}:12                    -> ppolicy
255  * OLcfgOv{Oc|At}:13                    -> constraint
256  * OLcfgOv{Oc|At}:14                    -> translucent
257  * OLcfgOv{Oc|At}:15                    -> auditlog
258  * OLcfgOv{Oc|At}:16                    -> rwm
259  * OLcfgOv{Oc|At}:17                    -> dyngroup
260  * OLcfgOv{Oc|At}:18                    -> memberof
261  * OLcfgOv{Oc|At}:19                    -> collect
262  * OLcfgOv{Oc|At}:20                    -> retcode
263  */
264
265 /* alphabetical ordering */
266
267 static ConfigTable config_back_cf_table[] = {
268         /* This attr is read-only */
269         { "", "", 0, 0, 0, ARG_MAGIC,
270                 &config_fname, "( OLcfgGlAt:78 NAME 'olcConfigFile' "
271                         "DESC 'File for slapd configuration directives' "
272                         "EQUALITY caseIgnoreMatch "
273                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
274         { "", "", 0, 0, 0, ARG_MAGIC,
275                 &config_cfdir, "( OLcfgGlAt:79 NAME 'olcConfigDir' "
276                         "DESC 'Directory for slapd configuration backend' "
277                         "EQUALITY caseIgnoreMatch "
278                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
279         { "access",     NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_ACL,
280                 &config_generic, "( OLcfgGlAt:1 NAME 'olcAccess' "
281                         "DESC 'Access Control List' "
282                         "EQUALITY caseIgnoreMatch "
283                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
284         { "add_content_acl",    NULL, 0, 0, 0, ARG_MAY_DB|ARG_ON_OFF|ARG_MAGIC|CFG_ACL_ADD,
285                 &config_generic, "( OLcfgGlAt:86 NAME 'olcAddContentAcl' "
286                         "DESC 'Check ACLs against content of Add ops' "
287                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
288         { "allows",     "features", 2, 0, 5, ARG_PRE_DB|ARG_MAGIC,
289                 &config_allows, "( OLcfgGlAt:2 NAME 'olcAllows' "
290                         "DESC 'Allowed set of deprecated features' "
291                         "EQUALITY caseIgnoreMatch "
292                         "SYNTAX OMsDirectoryString )", NULL, NULL },
293         { "argsfile", "file", 2, 2, 0, ARG_STRING,
294                 &slapd_args_file, "( OLcfgGlAt:3 NAME 'olcArgsFile' "
295                         "DESC 'File for slapd command line options' "
296                         "EQUALITY caseIgnoreMatch "
297                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
298         { "attributeoptions", NULL, 0, 0, 0, ARG_MAGIC|CFG_ATOPT,
299                 &config_generic, "( OLcfgGlAt:5 NAME 'olcAttributeOptions' "
300                         "EQUALITY caseIgnoreMatch "
301                         "SYNTAX OMsDirectoryString )", NULL, NULL },
302         { "attribute",  "attribute", 2, 0, STRLENOF( "attribute" ),
303                 ARG_PAREN|ARG_MAGIC|CFG_ATTR,
304                 &config_generic, "( OLcfgGlAt:4 NAME 'olcAttributeTypes' "
305                         "DESC 'OpenLDAP attributeTypes' "
306                         "EQUALITY caseIgnoreMatch "
307                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
308                                 NULL, NULL },
309         { "authid-rewrite", NULL, 2, 0, STRLENOF( "authid-rewrite" ),
310 #ifdef SLAP_AUTH_REWRITE
311                 ARG_MAGIC|CFG_REWRITE|ARG_NO_INSERT, &config_generic,
312 #else
313                 ARG_IGNORED, NULL,
314 #endif
315                  "( OLcfgGlAt:6 NAME 'olcAuthIDRewrite' "
316                         "EQUALITY caseIgnoreMatch "
317                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
318         { "authz-policy", "policy", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_AZPOLICY,
319                 &config_generic, "( OLcfgGlAt:7 NAME 'olcAuthzPolicy' "
320                         "EQUALITY caseIgnoreMatch "
321                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
322         { "authz-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP|ARG_NO_INSERT,
323                 &config_generic, "( OLcfgGlAt:8 NAME 'olcAuthzRegexp' "
324                         "EQUALITY caseIgnoreMatch "
325                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
326         { "backend", "type", 2, 2, 0, ARG_PRE_DB|ARG_MAGIC|CFG_BACKEND,
327                 &config_generic, "( OLcfgGlAt:9 NAME 'olcBackend' "
328                         "DESC 'A type of backend' "
329                         "EQUALITY caseIgnoreMatch "
330                         "SYNTAX OMsDirectoryString SINGLE-VALUE X-ORDERED 'SIBLINGS' )",
331                                 NULL, NULL },
332         { "concurrency", "level", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_CONCUR,
333                 &config_generic, "( OLcfgGlAt:10 NAME 'olcConcurrency' "
334                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
335         { "conn_max_pending", "max", 2, 2, 0, ARG_INT,
336                 &slap_conn_max_pending, "( OLcfgGlAt:11 NAME 'olcConnMaxPending' "
337                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
338         { "conn_max_pending_auth", "max", 2, 2, 0, ARG_INT,
339                 &slap_conn_max_pending_auth, "( OLcfgGlAt:12 NAME 'olcConnMaxPendingAuth' "
340                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
341         { "database", "type", 2, 2, 0, ARG_MAGIC|CFG_DATABASE,
342                 &config_generic, "( OLcfgGlAt:13 NAME 'olcDatabase' "
343                         "DESC 'The backend type for a database instance' "
344                         "SUP olcBackend SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
345         { "defaultSearchBase", "dn", 2, 2, 0, ARG_PRE_BI|ARG_PRE_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
346                 &config_search_base, "( OLcfgGlAt:14 NAME 'olcDefaultSearchBase' "
347                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
348         { "disallows", "features", 2, 0, 8, ARG_PRE_DB|ARG_MAGIC,
349                 &config_disallows, "( OLcfgGlAt:15 NAME 'olcDisallows' "
350                         "EQUALITY caseIgnoreMatch "
351                         "SYNTAX OMsDirectoryString )", NULL, NULL },
352         { "ditcontentrule",     NULL, 0, 0, 0, ARG_MAGIC|CFG_DIT|ARG_NO_DELETE|ARG_NO_INSERT,
353                 &config_generic, "( OLcfgGlAt:16 NAME 'olcDitContentRules' "
354                         "DESC 'OpenLDAP DIT content rules' "
355                         "EQUALITY caseIgnoreMatch "
356                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
357                         NULL, NULL },
358         { "gentlehup", "on|off", 2, 2, 0,
359 #ifdef SIGHUP
360                 ARG_ON_OFF, &global_gentlehup,
361 #else
362                 ARG_IGNORED, NULL,
363 #endif
364                 "( OLcfgGlAt:17 NAME 'olcGentleHUP' "
365                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
366         { "hidden", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_HIDDEN,
367                 &config_generic, "( OLcfgDbAt:0.17 NAME 'olcHidden' "
368                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
369         { "idletimeout", "timeout", 2, 2, 0, ARG_INT,
370                 &global_idletimeout, "( OLcfgGlAt:18 NAME 'olcIdleTimeout' "
371                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
372         { "include", "file", 2, 2, 0, ARG_MAGIC,
373                 &config_include, "( OLcfgGlAt:19 NAME 'olcInclude' "
374                         "SUP labeledURI )", NULL, NULL },
375         { "index_substr_if_minlen", "min", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MIN,
376                 &config_generic, "( OLcfgGlAt:20 NAME 'olcIndexSubstrIfMinLen' "
377                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
378         { "index_substr_if_maxlen", "max", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MAX,
379                 &config_generic, "( OLcfgGlAt:21 NAME 'olcIndexSubstrIfMaxLen' "
380                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
381         { "index_substr_any_len", "len", 2, 2, 0, ARG_INT|ARG_NONZERO,
382                 &index_substr_any_len, "( OLcfgGlAt:22 NAME 'olcIndexSubstrAnyLen' "
383                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
384         { "index_substr_any_step", "step", 2, 2, 0, ARG_INT|ARG_NONZERO,
385                 &index_substr_any_step, "( OLcfgGlAt:23 NAME 'olcIndexSubstrAnyStep' "
386                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
387         { "index_intlen", "len", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_IX_INTLEN,
388                 &config_generic, "( OLcfgGlAt:84 NAME 'olcIndexIntLen' "
389                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
390         { "lastmod", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_LASTMOD,
391                 &config_generic, "( OLcfgDbAt:0.4 NAME 'olcLastMod' "
392                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
393         { "ldapsyntax", "syntax", 2, 0, 0,
394                 ARG_PAREN|ARG_MAGIC|CFG_SYNTAX,
395                 &config_generic, "( OLcfgGlAt:85 NAME 'olcLdapSyntaxes' "
396                         "DESC 'OpenLDAP ldapSyntax' "
397                         "EQUALITY caseIgnoreMatch "
398                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
399                                 NULL, NULL },
400         { "limits", "limits", 2, 0, 0, ARG_DB|ARG_MAGIC|CFG_LIMITS,
401                 &config_generic, "( OLcfgDbAt:0.5 NAME 'olcLimits' "
402                         "EQUALITY caseIgnoreMatch "
403                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
404         { "localSSF", "ssf", 2, 2, 0, ARG_INT,
405                 &local_ssf, "( OLcfgGlAt:26 NAME 'olcLocalSSF' "
406                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
407         { "logfile", "file", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_LOGFILE,
408                 &config_generic, "( OLcfgGlAt:27 NAME 'olcLogFile' "
409                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
410         { "loglevel", "level", 2, 0, 0, ARG_MAGIC,
411                 &config_loglevel, "( OLcfgGlAt:28 NAME 'olcLogLevel' "
412                         "EQUALITY caseIgnoreMatch "
413                         "SYNTAX OMsDirectoryString )", NULL, NULL },
414         { "maxDerefDepth", "depth", 2, 2, 0, ARG_DB|ARG_INT|ARG_MAGIC|CFG_DEPTH,
415                 &config_generic, "( OLcfgDbAt:0.6 NAME 'olcMaxDerefDepth' "
416                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
417         { "mirrormode", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_MIRRORMODE,
418                 &config_generic, "( OLcfgDbAt:0.16 NAME 'olcMirrorMode' "
419                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
420         { "moduleload", "file", 2, 0, 0,
421 #ifdef SLAPD_MODULES
422                 ARG_MAGIC|CFG_MODLOAD|ARG_NO_DELETE, &config_generic,
423 #else
424                 ARG_IGNORED, NULL,
425 #endif
426                 "( OLcfgGlAt:30 NAME 'olcModuleLoad' "
427                         "EQUALITY caseIgnoreMatch "
428                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
429         { "modulepath", "path", 2, 2, 0,
430 #ifdef SLAPD_MODULES
431                 ARG_MAGIC|CFG_MODPATH|ARG_NO_DELETE|ARG_NO_INSERT, &config_generic,
432 #else
433                 ARG_IGNORED, NULL,
434 #endif
435                 "( OLcfgGlAt:31 NAME 'olcModulePath' "
436                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
437         { "monitoring", "TRUE|FALSE", 2, 2, 0,
438                 ARG_MAGIC|CFG_MONITORING|ARG_DB|ARG_ON_OFF, &config_generic,
439                 "( OLcfgDbAt:0.18 NAME 'olcMonitoring' "
440                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
441         { "objectclass", "objectclass", 2, 0, 0, ARG_PAREN|ARG_MAGIC|CFG_OC,
442                 &config_generic, "( OLcfgGlAt:32 NAME 'olcObjectClasses' "
443                 "DESC 'OpenLDAP object classes' "
444                 "EQUALITY caseIgnoreMatch "
445                 "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
446                         NULL, NULL },
447         { "objectidentifier", "name> <oid",     3, 3, 0, ARG_MAGIC|CFG_OID,
448                 &config_generic, "( OLcfgGlAt:33 NAME 'olcObjectIdentifier' "
449                         "EQUALITY caseIgnoreMatch "
450                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
451         { "overlay", "overlay", 2, 2, 0, ARG_MAGIC,
452                 &config_overlay, "( OLcfgGlAt:34 NAME 'olcOverlay' "
453                         "SUP olcDatabase SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
454         { "password-crypt-salt-format", "salt", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_SALT,
455                 &config_generic, "( OLcfgGlAt:35 NAME 'olcPasswordCryptSaltFormat' "
456                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
457         { "password-hash", "hash", 2, 2, 0, ARG_MAGIC,
458                 &config_passwd_hash, "( OLcfgGlAt:36 NAME 'olcPasswordHash' "
459                         "EQUALITY caseIgnoreMatch "
460                         "SYNTAX OMsDirectoryString )", NULL, NULL },
461         { "pidfile", "file", 2, 2, 0, ARG_STRING,
462                 &slapd_pid_file, "( OLcfgGlAt:37 NAME 'olcPidFile' "
463                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
464         { "plugin", NULL, 0, 0, 0,
465 #ifdef LDAP_SLAPI
466                 ARG_MAGIC|CFG_PLUGIN, &config_generic,
467 #else
468                 ARG_IGNORED, NULL,
469 #endif
470                 "( OLcfgGlAt:38 NAME 'olcPlugin' "
471                         "EQUALITY caseIgnoreMatch "
472                         "SYNTAX OMsDirectoryString )", NULL, NULL },
473         { "pluginlog", "filename", 2, 2, 0,
474 #ifdef LDAP_SLAPI
475                 ARG_STRING, &slapi_log_file,
476 #else
477                 ARG_IGNORED, NULL,
478 #endif
479                 "( OLcfgGlAt:39 NAME 'olcPluginLogFile' "
480                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
481         { "readonly", "on|off", 2, 2, 0, ARG_MAY_DB|ARG_ON_OFF|ARG_MAGIC|CFG_RO,
482                 &config_generic, "( OLcfgGlAt:40 NAME 'olcReadOnly' "
483                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
484         { "referral", "url", 2, 2, 0, ARG_MAGIC,
485                 &config_referral, "( OLcfgGlAt:41 NAME 'olcReferral' "
486                         "SUP labeledURI SINGLE-VALUE )", NULL, NULL },
487         { "replica", "host or uri", 2, 0, 0, ARG_DB|ARG_MAGIC,
488                 &config_obsolete, "( OLcfgDbAt:0.7 NAME 'olcReplica' "
489                         "EQUALITY caseIgnoreMatch "
490                         "SUP labeledURI X-ORDERED 'VALUES' )", NULL, NULL },
491         { "replica-argsfile", NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC,
492                 &config_obsolete, "( OLcfgGlAt:43 NAME 'olcReplicaArgsFile' "
493                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
494         { "replica-pidfile", NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC,
495                 &config_obsolete, "( OLcfgGlAt:44 NAME 'olcReplicaPidFile' "
496                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
497         { "replicationInterval", NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC,
498                 &config_obsolete, "( OLcfgGlAt:45 NAME 'olcReplicationInterval' "
499                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
500         { "replogfile", "filename", 2, 2, 0, ARG_MAY_DB|ARG_MAGIC,
501                 &config_obsolete, "( OLcfgGlAt:46 NAME 'olcReplogFile' "
502                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
503         { "require", "features", 2, 0, 7, ARG_MAY_DB|ARG_MAGIC,
504                 &config_requires, "( OLcfgGlAt:47 NAME 'olcRequires' "
505                         "EQUALITY caseIgnoreMatch "
506                         "SYNTAX OMsDirectoryString )", NULL, NULL },
507         { "restrict", "op_list", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
508                 &config_restrict, "( OLcfgGlAt:48 NAME 'olcRestrict' "
509                         "EQUALITY caseIgnoreMatch "
510                         "SYNTAX OMsDirectoryString )", NULL, NULL },
511         { "reverse-lookup", "on|off", 2, 2, 0,
512 #ifdef SLAPD_RLOOKUPS
513                 ARG_ON_OFF, &use_reverse_lookup,
514 #else
515                 ARG_IGNORED, NULL,
516 #endif
517                 "( OLcfgGlAt:49 NAME 'olcReverseLookup' "
518                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
519         { "rootdn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
520                 &config_rootdn, "( OLcfgDbAt:0.8 NAME 'olcRootDN' "
521                         "EQUALITY distinguishedNameMatch "
522                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
523         { "rootDSE", "file", 2, 2, 0, ARG_MAGIC|CFG_ROOTDSE,
524                 &config_generic, "( OLcfgGlAt:51 NAME 'olcRootDSE' "
525                         "EQUALITY caseIgnoreMatch "
526                         "SYNTAX OMsDirectoryString )", NULL, NULL },
527         { "rootpw", "password", 2, 2, 0, ARG_BERVAL|ARG_DB|ARG_MAGIC,
528                 &config_rootpw, "( OLcfgDbAt:0.9 NAME 'olcRootPW' "
529                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
530         { "sasl-authz-policy", NULL, 2, 2, 0, ARG_MAGIC|CFG_AZPOLICY,
531                 &config_generic, NULL, NULL, NULL },
532         { "sasl-host", "host", 2, 2, 0,
533 #ifdef HAVE_CYRUS_SASL
534                 ARG_STRING|ARG_UNIQUE, &sasl_host,
535 #else
536                 ARG_IGNORED, NULL,
537 #endif
538                 "( OLcfgGlAt:53 NAME 'olcSaslHost' "
539                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
540         { "sasl-realm", "realm", 2, 2, 0,
541 #ifdef HAVE_CYRUS_SASL
542                 ARG_STRING|ARG_UNIQUE, &global_realm,
543 #else
544                 ARG_IGNORED, NULL,
545 #endif
546                 "( OLcfgGlAt:54 NAME 'olcSaslRealm' "
547                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
548         { "sasl-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
549                 &config_generic, NULL, NULL, NULL },
550         { "sasl-secprops", "properties", 2, 2, 0,
551 #ifdef HAVE_CYRUS_SASL
552                 ARG_MAGIC|CFG_SASLSECP, &config_generic,
553 #else
554                 ARG_IGNORED, NULL,
555 #endif
556                 "( OLcfgGlAt:56 NAME 'olcSaslSecProps' "
557                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
558         { "saslRegexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
559                 &config_generic, NULL, NULL, NULL },
560         { "schemadn", "dn", 2, 2, 0, ARG_MAY_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
561                 &config_schema_dn, "( OLcfgGlAt:58 NAME 'olcSchemaDN' "
562                         "EQUALITY distinguishedNameMatch "
563                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
564         { "security", "factors", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
565                 &config_security, "( OLcfgGlAt:59 NAME 'olcSecurity' "
566                         "EQUALITY caseIgnoreMatch "
567                         "SYNTAX OMsDirectoryString )", NULL, NULL },
568         { "serverID", "number> <[URI]", 2, 3, 0, ARG_MAGIC|CFG_SERVERID,
569                 &config_generic, "( OLcfgGlAt:81 NAME 'olcServerID' "
570                         "EQUALITY caseIgnoreMatch "
571                         "SYNTAX OMsDirectoryString )", NULL, NULL },
572         { "sizelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
573                 &config_sizelimit, "( OLcfgGlAt:60 NAME 'olcSizeLimit' "
574                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
575         { "sockbuf_max_incoming", "max", 2, 2, 0, ARG_BER_LEN_T,
576                 &sockbuf_max_incoming, "( OLcfgGlAt:61 NAME 'olcSockbufMaxIncoming' "
577                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
578         { "sockbuf_max_incoming_auth", "max", 2, 2, 0, ARG_BER_LEN_T,
579                 &sockbuf_max_incoming_auth, "( OLcfgGlAt:62 NAME 'olcSockbufMaxIncomingAuth' "
580                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
581         { "sortvals", "attr", 2, 0, 0, ARG_MAGIC|CFG_SORTVALS,
582                 &config_generic, "( OLcfgGlAt:83 NAME 'olcSortVals' "
583                         "DESC 'Attributes whose values will always be sorted' "
584                         "EQUALITY caseIgnoreMatch "
585                         "SYNTAX OMsDirectoryString )", NULL, NULL },
586         { "subordinate", "[advertise]", 1, 2, 0, ARG_DB|ARG_MAGIC,
587                 &config_subordinate, "( OLcfgDbAt:0.15 NAME 'olcSubordinate' "
588                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
589         { "suffix",     "suffix", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
590                 &config_suffix, "( OLcfgDbAt:0.10 NAME 'olcSuffix' "
591                         "EQUALITY distinguishedNameMatch "
592                         "SYNTAX OMsDN )", NULL, NULL },
593         { "syncrepl", NULL, 0, 0, 0, ARG_DB|ARG_MAGIC,
594                 &syncrepl_config, "( OLcfgDbAt:0.11 NAME 'olcSyncrepl' "
595                         "EQUALITY caseIgnoreMatch "
596                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
597         { "threads", "count", 2, 2, 0,
598 #ifdef NO_THREADS
599                 ARG_IGNORED, NULL,
600 #else
601                 ARG_INT|ARG_MAGIC|CFG_THREADS, &config_generic,
602 #endif
603                 "( OLcfgGlAt:66 NAME 'olcThreads' "
604                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
605         { "timelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
606                 &config_timelimit, "( OLcfgGlAt:67 NAME 'olcTimeLimit' "
607                         "SYNTAX OMsDirectoryString )", NULL, NULL },
608         { "TLSCACertificateFile", NULL, 0, 0, 0,
609 #ifdef HAVE_TLS
610                 CFG_TLS_CA_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
611 #else
612                 ARG_IGNORED, NULL,
613 #endif
614                 "( OLcfgGlAt:68 NAME 'olcTLSCACertificateFile' "
615                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
616         { "TLSCACertificatePath", NULL, 0, 0, 0,
617 #ifdef HAVE_TLS
618                 CFG_TLS_CA_PATH|ARG_STRING|ARG_MAGIC, &config_tls_option,
619 #else
620                 ARG_IGNORED, NULL,
621 #endif
622                 "( OLcfgGlAt:69 NAME 'olcTLSCACertificatePath' "
623                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
624         { "TLSCertificateFile", NULL, 0, 0, 0,
625 #ifdef HAVE_TLS
626                 CFG_TLS_CERT_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
627 #else
628                 ARG_IGNORED, NULL,
629 #endif
630                 "( OLcfgGlAt:70 NAME 'olcTLSCertificateFile' "
631                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
632         { "TLSCertificateKeyFile", NULL, 0, 0, 0,
633 #ifdef HAVE_TLS
634                 CFG_TLS_CERT_KEY|ARG_STRING|ARG_MAGIC, &config_tls_option,
635 #else
636                 ARG_IGNORED, NULL,
637 #endif
638                 "( OLcfgGlAt:71 NAME 'olcTLSCertificateKeyFile' "
639                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
640         { "TLSCipherSuite",     NULL, 0, 0, 0,
641 #ifdef HAVE_TLS
642                 CFG_TLS_CIPHER|ARG_STRING|ARG_MAGIC, &config_tls_option,
643 #else
644                 ARG_IGNORED, NULL,
645 #endif
646                 "( OLcfgGlAt:72 NAME 'olcTLSCipherSuite' "
647                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
648         { "TLSCRLCheck", NULL, 0, 0, 0,
649 #if defined(HAVE_TLS) && defined(HAVE_OPENSSL_CRL)
650                 CFG_TLS_CRLCHECK|ARG_STRING|ARG_MAGIC, &config_tls_config,
651 #else
652                 ARG_IGNORED, NULL,
653 #endif
654                 "( OLcfgGlAt:73 NAME 'olcTLSCRLCheck' "
655                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
656         { "TLSCRLFile", NULL, 0, 0, 0,
657 #if defined(HAVE_GNUTLS)
658                 CFG_TLS_CRL_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
659 #else
660                 ARG_IGNORED, NULL,
661 #endif
662                 "( OLcfgGlAt:82 NAME 'olcTLSCRLFile' "
663                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
664         { "TLSRandFile", NULL, 0, 0, 0,
665 #ifdef HAVE_TLS
666                 CFG_TLS_RAND|ARG_STRING|ARG_MAGIC, &config_tls_option,
667 #else
668                 ARG_IGNORED, NULL,
669 #endif
670                 "( OLcfgGlAt:74 NAME 'olcTLSRandFile' "
671                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
672         { "TLSVerifyClient", NULL, 0, 0, 0,
673 #ifdef HAVE_TLS
674                 CFG_TLS_VERIFY|ARG_STRING|ARG_MAGIC, &config_tls_config,
675 #else
676                 ARG_IGNORED, NULL,
677 #endif
678                 "( OLcfgGlAt:75 NAME 'olcTLSVerifyClient' "
679                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
680         { "TLSDHParamFile", NULL, 0, 0, 0,
681 #ifdef HAVE_TLS
682                 CFG_TLS_DH_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
683 #else
684                 ARG_IGNORED, NULL,
685 #endif
686                 "( OLcfgGlAt:77 NAME 'olcTLSDHParamFile' "
687                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
688         { "tool-threads", "count", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_TTHREADS,
689                 &config_generic, "( OLcfgGlAt:80 NAME 'olcToolThreads' "
690                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
691         { "ucdata-path", "path", 2, 2, 0, ARG_IGNORED,
692                 NULL, NULL, NULL, NULL },
693         { "updatedn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
694                 &config_updatedn, "( OLcfgDbAt:0.12 NAME 'olcUpdateDN' "
695                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
696         { "updateref", "url", 2, 2, 0, ARG_DB|ARG_MAGIC,
697                 &config_updateref, "( OLcfgDbAt:0.13 NAME 'olcUpdateRef' "
698                         "EQUALITY caseIgnoreMatch "
699                         "SUP labeledURI )", NULL, NULL },
700         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
701                 NULL, NULL, NULL, NULL }
702 };
703
704 /* Need to no-op this keyword for dynamic config */
705 ConfigTable olcDatabaseDummy[] = {
706         { "", "", 0, 0, 0, ARG_IGNORED,
707                 NULL, "( OLcfgGlAt:13 NAME 'olcDatabase' "
708                         "DESC 'The backend type for a database instance' "
709                         "SUP olcBackend SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
710         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
711 };
712
713 /* Routines to check if a child can be added to this type */
714 static ConfigLDAPadd cfAddSchema, cfAddInclude, cfAddDatabase,
715         cfAddBackend, cfAddModule, cfAddOverlay;
716
717 /* NOTE: be careful when defining array members
718  * that can be conditionally compiled */
719 #define CFOC_GLOBAL     cf_ocs[1]
720 #define CFOC_SCHEMA     cf_ocs[2]
721 #define CFOC_BACKEND    cf_ocs[3]
722 #define CFOC_DATABASE   cf_ocs[4]
723 #define CFOC_OVERLAY    cf_ocs[5]
724 #define CFOC_INCLUDE    cf_ocs[6]
725 #define CFOC_FRONTEND   cf_ocs[7]
726 #ifdef SLAPD_MODULES
727 #define CFOC_MODULE     cf_ocs[8]
728 #endif /* SLAPD_MODULES */
729
730 static ConfigOCs cf_ocs[] = {
731         { "( OLcfgGlOc:0 "
732                 "NAME 'olcConfig' "
733                 "DESC 'OpenLDAP configuration object' "
734                 "ABSTRACT SUP top )", Cft_Abstract, NULL },
735         { "( OLcfgGlOc:1 "
736                 "NAME 'olcGlobal' "
737                 "DESC 'OpenLDAP Global configuration options' "
738                 "SUP olcConfig STRUCTURAL "
739                 "MAY ( cn $ olcConfigFile $ olcConfigDir $ olcAllows $ olcArgsFile $ "
740                  "olcAttributeOptions $ olcAuthIDRewrite $ "
741                  "olcAuthzPolicy $ olcAuthzRegexp $ olcConcurrency $ "
742                  "olcConnMaxPending $ olcConnMaxPendingAuth $ "
743                  "olcDisallows $ olcGentleHUP $ olcIdleTimeout $ "
744                  "olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
745                  "olcIndexSubstrAnyLen $ olcIndexSubstrAnyStep $ olcIndexIntLen $ "
746                  "olcLocalSSF $ olcLogFile $ olcLogLevel $ "
747                  "olcPasswordCryptSaltFormat $ olcPasswordHash $ olcPidFile $ "
748                  "olcPluginLogFile $ olcReadOnly $ olcReferral $ "
749                  "olcReplogFile $ olcRequires $ olcRestrict $ olcReverseLookup $ "
750                  "olcRootDSE $ "
751                  "olcSaslHost $ olcSaslRealm $ olcSaslSecProps $ "
752                  "olcSecurity $ olcServerID $ olcSizeLimit $ "
753                  "olcSockbufMaxIncoming $ olcSockbufMaxIncomingAuth $ "
754                  "olcThreads $ olcTimeLimit $ olcTLSCACertificateFile $ "
755                  "olcTLSCACertificatePath $ olcTLSCertificateFile $ "
756                  "olcTLSCertificateKeyFile $ olcTLSCipherSuite $ olcTLSCRLCheck $ "
757                  "olcTLSRandFile $ olcTLSVerifyClient $ olcTLSDHParamFile $ "
758                  "olcTLSCRLFile $ olcToolThreads $ "
759                  "olcObjectIdentifier $ olcAttributeTypes $ olcObjectClasses $ "
760                  "olcDitContentRules $ olcLdapSyntaxes ) )", Cft_Global },
761         { "( OLcfgGlOc:2 "
762                 "NAME 'olcSchemaConfig' "
763                 "DESC 'OpenLDAP schema object' "
764                 "SUP olcConfig STRUCTURAL "
765                 "MAY ( cn $ olcObjectIdentifier $ olcAttributeTypes $ "
766                  "olcObjectClasses $ olcDitContentRules $ olcLdapSyntaxes ) )",
767                         Cft_Schema, NULL, cfAddSchema },
768         { "( OLcfgGlOc:3 "
769                 "NAME 'olcBackendConfig' "
770                 "DESC 'OpenLDAP Backend-specific options' "
771                 "SUP olcConfig STRUCTURAL "
772                 "MUST olcBackend )", Cft_Backend, NULL, cfAddBackend },
773         { "( OLcfgGlOc:4 "
774                 "NAME 'olcDatabaseConfig' "
775                 "DESC 'OpenLDAP Database-specific options' "
776                 "SUP olcConfig STRUCTURAL "
777                 "MUST olcDatabase "
778                 "MAY ( olcHidden $ olcSuffix $ olcSubordinate $ olcAccess $ "
779                  "olcAddContentAcl $ olcLastMod $ olcLimits $ "
780                  "olcMaxDerefDepth $ olcPlugin $ olcReadOnly $ olcReplica $ "
781                  "olcReplicaArgsFile $ olcReplicaPidFile $ olcReplicationInterval $ "
782                  "olcReplogFile $ olcRequires $ olcRestrict $ olcRootDN $ olcRootPW $ "
783                  "olcSchemaDN $ olcSecurity $ olcSizeLimit $ olcSyncrepl $ "
784                  "olcTimeLimit $ olcUpdateDN $ olcUpdateRef $ olcMirrorMode $ "
785                  "olcMonitoring ) )",
786                         Cft_Database, NULL, cfAddDatabase },
787         { "( OLcfgGlOc:5 "
788                 "NAME 'olcOverlayConfig' "
789                 "DESC 'OpenLDAP Overlay-specific options' "
790                 "SUP olcConfig STRUCTURAL "
791                 "MUST olcOverlay )", Cft_Overlay, NULL, cfAddOverlay },
792         { "( OLcfgGlOc:6 "
793                 "NAME 'olcIncludeFile' "
794                 "DESC 'OpenLDAP configuration include file' "
795                 "SUP olcConfig STRUCTURAL "
796                 "MUST olcInclude "
797                 "MAY ( cn $ olcRootDSE ) )",
798                 /* Used to be Cft_Include, that def has been removed */
799                 Cft_Abstract, NULL, cfAddInclude },
800         /* This should be STRUCTURAL like all the other database classes, but
801          * that would mean inheriting all of the olcDatabaseConfig attributes,
802          * which causes them to be merged twice in config_build_entry.
803          */
804         { "( OLcfgGlOc:7 "
805                 "NAME 'olcFrontendConfig' "
806                 "DESC 'OpenLDAP frontend configuration' "
807                 "AUXILIARY "
808                 "MAY ( olcDefaultSearchBase $ olcPasswordHash $ olcSortVals ) )",
809                 Cft_Database, NULL, NULL },
810 #ifdef SLAPD_MODULES
811         { "( OLcfgGlOc:8 "
812                 "NAME 'olcModuleList' "
813                 "DESC 'OpenLDAP dynamic module info' "
814                 "SUP olcConfig STRUCTURAL "
815                 "MAY ( cn $ olcModulePath $ olcModuleLoad ) )",
816                 Cft_Module, NULL, cfAddModule },
817 #endif
818         { NULL, 0, NULL }
819 };
820
821 typedef struct ServerID {
822         struct ServerID *si_next;
823         struct berval si_url;
824         int si_num;
825 } ServerID;
826
827 static ServerID *sid_list;
828
829 typedef struct voidList {
830         struct voidList *vl_next;
831         void *vl_ptr;
832 } voidList;
833
834 typedef struct ADlist {
835         struct ADlist *al_next;
836         AttributeDescription *al_desc;
837 } ADlist;
838
839 static ADlist *sortVals;
840
841 static int
842 config_generic(ConfigArgs *c) {
843         int i;
844
845         if ( c->op == SLAP_CONFIG_EMIT ) {
846                 int rc = 0;
847                 switch(c->type) {
848                 case CFG_CONCUR:
849                         c->value_int = ldap_pvt_thread_get_concurrency();
850                         break;
851                 case CFG_THREADS:
852                         c->value_int = connection_pool_max;
853                         break;
854                 case CFG_TTHREADS:
855                         c->value_int = slap_tool_thread_max;
856                         break;
857                 case CFG_SALT:
858                         if ( passwd_salt )
859                                 c->value_string = ch_strdup( passwd_salt );
860                         else
861                                 rc = 1;
862                         break;
863                 case CFG_LIMITS:
864                         if ( c->be->be_limits ) {
865                                 char buf[4096*3];
866                                 struct berval bv;
867
868                                 for ( i=0; c->be->be_limits[i]; i++ ) {
869                                         bv.bv_len = snprintf( buf, sizeof( buf ), SLAP_X_ORDERED_FMT, i );
870                                         if ( bv.bv_len >= sizeof( buf ) ) {
871                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
872                                                 c->rvalue_vals = NULL;
873                                                 rc = 1;
874                                                 break;
875                                         }
876                                         bv.bv_val = buf + bv.bv_len;
877                                         limits_unparse( c->be->be_limits[i], &bv,
878                                                         sizeof( buf ) - ( bv.bv_val - buf ) );
879                                         bv.bv_len += bv.bv_val - buf;
880                                         bv.bv_val = buf;
881                                         value_add_one( &c->rvalue_vals, &bv );
882                                 }
883                         }
884                         if ( !c->rvalue_vals ) rc = 1;
885                         break;
886                 case CFG_RO:
887                         c->value_int = (c->be->be_restrictops & SLAP_RESTRICT_OP_WRITES) ==
888                                 SLAP_RESTRICT_OP_WRITES;
889                         break;
890                 case CFG_AZPOLICY:
891                         c->value_string = ch_strdup( slap_sasl_getpolicy());
892                         break;
893                 case CFG_AZREGEXP:
894                         slap_sasl_regexp_unparse( &c->rvalue_vals );
895                         if ( !c->rvalue_vals ) rc = 1;
896                         break;
897 #ifdef HAVE_CYRUS_SASL
898                 case CFG_SASLSECP: {
899                         struct berval bv = BER_BVNULL;
900                         slap_sasl_secprops_unparse( &bv );
901                         if ( !BER_BVISNULL( &bv )) {
902                                 ber_bvarray_add( &c->rvalue_vals, &bv );
903                         } else {
904                                 rc = 1;
905                         }
906                         }
907                         break;
908 #endif
909                 case CFG_DEPTH:
910                         c->value_int = c->be->be_max_deref_depth;
911                         break;
912                 case CFG_HIDDEN:
913                         if ( SLAP_DBHIDDEN( c->be )) {
914                                 c->value_int = 1;
915                         } else {
916                                 rc = 1;
917                         }
918                         break;
919                 case CFG_OID: {
920                         ConfigFile *cf = c->ca_private;
921                         if ( !cf )
922                                 oidm_unparse( &c->rvalue_vals, NULL, NULL, 1 );
923                         else if ( cf->c_om_head )
924                                 oidm_unparse( &c->rvalue_vals, cf->c_om_head,
925                                         cf->c_om_tail, 0 );
926                         if ( !c->rvalue_vals )
927                                 rc = 1;
928                         }
929                         break;
930                 case CFG_ATOPT:
931                         ad_unparse_options( &c->rvalue_vals );
932                         break;
933                 case CFG_OC: {
934                         ConfigFile *cf = c->ca_private;
935                         if ( !cf )
936                                 oc_unparse( &c->rvalue_vals, NULL, NULL, 1 );
937                         else if ( cf->c_oc_head )
938                                 oc_unparse( &c->rvalue_vals, cf->c_oc_head,
939                                         cf->c_oc_tail, 0 );
940                         if ( !c->rvalue_vals )
941                                 rc = 1;
942                         }
943                         break;
944                 case CFG_ATTR: {
945                         ConfigFile *cf = c->ca_private;
946                         if ( !cf )
947                                 at_unparse( &c->rvalue_vals, NULL, NULL, 1 );
948                         else if ( cf->c_at_head )
949                                 at_unparse( &c->rvalue_vals, cf->c_at_head,
950                                         cf->c_at_tail, 0 );
951                         if ( !c->rvalue_vals )
952                                 rc = 1;
953                         }
954                         break;
955                 case CFG_SYNTAX: {
956                         ConfigFile *cf = c->ca_private;
957                         if ( !cf )
958                                 syn_unparse( &c->rvalue_vals, NULL, NULL, 1 );
959                         else if ( cf->c_syn_head )
960                                 syn_unparse( &c->rvalue_vals, cf->c_syn_head,
961                                         cf->c_syn_tail, 0 );
962                         if ( !c->rvalue_vals )
963                                 rc = 1;
964                         }
965                         break;
966                 case CFG_DIT: {
967                         ConfigFile *cf = c->ca_private;
968                         if ( !cf )
969                                 cr_unparse( &c->rvalue_vals, NULL, NULL, 1 );
970                         else if ( cf->c_cr_head )
971                                 cr_unparse( &c->rvalue_vals, cf->c_cr_head,
972                                         cf->c_cr_tail, 0 );
973                         if ( !c->rvalue_vals )
974                                 rc = 1;
975                         }
976                         break;
977                         
978                 case CFG_ACL: {
979                         AccessControl *a;
980                         char *src, *dst, ibuf[11];
981                         struct berval bv, abv;
982                         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                                                 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         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SINGLE_SHADOW | flag);
3073
3074         return 0;
3075 }
3076
3077 static int
3078 config_updateref(ConfigArgs *c) {
3079         struct berval val;
3080         if (c->op == SLAP_CONFIG_EMIT) {
3081                 if ( c->be->be_update_refs ) {
3082                         value_add( &c->rvalue_vals, c->be->be_update_refs );
3083                         return 0;
3084                 } else {
3085                         return 1;
3086                 }
3087         } else if ( c->op == LDAP_MOD_DELETE ) {
3088                 if ( c->valx < 0 ) {
3089                         ber_bvarray_free( c->be->be_update_refs );
3090                         c->be->be_update_refs = NULL;
3091                 } else {
3092                         int i = c->valx;
3093                         ch_free( c->be->be_update_refs[i].bv_val );
3094                         for (; c->be->be_update_refs[i].bv_val; i++)
3095                                 c->be->be_update_refs[i] = c->be->be_update_refs[i+1];
3096                 }
3097                 return 0;
3098         }
3099         if(!SLAP_SHADOW(c->be) && !c->be->be_syncinfo) {
3100                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must appear after syncrepl or updatedn",
3101                         c->argv[0] );
3102                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
3103                         c->log, c->cr_msg, 0);
3104                 return(1);
3105         }
3106
3107         if(validate_global_referral(c->argv[1])) {
3108                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid URL", c->argv[0] );
3109                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
3110                         c->log, c->cr_msg, c->argv[1]);
3111                 return(1);
3112         }
3113         ber_str2bv(c->argv[1], 0, 0, &val);
3114         if(value_add_one(&c->be->be_update_refs, &val)) return(LDAP_OTHER);
3115         return(0);
3116 }
3117
3118 static int
3119 config_obsolete(ConfigArgs *c) {
3120         if (c->op == SLAP_CONFIG_EMIT)
3121                 return 1;
3122
3123         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> keyword is obsolete (ignored)",
3124                 c->argv[0] );
3125         Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0);
3126         return(0);
3127 }
3128
3129 static int
3130 config_include(ConfigArgs *c) {
3131         int savelineno = c->lineno;
3132         int rc;
3133         ConfigFile *cf;
3134         ConfigFile *cfsave = cfn;
3135         ConfigFile *cf2 = NULL;
3136
3137         /* Leftover from RE23. No dynamic config for include files */
3138         if ( c->op == SLAP_CONFIG_EMIT || c->op == LDAP_MOD_DELETE )
3139                 return 1;
3140
3141         cf = ch_calloc( 1, sizeof(ConfigFile));
3142         if ( cfn->c_kids ) {
3143                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
3144                 cf2->c_sibs = cf;
3145         } else {
3146                 cfn->c_kids = cf;
3147         }
3148         cfn = cf;
3149         ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
3150         rc = read_config_file(c->argv[1], c->depth + 1, c, config_back_cf_table);
3151         c->lineno = savelineno - 1;
3152         cfn = cfsave;
3153         if ( rc ) {
3154                 if ( cf2 ) cf2->c_sibs = NULL;
3155                 else cfn->c_kids = NULL;
3156                 ch_free( cf->c_file.bv_val );
3157                 ch_free( cf );
3158         } else {
3159                 c->ca_private = cf;
3160         }
3161         return(rc);
3162 }
3163
3164 #ifdef HAVE_TLS
3165 static int
3166 config_tls_option(ConfigArgs *c) {
3167         int flag;
3168         LDAP *ld = slap_tls_ld;
3169         switch(c->type) {
3170         case CFG_TLS_RAND:      flag = LDAP_OPT_X_TLS_RANDOM_FILE;      ld = NULL; break;
3171         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
3172         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
3173         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
3174         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
3175         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
3176         case CFG_TLS_DH_FILE:   flag = LDAP_OPT_X_TLS_DHFILE;   break;
3177 #ifdef HAVE_GNUTLS
3178         case CFG_TLS_CRL_FILE:  flag = LDAP_OPT_X_TLS_CRLFILE;  break;
3179 #endif
3180         default:                Debug(LDAP_DEBUG_ANY, "%s: "
3181                                         "unknown tls_option <0x%x>\n",
3182                                         c->log, c->type, 0);
3183                 return 1;
3184         }
3185         if (c->op == SLAP_CONFIG_EMIT) {
3186                 return ldap_pvt_tls_get_option( ld, flag, &c->value_string );
3187         } else if ( c->op == LDAP_MOD_DELETE ) {
3188                 return ldap_pvt_tls_set_option( ld, flag, NULL );
3189         }
3190         ch_free(c->value_string);
3191         return(ldap_pvt_tls_set_option(ld, flag, c->argv[1]));
3192 }
3193
3194 /* FIXME: this ought to be provided by libldap */
3195 static int
3196 config_tls_config(ConfigArgs *c) {
3197         int i, flag;
3198         switch(c->type) {
3199         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK; break;
3200         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT; break;
3201         default:
3202                 Debug(LDAP_DEBUG_ANY, "%s: "
3203                                 "unknown tls_option <0x%x>\n",
3204                                 c->log, c->type, 0);
3205                 return 1;
3206         }
3207         if (c->op == SLAP_CONFIG_EMIT) {
3208                 return slap_tls_get_config( slap_tls_ld, flag, &c->value_string );
3209         } else if ( c->op == LDAP_MOD_DELETE ) {
3210                 int i = 0;
3211                 return ldap_pvt_tls_set_option( slap_tls_ld, flag, &i );
3212         }
3213         ch_free( c->value_string );
3214         if ( isdigit( (unsigned char)c->argv[1][0] ) ) {
3215                 if ( lutil_atoi( &i, c->argv[1] ) != 0 ) {
3216                         Debug(LDAP_DEBUG_ANY, "%s: "
3217                                 "unable to parse %s \"%s\"\n",
3218                                 c->log, c->argv[0], c->argv[1] );
3219                         return 1;
3220                 }
3221                 return(ldap_pvt_tls_set_option(slap_tls_ld, flag, &i));
3222         } else {
3223                 return(ldap_int_tls_config(slap_tls_ld, flag, c->argv[1]));
3224         }
3225 }
3226 #endif
3227
3228 static CfEntryInfo *
3229 config_find_base( CfEntryInfo *root, struct berval *dn, CfEntryInfo **last )
3230 {
3231         struct berval cdn;
3232         char *c;
3233
3234         if ( !root ) {
3235                 *last = NULL;
3236                 return NULL;
3237         }
3238
3239         if ( dn_match( &root->ce_entry->e_nname, dn ))
3240                 return root;
3241
3242         c = dn->bv_val+dn->bv_len;
3243         for (;*c != ',';c--);
3244
3245         while(root) {
3246                 *last = root;
3247                 for (--c;c>dn->bv_val && *c != ',';c--);
3248                 cdn.bv_val = c;
3249                 if ( *c == ',' )
3250                         cdn.bv_val++;
3251                 cdn.bv_len = dn->bv_len - (cdn.bv_val - dn->bv_val);
3252
3253                 root = root->ce_kids;
3254
3255                 for (;root;root=root->ce_sibs) {
3256                         if ( dn_match( &root->ce_entry->e_nname, &cdn )) {
3257                                 if ( cdn.bv_val == dn->bv_val ) {
3258                                         return root;
3259                                 }
3260                                 break;
3261                         }
3262                 }
3263         }
3264         return root;
3265 }
3266
3267 typedef struct setup_cookie {
3268         CfBackInfo *cfb;
3269         ConfigArgs *ca;
3270         Entry *frontend;
3271         Entry *config;
3272         int got_frontend;
3273         int got_config;
3274 } setup_cookie;
3275
3276 static int
3277 config_ldif_resp( Operation *op, SlapReply *rs )
3278 {
3279         if ( rs->sr_type == REP_SEARCH ) {
3280                 setup_cookie *sc = op->o_callback->sc_private;
3281                 struct berval pdn;
3282
3283                 sc->cfb->cb_got_ldif = 1;
3284                 /* Does the frontend exist? */
3285                 if ( !sc->got_frontend ) {
3286                         if ( !strncmp( rs->sr_entry->e_nname.bv_val,
3287                                 "olcDatabase", STRLENOF( "olcDatabase" )))
3288                         {
3289                                 if ( strncmp( rs->sr_entry->e_nname.bv_val +
3290                                         STRLENOF( "olcDatabase" ), "={-1}frontend",
3291                                         STRLENOF( "={-1}frontend" )))
3292                                 {
3293                                         struct berval rdn;
3294                                         int i = op->o_noop;
3295                                         sc->ca->be = frontendDB;
3296                                         sc->ca->bi = frontendDB->bd_info;
3297                                         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
3298                                         rdn.bv_val = sc->ca->log;
3299                                         rdn.bv_len = snprintf(rdn.bv_val, sizeof( sc->ca->log ),
3300                                                 "%s=" SLAP_X_ORDERED_FMT "%s",
3301                                                 cfAd_database->ad_cname.bv_val, -1,
3302                                                 sc->ca->bi->bi_type);
3303                                         op->o_noop = 1;
3304                                         sc->frontend = config_build_entry( op, rs,
3305                                                 sc->cfb->cb_root, sc->ca, &rdn, &CFOC_DATABASE,
3306                                                 sc->ca->be->be_cf_ocs );
3307                                         op->o_noop = i;
3308                                         sc->got_frontend++;
3309                                 } else {
3310                                         sc->got_frontend++;
3311                                         goto ok;
3312                                 }
3313                         }
3314                 }
3315
3316                 dnParent( &rs->sr_entry->e_nname, &pdn );
3317
3318                 /* Does the configDB exist? */
3319                 if ( sc->got_frontend && !sc->got_config &&
3320                         !strncmp( rs->sr_entry->e_nname.bv_val,
3321                         "olcDatabase", STRLENOF( "olcDatabase" )) &&
3322                         dn_match( &config_rdn, &pdn ) )
3323                 {
3324                         if ( strncmp( rs->sr_entry->e_nname.bv_val +
3325                                 STRLENOF( "olcDatabase" ), "={0}config",
3326                                 STRLENOF( "={0}config" )))
3327                         {
3328                                 struct berval rdn;
3329                                 int i = op->o_noop;
3330                                 sc->ca->be = LDAP_STAILQ_FIRST( &backendDB );
3331                                 sc->ca->bi = sc->ca->be->bd_info;
3332                                 rdn.bv_val = sc->ca->log;
3333                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( sc->ca->log ),
3334                                         "%s=" SLAP_X_ORDERED_FMT "%s",
3335                                         cfAd_database->ad_cname.bv_val, 0,
3336                                         sc->ca->bi->bi_type);
3337                                 op->o_noop = 1;
3338                                 sc->config = config_build_entry( op, rs, sc->cfb->cb_root,
3339                                         sc->ca, &rdn, &CFOC_DATABASE, sc->ca->be->be_cf_ocs );
3340                                 op->o_noop = i;
3341                         }
3342                         sc->got_config++;
3343                 }
3344
3345 ok:
3346                 rs->sr_err = config_add_internal( sc->cfb, rs->sr_entry, sc->ca, NULL, NULL, NULL );
3347                 if ( rs->sr_err != LDAP_SUCCESS ) {
3348                         Debug( LDAP_DEBUG_ANY, "config error processing %s: %s\n",
3349                                 rs->sr_entry->e_name.bv_val, sc->ca->cr_msg, 0 );
3350                 }
3351         }
3352         return rs->sr_err;
3353 }
3354
3355 /* Configure and read the underlying back-ldif store */
3356 static int
3357 config_setup_ldif( BackendDB *be, const char *dir, int readit ) {
3358         CfBackInfo *cfb = be->be_private;
3359         ConfigArgs c = {0};
3360         ConfigTable *ct;
3361         char *argv[3];
3362         int rc = 0;
3363         setup_cookie sc;
3364         slap_callback cb = { NULL, config_ldif_resp, NULL, NULL };
3365         Connection conn = {0};
3366         OperationBuffer opbuf;
3367         Operation *op;
3368         SlapReply rs = {REP_RESULT};
3369         Filter filter = { LDAP_FILTER_PRESENT };
3370         struct berval filterstr = BER_BVC("(objectclass=*)");
3371         struct stat st;
3372
3373         /* Is the config directory available? */
3374         if ( stat( dir, &st ) < 0 ) {
3375                 /* No, so don't bother using the backing store.
3376                  * All changes will be in-memory only.
3377                  */
3378                 return 0;
3379         }
3380                 
3381         cfb->cb_db.bd_info = backend_info( "ldif" );
3382         if ( !cfb->cb_db.bd_info )
3383                 return 0;       /* FIXME: eventually this will be a fatal error */
3384
3385         if ( backend_db_init( "ldif", &cfb->cb_db, -1, NULL ) == NULL )
3386                 return 1;
3387
3388         cfb->cb_db.be_suffix = be->be_suffix;
3389         cfb->cb_db.be_nsuffix = be->be_nsuffix;
3390
3391         /* The suffix is always "cn=config". The underlying DB's rootdn
3392          * is always the same as the suffix.
3393          */
3394         cfb->cb_db.be_rootdn = be->be_suffix[0];
3395         cfb->cb_db.be_rootndn = be->be_nsuffix[0];
3396
3397         ber_str2bv( dir, 0, 1, &cfdir );
3398
3399         c.be = &cfb->cb_db;
3400         c.fname = "slapd";
3401         c.argc = 2;
3402         argv[0] = "directory";
3403         argv[1] = (char *)dir;
3404         argv[2] = NULL;
3405         c.argv = argv;
3406         c.reply.err = 0;
3407         c.reply.msg[0] = 0;
3408         c.table = Cft_Database;
3409
3410         ct = config_find_keyword( c.be->be_cf_ocs->co_table, &c );
3411         if ( !ct )
3412                 return 1;
3413
3414         if ( config_add_vals( ct, &c ))
3415                 return 1;
3416
3417         if ( backend_startup_one( &cfb->cb_db, &c.reply ))
3418                 return 1;
3419
3420         if ( readit ) {
3421                 void *thrctx = ldap_pvt_thread_pool_context();
3422                 int prev_DN_strict;
3423
3424                 connection_fake_init( &conn, &opbuf, thrctx );
3425                 op = &opbuf.ob_op;
3426
3427                 filter.f_desc = slap_schema.si_ad_objectClass;
3428
3429                 op->o_tag = LDAP_REQ_SEARCH;
3430
3431                 op->ors_filter = &filter;
3432                 op->ors_filterstr = filterstr;
3433                 op->ors_scope = LDAP_SCOPE_SUBTREE;
3434
3435                 op->o_dn = c.be->be_rootdn;
3436                 op->o_ndn = c.be->be_rootndn;
3437
3438                 op->o_req_dn = be->be_suffix[0];
3439                 op->o_req_ndn = be->be_nsuffix[0];
3440
3441                 op->ors_tlimit = SLAP_NO_LIMIT;
3442                 op->ors_slimit = SLAP_NO_LIMIT;
3443
3444                 op->ors_attrs = slap_anlist_all_attributes;
3445                 op->ors_attrsonly = 0;
3446
3447                 op->o_callback = &cb;
3448                 sc.cfb = cfb;
3449                 sc.ca = &c;
3450                 cb.sc_private = &sc;
3451                 sc.got_frontend = 0;
3452                 sc.got_config = 0;
3453                 sc.frontend = NULL;
3454                 sc.config = NULL;
3455
3456                 op->o_bd = &cfb->cb_db;
3457                 
3458                 /* Allow unknown attrs in DNs */
3459                 prev_DN_strict = slap_DN_strict;
3460                 slap_DN_strict = 0;
3461
3462                 rc = op->o_bd->be_search( op, &rs );
3463
3464                 /* Restore normal DN validation */
3465                 slap_DN_strict = prev_DN_strict;
3466
3467                 op->o_tag = LDAP_REQ_ADD;
3468                 if ( rc == LDAP_SUCCESS && sc.frontend ) {
3469                         op->ora_e = sc.frontend;
3470                         rc = op->o_bd->be_add( op, &rs );
3471                 }
3472                 if ( rc == LDAP_SUCCESS && sc.config ) {
3473                         op->ora_e = sc.config;
3474                         rc = op->o_bd->be_add( op, &rs );
3475                 }
3476                 ldap_pvt_thread_pool_context_reset( thrctx );
3477         }
3478
3479         /* ITS#4194 - only use if it's present, or we're converting. */
3480         if ( !readit || rc == LDAP_SUCCESS )
3481                 cfb->cb_use_ldif = 1;
3482
3483         return rc;
3484 }
3485
3486 static int
3487 CfOc_cmp( const void *c1, const void *c2 ) {
3488         const ConfigOCs *co1 = c1;
3489         const ConfigOCs *co2 = c2;
3490
3491         return ber_bvcmp( co1->co_name, co2->co_name );
3492 }
3493
3494 int
3495 config_register_schema(ConfigTable *ct, ConfigOCs *ocs) {
3496         int i;
3497
3498         i = init_config_attrs( ct );
3499         if ( i ) return i;
3500
3501         /* set up the objectclasses */
3502         i = init_config_ocs( ocs );
3503         if ( i ) return i;
3504
3505         for (i=0; ocs[i].co_def; i++) {
3506                 if ( ocs[i].co_oc ) {
3507                         ocs[i].co_name = &ocs[i].co_oc->soc_cname;
3508                         if ( !ocs[i].co_table )
3509                                 ocs[i].co_table = ct;
3510                         avl_insert( &CfOcTree, &ocs[i], CfOc_cmp, avl_dup_error );
3511                 }
3512         }
3513         return 0;
3514 }
3515
3516 int
3517 read_config(const char *fname, const char *dir) {
3518         BackendDB *be;
3519         CfBackInfo *cfb;
3520         const char *cfdir, *cfname;
3521         int rc;
3522
3523         /* Setup the config backend */
3524         be = backend_db_init( "config", NULL, 0, NULL );
3525         if ( !be )
3526                 return 1;
3527
3528         cfb = be->be_private;
3529         be->be_dfltaccess = ACL_NONE;
3530
3531         /* If no .conf, or a dir was specified, setup the dir */
3532         if ( !fname || dir ) {
3533                 if ( dir ) {
3534                         /* If explicitly given, check for existence */
3535                         struct stat st;
3536
3537                         if ( stat( dir, &st ) < 0 ) {
3538                                 Debug( LDAP_DEBUG_ANY,
3539                                         "invalid config directory %s, error %d\n",
3540                                                 dir, errno, 0 );
3541                                 return 1;
3542                         }
3543                         cfdir = dir;
3544                 } else {
3545                         cfdir = SLAPD_DEFAULT_CONFIGDIR;
3546                 }
3547                 /* if fname is defaulted, try reading .d */
3548                 rc = config_setup_ldif( be, cfdir, !fname );
3549
3550                 if ( rc ) {
3551                         /* It may be OK if the base object doesn't exist yet. */
3552                         if ( rc != LDAP_NO_SUCH_OBJECT )
3553                                 return 1;
3554                         /* ITS#4194: But if dir was specified and no fname,
3555                          * then we were supposed to read the dir. Unless we're
3556                          * trying to slapadd the dir...
3557                          */
3558                         if ( dir && !fname ) {
3559                                 if ( slapMode & (SLAP_SERVER_MODE|SLAP_TOOL_READMAIN|SLAP_TOOL_READONLY))
3560                                         return 1;
3561                                 /* Assume it's slapadd with a config dir, let it continue */
3562                                 rc = 0;
3563                                 cfb->cb_got_ldif = 1;
3564                                 cfb->cb_use_ldif = 1;
3565                                 goto done;
3566                         }
3567                 }
3568
3569                 /* If we read the config from back-ldif, nothing to do here */
3570                 if ( cfb->cb_got_ldif ) {
3571                         rc = 0;
3572                         goto done;
3573                 }
3574         }
3575
3576         if ( fname )
3577                 cfname = fname;
3578         else
3579                 cfname = SLAPD_DEFAULT_CONFIGFILE;
3580
3581         rc = read_config_file(cfname, 0, NULL, config_back_cf_table);
3582
3583         if ( rc == 0 )
3584                 ber_str2bv( cfname, 0, 1, &cfb->cb_config->c_file );
3585
3586 done:
3587         if ( rc == 0 && BER_BVISNULL( &frontendDB->be_schemadn ) ) {
3588                 ber_str2bv( SLAPD_SCHEMA_DN, STRLENOF( SLAPD_SCHEMA_DN ), 1,
3589                         &frontendDB->be_schemadn );
3590                 rc = dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
3591                 if ( rc != LDAP_SUCCESS ) {
3592                         Debug(LDAP_DEBUG_ANY, "read_config: "
3593                                 "unable to normalize default schema DN \"%s\"\n",
3594                                 frontendDB->be_schemadn.bv_val, 0, 0 );
3595                         /* must not happen */
3596                         assert( 0 );
3597                 }
3598         }
3599         return rc;
3600 }
3601
3602 static int
3603 config_back_bind( Operation *op, SlapReply *rs )
3604 {
3605         if ( be_isroot_pw( op ) ) {
3606                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ));
3607                 /* frontend sends result */
3608                 return LDAP_SUCCESS;
3609         }
3610
3611         rs->sr_err = LDAP_INVALID_CREDENTIALS;
3612         send_ldap_result( op, rs );
3613
3614         return rs->sr_err;
3615 }
3616
3617 static int
3618 config_send( Operation *op, SlapReply *rs, CfEntryInfo *ce, int depth )
3619 {
3620         int rc = 0;
3621
3622         if ( test_filter( op, ce->ce_entry, op->ors_filter ) == LDAP_COMPARE_TRUE )
3623         {
3624                 rs->sr_attrs = op->ors_attrs;
3625                 rs->sr_entry = ce->ce_entry;
3626                 rs->sr_flags = 0;
3627                 rc = send_search_entry( op, rs );
3628         }
3629         if ( op->ors_scope == LDAP_SCOPE_SUBTREE ) {
3630                 if ( ce->ce_kids ) {
3631                         rc = config_send( op, rs, ce->ce_kids, 1 );
3632                         if ( rc ) return rc;
3633                 }
3634                 if ( depth ) {
3635                         for (ce=ce->ce_sibs; ce; ce=ce->ce_sibs) {
3636                                 rc = config_send( op, rs, ce, 0 );
3637                                 if ( rc ) break;
3638                         }
3639                 }
3640         }
3641         return rc;
3642 }
3643
3644 static ConfigTable *
3645 config_find_table( ConfigOCs **colst, int nocs, AttributeDescription *ad,
3646         ConfigArgs *ca )
3647 {
3648         int i, j;
3649
3650         for (j=0; j<nocs; j++) {
3651                 for (i=0; colst[j]->co_table[i].name; i++)
3652                         if ( colst[j]->co_table[i].ad == ad ) {
3653                                 ca->table = colst[j]->co_type;
3654                                 return &colst[j]->co_table[i];
3655                         }
3656         }
3657         return NULL;
3658 }
3659
3660 /* Sort the attributes of the entry according to the order defined
3661  * in the objectclass, with required attributes occurring before
3662  * allowed attributes. For any attributes with sequencing dependencies
3663  * (e.g., rootDN must be defined after suffix) the objectclass must
3664  * list the attributes in the desired sequence.
3665  */
3666 static void
3667 sort_attrs( Entry *e, ConfigOCs **colst, int nocs )
3668 {
3669         Attribute *a, *head = NULL, *tail = NULL, **prev;
3670         int i, j;
3671
3672         for (i=0; i<nocs; i++) {
3673                 if ( colst[i]->co_oc->soc_required ) {
3674                         AttributeType **at = colst[i]->co_oc->soc_required;
3675                         for (j=0; at[j]; j++) {
3676                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
3677                                         prev = &(*prev)->a_next, a=a->a_next) {
3678                                         if ( a->a_desc == at[j]->sat_ad ) {
3679                                                 *prev = a->a_next;
3680                                                 if (!head) {
3681                                                         head = a;
3682                                                         tail = a;
3683                                                 } else {
3684                                                         tail->a_next = a;
3685                                                         tail = a;
3686                                                 }
3687                                                 break;
3688                                         }
3689                                 }
3690                         }
3691                 }
3692                 if ( colst[i]->co_oc->soc_allowed ) {
3693                         AttributeType **at = colst[i]->co_oc->soc_allowed;
3694                         for (j=0; at[j]; j++) {
3695                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
3696                                         prev = &(*prev)->a_next, a=a->a_next) {
3697                                         if ( a->a_desc == at[j]->sat_ad ) {
3698                                                 *prev = a->a_next;
3699                                                 if (!head) {
3700                                                         head = a;
3701                                                         tail = a;
3702                                                 } else {
3703                                                         tail->a_next = a;
3704                                                         tail = a;
3705                                                 }
3706                                                 break;
3707                                         }
3708                                 }
3709                         }
3710                 }
3711         }
3712         if ( tail ) {
3713                 tail->a_next = e->e_attrs;
3714                 e->e_attrs = head;
3715         }
3716 }
3717
3718 static int
3719 check_vals( ConfigTable *ct, ConfigArgs *ca, void *ptr, int isAttr )
3720 {
3721         Attribute *a = NULL;
3722         AttributeDescription *ad;
3723         BerVarray vals;
3724
3725         int i, rc = 0;
3726
3727         if ( isAttr ) {
3728                 a = ptr;
3729                 ad = a->a_desc;
3730                 vals = a->a_vals;
3731         } else {
3732                 Modifications *ml = ptr;
3733                 ad = ml->sml_desc;
3734                 vals = ml->sml_values;
3735         }
3736
3737         if ( a && ( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL )) {
3738                 rc = ordered_value_sort( a, 1 );
3739                 if ( rc ) {
3740                         snprintf(ca->cr_msg, sizeof( ca->cr_msg ), "ordered_value_sort failed on attr %s\n",
3741                                 ad->ad_cname.bv_val );
3742                         return rc;
3743                 }
3744         }
3745         for ( i=0; vals[i].bv_val; i++ ) {
3746                 ca->line = vals[i].bv_val;
3747                 if (( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) &&
3748                         ca->line[0] == '{' ) {
3749                         char *idx = strchr( ca->line, '}' );
3750                         if ( idx ) ca->line = idx+1;
3751                 }
3752                 rc = config_parse_vals( ct, ca, i );
3753                 if ( rc ) {
3754                         break;
3755                 }
3756         }
3757         return rc;
3758 }
3759
3760 static int
3761 config_rename_attr( SlapReply *rs, Entry *e, struct berval *rdn,
3762         Attribute **at )
3763 {
3764         struct berval rtype, rval;
3765         Attribute *a;
3766         AttributeDescription *ad = NULL;
3767
3768         dnRdn( &e->e_name, rdn );
3769         rval.bv_val = strchr(rdn->bv_val, '=' ) + 1;
3770         rval.bv_len = rdn->bv_len - (rval.bv_val - rdn->bv_val);
3771         rtype.bv_val = rdn->bv_val;
3772         rtype.bv_len = rval.bv_val - rtype.bv_val - 1;
3773
3774         /* Find attr */
3775         slap_bv2ad( &rtype, &ad, &rs->sr_text );
3776         a = attr_find( e->e_attrs, ad );
3777         if (!a ) return LDAP_NAMING_VIOLATION;
3778         *at = a;
3779
3780         return 0;
3781 }
3782
3783 static void
3784 config_rename_kids( CfEntryInfo *ce )
3785 {
3786         CfEntryInfo *ce2;
3787         struct berval rdn, nrdn;
3788
3789         for (ce2 = ce->ce_kids; ce2; ce2 = ce2->ce_sibs) {
3790                 dnRdn ( &ce2->ce_entry->e_name, &rdn );
3791                 dnRdn ( &ce2->ce_entry->e_nname, &nrdn );
3792                 free( ce2->ce_entry->e_name.bv_val );
3793                 free( ce2->ce_entry->e_nname.bv_val );
3794                 build_new_dn( &ce2->ce_entry->e_name, &ce->ce_entry->e_name,
3795                         &rdn, NULL );
3796                 build_new_dn( &ce2->ce_entry->e_nname, &ce->ce_entry->e_nname,
3797                         &nrdn, NULL );
3798                 config_rename_kids( ce2 );
3799         }
3800 }
3801
3802 static int
3803 config_rename_one( Operation *op, SlapReply *rs, Entry *e,
3804         CfEntryInfo *parent, Attribute *a, struct berval *newrdn,
3805         struct berval *nnewrdn, int use_ldif )
3806 {
3807         char *ptr1;
3808         int rc = 0;
3809         struct berval odn, ondn;
3810
3811         odn = e->e_name;
3812         ondn = e->e_nname;
3813         build_new_dn( &e->e_name, &parent->ce_entry->e_name, newrdn, NULL );
3814         build_new_dn( &e->e_nname, &parent->ce_entry->e_nname, nnewrdn, NULL );
3815
3816         /* Replace attr */
3817         free( a->a_vals[0].bv_val );
3818         ptr1 = strchr( newrdn->bv_val, '=' ) + 1;
3819         a->a_vals[0].bv_len = newrdn->bv_len - (ptr1 - newrdn->bv_val);
3820         a->a_vals[0].bv_val = ch_malloc( a->a_vals[0].bv_len + 1 );
3821         strcpy( a->a_vals[0].bv_val, ptr1 );
3822
3823         if ( a->a_nvals != a->a_vals ) {
3824                 free( a->a_nvals[0].bv_val );
3825                 ptr1 = strchr( nnewrdn->bv_val, '=' ) + 1;
3826                 a->a_nvals[0].bv_len = nnewrdn->bv_len - (ptr1 - nnewrdn->bv_val);
3827                 a->a_nvals[0].bv_val = ch_malloc( a->a_nvals[0].bv_len + 1 );
3828                 strcpy( a->a_nvals[0].bv_val, ptr1 );
3829         }
3830         if ( use_ldif ) {
3831                 CfBackInfo *cfb = (CfBackInfo *)op->o_bd->be_private;
3832                 BackendDB *be = op->o_bd;
3833                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
3834                 struct berval dn, ndn, xdn, xndn;
3835
3836                 op->o_bd = &cfb->cb_db;
3837
3838                 /* Save current rootdn; use the underlying DB's rootdn */
3839                 dn = op->o_dn;
3840                 ndn = op->o_ndn;
3841                 xdn = op->o_req_dn;
3842                 xndn = op->o_req_ndn;
3843                 op->o_dn = op->o_bd->be_rootdn;
3844                 op->o_ndn = op->o_bd->be_rootndn;
3845                 op->o_req_dn = odn;
3846                 op->o_req_ndn = ondn;
3847
3848                 scp = op->o_callback;
3849                 op->o_callback = &sc;
3850                 op->orr_newrdn = *newrdn;
3851                 op->orr_nnewrdn = *nnewrdn;
3852                 op->orr_newSup = NULL;
3853                 op->orr_nnewSup = NULL;
3854                 op->orr_deleteoldrdn = 1;
3855                 op->orr_modlist = NULL;
3856                 slap_modrdn2mods( op, rs );
3857                 slap_mods_opattrs( op, &op->orr_modlist, 1 );
3858                 rc = op->o_bd->be_modrdn( op, rs );
3859                 slap_mods_free( op->orr_modlist, 1 );
3860
3861                 op->o_bd = be;
3862                 op->o_callback = scp;
3863                 op->o_dn = dn;
3864                 op->o_ndn = ndn;
3865                 op->o_req_dn = xdn;
3866                 op->o_req_ndn = xndn;
3867         }
3868         free( odn.bv_val );
3869         free( ondn.bv_val );
3870         if ( e->e_private )
3871                 config_rename_kids( e->e_private );
3872         return rc;
3873 }
3874
3875 static int
3876 config_renumber_one( Operation *op, SlapReply *rs, CfEntryInfo *parent, 
3877         Entry *e, int idx, int tailindex, int use_ldif )
3878 {
3879         struct berval ival, newrdn, nnewrdn;
3880         struct berval rdn;
3881         Attribute *a;
3882         char ibuf[32], *ptr1, *ptr2 = NULL;
3883         int rc = 0;
3884
3885         rc = config_rename_attr( rs, e, &rdn, &a );
3886         if ( rc ) return rc;
3887
3888         ival.bv_val = ibuf;
3889         ival.bv_len = snprintf( ibuf, sizeof( ibuf ), SLAP_X_ORDERED_FMT, idx );
3890         if ( ival.bv_len >= sizeof( ibuf ) ) {
3891                 return LDAP_NAMING_VIOLATION;
3892         }
3893         
3894         newrdn.bv_len = rdn.bv_len + ival.bv_len;
3895         newrdn.bv_val = ch_malloc( newrdn.bv_len+1 );
3896
3897         if ( tailindex ) {
3898                 ptr1 = lutil_strncopy( newrdn.bv_val, rdn.bv_val, rdn.bv_len );
3899                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
3900         } else {
3901                 int xlen;
3902                 ptr2 = ber_bvchr( &rdn, '}' );
3903                 if ( ptr2 ) {
3904                         ptr2++;
3905                 } else {
3906                         ptr2 = rdn.bv_val + a->a_desc->ad_cname.bv_len + 1;
3907                 }
3908                 xlen = rdn.bv_len - (ptr2 - rdn.bv_val);
3909                 ptr1 = lutil_strncopy( newrdn.bv_val, a->a_desc->ad_cname.bv_val,
3910                         a->a_desc->ad_cname.bv_len );
3911                 *ptr1++ = '=';
3912                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
3913                 ptr1 = lutil_strncopy( ptr1, ptr2, xlen );
3914                 *ptr1 = '\0';
3915         }
3916
3917         /* Do the equivalent of ModRDN */
3918         /* Replace DN / NDN */
3919         newrdn.bv_len = ptr1 - newrdn.bv_val;
3920         rdnNormalize( 0, NULL, NULL, &newrdn, &nnewrdn, NULL );
3921         rc = config_rename_one( op, rs, e, parent, a, &newrdn, &nnewrdn, use_ldif );
3922
3923         free( nnewrdn.bv_val );
3924         free( newrdn.bv_val );
3925         return rc;
3926 }
3927
3928 static int
3929 check_name_index( CfEntryInfo *parent, ConfigType ce_type, Entry *e,
3930         SlapReply *rs, int *renum, int *ibase )
3931 {
3932         CfEntryInfo *ce;
3933         int index = -1, gotindex = 0, nsibs, rc = 0;
3934         int renumber = 0, tailindex = 0, isfrontend = 0, isconfig = 0;
3935         char *ptr1, *ptr2 = NULL;
3936         struct berval rdn;
3937
3938         if ( renum ) *renum = 0;
3939
3940         /* These entries don't get indexed/renumbered */
3941         if ( ce_type == Cft_Global ) return 0;
3942         if ( ce_type == Cft_Schema && parent->ce_type == Cft_Global ) return 0;
3943
3944         if ( ce_type == Cft_Module )
3945                 tailindex = 1;
3946
3947         /* See if the rdn has an index already */
3948         dnRdn( &e->e_name, &rdn );
3949         if ( ce_type == Cft_Database ) {
3950                 if ( !strncmp( rdn.bv_val + rdn.bv_len - STRLENOF("frontend"),
3951                                 "frontend", STRLENOF("frontend") )) 
3952                         isfrontend = 1;
3953                 else if ( !strncmp( rdn.bv_val + rdn.bv_len - STRLENOF("config"),
3954                                 "config", STRLENOF("config") )) 
3955                         isconfig = 1;
3956         }
3957         ptr1 = ber_bvchr( &e->e_name, '{' );
3958         if ( ptr1 && ptr1 < &e->e_name.bv_val[ rdn.bv_len ] ) {
3959                 char    *next;
3960                 ptr2 = strchr( ptr1, '}' );
3961                 if ( !ptr2 || ptr2 > &e->e_name.bv_val[ rdn.bv_len ] )
3962                         return LDAP_NAMING_VIOLATION;
3963                 if ( ptr2-ptr1 == 1)
3964                         return LDAP_NAMING_VIOLATION;
3965                 gotindex = 1;
3966                 index = strtol( ptr1 + 1, &next, 10 );
3967                 if ( next == ptr1 + 1 || next[ 0 ] != '}' ) {
3968                         return LDAP_NAMING_VIOLATION;
3969                 }
3970                 if ( index < 0 ) {
3971                         /* Special case, we allow -1 for the frontendDB */
3972                         if ( index != -1 || !isfrontend )
3973                                 return LDAP_NAMING_VIOLATION;
3974                 }
3975                 if ( isconfig && index != 0 ){
3976                         return LDAP_NAMING_VIOLATION;
3977                 }
3978         }
3979
3980         /* count related kids */
3981         for (nsibs=0, ce=parent->ce_kids; ce; ce=ce->ce_sibs) {
3982                 if ( ce->ce_type == ce_type ) nsibs++;
3983         }
3984
3985         /* account for -1 frontend */
3986         if ( ce_type == Cft_Database )
3987                 nsibs--;
3988
3989         if ( index != nsibs ) {
3990                 if ( gotindex ) {
3991                         if ( index < nsibs ) {
3992                                 if ( tailindex ) return LDAP_NAMING_VIOLATION;
3993                                 /* Siblings need to be renumbered */
3994                                 if ( index != -1 || !isfrontend )
3995                                         renumber = 1;
3996                         }
3997                 }
3998                 /* config DB is always "0" */
3999                 if ( isconfig && index == -1 ) {
4000                         index = 0;
4001                 }
4002                 if (( !isfrontend && index == -1 ) || ( index > nsibs ) ){
4003                         index = nsibs;
4004                 }
4005
4006                 /* just make index = nsibs */
4007                 if ( !renumber ) {
4008                         rc = config_renumber_one( NULL, rs, parent, e, index, tailindex, 0 );
4009                 }
4010         }
4011         if ( ibase ) *ibase = index;
4012         if ( renum ) *renum = renumber;
4013         return rc;
4014 }
4015
4016 static int
4017 count_oc( ObjectClass *oc, ConfigOCs ***copp, int *nocs )
4018 {
4019         ConfigOCs       co, *cop;
4020         ObjectClass     **sups;
4021
4022         co.co_name = &oc->soc_cname;
4023         cop = avl_find( CfOcTree, &co, CfOc_cmp );
4024         if ( cop ) {
4025                 int     i;
4026
4027                 /* check for duplicates */
4028                 for ( i = 0; i < *nocs; i++ ) {
4029                         if ( *copp && (*copp)[i] == cop ) {
4030                                 break;
4031                         }
4032                 }
4033
4034                 if ( i == *nocs ) {
4035                         ConfigOCs **tmp = ch_realloc( *copp, (*nocs + 1)*sizeof( ConfigOCs * ) );
4036                         if ( tmp == NULL ) {
4037                                 return -1;
4038                         }
4039                         *copp = tmp;
4040                         (*copp)[*nocs] = cop;
4041                         (*nocs)++;
4042                 }
4043         }
4044
4045         for ( sups = oc->soc_sups; sups && *sups; sups++ ) {
4046                 if ( count_oc( *sups, copp, nocs ) ) {
4047                         return -1;
4048                 }
4049         }
4050
4051         return 0;
4052 }
4053
4054 static ConfigOCs **
4055 count_ocs( Attribute *oc_at, int *nocs )
4056 {
4057         int             i;
4058         ConfigOCs       **colst = NULL;
4059
4060         *nocs = 0;
4061
4062         for ( i = 0; !BER_BVISNULL( &oc_at->a_nvals[i] ); i++ )
4063                 /* count attrs */ ;
4064
4065         for ( ; i--; ) {
4066                 ObjectClass     *oc = oc_bvfind( &oc_at->a_nvals[i] );
4067
4068                 assert( oc != NULL );
4069                 if ( count_oc( oc, &colst, nocs ) ) {
4070                         ch_free( colst );
4071                         return NULL;
4072                 }
4073         }
4074
4075         return colst;
4076 }
4077
4078 static int
4079 cfAddInclude( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
4080 {
4081         /* Leftover from RE23. Never parse this entry */
4082         return LDAP_COMPARE_TRUE;
4083 }
4084
4085 static int
4086 cfAddSchema( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
4087 {
4088         ConfigFile *cfo;
4089
4090         /* This entry is hardcoded, don't re-parse it */
4091         if ( p->ce_type == Cft_Global ) {
4092                 cfn = p->ce_private;
4093                 ca->ca_private = cfn;
4094                 return LDAP_COMPARE_TRUE;
4095         }
4096         if ( p->ce_type != Cft_Schema )
4097                 return LDAP_CONSTRAINT_VIOLATION;
4098
4099         cfn = ch_calloc( 1, sizeof(ConfigFile) );
4100         ca->ca_private = cfn;
4101         cfo = p->ce_private;
4102         cfn->c_sibs = cfo->c_kids;
4103         cfo->c_kids = cfn;
4104         return LDAP_SUCCESS;
4105 }
4106
4107 static int
4108 cfAddDatabase( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4109 {
4110         if ( p->ce_type != Cft_Global ) {
4111                 return LDAP_CONSTRAINT_VIOLATION;
4112         }
4113         /* config must be {0}, nothing else allowed */
4114         if ( !strncmp( e->e_nname.bv_val, "olcDatabase={0}", STRLENOF("olcDatabase={0}")) &&
4115                 strncmp( e->e_nname.bv_val + STRLENOF("olcDatabase={0}"), "config,", STRLENOF("config,") )) {
4116                 return LDAP_CONSTRAINT_VIOLATION;
4117         }
4118         ca->be = frontendDB;    /* just to get past check_vals */
4119         return LDAP_SUCCESS;
4120 }
4121
4122 static int
4123 cfAddBackend( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4124 {
4125         if ( p->ce_type != Cft_Global ) {
4126                 return LDAP_CONSTRAINT_VIOLATION;
4127         }
4128         return LDAP_SUCCESS;
4129 }
4130
4131 static int
4132 cfAddModule( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4133 {
4134         if ( p->ce_type != Cft_Global ) {
4135                 return LDAP_CONSTRAINT_VIOLATION;
4136         }
4137         return LDAP_SUCCESS;
4138 }
4139
4140 static int
4141 cfAddOverlay( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4142 {
4143         if ( p->ce_type != Cft_Database ) {
4144                 return LDAP_CONSTRAINT_VIOLATION;
4145         }
4146         ca->be = p->ce_be;
4147         return LDAP_SUCCESS;
4148 }
4149
4150 static void
4151 schema_destroy_one( ConfigArgs *ca, ConfigOCs **colst, int nocs,
4152         CfEntryInfo *p )
4153 {
4154         ConfigTable *ct;
4155         ConfigFile *cfo;
4156         AttributeDescription *ad;
4157         const char *text;
4158
4159         ca->valx = -1;
4160         ca->line = NULL;
4161         if ( cfn->c_cr_head ) {
4162                 struct berval bv = BER_BVC("olcDitContentRules");
4163                 ad = NULL;
4164                 slap_bv2ad( &bv, &ad, &text );
4165                 ct = config_find_table( colst, nocs, ad, ca );
4166                 config_del_vals( ct, ca );
4167         }
4168         if ( cfn->c_oc_head ) {
4169                 struct berval bv = BER_BVC("olcObjectClasses");
4170                 ad = NULL;
4171                 slap_bv2ad( &bv, &ad, &text );
4172                 ct = config_find_table( colst, nocs, ad, ca );
4173                 config_del_vals( ct, ca );
4174         }
4175         if ( cfn->c_at_head ) {
4176                 struct berval bv = BER_BVC("olcAttributeTypes");
4177                 ad = NULL;
4178                 slap_bv2ad( &bv, &ad, &text );
4179                 ct = config_find_table( colst, nocs, ad, ca );
4180                 config_del_vals( ct, ca );
4181         }
4182         if ( cfn->c_syn_head ) {
4183                 struct berval bv = BER_BVC("olcLdapSyntaxes");
4184                 ad = NULL;
4185                 slap_bv2ad( &bv, &ad, &text );
4186                 ct = config_find_table( colst, nocs, ad, ca );
4187                 config_del_vals( ct, ca );
4188         }
4189         if ( cfn->c_om_head ) {
4190                 struct berval bv = BER_BVC("olcObjectIdentifier");
4191                 ad = NULL;
4192                 slap_bv2ad( &bv, &ad, &text );
4193                 ct = config_find_table( colst, nocs, ad, ca );
4194                 config_del_vals( ct, ca );
4195         }
4196         cfo = p->ce_private;
4197         cfo->c_kids = cfn->c_sibs;
4198         ch_free( cfn );
4199 }
4200
4201 static int
4202 config_add_oc( ConfigOCs **cop, CfEntryInfo *last, Entry *e, ConfigArgs *ca )
4203 {
4204         int             rc = LDAP_CONSTRAINT_VIOLATION;
4205         ObjectClass     **ocp;
4206
4207         if ( (*cop)->co_ldadd ) {
4208                 rc = (*cop)->co_ldadd( last, e, ca );
4209                 if ( rc != LDAP_CONSTRAINT_VIOLATION ) {
4210                         return rc;
4211                 }
4212         }
4213
4214         for ( ocp = (*cop)->co_oc->soc_sups; ocp && *ocp; ocp++ ) {
4215                 ConfigOCs       co = { 0 };
4216
4217                 co.co_name = &(*ocp)->soc_cname;
4218                 *cop = avl_find( CfOcTree, &co, CfOc_cmp );
4219                 if ( *cop == NULL ) {
4220                         return rc;
4221                 }
4222
4223                 rc = config_add_oc( cop, last, e, ca );
4224                 if ( rc != LDAP_CONSTRAINT_VIOLATION ) {
4225                         return rc;
4226                 }
4227         }
4228
4229         return rc;
4230 }
4231
4232 /* Parse an LDAP entry into config directives */
4233 static int
4234 config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca, SlapReply *rs,
4235         int *renum, Operation *op )
4236 {
4237         CfEntryInfo     *ce, *last = NULL;
4238         ConfigOCs       co, *coptr, **colst;
4239         Attribute       *a, *oc_at, *soc_at;
4240         int             i, ibase = -1, nocs, rc = 0;
4241         struct berval   pdn;
4242         ConfigTable     *ct;
4243         char            *ptr, *log_prefix = op ? op->o_log_prefix : "";
4244
4245         memset( ca, 0, sizeof(ConfigArgs));
4246
4247         /* Make sure parent exists and entry does not. But allow
4248          * Databases and Overlays to be inserted. Don't do any
4249          * auto-renumbering if manageDSAit control is present.
4250          */
4251         ce = config_find_base( cfb->cb_root, &e->e_nname, &last );
4252         if ( ce ) {
4253                 if ( ( op && op->o_managedsait ) ||
4254                         ( ce->ce_type != Cft_Database && ce->ce_type != Cft_Overlay &&
4255                           ce->ce_type != Cft_Module ) )
4256                 {
4257                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4258                                 "DN=\"%s\" already exists\n",
4259                                 log_prefix, e->e_name.bv_val, 0 );
4260                         return LDAP_ALREADY_EXISTS;
4261                 }
4262         }
4263
4264         dnParent( &e->e_nname, &pdn );
4265
4266         /* If last is NULL, the new entry is the root/suffix entry, 
4267          * otherwise last should be the parent.
4268          */
4269         if ( last && !dn_match( &last->ce_entry->e_nname, &pdn ) ) {
4270                 if ( rs ) {
4271                         rs->sr_matched = last->ce_entry->e_name.bv_val;
4272                 }
4273                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4274                         "DN=\"%s\" not child of DN=\"%s\"\n",
4275                         log_prefix, e->e_name.bv_val,
4276                         last->ce_entry->e_name.bv_val );
4277                 return LDAP_NO_SUCH_OBJECT;
4278         }
4279
4280         if ( op ) {
4281                 /* No parent, must be root. This will never happen... */
4282                 if ( !last && !be_isroot( op ) && !be_shadow_update( op ) ) {
4283                         return LDAP_NO_SUCH_OBJECT;
4284                 }
4285
4286                 if ( last && !access_allowed( op, last->ce_entry,
4287                         slap_schema.si_ad_children, NULL, ACL_WADD, NULL ) )
4288                 {
4289                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4290                                 "DN=\"%s\" no write access to \"children\" of parent\n",
4291                                 log_prefix, e->e_name.bv_val, 0 );
4292                         return LDAP_INSUFFICIENT_ACCESS;
4293                 }
4294         }
4295
4296         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
4297         if ( !oc_at ) {
4298                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4299                         "DN=\"%s\" no objectClass\n",
4300                         log_prefix, e->e_name.bv_val, 0 );
4301                 return LDAP_OBJECT_CLASS_VIOLATION;
4302         }
4303
4304         soc_at = attr_find( e->e_attrs, slap_schema.si_ad_structuralObjectClass );
4305         if ( !soc_at ) {
4306                 ObjectClass     *soc = NULL;
4307                 char            textbuf[ SLAP_TEXT_BUFLEN ];
4308                 const char      *text = textbuf;
4309
4310                 /* FIXME: check result */
4311                 rc = structural_class( oc_at->a_nvals, &soc, NULL,
4312                         &text, textbuf, sizeof(textbuf), NULL );
4313                 if ( rc != LDAP_SUCCESS ) {
4314                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4315                                 "DN=\"%s\" no structural objectClass (%s)\n",
4316                                 log_prefix, e->e_name.bv_val, text );
4317                         return rc;
4318                 }
4319                 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &soc->soc_cname, NULL );
4320                 soc_at = attr_find( e->e_attrs, slap_schema.si_ad_structuralObjectClass );
4321                 if ( soc_at == NULL ) {
4322                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4323                                 "DN=\"%s\" no structural objectClass; "
4324                                 "unable to merge computed class %s\n",
4325                                 log_prefix, e->e_name.bv_val,
4326                                 soc->soc_cname.bv_val );
4327                         return LDAP_OBJECT_CLASS_VIOLATION;
4328                 }
4329
4330                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4331                         "DN=\"%s\" no structural objectClass; "
4332                         "computed objectClass %s merged\n",
4333                         log_prefix, e->e_name.bv_val,
4334                         soc->soc_cname.bv_val );
4335         }
4336
4337         /* Fake the coordinates based on whether we're part of an
4338          * LDAP Add or if reading the config dir
4339          */
4340         if ( rs ) {
4341                 ca->fname = "slapd";
4342                 ca->lineno = 0;
4343         } else {
4344                 ca->fname = cfdir.bv_val;
4345                 ca->lineno = 1;
4346         }
4347         ca->ca_op = op;
4348
4349         co.co_name = &soc_at->a_nvals[0];
4350         coptr = avl_find( CfOcTree, &co, CfOc_cmp );
4351         if ( coptr == NULL ) {
4352                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4353                         "DN=\"%s\" no structural objectClass in configuration table\n",
4354                         log_prefix, e->e_name.bv_val, 0 );
4355                 return LDAP_OBJECT_CLASS_VIOLATION;
4356         }
4357
4358         /* Only the root can be Cft_Global, everything else must
4359          * have a parent. Only limited nesting arrangements are allowed.
4360          */
4361         rc = LDAP_CONSTRAINT_VIOLATION;
4362         if ( coptr->co_type == Cft_Global && !last ) {
4363                 cfn = cfb->cb_config;
4364                 ca->ca_private = cfn;
4365                 ca->be = frontendDB;    /* just to get past check_vals */
4366                 rc = LDAP_SUCCESS;
4367         }
4368
4369         colst = count_ocs( oc_at, &nocs );
4370
4371         /* Check whether the Add is allowed by its parent, and do
4372          * any necessary arg setup
4373          */
4374         if ( last ) {
4375                 rc = config_add_oc( &coptr, last, e, ca );
4376                 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
4377                         for ( i = 0; i<nocs; i++ ) {
4378                                 /* Already checked these */
4379                                 if ( colst[i]->co_oc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
4380                                         continue;
4381                                 if ( colst[i]->co_ldadd &&
4382                                         ( rc = colst[i]->co_ldadd( last, e, ca ))
4383                                                 != LDAP_CONSTRAINT_VIOLATION ) {
4384                                         coptr = colst[i];
4385                                         break;
4386                                 }
4387                         }
4388                 }
4389                 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
4390                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4391                                 "DN=\"%s\" no structural objectClass add function\n",
4392                                 log_prefix, e->e_name.bv_val, 0 );
4393                         return LDAP_OBJECT_CLASS_VIOLATION;
4394                 }
4395         }
4396
4397         /* Add the entry but don't parse it, we already have its contents */
4398         if ( rc == LDAP_COMPARE_TRUE ) {
4399                 rc = LDAP_SUCCESS;
4400                 goto ok;
4401         }
4402
4403         if ( rc != LDAP_SUCCESS )
4404                 goto done_noop;
4405
4406         /* Parse all the values and check for simple syntax errors before
4407          * performing any set actions.
4408          *
4409          * If doing an LDAPadd, check for indexed names and any necessary
4410          * renaming/renumbering. Entries that don't need indexed names are
4411          * ignored. Entries that need an indexed name and arrive without one
4412          * are assigned to the end. Entries that arrive with an index may
4413          * cause the following entries to be renumbered/bumped down.
4414          *
4415          * Note that "pseudo-indexed" entries (cn=Include{xx}, cn=Module{xx})
4416          * don't allow Adding an entry with an index that's already in use.
4417          * This is flagged as an error (LDAP_ALREADY_EXISTS) up above.
4418          *
4419          * These entries can have auto-assigned indexes (appended to the end)
4420          * but only the other types support auto-renumbering of siblings.
4421          */
4422         {
4423                 rc = check_name_index( last, coptr->co_type, e, rs, renum,
4424                         &ibase );
4425                 if ( rc ) {
4426                         goto done_noop;
4427                 }
4428                 if ( renum && *renum && coptr->co_type != Cft_Database &&
4429                         coptr->co_type != Cft_Overlay )
4430                 {
4431                         snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
4432                                 "operation requires sibling renumbering" );
4433                         rc = LDAP_UNWILLING_TO_PERFORM;
4434                         goto done_noop;
4435                 }
4436         }
4437
4438         init_config_argv( ca );
4439
4440         /* Make sure we process attrs in the required order */
4441         sort_attrs( e, colst, nocs );
4442
4443         for ( a = e->e_attrs; a; a = a->a_next ) {
4444                 if ( a == oc_at ) continue;
4445                 ct = config_find_table( colst, nocs, a->a_desc, ca );
4446                 if ( !ct ) continue;    /* user data? */
4447                 rc = check_vals( ct, ca, a, 1 );
4448                 if ( rc ) goto done_noop;
4449         }
4450
4451         /* Basic syntax checks are OK. Do the actual settings. */
4452         for ( a=e->e_attrs; a; a=a->a_next ) {
4453                 if ( a == oc_at ) continue;
4454                 ct = config_find_table( colst, nocs, a->a_desc, ca );
4455                 if ( !ct ) continue;    /* user data? */
4456                 for (i=0; a->a_vals[i].bv_val; i++) {
4457                         char *iptr = NULL;
4458                         ca->line = a->a_vals[i].bv_val;
4459                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
4460                                 ptr = strchr( ca->line, '}' );
4461                                 if ( ptr ) {
4462                                         iptr = strchr( ca->line, '{' );
4463                                         ca->line = ptr+1;
4464                                 }
4465                         }
4466                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED_SIB ) {
4467                                 if ( iptr ) {
4468                                         ca->valx = strtol( iptr+1, NULL, 0 );
4469                                 } else {
4470                                         ca->valx = -1;
4471                                 }
4472                         } else {
4473                                 ca->valx = i;
4474                         }
4475                         rc = config_parse_add( ct, ca, i );
4476                         if ( rc ) {
4477                                 rc = LDAP_OTHER;
4478                                 goto done;
4479                         }
4480                 }
4481         }
4482 ok:
4483         /* Newly added databases and overlays need to be started up */
4484         if ( CONFIG_ONLINE_ADD( ca )) {
4485                 if ( colst[0]->co_type == Cft_Database ) {
4486                         rc = backend_startup_one( ca->be, &ca->reply );
4487
4488                 } else if ( colst[0]->co_type == Cft_Overlay ) {
4489                         if ( ca->bi->bi_db_open ) {
4490                                 BackendInfo *bi_orig = ca->be->bd_info;
4491                                 ca->be->bd_info = ca->bi;
4492                                 rc = ca->bi->bi_db_open( ca->be, &ca->reply );
4493                                 ca->be->bd_info = bi_orig;
4494                         }
4495                 } else if ( ca->cleanup ) {
4496                         rc = ca->cleanup( ca );
4497                 }
4498                 if ( rc ) {
4499                         if (ca->cr_msg[0] == '\0')
4500                                 snprintf( ca->cr_msg, sizeof( ca->cr_msg ), "<%s> failed startup", ca->argv[0] );
4501
4502                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
4503                                 ca->log, ca->cr_msg, ca->argv[1] );
4504                         rc = LDAP_OTHER;
4505                         goto done;
4506                 }
4507         }
4508
4509         ca->valx = ibase;
4510         ce = ch_calloc( 1, sizeof(CfEntryInfo) );
4511         ce->ce_parent = last;
4512         ce->ce_entry = entry_dup( e );
4513         ce->ce_entry->e_private = ce;
4514         ce->ce_type = colst[0]->co_type;
4515         ce->ce_be = ca->be;
4516         ce->ce_bi = ca->bi;
4517         ce->ce_private = ca->ca_private;
4518         ca->ca_entry = ce->ce_entry;
4519         if ( !last ) {
4520                 cfb->cb_root = ce;
4521         } else if ( last->ce_kids ) {
4522                 CfEntryInfo *c2, **cprev;
4523
4524                 /* Advance to first of this type */
4525                 cprev = &last->ce_kids;
4526                 for ( c2 = *cprev; c2 && c2->ce_type < ce->ce_type; ) {
4527                         cprev = &c2->ce_sibs;
4528                         c2 = c2->ce_sibs;
4529                 }
4530                 /* Account for the (-1) frontendDB entry */
4531                 if ( ce->ce_type == Cft_Database ) {
4532                         if ( ca->be == frontendDB )
4533                                 ibase = 0;
4534                         else if ( ibase != -1 )
4535                                 ibase++;
4536                 }
4537                 /* Append */
4538                 if ( ibase < 0 ) {
4539                         for (c2 = *cprev; c2 && c2->ce_type == ce->ce_type;) {
4540                                 cprev = &c2->ce_sibs;
4541                                 c2 = c2->ce_sibs;
4542                         }
4543                 } else {
4544                 /* Insert */
4545                         int i;
4546                         for ( i=0; i<ibase; i++ ) {
4547                                 c2 = *cprev;
4548                                 cprev = &c2->ce_sibs;
4549                         }
4550                 }
4551                 ce->ce_sibs = *cprev;
4552                 *cprev = ce;
4553         } else {
4554                 last->ce_kids = ce;
4555         }
4556
4557 done:
4558         if ( rc ) {
4559                 if ( (colst[0]->co_type == Cft_Database) && ca->be ) {
4560                         if ( ca->be != frontendDB )
4561                                 backend_destroy_one( ca->be, 1 );
4562                 } else if ( (colst[0]->co_type == Cft_Overlay) && ca->bi ) {
4563                         overlay_destroy_one( ca->be, (slap_overinst *)ca->bi );
4564                 } else if ( colst[0]->co_type == Cft_Schema ) {
4565                         schema_destroy_one( ca, colst, nocs, last );
4566                 }
4567         }
4568 done_noop:
4569
4570         ch_free( ca->argv );
4571         if ( colst ) ch_free( colst );
4572         return rc;
4573 }
4574
4575 #define BIGTMP  10000
4576 static int
4577 config_rename_add( Operation *op, SlapReply *rs, CfEntryInfo *ce,
4578         int base, int rebase, int max, int use_ldif )
4579 {
4580         CfEntryInfo *ce2, *ce3, *cetmp = NULL, *cerem = NULL;
4581         ConfigType etype = ce->ce_type;
4582         int count = 0, rc = 0;
4583
4584         /* Reverse ce list */
4585         for (ce2 = ce->ce_sibs;ce2;ce2 = ce3) {
4586                 if (ce2->ce_type != etype) {
4587                         cerem = ce2;
4588                         break;
4589                 }
4590                 ce3 = ce2->ce_sibs;
4591                 ce2->ce_sibs = cetmp;
4592                 cetmp = ce2;
4593                 count++;
4594                 if ( max && count >= max ) {
4595                         cerem = ce3;
4596                         break;
4597                 }
4598         }
4599
4600         /* Move original to a temp name until increments are done */
4601         if ( rebase ) {
4602                 ce->ce_entry->e_private = NULL;
4603                 rc = config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
4604                         base+BIGTMP, 0, use_ldif );
4605                 ce->ce_entry->e_private = ce;
4606         }
4607         /* start incrementing */
4608         for (ce2=cetmp; ce2; ce2=ce3) {
4609                 ce3 = ce2->ce_sibs;
4610                 ce2->ce_sibs = cerem;
4611                 cerem = ce2;
4612                 if ( rc == 0 ) 
4613                         rc = config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
4614                                 count+base, 0, use_ldif );
4615                 count--;
4616         }
4617         if ( rebase )
4618                 rc = config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
4619                         base, 0, use_ldif );
4620         return rc;
4621 }
4622
4623 static int
4624 config_rename_del( Operation *op, SlapReply *rs, CfEntryInfo *ce,
4625         CfEntryInfo *ce2, int old, int use_ldif )
4626 {
4627         int count = 0;
4628
4629         /* Renumber original to a temp value */
4630         ce->ce_entry->e_private = NULL;
4631         config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
4632                 old+BIGTMP, 0, use_ldif );
4633         ce->ce_entry->e_private = ce;
4634
4635         /* start decrementing */
4636         for (; ce2 != ce; ce2=ce2->ce_sibs) {
4637                 config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
4638                         count+old, 0, use_ldif );
4639                 count++;
4640         }
4641         return config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
4642                 count+old, 0, use_ldif );
4643 }
4644
4645 /* Parse an LDAP entry into config directives, then store in underlying
4646  * database.
4647  */
4648 static int
4649 config_back_add( Operation *op, SlapReply *rs )
4650 {
4651         CfBackInfo *cfb;
4652         int renumber;
4653         ConfigArgs ca;
4654
4655         if ( !access_allowed( op, op->ora_e, slap_schema.si_ad_entry,
4656                 NULL, ACL_WADD, NULL )) {
4657                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
4658                 goto out;
4659         }
4660
4661         /*
4662          * Check for attribute ACL
4663          */
4664         if ( !acl_check_modlist( op, op->ora_e, op->orm_modlist )) {
4665                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
4666                 rs->sr_text = "no write access to attribute";
4667                 goto out;
4668         }
4669
4670         cfb = (CfBackInfo *)op->o_bd->be_private;
4671
4672         /* add opattrs for syncprov */
4673         {
4674                 char textbuf[SLAP_TEXT_BUFLEN];
4675                 size_t textlen = sizeof textbuf;
4676                 rs->sr_err = entry_schema_check(op, op->ora_e, NULL, 0, 1,
4677                         &rs->sr_text, textbuf, sizeof( textbuf ) );
4678                 if ( rs->sr_err != LDAP_SUCCESS )
4679                         goto out;
4680                 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
4681                 if ( rs->sr_err != LDAP_SUCCESS ) {
4682                         Debug( LDAP_DEBUG_TRACE,
4683                                 LDAP_XSTRING(config_back_add) ": entry failed op attrs add: "
4684                                 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
4685                         goto out;
4686                 }
4687         }
4688
4689         ldap_pvt_thread_pool_pause( &connection_pool );
4690
4691         /* Strategy:
4692          * 1) check for existence of entry
4693          * 2) check for sibling renumbering
4694          * 3) perform internal add
4695          * 4) perform any necessary renumbering
4696          * 5) store entry in underlying database
4697          */
4698         rs->sr_err = config_add_internal( cfb, op->ora_e, &ca, rs, &renumber, op );
4699         if ( rs->sr_err != LDAP_SUCCESS ) {
4700                 rs->sr_text = ca.cr_msg;
4701                 goto out2;
4702         }
4703
4704         if ( renumber ) {
4705                 CfEntryInfo *ce = ca.ca_entry->e_private;
4706                 req_add_s addr = op->oq_add;
4707                 op->o_tag = LDAP_REQ_MODRDN;
4708                 rs->sr_err = config_rename_add( op, rs, ce, ca.valx, 0, 0, cfb->cb_use_ldif );
4709                 op->o_tag = LDAP_REQ_ADD;
4710                 op->oq_add = addr;
4711                 if ( rs->sr_err != LDAP_SUCCESS ) {
4712                         goto out2;
4713                 }
4714         }
4715
4716         if ( cfb->cb_use_ldif ) {
4717                 BackendDB *be = op->o_bd;
4718                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
4719                 struct berval dn, ndn;
4720
4721                 op->o_bd = &cfb->cb_db;
4722
4723                 /* Save current rootdn; use the underlying DB's rootdn */
4724                 dn = op->o_dn;
4725                 ndn = op->o_ndn;
4726                 op->o_dn = op->o_bd->be_rootdn;
4727                 op->o_ndn = op->o_bd->be_rootndn;
4728
4729                 scp = op->o_callback;
4730                 op->o_callback = &sc;
4731                 op->o_bd->be_add( op, rs );
4732                 op->o_bd = be;
4733                 op->o_callback = scp;
4734                 op->o_dn = dn;
4735                 op->o_ndn = ndn;
4736         }
4737
4738 out2:;
4739         ldap_pvt_thread_pool_resume( &connection_pool );
4740
4741 out:;
4742         send_ldap_result( op, rs );
4743         slap_graduate_commit_csn( op );
4744         return rs->sr_err;
4745 }
4746
4747 typedef struct delrec {
4748         struct delrec *next;
4749         int nidx;
4750         int idx[1];
4751 } delrec;
4752
4753 static int
4754 config_modify_add( ConfigTable *ct, ConfigArgs *ca, AttributeDescription *ad,
4755         int i )
4756 {
4757         int rc;
4758
4759         if (ad->ad_type->sat_flags & SLAP_AT_ORDERED &&
4760                 ca->line[0] == '{' )
4761         {
4762                 char *ptr = strchr( ca->line + 1, '}' );
4763                 if ( ptr ) {
4764                         char    *next;
4765
4766                         ca->valx = strtol( ca->line + 1, &next, 0 );
4767                         if ( next == ca->line + 1 || next[ 0 ] != '}' ) {
4768                                 return LDAP_OTHER;
4769                         }
4770                         ca->line = ptr+1;
4771                 }
4772         }
4773         rc = config_parse_add( ct, ca, i );
4774         if ( rc ) {
4775                 rc = LDAP_OTHER;
4776         }
4777         return rc;
4778 }
4779
4780 static int
4781 config_modify_internal( CfEntryInfo *ce, Operation *op, SlapReply *rs,
4782         ConfigArgs *ca )
4783 {
4784         int rc = LDAP_UNWILLING_TO_PERFORM;
4785         Modifications *ml;
4786         Entry *e = ce->ce_entry;
4787         Attribute *save_attrs = e->e_attrs, *oc_at, *s, *a;
4788         ConfigTable *ct;
4789         ConfigOCs **colst;
4790         int i, nocs;
4791         char *ptr;
4792         delrec *dels = NULL, *deltail = NULL;
4793
4794         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
4795         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
4796
4797         colst = count_ocs( oc_at, &nocs );
4798
4799         /* make sure add/del flags are clear; should always be true */
4800         for ( s = save_attrs; s; s = s->a_next ) {
4801                 s->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
4802         }
4803
4804         e->e_attrs = attrs_dup( e->e_attrs );
4805
4806         init_config_argv( ca );
4807         ca->be = ce->ce_be;
4808         ca->bi = ce->ce_bi;
4809         ca->ca_private = ce->ce_private;
4810         ca->ca_entry = e;
4811         ca->fname = "slapd";
4812         ca->ca_op = op;
4813         strcpy( ca->log, "back-config" );
4814
4815         for (ml = op->orm_modlist; ml; ml=ml->sml_next) {
4816                 ct = config_find_table( colst, nocs, ml->sml_desc, ca );
4817                 switch (ml->sml_op) {
4818                 case LDAP_MOD_DELETE:
4819                 case LDAP_MOD_REPLACE: {
4820                         BerVarray vals = NULL, nvals = NULL;
4821                         int *idx = NULL;
4822                         if ( ct && ( ct->arg_type & ARG_NO_DELETE )) {
4823                                 rc = LDAP_OTHER;
4824                                 snprintf(ca->cr_msg, sizeof(ca->cr_msg), "cannot delete %s",
4825                                         ml->sml_desc->ad_cname.bv_val );
4826                                 goto out_noop;
4827                         }
4828                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
4829                                 vals = ml->sml_values;
4830                                 nvals = ml->sml_nvalues;
4831                                 ml->sml_values = NULL;
4832                                 ml->sml_nvalues = NULL;
4833                         }
4834                         /* If we're deleting by values, remember the indexes of the
4835                          * values we deleted.
4836                          */
4837                         if ( ct && ml->sml_values ) {
4838                                 delrec *d;
4839                                 i = ml->sml_numvals;
4840                                 d = ch_malloc( sizeof(delrec) + (i - 1)* sizeof(int));
4841                                 d->nidx = i;
4842                                 d->next = NULL;
4843                                 if ( dels ) {
4844                                         deltail->next = d;
4845                                 } else {
4846                                         dels = d;
4847                                 }
4848                                 deltail = d;
4849                                 idx = d->idx;
4850                         }
4851                         rc = modify_delete_vindex(e, &ml->sml_mod,
4852                                 get_permissiveModify(op),
4853                                 &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg), idx );
4854                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
4855                                 ml->sml_values = vals;
4856                                 ml->sml_nvalues = nvals;
4857                         }
4858                         if ( !vals )
4859                                 break;
4860                         }
4861                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
4862
4863                 case LDAP_MOD_ADD:
4864                 case SLAP_MOD_SOFTADD: {
4865                         int mop = ml->sml_op;
4866                         int navals = -1;
4867                         ml->sml_op = LDAP_MOD_ADD;
4868                         if ( ct ) {
4869                                 if ( ct->arg_type & ARG_NO_INSERT ) {
4870                                         Attribute *a = attr_find( e->e_attrs, ml->sml_desc );
4871                                         if ( a ) {
4872                                                 navals = a->a_numvals;
4873                                         }
4874                                 }
4875                                 for ( i=0; !BER_BVISNULL( &ml->sml_values[i] ); i++ ) {
4876                                         if ( ml->sml_values[i].bv_val[0] == '{' &&
4877                                                 navals >= 0 )
4878                                         {
4879                                                 char    *next, *val = ml->sml_values[i].bv_val + 1;
4880                                                 int     j;
4881
4882                                                 j = strtol( val, &next, 0 );
4883                                                 if ( next == val || next[ 0 ] != '}' || j < navals ) {
4884                                                         rc = LDAP_OTHER;
4885                                                         snprintf(ca->cr_msg, sizeof(ca->cr_msg), "cannot insert %s",
4886                                                                 ml->sml_desc->ad_cname.bv_val );
4887                                                         goto out_noop;
4888                                                 }
4889                                         }
4890                                         rc = check_vals( ct, ca, ml, 0 );
4891                                         if ( rc ) goto out_noop;
4892                                 }
4893                         }
4894                         rc = modify_add_values(e, &ml->sml_mod,
4895                                    get_permissiveModify(op),
4896                                    &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg) );
4897
4898                         /* If value already exists, show success here
4899                          * and ignore this operation down below.
4900                          */
4901                         if ( mop == SLAP_MOD_SOFTADD ) {
4902                                 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS )
4903                                         rc = LDAP_SUCCESS;
4904                                 else
4905                                         mop = LDAP_MOD_ADD;
4906                         }
4907                         ml->sml_op = mop;
4908                         break;
4909                         }
4910
4911                         break;
4912                 case LDAP_MOD_INCREMENT:        /* FIXME */
4913                         break;
4914                 default:
4915                         break;
4916                 }
4917                 if(rc != LDAP_SUCCESS) break;
4918         }
4919         
4920         if ( rc == LDAP_SUCCESS) {
4921                 /* check that the entry still obeys the schema */
4922                 rc = entry_schema_check(op, e, NULL, 0, 0,
4923                         &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg) );
4924         }
4925         if ( rc ) goto out_noop;
4926
4927         /* Basic syntax checks are OK. Do the actual settings. */
4928         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
4929                 ct = config_find_table( colst, nocs, ml->sml_desc, ca );
4930                 if ( !ct ) continue;
4931
4932                 s = attr_find( save_attrs, ml->sml_desc );
4933                 a = attr_find( e->e_attrs, ml->sml_desc );
4934
4935                 switch (ml->sml_op) {
4936                 case LDAP_MOD_DELETE:
4937                 case LDAP_MOD_REPLACE: {
4938                         BerVarray vals = NULL, nvals = NULL;
4939                         delrec *d = NULL;
4940
4941                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
4942                                 vals = ml->sml_values;
4943                                 nvals = ml->sml_nvalues;
4944                                 ml->sml_values = NULL;
4945                                 ml->sml_nvalues = NULL;
4946                         }
4947
4948                         if ( ml->sml_values )
4949                                 d = dels;
4950
4951                         /* If we didn't delete the whole attribute */
4952                         if ( ml->sml_values && a ) {
4953                                 struct berval *mvals;
4954                                 int j;
4955
4956                                 if ( ml->sml_nvalues )
4957                                         mvals = ml->sml_nvalues;
4958                                 else
4959                                         mvals = ml->sml_values;
4960
4961                                 /* use the indexes we saved up above */
4962                                 for (i=0; i < d->nidx; i++) {
4963                                         struct berval bv = *mvals++;
4964                                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED &&
4965                                                 bv.bv_val[0] == '{' ) {
4966                                                 ptr = strchr( bv.bv_val, '}' ) + 1;
4967                                                 bv.bv_len -= ptr - bv.bv_val;
4968                                                 bv.bv_val = ptr;
4969                                         }
4970                                         ca->line = bv.bv_val;
4971                                         ca->valx = d->idx[i];
4972                                         rc = config_del_vals( ct, ca );
4973                                         if ( rc != LDAP_SUCCESS ) break;
4974                                         if ( s )
4975                                                 s->a_flags |= SLAP_ATTR_IXDEL;
4976                                         for (j=i+1; j < d->nidx; j++)
4977                                                 if ( d->idx[j] >d->idx[i] )
4978                                                         d->idx[j]--;
4979                                 }
4980                         } else {
4981                                 ca->valx = -1;
4982                                 ca->line = NULL;
4983                                 rc = config_del_vals( ct, ca );
4984                                 if ( rc ) rc = LDAP_OTHER;
4985                                 if ( s )
4986                                         s->a_flags |= SLAP_ATTR_IXDEL;
4987                         }
4988                         if ( ml->sml_values ) {
4989                                 d = d->next;
4990                                 ch_free( dels );
4991                                 dels = d;
4992                         }
4993                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
4994                                 ml->sml_values = vals;
4995                                 ml->sml_nvalues = nvals;
4996                         }
4997                         if ( !vals || rc != LDAP_SUCCESS )
4998                                 break;
4999                         }
5000                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
5001
5002                 case LDAP_MOD_ADD:
5003                         for (i=0; ml->sml_values[i].bv_val; i++) {
5004                                 ca->line = ml->sml_values[i].bv_val;
5005                                 ca->valx = -1;
5006                                 rc = config_modify_add( ct, ca, ml->sml_desc, i );
5007                                 if ( rc )
5008                                         goto out;
5009                                 a->a_flags |= SLAP_ATTR_IXADD;
5010                         }
5011                         break;
5012                 }
5013         }
5014
5015 out:
5016         /* Undo for a failed operation */
5017         if ( rc != LDAP_SUCCESS ) {
5018                 ConfigReply msg = ca->reply;
5019                 for ( s = save_attrs; s; s = s->a_next ) {
5020                         if ( s->a_flags & SLAP_ATTR_IXDEL ) {
5021                                 s->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
5022                                 ct = config_find_table( colst, nocs, s->a_desc, ca );
5023                                 a = attr_find( e->e_attrs, s->a_desc );
5024                                 if ( a ) {
5025                                         /* clear the flag so the add check below will skip it */
5026                                         a->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
5027                                         ca->valx = -1;
5028                                         ca->line = NULL;
5029                                         config_del_vals( ct, ca );
5030                                 }
5031                                 for ( i=0; !BER_BVISNULL( &s->a_vals[i] ); i++ ) {
5032                                         ca->line = s->a_vals[i].bv_val;
5033                                         ca->valx = -1;
5034                                         config_modify_add( ct, ca, s->a_desc, i );
5035                                 }
5036                         }
5037                 }
5038                 for ( a = e->e_attrs; a; a = a->a_next ) {
5039                         if ( a->a_flags & SLAP_ATTR_IXADD ) {
5040                                 ct = config_find_table( colst, nocs, a->a_desc, ca );
5041                                 ca->valx = -1;
5042                                 ca->line = NULL;
5043                                 config_del_vals( ct, ca );
5044                                 s = attr_find( save_attrs, a->a_desc );
5045                                 if ( s ) {
5046                                         s->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
5047                                         for ( i=0; !BER_BVISNULL( &s->a_vals[i] ); i++ ) {
5048                                                 ca->line = s->a_vals[i].bv_val;
5049                                                 ca->valx = -1;
5050                                                 config_modify_add( ct, ca, s->a_desc, i );
5051                                         }
5052                                 }
5053                         }
5054                 }
5055                 ca->reply = msg;
5056         }
5057
5058         if ( ca->cleanup )
5059                 ca->cleanup( ca );
5060 out_noop:
5061         if ( rc == LDAP_SUCCESS ) {
5062                 attrs_free( save_attrs );
5063         } else {
5064                 attrs_free( e->e_attrs );
5065                 e->e_attrs = save_attrs;
5066         }
5067         ch_free( ca->argv );
5068         if ( colst ) ch_free( colst );
5069         while( dels ) {
5070                 deltail = dels->next;
5071                 ch_free( dels );
5072                 dels = deltail;
5073         }
5074
5075         return rc;
5076 }
5077
5078 static int
5079 config_back_modify( Operation *op, SlapReply *rs )
5080 {
5081         CfBackInfo *cfb;
5082         CfEntryInfo *ce, *last;
5083         Modifications *ml;
5084         ConfigArgs ca = {0};
5085         struct berval rdn;
5086         char *ptr;
5087         AttributeDescription *rad = NULL;
5088         int do_pause = 1;
5089
5090         cfb = (CfBackInfo *)op->o_bd->be_private;
5091
5092         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5093         if ( !ce ) {
5094                 if ( last )
5095                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5096                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5097                 goto out;
5098         }
5099
5100         if ( !acl_check_modlist( op, ce->ce_entry, op->orm_modlist )) {
5101                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5102                 goto out;
5103         }
5104
5105         /* Get type of RDN */
5106         rdn = ce->ce_entry->e_nname;
5107         ptr = strchr( rdn.bv_val, '=' );
5108         rdn.bv_len = ptr - rdn.bv_val;
5109         slap_bv2ad( &rdn, &rad, &rs->sr_text );
5110
5111         /* Some basic validation... */
5112         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
5113                 /* Don't allow Modify of RDN; must use ModRdn for that. */
5114                 if ( ml->sml_desc == rad ) {
5115                         rs->sr_err = LDAP_NOT_ALLOWED_ON_RDN;
5116                         rs->sr_text = "Use modrdn to change the entry name";
5117                         goto out;
5118                 }
5119                 /* Internal update of contextCSN? */
5120                 if ( ml->sml_desc == slap_schema.si_ad_contextCSN && op->o_conn->c_conn_idx == -1 ) {
5121                         do_pause = 0;
5122                         break;
5123                 }
5124         }
5125
5126         slap_mods_opattrs( op, &op->orm_modlist, 1 );
5127
5128         if ( do_pause )
5129                 ldap_pvt_thread_pool_pause( &connection_pool );
5130
5131         /* Strategy:
5132          * 1) perform the Modify on the cached Entry.
5133          * 2) verify that the Entry still satisfies the schema.
5134          * 3) perform the individual config operations.
5135          * 4) store Modified entry in underlying LDIF backend.
5136          */
5137         rs->sr_err = config_modify_internal( ce, op, rs, &ca );
5138         if ( rs->sr_err ) {
5139                 rs->sr_text = ca.cr_msg;
5140         } else if ( cfb->cb_use_ldif ) {
5141                 BackendDB *be = op->o_bd;
5142                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
5143                 struct berval dn, ndn;
5144
5145                 op->o_bd = &cfb->cb_db;
5146
5147                 dn = op->o_dn;
5148                 ndn = op->o_ndn;
5149                 op->o_dn = op->o_bd->be_rootdn;
5150                 op->o_ndn = op->o_bd->be_rootndn;
5151
5152                 scp = op->o_callback;
5153                 op->o_callback = &sc;
5154                 op->o_bd->be_modify( op, rs );
5155                 op->o_bd = be;
5156                 op->o_callback = scp;
5157                 op->o_dn = dn;
5158                 op->o_ndn = ndn;
5159         }
5160
5161         if ( do_pause )
5162                 ldap_pvt_thread_pool_resume( &connection_pool );
5163 out:
5164         send_ldap_result( op, rs );
5165         slap_graduate_commit_csn( op );
5166         return rs->sr_err;
5167 }
5168
5169 static int
5170 config_back_modrdn( Operation *op, SlapReply *rs )
5171 {
5172         CfBackInfo *cfb;
5173         CfEntryInfo *ce, *last;
5174         struct berval rdn;
5175         int ixold, ixnew;
5176
5177         cfb = (CfBackInfo *)op->o_bd->be_private;
5178
5179         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5180         if ( !ce ) {
5181                 if ( last )
5182                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5183                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5184                 goto out;
5185         }
5186         if ( !access_allowed( op, ce->ce_entry, slap_schema.si_ad_entry,
5187                 NULL, ACL_WRITE, NULL )) {
5188                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5189                 goto out;
5190         }
5191         { Entry *parent;
5192                 if ( ce->ce_parent )
5193                         parent = ce->ce_parent->ce_entry;
5194                 else
5195                         parent = (Entry *)&slap_entry_root;
5196                 if ( !access_allowed( op, parent, slap_schema.si_ad_children,
5197                         NULL, ACL_WRITE, NULL )) {
5198                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5199                         goto out;
5200                 }
5201         }
5202
5203         /* We don't allow moving objects to new parents.
5204          * Generally we only allow reordering a set of ordered entries.
5205          */
5206         if ( op->orr_newSup ) {
5207                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5208                 goto out;
5209         }
5210
5211         /* If newRDN == oldRDN, quietly succeed */
5212         dnRdn( &op->o_req_ndn, &rdn );
5213         if ( dn_match( &rdn, &op->orr_nnewrdn )) {
5214                 rs->sr_err = LDAP_SUCCESS;
5215                 goto out;
5216         }
5217
5218         /* Current behavior, subject to change as needed:
5219          *
5220          * For backends and overlays, we only allow renumbering.
5221          * For schema, we allow renaming with the same number.
5222          * Otherwise, the op is not allowed.
5223          */
5224
5225         if ( ce->ce_type == Cft_Schema ) {
5226                 char *ptr1, *ptr2;
5227                 int len;
5228
5229                 /* Can't alter the main cn=schema entry */
5230                 if ( ce->ce_parent->ce_type == Cft_Global ) {
5231                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5232                         rs->sr_text = "renaming not allowed for this entry";
5233                         goto out;
5234                 }
5235
5236                 /* We could support this later if desired */
5237                 ptr1 = ber_bvchr( &rdn, '}' );
5238                 ptr2 = ber_bvchr( &op->orr_newrdn, '}' );
5239                 len = ptr1 - rdn.bv_val;
5240                 if ( len != ptr2 - op->orr_newrdn.bv_val ||
5241                         strncmp( rdn.bv_val, op->orr_newrdn.bv_val, len )) {
5242                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5243                         rs->sr_text = "schema reordering not supported";
5244                         goto out;
5245                 }
5246         } else if ( ce->ce_type == Cft_Database ||
5247                 ce->ce_type == Cft_Overlay ) {
5248                 char *ptr1, *ptr2, *iptr1, *iptr2;
5249                 int len1, len2;
5250
5251                 iptr2 = ber_bvchr( &op->orr_newrdn, '=' ) + 1;
5252                 if ( *iptr2 != '{' ) {
5253                         rs->sr_err = LDAP_NAMING_VIOLATION;
5254                         rs->sr_text = "new ordering index is required";
5255                         goto out;
5256                 }
5257                 iptr2++;
5258                 iptr1 = ber_bvchr( &rdn, '{' ) + 1;
5259                 ptr1 = ber_bvchr( &rdn, '}' );
5260                 ptr2 = ber_bvchr( &op->orr_newrdn, '}' );
5261                 if ( !ptr2 ) {
5262                         rs->sr_err = LDAP_NAMING_VIOLATION;
5263                         rs->sr_text = "new ordering index is required";
5264                         goto out;
5265                 }
5266
5267                 len1 = ptr1 - rdn.bv_val;
5268                 len2 = ptr2 - op->orr_newrdn.bv_val;
5269
5270                 if ( rdn.bv_len - len1 != op->orr_newrdn.bv_len - len2 ||
5271                         strncmp( ptr1, ptr2, rdn.bv_len - len1 )) {
5272                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5273                         rs->sr_text = "changing database/overlay type not allowed";
5274                         goto out;
5275                 }
5276                 ixold = strtol( iptr1, NULL, 0 );
5277                 ixnew = strtol( iptr2, &ptr1, 0 );
5278                 if ( ptr1 != ptr2 || ixold < 0 || ixnew < 0 ) {
5279                         rs->sr_err = LDAP_NAMING_VIOLATION;
5280                         goto out;
5281                 }
5282                 /* config DB is always 0, cannot be changed */
5283                 if ( ce->ce_type == Cft_Database && ( ixold == 0 || ixnew == 0 )) {
5284                         rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
5285                         goto out;
5286                 }
5287         } else {
5288                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5289                 rs->sr_text = "renaming not supported for this entry";
5290                 goto out;
5291         }
5292
5293         ldap_pvt_thread_pool_pause( &connection_pool );
5294
5295         if ( ce->ce_type == Cft_Schema ) {
5296                 req_modrdn_s modr = op->oq_modrdn;
5297                 struct berval rdn;
5298                 Attribute *a;
5299                 rs->sr_err = config_rename_attr( rs, ce->ce_entry, &rdn, &a );
5300                 if ( rs->sr_err == LDAP_SUCCESS ) {
5301                         rs->sr_err = config_rename_one( op, rs, ce->ce_entry,
5302                                 ce->ce_parent, a, &op->orr_newrdn, &op->orr_nnewrdn,
5303                                 cfb->cb_use_ldif );
5304                 }
5305                 op->oq_modrdn = modr;
5306         } else {
5307                 CfEntryInfo *ce2, *cebase, **cprev, **cbprev, *ceold;
5308                 req_modrdn_s modr = op->oq_modrdn;
5309                 int i;
5310
5311                 /* Advance to first of this type */
5312                 cprev = &ce->ce_parent->ce_kids;
5313                 for ( ce2 = *cprev; ce2 && ce2->ce_type != ce->ce_type; ) {
5314                         cprev = &ce2->ce_sibs;
5315                         ce2 = ce2->ce_sibs;
5316                 }
5317                 /* Skip the -1 entry */
5318                 if ( ce->ce_type == Cft_Database ) {
5319                         cprev = &ce2->ce_sibs;
5320                         ce2 = ce2->ce_sibs;
5321                 }
5322                 cebase = ce2;
5323                 cbprev = cprev;
5324
5325                 /* Remove from old slot */
5326                 for ( ce2 = *cprev; ce2 && ce2 != ce; ce2 = ce2->ce_sibs )
5327                         cprev = &ce2->ce_sibs;
5328                 *cprev = ce->ce_sibs;
5329                 ceold = ce->ce_sibs;
5330
5331                 /* Insert into new slot */
5332                 cprev = cbprev;
5333                 for ( i=0; i<ixnew; i++ ) {
5334                         ce2 = *cprev;
5335                         if ( !ce2 )
5336                                 break;
5337                         cprev = &ce2->ce_sibs;
5338                 }
5339                 ce->ce_sibs = *cprev;
5340                 *cprev = ce;
5341
5342                 ixnew = i;
5343
5344                 /* NOTE: These should be encoded in the OC tables, not inline here */
5345                 if ( ce->ce_type == Cft_Database )
5346                         backend_db_move( ce->ce_be, ixnew );
5347                 else if ( ce->ce_type == Cft_Overlay )
5348                         overlay_move( ce->ce_be, (slap_overinst *)ce->ce_bi, ixnew );
5349                         
5350                 if ( ixold < ixnew ) {
5351                         rs->sr_err = config_rename_del( op, rs, ce, ceold, ixold,
5352                                 cfb->cb_use_ldif );
5353                 } else {
5354                         rs->sr_err = config_rename_add( op, rs, ce, ixnew, 1,
5355                                 ixold - ixnew, cfb->cb_use_ldif );
5356                 }
5357                 op->oq_modrdn = modr;
5358         }
5359
5360         ldap_pvt_thread_pool_resume( &connection_pool );
5361 out:
5362         send_ldap_result( op, rs );
5363         return rs->sr_err;
5364 }
5365
5366 static int
5367 config_back_delete( Operation *op, SlapReply *rs )
5368 {
5369 #ifdef SLAP_CONFIG_DELETE
5370         CfBackInfo *cfb;
5371         CfEntryInfo *ce, *last, *ce2;
5372
5373         cfb = (CfBackInfo *)op->o_bd->be_private;
5374
5375         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5376         if ( !ce ) {
5377                 if ( last )
5378                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5379                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5380         } else if ( ce->ce_kids ) {
5381                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5382         } else if ( ce->ce_type == Cft_Overlay || ce->ce_type == Cft_Database ){
5383                 char *iptr;
5384                 int count, ixold;
5385
5386                 ldap_pvt_thread_pool_pause( &connection_pool );
5387
5388                 if ( ce->ce_type == Cft_Overlay ){
5389                         overlay_remove( ce->ce_be, (slap_overinst *)ce->ce_bi );
5390                 } else { /* Cft_Database*/
5391                         if ( ce->ce_be == frontendDB || ce->ce_be == op->o_bd ){
5392                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5393                                 rs->sr_text = "Cannot delete config or frontend database";
5394                                 ldap_pvt_thread_pool_resume( &connection_pool );
5395                                 goto out;
5396                         } 
5397                         if ( ce->ce_be->bd_info->bi_db_close ) {
5398                                 ce->ce_be->bd_info->bi_db_close( ce->ce_be, NULL );
5399                         }
5400                         backend_destroy_one( ce->ce_be, 1);
5401                 }
5402
5403                 /* remove CfEntryInfo from the siblings list */
5404                 if ( ce->ce_parent->ce_kids == ce ) {
5405                         ce->ce_parent->ce_kids = ce->ce_sibs;
5406                 } else {
5407                         for ( ce2 = ce->ce_parent->ce_kids ; ce2; ce2 = ce2->ce_sibs ) {
5408                                 if ( ce2->ce_sibs == ce ) {
5409                                         ce2->ce_sibs = ce->ce_sibs;
5410                                         break;
5411                                 }
5412                         }
5413                 }
5414
5415                 /* remove from underlying database */
5416                 if ( cfb->cb_use_ldif ) {
5417                         BackendDB *be = op->o_bd;
5418                         slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
5419                         struct berval dn, ndn, req_dn, req_ndn;
5420
5421                         op->o_bd = &cfb->cb_db;
5422
5423                         dn = op->o_dn;
5424                         ndn = op->o_ndn;
5425                         req_dn = op->o_req_dn;
5426                         req_ndn = op->o_req_ndn;
5427
5428                         op->o_dn = op->o_bd->be_rootdn;
5429                         op->o_ndn = op->o_bd->be_rootndn;
5430                         op->o_req_dn = ce->ce_entry->e_name;
5431                         op->o_req_ndn = ce->ce_entry->e_nname;
5432
5433                         scp = op->o_callback;
5434                         op->o_callback = &sc;
5435                         op->o_bd->be_delete( op, rs );
5436                         op->o_bd = be;
5437                         op->o_callback = scp;
5438                         op->o_dn = dn;
5439                         op->o_ndn = ndn;
5440                         op->o_req_dn = req_dn;
5441                         op->o_req_ndn = req_ndn;
5442                 }
5443
5444                 /* renumber siblings */
5445                 iptr = ber_bvchr( &op->o_req_ndn, '{' ) + 1;
5446                 ixold = strtol( iptr, NULL, 0 );
5447                 for (ce2 = ce->ce_sibs, count=0; ce2; ce2=ce2->ce_sibs) {
5448                         config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
5449                                 count+ixold, 0, cfb->cb_use_ldif );
5450                         count++;
5451                 }
5452
5453                 ce->ce_entry->e_private=NULL;
5454                 entry_free(ce->ce_entry);
5455                 ch_free(ce);
5456                 ldap_pvt_thread_pool_resume( &connection_pool );
5457         } else {
5458                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5459         }
5460 #else
5461         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5462 #endif /* SLAP_CONFIG_DELETE */
5463 out:
5464         send_ldap_result( op, rs );
5465         return rs->sr_err;
5466 }
5467
5468 static int
5469 config_back_search( Operation *op, SlapReply *rs )
5470 {
5471         CfBackInfo *cfb;
5472         CfEntryInfo *ce, *last;
5473         slap_mask_t mask;
5474
5475         cfb = (CfBackInfo *)op->o_bd->be_private;
5476
5477         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5478         if ( !ce ) {
5479                 if ( last )
5480                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5481                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5482                 goto out;
5483         }
5484         if ( !access_allowed_mask( op, ce->ce_entry, slap_schema.si_ad_entry, NULL,
5485                 ACL_SEARCH, NULL, &mask ))
5486         {
5487                 if ( !ACL_GRANT( mask, ACL_DISCLOSE )) {
5488                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
5489                 } else {
5490                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5491                 }
5492                 goto out;
5493         }
5494         switch ( op->ors_scope ) {
5495         case LDAP_SCOPE_BASE:
5496         case LDAP_SCOPE_SUBTREE:
5497                 config_send( op, rs, ce, 0 );
5498                 break;
5499                 
5500         case LDAP_SCOPE_ONELEVEL:
5501                 for (ce = ce->ce_kids; ce; ce=ce->ce_sibs) {
5502                         config_send( op, rs, ce, 1 );
5503                 }
5504                 break;
5505         }
5506                 
5507         rs->sr_err = LDAP_SUCCESS;
5508 out:
5509         send_ldap_result( op, rs );
5510         return 0;
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