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