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