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