]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
8417f3ab8bbf0fcd0c88a8c1db89322cacd75721
[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:        /* FIXME */
988                         rc = 1;
989                         break;
990                 case CFG_RO:
991                         c->value_int = (c->be->be_restrictops & SLAP_RESTRICT_OP_WRITES) != 0;
992                         break;
993                 case CFG_AZPOLICY:
994                         c->value_string = ch_strdup( slap_sasl_getpolicy());
995                         break;
996                 case CFG_AZREGEXP:
997                         slap_sasl_regexp_unparse( &c->rvalue_vals );
998                         if ( !c->rvalue_vals ) rc = 1;
999                         break;
1000 #ifdef HAVE_CYRUS_SASL
1001                 case CFG_SASLSECP: {
1002                         struct berval bv = BER_BVNULL;
1003                         slap_sasl_secprops_unparse( &bv );
1004                         if ( !BER_BVISNULL( &bv )) {
1005                                 ber_bvarray_add( &c->rvalue_vals, &bv );
1006                         } else {
1007                                 rc = 1;
1008                         }
1009                         }
1010                         break;
1011 #endif
1012                 case CFG_DEPTH:
1013                         c->value_int = c->be->be_max_deref_depth;
1014                         break;
1015                 case CFG_OID:
1016                         oidm_unparse( &c->rvalue_vals );
1017                         if ( !c->rvalue_vals )
1018                                 rc = 1;
1019                         break;
1020                 case CFG_CHECK:
1021                         c->value_int = global_schemacheck;
1022                         break;
1023                 case CFG_ACL: {
1024                         AccessControl *a;
1025                         char *src, *dst, ibuf[11];
1026                         struct berval bv, abv;
1027                         for (i=0, a=c->be->be_acl; a; i++,a=a->acl_next) {
1028                                 abv.bv_len = sprintf( ibuf, "{%x}", i );
1029                                 acl_unparse( a, &bv );
1030                                 abv.bv_val = ch_malloc( abv.bv_len + bv.bv_len + 1 );
1031                                 AC_MEMCPY( abv.bv_val, ibuf, abv.bv_len );
1032                                 /* Turn TAB / EOL into plain space */
1033                                 for (src=bv.bv_val,dst=abv.bv_val+abv.bv_len; *src; src++) {
1034                                         if (isspace(*src)) *dst++ = ' ';
1035                                         else *dst++ = *src;
1036                                 }
1037                                 *dst = '\0';
1038                                 if (dst[-1] == ' ') {
1039                                         dst--;
1040                                         *dst = '\0';
1041                                 }
1042                                 abv.bv_len = dst - abv.bv_val;
1043                                 ber_bvarray_add( &c->rvalue_vals, &abv );
1044                         }
1045                         rc = (!i);
1046                         break;
1047                 }
1048                 case CFG_REPLOG:
1049                         if ( c->be->be_replogfile )
1050                                 c->value_string = ch_strdup( c->be->be_replogfile );
1051                         break;
1052                 case CFG_ROOTDSE: {
1053                         ConfigFile *cf = (ConfigFile *)c->line;
1054                         if ( cf->c_dseFiles ) {
1055                                 value_add( &c->rvalue_vals, cf->c_dseFiles );
1056                         } else {
1057                                 rc = 1;
1058                         }
1059                         }
1060                         break;
1061                 case CFG_LOGFILE:
1062                         if ( logfileName )
1063                                 c->value_string = ch_strdup( logfileName );
1064                         else
1065                                 rc = 1;
1066                         break;
1067                 case CFG_LASTMOD:
1068                         c->value_int = (SLAP_NOLASTMOD(c->be) == 0);
1069                         break;
1070                 case CFG_SSTR_IF_MAX:
1071                         c->value_int = index_substr_if_maxlen;
1072                         break;
1073                 case CFG_SSTR_IF_MIN:
1074                         c->value_int = index_substr_if_minlen;
1075                         break;
1076 #ifdef SLAPD_MODULES
1077                 case CFG_MODLOAD: {
1078                         ConfigFile *cf = (ConfigFile *)c->line;
1079                         ModPaths *mp;
1080                         for (i=0, mp=&cf->c_modpaths; mp; mp=mp->mp_next, i++) {
1081                                 int j;
1082                                 if (!mp->mp_loads) continue;
1083                                 for (j=0; !BER_BVISNULL(&mp->mp_loads[j]); j++) {
1084                                         struct berval bv;
1085                                         bv.bv_val = c->log;
1086                                         bv.bv_len = sprintf( bv.bv_val, "{%d}{%d}%s", i, j,
1087                                                 mp->mp_loads[j].bv_val );
1088                                         value_add_one( &c->rvalue_vals, &bv );
1089                                 }
1090                         }
1091                         rc = c->rvalue_vals ? 0 : 1;
1092                         }
1093                         break;
1094                 case CFG_MODPATH: {
1095                         ConfigFile *cf = (ConfigFile *)c->line;
1096                         ModPaths *mp;
1097                         for (i=0, mp=&cf->c_modpaths; mp; mp=mp->mp_next, i++) {
1098                                 struct berval bv;
1099                                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
1100                                         continue;
1101                                 bv.bv_val = c->log;
1102                                 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
1103                                         mp->mp_path.bv_val );
1104                                 value_add_one( &c->rvalue_vals, &bv );
1105                         }
1106                         rc = c->rvalue_vals ? 0 : 1;
1107                         }
1108                         break;
1109 #endif
1110 #ifdef LDAP_SLAPI
1111                 case CFG_PLUGIN:
1112                         slapi_int_plugin_unparse( c->be, &c->rvalue_vals );
1113                         if ( !c->rvalue_vals ) rc = 1;
1114                         break;
1115 #endif
1116 #ifdef SLAP_AUTH_REWRITE
1117                 case CFG_REWRITE:
1118                         if ( authz_rewrites ) {
1119                                 struct berval bv, idx;
1120                                 char ibuf[32];
1121                                 int i;
1122
1123                                 idx.bv_val = ibuf;
1124                                 for ( i=0; !BER_BVISNULL( &authz_rewrites[i] ); i++ ) {
1125                                         idx.bv_len = sprintf( idx.bv_val, "{%d}", i );
1126                                         bv.bv_len = idx.bv_len + authz_rewrites[i].bv_len;
1127                                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
1128                                         strcpy( bv.bv_val, idx.bv_val );
1129                                         strcpy( bv.bv_val+idx.bv_len, authz_rewrites[i].bv_val );
1130                                         ber_bvarray_add( &c->rvalue_vals, &bv );
1131                                 }
1132                         }
1133                         if ( !c->rvalue_vals ) rc = 1;
1134                         break;
1135 #endif
1136                 default:
1137                         rc = 1;
1138                 }
1139                 return rc;
1140         }
1141
1142         p = strchr(c->line,'(' /*')'*/);
1143         switch(c->type) {
1144                 case CFG_BACKEND:
1145                         if(!(c->bi = backend_info(c->argv[1]))) {
1146                                 Debug(LDAP_DEBUG_ANY, "%s: "
1147                                         "backend %s failed init!\n", c->log, c->argv[1], 0);
1148                                 return(1);
1149                         }
1150                         break;
1151
1152                 case CFG_DATABASE:
1153                         c->bi = NULL;
1154                         /* NOTE: config is always the first backend!
1155                          */
1156                         if ( !strcasecmp( c->argv[1], "config" )) {
1157                                 c->be = backendDB;
1158                         } else if(!(c->be = backend_db_init(c->argv[1]))) {
1159                                 Debug(LDAP_DEBUG_ANY, "%s: "
1160                                         "database %s failed init!\n", c->log, c->argv[1], 0);
1161                                 return(1);
1162                         }
1163                         break;
1164
1165                 case CFG_CONCUR:
1166                         ldap_pvt_thread_set_concurrency(c->value_int);
1167                         break;
1168
1169                 case CFG_THREADS:
1170                         ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
1171                         connection_pool_max = c->value_int;     /* save for reference */
1172                         break;
1173
1174                 case CFG_SALT:
1175                         if ( passwd_salt ) ch_free( passwd_salt );
1176                         passwd_salt = c->value_string;
1177                         lutil_salt_format(passwd_salt);
1178                         break;
1179
1180                 case CFG_LIMITS:
1181                         if(limits_parse(c->be, c->fname, c->lineno, c->argc, c->argv))
1182                                 return(1);
1183                         break;
1184
1185                 case CFG_RO:
1186                         if(c->value_int)
1187                                 c->be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
1188                         else
1189                                 c->be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
1190                         break;
1191
1192                 case CFG_AZPOLICY:
1193                         ch_free(c->value_string);
1194                         if (slap_sasl_setpolicy( c->argv[1] )) {
1195                                 Debug(LDAP_DEBUG_ANY, "%s: unable to parse value \"%s\" in"
1196                                         " \"authz-policy <policy>\"\n",
1197                                         c->log, c->argv[1], 0 );
1198                                 return(1);
1199                         }
1200                         break;
1201                 
1202                 case CFG_AZREGEXP:
1203                         if (slap_sasl_regexp_config( c->argv[1], c->argv[2] ))
1204                                 return(1);
1205                         break;
1206                                 
1207 #ifdef HAVE_CYRUS_SASL
1208                 case CFG_SASLSECP:
1209                         {
1210                         char *txt = slap_sasl_secprops( c->argv[1] );
1211                         if ( txt ) {
1212                                 Debug(LDAP_DEBUG_ANY, "%s: sasl-secprops: %s\n",
1213                                         c->log, txt, 0 );
1214                                 return(1);
1215                         }
1216                         break;
1217                         }
1218 #endif
1219
1220                 case CFG_DEPTH:
1221                         c->be->be_max_deref_depth = c->value_int;
1222                         break;
1223
1224                 case CFG_OID:
1225                         if(parse_oidm(c->fname, c->lineno, c->argc, c->argv)) return(1);
1226                         break;
1227
1228                 case CFG_OC:
1229                         if(parse_oc(c->fname, c->lineno, p, c->argv)) return(1);
1230                         break;
1231
1232                 case CFG_DIT:
1233                         if(parse_cr(c->fname, c->lineno, p, c->argv)) return(1);
1234                         break;
1235
1236                 case CFG_ATTR:
1237                         if(parse_at(c->fname, c->lineno, p, c->argv)) return(1);
1238                         break;
1239
1240                 case CFG_ATOPT:
1241                         ad_define_option(NULL, NULL, 0);
1242                         for(i = 1; i < c->argc; i++)
1243                                 if(ad_define_option(c->argv[i], c->fname, c->lineno))
1244                                         return(1);
1245                         break;
1246
1247                 case CFG_CHECK:
1248                         global_schemacheck = c->value_int;
1249                         if(!global_schemacheck) Debug(LDAP_DEBUG_ANY, "%s: "
1250                                 "schema checking disabled! your mileage may vary!\n",
1251                                 c->log, 0, 0);
1252                         break;
1253
1254                 case CFG_ACL:
1255                         parse_acl(c->be, c->fname, c->lineno, c->argc, c->argv);
1256                         break;
1257
1258                 case CFG_REPLOG:
1259                         if(SLAP_MONITOR(c->be)) {
1260                                 Debug(LDAP_DEBUG_ANY, "%s: "
1261                                         "\"replogfile\" should not be used "
1262                                         "inside monitor database\n",
1263                                         c->log, 0, 0);
1264                                 return(0);      /* FIXME: should this be an error? */
1265                         }
1266
1267                         c->be->be_replogfile = c->value_string;
1268                         break;
1269
1270                 case CFG_ROOTDSE:
1271                         if(read_root_dse_file(c->argv[1])) {
1272                                 Debug(LDAP_DEBUG_ANY, "%s: "
1273                                         "could not read \"rootDSE <filename>\" line\n",
1274                                         c->log, 0, 0);
1275                                 return(1);
1276                         }
1277                         {
1278                                 struct berval bv;
1279                                 ber_str2bv( c->argv[1], 0, 1, &bv );
1280                                 ber_bvarray_add( &cfn->c_dseFiles, &bv );
1281                         }
1282                         break;
1283
1284                 case CFG_LOGFILE: {
1285                                 FILE *logfile;
1286                                 if ( logfileName ) ch_free( logfileName );
1287                                 logfileName = c->value_string;
1288                                 logfile = fopen(logfileName, "w");
1289                                 if(logfile) lutil_debug_file(logfile);
1290                         } break;
1291
1292                 case CFG_LASTMOD:
1293                         if(SLAP_NOLASTMODCMD(c->be)) {
1294                                 Debug(LDAP_DEBUG_ANY, "%s: "
1295                                         "lastmod not available for %s databases\n",
1296                                         c->log, c->be->bd_info->bi_type, 0);
1297                                 return(1);
1298                         }
1299                         if(c->value_int)
1300                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_NOLASTMOD;
1301                         else
1302                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NOLASTMOD;
1303                         break;
1304
1305                 case CFG_SSTR_IF_MAX:
1306                         if (c->value_int < index_substr_if_minlen) {
1307                                 Debug(LDAP_DEBUG_ANY, "%s: "
1308                                         "invalid max value (%d)\n",
1309                                         c->log, c->value_int, 0 );
1310                                 return(1);
1311                         }
1312                         index_substr_if_maxlen = c->value_int;
1313                         break;
1314
1315                 case CFG_SSTR_IF_MIN:
1316                         if (c->value_int > index_substr_if_maxlen) {
1317                                 Debug(LDAP_DEBUG_ANY, "%s: "
1318                                         "invalid min value (%d)\n",
1319                                         c->log, c->value_int, 0 );
1320                                 return(1);
1321                         }
1322                         index_substr_if_minlen = c->value_int;
1323                         break;
1324
1325 #ifdef SLAPD_MODULES
1326                 case CFG_MODLOAD:
1327                         if(module_load(c->argv[1], c->argc - 2, (c->argc > 2) ? c->argv + 2 : NULL))
1328                                 return(1);
1329                         /* Record this load on the current path */
1330                         {
1331                                 struct berval bv;
1332                                 ber_str2bv(c->line, 0, 1, &bv);
1333                                 ber_bvarray_add( &cfn->c_modlast->mp_loads, &bv );
1334                         }
1335                         break;
1336
1337                 case CFG_MODPATH:
1338                         if(module_path(c->argv[1])) return(1);
1339                         /* Record which path was used with each module */
1340                         {
1341                                 ModPaths *mp;
1342
1343                                 if (!cfn->c_modpaths.mp_loads) {
1344                                         mp = &cfn->c_modpaths;
1345                                 } else {
1346                                         mp = ch_malloc( sizeof( ModPaths ));
1347                                         cfn->c_modlast->mp_next = mp;
1348                                 }
1349                                 ber_str2bv(c->argv[1], 0, 1, &mp->mp_path);
1350                                 mp->mp_next = NULL;
1351                                 mp->mp_loads = NULL;
1352                                 cfn->c_modlast = mp;
1353                         }
1354                         
1355                         break;
1356 #endif
1357
1358 #ifdef LDAP_SLAPI
1359                 case CFG_PLUGIN:
1360                         if(slapi_int_read_config(c->be, c->fname, c->lineno, c->argc, c->argv) != LDAP_SUCCESS)
1361                                 return(1);
1362                         slapi_plugins_used++;
1363                         break;
1364 #endif
1365
1366 #ifdef SLAP_AUTH_REWRITE
1367                 case CFG_REWRITE: {
1368                         struct berval bv;
1369                         if(slap_sasl_rewrite_config(c->fname, c->lineno, c->argc, c->argv))
1370                                 return(1);
1371                         ber_str2bv( c->line, 0, 1, &bv );
1372                         ber_bvarray_add( &authz_rewrites, &bv );
1373                         }
1374                         break;
1375 #endif
1376
1377
1378                 default:
1379                         Debug(LDAP_DEBUG_ANY, "%s: unknown CFG_TYPE %d"
1380                                 "(ignored)\n", c->log, c->type, 0);
1381
1382         }
1383         return(0);
1384 }
1385
1386
1387 static int
1388 config_fname(ConfigArgs *c) {
1389         if(c->emit && c->line) {
1390                 ConfigFile *cf = (ConfigFile *)c->line;
1391                 value_add_one( &c->rvalue_vals, &cf->c_file );
1392                 return 0;
1393         }
1394         return(1);
1395 }
1396
1397 static int
1398 config_search_base(ConfigArgs *c) {
1399         struct berval dn;
1400
1401         if(c->emit) {
1402                 int rc = 1;
1403                 if (!BER_BVISEMPTY(&default_search_base)) {
1404                         value_add_one(&c->rvalue_vals, &default_search_base);
1405                         value_add_one(&c->rvalue_nvals, &default_search_nbase);
1406                         rc = 0;
1407                 }
1408                 return rc;
1409         }
1410
1411         if(c->bi || c->be != frontendDB) {
1412                 Debug(LDAP_DEBUG_ANY, "%s: defaultSearchBase line must appear "
1413                         "prior to any backend or database definition\n",
1414                         c->log, 0, 0);
1415                 return(1);
1416         }
1417
1418         if(default_search_nbase.bv_len) {
1419                 Debug(LDAP_DEBUG_ANY, "%s: "
1420                         "default search base \"%s\" already defined "
1421                         "(discarding old)\n",
1422                         c->log, default_search_base.bv_val, 0);
1423                 free(default_search_base.bv_val);
1424                 free(default_search_nbase.bv_val);
1425         }
1426
1427         default_search_base = c->value_dn;
1428         default_search_nbase = c->value_ndn;
1429         return(0);
1430 }
1431
1432 static int
1433 config_passwd_hash(ConfigArgs *c) {
1434         int i;
1435         if (c->emit) {
1436                 struct berval bv;
1437                 for (i=0; default_passwd_hash && default_passwd_hash[i]; i++) {
1438                         ber_str2bv(default_passwd_hash[i], 0, 0, &bv);
1439                         value_add_one(&c->rvalue_vals, &bv);
1440                 }
1441                 return i ? 0 : 1;
1442         }
1443         if(default_passwd_hash) {
1444                 Debug(LDAP_DEBUG_ANY, "%s: "
1445                         "already set default password_hash\n",
1446                         c->log, 0, 0);
1447                 return(1);
1448         }
1449         for(i = 1; i < c->argc; i++) {
1450                 if(!lutil_passwd_scheme(c->argv[i])) {
1451                         Debug(LDAP_DEBUG_ANY, "%s: "
1452                                 "password scheme \"%s\" not available\n",
1453                                 c->log, c->argv[i], 0);
1454                 } else {
1455                         ldap_charray_add(&default_passwd_hash, c->argv[i]);
1456                 }
1457                 if(!default_passwd_hash) {
1458                         Debug(LDAP_DEBUG_ANY, "%s: no valid hashes found\n",
1459                                 c->log, 0, 0 );
1460                         return(1);
1461                 }
1462         }
1463         return(0);
1464 }
1465
1466 static int
1467 config_schema_dn(ConfigArgs *c) {
1468         struct berval dn;
1469         int rc;
1470         if ( c->emit ) {
1471                 value_add_one(&c->rvalue_vals, &c->be->be_schemadn);
1472                 value_add_one(&c->rvalue_nvals, &c->be->be_schemandn);
1473                 return 0;
1474         }
1475         c->be->be_schemadn = c->value_dn;
1476         c->be->be_schemandn = c->value_ndn;
1477         return(0);
1478 }
1479
1480 static int
1481 config_sizelimit(ConfigArgs *c) {
1482         int i, rc = 0;
1483         char *next;
1484         struct slap_limits_set *lim = &c->be->be_def_limit;
1485         if (c->emit) {
1486                 struct berval bv = BER_BVNULL;
1487                 limits_unparse_one( lim, SLAP_LIMIT_SIZE, &bv );
1488                 if ( !BER_BVISEMPTY( &bv ))
1489                         ber_bvarray_add( &c->rvalue_vals, &bv );
1490                 else
1491                         rc = 1;
1492                 return rc;
1493         }
1494         for(i = 1; i < c->argc; i++) {
1495                 if(!strncasecmp(c->argv[i], "size", 4)) {
1496                         rc = limits_parse_one(c->argv[i], lim);
1497                         if ( rc ) {
1498                                 Debug(LDAP_DEBUG_ANY, "%s: "
1499                                         "unable to parse value \"%s\" in \"sizelimit <limit>\" line\n",
1500                                         c->log, c->argv[i], 0);
1501                                 return(1);
1502                         }
1503                 } else {
1504                         if(!strcasecmp(c->argv[i], "unlimited")) {
1505                                 lim->lms_s_soft = -1;
1506                         } else {
1507                                 lim->lms_s_soft = strtol(c->argv[i], &next, 0);
1508                                 if(next == c->argv[i]) {
1509                                         Debug(LDAP_DEBUG_ANY, "%s: "
1510                                                 "unable to parse limit \"%s\" in \"sizelimit <limit>\" line\n",
1511                                                 c->log, c->argv[i], 0);
1512                                         return(1);
1513                                 } else if(next[0] != '\0') {
1514                                         Debug(LDAP_DEBUG_ANY, "%s: "
1515                                                 "trailing chars \"%s\" in \"sizelimit <limit>\" line (ignored)\n",
1516                                                 c->log, next, 0);
1517                                 }
1518                         }
1519                         lim->lms_s_hard = 0;
1520                 }
1521         }
1522         return(0);
1523 }
1524
1525 static int
1526 config_timelimit(ConfigArgs *c) {
1527         int i, rc = 0;
1528         char *next;
1529         struct slap_limits_set *lim = &c->be->be_def_limit;
1530         if (c->emit) {
1531                 struct berval bv = BER_BVNULL;
1532                 limits_unparse_one( lim, SLAP_LIMIT_TIME, &bv );
1533                 if ( !BER_BVISEMPTY( &bv ))
1534                         ber_bvarray_add( &c->rvalue_vals, &bv );
1535                 else
1536                         rc = 1;
1537                 return rc;
1538         }
1539         for(i = 1; i < c->argc; i++) {
1540                 if(!strncasecmp(c->argv[i], "time", 4)) {
1541                         rc = limits_parse_one(c->argv[i], lim);
1542                         if ( rc ) {
1543                                 Debug(LDAP_DEBUG_ANY, "%s: "
1544                                         "unable to parse value \"%s\" in \"timelimit <limit>\" line\n",
1545                                         c->log, c->argv[i], 0);
1546                                 return(1);
1547                         }
1548                 } else {
1549                         if(!strcasecmp(c->argv[i], "unlimited")) {
1550                                 lim->lms_t_soft = -1;
1551                         } else {
1552                                 lim->lms_t_soft = strtol(c->argv[i], &next, 0);
1553                                 if(next == c->argv[i]) {
1554                                         Debug(LDAP_DEBUG_ANY, "%s: "
1555                                                 "unable to parse limit \"%s\" in \"timelimit <limit>\" line\n",
1556                                                 c->log, c->argv[i], 0);
1557                                         return(1);
1558                                 } else if(next[0] != '\0') {
1559                                         Debug(LDAP_DEBUG_ANY, "%s: "
1560                                                 "trailing chars \"%s\" in \"timelimit <limit>\" line (ignored)\n",
1561                                                 c->log, next, 0);
1562                                 }
1563                         }
1564                         lim->lms_t_hard = 0;
1565                 }
1566         }
1567         return(0);
1568 }
1569
1570 static int
1571 config_overlay(ConfigArgs *c) {
1572         if (c->emit) {
1573                 return 1;
1574         }
1575         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1])) {
1576                 /* log error */
1577                 Debug(LDAP_DEBUG_ANY, "%s: (optional) %s overlay \"%s\" configuration failed (ignored)\n",
1578                         c->log, c->be == frontendDB ? "global " : "", c->argv[1][1]);
1579         } else if(overlay_config(c->be, c->argv[1])) {
1580                 return(1);
1581         }
1582         return(0);
1583 }
1584
1585 static int
1586 config_suffix(ConfigArgs *c) {
1587         Backend *tbe;
1588         struct berval pdn, ndn;
1589         int rc;
1590         if (c->emit) {
1591                 if ( !BER_BVISNULL( &c->be->be_suffix[0] )) {
1592                         value_add( &c->rvalue_vals, c->be->be_suffix );
1593                         value_add( &c->rvalue_nvals, c->be->be_nsuffix );
1594                         return 0;
1595                 } else {
1596                         return 1;
1597                 }
1598         }
1599 #ifdef SLAPD_MONITOR_DN
1600         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
1601                 Debug(LDAP_DEBUG_ANY, "%s: "
1602                         "\"%s\" is reserved for monitoring slapd\n",
1603                         c->log, SLAPD_MONITOR_DN, 0);
1604                 return(1);
1605         }
1606 #endif
1607
1608         pdn = c->value_dn;
1609         ndn = c->value_ndn;
1610         tbe = select_backend(&ndn, 0, 0);
1611         if(tbe == c->be) {
1612                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by this backend! (ignored)\n",
1613                         c->log, 0, 0);
1614                 free(pdn.bv_val);
1615                 free(ndn.bv_val);
1616         } else if(tbe) {
1617                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by a preceding backend \"%s\"\n",
1618                         c->log, tbe->be_suffix[0].bv_val, 0);
1619                 free(pdn.bv_val);
1620                 free(ndn.bv_val);
1621                 return(1);
1622         } else if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
1623                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
1624                         "base provided \"%s\" (assuming okay)\n",
1625                         c->log, default_search_base.bv_val, 0);
1626         }
1627         ber_bvarray_add(&c->be->be_suffix, &pdn);
1628         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
1629         return(0);
1630 }
1631
1632 static int
1633 config_rootdn(ConfigArgs *c) {
1634         if (c->emit) {
1635                 if ( !BER_BVISNULL( &c->be->be_rootdn )) {
1636                         value_add_one(&c->rvalue_vals, &c->be->be_rootdn);
1637                         value_add_one(&c->rvalue_nvals, &c->be->be_rootndn);
1638                         return 0;
1639                 } else {
1640                         return 1;
1641                 }
1642         }
1643         c->be->be_rootdn = c->value_dn;
1644         c->be->be_rootndn = c->value_ndn;
1645         return(0);
1646 }
1647
1648 static int
1649 config_rootpw(ConfigArgs *c) {
1650         Backend *tbe;
1651         if (c->emit) {
1652                 if (!BER_BVISEMPTY(&c->be->be_rootpw)) {
1653                         c->value_string=ch_strdup("*");
1654                         return 0;
1655                 }
1656                 return 1;
1657         }
1658
1659         tbe = select_backend(&c->be->be_rootndn, 0, 0);
1660         if(tbe != c->be) {
1661                 Debug(LDAP_DEBUG_ANY, "%s: "
1662                         "rootpw can only be set when rootdn is under suffix\n",
1663                         c->log, 0, 0);
1664                 return(1);
1665         }
1666         ber_str2bv(c->value_string, 0, 0, &c->be->be_rootpw);
1667         return(0);
1668 }
1669
1670 /* restrictops, allows, disallows, requires, loglevel */
1671
1672 struct verb_mask_list { char *word; int mask; };
1673
1674 int
1675 verb_to_mask(ConfigArgs *c, struct verb_mask_list *v, int word) {
1676         int j;
1677         for(j = 0; v[j].word; j++)
1678                 if(!strcasecmp(c->argv[word], v[j].word))
1679                         break;
1680         return(j);
1681 }
1682
1683 int
1684 verbs_to_mask(ConfigArgs *c, struct verb_mask_list *v, slap_mask_t *m) {
1685         int i, j;
1686         for(i = 1; i < c->argc; i++) {
1687                 j = verb_to_mask(c, v, i);
1688                 if(!v[j].word) return(1);
1689                 while (!v[j].mask) j--;
1690                 *m |= v[j].mask;
1691         }
1692         return(0);
1693 }
1694
1695 int
1696 mask_to_verbs(ConfigArgs *c, struct verb_mask_list *v, slap_mask_t m) {
1697         int i, j;
1698         struct berval bv;
1699
1700         if (!m) return 1;
1701         for (i=0; v[i].word; i++) {
1702                 if (!v[i].mask) continue;
1703                 if (( m & v[i].mask ) == v[i].mask ) {
1704                         ber_str2bv( v[i].word, 0, 0, &bv );
1705                         value_add_one( &c->rvalue_vals, &bv );
1706                 }
1707         }
1708         return 0;
1709 }
1710
1711 static int
1712 config_restrict(ConfigArgs *c) {
1713         slap_mask_t restrictops = 0;
1714         int i;
1715         struct verb_mask_list restrictable_ops[] = {
1716                 { "bind",               SLAP_RESTRICT_OP_BIND },
1717                 { "add",                SLAP_RESTRICT_OP_ADD },
1718                 { "modify",             SLAP_RESTRICT_OP_MODIFY },
1719                 { "rename",             SLAP_RESTRICT_OP_RENAME },
1720                 { "modrdn",             0 },
1721                 { "delete",             SLAP_RESTRICT_OP_DELETE },
1722                 { "search",             SLAP_RESTRICT_OP_SEARCH },
1723                 { "compare",    SLAP_RESTRICT_OP_COMPARE },
1724                 { "read",               SLAP_RESTRICT_OP_READS },
1725                 { "write",              SLAP_RESTRICT_OP_WRITES },
1726                 { "extended",   SLAP_RESTRICT_OP_EXTENDED },
1727                 { "extended=" LDAP_EXOP_START_TLS,              SLAP_RESTRICT_EXOP_START_TLS },
1728                 { "extended=" LDAP_EXOP_MODIFY_PASSWD,  SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
1729                 { "extended=" LDAP_EXOP_X_WHO_AM_I,             SLAP_RESTRICT_EXOP_WHOAMI },
1730                 { "extended=" LDAP_EXOP_X_CANCEL,               SLAP_RESTRICT_EXOP_CANCEL },
1731                 { NULL, 0 }
1732         };
1733
1734         if (c->emit) {
1735                 return mask_to_verbs( c, restrictable_ops, c->be->be_restrictops );
1736         }
1737         i = verbs_to_mask( c, restrictable_ops, &restrictops );
1738         if ( i ) {
1739                 Debug(LDAP_DEBUG_ANY, "%s: "
1740                         "unknown operation %s in \"restrict <features>\" line\n",
1741                         c->log, c->argv[i], 0);
1742                 return(1);
1743         }
1744         if ( restrictops & SLAP_RESTRICT_OP_EXTENDED )
1745                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
1746         c->be->be_restrictops |= restrictops;
1747         return(0);
1748 }
1749
1750 static int
1751 config_allows(ConfigArgs *c) {
1752         slap_mask_t allows = 0;
1753         int i;
1754         struct verb_mask_list allowable_ops[] = {
1755                 { "bind_v2",            SLAP_ALLOW_BIND_V2 },
1756                 { "bind_anon_cred",     SLAP_ALLOW_BIND_ANON_CRED },
1757                 { "bind_anon_dn",       SLAP_ALLOW_BIND_ANON_DN },
1758                 { "update_anon",        SLAP_ALLOW_UPDATE_ANON },
1759                 { NULL, 0 }
1760         };
1761         if (c->emit) {
1762                 return mask_to_verbs( c, allowable_ops, global_allows );
1763         }
1764         i = verbs_to_mask(c, allowable_ops, &allows);
1765         if ( i ) {
1766                 Debug(LDAP_DEBUG_ANY, "%s: "
1767                         "unknown feature %s in \"allow <features>\" line\n",
1768                         c->log, c->argv[i], 0);
1769                 return(1);
1770         }
1771         global_allows |= allows;
1772         return(0);
1773 }
1774
1775 static int
1776 config_disallows(ConfigArgs *c) {
1777         slap_mask_t disallows = 0;
1778         int i;
1779         struct verb_mask_list disallowable_ops[] = {
1780                 { "bind_anon",          SLAP_DISALLOW_BIND_ANON },
1781                 { "bind_simple",        SLAP_DISALLOW_BIND_SIMPLE },
1782                 { "bind_krb4",          SLAP_DISALLOW_BIND_KRBV4 },
1783                 { "tls_2_anon",         SLAP_DISALLOW_TLS_2_ANON },
1784                 { "tls_authc",          SLAP_DISALLOW_TLS_AUTHC },
1785                 { NULL, 0 }
1786         };
1787         if (c->emit) {
1788                 return mask_to_verbs( c, disallowable_ops, global_disallows );
1789         }
1790         i = verbs_to_mask(c, disallowable_ops, &disallows);
1791         if ( i ) {
1792                 Debug(LDAP_DEBUG_ANY, "%s: "
1793                         "unknown feature %s in \"disallow <features>\" line\n",
1794                         c->log, c->argv[i], 0);
1795                 return(1);
1796         }
1797         global_disallows |= disallows;
1798         return(0);
1799 }
1800
1801 static int
1802 config_requires(ConfigArgs *c) {
1803         slap_mask_t requires = 0;
1804         int i;
1805         struct verb_mask_list requires_ops[] = {
1806                 { "bind",               SLAP_REQUIRE_BIND },
1807                 { "LDAPv3",             SLAP_REQUIRE_LDAP_V3 },
1808                 { "authc",              SLAP_REQUIRE_AUTHC },
1809                 { "sasl",               SLAP_REQUIRE_SASL },
1810                 { "strong",             SLAP_REQUIRE_STRONG },
1811                 { NULL, 0 }
1812         };
1813         if (c->emit) {
1814                 return mask_to_verbs( c, requires_ops, c->be->be_requires );
1815         }
1816         i = verbs_to_mask(c, requires_ops, &requires);
1817         if ( i ) {
1818                 Debug(LDAP_DEBUG_ANY, "%s: "
1819                         "unknown feature %s in \"require <features>\" line\n",
1820                         c->log, c->argv[i], 0);
1821                 return(1);
1822         }
1823         c->be->be_requires = requires;
1824         return(0);
1825 }
1826
1827 static int
1828 config_loglevel(ConfigArgs *c) {
1829         int i;
1830         char *next;
1831         struct verb_mask_list loglevel_ops[] = {
1832                 { "Trace",      LDAP_DEBUG_TRACE },
1833                 { "Packets",    LDAP_DEBUG_PACKETS },
1834                 { "Args",       LDAP_DEBUG_ARGS },
1835                 { "Conns",      LDAP_DEBUG_CONNS },
1836                 { "BER",        LDAP_DEBUG_BER },
1837                 { "Filter",     LDAP_DEBUG_FILTER },
1838                 { "Config",     LDAP_DEBUG_CONFIG },
1839                 { "ACL",        LDAP_DEBUG_ACL },
1840                 { "Stats",      LDAP_DEBUG_STATS },
1841                 { "Stats2",     LDAP_DEBUG_STATS2 },
1842                 { "Shell",      LDAP_DEBUG_SHELL },
1843                 { "Parse",      LDAP_DEBUG_PARSE },
1844                 { "Cache",      LDAP_DEBUG_CACHE },
1845                 { "Index",      LDAP_DEBUG_INDEX },
1846                 { "Any",        -1 },
1847                 { NULL, 0 }
1848         };
1849
1850         if (c->emit) {
1851                 return mask_to_verbs( c, loglevel_ops, ldap_syslog );
1852         }
1853
1854         ldap_syslog = 0;
1855
1856         for( i=1; i < c->argc; i++ ) {
1857                 int     level;
1858
1859                 if ( isdigit( c->argv[i][0] ) ) {
1860                         level = strtol( c->argv[i], &next, 10 );
1861                         if ( next == NULL || next[0] != '\0' ) {
1862                                 Debug( LDAP_DEBUG_ANY,
1863                                         "%s: unable to parse level \"%s\" "
1864                                         "in \"loglevel <level> [...]\" line.\n",
1865                                         c->log, c->argv[i], 0);
1866                                 return( 1 );
1867                         }
1868                 } else {
1869                         int j = verb_to_mask(c, loglevel_ops, c->argv[i][0]);
1870                         if(!loglevel_ops[j].word) {
1871                                 Debug( LDAP_DEBUG_ANY,
1872                                         "%s: unknown level \"%s\" "
1873                                         "in \"loglevel <level> [...]\" line.\n",
1874                                         c->log, c->argv[i], 0);
1875                                 return( 1 );
1876                         }
1877                         level = loglevel_ops[j].mask;
1878                 }
1879                 ldap_syslog |= level;
1880         }
1881         return(0);
1882 }
1883
1884 static int
1885 config_syncrepl(ConfigArgs *c) {
1886         if (c->emit) {
1887                 if ( c->be->be_syncinfo ) {
1888                         struct berval bv;
1889                         syncrepl_unparse( c->be->be_syncinfo, &bv ); 
1890                         ber_bvarray_add( &c->rvalue_vals, &bv );
1891                         return 0;
1892                 }
1893                 return 1;
1894         }
1895         if(SLAP_SHADOW(c->be)) {
1896                 Debug(LDAP_DEBUG_ANY, "%s: "
1897                         "syncrepl: database already shadowed.\n",
1898                         c->log, 0, 0);
1899                 return(1);
1900         } else if(add_syncrepl(c->be, c->argv, c->argc)) {
1901                 return(1);
1902         }
1903         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);
1904         return(0);
1905 }
1906
1907 static int
1908 config_referral(ConfigArgs *c) {
1909         struct berval vals[2];
1910         if (c->emit) {
1911                 if ( default_referral ) {
1912                         value_add( &c->rvalue_vals, default_referral );
1913                         return 0;
1914                 } else {
1915                         return 1;
1916                 }
1917         }
1918         if(validate_global_referral(c->argv[1])) {
1919                 Debug(LDAP_DEBUG_ANY, "%s: "
1920                         "invalid URL (%s) in \"referral\" line.\n",
1921                         c->log, c->argv[1], 0);
1922                 return(1);
1923         }
1924
1925         ber_str2bv(c->argv[1], 0, 0, &vals[0]);
1926         vals[1].bv_val = NULL; vals[1].bv_len = 0;
1927         if(value_add(&default_referral, vals)) return(LDAP_OTHER);
1928         return(0);
1929 }
1930
1931 static struct {
1932         struct berval key;
1933         int off;
1934 } sec_keys[] = {
1935         { BER_BVC("ssf="), offsetof(slap_ssf_set_t, sss_ssf) },
1936         { BER_BVC("transport="), offsetof(slap_ssf_set_t, sss_transport) },
1937         { BER_BVC("tls="), offsetof(slap_ssf_set_t, sss_tls) },
1938         { BER_BVC("sasl="), offsetof(slap_ssf_set_t, sss_sasl) },
1939         { BER_BVC("update_ssf="), offsetof(slap_ssf_set_t, sss_update_ssf) },
1940         { BER_BVC("update_transport="), offsetof(slap_ssf_set_t, sss_update_transport) },
1941         { BER_BVC("update_tls="), offsetof(slap_ssf_set_t, sss_update_tls) },
1942         { BER_BVC("update_sasl="), offsetof(slap_ssf_set_t, sss_update_sasl) },
1943         { BER_BVC("simple_bind="), offsetof(slap_ssf_set_t, sss_simple_bind) },
1944         { BER_BVNULL, 0 }
1945 };
1946
1947 static int
1948 config_security(ConfigArgs *c) {
1949         slap_ssf_set_t *set = &c->be->be_ssf_set;
1950         char *next;
1951         int i, j;
1952         if (c->emit) {
1953                 char numbuf[32];
1954                 struct berval bv;
1955                 slap_ssf_t *tgt;
1956                 int rc = 1;
1957
1958                 for (i=0; !BER_BVISNULL( &sec_keys[i].key ); i++) {
1959                         tgt = (slap_ssf_t *)((char *)set + sec_keys[i].off);
1960                         if ( *tgt ) {
1961                                 rc = 0;
1962                                 bv.bv_len = sprintf( numbuf, "%u", *tgt );
1963                                 bv.bv_len += sec_keys[i].key.bv_len;
1964                                 bv.bv_val = ch_malloc( bv.bv_len + 1);
1965                                 next = lutil_strcopy( bv.bv_val, sec_keys[i].key.bv_val );
1966                                 strcpy( next, numbuf );
1967                                 ber_bvarray_add( &c->rvalue_vals, &bv );
1968                         }
1969                 }
1970                 return rc;
1971         }
1972         for(i = 1; i < c->argc; i++) {
1973                 slap_ssf_t *tgt = NULL;
1974                 char *src;
1975                 for ( j=0; !BER_BVISNULL( &sec_keys[j].key ); j++ ) {
1976                         if(!strncasecmp(c->argv[i], sec_keys[j].key.bv_val,
1977                                 sec_keys[j].key.bv_len)) {
1978                                 src = c->argv[i] + sec_keys[j].key.bv_len;
1979                                 tgt = (slap_ssf_t *)((char *)set + sec_keys[j].off);
1980                                 break;
1981                         }
1982                 }
1983                 if ( !tgt ) {
1984                         Debug(LDAP_DEBUG_ANY, "%s: "
1985                                 "unknown factor %s in \"security <factors>\" line\n",
1986                                 c->log, c->argv[i], 0);
1987                         return(1);
1988                 }
1989
1990                 *tgt = strtol(src, &next, 10);
1991                 if(next == NULL || next[0] != '\0' ) {
1992                         Debug(LDAP_DEBUG_ANY, "%s: "
1993                                 "unable to parse factor \"%s\" in \"security <factors>\" line\n",
1994                                 c->log, c->argv[i], 0);
1995                         return(1);
1996                 }
1997         }
1998         return(0);
1999 }
2000
2001 static struct verb_mask_list tlskey[] = {
2002         { "no",         SB_TLS_OFF },
2003         { "yes",                SB_TLS_ON },
2004         { "critical",   SB_TLS_CRITICAL }
2005 };
2006
2007 static struct verb_mask_list methkey[] = {
2008         { "simple",     LDAP_AUTH_SIMPLE },
2009 #ifdef HAVE_CYRUS_SASL
2010         { "sasl",       LDAP_AUTH_SASL },
2011 #endif
2012         { NULL, 0 }
2013 };
2014
2015 typedef struct cf_aux_table {
2016         struct berval key;
2017         int off;
2018         int quote;
2019         struct verb_mask_list *aux;
2020 } cf_aux_table;
2021
2022 static cf_aux_table bindkey[] = {
2023         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 0, tlskey },
2024         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 0, methkey },
2025         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 1, NULL },
2026         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 1, NULL },
2027         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 0, NULL },
2028         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 0, NULL },
2029         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 0, NULL },
2030         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 0, NULL },
2031         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 1, NULL },
2032         { BER_BVNULL, 0, 0, NULL }
2033 };
2034
2035 int bindconf_parse( char *word, slap_bindconf *bc ) {
2036         int i, rc = 0;
2037         char **cptr;
2038         cf_aux_table *tab;
2039
2040         for (tab = bindkey; !BER_BVISNULL(&tab->key); tab++) {
2041                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
2042                         cptr = (char **)((char *)bc + tab->off);
2043                         if ( tab->aux ) {
2044                                 int j;
2045                                 rc = 1;
2046                                 for (j=0; tab->aux[j].word; j++) {
2047                                         if (!strcasecmp(word+tab->key.bv_len, tab->aux[j].word)) {
2048                                                 int *ptr = (int *)cptr;
2049                                                 *ptr = tab->aux[j].mask;
2050                                                 rc = 0;
2051                                         }
2052                                 }
2053                                 if (rc ) {
2054                                         Debug(LDAP_DEBUG_ANY, "invalid bind config value %s\n",
2055                                                 word, 0, 0 );
2056                                 }
2057                                 return rc;
2058                         }
2059                         *cptr = ch_strdup(word+tab->key.bv_len);
2060                         return 0;
2061                 }
2062         }
2063         return rc;
2064 }
2065
2066 int bindconf_unparse( slap_bindconf *bc, struct berval *bv ) {
2067         char buf[BUFSIZ], *ptr;
2068         cf_aux_table *tab;
2069         char **cptr;
2070         struct berval tmp;
2071
2072         ptr = buf;
2073         for (tab = bindkey; !BER_BVISNULL(&tab->key); tab++) {
2074                 cptr = (char **)((char *)bc + tab->off);
2075                 if ( tab->aux ) {
2076                         int *ip = (int *)cptr, i;
2077                         for ( i=0; tab->aux[i].word; i++ ) {
2078                                 if ( *ip == tab->aux[i].mask ) {
2079                                         *ptr++ = ' ';
2080                                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
2081                                         ptr = lutil_strcopy( ptr, tab->aux[i].word );
2082                                         break;
2083                                 }
2084                         }
2085                 } else if ( *cptr ) {
2086                         *ptr++ = ' ';
2087                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
2088                         if ( tab->quote ) *ptr++ = '"';
2089                         ptr = lutil_strcopy( ptr, *cptr );
2090                         if ( tab->quote ) *ptr++ = '"';
2091                 }
2092         }
2093         tmp.bv_val = buf;
2094         tmp.bv_len = ptr - buf;
2095         ber_dupbv( bv, &tmp );
2096         return 0;
2097 }
2098
2099 void bindconf_free( slap_bindconf *bc ) {
2100         if ( bc->sb_binddn ) {
2101                 ch_free( bc->sb_binddn );
2102         }
2103         if ( bc->sb_cred ) {
2104                 ch_free( bc->sb_cred );
2105         }
2106         if ( bc->sb_saslmech ) {
2107                 ch_free( bc->sb_saslmech );
2108         }
2109         if ( bc->sb_secprops ) {
2110                 ch_free( bc->sb_secprops );
2111         }
2112         if ( bc->sb_realm ) {
2113                 ch_free( bc->sb_realm );
2114         }
2115         if ( bc->sb_authcId ) {
2116                 ch_free( bc->sb_authcId );
2117         }
2118         if ( bc->sb_authzId ) {
2119                 ch_free( bc->sb_authzId );
2120         }
2121 }
2122
2123 static char *
2124 anlist_unparse( AttributeName *an, char *ptr ) {
2125         int comma = 0;
2126
2127         for (; !BER_BVISNULL( &an->an_name ); an++) {
2128                 if ( comma ) *ptr++ = ',';
2129                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
2130                 comma = 1;
2131         }
2132         return ptr;
2133 }
2134
2135 static void
2136 replica_unparse( struct slap_replica_info *ri, int i, struct berval *bv )
2137 {
2138         int len;
2139         char *ptr;
2140         struct berval bc = {0};
2141         char numbuf[32];
2142
2143         len = sprintf(numbuf, "{%d}", i );
2144
2145         len += strlen( ri->ri_uri ) + STRLENOF("uri=");
2146         if ( ri->ri_nsuffix ) {
2147                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
2148                         len += ri->ri_nsuffix[i].bv_len + STRLENOF(" suffix=\"\"");
2149                 }
2150         }
2151         if ( ri->ri_attrs ) {
2152                 len += STRLENOF("attr");
2153                 if ( ri->ri_exclude ) len++;
2154                 for (i=0; !BER_BVISNULL( &ri->ri_attrs[i].an_name ); i++) {
2155                         len += 1 + ri->ri_attrs[i].an_name.bv_len;
2156                 }
2157         }
2158         bindconf_unparse( &ri->ri_bindconf, &bc );
2159         len += bc.bv_len;
2160
2161         bv->bv_val = ch_malloc(len + 1);
2162         bv->bv_len = len;
2163
2164         ptr = lutil_strcopy( bv->bv_val, numbuf );
2165         ptr = lutil_strcopy( ptr, "uri=" );
2166         ptr = lutil_strcopy( ptr, ri->ri_uri );
2167
2168         if ( ri->ri_nsuffix ) {
2169                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
2170                         ptr = lutil_strcopy( ptr, " suffix=\"" );
2171                         ptr = lutil_strcopy( ptr, ri->ri_nsuffix[i].bv_val );
2172                         *ptr++ = '"';
2173                 }
2174         }
2175         if ( ri->ri_attrs ) {
2176                 ptr = lutil_strcopy( ptr, "attr" );
2177                 if ( ri->ri_exclude ) *ptr++ = '!';
2178                 *ptr++ = '=';
2179                 ptr = anlist_unparse( ri->ri_attrs, ptr );
2180         }
2181         if ( bc.bv_val ) {
2182                 strcpy( ptr, bc.bv_val );
2183                 ch_free( bc.bv_val );
2184         }
2185 }
2186
2187 static int
2188 config_replica(ConfigArgs *c) {
2189         int i, nr = -1, len;
2190         char *replicahost, *replicauri;
2191         LDAPURLDesc *ludp;
2192
2193         if (c->emit) {
2194                 if (c->be->be_replica) {
2195                         struct berval bv;
2196                         for (i=0;c->be->be_replica[i]; i++) {
2197                                 replica_unparse( c->be->be_replica[i], i, &bv );
2198                                 ber_bvarray_add( &c->rvalue_vals, &bv );
2199                         }
2200                         return 0;
2201                 }
2202                 return 1;
2203         }
2204         if(SLAP_MONITOR(c->be)) {
2205                 Debug(LDAP_DEBUG_ANY, "%s: "
2206                         "\"replica\" should not be used inside monitor database\n",
2207                         c->log, 0, 0);
2208                 return(0);      /* FIXME: should this be an error? */
2209         }
2210
2211         for(i = 1; i < c->argc; i++) {
2212                 if(!strncasecmp(c->argv[i], "host=", STRLENOF("host="))) {
2213                         replicahost = c->argv[i] + STRLENOF("host=");
2214                         len = strlen( replicahost );
2215                         replicauri = ch_malloc( len + STRLENOF("ldap://") + 1 );
2216                         sprintf( replicauri, "ldap://%s", replicahost );
2217                         replicahost = replicauri + STRLENOF( "ldap://");
2218                         nr = add_replica_info(c->be, replicauri, replicahost);
2219                         break;
2220                 } else if(!strncasecmp(c->argv[i], "uri=", STRLENOF("uri="))) {
2221                         if(ldap_url_parse(c->argv[i] + STRLENOF("uri="), &ludp) != LDAP_SUCCESS) {
2222                                 Debug(LDAP_DEBUG_ANY, "%s: "
2223                                         "replica line contains invalid "
2224                                         "uri definition.\n", c->log, 0, 0);
2225                                 return(1);
2226                         }
2227                         if(!ludp->lud_host) {
2228                                 Debug(LDAP_DEBUG_ANY, "%s: "
2229                                         "replica line contains invalid "
2230                                         "uri definition - missing hostname.\n",
2231                                         c->log, 0, 0);
2232                                 return(1);
2233                         }
2234                         ldap_free_urldesc(ludp);
2235                         replicauri = c->argv[i] + STRLENOF("uri=");
2236                         replicauri = ch_strdup( replicauri );
2237                         replicahost = strchr( replicauri, '/' );
2238                         replicahost += 2;
2239                         nr = add_replica_info(c->be, replicauri, replicahost);
2240                         break;
2241                 }
2242         }
2243         if(i == c->argc) {
2244                 Debug(LDAP_DEBUG_ANY, "%s: "
2245                         "missing host or uri in \"replica\" line\n",
2246                         c->log, 0, 0);
2247                 return(1);
2248         } else if(nr == -1) {
2249                 Debug(LDAP_DEBUG_ANY, "%s: "
2250                         "unable to add replica \"%s\"\n",
2251                         c->log, replicauri, 0);
2252                 return(1);
2253         } else {
2254                 for(i = 1; i < c->argc; i++) {
2255                         if(!strncasecmp(c->argv[i], "suffix=", STRLENOF( "suffix="))) {
2256                                 switch(add_replica_suffix(c->be, nr, c->argv[i] + STRLENOF("suffix="))) {
2257                                         case 1:
2258                                                 Debug(LDAP_DEBUG_ANY, "%s: "
2259                                                 "suffix \"%s\" in \"replica\" line is not valid for backend (ignored)\n",
2260                                                 c->log, c->argv[i] + STRLENOF("suffix="), 0);
2261                                                 break;
2262                                         case 2:
2263                                                 Debug(LDAP_DEBUG_ANY, "%s: "
2264                                                 "unable to normalize suffix in \"replica\" line (ignored)\n",
2265                                                 c->log, 0, 0);
2266                                                 break;
2267                                 }
2268
2269                         } else if(!strncasecmp(c->argv[i], "attr", STRLENOF("attr"))) {
2270                                 int exclude = 0;
2271                                 char *arg = c->argv[i] + STRLENOF("attr");
2272                                 if(arg[0] == '!') {
2273                                         arg++;
2274                                         exclude = 1;
2275                                 }
2276                                 if(arg[0] != '=') {
2277                                         continue;
2278                                 }
2279                                 if(add_replica_attrs(c->be, nr, arg + 1, exclude)) {
2280                                         Debug(LDAP_DEBUG_ANY, "%s: "
2281                                                 "attribute \"%s\" in \"replica\" line is unknown\n",
2282                                                 c->log, arg + 1, 0);
2283                                         return(1);
2284                                 }
2285                         } else if ( bindconf_parse( c->argv[i],
2286                                         &c->be->be_replica[nr]->ri_bindconf ) ) {
2287                                 return(1);
2288                         }
2289                 }
2290         }
2291         return(0);
2292 }
2293
2294 static int
2295 config_updatedn(ConfigArgs *c) {
2296         struct berval dn;
2297         int rc;
2298         if (c->emit) {
2299                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
2300                         value_add_one(&c->rvalue_vals, &c->be->be_update_ndn);
2301                         value_add_one(&c->rvalue_nvals, &c->be->be_update_ndn);
2302                         return 0;
2303                 }
2304                 return 1;
2305         }
2306         if(SLAP_SHADOW(c->be)) {
2307                 Debug(LDAP_DEBUG_ANY, "%s: "
2308                         "updatedn: database already shadowed.\n",
2309                         c->log, 0, 0);
2310                 return(1);
2311         }
2312
2313         ber_str2bv(c->argv[1], 0, 0, &dn);
2314
2315         rc = dnNormalize(0, NULL, NULL, &dn, &c->be->be_update_ndn, NULL);
2316
2317         if(rc != LDAP_SUCCESS) {
2318                 Debug(LDAP_DEBUG_ANY, "%s: "
2319                         "updatedn DN is invalid: %d (%s)\n",
2320                         c->log, rc, ldap_err2string( rc ));
2321                 return(1);
2322         }
2323
2324         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
2325         return(0);
2326 }
2327
2328 static int
2329 config_updateref(ConfigArgs *c) {
2330         struct berval vals[2];
2331         if (c->emit) {
2332                 if ( c->be->be_update_refs ) {
2333                         value_add( &c->rvalue_vals, c->be->be_update_refs );
2334                         return 0;
2335                 } else {
2336                         return 1;
2337                 }
2338         }
2339         if(!SLAP_SHADOW(c->be)) {
2340                 Debug(LDAP_DEBUG_ANY, "%s: "
2341                         "updateref line must come after syncrepl or updatedn.\n",
2342                         c->log, 0, 0);
2343                 return(1);
2344         }
2345
2346         if(validate_global_referral(c->argv[1])) {
2347                 Debug(LDAP_DEBUG_ANY, "%s: "
2348                         "invalid URL (%s) in \"updateref\" line.\n",
2349                         c->log, c->argv[1], 0);
2350                 return(1);
2351         }
2352         ber_str2bv(c->argv[1], 0, 0, &vals[0]);
2353         vals[1].bv_val = NULL;
2354         if(value_add(&c->be->be_update_refs, vals)) return(LDAP_OTHER);
2355         return(0);
2356 }
2357
2358 static int
2359 config_include(ConfigArgs *c) {
2360         unsigned long savelineno = c->lineno;
2361         int rc;
2362         ConfigFile *cf;
2363         ConfigFile *cfsave = cfn;
2364         ConfigFile *cf2 = NULL;
2365         if (c->emit) {
2366                 return 1;
2367         }
2368         cf = ch_calloc( 1, sizeof(ConfigFile));
2369         if ( cfn->c_kids ) {
2370                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
2371                 cf2->c_sibs = cf;
2372         } else {
2373                 cfn->c_kids = cf;
2374         }
2375         cfn = cf;
2376         rc = read_config_file(c->argv[1], c->depth + 1, c);
2377         c->lineno = savelineno - 1;
2378         cfn = cfsave;
2379         if ( rc ) {
2380                 if ( cf2 ) cf2->c_sibs = NULL;
2381                 else cfn->c_kids = NULL;
2382                 ch_free( cf );
2383         }
2384         return(rc);
2385 }
2386
2387 #ifdef HAVE_TLS
2388 static int
2389 config_tls_option(ConfigArgs *c) {
2390         int flag;
2391         switch(c->type) {
2392         case CFG_TLS_RAND:              flag = LDAP_OPT_X_TLS_RANDOM_FILE;      break;
2393         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
2394         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
2395         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
2396         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
2397         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
2398         default:                Debug(LDAP_DEBUG_ANY, "%s: "
2399                                         "unknown tls_option <%x>\n",
2400                                         c->log, c->type, 0);
2401         }
2402         if (c->emit) {
2403                 return ldap_pvt_tls_get_option( NULL, flag, &c->value_string );
2404         }
2405         ch_free(c->value_string);
2406         return(ldap_pvt_tls_set_option(NULL, flag, c->argv[1]));
2407 }
2408
2409 /* FIXME: this ought to be provided by libldap */
2410 static int
2411 config_tls_config(ConfigArgs *c) {
2412         int i, flag;
2413         struct verb_mask_list crlkeys[] = {
2414                 { "none",       LDAP_OPT_X_TLS_CRL_NONE },
2415                 { "peer",       LDAP_OPT_X_TLS_CRL_PEER },
2416                 { "all",        LDAP_OPT_X_TLS_CRL_ALL },
2417                 { NULL, 0 }
2418         };
2419         struct verb_mask_list vfykeys[] = {
2420                 { "never",      LDAP_OPT_X_TLS_NEVER },
2421                 { "demand",     LDAP_OPT_X_TLS_DEMAND },
2422                 { "try",        LDAP_OPT_X_TLS_TRY },
2423                 { "hard",       LDAP_OPT_X_TLS_HARD },
2424                 { NULL, 0 }
2425         }, *keys;
2426         switch(c->type) {
2427 #ifdef HAVE_OPENSSL_CRL
2428         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK; keys = crlkeys;
2429                 break;
2430 #endif
2431         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT; keys = vfykeys;
2432                 break;
2433         default:                Debug(LDAP_DEBUG_ANY, "%s: "
2434                                         "unknown tls_option <%x>\n",
2435                                         c->log, c->type, 0);
2436         }
2437         if (c->emit) {
2438                 ldap_pvt_tls_get_option( NULL, flag, &c->value_int );
2439                 for (i=0; keys[i].word; i++) {
2440                         if (keys[i].mask == c->value_int) {
2441                                 c->value_string = ch_strdup( keys[i].word );
2442                                 return 0;
2443                         }
2444                 }
2445                 return 1;
2446         }
2447         ch_free( c->value_string );
2448         if(isdigit((unsigned char)c->argv[1][0])) {
2449                 i = atoi(c->argv[1]);
2450                 return(ldap_pvt_tls_set_option(NULL, flag, &i));
2451         } else {
2452                 return(ldap_int_tls_config(NULL, flag, c->argv[1]));
2453         }
2454 }
2455 #endif
2456
2457 /* -------------------------------------- */
2458
2459
2460 static char *
2461 strtok_quote( char *line, char *sep )
2462 {
2463         int             inquote;
2464         char            *tmp;
2465         static char     *next;
2466
2467         strtok_quote_ptr = NULL;
2468         if ( line != NULL ) {
2469                 next = line;
2470         }
2471         while ( *next && strchr( sep, *next ) ) {
2472                 next++;
2473         }
2474
2475         if ( *next == '\0' ) {
2476                 next = NULL;
2477                 return( NULL );
2478         }
2479         tmp = next;
2480
2481         for ( inquote = 0; *next; ) {
2482                 switch ( *next ) {
2483                 case '"':
2484                         if ( inquote ) {
2485                                 inquote = 0;
2486                         } else {
2487                                 inquote = 1;
2488                         }
2489                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
2490                         break;
2491
2492                 case '\\':
2493                         if ( next[1] )
2494                                 AC_MEMCPY( next,
2495                                             next + 1, strlen( next + 1 ) + 1 );
2496                         next++;         /* dont parse the escaped character */
2497                         break;
2498
2499                 default:
2500                         if ( ! inquote ) {
2501                                 if ( strchr( sep, *next ) != NULL ) {
2502                                         strtok_quote_ptr = next;
2503                                         *next++ = '\0';
2504                                         return( tmp );
2505                                 }
2506                         }
2507                         next++;
2508                         break;
2509                 }
2510         }
2511
2512         return( tmp );
2513 }
2514
2515 static char     buf[BUFSIZ];
2516 static char     *line;
2517 static size_t lmax, lcur;
2518
2519 #define CATLINE( buf ) \
2520         do { \
2521                 size_t len = strlen( buf ); \
2522                 while ( lcur + len + 1 > lmax ) { \
2523                         lmax += BUFSIZ; \
2524                         line = (char *) ch_realloc( line, lmax ); \
2525                 } \
2526                 strcpy( line + lcur, buf ); \
2527                 lcur += len; \
2528         } while( 0 )
2529
2530 static void
2531 fp_getline_init(ConfigArgs *c) {
2532         c->lineno = -1;
2533         buf[0] = '\0';
2534 }
2535
2536 static int
2537 fp_getline( FILE *fp, ConfigArgs *c )
2538 {
2539         char    *p;
2540
2541         lcur = 0;
2542         CATLINE(buf);
2543         c->lineno++;
2544
2545         /* avoid stack of bufs */
2546         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
2547                 buf[0] = '\0';
2548                 c->line = line;
2549                 return(1);
2550         }
2551
2552         while ( fgets( buf, sizeof( buf ), fp ) ) {
2553                 p = strchr( buf, '\n' );
2554                 if ( p ) {
2555                         if ( p > buf && p[-1] == '\r' ) {
2556                                 --p;
2557                         }
2558                         *p = '\0';
2559                 }
2560                 /* XXX ugly */
2561                 c->line = line;
2562                 if ( line[0]
2563                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
2564                                 && p[-1] != '\\' )
2565                 {
2566                         p[0] = '\0';
2567                         lcur--;
2568                         
2569                 } else {
2570                         if ( !isspace( (unsigned char)buf[0] ) ) {
2571                                 return(1);
2572                         }
2573                         buf[0] = ' ';
2574                 }
2575                 CATLINE(buf);
2576                 c->lineno++;
2577         }
2578
2579         buf[0] = '\0';
2580         c->line = line;
2581         return(line[0] ? 1 : 0);
2582 }
2583
2584 static int
2585 fp_parse_line(ConfigArgs *c)
2586 {
2587         char *token;
2588         char *tline = ch_strdup(c->line);
2589         char *hide[] = { "rootpw", "replica", "bindpw", "pseudorootpw", "dbpasswd", '\0' };
2590         int i;
2591
2592         c->argc = 0;
2593         token = strtok_quote(tline, " \t");
2594
2595         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
2596         if(strtok_quote_ptr) *strtok_quote_ptr = ' ';
2597         Debug(LDAP_DEBUG_CONFIG, "line %lu (%s%s)\n", c->lineno, hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
2598         if(strtok_quote_ptr) *strtok_quote_ptr = '\0';
2599
2600         for(; token; token = strtok_quote(NULL, " \t")) {
2601                 if(c->argc == c->argv_size - 1) {
2602                         char **tmp;
2603                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
2604                         if(!tmp) {
2605                                 Debug(LDAP_DEBUG_ANY, "line %lu: out of memory\n", c->lineno, 0, 0);
2606                                 return -1;
2607                         }
2608                         c->argv = tmp;
2609                         c->argv_size += ARGS_STEP;
2610                 }
2611                 c->argv[c->argc++] = token;
2612         }
2613         c->argv[c->argc] = NULL;
2614         return(0);
2615 }
2616
2617 void
2618 config_destroy( )
2619 {
2620         ucdata_unload( UCDATA_ALL );
2621         if ( frontendDB ) {
2622                 /* NOTE: in case of early exit, frontendDB can be NULL */
2623                 if ( frontendDB->be_schemandn.bv_val )
2624                         free( frontendDB->be_schemandn.bv_val );
2625                 if ( frontendDB->be_schemadn.bv_val )
2626                         free( frontendDB->be_schemadn.bv_val );
2627                 if ( frontendDB->be_acl )
2628                         acl_destroy( frontendDB->be_acl, NULL );
2629         }
2630         free( line );
2631         if ( slapd_args_file )
2632                 free ( slapd_args_file );
2633         if ( slapd_pid_file )
2634                 free ( slapd_pid_file );
2635         if ( default_passwd_hash )
2636                 ldap_charray_free( default_passwd_hash );
2637 }
2638
2639 static int
2640 add_syncrepl(
2641         Backend *be,
2642         char    **cargv,
2643         int     cargc
2644 )
2645 {
2646         syncinfo_t *si;
2647         int     rc = 0;
2648
2649         si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
2650
2651         if ( si == NULL ) {
2652                 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
2653                 return 1;
2654         }
2655
2656         si->si_bindconf.sb_tls = SB_TLS_OFF;
2657         si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
2658         si->si_schemachecking = 0;
2659         ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
2660                 &si->si_filterstr );
2661         si->si_base.bv_val = NULL;
2662         si->si_scope = LDAP_SCOPE_SUBTREE;
2663         si->si_attrsonly = 0;
2664         si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
2665         si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
2666         si->si_attrs = NULL;
2667         si->si_allattrs = 0;
2668         si->si_allopattrs = 0;
2669         si->si_exattrs = NULL;
2670         si->si_type = LDAP_SYNC_REFRESH_ONLY;
2671         si->si_interval = 86400;
2672         si->si_retryinterval = NULL;
2673         si->si_retrynum_init = NULL;
2674         si->si_retrynum = NULL;
2675         si->si_manageDSAit = 0;
2676         si->si_tlimit = 0;
2677         si->si_slimit = 0;
2678
2679         si->si_presentlist = NULL;
2680         LDAP_LIST_INIT( &si->si_nonpresentlist );
2681         ldap_pvt_thread_mutex_init( &si->si_mutex );
2682
2683         rc = parse_syncrepl_line( cargv, cargc, si );
2684
2685         if ( rc < 0 ) {
2686                 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
2687                 syncinfo_free( si );    
2688                 return 1;
2689         } else {
2690                 Debug( LDAP_DEBUG_CONFIG,
2691                         "Config: ** successfully added syncrepl \"%s\"\n",
2692                         BER_BVISNULL( &si->si_provideruri ) ?
2693                         "(null)" : si->si_provideruri.bv_val, 0, 0 );
2694                 if ( !si->si_schemachecking ) {
2695                         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
2696                 }
2697                 si->si_be = be;
2698                 be->be_syncinfo = si;
2699                 return 0;
2700         }
2701 }
2702
2703 /* NOTE: used & documented in slapd.conf(5) */
2704 #define IDSTR                   "rid"
2705 #define PROVIDERSTR             "provider"
2706 #define TYPESTR                 "type"
2707 #define INTERVALSTR             "interval"
2708 #define SEARCHBASESTR           "searchbase"
2709 #define FILTERSTR               "filter"
2710 #define SCOPESTR                "scope"
2711 #define ATTRSSTR                "attrs"
2712 #define ATTRSONLYSTR            "attrsonly"
2713 #define SLIMITSTR               "sizelimit"
2714 #define TLIMITSTR               "timelimit"
2715 #define SCHEMASTR               "schemachecking"
2716
2717 /* FIXME: undocumented */
2718 #define OLDAUTHCSTR             "bindprincipal"
2719 #define EXATTRSSTR              "exattrs"
2720 #define RETRYSTR                "retry"
2721
2722 /* FIXME: unused */
2723 #define LASTMODSTR              "lastmod"
2724 #define LMGENSTR                "gen"
2725 #define LMNOSTR                 "no"
2726 #define LMREQSTR                "req"
2727 #define SRVTABSTR               "srvtab"
2728 #define SUFFIXSTR               "suffix"
2729 #define MANAGEDSAITSTR          "manageDSAit"
2730
2731 /* mandatory */
2732 #define GOT_ID                  0x0001
2733 #define GOT_PROVIDER            0x0002
2734
2735 /* check */
2736 #define GOT_ALL                 (GOT_ID|GOT_PROVIDER)
2737
2738 static struct {
2739         struct berval key;
2740         int val;
2741 } scopes[] = {
2742         { BER_BVC("base"), LDAP_SCOPE_BASE },
2743         { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2744 #ifdef LDAP_SCOPE_SUBORDINATE
2745         { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2746         { BER_BVC("subordinate"), 0 },
2747 #endif
2748         { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2749         { BER_BVNULL, 0 }
2750 };
2751
2752 static int
2753 parse_syncrepl_line(
2754         char            **cargv,
2755         int             cargc,
2756         syncinfo_t      *si
2757 )
2758 {
2759         int     gots = 0;
2760         int     i;
2761         char    *val;
2762
2763         for ( i = 1; i < cargc; i++ ) {
2764                 if ( !strncasecmp( cargv[ i ], IDSTR "=",
2765                                         STRLENOF( IDSTR "=" ) ) )
2766                 {
2767                         int tmp;
2768                         /* '\0' string terminator accounts for '=' */
2769                         val = cargv[ i ] + STRLENOF( IDSTR "=" );
2770                         tmp= atoi( val );
2771                         if ( tmp >= 1000 || tmp < 0 ) {
2772                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2773                                          "syncrepl id %d is out of range [0..999]\n", tmp );
2774                                 return -1;
2775                         }
2776                         si->si_rid = tmp;
2777                         gots |= GOT_ID;
2778                 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
2779                                         STRLENOF( PROVIDERSTR "=" ) ) )
2780                 {
2781                         val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
2782                         ber_str2bv( val, 0, 1, &si->si_provideruri );
2783                         gots |= GOT_PROVIDER;
2784                 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
2785                                         STRLENOF( SCHEMASTR "=" ) ) )
2786                 {
2787                         val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
2788                         if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2789                                 si->si_schemachecking = 1;
2790                         } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2791                                 si->si_schemachecking = 0;
2792                         } else {
2793                                 si->si_schemachecking = 1;
2794                         }
2795                 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
2796                                         STRLENOF( FILTERSTR "=" ) ) )
2797                 {
2798                         val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
2799                         ber_str2bv( val, 0, 1, &si->si_filterstr );
2800                 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
2801                                         STRLENOF( SEARCHBASESTR "=" ) ) )
2802                 {
2803                         struct berval   bv;
2804                         int             rc;
2805
2806                         val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2807                         if ( si->si_base.bv_val ) {
2808                                 ch_free( si->si_base.bv_val );
2809                         }
2810                         ber_str2bv( val, 0, 0, &bv );
2811                         rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2812                         if ( rc != LDAP_SUCCESS ) {
2813                                 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
2814                                         val, rc, ldap_err2string( rc ) );
2815                                 return -1;
2816                         }
2817                 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
2818                                         STRLENOF( SCOPESTR "=" ) ) )
2819                 {
2820                         int j;
2821                         val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
2822                         for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2823                                 if (!strncasecmp( val, scopes[j].key.bv_val,
2824                                         scopes[j].key.bv_len )) {
2825                                         while (!scopes[j].val) j--;
2826                                         si->si_scope = scopes[j].val;
2827                                         break;
2828                                 }
2829                         }
2830                         if ( BER_BVISNULL(&scopes[j].key) ) {
2831                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2832                                         "unknown scope \"%s\"\n", val);
2833                                 return -1;
2834                         }
2835                 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
2836                                         STRLENOF( ATTRSONLYSTR "=" ) ) )
2837                 {
2838                         si->si_attrsonly = 1;
2839                 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
2840                                         STRLENOF( ATTRSSTR "=" ) ) )
2841                 {
2842                         val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
2843                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2844                                 char *attr_fname;
2845                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2846                                 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2847                                 if ( si->si_anlist == NULL ) {
2848                                         ch_free( attr_fname );
2849                                         return -1;
2850                                 }
2851                                 si->si_anfile = attr_fname;
2852                         } else {
2853                                 char *str, *s, *next;
2854                                 char delimstr[] = " ,\t";
2855                                 str = ch_strdup( val );
2856                                 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2857                                                 s != NULL;
2858                                                 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2859                                 {
2860                                         if ( strlen(s) == 1 && *s == '*' ) {
2861                                                 si->si_allattrs = 1;
2862                                                 *(val + ( s - str )) = delimstr[0];
2863                                         }
2864                                         if ( strlen(s) == 1 && *s == '+' ) {
2865                                                 si->si_allopattrs = 1;
2866                                                 *(val + ( s - str )) = delimstr[0];
2867                                         }
2868                                 }
2869                                 ch_free( str );
2870                                 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2871                                 if ( si->si_anlist == NULL ) {
2872                                         return -1;
2873                                 }
2874                         }
2875                 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
2876                                         STRLENOF( EXATTRSSTR "=" ) ) )
2877                 {
2878                         val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
2879                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2880                                 char *attr_fname;
2881                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2882                                 si->si_exanlist = file2anlist(
2883                                                                         si->si_exanlist, attr_fname, " ,\t" );
2884                                 if ( si->si_exanlist == NULL ) {
2885                                         ch_free( attr_fname );
2886                                         return -1;
2887                                 }
2888                                 ch_free( attr_fname );
2889                         } else {
2890                                 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2891                                 if ( si->si_exanlist == NULL ) {
2892                                         return -1;
2893                                 }
2894                         }
2895                 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
2896                                         STRLENOF( TYPESTR "=" ) ) )
2897                 {
2898                         val = cargv[ i ] + STRLENOF( TYPESTR "=" );
2899                         if ( !strncasecmp( val, "refreshOnly",
2900                                                 STRLENOF("refreshOnly") ))
2901                         {
2902                                 si->si_type = LDAP_SYNC_REFRESH_ONLY;
2903                         } else if ( !strncasecmp( val, "refreshAndPersist",
2904                                                 STRLENOF("refreshAndPersist") ))
2905                         {
2906                                 si->si_type = LDAP_SYNC_REFRESH_AND_PERSIST;
2907                                 si->si_interval = 60;
2908                         } else {
2909                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2910                                         "unknown sync type \"%s\"\n", val);
2911                                 return -1;
2912                         }
2913                 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2914                                         STRLENOF( INTERVALSTR "=" ) ) )
2915                 {
2916                         val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2917                         if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2918                                 si->si_interval = 0;
2919                         } else {
2920                                 char *hstr;
2921                                 char *mstr;
2922                                 char *dstr;
2923                                 char *sstr;
2924                                 int dd, hh, mm, ss;
2925                                 dstr = val;
2926                                 hstr = strchr( dstr, ':' );
2927                                 if ( hstr == NULL ) {
2928                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2929                                                 "invalid interval \"%s\"\n", val );
2930                                         return -1;
2931                                 }
2932                                 *hstr++ = '\0';
2933                                 mstr = strchr( hstr, ':' );
2934                                 if ( mstr == NULL ) {
2935                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2936                                                 "invalid interval \"%s\"\n", val );
2937                                         return -1;
2938                                 }
2939                                 *mstr++ = '\0';
2940                                 sstr = strchr( mstr, ':' );
2941                                 if ( sstr == NULL ) {
2942                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2943                                                 "invalid interval \"%s\"\n", val );
2944                                         return -1;
2945                                 }
2946                                 *sstr++ = '\0';
2947
2948                                 dd = atoi( dstr );
2949                                 hh = atoi( hstr );
2950                                 mm = atoi( mstr );
2951                                 ss = atoi( sstr );
2952                                 if (( hh > 24 ) || ( hh < 0 ) ||
2953                                         ( mm > 60 ) || ( mm < 0 ) ||
2954                                         ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2955                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2956                                                 "invalid interval \"%s\"\n", val );
2957                                         return -1;
2958                                 }
2959                                 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2960                         }
2961                         if ( si->si_interval < 0 ) {
2962                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2963                                         "invalid interval \"%ld\"\n",
2964                                         (long) si->si_interval);
2965                                 return -1;
2966                         }
2967                 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2968                                         STRLENOF( RETRYSTR "=" ) ) )
2969                 {
2970                         char **retry_list;
2971                         int j, k, n;
2972
2973                         val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2974                         retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2975                         retry_list[0] = NULL;
2976
2977                         slap_str2clist( &retry_list, val, " ,\t" );
2978
2979                         for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2980                         n = k / 2;
2981                         if ( k % 2 ) {
2982                                 fprintf( stderr,
2983                                                 "Error: incomplete syncrepl retry list\n" );
2984                                 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2985                                         ch_free( retry_list[k] );
2986                                 }
2987                                 ch_free( retry_list );
2988                                 exit( EXIT_FAILURE );
2989                         }
2990                         si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2991                         si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2992                         si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2993                         for ( j = 0; j < n; j++ ) {
2994                                 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2995                                 if ( *retry_list[j*2+1] == '+' ) {
2996                                         si->si_retrynum_init[j] = -1;
2997                                         si->si_retrynum[j] = -1;
2998                                         j++;
2999                                         break;
3000                                 } else {
3001                                         si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
3002                                         si->si_retrynum[j] = atoi( retry_list[j*2+1] );
3003                                 }
3004                         }
3005                         si->si_retrynum_init[j] = -2;
3006                         si->si_retrynum[j] = -2;
3007                         si->si_retryinterval[j] = 0;
3008                         
3009                         for ( k = 0; retry_list && retry_list[k]; k++ ) {
3010                                 ch_free( retry_list[k] );
3011                         }
3012                         ch_free( retry_list );
3013                 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
3014                                         STRLENOF( MANAGEDSAITSTR "=" ) ) )
3015                 {
3016                         val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3017                         si->si_manageDSAit = atoi( val );
3018                 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
3019                                         STRLENOF( SLIMITSTR "=") ) )
3020                 {
3021                         val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
3022                         si->si_slimit = atoi( val );
3023                 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
3024                                         STRLENOF( TLIMITSTR "=" ) ) )
3025                 {
3026                         val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
3027                         si->si_tlimit = atoi( val );
3028                 } else if ( bindconf_parse( cargv[i], &si->si_bindconf )) {
3029                         fprintf( stderr, "Error: parse_syncrepl_line: "
3030                                 "unknown keyword \"%s\"\n", cargv[ i ] );
3031                         return -1;
3032                 }
3033         }
3034
3035         if ( gots != GOT_ALL ) {
3036                 fprintf( stderr,
3037                         "Error: Malformed \"syncrepl\" line in slapd config file" );
3038                 return -1;
3039         }
3040
3041         return 0;
3042 }
3043
3044
3045
3046 static void
3047 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3048 {
3049         struct berval bc;
3050         char buf[BUFSIZ*2], *ptr;
3051         int i, len;
3052
3053         bindconf_unparse( &si->si_bindconf, &bc );
3054         ptr = buf;
3055         ptr += sprintf( ptr, IDSTR "=%03d " PROVIDERSTR "=%s",
3056                 si->si_rid, si->si_provideruri.bv_val );
3057         if ( !BER_BVISNULL( &bc )) {
3058                 ptr = lutil_strcopy( ptr, bc.bv_val );
3059                 free( bc.bv_val );
3060         }
3061         if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3062                 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3063                 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3064                 *ptr++ = '"';
3065         }
3066         if ( !BER_BVISNULL( &si->si_base )) {
3067                 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3068                 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3069                 *ptr++ = '"';
3070         }
3071         for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3072                 if ( si->si_scope == scopes[i].val ) {
3073                         ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3074                         ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3075                         break;
3076                 }
3077         }
3078         if ( si->si_attrsonly ) {
3079                 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR "=yes" );
3080         }
3081         if ( si->si_anfile ) {
3082                 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
3083                 ptr = lutil_strcopy( ptr, si->si_anfile );
3084         } else if ( si->si_allattrs || si->si_allopattrs ||
3085                 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
3086                 char *old;
3087                 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3088                 old = ptr;
3089                 ptr = anlist_unparse( si->si_anlist, ptr );
3090                 if ( si->si_allattrs ) {
3091                         if ( old != ptr ) *ptr++ = ',';
3092                         *ptr++ = '*';
3093                 }
3094                 if ( si->si_allopattrs ) {
3095                         if ( old != ptr ) *ptr++ = ',';
3096                         *ptr++ = '+';
3097                 }
3098                 *ptr++ = '"';
3099         }
3100         if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3101                 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3102                 ptr = anlist_unparse( si->si_exanlist, ptr );
3103         }
3104         ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3105         ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3106         
3107         ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3108         ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3109                 "refreshAndPersist" : "refreshOnly" );
3110
3111         if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3112                 int dd, hh, mm, ss;
3113
3114                 dd = si->si_interval;
3115                 ss = dd % 60;
3116                 dd /= 60;
3117                 mm = dd % 60;
3118                 dd /= 60;
3119                 hh = dd % 24;
3120                 dd /= 24;
3121                 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3122                 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3123         } else if ( si->si_retryinterval ) {
3124                 int space=0;
3125                 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3126                 for (i=0; si->si_retryinterval[i]; i++) {
3127                         if ( space ) *ptr++ = ' ';
3128                         space = 1;
3129                         ptr += sprintf( ptr, "%d", si->si_retryinterval[i] );
3130                         if ( si->si_retrynum_init[i] == -1 )
3131                                 *ptr++ = '+';
3132                         else
3133                                 ptr += sprintf( ptr, "%d", si->si_retrynum_init );
3134                 }
3135                 *ptr++ = '"';
3136         }
3137
3138 #if 0 /* FIXME: unused in syncrepl.c, should remove it */
3139         ptr = lutil_strcopy( ptr, " " MANAGEDSAITSTR "=" );
3140         ptr += sprintf( ptr, "%d", si->si_manageDSAit );
3141 #endif
3142
3143         if ( si->si_slimit ) {
3144                 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3145                 ptr += sprintf( ptr, "%d", si->si_slimit );
3146         }
3147
3148         if ( si->si_tlimit ) {
3149                 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3150                 ptr += sprintf( ptr, "%d", si->si_tlimit );
3151         }
3152         bc.bv_len = ptr - buf;
3153         bc.bv_val = buf;
3154         ber_dupbv( bv, &bc );
3155 }
3156
3157 char **
3158 slap_str2clist( char ***out, char *in, const char *brkstr )
3159 {
3160         char    *str;
3161         char    *s;
3162         char    *lasts;
3163         int     i, j;
3164         char    **new;
3165
3166         /* find last element in list */
3167         for (i = 0; *out && (*out)[i]; i++);
3168
3169         /* protect the input string from strtok */
3170         str = ch_strdup( in );
3171
3172         if ( *str == '\0' ) {
3173                 free( str );
3174                 return( *out );
3175         }
3176
3177         /* Count words in string */
3178         j=1;
3179         for ( s = str; *s; s++ ) {
3180                 if ( strchr( brkstr, *s ) != NULL ) {
3181                         j++;
3182                 }
3183         }
3184
3185         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
3186         new = *out + i;
3187         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
3188                 s != NULL;
3189                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
3190         {
3191                 *new = ch_strdup( s );
3192                 new++;
3193         }
3194
3195         *new = NULL;
3196         free( str );
3197         return( *out );
3198 }