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