]> git.sur5r.net Git - openldap/blob - servers/slapd/bconfig.c
58e6dd6b00807efdfae9a59a6e1330bc38790581
[openldap] / servers / slapd / bconfig.c
1 /* bconfig.c - the config backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2005 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 /* ACKNOWLEDGEMENTS:
17  * This work was originally developed by Howard Chu for inclusion
18  * in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <ac/string.h>
25 #include <ac/ctype.h>
26 #include <ac/errno.h>
27
28 #include "slap.h"
29
30 #ifdef LDAP_SLAPI
31 #include "slapi/slapi.h"
32 #endif
33
34 #include <lutil.h>
35
36 #include "config.h"
37
38 static struct berval config_rdn = BER_BVC("cn=config");
39 static struct berval schema_rdn = BER_BVC("cn=schema");
40
41 #ifdef SLAPD_MODULES
42 typedef struct modpath_s {
43         struct modpath_s *mp_next;
44         struct berval mp_path;
45         BerVarray mp_loads;
46 } ModPaths;
47
48 static ModPaths modpaths, *modlast = &modpaths, *modcur = &modpaths;
49 #endif
50
51 typedef struct ConfigFile {
52         struct ConfigFile *c_sibs;
53         struct ConfigFile *c_kids;
54         struct berval c_file;
55         AttributeType *c_at_head, *c_at_tail;
56         ContentRule *c_cr_head, *c_cr_tail;
57         ObjectClass *c_oc_head, *c_oc_tail;
58         OidMacro *c_om_head, *c_om_tail;
59         BerVarray c_dseFiles;
60 } ConfigFile;
61
62 typedef struct CfOcInfo {
63         struct berval *co_name;
64         ConfigTable *co_table;
65         ConfigType co_type;
66 } CfOcInfo;
67
68 typedef struct CfEntryInfo {
69         struct CfEntryInfo *ce_parent;
70         struct CfEntryInfo *ce_sibs;
71         struct CfEntryInfo *ce_kids;
72         Entry *ce_entry;
73         ConfigType ce_type;
74         BackendInfo *ce_bi;
75         BackendDB *ce_be;
76 } CfEntryInfo;
77
78 typedef struct {
79         ConfigFile *cb_config;
80         CfEntryInfo *cb_root;
81         BackendDB       *cb_be; /* config backend */
82         BackendDB       cb_db;  /* underlying database */
83         int             cb_got_ldif;
84 } CfBackInfo;
85
86 /* These do nothing in slapd, they're kept only to make them
87  * editable here.
88  */
89 static char *replica_pidFile, *replica_argsFile;
90 static int replicationInterval;
91
92 static char     *passwd_salt;
93 static char     *logfileName;
94 static BerVarray authz_rewrites;
95
96 static struct berval cfdir;
97
98 /* Private state */
99 static AttributeDescription *cfAd_backend, *cfAd_database, *cfAd_overlay,
100         *cfAd_include;
101
102 static ObjectClass *cfOc_schema, *cfOc_global, *cfOc_backend, *cfOc_database,
103         *cfOc_include, *cfOc_overlay, *cfOc_module;
104
105 static ConfigFile cf_prv, *cfn = &cf_prv;
106
107 static Avlnode *CfOcTree;
108
109 static int add_syncrepl LDAP_P(( Backend *, char **, int ));
110 static int parse_syncrepl_line LDAP_P(( char **, int, syncinfo_t *));
111 static void syncrepl_unparse LDAP_P (( syncinfo_t *, struct berval *));
112 static int config_add_internal( CfBackInfo *cfb, Entry *e, SlapReply *rs );
113
114 static ConfigDriver config_fname;
115 static ConfigDriver config_cfdir;
116 static ConfigDriver config_generic;
117 static ConfigDriver config_search_base;
118 static ConfigDriver config_passwd_hash;
119 static ConfigDriver config_schema_dn;
120 static ConfigDriver config_sizelimit;
121 static ConfigDriver config_timelimit;
122 static ConfigDriver config_limits; 
123 static ConfigDriver config_overlay;
124 static ConfigDriver config_suffix; 
125 static ConfigDriver config_deref_depth;
126 static ConfigDriver config_rootdn;
127 static ConfigDriver config_rootpw;
128 static ConfigDriver config_restrict;
129 static ConfigDriver config_allows;
130 static ConfigDriver config_disallows;
131 static ConfigDriver config_requires;
132 static ConfigDriver config_security;
133 static ConfigDriver config_referral;
134 static ConfigDriver config_loglevel;
135 static ConfigDriver config_syncrepl;
136 static ConfigDriver config_replica;
137 static ConfigDriver config_updatedn;
138 static ConfigDriver config_updateref;
139 static ConfigDriver config_include;
140 #ifdef HAVE_TLS
141 static ConfigDriver config_tls_option;
142 static ConfigDriver config_tls_config;
143 #endif
144
145 enum {
146         CFG_ACL = 1,
147         CFG_BACKEND,
148         CFG_DATABASE,
149         CFG_TLS_RAND,
150         CFG_TLS_CIPHER,
151         CFG_TLS_CERT_FILE,
152         CFG_TLS_CERT_KEY,
153         CFG_TLS_CA_PATH,
154         CFG_TLS_CA_FILE,
155         CFG_TLS_VERIFY,
156         CFG_TLS_CRLCHECK,
157         CFG_SIZE,
158         CFG_TIME,
159         CFG_CONCUR,
160         CFG_THREADS,
161         CFG_SALT,
162         CFG_LIMITS,
163         CFG_RO,
164         CFG_REWRITE,
165         CFG_DEPTH,
166         CFG_OID,
167         CFG_OC,
168         CFG_DIT,
169         CFG_ATTR,
170         CFG_ATOPT,
171         CFG_CHECK,
172         CFG_AUDITLOG,
173         CFG_REPLOG,
174         CFG_ROOTDSE,
175         CFG_LOGFILE,
176         CFG_PLUGIN,
177         CFG_MODLOAD,
178         CFG_MODPATH,
179         CFG_LASTMOD,
180         CFG_AZPOLICY,
181         CFG_AZREGEXP,
182         CFG_SASLSECP,
183         CFG_SSTR_IF_MAX,
184         CFG_SSTR_IF_MIN,
185 };
186
187 typedef struct {
188         char *name, *oid;
189 } OidRec;
190
191 static OidRec OidMacros[] = {
192         /* OpenLDAProot:666.11.1 */
193         { "OLcfg", "1.3.6.1.4.1.4203.666.11.1" },
194         { "OLcfgAt", "OLcfg:3" },
195         { "OLcfgOc", "OLcfg:4" },
196         { "OMsyn", "1.3.6.1.4.1.1466.115.121.1" },
197         { "OMsInteger", "OMsyn:2" },
198         { "OMsBoolean", "OMsyn:7" },
199         { "OMsDN", "OMsyn:12" },
200         { "OMsDirectoryString", "OMsyn:15" },
201         { "OMsOctetString", "OMsyn:40" },
202         { NULL, NULL }
203 };
204
205 /* alphabetical ordering */
206
207 ConfigTable config_back_cf_table[] = {
208         /* This attr is read-only */
209         { "", "", 0, 0, 0, ARG_MAGIC,
210                 &config_fname, "( OLcfgAt:78 NAME 'olcConfigFile' "
211                         "DESC 'File for slapd configuration directives' "
212                         "EQUALITY caseIgnoreMatch "
213                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
214         { "", "", 0, 0, 0, ARG_MAGIC,
215                 &config_cfdir, "( OLcfgAt:79 NAME 'olcConfigDir' "
216                         "DESC 'Directory for slapd configuration backend' "
217                         "EQUALITY caseIgnoreMatch "
218                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
219         { "access",     NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_ACL,
220                 &config_generic, "( OLcfgAt:1 NAME 'olcAccess' "
221                         "DESC 'Access Control List' "
222                         "EQUALITY caseIgnoreMatch "
223                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
224         { "allows",     "features", 2, 0, 5, ARG_PRE_DB|ARG_MAGIC,
225                 &config_allows, "( OLcfgAt:2 NAME 'olcAllows' "
226                         "DESC 'Allowed set of deprecated features' "
227                         "EQUALITY caseIgnoreMatch "
228                         "SYNTAX OMsDirectoryString )", NULL, NULL },
229         { "argsfile", "file", 2, 2, 0, ARG_STRING,
230                 &slapd_args_file, "( OLcfgAt:3 NAME 'olcArgsFile' "
231                         "DESC 'File for slapd command line options' "
232                         "EQUALITY caseIgnoreMatch "
233                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
234         { "attribute",  "attribute", 2, 0, 9, ARG_PAREN|ARG_MAGIC|CFG_ATTR,
235                 &config_generic, "( OLcfgAt:4 NAME 'olcAttributeTypes' "
236                         "DESC 'OpenLDAP attributeTypes' "
237                         "EQUALITY objectIdentifierFirstComponentMatch "
238                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.3 X-ORDERED 'VALUES' )",
239                                 NULL, NULL },
240         { "attributeoptions", NULL, 0, 0, 0, ARG_MAGIC|CFG_ATOPT,
241                 &config_generic, "( OLcfgAt:5 NAME 'olcAttributeOptions' "
242                         "EQUALITY caseIgnoreMatch "
243                         "SYNTAX OMsDirectoryString )", NULL, NULL },
244         { "authid-rewrite", NULL, 2, 0, 0,
245 #ifdef SLAP_AUTH_REWRITE
246                 ARG_MAGIC|CFG_REWRITE, &config_generic,
247 #else
248                 ARG_IGNORED, NULL,
249 #endif
250                  "( OLcfgAt:6 NAME 'olcAuthIDRewrite' "
251                         "EQUALITY caseIgnoreMatch "
252                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
253         { "authz-policy", "policy", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_AZPOLICY,
254                 &config_generic, "( OLcfgAt:7 NAME 'olcAuthzPolicy' "
255                         "EQUALITY caseIgnoreMatch "
256                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
257         { "authz-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
258                 &config_generic, "( OLcfgAt:8 NAME 'olcAuthzRegexp' "
259                         "EQUALITY caseIgnoreMatch "
260                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
261         { "backend", "type", 2, 2, 0, ARG_PRE_DB|ARG_MAGIC|CFG_BACKEND,
262                 &config_generic, "( OLcfgAt:9 NAME 'olcBackend' "
263                         "DESC 'A type of backend' "
264                         "EQUALITY caseIgnoreMatch "
265                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
266         { "concurrency", "level", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_CONCUR,
267                 &config_generic, "( OLcfgAt:10 NAME 'olcConcurrency' "
268                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
269         { "conn_max_pending", "max", 2, 2, 0, ARG_INT,
270                 &slap_conn_max_pending, "( OLcfgAt:11 NAME 'olcConnMaxPending' "
271                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
272         { "conn_max_pending_auth", "max", 2, 2, 0, ARG_INT,
273                 &slap_conn_max_pending_auth, "( OLcfgAt:12 NAME 'olcConnMaxPendingAuth' "
274                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
275         { "database", "type", 2, 2, 0, ARG_MAGIC|CFG_DATABASE,
276                 &config_generic, "( OLcfgAt:13 NAME 'olcDatabase' "
277                         "DESC 'The backend type for a database instance' "
278                         "SUP olcBackend X-ORDERED 'VALUES' )", NULL, NULL },
279         { "defaultSearchBase", "dn", 2, 2, 0, ARG_PRE_BI|ARG_PRE_DB|ARG_DN|ARG_MAGIC,
280                 &config_search_base, "( OLcfgAt:14 NAME 'olcDefaultSearchBase' "
281                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
282         { "disallows", "features", 2, 0, 8, ARG_PRE_DB|ARG_MAGIC,
283                 &config_disallows, "( OLcfgAt:15 NAME 'olcDisallows' "
284                         "EQUALITY caseIgnoreMatch "
285                         "SYNTAX OMsDirectoryString )", NULL, NULL },
286         { "ditcontentrule",     NULL, 0, 0, 0, ARG_MAGIC|CFG_DIT,
287                 &config_generic, "( OLcfgAt:16 NAME 'olcDitContentRules' "
288                         "DESC 'OpenLDAP DIT content rules' "
289                         "EQUALITY objectIdentifierFirstComponentMatch "
290                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.16 X-ORDERED 'VALUES' )",
291                         NULL, NULL },
292         { "gentlehup", "on|off", 2, 2, 0,
293 #ifdef SIGHUP
294                 ARG_ON_OFF, &global_gentlehup,
295 #else
296                 ARG_IGNORED, NULL,
297 #endif
298                 "( OLcfgAt:17 NAME 'olcGentleHUP' "
299                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
300         { "idletimeout", "timeout", 2, 2, 0, ARG_INT,
301                 &global_idletimeout, "( OLcfgAt:18 NAME 'olcIdleTimeout' "
302                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
303 /* XXX -- special case? */
304         { "include", "file", 2, 2, 0, ARG_MAGIC,
305                 &config_include, "( OLcfgAt:19 NAME 'olcInclude' "
306                         "SUP labeledURI )", NULL, NULL },
307         { "index_substr_if_minlen", "min", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MIN,
308                 &config_generic, "( OLcfgAt:20 NAME 'olcIndexSubstrIfMinLen' "
309                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
310         { "index_substr_if_maxlen", "max", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MAX,
311                 &config_generic, "( OLcfgAt:21 NAME 'olcIndexSubstrIfMaxLen' "
312                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
313         { "index_substr_any_len", "len", 2, 2, 0, ARG_INT|ARG_NONZERO,
314                 &index_substr_any_len, "( OLcfgAt:22 NAME 'olcIndexSubstrAnyLen' "
315                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
316         { "index_substr_step", "step", 2, 2, 0, ARG_INT|ARG_NONZERO,
317                 &index_substr_any_step, "( OLcfgAt:23 NAME 'olcIndexSubstrAnyStep' "
318                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
319         { "lastmod", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_LASTMOD,
320                 &config_generic, "( OLcfgAt:24 NAME 'olcLastMod' "
321                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
322         { "limits", "limits", 2, 0, 0, ARG_DB|ARG_MAGIC|CFG_LIMITS,
323                 &config_generic, "( OLcfgAt:25 NAME 'olcLimits' "
324                         "SYNTAX OMsDirectoryString )", NULL, NULL },
325         { "localSSF", "ssf", 2, 2, 0, ARG_INT,
326                 &local_ssf, "( OLcfgAt:26 NAME 'olcLocalSSF' "
327                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
328         { "logfile", "file", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_LOGFILE,
329                 &config_generic, "( OLcfgAt:27 NAME 'olcLogFile' "
330                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
331         { "loglevel", "level", 2, 0, 0, ARG_MAGIC,
332                 &config_loglevel, "( OLcfgAt:28 NAME 'olcLogLevel' "
333                         "SYNTAX OMsDirectoryString )", NULL, NULL },
334         { "maxDerefDepth", "depth", 2, 2, 0, ARG_DB|ARG_INT|ARG_MAGIC|CFG_DEPTH,
335                 &config_generic, "( OLcfgAt:29 NAME 'olcMaxDerefDepth' "
336                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
337         { "moduleload", "file", 2, 0, 0,
338 #ifdef SLAPD_MODULES
339                 ARG_MAGIC|CFG_MODLOAD, &config_generic,
340 #else
341                 ARG_IGNORED, NULL,
342 #endif
343                 "( OLcfgAt:30 NAME 'olcModuleLoad' "
344                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
345         { "modulepath", "path", 2, 2, 0,
346 #ifdef SLAPD_MODULES
347                 ARG_MAGIC|CFG_MODPATH, &config_generic,
348 #else
349                 ARG_IGNORED, NULL,
350 #endif
351                 "( OLcfgAt:31 NAME 'olcModulePath' "
352                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
353         { "objectclass", "objectclass", 2, 0, 0, ARG_PAREN|ARG_MAGIC|CFG_OC,
354                 &config_generic, "( OLcfgAt:32 NAME 'olcObjectClasses' "
355                 "DESC 'OpenLDAP object classes' "
356                 "EQUALITY objectIdentifierFirstComponentMatch "
357                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.37 X-ORDERED 'VALUES' )",
358                         NULL, NULL },
359         { "objectidentifier", NULL,     0, 0, 0, ARG_MAGIC|CFG_OID,
360                 &config_generic, "( OLcfgAt:33 NAME 'olcObjectIdentifier' "
361                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
362         { "overlay", "overlay", 2, 2, 0, ARG_MAGIC,
363                 &config_overlay, "( OLcfgAt:34 NAME 'olcOverlay' "
364                         "SUP olcDatabase X-ORDERED 'VALUES' )", NULL, NULL },
365         { "password-crypt-salt-format", "salt", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_SALT,
366                 &config_generic, "( OLcfgAt:35 NAME 'olcPasswordCryptSaltFormat' "
367                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
368         { "password-hash", "hash", 2, 2, 0, ARG_MAGIC,
369                 &config_passwd_hash, "( OLcfgAt:36 NAME 'olcPasswordHash' "
370                         "SYNTAX OMsDirectoryString )", NULL, NULL },
371         { "pidfile", "file", 2, 2, 0, ARG_STRING,
372                 &slapd_pid_file, "( OLcfgAt:37 NAME 'olcPidFile' "
373                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
374         { "plugin", NULL, 0, 0, 0,
375 #ifdef LDAP_SLAPI
376                 ARG_MAGIC|CFG_PLUGIN, &config_generic,
377 #else
378                 ARG_IGNORED, NULL,
379 #endif
380                 "( OLcfgAt:38 NAME 'olcPlugin' "
381                         "SYNTAX OMsDirectoryString )", NULL, NULL },
382         { "pluginlog", "filename", 2, 2, 0,
383 #ifdef LDAP_SLAPI
384                 ARG_STRING, &slapi_log_file,
385 #else
386                 ARG_IGNORED, NULL,
387 #endif
388                 "( OLcfgAt:39 NAME 'olcPluginLogFile' "
389                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
390         { "readonly", "on|off", 2, 2, 0, ARG_MAY_DB|ARG_ON_OFF|ARG_MAGIC|CFG_RO,
391                 &config_generic, "( OLcfgAt:40 NAME 'olcReadOnly' "
392                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
393         { "referral", "url", 2, 2, 0, ARG_MAGIC,
394                 &config_referral, "( OLcfgAt:41 NAME 'olcReferral' "
395                         "SUP labeledURI SINGLE-VALUE )", NULL, NULL },
396         { "replica", "host or uri", 2, 0, 0, ARG_DB|ARG_MAGIC,
397                 &config_replica, "( OLcfgAt:42 NAME 'olcReplica' "
398                         "SUP labeledURI )", NULL, NULL },
399         { "replica-argsfile", NULL, 0, 0, 0, ARG_STRING,
400                 &replica_argsFile, "( OLcfgAt:43 NAME 'olcReplicaArgsFile' "
401                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
402         { "replica-pidfile", NULL, 0, 0, 0, ARG_STRING,
403                 &replica_pidFile, "( OLcfgAt:44 NAME 'olcReplicaPidFile' "
404                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
405         { "replicationInterval", NULL, 0, 0, 0, ARG_INT,
406                 &replicationInterval, "( OLcfgAt:45 NAME 'olcReplicationInterval' "
407                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
408         { "replogfile", "filename", 2, 2, 0, ARG_MAY_DB|ARG_MAGIC|ARG_STRING|CFG_REPLOG,
409                 &config_generic, "( OLcfgAt:46 NAME 'olcReplogFile' "
410                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
411         { "require", "features", 2, 0, 7, ARG_MAY_DB|ARG_MAGIC,
412                 &config_requires, "( OLcfgAt:47 NAME 'olcRequires' "
413                         "SYNTAX OMsDirectoryString )", NULL, NULL },
414         { "restrict", "op_list", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
415                 &config_restrict, "( OLcfgAt:48 NAME 'olcRestrict' "
416                         "SYNTAX OMsDirectoryString )", NULL, NULL },
417         { "reverse-lookup", "on|off", 2, 2, 0,
418 #ifdef SLAPD_RLOOKUPS
419                 ARG_ON_OFF, &use_reverse_lookup,
420 #else
421                 ARG_IGNORED, NULL,
422 #endif
423                 "( OLcfgAt:49 NAME 'olcReverseLookup' "
424                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
425         { "rootdn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_MAGIC,
426                 &config_rootdn, "( OLcfgAt:50 NAME 'olcRootDN' "
427                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
428         { "rootDSE", "file", 2, 2, 0, ARG_MAGIC|CFG_ROOTDSE,
429                 &config_generic, "( OLcfgAt:51 NAME 'olcRootDSE' "
430                         "SYNTAX OMsDirectoryString )", NULL, NULL },
431         { "rootpw", "password", 2, 2, 0, ARG_BERVAL|ARG_DB|ARG_MAGIC,
432                 &config_rootpw, "( OLcfgAt:52 NAME 'olcRootPW' "
433                         "SYNTAX OMsOctetString SINGLE-VALUE )", NULL, NULL },
434         { "sasl-authz-policy", NULL, 2, 2, 0, ARG_MAGIC|CFG_AZPOLICY,
435                 &config_generic, NULL, NULL, NULL },
436         { "sasl-host", "host", 2, 2, 0,
437 #ifdef HAVE_CYRUS_SASL
438                 ARG_STRING|ARG_UNIQUE, &global_host,
439 #else
440                 ARG_IGNORED, NULL,
441 #endif
442                 "( OLcfgAt:53 NAME 'olcSaslHost' "
443                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
444         { "sasl-realm", "realm", 2, 2, 0,
445 #ifdef HAVE_CYRUS_SASL
446                 ARG_STRING|ARG_UNIQUE, &global_realm,
447 #else
448                 ARG_IGNORED, NULL,
449 #endif
450                 "( OLcfgAt:54 NAME 'olcSaslRealm' "
451                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
452         { "sasl-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
453                 &config_generic, NULL, NULL, NULL },
454         { "sasl-secprops", "properties", 2, 2, 0,
455 #ifdef HAVE_CYRUS_SASL
456                 ARG_MAGIC|CFG_SASLSECP, &config_generic,
457 #else
458                 ARG_IGNORED, NULL,
459 #endif
460                 "( OLcfgAt:56 NAME 'olcSaslSecProps' "
461                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
462         { "saslRegexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
463                 &config_generic, NULL, NULL, NULL },
464         { "schemacheck", "on|off", 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|CFG_CHECK,
465                 &config_generic, "( OLcfgAt:57 NAME 'olcSchemaCheck' "
466                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
467         { "schemadn", "dn", 2, 2, 0, ARG_MAY_DB|ARG_DN|ARG_MAGIC,
468                 &config_schema_dn, "( OLcfgAt:58 NAME 'olcSchemaDN' "
469                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
470         { "security", "factors", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
471                 &config_security, "( OLcfgAt:59 NAME 'olcSecurity' "
472                         "SYNTAX OMsDirectoryString )", NULL, NULL },
473         { "sizelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_SIZE,
474                 &config_sizelimit, "( OLcfgAt:60 NAME 'olcSizeLimit' "
475                         "SYNTAX OMsInteger )", NULL, NULL },
476         { "sockbuf_max_incoming", "max", 2, 2, 0, ARG_BER_LEN_T,
477                 &sockbuf_max_incoming, "( OLcfgAt:61 NAME 'olcSockbufMaxIncoming' "
478                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
479         { "sockbuf_max_incoming_auth", "max", 2, 2, 0, ARG_BER_LEN_T,
480                 &sockbuf_max_incoming_auth, "( OLcfgAt:62 NAME 'olcSockbufMaxIncomingAuth' "
481                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
482         { "srvtab", "file", 2, 2, 0,
483 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
484                 ARG_STRING, &ldap_srvtab,
485 #else
486                 ARG_IGNORED, NULL,
487 #endif
488                 "( OLcfgAt:63 NAME 'olcSrvtab' "
489                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
490         { "suffix",     "suffix", 2, 2, 0, ARG_DB|ARG_DN|ARG_MAGIC,
491                 &config_suffix, "( OLcfgAt:64 NAME 'olcSuffix' "
492                         "SYNTAX OMsDN )", NULL, NULL },
493         { "syncrepl", NULL, 0, 0, 0, ARG_DB|ARG_MAGIC,
494                 &config_syncrepl, "( OLcfgAt:65 NAME 'olcSyncrepl' "
495                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
496         { "threads", "count", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_THREADS,
497                 &config_generic, "( OLcfgAt:66 NAME 'olcThreads' "
498                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
499         { "timelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_TIME,
500                 &config_timelimit, "( OLcfgAt:67 NAME 'olcTimeLimit' "
501                         "SYNTAX OMsInteger )", NULL, NULL },
502         { "TLSCACertificateFile", NULL, 0, 0, 0,
503 #ifdef HAVE_TLS
504                 CFG_TLS_CA_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
505 #else
506                 ARG_IGNORED, NULL,
507 #endif
508                 "( OLcfgAt:68 NAME 'olcTLSCACertificateFile' "
509                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
510         { "TLSCACertificatePath", NULL, 0, 0, 0,
511 #ifdef HAVE_TLS
512                 CFG_TLS_CA_PATH|ARG_STRING|ARG_MAGIC, &config_tls_option,
513 #else
514                 ARG_IGNORED, NULL,
515 #endif
516                 "( OLcfgAt:69 NAME 'olcTLSCACertificatePath' "
517                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
518         { "TLSCertificateFile", NULL, 0, 0, 0,
519 #ifdef HAVE_TLS
520                 CFG_TLS_CERT_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
521 #else
522                 ARG_IGNORED, NULL,
523 #endif
524                 "( OLcfgAt:70 NAME 'olcTLSCertificateFile' "
525                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
526         { "TLSCertificateKeyFile", NULL, 0, 0, 0,
527 #ifdef HAVE_TLS
528                 CFG_TLS_CERT_KEY|ARG_STRING|ARG_MAGIC, &config_tls_option,
529 #else
530                 ARG_IGNORED, NULL,
531 #endif
532                 "( OLcfgAt:71 NAME 'olcTLSCertificateKeyFile' "
533                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
534         { "TLSCipherSuite",     NULL, 0, 0, 0,
535 #ifdef HAVE_TLS
536                 CFG_TLS_CIPHER|ARG_STRING|ARG_MAGIC, &config_tls_option,
537 #else
538                 ARG_IGNORED, NULL,
539 #endif
540                 "( OLcfgAt:72 NAME 'olcTLSCipherSuite' "
541                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
542         { "TLSCRLCheck", NULL, 0, 0, 0,
543 #ifdef HAVE_TLS
544                 CFG_TLS_CRLCHECK|ARG_STRING|ARG_MAGIC, &config_tls_config,
545 #else
546                 ARG_IGNORED, NULL,
547 #endif
548                 "( OLcfgAt:73 NAME 'olcTLSCRLCheck' "
549                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
550         { "TLSRandFile", NULL, 0, 0, 0,
551 #ifdef HAVE_TLS
552                 CFG_TLS_RAND|ARG_STRING|ARG_MAGIC, &config_tls_option,
553 #else
554                 ARG_IGNORED, NULL,
555 #endif
556                 "( OLcfgAt:74 NAME 'olcTLSRandFile' "
557                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
558         { "TLSVerifyClient", NULL, 0, 0, 0,
559 #ifdef HAVE_TLS
560                 CFG_TLS_VERIFY|ARG_STRING|ARG_MAGIC, &config_tls_config,
561 #else
562                 ARG_IGNORED, NULL,
563 #endif
564                 "( OLcfgAt:75 NAME 'olcTLSVerifyClient' "
565                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
566         { "ucdata-path", "path", 2, 2, 0, ARG_IGNORED,
567                 NULL, NULL, NULL, NULL },
568         { "updatedn", "dn", 2, 2, 0, ARG_DB|ARG_MAGIC,
569                 &config_updatedn, "( OLcfgAt:76 NAME 'olcUpdateDN' "
570                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
571         { "updateref", "url", 2, 2, 0, ARG_DB|ARG_MAGIC,
572                 &config_updateref, "( OLcfgAt:77 NAME 'olcUpdateRef' "
573                         "SUP labeledURI )", NULL, NULL },
574         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
575                 NULL, NULL, NULL, NULL }
576 };
577
578 static ConfigOCs cf_ocs[] = {
579         { "( OLcfgOc:1 "
580                 "NAME 'olcConfig' "
581                 "DESC 'OpenLDAP configuration object' "
582                 "ABSTRACT SUP top "
583                 "MAY ( cn $ olcConfigFile ) )", Cft_Abstract, NULL },
584         { "( OLcfgOc:2 "
585                 "NAME 'olcGlobal' "
586                 "DESC 'OpenLDAP Global configuration options' "
587                 "SUP olcConfig STRUCTURAL "
588                 "MAY ( olcConfigDir $ olcAllows $ olcArgsFile $ olcAttributeOptions $ "
589                  "olcAuthIDRewrite $ olcAuthzPolicy $ olcAuthzRegexp $ "
590                  "olcConcurrency $ olcConnMaxPending $ olcConnMaxPendingAuth $ "
591                  "olcDefaultSearchBase $ olcDisallows $ olcGentleHUP $ "
592                  "olcIdleTimeout $ olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
593                  "olcIndexSubstrAnyLen $ olcIndexSubstrAnyStep $ olcLocalSSF $ "
594                  "olcLogLevel $ olcModulePath $ olcObjectIdentifier $ "
595                  "olcPasswordCryptSaltFormat $ olcPasswordHash $ olcPidFile $ "
596                  "olcPlugin $ olcPluginLogFile $ olcReadOnly $ olcReferral $ "
597                  "olcReplicaPidFile $ olcReplicaArgsFile $ olcReplicationInterval $ "
598                  "olcReplogFile $ olcRequires $ olcRestrict $ olcReverseLookup $ "
599                  "olcRootDSE $ olcRootPW $ olcSaslHost $ olcSaslRealm $ olcSaslSecProps $ "
600                  "olcSchemaCheck $ olcSchemaDN $ olcSecurity $ olcSizeLimit $ "
601                  "olcSockbufMaxIncoming $ olcSockbufMaxIncomingAuth $ olcSrvtab $ "
602                  "olcThreads $ olcTimeLimit $ olcTLSCACertificateFile $ "
603                  "olcTLSCACertificatePath $ olcTLSCertificateFile $ "
604                  "olcTLSCertificateKeyFile $ olcTLSCipherSuite $ olcTLSCRLCheck $ "
605                  "olcTLSRandFile $ olcTLSVerifyClient ) )", Cft_Global, &cfOc_global },
606         { "( OLcfgOc:3 "
607                 "NAME 'olcSchemaConfig' "
608                 "DESC 'OpenLDAP schema object' "
609                 "SUP olcConfig STRUCTURAL "
610                 "MAY ( olcObjectIdentifier $ olcAttributeTypes $ olcObjectClasses $ "
611                  "olcDitContentRules ) )", Cft_Schema, &cfOc_schema },
612         { "( OLcfgOc:4 "
613                 "NAME 'olcBackendConfig' "
614                 "DESC 'OpenLDAP Backend-specific options' "
615                 "SUP olcConfig STRUCTURAL "
616                 "MAY ( olcBackend ) )", Cft_Backend, &cfOc_backend },
617         { "( OLcfgOc:5 "
618                 "NAME 'olcDatabaseConfig' "
619                 "DESC 'OpenLDAP Database-specific options' "
620                 "SUP olcConfig STRUCTURAL "
621                 "MAY ( olcDatabase $ olcAccess $ olcLastMod $ olcLimits $ "
622                  "olcMaxDerefDepth $ olcPlugin $ olcReadOnly $ olcReplica $ "
623                  "olcReplogFile $ olcRequires $ olcRestrict $ olcRootDN $ olcRootPW $ "
624                  "olcSchemaDN $ olcSecurity $ olcSizeLimit $ olcSuffix $ olcSyncrepl $ "
625                  "olcTimeLimit $ olcUpdateDN $ olcUpdateRef ) )",
626                         Cft_Database, &cfOc_database },
627         { "( OLcfgOc:6 "
628                 "NAME 'olcOverlayConfig' "
629                 "DESC 'OpenLDAP Overlay-specific options' "
630                 "SUP olcConfig STRUCTURAL "
631                 "MAY ( olcOverlay ) )", Cft_Overlay, &cfOc_overlay },
632         { "( OLcfgOc:7 "
633                 "NAME 'olcIncludeFile' "
634                 "DESC 'OpenLDAP configuration include file' "
635                 "SUP olcConfig STRUCTURAL "
636                 "MAY ( olcInclude $ olcRootDSE ) )",
637                 Cft_Include, &cfOc_include },
638 #ifdef SLAPD_MODULES
639         { "( OLcfgOc:8 "
640                 "NAME 'olcModuleList' "
641                 "DESC 'OpenLDAP dynamic module info' "
642                 "SUP olcConfig STRUCTURAL "
643                 "MUST olcModuleLoad  )",
644                 Cft_Module, &cfOc_module },
645 #endif
646         { NULL, 0, NULL }
647 };
648
649 static int
650 config_generic(ConfigArgs *c) {
651         char *p;
652         int i;
653
654         if ( c->op == SLAP_CONFIG_EMIT ) {
655                 int rc = 0;
656                 switch(c->type) {
657                 case CFG_CONCUR:
658                         c->value_int = ldap_pvt_thread_get_concurrency();
659                         break;
660                 case CFG_THREADS:
661                         c->value_int = connection_pool_max;
662                         break;
663                 case CFG_SALT:
664                         if ( passwd_salt )
665                                 c->value_string = ch_strdup( passwd_salt );
666                         else
667                                 rc = 1;
668                         break;
669                 case CFG_LIMITS:
670                         if ( c->be->be_limits ) {
671                                 char buf[4096*3];
672                                 struct berval bv;
673                                 int i;
674
675                                 for ( i=0; c->be->be_limits[i]; i++ ) {
676                                         bv.bv_len = sprintf( buf, "{%d}", i );
677                                         bv.bv_val = buf+bv.bv_len;
678                                         limits_unparse( c->be->be_limits[i], &bv );
679                                         bv.bv_len += bv.bv_val - buf;
680                                         bv.bv_val = buf;
681                                         value_add_one( &c->rvalue_vals, &bv );
682                                 }
683                         }
684                         if ( !c->rvalue_vals ) rc = 1;
685                         break;
686                 case CFG_RO:
687                         c->value_int = (c->be->be_restrictops & SLAP_RESTRICT_OP_WRITES) != 0;
688                         break;
689                 case CFG_AZPOLICY:
690                         c->value_string = ch_strdup( slap_sasl_getpolicy());
691                         break;
692                 case CFG_AZREGEXP:
693                         slap_sasl_regexp_unparse( &c->rvalue_vals );
694                         if ( !c->rvalue_vals ) rc = 1;
695                         break;
696 #ifdef HAVE_CYRUS_SASL
697                 case CFG_SASLSECP: {
698                         struct berval bv = BER_BVNULL;
699                         slap_sasl_secprops_unparse( &bv );
700                         if ( !BER_BVISNULL( &bv )) {
701                                 ber_bvarray_add( &c->rvalue_vals, &bv );
702                         } else {
703                                 rc = 1;
704                         }
705                         }
706                         break;
707 #endif
708                 case CFG_DEPTH:
709                         c->value_int = c->be->be_max_deref_depth;
710                         break;
711                 case CFG_OID: {
712                         ConfigFile *cf = c->private;
713                         if ( !cf )
714                                 oidm_unparse( &c->rvalue_vals, NULL, NULL, 1 );
715                         else if ( cf->c_om_head )
716                                 oidm_unparse( &c->rvalue_vals, cf->c_om_head,
717                                         cf->c_om_tail, 0 );
718                         if ( !c->rvalue_vals )
719                                 rc = 1;
720                         }
721                         break;
722                 case CFG_OC: {
723                         ConfigFile *cf = c->private;
724                         if ( !cf )
725                                 oc_unparse( &c->rvalue_vals, NULL, NULL, 1 );
726                         else if ( cf->c_oc_head )
727                                 oc_unparse( &c->rvalue_vals, cf->c_oc_head,
728                                         cf->c_oc_tail, 0 );
729                         if ( !c->rvalue_vals )
730                                 rc = 1;
731                         }
732                         break;
733                 case CFG_ATTR: {
734                         ConfigFile *cf = c->private;
735                         if ( !cf )
736                                 at_unparse( &c->rvalue_vals, NULL, NULL, 1 );
737                         else if ( cf->c_at_head )
738                                 at_unparse( &c->rvalue_vals, cf->c_at_head,
739                                         cf->c_at_tail, 0 );
740                         if ( !c->rvalue_vals )
741                                 rc = 1;
742                         }
743                         break;
744                 case CFG_DIT: {
745                         ConfigFile *cf = c->private;
746                         if ( !cf )
747                                 cr_unparse( &c->rvalue_vals, NULL, NULL, 1 );
748                         else if ( cf->c_cr_head )
749                                 cr_unparse( &c->rvalue_vals, cf->c_cr_head,
750                                         cf->c_cr_tail, 0 );
751                         if ( !c->rvalue_vals )
752                                 rc = 1;
753                         }
754                         break;
755                         
756                 case CFG_CHECK:
757                         c->value_int = global_schemacheck;
758                         break;
759                 case CFG_ACL: {
760                         AccessControl *a;
761                         char *src, *dst, ibuf[11];
762                         struct berval bv, abv;
763                         for (i=0, a=c->be->be_acl; a; i++,a=a->acl_next) {
764                                 abv.bv_len = sprintf( ibuf, "{%x}", i );
765                                 acl_unparse( a, &bv );
766                                 abv.bv_val = ch_malloc( abv.bv_len + bv.bv_len + 1 );
767                                 AC_MEMCPY( abv.bv_val, ibuf, abv.bv_len );
768                                 /* Turn TAB / EOL into plain space */
769                                 for (src=bv.bv_val,dst=abv.bv_val+abv.bv_len; *src; src++) {
770                                         if (isspace(*src)) *dst++ = ' ';
771                                         else *dst++ = *src;
772                                 }
773                                 *dst = '\0';
774                                 if (dst[-1] == ' ') {
775                                         dst--;
776                                         *dst = '\0';
777                                 }
778                                 abv.bv_len = dst - abv.bv_val;
779                                 ber_bvarray_add( &c->rvalue_vals, &abv );
780                         }
781                         rc = (!i);
782                         break;
783                 }
784                 case CFG_REPLOG:
785                         if ( c->be->be_replogfile )
786                                 c->value_string = ch_strdup( c->be->be_replogfile );
787                         break;
788                 case CFG_ROOTDSE: {
789                         ConfigFile *cf = c->private;
790                         if ( cf->c_dseFiles ) {
791                                 value_add( &c->rvalue_vals, cf->c_dseFiles );
792                         } else {
793                                 rc = 1;
794                         }
795                         }
796                         break;
797                 case CFG_LOGFILE:
798                         if ( logfileName )
799                                 c->value_string = ch_strdup( logfileName );
800                         else
801                                 rc = 1;
802                         break;
803                 case CFG_LASTMOD:
804                         c->value_int = (SLAP_NOLASTMOD(c->be) == 0);
805                         break;
806                 case CFG_SSTR_IF_MAX:
807                         c->value_int = index_substr_if_maxlen;
808                         break;
809                 case CFG_SSTR_IF_MIN:
810                         c->value_int = index_substr_if_minlen;
811                         break;
812 #ifdef SLAPD_MODULES
813                 case CFG_MODLOAD: {
814                         ModPaths *mp = c->private;
815                         if (mp->mp_loads) {
816                                 int i;
817                                 for (i=0; !BER_BVISNULL(&mp->mp_loads[i]); i++) {
818                                         struct berval bv;
819                                         bv.bv_val = c->log;
820                                         bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
821                                                 mp->mp_loads[i].bv_val );
822                                         value_add_one( &c->rvalue_vals, &bv );
823                                 }
824                         }
825
826                         rc = c->rvalue_vals ? 0 : 1;
827                         }
828                         break;
829                 case CFG_MODPATH: {
830                         ModPaths *mp;
831                         for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
832                                 struct berval bv;
833                                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
834                                         continue;
835                                 bv.bv_val = c->log;
836                                 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
837                                         mp->mp_path.bv_val );
838                                 value_add_one( &c->rvalue_vals, &bv );
839                         }
840                         rc = c->rvalue_vals ? 0 : 1;
841                         }
842                         break;
843 #endif
844 #ifdef LDAP_SLAPI
845                 case CFG_PLUGIN:
846                         slapi_int_plugin_unparse( c->be, &c->rvalue_vals );
847                         if ( !c->rvalue_vals ) rc = 1;
848                         break;
849 #endif
850 #ifdef SLAP_AUTH_REWRITE
851                 case CFG_REWRITE:
852                         if ( authz_rewrites ) {
853                                 struct berval bv, idx;
854                                 char ibuf[32];
855                                 int i;
856
857                                 idx.bv_val = ibuf;
858                                 for ( i=0; !BER_BVISNULL( &authz_rewrites[i] ); i++ ) {
859                                         idx.bv_len = sprintf( idx.bv_val, "{%d}", i );
860                                         bv.bv_len = idx.bv_len + authz_rewrites[i].bv_len;
861                                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
862                                         strcpy( bv.bv_val, idx.bv_val );
863                                         strcpy( bv.bv_val+idx.bv_len, authz_rewrites[i].bv_val );
864                                         ber_bvarray_add( &c->rvalue_vals, &bv );
865                                 }
866                         }
867                         if ( !c->rvalue_vals ) rc = 1;
868                         break;
869 #endif
870                 default:
871                         rc = 1;
872                 }
873                 return rc;
874         }
875
876         p = strchr(c->line,'(' /*')'*/);
877         switch(c->type) {
878                 case CFG_BACKEND:
879                         if(!(c->bi = backend_info(c->argv[1]))) {
880                                 Debug(LDAP_DEBUG_ANY, "%s: "
881                                         "backend %s failed init!\n", c->log, c->argv[1], 0);
882                                 return(1);
883                         }
884                         break;
885
886                 case CFG_DATABASE:
887                         c->bi = NULL;
888                         /* NOTE: config is always the first backend!
889                          */
890                         if ( !strcasecmp( c->argv[1], "config" )) {
891                                 c->be = backendDB;
892                         } else if ( !strcasecmp( c->argv[1], "frontend" )) {
893                                 c->be = frontendDB;
894                         } else if(!(c->be = backend_db_init(c->argv[1]))) {
895                                 Debug(LDAP_DEBUG_ANY, "%s: "
896                                         "database %s failed init!\n", c->log, c->argv[1], 0);
897                                 return(1);
898                         }
899                         break;
900
901                 case CFG_CONCUR:
902                         ldap_pvt_thread_set_concurrency(c->value_int);
903                         break;
904
905                 case CFG_THREADS:
906                         ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
907                         connection_pool_max = c->value_int;     /* save for reference */
908                         break;
909
910                 case CFG_SALT:
911                         if ( passwd_salt ) ch_free( passwd_salt );
912                         passwd_salt = c->value_string;
913                         lutil_salt_format(passwd_salt);
914                         break;
915
916                 case CFG_LIMITS:
917                         if(limits_parse(c->be, c->fname, c->lineno, c->argc, c->argv))
918                                 return(1);
919                         break;
920
921                 case CFG_RO:
922                         if(c->value_int)
923                                 c->be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
924                         else
925                                 c->be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
926                         break;
927
928                 case CFG_AZPOLICY:
929                         ch_free(c->value_string);
930                         if (slap_sasl_setpolicy( c->argv[1] )) {
931                                 Debug(LDAP_DEBUG_ANY, "%s: unable to parse value \"%s\" in"
932                                         " \"authz-policy <policy>\"\n",
933                                         c->log, c->argv[1], 0 );
934                                 return(1);
935                         }
936                         break;
937                 
938                 case CFG_AZREGEXP:
939                         if (slap_sasl_regexp_config( c->argv[1], c->argv[2] ))
940                                 return(1);
941                         break;
942                                 
943 #ifdef HAVE_CYRUS_SASL
944                 case CFG_SASLSECP:
945                         {
946                         char *txt = slap_sasl_secprops( c->argv[1] );
947                         if ( txt ) {
948                                 Debug(LDAP_DEBUG_ANY, "%s: sasl-secprops: %s\n",
949                                         c->log, txt, 0 );
950                                 return(1);
951                         }
952                         break;
953                         }
954 #endif
955
956                 case CFG_DEPTH:
957                         c->be->be_max_deref_depth = c->value_int;
958                         break;
959
960                 case CFG_OID: {
961                         OidMacro *om;
962
963                         if(parse_oidm(c->fname, c->lineno, c->argc, c->argv, 1, &om))
964                                 return(1);
965                         if (!cfn->c_om_head) cfn->c_om_head = om;
966                         cfn->c_om_tail = om;
967                         }
968                         break;
969
970                 case CFG_OC: {
971                         ObjectClass *oc;
972
973                         if(parse_oc(c->fname, c->lineno, p, c->argv, &oc)) return(1);
974                         if (!cfn->c_oc_head) cfn->c_oc_head = oc;
975                         cfn->c_oc_tail = oc;
976                         }
977                         break;
978
979                 case CFG_DIT: {
980                         ContentRule *cr;
981
982                         if(parse_cr(c->fname, c->lineno, p, c->argv, &cr)) return(1);
983                         if (!cfn->c_cr_head) cfn->c_cr_head = cr;
984                         cfn->c_cr_tail = cr;
985                         }
986                         break;
987
988                 case CFG_ATTR: {
989                         AttributeType *at;
990
991                         if(parse_at(c->fname, c->lineno, p, c->argv, &at)) return(1);
992                         if (!cfn->c_at_head) cfn->c_at_head = at;
993                         cfn->c_at_tail = at;
994                         }
995                         break;
996
997                 case CFG_ATOPT:
998                         ad_define_option(NULL, NULL, 0);
999                         for(i = 1; i < c->argc; i++)
1000                                 if(ad_define_option(c->argv[i], c->fname, c->lineno))
1001                                         return(1);
1002                         break;
1003
1004                 case CFG_CHECK:
1005                         global_schemacheck = c->value_int;
1006                         if(!global_schemacheck) Debug(LDAP_DEBUG_ANY, "%s: "
1007                                 "schema checking disabled! your mileage may vary!\n",
1008                                 c->log, 0, 0);
1009                         break;
1010
1011                 case CFG_ACL:
1012                         parse_acl(c->be, c->fname, c->lineno, c->argc, c->argv);
1013                         break;
1014
1015                 case CFG_REPLOG:
1016                         if(SLAP_MONITOR(c->be)) {
1017                                 Debug(LDAP_DEBUG_ANY, "%s: "
1018                                         "\"replogfile\" should not be used "
1019                                         "inside monitor database\n",
1020                                         c->log, 0, 0);
1021                                 return(0);      /* FIXME: should this be an error? */
1022                         }
1023
1024                         c->be->be_replogfile = c->value_string;
1025                         break;
1026
1027                 case CFG_ROOTDSE:
1028                         if(read_root_dse_file(c->argv[1])) {
1029                                 Debug(LDAP_DEBUG_ANY, "%s: "
1030                                         "could not read \"rootDSE <filename>\" line\n",
1031                                         c->log, 0, 0);
1032                                 return(1);
1033                         }
1034                         {
1035                                 struct berval bv;
1036                                 ber_str2bv( c->argv[1], 0, 1, &bv );
1037                                 ber_bvarray_add( &cfn->c_dseFiles, &bv );
1038                         }
1039                         break;
1040
1041                 case CFG_LOGFILE: {
1042                                 FILE *logfile;
1043                                 if ( logfileName ) ch_free( logfileName );
1044                                 logfileName = c->value_string;
1045                                 logfile = fopen(logfileName, "w");
1046                                 if(logfile) lutil_debug_file(logfile);
1047                         } break;
1048
1049                 case CFG_LASTMOD:
1050                         if(SLAP_NOLASTMODCMD(c->be)) {
1051                                 Debug(LDAP_DEBUG_ANY, "%s: "
1052                                         "lastmod not available for %s databases\n",
1053                                         c->log, c->be->bd_info->bi_type, 0);
1054                                 return(1);
1055                         }
1056                         if(c->value_int)
1057                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_NOLASTMOD;
1058                         else
1059                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NOLASTMOD;
1060                         break;
1061
1062                 case CFG_SSTR_IF_MAX:
1063                         if (c->value_int < index_substr_if_minlen) {
1064                                 Debug(LDAP_DEBUG_ANY, "%s: "
1065                                         "invalid max value (%d)\n",
1066                                         c->log, c->value_int, 0 );
1067                                 return(1);
1068                         }
1069                         index_substr_if_maxlen = c->value_int;
1070                         break;
1071
1072                 case CFG_SSTR_IF_MIN:
1073                         if (c->value_int > index_substr_if_maxlen) {
1074                                 Debug(LDAP_DEBUG_ANY, "%s: "
1075                                         "invalid min value (%d)\n",
1076                                         c->log, c->value_int, 0 );
1077                                 return(1);
1078                         }
1079                         index_substr_if_minlen = c->value_int;
1080                         break;
1081
1082 #ifdef SLAPD_MODULES
1083                 case CFG_MODLOAD:
1084                         if(module_load(c->argv[1], c->argc - 2, (c->argc > 2) ? c->argv + 2 : NULL))
1085                                 return(1);
1086                         /* Record this load on the current path */
1087                         {
1088                                 struct berval bv;
1089                                 ber_str2bv(c->line, 0, 1, &bv);
1090                                 ber_bvarray_add( &modcur->mp_loads, &bv );
1091                         }
1092                         break;
1093
1094                 case CFG_MODPATH:
1095                         if(module_path(c->argv[1])) return(1);
1096                         /* Record which path was used with each module */
1097                         {
1098                                 ModPaths *mp;
1099
1100                                 if (!modpaths.mp_loads) {
1101                                         mp = &modpaths;
1102                                 } else {
1103                                         mp = ch_malloc( sizeof( ModPaths ));
1104                                         modlast->mp_next = mp;
1105                                 }
1106                                 ber_str2bv(c->argv[1], 0, 1, &mp->mp_path);
1107                                 mp->mp_next = NULL;
1108                                 mp->mp_loads = NULL;
1109                                 modlast = mp;
1110                                 if ( c->op == SLAP_CONFIG_ADD )
1111                                         modcur = mp;
1112                         }
1113                         
1114                         break;
1115 #endif
1116
1117 #ifdef LDAP_SLAPI
1118                 case CFG_PLUGIN:
1119                         if(slapi_int_read_config(c->be, c->fname, c->lineno, c->argc, c->argv) != LDAP_SUCCESS)
1120                                 return(1);
1121                         slapi_plugins_used++;
1122                         break;
1123 #endif
1124
1125 #ifdef SLAP_AUTH_REWRITE
1126                 case CFG_REWRITE: {
1127                         struct berval bv;
1128                         if(slap_sasl_rewrite_config(c->fname, c->lineno, c->argc, c->argv))
1129                                 return(1);
1130                         ber_str2bv( c->line, 0, 1, &bv );
1131                         ber_bvarray_add( &authz_rewrites, &bv );
1132                         }
1133                         break;
1134 #endif
1135
1136
1137                 default:
1138                         Debug(LDAP_DEBUG_ANY, "%s: unknown CFG_TYPE %d"
1139                                 "(ignored)\n", c->log, c->type, 0);
1140
1141         }
1142         return(0);
1143 }
1144
1145
1146 static int
1147 config_fname(ConfigArgs *c) {
1148         if(c->op == SLAP_CONFIG_EMIT) {
1149                 if (c->private) {
1150                         ConfigFile *cf = c->private;
1151                         value_add_one( &c->rvalue_vals, &cf->c_file );
1152                         return 0;
1153                 }
1154                 return 1;
1155         }
1156         return(0);
1157 }
1158
1159 static int
1160 config_cfdir(ConfigArgs *c) {
1161         if(c->op == SLAP_CONFIG_EMIT) {
1162                 value_add_one( &c->rvalue_vals, &cfdir );
1163                 return 0;
1164         }
1165         return(0);
1166 }
1167
1168 static int
1169 config_search_base(ConfigArgs *c) {
1170         struct berval dn;
1171
1172         if(c->op == SLAP_CONFIG_EMIT) {
1173                 int rc = 1;
1174                 if (!BER_BVISEMPTY(&default_search_base)) {
1175                         value_add_one(&c->rvalue_vals, &default_search_base);
1176                         value_add_one(&c->rvalue_nvals, &default_search_nbase);
1177                         rc = 0;
1178                 }
1179                 return rc;
1180         }
1181
1182         if(c->bi || c->be != frontendDB) {
1183                 Debug(LDAP_DEBUG_ANY, "%s: defaultSearchBase line must appear "
1184                         "prior to any backend or database definition\n",
1185                         c->log, 0, 0);
1186                 return(1);
1187         }
1188
1189         if(default_search_nbase.bv_len) {
1190                 Debug(LDAP_DEBUG_ANY, "%s: "
1191                         "default search base \"%s\" already defined "
1192                         "(discarding old)\n",
1193                         c->log, default_search_base.bv_val, 0);
1194                 free(default_search_base.bv_val);
1195                 free(default_search_nbase.bv_val);
1196         }
1197
1198         default_search_base = c->value_dn;
1199         default_search_nbase = c->value_ndn;
1200         return(0);
1201 }
1202
1203 static int
1204 config_passwd_hash(ConfigArgs *c) {
1205         int i;
1206         if (c->op == SLAP_CONFIG_EMIT) {
1207                 struct berval bv;
1208                 for (i=0; default_passwd_hash && default_passwd_hash[i]; i++) {
1209                         ber_str2bv(default_passwd_hash[i], 0, 0, &bv);
1210                         value_add_one(&c->rvalue_vals, &bv);
1211                 }
1212                 return i ? 0 : 1;
1213         }
1214         if(default_passwd_hash) {
1215                 Debug(LDAP_DEBUG_ANY, "%s: "
1216                         "already set default password_hash\n",
1217                         c->log, 0, 0);
1218                 return(1);
1219         }
1220         for(i = 1; i < c->argc; i++) {
1221                 if(!lutil_passwd_scheme(c->argv[i])) {
1222                         Debug(LDAP_DEBUG_ANY, "%s: "
1223                                 "password scheme \"%s\" not available\n",
1224                                 c->log, c->argv[i], 0);
1225                 } else {
1226                         ldap_charray_add(&default_passwd_hash, c->argv[i]);
1227                 }
1228                 if(!default_passwd_hash) {
1229                         Debug(LDAP_DEBUG_ANY, "%s: no valid hashes found\n",
1230                                 c->log, 0, 0 );
1231                         return(1);
1232                 }
1233         }
1234         return(0);
1235 }
1236
1237 static int
1238 config_schema_dn(ConfigArgs *c) {
1239         if ( c->op == SLAP_CONFIG_EMIT ) {
1240                 int rc = 1;
1241                 if ( !BER_BVISEMPTY( &c->be->be_schemadn )) {
1242                         value_add_one(&c->rvalue_vals, &c->be->be_schemadn);
1243                         value_add_one(&c->rvalue_nvals, &c->be->be_schemandn);
1244                         rc = 0;
1245                 }
1246                 return rc;
1247         }
1248         c->be->be_schemadn = c->value_dn;
1249         c->be->be_schemandn = c->value_ndn;
1250         return(0);
1251 }
1252
1253 static int
1254 config_sizelimit(ConfigArgs *c) {
1255         int i, rc = 0;
1256         char *next;
1257         struct slap_limits_set *lim = &c->be->be_def_limit;
1258         if (c->op == SLAP_CONFIG_EMIT) {
1259                 char buf[8192];
1260                 struct berval bv;
1261                 bv.bv_val = buf;
1262                 bv.bv_len = 0;
1263                 limits_unparse_one( lim, SLAP_LIMIT_SIZE, &bv );
1264                 if ( !BER_BVISEMPTY( &bv ))
1265                         value_add_one( &c->rvalue_vals, &bv );
1266                 else
1267                         rc = 1;
1268                 return rc;
1269         }
1270         for(i = 1; i < c->argc; i++) {
1271                 if(!strncasecmp(c->argv[i], "size", 4)) {
1272                         rc = limits_parse_one(c->argv[i], lim);
1273                         if ( rc ) {
1274                                 Debug(LDAP_DEBUG_ANY, "%s: "
1275                                         "unable to parse value \"%s\" in \"sizelimit <limit>\" line\n",
1276                                         c->log, c->argv[i], 0);
1277                                 return(1);
1278                         }
1279                 } else {
1280                         if(!strcasecmp(c->argv[i], "unlimited")) {
1281                                 lim->lms_s_soft = -1;
1282                         } else {
1283                                 lim->lms_s_soft = strtol(c->argv[i], &next, 0);
1284                                 if(next == c->argv[i]) {
1285                                         Debug(LDAP_DEBUG_ANY, "%s: "
1286                                                 "unable to parse limit \"%s\" in \"sizelimit <limit>\" line\n",
1287                                                 c->log, c->argv[i], 0);
1288                                         return(1);
1289                                 } else if(next[0] != '\0') {
1290                                         Debug(LDAP_DEBUG_ANY, "%s: "
1291                                                 "trailing chars \"%s\" in \"sizelimit <limit>\" line (ignored)\n",
1292                                                 c->log, next, 0);
1293                                 }
1294                         }
1295                         lim->lms_s_hard = 0;
1296                 }
1297         }
1298         return(0);
1299 }
1300
1301 static int
1302 config_timelimit(ConfigArgs *c) {
1303         int i, rc = 0;
1304         char *next;
1305         struct slap_limits_set *lim = &c->be->be_def_limit;
1306         if (c->op == SLAP_CONFIG_EMIT) {
1307                 char buf[8192];
1308                 struct berval bv;
1309                 bv.bv_val = buf;
1310                 bv.bv_len = 0;
1311                 limits_unparse_one( lim, SLAP_LIMIT_TIME, &bv );
1312                 if ( !BER_BVISEMPTY( &bv ))
1313                         value_add_one( &c->rvalue_vals, &bv );
1314                 else
1315                         rc = 1;
1316                 return rc;
1317         }
1318         for(i = 1; i < c->argc; i++) {
1319                 if(!strncasecmp(c->argv[i], "time", 4)) {
1320                         rc = limits_parse_one(c->argv[i], lim);
1321                         if ( rc ) {
1322                                 Debug(LDAP_DEBUG_ANY, "%s: "
1323                                         "unable to parse value \"%s\" in \"timelimit <limit>\" line\n",
1324                                         c->log, c->argv[i], 0);
1325                                 return(1);
1326                         }
1327                 } else {
1328                         if(!strcasecmp(c->argv[i], "unlimited")) {
1329                                 lim->lms_t_soft = -1;
1330                         } else {
1331                                 lim->lms_t_soft = strtol(c->argv[i], &next, 0);
1332                                 if(next == c->argv[i]) {
1333                                         Debug(LDAP_DEBUG_ANY, "%s: "
1334                                                 "unable to parse limit \"%s\" in \"timelimit <limit>\" line\n",
1335                                                 c->log, c->argv[i], 0);
1336                                         return(1);
1337                                 } else if(next[0] != '\0') {
1338                                         Debug(LDAP_DEBUG_ANY, "%s: "
1339                                                 "trailing chars \"%s\" in \"timelimit <limit>\" line (ignored)\n",
1340                                                 c->log, next, 0);
1341                                 }
1342                         }
1343                         lim->lms_t_hard = 0;
1344                 }
1345         }
1346         return(0);
1347 }
1348
1349 static int
1350 config_overlay(ConfigArgs *c) {
1351         if (c->op == SLAP_CONFIG_EMIT) {
1352                 return 1;
1353         }
1354         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1])) {
1355                 /* log error */
1356                 Debug(LDAP_DEBUG_ANY, "%s: (optional) %s overlay \"%s\" configuration failed (ignored)\n",
1357                         c->log, c->be == frontendDB ? "global " : "", c->argv[1][1]);
1358         } else if(overlay_config(c->be, c->argv[1])) {
1359                 return(1);
1360         }
1361         return(0);
1362 }
1363
1364 static int
1365 config_suffix(ConfigArgs *c) {
1366         Backend *tbe;
1367         struct berval pdn, ndn;
1368         int rc;
1369
1370         if (c->be == frontendDB) return 1;
1371
1372         if (c->op == SLAP_CONFIG_EMIT) {
1373                 if (!BER_BVISNULL( &c->be->be_suffix[0] )) {
1374                         value_add( &c->rvalue_vals, c->be->be_suffix );
1375                         value_add( &c->rvalue_nvals, c->be->be_nsuffix );
1376                         return 0;
1377                 } else {
1378                         return 1;
1379                 }
1380         }
1381 #ifdef SLAPD_MONITOR_DN
1382         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
1383                 Debug(LDAP_DEBUG_ANY, "%s: "
1384                         "\"%s\" is reserved for monitoring slapd\n",
1385                         c->log, SLAPD_MONITOR_DN, 0);
1386                 return(1);
1387         }
1388 #endif
1389
1390         pdn = c->value_dn;
1391         ndn = c->value_ndn;
1392         tbe = select_backend(&ndn, 0, 0);
1393         if(tbe == c->be) {
1394                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by this backend! (ignored)\n",
1395                         c->log, 0, 0);
1396                 free(pdn.bv_val);
1397                 free(ndn.bv_val);
1398         } else if(tbe) {
1399                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by a preceding backend \"%s\"\n",
1400                         c->log, tbe->be_suffix[0].bv_val, 0);
1401                 free(pdn.bv_val);
1402                 free(ndn.bv_val);
1403                 return(1);
1404         } else if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
1405                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
1406                         "base provided \"%s\" (assuming okay)\n",
1407                         c->log, default_search_base.bv_val, 0);
1408         }
1409         ber_bvarray_add(&c->be->be_suffix, &pdn);
1410         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
1411         return(0);
1412 }
1413
1414 static int
1415 config_rootdn(ConfigArgs *c) {
1416         if (c->op == SLAP_CONFIG_EMIT) {
1417                 if ( !BER_BVISNULL( &c->be->be_rootdn )) {
1418                         value_add_one(&c->rvalue_vals, &c->be->be_rootdn);
1419                         value_add_one(&c->rvalue_nvals, &c->be->be_rootndn);
1420                         return 0;
1421                 } else {
1422                         return 1;
1423                 }
1424         }
1425         c->be->be_rootdn = c->value_dn;
1426         c->be->be_rootndn = c->value_ndn;
1427         return(0);
1428 }
1429
1430 static int
1431 config_rootpw(ConfigArgs *c) {
1432         Backend *tbe;
1433         if (c->op == SLAP_CONFIG_EMIT) {
1434                 if (!BER_BVISEMPTY(&c->be->be_rootpw)) {
1435                         ber_dupbv( &c->value_bv, &c->be->be_rootpw);
1436                         return 0;
1437                 }
1438                 return 1;
1439         }
1440
1441         tbe = select_backend(&c->be->be_rootndn, 0, 0);
1442         if(tbe != c->be) {
1443                 Debug(LDAP_DEBUG_ANY, "%s: "
1444                         "rootpw can only be set when rootdn is under suffix\n",
1445                         c->log, 0, 0);
1446                 return(1);
1447         }
1448         c->be->be_rootpw = c->value_bv;
1449         return(0);
1450 }
1451
1452 static int
1453 config_restrict(ConfigArgs *c) {
1454         slap_mask_t restrictops = 0;
1455         int i;
1456         slap_verbmasks restrictable_ops[] = {
1457                 { BER_BVC("bind"),              SLAP_RESTRICT_OP_BIND },
1458                 { BER_BVC("add"),               SLAP_RESTRICT_OP_ADD },
1459                 { BER_BVC("modify"),            SLAP_RESTRICT_OP_MODIFY },
1460                 { BER_BVC("rename"),            SLAP_RESTRICT_OP_RENAME },
1461                 { BER_BVC("modrdn"),            0 },
1462                 { BER_BVC("delete"),            SLAP_RESTRICT_OP_DELETE },
1463                 { BER_BVC("search"),            SLAP_RESTRICT_OP_SEARCH },
1464                 { BER_BVC("compare"),   SLAP_RESTRICT_OP_COMPARE },
1465                 { BER_BVC("read"),              SLAP_RESTRICT_OP_READS },
1466                 { BER_BVC("write"),             SLAP_RESTRICT_OP_WRITES },
1467                 { BER_BVC("extended"),  SLAP_RESTRICT_OP_EXTENDED },
1468                 { BER_BVC("extended=" LDAP_EXOP_START_TLS ),            SLAP_RESTRICT_EXOP_START_TLS },
1469                 { BER_BVC("extended=" LDAP_EXOP_MODIFY_PASSWD ),        SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
1470                 { BER_BVC("extended=" LDAP_EXOP_X_WHO_AM_I ),           SLAP_RESTRICT_EXOP_WHOAMI },
1471                 { BER_BVC("extended=" LDAP_EXOP_X_CANCEL ),             SLAP_RESTRICT_EXOP_CANCEL },
1472                 { BER_BVNULL,   0 }
1473         };
1474
1475         if (c->op == SLAP_CONFIG_EMIT) {
1476                 return mask_to_verbs( restrictable_ops, c->be->be_restrictops,
1477                         &c->rvalue_vals );
1478         }
1479         i = verbs_to_mask( c->argc, c->argv, restrictable_ops, &restrictops );
1480         if ( i ) {
1481                 Debug(LDAP_DEBUG_ANY, "%s: "
1482                         "unknown operation %s in \"restrict <features>\" line\n",
1483                         c->log, c->argv[i], 0);
1484                 return(1);
1485         }
1486         if ( restrictops & SLAP_RESTRICT_OP_EXTENDED )
1487                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
1488         c->be->be_restrictops |= restrictops;
1489         return(0);
1490 }
1491
1492 static int
1493 config_allows(ConfigArgs *c) {
1494         slap_mask_t allows = 0;
1495         int i;
1496         slap_verbmasks allowable_ops[] = {
1497                 { BER_BVC("bind_v2"),           SLAP_ALLOW_BIND_V2 },
1498                 { BER_BVC("bind_anon_cred"),    SLAP_ALLOW_BIND_ANON_CRED },
1499                 { BER_BVC("bind_anon_dn"),      SLAP_ALLOW_BIND_ANON_DN },
1500                 { BER_BVC("update_anon"),       SLAP_ALLOW_UPDATE_ANON },
1501                 { BER_BVNULL,   0 }
1502         };
1503         if (c->op == SLAP_CONFIG_EMIT) {
1504                 return mask_to_verbs( allowable_ops, global_allows, &c->rvalue_vals );
1505         }
1506         i = verbs_to_mask(c->argc, c->argv, allowable_ops, &allows);
1507         if ( i ) {
1508                 Debug(LDAP_DEBUG_ANY, "%s: "
1509                         "unknown feature %s in \"allow <features>\" line\n",
1510                         c->log, c->argv[i], 0);
1511                 return(1);
1512         }
1513         global_allows |= allows;
1514         return(0);
1515 }
1516
1517 static int
1518 config_disallows(ConfigArgs *c) {
1519         slap_mask_t disallows = 0;
1520         int i;
1521         slap_verbmasks disallowable_ops[] = {
1522                 { BER_BVC("bind_anon"),         SLAP_DISALLOW_BIND_ANON },
1523                 { BER_BVC("bind_simple"),       SLAP_DISALLOW_BIND_SIMPLE },
1524                 { BER_BVC("bind_krb4"),         SLAP_DISALLOW_BIND_KRBV4 },
1525                 { BER_BVC("tls_2_anon"),                SLAP_DISALLOW_TLS_2_ANON },
1526                 { BER_BVC("tls_authc"),         SLAP_DISALLOW_TLS_AUTHC },
1527                 { BER_BVNULL, 0 }
1528         };
1529         if (c->op == SLAP_CONFIG_EMIT) {
1530                 return mask_to_verbs( disallowable_ops, global_disallows, &c->rvalue_vals );
1531         }
1532         i = verbs_to_mask(c->argc, c->argv, disallowable_ops, &disallows);
1533         if ( i ) {
1534                 Debug(LDAP_DEBUG_ANY, "%s: "
1535                         "unknown feature %s in \"disallow <features>\" line\n",
1536                         c->log, c->argv[i], 0);
1537                 return(1);
1538         }
1539         global_disallows |= disallows;
1540         return(0);
1541 }
1542
1543 static int
1544 config_requires(ConfigArgs *c) {
1545         slap_mask_t requires = 0;
1546         int i;
1547         slap_verbmasks requires_ops[] = {
1548                 { BER_BVC("bind"),              SLAP_REQUIRE_BIND },
1549                 { BER_BVC("LDAPv3"),            SLAP_REQUIRE_LDAP_V3 },
1550                 { BER_BVC("authc"),             SLAP_REQUIRE_AUTHC },
1551                 { BER_BVC("sasl"),              SLAP_REQUIRE_SASL },
1552                 { BER_BVC("strong"),            SLAP_REQUIRE_STRONG },
1553                 { BER_BVNULL, 0 }
1554         };
1555         if (c->op == SLAP_CONFIG_EMIT) {
1556                 return mask_to_verbs( requires_ops, c->be->be_requires, &c->rvalue_vals );
1557         }
1558         i = verbs_to_mask(c->argc, c->argv, requires_ops, &requires);
1559         if ( i ) {
1560                 Debug(LDAP_DEBUG_ANY, "%s: "
1561                         "unknown feature %s in \"require <features>\" line\n",
1562                         c->log, c->argv[i], 0);
1563                 return(1);
1564         }
1565         c->be->be_requires = requires;
1566         return(0);
1567 }
1568
1569 static int
1570 config_loglevel(ConfigArgs *c) {
1571         int i;
1572         char *next;
1573         slap_verbmasks loglevel_ops[] = {
1574                 { BER_BVC("Trace"),     LDAP_DEBUG_TRACE },
1575                 { BER_BVC("Packets"),   LDAP_DEBUG_PACKETS },
1576                 { BER_BVC("Args"),      LDAP_DEBUG_ARGS },
1577                 { BER_BVC("Conns"),     LDAP_DEBUG_CONNS },
1578                 { BER_BVC("BER"),       LDAP_DEBUG_BER },
1579                 { BER_BVC("Filter"),    LDAP_DEBUG_FILTER },
1580                 { BER_BVC("Config"),    LDAP_DEBUG_CONFIG },
1581                 { BER_BVC("ACL"),       LDAP_DEBUG_ACL },
1582                 { BER_BVC("Stats"),     LDAP_DEBUG_STATS },
1583                 { BER_BVC("Stats2"),    LDAP_DEBUG_STATS2 },
1584                 { BER_BVC("Shell"),     LDAP_DEBUG_SHELL },
1585                 { BER_BVC("Parse"),     LDAP_DEBUG_PARSE },
1586                 { BER_BVC("Cache"),     LDAP_DEBUG_CACHE },
1587                 { BER_BVC("Index"),     LDAP_DEBUG_INDEX },
1588                 { BER_BVC("Any"),       -1 },
1589                 { BER_BVNULL,   0 }
1590         };
1591
1592         if (c->op == SLAP_CONFIG_EMIT) {
1593                 return mask_to_verbs( loglevel_ops, ldap_syslog, &c->rvalue_vals );
1594         }
1595
1596         ldap_syslog = 0;
1597
1598         for( i=1; i < c->argc; i++ ) {
1599                 int     level;
1600
1601                 if ( isdigit( c->argv[i][0] ) ) {
1602                         level = strtol( c->argv[i], &next, 10 );
1603                         if ( next == NULL || next[0] != '\0' ) {
1604                                 Debug( LDAP_DEBUG_ANY,
1605                                         "%s: unable to parse level \"%s\" "
1606                                         "in \"loglevel <level> [...]\" line.\n",
1607                                         c->log, c->argv[i], 0);
1608                                 return( 1 );
1609                         }
1610                 } else {
1611                         int j = verb_to_mask(c->argv[i], loglevel_ops);
1612                         if(BER_BVISNULL(&loglevel_ops[j].word)) {
1613                                 Debug( LDAP_DEBUG_ANY,
1614                                         "%s: unknown level \"%s\" "
1615                                         "in \"loglevel <level> [...]\" line.\n",
1616                                         c->log, c->argv[i], 0);
1617                                 return( 1 );
1618                         }
1619                         level = loglevel_ops[j].mask;
1620                 }
1621                 ldap_syslog |= level;
1622         }
1623         return(0);
1624 }
1625
1626 static int
1627 config_syncrepl(ConfigArgs *c) {
1628         if (c->op == SLAP_CONFIG_EMIT) {
1629                 if ( c->be->be_syncinfo ) {
1630                         struct berval bv;
1631                         syncrepl_unparse( c->be->be_syncinfo, &bv ); 
1632                         ber_bvarray_add( &c->rvalue_vals, &bv );
1633                         return 0;
1634                 }
1635                 return 1;
1636         }
1637         if(SLAP_SHADOW(c->be)) {
1638                 Debug(LDAP_DEBUG_ANY, "%s: "
1639                         "syncrepl: database already shadowed.\n",
1640                         c->log, 0, 0);
1641                 return(1);
1642         } else if(add_syncrepl(c->be, c->argv, c->argc)) {
1643                 return(1);
1644         }
1645         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);
1646         return(0);
1647 }
1648
1649 static int
1650 config_referral(ConfigArgs *c) {
1651         struct berval vals[2];
1652         if (c->op == SLAP_CONFIG_EMIT) {
1653                 if ( default_referral ) {
1654                         value_add( &c->rvalue_vals, default_referral );
1655                         return 0;
1656                 } else {
1657                         return 1;
1658                 }
1659         }
1660         if(validate_global_referral(c->argv[1])) {
1661                 Debug(LDAP_DEBUG_ANY, "%s: "
1662                         "invalid URL (%s) in \"referral\" line.\n",
1663                         c->log, c->argv[1], 0);
1664                 return(1);
1665         }
1666
1667         ber_str2bv(c->argv[1], 0, 0, &vals[0]);
1668         vals[1].bv_val = NULL; vals[1].bv_len = 0;
1669         if(value_add(&default_referral, vals)) return(LDAP_OTHER);
1670         return(0);
1671 }
1672
1673 static struct {
1674         struct berval key;
1675         int off;
1676 } sec_keys[] = {
1677         { BER_BVC("ssf="), offsetof(slap_ssf_set_t, sss_ssf) },
1678         { BER_BVC("transport="), offsetof(slap_ssf_set_t, sss_transport) },
1679         { BER_BVC("tls="), offsetof(slap_ssf_set_t, sss_tls) },
1680         { BER_BVC("sasl="), offsetof(slap_ssf_set_t, sss_sasl) },
1681         { BER_BVC("update_ssf="), offsetof(slap_ssf_set_t, sss_update_ssf) },
1682         { BER_BVC("update_transport="), offsetof(slap_ssf_set_t, sss_update_transport) },
1683         { BER_BVC("update_tls="), offsetof(slap_ssf_set_t, sss_update_tls) },
1684         { BER_BVC("update_sasl="), offsetof(slap_ssf_set_t, sss_update_sasl) },
1685         { BER_BVC("simple_bind="), offsetof(slap_ssf_set_t, sss_simple_bind) },
1686         { BER_BVNULL, 0 }
1687 };
1688
1689 static int
1690 config_security(ConfigArgs *c) {
1691         slap_ssf_set_t *set = &c->be->be_ssf_set;
1692         char *next;
1693         int i, j;
1694         if (c->op == SLAP_CONFIG_EMIT) {
1695                 char numbuf[32];
1696                 struct berval bv;
1697                 slap_ssf_t *tgt;
1698                 int rc = 1;
1699
1700                 for (i=0; !BER_BVISNULL( &sec_keys[i].key ); i++) {
1701                         tgt = (slap_ssf_t *)((char *)set + sec_keys[i].off);
1702                         if ( *tgt ) {
1703                                 rc = 0;
1704                                 bv.bv_len = sprintf( numbuf, "%u", *tgt );
1705                                 bv.bv_len += sec_keys[i].key.bv_len;
1706                                 bv.bv_val = ch_malloc( bv.bv_len + 1);
1707                                 next = lutil_strcopy( bv.bv_val, sec_keys[i].key.bv_val );
1708                                 strcpy( next, numbuf );
1709                                 ber_bvarray_add( &c->rvalue_vals, &bv );
1710                         }
1711                 }
1712                 return rc;
1713         }
1714         for(i = 1; i < c->argc; i++) {
1715                 slap_ssf_t *tgt = NULL;
1716                 char *src;
1717                 for ( j=0; !BER_BVISNULL( &sec_keys[j].key ); j++ ) {
1718                         if(!strncasecmp(c->argv[i], sec_keys[j].key.bv_val,
1719                                 sec_keys[j].key.bv_len)) {
1720                                 src = c->argv[i] + sec_keys[j].key.bv_len;
1721                                 tgt = (slap_ssf_t *)((char *)set + sec_keys[j].off);
1722                                 break;
1723                         }
1724                 }
1725                 if ( !tgt ) {
1726                         Debug(LDAP_DEBUG_ANY, "%s: "
1727                                 "unknown factor %s in \"security <factors>\" line\n",
1728                                 c->log, c->argv[i], 0);
1729                         return(1);
1730                 }
1731
1732                 *tgt = strtol(src, &next, 10);
1733                 if(next == NULL || next[0] != '\0' ) {
1734                         Debug(LDAP_DEBUG_ANY, "%s: "
1735                                 "unable to parse factor \"%s\" in \"security <factors>\" line\n",
1736                                 c->log, c->argv[i], 0);
1737                         return(1);
1738                 }
1739         }
1740         return(0);
1741 }
1742
1743 static char *
1744 anlist_unparse( AttributeName *an, char *ptr ) {
1745         int comma = 0;
1746
1747         for (; !BER_BVISNULL( &an->an_name ); an++) {
1748                 if ( comma ) *ptr++ = ',';
1749                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
1750                 comma = 1;
1751         }
1752         return ptr;
1753 }
1754
1755 static void
1756 replica_unparse( struct slap_replica_info *ri, int i, struct berval *bv )
1757 {
1758         int len;
1759         char *ptr;
1760         struct berval bc = {0};
1761         char numbuf[32];
1762
1763         len = sprintf(numbuf, "{%d}", i );
1764
1765         len += strlen( ri->ri_uri ) + STRLENOF("uri=");
1766         if ( ri->ri_nsuffix ) {
1767                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
1768                         len += ri->ri_nsuffix[i].bv_len + STRLENOF(" suffix=\"\"");
1769                 }
1770         }
1771         if ( ri->ri_attrs ) {
1772                 len += STRLENOF("attr");
1773                 if ( ri->ri_exclude ) len++;
1774                 for (i=0; !BER_BVISNULL( &ri->ri_attrs[i].an_name ); i++) {
1775                         len += 1 + ri->ri_attrs[i].an_name.bv_len;
1776                 }
1777         }
1778         bindconf_unparse( &ri->ri_bindconf, &bc );
1779         len += bc.bv_len;
1780
1781         bv->bv_val = ch_malloc(len + 1);
1782         bv->bv_len = len;
1783
1784         ptr = lutil_strcopy( bv->bv_val, numbuf );
1785         ptr = lutil_strcopy( ptr, "uri=" );
1786         ptr = lutil_strcopy( ptr, ri->ri_uri );
1787
1788         if ( ri->ri_nsuffix ) {
1789                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
1790                         ptr = lutil_strcopy( ptr, " suffix=\"" );
1791                         ptr = lutil_strcopy( ptr, ri->ri_nsuffix[i].bv_val );
1792                         *ptr++ = '"';
1793                 }
1794         }
1795         if ( ri->ri_attrs ) {
1796                 ptr = lutil_strcopy( ptr, "attr" );
1797                 if ( ri->ri_exclude ) *ptr++ = '!';
1798                 *ptr++ = '=';
1799                 ptr = anlist_unparse( ri->ri_attrs, ptr );
1800         }
1801         if ( bc.bv_val ) {
1802                 strcpy( ptr, bc.bv_val );
1803                 ch_free( bc.bv_val );
1804         }
1805 }
1806
1807 static int
1808 config_replica(ConfigArgs *c) {
1809         int i, nr = -1, len;
1810         char *replicahost, *replicauri;
1811         LDAPURLDesc *ludp;
1812
1813         if (c->op == SLAP_CONFIG_EMIT) {
1814                 if (c->be->be_replica) {
1815                         struct berval bv;
1816                         for (i=0;c->be->be_replica[i]; i++) {
1817                                 replica_unparse( c->be->be_replica[i], i, &bv );
1818                                 ber_bvarray_add( &c->rvalue_vals, &bv );
1819                         }
1820                         return 0;
1821                 }
1822                 return 1;
1823         }
1824         if(SLAP_MONITOR(c->be)) {
1825                 Debug(LDAP_DEBUG_ANY, "%s: "
1826                         "\"replica\" should not be used inside monitor database\n",
1827                         c->log, 0, 0);
1828                 return(0);      /* FIXME: should this be an error? */
1829         }
1830
1831         for(i = 1; i < c->argc; i++) {
1832                 if(!strncasecmp(c->argv[i], "host=", STRLENOF("host="))) {
1833                         replicahost = c->argv[i] + STRLENOF("host=");
1834                         len = strlen( replicahost );
1835                         replicauri = ch_malloc( len + STRLENOF("ldap://") + 1 );
1836                         sprintf( replicauri, "ldap://%s", replicahost );
1837                         replicahost = replicauri + STRLENOF( "ldap://");
1838                         nr = add_replica_info(c->be, replicauri, replicahost);
1839                         break;
1840                 } else if(!strncasecmp(c->argv[i], "uri=", STRLENOF("uri="))) {
1841                         if(ldap_url_parse(c->argv[i] + STRLENOF("uri="), &ludp) != LDAP_SUCCESS) {
1842                                 Debug(LDAP_DEBUG_ANY, "%s: "
1843                                         "replica line contains invalid "
1844                                         "uri definition.\n", c->log, 0, 0);
1845                                 return(1);
1846                         }
1847                         if(!ludp->lud_host) {
1848                                 Debug(LDAP_DEBUG_ANY, "%s: "
1849                                         "replica line contains invalid "
1850                                         "uri definition - missing hostname.\n",
1851                                         c->log, 0, 0);
1852                                 return(1);
1853                         }
1854                         ldap_free_urldesc(ludp);
1855                         replicauri = c->argv[i] + STRLENOF("uri=");
1856                         replicauri = ch_strdup( replicauri );
1857                         replicahost = strchr( replicauri, '/' );
1858                         replicahost += 2;
1859                         nr = add_replica_info(c->be, replicauri, replicahost);
1860                         break;
1861                 }
1862         }
1863         if(i == c->argc) {
1864                 Debug(LDAP_DEBUG_ANY, "%s: "
1865                         "missing host or uri in \"replica\" line\n",
1866                         c->log, 0, 0);
1867                 return(1);
1868         } else if(nr == -1) {
1869                 Debug(LDAP_DEBUG_ANY, "%s: "
1870                         "unable to add replica \"%s\"\n",
1871                         c->log, replicauri, 0);
1872                 return(1);
1873         } else {
1874                 for(i = 1; i < c->argc; i++) {
1875                         if(!strncasecmp(c->argv[i], "suffix=", STRLENOF( "suffix="))) {
1876                                 switch(add_replica_suffix(c->be, nr, c->argv[i] + STRLENOF("suffix="))) {
1877                                         case 1:
1878                                                 Debug(LDAP_DEBUG_ANY, "%s: "
1879                                                 "suffix \"%s\" in \"replica\" line is not valid for backend (ignored)\n",
1880                                                 c->log, c->argv[i] + STRLENOF("suffix="), 0);
1881                                                 break;
1882                                         case 2:
1883                                                 Debug(LDAP_DEBUG_ANY, "%s: "
1884                                                 "unable to normalize suffix in \"replica\" line (ignored)\n",
1885                                                 c->log, 0, 0);
1886                                                 break;
1887                                 }
1888
1889                         } else if(!strncasecmp(c->argv[i], "attr", STRLENOF("attr"))) {
1890                                 int exclude = 0;
1891                                 char *arg = c->argv[i] + STRLENOF("attr");
1892                                 if(arg[0] == '!') {
1893                                         arg++;
1894                                         exclude = 1;
1895                                 }
1896                                 if(arg[0] != '=') {
1897                                         continue;
1898                                 }
1899                                 if(add_replica_attrs(c->be, nr, arg + 1, exclude)) {
1900                                         Debug(LDAP_DEBUG_ANY, "%s: "
1901                                                 "attribute \"%s\" in \"replica\" line is unknown\n",
1902                                                 c->log, arg + 1, 0);
1903                                         return(1);
1904                                 }
1905                         } else if ( bindconf_parse( c->argv[i],
1906                                         &c->be->be_replica[nr]->ri_bindconf ) ) {
1907                                 return(1);
1908                         }
1909                 }
1910         }
1911         return(0);
1912 }
1913
1914 static int
1915 config_updatedn(ConfigArgs *c) {
1916         struct berval dn;
1917         int rc;
1918         if (c->op == SLAP_CONFIG_EMIT) {
1919                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
1920                         value_add_one(&c->rvalue_vals, &c->be->be_update_ndn);
1921                         value_add_one(&c->rvalue_nvals, &c->be->be_update_ndn);
1922                         return 0;
1923                 }
1924                 return 1;
1925         }
1926         if(SLAP_SHADOW(c->be)) {
1927                 Debug(LDAP_DEBUG_ANY, "%s: "
1928                         "updatedn: database already shadowed.\n",
1929                         c->log, 0, 0);
1930                 return(1);
1931         }
1932
1933         ber_str2bv(c->argv[1], 0, 0, &dn);
1934
1935         rc = dnNormalize(0, NULL, NULL, &dn, &c->be->be_update_ndn, NULL);
1936
1937         if(rc != LDAP_SUCCESS) {
1938                 Debug(LDAP_DEBUG_ANY, "%s: "
1939                         "updatedn DN is invalid: %d (%s)\n",
1940                         c->log, rc, ldap_err2string( rc ));
1941                 return(1);
1942         }
1943
1944         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
1945         return(0);
1946 }
1947
1948 static int
1949 config_updateref(ConfigArgs *c) {
1950         struct berval vals[2];
1951         if (c->op == SLAP_CONFIG_EMIT) {
1952                 if ( c->be->be_update_refs ) {
1953                         value_add( &c->rvalue_vals, c->be->be_update_refs );
1954                         return 0;
1955                 } else {
1956                         return 1;
1957                 }
1958         }
1959         if(!SLAP_SHADOW(c->be)) {
1960                 Debug(LDAP_DEBUG_ANY, "%s: "
1961                         "updateref line must come after syncrepl or updatedn.\n",
1962                         c->log, 0, 0);
1963                 return(1);
1964         }
1965
1966         if(validate_global_referral(c->argv[1])) {
1967                 Debug(LDAP_DEBUG_ANY, "%s: "
1968                         "invalid URL (%s) in \"updateref\" line.\n",
1969                         c->log, c->argv[1], 0);
1970                 return(1);
1971         }
1972         ber_str2bv(c->argv[1], 0, 0, &vals[0]);
1973         vals[1].bv_val = NULL;
1974         if(value_add(&c->be->be_update_refs, vals)) return(LDAP_OTHER);
1975         return(0);
1976 }
1977
1978 static int
1979 config_include(ConfigArgs *c) {
1980         unsigned long savelineno = c->lineno;
1981         int rc;
1982         ConfigFile *cf;
1983         ConfigFile *cfsave = cfn;
1984         ConfigFile *cf2 = NULL;
1985         if (c->op == SLAP_CONFIG_EMIT) {
1986                 return 1;
1987         }
1988         cf = ch_calloc( 1, sizeof(ConfigFile));
1989         if ( cfn->c_kids ) {
1990                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
1991                 cf2->c_sibs = cf;
1992         } else {
1993                 cfn->c_kids = cf;
1994         }
1995         cfn = cf;
1996         ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
1997         rc = read_config_file(c->argv[1], c->depth + 1, c);
1998         c->lineno = savelineno - 1;
1999         cfn = cfsave;
2000         if ( rc ) {
2001                 if ( cf2 ) cf2->c_sibs = NULL;
2002                 else cfn->c_kids = NULL;
2003                 ch_free( cf->c_file.bv_val );
2004                 ch_free( cf );
2005         }
2006         return(rc);
2007 }
2008
2009 #ifdef HAVE_TLS
2010 static int
2011 config_tls_option(ConfigArgs *c) {
2012         int flag;
2013         switch(c->type) {
2014         case CFG_TLS_RAND:              flag = LDAP_OPT_X_TLS_RANDOM_FILE;      break;
2015         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
2016         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
2017         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
2018         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
2019         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
2020         default:                Debug(LDAP_DEBUG_ANY, "%s: "
2021                                         "unknown tls_option <%x>\n",
2022                                         c->log, c->type, 0);
2023         }
2024         if (c->op == SLAP_CONFIG_EMIT) {
2025                 return ldap_pvt_tls_get_option( NULL, flag, &c->value_string );
2026         }
2027         ch_free(c->value_string);
2028         return(ldap_pvt_tls_set_option(NULL, flag, c->argv[1]));
2029 }
2030
2031 /* FIXME: this ought to be provided by libldap */
2032 static int
2033 config_tls_config(ConfigArgs *c) {
2034         int i, flag;
2035         slap_verbmasks crlkeys[] = {
2036                 { BER_BVC("none"),      LDAP_OPT_X_TLS_CRL_NONE },
2037                 { BER_BVC("peer"),      LDAP_OPT_X_TLS_CRL_PEER },
2038                 { BER_BVC("all"),       LDAP_OPT_X_TLS_CRL_ALL },
2039                 { BER_BVNULL, 0 }
2040         };
2041         slap_verbmasks vfykeys[] = {
2042                 { BER_BVC("never"),     LDAP_OPT_X_TLS_NEVER },
2043                 { BER_BVC("demand"),    LDAP_OPT_X_TLS_DEMAND },
2044                 { BER_BVC("try"),       LDAP_OPT_X_TLS_TRY },
2045                 { BER_BVC("hard"),      LDAP_OPT_X_TLS_HARD },
2046                 { BER_BVNULL, 0 }
2047         }, *keys;
2048         switch(c->type) {
2049 #ifdef HAVE_OPENSSL_CRL
2050         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK; keys = crlkeys;
2051                 break;
2052 #endif
2053         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT; keys = vfykeys;
2054                 break;
2055         default:                Debug(LDAP_DEBUG_ANY, "%s: "
2056                                         "unknown tls_option <%x>\n",
2057                                         c->log, c->type, 0);
2058         }
2059         if (c->op == SLAP_CONFIG_EMIT) {
2060                 ldap_pvt_tls_get_option( NULL, flag, &c->value_int );
2061                 for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
2062                         if (keys[i].mask == c->value_int) {
2063                                 c->value_string = ch_strdup( keys[i].word.bv_val );
2064                                 return 0;
2065                         }
2066                 }
2067                 return 1;
2068         }
2069         ch_free( c->value_string );
2070         if(isdigit((unsigned char)c->argv[1][0])) {
2071                 i = atoi(c->argv[1]);
2072                 return(ldap_pvt_tls_set_option(NULL, flag, &i));
2073         } else {
2074                 return(ldap_int_tls_config(NULL, flag, c->argv[1]));
2075         }
2076 }
2077 #endif
2078
2079 static int
2080 add_syncrepl(
2081         Backend *be,
2082         char    **cargv,
2083         int     cargc
2084 )
2085 {
2086         syncinfo_t *si;
2087         int     rc = 0;
2088
2089         si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
2090
2091         if ( si == NULL ) {
2092                 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
2093                 return 1;
2094         }
2095
2096         si->si_bindconf.sb_tls = SB_TLS_OFF;
2097         si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
2098         si->si_schemachecking = 0;
2099         ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
2100                 &si->si_filterstr );
2101         si->si_base.bv_val = NULL;
2102         si->si_scope = LDAP_SCOPE_SUBTREE;
2103         si->si_attrsonly = 0;
2104         si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
2105         si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
2106         si->si_attrs = NULL;
2107         si->si_allattrs = 0;
2108         si->si_allopattrs = 0;
2109         si->si_exattrs = NULL;
2110         si->si_type = LDAP_SYNC_REFRESH_ONLY;
2111         si->si_interval = 86400;
2112         si->si_retryinterval = NULL;
2113         si->si_retrynum_init = NULL;
2114         si->si_retrynum = NULL;
2115         si->si_manageDSAit = 0;
2116         si->si_tlimit = 0;
2117         si->si_slimit = 0;
2118
2119         si->si_presentlist = NULL;
2120         LDAP_LIST_INIT( &si->si_nonpresentlist );
2121         ldap_pvt_thread_mutex_init( &si->si_mutex );
2122
2123         rc = parse_syncrepl_line( cargv, cargc, si );
2124
2125         if ( rc < 0 ) {
2126                 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
2127                 syncinfo_free( si );    
2128                 return 1;
2129         } else {
2130                 Debug( LDAP_DEBUG_CONFIG,
2131                         "Config: ** successfully added syncrepl \"%s\"\n",
2132                         BER_BVISNULL( &si->si_provideruri ) ?
2133                         "(null)" : si->si_provideruri.bv_val, 0, 0 );
2134                 if ( !si->si_schemachecking ) {
2135                         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
2136                 }
2137                 si->si_be = be;
2138                 be->be_syncinfo = si;
2139                 return 0;
2140         }
2141 }
2142
2143 /* NOTE: used & documented in slapd.conf(5) */
2144 #define IDSTR                   "rid"
2145 #define PROVIDERSTR             "provider"
2146 #define TYPESTR                 "type"
2147 #define INTERVALSTR             "interval"
2148 #define SEARCHBASESTR           "searchbase"
2149 #define FILTERSTR               "filter"
2150 #define SCOPESTR                "scope"
2151 #define ATTRSSTR                "attrs"
2152 #define ATTRSONLYSTR            "attrsonly"
2153 #define SLIMITSTR               "sizelimit"
2154 #define TLIMITSTR               "timelimit"
2155 #define SCHEMASTR               "schemachecking"
2156
2157 /* FIXME: undocumented */
2158 #define OLDAUTHCSTR             "bindprincipal"
2159 #define EXATTRSSTR              "exattrs"
2160 #define RETRYSTR                "retry"
2161
2162 /* FIXME: unused */
2163 #define LASTMODSTR              "lastmod"
2164 #define LMGENSTR                "gen"
2165 #define LMNOSTR                 "no"
2166 #define LMREQSTR                "req"
2167 #define SRVTABSTR               "srvtab"
2168 #define SUFFIXSTR               "suffix"
2169 #define MANAGEDSAITSTR          "manageDSAit"
2170
2171 /* mandatory */
2172 #define GOT_ID                  0x0001
2173 #define GOT_PROVIDER            0x0002
2174
2175 /* check */
2176 #define GOT_ALL                 (GOT_ID|GOT_PROVIDER)
2177
2178 static struct {
2179         struct berval key;
2180         int val;
2181 } scopes[] = {
2182         { BER_BVC("base"), LDAP_SCOPE_BASE },
2183         { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2184 #ifdef LDAP_SCOPE_SUBORDINATE
2185         { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2186         { BER_BVC("subordinate"), 0 },
2187 #endif
2188         { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2189         { BER_BVNULL, 0 }
2190 };
2191
2192 static int
2193 parse_syncrepl_line(
2194         char            **cargv,
2195         int             cargc,
2196         syncinfo_t      *si
2197 )
2198 {
2199         int     gots = 0;
2200         int     i;
2201         char    *val;
2202
2203         for ( i = 1; i < cargc; i++ ) {
2204                 if ( !strncasecmp( cargv[ i ], IDSTR "=",
2205                                         STRLENOF( IDSTR "=" ) ) )
2206                 {
2207                         int tmp;
2208                         /* '\0' string terminator accounts for '=' */
2209                         val = cargv[ i ] + STRLENOF( IDSTR "=" );
2210                         tmp= atoi( val );
2211                         if ( tmp >= 1000 || tmp < 0 ) {
2212                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2213                                          "syncrepl id %d is out of range [0..999]\n", tmp );
2214                                 return -1;
2215                         }
2216                         si->si_rid = tmp;
2217                         gots |= GOT_ID;
2218                 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
2219                                         STRLENOF( PROVIDERSTR "=" ) ) )
2220                 {
2221                         val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
2222                         ber_str2bv( val, 0, 1, &si->si_provideruri );
2223                         gots |= GOT_PROVIDER;
2224                 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
2225                                         STRLENOF( SCHEMASTR "=" ) ) )
2226                 {
2227                         val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
2228                         if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2229                                 si->si_schemachecking = 1;
2230                         } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2231                                 si->si_schemachecking = 0;
2232                         } else {
2233                                 si->si_schemachecking = 1;
2234                         }
2235                 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
2236                                         STRLENOF( FILTERSTR "=" ) ) )
2237                 {
2238                         val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
2239                         ber_str2bv( val, 0, 1, &si->si_filterstr );
2240                 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
2241                                         STRLENOF( SEARCHBASESTR "=" ) ) )
2242                 {
2243                         struct berval   bv;
2244                         int             rc;
2245
2246                         val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2247                         if ( si->si_base.bv_val ) {
2248                                 ch_free( si->si_base.bv_val );
2249                         }
2250                         ber_str2bv( val, 0, 0, &bv );
2251                         rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2252                         if ( rc != LDAP_SUCCESS ) {
2253                                 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
2254                                         val, rc, ldap_err2string( rc ) );
2255                                 return -1;
2256                         }
2257                 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
2258                                         STRLENOF( SCOPESTR "=" ) ) )
2259                 {
2260                         int j;
2261                         val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
2262                         for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2263                                 if (!strncasecmp( val, scopes[j].key.bv_val,
2264                                         scopes[j].key.bv_len )) {
2265                                         while (!scopes[j].val) j--;
2266                                         si->si_scope = scopes[j].val;
2267                                         break;
2268                                 }
2269                         }
2270                         if ( BER_BVISNULL(&scopes[j].key) ) {
2271                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2272                                         "unknown scope \"%s\"\n", val);
2273                                 return -1;
2274                         }
2275                 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
2276                                         STRLENOF( ATTRSONLYSTR "=" ) ) )
2277                 {
2278                         si->si_attrsonly = 1;
2279                 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
2280                                         STRLENOF( ATTRSSTR "=" ) ) )
2281                 {
2282                         val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
2283                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2284                                 char *attr_fname;
2285                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2286                                 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2287                                 if ( si->si_anlist == NULL ) {
2288                                         ch_free( attr_fname );
2289                                         return -1;
2290                                 }
2291                                 si->si_anfile = attr_fname;
2292                         } else {
2293                                 char *str, *s, *next;
2294                                 char delimstr[] = " ,\t";
2295                                 str = ch_strdup( val );
2296                                 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2297                                                 s != NULL;
2298                                                 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2299                                 {
2300                                         if ( strlen(s) == 1 && *s == '*' ) {
2301                                                 si->si_allattrs = 1;
2302                                                 *(val + ( s - str )) = delimstr[0];
2303                                         }
2304                                         if ( strlen(s) == 1 && *s == '+' ) {
2305                                                 si->si_allopattrs = 1;
2306                                                 *(val + ( s - str )) = delimstr[0];
2307                                         }
2308                                 }
2309                                 ch_free( str );
2310                                 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2311                                 if ( si->si_anlist == NULL ) {
2312                                         return -1;
2313                                 }
2314                         }
2315                 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
2316                                         STRLENOF( EXATTRSSTR "=" ) ) )
2317                 {
2318                         val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
2319                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2320                                 char *attr_fname;
2321                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2322                                 si->si_exanlist = file2anlist(
2323                                                                         si->si_exanlist, attr_fname, " ,\t" );
2324                                 if ( si->si_exanlist == NULL ) {
2325                                         ch_free( attr_fname );
2326                                         return -1;
2327                                 }
2328                                 ch_free( attr_fname );
2329                         } else {
2330                                 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2331                                 if ( si->si_exanlist == NULL ) {
2332                                         return -1;
2333                                 }
2334                         }
2335                 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
2336                                         STRLENOF( TYPESTR "=" ) ) )
2337                 {
2338                         val = cargv[ i ] + STRLENOF( TYPESTR "=" );
2339                         if ( !strncasecmp( val, "refreshOnly",
2340                                                 STRLENOF("refreshOnly") ))
2341                         {
2342                                 si->si_type = LDAP_SYNC_REFRESH_ONLY;
2343                         } else if ( !strncasecmp( val, "refreshAndPersist",
2344                                                 STRLENOF("refreshAndPersist") ))
2345                         {
2346                                 si->si_type = LDAP_SYNC_REFRESH_AND_PERSIST;
2347                                 si->si_interval = 60;
2348                         } else {
2349                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2350                                         "unknown sync type \"%s\"\n", val);
2351                                 return -1;
2352                         }
2353                 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2354                                         STRLENOF( INTERVALSTR "=" ) ) )
2355                 {
2356                         val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2357                         if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2358                                 si->si_interval = 0;
2359                         } else {
2360                                 char *hstr;
2361                                 char *mstr;
2362                                 char *dstr;
2363                                 char *sstr;
2364                                 int dd, hh, mm, ss;
2365                                 dstr = val;
2366                                 hstr = strchr( dstr, ':' );
2367                                 if ( hstr == NULL ) {
2368                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2369                                                 "invalid interval \"%s\"\n", val );
2370                                         return -1;
2371                                 }
2372                                 *hstr++ = '\0';
2373                                 mstr = strchr( hstr, ':' );
2374                                 if ( mstr == NULL ) {
2375                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2376                                                 "invalid interval \"%s\"\n", val );
2377                                         return -1;
2378                                 }
2379                                 *mstr++ = '\0';
2380                                 sstr = strchr( mstr, ':' );
2381                                 if ( sstr == NULL ) {
2382                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2383                                                 "invalid interval \"%s\"\n", val );
2384                                         return -1;
2385                                 }
2386                                 *sstr++ = '\0';
2387
2388                                 dd = atoi( dstr );
2389                                 hh = atoi( hstr );
2390                                 mm = atoi( mstr );
2391                                 ss = atoi( sstr );
2392                                 if (( hh > 24 ) || ( hh < 0 ) ||
2393                                         ( mm > 60 ) || ( mm < 0 ) ||
2394                                         ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2395                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2396                                                 "invalid interval \"%s\"\n", val );
2397                                         return -1;
2398                                 }
2399                                 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2400                         }
2401                         if ( si->si_interval < 0 ) {
2402                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2403                                         "invalid interval \"%ld\"\n",
2404                                         (long) si->si_interval);
2405                                 return -1;
2406                         }
2407                 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2408                                         STRLENOF( RETRYSTR "=" ) ) )
2409                 {
2410                         char **retry_list;
2411                         int j, k, n;
2412
2413                         val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2414                         retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2415                         retry_list[0] = NULL;
2416
2417                         slap_str2clist( &retry_list, val, " ,\t" );
2418
2419                         for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2420                         n = k / 2;
2421                         if ( k % 2 ) {
2422                                 fprintf( stderr,
2423                                                 "Error: incomplete syncrepl retry list\n" );
2424                                 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2425                                         ch_free( retry_list[k] );
2426                                 }
2427                                 ch_free( retry_list );
2428                                 exit( EXIT_FAILURE );
2429                         }
2430                         si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2431                         si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2432                         si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2433                         for ( j = 0; j < n; j++ ) {
2434                                 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2435                                 if ( *retry_list[j*2+1] == '+' ) {
2436                                         si->si_retrynum_init[j] = -1;
2437                                         si->si_retrynum[j] = -1;
2438                                         j++;
2439                                         break;
2440                                 } else {
2441                                         si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2442                                         si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2443                                 }
2444                         }
2445                         si->si_retrynum_init[j] = -2;
2446                         si->si_retrynum[j] = -2;
2447                         si->si_retryinterval[j] = 0;
2448                         
2449                         for ( k = 0; retry_list && retry_list[k]; k++ ) {
2450                                 ch_free( retry_list[k] );
2451                         }
2452                         ch_free( retry_list );
2453                 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
2454                                         STRLENOF( MANAGEDSAITSTR "=" ) ) )
2455                 {
2456                         val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
2457                         si->si_manageDSAit = atoi( val );
2458                 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
2459                                         STRLENOF( SLIMITSTR "=") ) )
2460                 {
2461                         val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
2462                         si->si_slimit = atoi( val );
2463                 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
2464                                         STRLENOF( TLIMITSTR "=" ) ) )
2465                 {
2466                         val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
2467                         si->si_tlimit = atoi( val );
2468                 } else if ( bindconf_parse( cargv[i], &si->si_bindconf )) {
2469                         fprintf( stderr, "Error: parse_syncrepl_line: "
2470                                 "unknown keyword \"%s\"\n", cargv[ i ] );
2471                         return -1;
2472                 }
2473         }
2474
2475         if ( gots != GOT_ALL ) {
2476                 fprintf( stderr,
2477                         "Error: Malformed \"syncrepl\" line in slapd config file" );
2478                 return -1;
2479         }
2480
2481         return 0;
2482 }
2483
2484 static void
2485 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
2486 {
2487         struct berval bc;
2488         char buf[BUFSIZ*2], *ptr;
2489         int i, len;
2490
2491         bindconf_unparse( &si->si_bindconf, &bc );
2492         ptr = buf;
2493         ptr += sprintf( ptr, IDSTR "=%03d " PROVIDERSTR "=%s",
2494                 si->si_rid, si->si_provideruri.bv_val );
2495         if ( !BER_BVISNULL( &bc )) {
2496                 ptr = lutil_strcopy( ptr, bc.bv_val );
2497                 free( bc.bv_val );
2498         }
2499         if ( !BER_BVISEMPTY( &si->si_filterstr )) {
2500                 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
2501                 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
2502                 *ptr++ = '"';
2503         }
2504         if ( !BER_BVISNULL( &si->si_base )) {
2505                 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
2506                 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
2507                 *ptr++ = '"';
2508         }
2509         for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
2510                 if ( si->si_scope == scopes[i].val ) {
2511                         ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
2512                         ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
2513                         break;
2514                 }
2515         }
2516         if ( si->si_attrsonly ) {
2517                 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR "=yes" );
2518         }
2519         if ( si->si_anfile ) {
2520                 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
2521                 ptr = lutil_strcopy( ptr, si->si_anfile );
2522         } else if ( si->si_allattrs || si->si_allopattrs ||
2523                 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
2524                 char *old;
2525                 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
2526                 old = ptr;
2527                 ptr = anlist_unparse( si->si_anlist, ptr );
2528                 if ( si->si_allattrs ) {
2529                         if ( old != ptr ) *ptr++ = ',';
2530                         *ptr++ = '*';
2531                 }
2532                 if ( si->si_allopattrs ) {
2533                         if ( old != ptr ) *ptr++ = ',';
2534                         *ptr++ = '+';
2535                 }
2536                 *ptr++ = '"';
2537         }
2538         if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
2539                 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
2540                 ptr = anlist_unparse( si->si_exanlist, ptr );
2541         }
2542         ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
2543         ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
2544         
2545         ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
2546         ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
2547                 "refreshAndPersist" : "refreshOnly" );
2548
2549         if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
2550                 int dd, hh, mm, ss;
2551
2552                 dd = si->si_interval;
2553                 ss = dd % 60;
2554                 dd /= 60;
2555                 mm = dd % 60;
2556                 dd /= 60;
2557                 hh = dd % 24;
2558                 dd /= 24;
2559                 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
2560                 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
2561         } else if ( si->si_retryinterval ) {
2562                 int space=0;
2563                 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
2564                 for (i=0; si->si_retryinterval[i]; i++) {
2565                         if ( space ) *ptr++ = ' ';
2566                         space = 1;
2567                         ptr += sprintf( ptr, "%d", si->si_retryinterval[i] );
2568                         if ( si->si_retrynum_init[i] == -1 )
2569                                 *ptr++ = '+';
2570                         else
2571                                 ptr += sprintf( ptr, "%d", si->si_retrynum_init );
2572                 }
2573                 *ptr++ = '"';
2574         }
2575
2576 #if 0 /* FIXME: unused in syncrepl.c, should remove it */
2577         ptr = lutil_strcopy( ptr, " " MANAGEDSAITSTR "=" );
2578         ptr += sprintf( ptr, "%d", si->si_manageDSAit );
2579 #endif
2580
2581         if ( si->si_slimit ) {
2582                 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
2583                 ptr += sprintf( ptr, "%d", si->si_slimit );
2584         }
2585
2586         if ( si->si_tlimit ) {
2587                 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
2588                 ptr += sprintf( ptr, "%d", si->si_tlimit );
2589         }
2590         bc.bv_len = ptr - buf;
2591         bc.bv_val = buf;
2592         ber_dupbv( bv, &bc );
2593 }
2594
2595 static CfEntryInfo *
2596 config_find_base( CfEntryInfo *root, struct berval *dn, CfEntryInfo **last )
2597 {
2598         struct berval cdn;
2599         char *c;
2600
2601         if ( !root ) {
2602                 *last = NULL;
2603                 return NULL;
2604         }
2605
2606         if ( dn_match( &root->ce_entry->e_nname, dn ))
2607                 return root;
2608
2609         c = dn->bv_val+dn->bv_len;
2610         for (;*c != ',';c--);
2611
2612         while(root) {
2613                 *last = root;
2614                 for (--c;c>dn->bv_val && *c != ',';c--);
2615                 if ( *c == ',' )
2616                         c++;
2617                 cdn.bv_val = c;
2618                 cdn.bv_len = dn->bv_len - (c-dn->bv_val);
2619
2620                 root = root->ce_kids;
2621
2622                 for (;root;root=root->ce_sibs) {
2623                         if ( dn_match( &root->ce_entry->e_nname, &cdn )) {
2624                                 if ( cdn.bv_val == dn->bv_val ) {
2625                                         return root;
2626                                 }
2627                                 break;
2628                         }
2629                 }
2630         }
2631         return root;
2632 }
2633
2634 static int
2635 config_ldif_resp( Operation *op, SlapReply *rs )
2636 {
2637         if ( rs->sr_type == REP_SEARCH ) {
2638                 CfBackInfo *cfb = op->o_callback->sc_private;
2639
2640                 cfb->cb_got_ldif = 1;
2641                 rs->sr_err = config_add_internal( cfb, rs->sr_entry, NULL );
2642         }
2643         return rs->sr_err;
2644 }
2645
2646 /* Configure and read the underlying back-ldif store */
2647 static int
2648 config_setup_ldif( BackendDB *be, const char *dir ) {
2649         CfBackInfo *cfb = be->be_private;
2650         ConfigArgs c = {0};
2651         ConfigTable *ct;
2652         char *argv[3];
2653         int rc;
2654         slap_callback cb = { NULL, config_ldif_resp, NULL, NULL };
2655         Connection conn = {0};
2656         char opbuf[OPERATION_BUFFER_SIZE];
2657         Operation *op;
2658         SlapReply rs = {REP_RESULT};
2659         Filter filter = { LDAP_FILTER_PRESENT };
2660         struct berval filterstr = BER_BVC("(objectclass=*)");
2661
2662         cfb->cb_db.bd_info = backend_info( "ldif" );
2663         if ( !cfb->cb_db.bd_info )
2664                 return 0;       /* FIXME: eventually this will be a fatal error */
2665
2666         if ( cfb->cb_db.bd_info->bi_db_init( &cfb->cb_db )) return 1;
2667
2668         /* Mark that back-ldif type is in use */
2669         cfb->cb_db.bd_info->bi_nDB++;
2670
2671         cfb->cb_db.be_suffix = be->be_suffix;
2672         cfb->cb_db.be_nsuffix = be->be_nsuffix;
2673         cfb->cb_db.be_rootdn = be->be_rootdn;
2674         cfb->cb_db.be_rootndn = be->be_rootndn;
2675
2676         ber_str2bv( dir, 0, 1, &cfdir );
2677
2678         c.be = &cfb->cb_db;
2679         c.fname = "slapd";
2680         c.argc = 2;
2681         argv[0] = "directory";
2682         argv[1] = (char *)dir;
2683         argv[2] = NULL;
2684         c.argv = argv;
2685
2686         ct = config_find_keyword( c.be->be_cf_table, &c );
2687         if ( !ct )
2688                 return 1;
2689
2690         if ( config_add_vals( ct, &c ))
2691                 return 1;
2692
2693         if ( backend_startup_one( &cfb->cb_db ))
2694                 return 1;
2695
2696         op = (Operation *)opbuf;
2697         connection_fake_init( &conn, op, cfb );
2698
2699         filter.f_desc = slap_schema.si_ad_objectClass;
2700         
2701         op->o_tag = LDAP_REQ_SEARCH;
2702
2703         op->ors_filter = &filter;
2704         op->ors_filterstr = filterstr;
2705         op->ors_scope = LDAP_SCOPE_SUBTREE;
2706
2707         op->o_dn = be->be_rootdn;
2708         op->o_ndn = be->be_rootndn;
2709
2710         op->o_req_dn = be->be_suffix[0];
2711         op->o_req_ndn = be->be_nsuffix[0];
2712
2713         op->ors_tlimit = SLAP_NO_LIMIT;
2714         op->ors_slimit = SLAP_NO_LIMIT;
2715
2716         op->ors_attrs = slap_anlist_all_attributes;
2717         op->ors_attrsonly = 0;
2718
2719         op->o_callback = &cb;
2720         cb.sc_private = cfb;
2721
2722         op->o_bd = &cfb->cb_db;
2723         op->o_bd->be_search( op, &rs );
2724         
2725         return 0;
2726 }
2727
2728 static int
2729 CfOcInfo_cmp( const void *c1, const void *c2 ) {
2730         const CfOcInfo *co1 = c1;
2731         const CfOcInfo *co2 = c2;
2732
2733         return ber_bvcmp( co1->co_name, co2->co_name );
2734 }
2735
2736 int
2737 config_register_schema(ConfigTable *ct, ConfigOCs *ocs) {
2738         int i;
2739         CfOcInfo *co;
2740
2741         i = init_config_attrs( ct );
2742         if ( i ) return i;
2743
2744         /* set up the objectclasses */
2745         i = init_config_ocs( ocs );
2746         if ( i ) return i;
2747
2748         for (i=0; ocs[i].def; i++) {
2749                 if ( ocs[i].oc ) {
2750                         co = ch_malloc( sizeof(CfOcInfo) );
2751                         co->co_name = &(*ocs[i].oc)->soc_cname;
2752                         co->co_table = ct;
2753                         co->co_type = ocs[i].cft;
2754                         avl_insert( &CfOcTree, co, CfOcInfo_cmp, avl_dup_error );
2755                 }
2756         }
2757         return 0;
2758 }
2759
2760 int
2761 read_config(const char *fname, const char *dir) {
2762         BackendDB *be;
2763         CfBackInfo *cfb;
2764
2765         /* Setup the config backend */
2766         be = backend_db_init( "config" );
2767         if ( !be )
2768                 return 1;
2769
2770         /* Setup the underlying back-ldif backend */
2771         if ( config_setup_ldif( be, dir ))
2772                 return 1;
2773
2774 #if 0   /* not yet
2775         /* If we read the config from back-ldif, nothing to do here */
2776         cfb = be->be_private;
2777         if ( cfb->cb_got_ldif )
2778                 return 0;
2779 #endif
2780         ber_str2bv( fname, 0, 1, &cf_prv.c_file );
2781
2782         return read_config_file(fname, 0, NULL);
2783 }
2784
2785 static int
2786 config_back_bind( Operation *op, SlapReply *rs )
2787 {
2788         if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op )) {
2789                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ));
2790                 /* frontend sends result */
2791                 return LDAP_SUCCESS;
2792         }
2793
2794         rs->sr_err = LDAP_INVALID_CREDENTIALS;
2795         send_ldap_result( op, rs );
2796
2797         return rs->sr_err;
2798 }
2799
2800 static int
2801 config_send( Operation *op, SlapReply *rs, CfEntryInfo *ce, int depth )
2802 {
2803         int rc = 0;
2804
2805         if ( test_filter( op, ce->ce_entry, op->ors_filter ) == LDAP_COMPARE_TRUE )
2806         {
2807                 rs->sr_attrs = op->ors_attrs;
2808                 rs->sr_entry = ce->ce_entry;
2809                 rc = send_search_entry( op, rs );
2810         }
2811         if ( op->ors_scope == LDAP_SCOPE_SUBTREE ) {
2812                 if ( ce->ce_kids ) {
2813                         rc = config_send( op, rs, ce->ce_kids, 1 );
2814                         if ( rc ) return rc;
2815                 }
2816                 if ( depth ) {
2817                         for (ce=ce->ce_sibs; ce; ce=ce->ce_sibs) {
2818                                 rc = config_send( op, rs, ce, 0 );
2819                                 if ( rc ) break;
2820                         }
2821                 }
2822         }
2823         return rc;
2824 }
2825
2826 static ConfigTable *
2827 config_find_table( CfOcInfo *co, AttributeDescription *ad )
2828 {
2829         int i;
2830
2831         for (i=0; co->co_table[i].name; i++)
2832                 if ( co->co_table[i].ad == ad )
2833                         return &co->co_table[i];
2834         return NULL;
2835 }
2836
2837 /* Sort the values in an X-ORDERED attribute.
2838  * If the values have no index, leave them in their given order.
2839  * If the values have indexes, sort them and then strip the index.
2840  * If some are indexed and some are not, return Error.
2841  *
2842  * FIXME: This function probably belongs in the frontend somewhere,
2843  * like slap_mods_check.
2844  */
2845 static int
2846 sort_vals( Attribute *a )
2847 {
2848         int i;
2849         int index = 0, noindex = 0;
2850
2851         /* count attrs, look for index */
2852         for (i=0; a->a_vals[i].bv_val; i++) {
2853                 if ( a->a_vals[i].bv_val[0] == '{' ) {
2854                         char *ptr;
2855                         index = 1;
2856                         ptr = strchr( a->a_vals[i].bv_val, '}' );
2857                         if ( !ptr || !ptr[1] )
2858                                 return LDAP_INVALID_SYNTAX;
2859                         if ( noindex )
2860                                 return LDAP_INVALID_SYNTAX;
2861                 } else {
2862                         noindex = 1;
2863                         if ( index )
2864                                 return LDAP_INVALID_SYNTAX;
2865                 }
2866         }
2867
2868         if ( index ) {
2869                 int vals = i, *indexes, j, idx;
2870                 struct berval tmp, ntmp;
2871                 char *ptr;
2872
2873                 /* Strip index from normalized values */
2874                 if ( !a->a_nvals || a->a_vals == a->a_nvals ) {
2875                         a->a_nvals = ch_malloc( (vals+1)*sizeof(struct berval));
2876                         for ( i=0; i<vals; i++ ) {
2877                                 ptr = strchr(a->a_vals[i].bv_val, '}') + 1;
2878                                 a->a_nvals[i].bv_len = a->a_vals[i].bv_len -
2879                                         (ptr - a->a_vals[i].bv_val);
2880                                 a->a_nvals[i].bv_val = ch_malloc( a->a_nvals[i].bv_len + 1);
2881                                 strcpy(a->a_nvals[i].bv_val, ptr );
2882                         }
2883                 } else {
2884                         for ( i=0; i<vals; i++ ) {
2885                                 ptr = strchr(a->a_nvals[i].bv_val, '}') + 1;
2886                                 a->a_nvals[i].bv_len -= ptr - a->a_nvals[i].bv_val;
2887                                 strcpy(a->a_nvals[i].bv_val, ptr);
2888                         }
2889                 }
2890                                 
2891                 indexes = ch_malloc( vals * sizeof(int) );
2892                 for ( i=0; i<vals; i++)
2893                         indexes[i] = atoi(a->a_vals[i].bv_val+1);
2894
2895                 /* Insertion sort */
2896                 for ( i=1; i<vals; i++ ) {
2897                         idx = indexes[i];
2898                         tmp = a->a_vals[i];
2899                         ntmp = a->a_nvals[i];
2900                         j = i;
2901                         while ((j > 0) && (indexes[j-1] > idx)) {
2902                                 indexes[j] = indexes[j-1];
2903                                 a->a_vals[j] = a->a_vals[j-1];
2904                                 a->a_nvals[j] = a->a_nvals[j-1];
2905                                 j--;
2906                         }
2907                         indexes[j] = idx;
2908                         a->a_vals[j] = tmp;
2909                         a->a_nvals[j] = ntmp;
2910                 }
2911         }
2912         return 0;
2913 }
2914
2915 static int
2916 check_attr( ConfigTable *ct, ConfigArgs *ca, Attribute *a )
2917 {
2918         int i, rc = 0;
2919
2920         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
2921                 rc = sort_vals( a );
2922                 if ( rc )
2923                         return rc;
2924         }
2925         for ( i=0; a->a_nvals[i].bv_val; i++ ) {
2926                 ca->line = a->a_nvals[i].bv_val;
2927                 rc = config_parse_vals( ct, ca, i );
2928                 if ( rc )
2929                         break;
2930         }
2931         return rc;
2932 }
2933
2934 /* Parse an LDAP entry into config directives */
2935 static int
2936 config_add_internal( CfBackInfo *cfb, Entry *e, SlapReply *rs )
2937 {
2938         CfEntryInfo *ce, *last;
2939         CfOcInfo co, *coptr, **colst = NULL;
2940         Attribute *a, *oc_at, *type_attr;
2941         AttributeDescription *type_ad = NULL;
2942         int i, j, nocs, rc;
2943         ConfigArgs ca = {0};
2944         struct berval pdn;
2945         Entry *xe = NULL;
2946         ConfigTable *ct, *type_ct = NULL;
2947
2948         /* Make sure parent exists and entry does not */
2949         ce = config_find_base( cfb->cb_root, &e->e_nname, &last );
2950         if ( ce )
2951                 return LDAP_ALREADY_EXISTS;
2952
2953         dnParent( &e->e_nname, &pdn );
2954
2955         /* If last is NULL, the new entry is the root/suffix entry, 
2956          * otherwise last should be the parent.
2957          */
2958         if ( last && !dn_match( &last->ce_entry->e_nname, &pdn )) {
2959                 if ( rs )
2960                         rs->sr_matched = last->ce_entry->e_name.bv_val;
2961                 return LDAP_NO_SUCH_OBJECT;
2962         }
2963
2964         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
2965         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
2966
2967         /* count the objectclasses */
2968         for ( i=0; oc_at->a_nvals[i].bv_val; i++ );
2969         nocs = i;
2970         colst = (CfOcInfo **)ch_malloc( nocs * sizeof(CfOcInfo *));
2971
2972         for ( i=0, j=0; i<nocs; i++) {
2973                 co.co_name = &oc_at->a_nvals[i];
2974                 coptr = avl_find( CfOcTree, &co, CfOcInfo_cmp );
2975                 
2976                 /* ignore non-config objectclasses. probably should be
2977                  * an error, general data doesn't belong here.
2978                  */
2979                 if ( !coptr ) continue;
2980
2981                 /* Ignore the root objectclass, it has no implementation.
2982                  */
2983                 if ( coptr->co_type == Cft_Abstract ) continue;
2984                 colst[j++] = coptr;
2985         }
2986         nocs = j;
2987
2988         /* Only the root can be Cft_Global, everything else must
2989          * have a parent. Only limited nesting arrangements are allowed.
2990          */
2991         switch( colst[0]->co_type ) {
2992         case Cft_Global:
2993                 if ( last )  {
2994                         rc = LDAP_CONSTRAINT_VIOLATION;
2995                         goto leave;
2996                 }
2997                 break;
2998         case Cft_Schema:
2999         case Cft_Backend:
3000         case Cft_Database:
3001         case Cft_Include:
3002                 if ( !last || ( last->ce_type != Cft_Global &&
3003                         last->ce_type != colst[0]->co_type )) {
3004                         rc = LDAP_CONSTRAINT_VIOLATION;
3005                         goto leave;
3006                 }
3007                 break;
3008         case Cft_Overlay:
3009                 if ( !last || ( last->ce_type != Cft_Global &&
3010                         last->ce_type != Cft_Database &&
3011                         last->ce_type != colst[0]->co_type )) {
3012                         rc = LDAP_CONSTRAINT_VIOLATION;
3013                         goto leave;
3014                 }
3015 #ifdef SLAPD_MODULES
3016         case Cft_Module:
3017                 if ( !last || last->ce_type != Cft_Global ) {
3018                         rc = LDAP_CONSTRAINT_VIOLATION;
3019                         goto leave;
3020                 }
3021 #endif
3022                 break;
3023         }
3024
3025         /* Parse all the values and check for simple syntax errors before
3026          * performing any set actions.
3027          */
3028         switch (colst[0]->co_type) {
3029         case Cft_Schema:
3030                 /* The cn=schema entry is all hardcoded, so never reparse it */
3031                 if (last->ce_type == Cft_Global )
3032                         goto ok;
3033                 /* FALLTHRU */
3034         case Cft_Global:
3035                 ca.be = cfb->cb_be;
3036                 break;
3037
3038         case Cft_Backend:
3039                 if ( last->ce_type == Cft_Backend )
3040                         ca.bi = last->ce_bi;
3041                 else
3042                         type_ad = cfAd_backend;
3043                 break;
3044         case Cft_Database:
3045                 if ( last->ce_type == Cft_Database ) {
3046                         ca.be = last->ce_be;
3047                 } else {
3048                         type_ad = cfAd_database;
3049                         /* dummy, just to get past check_attr */
3050                         ca.be = frontendDB;
3051                 }
3052                 break;
3053
3054         case Cft_Overlay:
3055                 ca.be = last->ce_be;
3056                 type_ad = cfAd_overlay;
3057
3058         case Cft_Include:
3059                 if ( !rs )      /* ignored */
3060                         break;
3061                 type_ad = cfAd_include;
3062 #ifdef SLAPD_MODULES
3063         case Cft_Module: {
3064                 ModPaths *mp;
3065                 char *ptr;
3066                 ptr = strchr( e->e_name.bv_val, '{' );
3067                 if ( !ptr ) {
3068                         rc = LDAP_NAMING_VIOLATION;
3069                         goto leave;
3070                 }
3071                 j = atoi(ptr+1);
3072                 for (i=0, mp=&modpaths; mp && i<j; mp=mp->mp_next);
3073                 /* There is no corresponding modpath for this load? */
3074                 if ( i != j ) {
3075                         rc = LDAP_NAMING_VIOLATION;
3076                         goto leave;
3077                 }
3078                 module_path( mp->mp_path.bv_val );
3079                 ca.private = mp;
3080                 }
3081                 break;
3082 #endif
3083         }
3084         init_config_argv( &ca );
3085         if ( type_ad ) {
3086                 type_attr = attr_find( e->e_attrs, type_ad );
3087                 if ( !type_attr ) {
3088                         rc = LDAP_OBJECT_CLASS_VIOLATION;
3089                         goto leave;
3090                 }
3091                 for ( i=0; i<nocs; i++ ) {
3092                         type_ct = config_find_table( colst[i], type_ad );
3093                         if ( type_ct ) break;
3094                 }
3095                 if ( !type_ct ) {
3096                         rc = LDAP_OBJECT_CLASS_VIOLATION;
3097                         goto leave;
3098                 }
3099                 rc = check_attr( type_ct, &ca, type_attr );
3100                 if ( rc ) goto leave;
3101         }
3102         for ( a=e->e_attrs; a; a=a->a_next ) {
3103                 if ( a == type_attr || a == oc_at ) continue;
3104                 ct = NULL;
3105                 for ( i=0; i<nocs; i++ ) {
3106                         ct = config_find_table( colst[i], a->a_desc );
3107                         if ( ct ) break;
3108                 }
3109                 if ( !ct ) continue;    /* user data? */
3110                 rc = check_attr( ct, &ca, a );
3111                 if ( rc ) goto leave;
3112         }
3113
3114         /* Basic syntax checks are OK. Do the actual settings. */
3115         if ( type_ct ) {
3116                 ca.line = type_attr->a_nvals[0].bv_val;
3117                 rc = config_parse_add( type_ct, &ca, 0 );
3118                 if ( rc ) goto leave;
3119         }
3120         for ( a=e->e_attrs; a; a=a->a_next ) {
3121                 if ( a == type_attr || a == oc_at ) continue;
3122                 ct = NULL;
3123                 for ( i=0; i<nocs; i++ ) {
3124                         ct = config_find_table( colst[i], a->a_desc );
3125                         if ( ct ) break;
3126                 }
3127                 if ( !ct ) continue;    /* user data? */
3128                 for (i=0; a->a_nvals[i].bv_val; i++) {
3129                         ca.line = a->a_nvals[i].bv_val;
3130                         rc = config_parse_add( ct, &ca, i );
3131                         if ( rc ) goto leave;
3132                 }
3133         }
3134 ok:
3135         ce = ch_calloc( 1, sizeof(CfEntryInfo) );
3136         ce->ce_parent = last;
3137         ce->ce_entry = entry_dup( e );
3138         ce->ce_entry->e_private = ce;
3139         ce->ce_type = colst[0]->co_type;
3140         if ( !last ) {
3141                 cfb->cb_root = ce;
3142         } else if ( last->ce_kids ) {
3143                 CfEntryInfo *c2;
3144
3145                 for (c2=last->ce_kids; c2 && c2->ce_sibs; c2 = c2->ce_sibs);
3146                 c2->ce_sibs = ce;
3147         } else {
3148                 last->ce_kids = ce;
3149         }
3150
3151 leave:
3152         ch_free( ca.argv );
3153         if ( colst ) ch_free( colst );
3154         return rc;
3155 }
3156
3157 /* Parse an LDAP entry into config directives, then store in underlying
3158  * database.
3159  */
3160 static int
3161 config_back_add( Operation *op, SlapReply *rs )
3162 {
3163         CfBackInfo *cfb;
3164         CfEntryInfo *ce, *last;
3165
3166         if ( !be_isroot( op ) ) {
3167                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3168                 send_ldap_result( op, rs );
3169         }
3170
3171         cfb = (CfBackInfo *)op->o_bd->be_private;
3172
3173         ldap_pvt_thread_pool_pause( &connection_pool );
3174
3175         /* Strategy:
3176          * 1) check for existence of entry
3177          * 2) perform internal add
3178          * 3) store entry in underlying database
3179          */
3180
3181         ldap_pvt_thread_pool_resume( &connection_pool );
3182
3183 out:
3184         send_ldap_result( op, rs );
3185         return rs->sr_err;
3186 }
3187
3188 static int
3189 config_back_modify( Operation *op, SlapReply *rs )
3190 {
3191         CfBackInfo *cfb;
3192         CfEntryInfo *ce, *last;
3193
3194         if ( !be_isroot( op ) ) {
3195                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3196                 send_ldap_result( op, rs );
3197         }
3198
3199         cfb = (CfBackInfo *)op->o_bd->be_private;
3200
3201         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
3202         if ( !ce ) {
3203                 if ( last )
3204                         rs->sr_matched = last->ce_entry->e_name.bv_val;
3205                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
3206                 goto out;
3207         }
3208         ldap_pvt_thread_pool_pause( &connection_pool );
3209
3210         /* Strategy:
3211          * 1) perform the Modify on the cached Entry.
3212          * 2) verify that the Entry still satisfies the schema.
3213          * 3) perform the individual config operations.
3214          * 4) store Modified entry in underlying LDIF backend.
3215          */
3216         ldap_pvt_thread_pool_resume( &connection_pool );
3217 out:
3218         send_ldap_result( op, rs );
3219         return rs->sr_err;
3220 }
3221
3222 static int
3223 config_back_search( Operation *op, SlapReply *rs )
3224 {
3225         CfBackInfo *cfb;
3226         CfEntryInfo *ce, *last;
3227         int rc;
3228
3229         if ( !be_isroot( op ) ) {
3230                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3231                 send_ldap_result( op, rs );
3232         }
3233
3234         cfb = (CfBackInfo *)op->o_bd->be_private;
3235
3236         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
3237         if ( !ce ) {
3238                 if ( last )
3239                         rs->sr_matched = last->ce_entry->e_name.bv_val;
3240                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
3241                 goto out;
3242         }
3243         switch ( op->ors_scope ) {
3244         case LDAP_SCOPE_BASE:
3245         case LDAP_SCOPE_SUBTREE:
3246                 config_send( op, rs, ce, 0 );
3247                 break;
3248                 
3249         case LDAP_SCOPE_ONELEVEL:
3250                 for (ce = ce->ce_kids; ce; ce=ce->ce_sibs) {
3251                         config_send( op, rs, ce, 1 );
3252                 }
3253                 break;
3254         }
3255                 
3256         rs->sr_err = LDAP_SUCCESS;
3257 out:
3258         send_ldap_result( op, rs );
3259         return 0;
3260 }
3261
3262 static Entry *
3263 config_alloc_entry( CfEntryInfo *parent, struct berval *rdn )
3264 {
3265         Entry *e = ch_calloc( 1, sizeof(Entry) );
3266         CfEntryInfo *ce = ch_calloc( 1, sizeof(CfEntryInfo) );
3267         struct berval pdn;
3268
3269         e->e_private = ce;
3270         ce->ce_entry = e;
3271         ce->ce_parent = parent;
3272         if ( parent ) {
3273                 pdn = parent->ce_entry->e_nname;
3274         } else {
3275                 BER_BVZERO( &pdn );
3276         }
3277
3278         build_new_dn( &e->e_name, &pdn, rdn, NULL );
3279         ber_dupbv( &e->e_nname, &e->e_name );
3280         return e;
3281 }
3282
3283 #define NO_TABLE        0
3284 #define BI_TABLE        1
3285 #define BE_TABLE        2
3286
3287 static int
3288 config_build_entry( ConfigArgs *c, Entry *e, ObjectClass *oc,
3289          struct berval *rdn, ConfigTable *ct, int table )
3290 {
3291         struct berval vals[2];
3292         struct berval ad_name;
3293         AttributeDescription *ad = NULL;
3294         int rc, i;
3295         char *ptr;
3296         const char *text;
3297         char textbuf[SLAP_TEXT_BUFLEN];
3298         size_t textlen = sizeof(textbuf);
3299         AttributeType **at;
3300         Attribute *oc_at;
3301
3302         BER_BVZERO( &vals[1] );
3303
3304         vals[0] = oc->soc_cname;
3305         attr_merge(e, slap_schema.si_ad_objectClass, vals, NULL );
3306         ptr = strchr(rdn->bv_val, '=');
3307         ad_name.bv_val = rdn->bv_val;
3308         ad_name.bv_len = ptr - rdn->bv_val;
3309         rc = slap_bv2ad( &ad_name, &ad, &text );
3310         if ( rc ) {
3311                 return rc;
3312         }
3313         vals[0].bv_val = ptr+1;
3314         vals[0].bv_len = rdn->bv_len - (vals[0].bv_val - rdn->bv_val);
3315         attr_merge(e, ad, vals, NULL );
3316
3317         for (at=oc->soc_required; at && *at; at++) {
3318                 /* Skip the naming attr */
3319                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
3320                         continue;
3321                 for (i=0;ct[i].name;i++) {
3322                         if (ct[i].ad == (*at)->sat_ad) {
3323                                 rc = config_get_vals(&ct[i], c);
3324                                 if (rc == LDAP_SUCCESS) {
3325                                         attr_merge(e, ct[i].ad, c->rvalue_vals, c->rvalue_nvals);
3326                                         ber_bvarray_free( c->rvalue_nvals );
3327                                         ber_bvarray_free( c->rvalue_vals );
3328                                 }
3329                                 break;
3330                         }
3331                 }
3332         }
3333
3334         for (at=oc->soc_allowed; at && *at; at++) {
3335                 /* Skip the naming attr */
3336                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
3337                         continue;
3338                 for (i=0;ct[i].name;i++) {
3339                         if (ct[i].ad == (*at)->sat_ad) {
3340                                 rc = config_get_vals(&ct[i], c);
3341                                 if (rc == LDAP_SUCCESS) {
3342                                         attr_merge(e, ct[i].ad, c->rvalue_vals, c->rvalue_nvals);
3343                                         ber_bvarray_free( c->rvalue_nvals );
3344                                         ber_bvarray_free( c->rvalue_vals );
3345                                 }
3346                                 break;
3347                         }
3348                 }
3349         }
3350
3351         if ( table ) {
3352                 if ( table == BI_TABLE )
3353                         ct = c->bi->bi_cf_table;
3354                 else
3355                         ct = c->be->be_cf_table;
3356                 for (;ct && ct->name;ct++) {
3357                         if (!ct->ad) continue;
3358                         rc = config_get_vals(ct, c);
3359                         if (rc == LDAP_SUCCESS) {
3360                                 attr_merge(e, ct->ad, c->rvalue_vals, c->rvalue_nvals);
3361                         }
3362                 }
3363         }
3364         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
3365         rc = structural_class(oc_at->a_vals, vals, NULL, &text, textbuf, textlen);
3366         BER_BVZERO( &vals[1] );
3367         attr_merge(e, slap_schema.si_ad_structuralObjectClass, vals, NULL );
3368
3369         return 0;
3370 }
3371
3372 static CfEntryInfo *
3373 config_build_includes( ConfigArgs *c, CfEntryInfo *ceparent,
3374         Operation *op, SlapReply *rs )
3375 {
3376         Entry *e;
3377         int i;
3378         ConfigFile *cf = c->private;
3379         CfEntryInfo *ce, *ceprev;
3380
3381         if ( ceparent->ce_kids ) {
3382                 for ( ceprev = ceparent->ce_kids; ceprev->ce_sibs;
3383                         ceprev = ceprev->ce_sibs );
3384         }
3385
3386         for (i=0; cf; cf=cf->c_sibs, i++) {
3387                 c->value_dn.bv_val = c->log;
3388                 c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn=include{%02d}", i);
3389                 e = config_alloc_entry( ceparent, &c->value_dn );
3390                 c->private = cf;
3391                 config_build_entry( c, e, cfOc_include, &c->value_dn,
3392                         c->bi->bi_cf_table, NO_TABLE );
3393                 op->ora_e = e;
3394                 op->o_bd->be_add( op, rs );
3395                 ce = e->e_private;
3396                 ce->ce_type = Cft_Include;
3397                 ce->ce_bi = c->bi;
3398                 if ( !ceparent->ce_kids ) {
3399                         ceparent->ce_kids = ce;
3400                 } else {
3401                         ceprev->ce_sibs = ce;
3402                 }
3403                 ceprev = ce;
3404                 if ( cf->c_kids ) {
3405                         c->private = cf->c_kids;
3406                         config_build_includes( c, ce, op, rs );
3407                 }
3408         }
3409         return ce;
3410 }
3411
3412 #ifdef SLAPD_MODULES
3413
3414 static CfEntryInfo *
3415 config_build_modules( ConfigArgs *c, CfEntryInfo *ceparent,
3416         Operation *op, SlapReply *rs )
3417 {
3418         Entry *e;
3419         int i;
3420         CfEntryInfo *ce, *ceprev;
3421         ModPaths *mp;
3422
3423         if ( ceparent->ce_kids ) {
3424                 for ( ceprev = ceparent->ce_kids; ceprev->ce_sibs;
3425                         ceprev = ceprev->ce_sibs );
3426         }
3427
3428         for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
3429                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
3430                         continue;
3431                 c->value_dn.bv_val = c->log;
3432                 c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn=module{%02d}", i);
3433                 e = config_alloc_entry( ceparent, &c->value_dn );
3434                 ce = e->e_private;
3435                 ce->ce_type = Cft_Include;
3436                 c->private = mp;
3437                 config_build_entry( c, e, cfOc_module, &c->value_dn,
3438                         c->bi->bi_cf_table, NO_TABLE );
3439                 op->ora_e = e;
3440                 op->o_bd->be_add( op, rs );
3441                 ce->ce_bi = c->bi;
3442                 if ( !ceparent->ce_kids ) {
3443                         ceparent->ce_kids = ce;
3444                 } else {
3445                         ceprev->ce_sibs = ce;
3446                 }
3447                 ceprev = ce;
3448         }
3449         return ce;
3450 }
3451 #endif
3452
3453 static int
3454 config_back_db_open( BackendDB *be )
3455 {
3456         CfBackInfo *cfb = be->be_private;
3457         struct berval rdn;
3458         Entry *e, *parent;
3459         CfEntryInfo *ce, *ceparent, *ceprev;
3460         int i, rc;
3461         BackendInfo *bi;
3462         BackendDB *bptr;
3463         ConfigArgs c;
3464         ConfigTable *ct;
3465         Connection conn = {0};
3466         char opbuf[OPERATION_BUFFER_SIZE];
3467         Operation *op;
3468         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
3469         SlapReply rs = {REP_RESULT};
3470
3471         /* If we read the config from back-ldif, nothing to do here */
3472         if ( cfb->cb_got_ldif )
3473                 return 0;
3474
3475         op = (Operation *)opbuf;
3476         connection_fake_init( &conn, op, cfb );
3477
3478         op->o_dn = be->be_rootdn;
3479         op->o_ndn = be->be_rootndn;
3480
3481         op->o_tag = LDAP_REQ_ADD;
3482         op->o_callback = &cb;
3483         op->o_bd = &cfb->cb_db;
3484
3485         /* create root of tree */
3486         rdn = config_rdn;
3487         e = config_alloc_entry( NULL, &rdn );
3488         ce = e->e_private;
3489         ce->ce_type = Cft_Global;
3490         cfb->cb_root = ce;
3491         c.be = be;
3492         c.bi = be->bd_info;
3493         c.private = cfb->cb_config;
3494         ct = c.bi->bi_cf_table;
3495         config_build_entry( &c, e, cfOc_global, &rdn, ct, NO_TABLE );
3496         op->ora_e = e;
3497         op->o_bd->be_add( op, &rs );
3498         ce->ce_bi = c.bi;
3499
3500         parent = e;
3501         ceparent = ce;
3502
3503         /* Create schema nodes... cn=schema will contain the hardcoded core
3504          * schema, read-only. Child objects will contain runtime loaded schema
3505          * files.  FIXME
3506          */
3507         rdn = schema_rdn;
3508         e = config_alloc_entry( ceparent, &rdn );
3509         ce = e->e_private;
3510         ce->ce_type = Cft_Schema;
3511         c.private = NULL;
3512         config_build_entry( &c, e, cfOc_schema, &rdn, ct, NO_TABLE );
3513         op->ora_e = e;
3514         op->o_bd->be_add( op, &rs );
3515         if ( !ceparent->ce_kids ) {
3516                 ceparent->ce_kids = ce;
3517         } else {
3518                 ceprev->ce_sibs = ce;
3519         }
3520         ceprev = ce;
3521
3522         /* Create includeFile nodes... */
3523         if ( cfb->cb_config->c_kids ) {
3524                 c.private = cfb->cb_config->c_kids;
3525                 ceprev = config_build_includes( &c, ceparent, op, &rs );
3526         }
3527
3528 #ifdef SLAPD_MODULES
3529         /* Create Module nodes... */
3530         if ( modpaths.mp_loads ) {
3531                 ceprev = config_build_includes( &c, ceparent, op, &rs );
3532         }
3533 #endif
3534
3535         /* Create backend nodes. Skip if they don't provide a cf_table.
3536          * There usually aren't any of these.
3537          */
3538         
3539         c.line = 0;
3540         bi = backendInfo;
3541         for (i=0; i<nBackendInfo; i++, bi++) {
3542                 if (!bi->bi_cf_table) continue;
3543                 if (!bi->bi_private) continue;
3544
3545                 rdn.bv_val = c.log;
3546                 rdn.bv_len = sprintf(rdn.bv_val, "%s=%s", cfAd_backend->ad_cname.bv_val, bi->bi_type);
3547                 e = config_alloc_entry( ceparent, &rdn );
3548                 ce = e->e_private;
3549                 ce->ce_type = Cft_Backend;
3550                 ce->ce_bi = bi;
3551                 c.bi = bi;
3552                 config_build_entry( &c, e, cfOc_backend, &rdn, ct, BI_TABLE );
3553                 op->ora_e = e;
3554                 op->o_bd->be_add( op, &rs );
3555                 if ( !ceparent->ce_kids ) {
3556                         ceparent->ce_kids = ce;
3557                 } else {
3558                         ceprev->ce_sibs = ce;
3559                 }
3560                 ceprev = ce;
3561         }
3562
3563         /* Create database nodes... */
3564         for (i=0; i<nBackendDB; i++) {
3565                 slap_overinfo *oi = NULL;
3566                 if ( i == 0 ) {
3567                         bptr = frontendDB;
3568                 } else {
3569                         bptr = &backendDB[i];
3570                 }
3571                 if ( overlay_is_over( bptr )) {
3572                         oi = bptr->bd_info->bi_private;
3573                         bi = oi->oi_orig;
3574                 } else {
3575                         bi = bptr->bd_info;
3576                 }
3577                 rdn.bv_val = c.log;
3578                 rdn.bv_len = sprintf(rdn.bv_val, "%s={%0x}%s", cfAd_database->ad_cname.bv_val,
3579                         i, bi->bi_type);
3580                 e = config_alloc_entry( ceparent, &rdn );
3581                 ce = e->e_private;
3582                 c.be = bptr;
3583                 c.bi = bi;
3584                 ce->ce_type = Cft_Database;
3585                 ce->ce_be = c.be;
3586                 ce->ce_bi = c.bi;
3587                 config_build_entry( &c, e, cfOc_database, &rdn, ct, BE_TABLE );
3588                 op->ora_e = e;
3589                 op->o_bd->be_add( op, &rs );
3590                 if ( !ceparent->ce_kids ) {
3591                         ceparent->ce_kids = ce;
3592                 } else {
3593                         ceprev->ce_sibs = ce;
3594                 }
3595                 ceprev = ce;
3596                 /* Iterate through overlays */
3597                 if ( oi ) {
3598                         slap_overinst *on;
3599                         Entry *oe;
3600                         CfEntryInfo *opar = ce, *oprev = NULL;
3601                         int j;
3602
3603                         for (j=0,on=oi->oi_list; on; j++,on=on->on_next) {
3604                                 rdn.bv_val = c.log;
3605                                 rdn.bv_len = sprintf(rdn.bv_val, "%s={%0x}%s",
3606                                         cfAd_overlay->ad_cname.bv_val, j, on->on_bi.bi_type );
3607                                 oe = config_alloc_entry( opar, &rdn );
3608                                 ce = oe->e_private;
3609                                 c.be = bptr;
3610                                 c.bi = &on->on_bi;
3611                                 ce->ce_type = Cft_Overlay;
3612                                 ce->ce_be = c.be;
3613                                 ce->ce_bi = c.bi;
3614                                 config_build_entry( &c, oe, cfOc_overlay, &rdn, ct, BI_TABLE );
3615                                 op->ora_e = oe;
3616                                 op->o_bd->be_add( op, &rs );
3617                                 if ( !opar->ce_kids ) {
3618                                         opar->ce_kids = ce;
3619                                 } else {
3620                                         oprev->ce_sibs = ce;
3621                                 }
3622                                 oprev = ce;
3623                         }
3624                 }
3625         }
3626
3627         return 0;
3628 }
3629
3630 static int
3631 config_back_db_destroy( Backend *be )
3632 {
3633         free( be->be_private );
3634         return 0;
3635 }
3636
3637 static int
3638 config_back_db_init( Backend *be )
3639 {
3640         struct berval dn;
3641         CfBackInfo *cfb;
3642
3643         cfb = ch_calloc( 1, sizeof(CfBackInfo));
3644         cfb->cb_config = &cf_prv;
3645         cfb->cb_be = be;
3646         be->be_private = cfb;
3647
3648         ber_dupbv( &be->be_rootdn, &config_rdn );
3649         ber_dupbv( &be->be_rootndn, &be->be_rootdn );
3650         ber_dupbv( &dn, &be->be_rootdn );
3651         ber_bvarray_add( &be->be_suffix, &dn );
3652         ber_dupbv( &dn, &be->be_rootdn );
3653         ber_bvarray_add( &be->be_nsuffix, &dn );
3654
3655         /* Hide from namingContexts */
3656         SLAP_BFLAGS(be) |= SLAP_BFLAG_CONFIG;
3657
3658         return 0;
3659 }
3660
3661 static struct {
3662         char *name;
3663         AttributeDescription **desc;
3664 } ads[] = {
3665         { "backend", &cfAd_backend },
3666         { "database", &cfAd_database },
3667         { "include", &cfAd_include },
3668         { "overlay", &cfAd_overlay },
3669         { NULL, NULL, NULL }
3670 };
3671
3672 /* Notes:
3673  *   add / delete: all types that may be added or deleted must use an
3674  * X-ORDERED attributeType for their RDN. Adding and deleting entries
3675  * should automatically renumber the index of any siblings as needed,
3676  * so that no gaps in the numbering sequence exist after the add/delete
3677  * is completed.
3678  *   What can be added:
3679  *     schema objects
3680  *     backend objects for backend-specific config directives
3681  *     database objects
3682  *     overlay objects
3683  *
3684  *   delete: probably no support this time around.
3685  *
3686  *   modrdn: generally not done. Will be invoked automatically by add/
3687  * delete to update numbering sequence. Perform as an explicit operation
3688  * so that the renumbering effect may be replicated. Subtree rename must
3689  * be supported, since renumbering a database will affect all its child
3690  * overlays.
3691  *
3692  *  modify: must be fully supported. 
3693  */
3694
3695 int
3696 config_back_initialize( BackendInfo *bi )
3697 {
3698         ConfigTable *ct = config_back_cf_table;
3699         char *argv[4];
3700         int i;
3701
3702         bi->bi_open = 0;
3703         bi->bi_close = 0;
3704         bi->bi_config = 0;
3705         bi->bi_destroy = 0;
3706
3707         bi->bi_db_init = config_back_db_init;
3708         bi->bi_db_config = 0;
3709         bi->bi_db_open = config_back_db_open;
3710         bi->bi_db_close = 0;
3711         bi->bi_db_destroy = config_back_db_destroy;
3712
3713         bi->bi_op_bind = config_back_bind;
3714         bi->bi_op_unbind = 0;
3715         bi->bi_op_search = config_back_search;
3716         bi->bi_op_compare = 0;
3717         bi->bi_op_modify = config_back_modify;
3718         bi->bi_op_modrdn = 0;
3719         bi->bi_op_add = 0;
3720         bi->bi_op_delete = 0;
3721         bi->bi_op_abandon = 0;
3722
3723         bi->bi_extended = 0;
3724
3725         bi->bi_chk_referrals = 0;
3726
3727         bi->bi_connection_init = 0;
3728         bi->bi_connection_destroy = 0;
3729
3730         argv[3] = NULL;
3731         for (i=0; OidMacros[i].name; i++ ) {
3732                 argv[1] = OidMacros[i].name;
3733                 argv[2] = OidMacros[i].oid;
3734                 parse_oidm( "slapd", i, 3, argv, 0, NULL );
3735         }
3736
3737         bi->bi_cf_table = ct;
3738
3739         i = config_register_schema( ct, cf_ocs );
3740         if ( i ) return i;
3741
3742         /* set up the notable AttributeDescriptions */
3743         i = 0;
3744         for (;ct->name;ct++) {
3745                 if (strcmp(ct->name, ads[i].name)) continue;
3746                 *ads[i].desc = ct->ad;
3747                 i++;
3748                 if (!ads[i].name) break;
3749         }
3750
3751         return 0;
3752 }
3753