]> git.sur5r.net Git - openldap/blob - servers/slapd/bconfig.c
f32f8e98121000b3f9f6b1cbdc9bc1a1807057c8
[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 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 <lutil.h>
36
37 #include "config.h"
38
39 static struct berval config_rdn = BER_BVC("cn=config");
40 static struct berval schema_rdn = BER_BVC("cn=schema");
41
42 #define IFMT    "{%d}"
43
44 #ifdef SLAPD_MODULES
45 typedef struct modpath_s {
46         struct modpath_s *mp_next;
47         struct berval mp_path;
48         BerVarray mp_loads;
49 } ModPaths;
50
51 static ModPaths modpaths, *modlast = &modpaths, *modcur = &modpaths;
52 #endif
53
54 typedef struct ConfigFile {
55         struct ConfigFile *c_sibs;
56         struct ConfigFile *c_kids;
57         struct berval c_file;
58         AttributeType *c_at_head, *c_at_tail;
59         ContentRule *c_cr_head, *c_cr_tail;
60         ObjectClass *c_oc_head, *c_oc_tail;
61         OidMacro *c_om_head, *c_om_tail;
62         BerVarray c_dseFiles;
63 } ConfigFile;
64
65 typedef struct CfOcInfo {
66         struct berval *co_name;
67         ConfigTable *co_table;
68         ConfigType co_type;
69         ObjectClass *co_oc;
70 } CfOcInfo;
71
72 typedef struct CfEntryInfo {
73         struct CfEntryInfo *ce_parent;
74         struct CfEntryInfo *ce_sibs;
75         struct CfEntryInfo *ce_kids;
76         Entry *ce_entry;
77         ConfigType ce_type;
78         BackendInfo *ce_bi;
79         BackendDB *ce_be;
80         void *ce_private;
81 } CfEntryInfo;
82
83 typedef struct {
84         ConfigFile *cb_config;
85         CfEntryInfo *cb_root;
86         BackendDB       cb_db;  /* underlying database */
87         int             cb_got_ldif;
88         int             cb_use_ldif;
89 } CfBackInfo;
90
91 /* These do nothing in slapd, they're kept only to make them
92  * editable here.
93  */
94 static char *replica_pidFile, *replica_argsFile;
95 static int replicationInterval;
96
97 static char     *passwd_salt;
98 static char     *logfileName;
99 static BerVarray authz_rewrites;
100
101 static struct berval cfdir;
102
103 /* Private state */
104 static AttributeDescription *cfAd_backend, *cfAd_database, *cfAd_overlay,
105         *cfAd_include;
106
107 static ObjectClass *cfOc_schema, *cfOc_global, *cfOc_backend, *cfOc_database,
108         *cfOc_include, *cfOc_overlay, *cfOc_module;
109
110 static ConfigFile cf_prv, *cfn = &cf_prv;
111
112 static Avlnode *CfOcTree;
113
114 static int config_add_internal( CfBackInfo *cfb, Entry *e, SlapReply *rs,
115         int *renumber );
116
117 static ConfigDriver config_fname;
118 static ConfigDriver config_cfdir;
119 static ConfigDriver config_generic;
120 static ConfigDriver config_search_base;
121 static ConfigDriver config_passwd_hash;
122 static ConfigDriver config_schema_dn;
123 static ConfigDriver config_sizelimit;
124 static ConfigDriver config_timelimit;
125 static ConfigDriver config_limits; 
126 static ConfigDriver config_overlay;
127 static ConfigDriver config_suffix; 
128 static ConfigDriver config_deref_depth;
129 static ConfigDriver config_rootdn;
130 static ConfigDriver config_rootpw;
131 static ConfigDriver config_restrict;
132 static ConfigDriver config_allows;
133 static ConfigDriver config_disallows;
134 static ConfigDriver config_requires;
135 static ConfigDriver config_security;
136 static ConfigDriver config_referral;
137 static ConfigDriver config_loglevel;
138 static ConfigDriver config_replica;
139 static ConfigDriver config_updatedn;
140 static ConfigDriver config_updateref;
141 static ConfigDriver config_include;
142 #ifdef HAVE_TLS
143 static ConfigDriver config_tls_option;
144 static ConfigDriver config_tls_config;
145 #endif
146 extern ConfigDriver syncrepl_config;
147
148 enum {
149         CFG_ACL = 1,
150         CFG_BACKEND,
151         CFG_DATABASE,
152         CFG_TLS_RAND,
153         CFG_TLS_CIPHER,
154         CFG_TLS_CERT_FILE,
155         CFG_TLS_CERT_KEY,
156         CFG_TLS_CA_PATH,
157         CFG_TLS_CA_FILE,
158         CFG_TLS_VERIFY,
159         CFG_TLS_CRLCHECK,
160         CFG_CONCUR,
161         CFG_THREADS,
162         CFG_SALT,
163         CFG_LIMITS,
164         CFG_RO,
165         CFG_REWRITE,
166         CFG_DEPTH,
167         CFG_OID,
168         CFG_OC,
169         CFG_DIT,
170         CFG_ATTR,
171         CFG_ATOPT,
172         CFG_CHECK,
173         CFG_REPLOG,
174         CFG_ROOTDSE,
175         CFG_LOGFILE,
176         CFG_PLUGIN,
177         CFG_MODLOAD,
178         CFG_MODPATH,
179         CFG_LASTMOD,
180         CFG_AZPOLICY,
181         CFG_AZREGEXP,
182         CFG_SASLSECP,
183         CFG_SSTR_IF_MAX,
184         CFG_SSTR_IF_MIN,
185 };
186
187 typedef struct {
188         char *name, *oid;
189 } OidRec;
190
191 static OidRec OidMacros[] = {
192         /* OpenLDAProot:666.11.1 */
193         { "OLcfg", "1.3.6.1.4.1.4203.666.11.1" },
194         { "OLcfgAt", "OLcfg:3" },
195         { "OLcfgGlAt", "OLcfgAt:0" },
196         { "OLcfgBkAt", "OLcfgAt:1" },
197         { "OLcfgDbAt", "OLcfgAt:2" },
198         { "OLcfgOvAt", "OLcfgAt:3" },
199         { "OLcfgOc", "OLcfg:4" },
200         { "OLcfgGlOc", "OLcfgOc:0" },
201         { "OLcfgBkOc", "OLcfgOc:1" },
202         { "OLcfgDbOc", "OLcfgOc:2" },
203         { "OLcfgOvOc", "OLcfgOc:3" },
204         { "OMsyn", "1.3.6.1.4.1.1466.115.121.1" },
205         { "OMsInteger", "OMsyn:27" },
206         { "OMsBoolean", "OMsyn:7" },
207         { "OMsDN", "OMsyn:12" },
208         { "OMsDirectoryString", "OMsyn:15" },
209         { "OMsOctetString", "OMsyn:40" },
210         { NULL, NULL }
211 };
212
213 /*
214  * OLcfg{Bk|Db}{Oc|At}:0                -> common
215  * OLcfg{Bk|Db}{Oc|At}:1                -> bdb
216  * OLcfg{Bk|Db}{Oc|At}:2                -> ldif
217  * OLcfg{Bk|Db}{Oc|At}:3                -> ldap?
218  */
219
220 /* alphabetical ordering */
221
222 ConfigTable config_back_cf_table[] = {
223         /* This attr is read-only */
224         { "", "", 0, 0, 0, ARG_MAGIC,
225                 &config_fname, "( OLcfgGlAt:78 NAME 'olcConfigFile' "
226                         "DESC 'File for slapd configuration directives' "
227                         "EQUALITY caseIgnoreMatch "
228                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
229         { "", "", 0, 0, 0, ARG_MAGIC,
230                 &config_cfdir, "( OLcfgGlAt:79 NAME 'olcConfigDir' "
231                         "DESC 'Directory for slapd configuration backend' "
232                         "EQUALITY caseIgnoreMatch "
233                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
234         { "access",     NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_ACL,
235                 &config_generic, "( OLcfgGlAt:1 NAME 'olcAccess' "
236                         "DESC 'Access Control List' "
237                         "EQUALITY caseIgnoreMatch "
238                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
239         { "allows",     "features", 2, 0, 5, ARG_PRE_DB|ARG_MAGIC,
240                 &config_allows, "( OLcfgGlAt:2 NAME 'olcAllows' "
241                         "DESC 'Allowed set of deprecated features' "
242                         "EQUALITY caseIgnoreMatch "
243                         "SYNTAX OMsDirectoryString )", NULL, NULL },
244         { "argsfile", "file", 2, 2, 0, ARG_STRING,
245                 &slapd_args_file, "( OLcfgGlAt:3 NAME 'olcArgsFile' "
246                         "DESC 'File for slapd command line options' "
247                         "EQUALITY caseIgnoreMatch "
248                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
249         { "attribute",  "attribute", 2, 0, 9,
250                 ARG_PAREN|ARG_MAGIC|CFG_ATTR|ARG_NO_DELETE|ARG_NO_INSERT,
251                 &config_generic, "( OLcfgGlAt:4 NAME 'olcAttributeTypes' "
252                         "DESC 'OpenLDAP attributeTypes' "
253                         "EQUALITY caseIgnoreMatch "
254                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
255                                 NULL, NULL },
256         { "attributeoptions", NULL, 0, 0, 0, ARG_MAGIC|CFG_ATOPT,
257                 &config_generic, "( OLcfgGlAt:5 NAME 'olcAttributeOptions' "
258                         "EQUALITY caseIgnoreMatch "
259                         "SYNTAX OMsDirectoryString )", NULL, NULL },
260         { "authid-rewrite", NULL, 2, 0, 0,
261 #ifdef SLAP_AUTH_REWRITE
262                 ARG_MAGIC|CFG_REWRITE|ARG_NO_INSERT, &config_generic,
263 #else
264                 ARG_IGNORED, NULL,
265 #endif
266                  "( OLcfgGlAt:6 NAME 'olcAuthIDRewrite' "
267                         "EQUALITY caseIgnoreMatch "
268                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
269         { "authz-policy", "policy", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_AZPOLICY,
270                 &config_generic, "( OLcfgGlAt:7 NAME 'olcAuthzPolicy' "
271                         "EQUALITY caseIgnoreMatch "
272                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
273         { "authz-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP|ARG_NO_INSERT,
274                 &config_generic, "( OLcfgGlAt:8 NAME 'olcAuthzRegexp' "
275                         "EQUALITY caseIgnoreMatch "
276                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
277         { "backend", "type", 2, 2, 0, ARG_PRE_DB|ARG_MAGIC|CFG_BACKEND,
278                 &config_generic, "( OLcfgGlAt:9 NAME 'olcBackend' "
279                         "DESC 'A type of backend' "
280                         "EQUALITY caseIgnoreMatch "
281                         "SYNTAX OMsDirectoryString SINGLE-VALUE X-ORDERED 'SIBLINGS' )",
282                                 NULL, NULL },
283         { "concurrency", "level", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_CONCUR,
284                 &config_generic, "( OLcfgGlAt:10 NAME 'olcConcurrency' "
285                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
286         { "conn_max_pending", "max", 2, 2, 0, ARG_INT,
287                 &slap_conn_max_pending, "( OLcfgGlAt:11 NAME 'olcConnMaxPending' "
288                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
289         { "conn_max_pending_auth", "max", 2, 2, 0, ARG_INT,
290                 &slap_conn_max_pending_auth, "( OLcfgGlAt:12 NAME 'olcConnMaxPendingAuth' "
291                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
292         { "database", "type", 2, 2, 0, ARG_MAGIC|CFG_DATABASE,
293                 &config_generic, "( OLcfgGlAt:13 NAME 'olcDatabase' "
294                         "DESC 'The backend type for a database instance' "
295                         "SUP olcBackend SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
296         { "defaultSearchBase", "dn", 2, 2, 0, ARG_PRE_BI|ARG_PRE_DB|ARG_DN|ARG_MAGIC,
297                 &config_search_base, "( OLcfgGlAt:14 NAME 'olcDefaultSearchBase' "
298                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
299         { "disallows", "features", 2, 0, 8, ARG_PRE_DB|ARG_MAGIC,
300                 &config_disallows, "( OLcfgGlAt:15 NAME 'olcDisallows' "
301                         "EQUALITY caseIgnoreMatch "
302                         "SYNTAX OMsDirectoryString )", NULL, NULL },
303         { "ditcontentrule",     NULL, 0, 0, 0, ARG_MAGIC|CFG_DIT|ARG_NO_DELETE|ARG_NO_INSERT,
304                 &config_generic, "( OLcfgGlAt:16 NAME 'olcDitContentRules' "
305                         "DESC 'OpenLDAP DIT content rules' "
306                         "EQUALITY caseIgnoreMatch "
307                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
308                         NULL, NULL },
309         { "gentlehup", "on|off", 2, 2, 0,
310 #ifdef SIGHUP
311                 ARG_ON_OFF, &global_gentlehup,
312 #else
313                 ARG_IGNORED, NULL,
314 #endif
315                 "( OLcfgGlAt:17 NAME 'olcGentleHUP' "
316                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
317         { "idletimeout", "timeout", 2, 2, 0, ARG_INT,
318                 &global_idletimeout, "( OLcfgGlAt:18 NAME 'olcIdleTimeout' "
319                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
320         { "include", "file", 2, 2, 0, ARG_MAGIC,
321                 &config_include, "( OLcfgGlAt:19 NAME 'olcInclude' "
322                         "SUP labeledURI )", NULL, NULL },
323         { "index_substr_if_minlen", "min", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MIN,
324                 &config_generic, "( OLcfgGlAt:20 NAME 'olcIndexSubstrIfMinLen' "
325                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
326         { "index_substr_if_maxlen", "max", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MAX,
327                 &config_generic, "( OLcfgGlAt:21 NAME 'olcIndexSubstrIfMaxLen' "
328                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
329         { "index_substr_any_len", "len", 2, 2, 0, ARG_INT|ARG_NONZERO,
330                 &index_substr_any_len, "( OLcfgGlAt:22 NAME 'olcIndexSubstrAnyLen' "
331                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
332         { "index_substr_step", "step", 2, 2, 0, ARG_INT|ARG_NONZERO,
333                 &index_substr_any_step, "( OLcfgGlAt:23 NAME 'olcIndexSubstrAnyStep' "
334                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
335         { "lastmod", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_LASTMOD,
336                 &config_generic, "( OLcfgDbAt:0.4 NAME 'olcLastMod' "
337                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
338         { "limits", "limits", 2, 0, 0, ARG_DB|ARG_MAGIC|CFG_LIMITS,
339                 &config_generic, "( OLcfgDbAt:0.5 NAME 'olcLimits' "
340                         "SYNTAX OMsDirectoryString )", NULL, NULL },
341         { "localSSF", "ssf", 2, 2, 0, ARG_INT,
342                 &local_ssf, "( OLcfgGlAt:26 NAME 'olcLocalSSF' "
343                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
344         { "logfile", "file", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_LOGFILE,
345                 &config_generic, "( OLcfgGlAt:27 NAME 'olcLogFile' "
346                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
347         { "loglevel", "level", 2, 0, 0, ARG_MAGIC,
348                 &config_loglevel, "( OLcfgGlAt:28 NAME 'olcLogLevel' "
349                         "SYNTAX OMsDirectoryString )", NULL, NULL },
350         { "maxDerefDepth", "depth", 2, 2, 0, ARG_DB|ARG_INT|ARG_MAGIC|CFG_DEPTH,
351                 &config_generic, "( OLcfgDbAt:0.6 NAME 'olcMaxDerefDepth' "
352                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
353         { "moduleload", "file", 2, 0, 0,
354 #ifdef SLAPD_MODULES
355                 ARG_MAGIC|CFG_MODLOAD, &config_generic,
356 #else
357                 ARG_IGNORED, NULL,
358 #endif
359                 "( OLcfgGlAt:30 NAME 'olcModuleLoad' "
360                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
361         { "modulepath", "path", 2, 2, 0,
362 #ifdef SLAPD_MODULES
363                 ARG_MAGIC|CFG_MODPATH|ARG_NO_DELETE|ARG_NO_INSERT, &config_generic,
364 #else
365                 ARG_IGNORED, NULL,
366 #endif
367                 "( OLcfgGlAt:31 NAME 'olcModulePath' "
368                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
369         { "objectclass", "objectclass", 2, 0, 0, ARG_PAREN|ARG_MAGIC|CFG_OC|ARG_NO_DELETE|ARG_NO_INSERT,
370                 &config_generic, "( OLcfgGlAt:32 NAME 'olcObjectClasses' "
371                 "DESC 'OpenLDAP object classes' "
372                 "EQUALITY caseIgnoreMatch "
373                 "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
374                         NULL, NULL },
375         { "objectidentifier", NULL,     0, 0, 0, ARG_MAGIC|CFG_OID,
376                 &config_generic, "( OLcfgGlAt:33 NAME 'olcObjectIdentifier' "
377                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
378         { "overlay", "overlay", 2, 2, 0, ARG_MAGIC,
379                 &config_overlay, "( OLcfgGlAt:34 NAME 'olcOverlay' "
380                         "SUP olcDatabase SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
381         { "password-crypt-salt-format", "salt", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_SALT,
382                 &config_generic, "( OLcfgGlAt:35 NAME 'olcPasswordCryptSaltFormat' "
383                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
384         { "password-hash", "hash", 2, 2, 0, ARG_MAGIC,
385                 &config_passwd_hash, "( OLcfgGlAt:36 NAME 'olcPasswordHash' "
386                         "SYNTAX OMsDirectoryString )", NULL, NULL },
387         { "pidfile", "file", 2, 2, 0, ARG_STRING,
388                 &slapd_pid_file, "( OLcfgGlAt:37 NAME 'olcPidFile' "
389                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
390         { "plugin", NULL, 0, 0, 0,
391 #ifdef LDAP_SLAPI
392                 ARG_MAGIC|CFG_PLUGIN, &config_generic,
393 #else
394                 ARG_IGNORED, NULL,
395 #endif
396                 "( OLcfgGlAt:38 NAME 'olcPlugin' "
397                         "SYNTAX OMsDirectoryString )", NULL, NULL },
398         { "pluginlog", "filename", 2, 2, 0,
399 #ifdef LDAP_SLAPI
400                 ARG_STRING, &slapi_log_file,
401 #else
402                 ARG_IGNORED, NULL,
403 #endif
404                 "( OLcfgGlAt:39 NAME 'olcPluginLogFile' "
405                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
406         { "readonly", "on|off", 2, 2, 0, ARG_MAY_DB|ARG_ON_OFF|ARG_MAGIC|CFG_RO,
407                 &config_generic, "( OLcfgGlAt:40 NAME 'olcReadOnly' "
408                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
409         { "referral", "url", 2, 2, 0, ARG_MAGIC,
410                 &config_referral, "( OLcfgGlAt:41 NAME 'olcReferral' "
411                         "SUP labeledURI SINGLE-VALUE )", NULL, NULL },
412         { "replica", "host or uri", 2, 0, 0, ARG_DB|ARG_MAGIC,
413                 &config_replica, "( OLcfgDbAt:0.7 NAME 'olcReplica' "
414                         "SUP labeledURI )", NULL, NULL },
415         { "replica-argsfile", NULL, 0, 0, 0, ARG_STRING,
416                 &replica_argsFile, "( OLcfgGlAt:43 NAME 'olcReplicaArgsFile' "
417                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
418         { "replica-pidfile", NULL, 0, 0, 0, ARG_STRING,
419                 &replica_pidFile, "( OLcfgGlAt:44 NAME 'olcReplicaPidFile' "
420                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
421         { "replicationInterval", NULL, 0, 0, 0, ARG_INT,
422                 &replicationInterval, "( OLcfgGlAt:45 NAME 'olcReplicationInterval' "
423                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
424         { "replogfile", "filename", 2, 2, 0, ARG_MAY_DB|ARG_MAGIC|ARG_STRING|CFG_REPLOG,
425                 &config_generic, "( OLcfgGlAt:46 NAME 'olcReplogFile' "
426                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
427         { "require", "features", 2, 0, 7, ARG_MAY_DB|ARG_MAGIC,
428                 &config_requires, "( OLcfgGlAt:47 NAME 'olcRequires' "
429                         "SYNTAX OMsDirectoryString )", NULL, NULL },
430         { "restrict", "op_list", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
431                 &config_restrict, "( OLcfgGlAt:48 NAME 'olcRestrict' "
432                         "SYNTAX OMsDirectoryString )", NULL, NULL },
433         { "reverse-lookup", "on|off", 2, 2, 0,
434 #ifdef SLAPD_RLOOKUPS
435                 ARG_ON_OFF, &use_reverse_lookup,
436 #else
437                 ARG_IGNORED, NULL,
438 #endif
439                 "( OLcfgGlAt:49 NAME 'olcReverseLookup' "
440                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
441         { "rootdn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_MAGIC,
442                 &config_rootdn, "( OLcfgDbAt:0.8 NAME 'olcRootDN' "
443                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
444         { "rootDSE", "file", 2, 2, 0, ARG_MAGIC|CFG_ROOTDSE,
445                 &config_generic, "( OLcfgGlAt:51 NAME 'olcRootDSE' "
446                         "SYNTAX OMsDirectoryString )", NULL, NULL },
447         { "rootpw", "password", 2, 2, 0, ARG_BERVAL|ARG_DB|ARG_MAGIC,
448                 &config_rootpw, "( OLcfgDbAt:0.9 NAME 'olcRootPW' "
449                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
450         { "sasl-authz-policy", NULL, 2, 2, 0, ARG_MAGIC|CFG_AZPOLICY,
451                 &config_generic, NULL, NULL, NULL },
452         { "sasl-host", "host", 2, 2, 0,
453 #ifdef HAVE_CYRUS_SASL
454                 ARG_STRING|ARG_UNIQUE, &global_host,
455 #else
456                 ARG_IGNORED, NULL,
457 #endif
458                 "( OLcfgGlAt:53 NAME 'olcSaslHost' "
459                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
460         { "sasl-realm", "realm", 2, 2, 0,
461 #ifdef HAVE_CYRUS_SASL
462                 ARG_STRING|ARG_UNIQUE, &global_realm,
463 #else
464                 ARG_IGNORED, NULL,
465 #endif
466                 "( OLcfgGlAt:54 NAME 'olcSaslRealm' "
467                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
468         { "sasl-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
469                 &config_generic, NULL, NULL, NULL },
470         { "sasl-secprops", "properties", 2, 2, 0,
471 #ifdef HAVE_CYRUS_SASL
472                 ARG_MAGIC|CFG_SASLSECP, &config_generic,
473 #else
474                 ARG_IGNORED, NULL,
475 #endif
476                 "( OLcfgGlAt:56 NAME 'olcSaslSecProps' "
477                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
478         { "saslRegexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
479                 &config_generic, NULL, NULL, NULL },
480         { "schemacheck", "on|off", 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|CFG_CHECK,
481                 &config_generic, "( OLcfgGlAt:57 NAME 'olcSchemaCheck' "
482                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
483         { "schemadn", "dn", 2, 2, 0, ARG_MAY_DB|ARG_DN|ARG_MAGIC,
484                 &config_schema_dn, "( OLcfgGlAt:58 NAME 'olcSchemaDN' "
485                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
486         { "security", "factors", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
487                 &config_security, "( OLcfgGlAt:59 NAME 'olcSecurity' "
488                         "SYNTAX OMsDirectoryString )", NULL, NULL },
489         { "sizelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
490                 &config_sizelimit, "( OLcfgGlAt:60 NAME 'olcSizeLimit' "
491                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
492         { "sockbuf_max_incoming", "max", 2, 2, 0, ARG_BER_LEN_T,
493                 &sockbuf_max_incoming, "( OLcfgGlAt:61 NAME 'olcSockbufMaxIncoming' "
494                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
495         { "sockbuf_max_incoming_auth", "max", 2, 2, 0, ARG_BER_LEN_T,
496                 &sockbuf_max_incoming_auth, "( OLcfgGlAt:62 NAME 'olcSockbufMaxIncomingAuth' "
497                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
498         { "srvtab", "file", 2, 2, 0,
499 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
500                 ARG_STRING, &ldap_srvtab,
501 #else
502                 ARG_IGNORED, NULL,
503 #endif
504                 "( OLcfgGlAt:63 NAME 'olcSrvtab' "
505                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
506         { "suffix",     "suffix", 2, 2, 0, ARG_DB|ARG_DN|ARG_MAGIC,
507                 &config_suffix, "( OLcfgDbAt:0.10 NAME 'olcSuffix' "
508                         "SYNTAX OMsDN )", NULL, NULL },
509         { "syncrepl", NULL, 0, 0, 0, ARG_DB|ARG_MAGIC,
510                 &syncrepl_config, "( OLcfgDbAt:0.11 NAME 'olcSyncrepl' "
511                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
512         { "threads", "count", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_THREADS,
513                 &config_generic, "( OLcfgGlAt:66 NAME 'olcThreads' "
514                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
515         { "timelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
516                 &config_timelimit, "( OLcfgGlAt:67 NAME 'olcTimeLimit' "
517                         "SYNTAX OMsDirectoryString )", NULL, NULL },
518         { "TLSCACertificateFile", NULL, 0, 0, 0,
519 #ifdef HAVE_TLS
520                 CFG_TLS_CA_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
521 #else
522                 ARG_IGNORED, NULL,
523 #endif
524                 "( OLcfgGlAt:68 NAME 'olcTLSCACertificateFile' "
525                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
526         { "TLSCACertificatePath", NULL, 0, 0, 0,
527 #ifdef HAVE_TLS
528                 CFG_TLS_CA_PATH|ARG_STRING|ARG_MAGIC, &config_tls_option,
529 #else
530                 ARG_IGNORED, NULL,
531 #endif
532                 "( OLcfgGlAt:69 NAME 'olcTLSCACertificatePath' "
533                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
534         { "TLSCertificateFile", NULL, 0, 0, 0,
535 #ifdef HAVE_TLS
536                 CFG_TLS_CERT_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
537 #else
538                 ARG_IGNORED, NULL,
539 #endif
540                 "( OLcfgGlAt:70 NAME 'olcTLSCertificateFile' "
541                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
542         { "TLSCertificateKeyFile", NULL, 0, 0, 0,
543 #ifdef HAVE_TLS
544                 CFG_TLS_CERT_KEY|ARG_STRING|ARG_MAGIC, &config_tls_option,
545 #else
546                 ARG_IGNORED, NULL,
547 #endif
548                 "( OLcfgGlAt:71 NAME 'olcTLSCertificateKeyFile' "
549                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
550         { "TLSCipherSuite",     NULL, 0, 0, 0,
551 #ifdef HAVE_TLS
552                 CFG_TLS_CIPHER|ARG_STRING|ARG_MAGIC, &config_tls_option,
553 #else
554                 ARG_IGNORED, NULL,
555 #endif
556                 "( OLcfgGlAt:72 NAME 'olcTLSCipherSuite' "
557                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
558         { "TLSCRLCheck", NULL, 0, 0, 0,
559 #if defined(HAVE_TLS) && defined(HAVE_OPENSSL_CRL)
560                 CFG_TLS_CRLCHECK|ARG_STRING|ARG_MAGIC, &config_tls_config,
561 #else
562                 ARG_IGNORED, NULL,
563 #endif
564                 "( OLcfgGlAt:73 NAME 'olcTLSCRLCheck' "
565                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
566         { "TLSRandFile", NULL, 0, 0, 0,
567 #ifdef HAVE_TLS
568                 CFG_TLS_RAND|ARG_STRING|ARG_MAGIC, &config_tls_option,
569 #else
570                 ARG_IGNORED, NULL,
571 #endif
572                 "( OLcfgGlAt:74 NAME 'olcTLSRandFile' "
573                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
574         { "TLSVerifyClient", NULL, 0, 0, 0,
575 #ifdef HAVE_TLS
576                 CFG_TLS_VERIFY|ARG_STRING|ARG_MAGIC, &config_tls_config,
577 #else
578                 ARG_IGNORED, NULL,
579 #endif
580                 "( OLcfgGlAt:75 NAME 'olcTLSVerifyClient' "
581                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
582         { "ucdata-path", "path", 2, 2, 0, ARG_IGNORED,
583                 NULL, NULL, NULL, NULL },
584         { "updatedn", "dn", 2, 2, 0, ARG_DB|ARG_MAGIC,
585                 &config_updatedn, "( OLcfgDbAt:0.12 NAME 'olcUpdateDN' "
586                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
587         { "updateref", "url", 2, 2, 0, ARG_DB|ARG_MAGIC,
588                 &config_updateref, "( OLcfgDbAt:0.13 NAME 'olcUpdateRef' "
589                         "SUP labeledURI )", NULL, NULL },
590         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
591                 NULL, NULL, NULL, NULL }
592 };
593
594 static ConfigOCs cf_ocs[] = {
595         { "( OLcfgGlOc:1 "
596                 "NAME 'olcConfig' "
597                 "DESC 'OpenLDAP configuration object' "
598                 "ABSTRACT SUP top )", Cft_Abstract, NULL },
599         { "( OLcfgGlOc:2 "
600                 "NAME 'olcGlobal' "
601                 "DESC 'OpenLDAP Global configuration options' "
602                 "SUP olcConfig STRUCTURAL "
603                 "MAY ( cn $ olcConfigFile $ olcConfigDir $ olcAllows $ olcArgsFile $ "
604                  "olcAttributeOptions $ olcAuthIDRewrite $ "
605                  "olcAuthzPolicy $ olcAuthzRegexp $ olcConcurrency $ "
606                  "olcConnMaxPending $ olcConnMaxPendingAuth $ olcDefaultSearchBase $ "
607                  "olcDisallows $ olcGentleHUP $ olcIdleTimeout $ "
608                  "olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
609                  "olcIndexSubstrAnyLen $ olcIndexSubstrAnyStep $ olcLocalSSF $ "
610                  "olcLogLevel $ "
611                  "olcPasswordCryptSaltFormat $ olcPasswordHash $ olcPidFile $ "
612                  "olcPluginLogFile $ olcReadOnly $ olcReferral $ "
613                  "olcReplicaPidFile $ olcReplicaArgsFile $ olcReplicationInterval $ "
614                  "olcReplogFile $ olcRequires $ olcRestrict $ olcReverseLookup $ "
615                  "olcRootDSE $ olcRootPW $ "
616                  "olcSaslHost $ olcSaslRealm $ olcSaslSecProps $ "
617                  "olcSchemaCheck $ olcSecurity $ olcSizeLimit $ "
618                  "olcSockbufMaxIncoming $ olcSockbufMaxIncomingAuth $ olcSrvtab $ "
619                  "olcThreads $ olcTimeLimit $ olcTLSCACertificateFile $ "
620                  "olcTLSCACertificatePath $ olcTLSCertificateFile $ "
621                  "olcTLSCertificateKeyFile $ olcTLSCipherSuite $ olcTLSCRLCheck $ "
622                  "olcTLSRandFile $ olcTLSVerifyClient $ "
623                  "olcObjectIdentifier $ olcAttributeTypes $ olcObjectClasses $ "
624                  "olcDitContentRules ) )", Cft_Global, &cfOc_global },
625         { "( OLcfgGlOc:3 "
626                 "NAME 'olcSchemaConfig' "
627                 "DESC 'OpenLDAP schema object' "
628                 "SUP olcConfig STRUCTURAL "
629                 "MAY ( cn $ olcObjectIdentifier $ olcAttributeTypes $ "
630                  "olcObjectClasses $ olcDitContentRules ) )",
631                         Cft_Schema, &cfOc_schema },
632         { "( OLcfgGlOc:4 "
633                 "NAME 'olcBackendConfig' "
634                 "DESC 'OpenLDAP Backend-specific options' "
635                 "SUP olcConfig STRUCTURAL "
636                 "MUST olcBackend )", Cft_Backend, &cfOc_backend },
637         { "( OLcfgGlOc:5 "
638                 "NAME 'olcDatabaseConfig' "
639                 "DESC 'OpenLDAP Database-specific options' "
640                 "SUP olcConfig STRUCTURAL "
641                 "MUST olcDatabase "
642                 "MAY ( olcSuffix $ olcAccess $ olcLastMod $ olcLimits $ "
643                  "olcMaxDerefDepth $ olcPlugin $ olcReadOnly $ olcReplica $ "
644                  "olcReplogFile $ olcRequires $ olcRestrict $ olcRootDN $ olcRootPW $ "
645                  "olcSchemaDN $ olcSecurity $ olcSizeLimit $ olcSyncrepl $ "
646                  "olcTimeLimit $ olcUpdateDN $ olcUpdateRef ) )",
647                         Cft_Database, &cfOc_database },
648         { "( OLcfgGlOc:6 "
649                 "NAME 'olcOverlayConfig' "
650                 "DESC 'OpenLDAP Overlay-specific options' "
651                 "SUP olcConfig STRUCTURAL "
652                 "MUST olcOverlay )", Cft_Overlay, &cfOc_overlay },
653         { "( OLcfgGlOc:7 "
654                 "NAME 'olcIncludeFile' "
655                 "DESC 'OpenLDAP configuration include file' "
656                 "SUP olcConfig STRUCTURAL "
657                 "MUST olcInclude "
658                 "MAY ( cn $ olcRootDSE ) )",
659                 Cft_Include, &cfOc_include },
660 #ifdef SLAPD_MODULES
661         { "( OLcfgGlOc:8 "
662                 "NAME 'olcModuleList' "
663                 "DESC 'OpenLDAP dynamic module info' "
664                 "SUP olcConfig STRUCTURAL "
665                 "MUST ( olcModulePath $ olcModuleLoad ) "
666                 "MAY cn )", Cft_Module, &cfOc_module },
667 #endif
668         { NULL, 0, NULL }
669 };
670
671 static int
672 config_generic(ConfigArgs *c) {
673         char *p;
674         int i;
675
676         if ( c->op == SLAP_CONFIG_EMIT ) {
677                 int rc = 0;
678                 switch(c->type) {
679                 case CFG_CONCUR:
680                         c->value_int = ldap_pvt_thread_get_concurrency();
681                         break;
682                 case CFG_THREADS:
683                         c->value_int = connection_pool_max;
684                         break;
685                 case CFG_SALT:
686                         if ( passwd_salt )
687                                 c->value_string = ch_strdup( passwd_salt );
688                         else
689                                 rc = 1;
690                         break;
691                 case CFG_LIMITS:
692                         if ( c->be->be_limits ) {
693                                 char buf[4096*3];
694                                 struct berval bv;
695                                 int i;
696
697                                 for ( i=0; c->be->be_limits[i]; i++ ) {
698                                         bv.bv_len = sprintf( buf, IFMT, i );
699                                         bv.bv_val = buf+bv.bv_len;
700                                         limits_unparse( c->be->be_limits[i], &bv );
701                                         bv.bv_len += bv.bv_val - buf;
702                                         bv.bv_val = buf;
703                                         value_add_one( &c->rvalue_vals, &bv );
704                                 }
705                         }
706                         if ( !c->rvalue_vals ) rc = 1;
707                         break;
708                 case CFG_RO:
709                         c->value_int = (c->be->be_restrictops & SLAP_RESTRICT_OP_WRITES) != 0;
710                         break;
711                 case CFG_AZPOLICY:
712                         c->value_string = ch_strdup( slap_sasl_getpolicy());
713                         break;
714                 case CFG_AZREGEXP:
715                         slap_sasl_regexp_unparse( &c->rvalue_vals );
716                         if ( !c->rvalue_vals ) rc = 1;
717                         break;
718 #ifdef HAVE_CYRUS_SASL
719                 case CFG_SASLSECP: {
720                         struct berval bv = BER_BVNULL;
721                         slap_sasl_secprops_unparse( &bv );
722                         if ( !BER_BVISNULL( &bv )) {
723                                 ber_bvarray_add( &c->rvalue_vals, &bv );
724                         } else {
725                                 rc = 1;
726                         }
727                         }
728                         break;
729 #endif
730                 case CFG_DEPTH:
731                         c->value_int = c->be->be_max_deref_depth;
732                         break;
733                 case CFG_OID: {
734                         ConfigFile *cf = c->private;
735                         if ( !cf )
736                                 oidm_unparse( &c->rvalue_vals, NULL, NULL, 1 );
737                         else if ( cf->c_om_head )
738                                 oidm_unparse( &c->rvalue_vals, cf->c_om_head,
739                                         cf->c_om_tail, 0 );
740                         if ( !c->rvalue_vals )
741                                 rc = 1;
742                         }
743                         break;
744                 case CFG_OC: {
745                         ConfigFile *cf = c->private;
746                         if ( !cf )
747                                 oc_unparse( &c->rvalue_vals, NULL, NULL, 1 );
748                         else if ( cf->c_oc_head )
749                                 oc_unparse( &c->rvalue_vals, cf->c_oc_head,
750                                         cf->c_oc_tail, 0 );
751                         if ( !c->rvalue_vals )
752                                 rc = 1;
753                         }
754                         break;
755                 case CFG_ATTR: {
756                         ConfigFile *cf = c->private;
757                         if ( !cf )
758                                 at_unparse( &c->rvalue_vals, NULL, NULL, 1 );
759                         else if ( cf->c_at_head )
760                                 at_unparse( &c->rvalue_vals, cf->c_at_head,
761                                         cf->c_at_tail, 0 );
762                         if ( !c->rvalue_vals )
763                                 rc = 1;
764                         }
765                         break;
766                 case CFG_DIT: {
767                         ConfigFile *cf = c->private;
768                         if ( !cf )
769                                 cr_unparse( &c->rvalue_vals, NULL, NULL, 1 );
770                         else if ( cf->c_cr_head )
771                                 cr_unparse( &c->rvalue_vals, cf->c_cr_head,
772                                         cf->c_cr_tail, 0 );
773                         if ( !c->rvalue_vals )
774                                 rc = 1;
775                         }
776                         break;
777                         
778                 case CFG_CHECK:
779                         c->value_int = global_schemacheck;
780                         break;
781                 case CFG_ACL: {
782                         AccessControl *a;
783                         char *src, *dst, ibuf[11];
784                         struct berval bv, abv;
785                         for (i=0, a=c->be->be_acl; a; i++,a=a->acl_next) {
786                                 abv.bv_len = sprintf( ibuf, IFMT, i );
787                                 acl_unparse( a, &bv );
788                                 abv.bv_val = ch_malloc( abv.bv_len + bv.bv_len + 1 );
789                                 AC_MEMCPY( abv.bv_val, ibuf, abv.bv_len );
790                                 /* Turn TAB / EOL into plain space */
791                                 for (src=bv.bv_val,dst=abv.bv_val+abv.bv_len; *src; src++) {
792                                         if (isspace(*src)) *dst++ = ' ';
793                                         else *dst++ = *src;
794                                 }
795                                 *dst = '\0';
796                                 if (dst[-1] == ' ') {
797                                         dst--;
798                                         *dst = '\0';
799                                 }
800                                 abv.bv_len = dst - abv.bv_val;
801                                 ber_bvarray_add( &c->rvalue_vals, &abv );
802                         }
803                         rc = (!i);
804                         break;
805                 }
806                 case CFG_REPLOG:
807                         if ( c->be->be_replogfile )
808                                 c->value_string = ch_strdup( c->be->be_replogfile );
809                         break;
810                 case CFG_ROOTDSE: {
811                         ConfigFile *cf = c->private;
812                         if ( cf->c_dseFiles ) {
813                                 value_add( &c->rvalue_vals, cf->c_dseFiles );
814                         } else {
815                                 rc = 1;
816                         }
817                         }
818                         break;
819                 case CFG_LOGFILE:
820                         if ( logfileName )
821                                 c->value_string = ch_strdup( logfileName );
822                         else
823                                 rc = 1;
824                         break;
825                 case CFG_LASTMOD:
826                         c->value_int = (SLAP_NOLASTMOD(c->be) == 0);
827                         break;
828                 case CFG_SSTR_IF_MAX:
829                         c->value_int = index_substr_if_maxlen;
830                         break;
831                 case CFG_SSTR_IF_MIN:
832                         c->value_int = index_substr_if_minlen;
833                         break;
834 #ifdef SLAPD_MODULES
835                 case CFG_MODLOAD: {
836                         ModPaths *mp = c->private;
837                         if (mp->mp_loads) {
838                                 int i;
839                                 for (i=0; !BER_BVISNULL(&mp->mp_loads[i]); i++) {
840                                         struct berval bv;
841                                         bv.bv_val = c->log;
842                                         bv.bv_len = sprintf( bv.bv_val, IFMT "%s", i,
843                                                 mp->mp_loads[i].bv_val );
844                                         value_add_one( &c->rvalue_vals, &bv );
845                                 }
846                         }
847
848                         rc = c->rvalue_vals ? 0 : 1;
849                         }
850                         break;
851                 case CFG_MODPATH: {
852                         ModPaths *mp = c->private;
853                         value_add_one( &c->rvalue_vals, &mp->mp_path );
854
855                         rc = c->rvalue_vals ? 0 : 1;
856                         }
857                         break;
858 #endif
859 #ifdef LDAP_SLAPI
860                 case CFG_PLUGIN:
861                         slapi_int_plugin_unparse( c->be, &c->rvalue_vals );
862                         if ( !c->rvalue_vals ) rc = 1;
863                         break;
864 #endif
865 #ifdef SLAP_AUTH_REWRITE
866                 case CFG_REWRITE:
867                         if ( authz_rewrites ) {
868                                 struct berval bv, idx;
869                                 char ibuf[32];
870                                 int i;
871
872                                 idx.bv_val = ibuf;
873                                 for ( i=0; !BER_BVISNULL( &authz_rewrites[i] ); i++ ) {
874                                         idx.bv_len = sprintf( idx.bv_val, IFMT, i );
875                                         bv.bv_len = idx.bv_len + authz_rewrites[i].bv_len;
876                                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
877                                         strcpy( bv.bv_val, idx.bv_val );
878                                         strcpy( bv.bv_val+idx.bv_len, authz_rewrites[i].bv_val );
879                                         ber_bvarray_add( &c->rvalue_vals, &bv );
880                                 }
881                         }
882                         if ( !c->rvalue_vals ) rc = 1;
883                         break;
884 #endif
885                 default:
886                         rc = 1;
887                 }
888                 return rc;
889         } else if ( c->op == LDAP_MOD_DELETE ) {
890                 int rc = 0;
891                 switch(c->type) {
892                 /* single-valued attrs, no-ops */
893                 case CFG_CONCUR:
894                 case CFG_THREADS:
895                 case CFG_RO:
896                 case CFG_AZPOLICY:
897                 case CFG_DEPTH:
898                 case CFG_CHECK:
899                 case CFG_LASTMOD:
900                 case CFG_SASLSECP:
901                 case CFG_SSTR_IF_MAX:
902                 case CFG_SSTR_IF_MIN:
903                         break;
904
905                 /* no-ops, requires slapd restart */
906                 case CFG_PLUGIN:
907                 case CFG_MODLOAD:
908                 case CFG_AZREGEXP:
909                 case CFG_REWRITE:
910                         sprintf(c->log, "change requires slapd restart");
911                         break;
912
913                 case CFG_SALT:
914                         ch_free( passwd_salt );
915                         passwd_salt = NULL;
916                         break;
917
918                 case CFG_REPLOG:
919                         ch_free( c->be->be_replogfile );
920                         c->be->be_replogfile = NULL;
921                         break;
922
923                 case CFG_LOGFILE:
924                         ch_free( logfileName );
925                         logfileName = NULL;
926                         break;
927
928                 case CFG_ACL:
929                         if ( c->valx < 0 ) {
930                                 AccessControl *end;
931                                 if ( c->be == frontendDB )
932                                         end = NULL;
933                                 else
934                                         end = frontendDB->be_acl;
935                                 acl_destroy( c->be->be_acl, end );
936                         } else {
937                                 AccessControl **prev, *a;
938                                 int i;
939                                 for (i=0, prev = &c->be->be_acl; i < c->valx;
940                                         i++ ) {
941                                         a = *prev;
942                                         prev = &a->acl_next;
943                                 }
944                                 a = *prev;
945                                 *prev = a->acl_next;
946                                 acl_free( a );
947                         }
948                         break;
949
950                 case CFG_LIMITS:
951                         /* FIXME: there is no limits_free function */
952                 case CFG_ATOPT:
953                         /* FIXME: there is no ad_option_free function */
954                 case CFG_ROOTDSE:
955                         /* FIXME: there is no way to remove attributes added by
956                                 a DSE file */
957                 case CFG_OID:
958                 case CFG_OC:
959                 case CFG_DIT:
960                 case CFG_ATTR:
961                 case CFG_MODPATH:
962                 default:
963                         rc = 1;
964                         break;
965                 }
966                 return rc;
967         }
968
969         p = strchr(c->line,'(' /*')'*/);
970
971         switch(c->type) {
972                 case CFG_BACKEND:
973                         if(!(c->bi = backend_info(c->argv[1]))) {
974                                 sprintf( c->msg, "<%s> failed init", c->argv[0] );
975                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
976                                         c->log, c->msg, c->argv[1] );
977                                 return(1);
978                         }
979                         break;
980
981                 case CFG_DATABASE:
982                         c->bi = NULL;
983                         /* NOTE: config is always the first backend!
984                          */
985                         if ( !strcasecmp( c->argv[1], "config" )) {
986                                 c->be = LDAP_STAILQ_FIRST(&backendDB);
987                         } else if ( !strcasecmp( c->argv[1], "frontend" )) {
988                                 c->be = frontendDB;
989                         } else if(!(c->be = backend_db_init(c->argv[1]))) {
990                                 sprintf( c->msg, "<%s> failed init", c->argv[0] );
991                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
992                                         c->log, c->msg, c->argv[1] );
993                                 return(1);
994                         }
995                         break;
996
997                 case CFG_CONCUR:
998                         ldap_pvt_thread_set_concurrency(c->value_int);
999                         break;
1000
1001                 case CFG_THREADS:
1002                         ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
1003                         connection_pool_max = c->value_int;     /* save for reference */
1004                         break;
1005
1006                 case CFG_SALT:
1007                         if ( passwd_salt ) ch_free( passwd_salt );
1008                         passwd_salt = c->value_string;
1009                         lutil_salt_format(passwd_salt);
1010                         break;
1011
1012                 case CFG_LIMITS:
1013                         if(limits_parse(c->be, c->fname, c->lineno, c->argc, c->argv))
1014                                 return(1);
1015                         break;
1016
1017                 case CFG_RO:
1018                         if(c->value_int)
1019                                 c->be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
1020                         else
1021                                 c->be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
1022                         break;
1023
1024                 case CFG_AZPOLICY:
1025                         ch_free(c->value_string);
1026                         if (slap_sasl_setpolicy( c->argv[1] )) {
1027                                 sprintf( c->msg, "<%s> unable to parse value", c->argv[0] );
1028                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1029                                         c->log, c->msg, c->argv[1] );
1030                                 return(1);
1031                         }
1032                         break;
1033                 
1034                 case CFG_AZREGEXP:
1035                         if (slap_sasl_regexp_config( c->argv[1], c->argv[2] ))
1036                                 return(1);
1037                         break;
1038                                 
1039 #ifdef HAVE_CYRUS_SASL
1040                 case CFG_SASLSECP:
1041                         {
1042                         char *txt = slap_sasl_secprops( c->argv[1] );
1043                         if ( txt ) {
1044                                 snprintf( c->msg, sizeof(c->msg), "<%s> %s",
1045                                         c->argv[0], txt );
1046                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
1047                                 return(1);
1048                         }
1049                         break;
1050                         }
1051 #endif
1052
1053                 case CFG_DEPTH:
1054                         c->be->be_max_deref_depth = c->value_int;
1055                         break;
1056
1057                 case CFG_OID: {
1058                         OidMacro *om;
1059
1060                         if(parse_oidm(c->fname, c->lineno, c->argc, c->argv, 1, &om))
1061                                 return(1);
1062                         if (!cfn->c_om_head) cfn->c_om_head = om;
1063                         cfn->c_om_tail = om;
1064                         }
1065                         break;
1066
1067                 case CFG_OC: {
1068                         ObjectClass *oc;
1069
1070                         if(parse_oc(c->fname, c->lineno, p, c->argv, &oc)) return(1);
1071                         if (!cfn->c_oc_head) cfn->c_oc_head = oc;
1072                         cfn->c_oc_tail = oc;
1073                         }
1074                         break;
1075
1076                 case CFG_DIT: {
1077                         ContentRule *cr;
1078
1079                         if(parse_cr(c->fname, c->lineno, p, c->argv, &cr)) return(1);
1080                         if (!cfn->c_cr_head) cfn->c_cr_head = cr;
1081                         cfn->c_cr_tail = cr;
1082                         }
1083                         break;
1084
1085                 case CFG_ATTR: {
1086                         AttributeType *at;
1087
1088                         if(parse_at(c->fname, c->lineno, p, c->argv, &at)) return(1);
1089                         if (!cfn->c_at_head) cfn->c_at_head = at;
1090                         cfn->c_at_tail = at;
1091                         }
1092                         break;
1093
1094                 case CFG_ATOPT:
1095                         ad_define_option(NULL, NULL, 0);
1096                         for(i = 1; i < c->argc; i++)
1097                                 if(ad_define_option(c->argv[i], c->fname, c->lineno))
1098                                         return(1);
1099                         break;
1100
1101                 case CFG_CHECK:
1102                         global_schemacheck = c->value_int;
1103                         if(!global_schemacheck) Debug(LDAP_DEBUG_ANY, "%s: "
1104                                 "schema checking disabled! your mileage may vary!\n",
1105                                 c->log, 0, 0);
1106                         break;
1107
1108                 case CFG_ACL:
1109                         parse_acl(c->be, c->fname, c->lineno, c->argc, c->argv, c->valx);
1110                         break;
1111
1112                 case CFG_REPLOG:
1113                         if(SLAP_MONITOR(c->be)) {
1114                                 Debug(LDAP_DEBUG_ANY, "%s: "
1115                                         "\"replogfile\" should not be used "
1116                                         "inside monitor database\n",
1117                                         c->log, 0, 0);
1118                                 return(0);      /* FIXME: should this be an error? */
1119                         }
1120
1121                         c->be->be_replogfile = c->value_string;
1122                         break;
1123
1124                 case CFG_ROOTDSE:
1125                         if(read_root_dse_file(c->argv[1])) {
1126                                 sprintf( c->msg, "<%s> could not read file", c->argv[0] );
1127                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1128                                         c->log, c->msg, c->argv[1] );
1129                                 return(1);
1130                         }
1131                         {
1132                                 struct berval bv;
1133                                 ber_str2bv( c->argv[1], 0, 1, &bv );
1134                                 ber_bvarray_add( &cfn->c_dseFiles, &bv );
1135                         }
1136                         break;
1137
1138                 case CFG_LOGFILE: {
1139                                 FILE *logfile;
1140                                 if ( logfileName ) ch_free( logfileName );
1141                                 logfileName = c->value_string;
1142                                 logfile = fopen(logfileName, "w");
1143                                 if(logfile) lutil_debug_file(logfile);
1144                         } break;
1145
1146                 case CFG_LASTMOD:
1147                         if(SLAP_NOLASTMODCMD(c->be)) {
1148                                 sprintf( c->msg, "<%s> not available for %s database",
1149                                         c->argv[0], c->be->bd_info->bi_type );
1150                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1151                                         c->log, c->msg, 0 );
1152                                 return(1);
1153                         }
1154                         if(c->value_int)
1155                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_NOLASTMOD;
1156                         else
1157                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NOLASTMOD;
1158                         break;
1159
1160                 case CFG_SSTR_IF_MAX:
1161                         if (c->value_int < index_substr_if_minlen) {
1162                                 sprintf( c->msg, "<%s> invalid value", c->argv[0] );
1163                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%d)\n",
1164                                         c->log, c->msg, c->value_int );
1165                                 return(1);
1166                         }
1167                         index_substr_if_maxlen = c->value_int;
1168                         break;
1169
1170                 case CFG_SSTR_IF_MIN:
1171                         if (c->value_int > index_substr_if_maxlen) {
1172                                 sprintf( c->msg, "<%s> invalid value", c->argv[0] );
1173                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%d)\n",
1174                                         c->log, c->msg, c->value_int );
1175                                 return(1);
1176                         }
1177                         index_substr_if_minlen = c->value_int;
1178                         break;
1179
1180 #ifdef SLAPD_MODULES
1181                 case CFG_MODLOAD:
1182                         if(module_load(c->argv[1], c->argc - 2, (c->argc > 2) ? c->argv + 2 : NULL))
1183                                 return(1);
1184                         /* Record this load on the current path */
1185                         {
1186                                 struct berval bv;
1187                                 ModPaths *mp;
1188                                 char *ptr = c->line + STRLENOF("moduleload");
1189                                 while (!isspace(*ptr)) ptr++;
1190                                 while (isspace(*ptr)) ptr++;
1191                                 ber_str2bv(ptr, 0, 1, &bv);
1192                                 if ( c->op == SLAP_CONFIG_ADD )
1193                                         mp = modcur;
1194                                 else
1195                                         mp = c->private;
1196                                 ber_bvarray_add( &mp->mp_loads, &bv );
1197                         }
1198                         break;
1199
1200                 case CFG_MODPATH:
1201                         if(module_path(c->argv[1])) return(1);
1202                         /* Record which path was used with each module */
1203                         {
1204                                 ModPaths *mp;
1205
1206                                 if (!modpaths.mp_loads) {
1207                                         mp = &modpaths;
1208                                 } else {
1209                                         mp = ch_malloc( sizeof( ModPaths ));
1210                                         modlast->mp_next = mp;
1211                                 }
1212                                 ber_str2bv(c->argv[1], 0, 1, &mp->mp_path);
1213                                 mp->mp_next = NULL;
1214                                 mp->mp_loads = NULL;
1215                                 modlast = mp;
1216                                 c->private = mp;
1217                                 if ( c->op == SLAP_CONFIG_ADD )
1218                                         modcur = mp;
1219                         }
1220                         
1221                         break;
1222 #endif
1223
1224 #ifdef LDAP_SLAPI
1225                 case CFG_PLUGIN:
1226                         if(slapi_int_read_config(c->be, c->fname, c->lineno, c->argc, c->argv) != LDAP_SUCCESS)
1227                                 return(1);
1228                         slapi_plugins_used++;
1229                         break;
1230 #endif
1231
1232 #ifdef SLAP_AUTH_REWRITE
1233                 case CFG_REWRITE: {
1234                         struct berval bv;
1235                         if(slap_sasl_rewrite_config(c->fname, c->lineno, c->argc, c->argv))
1236                                 return(1);
1237                         ber_str2bv( c->line, 0, 1, &bv );
1238                         ber_bvarray_add( &authz_rewrites, &bv );
1239                         }
1240                         break;
1241 #endif
1242
1243
1244                 default:
1245                         Debug(LDAP_DEBUG_ANY, "%s: unknown CFG_TYPE %d"
1246                                 "(ignored)\n", c->log, c->type, 0);
1247
1248         }
1249         return(0);
1250 }
1251
1252
1253 static int
1254 config_fname(ConfigArgs *c) {
1255         if(c->op == SLAP_CONFIG_EMIT) {
1256                 if (c->private) {
1257                         ConfigFile *cf = c->private;
1258                         value_add_one( &c->rvalue_vals, &cf->c_file );
1259                         return 0;
1260                 }
1261                 return 1;
1262         }
1263         return(0);
1264 }
1265
1266 static int
1267 config_cfdir(ConfigArgs *c) {
1268         if(c->op == SLAP_CONFIG_EMIT) {
1269                 if ( !BER_BVISEMPTY( &cfdir )) {
1270                         value_add_one( &c->rvalue_vals, &cfdir );
1271                         return 0;
1272                 }
1273                 return 1;
1274         }
1275         return(0);
1276 }
1277
1278 static int
1279 config_search_base(ConfigArgs *c) {
1280         struct berval dn;
1281
1282         if(c->op == SLAP_CONFIG_EMIT) {
1283                 int rc = 1;
1284                 if (!BER_BVISEMPTY(&default_search_base)) {
1285                         value_add_one(&c->rvalue_vals, &default_search_base);
1286                         value_add_one(&c->rvalue_nvals, &default_search_nbase);
1287                         rc = 0;
1288                 }
1289                 return rc;
1290         } else if( c->op == LDAP_MOD_DELETE ) {
1291                 ch_free( default_search_base.bv_val );
1292                 ch_free( default_search_nbase.bv_val );
1293                 BER_BVZERO( &default_search_base );
1294                 BER_BVZERO( &default_search_nbase );
1295                 return 0;
1296         }
1297
1298         if(c->bi || c->be != frontendDB) {
1299                 Debug(LDAP_DEBUG_ANY, "%s: defaultSearchBase line must appear "
1300                         "prior to any backend or database definition\n",
1301                         c->log, 0, 0);
1302                 return(1);
1303         }
1304
1305         if(default_search_nbase.bv_len) {
1306                 free(default_search_base.bv_val);
1307                 free(default_search_nbase.bv_val);
1308         }
1309
1310         default_search_base = c->value_dn;
1311         default_search_nbase = c->value_ndn;
1312         return(0);
1313 }
1314
1315 static int
1316 config_passwd_hash(ConfigArgs *c) {
1317         int i;
1318         if (c->op == SLAP_CONFIG_EMIT) {
1319                 struct berval bv;
1320                 for (i=0; default_passwd_hash && default_passwd_hash[i]; i++) {
1321                         ber_str2bv(default_passwd_hash[i], 0, 0, &bv);
1322                         value_add_one(&c->rvalue_vals, &bv);
1323                 }
1324                 return i ? 0 : 1;
1325         } else if ( c->op == LDAP_MOD_DELETE ) {
1326                 if ( c->valx < 0 ) {
1327                         ldap_charray_free( default_passwd_hash );
1328                         default_passwd_hash = NULL;
1329                 } else {
1330                         i = c->valx;
1331                         ch_free( default_passwd_hash[i] );
1332                         for (; default_passwd_hash[i]; i++ )
1333                                 default_passwd_hash[i] = default_passwd_hash[i+1];
1334                 }
1335                 return 0;
1336         }
1337         if(default_passwd_hash) {
1338                 Debug(LDAP_DEBUG_ANY, "%s: "
1339                         "already set default password_hash\n",
1340                         c->log, 0, 0);
1341                 return(1);
1342         }
1343         for(i = 1; i < c->argc; i++) {
1344                 if(!lutil_passwd_scheme(c->argv[i])) {
1345                         sprintf( c->msg, "<%s> schema not available", c->argv[0] );
1346                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1347                                 c->log, c->msg, c->argv[i]);
1348                 } else {
1349                         ldap_charray_add(&default_passwd_hash, c->argv[i]);
1350                 }
1351                 if(!default_passwd_hash) {
1352                         sprintf( c->msg, "<%s> no valid hashes found", c->argv[0] );
1353                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1354                                 c->log, c->msg, 0 );
1355                         return(1);
1356                 }
1357         }
1358         return(0);
1359 }
1360
1361 static int
1362 config_schema_dn(ConfigArgs *c) {
1363         if ( c->op == SLAP_CONFIG_EMIT ) {
1364                 int rc = 1;
1365                 if ( !BER_BVISEMPTY( &c->be->be_schemadn )) {
1366                         value_add_one(&c->rvalue_vals, &c->be->be_schemadn);
1367                         value_add_one(&c->rvalue_nvals, &c->be->be_schemandn);
1368                         rc = 0;
1369                 }
1370                 return rc;
1371         } else if ( c->op == LDAP_MOD_DELETE ) {
1372                 ch_free( c->be->be_schemadn.bv_val );
1373                 ch_free( c->be->be_schemandn.bv_val );
1374                 BER_BVZERO( &c->be->be_schemadn );
1375                 BER_BVZERO( &c->be->be_schemandn );
1376                 return 0;
1377         }
1378         ch_free( c->be->be_schemadn.bv_val );
1379         ch_free( c->be->be_schemandn.bv_val );
1380         c->be->be_schemadn = c->value_dn;
1381         c->be->be_schemandn = c->value_ndn;
1382         return(0);
1383 }
1384
1385 static int
1386 config_sizelimit(ConfigArgs *c) {
1387         int i, rc = 0;
1388         char *next;
1389         struct slap_limits_set *lim = &c->be->be_def_limit;
1390         if (c->op == SLAP_CONFIG_EMIT) {
1391                 char buf[8192];
1392                 struct berval bv;
1393                 bv.bv_val = buf;
1394                 bv.bv_len = 0;
1395                 limits_unparse_one( lim, SLAP_LIMIT_SIZE, &bv );
1396                 if ( !BER_BVISEMPTY( &bv ))
1397                         value_add_one( &c->rvalue_vals, &bv );
1398                 else
1399                         rc = 1;
1400                 return rc;
1401         } else if ( c->op == LDAP_MOD_DELETE ) {
1402                 /* Reset to defaults */
1403                 lim->lms_s_soft = SLAPD_DEFAULT_SIZELIMIT;
1404                 lim->lms_s_hard = 0;
1405                 lim->lms_s_unchecked = -1;
1406                 lim->lms_s_pr = 0;
1407                 lim->lms_s_pr_hide = 0;
1408                 lim->lms_s_pr_total = 0;
1409                 return 0;
1410         }
1411         for(i = 1; i < c->argc; i++) {
1412                 if(!strncasecmp(c->argv[i], "size", 4)) {
1413                         rc = limits_parse_one(c->argv[i], lim);
1414                         if ( rc ) {
1415                                 sprintf( c->msg, "<%s> unable to parse value", c->argv[0] );
1416                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1417                                         c->log, c->msg, c->argv[i]);
1418                                 return(1);
1419                         }
1420                 } else {
1421                         if(!strcasecmp(c->argv[i], "unlimited")) {
1422                                 lim->lms_s_soft = -1;
1423                         } else {
1424                                 lim->lms_s_soft = strtol(c->argv[i], &next, 0);
1425                                 if(next == c->argv[i]) {
1426                                         sprintf( c->msg, "<%s> unable to parse limit", c->argv[0]);
1427                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1428                                                 c->log, c->msg, c->argv[i]);
1429                                         return(1);
1430                                 } else if(next[0] != '\0') {
1431                                         Debug(LDAP_DEBUG_ANY, "%s: "
1432                                                 "trailing chars \"%s\" in \"sizelimit <limit>\" line (ignored)\n",
1433                                                 c->log, next, 0);
1434                                 }
1435                         }
1436                         lim->lms_s_hard = 0;
1437                 }
1438         }
1439         return(0);
1440 }
1441
1442 static int
1443 config_timelimit(ConfigArgs *c) {
1444         int i, rc = 0;
1445         char *next;
1446         struct slap_limits_set *lim = &c->be->be_def_limit;
1447         if (c->op == SLAP_CONFIG_EMIT) {
1448                 char buf[8192];
1449                 struct berval bv;
1450                 bv.bv_val = buf;
1451                 bv.bv_len = 0;
1452                 limits_unparse_one( lim, SLAP_LIMIT_TIME, &bv );
1453                 if ( !BER_BVISEMPTY( &bv ))
1454                         value_add_one( &c->rvalue_vals, &bv );
1455                 else
1456                         rc = 1;
1457                 return rc;
1458         } else if ( c->op == LDAP_MOD_DELETE ) {
1459                 /* Reset to defaults */
1460                 lim->lms_t_soft = SLAPD_DEFAULT_TIMELIMIT;
1461                 lim->lms_t_hard = 0;
1462                 return 0;
1463         }
1464         for(i = 1; i < c->argc; i++) {
1465                 if(!strncasecmp(c->argv[i], "time", 4)) {
1466                         rc = limits_parse_one(c->argv[i], lim);
1467                         if ( rc ) {
1468                                 sprintf( c->msg, "<%s> unable to parse value", c->argv[0] );
1469                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1470                                         c->log, c->msg, c->argv[i]);
1471                                 return(1);
1472                         }
1473                 } else {
1474                         if(!strcasecmp(c->argv[i], "unlimited")) {
1475                                 lim->lms_t_soft = -1;
1476                         } else {
1477                                 lim->lms_t_soft = strtol(c->argv[i], &next, 0);
1478                                 if(next == c->argv[i]) {
1479                                         sprintf( c->msg, "<%s> unable to parse limit", c->argv[0]);
1480                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1481                                                 c->log, c->msg, c->argv[i]);
1482                                         return(1);
1483                                 } else if(next[0] != '\0') {
1484                                         Debug(LDAP_DEBUG_ANY, "%s: "
1485                                                 "trailing chars \"%s\" in \"timelimit <limit>\" line (ignored)\n",
1486                                                 c->log, next, 0);
1487                                 }
1488                         }
1489                         lim->lms_t_hard = 0;
1490                 }
1491         }
1492         return(0);
1493 }
1494
1495 static int
1496 config_overlay(ConfigArgs *c) {
1497         if (c->op == SLAP_CONFIG_EMIT) {
1498                 return 1;
1499         } else if ( c->op == LDAP_MOD_DELETE ) {
1500                 assert(0);
1501         }
1502         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1])) {
1503                 /* log error */
1504                 Debug(LDAP_DEBUG_ANY, "%s: (optional) %s overlay \"%s\" configuration failed (ignored)\n",
1505                         c->log, c->be == frontendDB ? "global " : "", c->argv[1][1]);
1506         } else if(overlay_config(c->be, c->argv[1])) {
1507                 return(1);
1508         }
1509         return(0);
1510 }
1511
1512 static int
1513 config_suffix(ConfigArgs *c) {
1514         Backend *tbe;
1515         struct berval pdn, ndn;
1516         int rc;
1517
1518         if (c->be == frontendDB || SLAP_MONITOR(c->be) ||
1519                 SLAP_CONFIG(c->be)) return 1;
1520
1521         if (c->op == SLAP_CONFIG_EMIT) {
1522                 if ( c->be->be_suffix == NULL
1523                                 || BER_BVISNULL( &c->be->be_suffix[0] ) )
1524                 {
1525                         return 1;
1526                 } else {
1527                         value_add( &c->rvalue_vals, c->be->be_suffix );
1528                         value_add( &c->rvalue_nvals, c->be->be_nsuffix );
1529                         return 0;
1530                 }
1531         } else if ( c->op == LDAP_MOD_DELETE ) {
1532                 if ( c->valx < 0 ) {
1533                         ber_bvarray_free( c->be->be_suffix );
1534                         ber_bvarray_free( c->be->be_nsuffix );
1535                         c->be->be_suffix = NULL;
1536                         c->be->be_nsuffix = NULL;
1537                 } else {
1538                         int i = c->valx;
1539                         ch_free( c->be->be_suffix[i].bv_val );
1540                         ch_free( c->be->be_nsuffix[i].bv_val );
1541                         for (; c->be->be_suffix[i].bv_val; i++) {
1542                                 c->be->be_suffix[i] = c->be->be_suffix[i+1];
1543                                 c->be->be_nsuffix[i] = c->be->be_nsuffix[i+1];
1544                         }
1545                 }
1546                 return 0;
1547         }
1548 #ifdef SLAPD_MONITOR_DN
1549         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
1550                 sprintf( c->msg, "<%s> DN is reserved for monitoring slapd",
1551                         c->argv[0] );
1552                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1553                         c->log, c->msg, SLAPD_MONITOR_DN);
1554                 return(1);
1555         }
1556 #endif
1557
1558         pdn = c->value_dn;
1559         ndn = c->value_ndn;
1560         tbe = select_backend(&ndn, 0, 0);
1561         if(tbe == c->be) {
1562                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by this backend! (ignored)\n",
1563                         c->log, 0, 0);
1564                 free(pdn.bv_val);
1565                 free(ndn.bv_val);
1566         } else if(tbe) {
1567                 sprintf( c->msg, "<%s> suffix already served by a preceding backend",
1568                         c->argv[0] );
1569                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1570                         c->log, c->msg, tbe->be_suffix[0].bv_val);
1571                 free(pdn.bv_val);
1572                 free(ndn.bv_val);
1573                 return(1);
1574         } else if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
1575                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
1576                         "base provided \"%s\" (assuming okay)\n",
1577                         c->log, default_search_base.bv_val, 0);
1578         }
1579         ber_bvarray_add(&c->be->be_suffix, &pdn);
1580         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
1581         return(0);
1582 }
1583
1584 static int
1585 config_rootdn(ConfigArgs *c) {
1586         if (c->op == SLAP_CONFIG_EMIT) {
1587                 if ( !BER_BVISNULL( &c->be->be_rootdn )) {
1588                         value_add_one(&c->rvalue_vals, &c->be->be_rootdn);
1589                         value_add_one(&c->rvalue_nvals, &c->be->be_rootndn);
1590                         return 0;
1591                 } else {
1592                         return 1;
1593                 }
1594         } else if ( c->op == LDAP_MOD_DELETE ) {
1595                 ch_free( c->be->be_rootdn.bv_val );
1596                 ch_free( c->be->be_rootndn.bv_val );
1597                 BER_BVZERO( &c->be->be_rootdn );
1598                 BER_BVZERO( &c->be->be_rootndn );
1599                 return 0;
1600         }
1601         if ( !BER_BVISNULL( &c->be->be_rootdn )) {
1602                 ch_free( c->be->be_rootdn.bv_val );
1603                 ch_free( c->be->be_rootndn.bv_val );
1604         }
1605         c->be->be_rootdn = c->value_dn;
1606         c->be->be_rootndn = c->value_ndn;
1607         return(0);
1608 }
1609
1610 static int
1611 config_rootpw(ConfigArgs *c) {
1612         Backend *tbe;
1613         if (c->op == SLAP_CONFIG_EMIT) {
1614                 if (!BER_BVISEMPTY(&c->be->be_rootpw)) {
1615                         ber_dupbv( &c->value_bv, &c->be->be_rootpw);
1616                         return 0;
1617                 }
1618                 return 1;
1619         } else if ( c->op == LDAP_MOD_DELETE ) {
1620                 ch_free( c->be->be_rootpw.bv_val );
1621                 BER_BVZERO( &c->be->be_rootpw );
1622                 return 0;
1623         }
1624
1625         tbe = select_backend(&c->be->be_rootndn, 0, 0);
1626         if(tbe != c->be) {
1627                 sprintf( c->msg, "<%s> can only be set when rootdn is under suffix",
1628                         c->argv[0] );
1629                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1630                         c->log, c->msg, 0);
1631                 return(1);
1632         }
1633         if ( !BER_BVISNULL( &c->be->be_rootpw ))
1634                 ch_free( c->be->be_rootpw.bv_val );
1635         c->be->be_rootpw = c->value_bv;
1636         return(0);
1637 }
1638
1639 static int
1640 config_restrict(ConfigArgs *c) {
1641         slap_mask_t restrictops = 0;
1642         int i;
1643         slap_verbmasks restrictable_ops[] = {
1644                 { BER_BVC("bind"),              SLAP_RESTRICT_OP_BIND },
1645                 { BER_BVC("add"),               SLAP_RESTRICT_OP_ADD },
1646                 { BER_BVC("modify"),            SLAP_RESTRICT_OP_MODIFY },
1647                 { BER_BVC("rename"),            SLAP_RESTRICT_OP_RENAME },
1648                 { BER_BVC("modrdn"),            0 },
1649                 { BER_BVC("delete"),            SLAP_RESTRICT_OP_DELETE },
1650                 { BER_BVC("search"),            SLAP_RESTRICT_OP_SEARCH },
1651                 { BER_BVC("compare"),   SLAP_RESTRICT_OP_COMPARE },
1652                 { BER_BVC("read"),              SLAP_RESTRICT_OP_READS },
1653                 { BER_BVC("write"),             SLAP_RESTRICT_OP_WRITES },
1654                 { BER_BVC("extended"),  SLAP_RESTRICT_OP_EXTENDED },
1655                 { BER_BVC("extended=" LDAP_EXOP_START_TLS ),            SLAP_RESTRICT_EXOP_START_TLS },
1656                 { BER_BVC("extended=" LDAP_EXOP_MODIFY_PASSWD ),        SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
1657                 { BER_BVC("extended=" LDAP_EXOP_X_WHO_AM_I ),           SLAP_RESTRICT_EXOP_WHOAMI },
1658                 { BER_BVC("extended=" LDAP_EXOP_X_CANCEL ),             SLAP_RESTRICT_EXOP_CANCEL },
1659                 { BER_BVNULL,   0 }
1660         };
1661
1662         if (c->op == SLAP_CONFIG_EMIT) {
1663                 return mask_to_verbs( restrictable_ops, c->be->be_restrictops,
1664                         &c->rvalue_vals );
1665         } else if ( c->op == LDAP_MOD_DELETE ) {
1666                 if ( !c->line ) {
1667                         c->be->be_restrictops = 0;
1668                 } else {
1669                         restrictops = verb_to_mask( c->line, restrictable_ops );
1670                         c->be->be_restrictops ^= restrictops;
1671                 }
1672                 return 0;
1673         }
1674         i = verbs_to_mask( c->argc, c->argv, restrictable_ops, &restrictops );
1675         if ( i ) {
1676                 sprintf( c->msg, "<%s> unknown operation", c->argv[0] );
1677                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1678                         c->log, c->msg, c->argv[i]);
1679                 return(1);
1680         }
1681         if ( restrictops & SLAP_RESTRICT_OP_EXTENDED )
1682                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
1683         c->be->be_restrictops |= restrictops;
1684         return(0);
1685 }
1686
1687 static int
1688 config_allows(ConfigArgs *c) {
1689         slap_mask_t allows = 0;
1690         int i;
1691         slap_verbmasks allowable_ops[] = {
1692                 { BER_BVC("bind_v2"),           SLAP_ALLOW_BIND_V2 },
1693                 { BER_BVC("bind_anon_cred"),    SLAP_ALLOW_BIND_ANON_CRED },
1694                 { BER_BVC("bind_anon_dn"),      SLAP_ALLOW_BIND_ANON_DN },
1695                 { BER_BVC("update_anon"),       SLAP_ALLOW_UPDATE_ANON },
1696                 { BER_BVNULL,   0 }
1697         };
1698         if (c->op == SLAP_CONFIG_EMIT) {
1699                 return mask_to_verbs( allowable_ops, global_allows, &c->rvalue_vals );
1700         } else if ( c->op == LDAP_MOD_DELETE ) {
1701                 if ( !c->line ) {
1702                         global_allows = 0;
1703                 } else {
1704                         allows = verb_to_mask( c->line, allowable_ops );
1705                         global_allows ^= allows;
1706                 }
1707                 return 0;
1708         }
1709         i = verbs_to_mask(c->argc, c->argv, allowable_ops, &allows);
1710         if ( i ) {
1711                 sprintf( c->msg, "<%s> unknown feature", c->argv[0] );
1712                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1713                         c->log, c->msg, c->argv[i]);
1714                 return(1);
1715         }
1716         global_allows |= allows;
1717         return(0);
1718 }
1719
1720 static int
1721 config_disallows(ConfigArgs *c) {
1722         slap_mask_t disallows = 0;
1723         int i;
1724         slap_verbmasks disallowable_ops[] = {
1725                 { BER_BVC("bind_anon"),         SLAP_DISALLOW_BIND_ANON },
1726                 { BER_BVC("bind_simple"),       SLAP_DISALLOW_BIND_SIMPLE },
1727                 { BER_BVC("bind_krb4"),         SLAP_DISALLOW_BIND_KRBV4 },
1728                 { BER_BVC("tls_2_anon"),                SLAP_DISALLOW_TLS_2_ANON },
1729                 { BER_BVC("tls_authc"),         SLAP_DISALLOW_TLS_AUTHC },
1730                 { BER_BVNULL, 0 }
1731         };
1732         if (c->op == SLAP_CONFIG_EMIT) {
1733                 return mask_to_verbs( disallowable_ops, global_disallows, &c->rvalue_vals );
1734         } else if ( c->op == LDAP_MOD_DELETE ) {
1735                 if ( !c->line ) {
1736                         global_disallows = 0;
1737                 } else {
1738                         disallows = verb_to_mask( c->line, disallowable_ops );
1739                         global_disallows ^= disallows;
1740                 }
1741                 return 0;
1742         }
1743         i = verbs_to_mask(c->argc, c->argv, disallowable_ops, &disallows);
1744         if ( i ) {
1745                 sprintf( c->msg, "<%s> unknown feature", c->argv[0] );
1746                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1747                         c->log, c->msg, c->argv[i]);
1748                 return(1);
1749         }
1750         global_disallows |= disallows;
1751         return(0);
1752 }
1753
1754 static int
1755 config_requires(ConfigArgs *c) {
1756         slap_mask_t requires = 0;
1757         int i;
1758         slap_verbmasks requires_ops[] = {
1759                 { BER_BVC("bind"),              SLAP_REQUIRE_BIND },
1760                 { BER_BVC("LDAPv3"),            SLAP_REQUIRE_LDAP_V3 },
1761                 { BER_BVC("authc"),             SLAP_REQUIRE_AUTHC },
1762                 { BER_BVC("sasl"),              SLAP_REQUIRE_SASL },
1763                 { BER_BVC("strong"),            SLAP_REQUIRE_STRONG },
1764                 { BER_BVNULL, 0 }
1765         };
1766         if (c->op == SLAP_CONFIG_EMIT) {
1767                 return mask_to_verbs( requires_ops, c->be->be_requires, &c->rvalue_vals );
1768         } else if ( c->op == LDAP_MOD_DELETE ) {
1769                 if ( !c->line ) {
1770                         c->be->be_requires = 0;
1771                 } else {
1772                         requires = verb_to_mask( c->line, requires_ops );
1773                         c->be->be_requires ^= requires;
1774                 }
1775                 return 0;
1776         }
1777         i = verbs_to_mask(c->argc, c->argv, requires_ops, &requires);
1778         if ( i ) {
1779                 sprintf( c->msg, "<%s> unknown feature", c->argv[0] );
1780                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1781                         c->log, c->msg, c->argv[i]);
1782                 return(1);
1783         }
1784         c->be->be_requires = requires;
1785         return(0);
1786 }
1787
1788 static int
1789 config_loglevel(ConfigArgs *c) {
1790         int i;
1791         char *next;
1792         slap_verbmasks loglevel_ops[] = {
1793                 { BER_BVC("Trace"),     LDAP_DEBUG_TRACE },
1794                 { BER_BVC("Packets"),   LDAP_DEBUG_PACKETS },
1795                 { BER_BVC("Args"),      LDAP_DEBUG_ARGS },
1796                 { BER_BVC("Conns"),     LDAP_DEBUG_CONNS },
1797                 { BER_BVC("BER"),       LDAP_DEBUG_BER },
1798                 { BER_BVC("Filter"),    LDAP_DEBUG_FILTER },
1799                 { BER_BVC("Config"),    LDAP_DEBUG_CONFIG },
1800                 { BER_BVC("ACL"),       LDAP_DEBUG_ACL },
1801                 { BER_BVC("Stats"),     LDAP_DEBUG_STATS },
1802                 { BER_BVC("Stats2"),    LDAP_DEBUG_STATS2 },
1803                 { BER_BVC("Shell"),     LDAP_DEBUG_SHELL },
1804                 { BER_BVC("Parse"),     LDAP_DEBUG_PARSE },
1805                 { BER_BVC("Cache"),     LDAP_DEBUG_CACHE },
1806                 { BER_BVC("Index"),     LDAP_DEBUG_INDEX },
1807                 { BER_BVC("Any"),       -1 },
1808                 { BER_BVNULL,   0 }
1809         };
1810
1811         if (c->op == SLAP_CONFIG_EMIT) {
1812                 return mask_to_verbs( loglevel_ops, ldap_syslog, &c->rvalue_vals );
1813         } else if ( c->op == LDAP_MOD_DELETE ) {
1814                 if ( !c->line ) {
1815                         ldap_syslog = 0;
1816                 } else {
1817                         int level = verb_to_mask( c->line, loglevel_ops );
1818                         ldap_syslog ^= level;
1819                 }
1820                 return 0;
1821         }
1822
1823         ldap_syslog = 0;
1824
1825         for( i=1; i < c->argc; i++ ) {
1826                 int     level;
1827
1828                 if ( isdigit( c->argv[i][0] ) ) {
1829                         level = strtol( c->argv[i], &next, 10 );
1830                         if ( next == NULL || next[0] != '\0' ) {
1831                                 sprintf( c->msg, "<%s> unable to parse level", c->argv[0] );
1832                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1833                                         c->log, c->msg, c->argv[i]);
1834                                 return( 1 );
1835                         }
1836                 } else {
1837                         int j = verb_to_mask(c->argv[i], loglevel_ops);
1838                         if(BER_BVISNULL(&loglevel_ops[j].word)) {
1839                                 sprintf( c->msg, "<%s> unknown level", c->argv[0] );
1840                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1841                                         c->log, c->msg, c->argv[i]);
1842                                 return( 1 );
1843                         }
1844                         level = loglevel_ops[j].mask;
1845                 }
1846                 ldap_syslog |= level;
1847         }
1848         return(0);
1849 }
1850
1851 static int
1852 config_referral(ConfigArgs *c) {
1853         struct berval val;
1854         if (c->op == SLAP_CONFIG_EMIT) {
1855                 if ( default_referral ) {
1856                         value_add( &c->rvalue_vals, default_referral );
1857                         return 0;
1858                 } else {
1859                         return 1;
1860                 }
1861         } else if ( c->op == LDAP_MOD_DELETE ) {
1862                 if ( c->valx < 0 ) {
1863                         ber_bvarray_free( default_referral );
1864                         default_referral = NULL;
1865                 } else {
1866                         int i = c->valx;
1867                         ch_free( default_referral[i].bv_val );
1868                         for (; default_referral[i].bv_val; i++ )
1869                                 default_referral[i] = default_referral[i+1];
1870                 }
1871                 return 0;
1872         }
1873         if(validate_global_referral(c->argv[1])) {
1874                 sprintf( c->msg, "<%s> invalid URL", c->argv[0] );
1875                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1876                         c->log, c->msg, c->argv[1]);
1877                 return(1);
1878         }
1879
1880         ber_str2bv(c->argv[1], 0, 0, &val);
1881         if(value_add_one(&default_referral, &val)) return(LDAP_OTHER);
1882         return(0);
1883 }
1884
1885 static struct {
1886         struct berval key;
1887         int off;
1888 } sec_keys[] = {
1889         { BER_BVC("ssf="), offsetof(slap_ssf_set_t, sss_ssf) },
1890         { BER_BVC("transport="), offsetof(slap_ssf_set_t, sss_transport) },
1891         { BER_BVC("tls="), offsetof(slap_ssf_set_t, sss_tls) },
1892         { BER_BVC("sasl="), offsetof(slap_ssf_set_t, sss_sasl) },
1893         { BER_BVC("update_ssf="), offsetof(slap_ssf_set_t, sss_update_ssf) },
1894         { BER_BVC("update_transport="), offsetof(slap_ssf_set_t, sss_update_transport) },
1895         { BER_BVC("update_tls="), offsetof(slap_ssf_set_t, sss_update_tls) },
1896         { BER_BVC("update_sasl="), offsetof(slap_ssf_set_t, sss_update_sasl) },
1897         { BER_BVC("simple_bind="), offsetof(slap_ssf_set_t, sss_simple_bind) },
1898         { BER_BVNULL, 0 }
1899 };
1900
1901 static int
1902 config_security(ConfigArgs *c) {
1903         slap_ssf_set_t *set = &c->be->be_ssf_set;
1904         char *next;
1905         int i, j;
1906         if (c->op == SLAP_CONFIG_EMIT) {
1907                 char numbuf[32];
1908                 struct berval bv;
1909                 slap_ssf_t *tgt;
1910                 int rc = 1;
1911
1912                 for (i=0; !BER_BVISNULL( &sec_keys[i].key ); i++) {
1913                         tgt = (slap_ssf_t *)((char *)set + sec_keys[i].off);
1914                         if ( *tgt ) {
1915                                 rc = 0;
1916                                 bv.bv_len = sprintf( numbuf, "%u", *tgt );
1917                                 bv.bv_len += sec_keys[i].key.bv_len;
1918                                 bv.bv_val = ch_malloc( bv.bv_len + 1);
1919                                 next = lutil_strcopy( bv.bv_val, sec_keys[i].key.bv_val );
1920                                 strcpy( next, numbuf );
1921                                 ber_bvarray_add( &c->rvalue_vals, &bv );
1922                         }
1923                 }
1924                 return rc;
1925         }
1926         for(i = 1; i < c->argc; i++) {
1927                 slap_ssf_t *tgt = NULL;
1928                 char *src;
1929                 for ( j=0; !BER_BVISNULL( &sec_keys[j].key ); j++ ) {
1930                         if(!strncasecmp(c->argv[i], sec_keys[j].key.bv_val,
1931                                 sec_keys[j].key.bv_len)) {
1932                                 src = c->argv[i] + sec_keys[j].key.bv_len;
1933                                 tgt = (slap_ssf_t *)((char *)set + sec_keys[j].off);
1934                                 break;
1935                         }
1936                 }
1937                 if ( !tgt ) {
1938                         sprintf( c->msg, "<%s> unknown factor", c->argv[0] );
1939                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1940                                 c->log, c->msg, c->argv[i]);
1941                         return(1);
1942                 }
1943
1944                 *tgt = strtol(src, &next, 10);
1945                 if(next == NULL || next[0] != '\0' ) {
1946                         sprintf( c->msg, "<%s> unable to parse factor", c->argv[0] );
1947                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1948                                 c->log, c->msg, c->argv[i]);
1949                         return(1);
1950                 }
1951         }
1952         return(0);
1953 }
1954
1955 char *
1956 anlist_unparse( AttributeName *an, char *ptr ) {
1957         int comma = 0;
1958
1959         for (; !BER_BVISNULL( &an->an_name ); an++) {
1960                 if ( comma ) *ptr++ = ',';
1961                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
1962                 comma = 1;
1963         }
1964         return ptr;
1965 }
1966
1967 static void
1968 replica_unparse( struct slap_replica_info *ri, int i, struct berval *bv )
1969 {
1970         int len;
1971         char *ptr;
1972         struct berval bc = {0};
1973         char numbuf[32];
1974
1975         len = sprintf(numbuf, IFMT, i );
1976
1977         len += strlen( ri->ri_uri ) + STRLENOF("uri=");
1978         if ( ri->ri_nsuffix ) {
1979                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
1980                         len += ri->ri_nsuffix[i].bv_len + STRLENOF(" suffix=\"\"");
1981                 }
1982         }
1983         if ( ri->ri_attrs ) {
1984                 len += STRLENOF("attr");
1985                 if ( ri->ri_exclude ) len++;
1986                 for (i=0; !BER_BVISNULL( &ri->ri_attrs[i].an_name ); i++) {
1987                         len += 1 + ri->ri_attrs[i].an_name.bv_len;
1988                 }
1989         }
1990         bindconf_unparse( &ri->ri_bindconf, &bc );
1991         len += bc.bv_len;
1992
1993         bv->bv_val = ch_malloc(len + 1);
1994         bv->bv_len = len;
1995
1996         ptr = lutil_strcopy( bv->bv_val, numbuf );
1997         ptr = lutil_strcopy( ptr, "uri=" );
1998         ptr = lutil_strcopy( ptr, ri->ri_uri );
1999
2000         if ( ri->ri_nsuffix ) {
2001                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
2002                         ptr = lutil_strcopy( ptr, " suffix=\"" );
2003                         ptr = lutil_strcopy( ptr, ri->ri_nsuffix[i].bv_val );
2004                         *ptr++ = '"';
2005                 }
2006         }
2007         if ( ri->ri_attrs ) {
2008                 ptr = lutil_strcopy( ptr, "attr" );
2009                 if ( ri->ri_exclude ) *ptr++ = '!';
2010                 *ptr++ = '=';
2011                 ptr = anlist_unparse( ri->ri_attrs, ptr );
2012         }
2013         if ( bc.bv_val ) {
2014                 strcpy( ptr, bc.bv_val );
2015                 ch_free( bc.bv_val );
2016         }
2017 }
2018
2019 static int
2020 config_replica(ConfigArgs *c) {
2021         int i, nr = -1, len;
2022         char *replicahost, *replicauri;
2023         LDAPURLDesc *ludp;
2024
2025         if (c->op == SLAP_CONFIG_EMIT) {
2026                 if (c->be->be_replica) {
2027                         struct berval bv;
2028                         for (i=0;c->be->be_replica[i]; i++) {
2029                                 replica_unparse( c->be->be_replica[i], i, &bv );
2030                                 ber_bvarray_add( &c->rvalue_vals, &bv );
2031                         }
2032                         return 0;
2033                 }
2034                 return 1;
2035         } else if ( c->op == LDAP_MOD_DELETE ) {
2036                 /* FIXME: there is no replica_free function */
2037                 if ( c->valx < 0 ) {
2038                 } else {
2039                 }
2040         }
2041         if(SLAP_MONITOR(c->be)) {
2042                 Debug(LDAP_DEBUG_ANY, "%s: "
2043                         "\"replica\" should not be used inside monitor database\n",
2044                         c->log, 0, 0);
2045                 return(0);      /* FIXME: should this be an error? */
2046         }
2047
2048         for(i = 1; i < c->argc; i++) {
2049                 if(!strncasecmp(c->argv[i], "host=", STRLENOF("host="))) {
2050                         replicahost = c->argv[i] + STRLENOF("host=");
2051                         len = strlen( replicahost );
2052                         replicauri = ch_malloc( len + STRLENOF("ldap://") + 1 );
2053                         sprintf( replicauri, "ldap://%s", replicahost );
2054                         replicahost = replicauri + STRLENOF( "ldap://");
2055                         nr = add_replica_info(c->be, replicauri, replicahost);
2056                         break;
2057                 } else if(!strncasecmp(c->argv[i], "uri=", STRLENOF("uri="))) {
2058                         if(ldap_url_parse(c->argv[i] + STRLENOF("uri="), &ludp) != LDAP_SUCCESS) {
2059                                 sprintf( c->msg, "<%s> invalid uri", c->argv[0] );
2060                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
2061                                 return(1);
2062                         }
2063                         if(!ludp->lud_host) {
2064                                 sprintf( c->msg, "<%s> invalid uri - missing hostname",
2065                                         c->argv[0] );
2066                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
2067                                 return(1);
2068                         }
2069                         ldap_free_urldesc(ludp);
2070                         replicauri = c->argv[i] + STRLENOF("uri=");
2071                         replicauri = ch_strdup( replicauri );
2072                         replicahost = strchr( replicauri, '/' );
2073                         replicahost += 2;
2074                         nr = add_replica_info(c->be, replicauri, replicahost);
2075                         break;
2076                 }
2077         }
2078         if(i == c->argc) {
2079                 sprintf( c->msg, "<%s> missing host or uri", c->argv[0] );
2080                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
2081                 return(1);
2082         } else if(nr == -1) {
2083                 sprintf( c->msg, "<%s> unable to add replica", c->argv[0] );
2084                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n", c->log, c->msg, replicauri );
2085                 return(1);
2086         } else {
2087                 for(i = 1; i < c->argc; i++) {
2088                         if(!strncasecmp(c->argv[i], "suffix=", STRLENOF( "suffix="))) {
2089                                 switch(add_replica_suffix(c->be, nr, c->argv[i] + STRLENOF("suffix="))) {
2090                                         case 1:
2091                                                 Debug(LDAP_DEBUG_ANY, "%s: "
2092                                                 "suffix \"%s\" in \"replica\" line is not valid for backend (ignored)\n",
2093                                                 c->log, c->argv[i] + STRLENOF("suffix="), 0);
2094                                                 break;
2095                                         case 2:
2096                                                 Debug(LDAP_DEBUG_ANY, "%s: "
2097                                                 "unable to normalize suffix in \"replica\" line (ignored)\n",
2098                                                 c->log, 0, 0);
2099                                                 break;
2100                                 }
2101
2102                         } else if(!strncasecmp(c->argv[i], "attr", STRLENOF("attr"))) {
2103                                 int exclude = 0;
2104                                 char *arg = c->argv[i] + STRLENOF("attr");
2105                                 if(arg[0] == '!') {
2106                                         arg++;
2107                                         exclude = 1;
2108                                 }
2109                                 if(arg[0] != '=') {
2110                                         continue;
2111                                 }
2112                                 if(add_replica_attrs(c->be, nr, arg + 1, exclude)) {
2113                                         sprintf( c->msg, "<%s> unknown attribute", c->argv[0] );
2114                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2115                                                 c->log, c->msg, arg + 1);
2116                                         return(1);
2117                                 }
2118                         } else if ( bindconf_parse( c->argv[i],
2119                                         &c->be->be_replica[nr]->ri_bindconf ) ) {
2120                                 return(1);
2121                         }
2122                 }
2123         }
2124         return(0);
2125 }
2126
2127 static int
2128 config_updatedn(ConfigArgs *c) {
2129         struct berval dn;
2130         int rc;
2131         if (c->op == SLAP_CONFIG_EMIT) {
2132                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
2133                         value_add_one(&c->rvalue_vals, &c->be->be_update_ndn);
2134                         value_add_one(&c->rvalue_nvals, &c->be->be_update_ndn);
2135                         return 0;
2136                 }
2137                 return 1;
2138         } else if ( c->op == LDAP_MOD_DELETE ) {
2139                 ch_free( c->be->be_update_ndn.bv_val );
2140                 c->be->be_update_ndn.bv_val = NULL;
2141                 SLAP_DBFLAGS(c->be) ^= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
2142                 return 0;
2143         }
2144         if(SLAP_SHADOW(c->be)) {
2145                 sprintf( c->msg, "<%s> database already shadowed", c->argv[0] );
2146                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2147                         c->log, c->msg, 0);
2148                 return(1);
2149         }
2150
2151         ber_str2bv(c->argv[1], 0, 0, &dn);
2152
2153         rc = dnNormalize(0, NULL, NULL, &dn, &c->be->be_update_ndn, NULL);
2154
2155         if(rc != LDAP_SUCCESS) {
2156                 sprintf( c->msg, "<%s> invalid DN %d (%s)", c->argv[0],
2157                         rc, ldap_err2string(rc));
2158                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2159                         c->log, c->msg, 0 );
2160                 return(1);
2161         }
2162
2163         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
2164         return(0);
2165 }
2166
2167 static int
2168 config_updateref(ConfigArgs *c) {
2169         struct berval val;
2170         if (c->op == SLAP_CONFIG_EMIT) {
2171                 if ( c->be->be_update_refs ) {
2172                         value_add( &c->rvalue_vals, c->be->be_update_refs );
2173                         return 0;
2174                 } else {
2175                         return 1;
2176                 }
2177         } else if ( c->op == LDAP_MOD_DELETE ) {
2178                 if ( c->valx < 0 ) {
2179                         ber_bvarray_free( c->be->be_update_refs );
2180                         c->be->be_update_refs = NULL;
2181                 } else {
2182                         int i = c->valx;
2183                         ch_free( c->be->be_update_refs[i].bv_val );
2184                         for (; c->be->be_update_refs[i].bv_val; i++)
2185                                 c->be->be_update_refs[i] = c->be->be_update_refs[i+1];
2186                 }
2187                 return 0;
2188         }
2189         if(!SLAP_SHADOW(c->be)) {
2190                 sprintf( c->msg, "<%s> must appear after syncrepl or updatedn",
2191                         c->argv[0] );
2192                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2193                         c->log, c->msg, 0);
2194                 return(1);
2195         }
2196
2197         if(validate_global_referral(c->argv[1])) {
2198                 sprintf( c->msg, "<%s> invalid URL", c->argv[0] );
2199                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2200                         c->log, c->msg, c->argv[1]);
2201                 return(1);
2202         }
2203         ber_str2bv(c->argv[1], 0, 0, &val);
2204         if(value_add_one(&c->be->be_update_refs, &val)) return(LDAP_OTHER);
2205         return(0);
2206 }
2207
2208 static int
2209 config_include(ConfigArgs *c) {
2210         unsigned long savelineno = c->lineno;
2211         int rc;
2212         ConfigFile *cf;
2213         ConfigFile *cfsave = cfn;
2214         ConfigFile *cf2 = NULL;
2215         if (c->op == SLAP_CONFIG_EMIT) {
2216                 if (c->private) {
2217                         ConfigFile *cf = c->private;
2218                         value_add_one( &c->rvalue_vals, &cf->c_file );
2219                         return 0;
2220                 }
2221                 return 1;
2222         } else if ( c->op == LDAP_MOD_DELETE ) {
2223         }
2224         cf = ch_calloc( 1, sizeof(ConfigFile));
2225         if ( cfn->c_kids ) {
2226                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
2227                 cf2->c_sibs = cf;
2228         } else {
2229                 cfn->c_kids = cf;
2230         }
2231         cfn = cf;
2232         ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
2233         rc = read_config_file(c->argv[1], c->depth + 1, c);
2234         c->lineno = savelineno - 1;
2235         cfn = cfsave;
2236         if ( rc ) {
2237                 if ( cf2 ) cf2->c_sibs = NULL;
2238                 else cfn->c_kids = NULL;
2239                 ch_free( cf->c_file.bv_val );
2240                 ch_free( cf );
2241         } else {
2242                 c->private = cf;
2243         }
2244         return(rc);
2245 }
2246
2247 #ifdef HAVE_TLS
2248 static int
2249 config_tls_option(ConfigArgs *c) {
2250         int flag;
2251         switch(c->type) {
2252         case CFG_TLS_RAND:      flag = LDAP_OPT_X_TLS_RANDOM_FILE;      break;
2253         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
2254         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
2255         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
2256         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
2257         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
2258         default:                Debug(LDAP_DEBUG_ANY, "%s: "
2259                                         "unknown tls_option <0x%x>\n",
2260                                         c->log, c->type, 0);
2261         }
2262         if (c->op == SLAP_CONFIG_EMIT) {
2263                 return ldap_pvt_tls_get_option( NULL, flag, &c->value_string );
2264         } else if ( c->op == LDAP_MOD_DELETE ) {
2265                 return ldap_pvt_tls_set_option( NULL, flag, NULL );
2266         }
2267         ch_free(c->value_string);
2268         return(ldap_pvt_tls_set_option(NULL, flag, c->argv[1]));
2269 }
2270
2271 /* FIXME: this ought to be provided by libldap */
2272 static int
2273 config_tls_config(ConfigArgs *c) {
2274         int i, flag;
2275         slap_verbmasks crlkeys[] = {
2276                 { BER_BVC("none"),      LDAP_OPT_X_TLS_CRL_NONE },
2277                 { BER_BVC("peer"),      LDAP_OPT_X_TLS_CRL_PEER },
2278                 { BER_BVC("all"),       LDAP_OPT_X_TLS_CRL_ALL },
2279                 { BER_BVNULL, 0 }
2280         };
2281         slap_verbmasks vfykeys[] = {
2282                 { BER_BVC("never"),     LDAP_OPT_X_TLS_NEVER },
2283                 { BER_BVC("demand"),    LDAP_OPT_X_TLS_DEMAND },
2284                 { BER_BVC("try"),       LDAP_OPT_X_TLS_TRY },
2285                 { BER_BVC("hard"),      LDAP_OPT_X_TLS_HARD },
2286                 { BER_BVNULL, 0 }
2287         }, *keys;
2288         switch(c->type) {
2289         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK;         keys = crlkeys; break;
2290         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT;     keys = vfykeys; break;
2291         default:
2292                 Debug(LDAP_DEBUG_ANY, "%s: "
2293                                 "unknown tls_option <0x%x>\n",
2294                                 c->log, c->type, 0);
2295         }
2296         if (c->op == SLAP_CONFIG_EMIT) {
2297                 ldap_pvt_tls_get_option( NULL, flag, &c->value_int );
2298                 for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
2299                         if (keys[i].mask == c->value_int) {
2300                                 c->value_string = ch_strdup( keys[i].word.bv_val );
2301                                 return 0;
2302                         }
2303                 }
2304                 return 1;
2305         } else if ( c->op == LDAP_MOD_DELETE ) {
2306                 int i = 0;
2307                 return ldap_pvt_tls_set_option( NULL, flag, &i );
2308         }
2309         ch_free( c->value_string );
2310         if(isdigit((unsigned char)c->argv[1][0])) {
2311                 i = atoi(c->argv[1]);
2312                 return(ldap_pvt_tls_set_option(NULL, flag, &i));
2313         } else {
2314                 return(ldap_int_tls_config(NULL, flag, c->argv[1]));
2315         }
2316 }
2317 #endif
2318
2319 static CfEntryInfo *
2320 config_find_base( CfEntryInfo *root, struct berval *dn, CfEntryInfo **last )
2321 {
2322         struct berval cdn;
2323         char *c;
2324
2325         if ( !root ) {
2326                 *last = NULL;
2327                 return NULL;
2328         }
2329
2330         if ( dn_match( &root->ce_entry->e_nname, dn ))
2331                 return root;
2332
2333         c = dn->bv_val+dn->bv_len;
2334         for (;*c != ',';c--);
2335
2336         while(root) {
2337                 *last = root;
2338                 for (--c;c>dn->bv_val && *c != ',';c--);
2339                 cdn.bv_val = c;
2340                 if ( *c == ',' )
2341                         cdn.bv_val++;
2342                 cdn.bv_len = dn->bv_len - (cdn.bv_val - dn->bv_val);
2343
2344                 root = root->ce_kids;
2345
2346                 for (;root;root=root->ce_sibs) {
2347                         if ( dn_match( &root->ce_entry->e_nname, &cdn )) {
2348                                 if ( cdn.bv_val == dn->bv_val ) {
2349                                         return root;
2350                                 }
2351                                 break;
2352                         }
2353                 }
2354         }
2355         return root;
2356 }
2357
2358 static int
2359 config_ldif_resp( Operation *op, SlapReply *rs )
2360 {
2361         if ( rs->sr_type == REP_SEARCH ) {
2362                 CfBackInfo *cfb = op->o_callback->sc_private;
2363
2364                 cfb->cb_got_ldif = 1;
2365                 rs->sr_err = config_add_internal( cfb, rs->sr_entry, NULL, NULL );
2366         }
2367         return rs->sr_err;
2368 }
2369
2370 /* Configure and read the underlying back-ldif store */
2371 static int
2372 config_setup_ldif( BackendDB *be, const char *dir, int readit ) {
2373         CfBackInfo *cfb = be->be_private;
2374         ConfigArgs c = {0};
2375         ConfigTable *ct;
2376         char *argv[3];
2377         int rc;
2378         slap_callback cb = { NULL, config_ldif_resp, NULL, NULL };
2379         Connection conn = {0};
2380         char opbuf[OPERATION_BUFFER_SIZE];
2381         Operation *op;
2382         SlapReply rs = {REP_RESULT};
2383         Filter filter = { LDAP_FILTER_PRESENT };
2384         struct berval filterstr = BER_BVC("(objectclass=*)");
2385         struct stat st;
2386
2387         /* Is the config directory available? */
2388         if ( stat( dir, &st ) < 0 ) {
2389                 /* No, so don't bother using the backing store.
2390                  * All changes will be in-memory only.
2391                  */
2392                 return 0;
2393         }
2394                 
2395         cfb->cb_db.bd_info = backend_info( "ldif" );
2396         if ( !cfb->cb_db.bd_info )
2397                 return 0;       /* FIXME: eventually this will be a fatal error */
2398
2399         if ( cfb->cb_db.bd_info->bi_db_init( &cfb->cb_db )) return 1;
2400
2401         /* Mark that back-ldif type is in use */
2402         cfb->cb_db.bd_info->bi_nDB++;
2403
2404         cfb->cb_db.be_suffix = be->be_suffix;
2405         cfb->cb_db.be_nsuffix = be->be_nsuffix;
2406         cfb->cb_db.be_rootdn = be->be_rootdn;
2407         cfb->cb_db.be_rootndn = be->be_rootndn;
2408
2409         ber_str2bv( dir, 0, 1, &cfdir );
2410
2411         c.be = &cfb->cb_db;
2412         c.fname = "slapd";
2413         c.argc = 2;
2414         argv[0] = "directory";
2415         argv[1] = (char *)dir;
2416         argv[2] = NULL;
2417         c.argv = argv;
2418
2419         ct = config_find_keyword( c.be->be_cf_table, &c );
2420         if ( !ct )
2421                 return 1;
2422
2423         if ( config_add_vals( ct, &c ))
2424                 return 1;
2425
2426         if ( backend_startup_one( &cfb->cb_db ))
2427                 return 1;
2428
2429         if ( readit ) {
2430                 op = (Operation *)opbuf;
2431                 connection_fake_init( &conn, op, cfb );
2432
2433                 filter.f_desc = slap_schema.si_ad_objectClass;
2434
2435                 op->o_tag = LDAP_REQ_SEARCH;
2436
2437                 op->ors_filter = &filter;
2438                 op->ors_filterstr = filterstr;
2439                 op->ors_scope = LDAP_SCOPE_SUBTREE;
2440
2441                 op->o_dn = be->be_rootdn;
2442                 op->o_ndn = be->be_rootndn;
2443
2444                 op->o_req_dn = be->be_suffix[0];
2445                 op->o_req_ndn = be->be_nsuffix[0];
2446
2447                 op->ors_tlimit = SLAP_NO_LIMIT;
2448                 op->ors_slimit = SLAP_NO_LIMIT;
2449
2450                 op->ors_attrs = slap_anlist_all_attributes;
2451                 op->ors_attrsonly = 0;
2452
2453                 op->o_callback = &cb;
2454                 cb.sc_private = cfb;
2455
2456                 op->o_bd = &cfb->cb_db;
2457                 op->o_bd->be_search( op, &rs );
2458         }
2459
2460         cfb->cb_use_ldif = 1;
2461
2462         return 0;
2463 }
2464
2465 static int
2466 CfOcInfo_cmp( const void *c1, const void *c2 ) {
2467         const CfOcInfo *co1 = c1;
2468         const CfOcInfo *co2 = c2;
2469
2470         return ber_bvcmp( co1->co_name, co2->co_name );
2471 }
2472
2473 int
2474 config_register_schema(ConfigTable *ct, ConfigOCs *ocs) {
2475         int i;
2476         CfOcInfo *co;
2477
2478         i = init_config_attrs( ct );
2479         if ( i ) return i;
2480
2481         /* set up the objectclasses */
2482         i = init_config_ocs( ocs );
2483         if ( i ) return i;
2484
2485         for (i=0; ocs[i].def; i++) {
2486                 if ( ocs[i].oc ) {
2487                         co = ch_malloc( sizeof(CfOcInfo) );
2488                         co->co_oc = *ocs[i].oc;
2489                         co->co_name = &co->co_oc->soc_cname;
2490                         co->co_table = ct;
2491                         co->co_type = ocs[i].cft;
2492                         avl_insert( &CfOcTree, co, CfOcInfo_cmp, avl_dup_error );
2493                 }
2494         }
2495         return 0;
2496 }
2497
2498 int
2499 read_config(const char *fname, const char *dir) {
2500         BackendDB *be;
2501         CfBackInfo *cfb;
2502
2503         /* Setup the config backend */
2504         be = backend_db_init( "config" );
2505         if ( !be )
2506                 return 1;
2507
2508         cfb = be->be_private;
2509
2510         /* If no .conf, or a dir was specified, setup the dir */
2511         if ( !fname || dir ) {
2512                 if ( dir ) {
2513                         /* If explicitly given, check for existence */
2514                         struct stat st;
2515
2516                         if ( stat( dir, &st ) < 0 ) {
2517                                 Debug( LDAP_DEBUG_ANY,
2518                                         "invalid config directory %s, error %d\n",
2519                                                 dir, errno, 0 );
2520                                 return 1;
2521                         }
2522                 } else {
2523                         dir = SLAPD_DEFAULT_CONFIGDIR;
2524                 }
2525                 /* if fname is defaulted, try reading .d */
2526                 if ( config_setup_ldif( be, dir, !fname ))
2527                         return 1;
2528
2529                 /* If we read the config from back-ldif, nothing to do here */
2530                 if ( cfb->cb_got_ldif )
2531                         return 0;
2532         }
2533
2534         if ( !fname )
2535                 fname = SLAPD_DEFAULT_CONFIGFILE;
2536         ber_str2bv( fname, 0, 1, &cf_prv.c_file );
2537
2538         return read_config_file(fname, 0, NULL);
2539 }
2540
2541 static int
2542 config_back_bind( Operation *op, SlapReply *rs )
2543 {
2544         if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op )) {
2545                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ));
2546                 /* frontend sends result */
2547                 return LDAP_SUCCESS;
2548         }
2549
2550         rs->sr_err = LDAP_INVALID_CREDENTIALS;
2551         send_ldap_result( op, rs );
2552
2553         return rs->sr_err;
2554 }
2555
2556 static int
2557 config_send( Operation *op, SlapReply *rs, CfEntryInfo *ce, int depth )
2558 {
2559         int rc = 0;
2560
2561         if ( test_filter( op, ce->ce_entry, op->ors_filter ) == LDAP_COMPARE_TRUE )
2562         {
2563                 rs->sr_attrs = op->ors_attrs;
2564                 rs->sr_entry = ce->ce_entry;
2565                 rc = send_search_entry( op, rs );
2566         }
2567         if ( op->ors_scope == LDAP_SCOPE_SUBTREE ) {
2568                 if ( ce->ce_kids ) {
2569                         rc = config_send( op, rs, ce->ce_kids, 1 );
2570                         if ( rc ) return rc;
2571                 }
2572                 if ( depth ) {
2573                         for (ce=ce->ce_sibs; ce; ce=ce->ce_sibs) {
2574                                 rc = config_send( op, rs, ce, 0 );
2575                                 if ( rc ) break;
2576                         }
2577                 }
2578         }
2579         return rc;
2580 }
2581
2582 static ConfigTable *
2583 config_find_table( CfOcInfo **colst, int nocs, AttributeDescription *ad )
2584 {
2585         int i, j;
2586
2587         for (j=0; j<nocs; j++) {
2588                 for (i=0; colst[j]->co_table[i].name; i++)
2589                         if ( colst[j]->co_table[i].ad == ad )
2590                                 return &colst[j]->co_table[i];
2591         }
2592         return NULL;
2593 }
2594
2595 /* Sort the attributes of the entry according to the order defined
2596  * in the objectclass, with required attributes occurring before
2597  * allowed attributes. For any attributes with sequencing dependencies
2598  * (e.g., rootDN must be defined after suffix) the objectclass must
2599  * list the attributes in the desired sequence.
2600  */
2601 static void
2602 sort_attrs( Entry *e, CfOcInfo **colst, int nocs )
2603 {
2604         Attribute *a, *head = NULL, *tail = NULL, **prev;
2605         int i, j;
2606
2607         for (i=0; i<nocs; i++) {
2608                 if ( colst[i]->co_oc->soc_required ) {
2609                         AttributeType **at = colst[i]->co_oc->soc_required;
2610                         for (j=0; at[j]; j++) {
2611                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
2612                                         prev = &(*prev)->a_next, a=a->a_next) {
2613                                         if ( a->a_desc == at[j]->sat_ad ) {
2614                                                 *prev = a->a_next;
2615                                                 if (!head) {
2616                                                         head = a;
2617                                                         tail = a;
2618                                                 } else {
2619                                                         tail->a_next = a;
2620                                                         tail = a;
2621                                                 }
2622                                                 break;
2623                                         }
2624                                 }
2625                         }
2626                 }
2627                 if ( colst[i]->co_oc->soc_allowed ) {
2628                         AttributeType **at = colst[i]->co_oc->soc_allowed;
2629                         for (j=0; at[j]; j++) {
2630                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
2631                                         prev = &(*prev)->a_next, a=a->a_next) {
2632                                         if ( a->a_desc == at[j]->sat_ad ) {
2633                                                 *prev = a->a_next;
2634                                                 if (!head) {
2635                                                         head = a;
2636                                                         tail = a;
2637                                                 } else {
2638                                                         tail->a_next = a;
2639                                                         tail = a;
2640                                                 }
2641                                                 break;
2642                                         }
2643                                 }
2644                         }
2645                 }
2646         }
2647         if ( tail ) {
2648                 tail->a_next = e->e_attrs;
2649                 e->e_attrs = head;
2650         }
2651 }
2652
2653 static int
2654 check_vals( ConfigTable *ct, ConfigArgs *ca, void *ptr, int isAttr )
2655 {
2656         Attribute *a = NULL;
2657         AttributeDescription *ad;
2658         BerVarray vals;
2659
2660         int i, rc = 0, sort = 0;
2661
2662         if ( isAttr ) {
2663                 a = ptr;
2664                 ad = a->a_desc;
2665                 vals = a->a_vals;
2666         } else {
2667                 Modifications *ml = ptr;
2668                 ad = ml->sml_desc;
2669                 vals = ml->sml_values;
2670         }
2671
2672         if ( a && ( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL )) {
2673                 sort = 1;
2674                 rc = ordered_value_sort( a, 1 );
2675                 if ( rc )
2676                         return rc;
2677         }
2678         for ( i=0; vals[i].bv_val; i++ ) {
2679                 ca->line = vals[i].bv_val;
2680                 if ( sort ) {
2681                         char *idx = strchr( ca->line, '}' );
2682                         if ( idx ) ca->line = idx+1;
2683                 }
2684                 rc = config_parse_vals( ct, ca, i );
2685                 if ( rc )
2686                         break;
2687         }
2688         return rc;
2689 }
2690
2691 static int
2692 check_name_index( CfEntryInfo *parent, ConfigType ce_type, Entry *e,
2693         SlapReply *rs, int *renum )
2694 {
2695         CfEntryInfo *ce;
2696         int index = -1, gotindex = 0, nsibs;
2697         int renumber = 0, tailindex = 0;
2698         char *ptr1, *ptr2;
2699         struct berval rdn;
2700
2701         if ( renum ) *renum = 0;
2702
2703         /* These entries don't get indexed/renumbered */
2704         if ( ce_type == Cft_Global ) return 0;
2705         if ( ce_type == Cft_Schema && parent->ce_type == Cft_Global ) return 0;
2706
2707         if ( ce_type == Cft_Include || ce_type == Cft_Module )
2708                 tailindex = 1;
2709
2710         /* See if the rdn has an index already */
2711         dnRdn( &e->e_name, &rdn );
2712         ptr1 = strchr( e->e_name.bv_val, '{' );
2713         if ( ptr1 && ptr1 - e->e_name.bv_val < rdn.bv_len ) {
2714                 ptr2 = strchr( ptr1, '}' );
2715                 if (!ptr2 || ptr2 - e->e_name.bv_val > rdn.bv_len)
2716                         return LDAP_NAMING_VIOLATION;
2717                 if ( ptr2-ptr1 == 1)
2718                         return LDAP_NAMING_VIOLATION;
2719                 gotindex = 1;
2720                 index = atoi(ptr1+1);
2721                 if ( index < 0 )
2722                         return LDAP_NAMING_VIOLATION;
2723         }
2724
2725         /* count related kids */
2726         for (nsibs=0, ce=parent->ce_kids; ce; ce=ce->ce_sibs) {
2727                 if ( ce->ce_type == ce_type ) nsibs++;
2728         }
2729
2730         if ( index != nsibs ) {
2731                 if ( gotindex ) {
2732                         if ( index < nsibs ) {
2733                                 if ( tailindex ) return LDAP_NAMING_VIOLATION;
2734                                 /* Siblings need to be renumbered */
2735                                 renumber = 1;
2736                         }
2737                 }
2738                 if ( !renumber ) {
2739                         struct berval ival, newrdn, nnewrdn;
2740                         struct berval rtype, rval;
2741                         Attribute *a;
2742                         AttributeDescription *ad = NULL;
2743                         char ibuf[32];
2744                         const char *text;
2745
2746                         rval.bv_val = strchr(rdn.bv_val, '=' ) + 1;
2747                         rval.bv_len = rdn.bv_len - (rval.bv_val - rdn.bv_val);
2748                         rtype.bv_val = rdn.bv_val;
2749                         rtype.bv_len = rval.bv_val - rtype.bv_val - 1;
2750
2751                         /* Find attr */
2752                         slap_bv2ad( &rtype, &ad, &text );
2753                         a = attr_find( e->e_attrs, ad );
2754                         if (!a ) return LDAP_NAMING_VIOLATION;
2755
2756                         ival.bv_val = ibuf;
2757                         ival.bv_len = sprintf( ibuf, IFMT, nsibs );
2758                         
2759                         newrdn.bv_len = rdn.bv_len + ival.bv_len;
2760                         newrdn.bv_val = ch_malloc( newrdn.bv_len+1 );
2761
2762                         if ( tailindex ) {
2763                                 ptr1 = lutil_strncopy( newrdn.bv_val, rdn.bv_val, rdn.bv_len );
2764                                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
2765                         } else {
2766                                 int xlen;
2767                                 if ( !gotindex ) {
2768                                         ptr2 = rval.bv_val;
2769                                         xlen = rval.bv_len;
2770                                 } else {
2771                                         xlen = rdn.bv_len - (ptr2 - rdn.bv_val);
2772                                 }
2773                                 ptr1 = lutil_strncopy( newrdn.bv_val, rtype.bv_val,
2774                                         rtype.bv_len );
2775                                 *ptr1++ = '=';
2776                                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
2777                                 ptr1 = lutil_strncopy( ptr1, ptr2, xlen );
2778                                 *ptr1 = '\0';
2779                         }
2780
2781                         /* Do the equivalent of ModRDN */
2782                         /* Replace DN / NDN */
2783                         newrdn.bv_len = ptr1 - newrdn.bv_val;
2784                         rdnNormalize( 0, NULL, NULL, &newrdn, &nnewrdn, NULL );
2785                         free( e->e_name.bv_val );
2786                         build_new_dn( &e->e_name, &parent->ce_entry->e_name,
2787                                 &newrdn, NULL );
2788                         free( e->e_nname.bv_val );
2789                         build_new_dn( &e->e_nname, &parent->ce_entry->e_nname,
2790                                 &nnewrdn, NULL );
2791
2792                         /* Replace attr */
2793                         free( a->a_vals[0].bv_val );
2794                         ptr1 = strchr( newrdn.bv_val, '=' ) + 1;
2795                         a->a_vals[0].bv_len = newrdn.bv_len - (ptr1 - newrdn.bv_val);
2796                         a->a_vals[0].bv_val = ch_malloc( a->a_vals[0].bv_len + 1 );
2797                         strcpy( a->a_vals[0].bv_val, ptr1 );
2798
2799                         if ( a->a_nvals != a->a_vals ) {
2800                                 free( a->a_nvals[0].bv_val );
2801                                 ptr1 = strchr( nnewrdn.bv_val, '=' ) + 1;
2802                                 a->a_nvals[0].bv_len = nnewrdn.bv_len - (ptr1 - nnewrdn.bv_val);
2803                                 a->a_nvals[0].bv_val = ch_malloc( a->a_nvals[0].bv_len + 1 );
2804                                 strcpy( a->a_nvals[0].bv_val, ptr1 );
2805                         }
2806                         free( nnewrdn.bv_val );
2807                         free( newrdn.bv_val );
2808                 }
2809         }
2810         if ( renum ) *renum = renumber;
2811         return 0;
2812 }
2813
2814 static CfOcInfo **
2815 count_ocs( Attribute *oc_at, int *nocs )
2816 {
2817         int i, j, n;
2818         CfOcInfo co, *coptr, **colst;
2819
2820         /* count the objectclasses */
2821         for ( i=0; oc_at->a_nvals[i].bv_val; i++ );
2822         n = i;
2823         colst = (CfOcInfo **)ch_malloc( n * sizeof(CfOcInfo *));
2824
2825         for ( i=0, j=0; i<n; i++) {
2826                 co.co_name = &oc_at->a_nvals[i];
2827                 coptr = avl_find( CfOcTree, &co, CfOcInfo_cmp );
2828                 
2829                 /* ignore non-config objectclasses. probably should be
2830                  * an error, general data doesn't belong here.
2831                  */
2832                 if ( !coptr ) continue;
2833
2834                 /* Ignore the root objectclass, it has no implementation.
2835                  */
2836                 if ( coptr->co_type == Cft_Abstract ) continue;
2837                 colst[j++] = coptr;
2838         }
2839         *nocs = j;
2840         return colst;
2841 }
2842
2843 /* Parse an LDAP entry into config directives */
2844 static int
2845 config_add_internal( CfBackInfo *cfb, Entry *e, SlapReply *rs, int *renum )
2846 {
2847         CfEntryInfo *ce, *last;
2848         CfOcInfo **colst;
2849         Attribute *a, *oc_at, *type_attr;
2850         AttributeDescription *type_ad = NULL;
2851         int i, j, nocs, rc;
2852         ConfigArgs ca = {0};
2853         struct berval pdn;
2854         ConfigTable *ct, *type_ct = NULL;
2855         char *ptr;
2856
2857         /* Make sure parent exists and entry does not */
2858         ce = config_find_base( cfb->cb_root, &e->e_nname, &last );
2859         if ( ce )
2860                 return LDAP_ALREADY_EXISTS;
2861
2862         dnParent( &e->e_nname, &pdn );
2863
2864         /* If last is NULL, the new entry is the root/suffix entry, 
2865          * otherwise last should be the parent.
2866          */
2867         if ( last && !dn_match( &last->ce_entry->e_nname, &pdn )) {
2868                 if ( rs )
2869                         rs->sr_matched = last->ce_entry->e_name.bv_val;
2870                 return LDAP_NO_SUCH_OBJECT;
2871         }
2872
2873         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
2874         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
2875
2876         colst = count_ocs( oc_at, &nocs );
2877
2878         /* Only the root can be Cft_Global, everything else must
2879          * have a parent. Only limited nesting arrangements are allowed.
2880          */
2881         switch( colst[0]->co_type ) {
2882         case Cft_Global:
2883                 if ( last )  {
2884                         rc = LDAP_CONSTRAINT_VIOLATION;
2885                         goto leave;
2886                 }
2887                 break;
2888         case Cft_Schema:
2889         case Cft_Backend:
2890         case Cft_Database:
2891         case Cft_Include:
2892                 if ( !last || ( last->ce_type != Cft_Global &&
2893                         last->ce_type != colst[0]->co_type )) {
2894                         rc = LDAP_CONSTRAINT_VIOLATION;
2895                         goto leave;
2896                 }
2897                 break;
2898         case Cft_Overlay:
2899                 if ( !last || ( last->ce_type != Cft_Global &&
2900                         last->ce_type != Cft_Database &&
2901                         last->ce_type != colst[0]->co_type )) {
2902                         rc = LDAP_CONSTRAINT_VIOLATION;
2903                         goto leave;
2904                 }
2905                 break;
2906 #ifdef SLAPD_MODULES
2907         case Cft_Module:
2908                 if ( !last || last->ce_type != Cft_Global ) {
2909                         rc = LDAP_CONSTRAINT_VIOLATION;
2910                         goto leave;
2911                 }
2912 #endif
2913                 break;
2914         }
2915
2916         sort_attrs( e, colst, nocs );
2917
2918         /* Parse all the values and check for simple syntax errors before
2919          * performing any set actions.
2920          */
2921         switch (colst[0]->co_type) {
2922         case Cft_Schema:
2923                 /* The cn=schema entry is all hardcoded, so never reparse it */
2924                 if (last->ce_type == Cft_Global )
2925                         goto ok;
2926                 /* FALLTHRU */
2927                 ca.private = ch_calloc( 1, sizeof(ConfigFile) );
2928                 cfn = ca.private;
2929         case Cft_Global:
2930                 ca.be = LDAP_STAILQ_FIRST(&backendDB);
2931                 break;
2932
2933         case Cft_Backend:
2934                 if ( last->ce_type == Cft_Backend )
2935                         ca.bi = last->ce_bi;
2936                 else
2937                         type_ad = cfAd_backend;
2938                 break;
2939         case Cft_Database:
2940                 if ( last->ce_type == Cft_Database ) {
2941                         ca.be = last->ce_be;
2942                 } else {
2943                         type_ad = cfAd_database;
2944                         /* dummy, just to get past check_attr */
2945                         ca.be = frontendDB;
2946                 }
2947                 break;
2948
2949         case Cft_Overlay:
2950                 ca.be = last->ce_be;
2951                 type_ad = cfAd_overlay;
2952                 break;
2953
2954         case Cft_Include:
2955                 if ( !rs ) {
2956                         nocs = 0; /* ignored */
2957                         break;
2958                 }
2959                 if ( last->ce_type == Cft_Global )
2960                         cfn = &cf_prv;
2961                 else
2962                         cfn = last->ce_private;
2963                 type_ad = cfAd_include;
2964                 break;
2965 #ifdef SLAPD_MODULES
2966         case Cft_Module: {
2967 #if 0
2968                 ModPaths *mp;
2969                 char *ptr;
2970                 ptr = strchr( e->e_name.bv_val, '{' );
2971                 if ( !ptr ) {
2972                         rc = LDAP_NAMING_VIOLATION;
2973                         goto leave;
2974                 }
2975                 j = atoi(ptr+1);
2976                 for (i=0, mp=&modpaths; mp && i<j; mp=mp->mp_next);
2977                 /* There is no corresponding modpath for this load? */
2978                 if ( i != j ) {
2979                         rc = LDAP_NAMING_VIOLATION;
2980                         goto leave;
2981                 }
2982                 module_path( mp->mp_path.bv_val );
2983                 ca.private = mp;
2984 #endif
2985                 }
2986                 break;
2987 #endif
2988         }
2989
2990         /* If doing an LDAPadd, check for indexed names and any necessary
2991          * renaming/renumbering. Entries that don't need indexed names are
2992          * ignored. Entries that need an indexed name and arrive without one
2993          * are assigned to the end. Entries that arrive with an index may
2994          * cause the following entries to be renumbered/bumped down.
2995          *
2996          * Note that "pseudo-indexed" entries (cn=Include{xx}, cn=Module{xx})
2997          * don't allow Adding an entry with an index that's already in use.
2998          * This is flagged as an error (LDAP_ALREADY_EXISTS) up above.
2999          *
3000          * These entries can have auto-assigned indexes (appended to the end)
3001          * but only the other types support auto-renumbering of siblings.
3002          */
3003         rc = check_name_index( last, colst[0]->co_type, e, rs, renum );
3004         if ( rc )
3005                 goto leave;
3006
3007         init_config_argv( &ca );
3008         if ( type_ad ) {
3009                 type_attr = attr_find( e->e_attrs, type_ad );
3010                 if ( !type_attr ) {
3011                         rc = LDAP_OBJECT_CLASS_VIOLATION;
3012                         goto leave;
3013                 }
3014                 type_ct = config_find_table( colst, nocs, type_ad );
3015                 if ( !type_ct ) {
3016                         rc = LDAP_OBJECT_CLASS_VIOLATION;
3017                         goto leave;
3018                 }
3019                 rc = check_vals( type_ct, &ca, type_attr, 1);
3020                 if ( rc ) goto leave;
3021         }
3022         for ( a=e->e_attrs; a; a=a->a_next ) {
3023                 if ( a == type_attr || a == oc_at ) continue;
3024                 ct = config_find_table( colst, nocs, a->a_desc );
3025                 if ( !ct ) continue;    /* user data? */
3026                 rc = check_vals( ct, &ca, a, 1 );
3027                 if ( rc ) goto leave;
3028         }
3029
3030         /* Basic syntax checks are OK. Do the actual settings. */
3031         if ( type_ct ) {
3032                 ca.line = type_attr->a_vals[0].bv_val;
3033                 if ( type_ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
3034                         ptr = strchr( ca.line, '}' );
3035                         if ( ptr ) ca.line = ptr+1;
3036                 }
3037                 ca.valx = 0;
3038                 rc = config_parse_add( type_ct, &ca );
3039                 if ( rc ) {
3040                         rc = LDAP_OTHER;
3041                         goto leave;
3042                 }
3043         }
3044         for ( a=e->e_attrs; a; a=a->a_next ) {
3045                 if ( a == type_attr || a == oc_at ) continue;
3046                 ct = config_find_table( colst, nocs, a->a_desc );
3047                 if ( !ct ) continue;    /* user data? */
3048                 for (i=0; a->a_vals[i].bv_val; i++) {
3049                         ca.line = a->a_vals[i].bv_val;
3050                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
3051                                 ptr = strchr( ca.line, '}' );
3052                                 if ( ptr ) ca.line = ptr+1;
3053                         }
3054                         ca.valx = i;
3055                         rc = config_parse_add( ct, &ca );
3056                         if ( rc ) {
3057                                 rc = LDAP_OTHER;
3058                                 goto leave;
3059                         }
3060                 }
3061         }
3062 ok:
3063         ce = ch_calloc( 1, sizeof(CfEntryInfo) );
3064         ce->ce_parent = last;
3065         ce->ce_entry = entry_dup( e );
3066         ce->ce_entry->e_private = ce;
3067         ce->ce_type = colst[0]->co_type;
3068         ce->ce_be = ca.be;
3069         ce->ce_bi = ca.bi;
3070         ce->ce_private = ca.private;
3071         if ( !last ) {
3072                 cfb->cb_root = ce;
3073         } else if ( last->ce_kids ) {
3074                 CfEntryInfo *c2;
3075
3076                 for (c2=last->ce_kids; c2 && c2->ce_sibs; c2 = c2->ce_sibs);
3077
3078                 c2->ce_sibs = ce;
3079         } else {
3080                 last->ce_kids = ce;
3081         }
3082
3083 leave:
3084         ch_free( ca.argv );
3085         if ( colst ) ch_free( colst );
3086         return rc;
3087 }
3088
3089 /* Parse an LDAP entry into config directives, then store in underlying
3090  * database.
3091  */
3092 static int
3093 config_back_add( Operation *op, SlapReply *rs )
3094 {
3095         CfBackInfo *cfb;
3096         CfEntryInfo *ce, *last;
3097         int renumber;
3098
3099         if ( !be_isroot( op ) ) {
3100                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3101                 goto out;
3102         }
3103
3104         cfb = (CfBackInfo *)op->o_bd->be_private;
3105
3106         ldap_pvt_thread_pool_pause( &connection_pool );
3107
3108         /* Strategy:
3109          * 1) check for existence of entry
3110          * 2) check for sibling renumbering
3111          * 3) perform internal add
3112          * 4) store entry in underlying database
3113          * 5) perform any necessary renumbering
3114          */
3115         rs->sr_err = config_add_internal( cfb, op->ora_e, rs, &renumber );
3116         if ( rs->sr_err == LDAP_SUCCESS && cfb->cb_use_ldif ) {
3117                 BackendDB *be = op->o_bd;
3118                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL };
3119                 op->o_bd = &cfb->cb_db;
3120                 sc.sc_next = op->o_callback;
3121                 op->o_callback = &sc;
3122                 op->o_bd->be_add( op, rs );
3123                 op->o_bd = be;
3124                 op->o_callback = sc.sc_next;
3125         }
3126         if ( renumber ) {
3127         }
3128
3129         ldap_pvt_thread_pool_resume( &connection_pool );
3130
3131 out:
3132         send_ldap_result( op, rs );
3133         return rs->sr_err;
3134 }
3135
3136 typedef struct delrec {
3137         struct delrec *next;
3138         int nidx;
3139         int idx[0];
3140 } delrec;
3141
3142 static int
3143 config_modify_internal( CfEntryInfo *ce, Operation *op, SlapReply *rs,
3144         ConfigArgs *ca )
3145 {
3146         CfBackInfo *cfb = (CfBackInfo *)op->o_bd->be_private;
3147         int rc = LDAP_UNWILLING_TO_PERFORM;
3148         Modifications *ml;
3149         Entry *e = ce->ce_entry;
3150         Attribute *save_attrs = e->e_attrs, *oc_at;
3151         ConfigTable *ct;
3152         CfOcInfo **colst;
3153         int i, nocs;
3154         char *ptr;
3155         delrec *dels = NULL, *deltail = NULL;
3156
3157         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
3158         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
3159
3160         colst = count_ocs( oc_at, &nocs );
3161
3162         e->e_attrs = attrs_dup( e->e_attrs );
3163
3164         init_config_argv( ca );
3165         ca->be = ce->ce_be;
3166         ca->bi = ce->ce_bi;
3167         ca->private = ce->ce_private;
3168         strcpy( ca->log, "back-config" );
3169
3170         for (ml = op->orm_modlist; ml; ml=ml->sml_next) {
3171                 ct = config_find_table( colst, nocs, ml->sml_desc );
3172                 switch (ml->sml_op) {
3173                 case LDAP_MOD_DELETE:
3174                 case LDAP_MOD_REPLACE: {
3175                         BerVarray vals = NULL, nvals;
3176                         int *idx = NULL;
3177                         if ( ct && ( ct->arg_type & ARG_NO_DELETE )) {
3178                                 rc = LDAP_OTHER;
3179                                 snprintf( ca->msg, sizeof(ca->msg),
3180                                         "<%s> cannot be deleted" );
3181                                 snprintf(ca->msg, sizeof(ca->msg), "cannot delete %s",
3182                                         ml->sml_desc->ad_cname.bv_val );
3183                                 goto out;
3184                         }
3185                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
3186                                 vals = ml->sml_values;
3187                                 nvals = ml->sml_nvalues;
3188                                 ml->sml_values = NULL;
3189                                 ml->sml_nvalues = NULL;
3190                         }
3191                         /* If we're deleting by values, remember the indexes of the
3192                          * values we deleted.
3193                          */
3194                         if ( ct && ml->sml_values ) {
3195                                 delrec *d;
3196                                 for (i=0; ml->sml_values[i].bv_val; i++);
3197                                 d = ch_malloc( sizeof(delrec) + i * sizeof(int));
3198                                 d->nidx = i;
3199                                 d->next = NULL;
3200                                 if ( dels ) {
3201                                         deltail->next = d;
3202                                 } else {
3203                                         dels = d;
3204                                 }
3205                                 deltail = d;
3206                                 idx = d->idx;
3207                         }
3208                         rc = modify_delete_vindex(e, &ml->sml_mod,
3209                                 get_permissiveModify(op),
3210                                 &rs->sr_text, ca->msg, sizeof(ca->msg), idx );
3211                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
3212                                 ml->sml_values = vals;
3213                                 ml->sml_nvalues = nvals;
3214                         }
3215                         if ( !vals )
3216                                 break;
3217                         }
3218                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
3219
3220                 case LDAP_MOD_ADD:
3221                 case SLAP_MOD_SOFTADD: {
3222                         int mop = ml->sml_op;
3223                         int navals = -1;
3224                         ml->sml_op = LDAP_MOD_ADD;
3225                         if ( ct ) {
3226                                 if ( ct->arg_type & ARG_NO_INSERT ) {
3227                                         Attribute *a = attr_find( e->e_attrs, ml->sml_desc );
3228                                         if ( a ) {
3229                                                 for (i = 0; a->a_vals[i].bv_val; i++ );
3230                                                 navals = i;
3231                                         }
3232                                 }
3233                                 for ( i=0; !BER_BVISNULL( &ml->sml_values[i] ); i++ ) {
3234                                         if ( ml->sml_values[i].bv_val[0] == '{' &&
3235                                                 navals >= 0 ) {
3236                                                 int j = strtol( ml->sml_values[i].bv_val+1, NULL, 0 );
3237                                                 if ( j < navals ) {
3238                                                         rc = LDAP_OTHER;
3239                                                         snprintf(ca->msg, sizeof(ca->msg), "cannot insert %s",
3240                                                                 ml->sml_desc->ad_cname.bv_val );
3241                                                         goto out;
3242                                                 }
3243                                         }
3244                                         rc = check_vals( ct, ca, ml, 0 );
3245                                         if ( rc ) goto out;
3246                                 }
3247                         }
3248                         rc = modify_add_values(e, &ml->sml_mod,
3249                                    get_permissiveModify(op),
3250                                    &rs->sr_text, ca->msg, sizeof(ca->msg) );
3251
3252                         /* If value already exists, show success here
3253                          * and ignore this operation down below.
3254                          */
3255                         if ( mop == SLAP_MOD_SOFTADD ) {
3256                                 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS )
3257                                         rc = LDAP_SUCCESS;
3258                                 else
3259                                         mop = LDAP_MOD_ADD;
3260                         }
3261                         ml->sml_op = mop;
3262                         break;
3263                         }
3264
3265                         break;
3266                 case LDAP_MOD_INCREMENT:        /* FIXME */
3267                         break;
3268                 default:
3269                         break;
3270                 }
3271                 if(rc != LDAP_SUCCESS) break;
3272         }
3273         
3274         if(rc == LDAP_SUCCESS) {
3275                 /* check that the entry still obeys the schema */
3276                 rc = entry_schema_check(op->o_bd, e, NULL,
3277                                   &rs->sr_text, ca->msg, sizeof(ca->msg) );
3278         }
3279         if ( rc == LDAP_SUCCESS ) {
3280                 /* Basic syntax checks are OK. Do the actual settings. */
3281                 for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
3282                         ct = config_find_table( colst, nocs, ml->sml_desc );
3283                         if ( !ct ) continue;
3284
3285                         switch (ml->sml_op) {
3286                         case LDAP_MOD_DELETE:
3287                         case LDAP_MOD_REPLACE: {
3288                                 BerVarray vals = NULL, nvals;
3289                                 Attribute *a;
3290                                 delrec *d;
3291
3292                                 a = attr_find( e->e_attrs, ml->sml_desc );
3293
3294                                 if ( ml->sml_op == LDAP_MOD_REPLACE ) {
3295                                         vals = ml->sml_values;
3296                                         nvals = ml->sml_nvalues;
3297                                         ml->sml_values = NULL;
3298                                         ml->sml_nvalues = NULL;
3299                                 }
3300
3301                                 if ( ml->sml_values )
3302                                         d = dels;
3303
3304                                 /* If we didn't delete the whole attribute */
3305                                 if ( ml->sml_values && a ) {
3306                                         struct berval *mvals;
3307                                         int j;
3308
3309                                         if ( ml->sml_nvalues )
3310                                                 mvals = ml->sml_nvalues;
3311                                         else
3312                                                 mvals = ml->sml_values;
3313
3314                                         /* use the indexes we saved up above */
3315                                         for (i=0; i < d->nidx; i++) {
3316                                                 struct berval bv = *mvals++;
3317                                                 if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED &&
3318                                                         bv.bv_val[0] == '{' ) {
3319                                                         ptr = strchr( bv.bv_val, '}' ) + 1;
3320                                                         bv.bv_len -= ptr - bv.bv_val;
3321                                                         bv.bv_val = ptr;
3322                                                 }
3323                                                 ca->line = bv.bv_val;
3324                                                 ca->valx = d->idx[i];
3325                                                 rc = config_del_vals( ct, ca );
3326                                                 if ( rc != LDAP_SUCCESS ) break;
3327                                                 for (j=i+1; j < d->nidx; j++)
3328                                                         if ( d->idx[j] >d->idx[i] )
3329                                                                 d->idx[j]--;
3330                                         }
3331                                 } else {
3332                                         ca->valx = -1;
3333                                         ca->line = NULL;
3334                                         rc = config_del_vals( ct, ca );
3335                                         if ( rc ) rc = LDAP_OTHER;
3336                                 }
3337                                 if ( ml->sml_values ) {
3338                                         ch_free( dels );
3339                                         dels = d->next;
3340                                 }
3341                                 if ( ml->sml_op == LDAP_MOD_REPLACE ) {
3342                                         ml->sml_values = vals;
3343                                         ml->sml_nvalues = nvals;
3344                                 }
3345                                 if ( !vals || rc != LDAP_SUCCESS )
3346                                         break;
3347                                 }
3348                                 /* FALLTHRU: LDAP_MOD_REPLACE && vals */
3349
3350                         case LDAP_MOD_ADD:
3351                                 for (i=0; ml->sml_values[i].bv_val; i++) {
3352                                         ca->line = ml->sml_values[i].bv_val;
3353                                         ca->valx = -1;
3354                                         if ( ml->sml_desc->ad_type->sat_flags & SLAP_AT_ORDERED &&
3355                                                 ca->line[0] == '{' ) {
3356                                                 ptr = strchr( ca->line, '}' );
3357                                                 if ( ptr ) {
3358                                                         ca->valx = strtol( ca->line+1, NULL, 0 );
3359                                                         ca->line = ptr+1;
3360                                                 }
3361                                         }
3362                                         rc = config_parse_add( ct, ca );
3363                                         if ( rc ) {
3364                                                 rc = LDAP_OTHER;
3365                                                 goto out;
3366                                         }
3367                                 }
3368
3369                                 break;
3370                         }
3371                 }
3372         }
3373
3374 out:
3375         if ( ca->cleanup )
3376                 ca->cleanup( ca );
3377         if ( rc == LDAP_SUCCESS ) {
3378                 attrs_free( save_attrs );
3379         } else {
3380                 attrs_free( e->e_attrs );
3381                 e->e_attrs = save_attrs;
3382         }
3383         ch_free( ca->argv );
3384         if ( colst ) ch_free( colst );
3385
3386         return rc;
3387 }
3388
3389 static int
3390 config_back_modify( Operation *op, SlapReply *rs )
3391 {
3392         CfBackInfo *cfb;
3393         CfEntryInfo *ce, *last;
3394         Modifications *ml;
3395         ConfigArgs ca = {0};
3396         struct berval rdn;
3397         char *ptr;
3398         AttributeDescription *rad = NULL;
3399
3400         if ( !be_isroot( op ) ) {
3401                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3402                 goto out;
3403         }
3404
3405         cfb = (CfBackInfo *)op->o_bd->be_private;
3406
3407         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
3408         if ( !ce ) {
3409                 if ( last )
3410                         rs->sr_matched = last->ce_entry->e_name.bv_val;
3411                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
3412                 goto out;
3413         }
3414
3415         /* Get type of RDN */
3416         rdn = ce->ce_entry->e_nname;
3417         ptr = strchr( rdn.bv_val, '=' );
3418         rdn.bv_len = ptr - rdn.bv_val;
3419         slap_bv2ad( &rdn, &rad, &rs->sr_text );
3420
3421         /* Some basic validation... */
3422         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
3423                 /* Don't allow Modify of RDN; must use ModRdn for that. */
3424                 if ( ml->sml_desc == rad ) {
3425                         rs->sr_err = LDAP_NOT_ALLOWED_ON_RDN;
3426                         rs->sr_text = "Use modrdn to change the entry name";
3427                         goto out;
3428                 }
3429         }
3430
3431         ldap_pvt_thread_pool_pause( &connection_pool );
3432
3433         /* Strategy:
3434          * 1) perform the Modify on the cached Entry.
3435          * 2) verify that the Entry still satisfies the schema.
3436          * 3) perform the individual config operations.
3437          * 4) store Modified entry in underlying LDIF backend.
3438          */
3439         rs->sr_err = config_modify_internal( ce, op, rs, &ca );
3440         if ( rs->sr_err ) {
3441                 rs->sr_text = ca.msg;
3442         } else if ( cfb->cb_use_ldif ) {
3443                 BackendDB *be = op->o_bd;
3444                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL };
3445                 op->o_bd = &cfb->cb_db;
3446                 sc.sc_next = op->o_callback;
3447                 op->o_callback = &sc;
3448                 op->o_bd->be_modify( op, rs );
3449                 op->o_bd = be;
3450                 op->o_callback = sc.sc_next;
3451         }
3452
3453         ldap_pvt_thread_pool_resume( &connection_pool );
3454 out:
3455         send_ldap_result( op, rs );
3456         return rs->sr_err;
3457 }
3458
3459 static int
3460 config_back_modrdn( Operation *op, SlapReply *rs )
3461 {
3462         CfBackInfo *cfb;
3463         CfEntryInfo *ce, *last;
3464
3465         if ( !be_isroot( op ) ) {
3466                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3467                 goto out;
3468         }
3469
3470         cfb = (CfBackInfo *)op->o_bd->be_private;
3471
3472         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
3473         if ( !ce ) {
3474                 if ( last )
3475                         rs->sr_matched = last->ce_entry->e_name.bv_val;
3476                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
3477                 goto out;
3478         }
3479
3480         /* We don't allow moving objects to new parents.
3481          * Generally we only allow reordering a set of ordered entries.
3482          */
3483         if ( op->orr_newSup ) {
3484                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
3485                 goto out;
3486         }
3487         ldap_pvt_thread_pool_pause( &connection_pool );
3488
3489         ldap_pvt_thread_pool_resume( &connection_pool );
3490 out:
3491         send_ldap_result( op, rs );
3492         return rs->sr_err;
3493 }
3494
3495 static int
3496 config_back_search( Operation *op, SlapReply *rs )
3497 {
3498         CfBackInfo *cfb;
3499         CfEntryInfo *ce, *last;
3500         int rc;
3501
3502         if ( !be_isroot( op ) ) {
3503                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3504                 goto out;
3505         }
3506
3507         cfb = (CfBackInfo *)op->o_bd->be_private;
3508
3509         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
3510         if ( !ce ) {
3511                 if ( last )
3512                         rs->sr_matched = last->ce_entry->e_name.bv_val;
3513                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
3514                 goto out;
3515         }
3516         switch ( op->ors_scope ) {
3517         case LDAP_SCOPE_BASE:
3518         case LDAP_SCOPE_SUBTREE:
3519                 config_send( op, rs, ce, 0 );
3520                 break;
3521                 
3522         case LDAP_SCOPE_ONELEVEL:
3523                 for (ce = ce->ce_kids; ce; ce=ce->ce_sibs) {
3524                         config_send( op, rs, ce, 1 );
3525                 }
3526                 break;
3527         }
3528                 
3529         rs->sr_err = LDAP_SUCCESS;
3530 out:
3531         send_ldap_result( op, rs );
3532         return 0;
3533 }
3534
3535 static Entry *
3536 config_alloc_entry( CfEntryInfo *parent, struct berval *rdn )
3537 {
3538         Entry *e = ch_calloc( 1, sizeof(Entry) );
3539         CfEntryInfo *ce = ch_calloc( 1, sizeof(CfEntryInfo) );
3540         struct berval pdn;
3541
3542         e->e_private = ce;
3543         ce->ce_entry = e;
3544         ce->ce_parent = parent;
3545         if ( parent ) {
3546                 pdn = parent->ce_entry->e_nname;
3547         } else {
3548                 BER_BVZERO( &pdn );
3549         }
3550
3551         build_new_dn( &e->e_name, &pdn, rdn, NULL );
3552         ber_dupbv( &e->e_nname, &e->e_name );
3553         return e;
3554 }
3555
3556 #define NO_TABLE        0
3557 #define BI_TABLE        1
3558 #define BE_TABLE        2
3559
3560 static int
3561 config_build_entry( ConfigArgs *c, Entry *e, ObjectClass *oc,
3562          struct berval *rdn, ConfigTable *ct, int table )
3563 {
3564         struct berval val;
3565         struct berval ad_name;
3566         AttributeDescription *ad = NULL;
3567         int rc, i;
3568         char *ptr;
3569         const char *text;
3570         AttributeType **at;
3571         Attribute *oc_at;
3572
3573         val = oc->soc_cname;
3574         attr_merge_normalize_one(e, slap_schema.si_ad_objectClass, &val, NULL );
3575         ptr = strchr(rdn->bv_val, '=');
3576         ad_name.bv_val = rdn->bv_val;
3577         ad_name.bv_len = ptr - rdn->bv_val;
3578         rc = slap_bv2ad( &ad_name, &ad, &text );
3579         if ( rc ) {
3580                 return rc;
3581         }
3582         val.bv_val = ptr+1;
3583         val.bv_len = rdn->bv_len - (val.bv_val - rdn->bv_val);
3584         attr_merge_normalize_one(e, ad, &val, NULL );
3585
3586         for (at=oc->soc_required; at && *at; at++) {
3587                 /* Skip the naming attr */
3588                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
3589                         continue;
3590                 for (i=0;ct[i].name;i++) {
3591                         if (ct[i].ad == (*at)->sat_ad) {
3592                                 rc = config_get_vals(&ct[i], c);
3593                                 if (rc == LDAP_SUCCESS) {
3594                                         if ( c->rvalue_nvals )
3595                                                 attr_merge(e, ct[i].ad, c->rvalue_vals,
3596                                                         c->rvalue_nvals);
3597                                         else
3598                                                 attr_merge_normalize(e, ct[i].ad,
3599                                                         c->rvalue_vals, NULL);
3600                                         ber_bvarray_free( c->rvalue_nvals );
3601                                         ber_bvarray_free( c->rvalue_vals );
3602                                 }
3603                                 break;
3604                         }
3605                 }
3606         }
3607
3608         for (at=oc->soc_allowed; at && *at; at++) {
3609                 /* Skip the naming attr */
3610                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
3611                         continue;
3612                 for (i=0;ct[i].name;i++) {
3613                         if (ct[i].ad == (*at)->sat_ad) {
3614                                 rc = config_get_vals(&ct[i], c);
3615                                 if (rc == LDAP_SUCCESS) {
3616                                         if ( c->rvalue_nvals )
3617                                                 attr_merge(e, ct[i].ad, c->rvalue_vals, c->rvalue_nvals);
3618                                         else
3619                                                 attr_merge_normalize(e, ct[i].ad, c->rvalue_vals, NULL);
3620                                         ber_bvarray_free( c->rvalue_nvals );
3621                                         ber_bvarray_free( c->rvalue_vals );
3622                                 }
3623                                 break;
3624                         }
3625                 }
3626         }
3627
3628         if ( table ) {
3629                 if ( table == BI_TABLE )
3630                         ct = c->bi->bi_cf_table;
3631                 else
3632                         ct = c->be->be_cf_table;
3633                 for (;ct && ct->name;ct++) {
3634                         if (!ct->ad) continue;
3635                         rc = config_get_vals(ct, c);
3636                         if (rc == LDAP_SUCCESS) {
3637                                 if ( c->rvalue_nvals )
3638                                         attr_merge(e, ct->ad, c->rvalue_vals, c->rvalue_nvals);
3639                                 else
3640                                         attr_merge_normalize(e, ct->ad, c->rvalue_vals, NULL);
3641                         }
3642                 }
3643         }
3644         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
3645         rc = structural_class(oc_at->a_vals, &val, NULL, &text, c->msg,
3646                 sizeof(c->msg));
3647         attr_merge_normalize_one(e, slap_schema.si_ad_structuralObjectClass, &val, NULL );
3648
3649         return 0;
3650 }
3651
3652 static void
3653 config_build_schema_inc( ConfigArgs *c, CfEntryInfo *ceparent,
3654         Operation *op, SlapReply *rs )
3655 {
3656         Entry *e;
3657         ConfigFile *cf = c->private;
3658         CfEntryInfo *ce, *ceprev;
3659         char *ptr;
3660         struct berval bv;
3661
3662         if ( ceparent->ce_kids ) {
3663                 for ( ceprev = ceparent->ce_kids; ceprev->ce_sibs;
3664                         ceprev = ceprev->ce_sibs );
3665         }
3666
3667         for (; cf; cf=cf->c_sibs, c->depth++) {
3668                 c->value_dn.bv_val = c->log;
3669                 bv.bv_val = strrchr(cf->c_file.bv_val, LDAP_DIRSEP[0]);
3670                 if ( !bv.bv_val ) {
3671                         bv = cf->c_file;
3672                 } else {
3673                         bv.bv_val++;
3674                         bv.bv_len = cf->c_file.bv_len - (bv.bv_val - cf->c_file.bv_val);
3675                 }
3676                 ptr = strchr( bv.bv_val, '.' );
3677                 if ( ptr )
3678                         bv.bv_len = ptr - bv.bv_val;
3679                 c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn=" IFMT, c->depth);
3680                 strncpy( c->value_dn.bv_val + c->value_dn.bv_len, bv.bv_val,
3681                         bv.bv_len );
3682                 c->value_dn.bv_len += bv.bv_len;
3683                 c->value_dn.bv_val[c->value_dn.bv_len] ='\0';
3684
3685                 e = config_alloc_entry( ceparent, &c->value_dn );
3686                 c->private = cf;
3687                 config_build_entry( c, e, cfOc_schema, &c->value_dn,
3688                         c->bi->bi_cf_table, NO_TABLE );
3689                 ce = e->e_private;
3690                 ce->ce_private = cf;
3691                 ce->ce_type = Cft_Schema;
3692                 if ( op ) {
3693                         op->ora_e = e;
3694                         op->o_bd->be_add( op, rs );
3695                 }
3696                 ce->ce_bi = c->bi;
3697                 if ( !ceparent->ce_kids ) {
3698                         ceparent->ce_kids = ce;
3699                 } else {
3700                         ceprev->ce_sibs = ce;
3701                 }
3702                 ceprev = ce;
3703                 if ( cf->c_kids ) {
3704                         c->private = cf->c_kids;
3705                         config_build_schema_inc( c, ceparent, op, rs );
3706                 }
3707         }
3708 }
3709
3710 static CfEntryInfo *
3711 config_build_includes( ConfigArgs *c, CfEntryInfo *ceparent,
3712         Operation *op, SlapReply *rs )
3713 {
3714         Entry *e;
3715         int i;
3716         ConfigFile *cf = c->private;
3717         CfEntryInfo *ce, *ceprev;
3718
3719         if ( ceparent->ce_kids ) {
3720                 for ( ceprev = ceparent->ce_kids; ceprev->ce_sibs;
3721                         ceprev = ceprev->ce_sibs );
3722         }
3723
3724         for (i=0; cf; cf=cf->c_sibs, i++) {
3725                 c->value_dn.bv_val = c->log;
3726                 c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn=include" IFMT, i);
3727                 e = config_alloc_entry( ceparent, &c->value_dn );
3728                 c->private = cf;
3729                 config_build_entry( c, e, cfOc_include, &c->value_dn,
3730                         c->bi->bi_cf_table, NO_TABLE );
3731                 if ( op ) {
3732                         op->ora_e = e;
3733                         op->o_bd->be_add( op, rs );
3734                 }
3735                 ce = e->e_private;
3736                 ce->ce_private = cf;
3737                 ce->ce_type = Cft_Include;
3738                 ce->ce_bi = c->bi;
3739                 if ( !ceparent->ce_kids ) {
3740                         ceparent->ce_kids = ce;
3741                 } else {
3742                         ceprev->ce_sibs = ce;
3743                 }
3744                 ceprev = ce;
3745                 if ( cf->c_kids ) {
3746                         c->private = cf->c_kids;
3747                         config_build_includes( c, ce, op, rs );
3748                 }
3749         }
3750         return ce;
3751 }
3752
3753 #ifdef SLAPD_MODULES
3754
3755 static CfEntryInfo *
3756 config_build_modules( ConfigArgs *c, CfEntryInfo *ceparent,
3757         Operation *op, SlapReply *rs )
3758 {
3759         Entry *e;
3760         int i;
3761         CfEntryInfo *ce, *ceprev;
3762         ModPaths *mp;
3763
3764         if ( ceparent->ce_kids ) {
3765                 for ( ceprev = ceparent->ce_kids; ceprev->ce_sibs;
3766                         ceprev = ceprev->ce_sibs );
3767         }
3768
3769         for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
3770                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
3771                         continue;
3772                 c->value_dn.bv_val = c->log;
3773                 c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn=module" IFMT, i);
3774                 e = config_alloc_entry( ceparent, &c->value_dn );
3775                 ce = e->e_private;
3776                 ce->ce_type = Cft_Include;
3777                 c->private = mp;
3778                 ce->ce_private = mp;
3779                 config_build_entry( c, e, cfOc_module, &c->value_dn,
3780                         c->bi->bi_cf_table, NO_TABLE );
3781                 if ( op ) {
3782                         op->ora_e = e;
3783                         op->o_bd->be_add( op, rs );
3784                 }
3785                 ce->ce_bi = c->bi;
3786                 if ( !ceparent->ce_kids ) {
3787                         ceparent->ce_kids = ce;
3788                 } else {
3789                         ceprev->ce_sibs = ce;
3790                 }
3791                 ceprev = ce;
3792         }
3793         return ce;
3794 }
3795 #endif
3796
3797 static int
3798 config_back_db_open( BackendDB *be )
3799 {
3800         CfBackInfo *cfb = be->be_private;
3801         struct berval rdn;
3802         Entry *e, *parent;
3803         CfEntryInfo *ce, *ceparent, *ceprev;
3804         int i, rc;
3805         BackendInfo *bi;
3806         BackendDB *bptr;
3807         ConfigArgs c;
3808         ConfigTable *ct;
3809         Connection conn = {0};
3810         char opbuf[OPERATION_BUFFER_SIZE];
3811         Operation *op;
3812         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
3813         SlapReply rs = {REP_RESULT};
3814
3815         /* If we read the config from back-ldif, nothing to do here */
3816         if ( cfb->cb_got_ldif )
3817                 return 0;
3818
3819         if ( cfb->cb_use_ldif ) {
3820                 op = (Operation *)opbuf;
3821                 connection_fake_init( &conn, op, cfb );
3822
3823                 op->o_dn = be->be_rootdn;
3824                 op->o_ndn = be->be_rootndn;
3825
3826                 op->o_tag = LDAP_REQ_ADD;
3827                 op->o_callback = &cb;
3828                 op->o_bd = &cfb->cb_db;
3829         } else {
3830                 op = NULL;
3831         }
3832
3833         /* create root of tree */
3834         rdn = config_rdn;
3835         e = config_alloc_entry( NULL, &rdn );
3836         ce = e->e_private;
3837         ce->ce_type = Cft_Global;
3838         cfb->cb_root = ce;
3839         c.be = be;
3840         c.bi = be->bd_info;
3841         c.private = cfb->cb_config;
3842         ct = c.bi->bi_cf_table;
3843         ce->ce_private = c.private;
3844         config_build_entry( &c, e, cfOc_global, &rdn, ct, NO_TABLE );
3845         if ( op ) {
3846                 op->ora_e = e;
3847                 op->o_bd->be_add( op, &rs );
3848         }
3849         ce->ce_bi = c.bi;
3850
3851         parent = e;
3852         ceparent = ce;
3853
3854         /* Create schema nodes... cn=schema will contain the hardcoded core
3855          * schema, read-only. Child objects will contain runtime loaded schema
3856          * files.
3857          */
3858         rdn = schema_rdn;
3859         e = config_alloc_entry( ceparent, &rdn );
3860         ce = e->e_private;
3861         ce->ce_type = Cft_Schema;
3862         c.private = NULL;
3863         config_build_entry( &c, e, cfOc_schema, &rdn, ct, NO_TABLE );
3864         if ( op ) {
3865                 op->ora_e = e;
3866                 op->o_bd->be_add( op, &rs );
3867         }
3868         if ( !ceparent->ce_kids ) {
3869                 ceparent->ce_kids = ce;
3870         } else {
3871                 ceprev->ce_sibs = ce;
3872         }
3873         ceprev = ce;
3874
3875         /* Create includeFile nodes and schema nodes for included schema... */
3876         if ( cfb->cb_config->c_kids ) {
3877                 c.depth = 0;
3878                 c.private = cfb->cb_config->c_kids;
3879                 config_build_schema_inc( &c, ce, op, &rs );
3880                 c.private = cfb->cb_config->c_kids;
3881                 ceprev = config_build_includes( &c, ceparent, op, &rs );
3882         }
3883
3884 #ifdef SLAPD_MODULES
3885         /* Create Module nodes... */
3886         if ( modpaths.mp_loads ) {
3887                 ceprev = config_build_modules( &c, ceparent, op, &rs );
3888         }
3889 #endif
3890
3891         /* Create backend nodes. Skip if they don't provide a cf_table.
3892          * There usually aren't any of these.
3893          */
3894         
3895         c.line = 0;
3896         LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next) {
3897                 if (!bi->bi_cf_table) continue;
3898                 if (!bi->bi_private) continue;
3899
3900                 rdn.bv_val = c.log;
3901                 rdn.bv_len = sprintf(rdn.bv_val, "%s=%s", cfAd_backend->ad_cname.bv_val, bi->bi_type);
3902                 e = config_alloc_entry( ceparent, &rdn );
3903                 ce = e->e_private;
3904                 ce->ce_type = Cft_Backend;
3905                 ce->ce_bi = bi;
3906                 c.bi = bi;
3907                 config_build_entry( &c, e, cfOc_backend, &rdn, ct, BI_TABLE );
3908                 if ( op ) {
3909                         op->ora_e = e;
3910                         op->o_bd->be_add( op, &rs );
3911                 }
3912                 if ( !ceparent->ce_kids ) {
3913                         ceparent->ce_kids = ce;
3914                 } else {
3915                         ceprev->ce_sibs = ce;
3916                 }
3917                 ceprev = ce;
3918         }
3919
3920         /* Create database nodes... */
3921         i = -1;
3922         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
3923                 slap_overinfo *oi = NULL;
3924                 i++;
3925                 if ( i == 0 ) {
3926                         bptr = frontendDB;
3927                 } else {
3928                         bptr = be;
3929                 }
3930                 if ( overlay_is_over( bptr )) {
3931                         oi = bptr->bd_info->bi_private;
3932                         bi = oi->oi_orig;
3933                 } else {
3934                         bi = bptr->bd_info;
3935                 }
3936                 rdn.bv_val = c.log;
3937                 rdn.bv_len = sprintf(rdn.bv_val, "%s=" IFMT "%s", cfAd_database->ad_cname.bv_val,
3938                         i, bi->bi_type);
3939                 e = config_alloc_entry( ceparent, &rdn );
3940                 ce = e->e_private;
3941                 c.be = bptr;
3942                 c.bi = bi;
3943                 ce->ce_type = Cft_Database;
3944                 ce->ce_be = c.be;
3945                 ce->ce_bi = c.bi;
3946                 config_build_entry( &c, e, cfOc_database, &rdn, ct, BE_TABLE );
3947                 if ( op ) {
3948                         op->ora_e = e;
3949                         op->o_bd->be_add( op, &rs );
3950                 }
3951                 if ( !ceparent->ce_kids ) {
3952                         ceparent->ce_kids = ce;
3953                 } else {
3954                         ceprev->ce_sibs = ce;
3955                 }
3956                 ceprev = ce;
3957                 /* Iterate through overlays */
3958                 if ( oi ) {
3959                         slap_overinst *on;
3960                         Entry *oe;
3961                         CfEntryInfo *opar = ce, *oprev = NULL;
3962                         int j;
3963
3964                         for (j=0,on=oi->oi_list; on; j++,on=on->on_next) {
3965                                 rdn.bv_val = c.log;
3966                                 rdn.bv_len = sprintf(rdn.bv_val, "%s=" IFMT "%s",
3967                                         cfAd_overlay->ad_cname.bv_val, j, on->on_bi.bi_type );
3968                                 oe = config_alloc_entry( opar, &rdn );
3969                                 ce = oe->e_private;
3970                                 c.be = bptr;
3971                                 c.bi = &on->on_bi;
3972                                 ce->ce_type = Cft_Overlay;
3973                                 ce->ce_be = c.be;
3974                                 ce->ce_bi = c.bi;
3975                                 config_build_entry( &c, oe, cfOc_overlay, &rdn, ct, BI_TABLE );
3976                                 if ( op ) {
3977                                         op->ora_e = oe;
3978                                         op->o_bd->be_add( op, &rs );
3979                                 }
3980                                 if ( !opar->ce_kids ) {
3981                                         opar->ce_kids = ce;
3982                                 } else {
3983                                         oprev->ce_sibs = ce;
3984                                 }
3985                                 oprev = ce;
3986                         }
3987                 }
3988         }
3989
3990         return 0;
3991 }
3992
3993 static int
3994 config_back_db_destroy( Backend *be )
3995 {
3996         free( be->be_private );
3997         return 0;
3998 }
3999
4000 static int
4001 config_back_db_init( Backend *be )
4002 {
4003         struct berval dn;
4004         CfBackInfo *cfb;
4005
4006         cfb = ch_calloc( 1, sizeof(CfBackInfo));
4007         cfb->cb_config = &cf_prv;
4008         be->be_private = cfb;
4009
4010         ber_dupbv( &be->be_rootdn, &config_rdn );
4011         ber_dupbv( &be->be_rootndn, &be->be_rootdn );
4012         ber_dupbv( &dn, &be->be_rootdn );
4013         ber_bvarray_add( &be->be_suffix, &dn );
4014         ber_dupbv( &dn, &be->be_rootdn );
4015         ber_bvarray_add( &be->be_nsuffix, &dn );
4016
4017         /* Hide from namingContexts */
4018         SLAP_BFLAGS(be) |= SLAP_BFLAG_CONFIG;
4019
4020         return 0;
4021 }
4022
4023 static int
4024 config_back_destroy( BackendInfo *bi )
4025 {
4026         ldif_must_b64_encode_release();
4027         return 0;
4028 }
4029
4030 static struct {
4031         char *name;
4032         AttributeDescription **desc;
4033 } ads[] = {
4034         { "backend", &cfAd_backend },
4035         { "database", &cfAd_database },
4036         { "include", &cfAd_include },
4037         { "overlay", &cfAd_overlay },
4038         { NULL, NULL }
4039 };
4040
4041 /* Notes:
4042  *   add / delete: all types that may be added or deleted must use an
4043  * X-ORDERED attributeType for their RDN. Adding and deleting entries
4044  * should automatically renumber the index of any siblings as needed,
4045  * so that no gaps in the numbering sequence exist after the add/delete
4046  * is completed.
4047  *   What can be added:
4048  *     schema objects
4049  *     backend objects for backend-specific config directives
4050  *     database objects
4051  *     overlay objects
4052  *
4053  *   delete: probably no support this time around.
4054  *
4055  *   modrdn: generally not done. Will be invoked automatically by add/
4056  * delete to update numbering sequence. Perform as an explicit operation
4057  * so that the renumbering effect may be replicated. Subtree rename must
4058  * be supported, since renumbering a database will affect all its child
4059  * overlays.
4060  *
4061  *  modify: must be fully supported. 
4062  */
4063
4064 int
4065 config_back_initialize( BackendInfo *bi )
4066 {
4067         ConfigTable             *ct = config_back_cf_table;
4068         char                    *argv[4];
4069         int                     i;
4070         AttributeDescription    *ad = NULL;
4071         const char              *text;
4072         static char             *controls[] = {
4073                 LDAP_CONTROL_MANAGEDSAIT,
4074                 NULL
4075         };
4076
4077         bi->bi_controls = controls;
4078
4079         bi->bi_open = 0;
4080         bi->bi_close = 0;
4081         bi->bi_config = 0;
4082         bi->bi_destroy = config_back_destroy;
4083
4084         bi->bi_db_init = config_back_db_init;
4085         bi->bi_db_config = 0;
4086         bi->bi_db_open = config_back_db_open;
4087         bi->bi_db_close = 0;
4088         bi->bi_db_destroy = config_back_db_destroy;
4089
4090         bi->bi_op_bind = config_back_bind;
4091         bi->bi_op_unbind = 0;
4092         bi->bi_op_search = config_back_search;
4093         bi->bi_op_compare = 0;
4094         bi->bi_op_modify = config_back_modify;
4095         bi->bi_op_modrdn = config_back_modrdn;
4096         bi->bi_op_add = config_back_add;
4097         bi->bi_op_delete = 0;
4098         bi->bi_op_abandon = 0;
4099
4100         bi->bi_extended = 0;
4101
4102         bi->bi_chk_referrals = 0;
4103
4104 #ifdef SLAP_OVERLAY_ACCESS
4105         bi->bi_access_allowed = slap_access_always_allowed;
4106 #endif /* SLAP_OVERLAY_ACCESS */
4107
4108         bi->bi_connection_init = 0;
4109         bi->bi_connection_destroy = 0;
4110
4111         argv[3] = NULL;
4112         for (i=0; OidMacros[i].name; i++ ) {
4113                 argv[1] = OidMacros[i].name;
4114                 argv[2] = OidMacros[i].oid;
4115                 parse_oidm( "slapd", i, 3, argv, 0, NULL );
4116         }
4117
4118         bi->bi_cf_table = ct;
4119
4120         i = config_register_schema( ct, cf_ocs );
4121         if ( i ) return i;
4122
4123         /* setup olcRootPW to be base64-encoded when written in LDIF form;
4124          * basically, we don't care if it fails */
4125         i = slap_str2ad( "olcRootPW", &ad, &text );
4126         if ( i ) {
4127                 Debug( LDAP_DEBUG_ANY, "config_back_initialize: "
4128                         "warning, unable to get \"olcRootPW\" "
4129                         "attribute description: %d: %s\n",
4130                         i, text, 0 );
4131         } else {
4132                 (void)ldif_must_b64_encode_register( ad->ad_cname.bv_val,
4133                         ad->ad_type->sat_oid );
4134         }
4135
4136         /* set up the notable AttributeDescriptions */
4137         i = 0;
4138         for (;ct->name;ct++) {
4139                 if (strcmp(ct->name, ads[i].name)) continue;
4140                 *ads[i].desc = ct->ad;
4141                 i++;
4142                 if (!ads[i].name) break;
4143         }
4144
4145         return 0;
4146 }
4147