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