]> git.sur5r.net Git - openldap/blob - servers/slapd/bconfig.c
04ce1a60d038c4fc04034f63d427f8f98c5e5784
[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
28 #include "slap.h"
29
30 #ifdef LDAP_SLAPI
31 #include "slapi/slapi.h"
32 #endif
33
34 #include <lutil.h>
35
36 #include "config.h"
37
38 #define CONFIG_DN       "cn=config"
39
40 #ifdef SLAPD_MODULES
41 typedef struct modpath_s {
42         struct modpath_s *mp_next;
43         struct berval mp_path;
44         BerVarray mp_loads;
45 } ModPaths;
46 #endif
47
48 typedef struct ConfigFile {
49         struct ConfigFile *c_sibs;
50         struct ConfigFile *c_kids;
51         struct berval c_file;
52 #ifdef SLAPD_MODULES
53         ModPaths c_modpaths;
54         ModPaths *c_modlast;
55 #endif
56         BerVarray c_dseFiles;
57 } ConfigFile;
58
59 typedef struct CfEntryInfo {
60         struct CfEntryInfo *ce_sibs;
61         struct CfEntryInfo *ce_kids;
62         Entry *ce_entry;
63         BackendInfo *ce_bi;
64         BackendDB *ce_be;
65 } CfEntryInfo;
66
67 typedef struct {
68         ConfigFile *cb_config;
69         CfEntryInfo *cb_root;
70 } CfBackInfo;
71
72 /* These do nothing in slapd, they're kept only to make them
73  * editable here.
74  */
75 static char *replica_pidFile, *replica_argsFile;
76 static int replicationInterval;
77
78 static char     *passwd_salt;
79 static char     *logfileName;
80 static BerVarray authz_rewrites;
81
82 static AttributeDescription *cfAd_backend, *cfAd_database, *cfAd_overlay,
83         *cfAd_include;
84
85 static ObjectClass *cfOc_global, *cfOc_backend, *cfOc_database,
86         *cfOc_include, *cfOc_overlay;
87
88 static ConfigFile cf_prv, *cfn = &cf_prv;
89
90 static int add_syncrepl LDAP_P(( Backend *, char **, int ));
91 static int parse_syncrepl_line LDAP_P(( char **, int, syncinfo_t *));
92 static void syncrepl_unparse LDAP_P (( syncinfo_t *, struct berval *));
93
94 static ConfigDriver config_fname;
95 static ConfigDriver config_generic;
96 static ConfigDriver config_search_base;
97 static ConfigDriver config_passwd_hash;
98 static ConfigDriver config_schema_dn;
99 static ConfigDriver config_sizelimit;
100 static ConfigDriver config_timelimit;
101 static ConfigDriver config_limits; 
102 static ConfigDriver config_overlay;
103 static ConfigDriver config_suffix; 
104 static ConfigDriver config_deref_depth;
105 static ConfigDriver config_rootdn;
106 static ConfigDriver config_rootpw;
107 static ConfigDriver config_restrict;
108 static ConfigDriver config_allows;
109 static ConfigDriver config_disallows;
110 static ConfigDriver config_requires;
111 static ConfigDriver config_security;
112 static ConfigDriver config_referral;
113 static ConfigDriver config_loglevel;
114 static ConfigDriver config_syncrepl;
115 static ConfigDriver config_replica;
116 static ConfigDriver config_updatedn;
117 static ConfigDriver config_updateref;
118 static ConfigDriver config_include;
119 #ifdef HAVE_TLS
120 static ConfigDriver config_tls_option;
121 static ConfigDriver config_tls_config;
122 #endif
123
124 enum {
125         CFG_ACL = 1,
126         CFG_BACKEND,
127         CFG_DATABASE,
128         CFG_TLS_RAND,
129         CFG_TLS_CIPHER,
130         CFG_TLS_CERT_FILE,
131         CFG_TLS_CERT_KEY,
132         CFG_TLS_CA_PATH,
133         CFG_TLS_CA_FILE,
134         CFG_TLS_VERIFY,
135         CFG_TLS_CRLCHECK,
136         CFG_SIZE,
137         CFG_TIME,
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_AUDITLOG,
152         CFG_REPLOG,
153         CFG_ROOTDSE,
154         CFG_LOGFILE,
155         CFG_PLUGIN,
156         CFG_MODLOAD,
157         CFG_MODPATH,
158         CFG_LASTMOD,
159         CFG_AZPOLICY,
160         CFG_AZREGEXP,
161         CFG_SASLSECP,
162         CFG_SSTR_IF_MAX,
163         CFG_SSTR_IF_MIN,
164 };
165
166 typedef struct {
167         char *name, *oid;
168 } OidRec;
169
170 static OidRec OidMacros[] = {
171         /* OpenLDAProot:666.11.1 */
172         { "OLcfg", "1.3.6.1.4.1.4203.666.11.1" },
173         { "OLcfgAt", "OLcfg:3" },
174         { "OLcfgOc", "OLcfg:4" },
175         { "OMsyn", "1.3.6.1.4.1.1466.115.121.1" },
176         { "OMsInteger", "OMsyn:2" },
177         { "OMsBoolean", "OMsyn:7" },
178         { "OMsDN", "OMsyn:12" },
179         { "OMsDirectoryString", "OMsyn:15" },
180         { "OMsOctetString", "OMsyn:40" },
181         { NULL, NULL }
182 };
183
184 /* alphabetical ordering */
185
186 ConfigTable config_back_cf_table[] = {
187         /* This attr is read-only */
188         { "", "", 0, 0, 0, ARG_MAGIC,
189                 &config_fname, "( OLcfgAt:78 NAME 'olcConfigFile' "
190                         "DESC 'File for slapd configuration directives' "
191                         "EQUALITY caseIgnoreMatch "
192                         "SYNTAX OMsDirectoryString )", NULL, NULL },
193         { "access",     NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_ACL,
194                 &config_generic, "( OLcfgAt:1 NAME 'olcAccess' "
195                         "DESC 'Access Control List' "
196                         "EQUALITY caseIgnoreMatch "
197                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
198         { "allows",     "features", 2, 0, 5, ARG_PRE_DB|ARG_MAGIC,
199                 &config_allows, "( OLcfgAt:2 NAME 'olcAllows' "
200                         "DESC 'Allowed set of deprecated features' "
201                         "EQUALITY caseIgnoreMatch "
202                         "SYNTAX OMsDirectoryString )", NULL, NULL },
203         { "argsfile", "file", 2, 2, 0, ARG_STRING,
204                 &slapd_args_file, "( OLcfgAt:3 NAME 'olcArgsFile' "
205                         "DESC 'File for slapd command line options' "
206                         "EQUALITY caseIgnoreMatch "
207                         "SYNTAX OMsDirectoryString )", NULL, NULL },
208         /* Use standard 'attributeTypes' attr */
209         { "attribute",  "attribute", 2, 0, 9, ARG_PAREN|ARG_MAGIC|CFG_ATTR,
210                 &config_generic, NULL, NULL, NULL },
211         { "attributeoptions", NULL, 0, 0, 0, ARG_MAGIC|CFG_ATOPT,
212                 &config_generic, "( OLcfgAt:5 NAME 'olcAttributeOptions' "
213                         "EQUALITY caseIgnoreMatch "
214                         "SYNTAX OMsDirectoryString )", NULL, NULL },
215         { "authid-rewrite", NULL, 2, 0, 0,
216 #ifdef SLAP_AUTH_REWRITE
217                 ARG_MAGIC|CFG_REWRITE, &config_generic,
218 #else
219                 ARG_IGNORED, NULL,
220 #endif
221                  "( OLcfgAt:6 NAME 'olcAuthIDRewrite' "
222                         "EQUALITY caseIgnoreMatch "
223                         "SYNTAX OMsDirectoryString )", NULL, NULL },
224         { "authz-policy", "policy", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_AZPOLICY,
225                 &config_generic, "( OLcfgAt:7 NAME 'olcAuthzPolicy' "
226                         "EQUALITY caseIgnoreMatch "
227                         "SYNTAX OMsDirectoryString )", NULL, NULL },
228         { "authz-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
229                 &config_generic, "( OLcfgAt:8 NAME 'olcAuthzRegexp' "
230                         "EQUALITY caseIgnoreMatch "
231                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
232         { "backend", "type", 2, 2, 0, ARG_PRE_DB|ARG_MAGIC|CFG_BACKEND,
233                 &config_generic, "( OLcfgAt:9 NAME 'olcBackend' "
234                         "DESC 'A type of backend' "
235                         "EQUALITY caseIgnoreMatch "
236                         "SYNTAX OMsDirectoryString )", NULL, NULL },
237         { "concurrency", "level", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_CONCUR,
238                 &config_generic, "( OLcfgAt:10 NAME 'olcConcurrency' "
239                         "SYNTAX OMsInteger )", NULL, NULL },
240         { "conn_max_pending", "max", 2, 2, 0, ARG_LONG,
241                 &slap_conn_max_pending, "( OLcfgAt:11 NAME 'olcConnMaxPending' "
242                         "SYNTAX OMsInteger )", NULL, NULL },
243         { "conn_max_pending_auth", "max", 2, 2, 0, ARG_LONG,
244                 &slap_conn_max_pending_auth, "( OLcfgAt:12 NAME 'olcConnMaxPendingAuth' "
245                         "SYNTAX OMsInteger )", NULL, NULL },
246         { "database", "type", 2, 2, 0, ARG_MAGIC|CFG_DATABASE,
247                 &config_generic, "( OLcfgAt:13 NAME 'olcDatabase' "
248                         "DESC 'The backend type for a database instance' "
249                         "SUP olcBackend )", NULL, NULL },
250         { "defaultSearchBase", "dn", 2, 2, 0, ARG_PRE_BI|ARG_PRE_DB|ARG_DN|ARG_MAGIC,
251                 &config_search_base, "( OLcfgAt:14 NAME 'olcDefaultSearchBase' "
252                         "SYNTAX OMsDN )", NULL, NULL },
253         { "disallows", "features", 2, 0, 8, ARG_PRE_DB|ARG_MAGIC,
254                 &config_disallows, "( OLcfgAt:15 NAME 'olcDisallows' "
255                         "EQUALITY caseIgnoreMatch "
256                         "SYNTAX OMsDirectoryString )", NULL, NULL },
257         /* use standard schema */
258         { "ditcontentrule",     NULL, 0, 0, 0, ARG_MAGIC|CFG_DIT,
259                 &config_generic, NULL, NULL, NULL },
260         { "gentlehup", "on|off", 2, 2, 0,
261 #ifdef SIGHUP
262                 ARG_ON_OFF, &global_gentlehup,
263 #else
264                 ARG_IGNORED, NULL,
265 #endif
266                 "( OLcfgAt:17 NAME 'olcGentleHUP' "
267                         "SYNTAX OMsBoolean )", NULL, NULL },
268         { "idletimeout", "timeout", 2, 2, 0, ARG_INT,
269                 &global_idletimeout, "( OLcfgAt:18 NAME 'olcIdleTimeout' "
270                         "SYNTAX OMsInteger )", NULL, NULL },
271 /* XXX -- special case? */
272         { "include", "file", 2, 2, 0, ARG_MAGIC,
273                 &config_include, "( OLcfgAt:19 NAME 'olcInclude' "
274                         "SUP labeledURI )", NULL, NULL },
275         { "index_substr_if_minlen", "min", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MIN,
276                 &config_generic, "( OLcfgAt:20 NAME 'olcIndexSubstrIfMinLen' "
277                         "SYNTAX OMsInteger )", NULL, NULL },
278         { "index_substr_if_maxlen", "max", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MAX,
279                 &config_generic, "( OLcfgAt:21 NAME 'olcIndexSubstrIfMaxLen' "
280                         "SYNTAX OMsInteger )", NULL, NULL },
281         { "index_substr_any_len", "len", 2, 2, 0, ARG_INT|ARG_NONZERO,
282                 &index_substr_any_len, "( OLcfgAt:22 NAME 'olcIndexSubstrAnyLen' "
283                         "SYNTAX OMsInteger )", NULL, NULL },
284         { "index_substr_step", "step", 2, 2, 0, ARG_INT|ARG_NONZERO,
285                 &index_substr_any_step, "( OLcfgAt:23 NAME 'olcIndexSubstrAnyStep' "
286                         "SYNTAX OMsInteger )", NULL, NULL },
287         { "lastmod", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_LASTMOD,
288                 &config_generic, "( OLcfgAt:24 NAME 'olcLastMod' "
289                         "SYNTAX OMsBoolean )", NULL, NULL },
290         { "limits", "limits", 2, 0, 0, ARG_DB|ARG_MAGIC|CFG_LIMITS,
291                 &config_generic, "( OLcfgAt:25 NAME 'olcLimits' "
292                         "SYNTAX OMsDirectoryString )", NULL, NULL },
293         { "localSSF", "ssf", 2, 2, 0, ARG_LONG,
294                 &local_ssf, "( OLcfgAt:26 NAME 'olcLocalSSF' "
295                         "SYNTAX OMsInteger )", NULL, NULL },
296         { "logfile", "file", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_LOGFILE,
297                 &config_generic, "( OLcfgAt:27 NAME 'olcLogFile' "
298                         "SYNTAX OMsDirectoryString )", NULL, NULL },
299         { "loglevel", "level", 2, 0, 0, ARG_MAGIC,
300                 &config_loglevel, "( OLcfgAt:28 NAME 'olcLogLevel' "
301                         "SYNTAX OMsDirectoryString )", NULL, NULL },
302         { "maxDerefDepth", "depth", 2, 2, 0, ARG_DB|ARG_INT|ARG_MAGIC|CFG_DEPTH,
303                 &config_generic, "( OLcfgAt:29 NAME 'olcMaxDerefDepth' "
304                         "SYNTAX OMsInteger )", NULL, NULL },
305         { "moduleload", "file", 2, 0, 0,
306 #ifdef SLAPD_MODULES
307                 ARG_MAGIC|CFG_MODLOAD, &config_generic,
308 #else
309                 ARG_IGNORED, NULL,
310 #endif
311                 "( OLcfgAt:30 NAME 'olcModuleLoad' "
312                         "SYNTAX OMsDirectoryString )", NULL, NULL },
313         { "modulepath", "path", 2, 2, 0,
314 #ifdef SLAPD_MODULES
315                 ARG_MAGIC|CFG_MODPATH, &config_generic,
316 #else
317                 ARG_IGNORED, NULL,
318 #endif
319                 "( OLcfgAt:31 NAME 'olcModulePath' "
320                         "SYNTAX OMsDirectoryString )", NULL, NULL },
321         /* use standard schema */
322         { "objectclass", "objectclass", 2, 0, 0, ARG_PAREN|ARG_MAGIC|CFG_OC,
323                 &config_generic, NULL, NULL, NULL },
324         { "objectidentifier", NULL,     0, 0, 0, ARG_MAGIC|CFG_OID,
325                 &config_generic, "( OLcfgAt:33 NAME 'olcObjectIdentifier' "
326                         "SYNTAX OMsDirectoryString )", NULL, NULL },
327         { "overlay", "overlay", 2, 2, 0, ARG_MAGIC,
328                 &config_overlay, "( OLcfgAt:34 NAME 'olcOverlay' "
329                         "SUP olcDatabase )", NULL, NULL },
330         { "password-crypt-salt-format", "salt", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_SALT,
331                 &config_generic, "( OLcfgAt:35 NAME 'olcPasswordCryptSaltFormat' "
332                         "SYNTAX OMsDirectoryString )", NULL, NULL },
333         { "password-hash", "hash", 2, 2, 0, ARG_MAGIC,
334                 &config_passwd_hash, "( OLcfgAt:36 NAME 'olcPasswordHash' "
335                         "SYNTAX OMsDirectoryString )", NULL, NULL },
336         { "pidfile", "file", 2, 2, 0, ARG_STRING,
337                 &slapd_pid_file, "( OLcfgAt:37 NAME 'olcPidFile' "
338                         "SYNTAX OMsDirectoryString )", NULL, NULL },
339         { "plugin", NULL, 0, 0, 0,
340 #ifdef LDAP_SLAPI
341                 ARG_MAGIC|CFG_PLUGIN, &config_generic,
342 #else
343                 ARG_IGNORED, NULL,
344 #endif
345                 "( OLcfgAt:38 NAME 'olcPlugin' "
346                         "SYNTAX OMsDirectoryString )", NULL, NULL },
347         { "pluginlog", "filename", 2, 2, 0,
348 #ifdef LDAP_SLAPI
349                 ARG_STRING, &slapi_log_file,
350 #else
351                 ARG_IGNORED, NULL,
352 #endif
353                 "( OLcfgAt:39 NAME 'olcPluginLogFile' "
354                         "SYNTAX OMsDirectoryString )", NULL, NULL },
355         { "readonly", "on|off", 2, 2, 0, ARG_MAY_DB|ARG_ON_OFF|ARG_MAGIC|CFG_RO,
356                 &config_generic, "( OLcfgAt:40 NAME 'olcReadOnly' "
357                         "SYNTAX OMsBoolean )", NULL, NULL },
358         { "referral", "url", 2, 2, 0, ARG_MAGIC,
359                 &config_referral, "( OLcfgAt:41 NAME 'olcReferral' "
360                         "SUP labeledURI )", NULL, NULL },
361         { "replica", "host or uri", 2, 0, 0, ARG_DB|ARG_MAGIC,
362                 &config_replica, "( OLcfgAt:42 NAME 'olcReplica' "
363                         "SUP labeledURI )", NULL, NULL },
364         { "replica-argsfile", NULL, 0, 0, 0, ARG_STRING,
365                 &replica_argsFile, "( OLcfgAt:43 NAME 'olcReplicaArgsFile' "
366                         "SYNTAX OMsDirectoryString )", NULL, NULL },
367         { "replica-pidfile", NULL, 0, 0, 0, ARG_STRING,
368                 &replica_pidFile, "( OLcfgAt:44 NAME 'olcReplicaPidFile' "
369                         "SYNTAX OMsDirectoryString )", NULL, NULL },
370         { "replicationInterval", NULL, 0, 0, 0, ARG_INT,
371                 &replicationInterval, "( OLcfgAt:45 NAME 'olcReplicationInterval' "
372                         "SYNTAX OMsInteger )", NULL, NULL },
373         { "replogfile", "filename", 2, 2, 0, ARG_MAY_DB|ARG_MAGIC|ARG_STRING|CFG_REPLOG,
374                 &config_generic, "( OLcfgAt:46 NAME 'olcReplogFile' "
375                         "SYNTAX OMsDirectoryString )", NULL, NULL },
376         { "require", "features", 2, 0, 7, ARG_MAY_DB|ARG_MAGIC,
377                 &config_requires, "( OLcfgAt:47 NAME 'olcRequires' "
378                         "SYNTAX OMsDirectoryString )", NULL, NULL },
379         { "restrict", "op_list", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
380                 &config_restrict, "( OLcfgAt:48 NAME 'olcRestrict' "
381                         "SYNTAX OMsDirectoryString )", NULL, NULL },
382         { "reverse-lookup", "on|off", 2, 2, 0,
383 #ifdef SLAPD_RLOOKUPS
384                 ARG_ON_OFF, &use_reverse_lookup,
385 #else
386                 ARG_IGNORED, NULL,
387 #endif
388                 "( OLcfgAt:49 NAME 'olcReverseLookup' "
389                         "SYNTAX OMsBoolean )", NULL, NULL },
390         { "rootdn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_MAGIC,
391                 &config_rootdn, "( OLcfgAt:50 NAME 'olcRootDN' "
392                         "SYNTAX OMsDN )", NULL, NULL },
393         { "rootDSE", "file", 2, 2, 0, ARG_MAGIC|CFG_ROOTDSE,
394                 &config_generic, "( OLcfgAt:51 NAME 'olcRootDSE' "
395                         "SYNTAX OMsDirectoryString )", NULL, NULL },
396         { "rootpw", "password", 2, 2, 0, ARG_STRING|ARG_DB|ARG_MAGIC,
397                 &config_rootpw, "( OLcfgAt:52 NAME 'olcRootPW' "
398                         "SYNTAX OMsOctetString )", NULL, NULL },
399         { "sasl-authz-policy", NULL, 2, 2, 0, ARG_MAGIC|CFG_AZPOLICY,
400                 &config_generic, NULL, NULL, NULL },
401         { "sasl-host", "host", 2, 2, 0,
402 #ifdef HAVE_CYRUS_SASL
403                 ARG_STRING|ARG_UNIQUE, &global_host,
404 #else
405                 ARG_IGNORED, NULL,
406 #endif
407                 "( OLcfgAt:53 NAME 'olcSaslHost' "
408                         "SYNTAX OMsDirectoryString )", NULL, NULL },
409         { "sasl-realm", "realm", 2, 2, 0,
410 #ifdef HAVE_CYRUS_SASL
411                 ARG_STRING|ARG_UNIQUE, &global_realm,
412 #else
413                 ARG_IGNORED, NULL,
414 #endif
415                 "( OLcfgAt:54 NAME 'olcSaslRealm' "
416                         "SYNTAX OMsDirectoryString )", NULL, NULL },
417         { "sasl-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
418                 &config_generic, NULL, NULL, NULL },
419         { "sasl-secprops", "properties", 2, 2, 0,
420 #ifdef HAVE_CYRUS_SASL
421                 ARG_MAGIC|CFG_SASLSECP, &config_generic,
422 #else
423                 ARG_IGNORED, NULL,
424 #endif
425                 "( OLcfgAt:56 NAME 'olcSaslSecProps' "
426                         "SYNTAX OMsDirectoryString )", NULL, NULL },
427         { "saslRegexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
428                 &config_generic, NULL, NULL, NULL },
429         { "schemacheck", "on|off", 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|CFG_CHECK,
430                 &config_generic, "( OLcfgAt:57 NAME 'olcSchemaCheck' "
431                         "SYNTAX OMsBoolean )", NULL, NULL },
432         { "schemadn", "dn", 2, 2, 0, ARG_MAY_DB|ARG_DN|ARG_MAGIC,
433                 &config_schema_dn, "( OLcfgAt:58 NAME 'olcSchemaDN' "
434                         "SYNTAX OMsDN )", NULL, NULL },
435         { "security", "factors", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
436                 &config_security, "( OLcfgAt:59 NAME 'olcSecurity' "
437                         "SYNTAX OMsDirectoryString )", NULL, NULL },
438         { "sizelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_SIZE,
439                 &config_sizelimit, "( OLcfgAt:60 NAME 'olcSizeLimit' "
440                         "SYNTAX OMsInteger )", NULL, NULL },
441         { "sockbuf_max_incoming", "max", 2, 2, 0, ARG_LONG,
442                 &sockbuf_max_incoming, "( OLcfgAt:61 NAME 'olcSockbufMaxIncoming' "
443                         "SYNTAX OMsInteger )", NULL, NULL },
444         { "sockbuf_max_incoming_auth", "max", 2, 2, 0, ARG_LONG,
445                 &sockbuf_max_incoming_auth, "( OLcfgAt:62 NAME 'olcSockbufMaxIncomingAuth' "
446                         "SYNTAX OMsInteger )", NULL, NULL },
447         { "srvtab", "file", 2, 2, 0,
448 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
449                 ARG_STRING, &ldap_srvtab,
450 #else
451                 ARG_IGNORED, NULL,
452 #endif
453                 "( OLcfgAt:63 NAME 'olcSrvtab' "
454                         "SYNTAX OMsDirectoryString )", NULL, NULL },
455         { "suffix",     "suffix", 2, 2, 0, ARG_DB|ARG_DN|ARG_MAGIC,
456                 &config_suffix, "( OLcfgAt:64 NAME 'olcSuffix' "
457                         "SYNTAX OMsDN )", NULL, NULL },
458         { "syncrepl", NULL, 0, 0, 0, ARG_DB|ARG_MAGIC,
459                 &config_syncrepl, "( OLcfgAt:65 NAME 'olcSyncrepl' "
460                         "SYNTAX OMsDirectoryString )", NULL, NULL },
461         { "threads", "count", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_THREADS,
462                 &config_generic, "( OLcfgAt:66 NAME 'olcThreads' "
463                         "SYNTAX OMsInteger )", NULL, NULL },
464         { "timelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_TIME,
465                 &config_timelimit, "( OLcfgAt:67 NAME 'olcTimeLimit' "
466                         "SYNTAX OMsInteger )", NULL, NULL },
467         { "TLSCACertificateFile", NULL, 0, 0, 0,
468 #ifdef HAVE_TLS
469                 CFG_TLS_CA_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
470 #else
471                 ARG_IGNORED, NULL,
472 #endif
473                 "( OLcfgAt:68 NAME 'olcTLSCACertificateFile' "
474                         "SYNTAX OMsDirectoryString )", NULL, NULL },
475         { "TLSCACertificatePath", NULL, 0, 0, 0,
476 #ifdef HAVE_TLS
477                 CFG_TLS_CA_PATH|ARG_STRING|ARG_MAGIC, &config_tls_option,
478 #else
479                 ARG_IGNORED, NULL,
480 #endif
481                 "( OLcfgAt:69 NAME 'olcTLSCACertificatePath' "
482                         "SYNTAX OMsDirectoryString )", NULL, NULL },
483         { "TLSCertificateFile", NULL, 0, 0, 0,
484 #ifdef HAVE_TLS
485                 CFG_TLS_CERT_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
486 #else
487                 ARG_IGNORED, NULL,
488 #endif
489                 "( OLcfgAt:70 NAME 'olcTLSCertificateFile' "
490                         "SYNTAX OMsDirectoryString )", NULL, NULL },
491         { "TLSCertificateKeyFile", NULL, 0, 0, 0,
492 #ifdef HAVE_TLS
493                 CFG_TLS_CERT_KEY|ARG_STRING|ARG_MAGIC, &config_tls_option,
494 #else
495                 ARG_IGNORED, NULL,
496 #endif
497                 "( OLcfgAt:71 NAME 'olcTLSCertificateKeyFile' "
498                         "SYNTAX OMsDirectoryString )", NULL, NULL },
499         { "TLSCipherSuite",     NULL, 0, 0, 0,
500 #ifdef HAVE_TLS
501                 CFG_TLS_CIPHER|ARG_STRING|ARG_MAGIC, &config_tls_option,
502 #else
503                 ARG_IGNORED, NULL,
504 #endif
505                 "( OLcfgAt:72 NAME 'olcTLSCipherSuite' "
506                         "SYNTAX OMsDirectoryString )", NULL, NULL },
507         { "TLSCRLCheck", NULL, 0, 0, 0,
508 #ifdef HAVE_TLS
509                 CFG_TLS_CRLCHECK|ARG_STRING|ARG_MAGIC, &config_tls_config,
510 #else
511                 ARG_IGNORED, NULL,
512 #endif
513                 "( OLcfgAt:73 NAME 'olcTLSCRLCheck' "
514                         "SYNTAX OMsDirectoryString )", NULL, NULL },
515         { "TLSRandFile", NULL, 0, 0, 0,
516 #ifdef HAVE_TLS
517                 CFG_TLS_RAND|ARG_STRING|ARG_MAGIC, &config_tls_option,
518 #else
519                 ARG_IGNORED, NULL,
520 #endif
521                 "( OLcfgAt:74 NAME 'olcTLSRandFile' "
522                         "SYNTAX OMsDirectoryString )", NULL, NULL },
523         { "TLSVerifyClient", NULL, 0, 0, 0,
524 #ifdef HAVE_TLS
525                 CFG_TLS_VERIFY|ARG_STRING|ARG_MAGIC, &config_tls_config,
526 #else
527                 ARG_IGNORED, NULL,
528 #endif
529                 "( OLcfgAt:75 NAME 'olcTLSVerifyClient' "
530                         "SYNTAX OMsDirectoryString )", NULL, NULL },
531         { "ucdata-path", "path", 2, 2, 0, ARG_IGNORED,
532                 NULL, NULL, NULL, NULL },
533         { "updatedn", "dn", 2, 2, 0, ARG_DB|ARG_MAGIC,
534                 &config_updatedn, "( OLcfgAt:76 NAME 'olcUpdateDN' "
535                         "SYNTAX OMsDN )", NULL, NULL },
536         { "updateref", "url", 2, 2, 0, ARG_DB|ARG_MAGIC,
537                 &config_updateref, "( OLcfgAt:77 NAME 'olcUpdateRef' "
538                         "SUP labeledURI )", NULL, NULL },
539         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
540                 NULL, NULL, NULL, NULL }
541 };
542
543 static ConfigOCs cf_ocs[] = {
544         { "( OLcfgOc:1 "
545                 "NAME 'olcConfig' "
546                 "DESC 'OpenLDAP configuration object' "
547                 "ABSTRACT SUP top "
548                 "MAY ( cn $ olcConfigFile ) )", NULL },
549         { "( OLcfgOc:3 "
550                 "NAME 'olcGlobal' "
551                 "DESC 'OpenLDAP Global configuration options' "
552                 "SUP olcConfig STRUCTURAL "
553                 "MAY ( olcAccess $ olcAllows $ olcArgsFile $ olcAttributeOptions $ "
554                  "olcAuthIDRewrite $ olcAuthzPolicy $ olcAuthzRegexp $ "
555                  "olcConcurrency $ olcConnMaxPending $ olcConnMaxPendingAuth $ "
556                  "olcDefaultSearchBase $ olcDisallows $ olcGentleHUP $ "
557                  "olcIdleTimeout $ olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
558                  "olcIndexSubstrAnyLen $ olcIndexSubstrAnyStep $ olcLocalSSF $ "
559                  "olcLogLevel $ olcModuleLoad $ olcModulePath $ olcObjectIdentifier $ "
560                  "olcPasswordCryptSaltFormat $ olcPasswordHash $ olcPidFile $ "
561                  "olcPlugin $ olcPluginLogFile $ olcReadOnly $ olcReferral $ "
562                  "olcReplicaPidFile $ olcReplicaArgsFile $ olcReplicationInterval $ "
563                  "olcReplogFile $ olcRequires $ olcRestrict $ olcReverseLookup $ "
564                  "olcRootDSE $ olcSaslHost $ olcSaslRealm $ olcSaslSecProps $ "
565                  "olcSchemaCheck $ olcSchemaDN $ olcSecurity $ olcSizeLimit $ "
566                  "olcSockbufMaxIncoming $ olcSockbufMaxIncomingAuth $ olcSrvtab $ "
567                  "olcThreads $ olcTimeLimit $ olcTLSCACertificateFile $ "
568                  "olcTLSCACertificatePath $ olcTLSCertificateFile $ "
569                  "olcTLSCertificateKeyFile $ olcTLSCipherSuite $ olcTLSCRLCheck $ "
570                  "olcTLSRandFile $ olcTLSVerifyClient ) )", &cfOc_global },
571         { "( OLcfgOc:4 "
572                 "NAME 'olcBackendConfig' "
573                 "DESC 'OpenLDAP Backend-specific options' "
574                 "SUP olcConfig STRUCTURAL "
575                 "MAY ( olcBackend ) )", &cfOc_backend },
576         { "( OLcfgOc:5 "
577                 "NAME 'olcDatabaseConfig' "
578                 "DESC 'OpenLDAP Database-specific options' "
579                 "SUP olcConfig STRUCTURAL "
580                 "MAY ( olcAccess $ olcDatabase $ olcLastMod $ olcLimits $ "
581                  "olcMaxDerefDepth $ olcPlugin $ olcReadOnly $ olcReplica $ "
582                  "olcReplogFile $ olcRequires $ olcRestrict $ olcRootDN $ olcRootPW $ "
583                  "olcSchemaDN $ olcSecurity $ olcSizeLimit $ olcSuffix $ olcSyncrepl $ "
584                  "olcTimeLimit $ olcUpdateDN $ olcUpdateRef ) )", &cfOc_database },
585         { "( OLcfgOc:6 "
586                 "NAME 'olcIncludeFile' "
587                 "DESC 'OpenLDAP configuration include file' "
588                 "SUP olcConfig STRUCTURAL "
589                 "MAY ( olcInclude $ olcModuleLoad $ olcModulePath $ olcRootDSE ) )",
590                 &cfOc_include },
591         { "( OLcfgOc:7 "
592                 "NAME 'olcOverlayConfig' "
593                 "DESC 'OpenLDAP Overlay-specific options' "
594                 "SUP olcConfig STRUCTURAL "
595                 "MAY ( olcOverlay ) )", &cfOc_overlay },
596         { NULL, NULL }
597 };
598
599 static int
600 config_generic(ConfigArgs *c) {
601         char *p;
602         int i;
603
604         if ( c->emit ) {
605                 int rc = 0;
606                 switch(c->type) {
607                 case CFG_CONCUR:
608                         c->value_int = ldap_pvt_thread_get_concurrency();
609                         break;
610                 case CFG_THREADS:
611                         c->value_int = connection_pool_max;
612                         break;
613                 case CFG_SALT:
614                         if ( passwd_salt )
615                                 c->value_string = ch_strdup( passwd_salt );
616                         else
617                                 rc = 1;
618                         break;
619                 case CFG_LIMITS:
620                         if ( c->be->be_limits ) {
621                                 char buf[4096*3];
622                                 struct berval bv;
623                                 int i;
624
625                                 for ( i=0; c->be->be_limits[i]; i++ ) {
626                                         bv.bv_len = sprintf( buf, "{%d}", i );
627                                         bv.bv_val = buf+bv.bv_len;
628                                         limits_unparse( c->be->be_limits[i], &bv );
629                                         bv.bv_len += bv.bv_val - buf;
630                                         bv.bv_val = buf;
631                                         value_add_one( &c->rvalue_vals, &bv );
632                                 }
633                         }
634                         if ( !c->rvalue_vals ) rc = 1;
635                         break;
636                 case CFG_RO:
637                         c->value_int = (c->be->be_restrictops & SLAP_RESTRICT_OP_WRITES) != 0;
638                         break;
639                 case CFG_AZPOLICY:
640                         c->value_string = ch_strdup( slap_sasl_getpolicy());
641                         break;
642                 case CFG_AZREGEXP:
643                         slap_sasl_regexp_unparse( &c->rvalue_vals );
644                         if ( !c->rvalue_vals ) rc = 1;
645                         break;
646 #ifdef HAVE_CYRUS_SASL
647                 case CFG_SASLSECP: {
648                         struct berval bv = BER_BVNULL;
649                         slap_sasl_secprops_unparse( &bv );
650                         if ( !BER_BVISNULL( &bv )) {
651                                 ber_bvarray_add( &c->rvalue_vals, &bv );
652                         } else {
653                                 rc = 1;
654                         }
655                         }
656                         break;
657 #endif
658                 case CFG_DEPTH:
659                         c->value_int = c->be->be_max_deref_depth;
660                         break;
661                 case CFG_OID:
662                         oidm_unparse( &c->rvalue_vals );
663                         if ( !c->rvalue_vals )
664                                 rc = 1;
665                         break;
666                 case CFG_CHECK:
667                         c->value_int = global_schemacheck;
668                         break;
669                 case CFG_ACL: {
670                         AccessControl *a;
671                         char *src, *dst, ibuf[11];
672                         struct berval bv, abv;
673                         for (i=0, a=c->be->be_acl; a; i++,a=a->acl_next) {
674                                 abv.bv_len = sprintf( ibuf, "{%x}", i );
675                                 acl_unparse( a, &bv );
676                                 abv.bv_val = ch_malloc( abv.bv_len + bv.bv_len + 1 );
677                                 AC_MEMCPY( abv.bv_val, ibuf, abv.bv_len );
678                                 /* Turn TAB / EOL into plain space */
679                                 for (src=bv.bv_val,dst=abv.bv_val+abv.bv_len; *src; src++) {
680                                         if (isspace(*src)) *dst++ = ' ';
681                                         else *dst++ = *src;
682                                 }
683                                 *dst = '\0';
684                                 if (dst[-1] == ' ') {
685                                         dst--;
686                                         *dst = '\0';
687                                 }
688                                 abv.bv_len = dst - abv.bv_val;
689                                 ber_bvarray_add( &c->rvalue_vals, &abv );
690                         }
691                         rc = (!i);
692                         break;
693                 }
694                 case CFG_REPLOG:
695                         if ( c->be->be_replogfile )
696                                 c->value_string = ch_strdup( c->be->be_replogfile );
697                         break;
698                 case CFG_ROOTDSE: {
699                         ConfigFile *cf = (ConfigFile *)c->line;
700                         if ( cf->c_dseFiles ) {
701                                 value_add( &c->rvalue_vals, cf->c_dseFiles );
702                         } else {
703                                 rc = 1;
704                         }
705                         }
706                         break;
707                 case CFG_LOGFILE:
708                         if ( logfileName )
709                                 c->value_string = ch_strdup( logfileName );
710                         else
711                                 rc = 1;
712                         break;
713                 case CFG_LASTMOD:
714                         c->value_int = (SLAP_NOLASTMOD(c->be) == 0);
715                         break;
716                 case CFG_SSTR_IF_MAX:
717                         c->value_int = index_substr_if_maxlen;
718                         break;
719                 case CFG_SSTR_IF_MIN:
720                         c->value_int = index_substr_if_minlen;
721                         break;
722 #ifdef SLAPD_MODULES
723                 case CFG_MODLOAD: {
724                         ConfigFile *cf = (ConfigFile *)c->line;
725                         ModPaths *mp;
726                         for (i=0, mp=&cf->c_modpaths; mp; mp=mp->mp_next, i++) {
727                                 int j;
728                                 if (!mp->mp_loads) continue;
729                                 for (j=0; !BER_BVISNULL(&mp->mp_loads[j]); j++) {
730                                         struct berval bv;
731                                         bv.bv_val = c->log;
732                                         bv.bv_len = sprintf( bv.bv_val, "{%d}{%d}%s", i, j,
733                                                 mp->mp_loads[j].bv_val );
734                                         value_add_one( &c->rvalue_vals, &bv );
735                                 }
736                         }
737                         rc = c->rvalue_vals ? 0 : 1;
738                         }
739                         break;
740                 case CFG_MODPATH: {
741                         ConfigFile *cf = (ConfigFile *)c->line;
742                         ModPaths *mp;
743                         for (i=0, mp=&cf->c_modpaths; mp; mp=mp->mp_next, i++) {
744                                 struct berval bv;
745                                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
746                                         continue;
747                                 bv.bv_val = c->log;
748                                 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
749                                         mp->mp_path.bv_val );
750                                 value_add_one( &c->rvalue_vals, &bv );
751                         }
752                         rc = c->rvalue_vals ? 0 : 1;
753                         }
754                         break;
755 #endif
756 #ifdef LDAP_SLAPI
757                 case CFG_PLUGIN:
758                         slapi_int_plugin_unparse( c->be, &c->rvalue_vals );
759                         if ( !c->rvalue_vals ) rc = 1;
760                         break;
761 #endif
762 #ifdef SLAP_AUTH_REWRITE
763                 case CFG_REWRITE:
764                         if ( authz_rewrites ) {
765                                 struct berval bv, idx;
766                                 char ibuf[32];
767                                 int i;
768
769                                 idx.bv_val = ibuf;
770                                 for ( i=0; !BER_BVISNULL( &authz_rewrites[i] ); i++ ) {
771                                         idx.bv_len = sprintf( idx.bv_val, "{%d}", i );
772                                         bv.bv_len = idx.bv_len + authz_rewrites[i].bv_len;
773                                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
774                                         strcpy( bv.bv_val, idx.bv_val );
775                                         strcpy( bv.bv_val+idx.bv_len, authz_rewrites[i].bv_val );
776                                         ber_bvarray_add( &c->rvalue_vals, &bv );
777                                 }
778                         }
779                         if ( !c->rvalue_vals ) rc = 1;
780                         break;
781 #endif
782                 default:
783                         rc = 1;
784                 }
785                 return rc;
786         }
787
788         p = strchr(c->line,'(' /*')'*/);
789         switch(c->type) {
790                 case CFG_BACKEND:
791                         if(!(c->bi = backend_info(c->argv[1]))) {
792                                 Debug(LDAP_DEBUG_ANY, "%s: "
793                                         "backend %s failed init!\n", c->log, c->argv[1], 0);
794                                 return(1);
795                         }
796                         break;
797
798                 case CFG_DATABASE:
799                         c->bi = NULL;
800                         /* NOTE: config is always the first backend!
801                          */
802                         if ( !strcasecmp( c->argv[1], "config" )) {
803                                 c->be = backendDB;
804                         } else if(!(c->be = backend_db_init(c->argv[1]))) {
805                                 Debug(LDAP_DEBUG_ANY, "%s: "
806                                         "database %s failed init!\n", c->log, c->argv[1], 0);
807                                 return(1);
808                         }
809                         break;
810
811                 case CFG_CONCUR:
812                         ldap_pvt_thread_set_concurrency(c->value_int);
813                         break;
814
815                 case CFG_THREADS:
816                         ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
817                         connection_pool_max = c->value_int;     /* save for reference */
818                         break;
819
820                 case CFG_SALT:
821                         if ( passwd_salt ) ch_free( passwd_salt );
822                         passwd_salt = c->value_string;
823                         lutil_salt_format(passwd_salt);
824                         break;
825
826                 case CFG_LIMITS:
827                         if(limits_parse(c->be, c->fname, c->lineno, c->argc, c->argv))
828                                 return(1);
829                         break;
830
831                 case CFG_RO:
832                         if(c->value_int)
833                                 c->be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
834                         else
835                                 c->be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
836                         break;
837
838                 case CFG_AZPOLICY:
839                         ch_free(c->value_string);
840                         if (slap_sasl_setpolicy( c->argv[1] )) {
841                                 Debug(LDAP_DEBUG_ANY, "%s: unable to parse value \"%s\" in"
842                                         " \"authz-policy <policy>\"\n",
843                                         c->log, c->argv[1], 0 );
844                                 return(1);
845                         }
846                         break;
847                 
848                 case CFG_AZREGEXP:
849                         if (slap_sasl_regexp_config( c->argv[1], c->argv[2] ))
850                                 return(1);
851                         break;
852                                 
853 #ifdef HAVE_CYRUS_SASL
854                 case CFG_SASLSECP:
855                         {
856                         char *txt = slap_sasl_secprops( c->argv[1] );
857                         if ( txt ) {
858                                 Debug(LDAP_DEBUG_ANY, "%s: sasl-secprops: %s\n",
859                                         c->log, txt, 0 );
860                                 return(1);
861                         }
862                         break;
863                         }
864 #endif
865
866                 case CFG_DEPTH:
867                         c->be->be_max_deref_depth = c->value_int;
868                         break;
869
870                 case CFG_OID:
871                         if(parse_oidm(c->fname, c->lineno, c->argc, c->argv)) return(1);
872                         break;
873
874                 case CFG_OC:
875                         if(parse_oc(c->fname, c->lineno, p, c->argv)) return(1);
876                         break;
877
878                 case CFG_DIT:
879                         if(parse_cr(c->fname, c->lineno, p, c->argv)) return(1);
880                         break;
881
882                 case CFG_ATTR:
883                         if(parse_at(c->fname, c->lineno, p, c->argv)) return(1);
884                         break;
885
886                 case CFG_ATOPT:
887                         ad_define_option(NULL, NULL, 0);
888                         for(i = 1; i < c->argc; i++)
889                                 if(ad_define_option(c->argv[i], c->fname, c->lineno))
890                                         return(1);
891                         break;
892
893                 case CFG_CHECK:
894                         global_schemacheck = c->value_int;
895                         if(!global_schemacheck) Debug(LDAP_DEBUG_ANY, "%s: "
896                                 "schema checking disabled! your mileage may vary!\n",
897                                 c->log, 0, 0);
898                         break;
899
900                 case CFG_ACL:
901                         parse_acl(c->be, c->fname, c->lineno, c->argc, c->argv);
902                         break;
903
904                 case CFG_REPLOG:
905                         if(SLAP_MONITOR(c->be)) {
906                                 Debug(LDAP_DEBUG_ANY, "%s: "
907                                         "\"replogfile\" should not be used "
908                                         "inside monitor database\n",
909                                         c->log, 0, 0);
910                                 return(0);      /* FIXME: should this be an error? */
911                         }
912
913                         c->be->be_replogfile = c->value_string;
914                         break;
915
916                 case CFG_ROOTDSE:
917                         if(read_root_dse_file(c->argv[1])) {
918                                 Debug(LDAP_DEBUG_ANY, "%s: "
919                                         "could not read \"rootDSE <filename>\" line\n",
920                                         c->log, 0, 0);
921                                 return(1);
922                         }
923                         {
924                                 struct berval bv;
925                                 ber_str2bv( c->argv[1], 0, 1, &bv );
926                                 ber_bvarray_add( &cfn->c_dseFiles, &bv );
927                         }
928                         break;
929
930                 case CFG_LOGFILE: {
931                                 FILE *logfile;
932                                 if ( logfileName ) ch_free( logfileName );
933                                 logfileName = c->value_string;
934                                 logfile = fopen(logfileName, "w");
935                                 if(logfile) lutil_debug_file(logfile);
936                         } break;
937
938                 case CFG_LASTMOD:
939                         if(SLAP_NOLASTMODCMD(c->be)) {
940                                 Debug(LDAP_DEBUG_ANY, "%s: "
941                                         "lastmod not available for %s databases\n",
942                                         c->log, c->be->bd_info->bi_type, 0);
943                                 return(1);
944                         }
945                         if(c->value_int)
946                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_NOLASTMOD;
947                         else
948                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NOLASTMOD;
949                         break;
950
951                 case CFG_SSTR_IF_MAX:
952                         if (c->value_int < index_substr_if_minlen) {
953                                 Debug(LDAP_DEBUG_ANY, "%s: "
954                                         "invalid max value (%d)\n",
955                                         c->log, c->value_int, 0 );
956                                 return(1);
957                         }
958                         index_substr_if_maxlen = c->value_int;
959                         break;
960
961                 case CFG_SSTR_IF_MIN:
962                         if (c->value_int > index_substr_if_maxlen) {
963                                 Debug(LDAP_DEBUG_ANY, "%s: "
964                                         "invalid min value (%d)\n",
965                                         c->log, c->value_int, 0 );
966                                 return(1);
967                         }
968                         index_substr_if_minlen = c->value_int;
969                         break;
970
971 #ifdef SLAPD_MODULES
972                 case CFG_MODLOAD:
973                         if(module_load(c->argv[1], c->argc - 2, (c->argc > 2) ? c->argv + 2 : NULL))
974                                 return(1);
975                         /* Record this load on the current path */
976                         {
977                                 struct berval bv;
978                                 ber_str2bv(c->line, 0, 1, &bv);
979                                 ber_bvarray_add( &cfn->c_modlast->mp_loads, &bv );
980                         }
981                         break;
982
983                 case CFG_MODPATH:
984                         if(module_path(c->argv[1])) return(1);
985                         /* Record which path was used with each module */
986                         {
987                                 ModPaths *mp;
988
989                                 if (!cfn->c_modpaths.mp_loads) {
990                                         mp = &cfn->c_modpaths;
991                                 } else {
992                                         mp = ch_malloc( sizeof( ModPaths ));
993                                         cfn->c_modlast->mp_next = mp;
994                                 }
995                                 ber_str2bv(c->argv[1], 0, 1, &mp->mp_path);
996                                 mp->mp_next = NULL;
997                                 mp->mp_loads = NULL;
998                                 cfn->c_modlast = mp;
999                         }
1000                         
1001                         break;
1002 #endif
1003
1004 #ifdef LDAP_SLAPI
1005                 case CFG_PLUGIN:
1006                         if(slapi_int_read_config(c->be, c->fname, c->lineno, c->argc, c->argv) != LDAP_SUCCESS)
1007                                 return(1);
1008                         slapi_plugins_used++;
1009                         break;
1010 #endif
1011
1012 #ifdef SLAP_AUTH_REWRITE
1013                 case CFG_REWRITE: {
1014                         struct berval bv;
1015                         if(slap_sasl_rewrite_config(c->fname, c->lineno, c->argc, c->argv))
1016                                 return(1);
1017                         ber_str2bv( c->line, 0, 1, &bv );
1018                         ber_bvarray_add( &authz_rewrites, &bv );
1019                         }
1020                         break;
1021 #endif
1022
1023
1024                 default:
1025                         Debug(LDAP_DEBUG_ANY, "%s: unknown CFG_TYPE %d"
1026                                 "(ignored)\n", c->log, c->type, 0);
1027
1028         }
1029         return(0);
1030 }
1031
1032
1033 static int
1034 config_fname(ConfigArgs *c) {
1035         if(c->emit && c->line) {
1036                 ConfigFile *cf = (ConfigFile *)c->line;
1037                 value_add_one( &c->rvalue_vals, &cf->c_file );
1038                 return 0;
1039         }
1040         return(1);
1041 }
1042
1043 static int
1044 config_search_base(ConfigArgs *c) {
1045         struct berval dn;
1046
1047         if(c->emit) {
1048                 int rc = 1;
1049                 if (!BER_BVISEMPTY(&default_search_base)) {
1050                         value_add_one(&c->rvalue_vals, &default_search_base);
1051                         value_add_one(&c->rvalue_nvals, &default_search_nbase);
1052                         rc = 0;
1053                 }
1054                 return rc;
1055         }
1056
1057         if(c->bi || c->be != frontendDB) {
1058                 Debug(LDAP_DEBUG_ANY, "%s: defaultSearchBase line must appear "
1059                         "prior to any backend or database definition\n",
1060                         c->log, 0, 0);
1061                 return(1);
1062         }
1063
1064         if(default_search_nbase.bv_len) {
1065                 Debug(LDAP_DEBUG_ANY, "%s: "
1066                         "default search base \"%s\" already defined "
1067                         "(discarding old)\n",
1068                         c->log, default_search_base.bv_val, 0);
1069                 free(default_search_base.bv_val);
1070                 free(default_search_nbase.bv_val);
1071         }
1072
1073         default_search_base = c->value_dn;
1074         default_search_nbase = c->value_ndn;
1075         return(0);
1076 }
1077
1078 static int
1079 config_passwd_hash(ConfigArgs *c) {
1080         int i;
1081         if (c->emit) {
1082                 struct berval bv;
1083                 for (i=0; default_passwd_hash && default_passwd_hash[i]; i++) {
1084                         ber_str2bv(default_passwd_hash[i], 0, 0, &bv);
1085                         value_add_one(&c->rvalue_vals, &bv);
1086                 }
1087                 return i ? 0 : 1;
1088         }
1089         if(default_passwd_hash) {
1090                 Debug(LDAP_DEBUG_ANY, "%s: "
1091                         "already set default password_hash\n",
1092                         c->log, 0, 0);
1093                 return(1);
1094         }
1095         for(i = 1; i < c->argc; i++) {
1096                 if(!lutil_passwd_scheme(c->argv[i])) {
1097                         Debug(LDAP_DEBUG_ANY, "%s: "
1098                                 "password scheme \"%s\" not available\n",
1099                                 c->log, c->argv[i], 0);
1100                 } else {
1101                         ldap_charray_add(&default_passwd_hash, c->argv[i]);
1102                 }
1103                 if(!default_passwd_hash) {
1104                         Debug(LDAP_DEBUG_ANY, "%s: no valid hashes found\n",
1105                                 c->log, 0, 0 );
1106                         return(1);
1107                 }
1108         }
1109         return(0);
1110 }
1111
1112 static int
1113 config_schema_dn(ConfigArgs *c) {
1114         struct berval dn;
1115         int rc;
1116         if ( c->emit ) {
1117                 value_add_one(&c->rvalue_vals, &c->be->be_schemadn);
1118                 value_add_one(&c->rvalue_nvals, &c->be->be_schemandn);
1119                 return 0;
1120         }
1121         c->be->be_schemadn = c->value_dn;
1122         c->be->be_schemandn = c->value_ndn;
1123         return(0);
1124 }
1125
1126 static int
1127 config_sizelimit(ConfigArgs *c) {
1128         int i, rc = 0;
1129         char *next;
1130         struct slap_limits_set *lim = &c->be->be_def_limit;
1131         if (c->emit) {
1132                 char buf[8192];
1133                 struct berval bv;
1134                 bv.bv_val = buf;
1135                 bv.bv_len = 0;
1136                 limits_unparse_one( lim, SLAP_LIMIT_SIZE, &bv );
1137                 if ( !BER_BVISEMPTY( &bv ))
1138                         value_add_one( &c->rvalue_vals, &bv );
1139                 else
1140                         rc = 1;
1141                 return rc;
1142         }
1143         for(i = 1; i < c->argc; i++) {
1144                 if(!strncasecmp(c->argv[i], "size", 4)) {
1145                         rc = limits_parse_one(c->argv[i], lim);
1146                         if ( rc ) {
1147                                 Debug(LDAP_DEBUG_ANY, "%s: "
1148                                         "unable to parse value \"%s\" in \"sizelimit <limit>\" line\n",
1149                                         c->log, c->argv[i], 0);
1150                                 return(1);
1151                         }
1152                 } else {
1153                         if(!strcasecmp(c->argv[i], "unlimited")) {
1154                                 lim->lms_s_soft = -1;
1155                         } else {
1156                                 lim->lms_s_soft = strtol(c->argv[i], &next, 0);
1157                                 if(next == c->argv[i]) {
1158                                         Debug(LDAP_DEBUG_ANY, "%s: "
1159                                                 "unable to parse limit \"%s\" in \"sizelimit <limit>\" line\n",
1160                                                 c->log, c->argv[i], 0);
1161                                         return(1);
1162                                 } else if(next[0] != '\0') {
1163                                         Debug(LDAP_DEBUG_ANY, "%s: "
1164                                                 "trailing chars \"%s\" in \"sizelimit <limit>\" line (ignored)\n",
1165                                                 c->log, next, 0);
1166                                 }
1167                         }
1168                         lim->lms_s_hard = 0;
1169                 }
1170         }
1171         return(0);
1172 }
1173
1174 static int
1175 config_timelimit(ConfigArgs *c) {
1176         int i, rc = 0;
1177         char *next;
1178         struct slap_limits_set *lim = &c->be->be_def_limit;
1179         if (c->emit) {
1180                 char buf[8192];
1181                 struct berval bv;
1182                 bv.bv_val = buf;
1183                 bv.bv_len = 0;
1184                 limits_unparse_one( lim, SLAP_LIMIT_TIME, &bv );
1185                 if ( !BER_BVISEMPTY( &bv ))
1186                         value_add_one( &c->rvalue_vals, &bv );
1187                 else
1188                         rc = 1;
1189                 return rc;
1190         }
1191         for(i = 1; i < c->argc; i++) {
1192                 if(!strncasecmp(c->argv[i], "time", 4)) {
1193                         rc = limits_parse_one(c->argv[i], lim);
1194                         if ( rc ) {
1195                                 Debug(LDAP_DEBUG_ANY, "%s: "
1196                                         "unable to parse value \"%s\" in \"timelimit <limit>\" line\n",
1197                                         c->log, c->argv[i], 0);
1198                                 return(1);
1199                         }
1200                 } else {
1201                         if(!strcasecmp(c->argv[i], "unlimited")) {
1202                                 lim->lms_t_soft = -1;
1203                         } else {
1204                                 lim->lms_t_soft = strtol(c->argv[i], &next, 0);
1205                                 if(next == c->argv[i]) {
1206                                         Debug(LDAP_DEBUG_ANY, "%s: "
1207                                                 "unable to parse limit \"%s\" in \"timelimit <limit>\" line\n",
1208                                                 c->log, c->argv[i], 0);
1209                                         return(1);
1210                                 } else if(next[0] != '\0') {
1211                                         Debug(LDAP_DEBUG_ANY, "%s: "
1212                                                 "trailing chars \"%s\" in \"timelimit <limit>\" line (ignored)\n",
1213                                                 c->log, next, 0);
1214                                 }
1215                         }
1216                         lim->lms_t_hard = 0;
1217                 }
1218         }
1219         return(0);
1220 }
1221
1222 static int
1223 config_overlay(ConfigArgs *c) {
1224         if (c->emit) {
1225                 return 1;
1226         }
1227         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1])) {
1228                 /* log error */
1229                 Debug(LDAP_DEBUG_ANY, "%s: (optional) %s overlay \"%s\" configuration failed (ignored)\n",
1230                         c->log, c->be == frontendDB ? "global " : "", c->argv[1][1]);
1231         } else if(overlay_config(c->be, c->argv[1])) {
1232                 return(1);
1233         }
1234         return(0);
1235 }
1236
1237 static int
1238 config_suffix(ConfigArgs *c) {
1239         Backend *tbe;
1240         struct berval pdn, ndn;
1241         int rc;
1242         if (c->emit) {
1243                 if ( !BER_BVISNULL( &c->be->be_suffix[0] )) {
1244                         value_add( &c->rvalue_vals, c->be->be_suffix );
1245                         value_add( &c->rvalue_nvals, c->be->be_nsuffix );
1246                         return 0;
1247                 } else {
1248                         return 1;
1249                 }
1250         }
1251 #ifdef SLAPD_MONITOR_DN
1252         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
1253                 Debug(LDAP_DEBUG_ANY, "%s: "
1254                         "\"%s\" is reserved for monitoring slapd\n",
1255                         c->log, SLAPD_MONITOR_DN, 0);
1256                 return(1);
1257         }
1258 #endif
1259
1260         pdn = c->value_dn;
1261         ndn = c->value_ndn;
1262         tbe = select_backend(&ndn, 0, 0);
1263         if(tbe == c->be) {
1264                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by this backend! (ignored)\n",
1265                         c->log, 0, 0);
1266                 free(pdn.bv_val);
1267                 free(ndn.bv_val);
1268         } else if(tbe) {
1269                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by a preceding backend \"%s\"\n",
1270                         c->log, tbe->be_suffix[0].bv_val, 0);
1271                 free(pdn.bv_val);
1272                 free(ndn.bv_val);
1273                 return(1);
1274         } else if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
1275                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
1276                         "base provided \"%s\" (assuming okay)\n",
1277                         c->log, default_search_base.bv_val, 0);
1278         }
1279         ber_bvarray_add(&c->be->be_suffix, &pdn);
1280         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
1281         return(0);
1282 }
1283
1284 static int
1285 config_rootdn(ConfigArgs *c) {
1286         if (c->emit) {
1287                 if ( !BER_BVISNULL( &c->be->be_rootdn )) {
1288                         value_add_one(&c->rvalue_vals, &c->be->be_rootdn);
1289                         value_add_one(&c->rvalue_nvals, &c->be->be_rootndn);
1290                         return 0;
1291                 } else {
1292                         return 1;
1293                 }
1294         }
1295         c->be->be_rootdn = c->value_dn;
1296         c->be->be_rootndn = c->value_ndn;
1297         return(0);
1298 }
1299
1300 static int
1301 config_rootpw(ConfigArgs *c) {
1302         Backend *tbe;
1303         if (c->emit) {
1304                 if (!BER_BVISEMPTY(&c->be->be_rootpw)) {
1305                         c->value_string=ch_strdup("*");
1306                         return 0;
1307                 }
1308                 return 1;
1309         }
1310
1311         tbe = select_backend(&c->be->be_rootndn, 0, 0);
1312         if(tbe != c->be) {
1313                 Debug(LDAP_DEBUG_ANY, "%s: "
1314                         "rootpw can only be set when rootdn is under suffix\n",
1315                         c->log, 0, 0);
1316                 return(1);
1317         }
1318         ber_str2bv(c->value_string, 0, 0, &c->be->be_rootpw);
1319         return(0);
1320 }
1321
1322 static int
1323 config_restrict(ConfigArgs *c) {
1324         slap_mask_t restrictops = 0;
1325         int i;
1326         struct verb_mask_list restrictable_ops[] = {
1327                 { "bind",               SLAP_RESTRICT_OP_BIND },
1328                 { "add",                SLAP_RESTRICT_OP_ADD },
1329                 { "modify",             SLAP_RESTRICT_OP_MODIFY },
1330                 { "rename",             SLAP_RESTRICT_OP_RENAME },
1331                 { "modrdn",             0 },
1332                 { "delete",             SLAP_RESTRICT_OP_DELETE },
1333                 { "search",             SLAP_RESTRICT_OP_SEARCH },
1334                 { "compare",    SLAP_RESTRICT_OP_COMPARE },
1335                 { "read",               SLAP_RESTRICT_OP_READS },
1336                 { "write",              SLAP_RESTRICT_OP_WRITES },
1337                 { "extended",   SLAP_RESTRICT_OP_EXTENDED },
1338                 { "extended=" LDAP_EXOP_START_TLS,              SLAP_RESTRICT_EXOP_START_TLS },
1339                 { "extended=" LDAP_EXOP_MODIFY_PASSWD,  SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
1340                 { "extended=" LDAP_EXOP_X_WHO_AM_I,             SLAP_RESTRICT_EXOP_WHOAMI },
1341                 { "extended=" LDAP_EXOP_X_CANCEL,               SLAP_RESTRICT_EXOP_CANCEL },
1342                 { NULL, 0 }
1343         };
1344
1345         if (c->emit) {
1346                 return mask_to_verbs( c, restrictable_ops, c->be->be_restrictops );
1347         }
1348         i = verbs_to_mask( c, restrictable_ops, &restrictops );
1349         if ( i ) {
1350                 Debug(LDAP_DEBUG_ANY, "%s: "
1351                         "unknown operation %s in \"restrict <features>\" line\n",
1352                         c->log, c->argv[i], 0);
1353                 return(1);
1354         }
1355         if ( restrictops & SLAP_RESTRICT_OP_EXTENDED )
1356                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
1357         c->be->be_restrictops |= restrictops;
1358         return(0);
1359 }
1360
1361 static int
1362 config_allows(ConfigArgs *c) {
1363         slap_mask_t allows = 0;
1364         int i;
1365         struct verb_mask_list allowable_ops[] = {
1366                 { "bind_v2",            SLAP_ALLOW_BIND_V2 },
1367                 { "bind_anon_cred",     SLAP_ALLOW_BIND_ANON_CRED },
1368                 { "bind_anon_dn",       SLAP_ALLOW_BIND_ANON_DN },
1369                 { "update_anon",        SLAP_ALLOW_UPDATE_ANON },
1370                 { NULL, 0 }
1371         };
1372         if (c->emit) {
1373                 return mask_to_verbs( c, allowable_ops, global_allows );
1374         }
1375         i = verbs_to_mask(c, allowable_ops, &allows);
1376         if ( i ) {
1377                 Debug(LDAP_DEBUG_ANY, "%s: "
1378                         "unknown feature %s in \"allow <features>\" line\n",
1379                         c->log, c->argv[i], 0);
1380                 return(1);
1381         }
1382         global_allows |= allows;
1383         return(0);
1384 }
1385
1386 static int
1387 config_disallows(ConfigArgs *c) {
1388         slap_mask_t disallows = 0;
1389         int i;
1390         struct verb_mask_list disallowable_ops[] = {
1391                 { "bind_anon",          SLAP_DISALLOW_BIND_ANON },
1392                 { "bind_simple",        SLAP_DISALLOW_BIND_SIMPLE },
1393                 { "bind_krb4",          SLAP_DISALLOW_BIND_KRBV4 },
1394                 { "tls_2_anon",         SLAP_DISALLOW_TLS_2_ANON },
1395                 { "tls_authc",          SLAP_DISALLOW_TLS_AUTHC },
1396                 { NULL, 0 }
1397         };
1398         if (c->emit) {
1399                 return mask_to_verbs( c, disallowable_ops, global_disallows );
1400         }
1401         i = verbs_to_mask(c, disallowable_ops, &disallows);
1402         if ( i ) {
1403                 Debug(LDAP_DEBUG_ANY, "%s: "
1404                         "unknown feature %s in \"disallow <features>\" line\n",
1405                         c->log, c->argv[i], 0);
1406                 return(1);
1407         }
1408         global_disallows |= disallows;
1409         return(0);
1410 }
1411
1412 static int
1413 config_requires(ConfigArgs *c) {
1414         slap_mask_t requires = 0;
1415         int i;
1416         struct verb_mask_list requires_ops[] = {
1417                 { "bind",               SLAP_REQUIRE_BIND },
1418                 { "LDAPv3",             SLAP_REQUIRE_LDAP_V3 },
1419                 { "authc",              SLAP_REQUIRE_AUTHC },
1420                 { "sasl",               SLAP_REQUIRE_SASL },
1421                 { "strong",             SLAP_REQUIRE_STRONG },
1422                 { NULL, 0 }
1423         };
1424         if (c->emit) {
1425                 return mask_to_verbs( c, requires_ops, c->be->be_requires );
1426         }
1427         i = verbs_to_mask(c, requires_ops, &requires);
1428         if ( i ) {
1429                 Debug(LDAP_DEBUG_ANY, "%s: "
1430                         "unknown feature %s in \"require <features>\" line\n",
1431                         c->log, c->argv[i], 0);
1432                 return(1);
1433         }
1434         c->be->be_requires = requires;
1435         return(0);
1436 }
1437
1438 static int
1439 config_loglevel(ConfigArgs *c) {
1440         int i;
1441         char *next;
1442         struct verb_mask_list loglevel_ops[] = {
1443                 { "Trace",      LDAP_DEBUG_TRACE },
1444                 { "Packets",    LDAP_DEBUG_PACKETS },
1445                 { "Args",       LDAP_DEBUG_ARGS },
1446                 { "Conns",      LDAP_DEBUG_CONNS },
1447                 { "BER",        LDAP_DEBUG_BER },
1448                 { "Filter",     LDAP_DEBUG_FILTER },
1449                 { "Config",     LDAP_DEBUG_CONFIG },
1450                 { "ACL",        LDAP_DEBUG_ACL },
1451                 { "Stats",      LDAP_DEBUG_STATS },
1452                 { "Stats2",     LDAP_DEBUG_STATS2 },
1453                 { "Shell",      LDAP_DEBUG_SHELL },
1454                 { "Parse",      LDAP_DEBUG_PARSE },
1455                 { "Cache",      LDAP_DEBUG_CACHE },
1456                 { "Index",      LDAP_DEBUG_INDEX },
1457                 { "Any",        -1 },
1458                 { NULL, 0 }
1459         };
1460
1461         if (c->emit) {
1462                 return mask_to_verbs( c, loglevel_ops, ldap_syslog );
1463         }
1464
1465         ldap_syslog = 0;
1466
1467         for( i=1; i < c->argc; i++ ) {
1468                 int     level;
1469
1470                 if ( isdigit( c->argv[i][0] ) ) {
1471                         level = strtol( c->argv[i], &next, 10 );
1472                         if ( next == NULL || next[0] != '\0' ) {
1473                                 Debug( LDAP_DEBUG_ANY,
1474                                         "%s: unable to parse level \"%s\" "
1475                                         "in \"loglevel <level> [...]\" line.\n",
1476                                         c->log, c->argv[i], 0);
1477                                 return( 1 );
1478                         }
1479                 } else {
1480                         int j = verb_to_mask(c, loglevel_ops, c->argv[i][0]);
1481                         if(!loglevel_ops[j].word) {
1482                                 Debug( LDAP_DEBUG_ANY,
1483                                         "%s: unknown level \"%s\" "
1484                                         "in \"loglevel <level> [...]\" line.\n",
1485                                         c->log, c->argv[i], 0);
1486                                 return( 1 );
1487                         }
1488                         level = loglevel_ops[j].mask;
1489                 }
1490                 ldap_syslog |= level;
1491         }
1492         return(0);
1493 }
1494
1495 static int
1496 config_syncrepl(ConfigArgs *c) {
1497         if (c->emit) {
1498                 if ( c->be->be_syncinfo ) {
1499                         struct berval bv;
1500                         syncrepl_unparse( c->be->be_syncinfo, &bv ); 
1501                         ber_bvarray_add( &c->rvalue_vals, &bv );
1502                         return 0;
1503                 }
1504                 return 1;
1505         }
1506         if(SLAP_SHADOW(c->be)) {
1507                 Debug(LDAP_DEBUG_ANY, "%s: "
1508                         "syncrepl: database already shadowed.\n",
1509                         c->log, 0, 0);
1510                 return(1);
1511         } else if(add_syncrepl(c->be, c->argv, c->argc)) {
1512                 return(1);
1513         }
1514         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);
1515         return(0);
1516 }
1517
1518 static int
1519 config_referral(ConfigArgs *c) {
1520         struct berval vals[2];
1521         if (c->emit) {
1522                 if ( default_referral ) {
1523                         value_add( &c->rvalue_vals, default_referral );
1524                         return 0;
1525                 } else {
1526                         return 1;
1527                 }
1528         }
1529         if(validate_global_referral(c->argv[1])) {
1530                 Debug(LDAP_DEBUG_ANY, "%s: "
1531                         "invalid URL (%s) in \"referral\" line.\n",
1532                         c->log, c->argv[1], 0);
1533                 return(1);
1534         }
1535
1536         ber_str2bv(c->argv[1], 0, 0, &vals[0]);
1537         vals[1].bv_val = NULL; vals[1].bv_len = 0;
1538         if(value_add(&default_referral, vals)) return(LDAP_OTHER);
1539         return(0);
1540 }
1541
1542 static struct {
1543         struct berval key;
1544         int off;
1545 } sec_keys[] = {
1546         { BER_BVC("ssf="), offsetof(slap_ssf_set_t, sss_ssf) },
1547         { BER_BVC("transport="), offsetof(slap_ssf_set_t, sss_transport) },
1548         { BER_BVC("tls="), offsetof(slap_ssf_set_t, sss_tls) },
1549         { BER_BVC("sasl="), offsetof(slap_ssf_set_t, sss_sasl) },
1550         { BER_BVC("update_ssf="), offsetof(slap_ssf_set_t, sss_update_ssf) },
1551         { BER_BVC("update_transport="), offsetof(slap_ssf_set_t, sss_update_transport) },
1552         { BER_BVC("update_tls="), offsetof(slap_ssf_set_t, sss_update_tls) },
1553         { BER_BVC("update_sasl="), offsetof(slap_ssf_set_t, sss_update_sasl) },
1554         { BER_BVC("simple_bind="), offsetof(slap_ssf_set_t, sss_simple_bind) },
1555         { BER_BVNULL, 0 }
1556 };
1557
1558 static int
1559 config_security(ConfigArgs *c) {
1560         slap_ssf_set_t *set = &c->be->be_ssf_set;
1561         char *next;
1562         int i, j;
1563         if (c->emit) {
1564                 char numbuf[32];
1565                 struct berval bv;
1566                 slap_ssf_t *tgt;
1567                 int rc = 1;
1568
1569                 for (i=0; !BER_BVISNULL( &sec_keys[i].key ); i++) {
1570                         tgt = (slap_ssf_t *)((char *)set + sec_keys[i].off);
1571                         if ( *tgt ) {
1572                                 rc = 0;
1573                                 bv.bv_len = sprintf( numbuf, "%u", *tgt );
1574                                 bv.bv_len += sec_keys[i].key.bv_len;
1575                                 bv.bv_val = ch_malloc( bv.bv_len + 1);
1576                                 next = lutil_strcopy( bv.bv_val, sec_keys[i].key.bv_val );
1577                                 strcpy( next, numbuf );
1578                                 ber_bvarray_add( &c->rvalue_vals, &bv );
1579                         }
1580                 }
1581                 return rc;
1582         }
1583         for(i = 1; i < c->argc; i++) {
1584                 slap_ssf_t *tgt = NULL;
1585                 char *src;
1586                 for ( j=0; !BER_BVISNULL( &sec_keys[j].key ); j++ ) {
1587                         if(!strncasecmp(c->argv[i], sec_keys[j].key.bv_val,
1588                                 sec_keys[j].key.bv_len)) {
1589                                 src = c->argv[i] + sec_keys[j].key.bv_len;
1590                                 tgt = (slap_ssf_t *)((char *)set + sec_keys[j].off);
1591                                 break;
1592                         }
1593                 }
1594                 if ( !tgt ) {
1595                         Debug(LDAP_DEBUG_ANY, "%s: "
1596                                 "unknown factor %s in \"security <factors>\" line\n",
1597                                 c->log, c->argv[i], 0);
1598                         return(1);
1599                 }
1600
1601                 *tgt = strtol(src, &next, 10);
1602                 if(next == NULL || next[0] != '\0' ) {
1603                         Debug(LDAP_DEBUG_ANY, "%s: "
1604                                 "unable to parse factor \"%s\" in \"security <factors>\" line\n",
1605                                 c->log, c->argv[i], 0);
1606                         return(1);
1607                 }
1608         }
1609         return(0);
1610 }
1611
1612 static char *
1613 anlist_unparse( AttributeName *an, char *ptr ) {
1614         int comma = 0;
1615
1616         for (; !BER_BVISNULL( &an->an_name ); an++) {
1617                 if ( comma ) *ptr++ = ',';
1618                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
1619                 comma = 1;
1620         }
1621         return ptr;
1622 }
1623
1624 static void
1625 replica_unparse( struct slap_replica_info *ri, int i, struct berval *bv )
1626 {
1627         int len;
1628         char *ptr;
1629         struct berval bc = {0};
1630         char numbuf[32];
1631
1632         len = sprintf(numbuf, "{%d}", i );
1633
1634         len += strlen( ri->ri_uri ) + STRLENOF("uri=");
1635         if ( ri->ri_nsuffix ) {
1636                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
1637                         len += ri->ri_nsuffix[i].bv_len + STRLENOF(" suffix=\"\"");
1638                 }
1639         }
1640         if ( ri->ri_attrs ) {
1641                 len += STRLENOF("attr");
1642                 if ( ri->ri_exclude ) len++;
1643                 for (i=0; !BER_BVISNULL( &ri->ri_attrs[i].an_name ); i++) {
1644                         len += 1 + ri->ri_attrs[i].an_name.bv_len;
1645                 }
1646         }
1647         bindconf_unparse( &ri->ri_bindconf, &bc );
1648         len += bc.bv_len;
1649
1650         bv->bv_val = ch_malloc(len + 1);
1651         bv->bv_len = len;
1652
1653         ptr = lutil_strcopy( bv->bv_val, numbuf );
1654         ptr = lutil_strcopy( ptr, "uri=" );
1655         ptr = lutil_strcopy( ptr, ri->ri_uri );
1656
1657         if ( ri->ri_nsuffix ) {
1658                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
1659                         ptr = lutil_strcopy( ptr, " suffix=\"" );
1660                         ptr = lutil_strcopy( ptr, ri->ri_nsuffix[i].bv_val );
1661                         *ptr++ = '"';
1662                 }
1663         }
1664         if ( ri->ri_attrs ) {
1665                 ptr = lutil_strcopy( ptr, "attr" );
1666                 if ( ri->ri_exclude ) *ptr++ = '!';
1667                 *ptr++ = '=';
1668                 ptr = anlist_unparse( ri->ri_attrs, ptr );
1669         }
1670         if ( bc.bv_val ) {
1671                 strcpy( ptr, bc.bv_val );
1672                 ch_free( bc.bv_val );
1673         }
1674 }
1675
1676 static int
1677 config_replica(ConfigArgs *c) {
1678         int i, nr = -1, len;
1679         char *replicahost, *replicauri;
1680         LDAPURLDesc *ludp;
1681
1682         if (c->emit) {
1683                 if (c->be->be_replica) {
1684                         struct berval bv;
1685                         for (i=0;c->be->be_replica[i]; i++) {
1686                                 replica_unparse( c->be->be_replica[i], i, &bv );
1687                                 ber_bvarray_add( &c->rvalue_vals, &bv );
1688                         }
1689                         return 0;
1690                 }
1691                 return 1;
1692         }
1693         if(SLAP_MONITOR(c->be)) {
1694                 Debug(LDAP_DEBUG_ANY, "%s: "
1695                         "\"replica\" should not be used inside monitor database\n",
1696                         c->log, 0, 0);
1697                 return(0);      /* FIXME: should this be an error? */
1698         }
1699
1700         for(i = 1; i < c->argc; i++) {
1701                 if(!strncasecmp(c->argv[i], "host=", STRLENOF("host="))) {
1702                         replicahost = c->argv[i] + STRLENOF("host=");
1703                         len = strlen( replicahost );
1704                         replicauri = ch_malloc( len + STRLENOF("ldap://") + 1 );
1705                         sprintf( replicauri, "ldap://%s", replicahost );
1706                         replicahost = replicauri + STRLENOF( "ldap://");
1707                         nr = add_replica_info(c->be, replicauri, replicahost);
1708                         break;
1709                 } else if(!strncasecmp(c->argv[i], "uri=", STRLENOF("uri="))) {
1710                         if(ldap_url_parse(c->argv[i] + STRLENOF("uri="), &ludp) != LDAP_SUCCESS) {
1711                                 Debug(LDAP_DEBUG_ANY, "%s: "
1712                                         "replica line contains invalid "
1713                                         "uri definition.\n", c->log, 0, 0);
1714                                 return(1);
1715                         }
1716                         if(!ludp->lud_host) {
1717                                 Debug(LDAP_DEBUG_ANY, "%s: "
1718                                         "replica line contains invalid "
1719                                         "uri definition - missing hostname.\n",
1720                                         c->log, 0, 0);
1721                                 return(1);
1722                         }
1723                         ldap_free_urldesc(ludp);
1724                         replicauri = c->argv[i] + STRLENOF("uri=");
1725                         replicauri = ch_strdup( replicauri );
1726                         replicahost = strchr( replicauri, '/' );
1727                         replicahost += 2;
1728                         nr = add_replica_info(c->be, replicauri, replicahost);
1729                         break;
1730                 }
1731         }
1732         if(i == c->argc) {
1733                 Debug(LDAP_DEBUG_ANY, "%s: "
1734                         "missing host or uri in \"replica\" line\n",
1735                         c->log, 0, 0);
1736                 return(1);
1737         } else if(nr == -1) {
1738                 Debug(LDAP_DEBUG_ANY, "%s: "
1739                         "unable to add replica \"%s\"\n",
1740                         c->log, replicauri, 0);
1741                 return(1);
1742         } else {
1743                 for(i = 1; i < c->argc; i++) {
1744                         if(!strncasecmp(c->argv[i], "suffix=", STRLENOF( "suffix="))) {
1745                                 switch(add_replica_suffix(c->be, nr, c->argv[i] + STRLENOF("suffix="))) {
1746                                         case 1:
1747                                                 Debug(LDAP_DEBUG_ANY, "%s: "
1748                                                 "suffix \"%s\" in \"replica\" line is not valid for backend (ignored)\n",
1749                                                 c->log, c->argv[i] + STRLENOF("suffix="), 0);
1750                                                 break;
1751                                         case 2:
1752                                                 Debug(LDAP_DEBUG_ANY, "%s: "
1753                                                 "unable to normalize suffix in \"replica\" line (ignored)\n",
1754                                                 c->log, 0, 0);
1755                                                 break;
1756                                 }
1757
1758                         } else if(!strncasecmp(c->argv[i], "attr", STRLENOF("attr"))) {
1759                                 int exclude = 0;
1760                                 char *arg = c->argv[i] + STRLENOF("attr");
1761                                 if(arg[0] == '!') {
1762                                         arg++;
1763                                         exclude = 1;
1764                                 }
1765                                 if(arg[0] != '=') {
1766                                         continue;
1767                                 }
1768                                 if(add_replica_attrs(c->be, nr, arg + 1, exclude)) {
1769                                         Debug(LDAP_DEBUG_ANY, "%s: "
1770                                                 "attribute \"%s\" in \"replica\" line is unknown\n",
1771                                                 c->log, arg + 1, 0);
1772                                         return(1);
1773                                 }
1774                         } else if ( bindconf_parse( c->argv[i],
1775                                         &c->be->be_replica[nr]->ri_bindconf ) ) {
1776                                 return(1);
1777                         }
1778                 }
1779         }
1780         return(0);
1781 }
1782
1783 static int
1784 config_updatedn(ConfigArgs *c) {
1785         struct berval dn;
1786         int rc;
1787         if (c->emit) {
1788                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
1789                         value_add_one(&c->rvalue_vals, &c->be->be_update_ndn);
1790                         value_add_one(&c->rvalue_nvals, &c->be->be_update_ndn);
1791                         return 0;
1792                 }
1793                 return 1;
1794         }
1795         if(SLAP_SHADOW(c->be)) {
1796                 Debug(LDAP_DEBUG_ANY, "%s: "
1797                         "updatedn: database already shadowed.\n",
1798                         c->log, 0, 0);
1799                 return(1);
1800         }
1801
1802         ber_str2bv(c->argv[1], 0, 0, &dn);
1803
1804         rc = dnNormalize(0, NULL, NULL, &dn, &c->be->be_update_ndn, NULL);
1805
1806         if(rc != LDAP_SUCCESS) {
1807                 Debug(LDAP_DEBUG_ANY, "%s: "
1808                         "updatedn DN is invalid: %d (%s)\n",
1809                         c->log, rc, ldap_err2string( rc ));
1810                 return(1);
1811         }
1812
1813         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
1814         return(0);
1815 }
1816
1817 static int
1818 config_updateref(ConfigArgs *c) {
1819         struct berval vals[2];
1820         if (c->emit) {
1821                 if ( c->be->be_update_refs ) {
1822                         value_add( &c->rvalue_vals, c->be->be_update_refs );
1823                         return 0;
1824                 } else {
1825                         return 1;
1826                 }
1827         }
1828         if(!SLAP_SHADOW(c->be)) {
1829                 Debug(LDAP_DEBUG_ANY, "%s: "
1830                         "updateref line must come after syncrepl or updatedn.\n",
1831                         c->log, 0, 0);
1832                 return(1);
1833         }
1834
1835         if(validate_global_referral(c->argv[1])) {
1836                 Debug(LDAP_DEBUG_ANY, "%s: "
1837                         "invalid URL (%s) in \"updateref\" line.\n",
1838                         c->log, c->argv[1], 0);
1839                 return(1);
1840         }
1841         ber_str2bv(c->argv[1], 0, 0, &vals[0]);
1842         vals[1].bv_val = NULL;
1843         if(value_add(&c->be->be_update_refs, vals)) return(LDAP_OTHER);
1844         return(0);
1845 }
1846
1847 static int
1848 config_include(ConfigArgs *c) {
1849         unsigned long savelineno = c->lineno;
1850         int rc;
1851         ConfigFile *cf;
1852         ConfigFile *cfsave = cfn;
1853         ConfigFile *cf2 = NULL;
1854         if (c->emit) {
1855                 return 1;
1856         }
1857         cf = ch_calloc( 1, sizeof(ConfigFile));
1858 #ifdef SLAPD_MODULES
1859         cf->c_modlast = &cf->c_modpaths;
1860 #endif
1861         if ( cfn->c_kids ) {
1862                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
1863                 cf2->c_sibs = cf;
1864         } else {
1865                 cfn->c_kids = cf;
1866         }
1867         cfn = cf;
1868         rc = read_config_file(c->argv[1], c->depth + 1, c);
1869         c->lineno = savelineno - 1;
1870         cfn = cfsave;
1871         if ( rc ) {
1872                 if ( cf2 ) cf2->c_sibs = NULL;
1873                 else cfn->c_kids = NULL;
1874                 ch_free( cf );
1875         } else {
1876                 ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
1877         }
1878         return(rc);
1879 }
1880
1881 #ifdef HAVE_TLS
1882 static int
1883 config_tls_option(ConfigArgs *c) {
1884         int flag;
1885         switch(c->type) {
1886         case CFG_TLS_RAND:              flag = LDAP_OPT_X_TLS_RANDOM_FILE;      break;
1887         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
1888         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
1889         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
1890         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
1891         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
1892         default:                Debug(LDAP_DEBUG_ANY, "%s: "
1893                                         "unknown tls_option <%x>\n",
1894                                         c->log, c->type, 0);
1895         }
1896         if (c->emit) {
1897                 return ldap_pvt_tls_get_option( NULL, flag, &c->value_string );
1898         }
1899         ch_free(c->value_string);
1900         return(ldap_pvt_tls_set_option(NULL, flag, c->argv[1]));
1901 }
1902
1903 /* FIXME: this ought to be provided by libldap */
1904 static int
1905 config_tls_config(ConfigArgs *c) {
1906         int i, flag;
1907         struct verb_mask_list crlkeys[] = {
1908                 { "none",       LDAP_OPT_X_TLS_CRL_NONE },
1909                 { "peer",       LDAP_OPT_X_TLS_CRL_PEER },
1910                 { "all",        LDAP_OPT_X_TLS_CRL_ALL },
1911                 { NULL, 0 }
1912         };
1913         struct verb_mask_list vfykeys[] = {
1914                 { "never",      LDAP_OPT_X_TLS_NEVER },
1915                 { "demand",     LDAP_OPT_X_TLS_DEMAND },
1916                 { "try",        LDAP_OPT_X_TLS_TRY },
1917                 { "hard",       LDAP_OPT_X_TLS_HARD },
1918                 { NULL, 0 }
1919         }, *keys;
1920         switch(c->type) {
1921 #ifdef HAVE_OPENSSL_CRL
1922         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK; keys = crlkeys;
1923                 break;
1924 #endif
1925         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT; keys = vfykeys;
1926                 break;
1927         default:                Debug(LDAP_DEBUG_ANY, "%s: "
1928                                         "unknown tls_option <%x>\n",
1929                                         c->log, c->type, 0);
1930         }
1931         if (c->emit) {
1932                 ldap_pvt_tls_get_option( NULL, flag, &c->value_int );
1933                 for (i=0; keys[i].word; i++) {
1934                         if (keys[i].mask == c->value_int) {
1935                                 c->value_string = ch_strdup( keys[i].word );
1936                                 return 0;
1937                         }
1938                 }
1939                 return 1;
1940         }
1941         ch_free( c->value_string );
1942         if(isdigit((unsigned char)c->argv[1][0])) {
1943                 i = atoi(c->argv[1]);
1944                 return(ldap_pvt_tls_set_option(NULL, flag, &i));
1945         } else {
1946                 return(ldap_int_tls_config(NULL, flag, c->argv[1]));
1947         }
1948 }
1949 #endif
1950
1951 static int
1952 add_syncrepl(
1953         Backend *be,
1954         char    **cargv,
1955         int     cargc
1956 )
1957 {
1958         syncinfo_t *si;
1959         int     rc = 0;
1960
1961         si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
1962
1963         if ( si == NULL ) {
1964                 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
1965                 return 1;
1966         }
1967
1968         si->si_bindconf.sb_tls = SB_TLS_OFF;
1969         si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
1970         si->si_schemachecking = 0;
1971         ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
1972                 &si->si_filterstr );
1973         si->si_base.bv_val = NULL;
1974         si->si_scope = LDAP_SCOPE_SUBTREE;
1975         si->si_attrsonly = 0;
1976         si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
1977         si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
1978         si->si_attrs = NULL;
1979         si->si_allattrs = 0;
1980         si->si_allopattrs = 0;
1981         si->si_exattrs = NULL;
1982         si->si_type = LDAP_SYNC_REFRESH_ONLY;
1983         si->si_interval = 86400;
1984         si->si_retryinterval = NULL;
1985         si->si_retrynum_init = NULL;
1986         si->si_retrynum = NULL;
1987         si->si_manageDSAit = 0;
1988         si->si_tlimit = 0;
1989         si->si_slimit = 0;
1990
1991         si->si_presentlist = NULL;
1992         LDAP_LIST_INIT( &si->si_nonpresentlist );
1993         ldap_pvt_thread_mutex_init( &si->si_mutex );
1994
1995         rc = parse_syncrepl_line( cargv, cargc, si );
1996
1997         if ( rc < 0 ) {
1998                 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
1999                 syncinfo_free( si );    
2000                 return 1;
2001         } else {
2002                 Debug( LDAP_DEBUG_CONFIG,
2003                         "Config: ** successfully added syncrepl \"%s\"\n",
2004                         BER_BVISNULL( &si->si_provideruri ) ?
2005                         "(null)" : si->si_provideruri.bv_val, 0, 0 );
2006                 if ( !si->si_schemachecking ) {
2007                         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
2008                 }
2009                 si->si_be = be;
2010                 be->be_syncinfo = si;
2011                 return 0;
2012         }
2013 }
2014
2015 /* NOTE: used & documented in slapd.conf(5) */
2016 #define IDSTR                   "rid"
2017 #define PROVIDERSTR             "provider"
2018 #define TYPESTR                 "type"
2019 #define INTERVALSTR             "interval"
2020 #define SEARCHBASESTR           "searchbase"
2021 #define FILTERSTR               "filter"
2022 #define SCOPESTR                "scope"
2023 #define ATTRSSTR                "attrs"
2024 #define ATTRSONLYSTR            "attrsonly"
2025 #define SLIMITSTR               "sizelimit"
2026 #define TLIMITSTR               "timelimit"
2027 #define SCHEMASTR               "schemachecking"
2028
2029 /* FIXME: undocumented */
2030 #define OLDAUTHCSTR             "bindprincipal"
2031 #define EXATTRSSTR              "exattrs"
2032 #define RETRYSTR                "retry"
2033
2034 /* FIXME: unused */
2035 #define LASTMODSTR              "lastmod"
2036 #define LMGENSTR                "gen"
2037 #define LMNOSTR                 "no"
2038 #define LMREQSTR                "req"
2039 #define SRVTABSTR               "srvtab"
2040 #define SUFFIXSTR               "suffix"
2041 #define MANAGEDSAITSTR          "manageDSAit"
2042
2043 /* mandatory */
2044 #define GOT_ID                  0x0001
2045 #define GOT_PROVIDER            0x0002
2046
2047 /* check */
2048 #define GOT_ALL                 (GOT_ID|GOT_PROVIDER)
2049
2050 static struct {
2051         struct berval key;
2052         int val;
2053 } scopes[] = {
2054         { BER_BVC("base"), LDAP_SCOPE_BASE },
2055         { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2056 #ifdef LDAP_SCOPE_SUBORDINATE
2057         { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2058         { BER_BVC("subordinate"), 0 },
2059 #endif
2060         { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2061         { BER_BVNULL, 0 }
2062 };
2063
2064 static int
2065 parse_syncrepl_line(
2066         char            **cargv,
2067         int             cargc,
2068         syncinfo_t      *si
2069 )
2070 {
2071         int     gots = 0;
2072         int     i;
2073         char    *val;
2074
2075         for ( i = 1; i < cargc; i++ ) {
2076                 if ( !strncasecmp( cargv[ i ], IDSTR "=",
2077                                         STRLENOF( IDSTR "=" ) ) )
2078                 {
2079                         int tmp;
2080                         /* '\0' string terminator accounts for '=' */
2081                         val = cargv[ i ] + STRLENOF( IDSTR "=" );
2082                         tmp= atoi( val );
2083                         if ( tmp >= 1000 || tmp < 0 ) {
2084                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2085                                          "syncrepl id %d is out of range [0..999]\n", tmp );
2086                                 return -1;
2087                         }
2088                         si->si_rid = tmp;
2089                         gots |= GOT_ID;
2090                 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
2091                                         STRLENOF( PROVIDERSTR "=" ) ) )
2092                 {
2093                         val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
2094                         ber_str2bv( val, 0, 1, &si->si_provideruri );
2095                         gots |= GOT_PROVIDER;
2096                 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
2097                                         STRLENOF( SCHEMASTR "=" ) ) )
2098                 {
2099                         val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
2100                         if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2101                                 si->si_schemachecking = 1;
2102                         } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2103                                 si->si_schemachecking = 0;
2104                         } else {
2105                                 si->si_schemachecking = 1;
2106                         }
2107                 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
2108                                         STRLENOF( FILTERSTR "=" ) ) )
2109                 {
2110                         val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
2111                         ber_str2bv( val, 0, 1, &si->si_filterstr );
2112                 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
2113                                         STRLENOF( SEARCHBASESTR "=" ) ) )
2114                 {
2115                         struct berval   bv;
2116                         int             rc;
2117
2118                         val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2119                         if ( si->si_base.bv_val ) {
2120                                 ch_free( si->si_base.bv_val );
2121                         }
2122                         ber_str2bv( val, 0, 0, &bv );
2123                         rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2124                         if ( rc != LDAP_SUCCESS ) {
2125                                 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
2126                                         val, rc, ldap_err2string( rc ) );
2127                                 return -1;
2128                         }
2129                 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
2130                                         STRLENOF( SCOPESTR "=" ) ) )
2131                 {
2132                         int j;
2133                         val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
2134                         for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2135                                 if (!strncasecmp( val, scopes[j].key.bv_val,
2136                                         scopes[j].key.bv_len )) {
2137                                         while (!scopes[j].val) j--;
2138                                         si->si_scope = scopes[j].val;
2139                                         break;
2140                                 }
2141                         }
2142                         if ( BER_BVISNULL(&scopes[j].key) ) {
2143                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2144                                         "unknown scope \"%s\"\n", val);
2145                                 return -1;
2146                         }
2147                 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
2148                                         STRLENOF( ATTRSONLYSTR "=" ) ) )
2149                 {
2150                         si->si_attrsonly = 1;
2151                 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
2152                                         STRLENOF( ATTRSSTR "=" ) ) )
2153                 {
2154                         val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
2155                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2156                                 char *attr_fname;
2157                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2158                                 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2159                                 if ( si->si_anlist == NULL ) {
2160                                         ch_free( attr_fname );
2161                                         return -1;
2162                                 }
2163                                 si->si_anfile = attr_fname;
2164                         } else {
2165                                 char *str, *s, *next;
2166                                 char delimstr[] = " ,\t";
2167                                 str = ch_strdup( val );
2168                                 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2169                                                 s != NULL;
2170                                                 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2171                                 {
2172                                         if ( strlen(s) == 1 && *s == '*' ) {
2173                                                 si->si_allattrs = 1;
2174                                                 *(val + ( s - str )) = delimstr[0];
2175                                         }
2176                                         if ( strlen(s) == 1 && *s == '+' ) {
2177                                                 si->si_allopattrs = 1;
2178                                                 *(val + ( s - str )) = delimstr[0];
2179                                         }
2180                                 }
2181                                 ch_free( str );
2182                                 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2183                                 if ( si->si_anlist == NULL ) {
2184                                         return -1;
2185                                 }
2186                         }
2187                 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
2188                                         STRLENOF( EXATTRSSTR "=" ) ) )
2189                 {
2190                         val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
2191                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2192                                 char *attr_fname;
2193                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2194                                 si->si_exanlist = file2anlist(
2195                                                                         si->si_exanlist, attr_fname, " ,\t" );
2196                                 if ( si->si_exanlist == NULL ) {
2197                                         ch_free( attr_fname );
2198                                         return -1;
2199                                 }
2200                                 ch_free( attr_fname );
2201                         } else {
2202                                 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2203                                 if ( si->si_exanlist == NULL ) {
2204                                         return -1;
2205                                 }
2206                         }
2207                 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
2208                                         STRLENOF( TYPESTR "=" ) ) )
2209                 {
2210                         val = cargv[ i ] + STRLENOF( TYPESTR "=" );
2211                         if ( !strncasecmp( val, "refreshOnly",
2212                                                 STRLENOF("refreshOnly") ))
2213                         {
2214                                 si->si_type = LDAP_SYNC_REFRESH_ONLY;
2215                         } else if ( !strncasecmp( val, "refreshAndPersist",
2216                                                 STRLENOF("refreshAndPersist") ))
2217                         {
2218                                 si->si_type = LDAP_SYNC_REFRESH_AND_PERSIST;
2219                                 si->si_interval = 60;
2220                         } else {
2221                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2222                                         "unknown sync type \"%s\"\n", val);
2223                                 return -1;
2224                         }
2225                 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2226                                         STRLENOF( INTERVALSTR "=" ) ) )
2227                 {
2228                         val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2229                         if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2230                                 si->si_interval = 0;
2231                         } else {
2232                                 char *hstr;
2233                                 char *mstr;
2234                                 char *dstr;
2235                                 char *sstr;
2236                                 int dd, hh, mm, ss;
2237                                 dstr = val;
2238                                 hstr = strchr( dstr, ':' );
2239                                 if ( hstr == NULL ) {
2240                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2241                                                 "invalid interval \"%s\"\n", val );
2242                                         return -1;
2243                                 }
2244                                 *hstr++ = '\0';
2245                                 mstr = strchr( hstr, ':' );
2246                                 if ( mstr == NULL ) {
2247                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2248                                                 "invalid interval \"%s\"\n", val );
2249                                         return -1;
2250                                 }
2251                                 *mstr++ = '\0';
2252                                 sstr = strchr( mstr, ':' );
2253                                 if ( sstr == NULL ) {
2254                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2255                                                 "invalid interval \"%s\"\n", val );
2256                                         return -1;
2257                                 }
2258                                 *sstr++ = '\0';
2259
2260                                 dd = atoi( dstr );
2261                                 hh = atoi( hstr );
2262                                 mm = atoi( mstr );
2263                                 ss = atoi( sstr );
2264                                 if (( hh > 24 ) || ( hh < 0 ) ||
2265                                         ( mm > 60 ) || ( mm < 0 ) ||
2266                                         ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2267                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2268                                                 "invalid interval \"%s\"\n", val );
2269                                         return -1;
2270                                 }
2271                                 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2272                         }
2273                         if ( si->si_interval < 0 ) {
2274                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2275                                         "invalid interval \"%ld\"\n",
2276                                         (long) si->si_interval);
2277                                 return -1;
2278                         }
2279                 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2280                                         STRLENOF( RETRYSTR "=" ) ) )
2281                 {
2282                         char **retry_list;
2283                         int j, k, n;
2284
2285                         val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2286                         retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2287                         retry_list[0] = NULL;
2288
2289                         slap_str2clist( &retry_list, val, " ,\t" );
2290
2291                         for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2292                         n = k / 2;
2293                         if ( k % 2 ) {
2294                                 fprintf( stderr,
2295                                                 "Error: incomplete syncrepl retry list\n" );
2296                                 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2297                                         ch_free( retry_list[k] );
2298                                 }
2299                                 ch_free( retry_list );
2300                                 exit( EXIT_FAILURE );
2301                         }
2302                         si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2303                         si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2304                         si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2305                         for ( j = 0; j < n; j++ ) {
2306                                 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2307                                 if ( *retry_list[j*2+1] == '+' ) {
2308                                         si->si_retrynum_init[j] = -1;
2309                                         si->si_retrynum[j] = -1;
2310                                         j++;
2311                                         break;
2312                                 } else {
2313                                         si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2314                                         si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2315                                 }
2316                         }
2317                         si->si_retrynum_init[j] = -2;
2318                         si->si_retrynum[j] = -2;
2319                         si->si_retryinterval[j] = 0;
2320                         
2321                         for ( k = 0; retry_list && retry_list[k]; k++ ) {
2322                                 ch_free( retry_list[k] );
2323                         }
2324                         ch_free( retry_list );
2325                 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
2326                                         STRLENOF( MANAGEDSAITSTR "=" ) ) )
2327                 {
2328                         val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
2329                         si->si_manageDSAit = atoi( val );
2330                 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
2331                                         STRLENOF( SLIMITSTR "=") ) )
2332                 {
2333                         val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
2334                         si->si_slimit = atoi( val );
2335                 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
2336                                         STRLENOF( TLIMITSTR "=" ) ) )
2337                 {
2338                         val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
2339                         si->si_tlimit = atoi( val );
2340                 } else if ( bindconf_parse( cargv[i], &si->si_bindconf )) {
2341                         fprintf( stderr, "Error: parse_syncrepl_line: "
2342                                 "unknown keyword \"%s\"\n", cargv[ i ] );
2343                         return -1;
2344                 }
2345         }
2346
2347         if ( gots != GOT_ALL ) {
2348                 fprintf( stderr,
2349                         "Error: Malformed \"syncrepl\" line in slapd config file" );
2350                 return -1;
2351         }
2352
2353         return 0;
2354 }
2355
2356 static void
2357 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
2358 {
2359         struct berval bc;
2360         char buf[BUFSIZ*2], *ptr;
2361         int i, len;
2362
2363         bindconf_unparse( &si->si_bindconf, &bc );
2364         ptr = buf;
2365         ptr += sprintf( ptr, IDSTR "=%03d " PROVIDERSTR "=%s",
2366                 si->si_rid, si->si_provideruri.bv_val );
2367         if ( !BER_BVISNULL( &bc )) {
2368                 ptr = lutil_strcopy( ptr, bc.bv_val );
2369                 free( bc.bv_val );
2370         }
2371         if ( !BER_BVISEMPTY( &si->si_filterstr )) {
2372                 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
2373                 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
2374                 *ptr++ = '"';
2375         }
2376         if ( !BER_BVISNULL( &si->si_base )) {
2377                 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
2378                 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
2379                 *ptr++ = '"';
2380         }
2381         for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
2382                 if ( si->si_scope == scopes[i].val ) {
2383                         ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
2384                         ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
2385                         break;
2386                 }
2387         }
2388         if ( si->si_attrsonly ) {
2389                 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR "=yes" );
2390         }
2391         if ( si->si_anfile ) {
2392                 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
2393                 ptr = lutil_strcopy( ptr, si->si_anfile );
2394         } else if ( si->si_allattrs || si->si_allopattrs ||
2395                 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
2396                 char *old;
2397                 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
2398                 old = ptr;
2399                 ptr = anlist_unparse( si->si_anlist, ptr );
2400                 if ( si->si_allattrs ) {
2401                         if ( old != ptr ) *ptr++ = ',';
2402                         *ptr++ = '*';
2403                 }
2404                 if ( si->si_allopattrs ) {
2405                         if ( old != ptr ) *ptr++ = ',';
2406                         *ptr++ = '+';
2407                 }
2408                 *ptr++ = '"';
2409         }
2410         if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
2411                 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
2412                 ptr = anlist_unparse( si->si_exanlist, ptr );
2413         }
2414         ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
2415         ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
2416         
2417         ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
2418         ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
2419                 "refreshAndPersist" : "refreshOnly" );
2420
2421         if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
2422                 int dd, hh, mm, ss;
2423
2424                 dd = si->si_interval;
2425                 ss = dd % 60;
2426                 dd /= 60;
2427                 mm = dd % 60;
2428                 dd /= 60;
2429                 hh = dd % 24;
2430                 dd /= 24;
2431                 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
2432                 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
2433         } else if ( si->si_retryinterval ) {
2434                 int space=0;
2435                 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
2436                 for (i=0; si->si_retryinterval[i]; i++) {
2437                         if ( space ) *ptr++ = ' ';
2438                         space = 1;
2439                         ptr += sprintf( ptr, "%d", si->si_retryinterval[i] );
2440                         if ( si->si_retrynum_init[i] == -1 )
2441                                 *ptr++ = '+';
2442                         else
2443                                 ptr += sprintf( ptr, "%d", si->si_retrynum_init );
2444                 }
2445                 *ptr++ = '"';
2446         }
2447
2448 #if 0 /* FIXME: unused in syncrepl.c, should remove it */
2449         ptr = lutil_strcopy( ptr, " " MANAGEDSAITSTR "=" );
2450         ptr += sprintf( ptr, "%d", si->si_manageDSAit );
2451 #endif
2452
2453         if ( si->si_slimit ) {
2454                 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
2455                 ptr += sprintf( ptr, "%d", si->si_slimit );
2456         }
2457
2458         if ( si->si_tlimit ) {
2459                 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
2460                 ptr += sprintf( ptr, "%d", si->si_tlimit );
2461         }
2462         bc.bv_len = ptr - buf;
2463         bc.bv_val = buf;
2464         ber_dupbv( bv, &bc );
2465 }
2466
2467
2468 int
2469 read_config(const char *fname, int depth) {
2470
2471         if ( !backend_db_init( "config" ))
2472                 return 1;
2473
2474         ber_str2bv( fname, 0, 1, &cf_prv.c_file );
2475         return read_config_file(fname, depth, NULL);
2476 }
2477
2478 static int
2479 config_back_bind( Operation *op, SlapReply *rs )
2480 {
2481         if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op )) {
2482                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ));
2483                 /* frontend sends result */
2484                 return LDAP_SUCCESS;
2485         }
2486
2487         rs->sr_err = LDAP_INVALID_CREDENTIALS;
2488         send_ldap_result( op, rs );
2489
2490         return rs->sr_err;
2491 }
2492
2493 static CfEntryInfo *
2494 config_find_base( CfEntryInfo *root, struct berval *dn, CfEntryInfo **last )
2495 {
2496         struct berval cdn;
2497         char *c;
2498
2499         if ( dn_match( &root->ce_entry->e_nname, dn ))
2500                 return root;
2501
2502         c = dn->bv_val+dn->bv_len;
2503         for (;*c != ',';c--);
2504
2505         while(root) {
2506                 *last = root;
2507                 for (--c;c>dn->bv_val && *c != ',';c--);
2508                 if ( *c == ',' )
2509                         c++;
2510                 cdn.bv_val = c;
2511                 cdn.bv_len = dn->bv_len - (c-dn->bv_val);
2512
2513                 root = root->ce_kids;
2514
2515                 for (;root;root=root->ce_sibs) {
2516                         if ( dn_match( &root->ce_entry->e_nname, &cdn )) {
2517                                 if ( cdn.bv_val == dn->bv_val ) {
2518                                         return root;
2519                                 }
2520                                 break;
2521                         }
2522                 }
2523         }
2524         return root;
2525 }
2526
2527 static int
2528 config_send( Operation *op, SlapReply *rs, CfEntryInfo *ce, int depth )
2529 {
2530         int rc = 0;
2531
2532         if ( test_filter( op, ce->ce_entry, op->ors_filter ) == LDAP_COMPARE_TRUE )
2533         {
2534                 rs->sr_attrs = op->ors_attrs;
2535                 rs->sr_entry = ce->ce_entry;
2536                 rc = send_search_entry( op, rs );
2537         }
2538         if ( op->ors_scope == LDAP_SCOPE_SUBTREE ) {
2539                 if ( ce->ce_kids ) {
2540                         rc = config_send( op, rs, ce->ce_kids, 1 );
2541                         if ( rc ) return rc;
2542                 }
2543                 if ( depth ) {
2544                         for (ce=ce->ce_sibs; ce; ce=ce->ce_sibs) {
2545                                 rc = config_send( op, rs, ce, 0 );
2546                                 if ( rc ) break;
2547                         }
2548                 }
2549         }
2550         return rc;
2551 }
2552
2553 static int
2554 config_back_search( Operation *op, SlapReply *rs )
2555 {
2556         CfBackInfo *cfb;
2557         CfEntryInfo *ce, *last;
2558         int rc;
2559
2560         if ( !be_isroot( op ) ) {
2561                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
2562                 send_ldap_result( op, rs );
2563         }
2564
2565         cfb = (CfBackInfo *)op->o_bd->be_private;
2566
2567         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
2568         if ( !ce ) {
2569                 if ( last )
2570                         rs->sr_matched = last->ce_entry->e_name.bv_val;
2571                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
2572                 goto out;
2573         }
2574         switch ( op->ors_scope ) {
2575         case LDAP_SCOPE_BASE:
2576         case LDAP_SCOPE_SUBTREE:
2577                 config_send( op, rs, ce, 0 );
2578                 break;
2579                 
2580         case LDAP_SCOPE_ONELEVEL:
2581                 for (ce = ce->ce_kids; ce; ce=ce->ce_sibs) {
2582                         config_send( op, rs, ce, 1 );
2583                 }
2584                 break;
2585         }
2586                 
2587         rs->sr_err = LDAP_SUCCESS;
2588 out:
2589         send_ldap_result( op, rs );
2590         return 0;
2591 }
2592
2593 static Entry *
2594 config_alloc_entry( struct berval *pdn, struct berval *rdn )
2595 {
2596         Entry *e = ch_calloc( 1, sizeof(Entry) );
2597         CfEntryInfo *ce = ch_calloc( 1, sizeof(CfEntryInfo) );
2598         e->e_private = ce;
2599         ce->ce_entry = e;
2600         build_new_dn( &e->e_name, pdn, rdn, NULL );
2601         ber_dupbv( &e->e_nname, &e->e_name );
2602         return e;
2603 }
2604
2605 #define NO_TABLE        0
2606 #define BI_TABLE        1
2607 #define BE_TABLE        2
2608
2609 static int
2610 config_build_entry( ConfigArgs *c, Entry *e, ObjectClass *oc,
2611          struct berval *rdn, ConfigTable *ct, int table )
2612 {
2613         struct berval vals[2];
2614         struct berval ad_name;
2615         AttributeDescription *ad = NULL;
2616         int rc, i;
2617         char *ptr;
2618         const char *text;
2619         AttributeType **at;
2620
2621         BER_BVZERO( &vals[1] );
2622
2623         vals[0] = oc->soc_cname;
2624         attr_merge(e, slap_schema.si_ad_objectClass, vals, NULL );
2625         ptr = strchr(rdn->bv_val, '=');
2626         ad_name.bv_val = rdn->bv_val;
2627         ad_name.bv_len = ptr - rdn->bv_val;
2628         rc = slap_bv2ad( &ad_name, &ad, &text );
2629         if ( rc ) {
2630                 return rc;
2631         }
2632         vals[0].bv_val = ptr+1;
2633         vals[0].bv_len = rdn->bv_len - (vals[0].bv_val - rdn->bv_val);
2634         attr_merge(e, ad, vals, NULL );
2635
2636         for (at=oc->soc_allowed;*at;at++) {
2637                 /* Skip the naming attr */
2638                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
2639                         continue;
2640                 for (i=0;ct[i].name;i++) {
2641                         if (ct[i].ad == (*at)->sat_ad)
2642                                 break;
2643                 }
2644                 rc = config_get_vals(&ct[i], c);
2645                 if (rc == LDAP_SUCCESS) {
2646                         attr_merge(e, ct[i].ad, c->rvalue_vals, c->rvalue_nvals);
2647                         ber_bvarray_free( c->rvalue_nvals );
2648                         ber_bvarray_free( c->rvalue_vals );
2649                 }
2650         }
2651
2652         if ( table ) {
2653                 if ( table == BI_TABLE )
2654                         ct = c->bi->bi_cf_table;
2655                 else
2656                         ct = c->be->be_cf_table;
2657                 for (;ct && ct->name;ct++) {
2658                         if (!ct->ad) continue;
2659                         rc = config_get_vals(ct, c);
2660                         if (rc == LDAP_SUCCESS) {
2661                                 attr_merge(e, ct->ad, c->rvalue_vals, c->rvalue_nvals);
2662                         }
2663                 }
2664         }
2665
2666         return 0;
2667 }
2668
2669 static CfEntryInfo *
2670 config_build_includes( ConfigArgs *c, Entry *parent )
2671 {
2672         Entry *e;
2673         int i;
2674         ConfigFile *cf = (ConfigFile *)c->line;
2675         CfEntryInfo *ce, *ceparent, *ceprev;
2676
2677         ceparent = parent->e_private;
2678
2679         for (i=0; cf; cf=cf->c_sibs, i++) {
2680                 c->value_dn.bv_val = c->log;
2681                 c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn=include{%d}", i);
2682                 e = config_alloc_entry( &parent->e_nname, &c->value_dn );
2683                 c->line = (char *)cf;
2684                 config_build_entry( c, e, cfOc_include, &c->value_dn,
2685                         c->bi->bi_cf_table, NO_TABLE );
2686                 ce = e->e_private;
2687                 if ( !ceparent->ce_kids ) {
2688                         ceparent->ce_kids = ce;
2689                 } else {
2690                         ceprev->ce_sibs = ce;
2691                 }
2692                 ceprev = ce;
2693                 if ( cf->c_kids ) {
2694                         c->line = (char *)cf->c_kids;
2695                         config_build_includes( c, e );
2696                 }
2697         }
2698         return ce;
2699 }
2700
2701 static int
2702 config_back_db_open( BackendDB *be )
2703 {
2704         CfBackInfo *cfb = be->be_private;
2705         struct berval rdn;
2706         Entry *e, *parent;
2707         CfEntryInfo *ce, *ceparent, *ceprev;
2708         int i, rc;
2709         BackendInfo *bi;
2710         BackendDB *bptr;
2711         ConfigArgs c;
2712         ConfigTable *ct;
2713
2714         /* create root of tree */
2715         ber_str2bv( CONFIG_DN, STRLENOF( CONFIG_DN ), 0, &rdn );
2716         e = config_alloc_entry( NULL, &rdn );
2717         ce = e->e_private;
2718         cfb->cb_root = ce;
2719         c.be = be;
2720         c.bi = be->bd_info;
2721         c.line = (char *)cfb->cb_config;
2722         ct = c.bi->bi_cf_table;
2723         config_build_entry( &c, e, cfOc_global, &rdn, ct, NO_TABLE );
2724
2725         parent = e;
2726         ceparent = ce;
2727
2728         /* Create includeFile nodes... */
2729         if ( cfb->cb_config->c_kids ) {
2730                 c.line = (char *)cfb->cb_config->c_kids;
2731                 ceprev = config_build_includes( &c, parent );
2732         }
2733
2734         /* Create backend nodes. Skip if they don't provide a cf_table.
2735          * There usually aren't any of these.
2736          */
2737         
2738         c.line = 0;
2739         bi = backendInfo;
2740         for (i=0; i<nBackendInfo; i++, bi++) {
2741                 if (!bi->bi_cf_table) continue;
2742                 if (!bi->bi_private) continue;
2743
2744                 rdn.bv_val = c.log;
2745                 rdn.bv_len = sprintf(rdn.bv_val, "%s=%s", cfAd_backend->ad_cname.bv_val, bi->bi_type);
2746                 e = config_alloc_entry( &parent->e_nname, &rdn );
2747                 ce = e->e_private;
2748                 ce->ce_bi = bi;
2749                 c.bi = bi;
2750                 config_build_entry( &c, e, cfOc_backend, &rdn, ct, BI_TABLE );
2751                 if ( !ceparent->ce_kids ) {
2752                         ceparent->ce_kids = ce;
2753                 } else {
2754                         ceprev->ce_sibs = ce;
2755                 }
2756                 ceprev = ce;
2757         }
2758
2759         /* Create database nodes... */
2760         for (i=0; i<nBackendDB; i++) {
2761                 slap_overinfo *oi = NULL;
2762                 if ( i == 0 ) {
2763                         bptr = frontendDB;
2764                 } else {
2765                         bptr = &backendDB[i];
2766                 }
2767                 if ( overlay_is_over( bptr )) {
2768                         oi = bptr->bd_info->bi_private;
2769                         bi = oi->oi_orig;
2770                 } else {
2771                         bi = bptr->bd_info;
2772                 }
2773                 rdn.bv_val = c.log;
2774                 rdn.bv_len = sprintf(rdn.bv_val, "%s={%0x}%s", cfAd_database->ad_cname.bv_val,
2775                         i, bi->bi_type);
2776                 e = config_alloc_entry( &parent->e_nname, &rdn );
2777                 ce = e->e_private;
2778                 c.be = bptr;
2779                 c.bi = bi;
2780                 ce->ce_be = c.be;
2781                 ce->ce_bi = c.bi;
2782                 config_build_entry( &c, e, cfOc_database, &rdn, ct, BE_TABLE );
2783                 if ( !ceparent->ce_kids ) {
2784                         ceparent->ce_kids = ce;
2785                 } else {
2786                         ceprev->ce_sibs = ce;
2787                 }
2788                 ceprev = ce;
2789                 /* Iterate through overlays */
2790                 if ( oi ) {
2791                         slap_overinst *on;
2792                         Entry *oe;
2793                         CfEntryInfo *opar = ce, *oprev = NULL;
2794                         int j;
2795
2796                         for (j=0,on=oi->oi_list; on; j++,on=on->on_next) {
2797                                 rdn.bv_val = c.log;
2798                                 rdn.bv_len = sprintf(rdn.bv_val, "%s={%0x}%s",
2799                                         cfAd_overlay->ad_cname.bv_val, j, on->on_bi.bi_type );
2800                                 oe = config_alloc_entry( &e->e_nname, &rdn );
2801                                 ce = oe->e_private;
2802                                 c.be = bptr;
2803                                 c.bi = &on->on_bi;
2804                                 ce->ce_be = c.be;
2805                                 ce->ce_bi = c.bi;
2806                                 config_build_entry( &c, oe, cfOc_overlay, &rdn, ct, BI_TABLE );
2807                                 if ( !opar->ce_kids ) {
2808                                         opar->ce_kids = ce;
2809                                 } else {
2810                                         oprev->ce_sibs = ce;
2811                                 }
2812                                 oprev = ce;
2813                         }
2814                 }
2815         }
2816
2817         return 0;
2818 }
2819
2820 static int
2821 config_back_db_destroy( Backend *be )
2822 {
2823         free( be->be_private );
2824         return 0;
2825 }
2826
2827 static int
2828 config_back_db_init( Backend *be )
2829 {
2830         struct berval dn;
2831         CfBackInfo *cfb;
2832
2833         cfb = ch_calloc( 1, sizeof(CfBackInfo));
2834         cfb->cb_config = &cf_prv;
2835         be->be_private = cfb;
2836
2837         ber_str2bv( CONFIG_DN, 0, 1, &be->be_rootdn );
2838         ber_dupbv( &be->be_rootndn, &be->be_rootdn );
2839         ber_dupbv( &dn, &be->be_rootdn );
2840         ber_bvarray_add( &be->be_suffix, &dn );
2841         ber_dupbv( &dn, &be->be_rootdn );
2842         ber_bvarray_add( &be->be_nsuffix, &dn );
2843
2844         return 0;
2845 }
2846
2847 static struct {
2848         char *name;
2849         AttributeDescription **desc;
2850         AttributeDescription *sub;
2851 } ads[] = {
2852         { "attribute", NULL, NULL },
2853         { "backend", &cfAd_backend, NULL },
2854         { "database", &cfAd_database, NULL },
2855         { "ditcontentrule", NULL, NULL },
2856         { "include", &cfAd_include, NULL },
2857         { "objectclass", NULL, NULL },
2858         { "overlay", &cfAd_overlay, NULL },
2859         { NULL, NULL, NULL }
2860 };
2861
2862 int
2863 config_back_initialize( BackendInfo *bi )
2864 {
2865         ConfigTable *ct = config_back_cf_table;
2866         char *argv[4];
2867         int i;
2868
2869         bi->bi_open = 0;
2870         bi->bi_close = 0;
2871         bi->bi_config = 0;
2872         bi->bi_destroy = 0;
2873
2874         bi->bi_db_init = config_back_db_init;
2875         bi->bi_db_config = 0;
2876         bi->bi_db_open = config_back_db_open;
2877         bi->bi_db_close = 0;
2878         bi->bi_db_destroy = config_back_db_destroy;
2879
2880         bi->bi_op_bind = config_back_bind;
2881         bi->bi_op_unbind = 0;
2882         bi->bi_op_search = config_back_search;
2883         bi->bi_op_compare = 0;
2884         bi->bi_op_modify = 0;
2885         bi->bi_op_modrdn = 0;
2886         bi->bi_op_add = 0;
2887         bi->bi_op_delete = 0;
2888         bi->bi_op_abandon = 0;
2889
2890         bi->bi_extended = 0;
2891
2892         bi->bi_chk_referrals = 0;
2893
2894         bi->bi_connection_init = 0;
2895         bi->bi_connection_destroy = 0;
2896
2897         argv[3] = NULL;
2898         for (i=0; OidMacros[i].name; i++ ) {
2899                 argv[1] = OidMacros[i].name;
2900                 argv[2] = OidMacros[i].oid;
2901                 parse_oidm( "slapd", i, 3, argv );
2902         }
2903
2904         i = init_config_attrs( ct );
2905         if ( i ) return i;
2906
2907         /* set up the notable AttributeDescriptions */
2908         ads[0].sub = slap_schema.si_ad_attributeTypes;
2909         ads[3].sub = slap_schema.si_ad_ditContentRules;
2910         ads[5].sub = slap_schema.si_ad_objectClasses;
2911
2912         bi->bi_cf_table = ct;
2913
2914         i = 0;
2915         for (;ct->name;ct++) {
2916                 if (strcmp(ct->name, ads[i].name)) continue;
2917                 if (ads[i].sub) {
2918                         ct->ad = ads[i].sub;
2919                 } else {
2920                         *ads[i].desc = ct->ad;
2921                 }
2922                 i++;
2923                 if (!ads[i].name) break;
2924         }
2925
2926         /* set up the objectclasses */
2927         i = init_config_ocs( cf_ocs );
2928
2929         return i;
2930 }
2931