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