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