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