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