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