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