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