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