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