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