]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
5b93d2ef822b45f4e39b891b8db895d207b43832
[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 #ifdef LDAP_SLAPI
80 int slapi_plugins_used = 0;
81 #endif
82
83 static int fp_getline(FILE *fp, ConfigArgs *c);
84 static void fp_getline_init(ConfigArgs *c);
85 static int fp_parse_line(ConfigArgs *c);
86
87 static char     *strtok_quote(char *line, char *sep);
88 #if 0
89 static int load_ucdata(char *path);
90 #endif
91
92 int read_config_file(const char *fname, int depth, ConfigArgs *cf);
93
94 static int add_syncrepl LDAP_P(( Backend *, char **, int ));
95 static int parse_syncrepl_line LDAP_P(( char **, int, syncinfo_t *));
96
97 int config_generic(ConfigArgs *c);
98 int config_search_base(ConfigArgs *c);
99 int config_passwd_hash(ConfigArgs *c);
100 int config_schema_dn(ConfigArgs *c);
101 int config_sizelimit(ConfigArgs *c);
102 int config_timelimit(ConfigArgs *c);
103 int config_limits(ConfigArgs *c); 
104 int config_overlay(ConfigArgs *c);
105 int config_suffix(ConfigArgs *c); 
106 int config_deref_depth(ConfigArgs *c);
107 int config_rootdn(ConfigArgs *c);
108 int config_rootpw(ConfigArgs *c);
109 int config_restrict(ConfigArgs *c);
110 int config_allows(ConfigArgs *c);
111 int config_disallows(ConfigArgs *c);
112 int config_requires(ConfigArgs *c);
113 int config_security(ConfigArgs *c);
114 int config_referral(ConfigArgs *c);
115 int config_loglevel(ConfigArgs *c);
116 int config_syncrepl(ConfigArgs *c);
117 int config_replica(ConfigArgs *c);
118 int config_updatedn(ConfigArgs *c);
119 int config_updateref(ConfigArgs *c);
120 int config_include(ConfigArgs *c);
121 #ifdef HAVE_TLS
122 int config_tls_option(ConfigArgs *c);
123 int config_tls_verify(ConfigArgs *c);
124 #endif
125 #ifdef LDAP_SLAPI
126 int config_plugin(ConfigArgs *c);
127 #endif
128 int config_pluginlog(ConfigArgs *c);
129
130 enum {
131         CFG_BACKEND = 1,
132         CFG_DATABASE,
133         CFG_TLS_RAND,
134         CFG_TLS_CIPHER,
135         CFG_TLS_CERT_FILE,
136         CFG_TLS_CERT_KEY,
137         CFG_TLS_CERT_PATH,
138         CFG_TLS_CA_FILE,
139         CFG_TLS_VERIFY,
140         CFG_TLS_CRLCHECK,
141         CFG_SIZE,
142         CFG_TIME,
143         CFG_CONCUR,
144         CFG_THREADS,
145         CFG_SALT,
146         CFG_LIMITS,
147         CFG_RO,
148         CFG_REWRITE,
149         CFG_DEPTH,
150         CFG_OID,
151         CFG_OC,
152         CFG_DIT,
153         CFG_ATTR,
154         CFG_ATOPT,
155         CFG_CHECK,
156         CFG_ACL,
157         CFG_AUDITLOG,
158         CFG_REPLOG,
159         CFG_ROOTDSE,
160         CFG_LOGFILE,
161         CFG_PLUGIN,
162         CFG_MODLOAD,
163         CFG_MODPATH,
164         CFG_LASTMOD,
165         CFG_AZPOLICY,
166         CFG_AZREGEXP,
167         CFG_SASLSECP
168 };
169
170 /* original config.c ordering */
171
172 ConfigTable SystemConfiguration[] = {
173   { "backend", "type",  2,  2,  0,  ARG_PRE_DB|ARG_MAGIC|CFG_BACKEND, &config_generic,  NULL, NULL, NULL },
174   { "database", "type",         2,  2,  0,  ARG_MAGIC|CFG_DATABASE,     &config_generic,                NULL, NULL, NULL },
175   { "localSSF", "ssf",                  2,  2,  0,  ARG_LONG,           &local_ssf,                     NULL, NULL, NULL },
176   { "concurrency",      "level",                2,  2,  0,  ARG_LONG|ARG_NONZERO|ARG_MAGIC|CFG_CONCUR, &config_generic, NULL, NULL, NULL },
177   { "index_substr_if_minlen","min",             2,  2,  0,  ARG_INT|ARG_NONZERO,        &index_substr_if_minlen,        NULL, NULL, NULL },
178   { "index_substr_if_maxlen","max",             2,  2,  0,  ARG_INT|ARG_NONZERO|ARG_SPECIAL, &index_substr_if_maxlen, NULL, NULL, NULL },
179   { "index_substr_any_len",     "len",  2,  2,  0,  ARG_INT|ARG_NONZERO,        &index_substr_any_len,          NULL, NULL, NULL },
180   { "index_substr_step",        "step", 2,  2,  0,  ARG_INT|ARG_NONZERO,        &index_substr_any_step,         NULL, NULL, NULL },
181   { "sockbuf_max_incoming",     "max",  2,  2,  0,  ARG_LONG,           &sockbuf_max_incoming,          NULL, NULL, NULL },
182   { "sockbuf_max_incoming_auth","max",  2,  2,  0,  ARG_LONG,           &sockbuf_max_incoming_auth,     NULL, NULL, NULL },
183   { "conn_max_pending", "max",          2,  2,  0,  ARG_LONG,           &slap_conn_max_pending,         NULL, NULL, NULL },
184   { "conn_max_pending_auth",    "max",  2,  2,  0,  ARG_LONG,           &slap_conn_max_pending_auth,    NULL, NULL, NULL },
185   { "defaultSearchBase",        "dn",   2,  2,  0,  ARG_DN|ARG_MAGIC,           &config_search_base,            NULL, NULL, NULL },
186   { "threads",  "count",                        2,  2,  0,  ARG_INT|ARG_MAGIC|CFG_THREADS, &config_generic,             NULL, NULL, NULL },
187   { "pidfile",  "file",                 2,  2,  0,  ARG_STRING,         &slapd_pid_file,                NULL, NULL, NULL },
188   { "argsfile", "file",                 2,  2,  0,  ARG_STRING,         &slapd_args_file,               NULL, NULL, NULL },
189   { "password-hash",    "hash",         2,  2,  0,  ARG_MAGIC,          &config_passwd_hash,            NULL, NULL, NULL },
190   { "password-crypt-salt-format","salt",        2, 2,  0,  ARG_MAGIC|CFG_SALT,  &config_generic,                NULL, NULL, NULL },
191 #ifdef SLAP_AUTH_REWRITE
192   { "auth-rewrite",     NULL,           2,  2, 14,  ARG_MAGIC|CFG_REWRITE,      &config_generic,                NULL, NULL, NULL },
193 #endif
194   { "authz-policy", "policy",           2,  2,  0,  ARG_MAGIC|CFG_AZPOLICY,     &config_generic,                NULL, NULL, NULL },
195   { "authz-regexp",     NULL,                   3,  3,  0,  ARG_MAGIC|CFG_AZREGEXP,     &config_generic,                NULL, NULL, NULL },
196   { "sasl-authz-policy",        NULL,                   2,  2,  0,  ARG_MAGIC|CFG_AZPOLICY,     &config_generic,                NULL, NULL, NULL },
197   { "sasl-regexp",      NULL,                   2,  2,  0,  ARG_MAGIC|CFG_AZREGEXP,     &config_generic,                NULL, NULL, NULL },
198   { "saslRegexp",       NULL,                   2,  2,  0,  ARG_MAGIC|CFG_AZREGEXP,     &config_generic,                NULL, NULL, NULL },
199 #ifdef HAVE_CYRUS_SASL
200   { "sasl-host", "host",                        2,  2,  0,  ARG_STRING|ARG_UNIQUE,      &global_host,           NULL, NULL, NULL },
201   { "sasl-realm", "realm",                      2,  2,  0,  ARG_STRING|ARG_UNIQUE,      &global_realm,          NULL, NULL, NULL },
202   { "sasl-secprops", "properties",      2,  2,  0,  ARG_MAGIC|CFG_SASLSECP,     &config_generic,                NULL, NULL, NULL },
203 #else
204   { "sasl-host",        NULL,           2,  2,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL },
205   { "sasl-realm",       NULL,           2,  2,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL },
206   { "sasl-secprops",    NULL,           2,  2,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL },
207 #endif
208   { "schemadn",         "dn",           2,  2,  0,  ARG_DN|ARG_MAGIC,           &config_schema_dn,              NULL, NULL, NULL },
209   { "ucdata-path",      "path",         2,  2,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL },
210   { "sizelimit",        "limit",                2,  2,  0,  ARG_MAGIC|CFG_SIZE, &config_sizelimit,              NULL, NULL, NULL },
211   { "timelimit",        "limit",                2,  2,  0,  ARG_MAGIC|CFG_TIME, &config_timelimit,              NULL, NULL, NULL },
212   { "limits",   "limits",                       2,  0,  0,  ARG_DB|ARG_MAGIC|CFG_LIMITS, &config_generic,               NULL, NULL, NULL },
213   { "overlay",  "overlay",                      2,  2,  0,  ARG_MAGIC,          &config_overlay,                NULL, NULL, NULL },
214   { "suffix",   "suffix",                       2,  2,  0,  ARG_DB|ARG_DN|ARG_MAGIC,    &config_suffix,                 NULL, NULL, NULL },
215   { "maxDerefDepth",    "depth",                2,  2,  0,  ARG_DB|ARG_INT|ARG_MAGIC|CFG_DEPTH, &config_generic,        NULL, NULL, NULL },
216   { "rootdn",   "dn",                   2,  2,  0,  ARG_DB|ARG_DN|ARG_MAGIC,    &config_rootdn,                 NULL, NULL, NULL },
217   { "rootpw",   "password",                     2,  2,  0,  ARG_DB|ARG_MAGIC,   &config_rootpw,                 NULL, NULL, NULL },
218   { "readonly",         "on|off",               2,  2,  0,  ARG_ON_OFF|ARG_MAGIC|CFG_RO, &config_generic,               NULL, NULL, NULL },
219   { "restrict", "op_list",                      2,  0,  0,  ARG_MAGIC,          &config_restrict,               NULL, NULL, NULL },
220   { "allows",   "features",                     2,  0,  5,  ARG_PRE_DB|ARG_MAGIC,       &config_allows,                 NULL, NULL, NULL },
221   { "disallows",        "features",             2,  0,  8,  ARG_PRE_DB|ARG_MAGIC,       &config_disallows,              NULL, NULL, NULL },
222   { "require",  "features",                     2,  0,  7,  ARG_MAGIC,          &config_requires,               NULL, NULL, NULL },
223   { "security", "factors",                      2,  0,  0,  ARG_MAGIC,          &config_security,               NULL, NULL, NULL },
224   { "referral",         "url",  2,  2,  0,  ARG_MAGIC,  &config_referral,               NULL, NULL, NULL },
225   { "logfile",  "file",                 2,  2,  0,  ARG_MAGIC|CFG_LOGFILE,      &config_generic,                NULL, NULL, NULL },
226   { "objectidentifier", NULL,           0,  0,  0,  ARG_MAGIC|CFG_OID,  &config_generic,                NULL, NULL, NULL },
227   { "objectclass",      "objectclass",  2,  0,  0,  ARG_PAREN|ARG_MAGIC|CFG_OC, &config_generic,                NULL, NULL, NULL },
228   { "ditcontentrule",   NULL,           0,  0,  0,  ARG_MAGIC|CFG_DIT,  &config_generic,                NULL, NULL, NULL },
229   { "attribute",        "attribute",    2,  0,  9,  ARG_PAREN|ARG_MAGIC|CFG_ATTR, &config_generic,              NULL, NULL, NULL },
230   { "attributeoptions", NULL,           0,  0,  0,  ARG_MAGIC|CFG_ATOPT,        &config_generic,                NULL, NULL, NULL },
231   { "schemacheck",      "on|off",               2,  2,  0,  ARG_ON_OFF|ARG_MAGIC|CFG_CHECK,     &config_generic,        NULL, NULL, NULL },
232   { "access",   NULL,                   0,  0,  0,  ARG_MAGIC|CFG_ACL,  &config_generic,                NULL, NULL, NULL },
233   { "loglevel", "level",                        2,  0,  0,  ARG_MAGIC,          &config_loglevel,               NULL, NULL, NULL },
234   { "syncrepl", NULL,                   0,  0,  0,  ARG_DB|ARG_MAGIC,   &config_syncrepl,               NULL, NULL, NULL },
235   { "replica",  "host or uri",          2,  0,  0,  ARG_DB|ARG_MAGIC,   &config_replica,                NULL, NULL, NULL },
236   { "replicationInterval",      NULL,   0,  0,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL },
237   { "updatedn", "dn",                   2,  2,  0,  ARG_DB|ARG_MAGIC,   &config_updatedn,               NULL, NULL, NULL },
238   { "updateref",        "url",          2,  2,  0,  ARG_DB|ARG_MAGIC,   &config_updateref,              NULL, NULL, NULL },
239   { "replogfile",       "filename",     2,  2,  0,  ARG_MAGIC|ARG_STRING|CFG_REPLOG,    &config_generic,                NULL, NULL, NULL },
240   { "rootDSE",  "filename",             2,  2,  0,  ARG_MAGIC|CFG_ROOTDSE,      &config_generic,                NULL, NULL, NULL },
241   { "lastmod",  "on|off",                       2,  2,  0,  ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_LASTMOD, &config_generic, NULL, NULL, NULL },
242 #ifdef SIGHUP
243   { "gentlehup",        "on|off",               2,  2,  0,  ARG_ON_OFF,         &global_gentlehup,              NULL, NULL, NULL },
244 #else
245   { "gentlehup",        NULL,           2,  2,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL },
246 #endif
247   { "idletimeout",      "timeout",              2,  2,  0,  ARG_INT,            &global_idletimeout,            NULL, NULL, NULL },
248 /* XXX -- special case? */
249   { "include",  "filename",                     2,  2,  0,  ARG_MAGIC,          &config_include,                NULL, NULL, NULL },
250   { "srvtab",   "filename",                     2,  2,  0,  ARG_STRING,         &ldap_srvtab,                   NULL, NULL, NULL },
251 #ifdef SLAPD_MODULES
252   { "moduleload",       "filename",             2,  2,  0,  ARG_MAGIC|CFG_MODLOAD,      &config_generic,                NULL, NULL, NULL },
253   { "modulepath",       "path",         2,  2,  0,  ARG_MAGIC|CFG_MODPATH,      &config_generic,                NULL, NULL, NULL },
254 #endif
255 #ifdef HAVE_TLS
256   { "TLSRandFile",      NULL,           0,  0,  0,  CFG_TLS_RAND|ARG_MAGIC,             &config_tls_option,     NULL, NULL, NULL },
257   { "TLSCipherSuite",   NULL,           0,  0,  0,  CFG_TLS_CIPHER|ARG_MAGIC,   &config_tls_option,     NULL, NULL, NULL },
258   { "TLSCertificateFile",       NULL,   0,  0,  0,  CFG_TLS_CERT_FILE|ARG_MAGIC,        &config_tls_option,     NULL, NULL, NULL },
259   { "TLSCertificateKeyFile",    NULL,   0,  0,  0,  CFG_TLS_CERT_KEY|ARG_MAGIC, &config_tls_option,     NULL, NULL, NULL },
260   { "TLSCertificatePath",       NULL,   0,  0,  0,  CFG_TLS_CERT_PATH|ARG_MAGIC,        &config_tls_option,     NULL, NULL, NULL },
261   { "TLSCACertificateFile",     NULL,   0,  0,  0,  CFG_TLS_CA_FILE|ARG_MAGIC,  &config_tls_option,     NULL, NULL, NULL },
262 #ifdef HAVE_OPENSSL_CRL
263   { "TLSCRLCheck",      NULL,           0,  0,  0,  CFG_TLS_CRLCHECK|ARG_MAGIC, &config_tls_option,     NULL, NULL, NULL },
264 #else
265   { "TLSCRLCheck",      NULL,           0,  0,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL },
266 #endif
267   { "TLSVerifyClient",  NULL,           0,  0,  0,  CFG_TLS_VERIFY|ARG_MAGIC,   &config_tls_verify,     NULL, NULL, NULL },
268 #endif
269 #ifdef SLAPD_RLOOKUPS
270   { "reverse-lookup",   "on|off",               2,  2,  0,  ARG_ON_OFF,         &use_reverse_lookup,            NULL, NULL, NULL },
271 #else
272   { "reverse-lookup",   NULL,           2,  2,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL },
273 #endif
274 #ifdef LDAP_SLAPI
275   { "plugin",   NULL,                   0,  0,  0,  ARG_MAGIC|CFG_PLUGIN,       &config_generic,                NULL, NULL, NULL },
276   { "pluginlog",        "filename",             2,  2,  0,  ARG_STRING,         &slapi_log_file,                NULL, NULL, NULL },
277 #else
278   { "plugin",   NULL,                   0,  0,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL },
279   { "pluginlog",        NULL,           0,  0,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL },
280 #endif
281   { "replica-pidfile",  NULL,           0,  0,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL },
282   { "replica-argsfile", NULL,           0,  0,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL },
283   { NULL,       NULL,                   0,  0,  0,  ARG_IGNORED,                NULL,                           NULL, NULL, NULL }
284 };
285
286
287 ConfigArgs *
288 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
289 {
290         ConfigArgs *c;
291         c = ch_calloc( 1, sizeof( ConfigArgs ) );
292         if ( c == NULL ) return(NULL);
293         c->be     = be; 
294         c->fname  = fname;
295         c->argc   = argc;
296         c->argv   = argv; 
297         c->lineno = lineno;
298         snprintf( c->log, sizeof( c->log ), "%s: line %lu", fname, lineno );
299         return(c);
300 }
301
302 int parse_config_table(ConfigTable *Conf, ConfigArgs *c) {
303         int i, rc, arg_user, arg_type, iarg;
304         long larg;
305         ber_len_t barg;
306         for(i = 0; Conf[i].name; i++)
307                 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
308                         (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
309         if(!Conf[i].name) return(ARG_UNKNOWN);
310         arg_type = Conf[i].arg_type;
311         if(arg_type == ARG_IGNORED) {
312                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
313                         c->log, Conf[i].name, 0);
314                 return(0);
315         }
316         if(Conf[i].min_args && (c->argc < Conf[i].min_args)) {
317                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> missing <%s> argument\n",
318                         c->log, Conf[i].name, Conf[i].what);
319                 return(ARG_BAD_CONF);
320         }
321         if(Conf[i].max_args && (c->argc > Conf[i].max_args)) {
322                 Debug(LDAP_DEBUG_CONFIG, "%s: extra cruft after <%s> in <%s> line (ignored)\n",
323                         c->log, Conf[i].what, Conf[i].name);
324         }
325         if((arg_type & ARG_DB) && !c->be) {
326                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> allowed only within database declaration\n",
327                         c->log, Conf[i].name, 0);
328                 return(ARG_BAD_CONF);
329         }
330         if((arg_type & ARG_PRE_DB) && c->be) {
331                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> must appear before any database declaration\n",
332                         c->log, Conf[i].name, 0);
333                 return(ARG_BAD_CONF);
334         }
335         if((arg_type & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
336                 Debug(LDAP_DEBUG_CONFIG, "%s: old <%s> format not supported\n",
337                         c->log, Conf[i].name, 0);
338                 return(ARG_BAD_CONF);
339         }
340         if((arg_type & ARGS_POINTER) && !Conf[i].arg_item) {
341                 Debug(LDAP_DEBUG_CONFIG, "%s: null arg_item for <%s>\n",
342                         c->log, Conf[i].name, 0);
343                 return(ARG_BAD_CONF);
344         }
345         c->type = arg_user = (arg_type & ARGS_USERLAND);
346         c->value_int = c->value_long = c->value_ber_t = 0;
347         c->value_string = NULL;
348         BER_BVZERO( &c->value_dn );
349         BER_BVZERO( &c->value_ndn );
350         if(arg_type & ARGS_NUMERIC) {
351                 int j;
352                 iarg = 0; larg = 0; barg = 0;
353                 switch(arg_type & ARGS_NUMERIC) {
354                         case ARG_INT:           iarg = atoi(c->argv[1]);                break;
355                         case ARG_LONG:          larg = atol(c->argv[1]);                break;
356                         case ARG_BER_LEN_T:     barg = (ber_len_t)atol(c->argv[1]);     break;
357                         case ARG_ON_OFF:
358                                 if(!strcasecmp(c->argv[1], "on")) {
359                                         iarg = 1;
360                                 } else if(!strcasecmp(c->argv[1], "off")) {
361                                         iarg = 0;
362                                 } else {
363                                         Debug(LDAP_DEBUG_CONFIG, "%s: ignoring ", c->log, 0, 0);
364                                         Debug(LDAP_DEBUG_CONFIG, "invalid %s value (%s) in <%s> line\n",
365                                                 Conf[i].what, c->argv[1], Conf[i].name);
366                                         return(0);
367                                 }
368                                 break;
369                 }
370                 j = (arg_type & ARG_NONZERO) ? 1 : 0;
371                 rc = (Conf == SystemConfiguration) ? ((arg_type & ARG_SPECIAL) && (larg < index_substr_if_maxlen)) : 0;
372                 if(iarg < j || larg < j || barg < j || rc) {
373                         larg = larg ? larg : (barg ? barg : iarg);
374                         Debug(LDAP_DEBUG_CONFIG, "%s: " , c->log, 0, 0);
375                         Debug(LDAP_DEBUG_CONFIG, "invalid %s value (%ld) in <%s> line\n", Conf[i].what, larg, Conf[i].name);
376                         return(ARG_BAD_CONF);
377                 }
378                 c->value_int = iarg;
379                 c->value_long = larg;
380                 c->value_ber_t = barg;
381         }
382         if(arg_type & ARG_STRING) c->value_string = ch_strdup(c->argv[1]);
383         if(arg_type & ARG_DN) {
384                 struct berval bv;
385                 ber_str2bv( c->argv[1], 0, 0, &bv );
386                 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
387                 if ( rc != LDAP_SUCCESS ) {
388                         Debug(LDAP_DEBUG_CONFIG, "%s: " , c->log, 0, 0);
389                         Debug(LDAP_DEBUG_CONFIG, "%s DN is invalid %d (%s)\n",
390                                 Conf[i].name, rc, ldap_err2string( rc ));
391                         return(ARG_BAD_CONF);
392                 }
393         }
394         if(arg_type & ARG_MAGIC) {
395                 if(!c->be) c->be = frontendDB;
396                 rc = (*((ConfigDriver*)Conf[i].arg_item))(c);
397                 if(c->be == frontendDB) c->be = NULL;
398                 if(rc) {
399                         Debug(LDAP_DEBUG_CONFIG, "%s: handler for <%s> exited with %d!",
400                                 c->log, Conf[i].name, rc);
401                         return(ARG_BAD_CONF);
402                 }
403                 return(0);
404         }
405         if(arg_type & ARGS_POINTER) switch(arg_type & ARGS_POINTER) {
406                         case ARG_ON_OFF:
407                         case ARG_INT:           *((int*)Conf[i].arg_item)               = iarg;                 break;
408                         case ARG_LONG:          *((long*)Conf[i].arg_item)              = larg;                 break;
409                         case ARG_BER_LEN_T:     *((ber_len_t*)Conf[i].arg_item)         = barg;                 break;
410                         case ARG_STRING: {
411                                 char *cc = *((char**)Conf[i].arg_item);
412                                 if(cc) {
413                                         if (arg_type & ARG_UNIQUE) {
414                                                 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
415                                                         c->log, Conf[i].name, 0 );
416                                                 return(ARG_BAD_CONF);
417                                         }
418                                         ch_free(cc);    /* potential memory leak */
419                                 }
420                                 *(char **)Conf[i].arg_item = c->value_string;
421                                 break;
422                                 }
423         }
424         return(arg_user);
425 }
426
427 int
428 read_config(const char *fname, int depth) {
429         return read_config_file(fname, depth, NULL);
430 }
431
432 int
433 read_config_file(const char *fname, int depth, ConfigArgs *cf)
434 {
435         FILE *fp;
436         ConfigArgs *c;
437         int rc;
438
439         c = ch_calloc( 1, sizeof( ConfigArgs ) );
440         if ( c == NULL ) {
441                 return 1;
442         }
443
444         if ( depth ) {
445                 memcpy( c, cf, sizeof( ConfigArgs ) );
446         } else {
447                 c->depth = depth; /* XXX */
448                 c->bi = NULL;
449                 c->be = NULL;
450         }
451
452         c->fname = fname;
453         c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
454         c->argv_size = ARGS_STEP + 1;
455
456         fp = fopen( fname, "r" );
457         if ( fp == NULL ) {
458                 ldap_syslog = 1;
459                 Debug(LDAP_DEBUG_ANY,
460                     "could not open config file \"%s\": %s (%d)\n",
461                     fname, strerror(errno), errno);
462                 return(1);
463         }
464
465         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
466
467         fp_getline_init(c);
468
469         while ( fp_getline( fp, c ) ) {
470                 /* skip comments and blank lines */
471                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
472                         continue;
473                 }
474
475                 snprintf( c->log, sizeof( c->log ), "%s: line %lu",
476                                 c->fname, c->lineno );
477
478                 if ( fp_parse_line( c ) ) {
479                         goto badline;
480                 }
481
482                 if ( c->argc < 1 ) {
483                         Debug(LDAP_DEBUG_CONFIG, "%s: bad config line (ignored)\n", c->log, 0, 0);
484                         continue;
485                 }
486
487                 rc = parse_config_table( SystemConfiguration, c );
488                 if ( !rc ) {
489                         continue;
490                 }
491                 if ( rc & ARGS_USERLAND ) {
492                         switch(rc) {    /* XXX a usertype would be opaque here */
493                         default:
494                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%d>\n",
495                                         c->log, *c->argv, 0);
496                                 goto badline;
497                         }
498
499                 } else if ( rc == ARG_BAD_CONF || rc != ARG_UNKNOWN ) {
500                         goto badline;
501                         
502                 } else if ( c->bi && c->bi->bi_config ) {               /* XXX to check: could both be/bi_config? oops */
503                         rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno, c->argc, c->argv);
504                         if ( rc ) {
505                                 switch(rc) {
506                                 case SLAP_CONF_UNKNOWN:
507                                         Debug(LDAP_DEBUG_CONFIG, "%s: "
508                                                 "unknown directive <%s> inside backend info definition (ignored)\n",
509                                                 c->log, *c->argv, 0);
510                                         continue;
511                                 default:
512                                         goto badline;
513                                 }
514                         }
515                         
516                 } else if ( c->be && c->be->be_config ) {
517                         rc = (*c->be->be_config)(c->be, c->fname, c->lineno, c->argc, c->argv);
518                         if ( rc ) {
519                                 switch(rc) {
520                                 case SLAP_CONF_UNKNOWN:
521                                         Debug( LDAP_DEBUG_CONFIG, "%s: "
522                                                 "unknown directive <%s> inside backend database definition (ignored)\n",
523                                                 c->log, *c->argv, 0);
524                                         continue;
525                                 default:
526                                         goto badline;
527                                 }
528                         }
529
530                 } else if ( frontendDB->be_config ) {
531                         rc = (*frontendDB->be_config)(frontendDB, c->fname, (int)c->lineno, c->argc, c->argv);
532                         if ( rc ) {
533                                 switch(rc) {
534                                 case SLAP_CONF_UNKNOWN:
535                                         Debug( LDAP_DEBUG_CONFIG, "%s: "
536                                                 "unknown directive <%s> inside global database definition (ignored)\n",
537                                                 c->log, *c->argv, 0);
538                                         continue;
539                                 default:
540                                         goto badline;
541                                 }
542                         }
543                         
544                 } else {
545                         Debug(LDAP_DEBUG_CONFIG, "%s: "
546                                 "unknown directive <%s> outside backend info and database definitions (ignored)\n",
547                                 c->log, *c->argv, 0);
548                         continue;
549
550                 }
551         }
552
553         fclose(fp);
554
555         if ( BER_BVISNULL( &frontendDB->be_schemadn ) ) {
556                 ber_str2bv( SLAPD_SCHEMA_DN, STRLENOF( SLAPD_SCHEMA_DN ), 1,
557                         &frontendDB->be_schemadn );
558                 rc = dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
559                 if ( rc != LDAP_SUCCESS ) {
560                         Debug(LDAP_DEBUG_ANY, "%s: "
561                                 "unable to normalize default schema DN \"%s\"\n",
562                                 c->log, frontendDB->be_schemadn.bv_val, 0 );
563                         /* must not happen */
564                         assert( 0 );
565                 }
566         }
567
568         ch_free(c->argv);
569         ch_free(c);
570         return(0);
571
572 badline:
573         fclose(fp);
574         ch_free(c->argv);
575         ch_free(c);
576         return(1);
577 }
578
579 int
580 config_generic(ConfigArgs *c) {
581         char *p = strchr(c->line,'(' /*')'*/);
582         int i;
583
584         switch(c->type) {
585                 case CFG_BACKEND:
586                         if(!(c->bi = backend_info(c->argv[1]))) {
587                                 Debug(LDAP_DEBUG_ANY, "%s: "
588                                         "backend %s failed init!\n", c->log, c->argv[1], 0);
589                                 return(1);
590                         }
591                         break;
592
593                 case CFG_DATABASE:
594                         c->bi = NULL;
595                         if(!(c->be = backend_db_init(c->argv[1]))) {
596                                 Debug(LDAP_DEBUG_ANY, "%s: "
597                                         "database %s failed init!\n", c->log, c->argv[1], 0);
598                                 return(1);
599                         }
600                         break;
601
602                 case CFG_CONCUR:
603                         ldap_pvt_thread_set_concurrency(c->value_long);
604                         break;
605
606                 case CFG_THREADS:
607                         ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
608                         connection_pool_max = c->value_int;     /* save for reference */
609                         break;
610
611                 case CFG_SALT:
612                         lutil_salt_format(c->argv[1]);
613                         break;
614
615                 case CFG_LIMITS:
616                         if(limits_parse(c->be, c->fname, c->lineno, c->argc, c->argv))
617                                 return(1);
618                         break;
619
620                 case CFG_RO:
621                         if(c->value_int)
622                                 c->be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
623                         else
624                                 c->be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
625                         break;
626
627                 case CFG_AZPOLICY:
628                         if (slap_sasl_setpolicy( c->argv[1] )) {
629                                 Debug(LDAP_DEBUG_ANY, "%s: unable to parse value \"%s\" in"
630                                         " \"authz-policy <policy>\"\n",
631                                         c->log, c->argv[1], 0 );
632                                 return(1);
633                         }
634                         break;
635                 
636                 case CFG_AZREGEXP:
637                         if (slap_sasl_regexp_config( c->argv[1], c->argv[2] ))
638                                 return(1);
639                         break;
640                                 
641 #ifdef HAVE_CYRUS_SASL
642                 case CFG_SASLSECP:
643                         {
644                         char *txt = slap_sasl_secprops( c->argv[1] );
645                         if ( txt ) {
646                                 Debug(LDAP_DEBUG_ANY, "%s: sasl-secprops: %s\n",
647                                         c->log, txt, 0 );
648                                 return(1);
649                         }
650                         break;
651 #endif
652
653                 case CFG_DEPTH:
654                         c->be->be_max_deref_depth = c->value_int;
655                         break;
656
657                 case CFG_OID:
658                         if(parse_oidm(c->fname, c->lineno, c->argc, c->argv)) return(1);
659                         break;
660
661                 case CFG_OC:
662                         if(parse_oc(c->fname, c->lineno, p, c->argv)) return(1);
663                         break;
664
665                 case CFG_DIT:
666                         if(parse_cr(c->fname, c->lineno, p, c->argv)) return(1);
667                         break;
668
669                 case CFG_ATTR:
670                         if(parse_at(c->fname, c->lineno, p, c->argv)) return(1);
671                         break;
672
673                 case CFG_ATOPT:
674                         ad_define_option(NULL, NULL, 0);
675                         for(i = 1; i < c->argc; i++)
676                                 if(ad_define_option(c->argv[i], c->fname, c->lineno))
677                                         return(1);
678                         break;
679
680                 case CFG_CHECK:
681                         global_schemacheck = c->value_int;
682                         if(!global_schemacheck) Debug(LDAP_DEBUG_ANY, "%s: "
683                                 "schema checking disabled! your mileage may vary!\n",
684                                 c->log, 0, 0);
685                         break;
686
687                 case CFG_ACL:
688                         parse_acl(c->be, c->fname, c->lineno, c->argc, c->argv);
689                         break;
690
691 #if 0
692                 case CFG_AUDITLOG:
693                         c->be->be_auditlogfile = c->value_string;
694                         break;
695 #endif
696
697                 case CFG_REPLOG:
698                         if(SLAP_MONITOR(c->be)) {
699                                 Debug(LDAP_DEBUG_ANY, "%s: "
700                                         "\"replogfile\" should not be used "
701                                         "inside monitor database\n",
702                                         c->log, 0, 0);
703                                 return(0);      /* FIXME: should this be an error? */
704                         }
705
706                         c->be->be_replogfile = c->value_string;
707                         break;
708
709                 case CFG_ROOTDSE:
710                         if(read_root_dse_file(c->argv[1])) {
711                                 Debug(LDAP_DEBUG_ANY, "%s: "
712                                         "could not read \"rootDSE <filename>\" line\n",
713                                         c->log, 0, 0);
714                                 return(1);
715                         }
716                         break;
717
718                 case CFG_LOGFILE: {
719                         FILE *logfile = fopen(c->argv[1], "w");
720                         if(logfile) lutil_debug_file(logfile);
721                         break;
722                         }
723
724                 case CFG_LASTMOD:
725                         if(SLAP_NOLASTMODCMD(c->be)) {
726                                 Debug(LDAP_DEBUG_ANY, "%s: "
727                                         "lastmod not available for %s databases\n",
728                                         c->log, c->be->bd_info->bi_type, 0);
729                                 return(1);
730                         }
731                         if(c->value_int)
732                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_NOLASTMOD;
733                         else
734                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NOLASTMOD;
735                         break;
736
737 #ifdef SLAPD_MODULES
738                 case CFG_MODLOAD:
739                         if(module_load(c->argv[1], c->argc - 2, (c->argc > 2) ? c->argv + 2 : NULL))
740                                 return(1);
741                         break;
742
743                 case CFG_MODPATH:
744                         if(module_path(c->argv[1])) return(1);
745                         break;
746 #endif
747
748 #ifdef LDAP_SLAPI
749                 case CFG_PLUGIN:
750                         if(slapi_int_read_config(c->be, c->fname, c->lineno, c->argc, c->argv) != LDAP_SUCCESS)
751                                 return(1);
752                         slapi_plugins_used++;
753                         break;
754 #endif
755
756 #ifdef SLAP_AUTH_REWRITE
757                 case CFG_REWRITE:
758                         if(slap_sasl_rewrite_config(c->fname, c->lineno, c->argc, c->argv))
759                                 return(1);
760                         break;
761 #endif
762
763
764                 default:
765                         Debug(LDAP_DEBUG_ANY, "%s: unknown CFG_TYPE %d"
766                                 "(ignored)\n", c->log, c->type, 0);
767
768         }
769         return(0);
770 }
771
772
773 int
774 config_search_base(ConfigArgs *c) {
775         struct berval dn;
776         int rc;
777         if(c->bi || c->be != frontendDB) {
778                 Debug(LDAP_DEBUG_ANY, "%s: defaultSearchBase line must appear "
779                         "prior to any backend or database definition\n",
780                         c->log, 0, 0);
781                 return(1);
782         }
783
784         if(default_search_nbase.bv_len) {
785                 Debug(LDAP_DEBUG_ANY, "%s: "
786                         "default search base \"%s\" already defined "
787                         "(discarding old)\n",
788                         c->log, default_search_base.bv_val, 0);
789                 free(default_search_base.bv_val);
790                 free(default_search_nbase.bv_val);
791         }
792
793         default_search_base = c->value_dn;
794         default_search_nbase = c->value_ndn;
795         return(0);
796 }
797
798 int
799 config_passwd_hash(ConfigArgs *c) {
800         int i;
801         if(default_passwd_hash) {
802                 Debug(LDAP_DEBUG_ANY, "%s: "
803                         "already set default password_hash\n",
804                         c->log, 0, 0);
805                 return(1);
806         }
807         for(i = 1; i < c->argc; i++) {
808                 if(!lutil_passwd_scheme(c->argv[i])) {
809                         Debug(LDAP_DEBUG_ANY, "%s: "
810                                 "password scheme \"%s\" not available\n",
811                                 c->log, c->argv[i], 0);
812                 } else {
813                         ldap_charray_add(&default_passwd_hash, c->argv[i]);
814                 }
815                 if(!default_passwd_hash) {
816                         Debug(LDAP_DEBUG_ANY, "%s: no valid hashes found\n",
817                                 c->log, 0, 0 );
818                         return(1);
819                 }
820         }
821         return(0);
822 }
823
824 int
825 config_schema_dn(ConfigArgs *c) {
826         struct berval dn;
827         int rc;
828         c->be->be_schemadn = c->value_dn;
829         c->be->be_schemandn = c->value_ndn;
830         return(0);
831 }
832
833 int
834 config_sizelimit(ConfigArgs *c) {
835         int i, rc = 0;
836         char *next;
837         struct slap_limits_set *lim = &c->be->be_def_limit;
838         for(i = 1; i < c->argc; i++) {
839                 if(!strncasecmp(c->argv[i], "size", 4)) {
840                         rc = limits_parse_one(c->argv[i], lim);
841                         if ( rc ) {
842                                 Debug(LDAP_DEBUG_ANY, "%s: "
843                                         "unable to parse value \"%s\" in \"sizelimit <limit>\" line\n",
844                                         c->log, c->argv[i], 0);
845                                 return(1);
846                         }
847                 } else {
848                         if(!strcasecmp(c->argv[i], "unlimited")) {
849                                 lim->lms_s_soft = -1;
850                         } else {
851                                 lim->lms_s_soft = strtol(c->argv[i], &next, 0);
852                                 if(next == c->argv[i]) {
853                                         Debug(LDAP_DEBUG_ANY, "%s: "
854                                                 "unable to parse limit \"%s\" in \"sizelimit <limit>\" line\n",
855                                                 c->log, c->argv[i], 0);
856                                         return(1);
857                                 } else if(next[0] != '\0') {
858                                         Debug(LDAP_DEBUG_ANY, "%s: "
859                                                 "trailing chars \"%s\" in \"sizelimit <limit>\" line (ignored)\n",
860                                                 c->log, next, 0);
861                                 }
862                         }
863                         lim->lms_s_hard = 0;
864                 }
865         }
866         return(0);
867 }
868
869 int
870 config_timelimit(ConfigArgs *c) {
871         int i, rc = 0;
872         char *next;
873         struct slap_limits_set *lim = &c->be->be_def_limit;
874         for(i = 1; i < c->argc; i++) {
875                 if(!strncasecmp(c->argv[i], "time", 4)) {
876                         rc = limits_parse_one(c->argv[i], lim);
877                         if ( rc ) {
878                                 Debug(LDAP_DEBUG_ANY, "%s: "
879                                         "unable to parse value \"%s\" in \"timelimit <limit>\" line\n",
880                                         c->log, c->argv[i], 0);
881                                 return(1);
882                         }
883                 } else {
884                         if(!strcasecmp(c->argv[i], "unlimited")) {
885                                 lim->lms_t_soft = -1;
886                         } else {
887                                 lim->lms_t_soft = strtol(c->argv[i], &next, 0);
888                                 if(next == c->argv[i]) {
889                                         Debug(LDAP_DEBUG_ANY, "%s: "
890                                                 "unable to parse limit \"%s\" in \"timelimit <limit>\" line\n",
891                                                 c->log, c->argv[i], 0);
892                                         return(1);
893                                 } else if(next[0] != '\0') {
894                                         Debug(LDAP_DEBUG_ANY, "%s: "
895                                                 "trailing chars \"%s\" in \"timelimit <limit>\" line (ignored)\n",
896                                                 c->log, next, 0);
897                                 }
898                         }
899                         lim->lms_t_hard = 0;
900                 }
901         }
902         return(0);
903 }
904
905 int
906 config_overlay(ConfigArgs *c) {
907         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1])) {
908                 /* log error */
909                 Debug(LDAP_DEBUG_ANY, "%s: (optional) %s overlay \"%s\" configuration failed (ignored)\n",
910                         c->log, c->be == frontendDB ? "global " : "", c->argv[1][1]);
911         } else if(overlay_config(c->be, c->argv[1])) {
912                 return(1);
913         }
914         return(0);
915 }
916
917 int
918 config_suffix(ConfigArgs *c) {
919         Backend *tbe;
920         struct berval pdn, ndn;
921         int rc;
922 #ifdef SLAPD_MONITOR_DN
923         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
924                 Debug(LDAP_DEBUG_ANY, "%s: "
925                         "\"%s\" is reserved for monitoring slapd\n",
926                         c->log, SLAPD_MONITOR_DN, 0);
927                 return(1);
928         }
929 #endif
930
931         pdn = c->value_dn;
932         ndn = c->value_ndn;
933         tbe = select_backend(&ndn, 0, 0);
934         if(tbe == c->be) {
935                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by this backend! (ignored)\n",
936                         c->log, 0, 0);
937                 free(pdn.bv_val);
938                 free(ndn.bv_val);
939         } else if(tbe) {
940                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by a preceding backend \"%s\"\n",
941                         c->log, tbe->be_suffix[0].bv_val, 0);
942                 free(pdn.bv_val);
943                 free(ndn.bv_val);
944                 return(1);
945         } else if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
946                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
947                         "base provided \"%s\" (assuming okay)\n",
948                         c->log, default_search_base.bv_val, 0);
949         }
950         ber_bvarray_add(&c->be->be_suffix, &pdn);
951         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
952         return(0);
953 }
954
955 int
956 config_rootdn(ConfigArgs *c) {
957         c->be->be_rootdn = c->value_dn;
958         c->be->be_rootndn = c->value_ndn;
959         return(0);
960 }
961
962 int
963 config_rootpw(ConfigArgs *c) {
964         Backend *tbe = select_backend(&c->be->be_rootndn, 0, 0);
965         if(tbe != c->be) {
966                 Debug(LDAP_DEBUG_ANY, "%s: "
967                         "rootpw can only be set when rootdn is under suffix\n",
968                         c->log, 0, 0);
969                 return(1);
970         }
971         ber_str2bv(c->argv[1], 0, 1, &c->be->be_rootpw);
972         return(0);
973 }
974
975 /* restrictops, allows, disallows, requires, loglevel */
976
977 struct verb_mask_list { char *word; int mask; };
978
979 int
980 verb_to_mask(ConfigArgs *c, struct verb_mask_list *v, int word) {
981         int j;
982         for(j = 0; v[j].word; j++)
983                 if(!strcasecmp(c->argv[word], v[j].word))
984                         break;
985         return(j);
986 }
987
988 int
989 verbs_to_mask(ConfigArgs *c, struct verb_mask_list *v, slap_mask_t *m) {
990         int i, j;
991         for(i = 1; i < c->argc; i++) {
992                 j = verb_to_mask(c, v, i);
993                 if(!v[j].word) return(1);
994                 *m |= v[j].mask;
995         }
996         return(0);
997 }
998
999 int
1000 config_restrict(ConfigArgs *c) {
1001         slap_mask_t restrictops = 0;
1002         int i, j;
1003         struct verb_mask_list restrictable_exops[] = {
1004                 { LDAP_EXOP_START_TLS,          SLAP_RESTRICT_EXOP_START_TLS },
1005                 { LDAP_EXOP_MODIFY_PASSWD,      SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
1006                 { LDAP_EXOP_X_WHO_AM_I,         SLAP_RESTRICT_EXOP_WHOAMI },
1007                 { LDAP_EXOP_X_CANCEL,           SLAP_RESTRICT_EXOP_CANCEL },
1008                 { NULL, 0 }
1009         };
1010         struct verb_mask_list restrictable_ops[] = {
1011                 { "bind",               SLAP_RESTRICT_OP_BIND },
1012                 { "add",                SLAP_RESTRICT_OP_ADD },
1013                 { "modify",             SLAP_RESTRICT_OP_MODIFY },
1014                 { "modrdn",             SLAP_RESTRICT_OP_RENAME },
1015                 { "rename",             SLAP_RESTRICT_OP_RENAME },
1016                 { "delete",             SLAP_RESTRICT_OP_DELETE },
1017                 { "search",             SLAP_RESTRICT_OP_SEARCH },
1018                 { "compare",            SLAP_RESTRICT_OP_COMPARE },
1019                 { "read",               SLAP_RESTRICT_OP_READS },
1020                 { "write",              SLAP_RESTRICT_OP_WRITES },
1021                 { NULL, 0 }
1022         };
1023
1024         for(i = 1; i < c->argc; i++) {
1025                 j = verb_to_mask(c, restrictable_ops, i);
1026                 if(restrictable_ops[j].word) {
1027                         restrictops |= restrictable_ops[j].mask;
1028                         continue;
1029                 } else if(!strncasecmp(c->argv[i], "extended", STRLENOF("extended"))) {
1030                         char *e = c->argv[i] + STRLENOF("extended");
1031                         if(e[0] == '=') {
1032                                 int k = verb_to_mask(c, restrictable_exops, e[1]);
1033                                 if(restrictable_exops[k].word) {
1034                                         restrictops |= restrictable_exops[k].mask;
1035                                         continue;
1036                                 } else break;
1037                         } else if(!e[0]) {
1038                                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
1039                                 restrictops |= SLAP_RESTRICT_OP_EXTENDED;
1040                         } else break;
1041                 }
1042         }
1043         if(i < c->argc) {
1044                 c->be->be_restrictops |= restrictops;
1045                 return(0);
1046         }
1047         Debug(LDAP_DEBUG_ANY, "%s: "
1048                 "unknown operation %s in \"restrict <features>\" line\n",
1049                 c->log, c->argv[i], 0);
1050         return(1);
1051 }
1052
1053 int
1054 config_allows(ConfigArgs *c) {
1055         slap_mask_t allows = 0;
1056         int i;
1057         struct verb_mask_list allowable_ops[] = {
1058                 { "bind_v2",            SLAP_ALLOW_BIND_V2 },
1059                 { "bind_anon_cred",     SLAP_ALLOW_BIND_ANON_CRED },
1060                 { "bind_anon_dn",       SLAP_ALLOW_BIND_ANON_DN },
1061                 { "update_anon",        SLAP_ALLOW_UPDATE_ANON },
1062                 { NULL, 0 }
1063         };
1064         i = verbs_to_mask(c, allowable_ops, &allows);
1065         if ( i ) {
1066                 Debug(LDAP_DEBUG_ANY, "%s: "
1067                         "unknown feature %s in \"allow <features>\" line\n",
1068                         c->log, c->argv[i], 0);
1069                 return(1);
1070         }
1071         global_allows |= allows;
1072         return(0);
1073 }
1074
1075 int
1076 config_disallows(ConfigArgs *c) {
1077         slap_mask_t disallows = 0;
1078         int i;
1079         struct verb_mask_list disallowable_ops[] = {
1080                 { "bind_anon",          SLAP_DISALLOW_BIND_ANON },
1081                 { "bind_simple",        SLAP_DISALLOW_BIND_SIMPLE },
1082                 { "bind_krb4",          SLAP_DISALLOW_BIND_KRBV4 },
1083                 { "tls_2_anon",         SLAP_DISALLOW_TLS_2_ANON },
1084                 { "tls_authc",          SLAP_DISALLOW_TLS_AUTHC },
1085                 { NULL, 0 }
1086         };
1087         i = verbs_to_mask(c, disallowable_ops, &disallows);
1088         if ( i ) {
1089                 Debug(LDAP_DEBUG_ANY, "%s: "
1090                         "unknown feature %s in \"disallow <features>\" line\n",
1091                         c->log, c->argv[i], 0);
1092                 return(1);
1093         }
1094         global_disallows |= disallows;
1095         return(0);
1096 }
1097
1098 int
1099 config_requires(ConfigArgs *c) {
1100         slap_mask_t requires = 0;
1101         int i;
1102         struct verb_mask_list requires_ops[] = {
1103                 { "bind",               SLAP_REQUIRE_BIND },
1104                 { "LDAPv3",             SLAP_REQUIRE_LDAP_V3 },
1105                 { "authc",              SLAP_REQUIRE_AUTHC },
1106                 { "sasl",               SLAP_REQUIRE_SASL },
1107                 { "strong",             SLAP_REQUIRE_STRONG },
1108                 { NULL, 0 }
1109         };
1110         i = verbs_to_mask(c, requires_ops, &requires);
1111         if ( i ) {
1112                 Debug(LDAP_DEBUG_ANY, "%s: "
1113                         "unknown feature %s in \"require <features>\" line\n",
1114                         c->log, c->argv[i], 0);
1115                 return(1);
1116         }
1117         c->be->be_requires = requires;
1118         return(0);
1119 }
1120
1121 int
1122 config_loglevel(ConfigArgs *c) {
1123         int i;
1124         char *next;
1125         struct verb_mask_list loglevel_ops[] = {
1126                 { "Trace",      LDAP_DEBUG_TRACE },
1127                 { "Packets",    LDAP_DEBUG_PACKETS },
1128                 { "Args",       LDAP_DEBUG_ARGS },
1129                 { "Conns",      LDAP_DEBUG_CONNS },
1130                 { "BER",        LDAP_DEBUG_BER },
1131                 { "Filter",     LDAP_DEBUG_FILTER },
1132                 { "Config",     LDAP_DEBUG_CONFIG },
1133                 { "ACL",        LDAP_DEBUG_ACL },
1134                 { "Stats",      LDAP_DEBUG_STATS },
1135                 { "Stats2",     LDAP_DEBUG_STATS2 },
1136                 { "Shell",      LDAP_DEBUG_SHELL },
1137                 { "Parse",      LDAP_DEBUG_PARSE },
1138                 { "Cache",      LDAP_DEBUG_CACHE },
1139                 { "Index",      LDAP_DEBUG_INDEX },
1140                 { "Any",        -1 },
1141                 { NULL, 0 }
1142         };
1143         ldap_syslog = 0;
1144
1145         for( i=1; i < c->argc; i++ ) {
1146                 int     level;
1147
1148                 if ( isdigit( c->argv[i][0] ) ) {
1149                         level = strtol( c->argv[i], &next, 10 );
1150                         if ( next == NULL || next[0] != '\0' ) {
1151                                 Debug( LDAP_DEBUG_ANY,
1152                                         "%s: unable to parse level \"%s\" "
1153                                         "in \"loglevel <level> [...]\" line.\n",
1154                                         c->log, c->argv[i], 0);
1155                                 return( 1 );
1156                         }
1157                 } else {
1158                         int j = verb_to_mask(c, loglevel_ops, c->argv[i][0]);
1159                         if(!loglevel_ops[j].word) {
1160                                 Debug( LDAP_DEBUG_ANY,
1161                                         "%s: unknown level \"%s\" "
1162                                         "in \"loglevel <level> [...]\" line.\n",
1163                                         c->log, c->argv[i], 0);
1164                                 return( 1 );
1165                         }
1166                         level = loglevel_ops[j].mask;
1167                 }
1168                 ldap_syslog |= level;
1169         }
1170         return(0);
1171 }
1172
1173 int
1174 config_syncrepl(ConfigArgs *c) {
1175         if(SLAP_SHADOW(c->be)) {
1176                 Debug(LDAP_DEBUG_ANY, "%s: "
1177                         "syncrepl: database already shadowed.\n",
1178                         c->log, 0, 0);
1179                 return(1);
1180         } else if(add_syncrepl(c->be, c->argv, c->argc)) {
1181                 return(1);
1182         }
1183         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);
1184         return(0);
1185 }
1186
1187 int
1188 config_referral(ConfigArgs *c) {
1189         struct berval vals[2];
1190         if(validate_global_referral(c->argv[1])) {
1191                 Debug(LDAP_DEBUG_ANY, "%s: "
1192                         "invalid URL (%s) in \"referral\" line.\n",
1193                         c->log, c->argv[1], 0);
1194                 return(1);
1195         }
1196
1197         ber_str2bv(c->argv[1], 0, 1, &vals[0]);
1198         vals[1].bv_val = NULL; vals[1].bv_len = 0;
1199         if(value_add(&default_referral, vals)) return(LDAP_OTHER);
1200         return(0);
1201 }
1202
1203 int
1204 config_security(ConfigArgs *c) {
1205         slap_ssf_set_t *set = &c->be->be_ssf_set;
1206         char *next;
1207         int i;
1208         for(i = 1; i < c->argc; i++) {
1209                 slap_ssf_t *tgt;
1210                 char *src;
1211                 if(!strncasecmp(c->argv[i], "ssf=", 4)) {
1212                         tgt = &set->sss_ssf;
1213                         src = &c->argv[i][4];
1214                 } else if(!strncasecmp(c->argv[i], "transport=", 10)) {
1215                         tgt = &set->sss_transport;
1216                         src = &c->argv[i][10];
1217                 } else if(!strncasecmp(c->argv[i], "tls=", 4)) {
1218                         tgt = &set->sss_tls;
1219                         src = &c->argv[i][4];
1220                 } else if(!strncasecmp(c->argv[i], "sasl=", 5)) {
1221                         tgt = &set->sss_sasl;
1222                         src = &c->argv[i][5];
1223                 } else if(!strncasecmp(c->argv[i], "update_ssf=", 11)) {
1224                         tgt = &set->sss_update_ssf;
1225                         src = &c->argv[i][11];
1226                 } else if(!strncasecmp(c->argv[i], "update_transport=", 17)) {
1227                         tgt = &set->sss_update_transport;
1228                         src = &c->argv[i][17];
1229                 } else if(!strncasecmp(c->argv[i], "update_tls=", 11)) {
1230                         tgt = &set->sss_update_tls;
1231                         src = &c->argv[i][11];
1232                 } else if(!strncasecmp(c->argv[i], "update_sasl=", 12)) {
1233                         tgt = &set->sss_update_sasl;
1234                         src = &c->argv[i][12];
1235                 } else if(!strncasecmp(c->argv[i], "simple_bind=", 12)) {
1236                         tgt = &set->sss_simple_bind;
1237                         src = &c->argv[i][12];
1238                 } else {
1239                         Debug(LDAP_DEBUG_ANY, "%s: "
1240                                 "unknown factor %s in \"security <factors>\" line\n",
1241                                 c->log, c->argv[i], 0);
1242                         return(1);
1243                 }
1244
1245                 *tgt = strtol(src, &next, 10);
1246                 if(next == NULL || next[0] != '\0' ) {
1247                         Debug(LDAP_DEBUG_ANY, "%s: "
1248                                 "unable to parse factor \"%s\" in \"security <factors>\" line\n",
1249                                 c->log, c->argv[i], 0);
1250                         return(1);
1251                 }
1252         }
1253         return(0);
1254 }
1255
1256 int
1257 config_replica(ConfigArgs *c) {
1258         int i, nr = -1;
1259         char *replicahost, *replicalog = NULL;
1260         LDAPURLDesc *ludp;
1261
1262         if(SLAP_MONITOR(c->be)) {
1263                 Debug(LDAP_DEBUG_ANY, "%s: "
1264                         "\"replica\" should not be used inside monitor database\n",
1265                         c->log, 0, 0);
1266                 return(0);      /* FIXME: should this be an error? */
1267         }
1268
1269         for(i = 1; i < c->argc; i++) {
1270                 if(!strncasecmp(c->argv[i], "host=", STRLENOF("host="))) {
1271                         replicalog = c->argv[i] + STRLENOF("host=");
1272                         nr = add_replica_info(c->be, c->argv[i] + STRLENOF("host="));
1273                         break;
1274                 } else if(!strncasecmp(c->argv[i], "uri=", STRLENOF("uri="))) {
1275                         if(ldap_url_parse(c->argv[i] + STRLENOF("uri="), &ludp) != LDAP_SUCCESS) {
1276                                 Debug(LDAP_DEBUG_ANY, "%s: "
1277                                         "replica line contains invalid "
1278                                         "uri definition.\n", c->log, 0, 0);
1279                                 return(1);
1280                         }
1281                         if(!ludp->lud_host) {
1282                                 Debug(LDAP_DEBUG_ANY, "%s: "
1283                                         "replica line contains invalid "
1284                                         "uri definition - missing hostname.\n",
1285                                         c->log, 0, 0);
1286                                 return(1);
1287                         }
1288                         replicahost = ch_malloc(strlen(c->argv[i]));
1289                         if(!replicahost) {
1290                                 Debug(LDAP_DEBUG_ANY,
1291                                         "out of memory in read_config\n", 0, 0, 0);
1292                                 ldap_free_urldesc(ludp);
1293                                 exit(EXIT_FAILURE);
1294                         }
1295                         sprintf(replicahost, "%s:%d", ludp->lud_host, ludp->lud_port);
1296                         replicalog = c->argv[i] + STRLENOF("uri=");
1297                         nr = add_replica_info(c->be, replicahost);
1298                         ldap_free_urldesc(ludp);
1299                         ch_free(replicahost);
1300                         break;
1301                 }
1302         }
1303         if(i == c->argc) {
1304                 Debug(LDAP_DEBUG_ANY, "%s: "
1305                         "missing host or uri in \"replica\" line\n",
1306                         c->log, 0, 0);
1307                 return(1);
1308         } else if(nr == -1) {
1309                 Debug(LDAP_DEBUG_ANY, "%s: "
1310                         "unable to add replica \"%s\"\n",
1311                         c->log, replicalog, 0);
1312                 return(1);
1313         } else {
1314                 for(i = 1; i < c->argc; i++) {
1315                         if(!strncasecmp(c->argv[i], "suffix=", STRLENOF( "suffix="))) {
1316                                 switch(add_replica_suffix(c->be, nr, c->argv[i] + STRLENOF("suffix="))) {
1317                                         case 1:
1318                                                 Debug(LDAP_DEBUG_ANY, "%s: "
1319                                                 "suffix \"%s\" in \"replica\" line is not valid for backend (ignored)\n",
1320                                                 c->log, c->argv[i] + STRLENOF("suffix="), 0);
1321                                                 break;
1322                                         case 2:
1323                                                 Debug(LDAP_DEBUG_ANY, "%s: "
1324                                                 "unable to normalize suffix in \"replica\" line (ignored)\n",
1325                                                 c->log, 0, 0);
1326                                                 break;
1327                                 }
1328
1329                         } else if(!strncasecmp(c->argv[i], "attr", STRLENOF("attr"))) {
1330                                 int exclude = 0;
1331                                 char *arg = c->argv[i] + STRLENOF("attr");
1332                                 if(arg[0] == '!') {
1333                                         arg++;
1334                                         exclude = 1;
1335                                 }
1336                                 if(arg[0] != '=') {
1337                                         continue;
1338                                 }
1339                                 if(add_replica_attrs(c->be, nr, arg + 1, exclude)) {
1340                                         Debug(LDAP_DEBUG_ANY, "%s: "
1341                                                 "attribute \"%s\" in \"replica\" line is unknown\n",
1342                                                 c->log, arg + 1, 0);
1343                                         return(1);
1344                                 }
1345                         }
1346                 }
1347         }
1348         return(0);
1349 }
1350
1351 int
1352 config_updatedn(ConfigArgs *c) {
1353         struct berval dn;
1354         int rc;
1355         if(SLAP_SHADOW(c->be)) {
1356                 Debug(LDAP_DEBUG_ANY, "%s: "
1357                         "updatedn: database already shadowed.\n",
1358                         c->log, 0, 0);
1359                 return(1);
1360         }
1361
1362         ber_str2bv(c->argv[1], 0, 0, &dn);
1363
1364         rc = dnNormalize(0, NULL, NULL, &dn, &c->be->be_update_ndn, NULL);
1365
1366         if(rc != LDAP_SUCCESS) {
1367                 Debug(LDAP_DEBUG_ANY, "%s: "
1368                         "updatedn DN is invalid: %d (%s)\n",
1369                         c->log, rc, ldap_err2string( rc ));
1370                 return(1);
1371         }
1372
1373         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
1374         return(0);
1375 }
1376
1377 int
1378 config_updateref(ConfigArgs *c) {
1379         struct berval vals[2];
1380         if(!SLAP_SHADOW(c->be)) {
1381                 Debug(LDAP_DEBUG_ANY, "%s: "
1382                         "updateref line must after syncrepl or updatedn.\n",
1383                         c->log, 0, 0);
1384                 return(1);
1385         }
1386
1387         if(validate_global_referral(c->argv[1])) {
1388                 Debug(LDAP_DEBUG_ANY, "%s: "
1389                         "invalid URL (%s) in \"updateref\" line.\n",
1390                         c->log, c->argv[1], 0);
1391                 return(1);
1392         }
1393         ber_str2bv(c->argv[1], 0, 0, &vals[0]);
1394         vals[1].bv_val = NULL;
1395         if(value_add(&c->be->be_update_refs, vals)) return(LDAP_OTHER);
1396         return(0);
1397 }
1398
1399 /* XXX meaningless in ldif */
1400
1401 int
1402 config_include(ConfigArgs *c) {
1403         char *savefname = ch_strdup(c->argv[1]);
1404         unsigned long savelineno = c->lineno;
1405         int rc;
1406         rc = read_config_file(savefname, c->depth + 1, c);
1407         free(savefname);
1408         c->lineno = savelineno - 1;
1409         return(rc);
1410 }
1411
1412 #ifdef HAVE_TLS
1413 int
1414 config_tls_option(ConfigArgs *c) {
1415         int flag;
1416         switch(c->type) {
1417         case CFG_TLS_RAND:              flag = LDAP_OPT_X_TLS_RANDOM_FILE;      break;
1418         case CFG_TLS_CIPHER:            flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
1419         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
1420         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
1421         case CFG_TLS_CERT_PATH: flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
1422         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
1423 #ifdef HAVE_OPENSSL_CRL
1424         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK;         break;
1425 #endif
1426                 default:                Debug(LDAP_DEBUG_ANY, "%s: "
1427                                                 "unknown tls_option <%x>\n",
1428                                                 c->log, c->type, 0);
1429         }
1430         return(ldap_pvt_tls_set_option(NULL, flag, c->argv[1]));
1431 }
1432
1433 int
1434 config_tls_verify(ConfigArgs *c) {
1435         int i;
1436         if(isdigit((unsigned char)c->argv[1][0])) {
1437                 i = atoi(c->argv[1]);
1438                 return(ldap_pvt_tls_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &i));
1439         } else {
1440                 return(ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, c->argv[1]));
1441         }
1442 }
1443 #endif
1444
1445 /* -------------------------------------- */
1446
1447
1448 static char *
1449 strtok_quote( char *line, char *sep )
1450 {
1451         int             inquote;
1452         char            *tmp;
1453         static char     *next;
1454
1455         strtok_quote_ptr = NULL;
1456         if ( line != NULL ) {
1457                 next = line;
1458         }
1459         while ( *next && strchr( sep, *next ) ) {
1460                 next++;
1461         }
1462
1463         if ( *next == '\0' ) {
1464                 next = NULL;
1465                 return( NULL );
1466         }
1467         tmp = next;
1468
1469         for ( inquote = 0; *next; ) {
1470                 switch ( *next ) {
1471                 case '"':
1472                         if ( inquote ) {
1473                                 inquote = 0;
1474                         } else {
1475                                 inquote = 1;
1476                         }
1477                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1478                         break;
1479
1480                 case '\\':
1481                         if ( next[1] )
1482                                 AC_MEMCPY( next,
1483                                             next + 1, strlen( next + 1 ) + 1 );
1484                         next++;         /* dont parse the escaped character */
1485                         break;
1486
1487                 default:
1488                         if ( ! inquote ) {
1489                                 if ( strchr( sep, *next ) != NULL ) {
1490                                         strtok_quote_ptr = next;
1491                                         *next++ = '\0';
1492                                         return( tmp );
1493                                 }
1494                         }
1495                         next++;
1496                         break;
1497                 }
1498         }
1499
1500         return( tmp );
1501 }
1502
1503 static char     buf[BUFSIZ];
1504 static char     *line;
1505 static size_t lmax, lcur;
1506
1507 #define CATLINE( buf ) \
1508         do { \
1509                 size_t len = strlen( buf ); \
1510                 while ( lcur + len + 1 > lmax ) { \
1511                         lmax += BUFSIZ; \
1512                         line = (char *) ch_realloc( line, lmax ); \
1513                 } \
1514                 strcpy( line + lcur, buf ); \
1515                 lcur += len; \
1516         } while( 0 )
1517
1518 static void
1519 fp_getline_init(ConfigArgs *c) {
1520         c->lineno = -1;
1521         buf[0] = '\0';
1522 }
1523
1524 static int
1525 fp_getline( FILE *fp, ConfigArgs *c )
1526 {
1527         char    *p;
1528
1529         lcur = 0;
1530         CATLINE(buf);
1531         c->lineno++;
1532
1533         /* avoid stack of bufs */
1534         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1535                 buf[0] = '\0';
1536                 c->line = line;
1537                 return(1);
1538         }
1539
1540         while ( fgets( buf, sizeof( buf ), fp ) ) {
1541                 p = strchr( buf, '\n' );
1542                 if ( p ) {
1543                         if ( p > buf && p[-1] == '\r' ) {
1544                                 --p;
1545                         }
1546                         *p = '\0';
1547                 }
1548                 /* XXX ugly */
1549                 c->line = line;
1550                 if ( line[0]
1551                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1552                                 && p[-1] != '\\' )
1553                 {
1554                         p[0] = '\0';
1555                         lcur--;
1556                         
1557                 } else {
1558                         if ( !isspace( (unsigned char)buf[0] ) ) {
1559                                 return(1);
1560                         }
1561                         buf[0] = ' ';
1562                 }
1563                 CATLINE(buf);
1564                 c->lineno++;
1565         }
1566
1567         buf[0] = '\0';
1568         c->line = line;
1569         return(line[0] ? 1 : 0);
1570 }
1571
1572 static int
1573 fp_parse_line(ConfigArgs *c)
1574 {
1575         char *token;
1576         char *tline = ch_strdup(c->line);
1577         char *hide[] = { "rootpw", "replica", "bindpw", "pseudorootpw", "dbpasswd", '\0' };
1578         int i;
1579
1580         c->argc = 0;
1581         token = strtok_quote(tline, " \t");
1582
1583         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1584         if(strtok_quote_ptr) *strtok_quote_ptr = ' ';
1585         Debug(LDAP_DEBUG_CONFIG, "line %lu (%s%s)\n", c->lineno, hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1586         if(strtok_quote_ptr) *strtok_quote_ptr = '\0';
1587
1588         for(; token; token = strtok_quote(NULL, " \t")) {
1589                 if(c->argc == c->argv_size - 1) {
1590                         char **tmp;
1591                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1592                         if(!tmp) {
1593                                 Debug(LDAP_DEBUG_ANY, "line %lu: out of memory\n", c->lineno, 0, 0);
1594                                 return -1;
1595                         }
1596                         c->argv = tmp;
1597                         c->argv_size += ARGS_STEP;
1598                 }
1599                 c->argv[c->argc++] = token;
1600         }
1601         c->argv[c->argc] = NULL;
1602         return(0);
1603 }
1604
1605
1606 #if 0
1607 /* Loads ucdata, returns 1 if loading, 0 if already loaded, -1 on error */
1608 static int
1609 load_ucdata( char *path )
1610 {
1611 #if 0
1612         static int loaded = 0;
1613         int err;
1614         
1615         if ( loaded ) {
1616                 return( 0 );
1617         }
1618         err = ucdata_load( path ? path : SLAPD_DEFAULT_UCDATA, UCDATA_ALL );
1619         if ( err ) {
1620                 Debug( LDAP_DEBUG_ANY, "error loading ucdata (error %d)\n",
1621                        err, 0, 0 );
1622
1623                 return( -1 );
1624         }
1625         loaded = 1;
1626         return( 1 );
1627 #else
1628         /* ucdata is now hardcoded */
1629         return( 0 );
1630 #endif
1631 }
1632 #endif
1633
1634 void
1635 config_destroy( )
1636 {
1637         ucdata_unload( UCDATA_ALL );
1638         if ( frontendDB ) {
1639                 /* NOTE: in case of early exit, frontendDB can be NULL */
1640                 if ( frontendDB->be_schemandn.bv_val )
1641                         free( frontendDB->be_schemandn.bv_val );
1642                 if ( frontendDB->be_schemadn.bv_val )
1643                         free( frontendDB->be_schemadn.bv_val );
1644                 if ( frontendDB->be_acl )
1645                         acl_destroy( frontendDB->be_acl, NULL );
1646         }
1647         free( line );
1648         if ( slapd_args_file )
1649                 free ( slapd_args_file );
1650         if ( slapd_pid_file )
1651                 free ( slapd_pid_file );
1652         if ( default_passwd_hash )
1653                 ldap_charray_free( default_passwd_hash );
1654 }
1655
1656 static int
1657 add_syncrepl(
1658         Backend *be,
1659         char    **cargv,
1660         int     cargc
1661 )
1662 {
1663         syncinfo_t *si;
1664         int     rc = 0;
1665
1666         si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
1667
1668         if ( si == NULL ) {
1669                 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
1670                 return 1;
1671         }
1672
1673         si->si_tls = SYNCINFO_TLS_OFF;
1674         si->si_bindmethod = LDAP_AUTH_SIMPLE;
1675         si->si_schemachecking = 0;
1676         ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
1677                 &si->si_filterstr );
1678         si->si_base.bv_val = NULL;
1679         si->si_scope = LDAP_SCOPE_SUBTREE;
1680         si->si_attrsonly = 0;
1681         si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
1682         si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
1683         si->si_attrs = NULL;
1684         si->si_allattrs = 0;
1685         si->si_allopattrs = 0;
1686         si->si_exattrs = NULL;
1687         si->si_type = LDAP_SYNC_REFRESH_ONLY;
1688         si->si_interval = 86400;
1689         si->si_retryinterval = NULL;
1690         si->si_retrynum_init = NULL;
1691         si->si_retrynum = NULL;
1692         si->si_manageDSAit = 0;
1693         si->si_tlimit = 0;
1694         si->si_slimit = 0;
1695
1696         si->si_presentlist = NULL;
1697         LDAP_LIST_INIT( &si->si_nonpresentlist );
1698         ldap_pvt_thread_mutex_init( &si->si_mutex );
1699
1700         rc = parse_syncrepl_line( cargv, cargc, si );
1701
1702         if ( rc < 0 ) {
1703                 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
1704                 syncinfo_free( si );    
1705                 return 1;
1706         } else {
1707                 Debug( LDAP_DEBUG_CONFIG,
1708                         "Config: ** successfully added syncrepl \"%s\"\n",
1709                         BER_BVISNULL( &si->si_provideruri ) ?
1710                         "(null)" : si->si_provideruri.bv_val, 0, 0 );
1711                 if ( !si->si_schemachecking ) {
1712                         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1713                 }
1714                 si->si_be = be;
1715                 be->be_syncinfo = si;
1716                 return 0;
1717         }
1718 }
1719
1720 /* NOTE: used & documented in slapd.conf(5) */
1721 #define IDSTR                   "rid"
1722 #define PROVIDERSTR             "provider"
1723 #define TYPESTR                 "type"
1724 #define INTERVALSTR             "interval"
1725 #define SEARCHBASESTR           "searchbase"
1726 #define FILTERSTR               "filter"
1727 #define SCOPESTR                "scope"
1728 #define ATTRSSTR                "attrs"
1729 #define ATTRSONLYSTR            "attrsonly"
1730 #define SLIMITSTR               "sizelimit"
1731 #define TLIMITSTR               "timelimit"
1732 #define SCHEMASTR               "schemachecking"
1733 #define BINDMETHSTR             "bindmethod"
1734 #define SIMPLESTR                       "simple"
1735 #define SASLSTR                         "sasl"
1736 #define BINDDNSTR               "binddn"
1737 #define SASLMECHSTR             "saslmech"
1738 #define AUTHCSTR                "authcID"
1739 #define AUTHZSTR                "authzID"
1740 #define CREDSTR                 "credentials"
1741 #define REALMSTR                "realm"
1742 #define SECPROPSSTR             "secprops"
1743
1744 /* FIXME: undocumented */
1745 #define OLDAUTHCSTR             "bindprincipal"
1746 #define STARTTLSSTR             "starttls"
1747 #define CRITICALSTR                     "critical"
1748 #define EXATTRSSTR              "exattrs"
1749 #define MANAGEDSAITSTR          "manageDSAit"
1750 #define RETRYSTR                "retry"
1751
1752 /* FIXME: unused */
1753 #define LASTMODSTR              "lastmod"
1754 #define LMGENSTR                "gen"
1755 #define LMNOSTR                 "no"
1756 #define LMREQSTR                "req"
1757 #define SRVTABSTR               "srvtab"
1758 #define SUFFIXSTR               "suffix"
1759 #define UPDATEDNSTR             "updatedn"
1760
1761 /* mandatory */
1762 #define GOT_ID                  0x0001
1763 #define GOT_PROVIDER            0x0002
1764 #define GOT_METHOD              0x0004
1765
1766 /* check */
1767 #define GOT_ALL                 (GOT_ID|GOT_PROVIDER|GOT_METHOD)
1768
1769 static int
1770 parse_syncrepl_line(
1771         char            **cargv,
1772         int             cargc,
1773         syncinfo_t      *si
1774 )
1775 {
1776         int     gots = 0;
1777         int     i;
1778         char    *val;
1779
1780         for ( i = 1; i < cargc; i++ ) {
1781                 if ( !strncasecmp( cargv[ i ], IDSTR "=",
1782                                         STRLENOF( IDSTR "=" ) ) )
1783                 {
1784                         int tmp;
1785                         /* '\0' string terminator accounts for '=' */
1786                         val = cargv[ i ] + STRLENOF( IDSTR "=" );
1787                         tmp= atoi( val );
1788                         if ( tmp >= 1000 || tmp < 0 ) {
1789                                 fprintf( stderr, "Error: parse_syncrepl_line: "
1790                                          "syncrepl id %d is out of range [0..999]\n", tmp );
1791                                 return -1;
1792                         }
1793                         si->si_rid = tmp;
1794                         gots |= GOT_ID;
1795                 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
1796                                         STRLENOF( PROVIDERSTR "=" ) ) )
1797                 {
1798                         val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
1799                         ber_str2bv( val, 0, 1, &si->si_provideruri );
1800                         gots |= GOT_PROVIDER;
1801                 } else if ( !strncasecmp( cargv[ i ], STARTTLSSTR "=",
1802                                         STRLENOF(STARTTLSSTR "=") ) )
1803                 {
1804                         val = cargv[ i ] + STRLENOF( STARTTLSSTR "=" );
1805                         if( !strcasecmp( val, CRITICALSTR ) ) {
1806                                 si->si_tls = SYNCINFO_TLS_CRITICAL;
1807                         } else {
1808                                 si->si_tls = SYNCINFO_TLS_ON;
1809                         }
1810                 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR "=",
1811                                 STRLENOF( BINDMETHSTR "=" ) ) )
1812                 {
1813                         val = cargv[ i ] + STRLENOF( BINDMETHSTR "=" );
1814                         if ( !strcasecmp( val, SIMPLESTR )) {
1815                                 si->si_bindmethod = LDAP_AUTH_SIMPLE;
1816                                 gots |= GOT_METHOD;
1817                         } else if ( !strcasecmp( val, SASLSTR )) {
1818 #ifdef HAVE_CYRUS_SASL
1819                                 si->si_bindmethod = LDAP_AUTH_SASL;
1820                                 gots |= GOT_METHOD;
1821 #else /* HAVE_CYRUS_SASL */
1822                                 fprintf( stderr, "Error: parse_syncrepl_line: "
1823                                         "not compiled with SASL support\n" );
1824                                 return -1;
1825 #endif /* HAVE_CYRUS_SASL */
1826                         } else {
1827                                 si->si_bindmethod = -1;
1828                         }
1829                 } else if ( !strncasecmp( cargv[ i ], BINDDNSTR "=",
1830                                         STRLENOF( BINDDNSTR "=" ) ) )
1831                 {
1832                         val = cargv[ i ] + STRLENOF( BINDDNSTR "=" );
1833                         si->si_binddn = ch_strdup( val );
1834                 } else if ( !strncasecmp( cargv[ i ], CREDSTR "=",
1835                                         STRLENOF( CREDSTR "=" ) ) )
1836                 {
1837                         val = cargv[ i ] + STRLENOF( CREDSTR "=" );
1838                         si->si_passwd = ch_strdup( val );
1839                 } else if ( !strncasecmp( cargv[ i ], SASLMECHSTR "=",
1840                                         STRLENOF( SASLMECHSTR "=" ) ) )
1841                 {
1842                         val = cargv[ i ] + STRLENOF( SASLMECHSTR "=" );
1843                         si->si_saslmech = ch_strdup( val );
1844                 } else if ( !strncasecmp( cargv[ i ], SECPROPSSTR "=",
1845                                         STRLENOF( SECPROPSSTR "=" ) ) )
1846                 {
1847                         val = cargv[ i ] + STRLENOF( SECPROPSSTR "=" );
1848                         si->si_secprops = ch_strdup( val );
1849                 } else if ( !strncasecmp( cargv[ i ], REALMSTR "=",
1850                                         STRLENOF( REALMSTR "=" ) ) )
1851                 {
1852                         val = cargv[ i ] + STRLENOF( REALMSTR "=" );
1853                         si->si_realm = ch_strdup( val );
1854                 } else if ( !strncasecmp( cargv[ i ], AUTHCSTR "=",
1855                                         STRLENOF( AUTHCSTR "=" ) ) )
1856                 {
1857                         val = cargv[ i ] + STRLENOF( AUTHCSTR "=" );
1858                         if ( si->si_authcId )
1859                                 ch_free( si->si_authcId );
1860                         si->si_authcId = ch_strdup( val );
1861                 } else if ( !strncasecmp( cargv[ i ], OLDAUTHCSTR "=",
1862                                         STRLENOF( OLDAUTHCSTR "=" ) ) ) 
1863                 {
1864                         /* Old authcID is provided for some backwards compatibility */
1865                         val = cargv[ i ] + STRLENOF( OLDAUTHCSTR "=" );
1866                         if ( si->si_authcId )
1867                                 ch_free( si->si_authcId );
1868                         si->si_authcId = ch_strdup( val );
1869                 } else if ( !strncasecmp( cargv[ i ], AUTHZSTR "=",
1870                                         STRLENOF( AUTHZSTR "=" ) ) )
1871                 {
1872                         val = cargv[ i ] + STRLENOF( AUTHZSTR "=" );
1873                         si->si_authzId = ch_strdup( val );
1874                 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
1875                                         STRLENOF( SCHEMASTR "=" ) ) )
1876                 {
1877                         val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
1878                         if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
1879                                 si->si_schemachecking = 1;
1880                         } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
1881                                 si->si_schemachecking = 0;
1882                         } else {
1883                                 si->si_schemachecking = 1;
1884                         }
1885                 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
1886                                         STRLENOF( FILTERSTR "=" ) ) )
1887                 {
1888                         val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
1889                         ber_str2bv( val, 0, 1, &si->si_filterstr );
1890                 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
1891                                         STRLENOF( SEARCHBASESTR "=" ) ) )
1892                 {
1893                         struct berval   bv;
1894                         int             rc;
1895
1896                         val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
1897                         if ( si->si_base.bv_val ) {
1898                                 ch_free( si->si_base.bv_val );
1899                         }
1900                         ber_str2bv( val, 0, 0, &bv );
1901                         rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
1902                         if ( rc != LDAP_SUCCESS ) {
1903                                 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
1904                                         val, rc, ldap_err2string( rc ) );
1905                                 return -1;
1906                         }
1907                 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
1908                                         STRLENOF( SCOPESTR "=" ) ) )
1909                 {
1910                         val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
1911                         if ( !strncasecmp( val, "base", STRLENOF( "base" ) )) {
1912                                 si->si_scope = LDAP_SCOPE_BASE;
1913                         } else if ( !strncasecmp( val, "one", STRLENOF( "one" ) )) {
1914                                 si->si_scope = LDAP_SCOPE_ONELEVEL;
1915 #ifdef LDAP_SCOPE_SUBORDINATE
1916                         } else if ( !strcasecmp( val, "subordinate" ) ||
1917                                 !strcasecmp( val, "children" ))
1918                         {
1919                                 si->si_scope = LDAP_SCOPE_SUBORDINATE;
1920 #endif
1921                         } else if ( !strncasecmp( val, "sub", STRLENOF( "sub" ) )) {
1922                                 si->si_scope = LDAP_SCOPE_SUBTREE;
1923                         } else {
1924                                 fprintf( stderr, "Error: parse_syncrepl_line: "
1925                                         "unknown scope \"%s\"\n", val);
1926                                 return -1;
1927                         }
1928                 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
1929                                         STRLENOF( ATTRSONLYSTR "=" ) ) )
1930                 {
1931                         si->si_attrsonly = 1;
1932                 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
1933                                         STRLENOF( ATTRSSTR "=" ) ) )
1934                 {
1935                         val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
1936                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
1937                                 char *attr_fname;
1938                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
1939                                 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
1940                                 if ( si->si_anlist == NULL ) {
1941                                         ch_free( attr_fname );
1942                                         return -1;
1943                                 }
1944                                 ch_free( attr_fname );
1945                         } else {
1946                                 char *str, *s, *next;
1947                                 char delimstr[] = " ,\t";
1948                                 str = ch_strdup( val );
1949                                 for ( s = ldap_pvt_strtok( str, delimstr, &next );
1950                                                 s != NULL;
1951                                                 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
1952                                 {
1953                                         if ( strlen(s) == 1 && *s == '*' ) {
1954                                                 si->si_allattrs = 1;
1955                                                 *(val + ( s - str )) = delimstr[0];
1956                                         }
1957                                         if ( strlen(s) == 1 && *s == '+' ) {
1958                                                 si->si_allopattrs = 1;
1959                                                 *(val + ( s - str )) = delimstr[0];
1960                                         }
1961                                 }
1962                                 ch_free( str );
1963                                 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
1964                                 if ( si->si_anlist == NULL ) {
1965                                         return -1;
1966                                 }
1967                         }
1968                 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
1969                                         STRLENOF( EXATTRSSTR "=" ) ) )
1970                 {
1971                         val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
1972                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
1973                                 char *attr_fname;
1974                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
1975                                 si->si_exanlist = file2anlist(
1976                                                                         si->si_exanlist, attr_fname, " ,\t" );
1977                                 if ( si->si_exanlist == NULL ) {
1978                                         ch_free( attr_fname );
1979                                         return -1;
1980                                 }
1981                                 ch_free( attr_fname );
1982                         } else {
1983                                 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
1984                                 if ( si->si_exanlist == NULL ) {
1985                                         return -1;
1986                                 }
1987                         }
1988                 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
1989                                         STRLENOF( TYPESTR "=" ) ) )
1990                 {
1991                         val = cargv[ i ] + STRLENOF( TYPESTR "=" );
1992                         if ( !strncasecmp( val, "refreshOnly",
1993                                                 STRLENOF("refreshOnly") ))
1994                         {
1995                                 si->si_type = LDAP_SYNC_REFRESH_ONLY;
1996                         } else if ( !strncasecmp( val, "refreshAndPersist",
1997                                                 STRLENOF("refreshAndPersist") ))
1998                         {
1999                                 si->si_type = LDAP_SYNC_REFRESH_AND_PERSIST;
2000                                 si->si_interval = 60;
2001                         } else {
2002                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2003                                         "unknown sync type \"%s\"\n", val);
2004                                 return -1;
2005                         }
2006                 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2007                                         STRLENOF( INTERVALSTR "=" ) ) )
2008                 {
2009                         val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2010                         if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2011                                 si->si_interval = 0;
2012                         } else {
2013                                 char *hstr;
2014                                 char *mstr;
2015                                 char *dstr;
2016                                 char *sstr;
2017                                 int dd, hh, mm, ss;
2018                                 dstr = val;
2019                                 hstr = strchr( dstr, ':' );
2020                                 if ( hstr == NULL ) {
2021                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2022                                                 "invalid interval \"%s\"\n", val );
2023                                         return -1;
2024                                 }
2025                                 *hstr++ = '\0';
2026                                 mstr = strchr( hstr, ':' );
2027                                 if ( mstr == NULL ) {
2028                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2029                                                 "invalid interval \"%s\"\n", val );
2030                                         return -1;
2031                                 }
2032                                 *mstr++ = '\0';
2033                                 sstr = strchr( mstr, ':' );
2034                                 if ( sstr == NULL ) {
2035                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2036                                                 "invalid interval \"%s\"\n", val );
2037                                         return -1;
2038                                 }
2039                                 *sstr++ = '\0';
2040
2041                                 dd = atoi( dstr );
2042                                 hh = atoi( hstr );
2043                                 mm = atoi( mstr );
2044                                 ss = atoi( sstr );
2045                                 if (( hh > 24 ) || ( hh < 0 ) ||
2046                                         ( mm > 60 ) || ( mm < 0 ) ||
2047                                         ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2048                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2049                                                 "invalid interval \"%s\"\n", val );
2050                                         return -1;
2051                                 }
2052                                 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2053                         }
2054                         if ( si->si_interval < 0 ) {
2055                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2056                                         "invalid interval \"%ld\"\n",
2057                                         (long) si->si_interval);
2058                                 return -1;
2059                         }
2060                 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2061                                         STRLENOF( RETRYSTR "=" ) ) )
2062                 {
2063                         char **retry_list;
2064                         int j, k, n;
2065
2066                         val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2067                         retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2068                         retry_list[0] = NULL;
2069
2070                         slap_str2clist( &retry_list, val, " ,\t" );
2071
2072                         for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2073                         n = k / 2;
2074                         if ( k % 2 ) {
2075                                 fprintf( stderr,
2076                                                 "Error: incomplete syncrepl retry list\n" );
2077                                 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2078                                         ch_free( retry_list[k] );
2079                                 }
2080                                 ch_free( retry_list );
2081                                 exit( EXIT_FAILURE );
2082                         }
2083                         si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2084                         si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2085                         si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2086                         for ( j = 0; j < n; j++ ) {
2087                                 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2088                                 if ( *retry_list[j*2+1] == '+' ) {
2089                                         si->si_retrynum_init[j] = -1;
2090                                         si->si_retrynum[j] = -1;
2091                                         j++;
2092                                         break;
2093                                 } else {
2094                                         si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2095                                         si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2096                                 }
2097                         }
2098                         si->si_retrynum_init[j] = -2;
2099                         si->si_retrynum[j] = -2;
2100                         si->si_retryinterval[j] = 0;
2101                         
2102                         for ( k = 0; retry_list && retry_list[k]; k++ ) {
2103                                 ch_free( retry_list[k] );
2104                         }
2105                         ch_free( retry_list );
2106                 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
2107                                         STRLENOF( MANAGEDSAITSTR "=" ) ) )
2108                 {
2109                         val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
2110                         si->si_manageDSAit = atoi( val );
2111                 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
2112                                         STRLENOF( SLIMITSTR "=") ) )
2113                 {
2114                         val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
2115                         si->si_slimit = atoi( val );
2116                 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
2117                                         STRLENOF( TLIMITSTR "=" ) ) )
2118                 {
2119                         val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
2120                         si->si_tlimit = atoi( val );
2121                 } else {
2122                         fprintf( stderr, "Error: parse_syncrepl_line: "
2123                                 "unknown keyword \"%s\"\n", cargv[ i ] );
2124                         return -1;
2125                 }
2126         }
2127
2128         if ( gots != GOT_ALL ) {
2129                 fprintf( stderr,
2130                         "Error: Malformed \"syncrepl\" line in slapd config file" );
2131                 return -1;
2132         }
2133
2134         return 0;
2135 }
2136
2137 char **
2138 slap_str2clist( char ***out, char *in, const char *brkstr )
2139 {
2140         char    *str;
2141         char    *s;
2142         char    *lasts;
2143         int     i, j;
2144         char    **new;
2145
2146         /* find last element in list */
2147         for (i = 0; *out && (*out)[i]; i++);
2148
2149         /* protect the input string from strtok */
2150         str = ch_strdup( in );
2151
2152         if ( *str == '\0' ) {
2153                 free( str );
2154                 return( *out );
2155         }
2156
2157         /* Count words in string */
2158         j=1;
2159         for ( s = str; *s; s++ ) {
2160                 if ( strchr( brkstr, *s ) != NULL ) {
2161                         j++;
2162                 }
2163         }
2164
2165         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
2166         new = *out + i;
2167         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2168                 s != NULL;
2169                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2170         {
2171                 *new = ch_strdup( s );
2172                 new++;
2173         }
2174
2175         *new = NULL;
2176         free( str );
2177         return( *out );
2178 }