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