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