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