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