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