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