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