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