]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
e86a23328bcf32d1c5d45b1bfd667644b20a5b34
[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_verify(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|ARG_STRING,
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_option,
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_verify,
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:        c->value_string = *(char **)cf->arg_item; break;
731                 }
732         }
733         if ( cf->arg_type & ARGS_POINTER) {
734                 bv.bv_val = c->log;
735                 switch(cf->arg_type & ARGS_POINTER) {
736                 case ARG_INT: bv.bv_len = sprintf(bv.bv_val, "%d", c->value_int); break;
737                 case ARG_LONG: bv.bv_len = sprintf(bv.bv_val, "%l", c->value_long); break;
738                 case ARG_BER_LEN_T: bv.bv_len =sprintf(bv.bv_val, "%l",c->value_ber_t); break;
739                 case ARG_ON_OFF: bv.bv_len = sprintf(bv.bv_val, "%s",
740                         c->value_int ? "TRUE" : "FALSE"); break;
741                 case ARG_STRING:
742                         if ( c->value_string && c->value_string[0]) {
743                                 ber_str2bv( c->value_string, 0, 0, &bv);
744                         } else {
745                                 rc = 1;
746                         }
747                         break;
748                 }
749                 ber_bvarray_add(&c->rvalue_vals, &bv);
750         }
751         return rc;
752 }
753
754 int
755 init_config_attrs(ConfigTable *ct) {
756         LDAPAttributeType *at;
757         int i, code;
758         const char *err;
759
760         for (i=0; ct[i].name; i++ ) {
761                 if ( !ct[i].attribute ) continue;
762                 at = ldap_str2attributetype( ct[i].attribute,
763                         &code, &err, LDAP_SCHEMA_ALLOW_ALL );
764                 if ( !at ) {
765                         fprintf( stderr, "init_config_schema: AttributeType \"%s\": %s, %s\n",
766                                 ct[i].attribute, ldap_scherr2str(code), err );
767                         return code;
768                 }
769                 code = at_add( at, &err );
770                 if ( code ) {
771                         fprintf( stderr, "init_config_schema: AttributeType \"%s\": %s, %s\n",
772                                 ct[i].attribute, scherr2str(code), err );
773                         return code;
774                 }
775                 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
776                 if ( code ) {
777                         fprintf( stderr, "init_config_schema: AttributeType \"%s\": %s\n",
778                                 ct[i].attribute, err );
779                         return code;
780                 }
781                 ldap_memfree( at );
782         }
783
784         return 0;
785 }
786
787 int
788 read_config(const char *fname, int depth) {
789         int i;
790         char *argv[3];
791
792         /* Schema initialization should normally be part of bi_open */
793         for (i=0; OidMacros[i].name; i++ ) {
794                 argv[1] = OidMacros[i].name;
795                 argv[2] = OidMacros[i].oid;
796                 parse_oidm( "slapd", i, 3, argv );
797         }
798         i = init_config_attrs(SystemConfiguration);
799         if ( i ) return i;
800         config_back_init( &cf_prv, SystemConfiguration );
801         return read_config_file(fname, depth, NULL);
802 }
803
804 int
805 read_config_file(const char *fname, int depth, ConfigArgs *cf)
806 {
807         FILE *fp;
808         ConfigArgs *c;
809         int rc;
810
811         c = ch_calloc( 1, sizeof( ConfigArgs ) );
812         if ( c == NULL ) {
813                 return 1;
814         }
815
816         if ( depth ) {
817                 memcpy( c, cf, sizeof( ConfigArgs ) );
818         } else {
819                 c->depth = depth; /* XXX */
820                 c->bi = NULL;
821                 c->be = NULL;
822         }
823
824         c->fname = fname;
825         c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
826         c->argv_size = ARGS_STEP + 1;
827
828         fp = fopen( fname, "r" );
829         if ( fp == NULL ) {
830                 ldap_syslog = 1;
831                 Debug(LDAP_DEBUG_ANY,
832                     "could not open config file \"%s\": %s (%d)\n",
833                     fname, strerror(errno), errno);
834                 return(1);
835         }
836 #ifdef SLAPD_MODULES
837         cfn->c_modlast = &cfn->c_modpaths;
838 #endif
839         ber_str2bv( fname, 0, 1, &cfn->c_file );
840         fname = cfn->c_file.bv_val;
841
842         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
843
844         fp_getline_init(c);
845
846         while ( fp_getline( fp, c ) ) {
847                 /* skip comments and blank lines */
848                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
849                         continue;
850                 }
851
852                 snprintf( c->log, sizeof( c->log ), "%s: line %lu",
853                                 c->fname, c->lineno );
854
855                 if ( fp_parse_line( c ) ) {
856                         goto badline;
857                 }
858
859                 if ( c->argc < 1 ) {
860                         Debug(LDAP_DEBUG_CONFIG, "%s: bad config line (ignored)\n", c->log, 0, 0);
861                         continue;
862                 }
863
864                 rc = parse_config_table( SystemConfiguration, c );
865                 if ( !rc ) {
866                         continue;
867                 }
868                 if ( rc & ARGS_USERLAND ) {
869                         switch(rc) {    /* XXX a usertype would be opaque here */
870                         default:
871                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%d>\n",
872                                         c->log, *c->argv, 0);
873                                 goto badline;
874                         }
875
876                 } else if ( rc == ARG_BAD_CONF || rc != ARG_UNKNOWN ) {
877                         goto badline;
878                         
879                 } else if ( c->bi && c->bi->bi_config ) {               /* XXX to check: could both be/bi_config? oops */
880                         rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno, c->argc, c->argv);
881                         if ( rc ) {
882                                 switch(rc) {
883                                 case SLAP_CONF_UNKNOWN:
884                                         Debug(LDAP_DEBUG_CONFIG, "%s: "
885                                                 "unknown directive <%s> inside backend info definition (ignored)\n",
886                                                 c->log, *c->argv, 0);
887                                         continue;
888                                 default:
889                                         goto badline;
890                                 }
891                         }
892                         
893                 } else if ( c->be && c->be->be_config ) {
894                         rc = (*c->be->be_config)(c->be, c->fname, c->lineno, c->argc, c->argv);
895                         if ( rc ) {
896                                 switch(rc) {
897                                 case SLAP_CONF_UNKNOWN:
898                                         Debug( LDAP_DEBUG_CONFIG, "%s: "
899                                                 "unknown directive <%s> inside backend database definition (ignored)\n",
900                                                 c->log, *c->argv, 0);
901                                         continue;
902                                 default:
903                                         goto badline;
904                                 }
905                         }
906
907                 } else if ( frontendDB->be_config ) {
908                         rc = (*frontendDB->be_config)(frontendDB, c->fname, (int)c->lineno, c->argc, c->argv);
909                         if ( rc ) {
910                                 switch(rc) {
911                                 case SLAP_CONF_UNKNOWN:
912                                         Debug( LDAP_DEBUG_CONFIG, "%s: "
913                                                 "unknown directive <%s> inside global database definition (ignored)\n",
914                                                 c->log, *c->argv, 0);
915                                         continue;
916                                 default:
917                                         goto badline;
918                                 }
919                         }
920                         
921                 } else {
922                         Debug(LDAP_DEBUG_CONFIG, "%s: "
923                                 "unknown directive <%s> outside backend info and database definitions (ignored)\n",
924                                 c->log, *c->argv, 0);
925                         continue;
926
927                 }
928         }
929
930         fclose(fp);
931
932         if ( BER_BVISNULL( &frontendDB->be_schemadn ) ) {
933                 ber_str2bv( SLAPD_SCHEMA_DN, STRLENOF( SLAPD_SCHEMA_DN ), 1,
934                         &frontendDB->be_schemadn );
935                 rc = dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
936                 if ( rc != LDAP_SUCCESS ) {
937                         Debug(LDAP_DEBUG_ANY, "%s: "
938                                 "unable to normalize default schema DN \"%s\"\n",
939                                 c->log, frontendDB->be_schemadn.bv_val, 0 );
940                         /* must not happen */
941                         assert( 0 );
942                 }
943         }
944
945         ch_free(c->argv);
946         ch_free(c);
947         return(0);
948
949 badline:
950         fclose(fp);
951         ch_free(c->argv);
952         ch_free(c);
953         return(1);
954 }
955
956 static int
957 config_generic(ConfigArgs *c) {
958         char *p;
959         int i;
960
961         if ( c->emit ) {
962                 int rc = 0;
963                 switch(c->type) {
964                 case CFG_CONCUR:
965                         c->value_int = ldap_pvt_thread_get_concurrency();
966                         break;
967                 case CFG_THREADS:
968                         c->value_int = connection_pool_max;
969                         break;
970                 case CFG_SALT:
971                         if ( passwd_salt )
972                                 c->value_string = passwd_salt;
973                         else
974                                 rc = 1;
975                         break;
976                 case CFG_LIMITS:
977                         rc = 1;
978                         break;
979                 case CFG_RO:
980                         c->value_int = (c->be->be_restrictops & SLAP_RESTRICT_OP_WRITES) != 0;
981                         break;
982                 case CFG_AZPOLICY:
983                         c->value_string = (char *)slap_sasl_getpolicy();
984                         break;
985                 case CFG_AZREGEXP:
986                         rc = 1;
987                         break;
988 #ifdef HAVE_CYRUS_SASL
989                 case CFG_SASLSECP:
990                         rc = 1;
991                         break;
992 #endif
993                 case CFG_DEPTH:
994                         c->value_int = c->be->be_max_deref_depth;
995                         break;
996                 case CFG_OID:
997                         rc = 1;
998                         break;
999                 case CFG_CHECK:
1000                         c->value_int = global_schemacheck;
1001                         break;
1002                 case CFG_ACL: {
1003                         AccessControl *a;
1004                         char *src, *dst, ibuf[11];
1005                         struct berval bv, abv;
1006                         for (i=0, a=c->be->be_acl; a; i++,a=a->acl_next) {
1007                                 abv.bv_len = sprintf( ibuf, "{%x}", i );
1008                                 acl_unparse( a, &bv );
1009                                 abv.bv_val = ch_malloc( abv.bv_len + bv.bv_len + 1 );
1010                                 AC_MEMCPY( abv.bv_val, ibuf, abv.bv_len );
1011                                 /* Turn TAB / EOL into plain space */
1012                                 for (src=bv.bv_val,dst=abv.bv_val+abv.bv_len; *src; src++) {
1013                                         if (isspace(*src)) *dst++ = ' ';
1014                                         else *dst++ = *src;
1015                                 }
1016                                 *dst = '\0';
1017                                 if (dst[-1] == ' ') {
1018                                         dst--;
1019                                         *dst = '\0';
1020                                 }
1021                                 abv.bv_len = dst - abv.bv_val;
1022                                 ber_bvarray_add( &c->rvalue_vals, &abv );
1023                         }
1024                         rc = (!i);
1025                         break;
1026                 }
1027                 case CFG_REPLOG:
1028                         c->value_string = c->be->be_replogfile;
1029                         break;
1030                 case CFG_ROOTDSE: {
1031                         ConfigFile *cf = (ConfigFile *)c->line;
1032                         if ( cf->c_dseFiles ) {
1033                                 c->rvalue_vals = cf->c_dseFiles;
1034                         } else {
1035                                 rc = 1;
1036                         }
1037                         }
1038                         break;
1039                 case CFG_LOGFILE:
1040                         if ( logfileName )
1041                                 c->value_string = logfileName;
1042                         else
1043                                 rc = 1;
1044                         break;
1045                 case CFG_LASTMOD:
1046                         c->value_int = (SLAP_NOLASTMOD(c->be) == 0);
1047                         break;
1048                 case CFG_SSTR_IF_MAX:
1049                         c->value_int = index_substr_if_maxlen;
1050                         break;
1051                 case CFG_SSTR_IF_MIN:
1052                         c->value_int = index_substr_if_minlen;
1053                         break;
1054 #ifdef SLAPD_MODULES
1055                 case CFG_MODLOAD:
1056                 case CFG_MODPATH:
1057                         rc = 1;
1058                         break;
1059 #endif
1060 #ifdef LDAP_SLAPI
1061                 case CFG_PLUGIN:
1062                         rc = 1;
1063                         break;
1064 #endif
1065 #ifdef SLAP_AUTH_REWRITE
1066                 case CFG_REWRITE:
1067                         rc = 1;
1068                         break;
1069 #endif
1070                 default:
1071                         rc = 1;
1072                 }
1073                 return rc;
1074         }
1075
1076         p = strchr(c->line,'(' /*')'*/);
1077         switch(c->type) {
1078                 case CFG_BACKEND:
1079                         if(!(c->bi = backend_info(c->argv[1]))) {
1080                                 Debug(LDAP_DEBUG_ANY, "%s: "
1081                                         "backend %s failed init!\n", c->log, c->argv[1], 0);
1082                                 return(1);
1083                         }
1084                         break;
1085
1086                 case CFG_DATABASE:
1087                         c->bi = NULL;
1088                         /* FIXME - config should probably be the
1089                          * last backend, not the first.
1090                          */
1091                         if ( !strcasecmp( c->argv[1], "config" )) {
1092                                 c->be = backendDB;
1093                         } else if(!(c->be = backend_db_init(c->argv[1]))) {
1094                                 Debug(LDAP_DEBUG_ANY, "%s: "
1095                                         "database %s failed init!\n", c->log, c->argv[1], 0);
1096                                 return(1);
1097                         }
1098                         break;
1099
1100                 case CFG_CONCUR:
1101                         ldap_pvt_thread_set_concurrency(c->value_int);
1102                         break;
1103
1104                 case CFG_THREADS:
1105                         ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
1106                         connection_pool_max = c->value_int;     /* save for reference */
1107                         break;
1108
1109                 case CFG_SALT:
1110                         if ( passwd_salt ) ch_free( passwd_salt );
1111                         passwd_salt = c->value_string;
1112                         lutil_salt_format(passwd_salt);
1113                         break;
1114
1115                 case CFG_LIMITS:
1116                         if(limits_parse(c->be, c->fname, c->lineno, c->argc, c->argv))
1117                                 return(1);
1118                         break;
1119
1120                 case CFG_RO:
1121                         if(c->value_int)
1122                                 c->be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
1123                         else
1124                                 c->be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
1125                         break;
1126
1127                 case CFG_AZPOLICY:
1128                         ch_free(c->value_string);
1129                         if (slap_sasl_setpolicy( c->argv[1] )) {
1130                                 Debug(LDAP_DEBUG_ANY, "%s: unable to parse value \"%s\" in"
1131                                         " \"authz-policy <policy>\"\n",
1132                                         c->log, c->argv[1], 0 );
1133                                 return(1);
1134                         }
1135                         break;
1136                 
1137                 case CFG_AZREGEXP:
1138                         if (slap_sasl_regexp_config( c->argv[1], c->argv[2] ))
1139                                 return(1);
1140                         break;
1141                                 
1142 #ifdef HAVE_CYRUS_SASL
1143                 case CFG_SASLSECP:
1144                         {
1145                         char *txt = slap_sasl_secprops( c->argv[1] );
1146                         if ( txt ) {
1147                                 Debug(LDAP_DEBUG_ANY, "%s: sasl-secprops: %s\n",
1148                                         c->log, txt, 0 );
1149                                 return(1);
1150                         }
1151                         break;
1152                         }
1153 #endif
1154
1155                 case CFG_DEPTH:
1156                         c->be->be_max_deref_depth = c->value_int;
1157                         break;
1158
1159                 case CFG_OID:
1160                         if(parse_oidm(c->fname, c->lineno, c->argc, c->argv)) return(1);
1161                         break;
1162
1163                 case CFG_OC:
1164                         if(parse_oc(c->fname, c->lineno, p, c->argv)) return(1);
1165                         break;
1166
1167                 case CFG_DIT:
1168                         if(parse_cr(c->fname, c->lineno, p, c->argv)) return(1);
1169                         break;
1170
1171                 case CFG_ATTR:
1172                         if(parse_at(c->fname, c->lineno, p, c->argv)) return(1);
1173                         break;
1174
1175                 case CFG_ATOPT:
1176                         ad_define_option(NULL, NULL, 0);
1177                         for(i = 1; i < c->argc; i++)
1178                                 if(ad_define_option(c->argv[i], c->fname, c->lineno))
1179                                         return(1);
1180                         break;
1181
1182                 case CFG_CHECK:
1183                         global_schemacheck = c->value_int;
1184                         if(!global_schemacheck) Debug(LDAP_DEBUG_ANY, "%s: "
1185                                 "schema checking disabled! your mileage may vary!\n",
1186                                 c->log, 0, 0);
1187                         break;
1188
1189                 case CFG_ACL:
1190                         parse_acl(c->be, c->fname, c->lineno, c->argc, c->argv);
1191                         break;
1192
1193                 case CFG_REPLOG:
1194                         if(SLAP_MONITOR(c->be)) {
1195                                 Debug(LDAP_DEBUG_ANY, "%s: "
1196                                         "\"replogfile\" should not be used "
1197                                         "inside monitor database\n",
1198                                         c->log, 0, 0);
1199                                 return(0);      /* FIXME: should this be an error? */
1200                         }
1201
1202                         c->be->be_replogfile = c->value_string;
1203                         break;
1204
1205                 case CFG_ROOTDSE:
1206                         if(read_root_dse_file(c->argv[1])) {
1207                                 Debug(LDAP_DEBUG_ANY, "%s: "
1208                                         "could not read \"rootDSE <filename>\" line\n",
1209                                         c->log, 0, 0);
1210                                 return(1);
1211                         }
1212                         {
1213                                 struct berval bv;
1214                                 ber_str2bv( c->argv[1], 0, 1, &bv );
1215                                 ber_bvarray_add( &cfn->c_dseFiles, &bv );
1216                         }
1217                         break;
1218
1219                 case CFG_LOGFILE: {
1220                         if ( logfileName ) ch_free( logfileName );
1221                         logfileName = c->value_string;
1222                         FILE *logfile = fopen(logfileName, "w");
1223                         if(logfile) lutil_debug_file(logfile);
1224                         break;
1225                         }
1226
1227                 case CFG_LASTMOD:
1228                         if(SLAP_NOLASTMODCMD(c->be)) {
1229                                 Debug(LDAP_DEBUG_ANY, "%s: "
1230                                         "lastmod not available for %s databases\n",
1231                                         c->log, c->be->bd_info->bi_type, 0);
1232                                 return(1);
1233                         }
1234                         if(c->value_int)
1235                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_NOLASTMOD;
1236                         else
1237                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NOLASTMOD;
1238                         break;
1239
1240                 case CFG_SSTR_IF_MAX:
1241                         if (c->value_int < index_substr_if_minlen) {
1242                                 Debug(LDAP_DEBUG_ANY, "%s: "
1243                                         "invalid max value (%d)\n",
1244                                         c->log, c->value_int, 0 );
1245                                 return(1);
1246                         }
1247                         index_substr_if_maxlen = c->value_int;
1248                         break;
1249
1250                 case CFG_SSTR_IF_MIN:
1251                         if (c->value_int > index_substr_if_maxlen) {
1252                                 Debug(LDAP_DEBUG_ANY, "%s: "
1253                                         "invalid min value (%d)\n",
1254                                         c->log, c->value_int, 0 );
1255                                 return(1);
1256                         }
1257                         index_substr_if_minlen = c->value_int;
1258                         break;
1259
1260 #ifdef SLAPD_MODULES
1261                 case CFG_MODLOAD:
1262                         if(module_load(c->argv[1], c->argc - 2, (c->argc > 2) ? c->argv + 2 : NULL))
1263                                 return(1);
1264                         /* Record this load on the current path */
1265                         {
1266                                 struct berval bv;
1267                                 ber_str2bv(c->line, 0, 1, &bv);
1268                                 ber_bvarray_add( &cfn->c_modlast->mp_loads, &bv );
1269                         }
1270                         break;
1271
1272                 case CFG_MODPATH:
1273                         if(module_path(c->argv[1])) return(1);
1274                         /* Record which path was used with each module */
1275                         {
1276                                 ModPaths *mp;
1277
1278                                 if (!cfn->c_modpaths.mp_loads)
1279                                         mp = &cfn->c_modpaths;
1280                                 else
1281                                         mp = ch_malloc( sizeof( ModPaths ));
1282                                 ber_str2bv(c->argv[1], 0, 1, &mp->mp_path);
1283                                 mp->mp_next = NULL;
1284                                 mp->mp_loads = NULL;
1285                                 cfn->c_modlast->mp_next = mp;
1286                                 cfn->c_modlast = mp;
1287                         }
1288                         
1289                         break;
1290 #endif
1291
1292 #ifdef LDAP_SLAPI
1293                 case CFG_PLUGIN:
1294                         if(slapi_int_read_config(c->be, c->fname, c->lineno, c->argc, c->argv) != LDAP_SUCCESS)
1295                                 return(1);
1296                         slapi_plugins_used++;
1297                         break;
1298 #endif
1299
1300 #ifdef SLAP_AUTH_REWRITE
1301                 case CFG_REWRITE:
1302                         if(slap_sasl_rewrite_config(c->fname, c->lineno, c->argc, c->argv))
1303                                 return(1);
1304                         break;
1305 #endif
1306
1307
1308                 default:
1309                         Debug(LDAP_DEBUG_ANY, "%s: unknown CFG_TYPE %d"
1310                                 "(ignored)\n", c->log, c->type, 0);
1311
1312         }
1313         return(0);
1314 }
1315
1316
1317 static int
1318 config_fname(ConfigArgs *c) {
1319         if(c->emit && c->line) {
1320                 ConfigFile *cf = (ConfigFile *)c->line;
1321                 c->value_string = cf->c_file.bv_val;
1322                 return 0;
1323         }
1324         return(1);
1325 }
1326
1327 static int
1328 config_search_base(ConfigArgs *c) {
1329         struct berval dn;
1330
1331         if(c->emit) {
1332                 int rc = 1;
1333                 if (!BER_BVISEMPTY(&default_search_base)) {
1334                         ber_bvarray_add(&c->rvalue_vals, &default_search_base);
1335                         ber_bvarray_add(&c->rvalue_nvals, &default_search_nbase);
1336                         rc = 0;
1337                 }
1338                 return rc;
1339         }
1340
1341         if(c->bi || c->be != frontendDB) {
1342                 Debug(LDAP_DEBUG_ANY, "%s: defaultSearchBase line must appear "
1343                         "prior to any backend or database definition\n",
1344                         c->log, 0, 0);
1345                 return(1);
1346         }
1347
1348         if(default_search_nbase.bv_len) {
1349                 Debug(LDAP_DEBUG_ANY, "%s: "
1350                         "default search base \"%s\" already defined "
1351                         "(discarding old)\n",
1352                         c->log, default_search_base.bv_val, 0);
1353                 free(default_search_base.bv_val);
1354                 free(default_search_nbase.bv_val);
1355         }
1356
1357         default_search_base = c->value_dn;
1358         default_search_nbase = c->value_ndn;
1359         return(0);
1360 }
1361
1362 static int
1363 config_passwd_hash(ConfigArgs *c) {
1364         int i;
1365         if (c->emit) {
1366                 struct berval bv;
1367                 for (i=0; default_passwd_hash && default_passwd_hash[i]; i++) {
1368                         ber_str2bv(default_passwd_hash[i], 0, 0, &bv);
1369                         ber_bvarray_add(&c->rvalue_vals, &bv);
1370                 }
1371                 return i ? 0 : 1;
1372         }
1373         if(default_passwd_hash) {
1374                 Debug(LDAP_DEBUG_ANY, "%s: "
1375                         "already set default password_hash\n",
1376                         c->log, 0, 0);
1377                 return(1);
1378         }
1379         for(i = 1; i < c->argc; i++) {
1380                 if(!lutil_passwd_scheme(c->argv[i])) {
1381                         Debug(LDAP_DEBUG_ANY, "%s: "
1382                                 "password scheme \"%s\" not available\n",
1383                                 c->log, c->argv[i], 0);
1384                 } else {
1385                         ldap_charray_add(&default_passwd_hash, c->argv[i]);
1386                 }
1387                 if(!default_passwd_hash) {
1388                         Debug(LDAP_DEBUG_ANY, "%s: no valid hashes found\n",
1389                                 c->log, 0, 0 );
1390                         return(1);
1391                 }
1392         }
1393         return(0);
1394 }
1395
1396 static int
1397 config_schema_dn(ConfigArgs *c) {
1398         struct berval dn;
1399         int rc;
1400         if ( c->emit ) {
1401                 ber_bvarray_add(&c->rvalue_vals, &c->be->be_schemadn);
1402                 ber_bvarray_add(&c->rvalue_nvals, &c->be->be_schemandn);
1403                 return 0;
1404         }
1405         c->be->be_schemadn = c->value_dn;
1406         c->be->be_schemandn = c->value_ndn;
1407         return(0);
1408 }
1409
1410 static int
1411 config_sizelimit(ConfigArgs *c) {
1412         int i, rc = 0;
1413         char *next;
1414         struct slap_limits_set *lim = &c->be->be_def_limit;
1415         if (c->emit) {
1416                 return 1;
1417         }
1418         for(i = 1; i < c->argc; i++) {
1419                 if(!strncasecmp(c->argv[i], "size", 4)) {
1420                         rc = limits_parse_one(c->argv[i], lim);
1421                         if ( rc ) {
1422                                 Debug(LDAP_DEBUG_ANY, "%s: "
1423                                         "unable to parse value \"%s\" in \"sizelimit <limit>\" line\n",
1424                                         c->log, c->argv[i], 0);
1425                                 return(1);
1426                         }
1427                 } else {
1428                         if(!strcasecmp(c->argv[i], "unlimited")) {
1429                                 lim->lms_s_soft = -1;
1430                         } else {
1431                                 lim->lms_s_soft = strtol(c->argv[i], &next, 0);
1432                                 if(next == c->argv[i]) {
1433                                         Debug(LDAP_DEBUG_ANY, "%s: "
1434                                                 "unable to parse limit \"%s\" in \"sizelimit <limit>\" line\n",
1435                                                 c->log, c->argv[i], 0);
1436                                         return(1);
1437                                 } else if(next[0] != '\0') {
1438                                         Debug(LDAP_DEBUG_ANY, "%s: "
1439                                                 "trailing chars \"%s\" in \"sizelimit <limit>\" line (ignored)\n",
1440                                                 c->log, next, 0);
1441                                 }
1442                         }
1443                         lim->lms_s_hard = 0;
1444                 }
1445         }
1446         return(0);
1447 }
1448
1449 static int
1450 config_timelimit(ConfigArgs *c) {
1451         int i, rc = 0;
1452         char *next;
1453         struct slap_limits_set *lim = &c->be->be_def_limit;
1454         if (c->emit) {
1455                 return 1;
1456         }
1457         for(i = 1; i < c->argc; i++) {
1458                 if(!strncasecmp(c->argv[i], "time", 4)) {
1459                         rc = limits_parse_one(c->argv[i], lim);
1460                         if ( rc ) {
1461                                 Debug(LDAP_DEBUG_ANY, "%s: "
1462                                         "unable to parse value \"%s\" in \"timelimit <limit>\" line\n",
1463                                         c->log, c->argv[i], 0);
1464                                 return(1);
1465                         }
1466                 } else {
1467                         if(!strcasecmp(c->argv[i], "unlimited")) {
1468                                 lim->lms_t_soft = -1;
1469                         } else {
1470                                 lim->lms_t_soft = strtol(c->argv[i], &next, 0);
1471                                 if(next == c->argv[i]) {
1472                                         Debug(LDAP_DEBUG_ANY, "%s: "
1473                                                 "unable to parse limit \"%s\" in \"timelimit <limit>\" line\n",
1474                                                 c->log, c->argv[i], 0);
1475                                         return(1);
1476                                 } else if(next[0] != '\0') {
1477                                         Debug(LDAP_DEBUG_ANY, "%s: "
1478                                                 "trailing chars \"%s\" in \"timelimit <limit>\" line (ignored)\n",
1479                                                 c->log, next, 0);
1480                                 }
1481                         }
1482                         lim->lms_t_hard = 0;
1483                 }
1484         }
1485         return(0);
1486 }
1487
1488 static int
1489 config_overlay(ConfigArgs *c) {
1490         if (c->emit) {
1491                 return 1;
1492         }
1493         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1])) {
1494                 /* log error */
1495                 Debug(LDAP_DEBUG_ANY, "%s: (optional) %s overlay \"%s\" configuration failed (ignored)\n",
1496                         c->log, c->be == frontendDB ? "global " : "", c->argv[1][1]);
1497         } else if(overlay_config(c->be, c->argv[1])) {
1498                 return(1);
1499         }
1500         return(0);
1501 }
1502
1503 static int
1504 config_suffix(ConfigArgs *c) {
1505         Backend *tbe;
1506         struct berval pdn, ndn;
1507         int rc;
1508         if (c->emit) {
1509                 int i;
1510                 for (i=0; !BER_BVISNULL(&c->be->be_suffix[i]); i++) {
1511                         ber_bvarray_add(&c->rvalue_vals, &c->be->be_suffix[i]);
1512                         ber_bvarray_add(&c->rvalue_nvals, &c->be->be_nsuffix[i]);
1513                 }
1514                 return i ? 0 : 1;
1515         }
1516 #ifdef SLAPD_MONITOR_DN
1517         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
1518                 Debug(LDAP_DEBUG_ANY, "%s: "
1519                         "\"%s\" is reserved for monitoring slapd\n",
1520                         c->log, SLAPD_MONITOR_DN, 0);
1521                 return(1);
1522         }
1523 #endif
1524
1525         pdn = c->value_dn;
1526         ndn = c->value_ndn;
1527         tbe = select_backend(&ndn, 0, 0);
1528         if(tbe == c->be) {
1529                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by this backend! (ignored)\n",
1530                         c->log, 0, 0);
1531                 free(pdn.bv_val);
1532                 free(ndn.bv_val);
1533         } else if(tbe) {
1534                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by a preceding backend \"%s\"\n",
1535                         c->log, tbe->be_suffix[0].bv_val, 0);
1536                 free(pdn.bv_val);
1537                 free(ndn.bv_val);
1538                 return(1);
1539         } else if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
1540                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
1541                         "base provided \"%s\" (assuming okay)\n",
1542                         c->log, default_search_base.bv_val, 0);
1543         }
1544         ber_bvarray_add(&c->be->be_suffix, &pdn);
1545         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
1546         return(0);
1547 }
1548
1549 static int
1550 config_rootdn(ConfigArgs *c) {
1551         if (c->emit) {
1552                 ber_bvarray_add(&c->rvalue_vals, &c->be->be_rootdn);
1553                 ber_bvarray_add(&c->rvalue_nvals, &c->be->be_rootndn);
1554                 return 0;
1555         }
1556         c->be->be_rootdn = c->value_dn;
1557         c->be->be_rootndn = c->value_ndn;
1558         return(0);
1559 }
1560
1561 static int
1562 config_rootpw(ConfigArgs *c) {
1563         Backend *tbe;
1564         if (c->emit) {
1565                 if (!BER_BVISEMPTY(&c->be->be_rootpw)) {
1566                         c->value_string="*";
1567                         return 0;
1568                 }
1569                 return 1;
1570         }
1571
1572         tbe = select_backend(&c->be->be_rootndn, 0, 0);
1573         if(tbe != c->be) {
1574                 Debug(LDAP_DEBUG_ANY, "%s: "
1575                         "rootpw can only be set when rootdn is under suffix\n",
1576                         c->log, 0, 0);
1577                 return(1);
1578         }
1579         ber_str2bv(c->value_string, 0, 0, &c->be->be_rootpw);
1580         return(0);
1581 }
1582
1583 /* restrictops, allows, disallows, requires, loglevel */
1584
1585 struct verb_mask_list { char *word; int mask; };
1586
1587 int
1588 verb_to_mask(ConfigArgs *c, struct verb_mask_list *v, int word) {
1589         int j;
1590         for(j = 0; v[j].word; j++)
1591                 if(!strcasecmp(c->argv[word], v[j].word))
1592                         break;
1593         return(j);
1594 }
1595
1596 int
1597 verbs_to_mask(ConfigArgs *c, struct verb_mask_list *v, slap_mask_t *m) {
1598         int i, j;
1599         for(i = 1; i < c->argc; i++) {
1600                 j = verb_to_mask(c, v, i);
1601                 if(!v[j].word) return(1);
1602                 *m |= v[j].mask;
1603         }
1604         return(0);
1605 }
1606
1607 static int
1608 config_restrict(ConfigArgs *c) {
1609         slap_mask_t restrictops = 0;
1610         int i, j;
1611         struct verb_mask_list restrictable_exops[] = {
1612                 { LDAP_EXOP_START_TLS,          SLAP_RESTRICT_EXOP_START_TLS },
1613                 { LDAP_EXOP_MODIFY_PASSWD,      SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
1614                 { LDAP_EXOP_X_WHO_AM_I,         SLAP_RESTRICT_EXOP_WHOAMI },
1615                 { LDAP_EXOP_X_CANCEL,           SLAP_RESTRICT_EXOP_CANCEL },
1616                 { NULL, 0 }
1617         };
1618         struct verb_mask_list restrictable_ops[] = {
1619                 { "bind",               SLAP_RESTRICT_OP_BIND },
1620                 { "add",                SLAP_RESTRICT_OP_ADD },
1621                 { "modify",             SLAP_RESTRICT_OP_MODIFY },
1622                 { "modrdn",             SLAP_RESTRICT_OP_RENAME },
1623                 { "rename",             SLAP_RESTRICT_OP_RENAME },
1624                 { "delete",             SLAP_RESTRICT_OP_DELETE },
1625                 { "search",             SLAP_RESTRICT_OP_SEARCH },
1626                 { "compare",            SLAP_RESTRICT_OP_COMPARE },
1627                 { "read",               SLAP_RESTRICT_OP_READS },
1628                 { "write",              SLAP_RESTRICT_OP_WRITES },
1629                 { NULL, 0 }
1630         };
1631
1632         if (c->emit) {
1633                 return 1;
1634         }
1635         for(i = 1; i < c->argc; i++) {
1636                 j = verb_to_mask(c, restrictable_ops, i);
1637                 if(restrictable_ops[j].word) {
1638                         restrictops |= restrictable_ops[j].mask;
1639                         continue;
1640                 } else if(!strncasecmp(c->argv[i], "extended", STRLENOF("extended"))) {
1641                         char *e = c->argv[i] + STRLENOF("extended");
1642                         if(e[0] == '=') {
1643                                 int k = verb_to_mask(c, restrictable_exops, e[1]);
1644                                 if(restrictable_exops[k].word) {
1645                                         restrictops |= restrictable_exops[k].mask;
1646                                         continue;
1647                                 } else break;
1648                         } else if(!e[0]) {
1649                                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
1650                                 restrictops |= SLAP_RESTRICT_OP_EXTENDED;
1651                         } else break;
1652                 }
1653         }
1654         if(i < c->argc) {
1655                 c->be->be_restrictops |= restrictops;
1656                 return(0);
1657         }
1658         Debug(LDAP_DEBUG_ANY, "%s: "
1659                 "unknown operation %s in \"restrict <features>\" line\n",
1660                 c->log, c->argv[i], 0);
1661         return(1);
1662 }
1663
1664 static int
1665 config_allows(ConfigArgs *c) {
1666         slap_mask_t allows = 0;
1667         int i;
1668         struct verb_mask_list allowable_ops[] = {
1669                 { "bind_v2",            SLAP_ALLOW_BIND_V2 },
1670                 { "bind_anon_cred",     SLAP_ALLOW_BIND_ANON_CRED },
1671                 { "bind_anon_dn",       SLAP_ALLOW_BIND_ANON_DN },
1672                 { "update_anon",        SLAP_ALLOW_UPDATE_ANON },
1673                 { NULL, 0 }
1674         };
1675         if (c->emit) {
1676                 return 1;
1677         }
1678         i = verbs_to_mask(c, allowable_ops, &allows);
1679         if ( i ) {
1680                 Debug(LDAP_DEBUG_ANY, "%s: "
1681                         "unknown feature %s in \"allow <features>\" line\n",
1682                         c->log, c->argv[i], 0);
1683                 return(1);
1684         }
1685         global_allows |= allows;
1686         return(0);
1687 }
1688
1689 static int
1690 config_disallows(ConfigArgs *c) {
1691         slap_mask_t disallows = 0;
1692         int i;
1693         struct verb_mask_list disallowable_ops[] = {
1694                 { "bind_anon",          SLAP_DISALLOW_BIND_ANON },
1695                 { "bind_simple",        SLAP_DISALLOW_BIND_SIMPLE },
1696                 { "bind_krb4",          SLAP_DISALLOW_BIND_KRBV4 },
1697                 { "tls_2_anon",         SLAP_DISALLOW_TLS_2_ANON },
1698                 { "tls_authc",          SLAP_DISALLOW_TLS_AUTHC },
1699                 { NULL, 0 }
1700         };
1701         if (c->emit) {
1702                 return 1;
1703         }
1704         i = verbs_to_mask(c, disallowable_ops, &disallows);
1705         if ( i ) {
1706                 Debug(LDAP_DEBUG_ANY, "%s: "
1707                         "unknown feature %s in \"disallow <features>\" line\n",
1708                         c->log, c->argv[i], 0);
1709                 return(1);
1710         }
1711         global_disallows |= disallows;
1712         return(0);
1713 }
1714
1715 static int
1716 config_requires(ConfigArgs *c) {
1717         slap_mask_t requires = 0;
1718         int i;
1719         struct verb_mask_list requires_ops[] = {
1720                 { "bind",               SLAP_REQUIRE_BIND },
1721                 { "LDAPv3",             SLAP_REQUIRE_LDAP_V3 },
1722                 { "authc",              SLAP_REQUIRE_AUTHC },
1723                 { "sasl",               SLAP_REQUIRE_SASL },
1724                 { "strong",             SLAP_REQUIRE_STRONG },
1725                 { NULL, 0 }
1726         };
1727         if (c->emit) {
1728                 return 1;
1729         }
1730         i = verbs_to_mask(c, requires_ops, &requires);
1731         if ( i ) {
1732                 Debug(LDAP_DEBUG_ANY, "%s: "
1733                         "unknown feature %s in \"require <features>\" line\n",
1734                         c->log, c->argv[i], 0);
1735                 return(1);
1736         }
1737         c->be->be_requires = requires;
1738         return(0);
1739 }
1740
1741 static int
1742 config_loglevel(ConfigArgs *c) {
1743         int i;
1744         char *next;
1745         struct verb_mask_list loglevel_ops[] = {
1746                 { "Trace",      LDAP_DEBUG_TRACE },
1747                 { "Packets",    LDAP_DEBUG_PACKETS },
1748                 { "Args",       LDAP_DEBUG_ARGS },
1749                 { "Conns",      LDAP_DEBUG_CONNS },
1750                 { "BER",        LDAP_DEBUG_BER },
1751                 { "Filter",     LDAP_DEBUG_FILTER },
1752                 { "Config",     LDAP_DEBUG_CONFIG },
1753                 { "ACL",        LDAP_DEBUG_ACL },
1754                 { "Stats",      LDAP_DEBUG_STATS },
1755                 { "Stats2",     LDAP_DEBUG_STATS2 },
1756                 { "Shell",      LDAP_DEBUG_SHELL },
1757                 { "Parse",      LDAP_DEBUG_PARSE },
1758                 { "Cache",      LDAP_DEBUG_CACHE },
1759                 { "Index",      LDAP_DEBUG_INDEX },
1760                 { "Any",        -1 },
1761                 { NULL, 0 }
1762         };
1763         ldap_syslog = 0;
1764
1765         if (c->emit) {
1766                 return 1;
1767         }
1768
1769         for( i=1; i < c->argc; i++ ) {
1770                 int     level;
1771
1772                 if ( isdigit( c->argv[i][0] ) ) {
1773                         level = strtol( c->argv[i], &next, 10 );
1774                         if ( next == NULL || next[0] != '\0' ) {
1775                                 Debug( LDAP_DEBUG_ANY,
1776                                         "%s: unable to parse level \"%s\" "
1777                                         "in \"loglevel <level> [...]\" line.\n",
1778                                         c->log, c->argv[i], 0);
1779                                 return( 1 );
1780                         }
1781                 } else {
1782                         int j = verb_to_mask(c, loglevel_ops, c->argv[i][0]);
1783                         if(!loglevel_ops[j].word) {
1784                                 Debug( LDAP_DEBUG_ANY,
1785                                         "%s: unknown level \"%s\" "
1786                                         "in \"loglevel <level> [...]\" line.\n",
1787                                         c->log, c->argv[i], 0);
1788                                 return( 1 );
1789                         }
1790                         level = loglevel_ops[j].mask;
1791                 }
1792                 ldap_syslog |= level;
1793         }
1794         return(0);
1795 }
1796
1797 static int
1798 config_syncrepl(ConfigArgs *c) {
1799         if (c->emit) {
1800                 return 1;
1801         }
1802         if(SLAP_SHADOW(c->be)) {
1803                 Debug(LDAP_DEBUG_ANY, "%s: "
1804                         "syncrepl: database already shadowed.\n",
1805                         c->log, 0, 0);
1806                 return(1);
1807         } else if(add_syncrepl(c->be, c->argv, c->argc)) {
1808                 return(1);
1809         }
1810         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);
1811         return(0);
1812 }
1813
1814 static int
1815 config_referral(ConfigArgs *c) {
1816         struct berval vals[2];
1817         if (c->emit) {
1818                 return 1;
1819         }
1820         if(validate_global_referral(c->argv[1])) {
1821                 Debug(LDAP_DEBUG_ANY, "%s: "
1822                         "invalid URL (%s) in \"referral\" line.\n",
1823                         c->log, c->argv[1], 0);
1824                 return(1);
1825         }
1826
1827         ber_str2bv(c->argv[1], 0, 1, &vals[0]);
1828         vals[1].bv_val = NULL; vals[1].bv_len = 0;
1829         if(value_add(&default_referral, vals)) return(LDAP_OTHER);
1830         return(0);
1831 }
1832
1833 static int
1834 config_security(ConfigArgs *c) {
1835         slap_ssf_set_t *set = &c->be->be_ssf_set;
1836         char *next;
1837         int i;
1838         if (c->emit) {
1839                 return 1;
1840         }
1841         for(i = 1; i < c->argc; i++) {
1842                 slap_ssf_t *tgt;
1843                 char *src;
1844                 if(!strncasecmp(c->argv[i], "ssf=", 4)) {
1845                         tgt = &set->sss_ssf;
1846                         src = &c->argv[i][4];
1847                 } else if(!strncasecmp(c->argv[i], "transport=", 10)) {
1848                         tgt = &set->sss_transport;
1849                         src = &c->argv[i][10];
1850                 } else if(!strncasecmp(c->argv[i], "tls=", 4)) {
1851                         tgt = &set->sss_tls;
1852                         src = &c->argv[i][4];
1853                 } else if(!strncasecmp(c->argv[i], "sasl=", 5)) {
1854                         tgt = &set->sss_sasl;
1855                         src = &c->argv[i][5];
1856                 } else if(!strncasecmp(c->argv[i], "update_ssf=", 11)) {
1857                         tgt = &set->sss_update_ssf;
1858                         src = &c->argv[i][11];
1859                 } else if(!strncasecmp(c->argv[i], "update_transport=", 17)) {
1860                         tgt = &set->sss_update_transport;
1861                         src = &c->argv[i][17];
1862                 } else if(!strncasecmp(c->argv[i], "update_tls=", 11)) {
1863                         tgt = &set->sss_update_tls;
1864                         src = &c->argv[i][11];
1865                 } else if(!strncasecmp(c->argv[i], "update_sasl=", 12)) {
1866                         tgt = &set->sss_update_sasl;
1867                         src = &c->argv[i][12];
1868                 } else if(!strncasecmp(c->argv[i], "simple_bind=", 12)) {
1869                         tgt = &set->sss_simple_bind;
1870                         src = &c->argv[i][12];
1871                 } else {
1872                         Debug(LDAP_DEBUG_ANY, "%s: "
1873                                 "unknown factor %s in \"security <factors>\" line\n",
1874                                 c->log, c->argv[i], 0);
1875                         return(1);
1876                 }
1877
1878                 *tgt = strtol(src, &next, 10);
1879                 if(next == NULL || next[0] != '\0' ) {
1880                         Debug(LDAP_DEBUG_ANY, "%s: "
1881                                 "unable to parse factor \"%s\" in \"security <factors>\" line\n",
1882                                 c->log, c->argv[i], 0);
1883                         return(1);
1884                 }
1885         }
1886         return(0);
1887 }
1888
1889 static int
1890 config_replica(ConfigArgs *c) {
1891         int i, nr = -1;
1892         char *replicahost, *replicalog = NULL;
1893         LDAPURLDesc *ludp;
1894
1895         if (c->emit) {
1896                 return 1;
1897         }
1898         if(SLAP_MONITOR(c->be)) {
1899                 Debug(LDAP_DEBUG_ANY, "%s: "
1900                         "\"replica\" should not be used inside monitor database\n",
1901                         c->log, 0, 0);
1902                 return(0);      /* FIXME: should this be an error? */
1903         }
1904
1905         for(i = 1; i < c->argc; i++) {
1906                 if(!strncasecmp(c->argv[i], "host=", STRLENOF("host="))) {
1907                         replicalog = c->argv[i] + STRLENOF("host=");
1908                         nr = add_replica_info(c->be, c->argv[i] + STRLENOF("host="));
1909                         break;
1910                 } else if(!strncasecmp(c->argv[i], "uri=", STRLENOF("uri="))) {
1911                         if(ldap_url_parse(c->argv[i] + STRLENOF("uri="), &ludp) != LDAP_SUCCESS) {
1912                                 Debug(LDAP_DEBUG_ANY, "%s: "
1913                                         "replica line contains invalid "
1914                                         "uri definition.\n", c->log, 0, 0);
1915                                 return(1);
1916                         }
1917                         if(!ludp->lud_host) {
1918                                 Debug(LDAP_DEBUG_ANY, "%s: "
1919                                         "replica line contains invalid "
1920                                         "uri definition - missing hostname.\n",
1921                                         c->log, 0, 0);
1922                                 return(1);
1923                         }
1924                         replicahost = ch_malloc(strlen(c->argv[i]));
1925                         if(!replicahost) {
1926                                 Debug(LDAP_DEBUG_ANY,
1927                                         "out of memory in read_config\n", 0, 0, 0);
1928                                 ldap_free_urldesc(ludp);
1929                                 exit(EXIT_FAILURE);
1930                         }
1931                         sprintf(replicahost, "%s:%d", ludp->lud_host, ludp->lud_port);
1932                         replicalog = c->argv[i] + STRLENOF("uri=");
1933                         nr = add_replica_info(c->be, replicahost);
1934                         ldap_free_urldesc(ludp);
1935                         ch_free(replicahost);
1936                         break;
1937                 }
1938         }
1939         if(i == c->argc) {
1940                 Debug(LDAP_DEBUG_ANY, "%s: "
1941                         "missing host or uri in \"replica\" line\n",
1942                         c->log, 0, 0);
1943                 return(1);
1944         } else if(nr == -1) {
1945                 Debug(LDAP_DEBUG_ANY, "%s: "
1946                         "unable to add replica \"%s\"\n",
1947                         c->log, replicalog, 0);
1948                 return(1);
1949         } else {
1950                 for(i = 1; i < c->argc; i++) {
1951                         if(!strncasecmp(c->argv[i], "suffix=", STRLENOF( "suffix="))) {
1952                                 switch(add_replica_suffix(c->be, nr, c->argv[i] + STRLENOF("suffix="))) {
1953                                         case 1:
1954                                                 Debug(LDAP_DEBUG_ANY, "%s: "
1955                                                 "suffix \"%s\" in \"replica\" line is not valid for backend (ignored)\n",
1956                                                 c->log, c->argv[i] + STRLENOF("suffix="), 0);
1957                                                 break;
1958                                         case 2:
1959                                                 Debug(LDAP_DEBUG_ANY, "%s: "
1960                                                 "unable to normalize suffix in \"replica\" line (ignored)\n",
1961                                                 c->log, 0, 0);
1962                                                 break;
1963                                 }
1964
1965                         } else if(!strncasecmp(c->argv[i], "attr", STRLENOF("attr"))) {
1966                                 int exclude = 0;
1967                                 char *arg = c->argv[i] + STRLENOF("attr");
1968                                 if(arg[0] == '!') {
1969                                         arg++;
1970                                         exclude = 1;
1971                                 }
1972                                 if(arg[0] != '=') {
1973                                         continue;
1974                                 }
1975                                 if(add_replica_attrs(c->be, nr, arg + 1, exclude)) {
1976                                         Debug(LDAP_DEBUG_ANY, "%s: "
1977                                                 "attribute \"%s\" in \"replica\" line is unknown\n",
1978                                                 c->log, arg + 1, 0);
1979                                         return(1);
1980                                 }
1981                         }
1982                 }
1983         }
1984         return(0);
1985 }
1986
1987 static int
1988 config_updatedn(ConfigArgs *c) {
1989         struct berval dn;
1990         int rc;
1991         if (c->emit) {
1992                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
1993                         ber_bvarray_add(&c->rvalue_vals, &c->be->be_update_ndn);
1994                         ber_bvarray_add(&c->rvalue_nvals, &c->be->be_update_ndn);
1995                         return 0;
1996                 }
1997                 return 1;
1998         }
1999         if(SLAP_SHADOW(c->be)) {
2000                 Debug(LDAP_DEBUG_ANY, "%s: "
2001                         "updatedn: database already shadowed.\n",
2002                         c->log, 0, 0);
2003                 return(1);
2004         }
2005
2006         ber_str2bv(c->argv[1], 0, 0, &dn);
2007
2008         rc = dnNormalize(0, NULL, NULL, &dn, &c->be->be_update_ndn, NULL);
2009
2010         if(rc != LDAP_SUCCESS) {
2011                 Debug(LDAP_DEBUG_ANY, "%s: "
2012                         "updatedn DN is invalid: %d (%s)\n",
2013                         c->log, rc, ldap_err2string( rc ));
2014                 return(1);
2015         }
2016
2017         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
2018         return(0);
2019 }
2020
2021 static int
2022 config_updateref(ConfigArgs *c) {
2023         struct berval vals[2];
2024         if (c->emit) {
2025                 return 1;
2026         }
2027         if(!SLAP_SHADOW(c->be)) {
2028                 Debug(LDAP_DEBUG_ANY, "%s: "
2029                         "updateref line must after syncrepl or updatedn.\n",
2030                         c->log, 0, 0);
2031                 return(1);
2032         }
2033
2034         if(validate_global_referral(c->argv[1])) {
2035                 Debug(LDAP_DEBUG_ANY, "%s: "
2036                         "invalid URL (%s) in \"updateref\" line.\n",
2037                         c->log, c->argv[1], 0);
2038                 return(1);
2039         }
2040         ber_str2bv(c->argv[1], 0, 0, &vals[0]);
2041         vals[1].bv_val = NULL;
2042         if(value_add(&c->be->be_update_refs, vals)) return(LDAP_OTHER);
2043         return(0);
2044 }
2045
2046 /* XXX meaningless in ldif */
2047
2048 static int
2049 config_include(ConfigArgs *c) {
2050         unsigned long savelineno = c->lineno;
2051         int rc;
2052         ConfigFile *cf = ch_calloc( 1, sizeof(ConfigFile));
2053         ConfigFile *cfsave = cfn;
2054         ConfigFile *cf2 = NULL;
2055         if (c->emit) {
2056                 return 1;
2057         }
2058         if ( cfn->c_kids ) {
2059                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
2060                 cf2->c_sibs = cf;
2061         } else {
2062                 cfn->c_kids = cf;
2063         }
2064         cfn = cf;
2065         rc = read_config_file(c->argv[1], c->depth + 1, c);
2066         c->lineno = savelineno - 1;
2067         cfn = cfsave;
2068         if ( rc ) {
2069                 if ( cf2 ) cf2->c_sibs = NULL;
2070                 else cfn->c_kids = NULL;
2071                 ch_free( cf );
2072         }
2073         return(rc);
2074 }
2075
2076 #ifdef HAVE_TLS
2077 static int
2078 config_tls_option(ConfigArgs *c) {
2079         int flag;
2080         if (c->emit) {
2081                 return 1;
2082         }
2083         switch(c->type) {
2084         case CFG_TLS_RAND:              flag = LDAP_OPT_X_TLS_RANDOM_FILE;      break;
2085         case CFG_TLS_CIPHER:            flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
2086         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
2087         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
2088         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
2089         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
2090 #ifdef HAVE_OPENSSL_CRL
2091         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK;         break;
2092 #endif
2093                 default:                Debug(LDAP_DEBUG_ANY, "%s: "
2094                                                 "unknown tls_option <%x>\n",
2095                                                 c->log, c->type, 0);
2096         }
2097         return(ldap_pvt_tls_set_option(NULL, flag, c->argv[1]));
2098 }
2099
2100 static int
2101 config_tls_verify(ConfigArgs *c) {
2102         int i;
2103         if (c->emit) {
2104                 return 1;
2105         }
2106         if(isdigit((unsigned char)c->argv[1][0])) {
2107                 i = atoi(c->argv[1]);
2108                 return(ldap_pvt_tls_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &i));
2109         } else {
2110                 return(ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, c->argv[1]));
2111         }
2112 }
2113 #endif
2114
2115 /* -------------------------------------- */
2116
2117
2118 static char *
2119 strtok_quote( char *line, char *sep )
2120 {
2121         int             inquote;
2122         char            *tmp;
2123         static char     *next;
2124
2125         strtok_quote_ptr = NULL;
2126         if ( line != NULL ) {
2127                 next = line;
2128         }
2129         while ( *next && strchr( sep, *next ) ) {
2130                 next++;
2131         }
2132
2133         if ( *next == '\0' ) {
2134                 next = NULL;
2135                 return( NULL );
2136         }
2137         tmp = next;
2138
2139         for ( inquote = 0; *next; ) {
2140                 switch ( *next ) {
2141                 case '"':
2142                         if ( inquote ) {
2143                                 inquote = 0;
2144                         } else {
2145                                 inquote = 1;
2146                         }
2147                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
2148                         break;
2149
2150                 case '\\':
2151                         if ( next[1] )
2152                                 AC_MEMCPY( next,
2153                                             next + 1, strlen( next + 1 ) + 1 );
2154                         next++;         /* dont parse the escaped character */
2155                         break;
2156
2157                 default:
2158                         if ( ! inquote ) {
2159                                 if ( strchr( sep, *next ) != NULL ) {
2160                                         strtok_quote_ptr = next;
2161                                         *next++ = '\0';
2162                                         return( tmp );
2163                                 }
2164                         }
2165                         next++;
2166                         break;
2167                 }
2168         }
2169
2170         return( tmp );
2171 }
2172
2173 static char     buf[BUFSIZ];
2174 static char     *line;
2175 static size_t lmax, lcur;
2176
2177 #define CATLINE( buf ) \
2178         do { \
2179                 size_t len = strlen( buf ); \
2180                 while ( lcur + len + 1 > lmax ) { \
2181                         lmax += BUFSIZ; \
2182                         line = (char *) ch_realloc( line, lmax ); \
2183                 } \
2184                 strcpy( line + lcur, buf ); \
2185                 lcur += len; \
2186         } while( 0 )
2187
2188 static void
2189 fp_getline_init(ConfigArgs *c) {
2190         c->lineno = -1;
2191         buf[0] = '\0';
2192 }
2193
2194 static int
2195 fp_getline( FILE *fp, ConfigArgs *c )
2196 {
2197         char    *p;
2198
2199         lcur = 0;
2200         CATLINE(buf);
2201         c->lineno++;
2202
2203         /* avoid stack of bufs */
2204         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
2205                 buf[0] = '\0';
2206                 c->line = line;
2207                 return(1);
2208         }
2209
2210         while ( fgets( buf, sizeof( buf ), fp ) ) {
2211                 p = strchr( buf, '\n' );
2212                 if ( p ) {
2213                         if ( p > buf && p[-1] == '\r' ) {
2214                                 --p;
2215                         }
2216                         *p = '\0';
2217                 }
2218                 /* XXX ugly */
2219                 c->line = line;
2220                 if ( line[0]
2221                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
2222                                 && p[-1] != '\\' )
2223                 {
2224                         p[0] = '\0';
2225                         lcur--;
2226                         
2227                 } else {
2228                         if ( !isspace( (unsigned char)buf[0] ) ) {
2229                                 return(1);
2230                         }
2231                         buf[0] = ' ';
2232                 }
2233                 CATLINE(buf);
2234                 c->lineno++;
2235         }
2236
2237         buf[0] = '\0';
2238         c->line = line;
2239         return(line[0] ? 1 : 0);
2240 }
2241
2242 static int
2243 fp_parse_line(ConfigArgs *c)
2244 {
2245         char *token;
2246         char *tline = ch_strdup(c->line);
2247         char *hide[] = { "rootpw", "replica", "bindpw", "pseudorootpw", "dbpasswd", '\0' };
2248         int i;
2249
2250         c->argc = 0;
2251         token = strtok_quote(tline, " \t");
2252
2253         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
2254         if(strtok_quote_ptr) *strtok_quote_ptr = ' ';
2255         Debug(LDAP_DEBUG_CONFIG, "line %lu (%s%s)\n", c->lineno, hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
2256         if(strtok_quote_ptr) *strtok_quote_ptr = '\0';
2257
2258         for(; token; token = strtok_quote(NULL, " \t")) {
2259                 if(c->argc == c->argv_size - 1) {
2260                         char **tmp;
2261                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
2262                         if(!tmp) {
2263                                 Debug(LDAP_DEBUG_ANY, "line %lu: out of memory\n", c->lineno, 0, 0);
2264                                 return -1;
2265                         }
2266                         c->argv = tmp;
2267                         c->argv_size += ARGS_STEP;
2268                 }
2269                 c->argv[c->argc++] = token;
2270         }
2271         c->argv[c->argc] = NULL;
2272         return(0);
2273 }
2274
2275
2276 #if 0
2277 /* Loads ucdata, returns 1 if loading, 0 if already loaded, -1 on error */
2278 static int
2279 load_ucdata( char *path )
2280 {
2281 #if 0
2282         static int loaded = 0;
2283         int err;
2284         
2285         if ( loaded ) {
2286                 return( 0 );
2287         }
2288         err = ucdata_load( path ? path : SLAPD_DEFAULT_UCDATA, UCDATA_ALL );
2289         if ( err ) {
2290                 Debug( LDAP_DEBUG_ANY, "error loading ucdata (error %d)\n",
2291                        err, 0, 0 );
2292
2293                 return( -1 );
2294         }
2295         loaded = 1;
2296         return( 1 );
2297 #else
2298         /* ucdata is now hardcoded */
2299         return( 0 );
2300 #endif
2301 }
2302 #endif
2303
2304 void
2305 config_destroy( )
2306 {
2307         ucdata_unload( UCDATA_ALL );
2308         if ( frontendDB ) {
2309                 /* NOTE: in case of early exit, frontendDB can be NULL */
2310                 if ( frontendDB->be_schemandn.bv_val )
2311                         free( frontendDB->be_schemandn.bv_val );
2312                 if ( frontendDB->be_schemadn.bv_val )
2313                         free( frontendDB->be_schemadn.bv_val );
2314                 if ( frontendDB->be_acl )
2315                         acl_destroy( frontendDB->be_acl, NULL );
2316         }
2317         free( line );
2318         if ( slapd_args_file )
2319                 free ( slapd_args_file );
2320         if ( slapd_pid_file )
2321                 free ( slapd_pid_file );
2322         if ( default_passwd_hash )
2323                 ldap_charray_free( default_passwd_hash );
2324 }
2325
2326 static int
2327 add_syncrepl(
2328         Backend *be,
2329         char    **cargv,
2330         int     cargc
2331 )
2332 {
2333         syncinfo_t *si;
2334         int     rc = 0;
2335
2336         si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
2337
2338         if ( si == NULL ) {
2339                 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
2340                 return 1;
2341         }
2342
2343         si->si_tls = SYNCINFO_TLS_OFF;
2344         si->si_bindmethod = LDAP_AUTH_SIMPLE;
2345         si->si_schemachecking = 0;
2346         ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
2347                 &si->si_filterstr );
2348         si->si_base.bv_val = NULL;
2349         si->si_scope = LDAP_SCOPE_SUBTREE;
2350         si->si_attrsonly = 0;
2351         si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
2352         si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
2353         si->si_attrs = NULL;
2354         si->si_allattrs = 0;
2355         si->si_allopattrs = 0;
2356         si->si_exattrs = NULL;
2357         si->si_type = LDAP_SYNC_REFRESH_ONLY;
2358         si->si_interval = 86400;
2359         si->si_retryinterval = NULL;
2360         si->si_retrynum_init = NULL;
2361         si->si_retrynum = NULL;
2362         si->si_manageDSAit = 0;
2363         si->si_tlimit = 0;
2364         si->si_slimit = 0;
2365
2366         si->si_presentlist = NULL;
2367         LDAP_LIST_INIT( &si->si_nonpresentlist );
2368         ldap_pvt_thread_mutex_init( &si->si_mutex );
2369
2370         rc = parse_syncrepl_line( cargv, cargc, si );
2371
2372         if ( rc < 0 ) {
2373                 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
2374                 syncinfo_free( si );    
2375                 return 1;
2376         } else {
2377                 Debug( LDAP_DEBUG_CONFIG,
2378                         "Config: ** successfully added syncrepl \"%s\"\n",
2379                         BER_BVISNULL( &si->si_provideruri ) ?
2380                         "(null)" : si->si_provideruri.bv_val, 0, 0 );
2381                 if ( !si->si_schemachecking ) {
2382                         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
2383                 }
2384                 si->si_be = be;
2385                 be->be_syncinfo = si;
2386                 return 0;
2387         }
2388 }
2389
2390 /* NOTE: used & documented in slapd.conf(5) */
2391 #define IDSTR                   "rid"
2392 #define PROVIDERSTR             "provider"
2393 #define TYPESTR                 "type"
2394 #define INTERVALSTR             "interval"
2395 #define SEARCHBASESTR           "searchbase"
2396 #define FILTERSTR               "filter"
2397 #define SCOPESTR                "scope"
2398 #define ATTRSSTR                "attrs"
2399 #define ATTRSONLYSTR            "attrsonly"
2400 #define SLIMITSTR               "sizelimit"
2401 #define TLIMITSTR               "timelimit"
2402 #define SCHEMASTR               "schemachecking"
2403 #define BINDMETHSTR             "bindmethod"
2404 #define SIMPLESTR                       "simple"
2405 #define SASLSTR                         "sasl"
2406 #define BINDDNSTR               "binddn"
2407 #define SASLMECHSTR             "saslmech"
2408 #define AUTHCSTR                "authcID"
2409 #define AUTHZSTR                "authzID"
2410 #define CREDSTR                 "credentials"
2411 #define REALMSTR                "realm"
2412 #define SECPROPSSTR             "secprops"
2413
2414 /* FIXME: undocumented */
2415 #define OLDAUTHCSTR             "bindprincipal"
2416 #define STARTTLSSTR             "starttls"
2417 #define CRITICALSTR                     "critical"
2418 #define EXATTRSSTR              "exattrs"
2419 #define MANAGEDSAITSTR          "manageDSAit"
2420 #define RETRYSTR                "retry"
2421
2422 /* FIXME: unused */
2423 #define LASTMODSTR              "lastmod"
2424 #define LMGENSTR                "gen"
2425 #define LMNOSTR                 "no"
2426 #define LMREQSTR                "req"
2427 #define SRVTABSTR               "srvtab"
2428 #define SUFFIXSTR               "suffix"
2429 #define UPDATEDNSTR             "updatedn"
2430
2431 /* mandatory */
2432 #define GOT_ID                  0x0001
2433 #define GOT_PROVIDER            0x0002
2434 #define GOT_METHOD              0x0004
2435
2436 /* check */
2437 #define GOT_ALL                 (GOT_ID|GOT_PROVIDER|GOT_METHOD)
2438
2439 static int
2440 parse_syncrepl_line(
2441         char            **cargv,
2442         int             cargc,
2443         syncinfo_t      *si
2444 )
2445 {
2446         int     gots = 0;
2447         int     i;
2448         char    *val;
2449
2450         for ( i = 1; i < cargc; i++ ) {
2451                 if ( !strncasecmp( cargv[ i ], IDSTR "=",
2452                                         STRLENOF( IDSTR "=" ) ) )
2453                 {
2454                         int tmp;
2455                         /* '\0' string terminator accounts for '=' */
2456                         val = cargv[ i ] + STRLENOF( IDSTR "=" );
2457                         tmp= atoi( val );
2458                         if ( tmp >= 1000 || tmp < 0 ) {
2459                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2460                                          "syncrepl id %d is out of range [0..999]\n", tmp );
2461                                 return -1;
2462                         }
2463                         si->si_rid = tmp;
2464                         gots |= GOT_ID;
2465                 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
2466                                         STRLENOF( PROVIDERSTR "=" ) ) )
2467                 {
2468                         val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
2469                         ber_str2bv( val, 0, 1, &si->si_provideruri );
2470                         gots |= GOT_PROVIDER;
2471                 } else if ( !strncasecmp( cargv[ i ], STARTTLSSTR "=",
2472                                         STRLENOF(STARTTLSSTR "=") ) )
2473                 {
2474                         val = cargv[ i ] + STRLENOF( STARTTLSSTR "=" );
2475                         if( !strcasecmp( val, CRITICALSTR ) ) {
2476                                 si->si_tls = SYNCINFO_TLS_CRITICAL;
2477                         } else {
2478                                 si->si_tls = SYNCINFO_TLS_ON;
2479                         }
2480                 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR "=",
2481                                 STRLENOF( BINDMETHSTR "=" ) ) )
2482                 {
2483                         val = cargv[ i ] + STRLENOF( BINDMETHSTR "=" );
2484                         if ( !strcasecmp( val, SIMPLESTR )) {
2485                                 si->si_bindmethod = LDAP_AUTH_SIMPLE;
2486                                 gots |= GOT_METHOD;
2487                         } else if ( !strcasecmp( val, SASLSTR )) {
2488 #ifdef HAVE_CYRUS_SASL
2489                                 si->si_bindmethod = LDAP_AUTH_SASL;
2490                                 gots |= GOT_METHOD;
2491 #else /* HAVE_CYRUS_SASL */
2492                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2493                                         "not compiled with SASL support\n" );
2494                                 return -1;
2495 #endif /* HAVE_CYRUS_SASL */
2496                         } else {
2497                                 si->si_bindmethod = -1;
2498                         }
2499                 } else if ( !strncasecmp( cargv[ i ], BINDDNSTR "=",
2500                                         STRLENOF( BINDDNSTR "=" ) ) )
2501                 {
2502                         val = cargv[ i ] + STRLENOF( BINDDNSTR "=" );
2503                         si->si_binddn = ch_strdup( val );
2504                 } else if ( !strncasecmp( cargv[ i ], CREDSTR "=",
2505                                         STRLENOF( CREDSTR "=" ) ) )
2506                 {
2507                         val = cargv[ i ] + STRLENOF( CREDSTR "=" );
2508                         si->si_passwd = ch_strdup( val );
2509                 } else if ( !strncasecmp( cargv[ i ], SASLMECHSTR "=",
2510                                         STRLENOF( SASLMECHSTR "=" ) ) )
2511                 {
2512                         val = cargv[ i ] + STRLENOF( SASLMECHSTR "=" );
2513                         si->si_saslmech = ch_strdup( val );
2514                 } else if ( !strncasecmp( cargv[ i ], SECPROPSSTR "=",
2515                                         STRLENOF( SECPROPSSTR "=" ) ) )
2516                 {
2517                         val = cargv[ i ] + STRLENOF( SECPROPSSTR "=" );
2518                         si->si_secprops = ch_strdup( val );
2519                 } else if ( !strncasecmp( cargv[ i ], REALMSTR "=",
2520                                         STRLENOF( REALMSTR "=" ) ) )
2521                 {
2522                         val = cargv[ i ] + STRLENOF( REALMSTR "=" );
2523                         si->si_realm = ch_strdup( val );
2524                 } else if ( !strncasecmp( cargv[ i ], AUTHCSTR "=",
2525                                         STRLENOF( AUTHCSTR "=" ) ) )
2526                 {
2527                         val = cargv[ i ] + STRLENOF( AUTHCSTR "=" );
2528                         if ( si->si_authcId )
2529                                 ch_free( si->si_authcId );
2530                         si->si_authcId = ch_strdup( val );
2531                 } else if ( !strncasecmp( cargv[ i ], OLDAUTHCSTR "=",
2532                                         STRLENOF( OLDAUTHCSTR "=" ) ) ) 
2533                 {
2534                         /* Old authcID is provided for some backwards compatibility */
2535                         val = cargv[ i ] + STRLENOF( OLDAUTHCSTR "=" );
2536                         if ( si->si_authcId )
2537                                 ch_free( si->si_authcId );
2538                         si->si_authcId = ch_strdup( val );
2539                 } else if ( !strncasecmp( cargv[ i ], AUTHZSTR "=",
2540                                         STRLENOF( AUTHZSTR "=" ) ) )
2541                 {
2542                         val = cargv[ i ] + STRLENOF( AUTHZSTR "=" );
2543                         si->si_authzId = ch_strdup( val );
2544                 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
2545                                         STRLENOF( SCHEMASTR "=" ) ) )
2546                 {
2547                         val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
2548                         if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2549                                 si->si_schemachecking = 1;
2550                         } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2551                                 si->si_schemachecking = 0;
2552                         } else {
2553                                 si->si_schemachecking = 1;
2554                         }
2555                 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
2556                                         STRLENOF( FILTERSTR "=" ) ) )
2557                 {
2558                         val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
2559                         ber_str2bv( val, 0, 1, &si->si_filterstr );
2560                 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
2561                                         STRLENOF( SEARCHBASESTR "=" ) ) )
2562                 {
2563                         struct berval   bv;
2564                         int             rc;
2565
2566                         val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2567                         if ( si->si_base.bv_val ) {
2568                                 ch_free( si->si_base.bv_val );
2569                         }
2570                         ber_str2bv( val, 0, 0, &bv );
2571                         rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2572                         if ( rc != LDAP_SUCCESS ) {
2573                                 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
2574                                         val, rc, ldap_err2string( rc ) );
2575                                 return -1;
2576                         }
2577                 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
2578                                         STRLENOF( SCOPESTR "=" ) ) )
2579                 {
2580                         val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
2581                         if ( !strncasecmp( val, "base", STRLENOF( "base" ) )) {
2582                                 si->si_scope = LDAP_SCOPE_BASE;
2583                         } else if ( !strncasecmp( val, "one", STRLENOF( "one" ) )) {
2584                                 si->si_scope = LDAP_SCOPE_ONELEVEL;
2585 #ifdef LDAP_SCOPE_SUBORDINATE
2586                         } else if ( !strcasecmp( val, "subordinate" ) ||
2587                                 !strcasecmp( val, "children" ))
2588                         {
2589                                 si->si_scope = LDAP_SCOPE_SUBORDINATE;
2590 #endif
2591                         } else if ( !strncasecmp( val, "sub", STRLENOF( "sub" ) )) {
2592                                 si->si_scope = LDAP_SCOPE_SUBTREE;
2593                         } else {
2594                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2595                                         "unknown scope \"%s\"\n", val);
2596                                 return -1;
2597                         }
2598                 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
2599                                         STRLENOF( ATTRSONLYSTR "=" ) ) )
2600                 {
2601                         si->si_attrsonly = 1;
2602                 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
2603                                         STRLENOF( ATTRSSTR "=" ) ) )
2604                 {
2605                         val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
2606                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2607                                 char *attr_fname;
2608                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2609                                 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2610                                 if ( si->si_anlist == NULL ) {
2611                                         ch_free( attr_fname );
2612                                         return -1;
2613                                 }
2614                                 ch_free( attr_fname );
2615                         } else {
2616                                 char *str, *s, *next;
2617                                 char delimstr[] = " ,\t";
2618                                 str = ch_strdup( val );
2619                                 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2620                                                 s != NULL;
2621                                                 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2622                                 {
2623                                         if ( strlen(s) == 1 && *s == '*' ) {
2624                                                 si->si_allattrs = 1;
2625                                                 *(val + ( s - str )) = delimstr[0];
2626                                         }
2627                                         if ( strlen(s) == 1 && *s == '+' ) {
2628                                                 si->si_allopattrs = 1;
2629                                                 *(val + ( s - str )) = delimstr[0];
2630                                         }
2631                                 }
2632                                 ch_free( str );
2633                                 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2634                                 if ( si->si_anlist == NULL ) {
2635                                         return -1;
2636                                 }
2637                         }
2638                 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
2639                                         STRLENOF( EXATTRSSTR "=" ) ) )
2640                 {
2641                         val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
2642                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2643                                 char *attr_fname;
2644                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2645                                 si->si_exanlist = file2anlist(
2646                                                                         si->si_exanlist, attr_fname, " ,\t" );
2647                                 if ( si->si_exanlist == NULL ) {
2648                                         ch_free( attr_fname );
2649                                         return -1;
2650                                 }
2651                                 ch_free( attr_fname );
2652                         } else {
2653                                 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2654                                 if ( si->si_exanlist == NULL ) {
2655                                         return -1;
2656                                 }
2657                         }
2658                 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
2659                                         STRLENOF( TYPESTR "=" ) ) )
2660                 {
2661                         val = cargv[ i ] + STRLENOF( TYPESTR "=" );
2662                         if ( !strncasecmp( val, "refreshOnly",
2663                                                 STRLENOF("refreshOnly") ))
2664                         {
2665                                 si->si_type = LDAP_SYNC_REFRESH_ONLY;
2666                         } else if ( !strncasecmp( val, "refreshAndPersist",
2667                                                 STRLENOF("refreshAndPersist") ))
2668                         {
2669                                 si->si_type = LDAP_SYNC_REFRESH_AND_PERSIST;
2670                                 si->si_interval = 60;
2671                         } else {
2672                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2673                                         "unknown sync type \"%s\"\n", val);
2674                                 return -1;
2675                         }
2676                 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2677                                         STRLENOF( INTERVALSTR "=" ) ) )
2678                 {
2679                         val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2680                         if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2681                                 si->si_interval = 0;
2682                         } else {
2683                                 char *hstr;
2684                                 char *mstr;
2685                                 char *dstr;
2686                                 char *sstr;
2687                                 int dd, hh, mm, ss;
2688                                 dstr = val;
2689                                 hstr = strchr( dstr, ':' );
2690                                 if ( hstr == NULL ) {
2691                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2692                                                 "invalid interval \"%s\"\n", val );
2693                                         return -1;
2694                                 }
2695                                 *hstr++ = '\0';
2696                                 mstr = strchr( hstr, ':' );
2697                                 if ( mstr == NULL ) {
2698                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2699                                                 "invalid interval \"%s\"\n", val );
2700                                         return -1;
2701                                 }
2702                                 *mstr++ = '\0';
2703                                 sstr = strchr( mstr, ':' );
2704                                 if ( sstr == NULL ) {
2705                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2706                                                 "invalid interval \"%s\"\n", val );
2707                                         return -1;
2708                                 }
2709                                 *sstr++ = '\0';
2710
2711                                 dd = atoi( dstr );
2712                                 hh = atoi( hstr );
2713                                 mm = atoi( mstr );
2714                                 ss = atoi( sstr );
2715                                 if (( hh > 24 ) || ( hh < 0 ) ||
2716                                         ( mm > 60 ) || ( mm < 0 ) ||
2717                                         ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2718                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2719                                                 "invalid interval \"%s\"\n", val );
2720                                         return -1;
2721                                 }
2722                                 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2723                         }
2724                         if ( si->si_interval < 0 ) {
2725                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2726                                         "invalid interval \"%ld\"\n",
2727                                         (long) si->si_interval);
2728                                 return -1;
2729                         }
2730                 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2731                                         STRLENOF( RETRYSTR "=" ) ) )
2732                 {
2733                         char **retry_list;
2734                         int j, k, n;
2735
2736                         val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2737                         retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2738                         retry_list[0] = NULL;
2739
2740                         slap_str2clist( &retry_list, val, " ,\t" );
2741
2742                         for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2743                         n = k / 2;
2744                         if ( k % 2 ) {
2745                                 fprintf( stderr,
2746                                                 "Error: incomplete syncrepl retry list\n" );
2747                                 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2748                                         ch_free( retry_list[k] );
2749                                 }
2750                                 ch_free( retry_list );
2751                                 exit( EXIT_FAILURE );
2752                         }
2753                         si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2754                         si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2755                         si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2756                         for ( j = 0; j < n; j++ ) {
2757                                 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2758                                 if ( *retry_list[j*2+1] == '+' ) {
2759                                         si->si_retrynum_init[j] = -1;
2760                                         si->si_retrynum[j] = -1;
2761                                         j++;
2762                                         break;
2763                                 } else {
2764                                         si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2765                                         si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2766                                 }
2767                         }
2768                         si->si_retrynum_init[j] = -2;
2769                         si->si_retrynum[j] = -2;
2770                         si->si_retryinterval[j] = 0;
2771                         
2772                         for ( k = 0; retry_list && retry_list[k]; k++ ) {
2773                                 ch_free( retry_list[k] );
2774                         }
2775                         ch_free( retry_list );
2776                 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
2777                                         STRLENOF( MANAGEDSAITSTR "=" ) ) )
2778                 {
2779                         val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
2780                         si->si_manageDSAit = atoi( val );
2781                 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
2782                                         STRLENOF( SLIMITSTR "=") ) )
2783                 {
2784                         val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
2785                         si->si_slimit = atoi( val );
2786                 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
2787                                         STRLENOF( TLIMITSTR "=" ) ) )
2788                 {
2789                         val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
2790                         si->si_tlimit = atoi( val );
2791                 } else {
2792                         fprintf( stderr, "Error: parse_syncrepl_line: "
2793                                 "unknown keyword \"%s\"\n", cargv[ i ] );
2794                         return -1;
2795                 }
2796         }
2797
2798         if ( gots != GOT_ALL ) {
2799                 fprintf( stderr,
2800                         "Error: Malformed \"syncrepl\" line in slapd config file" );
2801                 return -1;
2802         }
2803
2804         return 0;
2805 }
2806
2807 char **
2808 slap_str2clist( char ***out, char *in, const char *brkstr )
2809 {
2810         char    *str;
2811         char    *s;
2812         char    *lasts;
2813         int     i, j;
2814         char    **new;
2815
2816         /* find last element in list */
2817         for (i = 0; *out && (*out)[i]; i++);
2818
2819         /* protect the input string from strtok */
2820         str = ch_strdup( in );
2821
2822         if ( *str == '\0' ) {
2823                 free( str );
2824                 return( *out );
2825         }
2826
2827         /* Count words in string */
2828         j=1;
2829         for ( s = str; *s; s++ ) {
2830                 if ( strchr( brkstr, *s ) != NULL ) {
2831                         j++;
2832                 }
2833         }
2834
2835         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
2836         new = *out + i;
2837         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2838                 s != NULL;
2839                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2840         {
2841                 *new = ch_strdup( s );
2842                 new++;
2843         }
2844
2845         *new = NULL;
2846         free( str );
2847         return( *out );
2848 }