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