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