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