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