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