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