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