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