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