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