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